-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(NODE-7315): Use BSON ByteUtils instead of Nodejs Buffer #4840
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
24daacd
51efd36
3d28ae5
74fc9e3
732b12e
0ee1a17
5ddfae4
45ebf1e
79d0d19
82a34bb
fbb742e
cbc488c
e649b6f
12ff51f
983571b
e637852
6c9d928
736ba62
722379a
45c9875
ecc19f2
a04ec7b
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 | |||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -8,6 +8,7 @@ export { | ||||||||||
| BSONRegExp, | |||||||||||
| BSONSymbol, | |||||||||||
| BSONType, | |||||||||||
| ByteUtils, | |||||||||||
| calculateObjectSize, | |||||||||||
| Code, | |||||||||||
| DBRef, | |||||||||||
|
|
@@ -38,10 +39,56 @@ export function parseToElementsToArray(bytes: Uint8Array, offset?: number): BSON | ||||||||||
| return Array.isArray(res) ? res : [...res]; | |||||||||||
| } | |||||||||||
|
|
|||||||||||
| export const getInt32LE = BSON.onDemand.NumberUtils.getInt32LE; | |||||||||||
| export const getFloat64LE = BSON.onDemand.NumberUtils.getFloat64LE; | |||||||||||
| export const getBigInt64LE = BSON.onDemand.NumberUtils.getBigInt64LE; | |||||||||||
| export const toUTF8 = BSON.onDemand.ByteUtils.toUTF8; | |||||||||||
| export const getInt32LE = BSON.NumberUtils.getInt32LE; | |||||||||||
| export const getFloat64LE = BSON.NumberUtils.getFloat64LE; | |||||||||||
| export const getBigInt64LE = BSON.NumberUtils.getBigInt64LE; | |||||||||||
| export const toUTF8 = BSON.ByteUtils.toUTF8; | |||||||||||
| export const fromUTF8 = BSON.ByteUtils.fromUTF8; | |||||||||||
|
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. I guess it's too late to change but this PR made me realize that the names
Member
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. oh, great spotted! I feel like we can only change that in future major version, I will create ticket for this
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. ByteUtils is experimental - we can make changes outside of semver if we want to fix it now
Member
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. As the driver already rely on
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.
Not saying it's right or semantically makes sense, but this was the inspo for the naming. There are only two hard things in Computer Science: cache invalidation and naming things.
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. Yeah, the |
|||||||||||
| export const fromBase64 = BSON.ByteUtils.fromBase64; | |||||||||||
| export const fromNumberArray = BSON.ByteUtils.fromNumberArray; | |||||||||||
| export const concatBuffers = BSON.ByteUtils.concat; | |||||||||||
| export const allocateBuffer = BSON.ByteUtils.allocate; | |||||||||||
| export const allocateUnsafeBuffer = BSON.ByteUtils.allocateUnsafe; | |||||||||||
|
|
|||||||||||
| // writeInt32LE, same order of arguments as Buffer.writeInt32LE | |||||||||||
| export const writeInt32LE = (destination: Uint8Array, value: number, offset: number) => | |||||||||||
| BSON.NumberUtils.setInt32LE(destination, offset, value); | |||||||||||
|
|
|||||||||||
| // copyBuffer: copies from source buffer to target buffer, returns number of bytes copied | |||||||||||
| // inputs are explicitly named to avoid confusion | |||||||||||
| export const copyBuffer = (input: { | |||||||||||
baileympearson marked this conversation as resolved.
Show resolved
Hide resolved
|
|||||||||||
| source: Uint8Array; | |||||||||||
| target: Uint8Array; | |||||||||||
| targetStart?: number; | |||||||||||
| sourceStart?: number; | |||||||||||
| sourceEnd?: number; | |||||||||||
| }): number => { | |||||||||||
| const { source, target, targetStart = 0, sourceStart = 0, sourceEnd } = input; | |||||||||||
| const sourceEndActual = sourceEnd ?? source.length; | |||||||||||
| const srcSlice = source.subarray(sourceStart, sourceEndActual); | |||||||||||
| const maxLen = Math.min(srcSlice.length, target.length - targetStart); | |||||||||||
| if (maxLen <= 0) { | |||||||||||
| return 0; | |||||||||||
| } | |||||||||||
| target.set(srcSlice.subarray(0, maxLen), targetStart); | |||||||||||
| return maxLen; | |||||||||||
| }; | |||||||||||
|
|
|||||||||||
| // validates buffer inputs, used for read operations | |||||||||||
| const validateBufferInputs = (buffer: Uint8Array, offset: number, length: number) => { | |||||||||||
| if (offset < 0 || offset + length > buffer.length) { | |||||||||||
| throw new RangeError( | |||||||||||
| `Attempt to access memory outside buffer bounds: buffer length: ${buffer.length}, offset: ${offset}, length: ${length}` | |||||||||||
| ); | |||||||||||
| } | |||||||||||
| }; | |||||||||||
|
|
|||||||||||
| // readInt32LE, reads a 32-bit integer from buffer at given offset | |||||||||||
| // throws if offset is out of bounds | |||||||||||
| export const readInt32LE = (buffer: Uint8Array, offset: number): number => { | |||||||||||
| validateBufferInputs(buffer, offset, 4); | |||||||||||
baileympearson marked this conversation as resolved.
Show resolved
Hide resolved
|
|||||||||||
| return getInt32LE(buffer, offset); | |||||||||||
| }; | |||||||||||
|
|
|||||||||||
| /** | |||||||||||
| * BSON Serialization options. | |||||||||||
|
|
|||||||||||
Uh oh!
There was an error while loading. Please reload this page.