-
Notifications
You must be signed in to change notification settings - Fork 260
feat(NODE-7314): export byteUtils & add missing methods #852
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -69,7 +69,10 @@ const webRandomBytes: (byteLength: number) => Uint8Array = (() => { | |||||
|
|
||||||
| const HEX_DIGIT = /(\d|[a-f])/i; | ||||||
|
|
||||||
| /** @internal */ | ||||||
| /** | ||||||
| * @public | ||||||
| * @experimental | ||||||
| */ | ||||||
| export const webByteUtils = { | ||||||
| toLocalBufferType( | ||||||
| potentialUint8array: Uint8Array | ArrayBufferViewWithTag | ArrayBuffer | ||||||
|
|
@@ -114,6 +117,42 @@ export const webByteUtils = { | |||||
| return webByteUtils.allocate(size); | ||||||
| }, | ||||||
|
|
||||||
| compare(a: Uint8Array, b: Uint8Array): -1 | 0 | 1 { | ||||||
| if (a === b) return 0; | ||||||
|
|
||||||
| const len = Math.min(a.length, b.length); | ||||||
|
|
||||||
| for (let i = 0; i < len; i++) { | ||||||
| if (a[i] < b[i]) return -1; | ||||||
| if (a[i] > b[i]) return 1; | ||||||
| } | ||||||
|
|
||||||
| if (a.length < b.length) return -1; | ||||||
| if (a.length > b.length) return 1; | ||||||
|
|
||||||
| return 0; | ||||||
| }, | ||||||
|
|
||||||
| concat(list: Uint8Array[]): Uint8Array { | ||||||
| if (list.length === 0) return webByteUtils.allocate(0); | ||||||
| if (list.length === 1) return list[0]; | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Very clever optimization, but I wonder if returning the input instance could lead to small surprises when BSON is running using this "web mode" implementation. In Node.js a copy to a new buffer is always performed |
||||||
|
|
||||||
| let totalLength = 0; | ||||||
| for (const arr of list) { | ||||||
| totalLength += arr.length; | ||||||
| } | ||||||
|
|
||||||
| const result = webByteUtils.allocate(totalLength); | ||||||
| let offset = 0; | ||||||
|
|
||||||
| for (const arr of list) { | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
same as above |
||||||
| result.set(arr, offset); | ||||||
| offset += arr.length; | ||||||
| } | ||||||
|
|
||||||
| return result; | ||||||
| }, | ||||||
|
|
||||||
| equals(a: Uint8Array, b: Uint8Array): boolean { | ||||||
| if (a.byteLength !== b.byteLength) { | ||||||
| return false; | ||||||
|
|
||||||
Uh oh!
There was an error while loading. Please reload this page.
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.
or
or something.
strive for as descriptive variable names as possible