Skip to content

Commit 01c490e

Browse files
authored
Merge pull request #125 from achingbrain/fix/remove-node-buffers
fix: remove node buffers
2 parents 04c9009 + d7c517b commit 01c490e

30 files changed

Lines changed: 2538 additions & 2087 deletions

.aegir.js

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,13 @@
11
const path = require('path')
22

3-
/** @type {import('aegir').Options["build"]["config"]} */
4-
const esbuild = {
5-
inject: [path.join(__dirname, 'test/fixtures/node-globals.js')]
6-
}
7-
8-
93
/** @type {import('aegir').PartialOptions} */
104
const config = {
115
tsRepo: true,
126
docs: {
137
entryPoint: "src/index.ts"
148
},
15-
test: {
16-
browser :{
17-
config: {
18-
buildConfig: esbuild
19-
}
20-
}
21-
},
229
build: {
23-
bundlesizeMax: '231KB',
24-
config: esbuild
10+
bundlesizeMax: '231KB'
2511
}
2612
}
2713

.github/workflows/ci.yml

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
name: Lint and Typecheck
1414
runs-on: ubuntu-latest
1515
steps:
16-
- uses: actions/setup-node@v1
16+
- uses: actions/setup-node@v2
1717
with:
1818
node-version: 16
1919
- uses: actions/checkout@v2
@@ -44,7 +44,7 @@ jobs:
4444
os: ["ubuntu-latest", "macos-latest", "windows-latest"]
4545
runs-on: ${{ matrix.os }}
4646
steps:
47-
- uses: actions/setup-node@v1
47+
- uses: actions/setup-node@v2
4848
with:
4949
node-version: ${{ matrix.node }}
5050
- uses: actions/checkout@v2
@@ -67,7 +67,7 @@ jobs:
6767
needs: check
6868
runs-on: ubuntu-latest
6969
steps:
70-
- uses: actions/setup-node@v1
70+
- uses: actions/setup-node@v2
7171
with:
7272
node-version: 16
7373
- uses: actions/checkout@v2
@@ -89,7 +89,7 @@ jobs:
8989
needs: check
9090
runs-on: ubuntu-latest
9191
steps:
92-
- uses: actions/setup-node@v1
92+
- uses: actions/setup-node@v2
9393
with:
9494
node-version: 16
9595
- uses: actions/checkout@v2
@@ -111,7 +111,7 @@ jobs:
111111
needs: check
112112
runs-on: ubuntu-latest
113113
steps:
114-
- uses: actions/setup-node@v1
114+
- uses: actions/setup-node@v2
115115
with:
116116
node-version: 16
117117
- uses: actions/checkout@v2
@@ -134,7 +134,7 @@ jobs:
134134
needs: check
135135
runs-on: ubuntu-latest
136136
steps:
137-
- uses: actions/setup-node@v1
137+
- uses: actions/setup-node@v2
138138
with:
139139
node-version: 16
140140
- uses: actions/checkout@v2
@@ -156,7 +156,7 @@ jobs:
156156
needs: check
157157
runs-on: ubuntu-latest
158158
steps:
159-
- uses: actions/setup-node@v1
159+
- uses: actions/setup-node@v2
160160
with:
161161
node-version: 16
162162
- uses: actions/checkout@v2
@@ -172,3 +172,5 @@ jobs:
172172
${{ runner.os }}-yarn-
173173
- run: yarn --prefer-offline --frozen-lockfile
174174
- run: npx xvfb-maybe aegir test -t electron-renderer --bail
175+
env:
176+
ESBUILD_WORKER_THREADS: 0

package.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,30 +33,33 @@
3333
"proto:gen": "pbjs -t static-module -r libp2p-noise -o ./src/proto/payload.js ./src/proto/payload.proto && pbts -o ./src/proto/payload.d.ts ./src/proto/payload.js && yarn run lint --fix"
3434
},
3535
"browser": {
36+
"./dist/src/alloc-unsafe.js": "./dist/src/alloc-unsafe-browser.js",
3637
"util": false
3738
},
3839
"devDependencies": {
3940
"@types/bl": "^5.0.2",
40-
"aegir": "^33.1.0",
41+
"aegir": "^36.1.1",
4142
"benchmark": "^2.1.4",
4243
"buffer": "^6.0.3",
4344
"chai": "^4.3.4",
4445
"events": "^3.3.0",
4546
"libp2p-crypto": "^0.21.0",
4647
"microtime": "^3.0.0",
4748
"mocha": "^9.0.2",
48-
"sinon": "^11.1.1"
49+
"sinon": "^12.0.1",
50+
"util": "^0.12.4"
4951
},
5052
"dependencies": {
5153
"@stablelib/chacha20poly1305": "^1.0.1",
5254
"@stablelib/hkdf": "^1.0.1",
5355
"@stablelib/sha256": "^1.0.1",
5456
"@stablelib/x25519": "^1.0.1",
57+
"bl": "^5.0.0",
5558
"debug": "^4.3.1",
5659
"it-buffer": "^0.1.3",
5760
"it-length-prefixed": "^5.0.3",
5861
"it-pair": "^1.0.0",
59-
"it-pb-rpc": "^0.1.11",
62+
"it-pb-rpc": "^0.2.0",
6063
"it-pipe": "^1.1.0",
6164
"peer-id": "^0.16.0",
6265
"protobufjs": "^6.11.2",
@@ -74,6 +77,7 @@
7477
},
7578
"ignorePatterns": [
7679
"src/proto/payload.js",
80+
"src/proto/payload.d.ts",
7781
"test/fixtures/node-globals.js"
7882
]
7983
}

