Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: replace node buffers with uint8arrays #118

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ gsub.on('fruit', (data) => {
})
gsub.subscribe('fruit')

gsub.publish('fruit', new Buffer('banana'))
gsub.publish('fruit', new TextEncoder().encode('banana'))
```

## API
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/benchmarks.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const suite = new Benchmark.Suite('gossipsub')
suite
.add('publish and receive', (deferred) => {
peers[1].gs.once('Z', (msg) => deferred.resolve(msg))
peers[0].gs.publish('Z', Buffer.alloc(1024))
peers[0].gs.publish('Z', new Uint8Array(1024))
}, {
defer: true
})
Expand Down
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,22 @@
"lint"
],
"dependencies": {
"buffer": "^5.6.0",
"debug": "^4.1.1",
"err-code": "^2.0.0",
"it-length-prefixed": "^3.0.0",
"it-pipe": "^1.0.1",
"libp2p-pubsub": "~0.5.2",
"libp2p-pubsub": "^0.6.0",
"p-map": "^4.0.0",
"peer-id": "~0.13.12",
"protons": "^1.0.1",
"peer-id": "^0.14.0",
"protons": "^2.0.0",
"time-cache": "^0.3.0"
},
"devDependencies": {
"@types/chai": "^4.2.3",
"@types/mocha": "^7.0.2",
"@typescript-eslint/eslint-plugin": "^3.0.2",
"@typescript-eslint/parser": "^3.0.2",
"aegir": "^21.10.2",
"aegir": "^25.0.0",
"benchmark": "^2.1.4",
"chai": "^4.2.0",
"chai-spies": "^1.0.0",
Expand All @@ -72,7 +71,8 @@
"p-wait-for": "^3.1.0",
"promisify-es6": "^1.0.3",
"sinon": "^9.0.2",
"typescript": "^3.9.3"
"typescript": "^3.9.3",
"uint8arrays": "^1.1.0"
},
"contributors": [
"Cayman <caymannava@gmail.com>",
Expand Down
19 changes: 10 additions & 9 deletions test/2-nodes.spec.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
/* eslint-env mocha */
'use strict'

const { Buffer } = require('buffer')
const chai = require('chai')
chai.use(require('dirty-chai'))
chai.use(require('chai-spies'))
const expect = chai.expect
const uint8ArrayFromString = require('uint8arrays/from-string')


const { multicodec } = require('../src')

Expand Down Expand Up @@ -151,7 +152,7 @@ describe('2 nodes', () => {
const promise = new Promise((resolve) => nodes[1].once(topic, resolve))
nodes[0].once(topic, (m) => shouldNotHappen)

nodes[0].publish(topic, Buffer.from('hey'))
nodes[0].publish(topic, uint8ArrayFromString('hey'))

const msg = await promise

Expand All @@ -165,7 +166,7 @@ describe('2 nodes', () => {
const promise = new Promise((resolve) => nodes[0].once(topic, resolve))
nodes[1].once(topic, shouldNotHappen)

nodes[1].publish(topic, Buffer.from('banana'))
nodes[1].publish(topic, uint8ArrayFromString('banana'))

const msg = await promise

Expand All @@ -185,7 +186,7 @@ describe('2 nodes', () => {
function receivedMsg (msg) {
expect(msg.data.toString()).to.equal('banana')
expect(msg.from).to.be.eql(nodes[1].peerId.toB58String())
expect(Buffer.isBuffer(msg.seqno)).to.be.true()
expect(msg.seqno).to.be.a('Uint8Array')
expect(msg.topicIDs).to.be.eql([topic])

if (++counter === 10) {
Expand All @@ -196,7 +197,7 @@ describe('2 nodes', () => {
}

Array.from({ length: 10 }).forEach(() => {
nodes[1].publish(topic, Buffer.from('banana'))
nodes[1].publish(topic, uint8ArrayFromString('banana'))
})
})

Expand All @@ -210,7 +211,7 @@ describe('2 nodes', () => {
function receivedMsg (msg) {
expect(msg.data.toString()).to.equal('banana')
expect(msg.from).to.be.eql(nodes[1].peerId.toB58String())
expect(Buffer.isBuffer(msg.seqno)).to.be.true()
expect(msg.seqno).to.be.a('Uint8Array')
expect(msg.topicIDs).to.be.eql([topic])

if (++counter === 10) {
Expand All @@ -222,7 +223,7 @@ describe('2 nodes', () => {

const msgs = []
Array.from({ length: 10 }).forEach(() => {
msgs.push(Buffer.from('banana'))
msgs.push(uint8ArrayFromString('banana'))
})
nodes[1].publish(topic, msgs)
})
Expand Down Expand Up @@ -281,8 +282,8 @@ describe('2 nodes', () => {
}, 100)
})

nodes[1].publish('Z', Buffer.from('banana'))
nodes[0].publish('Z', Buffer.from('banana'))
nodes[1].publish('Z', uint8ArrayFromString('banana'))
nodes[0].publish('Z', uint8ArrayFromString('banana'))

try {
await promise
Expand Down
7 changes: 4 additions & 3 deletions test/emit-self.spec.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
/* eslint-env mocha */
'use strict'
const { Buffer } = require('buffer')

const chai = require('chai')
chai.use(require('dirty-chai'))
chai.use(require('chai-spies'))
const expect = chai.expect
const uint8ArrayFromString = require('uint8arrays/from-string')

const {
createGossipsub,
Expand All @@ -29,7 +30,7 @@ describe('emit self', () => {
it('should emit to self on publish', async () => {
const promise = new Promise((resolve) => gossipsub.once(topic, resolve))

gossipsub.publish(topic, Buffer.from('hey'))
gossipsub.publish(topic, uint8ArrayFromString('hey'))

await promise
})
Expand All @@ -46,7 +47,7 @@ describe('emit self', () => {
it('should emit to self on publish', async () => {
gossipsub.once(topic, (m) => shouldNotHappen)

gossipsub.publish(topic, Buffer.from('hey'))
gossipsub.publish(topic, uint8ArrayFromString('hey'))

// Wait 1 second to guarantee that self is not noticed
await new Promise((resolve) => setTimeout(() => resolve(), 1000))
Expand Down
18 changes: 9 additions & 9 deletions test/floodsub.spec.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/* eslint-env mocha */
'use strict'

const { Buffer } = require('buffer')
const chai = require('chai')
chai.use(require('dirty-chai'))
const uint8ArrayFromString = require('uint8arrays/from-string')

const expect = chai.expect
const times = require('lodash/times')
Expand Down Expand Up @@ -228,7 +228,7 @@ describe('gossipsub fallbacks to floodsub', () => {
const promise = new Promise((resolve) => nodeFs.once(topic, resolve))
nodeGs.once(topic, (m) => shouldNotHappen)

nodeGs.publish(topic, Buffer.from('hey'))
nodeGs.publish(topic, uint8ArrayFromString('hey'))

promise.then((msg) => {
expect(msg.data.toString()).to.equal('hey')
Expand All @@ -242,7 +242,7 @@ describe('gossipsub fallbacks to floodsub', () => {
it('Publish to a topic - nodeFs', async () => {
const promise = new Promise((resolve) => nodeGs.once(topic, resolve))

nodeFs.publish(topic, Buffer.from('banana'))
nodeFs.publish(topic, uint8ArrayFromString('banana'))

const msg = await promise

Expand All @@ -263,7 +263,7 @@ describe('gossipsub fallbacks to floodsub', () => {
function receivedMsg (msg) {
expect(msg.data.toString()).to.equal('banana ' + counter)
expect(msg.from).to.be.eql(nodeGs.peerId.toB58String())
expect(Buffer.isBuffer(msg.seqno)).to.be.true()
expect(msg.seqno).to.be.a('Uint8Array')
expect(msg.topicIDs).to.be.eql([topic])

if (++counter === 10) {
Expand All @@ -273,7 +273,7 @@ describe('gossipsub fallbacks to floodsub', () => {
}
}

times(10, (index) => nodeGs.publish(topic, Buffer.from('banana ' + index)))
times(10, (index) => nodeGs.publish(topic, uint8ArrayFromString('banana ' + index)))
})

it('Publish 10 msg to a topic as array', (done) => {
Expand All @@ -290,7 +290,7 @@ describe('gossipsub fallbacks to floodsub', () => {
function receivedMsg (msg) {
expect(msg.data.toString()).to.equal('banana ' + counter)
expect(msg.from).to.be.eql(nodeGs.peerId.toB58String())
expect(Buffer.isBuffer(msg.seqno)).to.be.true()
expect(msg.seqno).to.be.a('Uint8Array')
expect(msg.topicIDs).to.be.eql([topic])

if (++counter === 10) {
Expand All @@ -301,7 +301,7 @@ describe('gossipsub fallbacks to floodsub', () => {
}

const msgs = []
times(10, (index) => msgs.push(Buffer.from('banana ' + index)))
times(10, (index) => msgs.push(uint8ArrayFromString('banana ' + index)))
nodeGs.publish(topic, msgs)
})
})
Expand Down Expand Up @@ -384,8 +384,8 @@ describe('gossipsub fallbacks to floodsub', () => {
}, 100)
})

nodeFs.publish('Z', Buffer.from('banana'))
nodeGs.publish('Z', Buffer.from('banana'))
nodeFs.publish('Z', uint8ArrayFromString('banana'))
nodeGs.publish('Z', uint8ArrayFromString('banana'))

try {
await promise
Expand Down
7 changes: 4 additions & 3 deletions test/gossip-incoming.spec.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
/* eslint-env mocha */
'use strict'

const { Buffer } = require('buffer')
const chai = require('chai')
chai.use(require('dirty-chai'))
chai.use(require('chai-spies'))
const expect = chai.expect
const uint8ArrayFromString = require('uint8arrays/from-string')


const { GossipsubID: multicodec } = require('../src/constants')
const { createGossipsubConnectedNodes } = require('./utils')
Expand Down Expand Up @@ -43,7 +44,7 @@ describe('gossip incoming', () => {
const promise = new Promise((resolve) => nodes[2].once(topic, resolve))
nodes[0].once(topic, (m) => shouldNotHappen)

nodes[0].publish(topic, Buffer.from('hey'))
nodes[0].publish(topic, uint8ArrayFromString('hey'))

const msg = await promise

Expand Down Expand Up @@ -80,7 +81,7 @@ describe('gossip incoming', () => {
it('should not gossip incoming messages', async () => {
nodes[2].once(topic, (m) => shouldNotHappen)

nodes[0].publish(topic, Buffer.from('hey'))
nodes[0].publish(topic, uint8ArrayFromString('hey'))

await new Promise((resolve) => setTimeout(resolve, 1000))

Expand Down
6 changes: 3 additions & 3 deletions test/gossip.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict'
/* eslint-env mocha */

const { Buffer } = require('buffer')
const { expect } = require('chai')
const sinon = require('sinon')
const uint8ArrayFromString = require('uint8arrays/from-string')

const { GossipsubID: multicodec, GossipsubDhi } = require('../src/constants')
const {
Expand Down Expand Up @@ -42,7 +42,7 @@ describe('gossip', () => {
// set spy
sinon.spy(nodeA, 'log')

await nodeA.publish(topic, Buffer.from('hey'))
await nodeA.publish(topic, uint8ArrayFromString('hey'))

await new Promise((resolve) => nodeA.once('gossipsub:heartbeat', resolve))

Expand Down Expand Up @@ -84,7 +84,7 @@ describe('gossip', () => {
// manually add control message to be sent to peerB
nodeA.control.set(peerB, { graft: [{ topicID: topic }] })

await nodeA.publish(topic, Buffer.from('hey'))
await nodeA.publish(topic, uint8ArrayFromString('hey'))

await new Promise((resolve) => nodeA.once('gossipsub:heartbeat', resolve))
expect(nodeB.log.callCount).to.be.gt(1)
Expand Down
5 changes: 3 additions & 2 deletions test/messageCache.spec.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
/* eslint-env mocha */
/* eslint-disable no-unused-expressions */
'use strict'
const { Buffer } = require('buffer')

const chai = require('chai')
const dirtyChai = require('dirty-chai')
chai.use(dirtyChai)
const chaiSpies = require('chai-spies')
chai.use(chaiSpies)
const expect = chai.expect
const uint8ArrayFromString = require('uint8arrays/from-string')

const { MessageCache } = require('../src/messageCache')
const { utils } = require('libp2p-pubsub')
Expand All @@ -24,7 +25,7 @@ describe('Testing Message Cache Operations', () => {
const makeTestMessage = (n) => {
return {
from: 'test',
data: Buffer.from(n.toString()),
data: uint8ArrayFromString(n.toString()),
seqno: utils.randomSeqno(),
topicIDs: ['test']
}
Expand Down
11 changes: 6 additions & 5 deletions test/multiple-nodes.spec.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
/* eslint-env mocha */
/* eslint max-nested-callbacks: ["error", 8] */
'use strict'
const { Buffer } = require('buffer')

const chai = require('chai')
chai.use(require('dirty-chai'))
const expect = chai.expect
const promisify = require('promisify-es6')
const uint8ArrayFromString = require('uint8arrays/from-string')

const { GossipsubID: multicodec } = require('../src/constants')
const {
Expand Down Expand Up @@ -192,7 +193,7 @@ describe('multiple nodes (more than 2)', () => {
let msgB = new Promise((resolve) => b.once('Z', resolve))
let msgC = new Promise((resolve) => c.once('Z', resolve))

a.publish('Z', Buffer.from('hey'))
a.publish('Z', uint8ArrayFromString('hey'))
msgB = await msgB
msgC = await msgC

Expand Down Expand Up @@ -222,7 +223,7 @@ describe('multiple nodes (more than 2)', () => {
})
})

a.publish('Z', [Buffer.from('hey'), Buffer.from('hey')])
a.publish('Z', [uint8ArrayFromString('hey'), uint8ArrayFromString('hey')])
msgB = await msgB
msgC = await msgC

Expand Down Expand Up @@ -318,7 +319,7 @@ describe('multiple nodes (more than 2)', () => {
let msgA = new Promise((resolve) => a.once('Z', resolve))
let msgC = new Promise((resolve) => c.once('Z', resolve))

b.publish('Z', Buffer.from('hey'))
b.publish('Z', uint8ArrayFromString('hey'))
msgA = await msgA
msgC = await msgC

Expand Down Expand Up @@ -460,7 +461,7 @@ describe('multiple nodes (more than 2)', () => {
let msgE = new Promise((resolve) => e.once('Z', resolve))

const msg = 'hey from c'
c.publish('Z', Buffer.from(msg))
c.publish('Z', uint8ArrayFromString(msg))

msgA = await msgA
msgB = await msgB
Expand Down
Loading