-
Type:
Improvement
-
Resolution: Unresolved
-
Priority:
Unknown
-
None
-
Affects Version/s: None
-
Component/s: None
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); } }