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.mjs CHANGED
@@ -48,7 +48,7 @@ function getStylizeFunction(options) {
48
48
  const BSON_MAJOR_VERSION = 6;
49
49
  const BSON_VERSION_SYMBOL = Symbol.for('@@mdb.bson.version');
50
50
  const BSON_INT32_MAX = 0x7fffffff;
51
- const BSON_INT32_MIN = -0x80000000;
51
+ const BSON_INT32_MIN = -2147483648;
52
52
  const BSON_INT64_MAX = Math.pow(2, 63) - 1;
53
53
  const BSON_INT64_MIN = -Math.pow(2, 63);
54
54
  const JS_INT_MAX = Math.pow(2, 53);
@@ -216,14 +216,7 @@ function tryWriteBasicLatin(destination, source, offset) {
216
216
  function nodejsMathRandomBytes(byteLength) {
217
217
  return nodeJsByteUtils.fromNumberArray(Array.from({ length: byteLength }, () => Math.floor(Math.random() * 256)));
218
218
  }
219
- const nodejsRandomBytes = await (async () => {
220
- try {
221
- return (await import('crypto')).randomBytes;
222
- }
223
- catch {
224
- return nodejsMathRandomBytes;
225
- }
226
- })();
219
+ const nodejsRandomBytes = nodejsMathRandomBytes;
227
220
  const nodeJsByteUtils = {
228
221
  toLocalBufferType(potentialBuffer) {
229
222
  if (Buffer.isBuffer(potentialBuffer)) {
@@ -482,9 +475,15 @@ const NumberUtils = {
482
475
  source[offset] * 16777216);
483
476
  },
484
477
  getBigInt64LE(source, offset) {
485
- const lo = NumberUtils.getUint32LE(source, offset);
486
- const hi = NumberUtils.getUint32LE(source, offset + 4);
487
- return (BigInt(hi) << BigInt(32)) + BigInt(lo);
478
+ const hi = BigInt(source[offset + 4] +
479
+ source[offset + 5] * 256 +
480
+ source[offset + 6] * 65536 +
481
+ (source[offset + 7] << 24));
482
+ const lo = BigInt(source[offset] +
483
+ source[offset + 1] * 256 +
484
+ source[offset + 2] * 65536 +
485
+ source[offset + 3] * 16777216);
486
+ return (hi << BigInt(32)) + lo;
488
487
  },
489
488
  getFloat64LE: isBigEndian
490
489
  ? (source, offset) => {
@@ -742,6 +741,7 @@ class Binary extends BSONValue {
742
741
  if (this.buffer[0] !== Binary.VECTOR_TYPE.Int8) {
743
742
  throw new BSONError('Binary datatype field is not Int8');
744
743
  }
744
+ validateBinaryVector(this);
745
745
  return new Int8Array(this.buffer.buffer.slice(this.buffer.byteOffset + 2, this.buffer.byteOffset + this.position));
746
746
  }
747
747
  toFloat32Array() {
@@ -751,6 +751,7 @@ class Binary extends BSONValue {
751
751
  if (this.buffer[0] !== Binary.VECTOR_TYPE.Float32) {
752
752
  throw new BSONError('Binary datatype field is not Float32');
753
753
  }
754
+ validateBinaryVector(this);
754
755
  const floatBytes = new Uint8Array(this.buffer.buffer.slice(this.buffer.byteOffset + 2, this.buffer.byteOffset + this.position));
755
756
  if (NumberUtils.isBigEndian)
756
757
  ByteUtils.swap32(floatBytes);
@@ -763,6 +764,7 @@ class Binary extends BSONValue {
763
764
  if (this.buffer[0] !== Binary.VECTOR_TYPE.PackedBit) {
764
765
  throw new BSONError('Binary datatype field is not packed bit');
765
766
  }
767
+ validateBinaryVector(this);
766
768
  return new Uint8Array(this.buffer.buffer.slice(this.buffer.byteOffset + 2, this.buffer.byteOffset + this.position));
767
769
  }
768
770
  toBits() {
@@ -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
  const byteCount = this.length() - 2;
776
779
  const bitCount = byteCount * 8 - this.buffer[1];
777
780
  const bits = new Int8Array(bitCount);
@@ -790,7 +793,9 @@ class Binary extends BSONValue {
790
793
  buffer[1] = 0;
791
794
  const intBytes = new Uint8Array(array.buffer, array.byteOffset, array.byteLength);
792
795
  buffer.set(intBytes, 2);
793
- return new this(buffer, this.SUBTYPE_VECTOR);
796
+ const bin = new this(buffer, this.SUBTYPE_VECTOR);
797
+ validateBinaryVector(bin);
798
+ return bin;
794
799
  }
795
800
  static fromFloat32Array(array) {
796
801
  const binaryBytes = ByteUtils.allocate(array.byteLength + 2);
@@ -800,14 +805,18 @@ class Binary extends BSONValue {
800
805
  binaryBytes.set(floatBytes, 2);
801
806
  if (NumberUtils.isBigEndian)
802
807
  ByteUtils.swap32(new Uint8Array(binaryBytes.buffer, 2));
803
- return new this(binaryBytes, this.SUBTYPE_VECTOR);
808
+ const bin = new this(binaryBytes, this.SUBTYPE_VECTOR);
809
+ validateBinaryVector(bin);
810
+ return bin;
804
811
  }
805
812
  static fromPackedBits(array, padding = 0) {
806
813
  const buffer = ByteUtils.allocate(array.byteLength + 2);
807
814
  buffer[0] = Binary.VECTOR_TYPE.PackedBit;
808
815
  buffer[1] = padding;
809
816
  buffer.set(array, 2);
810
- return new this(buffer, this.SUBTYPE_VECTOR);
817
+ const bin = new this(buffer, this.SUBTYPE_VECTOR);
818
+ validateBinaryVector(bin);
819
+ return bin;
811
820
  }
812
821
  static fromBits(bits) {
813
822
  const byteLength = (bits.length + 7) >>> 3;
@@ -857,6 +866,11 @@ function validateBinaryVector(vector) {
857
866
  padding !== 0) {
858
867
  throw new BSONError('Invalid Vector: padding must be zero for int8 and float32 vectors');
859
868
  }
869
+ if (datatype === Binary.VECTOR_TYPE.Float32) {
870
+ if (size !== 0 && size - 2 !== 0 && (size - 2) % 4 !== 0) {
871
+ throw new BSONError('Invalid Vector: Float32 vector must contain a multiple of 4 bytes');
872
+ }
873
+ }
860
874
  if (datatype === Binary.VECTOR_TYPE.PackedBit && padding !== 0 && size === 2) {
861
875
  throw new BSONError('Invalid Vector: padding must be zero for packed bit vectors that are empty');
862
876
  }
@@ -1185,7 +1199,7 @@ class Long extends BSONValue {
1185
1199
  return Long.MAX_UNSIGNED_VALUE;
1186
1200
  }
1187
1201
  else {
1188
- if (value <= -TWO_PWR_63_DBL)
1202
+ if (value <= -9223372036854776e3)
1189
1203
  return Long.MIN_VALUE;
1190
1204
  if (value + 1 >= TWO_PWR_63_DBL)
1191
1205
  return Long.MAX_VALUE;
@@ -1344,7 +1358,7 @@ class Long extends BSONValue {
1344
1358
  throw new BSONError('division by zero');
1345
1359
  if (wasm) {
1346
1360
  if (!this.unsigned &&
1347
- this.high === -0x80000000 &&
1361
+ this.high === -2147483648 &&
1348
1362
  divisor.low === -1 &&
1349
1363
  divisor.high === -1) {
1350
1364
  return this;
@@ -3099,9 +3113,8 @@ function deserializeObject(buffer, index, options, isArray = false) {
3099
3113
  throw new BSONError('corrupt bson message');
3100
3114
  const object = isArray ? [] : {};
3101
3115
  let arrayIndex = 0;
3102
- const done = false;
3103
3116
  let isPossibleDBRef = isArray ? false : null;
3104
- while (!done) {
3117
+ while (true) {
3105
3118
  const elementType = buffer[index++];
3106
3119
  if (elementType === 0)
3107
3120
  break;
@@ -3849,8 +3862,8 @@ function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializ
3849
3862
  done = !!entry.done;
3850
3863
  if (done)
3851
3864
  continue;
3852
- const key = entry.value[0];
3853
- let value = entry.value[1];
3865
+ const key = entry.value ? entry.value[0] : undefined;
3866
+ let value = entry.value ? entry.value[1] : undefined;
3854
3867
  if (typeof value?.toBSON === 'function') {
3855
3868
  value = value.toBSON();
3856
3869
  }
@@ -4369,6 +4382,29 @@ EJSON.serialize = EJSONserialize;
4369
4382
  EJSON.deserialize = EJSONdeserialize;
4370
4383
  Object.freeze(EJSON);
4371
4384
 
4385
+ const BSONElementType = {
4386
+ double: 1,
4387
+ string: 2,
4388
+ object: 3,
4389
+ array: 4,
4390
+ binData: 5,
4391
+ undefined: 6,
4392
+ objectId: 7,
4393
+ bool: 8,
4394
+ date: 9,
4395
+ null: 10,
4396
+ regex: 11,
4397
+ dbPointer: 12,
4398
+ javascript: 13,
4399
+ symbol: 14,
4400
+ javascriptWithScope: 15,
4401
+ int: 16,
4402
+ timestamp: 17,
4403
+ long: 18,
4404
+ decimal: 19,
4405
+ minKey: 255,
4406
+ maxKey: 127
4407
+ };
4372
4408
  function getSize(source, offset) {
4373
4409
  try {
4374
4410
  return NumberUtils.getNonnegativeInt32LE(source, offset);
@@ -4413,48 +4449,48 @@ function parseToElements(bytes, startOffset = 0) {
4413
4449
  const nameLength = findNull(bytes, offset) - nameOffset;
4414
4450
  offset += nameLength + 1;
4415
4451
  let length;
4416
- if (type === 1 ||
4417
- type === 18 ||
4418
- type === 9 ||
4419
- type === 17) {
4452
+ if (type === BSONElementType.double ||
4453
+ type === BSONElementType.long ||
4454
+ type === BSONElementType.date ||
4455
+ type === BSONElementType.timestamp) {
4420
4456
  length = 8;
4421
4457
  }
4422
- else if (type === 16) {
4458
+ else if (type === BSONElementType.int) {
4423
4459
  length = 4;
4424
4460
  }
4425
- else if (type === 7) {
4461
+ else if (type === BSONElementType.objectId) {
4426
4462
  length = 12;
4427
4463
  }
4428
- else if (type === 19) {
4464
+ else if (type === BSONElementType.decimal) {
4429
4465
  length = 16;
4430
4466
  }
4431
- else if (type === 8) {
4467
+ else if (type === BSONElementType.bool) {
4432
4468
  length = 1;
4433
4469
  }
4434
- else if (type === 10 ||
4435
- type === 6 ||
4436
- type === 127 ||
4437
- type === 255) {
4470
+ else if (type === BSONElementType.null ||
4471
+ type === BSONElementType.undefined ||
4472
+ type === BSONElementType.maxKey ||
4473
+ type === BSONElementType.minKey) {
4438
4474
  length = 0;
4439
4475
  }
4440
- else if (type === 11) {
4476
+ else if (type === BSONElementType.regex) {
4441
4477
  length = findNull(bytes, findNull(bytes, offset) + 1) + 1 - offset;
4442
4478
  }
4443
- else if (type === 3 ||
4444
- type === 4 ||
4445
- type === 15) {
4479
+ else if (type === BSONElementType.object ||
4480
+ type === BSONElementType.array ||
4481
+ type === BSONElementType.javascriptWithScope) {
4446
4482
  length = getSize(bytes, offset);
4447
4483
  }
4448
- else if (type === 2 ||
4449
- type === 5 ||
4450
- type === 12 ||
4451
- type === 13 ||
4452
- type === 14) {
4484
+ else if (type === BSONElementType.string ||
4485
+ type === BSONElementType.binData ||
4486
+ type === BSONElementType.dbPointer ||
4487
+ type === BSONElementType.javascript ||
4488
+ type === BSONElementType.symbol) {
4453
4489
  length = getSize(bytes, offset) + 4;
4454
- if (type === 5) {
4490
+ if (type === BSONElementType.binData) {
4455
4491
  length += 1;
4456
4492
  }
4457
- if (type === 12) {
4493
+ if (type === BSONElementType.dbPointer) {
4458
4494
  length += 12;
4459
4495
  }
4460
4496
  }