-
Type: Bug
-
Resolution: Unresolved
-
Priority: Minor - P4
-
None
-
Affects Version/s: 4.0.0
-
Component/s: BSON
This is affecting Compass, which a) uses the mongo driver, which uses bson@1.x, and b) uses webpack to bundle bson for some of its dependencies, and thus uses the browser version of bson.
Running the shell script below in a temporary directory should reproduce the problem. This works with bson@4.1.x in place of bson@4.2.0, i.e. this is an unintentional breaking change.
npm install bson-1.1.5@npm:bson@1.1.5 npm install bson-4.2.0@npm:bson@4.2.0 # copy with .mjs file extension so Node.js can load it cp node_modules/bson-4.2.0/dist/bson.browser.esm.js bson.browser.esm.mjs cat > test.mjs <<- EOS import bsonNew from './bson.browser.esm.mjs'; import bsonOld from 'bson-1.1.5'; console.log(bsonOld.prototype.serialize({ binary: new bsonNew.Binary('aaaa') })); EOS node test.mjs # fails with: # # file:///tmp/bsontest/bson.browser.esm.mjs:1766 # if (!Buffer.isBuffer(target)) throw new TypeError('argument should be a Buffer'); # ^ # # TypeError: argument should be a Buffer # at Uint8Array.copy (file:///tmp/bsontest/bson.browser.esm.mjs:1766:41) # at serializeBinary (/tmp/bsontest/node_modules/bson-1.1.5/lib/bson/parser/serializer.js:590:8) # at serializeInto (/tmp/bsontest/node_modules/bson-1.1.5/lib/bson/parser/serializer.js:973:17) # at Object.serialize (/tmp/bsontest/node_modules/bson-1.1.5/lib/bson/bson.js:64:28) # at file:///tmp/bsontest/test.mjs:4:31
The root cause here is that the buffer module that bson now imports does not do good type checking: It assumes that any object with ._isBuffer === true is a Buffer, which does not even hold true for built-in Node.js buffers. This check is also too strict in .copy() in general, because .copy() should accept any type of Uint8Array.
- is depended on by
-
NODE-2847 bson does not expose a default export
- Closed
- is related to
-
NODE-3376 Node.js BSON dist cannot serialize browser BSON dist ObjectId
- Closed
-
COMPASS-4502 Update bson to real version in compass-import-export
- Closed
- related to
-
NODE-3555 Refactor BSON to work with only cross platform JS APIs
- Closed