Skip to content

Commit be47b50

Browse files
achingbrainvasco-santos
authored andcommitted
fix: pick uintarrays commit
1 parent ae42ede commit be47b50

File tree

14 files changed

+78
-66
lines changed

14 files changed

+78
-66
lines changed

.aegir.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,11 @@ module.exports = {
5656
hooks: {
5757
pre: before,
5858
post: after
59+
},
60+
webpack: {
61+
node: {
62+
// this is needed until bcrypto stops using node buffers in browser code
63+
Buffer: true
64+
}
5965
}
6066
}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ gsub.on('fruit', (data) => {
5353
})
5454
gsub.subscribe('fruit')
5555

56-
gsub.publish('fruit', new Buffer('banana'))
56+
gsub.publish('fruit', new TextEncoder().encode('banana'))
5757
```
5858

5959
## API

benchmarks/benchmarks.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const suite = new Benchmark.Suite('gossipsub')
2525
suite
2626
.add('publish and receive', (deferred) => {
2727
peers[1].gs.once('Z', (msg) => deferred.resolve(msg))
28-
peers[0].gs.publish('Z', Buffer.alloc(1024))
28+
peers[0].gs.publish('Z', new Uint8Array(1024))
2929
}, {
3030
defer: true
3131
})

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,22 +38,21 @@
3838
"lint"
3939
],
4040
"dependencies": {
41-
"buffer": "^5.6.0",
4241
"debug": "^4.1.1",
4342
"denque": "^1.4.1",
4443
"err-code": "^2.0.0",
4544
"it-pipe": "^1.0.1",
4645
"libp2p-pubsub": "https://github.com/libp2p/js-libp2p-pubsub#v0.6.x",
47-
"peer-id": "~0.13.12",
48-
"protons": "^1.0.1",
46+
"peer-id": "^0.14.0",
47+
"protons": "^2.0.0",
4948
"time-cache": "^0.3.0"
5049
},
5150
"devDependencies": {
5251
"@types/chai": "^4.2.3",
5352
"@types/mocha": "^7.0.2",
5453
"@typescript-eslint/eslint-plugin": "^3.0.2",
5554
"@typescript-eslint/parser": "^3.0.2",
56-
"aegir": "^21.10.2",
55+
"aegir": "^25.0.0",
5756
"benchmark": "^2.1.4",
5857
"chai": "^4.2.0",
5958
"chai-spies": "^1.0.0",
@@ -79,7 +78,8 @@
7978
"p-wait-for": "^3.1.0",
8079
"promisify-es6": "^1.0.3",
8180
"sinon": "^9.0.2",
82-
"typescript": "^3.9.3"
81+
"typescript": "^3.9.3",
82+
"uint8arrays": "^1.1.0"
8383
},
8484
"peerDependencies": {
8585
"libp2p": "https://github.com/libp2p/js-libp2p#0.29.x"

test/2-nodes.spec.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
/* eslint-env mocha */
22
'use strict'
33

4-
const { Buffer } = require('buffer')
54
const chai = require('chai')
65
chai.use(require('dirty-chai'))
76
chai.use(require('chai-spies'))
87
const expect = chai.expect
8+
99
const delay = require('delay')
10+
const uint8ArrayFromString = require('uint8arrays/from-string')
11+
1012

1113
const { multicodec } = require('../src')
1214
const Gossipsub = require('../src')
@@ -148,7 +150,7 @@ describe('2 nodes', () => {
148150
const promise = new Promise((resolve) => nodes[1].once(topic, resolve))
149151
nodes[0].once(topic, (m) => shouldNotHappen)
150152

151-
nodes[0].publish(topic, Buffer.from('hey'))
153+
nodes[0].publish(topic, uint8ArrayFromString('hey'))
152154

153155
const msg = await promise
154156

@@ -162,7 +164,7 @@ describe('2 nodes', () => {
162164
const promise = new Promise((resolve) => nodes[0].once(topic, resolve))
163165
nodes[1].once(topic, shouldNotHappen)
164166

165-
nodes[1].publish(topic, Buffer.from('banana'))
167+
nodes[1].publish(topic, uint8ArrayFromString('banana'))
166168

167169
const msg = await promise
168170

@@ -182,7 +184,7 @@ describe('2 nodes', () => {
182184
function receivedMsg (msg) {
183185
expect(msg.data.toString()).to.equal('banana')
184186
expect(msg.from).to.be.eql(nodes[1].peerId.toB58String())
185-
expect(Buffer.isBuffer(msg.seqno)).to.be.true()
187+
expect(msg.seqno).to.be.a('Uint8Array')
186188
expect(msg.topicIDs).to.be.eql([topic])
187189

188190
if (++counter === 10) {
@@ -193,7 +195,7 @@ describe('2 nodes', () => {
193195
}
194196

195197
Array.from({ length: 10 }).forEach(() => {
196-
nodes[1].publish(topic, Buffer.from('banana'))
198+
nodes[1].publish(topic, uint8ArrayFromString('banana'))
197199
})
198200
})
199201
})
@@ -250,8 +252,8 @@ describe('2 nodes', () => {
250252
}, 100)
251253
})
252254

253-
nodes[1].publish('Z', Buffer.from('banana'))
254-
nodes[0].publish('Z', Buffer.from('banana'))
255+
nodes[1].publish('Z', uint8ArrayFromString('banana'))
256+
nodes[0].publish('Z', uint8ArrayFromString('banana'))
255257

256258
try {
257259
await promise

test/emit-self.spec.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
/* eslint-env mocha */
22
'use strict'
3-
const { Buffer } = require('buffer')
3+
44
const chai = require('chai')
55
chai.use(require('dirty-chai'))
66
chai.use(require('chai-spies'))
77
const expect = chai.expect
8+
const uint8ArrayFromString = require('uint8arrays/from-string')
89

910
const Gossipsub = require('../src')
1011
const {
@@ -31,7 +32,8 @@ describe('emit self', () => {
3132
it('should emit to self on publish', async () => {
3233
gossipsub.subscribe(topic)
3334
const promise = new Promise((resolve) => gossipsub.once(topic, resolve))
34-
gossipsub.publish(topic, Buffer.from('hey'))
35+
36+
gossipsub.publish(topic, uint8ArrayFromString('hey'))
3537

3638
await promise
3739
})
@@ -49,7 +51,7 @@ describe('emit self', () => {
4951
gossipsub.subscribe(topic)
5052
gossipsub.once(topic, (m) => shouldNotHappen)
5153

52-
gossipsub.publish(topic, Buffer.from('hey'))
54+
gossipsub.publish(topic, uint8ArrayFromString('hey'))
5355

5456
// Wait 1 second to guarantee that self is not noticed
5557
await new Promise((resolve) => setTimeout(() => resolve(), 1000))

test/floodsub.spec.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
/* eslint-env mocha */
22
'use strict'
33

4-
const { Buffer } = require('buffer')
54
const chai = require('chai')
65
chai.use(require('dirty-chai'))
6+
77
const delay = require('delay')
8+
const uint8ArrayFromString = require('uint8arrays/from-string')
89

910
const expect = chai.expect
1011
const times = require('lodash/times')
1112
const PeerId = require('peer-id')
1213

13-
const { multicodec: floodsubMulticodec } = require('libp2p-floodsub')
14-
1514
const Gossipsub = require('../src')
1615
const {
1716
createPeer,
@@ -176,7 +175,7 @@ describe('gossipsub fallbacks to floodsub', () => {
176175
const promise = new Promise((resolve) => nodeFs.once(topic, resolve))
177176
nodeGs.once(topic, (m) => shouldNotHappen)
178177

179-
nodeGs.publish(topic, Buffer.from('hey'))
178+
nodeGs.publish(topic, uint8ArrayFromString('hey'))
180179

181180
const msg = await promise
182181
expect(msg.data.toString()).to.equal('hey')
@@ -188,7 +187,7 @@ describe('gossipsub fallbacks to floodsub', () => {
188187
it('Publish to a topic - nodeFs', async () => {
189188
const promise = new Promise((resolve) => nodeGs.once(topic, resolve))
190189

191-
nodeFs.publish(topic, Buffer.from('banana'))
190+
nodeFs.publish(topic, uint8ArrayFromString('banana'))
192191

193192
const msg = await promise
194193

@@ -209,7 +208,7 @@ describe('gossipsub fallbacks to floodsub', () => {
209208
function receivedMsg (msg) {
210209
expect(msg.data.toString()).to.equal('banana ' + counter)
211210
expect(msg.from).to.be.eql(nodeGs.peerId.toB58String())
212-
expect(Buffer.isBuffer(msg.seqno)).to.be.true()
211+
expect(msg.seqno).to.be.a('Uint8Array')
213212
expect(msg.topicIDs).to.be.eql([topic])
214213

215214
if (++counter === 10) {
@@ -219,7 +218,7 @@ describe('gossipsub fallbacks to floodsub', () => {
219218
}
220219
}
221220

222-
times(10, (index) => nodeGs.publish(topic, Buffer.from('banana ' + index)))
221+
times(10, (index) => nodeGs.publish(topic, uint8ArrayFromString('banana ' + index)))
223222
})
224223

225224
it('Publish 10 msg to a topic as array', (done) => {
@@ -236,7 +235,7 @@ describe('gossipsub fallbacks to floodsub', () => {
236235
function receivedMsg (msg) {
237236
expect(msg.data.toString()).to.equal('banana ' + counter)
238237
expect(msg.from).to.be.eql(nodeGs.peerId.toB58String())
239-
expect(Buffer.isBuffer(msg.seqno)).to.be.true()
238+
expect(msg.seqno).to.be.a('Uint8Array')
240239
expect(msg.topicIDs).to.be.eql([topic])
241240

242241
if (++counter === 10) {
@@ -247,7 +246,7 @@ describe('gossipsub fallbacks to floodsub', () => {
247246
}
248247

249248
const msgs = []
250-
times(10, (index) => msgs.push(Buffer.from('banana ' + index)))
249+
times(10, (index) => msgs.push(uint8ArrayFromString('banana ' + index)))
251250
msgs.forEach(msg => nodeGs.publish(topic, msg))
252251
})
253252
})
@@ -314,8 +313,8 @@ describe('gossipsub fallbacks to floodsub', () => {
314313
}, 100)
315314
})
316315

317-
nodeFs.publish('Z', Buffer.from('banana'))
318-
nodeGs.publish('Z', Buffer.from('banana'))
316+
nodeFs.publish('Z', uint8ArrayFromString('banana'))
317+
nodeGs.publish('Z', uint8ArrayFromString('banana'))
319318

320319
try {
321320
await promise

test/gossip-incoming.spec.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
/* eslint-env mocha */
22
'use strict'
33

4-
const { Buffer } = require('buffer')
54
const chai = require('chai')
65
chai.use(require('dirty-chai'))
76
chai.use(require('chai-spies'))
87
const expect = chai.expect
8+
99
const delay = require('delay')
10+
const uint8ArrayFromString = require('uint8arrays/from-string')
1011

11-
const { GossipsubIDv11: multicodec } = require('../src/constants')
1212
const {
1313
createConnectedGossipsubs,
1414
stopNode
@@ -47,7 +47,7 @@ describe('gossip incoming', () => {
4747
const promise = new Promise((resolve) => nodes[2].once(topic, resolve))
4848
nodes[0].once(topic, (m) => shouldNotHappen)
4949

50-
nodes[0].publish(topic, Buffer.from('hey'))
50+
nodes[0].publish(topic, uint8ArrayFromString('hey'))
5151

5252
const msg = await promise
5353

@@ -84,7 +84,7 @@ describe('gossip incoming', () => {
8484
it('should not gossip incoming messages', async () => {
8585
nodes[2].once(topic, (m) => shouldNotHappen)
8686

87-
nodes[0].publish(topic, Buffer.from('hey'))
87+
nodes[0].publish(topic, uint8ArrayFromString('hey'))
8888

8989
await delay(1000)
9090

test/messageCache.spec.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
/* eslint-env mocha */
22
/* eslint-disable no-unused-expressions */
33
'use strict'
4-
const { Buffer } = require('buffer')
4+
55
const chai = require('chai')
66
const dirtyChai = require('dirty-chai')
77
chai.use(dirtyChai)
88
const chaiSpies = require('chai-spies')
99
chai.use(chaiSpies)
1010
const expect = chai.expect
11+
const uint8ArrayFromString = require('uint8arrays/from-string')
1112

1213
const { MessageCache } = require('../src/messageCache')
1314
const { utils } = require('libp2p-pubsub')
@@ -24,7 +25,7 @@ describe('Testing Message Cache Operations', () => {
2425
const makeTestMessage = (n) => {
2526
return {
2627
from: 'test',
27-
data: Buffer.from(n.toString()),
28+
data: uint8ArrayFromString(n.toString()),
2829
seqno: utils.randomSeqno(),
2930
topicIDs: ['test']
3031
}

test/multiple-nodes.spec.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
/* eslint-env mocha */
22
/* eslint max-nested-callbacks: ["error", 8] */
33
'use strict'
4-
const { Buffer } = require('buffer')
4+
55
const chai = require('chai')
66
chai.use(require('dirty-chai'))
77
const expect = chai.expect
8-
const promisify = require('promisify-es6')
8+
99
const delay = require('delay')
10+
const promisify = require('promisify-es6')
11+
const uint8ArrayFromString = require('uint8arrays/from-string')
1012

11-
const { GossipsubIDv11: multicodec } = require('../src/constants')
1213
const {
1314
createGossipsubs,
1415
expectSet,
@@ -111,7 +112,7 @@ describe('multiple nodes (more than 2)', () => {
111112
let msgB = new Promise((resolve) => b.once('Z', resolve))
112113
let msgC = new Promise((resolve) => c.once('Z', resolve))
113114

114-
a.publish('Z', Buffer.from('hey'))
115+
a.publish('Z', uint8ArrayFromString('hey'))
115116
msgB = await msgB
116117
msgC = await msgC
117118

@@ -162,7 +163,7 @@ describe('multiple nodes (more than 2)', () => {
162163
let msgA = new Promise((resolve) => a.once('Z', resolve))
163164
let msgC = new Promise((resolve) => c.once('Z', resolve))
164165

165-
b.publish('Z', Buffer.from('hey'))
166+
b.publish('Z', uint8ArrayFromString('hey'))
166167
msgA = await msgA
167168
msgC = await msgC
168169

@@ -227,7 +228,7 @@ describe('multiple nodes (more than 2)', () => {
227228
let msgE = new Promise((resolve) => e.once('Z', resolve))
228229

229230
const msg = 'hey from c'
230-
c.publish('Z', Buffer.from(msg))
231+
c.publish('Z', uint8ArrayFromString(msg))
231232

232233
msgA = await msgA
233234
msgB = await msgB

0 commit comments

Comments
 (0)