This repository was archived by the owner on Jun 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
chore: add duplex iterable type to connection #71
Closed
Closed
Changes from all commits
Commits
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,56 +1,23 @@ | ||
'use strict' | ||
|
||
const PeerId = require('peer-id') | ||
const multiaddr = require('multiaddr') | ||
const withIs = require('class-is') | ||
const errCode = require('err-code') | ||
const Status = require('./status') | ||
|
||
function validateArgs (localAddr, localPeer, remotePeer, newStream, close, getStreams, stat) { | ||
if (localAddr && !multiaddr.isMultiaddr(localAddr)) { | ||
throw errCode(new Error('localAddr must be an instance of multiaddr'), 'ERR_INVALID_PARAMETERS') | ||
} | ||
|
||
if (!PeerId.isPeerId(localPeer)) { | ||
throw errCode(new Error('localPeer must be an instance of peer-id'), 'ERR_INVALID_PARAMETERS') | ||
} | ||
|
||
if (!PeerId.isPeerId(remotePeer)) { | ||
throw errCode(new Error('remotePeer must be an instance of peer-id'), 'ERR_INVALID_PARAMETERS') | ||
} | ||
|
||
if (typeof newStream !== 'function') { | ||
throw errCode(new Error('new stream must be a function'), 'ERR_INVALID_PARAMETERS') | ||
} | ||
|
||
if (typeof close !== 'function') { | ||
throw errCode(new Error('close must be a function'), 'ERR_INVALID_PARAMETERS') | ||
} | ||
const { validateArgs } = require('./utils') | ||
|
||
if (typeof getStreams !== 'function') { | ||
throw errCode(new Error('getStreams must be a function'), 'ERR_INVALID_PARAMETERS') | ||
} | ||
|
||
if (!stat) { | ||
throw errCode(new Error('connection metadata object must be provided'), 'ERR_INVALID_PARAMETERS') | ||
} | ||
|
||
if (stat.direction !== 'inbound' && stat.direction !== 'outbound') { | ||
throw errCode(new Error('direction must be "inbound" or "outbound"'), 'ERR_INVALID_PARAMETERS') | ||
} | ||
|
||
if (!stat.timeline) { | ||
throw errCode(new Error('connection timeline object must be provided in the stat object'), 'ERR_INVALID_PARAMETERS') | ||
} | ||
|
||
if (!stat.timeline.open) { | ||
throw errCode(new Error('connection open timestamp must be provided'), 'ERR_INVALID_PARAMETERS') | ||
} | ||
|
||
if (!stat.timeline.upgraded) { | ||
throw errCode(new Error('connection upgraded timestamp must be provided'), 'ERR_INVALID_PARAMETERS') | ||
} | ||
} | ||
/** | ||
* @callback Sink | ||
* @param {Uint8Array} source | ||
* @returns {Promise<Uint8Array>} | ||
* | ||
* @typedef {object} DuplexIterableStream | ||
* @property {Sink} sink | ||
* @property {() AsyncIterator<Uint8Array>} source | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this |
||
* | ||
* @typedef {import('peer-id')} PeerId | ||
* @typedef {import('multiaddr')} Multiaddr | ||
*/ | ||
|
||
/** | ||
* An implementation of the js-libp2p connection. | ||
|
@@ -60,13 +27,13 @@ class Connection { | |
/** | ||
* Creates an instance of Connection. | ||
* @param {object} properties properties of the connection. | ||
* @param {multiaddr} [properties.localAddr] local multiaddr of the connection if known. | ||
* @param {multiaddr} [properties.remoteAddr] remote multiaddr of the connection. | ||
* @param {Multiaddr} [properties.localAddr] local multiaddr of the connection if known. | ||
* @param {Multiaddr} [properties.remoteAddr] remote multiaddr of the connection. | ||
* @param {PeerId} properties.localPeer local peer-id. | ||
* @param {PeerId} properties.remotePeer remote peer-id. | ||
* @param {function} properties.newStream new stream muxer function. | ||
* @param {function} properties.close close raw connection function. | ||
* @param {function(): Stream[]} properties.getStreams get streams from muxer function. | ||
* @param {function(): DuplexIterableStream[]} properties.getStreams get streams from muxer function. | ||
* @param {object} properties.stat metadata of the connection. | ||
* @param {string} properties.stat.direction connection establishment direction ("inbound" or "outbound"). | ||
* @param {object} properties.stat.timeline connection relevant events timestamp. | ||
|
@@ -157,7 +124,7 @@ class Connection { | |
/** | ||
* Create a new stream from this connection | ||
* @param {string[]} protocols intended protocol for the stream | ||
* @return {Promise<{stream: Stream, protocol: string}>} with muxed+multistream-selected stream and selected protocol | ||
* @return {Promise<{stream: DuplexIterableStream, protocol: string}>} with muxed+multistream-selected stream and selected protocol | ||
*/ | ||
async newStream (protocols) { | ||
if (this.stat.status === Status.CLOSING) { | ||
|
@@ -182,7 +149,7 @@ class Connection { | |
|
||
/** | ||
* Add a stream when it is opened to the registry. | ||
* @param {*} muxedStream a muxed stream | ||
* @param {DuplexIterableStream} muxedStream a muxed stream | ||
* @param {object} properties the stream properties to be registered | ||
* @param {string} properties.protocol the protocol used by the stream | ||
* @param {object} properties.metadata metadata of the stream | ||
|
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 @@ | ||
export function validateArgs(localAddr: any, localPeer: any, remotePeer: any, newStream: any, close: any, getStreams: any, stat: any): void; |
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,55 @@ | ||
'use strict' | ||
|
||
const errCode = require('err-code') | ||
const PeerId = require('peer-id') | ||
const multiaddr = require('multiaddr') | ||
|
||
function validateArgs (localAddr, localPeer, remotePeer, newStream, close, getStreams, stat) { | ||
if (localAddr && !multiaddr.isMultiaddr(localAddr)) { | ||
throw errCode(new Error('localAddr must be an instance of multiaddr'), 'ERR_INVALID_PARAMETERS') | ||
} | ||
|
||
if (!PeerId.isPeerId(localPeer)) { | ||
throw errCode(new Error('localPeer must be an instance of peer-id'), 'ERR_INVALID_PARAMETERS') | ||
} | ||
|
||
if (!PeerId.isPeerId(remotePeer)) { | ||
throw errCode(new Error('remotePeer must be an instance of peer-id'), 'ERR_INVALID_PARAMETERS') | ||
} | ||
|
||
if (typeof newStream !== 'function') { | ||
throw errCode(new Error('new stream must be a function'), 'ERR_INVALID_PARAMETERS') | ||
} | ||
|
||
if (typeof close !== 'function') { | ||
throw errCode(new Error('close must be a function'), 'ERR_INVALID_PARAMETERS') | ||
} | ||
|
||
if (typeof getStreams !== 'function') { | ||
throw errCode(new Error('getStreams must be a function'), 'ERR_INVALID_PARAMETERS') | ||
} | ||
|
||
if (!stat) { | ||
throw errCode(new Error('connection metadata object must be provided'), 'ERR_INVALID_PARAMETERS') | ||
} | ||
|
||
if (stat.direction !== 'inbound' && stat.direction !== 'outbound') { | ||
throw errCode(new Error('direction must be "inbound" or "outbound"'), 'ERR_INVALID_PARAMETERS') | ||
} | ||
|
||
if (!stat.timeline) { | ||
throw errCode(new Error('connection timeline object must be provided in the stat object'), 'ERR_INVALID_PARAMETERS') | ||
} | ||
|
||
if (!stat.timeline.open) { | ||
throw errCode(new Error('connection open timestamp must be provided'), 'ERR_INVALID_PARAMETERS') | ||
} | ||
|
||
if (!stat.timeline.upgraded) { | ||
throw errCode(new Error('connection upgraded timestamp must be provided'), 'ERR_INVALID_PARAMETERS') | ||
} | ||
} | ||
|
||
module.exports = { | ||
validateArgs | ||
} |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The type here should really just be a
Stream
, and it should match what's coming from mplex https://github.com/libp2p/js-libp2p-mplex/blob/v0.10.1/src/stream.js#L59There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we add the Stream in a type declaration file in the multiplexer interface?