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

Commit a277bf6

Browse files
authored
fix: replace node buffers with uint8arrays (#115)
* fix: replace node buffers with uint8arrays BREAKING CHANGES: - All deps used by this module now use Uint8Arrays in place of Buffers * chore: remove gh dep
1 parent a37c2bd commit a277bf6

File tree

4 files changed

+22
-29
lines changed

4 files changed

+22
-29
lines changed

package.json

+8-10
Original file line numberDiff line numberDiff line change
@@ -39,27 +39,25 @@
3939
"homepage": "https://github.com/libp2p/js-libp2p-websockets#readme",
4040
"dependencies": {
4141
"abortable-iterator": "^3.0.0",
42-
"buffer": "^5.5.0",
4342
"class-is": "^1.1.0",
4443
"debug": "^4.1.1",
4544
"err-code": "^2.0.0",
4645
"it-ws": "^3.0.0",
47-
"libp2p-utils": "~0.1.0",
48-
"mafmt": "^7.0.0",
49-
"multiaddr": "^7.1.0",
50-
"multiaddr-to-uri": "^5.0.0",
46+
"libp2p-utils": "^0.2.0",
47+
"mafmt": "^8.0.0",
48+
"multiaddr": "^8.0.0",
49+
"multiaddr-to-uri": "^6.0.0",
5150
"p-timeout": "^3.2.0"
5251
},
5352
"devDependencies": {
5453
"abort-controller": "^3.0.0",
55-
"aegir": "^21.4.4",
54+
"aegir": "^25.0.0",
5655
"bl": "^4.0.0",
57-
"chai": "^4.2.0",
58-
"dirty-chai": "^2.0.1",
5956
"it-goodbye": "^2.0.1",
6057
"it-pipe": "^1.0.1",
61-
"libp2p-interfaces": "^0.2.0",
62-
"streaming-iterables": "^4.1.0"
58+
"libp2p-interfaces": "^0.4.0",
59+
"streaming-iterables": "^5.0.2",
60+
"uint8arrays": "^1.1.0"
6361
},
6462
"contributors": [
6563
"David Dias <[email protected]>",

src/socket-to-conn.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
'use strict'
22

3-
const { Buffer } = require('buffer')
43
const abortable = require('abortable-iterator')
54
const { CLOSE_TIMEOUT } = require('./constants')
65
const toMultiaddr = require('libp2p-utils/src/ip-port-to-multiaddr')
@@ -24,7 +23,7 @@ module.exports = (stream, options = {}) => {
2423
await stream.sink((async function * () {
2524
for await (const chunk of source) {
2625
// Convert BufferList to Buffer
27-
yield Buffer.isBuffer(chunk) ? chunk : chunk.slice()
26+
yield chunk instanceof Uint8Array ? chunk : chunk.slice()
2827
}
2928
})())
3029
} catch (err) {

test/browser.js

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
/* eslint-env mocha */
22
'use strict'
33

4-
const chai = require('chai')
5-
const dirtyChai = require('dirty-chai')
6-
const expect = chai.expect
7-
chai.use(dirtyChai)
4+
const { expect } = require('aegir/utils/chai')
85

96
const multiaddr = require('multiaddr')
107
const pipe = require('it-pipe')
118
const goodbye = require('it-goodbye')
129
const { collect, take } = require('streaming-iterables')
10+
const uint8ArrayFromString = require('uint8arrays/from-string')
1311

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

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

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

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

3937
describe('stress', () => {
4038
it('one big write', async () => {
41-
const rawMessage = Buffer.allocUnsafe(1000000).fill('a')
39+
const rawMessage = new Uint8Array(1000000).fill('a')
4240

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

@@ -52,7 +50,7 @@ describe('libp2p-websockets', () => {
5250
source: pipe(
5351
{
5452
[Symbol.iterator] () { return this },
55-
next: () => ({ done: false, value: Buffer.from(Math.random().toString()) })
53+
next: () => ({ done: false, value: uint8ArrayFromString(Math.random().toString()) })
5654
},
5755
take(20000)
5856
),

test/node.js

+8-10
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@
55
const https = require('https')
66
const fs = require('fs')
77

8-
const chai = require('chai')
9-
const dirtyChai = require('dirty-chai')
10-
const expect = chai.expect
11-
chai.use(dirtyChai)
8+
const { expect } = require('aegir/utils/chai')
129
const multiaddr = require('multiaddr')
1310
const goodbye = require('it-goodbye')
1411
const { collect } = require('streaming-iterables')
1512
const pipe = require('it-pipe')
1613
const BufferList = require('bl/BufferList')
14+
const uint8ArrayFromString = require('uint8arrays/from-string')
1715

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

@@ -211,7 +209,7 @@ describe('dial', () => {
211209

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

214-
expect(result).to.be.eql([Buffer.from('hey')])
212+
expect(result).to.be.eql([uint8ArrayFromString('hey')])
215213
})
216214

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

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

224-
expect(result).to.be.eql([Buffer.from('hey')])
222+
expect(result).to.be.eql([uint8ArrayFromString('hey')])
225223
})
226224

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

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

279-
expect(result).to.be.eql([Buffer.from('hey')])
277+
expect(result).to.be.eql([uint8ArrayFromString('hey')])
280278
})
281279
})
282280

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

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

302-
expect(result).to.be.eql([Buffer.from('hey')])
300+
expect(result).to.be.eql([uint8ArrayFromString('hey')])
303301
})
304302

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

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

311-
expect(result).to.be.eql([Buffer.from('hey')])
309+
expect(result).to.be.eql([uint8ArrayFromString('hey')])
312310
})
313311

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

323321
const result = await pipe(s, conn, s)
324-
expect(result).to.be.eql([Buffer.from('hey')])
322+
expect(result).to.be.eql([uint8ArrayFromString('hey')])
325323
})
326324
})
327325
})

0 commit comments

Comments
 (0)