Skip to content

Commit 2379a1e

Browse files
committed
feat(NODE-7314): export byteUtils & add missing methods
1 parent a23e788 commit 2379a1e

File tree

5 files changed

+54
-2
lines changed

5 files changed

+54
-2
lines changed

src/bson.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ export {
4949
MaxKey,
5050
BSONRegExp,
5151
Decimal128,
52-
NumberUtils
52+
NumberUtils,
53+
ByteUtils
5354
};
5455
export { BSONValue, bsonType, type BSONTypeTag } from './bson_value';
5556
export { BSONError, BSONVersionError, BSONRuntimeError, BSONOffsetError } from './error';

src/utils/byte_utils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ export type ByteUtils = {
1515
allocate: (size: number) => Uint8Array;
1616
/** Create empty space of size, use pooled memory when available */
1717
allocateUnsafe: (size: number) => Uint8Array;
18+
/** Compare 2 Uint8Arrays lexicographically */
19+
compare: (buffer1: Uint8Array, buffer2: Uint8Array) => -1 | 0 | 1;
20+
/** Concatenating all the Uint8Arrays in new Uint8Array. */
21+
concat: (list: Uint8Array[]) => Uint8Array;
1822
/** Check if two Uint8Arrays are deep equal */
1923
equals: (a: Uint8Array, b: Uint8Array) => boolean;
2024
/** Check if two Uint8Arrays are deep equal */

src/utils/node_byte_utils.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ type NodeJsBuffer = ArrayBufferView &
1010
toString: (this: Uint8Array, encoding: NodeJsEncoding, start?: number, end?: number) => string;
1111
equals: (this: Uint8Array, other: Uint8Array) => boolean;
1212
swap32: (this: NodeJsBuffer) => NodeJsBuffer;
13+
compare: (this: Uint8Array, other: Uint8Array) => -1 | 0 | 1;
1314
};
1415
type NodeJsBufferConstructor = Omit<Uint8ArrayConstructor, 'from'> & {
1516
alloc: (size: number) => NodeJsBuffer;
@@ -21,6 +22,7 @@ type NodeJsBufferConstructor = Omit<Uint8ArrayConstructor, 'from'> & {
2122
from(base64: string, encoding: NodeJsEncoding): NodeJsBuffer;
2223
byteLength(input: string, encoding: 'utf8'): number;
2324
isBuffer(value: unknown): value is NodeJsBuffer;
25+
concat(list: Uint8Array[]): NodeJsBuffer;
2426
};
2527

2628
// This can be nullish, but we gate the nodejs functions on being exported whether or not this exists
@@ -88,6 +90,14 @@ export const nodeJsByteUtils = {
8890
return Buffer.allocUnsafe(size);
8991
},
9092

93+
compare(a: Uint8Array, b: Uint8Array) {
94+
return nodeJsByteUtils.toLocalBufferType(a).compare(b);
95+
},
96+
97+
concat(list: Uint8Array[]): NodeJsBuffer {
98+
return Buffer.concat(list);
99+
},
100+
91101
equals(a: Uint8Array, b: Uint8Array): boolean {
92102
return nodeJsByteUtils.toLocalBufferType(a).equals(b);
93103
},

src/utils/web_byte_utils.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,42 @@ export const webByteUtils = {
114114
return webByteUtils.allocate(size);
115115
},
116116

117+
compare(a: Uint8Array, b: Uint8Array): -1 | 0 | 1 {
118+
if (a === b) return 0;
119+
120+
const len = Math.min(a.length, b.length);
121+
122+
for (let i = 0; i < len; i++) {
123+
if (a[i] < b[i]) return -1;
124+
if (a[i] > b[i]) return 1;
125+
}
126+
127+
if (a.length < b.length) return -1;
128+
if (a.length > b.length) return 1;
129+
130+
return 0;
131+
},
132+
133+
concat(list: Uint8Array[]): Uint8Array {
134+
if (list.length === 0) return webByteUtils.allocate(0);
135+
if (list.length === 1) return list[0];
136+
137+
let totalLength = 0;
138+
for (const arr of list) {
139+
totalLength += arr.length;
140+
}
141+
142+
const result = webByteUtils.allocate(totalLength);
143+
let offset = 0;
144+
145+
for (const arr of list) {
146+
result.set(arr, offset);
147+
offset += arr.length;
148+
}
149+
150+
return result;
151+
},
152+
117153
equals(a: Uint8Array, b: Uint8Array): boolean {
118154
if (a.byteLength !== b.byteLength) {
119155
return false;

test/node/exports.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ const EXPECTED_EXPORTS = [
3939
'deserializeStream',
4040
'BSON',
4141
'bsonType',
42-
'NumberUtils'
42+
'NumberUtils',
43+
'ByteUtils'
4344
];
4445

4546
const EXPECTED_EJSON_EXPORTS = ['parse', 'stringify', 'serialize', 'deserialize'];

0 commit comments

Comments
 (0)