Skip to content

Commit

Permalink
fix: remove buffers for better browser compat (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
achingbrain authored Jun 23, 2022
1 parent 8c00b4f commit 5a85d0a
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 19 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ const room = Room(libp2p, 'some-room-name')

## room.broadcast(message)

Broacasts message (string or buffer).
Broacasts message (string or Uint8Array).

## room.sendTo(cid, message)

Sends message (string or buffer) to peer.
Sends message (string or Uint8Array) to peer.

## async room.leave()

Expand All @@ -94,7 +94,7 @@ Returns a boolean indicating if the given peer is present in the room.
Listens for messages. A `message` is an object containing the following properties:

* `from` (string): peer id
* `data` (Buffer): message content
* `data` (Uint8Array): message content

## room.on('peer joined', (cid) => {})

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
"dependencies": {
"hyperdiff": "^2.0.5",
"it-pipe": "^1.1.0",
"lodash.clonedeep": "^4.5.0"
"lodash.clonedeep": "^4.5.0",
"uint8arrays": "^3.0.0"
},
"devDependencies": {
"aegir": "^20.5.1",
Expand Down
5 changes: 3 additions & 2 deletions src/direct-connection-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const EventEmitter = require('events')
const pipe = require('it-pipe')
const { fromString: uint8ArrayFromString } = require('uint8arrays/from-string')

const emitter = new EventEmitter()

Expand Down Expand Up @@ -32,8 +33,8 @@ function handler ({ connection, stream }) {
continue // early
}

msg.data = Buffer.from(msg.data, 'hex')
msg.seqno = Buffer.from(msg.seqno, 'hex')
msg.data = uint8ArrayFromString(msg.data, 'hex')
msg.seqno = uint8ArrayFromString(msg.seqno.padStart(msg.seqno.length % 2 === 0 ? msg.seqno.length : msg.seqno.length + 1, '0'), 'hex')

topicIDs.forEach((topic) => {
emitter.emit(topic, msg)
Expand Down
6 changes: 4 additions & 2 deletions src/encoding.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
'use strict'

const { fromString: uint8arrayFromString } = require('uint8arrays/from-string')

module.exports = (_message) => {
let message = _message
if (!Buffer.isBuffer(message)) {
message = Buffer.from(message)
if (!(message instanceof Uint8Array)) {
message = uint8arrayFromString(message)
}
return message
}
12 changes: 7 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const PROTOCOL = require('./protocol')
const Connection = require('./connection')
const encoding = require('./encoding')
const directConnection = require('./direct-connection-handler')
const { fromString: uint8ArrayFromString } = require('uint8arrays/from-string')
const { toString: uint8ArrayToString } = require('uint8arrays/to-string')

const DEFAULT_OPTIONS = {
pollInterval: 1000
Expand Down Expand Up @@ -83,23 +85,23 @@ class PubSubRoom extends EventEmitter {
}

// We should use the same sequence number generation as js-libp2p-floosub does:
// const seqno = Buffer.from(utils.randomSeqno())
// const seqno = Uint8Array.from(utils.randomSeqno())

// Until we figure out a good way to bring in the js-libp2p-floosub's randomSeqno
// generator, let's use 0 as the sequence number for all private messages
// const seqno = Buffer.from([0])
const seqno = Buffer.from([0])
// const seqno = Uint8Array.from([0])
const seqno = Uint8Array.from([0])

const msg = {
to: peer,
from: this._libp2p.peerInfo.id.toB58String(),
data: Buffer.from(message).toString('hex'),
data: uint8ArrayToString(uint8ArrayFromString(message), 'hex'),
seqno: seqno.toString('hex'),
topicIDs: [this._topic],
topicCIDs: [this._topic]
}

conn.push(Buffer.from(JSON.stringify(msg)))
conn.push(uint8ArrayFromString(JSON.stringify(msg)))
}

async _pollPeers () {
Expand Down
6 changes: 3 additions & 3 deletions test/concurrent-rooms.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
const chai = require('chai')
chai.use(require('dirty-chai'))
const expect = chai.expect

const { toString: uint8ArrayToString } = require('uint8arrays/to-string')
const delay = require('delay')

const PubSubRoom = require('../')
Expand Down Expand Up @@ -107,10 +107,10 @@ describe('concurrent rooms', function () {
room2B.on('message', crash)
room2A.once('message', (message) => {
expect(message.from.toString()).to.equal(id1.toString())
expect(message.seqno.toString()).to.equal(Buffer.from([0]).toString())
expect(message.seqno.toString()).to.equal(Uint8Array.from([0]).toString())
expect(message.topicIDs).to.deep.equal([topicA])
expect(message.topicCIDs).to.deep.equal([topicA])
expect(message.data.toString()).to.equal('message 2')
expect(uint8ArrayToString(message.data)).to.equal('message 2')
room2B.removeListener('message', crash)
done()
})
Expand Down
6 changes: 3 additions & 3 deletions test/room.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
const chai = require('chai')
chai.use(require('dirty-chai'))
const expect = chai.expect

const { toString: uint8ArrayToString } = require('uint8arrays/to-string')
const PubSubRoom = require('../')
const createLibp2p = require('./utils/create-libp2p')

Expand Down Expand Up @@ -89,10 +89,10 @@ describe('room', function () {
}
gotMessage = true
expect(message.from).to.deep.equal(id1)
expect(message.seqno.toString()).to.equal(Buffer.from([0]).toString())
expect(message.seqno.toString()).to.equal(Uint8Array.from([0]).toString())
expect(message.topicIDs).to.deep.equal([topic])
expect(message.topicCIDs).to.deep.equal([topic])
expect(message.data.toString()).to.equal('message 2')
expect(uint8ArrayToString(message.data)).to.equal('message 2')
done()
})
rooms[n].a.sendTo(id2, 'message 2')
Expand Down

0 comments on commit 5a85d0a

Please sign in to comment.