Skip to content
Open
Show file tree
Hide file tree
Changes from all 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": [
"error",
{
"name": "Buffer",
"message": "Use Uint8Array instead"
}
]
}
},
Expand Down
25 changes: 20 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
},
"dependencies": {
"@mongodb-js/saslprep": "^1.3.0",
"bson": "^7.1.1",
"bson": "^7.2.0",
"mongodb-connection-string-url": "^7.0.0"
},
"peerDependencies": {
Expand Down
33 changes: 28 additions & 5 deletions src/bson.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable no-restricted-imports */
import { BSON, type DeserializeOptions, type SerializeOptions } from 'bson';
import { BSON, type DeserializeOptions, NumberUtils, type SerializeOptions } from 'bson';

export {
Binary,
Expand All @@ -8,6 +8,7 @@ export {
BSONRegExp,
BSONSymbol,
BSONType,
ByteUtils,
calculateObjectSize,
Code,
DBRef,
Expand Down Expand Up @@ -38,10 +39,32 @@ 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;
// 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);
return NumberUtils.getInt32LE(buffer, offset);
};

export const setUint32LE = (destination: Uint8Array, offset: number, value: number): 4 => {
destination[offset] = value;
value >>>= 8;
destination[offset + 1] = value;
value >>>= 8;
destination[offset + 2] = value;
value >>>= 8;
destination[offset + 3] = value;
return 4;
};

/**
* 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 = serialize(cmd, options);
const context = this._mongocrypt.makeEncryptionContext(
MongoDBCollectionNamespace.fromString(ns).db,
commandBuffer
Expand Down
4 changes: 1 addition & 3 deletions src/client-side-encryption/client_encryption.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,7 @@ export class ClientEncryption {

const mongoCryptOptions: MongoCryptOptions = {
...options,
kmsProviders: !Buffer.isBuffer(this._kmsProviders)
? (serialize(this._kmsProviders) as Buffer)
: this._kmsProviders,
kmsProviders: serialize(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
10 changes: 5 additions & 5 deletions src/cmap/auth/aws4.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BSON } from '../../bson';
import { ByteUtils } from '../../bson';
import { type AWSCredentials } from '../../deps';

export type AwsSigv4Options = {
Expand Down 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 = 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(ByteUtils.utf8ByteLength(str));
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 = ByteUtils.toHex(signatureBuffer);

// 6. Add the signature to the request
// Calculate the Authorization header
Expand Down
4 changes: 2 additions & 2 deletions src/cmap/auth/mongodb_aws.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { Binary, BSONSerializeOptions } from '../../bson';
import { type Binary, type BSONSerializeOptions, ByteUtils } from '../../bson';
import * as BSON from '../../bson';
import {
MongoCompatibilityError,
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
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, ByteUtils } 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(ByteUtils.fromUTF8(`\x00${username}\x00${password}`));
const command = {
saslStart: 1,
mechanism: 'PLAIN',
Expand Down
Loading