bson 7.1.1 → 7.2.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
@@ -207,6 +207,9 @@ declare namespace BSON {
207
207
  Decimal128Extended,
208
208
  DoubleExtended,
209
209
  EJSONOptions,
210
+ EJSONOptionsBase,
211
+ EJSONSerializeOptions,
212
+ EJSONParseOptions,
210
213
  Int32Extended,
211
214
  LongExtended,
212
215
  MaxKeyExtended,
@@ -465,6 +468,8 @@ export declare type ByteUtils = {
465
468
  compare: (buffer1: Uint8Array, buffer2: Uint8Array) => -1 | 0 | 1;
466
469
  /** Concatenating all the Uint8Arrays in new Uint8Array. */
467
470
  concat: (list: Uint8Array[]) => Uint8Array;
471
+ /** Copy bytes from source Uint8Array to target Uint8Array */
472
+ copy: (source: Uint8Array, target: Uint8Array, targetStart?: number, sourceStart?: number, sourceEnd?: number) => number;
468
473
  /** Check if two Uint8Arrays are deep equal */
469
474
  equals: (a: Uint8Array, b: Uint8Array) => boolean;
470
475
  /** Create a Uint8Array from an array of numbers */
@@ -782,10 +787,13 @@ export declare const EJSON: {
782
787
  * @param ejson - The Extended JSON object to deserialize
783
788
  * @param options - Optional settings passed to the parse method
784
789
  */
785
- declare function EJSONdeserialize(ejson: Document, options?: EJSONOptions): any;
790
+ declare function EJSONdeserialize(ejson: Document, options?: EJSONParseOptions): any;
786
791
 
787
792
  /** @public */
788
- export declare type EJSONOptions = {
793
+ export declare type EJSONOptions = EJSONSerializeOptions & EJSONParseOptions;
794
+
795
+ /** @public */
796
+ export declare type EJSONOptionsBase = {
789
797
  /**
790
798
  * Output using the Extended JSON v1 spec
791
799
  * @defaultValue `false`
@@ -793,8 +801,13 @@ export declare type EJSONOptions = {
793
801
  legacy?: boolean;
794
802
  /**
795
803
  * Enable Extended JSON's `relaxed` mode, which attempts to return native JS types where possible, rather than BSON types
796
- * @defaultValue `false` */
804
+ * @defaultValue `false`
805
+ */
797
806
  relaxed?: boolean;
807
+ };
808
+
809
+ /** @public */
810
+ export declare type EJSONParseOptions = EJSONOptionsBase & {
798
811
  /**
799
812
  * Enable native bigint support
800
813
  * @defaultValue `false`
@@ -808,7 +821,16 @@ export declare type EJSONOptions = {
808
821
  * @param value - The object to serialize
809
822
  * @param options - Optional settings passed to the `stringify` function
810
823
  */
811
- declare function EJSONserialize(value: any, options?: EJSONOptions): Document;
824
+ declare function EJSONserialize(value: any, options?: EJSONSerializeOptions): Document;
825
+
826
+ /** @public */
827
+ export declare type EJSONSerializeOptions = EJSONOptionsBase & {
828
+ /**
829
+ * Omits undefined values from the output instead of converting them to null
830
+ * @defaultValue `false`
831
+ */
832
+ ignoreUndefined?: boolean;
833
+ };
812
834
 
813
835
  declare type InspectFn = (x: unknown, options?: unknown) => string;
814
836
 
@@ -1519,7 +1541,7 @@ export declare const onDemand: OnDemand;
1519
1541
  * console.log(EJSON.parse(text));
1520
1542
  * ```
1521
1543
  */
1522
- declare function parse(text: string, options?: EJSONOptions): any;
1544
+ declare function parse(text: string, options?: EJSONParseOptions): any;
1523
1545
 
1524
1546
  /**
1525
1547
  * Serialize a Javascript object.
@@ -1598,7 +1620,7 @@ export declare function setInternalBufferSize(size: number): void;
1598
1620
  * console.log(EJSON.stringify(doc));
1599
1621
  * ```
1600
1622
  */
1601
- declare function stringify(value: any, replacer?: (number | string)[] | ((this: any, key: string, value: any) => any) | EJSONOptions, space?: string | number, options?: EJSONOptions): string;
1623
+ declare function stringify(value: any, replacer?: (number | string)[] | ((this: any, key: string, value: any) => any) | EJSONSerializeOptions, space?: string | number, options?: EJSONSerializeOptions): string;
1602
1624
 
1603
1625
  /**
1604
1626
  * @public
@@ -262,6 +262,11 @@ const nodeJsByteUtils = {
262
262
  concat(list) {
263
263
  return Buffer.concat(list);
264
264
  },
265
+ copy(source, target, targetStart, sourceStart, sourceEnd) {
266
+ return nodeJsByteUtils
267
+ .toLocalBufferType(source)
268
+ .copy(target, targetStart ?? 0, sourceStart ?? 0, sourceEnd ?? source.length);
269
+ },
265
270
  equals(a, b) {
266
271
  return nodeJsByteUtils.toLocalBufferType(a).equals(b);
267
272
  },
@@ -406,6 +411,27 @@ const webByteUtils = {
406
411
  }
407
412
  return result;
408
413
  },
414
+ copy(source, target, targetStart, sourceStart, sourceEnd) {
415
+ if (sourceEnd !== undefined && sourceEnd < 0) {
416
+ throw new RangeError(`The value of "sourceEnd" is out of range. It must be >= 0. Received ${sourceEnd}`);
417
+ }
418
+ sourceEnd = sourceEnd ?? source.length;
419
+ if (sourceStart !== undefined && (sourceStart < 0 || sourceStart > sourceEnd)) {
420
+ throw new RangeError(`The value of "sourceStart" is out of range. It must be >= 0 and <= ${sourceEnd}. Received ${sourceStart}`);
421
+ }
422
+ sourceStart = sourceStart ?? 0;
423
+ if (targetStart !== undefined && targetStart < 0) {
424
+ throw new RangeError(`The value of "targetStart" is out of range. It must be >= 0. Received ${targetStart}`);
425
+ }
426
+ targetStart = targetStart ?? 0;
427
+ const srcSlice = source.subarray(sourceStart, sourceEnd);
428
+ const maxLen = Math.min(srcSlice.length, target.length - targetStart);
429
+ if (maxLen <= 0) {
430
+ return 0;
431
+ }
432
+ target.set(srcSlice.subarray(0, maxLen), targetStart);
433
+ return maxLen;
434
+ },
409
435
  equals(uint8Array, otherUint8Array) {
410
436
  if (uint8Array.byteLength !== otherUint8Array.byteLength) {
411
437
  return false;
@@ -4311,7 +4337,7 @@ function serializeValue(value, options) {
4311
4337
  if (Array.isArray(value))
4312
4338
  return serializeArray(value, options);
4313
4339
  if (value === undefined)
4314
- return null;
4340
+ return options.ignoreUndefined ? undefined : null;
4315
4341
  if (value instanceof Date || isDate(value)) {
4316
4342
  const dateNum = value.getTime(), inRange = dateNum > -1 && dateNum < 253402318800000;
4317
4343
  if (options.legacy) {