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