This repository was archived by the owner on Jul 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 46
refactor: async with multiaddr conn #92
Merged
Merged
Changes from 17 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
26d6892
refactor: wip switch to it-ws and async iterators
1808666
feat: adaper with passing tests
7713c6f
refactor: async dial that resolves on connection open
083a806
fix: listener params and adapter tests
71965fa
test: add interface tests
0a97cc8
fix: tests
b600f68
feat: abortable dials
01a8217
chore: update interface-transport dependency
e2af4a6
fix: describe name
3e50f5f
fix: tests
a0b039d
refactor: use adapter class in interface-transport
583f8e6
fix: remove async from non-async function
aa8eeb8
chore: cleanup
eb071fb
chore: remove old interface-transport tests
cc53e7a
refactor: remove test covered by interface-transport tests
20358d3
refactor: use abort error from interface-transport
c97a0f3
refactory: async with multiaddr conn
vasco-santos f610735
chore: address review
vasco-santos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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 |
---|---|---|
@@ -1,43 +1,4 @@ | ||
docs | ||
node_modules | ||
package-lock.json | ||
yarn.lock | ||
|
||
# Logs | ||
logs | ||
*.log | ||
npm-debug.log* | ||
|
||
# Runtime data | ||
pids | ||
*.pid | ||
*.seed | ||
|
||
# Directory for instrumented libs generated by jscoverage/JSCover | ||
lib-cov | ||
|
||
# Coverage directory used by tools like istanbul | ||
coverage | ||
|
||
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) | ||
.grunt | ||
|
||
# node-waf configuration | ||
.lock-wscript | ||
|
||
# Compiled binary addons (http://nodejs.org/api/addons.html) | ||
build/Release | ||
|
||
# Dependency directory | ||
node_modules | ||
|
||
# Optional npm cache directory | ||
.npm | ||
|
||
# Optional REPL history | ||
.node_repl_history | ||
|
||
# Vim editor swap files | ||
*.swp | ||
|
||
dist | ||
|
||
.nyc_output |
This file contains hidden or 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
This file contains hidden or 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
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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 |
---|---|---|
|
@@ -13,8 +13,7 @@ | |
"release": "aegir release -t node -t browser ", | ||
"release-minor": "aegir release --type minor -t node -t browser", | ||
"release-major": "aegir release --type major -t node -t browser", | ||
"coverage": "aegir coverage", | ||
"coverage-publish": "aegir coverage --provider coveralls" | ||
"coverage": "nyc --reporter=lcov --reporter=text npm run test:node" | ||
}, | ||
"browser": { | ||
"src/listener": "./src/listener.browser.js" | ||
|
@@ -24,8 +23,7 @@ | |
"dist" | ||
], | ||
"pre-push": [ | ||
"lint", | ||
"test" | ||
"lint" | ||
], | ||
"repository": { | ||
"type": "git", | ||
|
@@ -40,21 +38,25 @@ | |
}, | ||
"homepage": "https://github.com/libp2p/js-libp2p-websockets#readme", | ||
"dependencies": { | ||
"abortable-iterator": "^2.1.0", | ||
"class-is": "^1.1.0", | ||
"debug": "^4.1.1", | ||
"interface-connection": "~0.3.3", | ||
"mafmt": "^6.0.7", | ||
"multiaddr-to-uri": "^5.0.0", | ||
"pull-ws": "hugomrdias/pull-ws#fix/bundle-size" | ||
"err-code": "^2.0.0", | ||
"it-ws": "vasco-santos/it-ws#feat/add-properties-and-functions-to-client-and-server", | ||
"libp2p-utils": "~0.1.0", | ||
"mafmt": "^7.0.0", | ||
"multiaddr": "^7.1.0", | ||
"multiaddr-to-uri": "^5.0.0" | ||
}, | ||
"devDependencies": { | ||
"aegir": "^20.0.0", | ||
"abort-controller": "^3.0.0", | ||
"aegir": "^20.3.1", | ||
"chai": "^4.2.0", | ||
"dirty-chai": "^2.0.1", | ||
"interface-transport": "~0.3.7", | ||
"multiaddr": "^6.0.6", | ||
"pull-goodbye": "0.0.2", | ||
"pull-stream": "^3.6.9" | ||
"interface-transport": "^0.7.0", | ||
"it-goodbye": "^2.0.1", | ||
"it-pipe": "^1.0.1", | ||
"streaming-iterables": "^4.1.0" | ||
}, | ||
"contributors": [ | ||
"Chris Campbell <[email protected]>", | ||
|
This file contains hidden or 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,8 @@ | ||
'use strict' | ||
|
||
// p2p multi-address code | ||
exports.CODE_P2P = 421 | ||
exports.CODE_CIRCUIT = 290 | ||
|
||
// Time to wait for a connection to close gracefully before destroying it manually | ||
exports.CLOSE_TIMEOUT = 2000 |
This file contains hidden or 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 |
---|---|---|
@@ -1,68 +1,136 @@ | ||
'use strict' | ||
|
||
const connect = require('pull-ws/client') | ||
const connect = require('it-ws/client') | ||
const mafmt = require('mafmt') | ||
const withIs = require('class-is') | ||
const Connection = require('interface-connection').Connection | ||
|
||
const toUri = require('multiaddr-to-uri') | ||
const debug = require('debug') | ||
const log = debug('libp2p:websockets:dialer') | ||
const { AbortError } = require('abortable-iterator') | ||
|
||
const log = require('debug')('libp2p:websockets') | ||
const assert = require('assert') | ||
|
||
const createListener = require('./listener') | ||
const toConnection = require('./socket-to-conn') | ||
const { CODE_CIRCUIT, CODE_P2P } = require('./constants') | ||
|
||
/** | ||
* @class WebSockets | ||
*/ | ||
class WebSockets { | ||
dial (ma, options, callback) { | ||
if (typeof options === 'function') { | ||
callback = options | ||
options = {} | ||
/** | ||
* @constructor | ||
* @param {object} options | ||
* @param {Upgrader} options.upgrader | ||
*/ | ||
constructor ({ upgrader }) { | ||
assert(upgrader, 'An upgrader must be provided. See https://github.com/libp2p/interface-transport#upgrader.') | ||
this._upgrader = upgrader | ||
} | ||
|
||
/** | ||
* @async | ||
* @param {Multiaddr} ma | ||
* @param {object} [options] | ||
* @param {AbortSignal} [options.signal] Used to abort dial requests | ||
* @returns {Connection} An upgraded Connection | ||
*/ | ||
async dial (ma, options = {}) { | ||
log('dialing %s', ma) | ||
|
||
const stream = await this._connect(ma, options) | ||
vasco-santos marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
const maConn = toConnection(stream, { socket: stream.socket, remoteAddr: ma, signal: options.signal }) | ||
vasco-santos marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
log('new outbound connection %s', maConn.remoteAddr) | ||
|
||
const conn = await this._upgrader.upgradeOutbound(maConn) | ||
log('outbound connection %s upgraded', maConn.remoteAddr) | ||
return conn | ||
} | ||
|
||
/** | ||
* @private | ||
* @param {Multiaddr} ma | ||
* @param {object} [options] | ||
* @param {AbortSignal} [options.signal] Used to abort dial requests | ||
* @returns {Promise<Socket>} Resolves a TCP Socket | ||
vasco-santos marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
*/ | ||
async _connect (ma, options = {}) { | ||
if (options.signal && options.signal.aborted) { | ||
throw new AbortError() | ||
} | ||
const cOpts = ma.toOptions() | ||
log('dialing %s:%s', cOpts.host, cOpts.port) | ||
|
||
const rawSocket = connect(toUri(ma), Object.assign({ binary: true }, options)) | ||
|
||
callback = callback || function () { } | ||
if (!options.signal) { | ||
await rawSocket.connected() | ||
|
||
const url = toUri(ma) | ||
log('dialing %s', url) | ||
const socket = connect(url, { | ||
binary: true, | ||
onConnect: (err) => { | ||
callback(err) | ||
log('connected %s', ma) | ||
return rawSocket | ||
} | ||
|
||
// Allow abort via signal during connect | ||
let onAbort | ||
const abort = new Promise((resolve, reject) => { | ||
onAbort = () => { | ||
reject(new AbortError()) | ||
rawSocket.close() | ||
} | ||
|
||
// Already aborted? | ||
if (options.signal.aborted) return onAbort() | ||
options.signal.addEventListener('abort', onAbort) | ||
}) | ||
|
||
const conn = new Connection(socket) | ||
conn.getObservedAddrs = (cb) => cb(null, [ma]) | ||
conn.close = (cb) => socket.close(cb) | ||
try { | ||
await Promise.race([abort, rawSocket.connected()]) | ||
} finally { | ||
options.signal.removeEventListener('abort', onAbort) | ||
} | ||
|
||
return conn | ||
log('connected %s', ma) | ||
return rawSocket | ||
} | ||
|
||
/** | ||
* Creates a Websockets listener. The provided `handler` function will be called | ||
* anytime a new incoming Connection has been successfully upgraded via | ||
* `upgrader.upgradeInbound`. | ||
* @param {object} [options] | ||
* @param {http.Server} [options.server] A pre-created Node.js HTTP/S server. | ||
* @param {function (Connection)} handler | ||
* @returns {Listener} A Websockets listener | ||
*/ | ||
createListener (options, handler) { | ||
if (typeof options === 'function') { | ||
handler = options | ||
options = {} | ||
} | ||
options = options || {} | ||
vasco-santos marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
return createListener(options, handler) | ||
return createListener({ handler, upgrader: this._upgrader }, options) | ||
} | ||
|
||
/** | ||
* Takes a list of `Multiaddr`s and returns only valid Websockets addresses | ||
* @param {Multiaddr[]} multiaddrs | ||
* @returns {Multiaddr[]} Valid Websockets multiaddrs | ||
*/ | ||
filter (multiaddrs) { | ||
if (!Array.isArray(multiaddrs)) { | ||
multiaddrs = [multiaddrs] | ||
} | ||
multiaddrs = Array.isArray(multiaddrs) ? multiaddrs : [multiaddrs] | ||
|
||
return multiaddrs.filter((ma) => { | ||
if (ma.protoNames().includes('p2p-circuit')) { | ||
if (ma.protoNames().includes(CODE_CIRCUIT)) { | ||
return false | ||
} | ||
|
||
if (ma.protoNames().includes('ipfs')) { | ||
ma = ma.decapsulate('ipfs') | ||
} | ||
|
||
return mafmt.WebSockets.matches(ma) || | ||
mafmt.WebSocketsSecure.matches(ma) | ||
return mafmt.WebSockets.matches(ma.decapsulateCode(CODE_P2P)) || | ||
mafmt.WebSocketsSecure.matches(ma.decapsulateCode(CODE_P2P)) | ||
}) | ||
} | ||
} | ||
|
||
module.exports = withIs(WebSockets, { className: 'WebSockets', symbolName: '@libp2p/js-libp2p-websockets/websockets' }) | ||
module.exports = withIs(WebSockets, { | ||
className: 'WebSockets', | ||
symbolName: '@libp2p/js-libp2p-websockets/websockets' | ||
}) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.