bson 6.2.0 → 6.4.0

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.2.0",
17
+ "version": "6.4.0",
18
18
  "author": {
19
19
  "name": "The MongoDB NodeJS Team",
20
20
  "email": "dbx-node@mongodb.com"
@@ -27,41 +27,42 @@
27
27
  },
28
28
  "devDependencies": {
29
29
  "@istanbuljs/nyc-config-typescript": "^1.0.2",
30
- "@microsoft/api-extractor": "^7.36.4",
31
- "@octokit/core": "^4.2.4",
32
- "@rollup/plugin-node-resolve": "^15.1.0",
33
- "@rollup/plugin-typescript": "^11.1.2",
34
- "@types/chai": "^4.3.5",
35
- "@types/mocha": "^10.0.1",
36
- "@types/node": "^18.17.3",
37
- "@types/sinon": "^10.0.16",
38
- "@types/sinon-chai": "^3.2.9",
39
- "@typescript-eslint/eslint-plugin": "^5.62.0",
40
- "@typescript-eslint/parser": "^5.62.0",
30
+ "@microsoft/api-extractor": "^7.40.5",
31
+ "@octokit/core": "^5.1.0",
32
+ "@rollup/plugin-node-resolve": "^15.2.3",
33
+ "@rollup/plugin-typescript": "^11.1.6",
34
+ "@types/chai": "^4.3.11",
35
+ "@types/mocha": "^10.0.6",
36
+ "@types/node": "^20.11.19",
37
+ "@types/sinon": "^17.0.3",
38
+ "@types/sinon-chai": "^3.2.12",
39
+ "@typescript-eslint/eslint-plugin": "^7.0.2",
40
+ "@typescript-eslint/parser": "^7.0.2",
41
41
  "benchmark": "^2.1.4",
42
- "chai": "^4.3.7",
42
+ "chai": "^4.3.10",
43
43
  "chalk": "^5.3.0",
44
- "eslint": "^8.46.0",
45
- "eslint-config-prettier": "^8.10.0",
44
+ "dbx-js-tools": "github:mongodb-js/dbx-js-tools",
45
+ "eslint": "^8.56.0",
46
+ "eslint-config-prettier": "^9.1.0",
46
47
  "eslint-plugin-no-bigint-usage": "file:etc/eslint/no-bigint-usage",
47
- "eslint-plugin-prettier": "^4.2.1",
48
+ "eslint-plugin-prettier": "^5.1.3",
48
49
  "eslint-plugin-tsdoc": "^0.2.17",
49
- "magic-string": "^0.30.2",
50
- "mocha": "10.2.0",
50
+ "magic-string": "^0.30.7",
51
+ "mocha": "10.3.0",
51
52
  "node-fetch": "^3.3.2",
52
53
  "nyc": "^15.1.0",
53
- "prettier": "^2.8.8",
54
- "rollup": "^3.27.2",
55
- "sinon": "^15.2.0",
54
+ "prettier": "^3.2.5",
55
+ "rollup": "^4.12.0",
56
+ "sinon": "^17.0.1",
56
57
  "sinon-chai": "^3.7.0",
57
58
  "source-map-support": "^0.5.21",
58
59
  "standard-version": "^9.5.0",
59
- "tar": "^6.1.15",
60
- "ts-node": "^10.9.1",
61
- "tsd": "^0.28.1",
60
+ "tar": "^6.2.0",
61
+ "ts-node": "^10.9.2",
62
+ "tsd": "^0.30.5",
62
63
  "typescript": "^5.0.4",
63
64
  "typescript-cached-transpile": "0.0.6",
64
- "uuid": "^9.0.0"
65
+ "uuid": "^9.0.1"
65
66
  },
66
67
  "tsd": {
67
68
  "directory": "test/types",
@@ -103,7 +104,9 @@
103
104
  "check:tsd": "npm run build:dts && tsd",
104
105
  "check:web": "WEB=true mocha test/node",
105
106
  "check:web-no-bigint": "WEB=true NO_BIGINT=true mocha test/node",
106
- "check:bench": "cd test/bench && npx tsc && node ./lib/index.js && mv benchmarks.json ../../.",
107
+ "check:granular-bench": "npm run build:bench && node ./test/bench/etc/run_granular_benchmarks.js",
108
+ "check:spec-bench": "npm run build:bench && node ./test/bench/lib/spec/bsonBench.js",
109
+ "build:bench": "cd test/bench && npx tsc",
107
110
  "build:ts": "node ./node_modules/typescript/bin/tsc",
108
111
  "build:dts": "npm run build:ts && api-extractor run --typescript-compiler-folder node_modules/typescript --local && node etc/clean_definition_files.cjs",
109
112
  "build:bundle": "rollup -c rollup.config.mjs",
package/src/binary.ts CHANGED
@@ -191,8 +191,8 @@ export class Binary extends BSONValue {
191
191
  if (encoding === 'hex') return ByteUtils.toHex(this.buffer);
192
192
  if (encoding === 'base64') return ByteUtils.toBase64(this.buffer);
193
193
  if (encoding === 'utf8' || encoding === 'utf-8')
194
- return ByteUtils.toUTF8(this.buffer, 0, this.buffer.byteLength);
195
- return ByteUtils.toUTF8(this.buffer, 0, this.buffer.byteLength);
194
+ return ByteUtils.toUTF8(this.buffer, 0, this.buffer.byteLength, false);
195
+ return ByteUtils.toUTF8(this.buffer, 0, this.buffer.byteLength, false);
196
196
  }
197
197
 
198
198
  /** @internal */
package/src/bson.ts CHANGED
@@ -16,6 +16,7 @@ import { BSONRegExp } from './regexp';
16
16
  import { BSONSymbol } from './symbol';
17
17
  import { Timestamp } from './timestamp';
18
18
  import { ByteUtils } from './utils/byte_utils';
19
+ import { NumberUtils } from './utils/number_utils';
19
20
  export type { UUIDExtended, BinaryExtended, BinaryExtendedLegacy, BinarySequence } from './binary';
20
21
  export type { CodeExtended } from './code';
21
22
  export type { DBRefLike } from './db_ref';
@@ -115,7 +116,7 @@ export function serialize(object: Document, options: SerializeOptions = {}): Uin
115
116
  );
116
117
 
117
118
  // Create the final buffer
118
- const finishedBuffer = ByteUtils.allocate(serializationIndex);
119
+ const finishedBuffer = ByteUtils.allocateUnsafe(serializationIndex);
119
120
 
120
121
  // Copy into the finished buffer
121
122
  finishedBuffer.set(buffer.subarray(0, serializationIndex), 0);
@@ -232,11 +233,7 @@ export function deserializeStream(
232
233
  // Loop over all documents
233
234
  for (let i = 0; i < numberOfDocuments; i++) {
234
235
  // Find size of the document
235
- const size =
236
- bufferData[index] |
237
- (bufferData[index + 1] << 8) |
238
- (bufferData[index + 2] << 16) |
239
- (bufferData[index + 3] << 24);
236
+ const size = NumberUtils.getInt32LE(bufferData, index);
240
237
  // Update options with index
241
238
  internalOptions.index = index;
242
239
  // Parse the document at this point
package/src/db_ref.ts CHANGED
@@ -51,7 +51,6 @@ export class DBRef extends BSONValue {
51
51
  const parts = collection.split('.');
52
52
  if (parts.length === 2) {
53
53
  db = parts.shift();
54
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
55
54
  collection = parts.shift()!;
56
55
  }
57
56
 
package/src/decimal128.ts CHANGED
@@ -591,7 +591,7 @@ export class Decimal128 extends BSONValue {
591
591
  }
592
592
 
593
593
  // Encode into a buffer
594
- const buffer = ByteUtils.allocate(16);
594
+ const buffer = ByteUtils.allocateUnsafe(16);
595
595
  index = 0;
596
596
 
597
597
  // Encode the low 64 bits of the decimal
package/src/error.ts CHANGED
@@ -4,7 +4,7 @@ import { BSON_MAJOR_VERSION } from './constants';
4
4
  * @public
5
5
  * @category Error
6
6
  *
7
- * `BSONError` objects are thrown when BSON ecounters an error.
7
+ * `BSONError` objects are thrown when BSON encounters an error.
8
8
  *
9
9
  * This is the parent class for all the other errors thrown by this library.
10
10
  */
@@ -23,8 +23,8 @@ export class BSONError extends Error {
23
23
  return 'BSONError';
24
24
  }
25
25
 
26
- constructor(message: string) {
27
- super(message);
26
+ constructor(message: string, options?: { cause?: unknown }) {
27
+ super(message, options);
28
28
  }
29
29
 
30
30
  /**
package/src/objectid.ts CHANGED
@@ -1,7 +1,8 @@
1
1
  import { BSONValue } from './bson_value';
2
2
  import { BSONError } from './error';
3
3
  import { type InspectFn, defaultInspect } from './parser/utils';
4
- import { BSONDataView, ByteUtils } from './utils/byte_utils';
4
+ import { ByteUtils } from './utils/byte_utils';
5
+ import { NumberUtils } from './utils/number_utils';
5
6
 
6
7
  // Regular expression that checks for hex value
7
8
  const checkForHexRegExp = new RegExp('^[0-9a-fA-F]{24}$');
@@ -21,8 +22,6 @@ export interface ObjectIdExtended {
21
22
  $oid: string;
22
23
  }
23
24
 
24
- const kId = Symbol('id');
25
-
26
25
  /**
27
26
  * A class representation of the BSON ObjectId type.
28
27
  * @public
@@ -39,14 +38,53 @@ export class ObjectId extends BSONValue {
39
38
  static cacheHexString: boolean;
40
39
 
41
40
  /** ObjectId Bytes @internal */
42
- private [kId]!: Uint8Array;
41
+ private buffer!: Uint8Array;
43
42
  /** ObjectId hexString cache @internal */
44
43
  private __id?: string;
45
44
 
46
45
  /**
47
- * Create an ObjectId type
46
+ * Create ObjectId from a number.
47
+ *
48
+ * @param inputId - A number.
49
+ * @deprecated Instead, use `static createFromTime()` to set a numeric value for the new ObjectId.
50
+ */
51
+ constructor(inputId: number);
52
+ /**
53
+ * Create ObjectId from a 24 character hex string.
54
+ *
55
+ * @param inputId - A 24 character hex string.
56
+ */
57
+ constructor(inputId: string);
58
+ /**
59
+ * Create ObjectId from the BSON ObjectId type.
60
+ *
61
+ * @param inputId - The BSON ObjectId type.
62
+ */
63
+ constructor(inputId: ObjectId);
64
+ /**
65
+ * Create ObjectId from the object type that has the toHexString method.
66
+ *
67
+ * @param inputId - The ObjectIdLike type.
68
+ */
69
+ constructor(inputId: ObjectIdLike);
70
+ /**
71
+ * Create ObjectId from a 12 byte binary Buffer.
72
+ *
73
+ * @param inputId - A 12 byte binary Buffer.
74
+ */
75
+ constructor(inputId: Uint8Array);
76
+ /** To generate a new ObjectId, use ObjectId() with no argument. */
77
+ constructor();
78
+ /**
79
+ * Implementation overload.
80
+ *
81
+ * @param inputId - All input types that are used in the constructor implementation.
82
+ */
83
+ constructor(inputId?: string | number | ObjectId | ObjectIdLike | Uint8Array);
84
+ /**
85
+ * Create a new ObjectId.
48
86
  *
49
- * @param inputId - Can be a 24 character hex string, 12 byte binary Buffer, or a number.
87
+ * @param inputId - An input value to create a new ObjectId from.
50
88
  */
51
89
  constructor(inputId?: string | number | ObjectId | ObjectIdLike | Uint8Array) {
52
90
  super();
@@ -65,17 +103,17 @@ export class ObjectId extends BSONValue {
65
103
  workingId = inputId;
66
104
  }
67
105
 
68
- // the following cases use workingId to construct an ObjectId
106
+ // The following cases use workingId to construct an ObjectId
69
107
  if (workingId == null || typeof workingId === 'number') {
70
108
  // The most common use case (blank id, new objectId instance)
71
109
  // Generate a new id
72
- this[kId] = ObjectId.generate(typeof workingId === 'number' ? workingId : undefined);
110
+ this.buffer = ObjectId.generate(typeof workingId === 'number' ? workingId : undefined);
73
111
  } else if (ArrayBuffer.isView(workingId) && workingId.byteLength === 12) {
74
112
  // If intstanceof matches we can escape calling ensure buffer in Node.js environments
75
- this[kId] = ByteUtils.toLocalBufferType(workingId);
113
+ this.buffer = ByteUtils.toLocalBufferType(workingId);
76
114
  } else if (typeof workingId === 'string') {
77
115
  if (workingId.length === 24 && checkForHexRegExp.test(workingId)) {
78
- this[kId] = ByteUtils.fromHex(workingId);
116
+ this.buffer = ByteUtils.fromHex(workingId);
79
117
  } else {
80
118
  throw new BSONError(
81
119
  'input must be a 24 character hex string, 12 byte Uint8Array, or an integer'
@@ -95,11 +133,11 @@ export class ObjectId extends BSONValue {
95
133
  * @readonly
96
134
  */
97
135
  get id(): Uint8Array {
98
- return this[kId];
136
+ return this.buffer;
99
137
  }
100
138
 
101
139
  set id(value: Uint8Array) {
102
- this[kId] = value;
140
+ this.buffer = value;
103
141
  if (ObjectId.cacheHexString) {
104
142
  this.__id = ByteUtils.toHex(value);
105
143
  }
@@ -139,10 +177,10 @@ export class ObjectId extends BSONValue {
139
177
  }
140
178
 
141
179
  const inc = ObjectId.getInc();
142
- const buffer = ByteUtils.allocate(12);
180
+ const buffer = ByteUtils.allocateUnsafe(12);
143
181
 
144
182
  // 4-byte timestamp
145
- BSONDataView.fromUint8Array(buffer).setUint32(0, time, false);
183
+ NumberUtils.setInt32BE(buffer, 0, time);
146
184
 
147
185
  // set PROCESS_UNIQUE if yet not initialized
148
186
  if (PROCESS_UNIQUE === null) {
@@ -201,7 +239,9 @@ export class ObjectId extends BSONValue {
201
239
  }
202
240
 
203
241
  if (ObjectId.is(otherId)) {
204
- return this[kId][11] === otherId[kId][11] && ByteUtils.equals(this[kId], otherId[kId]);
242
+ return (
243
+ this.buffer[11] === otherId.buffer[11] && ByteUtils.equals(this.buffer, otherId.buffer)
244
+ );
205
245
  }
206
246
 
207
247
  if (typeof otherId === 'string') {
@@ -220,7 +260,7 @@ export class ObjectId extends BSONValue {
220
260
  /** Returns the generation date (accurate up to the second) that this ID was generated. */
221
261
  getTimestamp(): Date {
222
262
  const timestamp = new Date();
223
- const time = BSONDataView.fromUint8Array(this.id).getUint32(0, false);
263
+ const time = NumberUtils.getUint32BE(this.buffer, 0);
224
264
  timestamp.setTime(Math.floor(time) * 1000);
225
265
  return timestamp;
226
266
  }
@@ -230,15 +270,33 @@ export class ObjectId extends BSONValue {
230
270
  return new ObjectId();
231
271
  }
232
272
 
273
+ /** @internal */
274
+ serializeInto(uint8array: Uint8Array, index: number): 12 {
275
+ uint8array[index] = this.buffer[0];
276
+ uint8array[index + 1] = this.buffer[1];
277
+ uint8array[index + 2] = this.buffer[2];
278
+ uint8array[index + 3] = this.buffer[3];
279
+ uint8array[index + 4] = this.buffer[4];
280
+ uint8array[index + 5] = this.buffer[5];
281
+ uint8array[index + 6] = this.buffer[6];
282
+ uint8array[index + 7] = this.buffer[7];
283
+ uint8array[index + 8] = this.buffer[8];
284
+ uint8array[index + 9] = this.buffer[9];
285
+ uint8array[index + 10] = this.buffer[10];
286
+ uint8array[index + 11] = this.buffer[11];
287
+ return 12;
288
+ }
289
+
233
290
  /**
234
291
  * Creates an ObjectId from a second based number, with the rest of the ObjectId zeroed out. Used for comparisons or sorting the ObjectId.
235
292
  *
236
293
  * @param time - an integer number representing a number of seconds.
237
294
  */
238
295
  static createFromTime(time: number): ObjectId {
239
- const buffer = ByteUtils.fromNumberArray([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
296
+ const buffer = ByteUtils.allocate(12);
297
+ for (let i = 11; i >= 4; i--) buffer[i] = 0;
240
298
  // Encode time into first 4 bytes
241
- BSONDataView.fromUint8Array(buffer).setUint32(0, time, false);
299
+ NumberUtils.setInt32BE(buffer, 0, time);
242
300
  // Return the new objectId
243
301
  return new ObjectId(buffer);
244
302
  }