Skip to content
This repository has been archived by the owner on Oct 19, 2022. It is now read-only.

fix: replace node buffers with uint8arrays #90

Merged
merged 2 commits into from
Aug 12, 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ const { stream: dhtStream, protocol } = await mss.select([
// ...it might then do something like this:
// try {
// await pipe(
// [Buffer.from('Some DHT data')]
// [uint8ArrayFromString('Some DHT data')]
// dhtStream,
// async source => {
// for await (const chunk of source)
Expand Down
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,23 @@
"homepage": "https://github.com/multiformats/js-multistream-select#readme",
"dependencies": {
"bl": "^4.0.0",
"buffer": "^5.2.1",
"debug": "^4.1.1",
"err-code": "^2.0.0",
"it-handshake": "^1.0.0",
"it-handshake": "^1.0.2",
"it-length-prefixed": "^3.0.0",
"it-pipe": "^1.0.1",
"it-reader": "^2.0.0",
"p-defer": "^3.0.0"
"p-defer": "^3.0.0",
"uint8arrays": "^1.1.0"
},
"devDependencies": {
"aegir": "^20.0.0",
"aegir": "^25.1.0",
"chai": "^4.2.0",
"dirty-chai": "^2.0.1",
"it-pair": "^1.0.0",
"mocha": "^6.2.0",
"mocha": "^8.1.1",
"p-timeout": "^3.2.0",
"streaming-iterables": "^4.1.0",
"streaming-iterables": "^5.0.2",
"varint": "^5.0.0"
},
"contributors": [
Expand Down
4 changes: 2 additions & 2 deletions src/multistream.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
'use strict'

const { Buffer } = require('buffer')
const BufferList = require('bl/BufferList')
const lp = require('it-length-prefixed')
const pipe = require('it-pipe')
const errCode = require('err-code')
const uint8ArrayFromString = require('uint8arrays/from-string')

const NewLine = Buffer.from('\n')
const NewLine = uint8ArrayFromString('\n')

async function oneChunk (source) {
for await (const chunk of source) return chunk // We only need one!
Expand Down
12 changes: 5 additions & 7 deletions test/dialer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,17 @@
/* eslint-env mocha */
/* eslint max-nested-callbacks: ["error", 5] */

const chai = require('chai')
chai.use(require('dirty-chai'))
const { expect } = chai
const { expect } = require('aegir/utils/chai')
const pipe = require('it-pipe')
const { collect } = require('streaming-iterables')
const Crypto = require('crypto')
const BufferList = require('bl/BufferList')
const Pair = require('it-pair')
const Reader = require('it-reader')
const pTimeout = require('p-timeout')
const throwsAsync = require('./helpers/throws-async')
const Multistream = require('../src/multistream')
const MSS = require('../')
const randomBytes = require('./helpers/random-bytes')

describe('Dialer', () => {
describe('dialer.select', () => {
Expand All @@ -27,7 +25,7 @@ describe('Dialer', () => {
expect(selection.protocol).to.equal(protocol)

// Ensure stream is usable after selection
const input = [Crypto.randomBytes(10), Crypto.randomBytes(64), Crypto.randomBytes(3)]
const input = [randomBytes(10), randomBytes(64), randomBytes(3)]
const output = await pipe(input, selection.stream, collect)
expect(BufferList(output).slice()).to.eql(BufferList(input).slice())
})
Expand Down Expand Up @@ -87,7 +85,7 @@ describe('Dialer', () => {
expect(selection.protocol).to.equal(selectedProtocol)

// Ensure stream is usable after selection
const input = [Crypto.randomBytes(10), Crypto.randomBytes(64), Crypto.randomBytes(3)]
const input = [randomBytes(10), randomBytes(64), randomBytes(3)]
const output = await pipe(input, selection.stream, collect)
expect(BufferList(output).slice()).to.eql(BufferList(input).slice())
})
Expand Down Expand Up @@ -169,7 +167,7 @@ describe('Dialer', () => {
expect(selection.protocol).to.equal(selectedProtocol)

// Ensure stream is usable after selection
const input = [Crypto.randomBytes(10), Crypto.randomBytes(64), Crypto.randomBytes(3)]
const input = [randomBytes(10), randomBytes(64), randomBytes(3)]
const output = await pipe(input, selection.stream, collect)
expect(BufferList(output).slice()).to.eql(BufferList(input).slice())
})
Expand Down
11 changes: 11 additions & 0 deletions test/helpers/random-bytes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict'

function getRandomInt (max) {
return Math.floor(Math.random() * Math.floor(max))
}

function randomBytes (num) {
return new Uint8Array(num).map(() => getRandomInt(256))
}

module.exports = randomBytes
6 changes: 3 additions & 3 deletions test/integration.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ chai.use(require('dirty-chai'))
const { expect } = chai
const pipe = require('it-pipe')
const { collect } = require('streaming-iterables')
const Crypto = require('crypto')
const BufferList = require('bl/BufferList')
const DuplexPair = require('it-pair/duplex')
const randomBytes = require('./helpers/random-bytes')
const MSS = require('../')

describe('Dialer and Listener integration', () => {
Expand All @@ -29,7 +29,7 @@ describe('Dialer and Listener integration', () => {
expect(listenerSelection.protocol).to.equal(selectedProtocol)

// Ensure stream is usable after selection
const input = [Crypto.randomBytes(10), Crypto.randomBytes(64), Crypto.randomBytes(3)]
const input = [randomBytes(10), randomBytes(64), randomBytes(3)]
const output = await Promise.all([
pipe(input, dialerSelection.stream, collect),
pipe(listenerSelection.stream, listenerSelection.stream)
Expand Down Expand Up @@ -58,7 +58,7 @@ describe('Dialer and Listener integration', () => {
expect(listenerSelection.protocol).to.equal(selectedProtocol)

// Ensure stream is usable after selection
const input = [Crypto.randomBytes(10), Crypto.randomBytes(64), Crypto.randomBytes(3)]
const input = [randomBytes(10), randomBytes(64), randomBytes(3)]
const output = await Promise.all([
pipe(input, dialerSelection.stream, collect),
pipe(listenerSelection.stream, listenerSelection.stream)
Expand Down
8 changes: 4 additions & 4 deletions test/listener.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ const chai = require('chai')
chai.use(require('dirty-chai'))
const { expect } = chai
const pipe = require('it-pipe')
const Crypto = require('crypto')
const BufferList = require('bl/BufferList')
const Reader = require('it-reader')
const { collect } = require('streaming-iterables')
const Lp = require('it-length-prefixed')
const Multistream = require('../src/multistream')
const randomBytes = require('./helpers/random-bytes')
const MSS = require('../')

describe('Listener', () => {
describe('listener.handle', () => {
it('should handle a protocol', async () => {
const protocol = '/echo/1.0.0'
const input = [Crypto.randomBytes(10), Crypto.randomBytes(64), Crypto.randomBytes(3)]
const input = [randomBytes(10), randomBytes(64), randomBytes(3)]

const duplex = {
sink: async source => {
Expand Down Expand Up @@ -54,7 +54,7 @@ describe('Listener', () => {
const protocols = ['/echo/2.0.0', '/echo/1.0.0']
const handledProtocols = ['/test/1.0.0', protocols[protocols.length - 1]]
const handledProtocol = protocols[protocols.length - 1]
const input = [Crypto.randomBytes(10), Crypto.randomBytes(64), Crypto.randomBytes(3)]
const input = [randomBytes(10), randomBytes(64), randomBytes(3)]

const duplex = {
sink: async source => {
Expand Down Expand Up @@ -97,7 +97,7 @@ describe('Listener', () => {
const protocols = ['/echo/2.0.0', '/echo/1.0.0']
const handledProtocols = ['/test/1.0.0', protocols[protocols.length - 1]]
const handledProtocol = protocols[protocols.length - 1]
const input = [Crypto.randomBytes(10), Crypto.randomBytes(64), Crypto.randomBytes(3)]
const input = [randomBytes(10), randomBytes(64), randomBytes(3)]

const duplex = {
sink: async source => {
Expand Down
40 changes: 21 additions & 19 deletions test/multistream.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,32 @@ const BufferList = require('bl/BufferList')
const Reader = require('it-reader')
const throwsAsync = require('./helpers/throws-async')
const Multistream = require('../src/multistream')
const uint8ArrayFromString = require('uint8arrays/from-string')
const uint8ArrayConcat = require('uint8arrays/concat')

describe('Multistream', () => {
describe('Multistream.encode', () => {
it('should encode data Buffer as a multistream-select message', () => {
const input = Buffer.from(`TEST${Date.now()}`)
const input = uint8ArrayFromString(`TEST${Date.now()}`)
const output = Multistream.encode(input)

const expected = Buffer.concat([
Buffer.from(Varint.encode(input.length + 1)), // +1 to include newline
const expected = uint8ArrayConcat([
Uint8Array.from(Varint.encode(input.length + 1)), // +1 to include newline
input,
Buffer.from('\n')
uint8ArrayFromString('\n')
])

expect(output.slice()).to.eql(expected)
})

it('should encode data BufferList as a multistream-select message', () => {
const input = new BufferList([Buffer.from('TEST'), Buffer.from(`${Date.now()}`)])
const input = new BufferList([uint8ArrayFromString('TEST'), uint8ArrayFromString(`${Date.now()}`)])
const output = Multistream.encode(input)

const expected = Buffer.concat([
Buffer.from(Varint.encode(input.length + 1)), // +1 to include newline
const expected = uint8ArrayConcat([
Uint8Array.from(Varint.encode(input.length + 1)), // +1 to include newline
input.slice(),
Buffer.from('\n')
uint8ArrayFromString('\n')
])

expect(output.slice()).to.eql(expected)
Expand All @@ -41,16 +43,16 @@ describe('Multistream', () => {

describe('Multistream.write', () => {
it('should encode and write a multistream-select message', () => {
const input = Buffer.from(`TEST${Date.now()}`)
const input = uint8ArrayFromString(`TEST${Date.now()}`)
const output = []
const mockWriter = { push: d => output.push(d) }

Multistream.write(mockWriter, input)

const expected = Buffer.concat([
Buffer.from(Varint.encode(input.length + 1)), // +1 to include newline
const expected = uint8ArrayConcat([
Uint8Array.from(Varint.encode(input.length + 1)), // +1 to include newline
input,
Buffer.from('\n')
uint8ArrayFromString('\n')
])

expect(output.length).to.equal(1)
Expand All @@ -60,23 +62,23 @@ describe('Multistream', () => {

describe('Multistream.read', () => {
it('should decode a multistream-select message', async () => {
const input = Buffer.from(`TEST${Date.now()}`)
const input = uint8ArrayFromString(`TEST${Date.now()}`)

const reader = Reader([Buffer.concat([
Buffer.from(Varint.encode(input.length + 1)), // +1 to include newline
const reader = Reader([uint8ArrayConcat([
Uint8Array.from(Varint.encode(input.length + 1)), // +1 to include newline
input,
Buffer.from('\n')
uint8ArrayFromString('\n')
])])

const output = await Multistream.read(reader)
expect(output.slice()).to.eql(input)
})

it('should throw for non-newline delimited message', async () => {
const input = Buffer.from(`TEST${Date.now()}`)
const input = uint8ArrayFromString(`TEST${Date.now()}`)

const reader = Reader([Buffer.concat([
Buffer.from(Varint.encode(input.length)),
const reader = Reader([uint8ArrayConcat([
Uint8Array.from(Varint.encode(input.length)),
input
])])

Expand Down