bson 7.0.0 → 7.1.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
@@ -236,6 +236,8 @@ declare namespace BSON {
236
236
  MaxKey,
237
237
  BSONRegExp,
238
238
  Decimal128,
239
+ NumberUtils,
240
+ ByteUtils,
239
241
  BSONValue,
240
242
  bsonType,
241
243
  BSONTypeTag,
@@ -450,19 +452,27 @@ export declare class BSONVersionError extends BSONError {
450
452
  * A collection of functions that help work with data in a Uint8Array.
451
453
  * ByteUtils is configured at load time to use Node.js or Web based APIs for the internal implementations.
452
454
  */
453
- declare type ByteUtils = {
455
+ export declare type ByteUtils = {
456
+ /** Checks if the given value is a Uint8Array. */
457
+ isUint8Array: (value: unknown) => value is Uint8Array;
454
458
  /** Transforms the input to an instance of Buffer if running on node, otherwise Uint8Array */
455
459
  toLocalBufferType: (buffer: Uint8Array | ArrayBufferView | ArrayBuffer) => Uint8Array;
456
460
  /** Create empty space of size */
457
461
  allocate: (size: number) => Uint8Array;
458
462
  /** Create empty space of size, use pooled memory when available */
459
463
  allocateUnsafe: (size: number) => Uint8Array;
464
+ /** Compare 2 Uint8Arrays lexicographically */
465
+ compare: (buffer1: Uint8Array, buffer2: Uint8Array) => -1 | 0 | 1;
466
+ /** Concatenating all the Uint8Arrays in new Uint8Array. */
467
+ concat: (list: Uint8Array[]) => Uint8Array;
460
468
  /** Check if two Uint8Arrays are deep equal */
461
469
  equals: (a: Uint8Array, b: Uint8Array) => boolean;
462
- /** Check if two Uint8Arrays are deep equal */
470
+ /** Create a Uint8Array from an array of numbers */
463
471
  fromNumberArray: (array: number[]) => Uint8Array;
464
472
  /** Create a Uint8Array from a base64 string */
465
473
  fromBase64: (base64: string) => Uint8Array;
474
+ /** Create a Uint8Array from a UTF8 string */
475
+ fromUTF8: (utf8: string) => Uint8Array;
466
476
  /** Create a base64 string from bytes */
467
477
  toBase64: (buffer: Uint8Array) => string;
468
478
  /** **Legacy** binary strings are an outdated method of data transfer. Do not add public API support for interpreting this format */
@@ -485,7 +495,16 @@ declare type ByteUtils = {
485
495
  swap32: (buffer: Uint8Array) => Uint8Array;
486
496
  };
487
497
 
488
- /* Excluded declaration from this release type: ByteUtils */
498
+ /**
499
+ * This is the only ByteUtils that should be used across the rest of the BSON library.
500
+ *
501
+ * The type annotation is important here, it asserts that each of the platform specific
502
+ * utils implementations are compatible with the common one.
503
+ *
504
+ * @public
505
+ * @experimental
506
+ */
507
+ export declare const ByteUtils: ByteUtils;
489
508
 
490
509
  /**
491
510
  * Calculate the bson size for a passed in Javascript object.
@@ -1321,7 +1340,7 @@ export declare interface MinKeyExtended {
1321
1340
  *
1322
1341
  * A collection of functions that get or set various numeric types and bit widths from a Uint8Array.
1323
1342
  */
1324
- declare type NumberUtils = {
1343
+ export declare type NumberUtils = {
1325
1344
  /** Is true if the current system is big endian. */
1326
1345
  isBigEndian: boolean;
1327
1346
  /**
@@ -1345,7 +1364,7 @@ declare type NumberUtils = {
1345
1364
  * @experimental
1346
1365
  * @public
1347
1366
  */
1348
- declare const NumberUtils: NumberUtils;
1367
+ export declare const NumberUtils: NumberUtils;
1349
1368
 
1350
1369
  /**
1351
1370
  * A class representation of the BSON ObjectId type.
@@ -233,6 +233,7 @@ const nodejsRandomBytes = (() => {
233
233
  }
234
234
  })();
235
235
  const nodeJsByteUtils = {
236
+ isUint8Array: isUint8Array,
236
237
  toLocalBufferType(potentialBuffer) {
237
238
  if (Buffer.isBuffer(potentialBuffer)) {
238
239
  return potentialBuffer;
@@ -255,6 +256,12 @@ const nodeJsByteUtils = {
255
256
  allocateUnsafe(size) {
256
257
  return Buffer.allocUnsafe(size);
257
258
  },
259
+ compare(a, b) {
260
+ return nodeJsByteUtils.toLocalBufferType(a).compare(b);
261
+ },
262
+ concat(list) {
263
+ return Buffer.concat(list);
264
+ },
258
265
  equals(a, b) {
259
266
  return nodeJsByteUtils.toLocalBufferType(a).equals(b);
260
267
  },
@@ -264,6 +271,9 @@ const nodeJsByteUtils = {
264
271
  fromBase64(base64) {
265
272
  return Buffer.from(base64, 'base64');
266
273
  },
274
+ fromUTF8(utf8) {
275
+ return Buffer.from(utf8, 'utf8');
276
+ },
267
277
  toBase64(buffer) {
268
278
  return nodeJsByteUtils.toLocalBufferType(buffer).toString('base64');
269
279
  },
@@ -338,6 +348,7 @@ const webRandomBytes = (() => {
338
348
  })();
339
349
  const HEX_DIGIT = /(\d|[a-f])/i;
340
350
  const webByteUtils = {
351
+ isUint8Array: isUint8Array,
341
352
  toLocalBufferType(potentialUint8array) {
342
353
  const stringTag = potentialUint8array?.[Symbol.toStringTag] ??
343
354
  Object.prototype.toString.call(potentialUint8array);
@@ -364,12 +375,43 @@ const webByteUtils = {
364
375
  allocateUnsafe(size) {
365
376
  return webByteUtils.allocate(size);
366
377
  },
367
- equals(a, b) {
368
- if (a.byteLength !== b.byteLength) {
378
+ compare(uint8Array, otherUint8Array) {
379
+ if (uint8Array === otherUint8Array)
380
+ return 0;
381
+ const len = Math.min(uint8Array.length, otherUint8Array.length);
382
+ for (let i = 0; i < len; i++) {
383
+ if (uint8Array[i] < otherUint8Array[i])
384
+ return -1;
385
+ if (uint8Array[i] > otherUint8Array[i])
386
+ return 1;
387
+ }
388
+ if (uint8Array.length < otherUint8Array.length)
389
+ return -1;
390
+ if (uint8Array.length > otherUint8Array.length)
391
+ return 1;
392
+ return 0;
393
+ },
394
+ concat(uint8Arrays) {
395
+ if (uint8Arrays.length === 0)
396
+ return webByteUtils.allocate(0);
397
+ let totalLength = 0;
398
+ for (const uint8Array of uint8Arrays) {
399
+ totalLength += uint8Array.length;
400
+ }
401
+ const result = webByteUtils.allocate(totalLength);
402
+ let offset = 0;
403
+ for (const uint8Array of uint8Arrays) {
404
+ result.set(uint8Array, offset);
405
+ offset += uint8Array.length;
406
+ }
407
+ return result;
408
+ },
409
+ equals(uint8Array, otherUint8Array) {
410
+ if (uint8Array.byteLength !== otherUint8Array.byteLength) {
369
411
  return false;
370
412
  }
371
- for (let i = 0; i < a.byteLength; i++) {
372
- if (a[i] !== b[i]) {
413
+ for (let i = 0; i < uint8Array.byteLength; i++) {
414
+ if (uint8Array[i] !== otherUint8Array[i]) {
373
415
  return false;
374
416
  }
375
417
  }
@@ -381,6 +423,9 @@ const webByteUtils = {
381
423
  fromBase64(base64) {
382
424
  return Uint8Array.from(atob(base64), c => c.charCodeAt(0));
383
425
  },
426
+ fromUTF8(utf8) {
427
+ return new TextEncoder().encode(utf8);
428
+ },
384
429
  toBase64(uint8array) {
385
430
  return btoa(webByteUtils.toISO88591(uint8array));
386
431
  },
@@ -4616,6 +4661,7 @@ BSONType: BSONType,
4616
4661
  BSONValue: BSONValue,
4617
4662
  BSONVersionError: BSONVersionError,
4618
4663
  Binary: Binary,
4664
+ ByteUtils: ByteUtils,
4619
4665
  Code: Code,
4620
4666
  DBRef: DBRef,
4621
4667
  Decimal128: Decimal128,
@@ -4625,6 +4671,7 @@ Int32: Int32,
4625
4671
  Long: Long,
4626
4672
  MaxKey: MaxKey,
4627
4673
  MinKey: MinKey,
4674
+ NumberUtils: NumberUtils,
4628
4675
  ObjectId: ObjectId,
4629
4676
  Timestamp: Timestamp,
4630
4677
  UUID: UUID,
@@ -4648,6 +4695,7 @@ exports.BSONType = BSONType;
4648
4695
  exports.BSONValue = BSONValue;
4649
4696
  exports.BSONVersionError = BSONVersionError;
4650
4697
  exports.Binary = Binary;
4698
+ exports.ByteUtils = ByteUtils;
4651
4699
  exports.Code = Code;
4652
4700
  exports.DBRef = DBRef;
4653
4701
  exports.Decimal128 = Decimal128;
@@ -4657,6 +4705,7 @@ exports.Int32 = Int32;
4657
4705
  exports.Long = Long;
4658
4706
  exports.MaxKey = MaxKey;
4659
4707
  exports.MinKey = MinKey;
4708
+ exports.NumberUtils = NumberUtils;
4660
4709
  exports.ObjectId = ObjectId;
4661
4710
  exports.Timestamp = Timestamp;
4662
4711
  exports.UUID = UUID;