Skip to content

Commit 4f9d7cd

Browse files
authored
fix: replace node buffers with uint8arrays (#251)
BREAKING CHANGES: - All use of node Buffers have been replaced with Uint8Arrays - All deps now use Uint8Arrays in place of node Buffers
1 parent cfdc95a commit 4f9d7cd

File tree

16 files changed

+96
-84
lines changed

16 files changed

+96
-84
lines changed

.travis.yml

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
language: node_js
22
cache: npm
33

4+
branches:
5+
only:
6+
- master
7+
- /^release\/.*$/
8+
9+
node_js:
10+
- 'lts/*'
11+
- 'node'
12+
413
stages:
514
- check
615
- test
716
- cov
817

9-
node_js:
10-
- '12'
11-
- '10'
12-
1318
os:
1419
- linux
1520
- osx

package.json

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,19 @@
4343
"homepage": "https://github.com/ipfs/js-ipfs-bitswap#readme",
4444
"devDependencies": {
4545
"@nodeutils/defaults-deep": "^1.1.0",
46-
"aegir": "^25.0.0",
46+
"aegir": "^26.0.0",
4747
"benchmark": "^2.1.4",
48-
"buffer": "^5.6.0",
4948
"delay": "^4.3.0",
50-
"ipfs-repo": "^4.0.0",
51-
"ipfs-utils": "^2.2.0",
49+
"ipfs-repo": "^6.0.1",
50+
"ipfs-utils": "^3.0.0",
5251
"iso-random-stream": "^1.1.1",
5352
"it-all": "^1.0.2",
5453
"it-drain": "^1.0.1",
55-
"libp2p": "^0.28.0",
56-
"libp2p-kad-dht": "^0.19.1",
57-
"libp2p-mplex": "^0.9.2",
58-
"libp2p-secio": "^0.12.1",
59-
"libp2p-tcp": "^0.14.2",
54+
"libp2p": "libp2p/js-libp2p#0.29.x",
55+
"libp2p-kad-dht": "^0.20.0",
56+
"libp2p-mplex": "^0.10.0",
57+
"libp2p-secio": "^0.13.0",
58+
"libp2p-tcp": "^0.15.0",
6059
"lodash.difference": "^4.5.0",
6160
"lodash.flatten": "^4.4.0",
6261
"lodash.range": "^3.2.0",
@@ -65,7 +64,7 @@
6564
"p-defer": "^3.0.0",
6665
"p-event": "^4.1.0",
6766
"p-wait-for": "^3.1.0",
68-
"peer-id": "^0.13.5",
67+
"peer-id": "^0.14.0",
6968
"promisify-es6": "^1.0.3",
7069
"rimraf": "^3.0.0",
7170
"sinon": "^9.0.0",
@@ -76,19 +75,20 @@
7675
"abort-controller": "^3.0.0",
7776
"any-signal": "^1.1.0",
7877
"bignumber.js": "^9.0.0",
79-
"cids": "^0.8.3",
78+
"cids": "^1.0.0",
8079
"debug": "^4.1.0",
81-
"ipld-block": "^0.9.1",
80+
"ipld-block": "^0.10.0",
8281
"it-length-prefixed": "^3.0.0",
8382
"it-pipe": "^1.1.0",
8483
"just-debounce-it": "^1.1.0",
85-
"libp2p-interfaces": "^0.3.0",
84+
"libp2p-interfaces": "^0.4.1",
8685
"moving-average": "^1.0.0",
87-
"multicodec": "^1.0.3",
88-
"multihashing-async": "^1.0.0",
89-
"protons": "^1.0.1",
86+
"multicodec": "^2.0.0",
87+
"multihashing-async": "^2.0.1",
88+
"protons": "^2.0.0",
9089
"streaming-iterables": "^5.0.2",
91-
"varint-decoder": "^0.4.0"
90+
"uint8arrays": "^1.1.0",
91+
"varint-decoder": "^1.0.0"
9292
},
9393
"pre-push": [
9494
"lint",

src/notifications.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22

33
const EventEmitter = require('events').EventEmitter
44
const Block = require('ipld-block')
5+
const uint8ArrayEquals = require('uint8arrays/equals')
6+
const uint8ArrayToString = require('uint8arrays/to-string')
57

68
const CONSTANTS = require('./constants')
79
const logger = require('./utils').logger
810

9-
const cidToMultihashString = (cid) => cid.multihash.toString('base64')
10-
const unwantEvent = (cid) => `unwant:${cidToMultihashString(cid)}`
11-
const blockEvent = (cid) => `block:${cidToMultihashString(cid)}`
11+
const unwantEvent = (cid) => `unwant:${uint8ArrayToString(cid.multihash, 'base64')}`
12+
const blockEvent = (cid) => `block:${uint8ArrayToString(cid.multihash, 'base64')}`
1213

1314
/**
1415
* Internal module used to track events about incoming blocks,
@@ -67,7 +68,7 @@ class Notifications extends EventEmitter {
6768
const onBlock = (block) => {
6869
this.removeListener(unwantEvt, onUnwant)
6970

70-
if (!cid.multihash.equals(block.cid.multihash)) {
71+
if (!uint8ArrayEquals(cid.multihash, block.cid.multihash)) {
7172
// wrong block
7273
return reject(new Error(`Incorrect block received for ${cid}`))
7374
} else if (cid.version !== block.cid.version || cid.codec !== block.cid.codec) {

src/stats/stat.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,11 @@ class Stats extends EventEmitter {
9191
_updateFrequency (latestTime) {
9292
const timeDiff = latestTime - this._frequencyLastTime
9393

94-
Object.keys(this._stats).forEach((key) => {
95-
this._updateFrequencyFor(key, timeDiff, latestTime)
96-
})
94+
if (timeDiff) {
95+
Object.keys(this._stats).forEach((key) => {
96+
this._updateFrequencyFor(key, timeDiff, latestTime)
97+
})
98+
}
9799

98100
this._frequencyLastTime = latestTime
99101
}

src/types/message/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ class BitswapMessage {
9191
wantlist: {
9292
entries: Array.from(this.wantlist.values()).map((entry) => {
9393
return {
94-
block: entry.cid.buffer, // cid
94+
block: entry.cid.bytes, // cid
9595
priority: Number(entry.priority),
9696
cancel: Boolean(entry.cancel)
9797
}
@@ -117,7 +117,7 @@ class BitswapMessage {
117117
wantlist: {
118118
entries: Array.from(this.wantlist.values()).map((entry) => {
119119
return {
120-
block: entry.cid.buffer, // cid
120+
block: entry.cid.bytes, // cid
121121
priority: Number(entry.priority),
122122
wantType: entry.wantType,
123123
cancel: Boolean(entry.cancel),
@@ -142,7 +142,7 @@ class BitswapMessage {
142142

143143
for (const [cidStr, bpType] of this.blockPresences) {
144144
msg.blockPresences.push({
145-
cid: new CID(cidStr).buffer,
145+
cid: new CID(cidStr).bytes,
146146
type: bpType
147147
})
148148
}
@@ -237,7 +237,7 @@ BitswapMessage.blockPresenceSize = (cid) => {
237237
// the HAVE / DONT_HAVE on the wire, but when doing that calculation we leave
238238
// plenty of padding under the maximum message size.
239239
// (It's more important for this to be fast).
240-
return cid.buffer.length + 1
240+
return cid.bytes.length + 1
241241
}
242242

243243
BitswapMessage.Entry = Entry

src/utils/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict'
22

33
const debug = require('debug')
4+
const uint8ArrayEquals = require('uint8arrays/equals')
45

56
/**
67
* Creates a logger for the given subsystem
@@ -103,7 +104,7 @@ const isMapEqual = (a, b) => {
103104
return false
104105
}
105106
// Support Blocks
106-
if (valueA._data && !valueA._data.equals(valueB._data)) {
107+
if (valueA._data && !uint8ArrayEquals(valueA._data, valueB._data)) {
107108
return false
108109
}
109110
}

test/bitswap-mock-internals.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,6 @@ describe('bitswap with mocks', function () {
332332
setTimeout(() => {
333333
bs2.put(block, () => {})
334334
}, 1000)
335-
336335
const b1 = await p1
337336
expect(b1).to.eql(block)
338337

test/bitswap-stats.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,10 @@ describe('bitswap stats', () => {
7373
bitswaps.map((bs) => bs.stop())
7474
)
7575
await Promise.all(
76-
repos.map(repo => repo.teardown())
76+
libp2pNodes.map((n) => n.stop())
7777
)
7878
await Promise.all(
79-
libp2pNodes.map((n) => n.stop())
79+
repos.map(repo => repo.teardown())
8080
)
8181
})
8282

@@ -216,10 +216,13 @@ describe('bitswap stats', () => {
216216
const peerStats = bs2.stat().forPeer(libp2pNodes[0].peerId)
217217
expect(peerStats).to.exist()
218218

219+
// trigger an update
220+
peerStats.push('dataReceived', 1)
221+
219222
const stats = await pEvent(peerStats, 'update')
220223

221224
expect(stats.blocksReceived.toNumber()).to.equal(1)
222-
expect(stats.dataReceived.toNumber()).to.equal(48)
225+
expect(stats.dataReceived.toNumber()).to.equal(49)
223226
expect(stats.dupBlksReceived.toNumber()).to.equal(0)
224227
expect(stats.dupDataReceived.toNumber()).to.equal(0)
225228
expect(stats.blocksSent.toNumber()).to.equal(0)

test/decision-engine/decision-engine.js

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ const flatten = require('lodash.flatten')
99
const Block = require('ipld-block')
1010
const CID = require('cids')
1111
const multihashing = require('multihashing-async')
12-
const { Buffer } = require('buffer')
12+
const uint8ArrayFromString = require('uint8arrays/from-string')
13+
const uint8ArrayToString = require('uint8arrays/to-string')
1314
const drain = require('it-drain')
1415

1516
const Message = require('../../src/types/message')
@@ -23,7 +24,7 @@ const sum = (nums) => nums.reduce((a, b) => a + b, 0)
2324

2425
function messageToString (m) {
2526
return Array.from(m[1].blocks.values())
26-
.map((b) => b.data.toString())
27+
.map((b) => uint8ArrayToString(b.data))
2728
}
2829

2930
function stringifyMessages (messages) {
@@ -53,7 +54,7 @@ describe('Engine', () => {
5354
const receiver = res[1]
5455

5556
await Promise.all(range(1000).map(async (i) => {
56-
const data = Buffer.from(`this is message ${i}`)
57+
const data = uint8ArrayFromString(`this is message ${i}`)
5758
const hash = await multihashing(data, 'sha2-256')
5859

5960
const m = new Message(false)
@@ -110,7 +111,7 @@ describe('Engine', () => {
110111
async function partnerWants (dEngine, values, partner) {
111112
const message = new Message(false)
112113

113-
const hashes = await Promise.all(values.map((v) => multihashing(Buffer.from(v), 'sha2-256')))
114+
const hashes = await Promise.all(values.map((v) => multihashing(uint8ArrayFromString(v), 'sha2-256')))
114115
hashes.forEach((hash, i) => {
115116
message.addEntry(new CID(hash), Math.pow(2, 32) - 1 - i)
116117
})
@@ -120,7 +121,7 @@ describe('Engine', () => {
120121
async function partnerCancels (dEngine, values, partner) {
121122
const message = new Message(false)
122123

123-
const hashes = await Promise.all(values.map((v) => multihashing(Buffer.from(v), 'sha2-256')))
124+
const hashes = await Promise.all(values.map((v) => multihashing(uint8ArrayFromString(v), 'sha2-256')))
124125
hashes.forEach((hash) => {
125126
message.cancel(new CID(hash))
126127
})
@@ -134,8 +135,8 @@ describe('Engine', () => {
134135
await dEngine.receivedBlocks(blocks)
135136
}
136137

137-
const hashes = await Promise.all(alphabet.map(v => multihashing(Buffer.from(v), 'sha2-256')))
138-
const blocks = hashes.map((h, i) => new Block(Buffer.from(alphabet[i]), new CID(h)))
138+
const hashes = await Promise.all(alphabet.map(v => multihashing(uint8ArrayFromString(v), 'sha2-256')))
139+
const blocks = hashes.map((h, i) => new Block(uint8ArrayFromString(alphabet[i]), new CID(h)))
139140
const partner = await PeerId.create({ bits: 512 })
140141
const somePeer = await PeerId.create({ bits: 512 })
141142

@@ -349,8 +350,8 @@ describe('Engine', () => {
349350
const vowels = 'aeiou'
350351

351352
const alphabetLs = alphabet.split('')
352-
const hashes = await Promise.all(alphabetLs.map(v => multihashing(Buffer.from(v), 'sha2-256')))
353-
const blocks = hashes.map((h, i) => new Block(Buffer.from(alphabetLs[i]), new CID(h)))
353+
const hashes = await Promise.all(alphabetLs.map(v => multihashing(uint8ArrayFromString(v), 'sha2-256')))
354+
const blocks = hashes.map((h, i) => new Block(uint8ArrayFromString(alphabetLs[i]), new CID(h)))
354355

355356
let testCases = [
356357
// Just send want-blocks
@@ -603,7 +604,7 @@ describe('Engine', () => {
603604
let i = wantBlks.length + wantHaves.length
604605
const message = new Message(false)
605606
for (const [wants, type] of wantTypes) {
606-
const hashes = await Promise.all(wants.map((v) => multihashing(Buffer.from(v), 'sha2-256')))
607+
const hashes = await Promise.all(wants.map((v) => multihashing(uint8ArrayFromString(v), 'sha2-256')))
607608
for (const hash of hashes) {
608609
message.addEntry(new CID(hash), i--, type, false, sendDontHave)
609610
}
@@ -661,21 +662,21 @@ describe('Engine', () => {
661662

662663
// Expect the correct block contents
663664
for (const expBlk of expBlks) {
664-
const hash = await multihashing(Buffer.from(expBlk), 'sha2-256')
665+
const hash = await multihashing(uint8ArrayFromString(expBlk), 'sha2-256')
665666
expect(msg.blocks.has(new CID(hash).toString()))
666667
}
667668

668669
// Expect the correct HAVEs
669670
for (const expHave of expHaves) {
670-
const hash = await multihashing(Buffer.from(expHave), 'sha2-256')
671+
const hash = await multihashing(uint8ArrayFromString(expHave), 'sha2-256')
671672
const cid = new CID(hash).toString()
672673
expect(msg.blockPresences.has(cid)).to.eql(true)
673674
expect(msg.blockPresences.get(cid)).to.eql(Message.BlockPresenceType.Have)
674675
}
675676

676677
// Expect the correct DONT_HAVEs
677678
for (const expDontHave of expDontHaves) {
678-
const hash = await multihashing(Buffer.from(expDontHave), 'sha2-256')
679+
const hash = await multihashing(uint8ArrayFromString(expDontHave), 'sha2-256')
679680
const cid = new CID(hash).toString()
680681
expect(msg.blockPresences.has(cid)).to.eql(true)
681682
expect(msg.blockPresences.get(cid)).to.eql(Message.BlockPresenceType.DontHave)
@@ -700,7 +701,7 @@ describe('Engine', () => {
700701
const them = await newEngine()
701702

702703
// add a block to our blockstore
703-
const data = Buffer.from(`this is message ${Date.now()}`)
704+
const data = uint8ArrayFromString(`this is message ${Date.now()}`)
704705
const hash = await multihashing(data, 'sha2-256')
705706
const cid = new CID(hash)
706707
const block = new Block(data, cid)

test/network/gen-bitswap-network.node.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
const { expect } = require('aegir/utils/chai')
66
const Block = require('ipld-block')
7-
const { Buffer } = require('buffer')
87
const crypto = require('crypto')
98
const CID = require('cids')
109
const multihashing = require('multihashing-async')
@@ -21,7 +20,7 @@ describe('gen Bitswap network', function () {
2120

2221
const node = nodes[0]
2322
const blocks = await Promise.all(range(100).map(async (k) => {
24-
const b = Buffer.alloc(1024)
23+
const b = new Uint8Array(1024)
2524
b.fill(k)
2625
const hash = await multihashing(b, 'sha2-256')
2726
const cid = new CID(hash)

0 commit comments

Comments
 (0)