Skip to content

Commit 5dbbf80

Browse files
committed
pr feedback:
- replace `BSON.x.y` code with `x.y` - added readUint32LE wrapper around NumberUtils.getUint32LE
1 parent 4258a42 commit 5dbbf80

File tree

6 files changed

+23
-15
lines changed

6 files changed

+23
-15
lines changed

src/bson.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@ export const readInt32LE = (buffer: Uint8Array, offset: number): number => {
5555
return NumberUtils.getInt32LE(buffer, offset);
5656
};
5757

58+
// readUint32LE, reads a 32-bit unsigned integer from buffer at given offset
59+
// throws if offset is out of bounds
60+
export const readUint32LE = (buffer: Uint8Array, offset: number): number => {
61+
validateBufferInputs(buffer, offset, 4);
62+
return NumberUtils.getUint32LE(buffer, offset);
63+
};
64+
5865
/**
5966
* BSON Serialization options.
6067
* @public

src/cmap/auth/aws4.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { BSON } from '../../bson';
1+
import { ByteUtils } from '../../bson';
22
import { type AWSCredentials } from '../../deps';
33

44
export type AwsSigv4Options = {
@@ -31,7 +31,7 @@ export type SignedHeaders = {
3131
const getHexSha256 = async (str: string): Promise<string> => {
3232
const data = stringToBuffer(str);
3333
const hashBuffer = await crypto.subtle.digest('SHA-256', data);
34-
const hashHex = BSON.ByteUtils.toHex(new Uint8Array(hashBuffer));
34+
const hashHex = ByteUtils.toHex(new Uint8Array(hashBuffer));
3535
return hashHex;
3636
};
3737

@@ -81,8 +81,8 @@ const convertHeaderValue = (value: string | number) => {
8181
* @returns Uint8Array containing the UTF-8 encoded string.
8282
*/
8383
function stringToBuffer(str: string): Uint8Array {
84-
const data = new Uint8Array(BSON.ByteUtils.utf8ByteLength(str));
85-
BSON.ByteUtils.encodeUTF8Into(data, str, 0);
84+
const data = new Uint8Array(ByteUtils.utf8ByteLength(str));
85+
ByteUtils.encodeUTF8Into(data, str, 0);
8686
return data;
8787
}
8888

@@ -189,7 +189,7 @@ export async function aws4Sign(
189189

190190
// 5. Calculate the signature
191191
const signatureBuffer = await getHmacSha256(signingKey, stringToSign);
192-
const signature = BSON.ByteUtils.toHex(signatureBuffer);
192+
const signature = ByteUtils.toHex(signatureBuffer);
193193

194194
// 6. Add the signature to the request
195195
// Calculate the Authorization header

src/cmap/auth/mongodb_aws.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Binary, BSONSerializeOptions } from '../../bson';
1+
import { type Binary, type BSONSerializeOptions, ByteUtils } from '../../bson';
22
import * as BSON from '../../bson';
33
import {
44
MongoCompatibilityError,
@@ -92,7 +92,7 @@ export class MongoDBAWS extends AuthProvider {
9292
throw new MongoRuntimeError(`Invalid server nonce length ${serverNonce.length}, expected 64`);
9393
}
9494

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

@@ -115,7 +115,7 @@ export class MongoDBAWS extends AuthProvider {
115115
headers: {
116116
'Content-Type': 'application/x-www-form-urlencoded',
117117
'Content-Length': body.length,
118-
'X-MongoDB-Server-Nonce': BSON.ByteUtils.toBase64(serverNonce),
118+
'X-MongoDB-Server-Nonce': ByteUtils.toBase64(serverNonce),
119119
'X-MongoDB-GS2-CB-Flag': 'n'
120120
},
121121
path: '/',

src/cmap/commands.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import {
55
type Document,
66
type Long,
77
NumberUtils,
8-
readInt32LE
8+
readInt32LE,
9+
readUint32LE
910
} from '../bson';
1011
import { MongoInvalidArgumentError, MongoRuntimeError } from '../error';
1112
import { type ReadPreference } from '../read_preference';
@@ -37,7 +38,7 @@ const QUERY_FAILURE = 2;
3738
const SHARD_CONFIG_STALE = 4;
3839
const AWAIT_CAPABLE = 8;
3940

40-
const encodeUTF8Into = BSON.ByteUtils.encodeUTF8Into;
41+
const encodeUTF8Into = ByteUtils.encodeUTF8Into;
4142

4243
/** @internal */
4344
export type WriteProtocolMessageType = OpQueryRequest | OpMsgRequest;
@@ -715,7 +716,7 @@ export class OpMsgResponse {
715716
while (this.index < this.data.length) {
716717
const payloadType = this.data[this.index++];
717718
if (payloadType === 0) {
718-
const bsonSize = readInt32LE(this.data, this.index);
719+
const bsonSize = readUint32LE(this.data, this.index);
719720
const bin = this.data.subarray(this.index, this.index + bsonSize);
720721

721722
this.sections.push(bin);

src/cmap/connection.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ function streamIdentifier(stream: Stream, options: ConnectionOptions): string {
176176
return HostAddress.fromHostPort(remoteAddress, remotePort).toString();
177177
}
178178

179-
return BSON.ByteUtils.toHex(uuidV4());
179+
return ByteUtils.toHex(uuidV4());
180180
}
181181

182182
/** @internal */

src/cmap/handshake/client_metadata.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as os from 'os';
22
import * as process from 'process';
33

4-
import { BSON, ByteUtils, type Document, Int32 } from '../../bson';
4+
import { BSON, ByteUtils, type Document, Int32, NumberUtils } from '../../bson';
55
import { MongoInvalidArgumentError } from '../../error';
66
import type { DriverInfo, MongoOptions } from '../../mongo_client';
77
import { fileIsAccessible } from '../../utils';
@@ -114,7 +114,7 @@ export async function makeClientMetadata(
114114
// Add app name first, it must be sent
115115
if (appName.length > 0) {
116116
const name =
117-
BSON.ByteUtils.utf8ByteLength(appName) <= 128
117+
ByteUtils.utf8ByteLength(appName) <= 128
118118
? appName
119119
: ByteUtils.toUTF8(ByteUtils.fromUTF8(appName), 0, 128, false);
120120
metadataDocument.ifItFitsItSits('application', { name });
@@ -336,7 +336,7 @@ declare const Bun: { (): void; version?: string } | undefined;
336336
* with a future change to these global objects.
337337
*/
338338
function getRuntimeInfo(): string {
339-
const endianness = BSON.NumberUtils.isBigEndian ? 'BE' : 'LE';
339+
const endianness = NumberUtils.isBigEndian ? 'BE' : 'LE';
340340
if ('Deno' in globalThis) {
341341
const version = typeof Deno?.version?.deno === 'string' ? Deno?.version?.deno : '0.0.0-unknown';
342342

0 commit comments

Comments
 (0)