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