bson 6.9.0 → 6.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -14,7 +14,7 @@
14
14
  "vendor"
15
15
  ],
16
16
  "types": "bson.d.ts",
17
- "version": "6.9.0",
17
+ "version": "6.10.0",
18
18
  "author": {
19
19
  "name": "The MongoDB NodeJS Team",
20
20
  "email": "dbx-node@mongodb.com"
package/src/binary.ts CHANGED
@@ -4,6 +4,7 @@ import { BSONError } from './error';
4
4
  import { BSON_BINARY_SUBTYPE_UUID_NEW } from './constants';
5
5
  import { ByteUtils } from './utils/byte_utils';
6
6
  import { BSONValue } from './bson_value';
7
+ import { NumberUtils } from './utils/number_utils';
7
8
 
8
9
  /** @public */
9
10
  export type BinarySequence = Uint8Array | number[];
@@ -58,12 +59,60 @@ export class Binary extends BSONValue {
58
59
  static readonly SUBTYPE_COLUMN = 7;
59
60
  /** Sensitive BSON type */
60
61
  static readonly SUBTYPE_SENSITIVE = 8;
62
+ /** Vector BSON type */
63
+ static readonly SUBTYPE_VECTOR = 9;
61
64
  /** User BSON type */
62
65
  static readonly SUBTYPE_USER_DEFINED = 128;
63
66
 
64
- buffer!: Uint8Array;
65
- sub_type!: number;
66
- position!: number;
67
+ /** datatype of a Binary Vector (subtype: 9) */
68
+ static readonly VECTOR_TYPE = Object.freeze({
69
+ Int8: 0x03,
70
+ Float32: 0x27,
71
+ PackedBit: 0x10
72
+ } as const);
73
+
74
+ /**
75
+ * The bytes of the Binary value.
76
+ *
77
+ * The format of a Binary value in BSON is defined as:
78
+ * ```txt
79
+ * binary ::= int32 subtype (byte*)
80
+ * ```
81
+ *
82
+ * This `buffer` is the "(byte*)" segment.
83
+ *
84
+ * Unless the value is subtype 2, then deserialize will read the first 4 bytes as an int32 and set this to the remaining bytes.
85
+ *
86
+ * ```txt
87
+ * binary ::= int32 unsigned_byte(2) int32 (byte*)
88
+ * ```
89
+ *
90
+ * @see https://bsonspec.org/spec.html
91
+ */
92
+ public buffer: Uint8Array;
93
+ /**
94
+ * The binary subtype.
95
+ *
96
+ * Current defined values are:
97
+ *
98
+ * - `unsigned_byte(0)` Generic binary subtype
99
+ * - `unsigned_byte(1)` Function
100
+ * - `unsigned_byte(2)` Binary (Deprecated)
101
+ * - `unsigned_byte(3)` UUID (Deprecated)
102
+ * - `unsigned_byte(4)` UUID
103
+ * - `unsigned_byte(5)` MD5
104
+ * - `unsigned_byte(6)` Encrypted BSON value
105
+ * - `unsigned_byte(7)` Compressed BSON column
106
+ * - `unsigned_byte(8)` Sensitive
107
+ * - `unsigned_byte(9)` Vector
108
+ * - `unsigned_byte(128)` - `unsigned_byte(255)` User defined
109
+ */
110
+ public sub_type: number;
111
+ /**
112
+ * The Binary's `buffer` can be larger than the Binary's content.
113
+ * This property is used to determine where the content ends in the buffer.
114
+ */
115
+ public position: number;
67
116
 
68
117
  /**
69
118
  * Create a new Binary instance.
@@ -160,16 +209,15 @@ export class Binary extends BSONValue {
160
209
  }
161
210
 
162
211
  /**
163
- * Reads **length** bytes starting at **position**.
212
+ * Returns a view of **length** bytes starting at **position**.
164
213
  *
165
214
  * @param position - read from the given position in the Binary.
166
215
  * @param length - the number of bytes to read.
167
216
  */
168
- read(position: number, length: number): BinarySequence {
217
+ read(position: number, length: number): Uint8Array {
169
218
  length = length && length > 0 ? length : this.position;
170
-
171
- // Let's return the data based on the type we have
172
- return this.buffer.slice(position, position + length);
219
+ const end = position + length;
220
+ return this.buffer.subarray(position, end > this.position ? this.position : end);
173
221
  }
174
222
 
175
223
  /** returns a view of the binary value as a Uint8Array */
@@ -200,6 +248,11 @@ export class Binary extends BSONValue {
200
248
  /** @internal */
201
249
  toExtendedJSON(options?: EJSONOptions): BinaryExtendedLegacy | BinaryExtended {
202
250
  options = options || {};
251
+
252
+ if (this.sub_type === Binary.SUBTYPE_VECTOR) {
253
+ validateBinaryVector(this);
254
+ }
255
+
203
256
  const base64String = ByteUtils.toBase64(this.buffer);
204
257
 
205
258
  const subType = Number(this.sub_type).toString(16);
@@ -219,7 +272,7 @@ export class Binary extends BSONValue {
219
272
 
220
273
  toUUID(): UUID {
221
274
  if (this.sub_type === Binary.SUBTYPE_UUID) {
222
- return new UUID(this.buffer.slice(0, this.position));
275
+ return new UUID(this.buffer.subarray(0, this.position));
223
276
  }
224
277
 
225
278
  throw new BSONError(
@@ -272,6 +325,209 @@ export class Binary extends BSONValue {
272
325
  const subTypeArg = inspect(this.sub_type, options);
273
326
  return `Binary.createFromBase64(${base64Arg}, ${subTypeArg})`;
274
327
  }
328
+
329
+ /**
330
+ * If this Binary represents a Int8 Vector (`binary.buffer[0] === Binary.VECTOR_TYPE.Int8`),
331
+ * returns a copy of the bytes in a new Int8Array.
332
+ *
333
+ * If the Binary is not a Vector, or the datatype is not Int8, an error is thrown.
334
+ */
335
+ public toInt8Array(): Int8Array {
336
+ if (this.sub_type !== Binary.SUBTYPE_VECTOR) {
337
+ throw new BSONError('Binary sub_type is not Vector');
338
+ }
339
+
340
+ if (this.buffer[0] !== Binary.VECTOR_TYPE.Int8) {
341
+ throw new BSONError('Binary datatype field is not Int8');
342
+ }
343
+
344
+ return new Int8Array(
345
+ this.buffer.buffer.slice(this.buffer.byteOffset + 2, this.buffer.byteOffset + this.position)
346
+ );
347
+ }
348
+
349
+ /**
350
+ * If this Binary represents a Float32 Vector (`binary.buffer[0] === Binary.VECTOR_TYPE.Float32`),
351
+ * returns a copy of the bytes in a new Float32Array.
352
+ *
353
+ * If the Binary is not a Vector, or the datatype is not Float32, an error is thrown.
354
+ */
355
+ public toFloat32Array(): Float32Array {
356
+ if (this.sub_type !== Binary.SUBTYPE_VECTOR) {
357
+ throw new BSONError('Binary sub_type is not Vector');
358
+ }
359
+
360
+ if (this.buffer[0] !== Binary.VECTOR_TYPE.Float32) {
361
+ throw new BSONError('Binary datatype field is not Float32');
362
+ }
363
+
364
+ const floatBytes = new Uint8Array(
365
+ this.buffer.buffer.slice(this.buffer.byteOffset + 2, this.buffer.byteOffset + this.position)
366
+ );
367
+
368
+ if (NumberUtils.isBigEndian) ByteUtils.swap32(floatBytes);
369
+
370
+ return new Float32Array(floatBytes.buffer);
371
+ }
372
+
373
+ /**
374
+ * If this Binary represents packed bit Vector (`binary.buffer[0] === Binary.VECTOR_TYPE.PackedBit`),
375
+ * returns a copy of the bytes that are packed bits.
376
+ *
377
+ * Use `toBits` to get the unpacked bits.
378
+ *
379
+ * If the Binary is not a Vector, or the datatype is not PackedBit, an error is thrown.
380
+ */
381
+ public toPackedBits(): Uint8Array {
382
+ if (this.sub_type !== Binary.SUBTYPE_VECTOR) {
383
+ throw new BSONError('Binary sub_type is not Vector');
384
+ }
385
+
386
+ if (this.buffer[0] !== Binary.VECTOR_TYPE.PackedBit) {
387
+ throw new BSONError('Binary datatype field is not packed bit');
388
+ }
389
+
390
+ return new Uint8Array(
391
+ this.buffer.buffer.slice(this.buffer.byteOffset + 2, this.buffer.byteOffset + this.position)
392
+ );
393
+ }
394
+
395
+ /**
396
+ * If this Binary represents a Packed bit Vector (`binary.buffer[0] === Binary.VECTOR_TYPE.PackedBit`),
397
+ * returns a copy of the bit unpacked into a new Int8Array.
398
+ *
399
+ * Use `toPackedBits` to get the bits still in packed form.
400
+ *
401
+ * If the Binary is not a Vector, or the datatype is not PackedBit, an error is thrown.
402
+ */
403
+ public toBits(): Int8Array {
404
+ if (this.sub_type !== Binary.SUBTYPE_VECTOR) {
405
+ throw new BSONError('Binary sub_type is not Vector');
406
+ }
407
+
408
+ if (this.buffer[0] !== Binary.VECTOR_TYPE.PackedBit) {
409
+ throw new BSONError('Binary datatype field is not packed bit');
410
+ }
411
+
412
+ const byteCount = this.length() - 2;
413
+ const bitCount = byteCount * 8 - this.buffer[1];
414
+ const bits = new Int8Array(bitCount);
415
+
416
+ for (let bitOffset = 0; bitOffset < bits.length; bitOffset++) {
417
+ const byteOffset = (bitOffset / 8) | 0;
418
+ const byte = this.buffer[byteOffset + 2];
419
+ const shift = 7 - (bitOffset % 8);
420
+ const bit = (byte >> shift) & 1;
421
+ bits[bitOffset] = bit;
422
+ }
423
+
424
+ return bits;
425
+ }
426
+
427
+ /**
428
+ * Constructs a Binary representing an Int8 Vector.
429
+ * @param array - The array to store as a view on the Binary class
430
+ */
431
+ public static fromInt8Array(array: Int8Array): Binary {
432
+ const buffer = ByteUtils.allocate(array.byteLength + 2);
433
+ buffer[0] = Binary.VECTOR_TYPE.Int8;
434
+ buffer[1] = 0;
435
+ const intBytes = new Uint8Array(array.buffer, array.byteOffset, array.byteLength);
436
+ buffer.set(intBytes, 2);
437
+ return new this(buffer, this.SUBTYPE_VECTOR);
438
+ }
439
+
440
+ /** Constructs a Binary representing an Float32 Vector. */
441
+ public static fromFloat32Array(array: Float32Array): Binary {
442
+ const binaryBytes = ByteUtils.allocate(array.byteLength + 2);
443
+ binaryBytes[0] = Binary.VECTOR_TYPE.Float32;
444
+ binaryBytes[1] = 0;
445
+
446
+ const floatBytes = new Uint8Array(array.buffer, array.byteOffset, array.byteLength);
447
+ binaryBytes.set(floatBytes, 2);
448
+
449
+ if (NumberUtils.isBigEndian) ByteUtils.swap32(new Uint8Array(binaryBytes.buffer, 2));
450
+
451
+ return new this(binaryBytes, this.SUBTYPE_VECTOR);
452
+ }
453
+
454
+ /**
455
+ * Constructs a Binary representing a packed bit Vector.
456
+ *
457
+ * Use `fromBits` to pack an array of 1s and 0s.
458
+ */
459
+ public static fromPackedBits(array: Uint8Array, padding = 0): Binary {
460
+ const buffer = ByteUtils.allocate(array.byteLength + 2);
461
+ buffer[0] = Binary.VECTOR_TYPE.PackedBit;
462
+ buffer[1] = padding;
463
+ buffer.set(array, 2);
464
+ return new this(buffer, this.SUBTYPE_VECTOR);
465
+ }
466
+
467
+ /**
468
+ * Constructs a Binary representing an Packed Bit Vector.
469
+ * @param array - The array of 1s and 0s to pack into the Binary instance
470
+ */
471
+ public static fromBits(bits: ArrayLike<number>): Binary {
472
+ const byteLength = (bits.length + 7) >>> 3; // ceil(bits.length / 8)
473
+ const bytes = new Uint8Array(byteLength + 2);
474
+ bytes[0] = Binary.VECTOR_TYPE.PackedBit;
475
+
476
+ const remainder = bits.length % 8;
477
+ bytes[1] = remainder === 0 ? 0 : 8 - remainder;
478
+
479
+ for (let bitOffset = 0; bitOffset < bits.length; bitOffset++) {
480
+ const byteOffset = bitOffset >>> 3; // floor(bitOffset / 8)
481
+ const bit = bits[bitOffset];
482
+
483
+ if (bit !== 0 && bit !== 1) {
484
+ throw new BSONError(
485
+ `Invalid bit value at ${bitOffset}: must be 0 or 1, found ${bits[bitOffset]}`
486
+ );
487
+ }
488
+
489
+ if (bit === 0) continue;
490
+
491
+ const shift = 7 - (bitOffset % 8);
492
+ bytes[byteOffset + 2] |= bit << shift;
493
+ }
494
+
495
+ return new this(bytes, Binary.SUBTYPE_VECTOR);
496
+ }
497
+ }
498
+
499
+ export function validateBinaryVector(vector: Binary): void {
500
+ if (vector.sub_type !== Binary.SUBTYPE_VECTOR) return;
501
+
502
+ const size = vector.position;
503
+
504
+ // NOTE: Validation is only applied to **KNOWN** vector types
505
+ // If a new datatype is introduced, a future version of the library will need to add validation
506
+ const datatype = vector.buffer[0];
507
+
508
+ // NOTE: We do not enable noUncheckedIndexedAccess so TS believes this is always number
509
+ // a Binary vector may be empty, in which case the padding is undefined
510
+ // this possible value is tolerable for our validation checks
511
+ const padding: number | undefined = vector.buffer[1];
512
+
513
+ if (
514
+ (datatype === Binary.VECTOR_TYPE.Float32 || datatype === Binary.VECTOR_TYPE.Int8) &&
515
+ padding !== 0
516
+ ) {
517
+ throw new BSONError('Invalid Vector: padding must be zero for int8 and float32 vectors');
518
+ }
519
+
520
+ if (datatype === Binary.VECTOR_TYPE.PackedBit && padding !== 0 && size === 2) {
521
+ throw new BSONError(
522
+ 'Invalid Vector: padding must be zero for packed bit vectors that are empty'
523
+ );
524
+ }
525
+
526
+ if (datatype === Binary.VECTOR_TYPE.PackedBit && padding > 7) {
527
+ throw new BSONError(
528
+ `Invalid Vector: padding must be a value between 0 and 7. found: ${padding}`
529
+ );
530
+ }
275
531
  }
276
532
 
277
533
  /** @public */
@@ -296,7 +296,7 @@ function deserializeObject(
296
296
 
297
297
  // We have a raw value
298
298
  if (raw) {
299
- value = buffer.slice(index, index + objectSize);
299
+ value = buffer.subarray(index, index + objectSize);
300
300
  } else {
301
301
  let objectOptions = options;
302
302
  if (!globalUTFValidation) {
@@ -374,52 +374,24 @@ function deserializeObject(
374
374
  if (binarySize > buffer.byteLength)
375
375
  throw new BSONError('Binary type size larger than document size');
376
376
 
377
- // Decode as raw Buffer object if options specifies it
378
- if (buffer['slice'] != null) {
379
- // If we have subtype 2 skip the 4 bytes for the size
380
- if (subType === Binary.SUBTYPE_BYTE_ARRAY) {
381
- binarySize = NumberUtils.getInt32LE(buffer, index);
382
- index += 4;
383
- if (binarySize < 0)
384
- throw new BSONError('Negative binary type element size found for subtype 0x02');
385
- if (binarySize > totalBinarySize - 4)
386
- throw new BSONError('Binary type with subtype 0x02 contains too long binary size');
387
- if (binarySize < totalBinarySize - 4)
388
- throw new BSONError('Binary type with subtype 0x02 contains too short binary size');
389
- }
377
+ // If we have subtype 2 skip the 4 bytes for the size
378
+ if (subType === Binary.SUBTYPE_BYTE_ARRAY) {
379
+ binarySize = NumberUtils.getInt32LE(buffer, index);
380
+ index += 4;
381
+ if (binarySize < 0)
382
+ throw new BSONError('Negative binary type element size found for subtype 0x02');
383
+ if (binarySize > totalBinarySize - 4)
384
+ throw new BSONError('Binary type with subtype 0x02 contains too long binary size');
385
+ if (binarySize < totalBinarySize - 4)
386
+ throw new BSONError('Binary type with subtype 0x02 contains too short binary size');
387
+ }
390
388
 
391
- if (promoteBuffers && promoteValues) {
392
- value = ByteUtils.toLocalBufferType(buffer.slice(index, index + binarySize));
393
- } else {
394
- value = new Binary(buffer.slice(index, index + binarySize), subType);
395
- if (subType === constants.BSON_BINARY_SUBTYPE_UUID_NEW && UUID.isValid(value)) {
396
- value = value.toUUID();
397
- }
398
- }
389
+ if (promoteBuffers && promoteValues) {
390
+ value = ByteUtils.toLocalBufferType(buffer.subarray(index, index + binarySize));
399
391
  } else {
400
- // If we have subtype 2 skip the 4 bytes for the size
401
- if (subType === Binary.SUBTYPE_BYTE_ARRAY) {
402
- binarySize = NumberUtils.getInt32LE(buffer, index);
403
- index += 4;
404
- if (binarySize < 0)
405
- throw new BSONError('Negative binary type element size found for subtype 0x02');
406
- if (binarySize > totalBinarySize - 4)
407
- throw new BSONError('Binary type with subtype 0x02 contains too long binary size');
408
- if (binarySize < totalBinarySize - 4)
409
- throw new BSONError('Binary type with subtype 0x02 contains too short binary size');
410
- }
411
-
412
- if (promoteBuffers && promoteValues) {
413
- value = ByteUtils.allocateUnsafe(binarySize);
414
- // Copy the data
415
- for (i = 0; i < binarySize; i++) {
416
- value[i] = buffer[index + i];
417
- }
418
- } else {
419
- value = new Binary(buffer.slice(index, index + binarySize), subType);
420
- if (subType === constants.BSON_BINARY_SUBTYPE_UUID_NEW && UUID.isValid(value)) {
421
- value = value.toUUID();
422
- }
392
+ value = new Binary(buffer.subarray(index, index + binarySize), subType);
393
+ if (subType === constants.BSON_BINARY_SUBTYPE_UUID_NEW && UUID.isValid(value)) {
394
+ value = value.toUUID();
423
395
  }
424
396
  }
425
397
 
@@ -1,4 +1,4 @@
1
- import { Binary } from '../binary';
1
+ import { Binary, validateBinaryVector } from '../binary';
2
2
  import type { BSONSymbol, DBRef, Document, MaxKey } from '../bson';
3
3
  import type { Code } from '../code';
4
4
  import * as constants from '../constants';
@@ -495,6 +495,10 @@ function serializeBinary(buffer: Uint8Array, key: string, value: Binary, index:
495
495
  index += NumberUtils.setInt32LE(buffer, index, size);
496
496
  }
497
497
 
498
+ if (value.sub_type === Binary.SUBTYPE_VECTOR) {
499
+ validateBinaryVector(value);
500
+ }
501
+
498
502
  if (size <= 16) {
499
503
  for (let i = 0; i < size; i++) buffer[index + i] = data[i];
500
504
  } else {
@@ -39,6 +39,8 @@ export type ByteUtils = {
39
39
  encodeUTF8Into: (destination: Uint8Array, source: string, byteOffset: number) => number;
40
40
  /** Generate a Uint8Array filled with random bytes with byteLength */
41
41
  randomBytes: (byteLength: number) => Uint8Array;
42
+ /** Interprets `buffer` as an array of 32-bit values and swaps the byte order in-place. */
43
+ swap32: (buffer: Uint8Array) => Uint8Array;
42
44
  };
43
45
 
44
46
  declare const Buffer: { new (): unknown; prototype?: { _isBuffer?: boolean } } | undefined;
@@ -9,6 +9,7 @@ type NodeJsBuffer = ArrayBufferView &
9
9
  copy(target: Uint8Array, targetStart: number, sourceStart: number, sourceEnd: number): number;
10
10
  toString: (this: Uint8Array, encoding: NodeJsEncoding, start?: number, end?: number) => string;
11
11
  equals: (this: Uint8Array, other: Uint8Array) => boolean;
12
+ swap32: (this: NodeJsBuffer) => NodeJsBuffer;
12
13
  };
13
14
  type NodeJsBufferConstructor = Omit<Uint8ArrayConstructor, 'from'> & {
14
15
  alloc: (size: number) => NodeJsBuffer;
@@ -83,7 +84,7 @@ export const nodeJsByteUtils = {
83
84
  return Buffer.from(potentialBuffer);
84
85
  }
85
86
 
86
- throw new BSONError(`Cannot create Buffer from ${String(potentialBuffer)}`);
87
+ throw new BSONError(`Cannot create Buffer from the passed potentialBuffer.`);
87
88
  },
88
89
 
89
90
  allocate(size: number): NodeJsBuffer {
@@ -159,5 +160,9 @@ export const nodeJsByteUtils = {
159
160
  return nodeJsByteUtils.toLocalBufferType(buffer).write(source, byteOffset, undefined, 'utf8');
160
161
  },
161
162
 
162
- randomBytes: nodejsRandomBytes
163
+ randomBytes: nodejsRandomBytes,
164
+
165
+ swap32(buffer: Uint8Array): NodeJsBuffer {
166
+ return nodeJsByteUtils.toLocalBufferType(buffer).swap32();
167
+ }
163
168
  };
@@ -13,6 +13,8 @@ const isBigEndian = FLOAT_BYTES[7] === 0;
13
13
  * A collection of functions that get or set various numeric types and bit widths from a Uint8Array.
14
14
  */
15
15
  export type NumberUtils = {
16
+ /** Is true if the current system is big endian. */
17
+ isBigEndian: boolean;
16
18
  /**
17
19
  * Parses a signed int32 at offset. Throws a `RangeError` if value is negative.
18
20
  */
@@ -35,6 +37,8 @@ export type NumberUtils = {
35
37
  * @public
36
38
  */
37
39
  export const NumberUtils: NumberUtils = {
40
+ isBigEndian,
41
+
38
42
  getNonnegativeInt32LE(source: Uint8Array, offset: number): number {
39
43
  if (source[offset + 3] > 127) {
40
44
  throw new RangeError(`Size cannot be negative at offset: ${offset}`);
@@ -100,7 +100,7 @@ export const webByteUtils = {
100
100
  return new Uint8Array(potentialUint8array);
101
101
  }
102
102
 
103
- throw new BSONError(`Cannot make a Uint8Array from ${String(potentialUint8array)}`);
103
+ throw new BSONError(`Cannot make a Uint8Array from passed potentialBuffer.`);
104
104
  },
105
105
 
106
106
  allocate(size: number): Uint8Array {
@@ -193,5 +193,24 @@ export const webByteUtils = {
193
193
  return bytes.byteLength;
194
194
  },
195
195
 
196
- randomBytes: webRandomBytes
196
+ randomBytes: webRandomBytes,
197
+
198
+ swap32(buffer: Uint8Array): Uint8Array {
199
+ if (buffer.length % 4 !== 0) {
200
+ throw new RangeError('Buffer size must be a multiple of 32-bits');
201
+ }
202
+
203
+ for (let i = 0; i < buffer.length; i += 4) {
204
+ const byte0 = buffer[i];
205
+ const byte1 = buffer[i + 1];
206
+ const byte2 = buffer[i + 2];
207
+ const byte3 = buffer[i + 3];
208
+ buffer[i] = byte3;
209
+ buffer[i + 1] = byte2;
210
+ buffer[i + 2] = byte1;
211
+ buffer[i + 3] = byte0;
212
+ }
213
+
214
+ return buffer;
215
+ }
197
216
  };