Skip to content
This repository was archived by the owner on Jun 26, 2023. It is now read-only.

Commit 8a6d3d0

Browse files
authored
chore: update pubsub interface, multiaddr and remove protons (#89)
1 parent edb8ca6 commit 8a6d3d0

File tree

23 files changed

+1793
-103
lines changed

23 files changed

+1793
-103
lines changed

.github/workflows/main.yml

+7-7
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ jobs:
1212
runs-on: ubuntu-latest
1313
steps:
1414
- uses: actions/checkout@v2
15-
- run: yarn
16-
- run: yarn lint
15+
- run: npm install
16+
- run: npx aegir lint
1717
- uses: gozala/[email protected]
18-
- run: yarn build
19-
- run: yarn aegir dep-check
18+
- run: npx aegir build
19+
- run: npx aegir dep-check
2020
- uses: ipfs/aegir/actions/bundle-size@master
2121
name: size
2222
with:
@@ -34,20 +34,20 @@ jobs:
3434
- uses: actions/setup-node@v1
3535
with:
3636
node-version: ${{ matrix.node }}
37-
- run: yarn
37+
- run: npm install
3838
- run: npx nyc --reporter=lcov aegir test -t node -- --bail
3939
- uses: codecov/codecov-action@v1
4040
test-chrome:
4141
needs: check
4242
runs-on: ubuntu-latest
4343
steps:
4444
- uses: actions/checkout@v2
45-
- run: yarn
45+
- run: npm install
4646
- run: npx aegir test -t browser -t webworker --bail
4747
test-firefox:
4848
needs: check
4949
runs-on: ubuntu-latest
5050
steps:
5151
- uses: actions/checkout@v2
52-
- run: yarn
52+
- run: npm install
5353
- run: npx aegir test -t browser -t webworker --bail -- --browsers FirefoxHeadless

package.json

+16-9
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,14 @@
2424
"scripts": {
2525
"prepare": "aegir build --no-bundle",
2626
"lint": "aegir lint",
27-
"build": "aegir build",
27+
"build": "npm run build:proto && npm run build:proto-types && npm run build:types",
28+
"build:types": "aegir build --no-bundle",
29+
"build:proto": "npm run build:proto:rpc && npm run build:proto:topic-descriptor",
30+
"build:proto:rpc": "pbjs -t static-module -w commonjs --force-number --no-verify --no-delimited --no-create --no-beautify --no-defaults --lint eslint-disable -o src/pubsub/message/rpc.js ./src/pubsub/message/rpc.proto",
31+
"build:proto:topic-descriptor": "pbjs -t static-module -w commonjs --force-number --no-verify --no-delimited --no-create --no-beautify --no-defaults --lint eslint-disable -o src/pubsub/message/topic-descriptor.js ./src/pubsub/message/topic-descriptor.proto",
32+
"build:proto-types": "npm run build:proto-types:rpc && npm run build:proto-types:topic-descriptor",
33+
"build:proto-types:rpc": "pbts -o src/pubsub/message/rpc.d.ts src/pubsub/message/rpc.js",
34+
"build:proto-types:topic-descriptor": "pbts -o src/pubsub/message/topic-descriptor.d.ts src/pubsub/message/topic-descriptor.js",
2835
"test": "aegir test",
2936
"test:node": "aegir test --target node",
3037
"test:browser": "aegir test --target browser",
@@ -47,7 +54,7 @@
4754
},
4855
"homepage": "https://github.com/libp2p/js-interfaces#readme",
4956
"dependencies": {
50-
"@types/bl": "4.1.0",
57+
"@types/bl": "^4.1.0",
5158
"abort-controller": "^3.0.0",
5259
"abortable-iterator": "^3.0.0",
5360
"chai": "^4.3.4",
@@ -57,31 +64,31 @@
5764
"detect-node": "^2.0.4",
5865
"dirty-chai": "^2.0.1",
5966
"err-code": "^3.0.1",
60-
"it-goodbye": "^2.0.2",
61-
"it-length-prefixed": "^3.1.0",
67+
"it-goodbye": "^3.0.0",
68+
"it-length-prefixed": "^5.0.2",
6269
"it-pair": "^1.0.0",
6370
"it-pipe": "^1.1.0",
6471
"it-pushable": "^1.4.2",
6572
"libp2p-crypto": "^0.19.0",
66-
"libp2p-tcp": "^0.15.0",
67-
"multiaddr": "^8.1.2",
73+
"libp2p-tcp": "^0.15.3",
74+
"multiaddr": "^9.0.1",
6875
"multibase": "^4.0.2",
6976
"multihashes": "^4.0.2",
7077
"p-defer": "^3.0.0",
7178
"p-limit": "^3.1.0",
7279
"p-wait-for": "^3.2.0",
7380
"peer-id": "^0.14.2",
74-
"protons": "^2.0.0",
81+
"protobufjs": "^6.10.2",
7582
"sinon": "^10.0.0",
7683
"streaming-iterables": "^5.0.4",
7784
"uint8arrays": "^2.1.3"
7885
},
7986
"devDependencies": {
8087
"@types/debug": "^4.1.5",
81-
"aegir": "^32.1.0",
88+
"aegir": "^33.0.0",
8289
"cids": "^1.1.6",
8390
"events": "^3.3.0",
84-
"it-handshake": "^1.0.2",
91+
"it-handshake": "^2.0.0",
8592
"rimraf": "^3.0.2",
8693
"util": "^0.12.3"
8794
},

src/connection/connection.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict'
22

33
const PeerId = require('peer-id')
4-
const multiaddr = require('multiaddr')
4+
const { Multiaddr } = require('multiaddr')
55
const errCode = require('err-code')
66
const { OPEN, CLOSING, CLOSED } = require('./status')
77

@@ -25,8 +25,8 @@ const connectionSymbol = Symbol.for('@libp2p/interface-connection/connection')
2525
* @property {string} [encryption] - connection encryption method identifier.
2626
*
2727
* @typedef {Object} ConnectionOptions
28-
* @property {multiaddr} [localAddr] - local multiaddr of the connection if known.
29-
* @property {multiaddr} remoteAddr - remote multiaddr of the connection.
28+
* @property {Multiaddr} [localAddr] - local multiaddr of the connection if known.
29+
* @property {Multiaddr} remoteAddr - remote multiaddr of the connection.
3030
* @property {PeerId} localPeer - local peer-id.
3131
* @property {PeerId} remotePeer - remote peer-id.
3232
* @property {(protocols: string|string[]) => Promise<{stream: MuxedStream, protocol: string}>} newStream - new stream muxer function.
@@ -231,7 +231,7 @@ class Connection {
231231
module.exports = Connection
232232

233233
/**
234-
* @param {multiaddr|undefined} localAddr
234+
* @param {Multiaddr|undefined} localAddr
235235
* @param {PeerId} localPeer
236236
* @param {PeerId} remotePeer
237237
* @param {(protocols: string | string[]) => Promise<{ stream: import("../stream-muxer/types").MuxedStream; protocol: string; }>} newStream
@@ -240,7 +240,7 @@ module.exports = Connection
240240
* @param {{ direction: any; timeline: any; multiplexer?: string | undefined; encryption?: string | undefined; }} stat
241241
*/
242242
function validateArgs (localAddr, localPeer, remotePeer, newStream, close, getStreams, stat) {
243-
if (localAddr && !multiaddr.isMultiaddr(localAddr)) {
243+
if (localAddr && !Multiaddr.isMultiaddr(localAddr)) {
244244
throw errCode(new Error('localAddr must be an instance of multiaddr'), 'ERR_INVALID_PARAMETERS')
245245
}
246246

src/peer-discovery/tests/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const chai = require('chai')
66
const expect = chai.expect
77
chai.use(require('dirty-chai'))
88

9-
const multiaddr = require('multiaddr')
9+
const { Multiaddr } = require('multiaddr')
1010
const PeerId = require('peer-id')
1111

1212
const delay = require('delay')
@@ -55,7 +55,7 @@ module.exports = (common) => {
5555
expect(PeerId.isPeerId(id)).to.eql(true)
5656
expect(multiaddrs).to.exist()
5757

58-
multiaddrs.forEach((m) => expect(multiaddr.isMultiaddr(m)).to.eql(true))
58+
multiaddrs.forEach((m) => expect(Multiaddr.isMultiaddr(m)).to.eql(true))
5959

6060
defer.resolve()
6161
})

src/pubsub/index.js

+29-24
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const { pipe } = require('it-pipe')
1111
const MulticodecTopology = require('../topology/multicodec-topology')
1212
const { codes } = require('./errors')
1313

14-
const message = require('./message')
14+
const { RPC } = require('./message/rpc')
1515
const PeerStreams = require('./peer-streams')
1616
const { SignaturePolicy } = require('./signature-policy')
1717
const utils = require('./utils')
@@ -27,10 +27,10 @@ const {
2727
* @typedef {import('bl')} BufferList
2828
* @typedef {import('../stream-muxer/types').MuxedStream} MuxedStream
2929
* @typedef {import('../connection/connection')} Connection
30-
* @typedef {import('./message/types').RPC} RPC
31-
* @typedef {import('./message/types').SubOpts} RPCSubOpts
32-
* @typedef {import('./message/types').Message} RPCMessage
3330
* @typedef {import('./signature-policy').SignaturePolicyType} SignaturePolicyType
31+
* @typedef {import('./message/rpc').IRPC} IRPC
32+
* @typedef {import('./message/rpc').RPC.SubOpts} RPCSubOpts
33+
* @typedef {import('./message/rpc').RPC.Message} RPCMessage
3434
*/
3535

3636
/**
@@ -382,7 +382,7 @@ class PubsubBaseProtocol extends EventEmitter {
382382

383383
if (subs.length) {
384384
// update peer subscriptions
385-
subs.forEach((/** @type {RPCSubOpts} */ subOpt) => {
385+
subs.forEach((subOpt) => {
386386
this._processRpcSubOpt(idB58Str, subOpt)
387387
})
388388
this.emit('pubsub:subscription-change', peerStreams.id, subs)
@@ -396,7 +396,7 @@ class PubsubBaseProtocol extends EventEmitter {
396396
if (msgs.length) {
397397
// @ts-ignore RPC message is modified
398398
msgs.forEach((message) => {
399-
if (!(this.canRelayMessage || message.topicIDs.some((/** @type {string} */ topic) => this.subscriptions.has(topic)))) {
399+
if (!(this.canRelayMessage || (message.topicIDs && message.topicIDs.some((topic) => this.subscriptions.has(topic))))) {
400400
this.log('received message we didn\'t subscribe to. Dropping.')
401401
return
402402
}
@@ -411,11 +411,15 @@ class PubsubBaseProtocol extends EventEmitter {
411411
* Handles a subscription change from a peer
412412
*
413413
* @param {string} id
414-
* @param {RPCSubOpts} subOpt
414+
* @param {RPC.ISubOpts} subOpt
415415
*/
416416
_processRpcSubOpt (id, subOpt) {
417417
const t = subOpt.topicID
418418

419+
if (!t) {
420+
return
421+
}
422+
419423
let topicSet = this.topics.get(t)
420424
if (!topicSet) {
421425
topicSet = new Set()
@@ -473,13 +477,14 @@ class PubsubBaseProtocol extends EventEmitter {
473477
* The default msgID implementation
474478
* Child class can override this.
475479
*
476-
* @param {RPCMessage} msg - the message object
480+
* @param {InMessage} msg - the message object
477481
* @returns {Uint8Array} message id as bytes
478482
*/
479483
getMsgId (msg) {
480484
const signaturePolicy = this.globalSignaturePolicy
481485
switch (signaturePolicy) {
482486
case SignaturePolicy.StrictSign:
487+
// @ts-ignore seqno is optional in protobuf definition but it will exist
483488
return utils.msgId(msg.from, msg.seqno)
484489
case SignaturePolicy.StrictNoSign:
485490
return utils.noSignMsgId(msg.data)
@@ -508,25 +513,25 @@ class PubsubBaseProtocol extends EventEmitter {
508513
* @returns {RPC}
509514
*/
510515
_decodeRpc (bytes) {
511-
return message.rpc.RPC.decode(bytes)
516+
return RPC.decode(bytes)
512517
}
513518

514519
/**
515520
* Encode RPC object into a Uint8Array.
516521
* This can be override to use a custom router protobuf.
517522
*
518-
* @param {RPC} rpc
523+
* @param {IRPC} rpc
519524
* @returns {Uint8Array}
520525
*/
521526
_encodeRpc (rpc) {
522-
return message.rpc.RPC.encode(rpc)
527+
return RPC.encode(rpc).finish()
523528
}
524529

525530
/**
526531
* Send an rpc object to a peer
527532
*
528533
* @param {string} id - peer id
529-
* @param {RPC} rpc
534+
* @param {IRPC} rpc
530535
* @returns {void}
531536
*/
532537
_sendRpc (id, rpc) {
@@ -592,12 +597,12 @@ class PubsubBaseProtocol extends EventEmitter {
592597
default:
593598
throw errcode(new Error('Cannot validate message: unhandled signature policy: ' + signaturePolicy), codes.ERR_UNHANDLED_SIGNATURE_POLICY)
594599
}
600+
595601
for (const topic of message.topicIDs) {
596602
const validatorFn = this.topicValidators.get(topic)
597-
if (!validatorFn) {
598-
continue // eslint-disable-line
603+
if (validatorFn) {
604+
await validatorFn(topic, message)
599605
}
600-
await validatorFn(topic, message)
601606
}
602607
}
603608

@@ -606,8 +611,8 @@ class PubsubBaseProtocol extends EventEmitter {
606611
* Should be used by the routers to create the message to send.
607612
*
608613
* @protected
609-
* @param {RPCMessage} message
610-
* @returns {Promise<RPCMessage>}
614+
* @param {InMessage} message
615+
* @returns {Promise<InMessage>}
611616
*/
612617
_buildMessage (message) {
613618
const signaturePolicy = this.globalSignaturePolicy
@@ -617,7 +622,7 @@ class PubsubBaseProtocol extends EventEmitter {
617622
message.seqno = utils.randomSeqno()
618623
return signMessage(this.peerId, utils.normalizeOutRpcMessage(message))
619624
case SignaturePolicy.StrictNoSign:
620-
return message
625+
return Promise.resolve(message)
621626
default:
622627
throw errcode(new Error('Cannot build message: unhandled signature policy: ' + signaturePolicy), codes.ERR_UNHANDLED_SIGNATURE_POLICY)
623628
}
@@ -663,29 +668,30 @@ class PubsubBaseProtocol extends EventEmitter {
663668
this.log('publish', topic, message)
664669

665670
const from = this.peerId.toB58String()
666-
let msgObject = {
671+
const msgObject = {
667672
receivedFrom: from,
668673
data: message,
669674
topicIDs: [topic]
670675
}
671676

672677
// ensure that the message follows the signature policy
673678
const outMsg = await this._buildMessage(msgObject)
674-
msgObject = utils.normalizeInRpcMessage(outMsg)
679+
// @ts-ignore different type as from is converted
680+
const msg = utils.normalizeInRpcMessage(outMsg)
675681

676682
// Emit to self if I'm interested and emitSelf enabled
677-
this.emitSelf && this._emitMessage(msgObject)
683+
this.emitSelf && this._emitMessage(msg)
678684

679685
// send to all the other peers
680-
await this._publish(msgObject)
686+
await this._publish(msg)
681687
}
682688

683689
/**
684690
* Overriding the implementation of publish should handle the appropriate algorithms for the publish/subscriber implementation.
685691
* For example, a Floodsub implementation might simply publish each message to each topic for every peer
686692
*
687693
* @abstract
688-
* @param {InMessage} message
694+
* @param {InMessage|RPCMessage} message
689695
* @returns {Promise<void>}
690696
*
691697
*/
@@ -744,7 +750,6 @@ class PubsubBaseProtocol extends EventEmitter {
744750
}
745751
}
746752

747-
PubsubBaseProtocol.message = message
748753
PubsubBaseProtocol.utils = utils
749754
PubsubBaseProtocol.SignaturePolicy = SignaturePolicy
750755

src/pubsub/message/index.js

-16
This file was deleted.

0 commit comments

Comments
 (0)