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/package.json CHANGED
@@ -14,7 +14,7 @@
14
14
  "vendor"
15
15
  ],
16
16
  "types": "bson.d.ts",
17
- "version": "6.10.2",
17
+ "version": "6.10.4",
18
18
  "author": {
19
19
  "name": "The MongoDB NodeJS Team",
20
20
  "email": "dbx-node@mongodb.com"
@@ -27,40 +27,40 @@
27
27
  },
28
28
  "devDependencies": {
29
29
  "@istanbuljs/nyc-config-typescript": "^1.0.2",
30
- "@microsoft/api-extractor": "^7.47.5",
31
- "@rollup/plugin-node-resolve": "^15.2.3",
32
- "@rollup/plugin-typescript": "^11.1.6",
30
+ "@microsoft/api-extractor": "^7.52.5",
31
+ "@rollup/plugin-node-resolve": "^16.0.1",
32
+ "@rollup/plugin-typescript": "^12.1.2",
33
33
  "@types/chai": "^4.3.17",
34
34
  "@types/mocha": "^10.0.7",
35
- "@types/node": "^22.1.0",
36
- "@types/sinon": "^17.0.3",
35
+ "@types/node": "^22.15.3",
36
+ "@types/sinon": "^17.0.4",
37
37
  "@types/sinon-chai": "^3.2.12",
38
- "@typescript-eslint/eslint-plugin": "^8.0.1",
39
- "@typescript-eslint/parser": "^8.0.1",
38
+ "@typescript-eslint/eslint-plugin": "^8.31.1",
39
+ "@typescript-eslint/parser": "^8.31.1",
40
40
  "benchmark": "^2.1.4",
41
41
  "chai": "^4.4.1",
42
42
  "chalk": "^5.3.0",
43
- "dbx-js-tools": "github:mongodb-js/dbx-js-tools",
44
- "eslint": "^9.8.0",
45
- "eslint-config-prettier": "^9.1.0",
43
+ "dbx-js-tools": "github:mongodb-js/dbx-js-tools#main",
44
+ "eslint": "^9.25.1",
45
+ "eslint-config-prettier": "^10.1.2",
46
46
  "eslint-plugin-no-bigint-usage": "file:etc/eslint/no-bigint-usage",
47
- "eslint-plugin-prettier": "^5.2.1",
48
- "eslint-plugin-tsdoc": "^0.3.0",
47
+ "eslint-plugin-prettier": "^5.2.6",
48
+ "eslint-plugin-tsdoc": "^0.4.0",
49
49
  "magic-string": "^0.30.11",
50
50
  "mocha": "^10.7.0",
51
51
  "node-fetch": "^3.3.2",
52
52
  "nyc": "^15.1.0",
53
- "prettier": "^3.3.3",
54
- "rollup": "^4.20.0",
53
+ "prettier": "^3.5.3",
54
+ "rollup": "^4.40.1",
55
55
  "sinon": "^18.0.0",
56
56
  "sinon-chai": "^3.7.0",
57
57
  "source-map-support": "^0.5.21",
58
58
  "tar": "^7.4.3",
59
59
  "ts-node": "^10.9.2",
60
60
  "tsd": "^0.31.1",
61
- "typescript": "^5.5.4",
61
+ "typescript": "^5.8.3",
62
62
  "typescript-cached-transpile": "0.0.6",
63
- "uuid": "^10.0.0"
63
+ "uuid": "^11.1.0"
64
64
  },
65
65
  "tsd": {
66
66
  "directory": "test/types",
@@ -75,18 +75,18 @@
75
75
  "native": false
76
76
  },
77
77
  "main": "./lib/bson.cjs",
78
- "module": "./lib/bson.mjs",
78
+ "module": "./lib/bson.node.mjs",
79
79
  "exports": {
80
- "import": {
80
+ "browser": {
81
81
  "types": "./bson.d.ts",
82
82
  "default": "./lib/bson.mjs"
83
83
  },
84
- "require": {
85
- "types": "./bson.d.ts",
86
- "default": "./lib/bson.cjs"
87
- },
88
84
  "react-native": "./lib/bson.rn.cjs",
89
- "browser": "./lib/bson.mjs"
85
+ "default": {
86
+ "types": "./bson.d.ts",
87
+ "import": "./lib/bson.node.mjs",
88
+ "require": "./lib/bson.cjs"
89
+ }
90
90
  },
91
91
  "compass:exports": {
92
92
  "import": "./lib/bson.cjs",
@@ -102,18 +102,19 @@
102
102
  "check:tsd": "npm run build:dts && tsd",
103
103
  "check:web": "WEB=true mocha test/node",
104
104
  "check:web-no-bigint": "WEB=true NO_BIGINT=true mocha test/node",
105
- "check:granular-bench": "npm run build:bench && node ./test/bench/etc/run_granular_benchmarks.js",
106
- "check:spec-bench": "npm run build:bench && node ./test/bench/lib/spec/bsonBench.js",
107
- "check:custom-bench": "npm run build && node ./test/bench/custom/main.mjs",
105
+ "check:granular-bench": "npm run build:bench && npm run check:baseline-bench && node ./test/bench/etc/run_granular_benchmarks.js",
106
+ "check:spec-bench": "npm run build:bench && npm run check:baseline-bench && node ./test/bench/lib/spec/bsonBench.js",
107
+ "check:custom-bench": "npm run build && npm run check:baseline-bench && node ./test/bench/custom/main.mjs",
108
+ "check:baseline-bench": "node ./test/bench/etc/cpuBaseline.js",
108
109
  "build:bench": "cd test/bench && npx tsc",
109
110
  "build:ts": "node ./node_modules/typescript/bin/tsc",
110
111
  "build:dts": "npm run build:ts && api-extractor run --typescript-compiler-folder node_modules/typescript --local && node etc/clean_definition_files.cjs",
111
112
  "build:bundle": "rollup -c rollup.config.mjs",
112
113
  "build": "npm run build:dts && npm run build:bundle",
113
114
  "check:lint": "ESLINT_USE_FLAT_CONFIG=false eslint -v && ESLINT_USE_FLAT_CONFIG=false eslint --ext '.js,.ts' --max-warnings=0 src test && npm run build:dts && npm run check:tsd",
114
- "format": "eslint --ext '.js,.ts' src test --fix",
115
+ "format": "ESLINT_USE_FLAT_CONFIG=false eslint --ext '.js,.ts' src test --fix",
115
116
  "check:coverage": "nyc --check-coverage npm run check:node",
116
117
  "prepare": "node etc/prepare.js",
117
118
  "release": "standard-version -i HISTORY.md"
118
119
  }
119
- }
120
+ }
package/src/binary.ts CHANGED
@@ -341,6 +341,8 @@ export class Binary extends BSONValue {
341
341
  throw new BSONError('Binary datatype field is not Int8');
342
342
  }
343
343
 
344
+ validateBinaryVector(this);
345
+
344
346
  return new Int8Array(
345
347
  this.buffer.buffer.slice(this.buffer.byteOffset + 2, this.buffer.byteOffset + this.position)
346
348
  );
@@ -361,6 +363,8 @@ export class Binary extends BSONValue {
361
363
  throw new BSONError('Binary datatype field is not Float32');
362
364
  }
363
365
 
366
+ validateBinaryVector(this);
367
+
364
368
  const floatBytes = new Uint8Array(
365
369
  this.buffer.buffer.slice(this.buffer.byteOffset + 2, this.buffer.byteOffset + this.position)
366
370
  );
@@ -387,6 +391,8 @@ export class Binary extends BSONValue {
387
391
  throw new BSONError('Binary datatype field is not packed bit');
388
392
  }
389
393
 
394
+ validateBinaryVector(this);
395
+
390
396
  return new Uint8Array(
391
397
  this.buffer.buffer.slice(this.buffer.byteOffset + 2, this.buffer.byteOffset + this.position)
392
398
  );
@@ -409,6 +415,8 @@ export class Binary extends BSONValue {
409
415
  throw new BSONError('Binary datatype field is not packed bit');
410
416
  }
411
417
 
418
+ validateBinaryVector(this);
419
+
412
420
  const byteCount = this.length() - 2;
413
421
  const bitCount = byteCount * 8 - this.buffer[1];
414
422
  const bits = new Int8Array(bitCount);
@@ -434,7 +442,9 @@ export class Binary extends BSONValue {
434
442
  buffer[1] = 0;
435
443
  const intBytes = new Uint8Array(array.buffer, array.byteOffset, array.byteLength);
436
444
  buffer.set(intBytes, 2);
437
- return new this(buffer, this.SUBTYPE_VECTOR);
445
+ const bin = new this(buffer, this.SUBTYPE_VECTOR);
446
+ validateBinaryVector(bin);
447
+ return bin;
438
448
  }
439
449
 
440
450
  /** Constructs a Binary representing an Float32 Vector. */
@@ -448,7 +458,9 @@ export class Binary extends BSONValue {
448
458
 
449
459
  if (NumberUtils.isBigEndian) ByteUtils.swap32(new Uint8Array(binaryBytes.buffer, 2));
450
460
 
451
- return new this(binaryBytes, this.SUBTYPE_VECTOR);
461
+ const bin = new this(binaryBytes, this.SUBTYPE_VECTOR);
462
+ validateBinaryVector(bin);
463
+ return bin;
452
464
  }
453
465
 
