This repository has been archived by the owner on Jul 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: stream to multiaddr connection converter
- Loading branch information
1 parent
8364aa8
commit e02d15b
Showing
3 changed files
with
94 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,13 +29,16 @@ | |
}, | ||
"homepage": "https://github.com/libp2p/js-libp2p-utils#readme", | ||
"devDependencies": { | ||
"aegir": "^20.3.1", | ||
"aegir": "^20.6.0", | ||
"chai": "^4.2.0", | ||
"dirty-chai": "^2.0.1" | ||
"dirty-chai": "^2.0.1", | ||
"it-pair": "^1.0.0", | ||
"multiaddr": "^7.3.0" | ||
}, | ||
"dependencies": { | ||
"ip-address": "^6.1.0", | ||
"multiaddr": "^7.1.0" | ||
"abortable-iterator": "^3.0.0", | ||
"debug": "^4.1.1", | ||
"ip-address": "^6.1.0" | ||
}, | ||
"contributors": [ | ||
"Vasco Santos <[email protected]>" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
'use strict' | ||
|
||
const abortable = require('abortable-iterator') | ||
const log = require('debug')('libp2p:stream:converter') | ||
|
||
// Convert a duplex iterable into a MultiaddrConnection | ||
// https://github.com/libp2p/interface-transport#multiaddrconnection | ||
module.exports = ({ stream, remoteAddr, localAddr }, options = {}) => { | ||
const { sink, source } = stream | ||
const maConn = { | ||
async sink (source) { | ||
if (options.signal) { | ||
source = abortable(source, options.signal) | ||
} | ||
|
||
try { | ||
await sink(source) | ||
} catch (err) { | ||
// If aborted we can safely ignore | ||
if (err.type !== 'aborted') { | ||
// If the source errored the socket will already have been destroyed by | ||
// toIterable.duplex(). If the socket errored it will already be | ||
// destroyed. There's nothing to do here except log the error & return. | ||
log(err) | ||
} | ||
} | ||
close() | ||
}, | ||
|
||
source: options.signal ? abortable(source, options.signal) : source, | ||
conn: stream, | ||
localAddr, | ||
remoteAddr, | ||
timeline: { open: Date.now() }, | ||
|
||
close () { | ||
sink([]) | ||
close() | ||
} | ||
} | ||
|
||
function close () { | ||
if (!maConn.timeline.close) { | ||
maConn.timeline.close = Date.now() | ||
} | ||
} | ||
|
||
return maConn | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* eslint-env mocha */ | ||
'use strict' | ||
|
||
const chai = require('chai') | ||
const dirtyChai = require('dirty-chai') | ||
const expect = chai.expect | ||
chai.use(dirtyChai) | ||
|
||
const pair = require('it-pair') | ||
const multiaddr = require('multiaddr') | ||
|
||
const streamToMaConn = require('../src/stream-to-ma-conn') | ||
|
||
describe('Convert stream into a multiaddr connection', () => { | ||
it('converts a stream and adds the provided metadata', () => { | ||
const stream = pair() | ||
const localAddr = multiaddr('/ip4/101.45.75.219/tcp/6000') | ||
const remoteAddr = multiaddr('/ip4/100.46.74.201/tcp/6002') | ||
|
||
const maConn = streamToMaConn({ | ||
stream, | ||
localAddr, | ||
remoteAddr | ||
}) | ||
|
||
expect(maConn).to.exist() | ||
expect(maConn.sink).to.exist() | ||
expect(maConn.source).to.exist() | ||
expect(maConn.localAddr).to.eql(localAddr) | ||
expect(maConn.remoteAddr).to.eql(remoteAddr) | ||
expect(maConn.timeline).to.exist() | ||
expect(maConn.timeline.open).to.exist() | ||
expect(maConn.timeline.close).to.not.exist() | ||
|
||
maConn.close() | ||
expect(maConn.timeline.close).to.exist() | ||
}) | ||
}) |