Skip to content

Commit 4d9884d

Browse files
authored
fix(NODE-5873): objectId symbol property not defined on instances from cross cjs and mjs (#643)
1 parent 78b737b commit 4d9884d

File tree

2 files changed

+10
-11
lines changed

2 files changed

+10
-11
lines changed

src/objectid.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ export interface ObjectIdExtended {
2121
$oid: string;
2222
}
2323

24-
const kId = Symbol('id');
25-
2624
/**
2725
* A class representation of the BSON ObjectId type.
2826
* @public
@@ -39,7 +37,7 @@ export class ObjectId extends BSONValue {
3937
static cacheHexString: boolean;
4038

4139
/** ObjectId Bytes @internal */
42-
private [kId]!: Uint8Array;
40+
private buffer!: Uint8Array;
4341
/** ObjectId hexString cache @internal */
4442
private __id?: string;
4543

@@ -108,13 +106,13 @@ export class ObjectId extends BSONValue {
108106
if (workingId == null || typeof workingId === 'number') {
109107
// The most common use case (blank id, new objectId instance)
110108
// Generate a new id
111-
this[kId] = ObjectId.generate(typeof workingId === 'number' ? workingId : undefined);
109+
this.buffer = ObjectId.generate(typeof workingId === 'number' ? workingId : undefined);
112110
} else if (ArrayBuffer.isView(workingId) && workingId.byteLength === 12) {
113111
// If intstanceof matches we can escape calling ensure buffer in Node.js environments
114-
this[kId] = ByteUtils.toLocalBufferType(workingId);
112+
this.buffer = ByteUtils.toLocalBufferType(workingId);
115113
} else if (typeof workingId === 'string') {
116114
if (workingId.length === 24 && checkForHexRegExp.test(workingId)) {
117-
this[kId] = ByteUtils.fromHex(workingId);
115+
this.buffer = ByteUtils.fromHex(workingId);
118116
} else {
119117
throw new BSONError(
120118
'input must be a 24 character hex string, 12 byte Uint8Array, or an integer'
@@ -134,11 +132,11 @@ export class ObjectId extends BSONValue {
134132
* @readonly
135133
*/
136134
get id(): Uint8Array {
137-
return this[kId];
135+
return this.buffer;
138136
}
139137

140138
set id(value: Uint8Array) {
141-
this[kId] = value;
139+
this.buffer = value;
142140
if (ObjectId.cacheHexString) {
143141
this.__id = ByteUtils.toHex(value);
144142
}
@@ -240,7 +238,9 @@ export class ObjectId extends BSONValue {
240238
}
241239

242240
if (ObjectId.is(otherId)) {
243-
return this[kId][11] === otherId[kId][11] && ByteUtils.equals(this[kId], otherId[kId]);
241+
return (
242+
this.buffer[11] === otherId.buffer[11] && ByteUtils.equals(this.buffer, otherId.buffer)
243+
);
244244
}
245245

246246
if (typeof otherId === 'string') {

test/node/object_id.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { BSON, BSONError, EJSON, ObjectId } from '../register-bson';
33
import * as util from 'util';
44
import { expect } from 'chai';
55
import { bufferFromHexArray } from './tools/utils';
6-
import { getSymbolFrom } from './tools/utils';
76
import { isBufferOrUint8Array } from './tools/utils';
87

98
describe('ObjectId', function () {
@@ -376,7 +375,7 @@ describe('ObjectId', function () {
376375
*/
377376
const oidString = '6b61666665656b6c61746368';
378377
const oid = new ObjectId(oidString);
379-
const oidKId = getSymbolFrom(oid, 'id');
378+
const oidKId = 'buffer';
380379
it('should return false for an undefined otherId', () => {
381380
// otherId === undefined || otherId === null
382381
expect(oid.equals(null)).to.be.false;

0 commit comments

Comments
 (0)