454
466
  /**
@@ -461,7 +473,9 @@ export class Binary extends BSONValue {
461
473
  buffer[0] = Binary.VECTOR_TYPE.PackedBit;
462
474
  buffer[1] = padding;
463
475
  buffer.set(array, 2);
464
- return new this(buffer, this.SUBTYPE_VECTOR);
476
+ const bin = new this(buffer, this.SUBTYPE_VECTOR);
477
+ validateBinaryVector(bin);
478
+ return bin;
465
479
  }
466
480
 
467
481
  /**
@@ -517,6 +531,12 @@ export function validateBinaryVector(vector: Binary): void {
517
531
  throw new BSONError('Invalid Vector: padding must be zero for int8 and float32 vectors');
518
532
  }
519
533
 
534
+ if (datatype === Binary.VECTOR_TYPE.Float32) {
535
+ if (size !== 0 && size - 2 !== 0 && (size - 2) % 4 !== 0) {
536
+ throw new BSONError('Invalid Vector: Float32 vector must contain a multiple of 4 bytes');
537
+ }
538
+ }
539
+
520
540
  if (datatype === Binary.VECTOR_TYPE.PackedBit && padding !== 0 && size === 2) {
521
541
  throw new BSONError(
522
542
  'Invalid Vector: padding must be zero for packed bit vectors that are empty'
@@ -9,29 +9,31 @@ import { NumberUtils } from '../../utils/number_utils';
9
9
  * - `minKey` is set to 255 so unsigned comparisons succeed
10
10
  * - Modify with caution, double check the bundle contains literals
11
11
  */
12
- const enum BSONElementType {
13
- double = 1,
14
- string = 2,
15
- object = 3,
16
- array = 4,
17
- binData = 5,
18
- undefined = 6,
19
- objectId = 7,
20
- bool = 8,
21
- date = 9,
22
- null = 10,
23
- regex = 11,
24
- dbPointer = 12,
25
- javascript = 13,
26
- symbol = 14,
27
- javascriptWithScope = 15,
28
- int = 16,
29
- timestamp = 17,
30
- long = 18,
31
- decimal = 19,
32
- minKey = 255,
33
- maxKey = 127
34
- }
12
+ const BSONElementType = {
13
+ double: 1,
14
+ string: 2,
15
+ object: 3,
16
+ array: 4,
17
+ binData: 5,
18
+ undefined: 6,
19
+ objectId: 7,
20
+ bool: 8,
21
+ date: 9,
22
+ null: 10,
23
+ regex: 11,
24
+ dbPointer: 12,
25
+ javascript: 13,
26
+ symbol: 14,
27
+ javascriptWithScope: 15,
28
+ int: 16,
29
+ timestamp: 17,
30
+ long: 18,
31
+ decimal: 19,
32
+ minKey: 255,
33
+ maxKey: 127
34
+ } as const;
35
+
36
+ type BSONElementType = (typeof BSONElementType)[keyof typeof BSONElementType];
35
37
 
36
38
  /**
37
39
  * @public
@@ -726,8 +726,8 @@ export function serializeInto(
726
726
  if (done) continue;
727
727
 
728
728
  // Get the entry values
729
- const key = entry.value[0];
730
- let value = entry.value[1];
729
+ const key = entry.value ? entry.value[0] : undefined;
730
+ let value = entry.value ? entry.value[1] : undefined;
731
731
 
732
732
  if (typeof value?.toBSON === 'function') {
733
733
  value = value.toBSON();
@@ -83,14 +83,23 @@ export const NumberUtils: NumberUtils = {
83
83
 
84
84
  /** Reads a little-endian 64-bit integer from source */
85
85
  getBigInt64LE(source: Uint8Array, offset: number): bigint {
86
- const lo = NumberUtils.getUint32LE(source, offset);
87
- const hi = NumberUtils.getUint32LE(source, offset + 4);
88
-
89
- /*
90
- eslint-disable-next-line no-restricted-globals
91
- -- This is allowed since this helper should not be called unless bigint features are enabled
92
- */
93
- return (BigInt(hi) << BigInt(32)) + BigInt(lo);
86
+ // eslint-disable-next-line no-restricted-globals
87
+ const hi = BigInt(
88
+ source[offset + 4] +
89
+ source[offset + 5] * 256 +
90
+ source[offset + 6] * 65536 +
91
+ (source[offset + 7] << 24)
92
+ ); // Overflow
93
+
94
+ // eslint-disable-next-line no-restricted-globals
95
+ const lo = BigInt(
96
+ source[offset] +
97
+ source[offset + 1] * 256 +
98
+ source[offset + 2] * 65536 +
99
+ source[offset + 3] * 16777216
100
+ );
101
+ // eslint-disable-next-line no-restricted-globals
102
+ return (hi << BigInt(32)) + lo;
94
103
  },
95
104
 
96
105
  /** Reads a little-endian 64-bit float from source */