Skip to content

Commit 82c8935

Browse files
authored
chore: fix types (#321)
- Remove bignumber.js in favour of BigInt - Use ipld-block in favour of the old ipfs-core-types - Use blockstore from ipfs-repo instead of the old ipfs-core-types
1 parent de83324 commit 82c8935

17 files changed

+84
-171
lines changed

package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"src"
3232
],
3333
"scripts": {
34+
"prepare": "npm run build",
3435
"build": "npm run build:proto && npm run build:proto-types && npm run build:bundle",
3536
"build:proto": "pbjs -t static-module -w commonjs --force-number --no-verify --no-delimited --no-create --no-beautify --no-defaults --lint eslint-disable -o src/types/message/message.js src/types/message/message.proto",
3637
"build:proto-types": "pbts -o src/types/message/message.d.ts src/types/message/message.js",
@@ -70,15 +71,15 @@
7071
"assert": "^2.0.0",
7172
"benchmark": "^2.1.4",
7273
"delay": "^5.0.0",
73-
"ipfs-repo": "^9.0.0",
74+
"ipfs-repo": "^9.1.0",
7475
"ipfs-utils": "^6.0.1",
7576
"iso-random-stream": "^2.0.0",
7677
"it-all": "^1.0.5",
7778
"it-drain": "^1.0.4",
78-
"libp2p": "^0.30.9",
79+
"libp2p": "0.31.0-rc.0",
7980
"libp2p-kad-dht": "^0.21.0",
8081
"libp2p-mplex": "^0.10.2",
81-
"libp2p-secio": "^0.13.1",
82+
"libp2p-noise": "^2.0.5",
8283
"libp2p-tcp": "^0.15.3",
8384
"lodash.difference": "^4.5.0",
8485
"lodash.flatten": "^4.4.0",
@@ -97,16 +98,15 @@
9798
"dependencies": {
9899
"abort-controller": "^3.0.0",
99100
"any-signal": "^2.1.2",
100-
"bignumber.js": "^9.0.0",
101101
"cids": "^1.1.6",
102102
"debug": "^4.2.0",
103-
"ipfs-core-types": "^0.3.1",
104103
"ipld-block": "^0.11.0",
105104
"it-length-prefixed": "^5.0.2",
106105
"it-pipe": "^1.1.0",
107106
"just-debounce-it": "^1.1.0",
108-
"libp2p-interfaces": "^0.8.3",
107+
"libp2p-interfaces": "^0.10.0",
109108
"moving-average": "^1.0.1",
109+
"multiaddr": "^9.0.1",
110110
"multicodec": "^3.0.1",
111111
"multihashing-async": "^2.1.2",
112112
"native-abort-controller": "^1.0.3",

src/decision-engine/index.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
'use strict'
22

33
/**
4-
* @typedef {import('peer-id')} PeerId
5-
* @typedef {import('ipfs-core-types/src/block-service').Block} Block
4+
* @typedef {import('ipld-block')} Block
65
* @typedef {import('../types/message/entry')} BitswapMessageEntry
6+
* @typedef {import('peer-id')} PeerId
77
*/
88

99
const CID = require('cids')
@@ -34,7 +34,7 @@ const MAX_SIZE_REPLACE_HAS_WITH_BLOCK = 1024
3434
class DecisionEngine {
3535
/**
3636
* @param {PeerId} peerId
37-
* @param {import('ipfs-core-types/src/block-store').BlockStore} blockstore
37+
* @param {import('ipfs-repo').Blockstore} blockstore
3838
* @param {import('../network')} network
3939
* @param {import('../stats')} stats
4040
* @param {Object} [opts]
@@ -180,15 +180,16 @@ class DecisionEngine {
180180

181181
/**
182182
* @param {PeerId} peerId
183-
* @returns {import('ipfs-core-types/src/bitswap').LedgerForPeer|null}
184183
*/
185184
ledgerForPeer (peerId) {
186185
const peerIdStr = peerId.toB58String()
187186

188187
const ledger = this.ledgerMap.get(peerIdStr)
188+
189189
if (!ledger) {
190190
return null
191191
}
192+
192193
return {
193194
peer: ledger.partner.toPrint(),
194195
value: ledger.debtRatio(),

src/index.js

Lines changed: 9 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,7 @@ const { AbortController } = require('native-abort-controller')
1010
const { anySignal } = require('any-signal')
1111

1212
/**
13-
* @typedef {import('ipfs-core-types/src/basic').AbortOptions} AbortOptions
14-
* @typedef {import('ipfs-core-types/src/bitswap').Bitswap} API
15-
* @typedef {import('ipfs-core-types/src/bitswap').WantListEntry} WantListEntry
16-
* @typedef {import('ipfs-core-types/src/bitswap').LedgerForPeer} LedgerForPeer
17-
* @typedef {import('ipfs-core-types/src/block-service').Block} Block
13+
* @typedef {import('ipld-block')} Block
1814
* @typedef {import('peer-id')} PeerId
1915
* @typedef {import('./types/message')} BitswapMessage
2016
* @typedef {import('cids')} CID
@@ -40,13 +36,11 @@ const statsKeys = [
4036
/**
4137
* JavaScript implementation of the Bitswap 'data exchange' protocol
4238
* used by IPFS.
43-
*
44-
* @implements {API}
4539
*/
4640
class Bitswap {
4741
/**
4842
* @param {import('libp2p')} libp2p
49-
* @param {import('ipfs-core-types/src/block-store').BlockStore} blockstore
43+
* @param {import('ipfs-repo').Blockstore} blockstore
5044
* @param {Object} [options]
5145
* @param {boolean} [options.statsEnabled=false]
5246
* @param {number} [options.statsComputeThrottleTimeout=1000]
@@ -91,7 +85,6 @@ class Bitswap {
9185
*
9286
* @param {PeerId} peerId
9387
* @param {BitswapMessage} incoming
94-
* @returns {Promise<void>}
9588
*/
9689
async _receiveMessage (peerId, incoming) {
9790
try {
@@ -130,7 +123,6 @@ class Bitswap {
130123
* @param {PeerId} peerId
131124
* @param {Block} block
132125
* @param {boolean} wasWanted
133-
* @returns {Promise<void>}
134126
*/
135127
async _handleReceivedBlock (peerId, block, wasWanted) {
136128
this._log('received block')
@@ -166,7 +158,6 @@ class Bitswap {
166158
* handle errors on the receiving channel
167159
*
168160
* @param {Error} err
169-
* @returns {void}
170161
*/
171162
_receiveError (err) {
172163
this._log.error('ReceiveError: %s', err.message)
@@ -192,16 +183,10 @@ class Bitswap {
192183
this._stats.disconnected(peerId)
193184
}
194185

195-
/**
196-
* @returns {void}
197-
*/
198186
enableStats () {
199187
this._stats.enable()
200188
}
201189

202-
/**
203-
* @returns {void}
204-
*/
205190
disableStats () {
206191
this._stats.disable()
207192
}
@@ -210,8 +195,7 @@ class Bitswap {
210195
* Return the current wantlist for a given `peerId`
211196
*
212197
* @param {PeerId} peerId
213-
* @param {AbortOptions} [_options]
214-
* @returns {Map<string, WantListEntry>}
198+
* @param {any} [_options]
215199
*/
216200
wantlistForPeer (peerId, _options) {
217201
return this.engine.wantlistForPeer(peerId)
@@ -221,7 +205,6 @@ class Bitswap {
221205
* Return ledger information for a given `peerId`
222206
*
223207
* @param {PeerId} peerId
224-
* @returns {null|LedgerForPeer}
225208
*/
226209
ledgerForPeer (peerId) {
227210
return this.engine.ledgerForPeer(peerId)
@@ -234,14 +217,12 @@ class Bitswap {
234217
* @param {CID} cid
235218
* @param {Object} [options]
236219
* @param {AbortSignal} [options.signal]
237-
* @returns {Promise<Block>}
238220
*/
239221
async get (cid, options = {}) {
240222
/**
241223
* @param {CID} cid
242224
* @param {Object} options
243225
* @param {AbortSignal} options.signal
244-
* @returns {Promise<Block>}
245226
*/
246227
const fetchFromNetwork = (cid, options) => {
247228
// add it to the want list - n.b. later we will abort the AbortSignal
@@ -258,7 +239,6 @@ class Bitswap {
258239
* @param {CID} cid
259240
* @param {Object} options
260241
* @param {AbortSignal} options.signal
261-
* @returns {Promise<Block>}
262242
*/
263243
const loadOrFetchFromNetwork = async (cid, options) => {
264244
try {
@@ -314,7 +294,6 @@ class Bitswap {
314294
* @param {AsyncIterable<CID>|Iterable<CID>} cids
315295
* @param {Object} [options]
316296
* @param {AbortSignal} [options.signal]
317-
* @returns {AsyncIterable<Block>}
318297
*/
319298
async * getMany (cids, options = {}) {
320299
for await (const cid of cids) {
@@ -331,7 +310,6 @@ class Bitswap {
331310
* AbortSignal in to `.get` or `.getMany` and abort it.
332311
*
333312
* @param {CID[]|CID} cids
334-
* @returns {void}
335313
*/
336314
unwant (cids) {
337315
const cidsArray = Array.isArray(cids) ? cids : [cids]
@@ -346,7 +324,6 @@ class Bitswap {
346324
* call `unwant(cids)` instead.
347325
*
348326
* @param {CID[]|CID} cids
349-
* @returns {void}
350327
*/
351328
cancelWants (cids) {
352329
this.wm.cancelWants(Array.isArray(cids) ? cids : [cids])
@@ -357,8 +334,7 @@ class Bitswap {
357334
* send it to nodes that have it in their wantlist.
358335
*
359336
* @param {Block} block
360-
* @param {AbortOptions} [_options]
361-
* @returns {Promise<void>}
337+
* @param {any} [_options]
362338
*/
363339
async put (block, _options) {
364340
await this.blockstore.put(block)
@@ -370,7 +346,6 @@ class Bitswap {
370346
* send it to nodes that have it them their wantlist.
371347
*
372348
* @param {AsyncIterable<Block>|Iterable<Block>} blocks
373-
* @returns {AsyncIterable<Block>}
374349
*/
375350
async * putMany (blocks) {
376351
for await (const block of this.blockstore.putMany(blocks)) {
@@ -396,36 +371,28 @@ class Bitswap {
396371
}
397372

398373
/**
399-
* Get the current list of wants.
400-
*
401-
* @returns {Iterable<[string, WantListEntry]>}
374+
* Get the current list of wants
402375
*/
403376
getWantlist () {
404377
return this.wm.wantlist.entries()
405378
}
406379

407380
/**
408-
* Get the current list of partners.
409-
*
410-
* @returns {PeerId[]}
381+
* Get the current list of partners
411382
*/
412383
peers () {
413384
return this.engine.peers()
414385
}
415386

416387
/**
417-
* Get stats about the bitswap node.
418-
*
419-
* @returns {import('ipfs-core-types/src/bitswap').Stats}
388+
* Get stats about the bitswap node
420389
*/
421390
stat () {
422391
return this._stats
423392
}
424393

425394
/**
426-
* Start the bitswap node.
427-
*
428-
* @returns {void}
395+
* Start the bitswap node
429396
*/
430397
start () {
431398
this.wm.start()
@@ -434,9 +401,7 @@ class Bitswap {
434401
}
435402

436403
/**
437-
* Stop the bitswap node.
438-
*
439-
* @returns {void}
404+
* Stop the bitswap node
440405
*/
441406
stop () {
442407
this._stats.stop()

src/network.js

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,9 @@ const logger = require('./utils').logger
1212
/**
1313
* @typedef {import('peer-id')} PeerId
1414
* @typedef {import('cids')} CID
15-
* @typedef {import('multiaddr')} Multiaddr
16-
*
17-
* @typedef {Object} Connection
18-
* @property {string} id
19-
* @property {PeerId} remotePeer
15+
* @typedef {import('multiaddr').Multiaddr} Multiaddr
16+
* @typedef {import('libp2p-interfaces/src/connection').Connection} Connection
17+
* @typedef {import('libp2p-interfaces/src/stream-muxer/types').MuxedStream} MuxedStream
2018
*
2119
* @typedef {Object} Provider
2220
* @property {PeerId} id
@@ -100,9 +98,8 @@ class Network {
10098
* @private
10199
* @param {object} connection
102100
* @param {string} connection.protocol - The protocol the stream is running
103-
* @param {Stream} connection.stream - A duplex iterable stream
101+
* @param {MuxedStream} connection.stream - A duplex iterable stream
104102
* @param {Connection} connection.connection - A libp2p Connection
105-
* @returns {Promise<void>}
106103
*/
107104
async _onConnection ({ protocol, stream, connection }) {
108105
if (!this._running) { return }
@@ -143,7 +140,6 @@ class Network {
143140
/**
144141
* @private
145142
* @param {PeerId} peerId
146-
* @returns {void}
147143
*/
148144
_onPeerDisconnect (peerId) {
149145
this._bitswap._onPeerDisconnected(peerId)
@@ -177,7 +173,6 @@ class Network {
177173
* @param {CID} cid
178174
* @param {Object} [options]
179175
* @param {AbortSignal} [options.signal]
180-
* @returns {Promise<void>}
181176
*/
182177
async findAndConnect (cid, options) {
183178
const connectAttempts = []
@@ -194,7 +189,6 @@ class Network {
194189
* @param {CID} cid
195190
* @param {Object} [options]
196191
* @param {AbortSignal} [options.signal]
197-
* @returns {Promise<void>}
198192
*/
199193
async provide (cid, options) {
200194
// @ts-expect-error - contentRouting takes no options
@@ -207,15 +201,15 @@ class Network {
207201
*
208202
* @param {PeerId} peer
209203
* @param {Message} msg
210-
* @returns {Promise<void>}
211204
*/
212205
async sendMessage (peer, msg) {
213206
if (!this._running) throw new Error('network isn\'t running')
214207

215208
const stringId = peer.toB58String()
216209
this._log('sendMessage to %s', stringId, msg)
217210

218-
const { stream, protocol } = await this._dialPeer(peer)
211+
const connection = await this._libp2p.dial(peer)
212+
const { stream, protocol } = await connection.newStream([BITSWAP120, BITSWAP110, BITSWAP100])
219213

220214
/** @type {Uint8Array} */
221215
let serialized
@@ -253,16 +247,6 @@ class Network {
253247
return this._libp2p.dial(peer, options)
254248
}
255249

256-
/**
257-
* Dial to the peer and try to use the most recent Bitswap
258-
*
259-
* @private
260-
* @param {PeerId|Multiaddr} peer
261-
*/
262-
_dialPeer (peer) {
263-
return this._libp2p.dialProtocol(peer, [BITSWAP120, BITSWAP110, BITSWAP100])
264-
}
265-
266250
/**
267251
* @private
268252
* @param {PeerId} peer
@@ -280,7 +264,7 @@ class Network {
280264

281265
/**
282266
*
283-
* @param {Stream} stream
267+
* @param {MuxedStream} stream
284268
* @param {Uint8Array} msg
285269
* @param {*} log
286270
*/

src/notifications.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const CONSTANTS = require('./constants')
99
const logger = require('./utils').logger
1010

1111
/**
12-
* @typedef {import('ipfs-core-types/src/block-service').Block} Block
12+
* @typedef {import('ipld-block')} Block
1313
*/
1414

1515
/**

0 commit comments

Comments
 (0)