bson 7.0.0-alpha.2 → 7.1.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/README.md CHANGED
@@ -99,12 +99,13 @@ npm install bson
99
99
 
100
100
  Only the following version combinations with the [MongoDB Node.js Driver](https://github.com/mongodb/node-mongodb-native) are considered stable.
101
101
 
102
- | | `bson@1.x` | `bson@4.x` | `bson@5.x` | `bson@6.x` |
103
- | ------------- | ---------- | ---------- | ---------- | ---------- |
104
- | `mongodb@6.x` | N/A | N/A | N/A | ✓ |
105
- | `mongodb@5.x` | N/A | N/A | ✓ | N/A |
106
- | `mongodb@4.x` | N/A | ✓ | N/A | N/A |
107
- | `mongodb@3.x` | ✓ | N/A | N/A | N/A |
102
+ | | `bson@1.x` | `bson@4.x` | `bson@5.x` | `bson@6.x` | `bson@7.x` |
103
+ | ------------- | ---------- | ---------- | ---------- | ---------- | ---------- |
104
+ | `mongodb@7.x` | N/A | N/A | N/A | N/A | ✓ |
105
+ | `mongodb@6.x` | N/A | N/A | N/A | ✓ | N/A |
106
+ | `mongodb@5.x` | N/A | N/A | ✓ | N/A | N/A |
107
+ | `mongodb@4.x` | N/A | ✓ | N/A | N/A | N/A |
108
+ | `mongodb@3.x` | ✓ | N/A | N/A | N/A | N/A |
108
109
 
109
110
  ## Documentation
110
111
 
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.
@@ -1474,8 +1493,6 @@ export declare interface ObjectIdLike {
1474
1493
  export declare type OnDemand = {
1475
1494
  parseToElements: (this: void, bytes: Uint8Array, startOffset?: number) => Iterable<BSONElement>;
1476
1495
  BSONElement: BSONElement;
1477
- ByteUtils: ByteUtils;
1478
- NumberUtils: NumberUtils;
1479
1496
  };
1480
1497
 
1481
1498
  /**
@@ -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
  },
@@ -4550,8 +4595,6 @@ function parseToElements(bytes, startOffset = 0) {
4550
4595
 
4551
4596
  const onDemand = Object.create(null);
4552
4597
  onDemand.parseToElements = parseToElements;
4553
- onDemand.ByteUtils = ByteUtils;
4554
- onDemand.NumberUtils = NumberUtils;
4555
4598
  Object.freeze(onDemand);
4556
4599
 
4557
4600
  const MAXSIZE = 1024 * 1024 * 17;
@@ -4616,6 +4659,7 @@ BSONType: BSONType,
4616
4659
  BSONValue: BSONValue,
4617
4660
  BSONVersionError: BSONVersionError,
4618
4661
  Binary: Binary,
4662
+ ByteUtils: ByteUtils,
4619
4663
  Code: Code,
4620
4664
  DBRef: DBRef,
4621
4665
  Decimal128: Decimal128,
@@ -4625,6 +4669,7 @@ Int32: Int32,
4625
4669
  Long: Long,
4626
4670
  MaxKey: MaxKey,
4627
4671
  MinKey: MinKey,
4672
+ NumberUtils: NumberUtils,
4628
4673
  ObjectId: ObjectId,
4629
4674
  Timestamp: Timestamp,
4630
4675
  UUID: UUID,
@@ -4648,6 +4693,7 @@ exports.BSONType = BSONType;
4648
4693
  exports.BSONValue = BSONValue;
4649
4694
  exports.BSONVersionError = BSONVersionError;
4650
4695
  exports.Binary = Binary;
4696
+ exports.ByteUtils = ByteUtils;
4651
4697
  exports.Code = Code;
4652
4698
  exports.DBRef = DBRef;
4653
4699
  exports.Decimal128 = Decimal128;
@@ -4657,6 +4703,7 @@ exports.Int32 = Int32;
4657
4703
  exports.Long = Long;
4658
4704
  exports.MaxKey = MaxKey;
4659
4705
  exports.MinKey = MinKey;
4706
+ exports.NumberUtils = NumberUtils;
4660
4707
  exports.ObjectId = ObjectId;
4661
4708
  exports.Timestamp = Timestamp;
4662
4709
  exports.UUID = UUID;