This repository has been archived by the owner on Jul 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: replace node buffers with uint8arrays (#114)
* fix: replace node buffers with uint8arrays BREAKING CHANGE: - All use of node Buffers has been replaced with Uint8Arrays * fix: keep allocUnsafe for node for performance Co-authored-by: Jacob Heun <jacobheun@gmail.com>
- Loading branch information
1 parent
c44bf9a
commit d005338
Showing
9 changed files
with
111 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
'use strict' | ||
|
||
module.exports = { | ||
webpack: { | ||
node: { | ||
// needed by random-bytes | ||
crypto: true, | ||
|
||
// needed by cipher-base | ||
stream: true, | ||
|
||
// needed by core-util-is | ||
Buffer: true | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
'use strict' | ||
|
||
const varint = require('varint') | ||
const BufferList = require('bl/BufferList') | ||
|
||
const POOL_SIZE = 10 * 1024 | ||
|
||
class Encoder { | ||
constructor () { | ||
this._pool = new Uint8Array(POOL_SIZE) | ||
this._poolOffset = 0 | ||
} | ||
|
||
/** | ||
* Encodes the given message and returns it and its header | ||
* @param {*} msg The message object to encode | ||
* @returns {Uint8Array|Uint8Array[]} | ||
*/ | ||
write (msg) { | ||
const pool = this._pool | ||
let offset = this._poolOffset | ||
|
||
varint.encode(msg.id << 3 | msg.type, pool, offset) | ||
offset += varint.encode.bytes | ||
varint.encode(msg.data ? msg.data.length : 0, pool, offset) | ||
offset += varint.encode.bytes | ||
|
||
const header = pool.subarray(this._poolOffset, offset) | ||
|
||
if (POOL_SIZE - offset < 100) { | ||
this._pool = new Uint8Array(POOL_SIZE) | ||
this._poolOffset = 0 | ||
} else { | ||
this._poolOffset = offset | ||
} | ||
|
||
if (!msg.data) return header | ||
|
||
return [header, msg.data] | ||
} | ||
} | ||
|
||
const encoder = new Encoder() | ||
|
||
// Encode one or more messages and yield a BufferList of encoded messages | ||
module.exports = source => (async function * encode () { | ||
for await (const msg of source) { | ||
if (Array.isArray(msg)) { | ||
yield new BufferList(msg.map(m => encoder.write(m))) | ||
} else { | ||
yield new BufferList(encoder.write(msg)) | ||
} | ||
} | ||
})() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters