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

Contain and conditionally use Buffer API in the driver

    • Type: Icon: Improvement Improvement
    • Resolution: Unresolved
    • Priority: Icon: Unknown Unknown
    • None
    • Affects Version/s: None
    • Component/s: None
    • Labels:

      How are you using Mongo? What version of the server and driver are you using?

      I am trying to bundle the driver for a browser that does not have Node.js APIs.

      The current scope of Node.js Buffer APIs used is:

      globalThis.Buffer
      export class Buffer extends Uint8Array {
       static isBuffer(buffer): boolean;
       static byteLength(string): number;
       static alloc(space: number): Buffer;
       static allocUnsafe(space): Buffer;
       write(string, offset, format): Buffer;
       writeInt32LE
       writeUInt32LE
       readInt32LE
       readUInt8
       readUInt32LE
       static concat(buffers: Array<Uint8Array>): Buffer;
      }
      

      What is the feature/improvement you would like?

      We can easily reduce a number of APIs currently provided by Buffer instances by porting the logic found in BSON's byteUtils to abstract the runtime's byte level APIs.

      We should box up the Node.js specific APIs into abstract helpers that can prefer Node.js APIs when available and otherwise use the equivalent common ES APIs.

      Example:

      const ByteUtils = {
        allocate(size) {
          // This way we continue to use the preferred performant API when it is offered
          if (globalThis.Buffer?.allocUnsafe) return Buffer.allocUnsafe(size);
          // But we don't need the Buffer to exist on the global to have a functioning driver
          return new Uint8Array(size);
        }
      }
      

      What use case would this feature/improvement enable?

            Assignee:
            Unassigned Unassigned
            Reporter:
            neal.beeken@mongodb.com Neal Beeken
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: