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