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

Node.js BSON dist cannot serialize browser BSON dist ObjectId

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • bson-4.4.1
    • Affects Version/s: None
    • Component/s: BSON
    • Not Needed

      This is very similar in structure to NODE-2848, but doesn’t involve multiple BSON versions, only the browser and the Node.js distribution variants.

      The root cause and the ideal fix are the same (namely, the `buffer` package fixing its instance checking, e.g. this PR). The main point of opening a new ticket is that, since Compass uses the 4.x driver, NODE-2848 is no longer as urgent for us, whereas this one is required for us to use the latest BSON version in Compass.

      Steps to reproduce is running the shell script below:

      npm install bson@4.4.0
      cp node_modules/bson/dist/bson.browser.esm.js bson.browser.esm.mjs
      
      cat > test.mjs <<- EOS
      import bsonBrowser from './bson.browser.esm.mjs';
      import bsonNode from 'bson';
      
      console.log(bsonNode.serialize({
        _id: new bsonBrowser.ObjectId()
      }));
      EOS
      
      node test.mjs
      # fails with:
      #
      # file:///tmp/v/bson.browser.esm.mjs:1775
      #     if (!Buffer.isBuffer(target)) throw new TypeError('argument should be a Buffer');
      #                                         ^
      # 
      # TypeError: argument should be a Buffer
      #     at Uint8Array.copy (file:///tmp/v/bson.browser.esm.mjs:1775:41)
      #     at serializeObjectId (/tmp/v/node_modules/bson/lib/parser/serializer.js:222:18)
      #     at Object.serializeInto (/tmp/v/node_modules/bson/lib/parser/serializer.js:801:25)
      #     at Object.serialize (/tmp/v/node_modules/bson/lib/bson.js:117:43)
      #     at file:///tmp/v/test.mjs:4:22
      #     at ModuleJob.run (internal/modules/esm/module_job.js:152:23)
      #     at async Loader.import (internal/modules/esm/loader.js:177:24)
      #     at async Object.loadESM (internal/process/esm_loader.js:68:5)
      

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

              Created:
              Updated:
              Resolved: