-
Notifications
You must be signed in to change notification settings - Fork 21
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
All useages of node `Buffer`s have been replaced with `Uint8Array`s. BREAKING CHANGES: - Where node `Buffer`s were returned, they are now `Uint8Array`s
- Loading branch information
1 parent
27c2ec1
commit 688a071
Showing
7 changed files
with
46 additions
and
46 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,37 @@ | ||
'use strict' | ||
|
||
const varint = require('varint') | ||
const { Buffer } = require('buffer') | ||
const uint8ArrayToString = require('uint8arrays/to-string') | ||
const uint8ArrayFromString = require('uint8arrays/from-string') | ||
|
||
module.exports = { | ||
numberToBuffer, | ||
bufferToNumber, | ||
varintBufferEncode, | ||
varintBufferDecode, | ||
numberToUint8Array, | ||
uint8ArrayToNumber, | ||
varintUint8ArrayEncode, | ||
varintUint8ArrayDecode, | ||
varintEncode | ||
} | ||
|
||
function bufferToNumber (buf) { | ||
return parseInt(buf.toString('hex'), 16) | ||
function uint8ArrayToNumber (buf) { | ||
return parseInt(uint8ArrayToString(buf, 'base16'), 16) | ||
} | ||
|
||
function numberToBuffer (num) { | ||
function numberToUint8Array (num) { | ||
let hexString = num.toString(16) | ||
if (hexString.length % 2 === 1) { | ||
hexString = '0' + hexString | ||
} | ||
return Buffer.from(hexString, 'hex') | ||
return uint8ArrayFromString(hexString, 'base16') | ||
} | ||
|
||
function varintBufferEncode (input) { | ||
return Buffer.from(varint.encode(bufferToNumber(input))) | ||
function varintUint8ArrayEncode (input) { | ||
return Uint8Array.from(varint.encode(uint8ArrayToNumber(input))) | ||
} | ||
|
||
function varintBufferDecode (input) { | ||
return numberToBuffer(varint.decode(input)) | ||
function varintUint8ArrayDecode (input) { | ||
return numberToUint8Array(varint.decode(input)) | ||
} | ||
|
||
function varintEncode (num) { | ||
return Buffer.from(varint.encode(num)) | ||
return Uint8Array.from(varint.encode(num)) | ||
} |
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