Compare commits

..

No commits in common. "master" and "v0.8.0" have entirely different histories.

47 changed files with 85 additions and 245 deletions

View File

@ -27,7 +27,7 @@ jobs:
strategy: strategy:
matrix: matrix:
os: [windows-latest, ubuntu-latest, macos-latest] os: [windows-latest, ubuntu-latest, macos-latest]
node: [14, 15] node: [12, 14]
fail-fast: true fail-fast: true
steps: steps:
- uses: actions/checkout@v2 - uses: actions/checkout@v2

View File

@ -1,38 +1,3 @@
# [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)
### Bug Fixes
* event emitter types with local types ([#80](https://github.com/libp2p/js-interfaces/issues/80)) ([ca52077](https://github.com/libp2p/js-interfaces/commit/ca520775eb26f5ed501375fdb24ba698c9a8c8c8))
## [0.8.1](https://github.com/libp2p/js-interfaces/compare/v0.8.0...v0.8.1) (2020-12-11)
### Bug Fixes
* pubsub publish message should be uint8array ([#77](https://github.com/libp2p/js-interfaces/issues/77)) ([5b99e6b](https://github.com/libp2p/js-interfaces/commit/5b99e6b56b10439a82ee88fb4e31fb95c182264f))
# [0.8.0](https://github.com/libp2p/js-interfaces/compare/v0.7.2...v0.8.0) (2020-12-10) # [0.8.0](https://github.com/libp2p/js-interfaces/compare/v0.7.2...v0.8.0) (2020-12-10)

View File

@ -4,8 +4,6 @@
[![](https://img.shields.io/badge/project-libp2p-yellow.svg?style=flat-square)](http://libp2p.io/) [![](https://img.shields.io/badge/project-libp2p-yellow.svg?style=flat-square)](http://libp2p.io/)
[![](https://img.shields.io/badge/freenode-%23libp2p-yellow.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23libp2p) [![](https://img.shields.io/badge/freenode-%23libp2p-yellow.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23libp2p)
[![](https://img.shields.io/discourse/https/discuss.libp2p.io/posts.svg)](https://discuss.libp2p.io) [![](https://img.shields.io/discourse/https/discuss.libp2p.io/posts.svg)](https://discuss.libp2p.io)
[![codecov](https://img.shields.io/codecov/c/github/libp2p/js-libp2p-interfaces.svg?style=flat-square)](https://codecov.io/gh/libp2p/js-libp2p-interfaces)
[![GitHub Workflow Status](https://img.shields.io/github/workflow/status/libp2p/js-libp2p-interfaces/ci?label=ci&style=flat-square)](https://github.com/libp2p/js-libp2p-interfaces/actions?query=branch%3Amaster+workflow%3Aci+)
> Contains test suites and interfaces you can use to implement the various components of libp2p. > Contains test suites and interfaces you can use to implement the various components of libp2p.

View File

@ -1,6 +1,6 @@
{ {
"name": "libp2p-interfaces", "name": "libp2p-interfaces",
"version": "0.9.0", "version": "0.8.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,7 +22,6 @@
"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",
@ -47,43 +46,39 @@
}, },
"homepage": "https://github.com/libp2p/js-interfaces#readme", "homepage": "https://github.com/libp2p/js-interfaces#readme",
"dependencies": { "dependencies": {
"@types/bl": "4.1.0", "@types/bl": "^2.1.0",
"abort-controller": "^3.0.0", "abort-controller": "^3.0.0",
"abortable-iterator": "^3.0.0", "abortable-iterator": "^3.0.0",
"chai": "^4.3.4", "chai": "^4.2.0",
"chai-checkmark": "^1.0.1", "chai-checkmark": "^1.0.1",
"debug": "^4.3.1", "debug": "^4.1.1",
"delay": "^5.0.0", "delay": "^4.3.0",
"detect-node": "^2.0.4", "detect-node": "^2.0.4",
"dirty-chai": "^2.0.1", "dirty-chai": "^2.0.1",
"err-code": "^3.0.1", "err-code": "^2.0.0",
"it-goodbye": "^2.0.2", "it-goodbye": "^2.0.1",
"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.2", "it-pushable": "^1.4.0",
"libp2p-crypto": "fluencelabs/js-libp2p-crypto", "libp2p-crypto": "^0.18.0",
"libp2p-tcp": "fluencelabs/js-libp2p-tcp", "libp2p-tcp": "^0.15.0",
"multiaddr": "^9.0.1", "multiaddr": "^8.0.0",
"multibase": "^4.0.2", "multibase": "^3.0.0",
"multihashes": "^4.0.2", "multihashes": "^3.0.1",
"p-defer": "^3.0.0", "p-defer": "^3.0.0",
"p-limit": "^3.1.0", "p-limit": "^2.3.0",
"p-wait-for": "^3.2.0", "p-wait-for": "^3.1.0",
"peer-id": "fluencelabs/js-peer-id", "peer-id": "^0.14.0",
"protons": "^2.0.0", "protons": "^2.0.0",
"sinon": "^10.0.0", "sinon": "^9.0.2",
"streaming-iterables": "^5.0.4", "streaming-iterables": "^5.0.2",
"uint8arrays": "^2.1.3" "uint8arrays": "^1.1.0"
}, },
"devDependencies": { "devDependencies": {
"@types/debug": "^4.1.5", "aegir": "^29.2.0",
"aegir": "^32.1.0", "it-handshake": "^1.0.1",
"cids": "^1.1.6", "rimraf": "^3.0.2"
"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>",

View File

@ -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 {'inbound' | 'outbound'} direction - connection establishment direction * @property {string} direction - connection establishment direction ("inbound" or "outbound").
* @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,17 +230,8 @@ 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.Multiaddr.isMultiaddr(localAddr)) { if (localAddr && !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')
} }

View File

@ -1,4 +1,3 @@
// @ts-nocheck interface tests
/* eslint-env mocha */ /* eslint-env mocha */
'use strict' 'use strict'

View File

@ -1,4 +1,3 @@
// @ts-nocheck interface tests
/* eslint-env mocha */ /* eslint-env mocha */
'use strict' 'use strict'

View File

@ -1,11 +0,0 @@
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[] }>;
}

View File

View File

@ -1,4 +1,3 @@
// @ts-nocheck interface tests
/* eslint-env mocha */ /* eslint-env mocha */
'use strict' 'use strict'

View File

@ -1,4 +1,3 @@
// @ts-nocheck interface tests
/* eslint-env mocha */ /* eslint-env mocha */
'use strict' 'use strict'
@ -55,7 +54,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.Multiaddr.isMultiaddr(m)).to.eql(true)) multiaddrs.forEach((m) => expect(multiaddr.isMultiaddr(m)).to.eql(true))
defer.resolve() defer.resolve()
}) })

View File

@ -1,10 +0,0 @@
export = PeerDiscovery;
import events from 'events';
declare class PeerDiscovery extends events.EventEmitter {
constructor (options: Object);
start (): Promise<void>;
stop (): Promise<void>;
tag: string;
}

View File

@ -1,10 +0,0 @@
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[] }>;
}

View File

@ -1,16 +1,16 @@
'use strict' 'use strict'
const debug = require('debug') const debug = require('debug')
/** @typedef {import('../types').EventEmitterFactory} Events */ const { EventEmitter } = require('events')
/** @type Events */
const EventEmitter = require('events')
const errcode = require('err-code') const errcode = require('err-code')
const { pipe } = require('it-pipe') 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')
@ -27,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/types').RPC} RPC * @typedef {import('./message').RPC} RPC
* @typedef {import('./message/types').SubOpts} RPCSubOpts * @typedef {import('./message').SubOpts} RPCSubOpts
* @typedef {import('./message/types').Message} RPCMessage * @typedef {import('./message').Message} RPCMessage
* @typedef {import('./signature-policy').SignaturePolicyType} SignaturePolicyType * @typedef {import('./signature-policy').SignaturePolicyType} SignaturePolicyType
*/ */
@ -42,16 +42,6 @@ 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
*/ */
/** /**
@ -60,7 +50,13 @@ const {
*/ */
class PubsubBaseProtocol extends EventEmitter { class PubsubBaseProtocol extends EventEmitter {
/** /**
* @param {PubsubProperties & PubsubOptions} props * @param {Object} 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 ({
@ -85,9 +81,8 @@ class PubsubBaseProtocol extends EventEmitter {
super() super()
this.log = Object.assign(debug(debugName), { this.log = debug(debugName)
err: debug(`${debugName}:error`) this.log.err = debug(`${debugName}:error`)
})
/** /**
* @type {Array<string>} * @type {Array<string>}
@ -125,7 +120,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_SIGNATURE_POLICY) throw errcode(new Error('Invalid global signature policy'), codes.ERR_INVALID_SIGUATURE_POLICY)
} }
/** /**
@ -382,9 +377,7 @@ class PubsubBaseProtocol extends EventEmitter {
if (subs.length) { if (subs.length) {
// update peer subscriptions // update peer subscriptions
subs.forEach((/** @type {RPCSubOpts} */ subOpt) => { subs.forEach((subOpt) => this._processRpcSubOpt(idB58Str, subOpt))
this._processRpcSubOpt(idB58Str, subOpt)
})
this.emit('pubsub:subscription-change', peerStreams.id, subs) this.emit('pubsub:subscription-change', peerStreams.id, subs)
} }
@ -394,9 +387,8 @@ class PubsubBaseProtocol extends EventEmitter {
} }
if (msgs.length) { if (msgs.length) {
// @ts-ignore RPC message is modified msgs.forEach(message => {
msgs.forEach((message) => { if (!(this.canRelayMessage || message.topicIDs.some((topic) => this.subscriptions.has(topic)))) {
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
} }
@ -595,7 +587,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 // eslint-disable-line continue
} }
await validatorFn(topic, message) await validatorFn(topic, message)
} }
@ -652,7 +644,7 @@ class PubsubBaseProtocol extends EventEmitter {
* *
* @override * @override
* @param {string} topic * @param {string} topic
* @param {Uint8Array} message * @param {Buffer} message
* @returns {Promise<void>} * @returns {Promise<void>}
*/ */
async publish (topic, message) { async publish (topic, message) {

View File

@ -1,16 +1,17 @@
'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 = { /**
rpc: rpcProto, * @module pubsub/message/index
td: topicDescriptorProto, */
RPC, exports = module.exports
Message: RPC.Message, exports.rpc = rpcProto
SubOpts: RPC.SubOpts exports.td = topicDescriptorProto
} exports.RPC = RPC
exports.Message = RPC.Message
exports.SubOpts = RPC.SubOpts

View File

@ -1,5 +0,0 @@
import { RPC, Message, SubOpts } from './types'
export type RPC = RPC
export type Message = Message
export type SubOpts = SubOpts

View File

@ -1,20 +1,19 @@
'use strict' 'use strict'
const debug = require('debug') const { EventEmitter } = require('events')
const log = Object.assign(debug('libp2p-pubsub:peer-streams'), {
error: debug('libp2p-pubsub:peer-streams:err')
})
/** @typedef {import('../types').EventEmitterFactory} Events */
/** @type 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
@ -169,7 +168,7 @@ class PeerStreams extends EventEmitter {
this.outboundStream, this.outboundStream,
lp.encode(), lp.encode(),
this._rawOutboundStream this._rawOutboundStream
).catch(/** @param {Error} err */ err => { ).catch(err => {
log.error(err) log.error(err)
}) })

View File

@ -1,4 +1,3 @@
// @ts-nocheck interface tests
/* eslint-env mocha */ /* eslint-env mocha */
'use strict' 'use strict'

View File

@ -1,4 +1,3 @@
// @ts-nocheck interface tests
/* eslint-env mocha */ /* eslint-env mocha */
'use strict' 'use strict'

View File

@ -1,4 +1,3 @@
// @ts-nocheck interface tests
/* eslint-env mocha */ /* eslint-env mocha */
'use strict' 'use strict'

View File

@ -1,4 +1,3 @@
// @ts-nocheck interface tests
/* eslint-env mocha */ /* eslint-env mocha */
'use strict' 'use strict'

View File

@ -1,4 +1,3 @@
// @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'

View File

@ -1,4 +1,3 @@
// @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'

View File

@ -1,4 +1,3 @@
// @ts-nocheck interface tests
'use strict' 'use strict'
const { expect } = require('chai') const { expect } = require('chai')

View File

@ -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 @@ const multihash = require('multihashes')
* @returns {Uint8Array} * @returns {Uint8Array}
* @private * @private
*/ */
const randomSeqno = () => { exports.randomSeqno = () => {
return randomBytes(8) return randomBytes(8)
} }
@ -25,7 +25,7 @@ const randomSeqno = () => {
* @returns {Uint8Array} * @returns {Uint8Array}
* @private * @private
*/ */
const msgId = (from, seqno) => { exports.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,28 +40,22 @@ const msgId = (from, seqno) => {
* @returns {Uint8Array} * @returns {Uint8Array}
* @private * @private
*/ */
const noSignMsgId = (data) => multihash.encode(data, 'sha2-256') exports.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<number>|Array<number>} a * @param {Set|Array} a
* @param {Set<number>|Array<number>} b * @param {Set|Array} b
* @returns {boolean} * @returns {boolean}
* @private * @private
*/ */
const anyMatch = (a, b) => { exports.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)
} }
@ -82,7 +76,7 @@ const anyMatch = (a, b) => {
* @returns {T[]} * @returns {T[]}
* @private * @private
*/ */
const ensureArray = (maybeArray) => { exports.ensureArray = (maybeArray) => {
if (!Array.isArray(maybeArray)) { if (!Array.isArray(maybeArray)) {
return [maybeArray] return [maybeArray]
} }
@ -98,7 +92,7 @@ const ensureArray = (maybeArray) => {
* @param {string} [peerId] * @param {string} [peerId]
* @returns {T & {from?: string, peerId?: string }} * @returns {T & {from?: string, peerId?: string }}
*/ */
const normalizeInRpcMessage = (message, peerId) => { exports.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')
@ -115,23 +109,13 @@ const normalizeInRpcMessage = (message, peerId) => {
* @param {T} message * @param {T} message
* @returns {T & {from?: Uint8Array, data?: Uint8Array}} * @returns {T & {from?: Uint8Array, data?: Uint8Array}}
*/ */
const normalizeOutRpcMessage = (message) => { exports.normalizeOutRpcMessage = (message) => {
const m = Object.assign({}, message) const m = Object.assign({}, message)
if (typeof message.from === 'string') { if (typeof message.from === 'string' || message.from instanceof String) {
m.from = uint8ArrayFromString(message.from, 'base58btc') m.from = uint8ArrayFromString(message.from, 'base58btc')
} }
if (typeof message.data === 'string') { if (typeof message.data === 'string' || message.data instanceof String) {
m.data = uint8ArrayFromString(message.data) m.data = uint8ArrayFromString(message.data)
} }
return m return m
} }
module.exports = {
randomSeqno,
msgId,
noSignMsgId,
anyMatch,
ensureArray,
normalizeInRpcMessage,
normalizeOutRpcMessage
}

View File

@ -1,4 +1,3 @@
// @ts-nocheck interface tests
/* eslint-env mocha */ /* eslint-env mocha */
'use strict' 'use strict'

View File

@ -1,4 +1,3 @@
// @ts-nocheck interface tests
/* eslint-env mocha */ /* eslint-env mocha */
'use strict' 'use strict'

View File

@ -1,4 +1,3 @@
// @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'

View File

@ -1,4 +1,3 @@
// @ts-nocheck interface tests
/* eslint-env mocha */ /* eslint-env mocha */
'use strict' 'use strict'

View File

@ -1,4 +1,3 @@
// @ts-nocheck interface tests
/* eslint-env mocha */ /* eslint-env mocha */
'use strict' 'use strict'

View File

@ -1,11 +1,10 @@
// @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') const pLimit = require('p-limit').default
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) => {

View File

@ -1,4 +1,3 @@
// @ts-nocheck interface tests
/* eslint-env mocha */ /* eslint-env mocha */
'use strict' 'use strict'

View File

@ -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<void>; export type Sink = (source: Uint8Array) => Promise<Uint8Array>;

View File

@ -62,9 +62,6 @@ 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
} }

View File

@ -56,9 +56,6 @@ 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)

View File

@ -1,4 +1,3 @@
// @ts-nocheck interface tests
/* eslint-env mocha */ /* eslint-env mocha */
'use strict' 'use strict'

View File

@ -1,4 +1,3 @@
// @ts-nocheck interface tests
/* eslint-env mocha */ /* eslint-env mocha */
'use strict' 'use strict'

View File

@ -1,4 +1,3 @@
// @ts-nocheck interface tests
/* eslint-env mocha */ /* eslint-env mocha */
'use strict' 'use strict'

View File

@ -1,4 +1,3 @@
// @ts-nocheck interface tests
/* eslint-env mocha */ /* eslint-env mocha */
'use strict' 'use strict'

View File

@ -1,4 +1,3 @@
// @ts-nocheck interface tests
/* eslint-env mocha */ /* eslint-env mocha */
'use strict' 'use strict'

View File

@ -1,4 +1,3 @@
// @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'

View File

@ -1,6 +1,5 @@
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'
@ -63,7 +62,7 @@ export type MultiaddrConnectionTimeline = {
export type MultiaddrConnection = { export type MultiaddrConnection = {
sink: Sink; sink: Sink;
source: AsyncIterable<Uint8Array | BufferList>; source: () => AsyncIterable<Uint8Array>;
close: (err?: Error) => Promise<void>; close: (err?: Error) => Promise<void>;
conn: unknown; conn: unknown;
remoteAddr: Multiaddr; remoteAddr: Multiaddr;

18
src/types.d.ts vendored
View File

@ -1,18 +0,0 @@
export interface EventEmitterFactory {
new(): EventEmitter;
}
export interface EventEmitter {
addListener(event: string | symbol, listener: (...args: any[]) => void): EventEmitter;
on(event: string | symbol, listener: (...args: any[]) => void): EventEmitter;
once(event: string | symbol, listener: (...args: any[]) => void): EventEmitter;
removeListener(event: string | symbol, listener: (...args: any[]) => void): EventEmitter;
off(event: string | symbol, listener: (...args: any[]) => void): EventEmitter;
removeAllListeners(event?: string | symbol): EventEmitter;
setMaxListeners(n: number): EventEmitter;
getMaxListeners(): number;
listeners(event: string | symbol): Function[]; // eslint-disable-line @typescript-eslint/ban-types
rawListeners(event: string | symbol): Function[]; // eslint-disable-line @typescript-eslint/ban-types
emit(event: string | symbol, ...args: any[]): boolean;
listenerCount(event: string | symbol): number;
}

View File

@ -17,8 +17,8 @@ describe('compliance tests', () => {
* @param {*} properties * @param {*} properties
*/ */
async setup (properties) { async setup (properties) {
const localAddr = new multiaddr.Multiaddr('/ip4/127.0.0.1/tcp/8080') const localAddr = multiaddr('/ip4/127.0.0.1/tcp/8080')
const remoteAddr = new multiaddr.Multiaddr('/ip4/127.0.0.1/tcp/8081') const remoteAddr = 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])

View File

@ -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: [new multiaddr.Multiaddr('/ip4/127.0.0.1/tcp/8000')] multiaddrs: [multiaddr('/ip4/127.0.0.1/tcp/8000')]
}) })
}, this.options.discoveryDelay || 1000) }, this.options.discoveryDelay || 1000)
} }