-
Type: Bug
-
Resolution: Fixed
-
Priority: Major - P3
-
Affects Version/s: None
-
Component/s: BSON
It seems as if on the web, using BSON.Binary is only possible when using a string that represents the data such that it uses the polyfilled Node.js Buffer. Other than that, the default typed arrays available on browsers do not seem to work as arguments. It seems like the one that should work is Uint8Array and Array. Passing in a Number also does not work but I can't tell if it's supposed to.
The following code below reproduces the issues listed above. You will need to replace the script sources with your own.
<script src="file:///Users/eric/Downloads/js-bson/browser_build/bson.js"></script> <script type="text/javascript"> global.bson = BSON; </script> <script src="file:///Users/eric/Downloads/mongodb-extjson/dist/bundle.js"></script> <script type="text/javascript"> global.EJSON = window["mongodb-extjson"]; </script> <script type="text/javascript"> console.log(EJSON.stringify(BSON.Binary("foo"))); // works try { console.log(EJSON.stringify(BSON.Binary(5))); // fails but is an instanceof Number (Uncaught RangeError: toString() radix argument must be between 2 and 36) } catch (e) { console.log(e); } console.log(EJSON.stringify(BSON.Binary(new Uint8Array([1, 2, 3])))) // produces incorrect extjson {"$binary":{"base64":"1,2,3","subType":"00"}} (one of accepted String, Buffer, Uint8Array or Array accepted) console.log(EJSON.stringify(BSON.Binary([1, 2, 3]))) // produces incorrect extjson {"$binary":{"base64":"1,2,3","subType":"00"}} (one of accepted String, Buffer, Uint8Array or Array accepted) </script>