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: pick uintarrays commit #126

Merged
merged 1 commit into from
Aug 18, 2020
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
6 changes: 6 additions & 0 deletions .aegir.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,11 @@ module.exports = {
hooks: {
pre: before,
post: after
},
webpack: {
node: {
// this is needed until bcrypto stops using node buffers in browser code
Buffer: true
}
}
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,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
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,21 @@
"lint"
],
"dependencies": {
"buffer": "^5.6.0",
"debug": "^4.1.1",
"denque": "^1.4.1",
"err-code": "^2.0.0",
"it-pipe": "^1.0.1",
"libp2p-pubsub": "https://github.com/libp2p/js-libp2p-pubsub#v0.6.x",
"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 @@ -79,7 +78,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"
},
"peerDependencies": {
"libp2p": "https://github.com/libp2p/js-libp2p#0.29.x"
Expand Down
16 changes: 9 additions & 7 deletions test/2-nodes.spec.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
/* 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 delay = require('delay')
const uint8ArrayFromString = require('uint8arrays/from-string')


const { multicodec } = require('../src')
const Gossipsub = require('../src')
Expand Down Expand Up @@ -148,7 +150,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 @@ -162,7 +164,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 @@ -182,7 +184,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 @@ -193,7 +195,7 @@ describe('2 nodes', () => {
}

Array.from({ length: 10 }).forEach(() => {
nodes[1].publish(topic, Buffer.from('banana'))
nodes[1].publish(topic, uint8ArrayFromString('banana'))
})
})
})
Expand Down Expand Up @@ -250,8 +252,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
8 changes: 5 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 Gossipsub = require('../src')
const {
Expand All @@ -31,7 +32,8 @@ describe('emit self', () => {
it('should emit to self on publish', async () => {
gossipsub.subscribe(topic)
const promise = new Promise((resolve) => gossipsub.once(topic, resolve))
gossipsub.publish(topic, Buffer.from('hey'))

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

await promise
})
Expand All @@ -49,7 +51,7 @@ describe('emit self', () => {
gossipsub.subscribe(topic)
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
21 changes: 10 additions & 11 deletions test/floodsub.spec.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
/* eslint-env mocha */
'use strict'

const { Buffer } = require('buffer')
const chai = require('chai')
chai.use(require('dirty-chai'))

const delay = require('delay')
const uint8ArrayFromString = require('uint8arrays/from-string')

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

const { multicodec: floodsubMulticodec } = require('libp2p-floodsub')

const Gossipsub = require('../src')
const {
createPeer,
Expand Down Expand Up @@ -176,7 +175,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'))

const msg = await promise
expect(msg.data.toString()).to.equal('hey')
Expand All @@ -188,7 +187,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 @@ -209,7 +208,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 @@ -219,7 +218,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 @@ -236,7 +235,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 @@ -247,7 +246,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)))
msgs.forEach(msg => nodeGs.publish(topic, msg))
})
})
Expand Down Expand Up @@ -314,8 +313,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
8 changes: 4 additions & 4 deletions test/gossip-incoming.spec.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
/* 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 delay = require('delay')
const uint8ArrayFromString = require('uint8arrays/from-string')

const { GossipsubIDv11: multicodec } = require('../src/constants')
const {
createConnectedGossipsubs,
stopNode
Expand Down Expand Up @@ -47,7 +47,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 @@ -84,7 +84,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 delay(1000)

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
13 changes: 7 additions & 6 deletions test/multiple-nodes.spec.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
/* 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 delay = require('delay')
const promisify = require('promisify-es6')
const uint8ArrayFromString = require('uint8arrays/from-string')

const { GossipsubIDv11: multicodec } = require('../src/constants')
const {
createGossipsubs,
expectSet,
Expand Down Expand Up @@ -111,7 +112,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 @@ -162,7 +163,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 @@ -227,7 +228,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