Skip to content
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
37 changes: 36 additions & 1 deletion snippets/mongocompat/mongotypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
Expand Down Expand Up @@ -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);
Expand Down
16 changes: 16 additions & 0 deletions snippets/mongocompat/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);