mirror of
https://github.com/fluencelabs/js-libp2p-interfaces
synced 2025-04-24 21:12:21 +00:00
Compare commits
13 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
96d4a118db | ||
|
d14c37d14b | ||
|
9340ec9d98 | ||
|
d9224d5409 | ||
|
edb8ca60e9 | ||
|
810642d8ff | ||
|
64a478d313 | ||
|
b948972c5b | ||
|
edcedf093d | ||
|
3b960d516f | ||
|
2429e7246f | ||
|
22a7f6a9f1 | ||
|
fb9fce8713 |
2
.github/workflows/main.yml
vendored
2
.github/workflows/main.yml
vendored
@ -27,7 +27,7 @@ jobs:
|
|||||||
strategy:
|
strategy:
|
||||||
matrix:
|
matrix:
|
||||||
os: [windows-latest, ubuntu-latest, macos-latest]
|
os: [windows-latest, ubuntu-latest, macos-latest]
|
||||||
node: [12, 14]
|
node: [14, 15]
|
||||||
fail-fast: true
|
fail-fast: true
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v2
|
- uses: actions/checkout@v2
|
||||||
|
17
CHANGELOG.md
17
CHANGELOG.md
@ -1,3 +1,20 @@
|
|||||||
|
# [0.9.0](https://github.com/libp2p/js-interfaces/compare/v0.8.4...v0.9.0) (2021-04-07)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
## [0.8.4](https://github.com/libp2p/js-interfaces/compare/v0.8.3...v0.8.4) (2021-03-22)
|
||||||
|
|
||||||
|
|
||||||
|
### Bug Fixes
|
||||||
|
|
||||||
|
* specify connection direction ([#86](https://github.com/libp2p/js-interfaces/issues/86)) ([3b960d5](https://github.com/libp2p/js-interfaces/commit/3b960d516f70f7e198574a736cb09000ddd7a94c))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
## [0.8.3](https://github.com/libp2p/js-interfaces/compare/v0.8.2...v0.8.3) (2021-01-26)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
## [0.8.2](https://github.com/libp2p/js-interfaces/compare/v0.8.1...v0.8.2) (2021-01-20)
|
## [0.8.2](https://github.com/libp2p/js-interfaces/compare/v0.8.1...v0.8.2) (2021-01-20)
|
||||||
|
|
||||||
|
|
||||||
|
49
package.json
49
package.json
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "libp2p-interfaces",
|
"name": "libp2p-interfaces",
|
||||||
"version": "0.8.2",
|
"version": "0.9.0",
|
||||||
"description": "Interfaces for JS Libp2p",
|
"description": "Interfaces for JS Libp2p",
|
||||||
"leadMaintainer": "Jacob Heun <jacobheun@gmail.com>",
|
"leadMaintainer": "Jacob Heun <jacobheun@gmail.com>",
|
||||||
"main": "src/index.js",
|
"main": "src/index.js",
|
||||||
@ -22,6 +22,7 @@
|
|||||||
"extends": "ipfs"
|
"extends": "ipfs"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
"prepare": "aegir build --no-bundle",
|
||||||
"lint": "aegir lint",
|
"lint": "aegir lint",
|
||||||
"build": "aegir build",
|
"build": "aegir build",
|
||||||
"test": "aegir test",
|
"test": "aegir test",
|
||||||
@ -46,39 +47,43 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://github.com/libp2p/js-interfaces#readme",
|
"homepage": "https://github.com/libp2p/js-interfaces#readme",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@types/bl": "^2.1.0",
|
"@types/bl": "4.1.0",
|
||||||
"abort-controller": "^3.0.0",
|
"abort-controller": "^3.0.0",
|
||||||
"abortable-iterator": "^3.0.0",
|
"abortable-iterator": "^3.0.0",
|
||||||
"chai": "^4.2.0",
|
"chai": "^4.3.4",
|
||||||
"chai-checkmark": "^1.0.1",
|
"chai-checkmark": "^1.0.1",
|
||||||
"debug": "^4.1.1",
|
"debug": "^4.3.1",
|
||||||
"delay": "^4.3.0",
|
"delay": "^5.0.0",
|
||||||
"detect-node": "^2.0.4",
|
"detect-node": "^2.0.4",
|
||||||
"dirty-chai": "^2.0.1",
|
"dirty-chai": "^2.0.1",
|
||||||
"err-code": "^2.0.0",
|
"err-code": "^3.0.1",
|
||||||
"it-goodbye": "^2.0.1",
|
"it-goodbye": "^2.0.2",
|
||||||
"it-length-prefixed": "^3.1.0",
|
"it-length-prefixed": "^3.1.0",
|
||||||
"it-pair": "^1.0.0",
|
"it-pair": "^1.0.0",
|
||||||
"it-pipe": "^1.1.0",
|
"it-pipe": "^1.1.0",
|
||||||
"it-pushable": "^1.4.0",
|
"it-pushable": "^1.4.2",
|
||||||
"libp2p-crypto": "^0.18.0",
|
"libp2p-crypto": "fluencelabs/js-libp2p-crypto",
|
||||||
"libp2p-tcp": "^0.15.0",
|
"libp2p-tcp": "fluencelabs/js-libp2p-tcp",
|
||||||
"multiaddr": "^8.0.0",
|
"multiaddr": "^9.0.1",
|
||||||
"multibase": "^3.0.0",
|
"multibase": "^4.0.2",
|
||||||
"multihashes": "^3.0.1",
|
"multihashes": "^4.0.2",
|
||||||
"p-defer": "^3.0.0",
|
"p-defer": "^3.0.0",
|
||||||
"p-limit": "^2.3.0",
|
"p-limit": "^3.1.0",
|
||||||
"p-wait-for": "^3.1.0",
|
"p-wait-for": "^3.2.0",
|
||||||
"peer-id": "^0.14.0",
|
"peer-id": "fluencelabs/js-peer-id",
|
||||||
"protons": "^2.0.0",
|
"protons": "^2.0.0",
|
||||||
"sinon": "^9.0.2",
|
"sinon": "^10.0.0",
|
||||||
"streaming-iterables": "^5.0.2",
|
"streaming-iterables": "^5.0.4",
|
||||||
"uint8arrays": "^1.1.0"
|
"uint8arrays": "^2.1.3"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"aegir": "^29.2.0",
|
"@types/debug": "^4.1.5",
|
||||||
"it-handshake": "^1.0.1",
|
"aegir": "^32.1.0",
|
||||||
"rimraf": "^3.0.2"
|
"cids": "^1.1.6",
|
||||||
|
"events": "^3.3.0",
|
||||||
|
"it-handshake": "^1.0.2",
|
||||||
|
"rimraf": "^3.0.2",
|
||||||
|
"util": "^0.12.3"
|
||||||
},
|
},
|
||||||
"contributors": [
|
"contributors": [
|
||||||
"Alan Shaw <alan.shaw@protocol.ai>",
|
"Alan Shaw <alan.shaw@protocol.ai>",
|
||||||
|
@ -19,7 +19,7 @@ const connectionSymbol = Symbol.for('@libp2p/interface-connection/connection')
|
|||||||
* @property {number} [close]
|
* @property {number} [close]
|
||||||
*
|
*
|
||||||
* @typedef {Object} ConectionStat
|
* @typedef {Object} ConectionStat
|
||||||
* @property {string} direction - connection establishment direction ("inbound" or "outbound").
|
* @property {'inbound' | 'outbound'} direction - connection establishment direction
|
||||||
* @property {Timeline} timeline - connection relevant events timestamp.
|
* @property {Timeline} timeline - connection relevant events timestamp.
|
||||||
* @property {string} [multiplexer] - connection multiplexing identifier.
|
* @property {string} [multiplexer] - connection multiplexing identifier.
|
||||||
* @property {string} [encryption] - connection encryption method identifier.
|
* @property {string} [encryption] - connection encryption method identifier.
|
||||||
@ -230,8 +230,17 @@ class Connection {
|
|||||||
|
|
||||||
module.exports = Connection
|
module.exports = Connection
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {multiaddr|undefined} localAddr
|
||||||
|
* @param {PeerId} localPeer
|
||||||
|
* @param {PeerId} remotePeer
|
||||||
|
* @param {(protocols: string | string[]) => Promise<{ stream: import("../stream-muxer/types").MuxedStream; protocol: string; }>} newStream
|
||||||
|
* @param {() => Promise<void>} close
|
||||||
|
* @param {() => import("../stream-muxer/types").MuxedStream[]} getStreams
|
||||||
|
* @param {{ direction: any; timeline: any; multiplexer?: string | undefined; encryption?: string | undefined; }} stat
|
||||||
|
*/
|
||||||
function validateArgs (localAddr, localPeer, remotePeer, newStream, close, getStreams, stat) {
|
function validateArgs (localAddr, localPeer, remotePeer, newStream, close, getStreams, stat) {
|
||||||
if (localAddr && !multiaddr.isMultiaddr(localAddr)) {
|
if (localAddr && !multiaddr.Multiaddr.isMultiaddr(localAddr)) {
|
||||||
throw errCode(new Error('localAddr must be an instance of multiaddr'), 'ERR_INVALID_PARAMETERS')
|
throw errCode(new Error('localAddr must be an instance of multiaddr'), 'ERR_INVALID_PARAMETERS')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
// @ts-nocheck interface tests
|
||||||
/* eslint-env mocha */
|
/* eslint-env mocha */
|
||||||
|
|
||||||
'use strict'
|
'use strict'
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
// @ts-nocheck interface tests
|
||||||
/* eslint-env mocha */
|
/* eslint-env mocha */
|
||||||
|
|
||||||
'use strict'
|
'use strict'
|
||||||
|
11
src/content-routing/types.d.ts
vendored
Normal file
11
src/content-routing/types.d.ts
vendored
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
export = ContentRouting;
|
||||||
|
|
||||||
|
import PeerId from 'peer-id'
|
||||||
|
import { Multiaddr } from 'multiaddr'
|
||||||
|
import CID from 'cids'
|
||||||
|
|
||||||
|
declare class ContentRouting {
|
||||||
|
constructor (options: Object);
|
||||||
|
provide (cid: CID): Promise<void>;
|
||||||
|
findProviders (cid: CID, options: Object): AsyncIterable<{ id: PeerId, multiaddrs: Multiaddr[] }>;
|
||||||
|
}
|
@ -1,3 +1,4 @@
|
|||||||
|
// @ts-nocheck interface tests
|
||||||
/* eslint-env mocha */
|
/* eslint-env mocha */
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
// @ts-nocheck interface tests
|
||||||
/* eslint-env mocha */
|
/* eslint-env mocha */
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
@ -54,7 +55,7 @@ module.exports = (common) => {
|
|||||||
expect(PeerId.isPeerId(id)).to.eql(true)
|
expect(PeerId.isPeerId(id)).to.eql(true)
|
||||||
expect(multiaddrs).to.exist()
|
expect(multiaddrs).to.exist()
|
||||||
|
|
||||||
multiaddrs.forEach((m) => expect(multiaddr.isMultiaddr(m)).to.eql(true))
|
multiaddrs.forEach((m) => expect(multiaddr.Multiaddr.isMultiaddr(m)).to.eql(true))
|
||||||
|
|
||||||
defer.resolve()
|
defer.resolve()
|
||||||
})
|
})
|
||||||
|
10
src/peer-discovery/types.d.ts
vendored
Normal file
10
src/peer-discovery/types.d.ts
vendored
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
export = PeerDiscovery;
|
||||||
|
|
||||||
|
import events from 'events';
|
||||||
|
|
||||||
|
declare class PeerDiscovery extends events.EventEmitter {
|
||||||
|
constructor (options: Object);
|
||||||
|
start (): Promise<void>;
|
||||||
|
stop (): Promise<void>;
|
||||||
|
tag: string;
|
||||||
|
}
|
10
src/peer-routing/types.d.ts
vendored
Normal file
10
src/peer-routing/types.d.ts
vendored
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
export = PeerRouting;
|
||||||
|
|
||||||
|
import PeerId from 'peer-id'
|
||||||
|
import { Multiaddr } from 'multiaddr'
|
||||||
|
|
||||||
|
declare class PeerRouting {
|
||||||
|
constructor (options?: Object);
|
||||||
|
findPeer (peerId: PeerId, options?: Object): Promise<{ id: PeerId, multiaddrs: Multiaddr[] }>;
|
||||||
|
getClosestPeers(key: Uint8Array, options?: Object): AsyncIterable<{ id: PeerId, multiaddrs: Multiaddr[] }>;
|
||||||
|
}
|
@ -10,9 +10,7 @@ const { pipe } = require('it-pipe')
|
|||||||
|
|
||||||
const MulticodecTopology = require('../topology/multicodec-topology')
|
const MulticodecTopology = require('../topology/multicodec-topology')
|
||||||
const { codes } = require('./errors')
|
const { codes } = require('./errors')
|
||||||
/**
|
|
||||||
* @type {typeof import('./message')}
|
|
||||||
*/
|
|
||||||
const message = require('./message')
|
const message = require('./message')
|
||||||
const PeerStreams = require('./peer-streams')
|
const PeerStreams = require('./peer-streams')
|
||||||
const { SignaturePolicy } = require('./signature-policy')
|
const { SignaturePolicy } = require('./signature-policy')
|
||||||
@ -29,9 +27,9 @@ const {
|
|||||||
* @typedef {import('bl')} BufferList
|
* @typedef {import('bl')} BufferList
|
||||||
* @typedef {import('../stream-muxer/types').MuxedStream} MuxedStream
|
* @typedef {import('../stream-muxer/types').MuxedStream} MuxedStream
|
||||||
* @typedef {import('../connection/connection')} Connection
|
* @typedef {import('../connection/connection')} Connection
|
||||||
* @typedef {import('./message').RPC} RPC
|
* @typedef {import('./message/types').RPC} RPC
|
||||||
* @typedef {import('./message').SubOpts} RPCSubOpts
|
* @typedef {import('./message/types').SubOpts} RPCSubOpts
|
||||||
* @typedef {import('./message').Message} RPCMessage
|
* @typedef {import('./message/types').Message} RPCMessage
|
||||||
* @typedef {import('./signature-policy').SignaturePolicyType} SignaturePolicyType
|
* @typedef {import('./signature-policy').SignaturePolicyType} SignaturePolicyType
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@ -44,6 +42,16 @@ const {
|
|||||||
* @property {Uint8Array} data
|
* @property {Uint8Array} data
|
||||||
* @property {Uint8Array} [signature]
|
* @property {Uint8Array} [signature]
|
||||||
* @property {Uint8Array} [key]
|
* @property {Uint8Array} [key]
|
||||||
|
*
|
||||||
|
* @typedef {Object} PubsubProperties
|
||||||
|
* @property {string} debugName - log namespace
|
||||||
|
* @property {Array<string>|string} multicodecs - protocol identificers to connect
|
||||||
|
* @property {Libp2p} libp2p
|
||||||
|
*
|
||||||
|
* @typedef {Object} PubsubOptions
|
||||||
|
* @property {SignaturePolicyType} [globalSignaturePolicy = SignaturePolicy.StrictSign] - defines how signatures should be handled
|
||||||
|
* @property {boolean} [canRelayMessage = false] - if can relay messages not subscribed
|
||||||
|
* @property {boolean} [emitSelf = false] - if publish should emit to self, if subscribed
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -52,13 +60,7 @@ const {
|
|||||||
*/
|
*/
|
||||||
class PubsubBaseProtocol extends EventEmitter {
|
class PubsubBaseProtocol extends EventEmitter {
|
||||||
/**
|
/**
|
||||||
* @param {Object} props
|
* @param {PubsubProperties & PubsubOptions} props
|
||||||
* @param {string} props.debugName - log namespace
|
|
||||||
* @param {Array<string>|string} props.multicodecs - protocol identificers to connect
|
|
||||||
* @param {Libp2p} props.libp2p
|
|
||||||
* @param {SignaturePolicyType} [props.globalSignaturePolicy = SignaturePolicy.StrictSign] - defines how signatures should be handled
|
|
||||||
* @param {boolean} [props.canRelayMessage = false] - if can relay messages not subscribed
|
|
||||||
* @param {boolean} [props.emitSelf = false] - if publish should emit to self, if subscribed
|
|
||||||
* @abstract
|
* @abstract
|
||||||
*/
|
*/
|
||||||
constructor ({
|
constructor ({
|
||||||
@ -83,8 +85,9 @@ class PubsubBaseProtocol extends EventEmitter {
|
|||||||
|
|
||||||
super()
|
super()
|
||||||
|
|
||||||
this.log = debug(debugName)
|
this.log = Object.assign(debug(debugName), {
|
||||||
this.log.err = debug(`${debugName}:error`)
|
err: debug(`${debugName}:error`)
|
||||||
|
})
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @type {Array<string>}
|
* @type {Array<string>}
|
||||||
@ -122,7 +125,7 @@ class PubsubBaseProtocol extends EventEmitter {
|
|||||||
|
|
||||||
// validate signature policy
|
// validate signature policy
|
||||||
if (!SignaturePolicy[globalSignaturePolicy]) {
|
if (!SignaturePolicy[globalSignaturePolicy]) {
|
||||||
throw errcode(new Error('Invalid global signature policy'), codes.ERR_INVALID_SIGUATURE_POLICY)
|
throw errcode(new Error('Invalid global signature policy'), codes.ERR_INVALID_SIGNATURE_POLICY)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -379,7 +382,9 @@ class PubsubBaseProtocol extends EventEmitter {
|
|||||||
|
|
||||||
if (subs.length) {
|
if (subs.length) {
|
||||||
// update peer subscriptions
|
// update peer subscriptions
|
||||||
subs.forEach((subOpt) => this._processRpcSubOpt(idB58Str, subOpt))
|
subs.forEach((/** @type {RPCSubOpts} */ subOpt) => {
|
||||||
|
this._processRpcSubOpt(idB58Str, subOpt)
|
||||||
|
})
|
||||||
this.emit('pubsub:subscription-change', peerStreams.id, subs)
|
this.emit('pubsub:subscription-change', peerStreams.id, subs)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -389,8 +394,9 @@ class PubsubBaseProtocol extends EventEmitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (msgs.length) {
|
if (msgs.length) {
|
||||||
msgs.forEach(message => {
|
// @ts-ignore RPC message is modified
|
||||||
if (!(this.canRelayMessage || message.topicIDs.some((topic) => this.subscriptions.has(topic)))) {
|
msgs.forEach((message) => {
|
||||||
|
if (!(this.canRelayMessage || message.topicIDs.some((/** @type {string} */ topic) => this.subscriptions.has(topic)))) {
|
||||||
this.log('received message we didn\'t subscribe to. Dropping.')
|
this.log('received message we didn\'t subscribe to. Dropping.')
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -589,7 +595,7 @@ class PubsubBaseProtocol extends EventEmitter {
|
|||||||
for (const topic of message.topicIDs) {
|
for (const topic of message.topicIDs) {
|
||||||
const validatorFn = this.topicValidators.get(topic)
|
const validatorFn = this.topicValidators.get(topic)
|
||||||
if (!validatorFn) {
|
if (!validatorFn) {
|
||||||
continue
|
continue // eslint-disable-line
|
||||||
}
|
}
|
||||||
await validatorFn(topic, message)
|
await validatorFn(topic, message)
|
||||||
}
|
}
|
||||||
|
@ -1,17 +1,16 @@
|
|||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
|
// @ts-ignore protons not typed
|
||||||
const protons = require('protons')
|
const protons = require('protons')
|
||||||
|
|
||||||
const rpcProto = protons(require('./rpc.proto.js'))
|
const rpcProto = protons(require('./rpc.proto.js'))
|
||||||
const RPC = rpcProto.RPC
|
const RPC = rpcProto.RPC
|
||||||
const topicDescriptorProto = protons(require('./topic-descriptor.proto.js'))
|
const topicDescriptorProto = protons(require('./topic-descriptor.proto.js'))
|
||||||
|
|
||||||
/**
|
module.exports = {
|
||||||
* @module pubsub/message/index
|
rpc: rpcProto,
|
||||||
*/
|
td: topicDescriptorProto,
|
||||||
exports = module.exports
|
RPC,
|
||||||
exports.rpc = rpcProto
|
Message: RPC.Message,
|
||||||
exports.td = topicDescriptorProto
|
SubOpts: RPC.SubOpts
|
||||||
exports.RPC = RPC
|
}
|
||||||
exports.Message = RPC.Message
|
|
||||||
exports.SubOpts = RPC.SubOpts
|
|
||||||
|
5
src/pubsub/message/types.d.ts
vendored
Normal file
5
src/pubsub/message/types.d.ts
vendored
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
import { RPC, Message, SubOpts } from './types'
|
||||||
|
|
||||||
|
export type RPC = RPC
|
||||||
|
export type Message = Message
|
||||||
|
export type SubOpts = SubOpts
|
@ -1,21 +1,20 @@
|
|||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
|
const debug = require('debug')
|
||||||
|
const log = Object.assign(debug('libp2p-pubsub:peer-streams'), {
|
||||||
|
error: debug('libp2p-pubsub:peer-streams:err')
|
||||||
|
})
|
||||||
|
|
||||||
/** @typedef {import('../types').EventEmitterFactory} Events */
|
/** @typedef {import('../types').EventEmitterFactory} Events */
|
||||||
/** @type Events */
|
/** @type Events */
|
||||||
const EventEmitter = require('events')
|
const EventEmitter = require('events')
|
||||||
|
|
||||||
const lp = require('it-length-prefixed')
|
const lp = require('it-length-prefixed')
|
||||||
|
|
||||||
/** @type {typeof import('it-pushable').default} */
|
|
||||||
// @ts-ignore
|
|
||||||
const pushable = require('it-pushable')
|
const pushable = require('it-pushable')
|
||||||
const { pipe } = require('it-pipe')
|
const { pipe } = require('it-pipe')
|
||||||
const { source: abortable } = require('abortable-iterator')
|
const { source: abortable } = require('abortable-iterator')
|
||||||
const AbortController = require('abort-controller').default
|
const AbortController = require('abort-controller').default
|
||||||
const debug = require('debug')
|
|
||||||
|
|
||||||
const log = debug('libp2p-pubsub:peer-streams')
|
|
||||||
log.error = debug('libp2p-pubsub:peer-streams:error')
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @typedef {import('../stream-muxer/types').MuxedStream} MuxedStream
|
* @typedef {import('../stream-muxer/types').MuxedStream} MuxedStream
|
||||||
@ -170,7 +169,7 @@ class PeerStreams extends EventEmitter {
|
|||||||
this.outboundStream,
|
this.outboundStream,
|
||||||
lp.encode(),
|
lp.encode(),
|
||||||
this._rawOutboundStream
|
this._rawOutboundStream
|
||||||
).catch(err => {
|
).catch(/** @param {Error} err */ err => {
|
||||||
log.error(err)
|
log.error(err)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
// @ts-nocheck interface tests
|
||||||
/* eslint-env mocha */
|
/* eslint-env mocha */
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
// @ts-nocheck interface tests
|
||||||
/* eslint-env mocha */
|
/* eslint-env mocha */
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
// @ts-nocheck interface tests
|
||||||
/* eslint-env mocha */
|
/* eslint-env mocha */
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
// @ts-nocheck interface tests
|
||||||
/* eslint-env mocha */
|
/* eslint-env mocha */
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
// @ts-nocheck interface tests
|
||||||
/* eslint-env mocha */
|
/* eslint-env mocha */
|
||||||
/* eslint max-nested-callbacks: ["error", 6] */
|
/* eslint max-nested-callbacks: ["error", 6] */
|
||||||
'use strict'
|
'use strict'
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
// @ts-nocheck interface tests
|
||||||
/* eslint-env mocha */
|
/* eslint-env mocha */
|
||||||
/* eslint max-nested-callbacks: ["error", 6] */
|
/* eslint max-nested-callbacks: ["error", 6] */
|
||||||
'use strict'
|
'use strict'
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
// @ts-nocheck interface tests
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
const { expect } = require('chai')
|
const { expect } = require('chai')
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
|
// @ts-ignore libp2p crypto has no types
|
||||||
const randomBytes = require('libp2p-crypto/src/random-bytes')
|
const randomBytes = require('libp2p-crypto/src/random-bytes')
|
||||||
const uint8ArrayToString = require('uint8arrays/to-string')
|
const uint8ArrayToString = require('uint8arrays/to-string')
|
||||||
const uint8ArrayFromString = require('uint8arrays/from-string')
|
const uint8ArrayFromString = require('uint8arrays/from-string')
|
||||||
const PeerId = require('peer-id')
|
const PeerId = require('peer-id')
|
||||||
const multihash = require('multihashes')
|
const multihash = require('multihashes')
|
||||||
exports = module.exports
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generatea random sequence number.
|
* Generatea random sequence number.
|
||||||
@ -13,7 +13,7 @@ exports = module.exports
|
|||||||
* @returns {Uint8Array}
|
* @returns {Uint8Array}
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
exports.randomSeqno = () => {
|
const randomSeqno = () => {
|
||||||
return randomBytes(8)
|
return randomBytes(8)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -25,7 +25,7 @@ exports.randomSeqno = () => {
|
|||||||
* @returns {Uint8Array}
|
* @returns {Uint8Array}
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
exports.msgId = (from, seqno) => {
|
const msgId = (from, seqno) => {
|
||||||
const fromBytes = PeerId.createFromB58String(from).id
|
const fromBytes = PeerId.createFromB58String(from).id
|
||||||
const msgId = new Uint8Array(fromBytes.length + seqno.length)
|
const msgId = new Uint8Array(fromBytes.length + seqno.length)
|
||||||
msgId.set(fromBytes, 0)
|
msgId.set(fromBytes, 0)
|
||||||
@ -40,22 +40,28 @@ exports.msgId = (from, seqno) => {
|
|||||||
* @returns {Uint8Array}
|
* @returns {Uint8Array}
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
exports.noSignMsgId = (data) => multihash.encode(data, 'sha2-256')
|
const noSignMsgId = (data) => multihash.encode(data, 'sha2-256')
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if any member of the first set is also a member
|
* Check if any member of the first set is also a member
|
||||||
* of the second set.
|
* of the second set.
|
||||||
*
|
*
|
||||||
* @param {Set|Array} a
|
* @param {Set<number>|Array<number>} a
|
||||||
* @param {Set|Array} b
|
* @param {Set<number>|Array<number>} b
|
||||||
* @returns {boolean}
|
* @returns {boolean}
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
exports.anyMatch = (a, b) => {
|
const anyMatch = (a, b) => {
|
||||||
let bHas
|
let bHas
|
||||||
if (Array.isArray(b)) {
|
if (Array.isArray(b)) {
|
||||||
|
/**
|
||||||
|
* @param {number} val
|
||||||
|
*/
|
||||||
bHas = (val) => b.indexOf(val) > -1
|
bHas = (val) => b.indexOf(val) > -1
|
||||||
} else {
|
} else {
|
||||||
|
/**
|
||||||
|
* @param {number} val
|
||||||
|
*/
|
||||||
bHas = (val) => b.has(val)
|
bHas = (val) => b.has(val)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -76,7 +82,7 @@ exports.anyMatch = (a, b) => {
|
|||||||
* @returns {T[]}
|
* @returns {T[]}
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
exports.ensureArray = (maybeArray) => {
|
const ensureArray = (maybeArray) => {
|
||||||
if (!Array.isArray(maybeArray)) {
|
if (!Array.isArray(maybeArray)) {
|
||||||
return [maybeArray]
|
return [maybeArray]
|
||||||
}
|
}
|
||||||
@ -92,7 +98,7 @@ exports.ensureArray = (maybeArray) => {
|
|||||||
* @param {string} [peerId]
|
* @param {string} [peerId]
|
||||||
* @returns {T & {from?: string, peerId?: string }}
|
* @returns {T & {from?: string, peerId?: string }}
|
||||||
*/
|
*/
|
||||||
exports.normalizeInRpcMessage = (message, peerId) => {
|
const normalizeInRpcMessage = (message, peerId) => {
|
||||||
const m = Object.assign({}, message)
|
const m = Object.assign({}, message)
|
||||||
if (message.from instanceof Uint8Array) {
|
if (message.from instanceof Uint8Array) {
|
||||||
m.from = uint8ArrayToString(message.from, 'base58btc')
|
m.from = uint8ArrayToString(message.from, 'base58btc')
|
||||||
@ -109,13 +115,23 @@ exports.normalizeInRpcMessage = (message, peerId) => {
|
|||||||
* @param {T} message
|
* @param {T} message
|
||||||
* @returns {T & {from?: Uint8Array, data?: Uint8Array}}
|
* @returns {T & {from?: Uint8Array, data?: Uint8Array}}
|
||||||
*/
|
*/
|
||||||
exports.normalizeOutRpcMessage = (message) => {
|
const normalizeOutRpcMessage = (message) => {
|
||||||
const m = Object.assign({}, message)
|
const m = Object.assign({}, message)
|
||||||
if (typeof message.from === 'string' || message.from instanceof String) {
|
if (typeof message.from === 'string') {
|
||||||
m.from = uint8ArrayFromString(message.from, 'base58btc')
|
m.from = uint8ArrayFromString(message.from, 'base58btc')
|
||||||
}
|
}
|
||||||
if (typeof message.data === 'string' || message.data instanceof String) {
|
if (typeof message.data === 'string') {
|
||||||
m.data = uint8ArrayFromString(message.data)
|
m.data = uint8ArrayFromString(message.data)
|
||||||
}
|
}
|
||||||
return m
|
return m
|
||||||
}
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
randomSeqno,
|
||||||
|
msgId,
|
||||||
|
noSignMsgId,
|
||||||
|
anyMatch,
|
||||||
|
ensureArray,
|
||||||
|
normalizeInRpcMessage,
|
||||||
|
normalizeOutRpcMessage
|
||||||
|
}
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
// @ts-nocheck interface tests
|
||||||
/* eslint-env mocha */
|
/* eslint-env mocha */
|
||||||
|
|
||||||
'use strict'
|
'use strict'
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
// @ts-nocheck interface tests
|
||||||
/* eslint-env mocha */
|
/* eslint-env mocha */
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
// @ts-nocheck interface tests
|
||||||
/* eslint-env mocha */
|
/* eslint-env mocha */
|
||||||
/* eslint max-nested-callbacks: ["error", 8] */
|
/* eslint max-nested-callbacks: ["error", 8] */
|
||||||
'use strict'
|
'use strict'
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
// @ts-nocheck interface tests
|
||||||
/* eslint-env mocha */
|
/* eslint-env mocha */
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
// @ts-nocheck interface tests
|
||||||
/* eslint-env mocha */
|
/* eslint-env mocha */
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
|
@ -1,10 +1,11 @@
|
|||||||
|
// @ts-nocheck interface tests
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
const { expect } = require('chai')
|
const { expect } = require('chai')
|
||||||
const pair = require('it-pair/duplex')
|
const pair = require('it-pair/duplex')
|
||||||
const { pipe } = require('it-pipe')
|
const { pipe } = require('it-pipe')
|
||||||
|
|
||||||
const pLimit = require('p-limit').default
|
const pLimit = require('p-limit')
|
||||||
const { collect, tap, consume } = require('streaming-iterables')
|
const { collect, tap, consume } = require('streaming-iterables')
|
||||||
|
|
||||||
module.exports = async (Muxer, nStreams, nMsg, limit) => {
|
module.exports = async (Muxer, nStreams, nMsg, limit) => {
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
// @ts-nocheck interface tests
|
||||||
/* eslint-env mocha */
|
/* eslint-env mocha */
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
|
@ -43,9 +43,9 @@ export interface MuxedStream extends AsyncIterable<Uint8Array | BufferList> {
|
|||||||
abort: () => void;
|
abort: () => void;
|
||||||
reset: () => void;
|
reset: () => void;
|
||||||
sink: Sink;
|
sink: Sink;
|
||||||
source: () => AsyncIterable<Uint8Array | BufferList>;
|
source: AsyncIterable<Uint8Array | BufferList>;
|
||||||
timeline: MuxedTimeline;
|
timeline: MuxedTimeline;
|
||||||
id: string;
|
id: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type Sink = (source: Uint8Array) => Promise<Uint8Array>;
|
export type Sink = (source: Uint8Array) => Promise<void>;
|
@ -62,6 +62,9 @@ class Topology {
|
|||||||
return Boolean(other && other[topologySymbol])
|
return Boolean(other && other[topologySymbol])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {any} registrar
|
||||||
|
*/
|
||||||
set registrar (registrar) { // eslint-disable-line
|
set registrar (registrar) { // eslint-disable-line
|
||||||
this._registrar = registrar
|
this._registrar = registrar
|
||||||
}
|
}
|
||||||
|
@ -56,6 +56,9 @@ class MulticodecTopology extends Topology {
|
|||||||
return Boolean(other && other[multicodecTopologySymbol])
|
return Boolean(other && other[multicodecTopologySymbol])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {any} registrar
|
||||||
|
*/
|
||||||
set registrar (registrar) { // eslint-disable-line
|
set registrar (registrar) { // eslint-disable-line
|
||||||
this._registrar = registrar
|
this._registrar = registrar
|
||||||
this._registrar.peerStore.on('change:protocols', this._onProtocolChange)
|
this._registrar.peerStore.on('change:protocols', this._onProtocolChange)
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
// @ts-nocheck interface tests
|
||||||
/* eslint-env mocha */
|
/* eslint-env mocha */
|
||||||
|
|
||||||
'use strict'
|
'use strict'
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
// @ts-nocheck interface tests
|
||||||
/* eslint-env mocha */
|
/* eslint-env mocha */
|
||||||
|
|
||||||
'use strict'
|
'use strict'
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
// @ts-nocheck interface tests
|
||||||
/* eslint-env mocha */
|
/* eslint-env mocha */
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
// @ts-nocheck interface tests
|
||||||
/* eslint-env mocha */
|
/* eslint-env mocha */
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
// @ts-nocheck interface tests
|
||||||
/* eslint-env mocha */
|
/* eslint-env mocha */
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
// @ts-nocheck interface tests
|
||||||
/* eslint max-nested-callbacks: ["error", 8] */
|
/* eslint max-nested-callbacks: ["error", 8] */
|
||||||
/* eslint-env mocha */
|
/* eslint-env mocha */
|
||||||
'use strict'
|
'use strict'
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
|
import BufferList from 'bl'
|
||||||
import events from 'events'
|
import events from 'events'
|
||||||
import Multiaddr from 'multiaddr'
|
import { Multiaddr } from 'multiaddr'
|
||||||
import Connection from '../connection/connection'
|
import Connection from '../connection/connection'
|
||||||
import { Sink } from '../stream-muxer/types'
|
import { Sink } from '../stream-muxer/types'
|
||||||
|
|
||||||
@ -62,7 +63,7 @@ export type MultiaddrConnectionTimeline = {
|
|||||||
|
|
||||||
export type MultiaddrConnection = {
|
export type MultiaddrConnection = {
|
||||||
sink: Sink;
|
sink: Sink;
|
||||||
source: () => AsyncIterable<Uint8Array>;
|
source: AsyncIterable<Uint8Array | BufferList>;
|
||||||
close: (err?: Error) => Promise<void>;
|
close: (err?: Error) => Promise<void>;
|
||||||
conn: unknown;
|
conn: unknown;
|
||||||
remoteAddr: Multiaddr;
|
remoteAddr: Multiaddr;
|
14
src/types.ts → src/types.d.ts
vendored
14
src/types.ts → src/types.d.ts
vendored
@ -3,13 +3,13 @@ export interface EventEmitterFactory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export interface EventEmitter {
|
export interface EventEmitter {
|
||||||
addListener(event: string | symbol, listener: (...args: any[]) => void);
|
addListener(event: string | symbol, listener: (...args: any[]) => void): EventEmitter;
|
||||||
on(event: string | symbol, listener: (...args: any[]) => void);
|
on(event: string | symbol, listener: (...args: any[]) => void): EventEmitter;
|
||||||
once(event: string | symbol, listener: (...args: any[]) => void);
|
once(event: string | symbol, listener: (...args: any[]) => void): EventEmitter;
|
||||||
removeListener(event: string | symbol, listener: (...args: any[]) => void);
|
removeListener(event: string | symbol, listener: (...args: any[]) => void): EventEmitter;
|
||||||
off(event: string | symbol, listener: (...args: any[]) => void);
|
off(event: string | symbol, listener: (...args: any[]) => void): EventEmitter;
|
||||||
removeAllListeners(event?: string | symbol);
|
removeAllListeners(event?: string | symbol): EventEmitter;
|
||||||
setMaxListeners(n: number);
|
setMaxListeners(n: number): EventEmitter;
|
||||||
getMaxListeners(): number;
|
getMaxListeners(): number;
|
||||||
listeners(event: string | symbol): Function[]; // eslint-disable-line @typescript-eslint/ban-types
|
listeners(event: string | symbol): Function[]; // eslint-disable-line @typescript-eslint/ban-types
|
||||||
rawListeners(event: string | symbol): Function[]; // eslint-disable-line @typescript-eslint/ban-types
|
rawListeners(event: string | symbol): Function[]; // eslint-disable-line @typescript-eslint/ban-types
|
@ -17,8 +17,8 @@ describe('compliance tests', () => {
|
|||||||
* @param {*} properties
|
* @param {*} properties
|
||||||
*/
|
*/
|
||||||
async setup (properties) {
|
async setup (properties) {
|
||||||
const localAddr = multiaddr('/ip4/127.0.0.1/tcp/8080')
|
const localAddr = new multiaddr.Multiaddr('/ip4/127.0.0.1/tcp/8080')
|
||||||
const remoteAddr = multiaddr('/ip4/127.0.0.1/tcp/8081')
|
const remoteAddr = new multiaddr.Multiaddr('/ip4/127.0.0.1/tcp/8081')
|
||||||
const [localPeer, remotePeer] = await Promise.all([
|
const [localPeer, remotePeer] = await Promise.all([
|
||||||
PeerId.createFromJSON(peers[0]),
|
PeerId.createFromJSON(peers[0]),
|
||||||
PeerId.createFromJSON(peers[1])
|
PeerId.createFromJSON(peers[1])
|
||||||
|
@ -41,7 +41,7 @@ class MockDiscovery extends EventEmitter {
|
|||||||
this._timer = setTimeout(() => {
|
this._timer = setTimeout(() => {
|
||||||
this.emit('peer', {
|
this.emit('peer', {
|
||||||
id: peerId,
|
id: peerId,
|
||||||
multiaddrs: [multiaddr('/ip4/127.0.0.1/tcp/8000')]
|
multiaddrs: [new multiaddr.Multiaddr('/ip4/127.0.0.1/tcp/8000')]
|
||||||
})
|
})
|
||||||
}, this.options.discoveryDelay || 1000)
|
}, this.options.discoveryDelay || 1000)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user