feat(NODE-4855): add hex and base64 ctor methods to Binary and ObjectId#569
Merged
baileympearson merged 11 commits intomainfrom Apr 4, 2023
Merged
feat(NODE-4855): add hex and base64 ctor methods to Binary and ObjectId#569baileympearson merged 11 commits intomainfrom
baileympearson merged 11 commits intomainfrom
Conversation
nbbeeken
commented
Mar 31, 2023
src/binary.ts
Outdated
|
|
||
| inspect(): string { | ||
| return `new Binary(Buffer.from("${ByteUtils.toHex(this.buffer)}", "hex"), ${this.sub_type})`; | ||
| if (this.position === 0) { |
Contributor
Author
There was a problem hiding this comment.
We had a bug here where the default new Binary() printed:
Binary(Buffer.from("00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", "hex"), 0)Which isn't the same as what would end up in BSON, b/c position determines how many bytes are serialized. The above constructs a Binary where position is set to 256, instead of set to zero.
nbbeeken
commented
Mar 31, 2023
| // Throw an error if it's not a valid setup | ||
| if (typeof hexString === 'undefined' || (hexString != null && hexString.length !== 24)) { | ||
| throw new BSONError( | ||
| 'Argument passed in must be a single String of 12 bytes or a string of 24 hex characters' |
Contributor
Author
There was a problem hiding this comment.
Looks like a copy pasta mistake, the if stmt asserts for 24 characters, so a 12 byte string wouldn't work here.
nbbeeken
commented
Mar 31, 2023
|
|
||
| describe('class Binary', () => { | ||
| context('constructor()', () => { | ||
| it('creates an 256 byte Binary with subtype 0 by default', () => { |
Contributor
Author
There was a problem hiding this comment.
Just testing the existing behavior here.
nbbeeken
commented
Mar 31, 2023
| it(`${name} returns string that is runnable and has deep equality`, () => { | ||
| const bsonValue = factory(); | ||
| const ctx = { ...BSON, module: { exports: { result: null } } }; | ||
| vm.runInNewContext(`module.exports.result = ${bsonValue.inspect()}`, ctx); |
Contributor
Author
There was a problem hiding this comment.
This will throw if we mistakenly introduce a syntax error or if we produce strings that when invoked lead to runtime errors
addaleax
previously approved these changes
Apr 3, 2023
addaleax
approved these changes
Apr 3, 2023
baileympearson
approved these changes
Apr 3, 2023
W-A-James
approved these changes
Apr 3, 2023
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
What is changing?
Adds APIs to create BSON Binary / UUID / ObjectId types from hex and base64 strings.
Also corrects Binary.prototype.inspect to print a roundtrip empty instance. As well as use the new base64 helper for more concise usage.
What is the motivation for this change?
Printing out
Buffer.from()as the first argument to Binary was clunky and didn't provide the best way to reproduce a Binary instance from the visible output.Double check the following
npm run lintscripttype(NODE-xxxx)[!]: descriptionfeat(NODE-1234)!: rewriting everything in coffeescript