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.mjs CHANGED
@@ -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)) {
@@ -748,6 +741,7 @@ class Binary extends BSONValue {
748
741
  if (this.buffer[0] !== Binary.VECTOR_TYPE.Int8) {
749
742
  throw new BSONError('Binary datatype field is not Int8');
750
743
  }
744
+ validateBinaryVector(this);
751
745
  return new Int8Array(this.buffer.buffer.slice(this.buffer.byteOffset + 2, this.buffer.byteOffset + this.position));
752
746
  }
753
747
  toFloat32Array() {
@@ -757,6 +751,7 @@ class Binary extends BSONValue {
757
751
  if (this.buffer[0] !== Binary.VECTOR_TYPE.Float32) {
758
752
  throw new BSONError('Binary datatype field is not Float32');
759
753
  }
754
+ validateBinaryVector(this);
760
755
  const floatBytes = new Uint8Array(this.buffer.buffer.slice(this.buffer.byteOffset + 2, this.buffer.byteOffset + this.position));
761
756
  if (NumberUtils.isBigEndian)
762
757
  ByteUtils.swap32(floatBytes);
@@ -769,6 +764,7 @@ class Binary extends BSONValue {
769
764
  if (this.buffer[0] !== Binary.VECTOR_TYPE.PackedBit) {
770
765
  throw new BSONError('Binary datatype field is not packed bit');
771
766
  }
767
+ validateBinaryVector(this);
772
768
  return new Uint8Array(this.buffer.buffer.slice(this.buffer.byteOffset + 2, this.buffer.byteOffset + this.position));
773
769
  }
774
770
  toBits() {
@@ -778,6 +774,7 @@ class Binary extends BSONValue {
778
774
  if (this.buffer[0] !== Binary.VECTOR_TYPE.PackedBit) {
779
775
  throw new BSONError('Binary datatype field is not packed bit');
780
776
  }
777
+ validateBinaryVector(this);
781
778
  const byteCount = this.length() - 2;
782
779
  const bitCount = byteCount * 8 - this.buffer[1];
783
780
  const bits = new Int8Array(bitCount);
@@ -796,7 +793,9 @@ class Binary extends BSONValue {
796
793
  buffer[1] = 0;
797
794
  const intBytes = new Uint8Array(array.buffer, array.byteOffset, array.byteLength);
798
795
  buffer.set(intBytes, 2);
799
- return new this(buffer, this.SUBTYPE_VECTOR);
796
+ const bin = new this(buffer, this.SUBTYPE_VECTOR);
797
+ validateBinaryVector(bin);
798
+ return bin;
800
799
  }
801
800
  static fromFloat32Array(array) {
802
801
  const binaryBytes = ByteUtils.allocate(array.byteLength + 2);
@@ -806,14 +805,18 @@ class Binary extends BSONValue {
806
805
  binaryBytes.set(floatBytes, 2);
807
806
  if (NumberUtils.isBigEndian)
808
807
  ByteUtils.swap32(new Uint8Array(binaryBytes.buffer, 2));
809
- return new this(binaryBytes, this.SUBTYPE_VECTOR);
808
+ const bin = new this(binaryBytes, this.SUBTYPE_VECTOR);
809
+ validateBinaryVector(bin);
810
+ return bin;
810
811
  }
811
812
  static fromPackedBits(array, padding = 0) {
812
813
  const buffer = ByteUtils.allocate(array.byteLength + 2);
813
814
  buffer[0] = Binary.VECTOR_TYPE.PackedBit;
814
815
  buffer[1] = padding;
815
816
  buffer.set(array, 2);
816
- return new this(buffer, this.SUBTYPE_VECTOR);
817
+ const bin = new this(buffer, this.SUBTYPE_VECTOR);
818
+ validateBinaryVector(bin);
819
+ return bin;
817
820
  }
818
821
  static fromBits(bits) {
819
822
  const byteLength = (bits.length + 7) >>> 3;
@@ -863,6 +866,11 @@ function validateBinaryVector(vector) {
863
866
  padding !== 0) {
864
867
  throw new BSONError('Invalid Vector: padding must be zero for int8 and float32 vectors');
865
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
+ }
866
874
  if (datatype === Binary.VECTOR_TYPE.PackedBit && padding !== 0 && size === 2) {
867
875
  throw new BSONError('Invalid Vector: padding must be zero for packed bit vectors that are empty');
868
876
  }
@@ -4374,6 +4382,29 @@ EJSON.serialize = EJSONserialize;
4374
4382
  EJSON.deserialize = EJSONdeserialize;
4375
4383
  Object.freeze(EJSON);
4376
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
+ };
4377
4408
  function getSize(source, offset) {
4378
4409
  try {
4379
4410
  return NumberUtils.getNonnegativeInt32LE(source, offset);
@@ -4418,48 +4449,48 @@ function parseToElements(bytes, startOffset = 0) {
4418
4449
  const nameLength = findNull(bytes, offset) - nameOffset;
4419
4450
  offset += nameLength + 1;
4420
4451
  let length;
4421
- if (type === 1 ||
4422
- type === 18 ||
4423
- type === 9 ||
4424
- type === 17) {
4452
+ if (type === BSONElementType.double ||
4453
+ type === BSONElementType.long ||
4454
+ type === BSONElementType.date ||
4455
+ type === BSONElementType.timestamp) {
4425
4456
  length = 8;
4426
4457
  }
4427
- else if (type === 16) {
4458
+ else if (type === BSONElementType.int) {
4428
4459
  length = 4;
4429
4460
  }
4430
- else if (type === 7) {
4461
+ else if (type === BSONElementType.objectId) {
4431
4462
  length = 12;
4432
4463
  }
4433
- else if (type === 19) {
4464
+ else if (type === BSONElementType.decimal) {
4434
4465
  length = 16;
4435
4466
  }
4436
- else if (type === 8) {
4467
+ else if (type === BSONElementType.bool) {
4437
4468
  length = 1;
4438
4469
  }
4439
- else if (type === 10 ||
4440
- type === 6 ||
4441
- type === 127 ||
4442
- type === 255) {
4470
+ else if (type === BSONElementType.null ||
4471
+ type === BSONElementType.undefined ||
4472
+ type === BSONElementType.maxKey ||
4473
+ type === BSONElementType.minKey) {
4443
4474
  length = 0;
4444
4475
  }
4445
- else if (type === 11) {
4476
+ else if (type === BSONElementType.regex) {
4446
4477
  length = findNull(bytes, findNull(bytes, offset) + 1) + 1 - offset;
4447
4478
  }
4448
- else if (type === 3 ||
4449
- type === 4 ||
4450
- type === 15) {
4479
+ else if (type === BSONElementType.object ||
4480
+ type === BSONElementType.array ||
4481
+ type === BSONElementType.javascriptWithScope) {
4451
4482
  length = getSize(bytes, offset);
4452
4483
  }
4453
- else if (type === 2 ||
4454
- type === 5 ||
4455
- type === 12 ||
4456
- type === 13 ||
4457
- type === 14) {
4484
+ else if (type === BSONElementType.string ||
4485
+ type === BSONElementType.binData ||
4486
+ type === BSONElementType.dbPointer ||
4487
+ type === BSONElementType.javascript ||
4488
+ type === BSONElementType.symbol) {
4458
4489
  length = getSize(bytes, offset) + 4;
4459
- if (type === 5) {
4490
+ if (type === BSONElementType.binData) {
4460
4491
  length += 1;
4461
4492
  }
4462
- if (type === 12) {
4493
+ if (type === BSONElementType.dbPointer) {
4463
4494
  length += 12;
4464
4495
  }
4465
4496
  }