From a5203c722a11bb7d1d44da35afa16c03e60d2845 Mon Sep 17 00:00:00 2001 From: bailey Date: Thu, 9 Oct 2025 13:53:33 -0600 Subject: [PATCH 1/6] remove number from OID constructor --- src/objectid.ts | 23 ++++++++--------------- test/node/bson_test.js | 2 +- test/node/object_id.test.ts | 33 +++++++++------------------------ 3 files changed, 18 insertions(+), 40 deletions(-) diff --git a/src/objectid.ts b/src/objectid.ts index a9abde9f4..a84a57909 100644 --- a/src/objectid.ts +++ b/src/objectid.ts @@ -40,13 +40,8 @@ export class ObjectId extends BSONValue { /** ObjectId Bytes @internal */ private buffer!: Uint8Array; - /** - * Create ObjectId from a number. - * - * @param inputId - A number. - * @deprecated Instead, use `static createFromTime()` to set a numeric value for the new ObjectId. - */ - constructor(inputId: number); + /** To generate a new ObjectId, use ObjectId() with no argument. */ + constructor(); /** * Create ObjectId from a 24 character hex string. * @@ -71,20 +66,18 @@ export class ObjectId extends BSONValue { * @param inputId - A 12 byte binary Buffer. */ constructor(inputId: Uint8Array); - /** To generate a new ObjectId, use ObjectId() with no argument. */ - constructor(); /** * Implementation overload. * * @param inputId - All input types that are used in the constructor implementation. */ - constructor(inputId?: string | number | ObjectId | ObjectIdLike | Uint8Array); + constructor(inputId?: string | ObjectId | ObjectIdLike | Uint8Array); /** * Create a new ObjectId. * * @param inputId - An input value to create a new ObjectId from. */ - constructor(inputId?: string | number | ObjectId | ObjectIdLike | Uint8Array) { + constructor(inputId?: string | ObjectId | ObjectIdLike | Uint8Array) { super(); // workingId is set based on type of input and whether valid id exists for the input let workingId; @@ -102,12 +95,12 @@ export class ObjectId extends BSONValue { } // The following cases use workingId to construct an ObjectId - if (workingId == null || typeof workingId === 'number') { + if (workingId == null) { // The most common use case (blank id, new objectId instance) // Generate a new id - this.buffer = ObjectId.generate(typeof workingId === 'number' ? workingId : undefined); + this.buffer = ObjectId.generate(); } else if (ArrayBuffer.isView(workingId) && workingId.byteLength === 12) { - // If intstanceof matches we can escape calling ensure buffer in Node.js environments + // If instanceof matches we can escape calling ensure buffer in Node.js environments this.buffer = ByteUtils.toLocalBufferType(workingId); } else if (typeof workingId === 'string') { if (ObjectId.validateHexString(workingId)) { @@ -349,7 +342,7 @@ export class ObjectId extends BSONValue { * Checks if a value can be used to create a valid bson ObjectId * @param id - any JS value */ - static isValid(id: string | number | ObjectId | ObjectIdLike | Uint8Array): boolean { + static isValid(id: string | ObjectId | ObjectIdLike | Uint8Array): boolean { if (id == null) return false; if (typeof id === 'string') return ObjectId.validateHexString(id); diff --git a/test/node/bson_test.js b/test/node/bson_test.js index a996ddf33..611af550e 100644 --- a/test/node/bson_test.js +++ b/test/node/bson_test.js @@ -1641,7 +1641,7 @@ describe('BSON', function () { expect(false).to.equal(ObjectId.isValid({ length: 12 })); expect(false).to.equal(ObjectId.isValid([])); expect(false).to.equal(ObjectId.isValid(true)); - expect(true).to.equal(ObjectId.isValid(0)); + expect(false).to.equal(ObjectId.isValid(0)); expect(false).to.equal(ObjectId.isValid('invalid')); expect(false).to.equal(ObjectId.isValid('zzzzzzzzzzzzzzzzzzzzzzzz')); expect(true).to.equal(ObjectId.isValid('000000000000000000000000')); diff --git a/test/node/object_id.test.ts b/test/node/object_id.test.ts index 62344c3a6..4a608a395 100644 --- a/test/node/object_id.test.ts +++ b/test/node/object_id.test.ts @@ -217,26 +217,11 @@ describe('ObjectId', function () { expect(new ObjectId(objectInvalidBuffer).id).to.deep.equal(bufferNew24Hex); }); - const numericIO = [ - { input: 42, output: 42, description: '42' }, - { input: 0x2a, output: 0x2a, description: '0x2a' }, - { input: 4.2, output: 4, description: '4.2' }, - { input: NaN, output: 0, description: 'NaN' } - ]; - - for (const { input, output } of numericIO) { - it(`should correctly create ObjectId from ${input} and result in ${output}`, function () { - const objId = new ObjectId(input); - expect(objId).to.have.property('id'); - expect( - isBufferOrUint8Array(objId.id), - `expected objId.id to be instanceof buffer or uint8Array` - ).to.be.true; - const num = new DataView(objId.id.buffer, objId.id.byteOffset, objId.id.byteLength).getInt32( - 0, - false - ); - expect(num).to.equal(output); + for (const input of [42, 0x2a, 4.2, NaN]) { + it(`should fail to create ObjectId from ${input}`, function () { + expect(() => { + new ObjectId(input); + }).to.throw(/Argument passed in does not match the accepted types/); }); } @@ -343,12 +328,12 @@ describe('ObjectId', function () { }); context('when called with a number', () => { - it('returns true for any number (0)', () => expect(ObjectId.isValid(0)).to.be.true); + it('returns false for any number (0)', () => expect(ObjectId.isValid(0)).to.be.false); - it('returns true for any number (MIN_SAFE_INTEGER)', () => - expect(ObjectId.isValid(Number.MIN_SAFE_INTEGER)).to.be.true); + it('returns false for any number (MIN_SAFE_INTEGER)', () => + expect(ObjectId.isValid(Number.MIN_SAFE_INTEGER)).to.be.false); - it('returns true for any number (NaN)', () => expect(ObjectId.isValid(NaN)).to.be.true); + it('returns false for any number (NaN)', () => expect(ObjectId.isValid(NaN)).to.be.false); }); context('when called with a ObjectId or ObjectIdLike', () => { From 87521e36239ce5b29b6d1722efb5a1cacf5337be Mon Sep 17 00:00:00 2001 From: bailey Date: Thu, 9 Oct 2025 13:59:56 -0600 Subject: [PATCH 2/6] fix type tests --- test/types/bson.test-d.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/types/bson.test-d.ts b/test/types/bson.test-d.ts index dbaacecdb..2a7a46a2e 100644 --- a/test/types/bson.test-d.ts +++ b/test/types/bson.test-d.ts @@ -80,8 +80,8 @@ expectType(bsonValue._bsontype); expectType<(depth?: number | undefined, options?: unknown, inspect?: InspectFn | undefined) => string>(bsonValue.inspect); expectNotDeprecated(new ObjectId('foo')); -expectDeprecated(new ObjectId(42)); -expectNotDeprecated(new ObjectId(42 as string | number)); +expectError(new ObjectId(42)); +expectError(new ObjectId(42 as string | number)); // Timestamp accepts timestamp because constructor allows: {i:number, t:number} new Timestamp(new Timestamp(0n)) From d7e12c852c7814485188bb261fc6e27af70ed717 Mon Sep 17 00:00:00 2001 From: bailey Date: Thu, 9 Oct 2025 14:30:59 -0600 Subject: [PATCH 3/6] fix TS --- .evergreen/config.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.evergreen/config.yml b/.evergreen/config.yml index a4d0cc2a1..5945de2e3 100644 --- a/.evergreen/config.yml +++ b/.evergreen/config.yml @@ -225,7 +225,8 @@ tasks: - func: install dependencies - func: "run typescript" vars: - TS_VERSION: "next" + # TODO(NODE-7233): switch to `next` again once compatible with TS 6.0 + TS_VERSION: "latest" TRY_COMPILING_LIBRARY: "false" - name: run-granular-benchmarks commands: From e9c846e5c5addc80d07892df2694f5c15e6e2ea5 Mon Sep 17 00:00:00 2001 From: bailey Date: Fri, 10 Oct 2025 09:48:57 -0600 Subject: [PATCH 4/6] comments --- .evergreen/run-typescript.sh | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.evergreen/run-typescript.sh b/.evergreen/run-typescript.sh index d814fd17a..5e4dd7da6 100644 --- a/.evergreen/run-typescript.sh +++ b/.evergreen/run-typescript.sh @@ -14,8 +14,7 @@ CURRENT_TS_VERSION=$(get_current_ts_version) export TSC="./node_modules/typescript/bin/tsc" export TS_VERSION=${TS_VERSION:=$CURRENT_TS_VERSION} -# On old versions of TS we need to put the node types back to 18.11.19 -npm install --no-save --force typescript@"$TS_VERSION" "$(if [[ $TS_VERSION == '4.0.2' ]]; then echo "@types/node@18.11.19"; else echo ""; fi)" +npm install --no-save --force typescript@"$TS_VERSION" echo "Typescript $($TSC -v)" From 101c1b1e9f9948fac42721b66e19c487ef050fe8 Mon Sep 17 00:00:00 2001 From: bailey Date: Fri, 10 Oct 2025 09:57:40 -0600 Subject: [PATCH 5/6] update TS minimum to 5.6.0 --- .evergreen/config.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.evergreen/config.yml b/.evergreen/config.yml index 5945de2e3..7b2b9d291 100644 --- a/.evergreen/config.yml +++ b/.evergreen/config.yml @@ -205,7 +205,7 @@ tasks: - func: install dependencies - func: "run typescript" vars: - TS_VERSION: "4.0.2" + TS_VERSION: "5.6.0" TRY_COMPILING_LIBRARY: "false" - name: check-typescript-current commands: From 91b7fa32d0aa4d7fb386eb5528e1d7cf22a3d3a8 Mon Sep 17 00:00:00 2001 From: bailey Date: Fri, 10 Oct 2025 13:23:20 -0600 Subject: [PATCH 6/6] TS 5.6, not 5.6.0 --- .evergreen/config.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.evergreen/config.yml b/.evergreen/config.yml index 7b2b9d291..ec0d5b47f 100644 --- a/.evergreen/config.yml +++ b/.evergreen/config.yml @@ -205,7 +205,7 @@ tasks: - func: install dependencies - func: "run typescript" vars: - TS_VERSION: "5.6.0" + TS_VERSION: "5.6" TRY_COMPILING_LIBRARY: "false" - name: check-typescript-current commands: