From 59cdc3dd98367f77e9412bfb0f76821d1c48d232 Mon Sep 17 00:00:00 2001 From: Taylor Cannon Date: Tue, 2 Dec 2025 14:39:03 -0600 Subject: [PATCH] STREAMS-1980: Add singleton compat for MaxKey --- snippets/mongocompat/mongotypes.js | 37 +++++++++++++++++++++++++++++- snippets/mongocompat/test.js | 16 +++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/snippets/mongocompat/mongotypes.js b/snippets/mongocompat/mongotypes.js index e9426c3..02f3478 100644 --- a/snippets/mongocompat/mongotypes.js +++ b/snippets/mongocompat/mongotypes.js @@ -4,7 +4,7 @@ if (typeof (Timestamp) != "undefined") { // Reference: https://github.com/mongodb/mongo/blob/c4d21d3346572e28df2f174df4d87e7618df4a77/src/mongo/scripting/mozjs/timestamp.cpp#L67-L78 function validateTimestampComponent(component, name) { - const MAX_UINT32 = 4294967295; + const MAX_UINT32 = 4294967295; if (typeof component !== 'number') { throw new TypeError(`${name} must be a number`); @@ -749,6 +749,41 @@ if (typeof (MinKey) != "undefined") { print("warning: no MinKey class"); } +// MaxKey +if (typeof (MaxKey) != "undefined") { + const OriginalMaxKey = MaxKey; + MaxKey = function () { + if (MaxKey.prototype.__instance__ === undefined) { + MaxKey.prototype.__instance__ = new OriginalMaxKey(); + } + + return MaxKey.prototype.__instance__; + }; + + MaxKey.prototype = OriginalMaxKey.prototype; + + for (const key of Object.getOwnPropertyNames(OriginalMaxKey)) { + // Skip prototype, length, name(function internals) + if (key !== 'prototype' && key !== 'length' && key !== 'name') { + MaxKey[key] = OriginalMaxKey[key]; + } + } + + MaxKey.prototype.toJSON = function () { + return this.tojson(); + }; + + MaxKey.prototype.tojson = function () { + return "{ \"$MaxKey\" : 1 }"; + }; + + MaxKey.prototype.toString = function () { + return "[object Function]"; + }; +} else { + print("warning: no MaxKey class"); +} + // Free Functions tojsononeline = function(x) { return tojson(x, " ", true); diff --git a/snippets/mongocompat/test.js b/snippets/mongocompat/test.js index 512ec8a..bacd56a 100644 --- a/snippets/mongocompat/test.js +++ b/snippets/mongocompat/test.js @@ -179,3 +179,19 @@ assert.strictEqual(MinKey(), MinKey()); const serializedBsonMinKey = bson.serialize({ key1: MinKey, key2: MinKey() }); const deserializedBsonMinKey = bson.deserialize(serializedBsonMinKey); assert.deepStrictEqual(deserializedBsonMinKey.key1, deserializedBsonMinKey.key2); + +// Test MaxKey +const maxKey = new MaxKey(); +assert(maxKey instanceof MaxKey, "MaxKey should be an instance of MaxKey"); +assert.strictEqual(maxKey.tojson(), '{ "$MaxKey" : 1 }'); +assert.strictEqual(maxKey.toString(), "[object Function]"); +assert.strictEqual(maxKey.toJSON(), '{ "$MaxKey" : 1 }'); + +// Test that multiple references return the same instance +const anotherMaxKeyRef = new MaxKey(); +assert.strictEqual(maxKey, anotherMaxKeyRef); +assert.strictEqual(MaxKey(), MaxKey()); + +const serializedBsonMaxKey = bson.serialize({ key1: MaxKey, key2: MaxKey() }); +const deserializedBsonMaxKey = bson.deserialize(serializedBsonMaxKey); +assert.deepStrictEqual(deserializedBsonMaxKey.key1, deserializedBsonMaxKey.key2);