Skip to content

Commit a3f5841

Browse files
STREAMS-1964: Add old shell compatibility of BinData for JS Engine (#14)
1 parent 4cc0eab commit a3f5841

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

snippets/mongocompat/mongotypes.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,31 @@ if (typeof (BinData) != "undefined") {
542542
BinData.prototype.length = function() {
543543
return this.len;
544544
};
545+
546+
BinData.prototype.nativeToString = BinData.prototype.toString;
547+
BinData.prototype.toString = function (encoding) {
548+
if (encoding) {
549+
return this.nativeToString(encoding);
550+
}
551+
return `BinData(${this.type}, ${this.base64()})`;
552+
};
553+
554+
BinData.prototype.base64 = function () {
555+
return this.toString("base64");
556+
};
557+
BinData.prototype.hex = function () {
558+
return this.toString("hex");
559+
};
560+
Object.defineProperty(BinData.prototype, "len", {
561+
get: function () {
562+
return this.buffer ? this.buffer.byteLength : 0;
563+
},
564+
});
565+
Object.defineProperty(BinData.prototype, "type", {
566+
get: function () {
567+
return this.sub_type;
568+
},
569+
});
545570
} else {
546571
print("warning: no BinData class");
547572
}

0 commit comments

Comments
 (0)