-
-
Notifications
You must be signed in to change notification settings - Fork 311
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement isomorphic utils for nodejs and browser (#7060)
* feat: implement isomorphic utils for nodes and browser * fix: avoid async import * chore: revise toHexString() comment as in PR review Co-authored-by: Nico Flaig <nflaig@protonmail.com> --------- Co-authored-by: Nico Flaig <nflaig@protonmail.com>
- Loading branch information
Showing
7 changed files
with
173 additions
and
35 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
export function toHex(bytes: Uint8Array): string { | ||
const charCodes = new Array<number>(bytes.length * 2 + 2); | ||
charCodes[0] = 48; | ||
charCodes[1] = 120; | ||
|
||
for (let i = 0; i < bytes.length; i++) { | ||
const byte = bytes[i]; | ||
const first = (byte & 0xf0) >> 4; | ||
const second = byte & 0x0f; | ||
|
||
// "0".charCodeAt(0) = 48 | ||
// "a".charCodeAt(0) = 97 => delta = 87 | ||
charCodes[2 + 2 * i] = first < 10 ? first + 48 : first + 87; | ||
charCodes[2 + 2 * i + 1] = second < 10 ? second + 48 : second + 87; | ||
} | ||
return String.fromCharCode(...charCodes); | ||
} | ||
|
||
const rootCharCodes = new Array<number>(32 * 2 + 2); | ||
// "0".charCodeAt(0) | ||
rootCharCodes[0] = 48; | ||
// "x".charCodeAt(0) | ||
rootCharCodes[1] = 120; | ||
|
||
/** | ||
* Convert a Uint8Array, length 32, to 0x-prefixed hex string | ||
*/ | ||
export function toRootHex(root: Uint8Array): string { | ||
if (root.length !== 32) { | ||
throw Error(`Expect root to be 32 bytes, got ${root.length}`); | ||
} | ||
|
||
for (let i = 0; i < root.length; i++) { | ||
const byte = root[i]; | ||
const first = (byte & 0xf0) >> 4; | ||
const second = byte & 0x0f; | ||
|
||
// "0".charCodeAt(0) = 48 | ||
// "a".charCodeAt(0) = 97 => delta = 87 | ||
rootCharCodes[2 + 2 * i] = first < 10 ? first + 48 : first + 87; | ||
rootCharCodes[2 + 2 * i + 1] = second < 10 ? second + 48 : second + 87; | ||
} | ||
return String.fromCharCode(...rootCharCodes); | ||
} | ||
|
||
export function fromHex(hex: string): Uint8Array { | ||
if (typeof hex !== "string") { | ||
throw new Error(`hex argument type ${typeof hex} must be of type string`); | ||
} | ||
|
||
if (hex.startsWith("0x")) { | ||
hex = hex.slice(2); | ||
} | ||
|
||
if (hex.length % 2 !== 0) { | ||
throw new Error(`hex string length ${hex.length} must be multiple of 2`); | ||
} | ||
|
||
const byteLen = hex.length / 2; | ||
const bytes = new Uint8Array(byteLen); | ||
for (let i = 0; i < byteLen; i++) { | ||
const byte = parseInt(hex.slice(i * 2, (i + 1) * 2), 16); | ||
bytes[i] = byte; | ||
} | ||
return bytes; | ||
} |
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,14 @@ | ||
import {toHex as browserToHex, toRootHex as browserToRootHex, fromHex as browserFromHex} from "./browser.js"; | ||
import {toHex as nodeToHex, toRootHex as nodeToRootHex, fromHex as nodeFromHex} from "./nodejs.js"; | ||
|
||
let toHex = browserToHex; | ||
let toRootHex = browserToRootHex; | ||
let fromHex = browserFromHex; | ||
|
||
if (typeof Buffer !== "undefined") { | ||
toHex = nodeToHex; | ||
toRootHex = nodeToRootHex; | ||
fromHex = nodeFromHex; | ||
} | ||
|
||
export {toHex, toRootHex, fromHex}; |
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,33 @@ | ||
export function toHex(buffer: Uint8Array | Parameters<typeof Buffer.from>[0]): string { | ||
if (Buffer.isBuffer(buffer)) { | ||
return "0x" + buffer.toString("hex"); | ||
} else if (buffer instanceof Uint8Array) { | ||
return "0x" + Buffer.from(buffer.buffer, buffer.byteOffset, buffer.length).toString("hex"); | ||
} else { | ||
return "0x" + Buffer.from(buffer).toString("hex"); | ||
} | ||
} | ||
|
||
// Shared buffer to convert root to hex | ||
let rootBuf: Buffer | undefined; | ||
|
||
/** | ||
* Convert a Uint8Array, length 32, to 0x-prefixed hex string | ||
*/ | ||
export function toRootHex(root: Uint8Array): string { | ||
if (root.length !== 32) { | ||
throw Error(`Expect root to be 32 bytes, got ${root.length}`); | ||
} | ||
|
||
if (rootBuf === undefined) { | ||
rootBuf = Buffer.alloc(32); | ||
} | ||
|
||
rootBuf.set(root); | ||
return `0x${rootBuf.toString("hex")}`; | ||
} | ||
|
||
export function fromHex(hex: string): Uint8Array { | ||
const b = Buffer.from(hex.replace("0x", ""), "hex"); | ||
return new Uint8Array(b.buffer, b.byteOffset, b.length); | ||
} |
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
19ac678
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Possible performance regression was detected for some benchmarks.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold.
Full benchmark results