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/lib/bson.cjs CHANGED
@@ -256,7 +256,7 @@ const nodeJsByteUtils = {
256
256
  stringTag === '[object SharedArrayBuffer]') {
257
257
  return Buffer.from(potentialBuffer);
258
258
  }
259
- throw new BSONError(`Cannot create Buffer from ${String(potentialBuffer)}`);
259
+ throw new BSONError(`Cannot create Buffer from the passed potentialBuffer.`);
260
260
  },
261
261
  allocate(size) {
262
262
  return Buffer.alloc(size);
@@ -314,7 +314,10 @@ const nodeJsByteUtils = {
314
314
  }
315
315
  return nodeJsByteUtils.toLocalBufferType(buffer).write(source, byteOffset, undefined, 'utf8');
316
316
  },
317
- randomBytes: nodejsRandomBytes
317
+ randomBytes: nodejsRandomBytes,
318
+ swap32(buffer) {
319
+ return nodeJsByteUtils.toLocalBufferType(buffer).swap32();
320
+ }
318
321
  };
319
322
 
320
323
  function isReactNative() {
@@ -359,7 +362,7 @@ const webByteUtils = {
359
362
  stringTag === '[object SharedArrayBuffer]') {
360
363
  return new Uint8Array(potentialUint8array);
361
364
  }
362
- throw new BSONError(`Cannot make a Uint8Array from ${String(potentialUint8array)}`);
365
+ throw new BSONError(`Cannot make a Uint8Array from passed potentialBuffer.`);
363
366
  },
364
367
  allocate(size) {
365
368
  if (typeof size !== 'number') {
@@ -431,7 +434,23 @@ const webByteUtils = {
431
434
  uint8array.set(bytes, byteOffset);
432
435
  return bytes.byteLength;
433
436
  },
434
- randomBytes: webRandomBytes
437
+ randomBytes: webRandomBytes,
438
+ swap32(buffer) {
439
+ if (buffer.length % 4 !== 0) {
440
+ throw new RangeError('Buffer size must be a multiple of 32-bits');
441
+ }
442
+ for (let i = 0; i < buffer.length; i += 4) {
443
+ const byte0 = buffer[i];
444
+ const byte1 = buffer[i + 1];
445
+ const byte2 = buffer[i + 2];
446
+ const byte3 = buffer[i + 3];
447
+ buffer[i] = byte3;
448
+ buffer[i + 1] = byte2;
449
+ buffer[i + 2] = byte1;
450
+ buffer[i + 3] = byte0;
451
+ }
452
+ return buffer;
453
+ }
435
454
  };
436
455
 
437
456
  const hasGlobalBuffer = typeof Buffer === 'function' && Buffer.prototype?._isBuffer !== true;
@@ -446,6 +465,134 @@ class BSONValue {
446
465
  }
447
466
  }
448
467
 
468
+ const FLOAT = new Float64Array(1);
469
+ const FLOAT_BYTES = new Uint8Array(FLOAT.buffer, 0, 8);
470
+ FLOAT[0] = -1;
471
+ const isBigEndian = FLOAT_BYTES[7] === 0;
472
+ const NumberUtils = {
473
+ isBigEndian,
474
+ getNonnegativeInt32LE(source, offset) {
475
+ if (source[offset + 3] > 127) {
476
+ throw new RangeError(`Size cannot be negative at offset: ${offset}`);
477
+ }
478
+ return (source[offset] |
479
+ (source[offset + 1] << 8) |
480
+ (source[offset + 2] << 16) |
481
+ (source[offset + 3] << 24));
482
+ },
483
+ getInt32LE(source, offset) {
484
+ return (source[offset] |
485
+ (source[offset + 1] << 8) |
486
+ (source[offset + 2] << 16) |
487
+ (source[offset + 3] << 24));
488
+ },
489
+ getUint32LE(source, offset) {
490
+ return (source[offset] +
491
+ source[offset + 1] * 256 +
492
+ source[offset + 2] * 65536 +
493
+ source[offset + 3] * 16777216);
494
+ },
495
+ getUint32BE(source, offset) {
496
+ return (source[offset + 3] +
497
+ source[offset + 2] * 256 +
498
+ source[offset + 1] * 65536 +
499
+ source[offset] * 16777216);
500
+ },
501
+ getBigInt64LE(source, offset) {
502
+ const lo = NumberUtils.getUint32LE(source, offset);
503
+ const hi = NumberUtils.getUint32LE(source, offset + 4);
504
+ return (BigInt(hi) << BigInt(32)) + BigInt(lo);
505
+ },
506
+ getFloat64LE: isBigEndian
507
+ ? (source, offset) => {
508
+ FLOAT_BYTES[7] = source[offset];
509
+ FLOAT_BYTES[6] = source[offset + 1];
510
+ FLOAT_BYTES[5] = source[offset + 2];
511
+ FLOAT_BYTES[4] = source[offset + 3];
512
+ FLOAT_BYTES[3] = source[offset + 4];
513
+ FLOAT_BYTES[2] = source[offset + 5];
514
+ FLOAT_BYTES[1] = source[offset + 6];
515
+ FLOAT_BYTES[0] = source[offset + 7];
516
+ return FLOAT[0];
517
+ }
518
+ : (source, offset) => {
519
+ FLOAT_BYTES[0] = source[offset];
520
+ FLOAT_BYTES[1] = source[offset + 1];
521
+ FLOAT_BYTES[2] = source[offset + 2];
522
+ FLOAT_BYTES[3] = source[offset + 3];
523
+ FLOAT_BYTES[4] = source[offset + 4];
524
+ FLOAT_BYTES[5] = source[offset + 5];
525
+ FLOAT_BYTES[6] = source[offset + 6];
526
+ FLOAT_BYTES[7] = source[offset + 7];
527
+ return FLOAT[0];
528
+ },
529
+ setInt32BE(destination, offset, value) {
530
+ destination[offset + 3] = value;
531
+ value >>>= 8;
532
+ destination[offset + 2] = value;
533
+ value >>>= 8;
534
+ destination[offset + 1] = value;
535
+ value >>>= 8;
536
+ destination[offset] = value;
537
+ return 4;
538
+ },
539
+ setInt32LE(destination, offset, value) {
540
+ destination[offset] = value;
541
+ value >>>= 8;
542
+ destination[offset + 1] = value;
543
+ value >>>= 8;
544
+ destination[offset + 2] = value;
545
+ value >>>= 8;
546
+ destination[offset + 3] = value;
547
+ return 4;
548
+ },
549
+ setBigInt64LE(destination, offset, value) {
550
+ const mask32bits = BigInt(0xffff_ffff);
551
+ let lo = Number(value & mask32bits);
552
+ destination[offset] = lo;
553
+ lo >>= 8;
554
+ destination[offset + 1] = lo;
555
+ lo >>= 8;
556
+ destination[offset + 2] = lo;
557
+ lo >>= 8;
558
+ destination[offset + 3] = lo;
559
+ let hi = Number((value >> BigInt(32)) & mask32bits);
560
+ destination[offset + 4] = hi;
561
+ hi >>= 8;
562
+ destination[offset + 5] = hi;
563
+ hi >>= 8;
564
+ destination[offset + 6] = hi;
565
+ hi >>= 8;
566
+ destination[offset + 7] = hi;
567
+ return 8;
568
+ },
569
+ setFloat64LE: isBigEndian
570
+ ? (destination, offset, value) => {
571
+ FLOAT[0] = value;
572
+ destination[offset] = FLOAT_BYTES[7];
573
+ destination[offset + 1] = FLOAT_BYTES[6];
574
+ destination[offset + 2] = FLOAT_BYTES[5];
575
+ destination[offset + 3] = FLOAT_BYTES[4];
576
+ destination[offset + 4] = FLOAT_BYTES[3];
577
+ destination[offset + 5] = FLOAT_BYTES[2];
578
+ destination[offset + 6] = FLOAT_BYTES[1];
579
+ destination[offset + 7] = FLOAT_BYTES[0];
580
+ return 8;
581
+ }
582
+ : (destination, offset, value) => {
583
+ FLOAT[0] = value;
584
+ destination[offset] = FLOAT_BYTES[0];
585
+ destination[offset + 1] = FLOAT_BYTES[1];
586
+ destination[offset + 2] = FLOAT_BYTES[2];
587
+ destination[offset + 3] = FLOAT_BYTES[3];
588
+ destination[offset + 4] = FLOAT_BYTES[4];
589
+ destination[offset + 5] = FLOAT_BYTES[5];
590
+ destination[offset + 6] = FLOAT_BYTES[6];
591
+ destination[offset + 7] = FLOAT_BYTES[7];
592
+ return 8;
593
+ }
594
+ };
595
+
449
596
  class Binary extends BSONValue {
450
597
  get _bsontype() {
451
598
  return 'Binary';
@@ -518,7 +665,8 @@ class Binary extends BSONValue {
518
665
  }
519
666
  read(position, length) {
520
667
  length = length && length > 0 ? length : this.position;
521
- return this.buffer.slice(position, position + length);
668
+ const end = position + length;
669
+ return this.buffer.subarray(position, end > this.position ? this.position : end);
522
670
  }
523
671
  value() {
524
672
  return this.buffer.length === this.position
@@ -542,6 +690,9 @@ class Binary extends BSONValue {
542
690
  }
543
691
  toExtendedJSON(options) {
544
692
  options = options || {};
693
+ if (this.sub_type === Binary.SUBTYPE_VECTOR) {
694
+ validateBinaryVector(this);
695
+ }
545
696
  const base64String = ByteUtils.toBase64(this.buffer);
546
697
  const subType = Number(this.sub_type).toString(16);
547
698
  if (options.legacy) {
@@ -559,7 +710,7 @@ class Binary extends BSONValue {
559
710
  }
560
711
  toUUID() {
561
712
  if (this.sub_type === Binary.SUBTYPE_UUID) {
562
- return new UUID(this.buffer.slice(0, this.position));
713
+ return new UUID(this.buffer.subarray(0, this.position));
563
714
  }
564
715
  throw new BSONError(`Binary sub_type "${this.sub_type}" is not supported for converting to UUID. Only "${Binary.SUBTYPE_UUID}" is currently supported.`);
565
716
  }
@@ -601,6 +752,99 @@ class Binary extends BSONValue {
601
752
  const subTypeArg = inspect(this.sub_type, options);
602
753
  return `Binary.createFromBase64(${base64Arg}, ${subTypeArg})`;
603
754
  }
755
+ toInt8Array() {
756
+ if (this.sub_type !== Binary.SUBTYPE_VECTOR) {
757
+ throw new BSONError('Binary sub_type is not Vector');
758
+ }
759
+ if (this.buffer[0] !== Binary.VECTOR_TYPE.Int8) {
760
+ throw new BSONError('Binary datatype field is not Int8');
761
+ }
762
+ return new Int8Array(this.buffer.buffer.slice(this.buffer.byteOffset + 2, this.buffer.byteOffset + this.position));
763
+ }
764
+ toFloat32Array() {
765
+ if (this.sub_type !== Binary.SUBTYPE_VECTOR) {
766
+ throw new BSONError('Binary sub_type is not Vector');
767
+ }
768
+ if (this.buffer[0] !== Binary.VECTOR_TYPE.Float32) {
769
+ throw new BSONError('Binary datatype field is not Float32');
770
+ }
771
+ const floatBytes = new Uint8Array(this.buffer.buffer.slice(this.buffer.byteOffset + 2, this.buffer.byteOffset + this.position));
772
+ if (NumberUtils.isBigEndian)
773
+ ByteUtils.swap32(floatBytes);
774
+ return new Float32Array(floatBytes.buffer);
775
+ }
776
+ toPackedBits() {
777
+ if (this.sub_type !== Binary.SUBTYPE_VECTOR) {
778
+ throw new BSONError('Binary sub_type is not Vector');
779
+ }
780
+ if (this.buffer[0] !== Binary.VECTOR_TYPE.PackedBit) {
781
+ throw new BSONError('Binary datatype field is not packed bit');
782
+ }
783
+ return new Uint8Array(this.buffer.buffer.slice(this.buffer.byteOffset + 2, this.buffer.byteOffset + this.position));
784
+ }
785
+ toBits() {
786
+ if (this.sub_type !== Binary.SUBTYPE_VECTOR) {
787
+ throw new BSONError('Binary sub_type is not Vector');
788
+ }
789
+ if (this.buffer[0] !== Binary.VECTOR_TYPE.PackedBit) {
790
+ throw new BSONError('Binary datatype field is not packed bit');
791
+ }
792
+ const byteCount = this.length() - 2;
793
+ const bitCount = byteCount * 8 - this.buffer[1];
794
+ const bits = new Int8Array(bitCount);
795
+ for (let bitOffset = 0; bitOffset < bits.length; bitOffset++) {
796
+ const byteOffset = (bitOffset / 8) | 0;
797
+ const byte = this.buffer[byteOffset + 2];
798
+ const shift = 7 - (bitOffset % 8);
799
+ const bit = (byte >> shift) & 1;
800
+ bits[bitOffset] = bit;
801
+ }
802
+ return bits;
803
+ }
804
+ static fromInt8Array(array) {
805
+ const buffer = ByteUtils.allocate(array.byteLength + 2);
806
+ buffer[0] = Binary.VECTOR_TYPE.Int8;
807
+ buffer[1] = 0;
808
+ const intBytes = new Uint8Array(array.buffer, array.byteOffset, array.byteLength);
809
+ buffer.set(intBytes, 2);
810
+ return new this(buffer, this.SUBTYPE_VECTOR);
811
+ }
812
+ static fromFloat32Array(array) {
813
+ const binaryBytes = ByteUtils.allocate(array.byteLength + 2);
814
+ binaryBytes[0] = Binary.VECTOR_TYPE.Float32;
815
+ binaryBytes[1] = 0;
816
+ const floatBytes = new Uint8Array(array.buffer, array.byteOffset, array.byteLength);
817
+ binaryBytes.set(floatBytes, 2);
818
+ if (NumberUtils.isBigEndian)
819
+ ByteUtils.swap32(new Uint8Array(binaryBytes.buffer, 2));
820
+ return new this(binaryBytes, this.SUBTYPE_VECTOR);
821
+ }
822
+ static fromPackedBits(array, padding = 0) {
823
+ const buffer = ByteUtils.allocate(array.byteLength + 2);
824
+ buffer[0] = Binary.VECTOR_TYPE.PackedBit;
825
+ buffer[1] = padding;
826
+ buffer.set(array, 2);
827
+ return new this(buffer, this.SUBTYPE_VECTOR);
828
+ }
829
+ static fromBits(bits) {
830
+ const byteLength = (bits.length + 7) >>> 3;
831
+ const bytes = new Uint8Array(byteLength + 2);
832
+ bytes[0] = Binary.VECTOR_TYPE.PackedBit;
833
+ const remainder = bits.length % 8;
834
+ bytes[1] = remainder === 0 ? 0 : 8 - remainder;
835
+ for (let bitOffset = 0; bitOffset < bits.length; bitOffset++) {
836
+ const byteOffset = bitOffset >>> 3;
837
+ const bit = bits[bitOffset];
838
+ if (bit !== 0 && bit !== 1) {
839
+ throw new BSONError(`Invalid bit value at ${bitOffset}: must be 0 or 1, found ${bits[bitOffset]}`);
840
+ }
841
+ if (bit === 0)
842
+ continue;
843
+ const shift = 7 - (bitOffset % 8);
844
+ bytes[byteOffset + 2] |= bit << shift;
845
+ }
846
+ return new this(bytes, Binary.SUBTYPE_VECTOR);
847
+ }
604
848
  }
605
849
  Binary.BSON_BINARY_SUBTYPE_DEFAULT = 0;
606
850
  Binary.BUFFER_SIZE = 256;
@@ -613,7 +857,30 @@ Binary.SUBTYPE_MD5 = 5;
613
857
  Binary.SUBTYPE_ENCRYPTED = 6;
614
858
  Binary.SUBTYPE_COLUMN = 7;
615
859
  Binary.SUBTYPE_SENSITIVE = 8;
860
+ Binary.SUBTYPE_VECTOR = 9;
616
861
  Binary.SUBTYPE_USER_DEFINED = 128;
862
+ Binary.VECTOR_TYPE = Object.freeze({
863
+ Int8: 0x03,
864
+ Float32: 0x27,
865
+ PackedBit: 0x10
866
+ });
867
+ function validateBinaryVector(vector) {
868
+ if (vector.sub_type !== Binary.SUBTYPE_VECTOR)
869
+ return;
870
+ const size = vector.position;
871
+ const datatype = vector.buffer[0];
872
+ const padding = vector.buffer[1];
873
+ if ((datatype === Binary.VECTOR_TYPE.Float32 || datatype === Binary.VECTOR_TYPE.Int8) &&
874
+ padding !== 0) {
875
+ throw new BSONError('Invalid Vector: padding must be zero for int8 and float32 vectors');
876
+ }
877
+ if (datatype === Binary.VECTOR_TYPE.PackedBit && padding !== 0 && size === 2) {
878
+ throw new BSONError('Invalid Vector: padding must be zero for packed bit vectors that are empty');
879
+ }
880
+ if (datatype === Binary.VECTOR_TYPE.PackedBit && padding > 7) {
881
+ throw new BSONError(`Invalid Vector: padding must be a value between 0 and 7. found: ${padding}`);
882
+ }
883
+ }
617
884
  const UUID_BYTE_LENGTH = 16;
618
885
  const UUID_WITHOUT_DASHES = /^[0-9A-F]{32}$/i;
619
886
  const UUID_WITH_DASHES = /^[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}$/i;
@@ -2225,133 +2492,6 @@ class MinKey extends BSONValue {
2225
2492
  }
2226
2493
  }
2227
2494
 
2228
- const FLOAT = new Float64Array(1);
2229
- const FLOAT_BYTES = new Uint8Array(FLOAT.buffer, 0, 8);
2230
- FLOAT[0] = -1;
2231
- const isBigEndian = FLOAT_BYTES[7] === 0;
2232
- const NumberUtils = {
2233
- getNonnegativeInt32LE(source, offset) {
2234
- if (source[offset + 3] > 127) {
2235
- throw new RangeError(`Size cannot be negative at offset: ${offset}`);
2236
- }
2237
- return (source[offset] |
2238
- (source[offset + 1] << 8) |
2239
- (source[offset + 2] << 16) |
2240
- (source[offset + 3] << 24));
2241
- },
2242
- getInt32LE(source, offset) {
2243
- return (source[offset] |
2244
- (source[offset + 1] << 8) |
2245
- (source[offset + 2] << 16) |
2246
- (source[offset + 3] << 24));
2247
- },
2248
- getUint32LE(source, offset) {
2249
- return (source[offset] +
2250
- source[offset + 1] * 256 +
2251
- source[offset + 2] * 65536 +
2252
- source[offset + 3] * 16777216);
2253
- },
2254
- getUint32BE(source, offset) {
2255
- return (source[offset + 3] +
2256
- source[offset + 2] * 256 +
2257
- source[offset + 1] * 65536 +
2258
- source[offset] * 16777216);
2259
- },
2260
- getBigInt64LE(source, offset) {
2261
- const lo = NumberUtils.getUint32LE(source, offset);
2262
- const hi = NumberUtils.getUint32LE(source, offset + 4);
2263
- return (BigInt(hi) << BigInt(32)) + BigInt(lo);
2264
- },
2265
- getFloat64LE: isBigEndian
2266
- ? (source, offset) => {
2267
- FLOAT_BYTES[7] = source[offset];
2268
- FLOAT_BYTES[6] = source[offset + 1];
2269
- FLOAT_BYTES[5] = source[offset + 2];
2270
- FLOAT_BYTES[4] = source[offset + 3];
2271
- FLOAT_BYTES[3] = source[offset + 4];
2272
- FLOAT_BYTES[2] = source[offset + 5];
2273
- FLOAT_BYTES[1] = source[offset + 6];
2274
- FLOAT_BYTES[0] = source[offset + 7];
2275
- return FLOAT[0];
2276
- }
2277
- : (source, offset) => {
2278
- FLOAT_BYTES[0] = source[offset];
2279
- FLOAT_BYTES[1] = source[offset + 1];
2280
- FLOAT_BYTES[2] = source[offset + 2];
2281
- FLOAT_BYTES[3] = source[offset + 3];
2282
- FLOAT_BYTES[4] = source[offset + 4];
2283
- FLOAT_BYTES[5] = source[offset + 5];
2284
- FLOAT_BYTES[6] = source[offset + 6];
2285
- FLOAT_BYTES[7] = source[offset + 7];
2286
- return FLOAT[0];
2287
- },
2288
- setInt32BE(destination, offset, value) {
2289
- destination[offset + 3] = value;
2290
- value >>>= 8;
2291
- destination[offset + 2] = value;
2292
- value >>>= 8;
2293
- destination[offset + 1] = value;
2294
- value >>>= 8;
2295
- destination[offset] = value;
2296
- return 4;
2297
- },
2298
- setInt32LE(destination, offset, value) {
2299
- destination[offset] = value;
2300
- value >>>= 8;
2301
- destination[offset + 1] = value;
2302
- value >>>= 8;
2303
- destination[offset + 2] = value;
2304
- value >>>= 8;
2305
- destination[offset + 3] = value;
2306
- return 4;
2307
- },
2308
- setBigInt64LE(destination, offset, value) {
2309
- const mask32bits = BigInt(0xffff_ffff);
2310
- let lo = Number(value & mask32bits);
2311
- destination[offset] = lo;
2312
- lo >>= 8;
2313
- destination[offset + 1] = lo;
2314
- lo >>= 8;
2315
- destination[offset + 2] = lo;
2316
- lo >>= 8;
2317
- destination[offset + 3] = lo;
2318
- let hi = Number((value >> BigInt(32)) & mask32bits);
2319
- destination[offset + 4] = hi;
2320
- hi >>= 8;
2321
- destination[offset + 5] = hi;
2322
- hi >>= 8;
2323
- destination[offset + 6] = hi;
2324
- hi >>= 8;
2325
- destination[offset + 7] = hi;
2326
- return 8;
2327
- },
2328
- setFloat64LE: isBigEndian
2329
- ? (destination, offset, value) => {
2330
- FLOAT[0] = value;
2331
- destination[offset] = FLOAT_BYTES[7];
2332
- destination[offset + 1] = FLOAT_BYTES[6];
2333
- destination[offset + 2] = FLOAT_BYTES[5];
2334
- destination[offset + 3] = FLOAT_BYTES[4];
2335
- destination[offset + 4] = FLOAT_BYTES[3];
2336
- destination[offset + 5] = FLOAT_BYTES[2];
2337
- destination[offset + 6] = FLOAT_BYTES[1];
2338
- destination[offset + 7] = FLOAT_BYTES[0];
2339
- return 8;
2340
- }
2341
- : (destination, offset, value) => {
2342
- FLOAT[0] = value;
2343
- destination[offset] = FLOAT_BYTES[0];
2344
- destination[offset + 1] = FLOAT_BYTES[1];
2345
- destination[offset + 2] = FLOAT_BYTES[2];
2346
- destination[offset + 3] = FLOAT_BYTES[3];
2347
- destination[offset + 4] = FLOAT_BYTES[4];
2348
- destination[offset + 5] = FLOAT_BYTES[5];
2349
- destination[offset + 6] = FLOAT_BYTES[6];
2350
- destination[offset + 7] = FLOAT_BYTES[7];
2351
- return 8;
2352
- }
2353
- };
2354
-
2355
2495
  let PROCESS_UNIQUE = null;
2356
2496
  class ObjectId extends BSONValue {
2357
2497
  get _bsontype() {
@@ -3038,7 +3178,7 @@ function deserializeObject(buffer, index, options, isArray = false) {
3038
3178
  if (objectSize <= 0 || objectSize > buffer.length - index)
3039
3179
  throw new BSONError('bad embedded document length in bson');
3040
3180
  if (raw) {
3041
- value = buffer.slice(index, index + objectSize);
3181
+ value = buffer.subarray(index, index + objectSize);
3042
3182
  }
3043
3183
  else {
3044
3184
  let objectOptions = options;
@@ -3110,49 +3250,23 @@ function deserializeObject(buffer, index, options, isArray = false) {
3110
3250
  throw new BSONError('Negative binary type element size found');
3111
3251
  if (binarySize > buffer.byteLength)
3112
3252
  throw new BSONError('Binary type size larger than document size');
3113
- if (buffer['slice'] != null) {
3114
- if (subType === Binary.SUBTYPE_BYTE_ARRAY) {
3115
- binarySize = NumberUtils.getInt32LE(buffer, index);
3116
- index += 4;
3117
- if (binarySize < 0)
3118
- throw new BSONError('Negative binary type element size found for subtype 0x02');
3119
- if (binarySize > totalBinarySize - 4)
3120
- throw new BSONError('Binary type with subtype 0x02 contains too long binary size');
3121
- if (binarySize < totalBinarySize - 4)
3122
- throw new BSONError('Binary type with subtype 0x02 contains too short binary size');
3123
- }
3124
- if (promoteBuffers && promoteValues) {
3125
- value = ByteUtils.toLocalBufferType(buffer.slice(index, index + binarySize));
3126
- }
3127
- else {
3128
- value = new Binary(buffer.slice(index, index + binarySize), subType);
3129
- if (subType === BSON_BINARY_SUBTYPE_UUID_NEW && UUID.isValid(value)) {
3130
- value = value.toUUID();
3131
- }
3132
- }
3253
+ if (subType === Binary.SUBTYPE_BYTE_ARRAY) {
3254
+ binarySize = NumberUtils.getInt32LE(buffer, index);
3255
+ index += 4;
3256
+ if (binarySize < 0)
3257
+ throw new BSONError('Negative binary type element size found for subtype 0x02');
3258
+ if (binarySize > totalBinarySize - 4)
3259
+ throw new BSONError('Binary type with subtype 0x02 contains too long binary size');
3260
+ if (binarySize < totalBinarySize - 4)
3261
+ throw new BSONError('Binary type with subtype 0x02 contains too short binary size');
3262
+ }
3263
+ if (promoteBuffers && promoteValues) {
3264
+ value = ByteUtils.toLocalBufferType(buffer.subarray(index, index + binarySize));
3133
3265
  }
3134
3266
  else {
3135
- if (subType === Binary.SUBTYPE_BYTE_ARRAY) {
3136
- binarySize = NumberUtils.getInt32LE(buffer, index);
3137
- index += 4;
3138
- if (binarySize < 0)
3139
- throw new BSONError('Negative binary type element size found for subtype 0x02');
3140
- if (binarySize > totalBinarySize - 4)
3141
- throw new BSONError('Binary type with subtype 0x02 contains too long binary size');
3142
- if (binarySize < totalBinarySize - 4)
3143
- throw new BSONError('Binary type with subtype 0x02 contains too short binary size');
3144
- }
3145
- if (promoteBuffers && promoteValues) {
3146
- value = ByteUtils.allocateUnsafe(binarySize);
3147
- for (i = 0; i < binarySize; i++) {
3148
- value[i] = buffer[index + i];
3149
- }
3150
- }
3151
- else {
3152
- value = new Binary(buffer.slice(index, index + binarySize), subType);
3153
- if (subType === BSON_BINARY_SUBTYPE_UUID_NEW && UUID.isValid(value)) {
3154
- value = value.toUUID();
3155
- }
3267
+ value = new Binary(buffer.subarray(index, index + binarySize), subType);
3268
+ if (subType === BSON_BINARY_SUBTYPE_UUID_NEW && UUID.isValid(value)) {
3269
+ value = value.toUUID();
3156
3270
  }
3157
3271
  }
3158
3272
  index = index + binarySize;
@@ -3574,6 +3688,9 @@ function serializeBinary(buffer, key, value, index) {
3574
3688
  size = size - 4;
3575
3689
  index += NumberUtils.setInt32LE(buffer, index, size);
3576
3690
  }
3691
+ if (value.sub_type === Binary.SUBTYPE_VECTOR) {
3692
+ validateBinaryVector(value);
3693
+ }
3577
3694
  if (size <= 16) {
3578
3695
  for (let i = 0; i < size; i++)
3579
3696
  buffer[index + i] = data[i];