bson 6.3.0 → 6.5.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.3.0",
17
+ "version": "6.5.0",
18
18
  "author": {
19
19
  "name": "The MongoDB NodeJS Team",
20
20
  "email": "dbx-node@mongodb.com"
@@ -27,39 +27,39 @@
27
27
  },
28
28
  "devDependencies": {
29
29
  "@istanbuljs/nyc-config-typescript": "^1.0.2",
30
- "@microsoft/api-extractor": "^7.38.4",
31
- "@octokit/core": "^4.2.4",
30
+ "@microsoft/api-extractor": "^7.40.5",
31
+ "@octokit/core": "^5.1.0",
32
32
  "@rollup/plugin-node-resolve": "^15.2.3",
33
- "@rollup/plugin-typescript": "^11.1.5",
33
+ "@rollup/plugin-typescript": "^11.1.6",
34
34
  "@types/chai": "^4.3.11",
35
35
  "@types/mocha": "^10.0.6",
36
- "@types/node": "^18.19.2",
37
- "@types/sinon": "^10.0.20",
36
+ "@types/node": "^20.11.19",
37
+ "@types/sinon": "^17.0.3",
38
38
  "@types/sinon-chai": "^3.2.12",
39
- "@typescript-eslint/eslint-plugin": "^5.62.0",
40
- "@typescript-eslint/parser": "^5.62.0",
39
+ "@typescript-eslint/eslint-plugin": "^7.0.2",
40
+ "@typescript-eslint/parser": "^7.0.2",
41
41
  "benchmark": "^2.1.4",
42
42
  "chai": "^4.3.10",
43
43
  "chalk": "^5.3.0",
44
44
  "dbx-js-tools": "github:mongodb-js/dbx-js-tools",
45
- "eslint": "^8.55.0",
46
- "eslint-config-prettier": "^8.10.0",
45
+ "eslint": "^8.56.0",
46
+ "eslint-config-prettier": "^9.1.0",
47
47
  "eslint-plugin-no-bigint-usage": "file:etc/eslint/no-bigint-usage",
48
- "eslint-plugin-prettier": "^4.2.1",
48
+ "eslint-plugin-prettier": "^5.1.3",
49
49
  "eslint-plugin-tsdoc": "^0.2.17",
50
- "magic-string": "^0.30.5",
51
- "mocha": "10.2.0",
50
+ "magic-string": "^0.30.7",
51
+ "mocha": "10.3.0",
52
52
  "node-fetch": "^3.3.2",
53
53
  "nyc": "^15.1.0",
54
- "prettier": "^2.8.8",
55
- "rollup": "^3.29.4",
56
- "sinon": "^15.2.0",
54
+ "prettier": "^3.2.5",
55
+ "rollup": "^4.12.0",
56
+ "sinon": "^17.0.1",
57
57
  "sinon-chai": "^3.7.0",
58
58
  "source-map-support": "^0.5.21",
59
59
  "standard-version": "^9.5.0",
60
60
  "tar": "^6.2.0",
61
- "ts-node": "^10.9.1",
62
- "tsd": "^0.28.1",
61
+ "ts-node": "^10.9.2",
62
+ "tsd": "^0.30.5",
63
63
  "typescript": "^5.0.4",
64
64
  "typescript-cached-transpile": "0.0.6",
65
65
  "uuid": "^9.0.1"
package/src/binary.ts CHANGED
@@ -56,6 +56,8 @@ export class Binary extends BSONValue {
56
56
  static readonly SUBTYPE_ENCRYPTED = 6;
57
57
  /** Column BSON type */
58
58
  static readonly SUBTYPE_COLUMN = 7;
59
+ /** Sensitive BSON type */
60
+ static readonly SUBTYPE_SENSITIVE = 8;
59
61
  /** User BSON type */
60
62
  static readonly SUBTYPE_USER_DEFINED = 128;
61
63
 
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';
@@ -53,6 +54,7 @@ export { BSONValue } from './bson_value';
53
54
  export { BSONError, BSONVersionError, BSONRuntimeError } from './error';
54
55
  export { BSONType } from './constants';
55
56
  export { EJSON } from './extended_json';
57
+ export { onDemand } from './parser/on_demand/index';
56
58
 
57
59
  /** @public */
58
60
  export interface Document {
@@ -115,7 +117,7 @@ export function serialize(object: Document, options: SerializeOptions = {}): Uin
115
117
  );
116
118
 
117
119
  // Create the final buffer
118
- const finishedBuffer = ByteUtils.allocate(serializationIndex);
120
+ const finishedBuffer = ByteUtils.allocateUnsafe(serializationIndex);
119
121
 
120
122
  // Copy into the finished buffer
121
123
  finishedBuffer.set(buffer.subarray(0, serializationIndex), 0);
@@ -232,11 +234,7 @@ export function deserializeStream(
232
234
  // Loop over all documents
233
235
  for (let i = 0; i < numberOfDocuments; i++) {
234
236
  // 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);
237
+ const size = NumberUtils.getInt32LE(bufferData, index);
240
238
  // Update options with index
241
239
  internalOptions.index = index;
242
240
  // Parse the document at this point
package/src/constants.ts CHANGED
@@ -109,6 +109,9 @@ export const BSON_BINARY_SUBTYPE_ENCRYPTED = 6;
109
109
  /** Column BSON type @internal */
110
110
  export const BSON_BINARY_SUBTYPE_COLUMN = 7;
111
111
 
112
+ /** Sensitive BSON type @internal */
113
+ export const BSON_BINARY_SUBTYPE_SENSITIVE = 8;
114
+
112
115
  /** Binary User Defined Type @internal */
113
116
  export const BSON_BINARY_SUBTYPE_USER_DEFINED = 128;
114
117
 
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
@@ -81,3 +81,25 @@ export class BSONRuntimeError extends BSONError {
81
81
  super(message);
82
82
  }
83
83
  }
84
+
85
+ /**
86
+ * @public
87
+ * @category Error
88
+ *
89
+ * @experimental
90
+ *
91
+ * An error generated when BSON bytes are invalid.
92
+ * Reports the offset the parser was able to reach before encountering the error.
93
+ */
94
+ export class BSONOffsetError extends BSONError {
95
+ public get name(): 'BSONOffsetError' {
96
+ return 'BSONOffsetError';
97
+ }
98
+
99
+ public offset: number;
100
+
101
+ constructor(message: string, offset: number) {
102
+ super(`${message}. offset: ${offset}`);
103
+ this.offset = offset;
104
+ }
105
+ }
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,7 +38,7 @@ 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
 
@@ -108,13 +107,13 @@ export class ObjectId extends BSONValue {
108
107
  if (workingId == null || typeof workingId === 'number') {
109
108
  // The most common use case (blank id, new objectId instance)
110
109
  // Generate a new id
111
- this[kId] = ObjectId.generate(typeof workingId === 'number' ? workingId : undefined);
110
+ this.buffer = ObjectId.generate(typeof workingId === 'number' ? workingId : undefined);
112
111
  } else if (ArrayBuffer.isView(workingId) && workingId.byteLength === 12) {
113
112
  // If intstanceof matches we can escape calling ensure buffer in Node.js environments
114
- this[kId] = ByteUtils.toLocalBufferType(workingId);
113
+ this.buffer = ByteUtils.toLocalBufferType(workingId);
115
114
  } else if (typeof workingId === 'string') {
116
115
  if (workingId.length === 24 && checkForHexRegExp.test(workingId)) {
117
- this[kId] = ByteUtils.fromHex(workingId);
116
+ this.buffer = ByteUtils.fromHex(workingId);
118
117
  } else {
119
118
  throw new BSONError(
120
119
  'input must be a 24 character hex string, 12 byte Uint8Array, or an integer'
@@ -134,11 +133,11 @@ export class ObjectId extends BSONValue {
134
133
  * @readonly
135
134
  */
136
135
  get id(): Uint8Array {
137
- return this[kId];
136
+ return this.buffer;
138
137
  }
139
138
 
140
139
  set id(value: Uint8Array) {
141
- this[kId] = value;
140
+ this.buffer = value;
142
141
  if (ObjectId.cacheHexString) {
143
142
  this.__id = ByteUtils.toHex(value);
144
143
  }
@@ -178,10 +177,10 @@ export class ObjectId extends BSONValue {
178
177
  }
179
178
 
180
179
  const inc = ObjectId.getInc();
181
- const buffer = ByteUtils.allocate(12);
180
+ const buffer = ByteUtils.allocateUnsafe(12);
182
181
 
183
182
  // 4-byte timestamp
184
- BSONDataView.fromUint8Array(buffer).setUint32(0, time, false);
183
+ NumberUtils.setInt32BE(buffer, 0, time);
185
184
 
186
185
  // set PROCESS_UNIQUE if yet not initialized
187
186
  if (PROCESS_UNIQUE === null) {
@@ -240,7 +239,9 @@ export class ObjectId extends BSONValue {
240
239
  }
241
240
 
242
241
  if (ObjectId.is(otherId)) {
243
- 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
+ );
244
245
  }
245
246
 
246
247
  if (typeof otherId === 'string') {
@@ -259,7 +260,7 @@ export class ObjectId extends BSONValue {
259
260
  /** Returns the generation date (accurate up to the second) that this ID was generated. */
260
261
  getTimestamp(): Date {
261
262
  const timestamp = new Date();
262
- const time = BSONDataView.fromUint8Array(this.id).getUint32(0, false);
263
+ const time = NumberUtils.getUint32BE(this.buffer, 0);
263
264
  timestamp.setTime(Math.floor(time) * 1000);
264
265
  return timestamp;
265
266
  }
@@ -269,15 +270,33 @@ export class ObjectId extends BSONValue {
269
270
  return new ObjectId();
270
271
  }
271
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
+
272
290
  /**
273
291
  * Creates an ObjectId from a second based number, with the rest of the ObjectId zeroed out. Used for comparisons or sorting the ObjectId.
274
292
  *
275
293
  * @param time - an integer number representing a number of seconds.
276
294
  */
277
295
  static createFromTime(time: number): ObjectId {
278
- 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;
279
298
  // Encode time into first 4 bytes
280
- BSONDataView.fromUint8Array(buffer).setUint32(0, time, false);
299
+ NumberUtils.setInt32BE(buffer, 0, time);
281
300
  // Return the new objectId
282
301
  return new ObjectId(buffer);
283
302
  }