Uploaded image for project: 'Node.js Driver'
  1. Node.js Driver
  2. NODE-2848

bson@4.2.0 breaks serialization with bson@1.x

    • Type: Icon: Bug Bug
    • Resolution: Unresolved
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: 4.0.0
    • Component/s: BSON
    • Labels:

      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.

            Assignee:
            Unassigned Unassigned
            Reporter:
            anna.henningsen@mongodb.com Anna Henningsen
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: