Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .evergreen/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ tasks:
- func: install dependencies
- func: "run typescript"
vars:
TS_VERSION: "4.0.2"
TS_VERSION: "5.6"
TRY_COMPILING_LIBRARY: "false"
- name: check-typescript-current
commands:
Expand All @@ -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:
Expand Down
3 changes: 1 addition & 2 deletions .evergreen/run-typescript.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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)"

Expand Down
23 changes: 8 additions & 15 deletions src/objectid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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;
Expand All @@ -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)) {
Expand Down Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion test/node/bson_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down
33 changes: 9 additions & 24 deletions test/node/object_id.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/);
});
}

Expand Down Expand Up @@ -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', () => {
Expand Down
4 changes: 2 additions & 2 deletions test/types/bson.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ expectType<string>(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))
Expand Down