Skip to content

Commit

Permalink
fix(NODE-5873): objectId symbol property not defined on instances fro…
Browse files Browse the repository at this point in the history
…m cross cjs and mjs (#643)
  • Loading branch information
dot-i authored Feb 5, 2024
1 parent 78b737b commit 4d9884d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
18 changes: 9 additions & 9 deletions src/objectid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ export interface ObjectIdExtended {
$oid: string;
}

const kId = Symbol('id');

/**
* A class representation of the BSON ObjectId type.
* @public
Expand All @@ -39,7 +37,7 @@ export class ObjectId extends BSONValue {
static cacheHexString: boolean;

/** ObjectId Bytes @internal */
private [kId]!: Uint8Array;
private buffer!: Uint8Array;
/** ObjectId hexString cache @internal */
private __id?: string;

Expand Down Expand Up @@ -108,13 +106,13 @@ export class ObjectId extends BSONValue {
if (workingId == null || typeof workingId === 'number') {
// The most common use case (blank id, new objectId instance)
// Generate a new id
this[kId] = ObjectId.generate(typeof workingId === 'number' ? workingId : undefined);
this.buffer = ObjectId.generate(typeof workingId === 'number' ? workingId : undefined);
} else if (ArrayBuffer.isView(workingId) && workingId.byteLength === 12) {
// If intstanceof matches we can escape calling ensure buffer in Node.js environments
this[kId] = ByteUtils.toLocalBufferType(workingId);
this.buffer = ByteUtils.toLocalBufferType(workingId);
} else if (typeof workingId === 'string') {
if (workingId.length === 24 && checkForHexRegExp.test(workingId)) {
this[kId] = ByteUtils.fromHex(workingId);
this.buffer = ByteUtils.fromHex(workingId);
} else {
throw new BSONError(
'input must be a 24 character hex string, 12 byte Uint8Array, or an integer'
Expand All @@ -134,11 +132,11 @@ export class ObjectId extends BSONValue {
* @readonly
*/
get id(): Uint8Array {
return this[kId];
return this.buffer;
}

set id(value: Uint8Array) {
this[kId] = value;
this.buffer = value;
if (ObjectId.cacheHexString) {
this.__id = ByteUtils.toHex(value);
}
Expand Down Expand Up @@ -240,7 +238,9 @@ export class ObjectId extends BSONValue {
}

if (ObjectId.is(otherId)) {
return this[kId][11] === otherId[kId][11] && ByteUtils.equals(this[kId], otherId[kId]);
return (
this.buffer[11] === otherId.buffer[11] && ByteUtils.equals(this.buffer, otherId.buffer)
);
}

if (typeof otherId === 'string') {
Expand Down
3 changes: 1 addition & 2 deletions test/node/object_id.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { BSON, BSONError, EJSON, ObjectId } from '../register-bson';
import * as util from 'util';
import { expect } from 'chai';
import { bufferFromHexArray } from './tools/utils';
import { getSymbolFrom } from './tools/utils';
import { isBufferOrUint8Array } from './tools/utils';

describe('ObjectId', function () {
Expand Down Expand Up @@ -376,7 +375,7 @@ describe('ObjectId', function () {
*/
const oidString = '6b61666665656b6c61746368';
const oid = new ObjectId(oidString);
const oidKId = getSymbolFrom(oid, 'id');
const oidKId = 'buffer';
it('should return false for an undefined otherId', () => {
// otherId === undefined || otherId === null
expect(oid.equals(null)).to.be.false;
Expand Down

0 comments on commit 4d9884d

Please sign in to comment.