Skip to content
This repository was archived by the owner on Jul 21, 2023. It is now read-only.

fix: replace node buffers with uint8arrays #115

Merged
merged 2 commits into from
Aug 11, 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
18 changes: 8 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,27 +39,25 @@
"homepage": "https://github.com/libp2p/js-libp2p-websockets#readme",
"dependencies": {
"abortable-iterator": "^3.0.0",
"buffer": "^5.5.0",
"class-is": "^1.1.0",
"debug": "^4.1.1",
"err-code": "^2.0.0",
"it-ws": "^3.0.0",
"libp2p-utils": "~0.1.0",
"mafmt": "^7.0.0",
"multiaddr": "^7.1.0",
"multiaddr-to-uri": "^5.0.0",
"libp2p-utils": "^0.2.0",
"mafmt": "^8.0.0",
"multiaddr": "^8.0.0",
"multiaddr-to-uri": "^6.0.0",
"p-timeout": "^3.2.0"
},
"devDependencies": {
"abort-controller": "^3.0.0",
"aegir": "^21.4.4",
"aegir": "^25.0.0",
"bl": "^4.0.0",
"chai": "^4.2.0",
"dirty-chai": "^2.0.1",
"it-goodbye": "^2.0.1",
"it-pipe": "^1.0.1",
"libp2p-interfaces": "^0.2.0",
"streaming-iterables": "^4.1.0"
"libp2p-interfaces": "^0.4.0",
"streaming-iterables": "^5.0.2",
"uint8arrays": "^1.1.0"
},
"contributors": [
"David Dias <daviddias.p@gmail.com>",
Expand Down
3 changes: 1 addition & 2 deletions src/socket-to-conn.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict'

const { Buffer } = require('buffer')
const abortable = require('abortable-iterator')
const { CLOSE_TIMEOUT } = require('./constants')
const toMultiaddr = require('libp2p-utils/src/ip-port-to-multiaddr')
Expand All @@ -24,7 +23,7 @@ module.exports = (stream, options = {}) => {
await stream.sink((async function * () {
for await (const chunk of source) {
// Convert BufferList to Buffer
yield Buffer.isBuffer(chunk) ? chunk : chunk.slice()
yield chunk instanceof Uint8Array ? chunk : chunk.slice()
}
})())
} catch (err) {
Expand Down
12 changes: 5 additions & 7 deletions test/browser.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
/* eslint-env mocha */
'use strict'

const chai = require('chai')
const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)
const { expect } = require('aegir/utils/chai')

const multiaddr = require('multiaddr')
const pipe = require('it-pipe')
const goodbye = require('it-goodbye')
const { collect, take } = require('streaming-iterables')
const uint8ArrayFromString = require('uint8arrays/from-string')

const WS = require('../src')

Expand All @@ -29,7 +27,7 @@ describe('libp2p-websockets', () => {
})

it('echo', async () => {
const message = Buffer.from('Hello World!')
const message = uint8ArrayFromString('Hello World!')
const s = goodbye({ source: [message], sink: collect })

const results = await pipe(s, conn, s)
Expand All @@ -38,7 +36,7 @@ describe('libp2p-websockets', () => {

describe('stress', () => {
it('one big write', async () => {
const rawMessage = Buffer.allocUnsafe(1000000).fill('a')
const rawMessage = new Uint8Array(1000000).fill('a')

const s = goodbye({ source: [rawMessage], sink: collect })

Expand All @@ -52,7 +50,7 @@ describe('libp2p-websockets', () => {
source: pipe(
{
[Symbol.iterator] () { return this },
next: () => ({ done: false, value: Buffer.from(Math.random().toString()) })
next: () => ({ done: false, value: uint8ArrayFromString(Math.random().toString()) })
},
take(20000)
),
Expand Down
18 changes: 8 additions & 10 deletions test/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@
const https = require('https')
const fs = require('fs')

const chai = require('chai')
const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)
const { expect } = require('aegir/utils/chai')
const multiaddr = require('multiaddr')
const goodbye = require('it-goodbye')
const { collect } = require('streaming-iterables')
const pipe = require('it-pipe')
const BufferList = require('bl/BufferList')
const uint8ArrayFromString = require('uint8arrays/from-string')

const WS = require('../src')

Expand Down Expand Up @@ -211,7 +209,7 @@ describe('dial', () => {

const result = await pipe(s, conn, s)

expect(result).to.be.eql([Buffer.from('hey')])
expect(result).to.be.eql([uint8ArrayFromString('hey')])
})

it('dial with p2p Id', async () => {
Expand All @@ -221,7 +219,7 @@ describe('dial', () => {

const result = await pipe(s, conn, s)

expect(result).to.be.eql([Buffer.from('hey')])
expect(result).to.be.eql([uint8ArrayFromString('hey')])
})

it('should resolve port 0', async () => {
Expand Down Expand Up @@ -276,7 +274,7 @@ describe('dial', () => {

const result = await pipe(s, conn, s)

expect(result).to.be.eql([Buffer.from('hey')])
expect(result).to.be.eql([uint8ArrayFromString('hey')])
})
})

Expand All @@ -299,7 +297,7 @@ describe('dial', () => {

const result = await pipe(s, conn, s)

expect(result).to.be.eql([Buffer.from('hey')])
expect(result).to.be.eql([uint8ArrayFromString('hey')])
})

it('dial and use BufferList', async () => {
Expand All @@ -308,7 +306,7 @@ describe('dial', () => {

const result = await pipe(s, conn, s)

expect(result).to.be.eql([Buffer.from('hey')])
expect(result).to.be.eql([uint8ArrayFromString('hey')])
})

it('dial with p2p Id', async () => {
Expand All @@ -321,7 +319,7 @@ describe('dial', () => {
})

const result = await pipe(s, conn, s)
expect(result).to.be.eql([Buffer.from('hey')])
expect(result).to.be.eql([uint8ArrayFromString('hey')])
})
})
})
Expand Down