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.rn.cjs CHANGED
@@ -273,7 +273,7 @@ const nodeJsByteUtils = {
273
273
  stringTag === '[object SharedArrayBuffer]') {
274
274
  return Buffer.from(potentialBuffer);
275
275
  }
276
- throw new BSONError(`Cannot create Buffer from ${String(potentialBuffer)}`);
276
+ throw new BSONError(`Cannot create Buffer from the passed potentialBuffer.`);
277
277
  },
278
278
  allocate(size) {
279
279
  return Buffer.alloc(size);
@@ -331,7 +331,10 @@ const nodeJsByteUtils = {
331
331
  }
332
332
  return nodeJsByteUtils.toLocalBufferType(buffer).write(source, byteOffset, undefined, 'utf8');
333
333
  },
334
- randomBytes: nodejsRandomBytes
334
+ randomBytes: nodejsRandomBytes,
335
+ swap32(buffer) {
336
+ return nodeJsByteUtils.toLocalBufferType(buffer).swap32();
337
+ }
335
338
  };
336
339
 
337
340
  const { TextEncoder } = require('../vendor/text-encoding');
@@ -378,7 +381,7 @@ const webByteUtils = {
378
381
  stringTag === '[object SharedArrayBuffer]') {
379
382
  return new Uint8Array(potentialUint8array);
380
383
  }
381
- throw new BSONError(`Cannot make a Uint8Array from ${String(potentialUint8array)}`);
384
+ throw new BSONError(`Cannot make a Uint8Array from passed potentialBuffer.`);
382
385
  },
383
386
  allocate(size) {
384
387
  if (typeof size !== 'number') {
@@ -450,7 +453,23 @@ const webByteUtils = {
450
453
  uint8array.set(bytes, byteOffset);
451
454
  return bytes.byteLength;
452
455
  },
453
- randomBytes: webRandomBytes
456
+ randomBytes: webRandomBytes,
457
+ swap32(buffer) {
458
+ if (buffer.length % 4 !== 0) {
459
+ throw new RangeError('Buffer size must be a multiple of 32-bits');
460
+ }
461
+ for (let i = 0; i < buffer.length; i += 4) {
462
+ const byte0 = buffer[i];
463
+ const byte1 = buffer[i + 1];
464
+ const byte2 = buffer[i + 2];
465
+ const byte3 = buffer[i + 3];
466
+ buffer[i] = byte3;
467
+ buffer[i + 1] = byte2;
468
+ buffer[i + 2] = byte1;
469
+ buffer[i + 3] = byte0;
470
+ }
471
+ return buffer;
472
+ }
454
473
  };
455
474
 
456
475
  const hasGlobalBuffer = typeof Buffer === 'function' && Buffer.prototype?._isBuffer !== true;
@@ -465,6 +484,134 @@ class BSONValue {
465
484
  }
466
485
  }
467
486
 
