bson 6.5.1 → 6.6.1

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/bson.d.ts CHANGED
@@ -140,9 +140,11 @@ declare namespace BSON {
140
140
  BSONError,
141
141
  BSONVersionError,
142
142
  BSONRuntimeError,
143
+ BSONOffsetError,
143
144
  BSONType,
144
145
  EJSON,
145
146
  onDemand,
147
+ OnDemand,
146
148
  Document,
147
149
  CalculateObjectSizeOptions
148
150
  }
@@ -196,10 +198,12 @@ export declare class BSONError extends Error {
196
198
  * An error generated when BSON bytes are invalid.
197
199
  * Reports the offset the parser was able to reach before encountering the error.
198
200
  */
199
- declare class BSONOffsetError extends BSONError {
201
+ export declare class BSONOffsetError extends BSONError {
200
202
  get name(): 'BSONOffsetError';
201
203
  offset: number;
202
- constructor(message: string, offset: number);
204
+ constructor(message: string, offset: number, options?: {
205
+ cause?: unknown;
206
+ });
203
207
  }
204
208
 
205
209
  /**
@@ -325,6 +329,48 @@ export declare class BSONVersionError extends BSONError {
325
329
  constructor();
326
330
  }
327
331
 
332
+ /**
333
+ * @public
334
+ * @experimental
335
+ *
336
+ * A collection of functions that help work with data in a Uint8Array.
337
+ * ByteUtils is configured at load time to use Node.js or Web based APIs for the internal implementations.
338
+ */
339
+ declare type ByteUtils = {
340
+ /** Transforms the input to an instance of Buffer if running on node, otherwise Uint8Array */
341
+ toLocalBufferType: (buffer: Uint8Array | ArrayBufferView | ArrayBuffer) => Uint8Array;
342
+ /** Create empty space of size */
343
+ allocate: (size: number) => Uint8Array;
344
+ /** Create empty space of size, use pooled memory when available */
345
+ allocateUnsafe: (size: number) => Uint8Array;
346
+ /** Check if two Uint8Arrays are deep equal */
347
+ equals: (a: Uint8Array, b: Uint8Array) => boolean;
348
+ /** Check if two Uint8Arrays are deep equal */
349
+ fromNumberArray: (array: number[]) => Uint8Array;
350
+ /** Create a Uint8Array from a base64 string */
351
+ fromBase64: (base64: string) => Uint8Array;
352
+ /** Create a base64 string from bytes */
353
+ toBase64: (buffer: Uint8Array) => string;
354
+ /** **Legacy** binary strings are an outdated method of data transfer. Do not add public API support for interpreting this format */
355
+ fromISO88591: (codePoints: string) => Uint8Array;
356
+ /** **Legacy** binary strings are an outdated method of data transfer. Do not add public API support for interpreting this format */
357
+ toISO88591: (buffer: Uint8Array) => string;
358
+ /** Create a Uint8Array from a hex string */
359
+ fromHex: (hex: string) => Uint8Array;
360
+ /** Create a lowercase hex string from bytes */
361
+ toHex: (buffer: Uint8Array) => string;
362
+ /** Create a string from utf8 code units, fatal=true will throw an error if UTF-8 bytes are invalid, fatal=false will insert replacement characters */
363
+ toUTF8: (buffer: Uint8Array, start: number, end: number, fatal: boolean) => string;
364
+ /** Get the utf8 code unit count from a string if it were to be transformed to utf8 */
365
+ utf8ByteLength: (input: string) => number;
366
+ /** Encode UTF8 bytes generated from `source` string into `destination` at byteOffset. Returns the number of bytes encoded. */
367
+ encodeUTF8Into: (destination: Uint8Array, source: string, byteOffset: number) => number;
368
+ /** Generate a Uint8Array filled with random bytes with byteLength */
369
+ randomBytes: (byteLength: number) => Uint8Array;
370
+ };
371
+
372
+ /* Excluded declaration from this release type: ByteUtils */
373
+
328
374
  /**
329
375
  * Calculate the bson size for a passed in Javascript object.
330
376
  *
@@ -1012,6 +1058,36 @@ export declare interface MinKeyExtended {
1012
1058
  $minKey: 1;
1013
1059
  }
1014
1060
 
1061
+ /**
1062
+ * @experimental
1063
+ * @public
1064
+ *
1065
+ * A collection of functions that get or set various numeric types and bit widths from a Uint8Array.
1066
+ */
1067
+ declare type NumberUtils = {
1068
+ /**
1069
+ * Parses a signed int32 at offset. Throws a `RangeError` if value is negative.
1070
+ */
1071
+ getNonnegativeInt32LE: (source: Uint8Array, offset: number) => number;
1072
+ getInt32LE: (source: Uint8Array, offset: number) => number;
1073
+ getUint32LE: (source: Uint8Array, offset: number) => number;
1074
+ getUint32BE: (source: Uint8Array, offset: number) => number;
1075
+ getBigInt64LE: (source: Uint8Array, offset: number) => bigint;
1076
+ getFloat64LE: (source: Uint8Array, offset: number) => number;
1077
+ setInt32BE: (destination: Uint8Array, offset: number, value: number) => 4;
1078
+ setInt32LE: (destination: Uint8Array, offset: number, value: number) => 4;
1079
+ setBigInt64LE: (destination: Uint8Array, offset: number, value: bigint) => 8;
1080
+ setFloat64LE: (destination: Uint8Array, offset: number, value: number) => 8;
1081
+ };
1082
+
1083
+ /**
1084
+ * Number parsing and serializing utilities.
1085
+ *
1086
+ * @experimental
1087
+ * @public
1088
+ */
1089
+ declare const NumberUtils: NumberUtils;
1090
+
1015
1091
  /**
1016
1092
  * A class representation of the BSON ObjectId type.
1017
1093
  * @public
@@ -1142,12 +1218,11 @@ export declare interface ObjectIdLike {
1142
1218
  *
1143
1219
  * A new set of BSON APIs that are currently experimental and not intended for production use.
1144
1220
  */
1145
- declare type OnDemand = {
1146
- BSONOffsetError: {
1147
- new (message: string, offset: number): BSONOffsetError;
1148
- isBSONError(value: unknown): value is BSONError;
1149
- };
1221
+ export declare type OnDemand = {
1150
1222
  parseToElements: (this: void, bytes: Uint8Array, startOffset?: number) => Iterable<BSONElement>;
1223
+ BSONElement: BSONElement;
1224
+ ByteUtils: ByteUtils;
1225
+ NumberUtils: NumberUtils;
1151
1226
  };
1152
1227
 
1153
1228
  /**
@@ -131,8 +131,8 @@ class BSONOffsetError extends BSONError {
131
131
  get name() {
132
132
  return 'BSONOffsetError';
133
133
  }
134
- constructor(message, offset) {
135
- super(`${message}. offset: ${offset}`);
134
+ constructor(message, offset, options) {
135
+ super(`${message}. offset: ${offset}`, options);
136
136
  this.offset = offset;
137
137
  }
138
138
  }
@@ -534,16 +534,16 @@ class Binary extends BSONValue {
534
534
  return this.position;
535
535
  }
536
536
  toJSON() {
537
- return ByteUtils.toBase64(this.buffer);
537
+ return ByteUtils.toBase64(this.buffer.subarray(0, this.position));
538
538
  }
539
539
  toString(encoding) {
540
540
  if (encoding === 'hex')
541
- return ByteUtils.toHex(this.buffer);
541
+ return ByteUtils.toHex(this.buffer.subarray(0, this.position));
542
542
  if (encoding === 'base64')
543
- return ByteUtils.toBase64(this.buffer);
543
+ return ByteUtils.toBase64(this.buffer.subarray(0, this.position));
544
544
  if (encoding === 'utf8' || encoding === 'utf-8')
545
- return ByteUtils.toUTF8(this.buffer, 0, this.buffer.byteLength, false);
546
- return ByteUtils.toUTF8(this.buffer, 0, this.buffer.byteLength, false);
545
+ return ByteUtils.toUTF8(this.buffer, 0, this.position, false);
546
+ return ByteUtils.toUTF8(this.buffer, 0, this.position, false);
547
547
  }
548
548
  toExtendedJSON(options) {
549
549
  options = options || {};
@@ -2139,6 +2139,15 @@ const FLOAT_BYTES = new Uint8Array(FLOAT.buffer, 0, 8);
2139
2139
  FLOAT[0] = -1;
2140
2140
  const isBigEndian = FLOAT_BYTES[7] === 0;
2141
2141
  const NumberUtils = {
2142
+ getNonnegativeInt32LE(source, offset) {
2143
+ if (source[offset + 3] > 127) {
2144
+ throw new RangeError(`Size cannot be negative at offset: ${offset}`);
2145
+ }
2146
+ return (source[offset] |
2147
+ (source[offset + 1] << 8) |
2148
+ (source[offset + 2] << 16) |
2149
+ (source[offset + 3] << 24));
2150
+ },
2142
2151
  getInt32LE(source, offset) {
2143
2152
  return (source[offset] |
2144
2153
  (source[offset + 1] << 8) |
@@ -4134,13 +4143,12 @@ EJSON.deserialize = EJSONdeserialize;
4134
4143
  Object.freeze(EJSON);
4135
4144
 
4136
4145
  function getSize(source, offset) {
4137
- if (source[offset + 3] > 127) {
4138
- throw new BSONOffsetError('BSON size cannot be negative', offset);
4146
+ try {
4147
+ return NumberUtils.getNonnegativeInt32LE(source, offset);
4148
+ }
4149
+ catch (cause) {
4150
+ throw new BSONOffsetError('BSON size cannot be negative', offset, { cause });
4139
4151
  }
4140
- return (source[offset] |
4141
- (source[offset + 1] << 8) |
4142
- (source[offset + 2] << 16) |
4143
- (source[offset + 3] << 24));
4144
4152
  }
4145
4153
  function findNull(bytes, offset) {
4146
4154
  let nullTerminatorOffset = offset;
@@ -4152,6 +4160,7 @@ function findNull(bytes, offset) {
4152
4160
  return nullTerminatorOffset;
4153
4161
  }
4154
4162
  function parseToElements(bytes, startOffset = 0) {
4163
+ startOffset ??= 0;
4155
4164
  if (bytes.length < 5) {
4156
4165
  throw new BSONOffsetError(`Input must be at least 5 bytes, got ${bytes.length} bytes`, startOffset);
4157
4166
  }
@@ -4177,7 +4186,10 @@ function parseToElements(bytes, startOffset = 0) {
4177
4186
  const nameLength = findNull(bytes, offset) - nameOffset;
4178
4187
  offset += nameLength + 1;
4179
4188
  let length;
4180
- if (type === 1 || type === 18 || type === 9 || type === 17) {
4189
+ if (type === 1 ||
4190
+ type === 18 ||
4191
+ type === 9 ||
4192
+ type === 17) {
4181
4193
  length = 8;
4182
4194
  }
4183
4195
  else if (type === 16) {
@@ -4192,13 +4204,18 @@ function parseToElements(bytes, startOffset = 0) {
4192
4204
  else if (type === 8) {
4193
4205
  length = 1;
4194
4206
  }
4195
- else if (type === 10 || type === 6 || type === 127 || type === 255) {
4207
+ else if (type === 10 ||
4208
+ type === 6 ||
4209
+ type === 127 ||
4210
+ type === 255) {
4196
4211
  length = 0;
4197
4212
  }
4198
4213
  else if (type === 11) {
4199
4214
  length = findNull(bytes, findNull(bytes, offset) + 1) + 1 - offset;
4200
4215
  }
4201
- else if (type === 3 || type === 4 || type === 15) {
4216
+ else if (type === 3 ||
4217
+ type === 4 ||
4218
+ type === 15) {
4202
4219
  length = getSize(bytes, offset);
4203
4220
  }
4204
4221
  else if (type === 2 ||
@@ -4228,7 +4245,8 @@ function parseToElements(bytes, startOffset = 0) {
4228
4245
 
4229
4246
  const onDemand = Object.create(null);
4230
4247
  onDemand.parseToElements = parseToElements;
4231
- onDemand.BSONOffsetError = BSONOffsetError;
4248
+ onDemand.ByteUtils = ByteUtils;
4249
+ onDemand.NumberUtils = NumberUtils;
4232
4250
  Object.freeze(onDemand);
4233
4251
 
4234
4252
  const MAXSIZE = 1024 * 1024 * 17;
@@ -4285,6 +4303,7 @@ function deserializeStream(data, startIndex, numberOfDocuments, documents, docSt
4285
4303
  var bson = /*#__PURE__*/Object.freeze({
4286
4304
  __proto__: null,
4287
4305
  BSONError: BSONError,
4306
+ BSONOffsetError: BSONOffsetError,
4288
4307
  BSONRegExp: BSONRegExp,
4289
4308
  BSONRuntimeError: BSONRuntimeError,
4290
4309
  BSONSymbol: BSONSymbol,
@@ -4315,6 +4334,7 @@ setInternalBufferSize: setInternalBufferSize
4315
4334
 
4316
4335
  exports.BSON = bson;
4317
4336
  exports.BSONError = BSONError;
4337
+ exports.BSONOffsetError = BSONOffsetError;
4318
4338
  exports.BSONRegExp = BSONRegExp;
4319
4339
  exports.BSONRuntimeError = BSONRuntimeError;
4320
4340
  exports.BSONSymbol = BSONSymbol;