Skip to content
Merged
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
25 changes: 25 additions & 0 deletions snippets/mongocompat/mongotypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,31 @@ if (typeof (BinData) != "undefined") {
BinData.prototype.length = function() {
return this.len;
};

BinData.prototype.nativeToString = BinData.prototype.toString;
BinData.prototype.toString = function (encoding) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The functionality of $function and new shell differs for toString(). Use const a = new BinData(0, 'SGVsbG8gV29ybGQ=') as an example. For $function, a.toString() returns BinData(0, 'SGVsbG8gV29ybGQ='). For new shell, it returns hello world. When encoding exists in the caller, they return the same result. Should we keep the behavior of existing $function or new shell.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, the goal here is maximum compatibility with $function and the legacy shell, so this seems reasonable to me

if (encoding) {
return this.nativeToString(encoding);
}
return `BinData(${this.type}, ${this.base64()})`;
};

BinData.prototype.base64 = function () {
return this.toString("base64");
};
BinData.prototype.hex = function () {
return this.toString("hex");
};
Object.defineProperty(BinData.prototype, "len", {
get: function () {
return this.buffer ? this.buffer.byteLength : 0;
},
});
Object.defineProperty(BinData.prototype, "type", {
get: function () {
return this.sub_type;
},
});
} else {
print("warning: no BinData class");
}
Expand Down