src/@types/basic.d.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
import { Buffer } from 'buffer'
2-
3-
export type bytes = Buffer
4-
export type bytes32 = Buffer
5-
export type bytes16 = Buffer
1+
export type bytes = Uint8Array
2+
export type bytes32 = Uint8Array
3+
export type bytes16 = Uint8Array
64

75
export type uint32 = number
86
export type uint64 = number

src/@types/handshake-interface.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import PeerId from 'peer-id'
55
export interface IHandshake {
66
session: NoiseSession
77
remotePeer: PeerId
8-
remoteEarlyData: Buffer
8+
remoteEarlyData: bytes
99
encrypt: (plaintext: bytes, session: NoiseSession) => bytes
1010
decrypt: (ciphertext: bytes, session: NoiseSession) => {plaintext: bytes, valid: boolean}
1111
}

src/@types/it-length-prefixed/index.d.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
declare module 'it-length-prefixed' {
2-
import BufferList from 'bl'
3-
import { Buffer } from 'buffer'
2+
import BufferList from 'bl/BufferList'
43

54
interface LengthDecoderFunction {
6-
(data: Buffer | BufferList): number
5+
(data: Uint8Array | BufferList): number
76
bytes: number
87
}
98

109
interface LengthEncoderFunction {
11-
(value: number, target: Buffer, offset: number): number|Buffer
10+
(value: number, target: Uint8Array, offset: number): number|Uint8Array
1211
bytes: number
1312
}
1413

1514
interface Encoder {
16-
(options?: Partial<{lengthEncoder: LengthEncoderFunction}>): AsyncGenerator<BufferList, Buffer>
17-
single: (chunk: Buffer, options?: Partial<{lengthEncoder: LengthEncoderFunction}>) => BufferList
15+
(options?: Partial<{lengthEncoder: LengthEncoderFunction}>): AsyncGenerator<BufferList, Uint8Array>
16+
single: (chunk: Uint8Array, options?: Partial<{lengthEncoder: LengthEncoderFunction}>) => BufferList
1817
MIN_POOL_SIZE: number
1918
DEFAULT_POOL_SIZE: number
2019
}

src/@types/libp2p.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ export interface INoiseConnection {
1414

1515
export interface SecureOutbound {
1616
conn: any
17-
remoteEarlyData: Buffer
17+
remoteEarlyData: Uint8Array
1818
remotePeer: PeerId
1919
}

src/crypto.ts

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { Buffer } from 'buffer'
21
import { IHandshake } from './@types/handshake-interface'
32
import { NOISE_MSG_MAX_LENGTH_BYTES, NOISE_MSG_MAX_LENGTH_BYTES_WITHOUT_TAG } from './constants'
43

@@ -10,15 +9,13 @@ interface IReturnEncryptionWrapper {
109
export function encryptStream (handshake: IHandshake): IReturnEncryptionWrapper {
1110
return async function * (source) {
1211
for await (const chunk of source) {
13-
const chunkBuffer = Buffer.from(chunk.buffer, chunk.byteOffset, chunk.length)
14-
15-
for (let i = 0; i < chunkBuffer.length; i += NOISE_MSG_MAX_LENGTH_BYTES_WITHOUT_TAG) {
12+
for (let i = 0; i < chunk.length; i += NOISE_MSG_MAX_LENGTH_BYTES_WITHOUT_TAG) {
1613
let end = i + NOISE_MSG_MAX_LENGTH_BYTES_WITHOUT_TAG
17-
if (end > chunkBuffer.length) {
18-
end = chunkBuffer.length
14+
if (end > chunk.length) {
15+
end = chunk.length
1916
}
2017

21-
const data = handshake.encrypt(chunkBuffer.slice(i, end), handshake.session)
18+
const data = handshake.encrypt(chunk.slice(i, end), handshake.session)
2219
yield data
2320
}
2421
}
@@ -29,16 +26,13 @@ export function encryptStream (handshake: IHandshake): IReturnEncryptionWrapper
2926
export function decryptStream (handshake: IHandshake): IReturnEncryptionWrapper {
3027
return async function * (source) {
3128
for await (const chunk of source) {
32-
const chunkBuffer = Buffer.from(chunk.buffer, chunk.byteOffset, chunk.length)
33-
34-
for (let i = 0; i < chunkBuffer.length; i += NOISE_MSG_MAX_LENGTH_BYTES) {
29+
for (let i = 0; i < chunk.length; i += NOISE_MSG_MAX_LENGTH_BYTES) {
3530
let end = i + NOISE_MSG_MAX_LENGTH_BYTES
36-
if (end > chunkBuffer.length) {
37-
end = chunkBuffer.length
31+
if (end > chunk.length) {
32+
end = chunk.length
3833
}
3934

40-
const chunk = chunkBuffer.slice(i, end)
41-
const { plaintext: decrypted, valid } = await handshake.decrypt(chunk, handshake.session)
35+
const { plaintext: decrypted, valid } = await handshake.decrypt(chunk.slice(i, end), handshake.session)
4236
if (!valid) {
4337
throw new Error('Failed to validate decrypted chunk')
4438
}

src/encoder.ts

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,46 @@
1-
import { Buffer } from 'buffer'
21
import { bytes } from './@types/basic'
32
import { MessageBuffer } from './@types/handshake'
4-
import BufferList from 'bl'
3+
import BufferList from 'bl/BufferList'
4+
import { concat as uint8ArrayConcat } from 'uint8arrays/concat'
55

6-
export const uint16BEEncode = (value: number, target: Buffer, offset: number): Buffer => {
7-
target = target || Buffer.allocUnsafe(2)
8-
target.writeUInt16BE(value, offset)
6+
const allocUnsafe = (len: number): Uint8Array => {
7+
if (globalThis.Buffer) {
8+
return globalThis.Buffer.allocUnsafe(len)
9+
}
10+
11+
return new Uint8Array(len)
12+
}
13+
14+
export const uint16BEEncode = (value: number, target: Uint8Array, offset: number): Uint8Array => {
15+
target = target || allocUnsafe(2)
16+
new DataView(target.buffer, target.byteOffset, target.byteLength).setUint16(offset, value, false)
917
return target
1018
}
1119
uint16BEEncode.bytes = 2
1220

13-
export const uint16BEDecode = (data: Buffer | BufferList): number => {
21+
export const uint16BEDecode = (data: Uint8Array | BufferList): number => {
1422
if (data.length < 2) throw RangeError('Could not decode int16BE')
15-
return data.readUInt16BE(0)
23+
24+
if (data instanceof BufferList) {
25+
return data.readUInt16BE(0)
26+
}
27+
28+
return new DataView(data.buffer, data.byteOffset, data.byteLength).getUint16(0, false)
1629
}
1730
uint16BEDecode.bytes = 2
1831

1932
// Note: IK and XX encoder usage is opposite (XX uses in stages encode0 where IK uses encode1)
2033

2134
export function encode0 (message: MessageBuffer): bytes {
22-
return Buffer.concat([message.ne, message.ciphertext])
35+
return uint8ArrayConcat([message.ne, message.ciphertext], message.ne.length + message.ciphertext.length)
2336
}
2437

2538
export function encode1 (message: MessageBuffer): bytes {
26-
return Buffer.concat([message.ne, message.ns, message.ciphertext])
39+
return uint8ArrayConcat([message.ne, message.ns, message.ciphertext], message.ne.length + message.ns.length + message.ciphertext.length)
2740
}
2841

2942
export function encode2 (message: MessageBuffer): bytes {
30-
return Buffer.concat([message.ns, message.ciphertext])
43+
return uint8ArrayConcat([message.ns, message.ciphertext], message.ns.length + message.ciphertext.length)
3144
}
3245

3346
export function decode0 (input: bytes): MessageBuffer {
@@ -38,7 +51,7 @@ export function decode0 (input: bytes): MessageBuffer {
3851
return {
3952
ne: input.slice(0, 32),
4053
ciphertext: input.slice(32, input.length),
41-
ns: Buffer.alloc(0)
54+
ns: new Uint8Array(0)
4255
}
4356
}
4457

@@ -60,7 +73,7 @@ export function decode2 (input: bytes): MessageBuffer {
6073
}
6174

6275
return {
63-
ne: Buffer.alloc(0),
76+
ne: new Uint8Array(0),
6477
ns: input.slice(0, 48),
6578
ciphertext: input.slice(48, input.length)
6679
}

src/errors.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import BufferList from 'bl'
22

33
export class FailedIKError extends Error {
4-
public initialMsg: string|BufferList|Buffer
4+
public initialMsg: string|BufferList|Uint8Array
55

6-
constructor (initialMsg: string|BufferList|Buffer, message?: string) {
6+
constructor (initialMsg: string|BufferList|Uint8Array, message?: string) {
77
super(message)
88

99
this.initialMsg = initialMsg

0 commit comments

Comments
 (0)