Skip to content
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

feat(NODE-4855): add hex and base64 ctor methods to Binary and ObjectId #569

Merged
merged 11 commits into from
Apr 4, 2023

Conversation

nbbeeken
Copy link
Contributor

@nbbeeken nbbeeken commented Mar 31, 2023

Description

What is changing?

Adds APIs to create BSON Binary / UUID / ObjectId types from hex and base64 strings.

class ObjectId {
  static createFromHexString(hex: string): ObjectId;
  static createFromBase64(base64: string): ObjectId;
}

class Binary {
  static createFromHexString(hex: string, subType? number): Binary;
  static createFromBase64(base64: string, subType? number): Binary;
}

class UUID extends Binary {
  static override createFromHexString(hex: string): UUID;
  static override createFromBase64(base64: string): UUID;
}

Also corrects Binary.prototype.inspect to print a roundtrip empty instance. As well as use the new base64 helper for more concise usage.

> new Binary()
Binary.createFromBase64("", 0)

> new Binary(null, 0x23)
Binary.createFromBase64("", 35)

> new Binary(Buffer.from('🤓', 'utf8'), 0x7F)
Binary.createFromBase64("8J+kkw==", 127)

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

  • Ran npm run lint script
  • Self-review completed using the steps outlined here
  • PR title follows the correct format: type(NODE-xxxx)[!]: description
    • Example: feat(NODE-1234)!: rewriting everything in coffeescript
  • Changes are covered by tests
  • New TODOs have a related JIRA ticket

src/binary.ts Outdated
@@ -292,7 +302,13 @@ export class Binary extends BSONValue {
}

inspect(): string {
return `new Binary(Buffer.from("${ByteUtils.toHex(this.buffer)}", "hex"), ${this.sub_type})`;
if (this.position === 0) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

// 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'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a copy pasta mistake, the if stmt asserts for 24 characters, so a 12 byte string wouldn't work here.


describe('class Binary', () => {
context('constructor()', () => {
it('creates an 256 byte Binary with subtype 0 by default', () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just testing the existing behavior here.

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);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will throw if we mistakenly introduce a syntax error or if we produce strings that when invoked lead to runtime errors

@nbbeeken nbbeeken marked this pull request as ready for review March 31, 2023 15:59
@nbbeeken nbbeeken requested a review from addaleax March 31, 2023 17:54
addaleax
addaleax previously approved these changes Apr 3, 2023
src/binary.ts Outdated Show resolved Hide resolved
@nbbeeken nbbeeken added the Primary Review In Review with primary reviewer, not yet ready for team's eyes label Apr 3, 2023
@baileympearson baileympearson added Team Review Needs review from team and removed Primary Review In Review with primary reviewer, not yet ready for team's eyes labels Apr 3, 2023
@W-A-James W-A-James self-requested a review April 3, 2023 17:43
@baileympearson baileympearson merged commit 0d49a63 into main Apr 4, 2023
@baileympearson baileympearson deleted the NODE-4855-fromX branch April 4, 2023 12:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Team Review Needs review from team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants