bson 6.10.2 → 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.
@@ -51,7 +51,7 @@ function getStylizeFunction(options) {
51
51
  const BSON_MAJOR_VERSION = 6;
52
52
  const BSON_VERSION_SYMBOL = Symbol.for('@@mdb.bson.version');
53
53
  const BSON_INT32_MAX = 0x7fffffff;
54
- const BSON_INT32_MIN = -0x80000000;
54
+ const BSON_INT32_MIN = -2147483648;
55
55
  const BSON_INT64_MAX = Math.pow(2, 63) - 1;
56
56
  const BSON_INT64_MIN = -Math.pow(2, 63);
57
57
  const JS_INT_MAX = Math.pow(2, 53);
@@ -485,9 +485,15 @@ const NumberUtils = {
485
485
  source[offset] * 16777216);
486
486
  },
487
487
  getBigInt64LE(source, offset) {
488
- const lo = NumberUtils.getUint32LE(source, offset);
489
- const hi = NumberUtils.getUint32LE(source, offset + 4);
490
- return (BigInt(hi) << BigInt(32)) + BigInt(lo);
488
+ const hi = BigInt(source[offset + 4] +
489
+ source[offset + 5] * 256 +
490
+ source[offset + 6] * 65536 +
491
+ (source[offset + 7] << 24));
492
+ const lo = BigInt(source[offset] +
493
+ source[offset + 1] * 256 +
494
+ source[offset + 2] * 65536 +
495
+ source[offset + 3] * 16777216);
496
+ return (hi << BigInt(32)) + lo;
491
497
  },
492
498
  getFloat64LE: isBigEndian