487
+ const FLOAT = new Float64Array(1);
488
+ const FLOAT_BYTES = new Uint8Array(FLOAT.buffer, 0, 8);
489
+ FLOAT[0] = -1;
490
+ const isBigEndian = FLOAT_BYTES[7] === 0;
491
+ const NumberUtils = {
492
+ isBigEndian,
493
+ getNonnegativeInt32LE(source, offset) {
494
+ if (source[offset + 3] > 127) {
495
+ throw new RangeError(`Size cannot be negative at offset: ${offset}`);
496
+ }
497
+ return (source[offset] |
498
+ (source[offset + 1] << 8) |
499
+ (source[offset + 2] << 16) |
500
+ (source[offset + 3] << 24));
501
+ },
502
+ getInt32LE(source, offset) {
503
+ return (source[offset] |
504
+ (source[offset + 1] << 8) |
505
+ (source[offset + 2] << 16) |
506
+ (source[offset + 3] << 24));
507
+ },
508
+ getUint32LE(source, offset) {
509
+ return (source[offset] +
510
+ source[offset + 1] * 256 +
511
+ source[offset + 2] * 65536 +
512
+ source[offset + 3] * 16777216);
513
+ },
514
+ getUint32BE(source, offset) {
515
+ return (source[offset + 3] +
516
+ source[offset + 2] * 256 +
517
+ source[offset + 1] * 65536 +
518
+ source[offset] * 16777216);
519
+ },
520
+ getBigInt64LE(source, offset) {
521
+ const lo = NumberUtils.getUint32LE(source, offset);
522
+ const hi = NumberUtils.getUint32LE(source, offset + 4);
523
+ return (BigInt(hi) << BigInt(32)) + BigInt(lo);
524
+ },
525
+ getFloat64LE: isBigEndian
526
+ ? (source, offset) => {
527
+ FLOAT_BYTES[7] = source[offset];
528
+ FLOAT_BYTES[6] = source[offset + 1];
529
+ FLOAT_BYTES[5] = source[offset + 2];
530
+ FLOAT_BYTES[4] = source[offset + 3];
531
+ FLOAT_BYTES[3] = source[offset + 4];
532
+ FLOAT_BYTES[2] = source[offset + 5];
533
+ FLOAT_BYTES[1] = source[offset + 6];
534
+ FLOAT_BYTES[0] = source[offset + 7];
535
+ return FLOAT[0];
536
+ }
537
+ : (source, offset) => {
538
+ FLOAT_BYTES[0] = source[offset];
539
+ FLOAT_BYTES[1] = source[offset + 1];
540
+ FLOAT_BYTES[2] = source[offset + 2];
541
+ FLOAT_BYTES[3] = source[offset + 3];
542
+ FLOAT_BYTES[4] = source[offset + 4];
543
+ FLOAT_BYTES[5] = source[offset + 5];
544
+ FLOAT_BYTES[6] = source[offset + 6];
545
+ FLOAT_BYTES[7] = source[offset + 7];
546
+ return FLOAT[0];
547
+ },
548
+ setInt32BE(destination, offset, value) {
549
+ destination[offset + 3] = value;
550
+ value >>>= 8;
551
+ destination[offset + 2] = value;
552
+ value >>>= 8;
553
+ destination[offset + 1] = value;
554
+ value >>>= 8;
555
+ destination[offset] = value;
556
+ return 4;
557
+ },
558
+ setInt32LE(destination, offset, value) {
559
+ destination[offset] = value;
560
+ value >>>= 8;
561
+ destination[offset + 1] = value;
562
+ value >>>= 8;
563
+ destination[offset + 2] = value;
564
+ value >>>= 8;
565
+ destination[offset + 3] = value;
566
+ return 4;
567
+ },
568
+ setBigInt64LE(destination, offset, value) {
569
+ const mask32bits = BigInt(0xffff_ffff);
570
+ let lo = Number(value & mask32bits);
571
+ destination[offset] = lo;
572
+ lo >>= 8;
573
+ destination[offset + 1] = lo;
574
+ lo >>= 8;
575
+ destination[offset + 2] = lo;
576
+ lo >>= 8;
577
+ destination[offset + 3] = lo;
578
+ let hi = Number((value >> BigInt(32)) & mask32bits);
579
+ destination[offset + 4] = hi;
580
+ hi >>= 8;
581
+ destination[offset + 5] = hi;
582
+ hi >>= 8;
583
+ destination[offset + 6] = hi;
584
+ hi >>= 8;
585
+ destination[offset + 7] = hi;
586
+ return 8;
587
+ },
588
+ setFloat64LE: isBigEndian
589
+ ? (destination, offset, value) => {
590
+ FLOAT[0] = value;
591
+ destination[offset] = FLOAT_BYTES[7];
592
+ destination[offset + 1] = FLOAT_BYTES[6];
593
+ destination[offset + 2] = FLOAT_BYTES[5];
594
+ destination[offset + 3] = FLOAT_BYTES[4];
595
+ destination[offset + 4] = FLOAT_BYTES[3];
596
+ destination[offset + 5] = FLOAT_BYTES[2];
597
+ destination[offset + 6] = FLOAT_BYTES[1];
598
+ destination[offset + 7] = FLOAT_BYTES[0];
599
+ return 8;
600
+ }
601
+ : (destination, offset, value) => {
602
+ FLOAT[0] = value;
603
+ destination[offset] = FLOAT_BYTES[0];
604
+ destination[offset + 1] = FLOAT_BYTES[1];
605
+ destination[offset + 2] = FLOAT_BYTES[2];
606
+ destination[offset + 3] = FLOAT_BYTES[3];
607
+ destination[offset + 4] = FLOAT_BYTES[4];
608
+ destination[offset + 5] = FLOAT_BYTES[5];
609
+ destination[offset + 6] = FLOAT_BYTES[6];
610
+ destination[offset + 7] = FLOAT_BYTES[7];
611
+ return 8;
612
+ }
613
+ };
614
+
468
615
  class Binary extends BSONValue {
469
616
  get _bsontype() {
470
617
  return 'Binary';
@@ -537,7 +684,8 @@ class Binary extends BSONValue {
537
684
  }
538
685
  read(position, length) {
539
686
  length = length && length > 0 ? length : this.position;
540
- return this.buffer.slice(position, position + length);
687
+ const end = position + length;
688
+ return this.buffer.subarray(position, end > this.position ? this.position : end);
541
689
  }
542
690
  value() {
543
691
  return this.buffer.length === this.position
@@ -561,6 +709,9 @@ class Binary extends BSONValue {
561
709
  }
562
710
  toExtendedJSON(options) {
563
711
  options = options || {};
712
+ if (this.sub_type === Binary.SUBTYPE_VECTOR) {
713
+ validateBinaryVector(this);
714
+ }
564
715
  const base64String = ByteUtils.toBase64(this.buffer);
565
716
  const subType = Number(this.sub_type).toString(16);
566
717
  if (options.legacy) {
@@ -578,7 +729,7 @@ class Binary extends BSONValue {
578
729
  }
579
730
  toUUID() {
580
731
  if (this.sub_type === Binary.SUBTYPE_UUID) {
581
- return new UUID(this.buffer.slice(0, this.position));
732
+ return new UUID(this.buffer.subarray(0, this.position));
582
733
  }
583
734
  throw new BSONError(`Binary sub_type "${this.sub_type}" is not supported for converting to UUID. Only "${Binary.SUBTYPE_UUID}" is currently supported.`);
584
735
  }
@@ -620,6 +771,99 @@ class Binary extends BSONValue {
620
771
  const subTypeArg = inspect(this.sub_type, options);
621
772
  return `Binary.createFromBase64(${base64Arg}, ${subTypeArg})`;
622
773
  }
774
+ toInt8Array() {
775
+ if (this.sub_type !== Binary.SUBTYPE_VECTOR) {
776
+ throw new BSONError('Binary sub_type is not Vector');
777
+ }
778
+ if (this.buffer[0] !== Binary.VECTOR_TYPE.Int8) {
779
+ throw new BSONError('Binary datatype field is not Int8');
780
+ }
781
+ return new Int8Array(this.buffer.buffer.slice(this.buffer.byteOffset + 2, this.buffer.byteOffset + this.position));
782
+ }
783
+ toFloat32Array() {
784
+ if (this.sub_type !== Binary.SUBTYPE_VECTOR) {
785
+ throw new BSONError('Binary sub_type is not Vector');
786
+ }
787
+ if (this.buffer[0] !== Binary.VECTOR_TYPE.Float32) {
788
+ throw new BSONError('Binary datatype field is not Float32');
789
+ }
790
+ const floatBytes = new Uint8Array(this.buffer.buffer.slice(this.buffer.byteOffset + 2, this.buffer.byteOffset + this.position));
791
+ if (NumberUtils.isBigEndian)
792
+ ByteUtils.swap32(floatBytes);
793
+ return new Float32Array(floatBytes.buffer);
794
+ }
795
+ toPackedBits() {
796
+ if (this.sub_type !== Binary.SUBTYPE_VECTOR) {
797
+ throw new BSONError('Binary sub_type is not Vector');
798
+ }
799
+ if (this.buffer[0] !== Binary.VECTOR_TYPE.PackedBit) {
800
+ throw new BSONError('Binary datatype field is not packed bit');
801
+ }
802
+ return new Uint8Array(this.buffer.buffer.slice(this.buffer.byteOffset + 2, this.buffer.byteOffset + this.position));
803
+ }
804
+ toBits() {
805
+ if (this.sub_type !== Binary.SUBTYPE_VECTOR) {
806
+ throw new BSONError('Binary sub_type is not Vector');
807
+ }
808
+ if (this.buffer[0] !== Binary.VECTOR_TYPE.PackedBit) {
809
+ throw new BSONError('Binary datatype field is not packed bit');
810
+ }
811
+ const byteCount = this.length() - 2;
812
+ const bitCount = byteCount * 8 - this.buffer[1];
813
+ const bits = new Int8Array(bitCount);
814
+ for (let bitOffset = 0; bitOffset < bits.length; bitOffset++) {
815
+ const byteOffset = (bitOffset / 8) | 0;
816
+ const byte = this.buffer[byteOffset + 2];
817
+ const shift = 7 - (bitOffset % 8);
818
+ const bit = (byte >> shift) & 1;
819
+ bits[bitOffset] = bit;
820
+ }
821
+ return bits;
822
+ }
823
+ static fromInt8Array(array) {
824
+ const buffer = ByteUtils.allocate(array.byteLength + 2);
825
+ buffer[0] = Binary.VECTOR_TYPE.Int8;
826
+ buffer[1] = 0;
827
+ const intBytes = new Uint8Array(array.buffer, array.byteOffset, array.byteLength);
828
+ buffer.set(intBytes, 2);
829
+ return new this(buffer, this.SUBTYPE_VECTOR);
830
+ }
831
+ static fromFloat32Array(array) {
832
+ const binaryBytes = ByteUtils.allocate(array.byteLength + 2);
833
+ binaryBytes[0] = Binary.VECTOR_TYPE.Float32;
834
+ binaryBytes[1] = 0;
835
+ const floatBytes = new Uint8Array(array.buffer, array.byteOffset, array.byteLength);
836
+ binaryBytes.set(floatBytes, 2);
837
+ if (NumberUtils.isBigEndian)
838
+ ByteUtils.swap32(new Uint8Array(binaryBytes.buffer, 2));
839
+ return new this(binaryBytes, this.SUBTYPE_VECTOR);
840
+ }
841
+ static fromPackedBits(array, padding = 0) {
842
+ const buffer = ByteUtils.allocate(array.byteLength + 2);
843
+ buffer[0] = Binary.VECTOR_TYPE.PackedBit;
844
+ buffer[1] = padding;
845
+ buffer.set(array, 2);
846
+ return new this(buffer, this.SUBTYPE_VECTOR);
847
+ }
848
+ static fromBits(bits) {
849
+ const byteLength = (bits.length + 7) >>> 3;
850
+ const bytes = new Uint8Array(byteLength + 2);
851
+ bytes[0] = Binary.VECTOR_TYPE.PackedBit;
852
+ const remainder = bits.length % 8;
853
+ bytes[1] = remainder === 0 ? 0 : 8 - remainder;
854
+ for (let bitOffset = 0; bitOffset < bits.length; bitOffset++) {
855
+ const byteOffset = bitOffset >>> 3;
856
+ const bit = bits[bitOffset];
857
+ if (bit !== 0 && bit !== 1) {
858
+ throw new BSONError(`Invalid bit value at ${bitOffset}: must be 0 or 1, found ${bits[bitOffset]}`);
859
+ }
860
+ if (bit === 0)
861
+ continue;
862
+ const shift = 7 - (bitOffset % 8);
863
+ bytes[byteOffset + 2] |= bit << shift;
864
+ }
865
+ return new this(bytes, Binary.SUBTYPE_VECTOR);
866
+ }
623
867
  }
624
868
  Binary.BSON_BINARY_SUBTYPE_DEFAULT = 0;
625
869
  Binary.BUFFER_SIZE = 256;
@@ -632,7 +876,30 @@ Binary.SUBTYPE_MD5 = 5;
632
876
  Binary.SUBTYPE_ENCRYPTED = 6;
633
877
  Binary.SUBTYPE_COLUMN = 7;
634
878
  Binary.SUBTYPE_SENSITIVE = 8;
879
+ Binary.SUBTYPE_VECTOR = 9;
635
880
  Binary.SUBTYPE_USER_DEFINED = 128;
881
+ Binary.VECTOR_TYPE = Object.freeze({
882
+ Int8: 0x03,
883
+ Float32: 0x27,
884
+ PackedBit: 0x10
885
+ });
886
+ function validateBinaryVector(vector) {
887
+ if (vector.sub_type !== Binary.SUBTYPE_VECTOR)
888
+ return;
889
+ const size = vector.position;
890
+ const datatype = vector.buffer[0];
891
+ const padding = vector.buffer[1];
892
+ if ((datatype === Binary.VECTOR_TYPE.Float32 || datatype === Binary.VECTOR_TYPE.Int8) &&
893
+ padding !== 0) {
894
+ throw new BSONError('Invalid Vector: padding must be zero for int8 and float32 vectors');
895
+ }
896
+ if (datatype === Binary.VECTOR_TYPE.PackedBit && padding !== 0 && size === 2) {
897
+ throw new BSONError('Invalid Vector: padding must be zero for packed bit vectors that are empty');
898
+ }
899
+ if (datatype === Binary.VECTOR_TYPE.PackedBit && padding > 7) {
900
+ throw new BSONError(`Invalid Vector: padding must be a value between 0 and 7. found: ${padding}`);
901
+ }
902
+ }
636
903
  const UUID_BYTE_LENGTH = 16;
637
904
  const UUID_WITHOUT_DASHES = /^[0-9A-F]{32}$/i;
638
905
  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;
@@ -2244,133 +2511,6 @@ class MinKey extends BSONValue {
2244
2511
  }
2245
2512
  }
2246
2513
 
2247
- const FLOAT = new Float64Array(1);
2248
- const FLOAT_BYTES = new Uint8Array(FLOAT.buffer, 0, 8);
2249
- FLOAT[0] = -1;
2250
- const isBigEndian = FLOAT_BYTES[7] === 0;
2251
- const NumberUtils = {
2252
- getNonnegativeInt32LE(source, offset) {
2253
- if (source[offset + 3] > 127) {
2254
- throw new RangeError(`Size cannot be negative at offset: ${offset}`);
2255
- }
2256
- return (source[offset] |
2257
- (source[offset + 1] << 8) |
2258
- (source[offset + 2] << 16) |
2259
- (source[offset + 3] << 24));
2260
- },
2261
- getInt32LE(source, offset) {
2262
- return (source[offset] |
2263
- (source[offset + 1] << 8) |
2264
- (source[offset + 2] << 16) |
2265
- (source[offset + 3] << 24));
2266
- },
2267
- getUint32LE(source, offset) {
2268
- return (source[offset] +
2269
- source[offset + 1] * 256 +
2270
- source[offset + 2] * 65536 +
2271
- source[offset + 3] * 16777216);
2272
- },
2273
- getUint32BE(source, offset) {
2274
- return (source[offset + 3] +
2275
- source[offset + 2] * 256 +
2276
- source[offset + 1] * 65536 +
2277
- source[offset] * 16777216);
2278
- },
2279
- getBigInt64LE(source, offset) {
2280
- const lo = NumberUtils.getUint32LE(source, offset);
2281
- const hi = NumberUtils.getUint32LE(source, offset + 4);
2282
- return (BigInt(hi) << BigInt(32)) + BigInt(lo);
2283
- },
2284
- getFloat64LE: isBigEndian
2285
- ? (source, offset) => {
2286
- FLOAT_BYTES[7] = source[offset];
2287
- FLOAT_BYTES[6] = source[offset + 1];
2288
- FLOAT_BYTES[5] = source[offset + 2];
2289
- FLOAT_BYTES[4] = source[offset + 3];
2290
- FLOAT_BYTES[3] = source[offset + 4];
2291
- FLOAT_BYTES[2] = source[offset + 5];
2292
- FLOAT_BYTES[1] = source[offset + 6];
2293
- FLOAT_BYTES[0] = source[offset + 7];
2294
- return FLOAT[0];
2295
- }
2296
- : (source, offset) => {
2297
- FLOAT_BYTES[0] = source[offset];
2298
- FLOAT_BYTES[1] = source[offset + 1];
2299
- FLOAT_BYTES[2] = source[offset + 2];
2300
- FLOAT_BYTES[3] = source[offset + 3];
2301
- FLOAT_BYTES[4] = source[offset + 4];
2302
- FLOAT_BYTES[5] = source[offset + 5];
2303
- FLOAT_BYTES[6] = source[offset + 6];
2304
- FLOAT_BYTES[7] = source[offset + 7];
2305
- return FLOAT[0];
2306
- },
2307
- setInt32BE(destination, offset, value) {
2308
- destination[offset + 3] = value;
2309
- value >>>= 8;
2310
- destination[offset + 2] = value;
2311
- value >>>= 8;
2312
- destination[offset + 1] = value;
2313
- value >>>= 8;
2314
- destination[offset] = value;
2315
- return 4;
2316
- },
2317
- setInt32LE(destination, offset, value) {
2318
- destination[offset] = value;
2319
- value >>>= 8;
2320
- destination[offset + 1] = value;
2321
- value >>>= 8;
2322
- destination[offset + 2] = value;
2323
- value >>>= 8;
2324
- destination[offset + 3] = value;
2325
- return 4;
2326
- },
2327
- setBigInt64LE(destination, offset, value) {
2328
- const mask32bits = BigInt(0xffff_ffff);
2329
- let lo = Number(value & mask32bits);
2330
- destination[offset] = lo;
2331
- lo >>= 8;
2332
- destination[offset + 1] = lo;
2333
- lo >>= 8;
2334
- destination[offset + 2] = lo;
2335
- lo >>= 8;
2336
- destination[offset + 3] = lo;
2337
- let hi = Number((value >> BigInt(32)) & mask32bits);
2338
- destination[offset + 4] = hi;
2339
- hi >>= 8;
2340
- destination[offset + 5] = hi;
2341
- hi >>= 8;
2342
- destination[offset + 6] = hi;
2343
- hi >>= 8;
2344
- destination[offset + 7] = hi;
2345
- return 8;
2346
- },
2347
- setFloat64LE: isBigEndian
2348
- ? (destination, offset, value) => {
2349
- FLOAT[0] = value;
2350
- destination[offset] = FLOAT_BYTES[7];
2351
- destination[offset + 1] = FLOAT_BYTES[6];
2352
- destination[offset + 2] = FLOAT_BYTES[5];
2353
- destination[offset + 3] = FLOAT_BYTES[4];
2354
- destination[offset + 4] = FLOAT_BYTES[3];
2355
- destination[offset + 5] = FLOAT_BYTES[2];
2356
- destination[offset + 6] = FLOAT_BYTES[1];
2357
- destination[offset + 7] = FLOAT_BYTES[0];
2358
- return 8;
2359
- }
2360
- : (destination, offset, value) => {
2361
- FLOAT[0] = value;
2362
- destination[offset] = FLOAT_BYTES[0];
2363
- destination[offset + 1] = FLOAT_BYTES[1];
2364
- destination[offset + 2] = FLOAT_BYTES[2];
2365
- destination[offset + 3] = FLOAT_BYTES[3];
2366
- destination[offset + 4] = FLOAT_BYTES[4];
2367
- destination[offset + 5] = FLOAT_BYTES[5];
2368
- destination[offset + 6] = FLOAT_BYTES[6];
2369
- destination[offset + 7] = FLOAT_BYTES[7];
2370
- return 8;
2371
- }
2372
- };
2373
-
2374
2514
  let PROCESS_UNIQUE = null;
2375
2515
  class ObjectId extends BSONValue {
2376
2516
  get _bsontype() {
@@ -3057,7 +3197,7 @@ function deserializeObject(buffer, index, options, isArray = false) {
3057
3197
  if (objectSize <= 0 || objectSize > buffer.length - index)
3058
3198
  throw new BSONError('bad embedded document length in bson');
3059
3199
  if (raw) {
3060
- value = buffer.slice(index, index + objectSize);
3200
+ value = buffer.subarray(index, index + objectSize);
3061
3201
  }
3062
3202
  else {
3063
3203
  let objectOptions = options;
@@ -3129,49 +3269,23 @@ function deserializeObject(buffer, index, options, isArray = false) {
3129
3269
  throw new BSONError('Negative binary type element size found');
3130
3270
  if (binarySize > buffer.byteLength)
3131
3271
  throw new BSONError('Binary type size larger than document size');
3132
- if (buffer['slice'] != null) {
3133
- if (subType === Binary.SUBTYPE_BYTE_ARRAY) {
3134
- binarySize = NumberUtils.getInt32LE(buffer, index);
3135
- index += 4;
3136
- if (binarySize < 0)
3137
- throw new BSONError('Negative binary type element size found for subtype 0x02');
3138
- if (binarySize > totalBinarySize - 4)
3139
- throw new BSONError('Binary type with subtype 0x02 contains too long binary size');
3140
- if (binarySize < totalBinarySize - 4)
3141
- throw new BSONError('Binary type with subtype 0x02 contains too short binary size');
3142
- }
3143
- if (promoteBuffers && promoteValues) {
3144
- value = ByteUtils.toLocalBufferType(buffer.slice(index, index + binarySize));
3145
- }
3146
- else {
3147
- value = new Binary(buffer.slice(index, index + binarySize), subType);
3148
- if (subType === BSON_BINARY_SUBTYPE_UUID_NEW && UUID.isValid(value)) {
3149
- value = value.toUUID();
3150
- }
3151
- }
3272
+ if (subType === Binary.SUBTYPE_BYTE_ARRAY) {
3273
+ binarySize = NumberUtils.getInt32LE(buffer, index);
3274
+ index += 4;
3275
+ if (binarySize < 0)
3276
+ throw new BSONError('Negative binary type element size found for subtype 0x02');
3277
+ if (binarySize > totalBinarySize - 4)
3278
+ throw new BSONError('Binary type with subtype 0x02 contains too long binary size');
3279
+ if (binarySize < totalBinarySize - 4)
3280
+ throw new BSONError('Binary type with subtype 0x02 contains too short binary size');
3281
+ }
3282
+ if (promoteBuffers && promoteValues) {
3283
+ value = ByteUtils.toLocalBufferType(buffer.subarray(index, index + binarySize));
3152
3284
  }
3153
3285
  else {
3154
- if (subType === Binary.SUBTYPE_BYTE_ARRAY) {
3155
- binarySize = NumberUtils.getInt32LE(buffer, index);
3156
- index += 4;
3157
- if (binarySize < 0)
3158
- throw new BSONError('Negative binary type element size found for subtype 0x02');
3159
- if (binarySize > totalBinarySize - 4)
3160
- throw new BSONError('Binary type with subtype 0x02 contains too long binary size');
3161
- if (binarySize < totalBinarySize - 4)
3162
- throw new BSONError('Binary type with subtype 0x02 contains too short binary size');
3163
- }
3164
- if (promoteBuffers && promoteValues) {
3165
- value = ByteUtils.allocateUnsafe(binarySize);
3166
- for (i = 0; i < binarySize; i++) {
3167
- value[i] = buffer[index + i];
3168
- }
3169
- }
3170
- else {
3171
- value = new Binary(buffer.slice(index, index + binarySize), subType);
3172
- if (subType === BSON_BINARY_SUBTYPE_UUID_NEW && UUID.isValid(value)) {
3173
- value = value.toUUID();
3174
- }
3286
+ value = new Binary(buffer.subarray(index, index + binarySize), subType);
3287
+ if (subType === BSON_BINARY_SUBTYPE_UUID_NEW && UUID.isValid(value)) {
3288
+ value = value.toUUID();
3175
3289
  }
3176
3290
  }
3177
3291
  index = index + binarySize;
@@ -3593,6 +3707,9 @@ function serializeBinary(buffer, key, value, index) {
3593
3707
  size = size - 4;
3594
3708
  index += NumberUtils.setInt32LE(buffer, index, size);
3595
3709
  }
3710
+ if (value.sub_type === Binary.SUBTYPE_VECTOR) {
3711
+ validateBinaryVector(value);
3712
+ }
3596
3713
  if (size <= 16) {
3597
3714
  for (let i = 0; i < size; i++)
3598
3715
  buffer[index + i] = data[i];