bson 6.5.0 → 6.6.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/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) |
@@ -4128,13 +4137,12 @@ EJSON.deserialize = EJSONdeserialize;
4128
4137
  Object.freeze(EJSON);
4129
4138
 
4130
4139
  function getSize(source, offset) {
4131
- if (source[offset + 3] > 127) {
4132
- throw new BSONOffsetError('BSON size cannot be negative', offset);
4140
+ try {
4141
+ return NumberUtils.getNonnegativeInt32LE(source, offset);
4142
+ }
4143
+ catch (cause) {
4144
+ throw new BSONOffsetError('BSON size cannot be negative', offset, { cause });
4133
4145
  }
4134
- return (source[offset] |
4135
- (source[offset + 1] << 8) |
4136
- (source[offset + 2] << 16) |
4137
- (source[offset + 3] << 24));
4138
4146
  }
4139
4147
  function findNull(bytes, offset) {
4140
4148
  let nullTerminatorOffset = offset;
@@ -4146,6 +4154,7 @@ function findNull(bytes, offset) {
4146
4154
  return nullTerminatorOffset;
4147
4155
  }
4148
4156
  function parseToElements(bytes, startOffset = 0) {
4157
+ startOffset ??= 0;
4149
4158
  if (bytes.length < 5) {
4150
4159
  throw new BSONOffsetError(`Input must be at least 5 bytes, got ${bytes.length} bytes`, startOffset);
4151
4160
  }
@@ -4171,7 +4180,10 @@ function parseToElements(bytes, startOffset = 0) {
4171
4180
  const nameLength = findNull(bytes, offset) - nameOffset;
4172
4181
  offset += nameLength + 1;
4173
4182
  let length;
4174
- if (type === 1 || type === 18 || type === 9 || type === 17) {
4183
+ if (type === 1 ||
4184
+ type === 18 ||
4185
+ type === 9 ||
4186
+ type === 17) {
4175
4187
  length = 8;
4176
4188
  }
4177
4189
  else if (type === 16) {
@@ -4186,13 +4198,18 @@ function parseToElements(bytes, startOffset = 0) {
4186
4198
  else if (type === 8) {
4187
4199
  length = 1;
4188
4200
  }
4189
- else if (type === 10 || type === 6 || type === 127 || type === 255) {
4201
+ else if (type === 10 ||
4202
+ type === 6 ||
4203
+ type === 127 ||
4204
+ type === 255) {
4190
4205
  length = 0;
4191
4206
  }
4192
4207
  else if (type === 11) {
4193
4208
  length = findNull(bytes, findNull(bytes, offset) + 1) + 1 - offset;
4194
4209
  }
4195
- else if (type === 3 || type === 4 || type === 15) {
4210
+ else if (type === 3 ||
4211
+ type === 4 ||
4212
+ type === 15) {
4196
4213
  length = getSize(bytes, offset);
4197
4214
  }
4198
4215
  else if (type === 2 ||
@@ -4222,7 +4239,8 @@ function parseToElements(bytes, startOffset = 0) {
4222
4239
 
4223
4240
  const onDemand = Object.create(null);
4224
4241
  onDemand.parseToElements = parseToElements;
4225
- onDemand.BSONOffsetError = BSONOffsetError;
4242
+ onDemand.ByteUtils = ByteUtils;
4243
+ onDemand.NumberUtils = NumberUtils;
4226
4244
  Object.freeze(onDemand);
4227
4245
 
4228
4246
  const MAXSIZE = 1024 * 1024 * 17;
@@ -4279,6 +4297,7 @@ function deserializeStream(data, startIndex, numberOfDocuments, documents, docSt
4279
4297
  var bson = /*#__PURE__*/Object.freeze({
4280
4298
  __proto__: null,
4281
4299
  BSONError: BSONError,
4300
+ BSONOffsetError: BSONOffsetError,
4282
4301
  BSONRegExp: BSONRegExp,
4283
4302
  BSONRuntimeError: BSONRuntimeError,
4284
4303
  BSONSymbol: BSONSymbol,
@@ -4309,6 +4328,7 @@ setInternalBufferSize: setInternalBufferSize
4309
4328
 
4310
4329
  exports.BSON = bson;
4311
4330
  exports.BSONError = BSONError;
4331
+ exports.BSONOffsetError = BSONOffsetError;
4312
4332
  exports.BSONRegExp = BSONRegExp;
4313
4333
  exports.BSONRuntimeError = BSONRuntimeError;
4314
4334
  exports.BSONSymbol = BSONSymbol;