Skip to content

Commit a212c76

Browse files
authored
STREAMS-1980: Add singleton compat for MaxKey (#24)
1 parent 3a99d9a commit a212c76

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

snippets/mongocompat/mongotypes.js

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ if (typeof (Timestamp) != "undefined") {
44

55
// Reference: https://github.com/mongodb/mongo/blob/c4d21d3346572e28df2f174df4d87e7618df4a77/src/mongo/scripting/mozjs/timestamp.cpp#L67-L78
66
function validateTimestampComponent(component, name) {
7-
const MAX_UINT32 = 4294967295;
7+
const MAX_UINT32 = 4294967295;
88

99
if (typeof component !== 'number') {
1010
throw new TypeError(`${name} must be a number`);
@@ -749,6 +749,41 @@ if (typeof (MinKey) != "undefined") {
749749
print("warning: no MinKey class");
750750
}
751751

752+
// MaxKey
753+
if (typeof (MaxKey) != "undefined") {
754+
const OriginalMaxKey = MaxKey;
755+
MaxKey = function () {
756+
if (MaxKey.prototype.__instance__ === undefined) {
757+
MaxKey.prototype.__instance__ = new OriginalMaxKey();
758+
}
759+
760+
return MaxKey.prototype.__instance__;
761+
};
762+
763+
MaxKey.prototype = OriginalMaxKey.prototype;
764+
765+
for (const key of Object.getOwnPropertyNames(OriginalMaxKey)) {
766+
// Skip prototype, length, name(function internals)
767+
if (key !== 'prototype' && key !== 'length' && key !== 'name') {
768+
MaxKey[key] = OriginalMaxKey[key];
769+
}
770+
}
771+
772+
MaxKey.prototype.toJSON = function () {
773+
return this.tojson();
774+
};
775+
776+
MaxKey.prototype.tojson = function () {
777+
return "{ \"$MaxKey\" : 1 }";
778+
};
779+
780+
MaxKey.prototype.toString = function () {
781+
return "[object Function]";
782+
};
783+
} else {
784+
print("warning: no MaxKey class");
785+
}
786+
752787
// Free Functions
753788
tojsononeline = function(x) {
754789
return tojson(x, " ", true);

snippets/mongocompat/test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,3 +179,19 @@ assert.strictEqual(MinKey(), MinKey());
179179
const serializedBsonMinKey = bson.serialize({ key1: MinKey, key2: MinKey() });
180180
const deserializedBsonMinKey = bson.deserialize(serializedBsonMinKey);
181181
assert.deepStrictEqual(deserializedBsonMinKey.key1, deserializedBsonMinKey.key2);
182+
183+
// Test MaxKey
184+
const maxKey = new MaxKey();
185+
assert(maxKey instanceof MaxKey, "MaxKey should be an instance of MaxKey");
186+
assert.strictEqual(maxKey.tojson(), '{ "$MaxKey" : 1 }');
187+
assert.strictEqual(maxKey.toString(), "[object Function]");
188+
assert.strictEqual(maxKey.toJSON(), '{ "$MaxKey" : 1 }');
189+
190+
// Test that multiple references return the same instance
191+
const anotherMaxKeyRef = new MaxKey();
192+
assert.strictEqual(maxKey, anotherMaxKeyRef);
193+
assert.strictEqual(MaxKey(), MaxKey());
194+
195+
const serializedBsonMaxKey = bson.serialize({ key1: MaxKey, key2: MaxKey() });
196+
const deserializedBsonMaxKey = bson.deserialize(serializedBsonMaxKey);
197+
assert.deepStrictEqual(deserializedBsonMaxKey.key1, deserializedBsonMaxKey.key2);

0 commit comments

Comments
 (0)