bson 6.10.3 → 6.10.4

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.
@@ -751,6 +751,7 @@ class Binary extends BSONValue {
751
751
  if (this.buffer[0] !== Binary.VECTOR_TYPE.Int8) {
752
752
  throw new BSONError('Binary datatype field is not Int8');
753
753
  }
754
+ validateBinaryVector(this);
754
755
  return new Int8Array(this.buffer.buffer.slice(this.buffer.byteOffset + 2, this.buffer.byteOffset + this.position));
755
756
  }
756
757
  toFloat32Array() {
@@ -760,6 +761,7 @@ class Binary extends BSONValue {
760
761
  if (this.buffer[0] !== Binary.VECTOR_TYPE.Float32) {
761
762
  throw new BSONError('Binary datatype field is not Float32');
762
763
  }
764
+ validateBinaryVector(this);
763
765
  const floatBytes = new Uint8Array(this.buffer.buffer.slice(this.buffer.byteOffset + 2, this.buffer.byteOffset + this.position));
764
766
  if (NumberUtils.isBigEndian)
765
767
  ByteUtils.swap32(floatBytes);
@@ -772,6 +774,7 @@ class Binary extends BSONValue {
772
774
  if (this.buffer[0] !== Binary.VECTOR_TYPE.PackedBit) {
773
775
  throw new BSONError('Binary datatype field is not packed bit');
774
776
  }
777
+ validateBinaryVector(this);
775
778
  return new Uint8Array(this.buffer.buffer.slice(this.buffer.byteOffset + 2, this.buffer.byteOffset + this.position));
776
779
  }
777
780
  toBits() {
@@ -781,6 +784,7 @@ class Binary extends BSONValue {
781
784
  if (this.buffer[0] !== Binary.VECTOR_TYPE.PackedBit) {
782
785
  throw new BSONError('Binary datatype field is not packed bit');
783
786
  }
787
+ validateBinaryVector(this);
784
788
  const byteCount = this.length() - 2;
785
789
  const bitCount = byteCount * 8 - this.buffer[1];
786
790
  const bits = new Int8Array(bitCount);
@@ -799,7 +803,9 @@ class Binary extends BSONValue {
799
803
  buffer[1] = 0;
800
804
  const intBytes = new Uint8Array(array.buffer, array.byteOffset, array.byteLength);
801
805
  buffer.set(intBytes, 2);
802
- return new this(buffer, this.SUBTYPE_VECTOR);
806
+ const bin = new this(buffer, this.SUBTYPE_VECTOR);
807
+ validateBinaryVector(bin);
808
+ return bin;
803
809
  }
804
810
  static fromFloat32Array(array) {
805
811
  const binaryBytes = ByteUtils.allocate(array.byteLength + 2);
@@ -809,14 +815,18 @@ class Binary extends BSONValue {
809
815
  binaryBytes.set(floatBytes, 2);
810
816
  if (NumberUtils.isBigEndian)
811
817
  ByteUtils.swap32(new Uint8Array(binaryBytes.buffer, 2));
812
- return new this(binaryBytes, this.SUBTYPE_VECTOR);
818
+ const bin = new this(binaryBytes, this.SUBTYPE_VECTOR);
819
+ validateBinaryVector(bin);
820
+ return bin;
813
821
  }
814
822
  static fromPackedBits(array, padding = 0) {
815
823
  const buffer = ByteUtils.allocate(array.byteLength + 2);
816
824
  buffer[0] = Binary.VECTOR_TYPE.PackedBit;
817
825
  buffer[1] = padding;
818
826
  buffer.set(array, 2);
819
- return new this(buffer, this.SUBTYPE_VECTOR);
827
+ const bin = new this(buffer, this.SUBTYPE_VECTOR);
828
+ validateBinaryVector(bin);
829
+ return bin;
820
830
  }
821
831
  static fromBits(bits) {
822
832
  const byteLength = (bits.length + 7) >>> 3;
@@ -866,6 +876,11 @@ function validateBinaryVector(vector) {
866
876
  padding !== 0) {
867
877
  throw new BSONError('Invalid Vector: padding must be zero for int8 and float32 vectors');
868
878
  }
879
+ if (datatype === Binary.VECTOR_TYPE.Float32) {
880
+ if (size !== 0 && size - 2 !== 0 && (size - 2) % 4 !== 0) {
881
+ throw new BSONError('Invalid Vector: Float32 vector must contain a multiple of 4 bytes');
882
+ }
883
+ }
869
884
  if (datatype === Binary.VECTOR_TYPE.PackedBit && padding !== 0 && size === 2) {
870
885
  throw new BSONError('Invalid Vector: padding must be zero for packed bit vectors that are empty');
871
886
  }
@@ -4377,6 +4392,29 @@ EJSON.serialize = EJSONserialize;
4377
4392
  EJSON.deserialize = EJSONdeserialize;
4378
4393
  Object.freeze(EJSON);
4379
4394
 
4395
+ const BSONElementType = {
4396
+ double: 1,
4397
+ string: 2,
4398
+ object: 3,
4399
+ array: 4,
4400
+ binData: 5,
4401
+ undefined: 6,
4402
+ objectId: 7,
4403
+ bool: 8,
4404
+ date: 9,
4405
+ null: 10,
4406
+ regex: 11,
4407
+ dbPointer: 12,
4408
+ javascript: 13,
4409
+ symbol: 14,
4410
+ javascriptWithScope: 15,
4411
+ int: 16,
4412
+ timestamp: 17,
4413
+ long: 18,
4414
+ decimal: 19,
4415
+ minKey: 255,
4416
+ maxKey: 127
4417
+ };
4380
4418
  function getSize(source, offset) {
4381
4419
  try {
4382
4420
  return NumberUtils.getNonnegativeInt32LE(source, offset);
@@ -4421,48 +4459,48 @@ function parseToElements(bytes, startOffset = 0) {
4421
4459
  const nameLength = findNull(bytes, offset) - nameOffset;
4422
4460
  offset += nameLength + 1;
4423
4461
  let length;
4424
- if (type === 1 ||
4425
- type === 18 ||
4426
- type === 9 ||
4427
- type === 17) {
4462
+ if (type === BSONElementType.double ||
4463
+ type === BSONElementType.long ||
4464
+ type === BSONElementType.date ||
4465
+ type === BSONElementType.timestamp) {
4428
4466
  length = 8;
4429
4467
  }
4430
- else if (type === 16) {
4468
+ else if (type === BSONElementType.int) {
4431
4469
  length = 4;
4432
4470
  }
4433
- else if (type === 7) {
4471
+ else if (type === BSONElementType.objectId) {
4434
4472
  length = 12;
4435
4473
  }
4436
- else if (type === 19) {
4474
+ else if (type === BSONElementType.decimal) {
4437
4475
  length = 16;
4438
4476
  }
4439
- else if (type === 8) {
4477
+ else if (type === BSONElementType.bool) {
4440
4478
  length = 1;
4441
4479
  }
4442
- else if (type === 10 ||
4443
- type === 6 ||
4444
- type === 127 ||
4445
- type === 255) {
4480
+ else if (type === BSONElementType.null ||
4481
+ type === BSONElementType.undefined ||
4482
+ type === BSONElementType.maxKey ||
4483
+ type === BSONElementType.minKey) {
4446
4484
  length = 0;
4447
4485
  }
4448
- else if (type === 11) {
4486
+ else if (type === BSONElementType.regex) {
4449
4487
  length = findNull(bytes, findNull(bytes, offset) + 1) + 1 - offset;
4450
4488
  }
4451
- else if (type === 3 ||
4452
- type === 4 ||
4453
- type === 15) {
4489
+ else if (type === BSONElementType.object ||
4490
+ type === BSONElementType.array ||
4491
+ type === BSONElementType.javascriptWithScope) {
4454
4492
  length = getSize(bytes, offset);
4455
4493
  }
4456
- else if (type === 2 ||
4457
- type === 5 ||
4458
- type === 12 ||
4459
- type === 13 ||
4460
- type === 14) {
4494
+ else if (type === BSONElementType.string ||
4495
+ type === BSONElementType.binData ||
4496
+ type === BSONElementType.dbPointer ||
4497
+ type === BSONElementType.javascript ||
4498
+ type === BSONElementType.symbol) {
4461
4499
  length = getSize(bytes, offset) + 4;
4462
- if (type === 5) {
4500
+ if (type === BSONElementType.binData) {
4463
4501
  length += 1;
4464
4502
  }
4465
- if (type === 12) {
4503
+ if (type === BSONElementType.dbPointer) {
4466
4504
  length += 12;
4467
4505
  }
4468
4506
  }