Skip to content
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
24daacd
using bson for buffer calls
PavelSafronov Jan 14, 2026
51efd36
removing unnecessary code
PavelSafronov Jan 14, 2026
3d28ae5
lots of changes related to removing Buffer from internal uses:
PavelSafronov Jan 14, 2026
74fc9e3
test fixes
PavelSafronov Jan 14, 2026
732b12e
remove onDemand accessors
PavelSafronov Jan 14, 2026
0ee1a17
update based on isUint8Array signature change
PavelSafronov Jan 14, 2026
5ddfae4
remove unnecessary toLocalBufferType calls
PavelSafronov Jan 14, 2026
45ebf1e
point to bson PR https://github.com/mongodb/js-bson/pull/860/
PavelSafronov Jan 15, 2026
79d0d19
use bson 7.1.0
PavelSafronov Jan 16, 2026
82a34bb
pick up BSON 7.1.1
PavelSafronov Jan 20, 2026
fbb742e
Merge branch 'main' into NODE-7315
PavelSafronov Jan 20, 2026
cbc488c
resolve conflicts
PavelSafronov Jan 20, 2026
e649b6f
Merge branch 'main' into NODE-7315
PavelSafronov Jan 20, 2026
12ff51f
add an easy-to-use copyBuffer method
PavelSafronov Jan 21, 2026
983571b
minor fix: create copy of data
PavelSafronov Jan 21, 2026
e637852
remove unnecessary ByteUtils.toLocalBufferType calls
PavelSafronov Jan 22, 2026
6c9d928
Merge branch 'main' into NODE-7315
PavelSafronov Jan 22, 2026
736ba62
pr feedback
PavelSafronov Jan 22, 2026
722379a
lint fix
PavelSafronov Jan 22, 2026
45c9875
Apply suggestion from @addaleax
PavelSafronov Jan 23, 2026
ecc19f2
pr feedback: undo test changes and remove ByteUtils
PavelSafronov Jan 26, 2026
a04ec7b
Merge branch 'main' into NODE-7315
PavelSafronov Jan 26, 2026
cafd0e4
pr feedback
PavelSafronov Feb 2, 2026
ca016d9
fix bugs
PavelSafronov Feb 3, 2026
615f37f
pick up BSON 7.2.0
PavelSafronov Feb 3, 2026
ea641e6
Merge branch 'main' into NODE-7315
PavelSafronov Feb 3, 2026
4258a42
removed writeInt32LE, use NumberUtils directly
PavelSafronov Feb 4, 2026
5dbbf80
pr feedback:
PavelSafronov Feb 4, 2026
6a5087a
lint fix
PavelSafronov Feb 4, 2026
58739b6
fix bug
PavelSafronov Feb 4, 2026
ad9e2aa
pr feedback
PavelSafronov Feb 4, 2026
ffcdfe0
pr feedback
PavelSafronov Feb 5, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,13 @@
}
]
}
],
"no-restricted-globals": [
Comment thread
baileympearson marked this conversation as resolved.
"error",
{
"name": "Buffer",
"message": "Use Uin8Array instead"
Comment thread
PavelSafronov marked this conversation as resolved.
Outdated
}
]
}
},
Expand Down
55 changes: 51 additions & 4 deletions src/bson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export {
BSONRegExp,
BSONSymbol,
BSONType,
ByteUtils,
calculateObjectSize,
Code,
DBRef,
Expand Down Expand Up @@ -38,10 +39,56 @@ export function parseToElementsToArray(bytes: Uint8Array, offset?: number): BSON
return Array.isArray(res) ? res : [...res];
}

export const getInt32LE = BSON.onDemand.NumberUtils.getInt32LE;
export const getFloat64LE = BSON.onDemand.NumberUtils.getFloat64LE;
export const getBigInt64LE = BSON.onDemand.NumberUtils.getBigInt64LE;
export const toUTF8 = BSON.onDemand.ByteUtils.toUTF8;
export const getInt32LE = BSON.NumberUtils.getInt32LE;
export const getFloat64LE = BSON.NumberUtils.getFloat64LE;
export const getBigInt64LE = BSON.NumberUtils.getBigInt64LE;
export const toUTF8 = BSON.ByteUtils.toUTF8;
export const fromUTF8 = BSON.ByteUtils.fromUTF8;
Comment thread
baileympearson marked this conversation as resolved.
Outdated
export const fromBase64 = BSON.ByteUtils.fromBase64;
export const fromNumberArray = BSON.ByteUtils.fromNumberArray;
export const concatBuffers = BSON.ByteUtils.concat;
export const allocateBuffer = BSON.ByteUtils.allocate;
export const allocateUnsafeBuffer = BSON.ByteUtils.allocateUnsafe;

// writeInt32LE, same order of arguments as Buffer.writeInt32LE
export const writeInt32LE = (destination: Uint8Array, value: number, offset: number) =>
BSON.NumberUtils.setInt32LE(destination, offset, value);

// copyBuffer: copies from source buffer to target buffer, returns number of bytes copied
// inputs are explicitly named to avoid confusion
export const copyBuffer = (input: {
Comment thread
baileympearson marked this conversation as resolved.
Outdated
source: Uint8Array;
target: Uint8Array;
targetStart?: number;
sourceStart?: number;
sourceEnd?: number;
}): number => {
const { source, target, targetStart = 0, sourceStart = 0, sourceEnd } = input;
const sourceEndActual = sourceEnd ?? source.length;
const srcSlice = source.subarray(sourceStart, sourceEndActual);
const maxLen = Math.min(srcSlice.length, target.length - targetStart);
if (maxLen <= 0) {
return 0;
}
target.set(srcSlice.subarray(0, maxLen), targetStart);
return maxLen;
};

// validates buffer inputs, used for read operations
const validateBufferInputs = (buffer: Uint8Array, offset: number, length: number) => {
if (offset < 0 || offset + length > buffer.length) {
throw new RangeError(
`Attempt to access memory outside buffer bounds: buffer length: ${buffer.length}, offset: ${offset}, length: ${length}`
);
}
};

// readInt32LE, reads a 32-bit integer from buffer at given offset
// throws if offset is out of bounds
export const readInt32LE = (buffer: Uint8Array, offset: number): number => {
validateBufferInputs(buffer, offset, 4);
Comment thread
baileympearson marked this conversation as resolved.
return getInt32LE(buffer, offset);
};

/**
* BSON Serialization options.
Expand Down
28 changes: 17 additions & 11 deletions src/client-side-encryption/auto_encrypter.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { type MongoCrypt, type MongoCryptOptions } from 'mongodb-client-encryption';
import * as net from 'net';

import { deserialize, type Document, serialize } from '../bson';
import { ByteUtils, deserialize, type Document, serialize } from '../bson';
import { type CommandOptions, type ProxyOptions } from '../cmap/connection';
import { kDecorateResult } from '../constants';
import { getMongoDBClientEncryption } from '../deps';
Expand Down Expand Up @@ -256,20 +256,26 @@ export class AutoEncrypter {
errorWrapper: defaultErrorWrapper
};
if (options.schemaMap) {
mongoCryptOptions.schemaMap = Buffer.isBuffer(options.schemaMap)
? options.schemaMap
: (serialize(options.schemaMap) as Buffer);
if (ByteUtils.isUint8Array(options.schemaMap)) {
mongoCryptOptions.schemaMap = options.schemaMap;
} else {
mongoCryptOptions.schemaMap = serialize(options.schemaMap);
}
}

if (options.encryptedFieldsMap) {
mongoCryptOptions.encryptedFieldsMap = Buffer.isBuffer(options.encryptedFieldsMap)
? options.encryptedFieldsMap
: (serialize(options.encryptedFieldsMap) as Buffer);
if (ByteUtils.isUint8Array(options.encryptedFieldsMap)) {
mongoCryptOptions.encryptedFieldsMap = options.encryptedFieldsMap;
} else {
mongoCryptOptions.encryptedFieldsMap = serialize(options.encryptedFieldsMap);
}
}

mongoCryptOptions.kmsProviders = !Buffer.isBuffer(this._kmsProviders)
? (serialize(this._kmsProviders) as Buffer)
: this._kmsProviders;
if (ByteUtils.isUint8Array(this._kmsProviders)) {
mongoCryptOptions.kmsProviders = this._kmsProviders;
} else {
mongoCryptOptions.kmsProviders = serialize(this._kmsProviders);
}

if (options.options?.logger) {
mongoCryptOptions.logger = options.options.logger;
Expand Down Expand Up @@ -396,7 +402,7 @@ export class AutoEncrypter {
return cmd;
}

const commandBuffer = Buffer.isBuffer(cmd) ? cmd : serialize(cmd, options);
const commandBuffer: Uint8Array = ByteUtils.isUint8Array(cmd) ? cmd : serialize(cmd, options);
Comment thread
baileympearson marked this conversation as resolved.
Outdated
const context = this._mongocrypt.makeEncryptionContext(
MongoDBCollectionNamespace.fromString(ns).db,
commandBuffer
Expand Down
3 changes: 2 additions & 1 deletion src/client-side-encryption/client_encryption.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {

import {
type Binary,
ByteUtils,
deserialize,
type Document,
type Int32,
Expand Down Expand Up @@ -143,7 +144,7 @@ export class ClientEncryption {

const mongoCryptOptions: MongoCryptOptions = {
...options,
kmsProviders: !Buffer.isBuffer(this._kmsProviders)
kmsProviders: !ByteUtils.isUint8Array(this._kmsProviders)
Comment thread
baileympearson marked this conversation as resolved.
Outdated
? (serialize(this._kmsProviders) as Buffer)
: this._kmsProviders,
errorWrapper: defaultErrorWrapper
Expand Down
2 changes: 1 addition & 1 deletion src/cmap/auth/auth_provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class AuthContext {
/** A response from an initial auth attempt, only some mechanisms use this (e.g, SCRAM) */
response?: Document;
/** A random nonce generated for use in an authentication conversation */
nonce?: Buffer;
nonce?: Uint8Array;

