Skip to content

Commit 21cc3ff

Browse files
feat(NODE-5763)!: remove support for new ObjectId(<number>) (#824)
1 parent f636254 commit 21cc3ff

File tree

6 files changed

+24
-46
lines changed

6 files changed

+24
-46
lines changed

.evergreen/config.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ tasks:
205205
- func: install dependencies
206206
- func: "run typescript"
207207
vars:
208-
TS_VERSION: "4.0.2"
208+
TS_VERSION: "5.6"
209209
TRY_COMPILING_LIBRARY: "false"
210210
- name: check-typescript-current
211211
commands:
@@ -225,7 +225,8 @@ tasks:
225225
- func: install dependencies
226226
- func: "run typescript"
227227
vars:
228-
TS_VERSION: "next"
228+
# TODO(NODE-7233): switch to `next` again once compatible with TS 6.0
229+
TS_VERSION: "latest"
229230
TRY_COMPILING_LIBRARY: "false"
230231
- name: run-granular-benchmarks
231232
commands:

.evergreen/run-typescript.sh

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ CURRENT_TS_VERSION=$(get_current_ts_version)
1414
export TSC="./node_modules/typescript/bin/tsc"
1515
export TS_VERSION=${TS_VERSION:=$CURRENT_TS_VERSION}
1616

17-
# On old versions of TS we need to put the node types back to 18.11.19
18-
npm install --no-save --force typescript@"$TS_VERSION" "$(if [[ $TS_VERSION == '4.0.2' ]]; then echo "@types/[email protected]"; else echo ""; fi)"
17+
npm install --no-save --force typescript@"$TS_VERSION"
1918

2019
echo "Typescript $($TSC -v)"
2120

src/objectid.ts

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,8 @@ export class ObjectId extends BSONValue {
4040
/** ObjectId Bytes @internal */
4141
private buffer!: Uint8Array;
4242

43-
/**
44-
* Create ObjectId from a number.
45-
*
46-
* @param inputId - A number.
47-
* @deprecated Instead, use `static createFromTime()` to set a numeric value for the new ObjectId.
48-
*/
49-
constructor(inputId: number);
43+
/** To generate a new ObjectId, use ObjectId() with no argument. */
44+
constructor();
5045
/**
5146
* Create ObjectId from a 24 character hex string.
5247
*
@@ -71,20 +66,18 @@ export class ObjectId extends BSONValue {
7166
* @param inputId - A 12 byte binary Buffer.
7267
*/
7368
constructor(inputId: Uint8Array);
74-
/** To generate a new ObjectId, use ObjectId() with no argument. */
75-
constructor();
7669
/**
7770
* Implementation overload.
7871
*
7972
* @param inputId - All input types that are used in the constructor implementation.
8073
*/
81-
constructor(inputId?: string | number | ObjectId | ObjectIdLike | Uint8Array);
74+
constructor(inputId?: string | ObjectId | ObjectIdLike | Uint8Array);
8275
/**
8376
* Create a new ObjectId.
8477
*
8578
* @param inputId - An input value to create a new ObjectId from.
8679
*/
87-
constructor(inputId?: string | number | ObjectId | ObjectIdLike | Uint8Array) {
80+
constructor(inputId?: string | ObjectId | ObjectIdLike | Uint8Array) {
8881
super();
8982
// workingId is set based on type of input and whether valid id exists for the input
9083
let workingId;
@@ -102,12 +95,12 @@ export class ObjectId extends BSONValue {
10295
}
10396

10497
// The following cases use workingId to construct an ObjectId
105-
if (workingId == null || typeof workingId === 'number') {
98+
if (workingId == null) {
10699
// The most common use case (blank id, new objectId instance)
107100
// Generate a new id
108-
this.buffer = ObjectId.generate(typeof workingId === 'number' ? workingId : undefined);
101+
this.buffer = ObjectId.generate();
109102
} else if (ArrayBuffer.isView(workingId) && workingId.byteLength === 12) {
110-
// If intstanceof matches we can escape calling ensure buffer in Node.js environments
103+
// If instanceof matches we can escape calling ensure buffer in Node.js environments
111104
this.buffer = ByteUtils.toLocalBufferType(workingId);
112105
} else if (typeof workingId === 'string') {
113106
if (ObjectId.validateHexString(workingId)) {
@@ -349,7 +342,7 @@ export class ObjectId extends BSONValue {
349342
* Checks if a value can be used to create a valid bson ObjectId
350343
* @param id - any JS value
351344
*/
352-
static isValid(id: string | number | ObjectId | ObjectIdLike | Uint8Array): boolean {
345+
static isValid(id: string | ObjectId | ObjectIdLike | Uint8Array): boolean {
353346
if (id == null) return false;
354347
if (typeof id === 'string') return ObjectId.validateHexString(id);
355348

test/node/bson_test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1641,7 +1641,7 @@ describe('BSON', function () {
16411641
expect(false).to.equal(ObjectId.isValid({ length: 12 }));
16421642
expect(false).to.equal(ObjectId.isValid([]));
16431643
expect(false).to.equal(ObjectId.isValid(true));
1644-
expect(true).to.equal(ObjectId.isValid(0));
1644+
expect(false).to.equal(ObjectId.isValid(0));
16451645
expect(false).to.equal(ObjectId.isValid('invalid'));
16461646
expect(false).to.equal(ObjectId.isValid('zzzzzzzzzzzzzzzzzzzzzzzz'));
16471647
expect(true).to.equal(ObjectId.isValid('000000000000000000000000'));

test/node/object_id.test.ts

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -217,26 +217,11 @@ describe('ObjectId', function () {
217217
expect(new ObjectId(objectInvalidBuffer).id).to.deep.equal(bufferNew24Hex);
218218
});
219219

220-
const numericIO = [
221-
{ input: 42, output: 42, description: '42' },
222-
{ input: 0x2a, output: 0x2a, description: '0x2a' },
223-
{ input: 4.2, output: 4, description: '4.2' },
224-
{ input: NaN, output: 0, description: 'NaN' }
225-
];
226-
227-
for (const { input, output } of numericIO) {
228-
it(`should correctly create ObjectId from ${input} and result in ${output}`, function () {
229-
const objId = new ObjectId(input);
230-
expect(objId).to.have.property('id');
231-
expect(
232-
isBufferOrUint8Array(objId.id),
233-
`expected objId.id to be instanceof buffer or uint8Array`
234-
).to.be.true;
235-
const num = new DataView(objId.id.buffer, objId.id.byteOffset, objId.id.byteLength).getInt32(
236-
0,
237-
false
238-
);
239-
expect(num).to.equal(output);
220+
for (const input of [42, 0x2a, 4.2, NaN]) {
221+
it(`should fail to create ObjectId from ${input}`, function () {
222+
expect(() => {
223+
new ObjectId(input);
224+
}).to.throw(/Argument passed in does not match the accepted types/);
240225
});
241226
}
242227

@@ -343,12 +328,12 @@ describe('ObjectId', function () {
343328
});
344329

345330
context('when called with a number', () => {
346-
it('returns true for any number (0)', () => expect(ObjectId.isValid(0)).to.be.true);
331+
it('returns false for any number (0)', () => expect(ObjectId.isValid(0)).to.be.false);
347332

348-
it('returns true for any number (MIN_SAFE_INTEGER)', () =>
349-
expect(ObjectId.isValid(Number.MIN_SAFE_INTEGER)).to.be.true);
333+
it('returns false for any number (MIN_SAFE_INTEGER)', () =>
334+
expect(ObjectId.isValid(Number.MIN_SAFE_INTEGER)).to.be.false);
350335

351-
it('returns true for any number (NaN)', () => expect(ObjectId.isValid(NaN)).to.be.true);
336+
it('returns false for any number (NaN)', () => expect(ObjectId.isValid(NaN)).to.be.false);
352337
});
353338

354339
context('when called with a ObjectId or ObjectIdLike', () => {

test/types/bson.test-d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ expectType<string>(bsonValue._bsontype);
8080
expectType<(depth?: number | undefined, options?: unknown, inspect?: InspectFn | undefined) => string>(bsonValue.inspect);
8181

8282
expectNotDeprecated(new ObjectId('foo'));
83-
expectDeprecated(new ObjectId(42));
84-
expectNotDeprecated(new ObjectId(42 as string | number));
83+
expectError(new ObjectId(42));
84+
expectError(new ObjectId(42 as string | number));
8585

8686
// Timestamp accepts timestamp because constructor allows: {i:number, t:number}
8787
new Timestamp(new Timestamp(0n))

0 commit comments

Comments
 (0)