Skip to content

Commit e1d1924

Browse files
Revert "refactor(NODE-6549): use private properties for hex string cache (#830)"
This reverts commit 8664d78.
1 parent 625bf7e commit e1d1924

File tree

5 files changed

+19
-81
lines changed

5 files changed

+19
-81
lines changed

.evergreen/run-typescript.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ echo "Typescript $($TSC -v)"
2222
echo "import * as BSON from '.'" > file.ts && node $TSC --noEmit --traceResolution file.ts | grep 'bson.d.ts' && rm file.ts
2323

2424
# check compilation
25-
node $TSC bson.d.ts --target es2022 --module nodenext
25+
rm -rf node_modules/@types/eslint # not a dependency we use, but breaks the build :(
26+
node $TSC bson.d.ts
2627

2728
if [[ $TRY_COMPILING_LIBRARY != "false" ]]; then
2829
npm run build:ts

package-lock.json

Lines changed: 3 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@
5757
"tar": "^7.4.3",
5858
"ts-node": "^10.9.2",
5959
"tsd": "^0.33.0",
60-
"tslib": "^2.8.1",
6160
"typescript": "^5.8.3",
6261
"typescript-cached-transpile": "0.0.6",
6362
"uuid": "^11.1.0"

src/objectid.ts

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import { NumberUtils } from './utils/number_utils';
77
// Unique sequence for the current process (initialized on first use)
88
let PROCESS_UNIQUE: Uint8Array | null = null;
99

10+
/** ObjectId hexString cache @internal */
11+
const __idCache = new WeakMap(); // TODO(NODE-6549): convert this to #__id private field when target updated to ES2022
12+
1013
/** @public */
1114
export interface ObjectIdLike {
1215
id: string | Uint8Array;
@@ -32,20 +35,11 @@ export class ObjectId extends BSONValue {
3235
/** @internal */
3336
private static index = Math.floor(Math.random() * 0xffffff);
3437

35-
static cacheHexString: boolean = false;
38+
static cacheHexString: boolean;
3639

3740
/** ObjectId Bytes @internal */
3841
private buffer!: Uint8Array;
3942

40-
/**
41-
* If hex string caching is enabled, contains the cached hex string. Otherwise, is null.
42-
*
43-
* Note that #hexString is populated lazily, and as a result simply checking `this.#hexString != null` is
44-
* not sufficient to determine if caching is enabled. `ObjectId.prototype.isCached()` can be used to
45-
* determine if the hex string has been cached yet for an ObjectId.
46-
*/
47-
#cachedHexString: string | null = null;
48-
4943
/** To generate a new ObjectId, use ObjectId() with no argument. */
5044
constructor();
5145
/**
@@ -113,7 +107,7 @@ export class ObjectId extends BSONValue {
113107
this.buffer = ByteUtils.fromHex(workingId);
114108
// If we are caching the hex string
115109
if (ObjectId.cacheHexString) {
116-
this.#cachedHexString = workingId;
110+
__idCache.set(this, workingId);
117111
}
118112
} else {
119113
throw new BSONError(
@@ -136,7 +130,7 @@ export class ObjectId extends BSONValue {
136130
set id(value: Uint8Array) {
137131
this.buffer = value;
138132
if (ObjectId.cacheHexString) {
139-
this.#cachedHexString = ByteUtils.toHex(value);
133+
__idCache.set(this, ByteUtils.toHex(value));
140134
}
141135
}
142136

@@ -165,12 +159,15 @@ export class ObjectId extends BSONValue {
165159

166160
/** Returns the ObjectId id as a 24 lowercase character hex string representation */
167161
toHexString(): string {
168-
if (this.#cachedHexString) return this.#cachedHexString.toLowerCase();
162+
if (ObjectId.cacheHexString) {
163+
const __id = __idCache.get(this);
164+
if (__id) return __id;
165+
}
169166

170167
const hexString = ByteUtils.toHex(this.id);
171168

172169
if (ObjectId.cacheHexString) {
173-
this.#cachedHexString = hexString;
170+
__idCache.set(this, hexString);
174171
}
175172

176173
return hexString;
@@ -368,13 +365,9 @@ export class ObjectId extends BSONValue {
368365
return new ObjectId(doc.$oid);
369366
}
370367

371-
/**
372-
* @internal
373-
*
374-
* used for testing
375-
*/
368+
/** @internal */
376369
private isCached(): boolean {
377-
return ObjectId.cacheHexString && this.#cachedHexString != null;
370+
return ObjectId.cacheHexString && __idCache.has(this);
378371
}
379372

380373
/**

test/node/object_id.test.ts

Lines changed: 1 addition & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -4,55 +4,8 @@ import * as util from 'util';
44
import { expect } from 'chai';
55
import { bufferFromHexArray } from './tools/utils';
66
import { isBufferOrUint8Array } from './tools/utils';
7-
import { test } from 'mocha';
87

98
describe('ObjectId', function () {
10-
describe('hex string caching does not impact deep equality', function () {
11-
const original = ObjectId.cacheHexString;
12-
before(function () {
13-
ObjectId.cacheHexString = true;
14-
});
15-
after(function () {
16-
ObjectId.cacheHexString = original;
17-
});
18-
test('no hex strings cached', function () {
19-
const id = new ObjectId();
20-
const id2 = new ObjectId(id.id);
21-
22-
// @ts-expect-error isCached() is internal
23-
expect(id.isCached()).to.be.false;
24-
// @ts-expect-error isCached() is internal
25-
expect(id2.isCached()).to.be.false;
26-
27-
expect(new ObjectId(id.id)).to.deep.equal(id);
28-
});
29-
30-
test('one id with cached hex string, one without', function () {
31-
const id = new ObjectId();
32-
const id2 = new ObjectId(id.id);
33-
id2.toHexString();
34-
35-
// @ts-expect-error isCached() is internal
36-
expect(id.isCached()).to.be.false;
37-
// @ts-expect-error isCached() is internal
38-
expect(id2.isCached()).to.be.true;
39-
40-
expect(id).to.deep.equal(id2);
41-
});
42-
43-
test('both with cached hex string', function () {
44-
const id = new ObjectId();
45-
const id2 = new ObjectId(id.toHexString());
46-
47-
// @ts-expect-error isCached() is internal
48-
expect(id.isCached()).to.be.true;
49-
// @ts-expect-error isCached() is internal
50-
expect(id2.isCached()).to.be.true;
51-
52-
expect(id).to.deep.equal(id2);
53-
});
54-
});
55-
569
describe('static createFromTime()', () => {
5710
it('creates an objectId with user defined value in the timestamp field', function () {
5811
const a = ObjectId.createFromTime(1);
@@ -312,7 +265,7 @@ describe('ObjectId', function () {
312265
let a = 'AAAAAAAAAAAAAAAAAAAAAAAA';
313266
let b = new ObjectId(a);
314267
let c = b.equals(a); // => false
315-
expect(c).to.be.true;
268+
expect(true).to.equal(c);
316269

317270
a = 'aaaaaaaaaaaaaaaaaaaaaaaa';
318271
b = new ObjectId(a);

0 commit comments

Comments
 (0)