constructor(
connection: Connection,
Expand Down
8 changes: 4 additions & 4 deletions src/cmap/auth/aws4.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export type SignedHeaders = {
const getHexSha256 = async (str: string): Promise<string> => {
const data = stringToBuffer(str);
const hashBuffer = await crypto.subtle.digest('SHA-256', data);
const hashHex = BSON.onDemand.ByteUtils.toHex(new Uint8Array(hashBuffer));
const hashHex = BSON.ByteUtils.toHex(new Uint8Array(hashBuffer));
return hashHex;
};

Expand Down Expand Up @@ -81,8 +81,8 @@ const convertHeaderValue = (value: string | number) => {
* @returns Uint8Array containing the UTF-8 encoded string.
*/
function stringToBuffer(str: string): Uint8Array {
const data = new Uint8Array(BSON.onDemand.ByteUtils.utf8ByteLength(str));
BSON.onDemand.ByteUtils.encodeUTF8Into(data, str, 0);
const data = new Uint8Array(BSON.ByteUtils.utf8ByteLength(str));
BSON.ByteUtils.encodeUTF8Into(data, str, 0);
return data;
}

Expand Down Expand Up @@ -189,7 +189,7 @@ export async function aws4Sign(

// 5. Calculate the signature
const signatureBuffer = await getHmacSha256(signingKey, stringToSign);
const signature = BSON.onDemand.ByteUtils.toHex(signatureBuffer);
const signature = BSON.ByteUtils.toHex(signatureBuffer);

// 6. Add the signature to the request
// Calculate the Authorization header
Expand Down
6 changes: 3 additions & 3 deletions src/cmap/auth/mongodb_aws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
MongoMissingCredentialsError,
MongoRuntimeError
} from '../../error';
import { ByteUtils, maxWireVersion, ns, randomBytes } from '../../utils';
import { maxWireVersion, ns, randomBytes } from '../../utils';
import { type AuthContext, AuthProvider } from './auth_provider';
import {
type AWSCredentialProvider,
Expand Down Expand Up @@ -92,7 +92,7 @@ export class MongoDBAWS extends AuthProvider {
throw new MongoRuntimeError(`Invalid server nonce length ${serverNonce.length}, expected 64`);
}

if (!ByteUtils.equals(serverNonce.subarray(0, nonce.byteLength), nonce)) {
if (!BSON.ByteUtils.equals(serverNonce.subarray(0, nonce.byteLength), nonce)) {
// throw because the serverNonce's leading 32 bytes must equal the client nonce's 32 bytes
// https://github.com/mongodb/specifications/blob/master/source/auth/auth.md#conversation-5

Expand All @@ -115,7 +115,7 @@ export class MongoDBAWS extends AuthProvider {
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': body.length,
'X-MongoDB-Server-Nonce': ByteUtils.toBase64(serverNonce),
'X-MongoDB-Server-Nonce': BSON.ByteUtils.toBase64(serverNonce),
'X-MongoDB-GS2-CB-Flag': 'n'
},
path: '/',
Expand Down
4 changes: 2 additions & 2 deletions src/cmap/auth/plain.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Binary } from '../../bson';
import { Binary, fromUTF8 } from '../../bson';
import { MongoMissingCredentialsError } from '../../error';
import { ns } from '../../utils';
import { type AuthContext, AuthProvider } from './auth_provider';
Expand All @@ -12,7 +12,7 @@ export class Plain extends AuthProvider {

const { username, password } = credentials;

const payload = new Binary(Buffer.from(`\x00${username}\x00${password}`));
const payload = new Binary(fromUTF8(`\x00${username}\x00${password}`));
const command = {
saslStart: 1,
mechanism: 'PLAIN',
Expand Down
Loading