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