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

Commit 97bebee

Browse files
committed
chore: update types
1 parent b948972 commit 97bebee

39 files changed

+136
-72
lines changed

.github/workflows/main.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ jobs:
2727
strategy:
2828
matrix:
2929
os: [windows-latest, ubuntu-latest, macos-latest]
30-
node: [12, 14]
30+
node: [14, 15]
3131
fail-fast: true
3232
steps:
3333
- uses: actions/checkout@v2

package.json

+15-12
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
"extends": "ipfs"
2323
},
2424
"scripts": {
25+
"prepare": "aegir build --no-bundle",
2526
"lint": "aegir lint",
2627
"build": "aegir build",
27-
"prepare": "aegir build --no-bundle",
2828
"test": "aegir test",
2929
"test:node": "aegir test --target node",
3030
"test:browser": "aegir test --target browser",
@@ -47,39 +47,42 @@
4747
},
4848
"homepage": "https://github.com/libp2p/js-interfaces#readme",
4949
"dependencies": {
50-
"@types/bl": "^2.1.0",
50+
"@types/bl": "4.1.0",
5151
"abort-controller": "^3.0.0",
5252
"abortable-iterator": "^3.0.0",
53-
"chai": "^4.2.0",
53+
"chai": "^4.3.4",
5454
"chai-checkmark": "^1.0.1",
5555
"debug": "^4.3.1",
56-
"delay": "^4.4.0",
56+
"delay": "^5.0.0",
5757
"detect-node": "^2.0.4",
5858
"dirty-chai": "^2.0.1",
59-
"err-code": "^2.0.3",
59+
"err-code": "^3.0.1",
6060
"it-goodbye": "^2.0.2",
6161
"it-length-prefixed": "^3.1.0",
6262
"it-pair": "^1.0.0",
6363
"it-pipe": "^1.1.0",
64-
"it-pushable": "^1.4.0",
64+
"it-pushable": "^1.4.2",
6565
"libp2p-crypto": "^0.19.0",
6666
"libp2p-tcp": "^0.15.0",
6767
"multiaddr": "^8.1.2",
68-
"multibase": "^3.1.1",
69-
"multihashes": "^3.1.1",
68+
"multibase": "^4.0.2",
69+
"multihashes": "^4.0.2",
7070
"p-defer": "^3.0.0",
7171
"p-limit": "^3.1.0",
7272
"p-wait-for": "^3.2.0",
7373
"peer-id": "^0.14.2",
7474
"protons": "^2.0.0",
75-
"sinon": "^9.2.4",
75+
"sinon": "^10.0.0",
7676
"streaming-iterables": "^5.0.4",
77-
"uint8arrays": "^2.0.5"
77+
"uint8arrays": "^2.1.3"
7878
},
7979
"devDependencies": {
80-
"aegir": "^29.2.0",
80+
"@types/debug": "^4.1.5",
81+
"aegir": "^32.1.0",
82+
"events": "^3.3.0",
8183
"it-handshake": "^1.0.2",
82-
"rimraf": "^3.0.2"
84+
"rimraf": "^3.0.2",
85+
"util": "^0.12.3"
8386
},
8487
"contributors": [
8588
"Alan Shaw <[email protected]>",

src/connection/connection.js

+9
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,15 @@ class Connection {
230230

231231
module.exports = Connection
232232

233+
/**
234+
* @param {multiaddr|undefined} localAddr
235+
* @param {PeerId} localPeer
236+
* @param {PeerId} remotePeer
237+
* @param {(protocols: string | string[]) => Promise<{ stream: import("../stream-muxer/types").MuxedStream; protocol: string; }>} newStream
238+
* @param {() => Promise<void>} close
239+
* @param {() => import("../stream-muxer/types").MuxedStream[]} getStreams
240+
* @param {{ direction: any; timeline: any; multiplexer?: string | undefined; encryption?: string | undefined; }} stat
241+
*/
233242
function validateArgs (localAddr, localPeer, remotePeer, newStream, close, getStreams, stat) {
234243
if (localAddr && !multiaddr.isMultiaddr(localAddr)) {
235244
throw errCode(new Error('localAddr must be an instance of multiaddr'), 'ERR_INVALID_PARAMETERS')

src/connection/tests/connection.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// @ts-nocheck
12
/* eslint-env mocha */
23

34
'use strict'

src/connection/tests/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// @ts-nocheck
12
/* eslint-env mocha */
23

34
'use strict'
File renamed without changes.

src/crypto/tests/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// @ts-nocheck
12
/* eslint-env mocha */
23
'use strict'
34

File renamed without changes.

src/peer-discovery/tests/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// @ts-nocheck
12
/* eslint-env mocha */
23
'use strict'
34

src/pubsub/index.js

+34-28
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,8 @@ const { pipe } = require('it-pipe')
1010

1111
const MulticodecTopology = require('../topology/multicodec-topology')
1212
const { codes } = require('./errors')
13-
/**
14-
* @type {typeof import('./message')}
15-
*/
16-
const message = require('./message')
13+
14+
const { rpc, RPC, Message, SubOpts } = require('./message') // eslint-disable-line no-unused-vars
1715
const PeerStreams = require('./peer-streams')
1816
const { SignaturePolicy } = require('./signature-policy')
1917
const utils = require('./utils')
@@ -29,9 +27,9 @@ const {
2927
* @typedef {import('bl')} BufferList
3028
* @typedef {import('../stream-muxer/types').MuxedStream} MuxedStream
3129
* @typedef {import('../connection/connection')} Connection
32-
* @typedef {import('./message').RPC} RPC
33-
* @typedef {import('./message').SubOpts} RPCSubOpts
34-
* @typedef {import('./message').Message} RPCMessage
30+
* @typedef {RPC} RPCM
31+
* @typedef {SubOpts} RPCSubOpts
32+
* @typedef {Message} RPCMessage
3533
* @typedef {import('./signature-policy').SignaturePolicyType} SignaturePolicyType
3634
*/
3735

@@ -44,6 +42,16 @@ const {
4442
* @property {Uint8Array} data
4543
* @property {Uint8Array} [signature]
4644
* @property {Uint8Array} [key]
45+
*
46+
* @typedef {Object} PubsubProperties
47+
* @property {string} debugName - log namespace
48+
* @property {Array<string>|string} multicodecs - protocol identificers to connect
49+
* @property {Libp2p} libp2p
50+
*
51+
* @typedef {Object} PubsubOptions
52+
* @property {SignaturePolicyType} [globalSignaturePolicy = SignaturePolicy.StrictSign] - defines how signatures should be handled
53+
* @property {boolean} [canRelayMessage = false] - if can relay messages not subscribed
54+
* @property {boolean} [emitSelf = false] - if publish should emit to self, if subscribed
4755
*/
4856

4957
/**
@@ -52,13 +60,7 @@ const {
5260
*/
5361
class PubsubBaseProtocol extends EventEmitter {
5462
/**
55-
* @param {Object} props
56-
* @param {string} props.debugName - log namespace
57-
* @param {Array<string>|string} props.multicodecs - protocol identificers to connect
58-
* @param {Libp2p} props.libp2p
59-
* @param {SignaturePolicyType} [props.globalSignaturePolicy = SignaturePolicy.StrictSign] - defines how signatures should be handled
60-
* @param {boolean} [props.canRelayMessage = false] - if can relay messages not subscribed
61-
* @param {boolean} [props.emitSelf = false] - if publish should emit to self, if subscribed
63+
* @param {PubsubProperties & PubsubOptions} props
6264
* @abstract
6365
*/
6466
constructor ({
@@ -83,8 +85,9 @@ class PubsubBaseProtocol extends EventEmitter {
8385

8486
super()
8587

86-
this.log = debug(debugName)
87-
this.log.err = debug(`${debugName}:error`)
88+
this.log = Object.assign(debug(debugName), {
89+
err: debug(`${debugName}:error`)
90+
})
8891

8992
/**
9093
* @type {Array<string>}
@@ -122,7 +125,7 @@ class PubsubBaseProtocol extends EventEmitter {
122125

123126
// validate signature policy
124127
if (!SignaturePolicy[globalSignaturePolicy]) {
125-
throw errcode(new Error('Invalid global signature policy'), codes.ERR_INVALID_SIGUATURE_POLICY)
128+
throw errcode(new Error('Invalid global signature policy'), codes.ERR_INVALID_SIGNATURE_POLICY)
126129
}
127130

128131
/**
@@ -369,7 +372,7 @@ class PubsubBaseProtocol extends EventEmitter {
369372
*
370373
* @param {string} idB58Str
371374
* @param {PeerStreams} peerStreams
372-
* @param {RPC} rpc
375+
* @param {RPCM} rpc
373376
* @returns {boolean}
374377
*/
375378
_processRpc (idB58Str, peerStreams, rpc) {
@@ -379,7 +382,9 @@ class PubsubBaseProtocol extends EventEmitter {
379382

380383
if (subs.length) {
381384
// update peer subscriptions
382-
subs.forEach((subOpt) => this._processRpcSubOpt(idB58Str, subOpt))
385+
subs.forEach((/** @type {RPCSubOpts} */ subOpt) => {
386+
this._processRpcSubOpt(idB58Str, subOpt)
387+
})
383388
this.emit('pubsub:subscription-change', peerStreams.id, subs)
384389
}
385390

@@ -389,8 +394,9 @@ class PubsubBaseProtocol extends EventEmitter {
389394
}
390395

391396
if (msgs.length) {
392-
msgs.forEach(message => {
393-
if (!(this.canRelayMessage || message.topicIDs.some((topic) => this.subscriptions.has(topic)))) {
397+
// @ts-ignore RPC message
398+
msgs.forEach((message) => {
399+
if (!(this.canRelayMessage || message.topicIDs.some((/** @type {string} */ topic) => this.subscriptions.has(topic)))) {
394400
this.log('received message we didn\'t subscribe to. Dropping.')
395401
return
396402
}
@@ -499,28 +505,28 @@ class PubsubBaseProtocol extends EventEmitter {
499505
* This can be override to use a custom router protobuf.
500506
*
501507
* @param {Uint8Array} bytes
502-
* @returns {RPC}
508+
* @returns {RPCM}
503509
*/
504510
_decodeRpc (bytes) {
505-
return message.rpc.RPC.decode(bytes)
511+
return rpc.RPC.decode(bytes)
506512
}
507513

508514
/**
509515
* Encode RPC object into a Uint8Array.
510516
* This can be override to use a custom router protobuf.
511517
*
512-
* @param {RPC} rpc
518+
* @param {RPCM} rpc
513519
* @returns {Uint8Array}
514520
*/
515521
_encodeRpc (rpc) {
516-
return message.rpc.RPC.encode(rpc)
522+
return rpc.RPC.encode(rpc)
517523
}
518524

519525
/**
520526
* Send an rpc object to a peer
521527
*
522528
* @param {string} id - peer id
523-
* @param {RPC} rpc
529+
* @param {RPCM} rpc
524530
* @returns {void}
525531
*/
526532
_sendRpc (id, rpc) {
@@ -589,7 +595,7 @@ class PubsubBaseProtocol extends EventEmitter {
589595
for (const topic of message.topicIDs) {
590596
const validatorFn = this.topicValidators.get(topic)
591597
if (!validatorFn) {
592-
continue
598+
continue // eslint-disable-line
593599
}
594600
await validatorFn(topic, message)
595601
}
@@ -738,7 +744,7 @@ class PubsubBaseProtocol extends EventEmitter {
738744
}
739745
}
740746

741-
PubsubBaseProtocol.message = message
747+
PubsubBaseProtocol.message = { rpc }
742748
PubsubBaseProtocol.utils = utils
743749
PubsubBaseProtocol.SignaturePolicy = SignaturePolicy
744750

src/pubsub/message/index.js

+8-9
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// @ts-nocheck
12
'use strict'
23

34
const protons = require('protons')
@@ -6,12 +7,10 @@ const rpcProto = protons(require('./rpc.proto.js'))
67
const RPC = rpcProto.RPC
78
const topicDescriptorProto = protons(require('./topic-descriptor.proto.js'))
89

9-
/**
10-
* @module pubsub/message/index
11-
*/
12-
exports = module.exports
13-
exports.rpc = rpcProto
14-
exports.td = topicDescriptorProto
15-
exports.RPC = RPC
16-
exports.Message = RPC.Message
17-
exports.SubOpts = RPC.SubOpts
10+
module.exports = {
11+
rpc: rpcProto,
12+
td: topicDescriptorProto,
13+
RPC,
14+
Message: RPC.Message,
15+
SubOpts: RPC.SubOpts
16+
}

src/pubsub/peer-streams.js

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

3+
const debug = require('debug')
4+
const log = Object.assign(debug('libp2p-pubsub:peer-streams'), {
5+
error: debug('libp2p-pubsub:peer-streams:err')
6+
})
7+
38
/** @typedef {import('../types').EventEmitterFactory} Events */
49
/** @type Events */
510
const EventEmitter = require('events')
@@ -10,10 +15,6 @@ const pushable = require('it-pushable')
1015
const { pipe } = require('it-pipe')
1116
const { source: abortable } = require('abortable-iterator')
1217
const AbortController = require('abort-controller').default
13-
const debug = require('debug')
14-
15-
const log = debug('libp2p-pubsub:peer-streams')
16-
log.error = debug('libp2p-pubsub:peer-streams:error')
1718

1819
/**
1920
* @typedef {import('../stream-muxer/types').MuxedStream} MuxedStream
@@ -168,7 +169,7 @@ class PeerStreams extends EventEmitter {
168169
this.outboundStream,
169170
lp.encode(),
170171
this._rawOutboundStream
171-
).catch(err => {
172+
).catch(/** @param {Error} err */ err => {
172173
log.error(err)
173174
})
174175

src/pubsub/tests/api.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// @ts-nocheck
12
/* eslint-env mocha */
23
'use strict'
34

src/pubsub/tests/emit-self.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// @ts-nocheck
12
/* eslint-env mocha */
23
'use strict'
34

src/pubsub/tests/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// @ts-nocheck
12
/* eslint-env mocha */
23
'use strict'
34

src/pubsub/tests/messages.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// @ts-nocheck
12
/* eslint-env mocha */
23
'use strict'
34

src/pubsub/tests/multiple-nodes.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// @ts-nocheck
12
/* eslint-env mocha */
23
/* eslint max-nested-callbacks: ["error", 6] */
34
'use strict'

src/pubsub/tests/two-nodes.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// @ts-nocheck
12
/* eslint-env mocha */
23
/* eslint max-nested-callbacks: ["error", 6] */
34
'use strict'

src/pubsub/tests/utils.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// @ts-nocheck
12
'use strict'
23

34
const { expect } = require('chai')

0 commit comments

Comments
 (0)