Use BSON ByteUtils instead of Nodejs Buffer

XMLWordPrintableJSON

    • Type: Sub-task
    • Resolution: Unresolved
    • Priority: Unknown
    • None
    • Affects Version/s: None
    • Component/s: None
    • 0
    • None
    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?
    • None
    • None
    • None
    • None
    • None
    • None

      Use Case

      As a... Node.js Driver Developer
      I want... to replace direct Buffer manipulations with the platform-agnostic ByteUtils from the BSON library.
      So that... I can eliminate the driver's hard dependency on the Node.js Buffer global, making the codebase compatible with standard Web APIs (Uint8Array) and portable to environments like Cloudflare Workers, Deno, and the Browser.

      User Experience

      • Internal: The driver codebase no longer contains direct references to Buffer.concat, Buffer.compare, Buffer.from, or buffer.toString().
      • Type Safety: Internal method signatures rely exclusively on Uint8Array.
      • Centralization: All binary operations are routed through import { ByteUtils } from 'bson', which handles the environment-specific implementation (using native Buffer on Node for speed, and Uint8Array polyfills on the Web).

      Dependencies

      • Requires a version of the bson library that exposes ByteUtils (or onDemand.ByteUtils).
      • This is a blocker for Milestone 0 (Global Sanitization). The sandbox tests will not pass until this is complete.

      Risks/Unknowns

      • Performance Regression: Maby methods such as Buffer.concat is extremely optimized in Node.js. A generic Uint8Array concatenation implementation might introduce latency in high-throughput scenarios.
        • Mitigation: ByteUtils inside bson should ideally detect if it is running in Node and use Buffer under the hood, while exposing a Uint8Array interface.
      • Type Incompatibility: Some Node.js core modules (like crypto or stream) explicitly demand Buffer. Passing a generic Uint8Array might throw errors in older Node versions.
        • Mitigation: We may need to cast Uint8Array to Buffer specifically at the "System Boundary" (where we talk to net or crypto), but keep the "Core Logic" pure.

      Acceptance Criteria

      Implementation Requirements

      Refactor: Search and replace all usages of:

      • Buffer.concat([...]) -> ByteUtils.concat([...])
      • Buffer.compare(a, b) -> ByteUtils.compare(a, b)
      • buf.equals(other)-> ByteUtils.equals(buf, other)
      • etc.

      Update internal interfaces (parsers, serializers, command builders) to accept Uint8Array instead of Buffer.

      Ensure public APIs that currently return Buffer continue to do so (or return a subclass of Uint8Array that is effectively a Buffer in Node) to avoid breaking user code.

      Testing Requirements

      • All existing and new sandbox tests pass

      Documentation Requirements

      • Add section to Contributing guide

      Follow Up Requirements

      • Enable no-restricted-globals: ["error", "Buffer"] in ESLint configuration for the src/ directory to prevent regression.
      • Highlight those changes in the release section
      •  

            Assignee:
            Unassigned
            Reporter:
            Sergey Zelenov
            None
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: