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

fix(NODE-5873): objectId symbol property not defined on instances from cross cjs and mjs #643

Merged
merged 1 commit into from
Feb 5, 2024
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
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