493
499
  ? (source, offset) => {
@@ -745,6 +751,7 @@ class Binary extends BSONValue {
745
751
  if (this.buffer[0] !== Binary.VECTOR_TYPE.Int8) {
746
752
  throw new BSONError('Binary datatype field is not Int8');
747
753
  }
754
+ validateBinaryVector(this);
748
755
  return new Int8Array(this.buffer.buffer.slice(this.buffer.byteOffset + 2, this.buffer.byteOffset + this.position));
749
756
  }
750
757
  toFloat32Array() {
@@ -754,6 +761,7 @@ class Binary extends BSONValue {
754
761
  if (this.buffer[0] !== Binary.VECTOR_TYPE.Float32) {
755
762
  throw new BSONError('Binary datatype field is not Float32');
756
763
  }
764
+ validateBinaryVector(this);
757
765
  const floatBytes = new Uint8Array(this.buffer.buffer.slice(this.buffer.byteOffset + 2, this.buffer.byteOffset + this.position));
758
766
  if (NumberUtils.isBigEndian)
759
767
  ByteUtils.swap32(floatBytes);
@@ -766,6 +774,7 @@ class Binary extends BSONValue {
766
774
  if (this.buffer[0] !== Binary.VECTOR_TYPE.PackedBit) {
767
775
  throw new BSONError('Binary datatype field is not packed bit');
768
776
  }
777
+ validateBinaryVector(this);
769
778
  return new Uint8Array(this.buffer.buffer.slice(this.buffer.byteOffset + 2, this.buffer.byteOffset + this.position));
770
779
  }
771
780
  toBits() {
@@ -775,6 +784,7 @@ class Binary extends BSONValue {
775
784
  if (this.buffer[0] !== Binary.VECTOR_TYPE.PackedBit) {
776
785
  throw new BSONError('Binary datatype field is not packed bit');
777
786
  }
787
+ validateBinaryVector(this);
778
788
  const byteCount = this.length() - 2;
779
789
  const bitCount = byteCount * 8 - this.buffer[1];
780
790
  const bits = new Int8Array(bitCount);
@@ -793,7 +803,9 @@ class Binary extends BSONValue {
793
803
  buffer[1] = 0;
794
804
  const intBytes = new Uint8Array(array.buffer, array.byteOffset, array.byteLength);
795
805
  buffer.set(intBytes, 2);
796
- return new this(buffer, this.SUBTYPE_VECTOR);
806
+ const bin = new this(buffer, this.SUBTYPE_VECTOR);
807
+ validateBinaryVector(bin);
808
+ return bin;
797
809
  }
798
810
  static fromFloat32Array(array) {
799
811
  const binaryBytes = ByteUtils.allocate(array.byteLength + 2);
@@ -803,14 +815,18 @@ class Binary extends BSONValue {
803
815
  binaryBytes.set(floatBytes, 2);
804
816
  if (NumberUtils.isBigEndian)
805
817
  ByteUtils.swap32(new Uint8Array(binaryBytes.buffer, 2));
806
- return new this(binaryBytes, this.SUBTYPE_VECTOR);
818
+ const bin = new this(binaryBytes, this.SUBTYPE_VECTOR);
819
+ validateBinaryVector(bin);
820
+ return bin;
807
821
  }
808
822
  static fromPackedBits(array, padding = 0) {
809
823
  const buffer = ByteUtils.allocate(array.byteLength + 2);
810
824
  buffer[0] = Binary.VECTOR_TYPE.PackedBit;
811
825
  buffer[1] = padding;
812
826
  buffer.set(array, 2);
813
- return new this(buffer, this.SUBTYPE_VECTOR);
827
+ const bin = new this(buffer, this.SUBTYPE_VECTOR);
828
+ validateBinaryVector(bin);
829
+ return bin;
814
830
  }
815
831
  static fromBits(bits) {
816
832
  const byteLength = (bits.length + 7) >>> 3;
@@ -860,6 +876,11 @@ function validateBinaryVector(vector) {
860
876
  padding !== 0) {
861
877
  throw new BSONError('Invalid Vector: padding must be zero for int8 and float32 vectors');
862
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
+ }
863
884
  if (datatype === Binary.VECTOR_TYPE.PackedBit && padding !== 0 && size === 2) {
864
885
  throw new BSONError('Invalid Vector: padding must be zero for packed bit vectors that are empty');
865
886
  }
@@ -1188,7 +1209,7 @@ class Long extends BSONValue {
1188
1209
  return Long.MAX_UNSIGNED_VALUE;
1189
1210
  }
1190
1211
  else {
1191
- if (value <= -TWO_PWR_63_DBL)
1212
+ if (value <= -9223372036854776e3)
1192
1213
  return Long.MIN_VALUE;
1193
1214
  if (value + 1 >= TWO_PWR_63_DBL)
1194
1215
  return Long.MAX_VALUE;
@@ -1347,7 +1368,7 @@ class Long extends BSONValue {
1347
1368
  throw new BSONError('division by zero');
1348
1369
  if (wasm) {
1349
1370
  if (!this.unsigned &&
1350
- this.high === -0x80000000 &&
1371
+ this.high === -2147483648 &&
1351
1372
  divisor.low === -1 &&
1352
1373
  divisor.high === -1) {
1353
1374
  return this;
@@ -3102,9 +3123,8 @@ function deserializeObject(buffer, index, options, isArray = false) {
3102
3123
  throw new BSONError('corrupt bson message');
3103
3124
  const object = isArray ? [] : {};
3104
3125
  let arrayIndex = 0;
3105
- const done = false;
3106
3126
  let isPossibleDBRef = isArray ? false : null;
3107
- while (!done) {
3127
+ while (true) {
3108
3128
  const elementType = buffer[index++];
3109
3129
  if (elementType === 0)
3110
3130
  break;
@@ -3852,8 +3872,8 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3852
3872
  done = !!entry.done;
3853
3873
  if (done)
3854
3874
  continue;
3855
- const key = entry.value[0];
3856
- let value = entry.value[1];
3875
+ const key = entry.value ? entry.value[0] : undefined;
3876
+ let value = entry.value ? entry.value[1] : undefined;
3857
3877
  if (typeof value?.toBSON === 'function') {
3858
3878
  value = value.toBSON();
3859
3879
  }
@@ -4372,6 +4392,29 @@ EJSON.serialize = EJSONserialize;
4372
4392
  EJSON.deserialize = EJSONdeserialize;
4373
4393
  Object.freeze(EJSON);
4374
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
+ };
4375
4418
  function getSize(source, offset) {
4376
4419
  try {
4377
4420
  return NumberUtils.getNonnegativeInt32LE(source, offset);
@@ -4416,48 +4459,48 @@ function parseToElements(bytes, startOffset = 0) {
4416
4459
  const nameLength = findNull(bytes, offset) - nameOffset;
4417
4460
  offset += nameLength + 1;
4418
4461
  let length;
4419
- if (type === 1 ||
4420
- type === 18 ||
4421
- type === 9 ||
4422
- type === 17) {
4462
+ if (type === BSONElementType.double ||
4463
+ type === BSONElementType.long ||
4464
+ type === BSONElementType.date ||
4465
+ type === BSONElementType.timestamp) {
4423
4466
  length = 8;
4424
4467
  }
4425
- else if (type === 16) {
4468
+ else if (type === BSONElementType.int) {
4426
4469
  length = 4;
4427
4470
  }
4428
- else if (type === 7) {
4471
+ else if (type === BSONElementType.objectId) {
4429
4472
  length = 12;
4430
4473
  }
4431
- else if (type === 19) {
4474
+ else if (type === BSONElementType.decimal) {
4432
4475
  length = 16;
4433
4476
  }
4434
- else if (type === 8) {
4477
+ else if (type === BSONElementType.bool) {
4435
4478
  length = 1;
4436
4479
  }
4437
- else if (type === 10 ||
4438
- type === 6 ||
4439
- type === 127 ||
4440
- type === 255) {
4480
+ else if (type === BSONElementType.null ||
4481
+ type === BSONElementType.undefined ||
4482
+ type === BSONElementType.maxKey ||
4483
+ type === BSONElementType.minKey) {
4441
4484
  length = 0;
4442
4485
  }
4443
- else if (type === 11) {
4486
+ else if (type === BSONElementType.regex) {
4444
4487
  length = findNull(bytes, findNull(bytes, offset) + 1) + 1 - offset;
4445
4488
  }
4446
- else if (type === 3 ||
4447
- type === 4 ||
4448
- type === 15) {
4489
+ else if (type === BSONElementType.object ||
4490
+ type === BSONElementType.array ||
4491
+ type === BSONElementType.javascriptWithScope) {
4449
4492
  length = getSize(bytes, offset);
4450
4493
  }
4451
- else if (type === 2 ||
4452
- type === 5 ||
4453
- type === 12 ||
4454
- type === 13 ||
4455
- type === 14) {
4494
+ else if (type === BSONElementType.string ||
4495
+ type === BSONElementType.binData ||
4496
+ type === BSONElementType.dbPointer ||
4497
+ type === BSONElementType.javascript ||
4498
+ type === BSONElementType.symbol) {
4456
4499
  length = getSize(bytes, offset) + 4;
4457
- if (type === 5) {
4500
+ if (type === BSONElementType.binData) {
4458
4501
  length += 1;
4459
4502
  }
4460
- if (type === 12) {
4503
+ if (type === BSONElementType.dbPointer) {
4461
4504
  length += 12;
4462
4505
  }
4463
4506
  }