bson 5.2.0 → 5.4.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
@@ -69,35 +69,9 @@ npm install bson
69
69
 
70
70
  ## Documentation
71
71
 
72
- ### Objects
73
-
74
- <dl>
75
- <dt><a href="#EJSON">EJSON</a> : <code>object</code></dt>
76
- <dd></dd>
77
- </dl>
78
-
79
- ### Functions
80
-
81
- <dl>
82
- <dt><a href="#setInternalBufferSize">setInternalBufferSize(size)</a></dt>
83
- <dd><p>Sets the size of the internal serialization buffer.</p>
84
- </dd>
85
- <dt><a href="#serialize">serialize(object)</a> ⇒ <code>Buffer</code></dt>
86
- <dd><p>Serialize a Javascript object.</p>
87
- </dd>
88
- <dt><a href="#serializeWithBufferAndIndex">serializeWithBufferAndIndex(object, buffer)</a> ⇒ <code>Number</code></dt>
89
- <dd><p>Serialize a Javascript object using a predefined Buffer and index into the buffer, useful when pre-allocating the space for serialization.</p>
90
- </dd>
91
- <dt><a href="#deserialize">deserialize(buffer)</a> ⇒ <code>Object</code></dt>
92
- <dd><p>Deserialize data as BSON.</p>
93
- </dd>
94
- <dt><a href="#calculateObjectSize">calculateObjectSize(object)</a> ⇒ <code>Number</code></dt>
95
- <dd><p>Calculate the bson size for a passed in Javascript object.</p>
96
- </dd>
97
- <dt><a href="#deserializeStream">deserializeStream(data, startIndex, numberOfDocuments, documents, docStartIndex, [options])</a> ⇒ <code>Number</code></dt>
98
- <dd><p>Deserialize stream data as BSON documents.</p>
99
- </dd>
100
- </dl>
72
+ ### BSON
73
+
74
+ [API documentation](https://mongodb.github.io/node-mongodb-native/Next/modules/BSON.html)
101
75
 
102
76
  <a name="EJSON"></a>
103
77
 
@@ -189,103 +163,6 @@ Serializes an object to an Extended JSON string, and reparse it as a JavaScript
189
163
 
190
164
  Deserializes an Extended JSON object into a plain JavaScript object with native/BSON types
191
165
 
192
- <a name="setInternalBufferSize"></a>
193
-
194
- ### setInternalBufferSize(size)
195
-
196
- | Param | Type | Description |
197
- | --- | --- | --- |
198
- | size | <code>number</code> | The desired size for the internal serialization buffer |
199
-
200
- Sets the size of the internal serialization buffer.
201
-
202
- <a name="serialize"></a>
203
-
204
- ### serialize(object)
205
-
206
- | Param | Type | Default | Description |
207
- | --- | --- | --- | --- |
208
- | object | <code>Object</code> | | the Javascript object to serialize. |
209
- | [options.checkKeys] | <code>Boolean</code> | | the serializer will check if keys are valid. |
210
- | [options.serializeFunctions] | <code>Boolean</code> | <code>false</code> | serialize the javascript functions **(default:false)**. |
211
- | [options.ignoreUndefined] | <code>Boolean</code> | <code>true</code> | ignore undefined fields **(default:true)**. |
212
-
213
- Serialize a Javascript object.
214
-
215
- **Returns**: <code>Buffer</code> - returns the Buffer object containing the serialized object.
216
- <a name="serializeWithBufferAndIndex"></a>
217
-
218
- ### serializeWithBufferAndIndex(object, buffer)
219
-
220
- | Param | Type | Default | Description |
221
- | --- | --- | --- | --- |
222
- | object | <code>Object</code> | | the Javascript object to serialize. |
223
- | buffer | <code>Buffer</code> | | the Buffer you pre-allocated to store the serialized BSON object. |
224
- | [options.checkKeys] | <code>Boolean</code> | | the serializer will check if keys are valid. |
225
- | [options.serializeFunctions] | <code>Boolean</code> | <code>false</code> | serialize the javascript functions **(default:false)**. |
226
- | [options.ignoreUndefined] | <code>Boolean</code> | <code>true</code> | ignore undefined fields **(default:true)**. |
227
- | [options.index] | <code>Number</code> | | the index in the buffer where we wish to start serializing into. |
228
-
229
- Serialize a Javascript object using a predefined Buffer and index into the buffer, useful when pre-allocating the space for serialization.
230
-
231
- **Returns**: <code>Number</code> - returns the index pointing to the last written byte in the buffer.
232
- <a name="deserialize"></a>
233
-
234
- ### deserialize(buffer)
235
-
236
- | Param | Type | Default | Description |
237
- | --- | --- | --- | --- |
238
- | buffer | <code>Buffer</code> | | the buffer containing the serialized set of BSON documents. |
239
- | [options.evalFunctions] | <code>Object</code> | <code>false</code> | evaluate functions in the BSON document scoped to the object deserialized. |
240
- | [options.cacheFunctions] | <code>Object</code> | <code>false</code> | cache evaluated functions for reuse. |
241
- | [options.useBigInt64] | <code>Object</code> | <code>false</code> | when deserializing a Long will return a BigInt. |
242
- | [options.promoteLongs] | <code>Object</code> | <code>true</code> | when deserializing a Long will fit it into a Number if it's smaller than 53 bits |
243
- | [options.promoteBuffers] | <code>Object</code> | <code>false</code> | when deserializing a Binary will return it as a node.js Buffer instance. |
244
- | [options.promoteValues] | <code>Object</code> | <code>false</code> | when deserializing will promote BSON values to their Node.js closest equivalent types. |
245
- | [options.fieldsAsRaw] | <code>Object</code> | <code></code> | allow to specify if there what fields we wish to return as unserialized raw buffer. |
246
- | [options.bsonRegExp] | <code>Object</code> | <code>false</code> | return BSON regular expressions as BSONRegExp instances. |
247
- | [options.allowObjectSmallerThanBufferSize] | <code>boolean</code> | <code>false</code> | allows the buffer to be larger than the parsed BSON object. |
248
-
249
- Deserialize data as BSON.
250
-
251
- **Returns**: <code>Object</code> - returns the deserialized Javascript Object.
252
- <a name="calculateObjectSize"></a>
253
-
254
- ### calculateObjectSize(object)
255
-
256
- | Param | Type | Default | Description |
257
- | --- | --- | --- | --- |
258
- | object | <code>Object</code> | | the Javascript object to calculate the BSON byte size for. |
259
- | [options.serializeFunctions] | <code>Boolean</code> | <code>false</code> | serialize the javascript functions **(default:false)**. |
260
- | [options.ignoreUndefined] | <code>Boolean</code> | <code>true</code> | ignore undefined fields **(default:true)**. |
261
-
262
- Calculate the bson size for a passed in Javascript object.
263
-
264
- **Returns**: <code>Number</code> - returns the number of bytes the BSON object will take up.
265
- <a name="deserializeStream"></a>
266
-
267
- ### deserializeStream(data, startIndex, numberOfDocuments, documents, docStartIndex, [options])
268
-
269
- | Param | Type | Default | Description |
270
- | --- | --- | --- | --- |
271
- | data | <code>Buffer</code> | | the buffer containing the serialized set of BSON documents. |
272
- | startIndex | <code>Number</code> | | the start index in the data Buffer where the deserialization is to start. |
273
- | numberOfDocuments | <code>Number</code> | | number of documents to deserialize. |
274
- | documents | <code>Array</code> | | an array where to store the deserialized documents. |
275
- | docStartIndex | <code>Number</code> | | the index in the documents array from where to start inserting documents. |
276
- | [options] | <code>Object</code> | | additional options used for the deserialization. |
277
- | [options.evalFunctions] | <code>Object</code> | <code>false</code> | evaluate functions in the BSON document scoped to the object deserialized. |
278
- | [options.cacheFunctions] | <code>Object</code> | <code>false</code> | cache evaluated functions for reuse. |
279
- | [options.promoteLongs] | <code>Object</code> | <code>true</code> | when deserializing a Long will fit it into a Number if it's smaller than 53 bits |
280
- | [options.promoteBuffers] | <code>Object</code> | <code>false</code> | when deserializing a Binary will return it as a node.js Buffer instance. |
281
- | [options.promoteValues] | <code>Object</code> | <code>false</code> | when deserializing will promote BSON values to their Node.js closest equivalent types. |
282
- | [options.fieldsAsRaw] | <code>Object</code> | <code></code> | allow to specify if there what fields we wish to return as unserialized raw buffer. |
283
- | [options.bsonRegExp] | <code>Object</code> | <code>false</code> | return BSON regular expressions as BSONRegExp instances. |
284
-
285
- Deserialize stream data as BSON documents.
286
-
287
- **Returns**: <code>Number</code> - returns the next index in the buffer after deserialization **x** numbers of documents.
288
-
289
166
  ## Error Handling
290
167
 
291
168
  It is our recommendation to use `BSONError.isBSONError()` checks on errors and to avoid relying on parsing `error.message` and `error.name` strings in your code. We guarantee `BSONError.isBSONError()` checks will pass according to semver guidelines, but errors may be sub-classed or their messages may change at any time, even patch releases, as we see fit to increase the helpfulness of the errors.
@@ -311,23 +188,16 @@ try {
311
188
 
312
189
  ## React Native
313
190
 
314
- BSON requires that `TextEncoder`, `TextDecoder`, `atob`, `btoa`, and `crypto.getRandomValues` are available globally. These are present in most Javascript runtimes but require polyfilling in React Native. Polyfills for the missing functionality can be installed with the following command:
191
+ BSON vendors the required polyfills for `TextEncoder`, `TextDecoder`, `atob`, `btoa` imported from React Native and therefore doesn't expect users to polyfill these. One additional polyfill, `crypto.getRandomValues` is recommended and can be installed with the following command:
192
+
315
193
  ```sh
316
- npm install --save react-native-get-random-values text-encoding-polyfill base-64
194
+ npm install --save react-native-get-random-values
317
195
  ```
318
196
 
319
197
  The following snippet should be placed at the top of the entrypoint (by default this is the root `index.js` file) for React Native projects using the BSON library. These lines must be placed for any code that imports `BSON`.
320
198
 
321
199
  ```typescript
322
200
  // Required Polyfills For ReactNative
323
- import {encode, decode} from 'base-64';
324
- if (global.btoa == null) {
325
- global.btoa = encode;
326
- }
327
- if (global.atob == null) {
328
- global.atob = decode;
329
- }
330
- import 'text-encoding-polyfill';
331
201
  import 'react-native-get-random-values';
332
202
  ```
333
203
 
@@ -337,7 +207,7 @@ Finally, import the `BSON` library like so:
337
207
  import { BSON, EJSON } from 'bson';
338
208
  ```
339
209
 
340
- This will cause React Native to import the `node_modules/bson/lib/bson.cjs` bundle (see the `"react-native"` setting we have in the `"exports"` section of our [package.json](./package.json).)
210
+ This will cause React Native to import the `node_modules/bson/lib/bson.rn.cjs` bundle (see the `"react-native"` setting we have in the `"exports"` section of our [package.json](./package.json).)
341
211
 
342
212
  ### Technical Note about React Native module import
343
213
 
package/bson.d.ts CHANGED
@@ -280,7 +280,7 @@ export declare const BSONType: Readonly<{
280
280
  }>;
281
281
 
282
282
  /** @public */
283
- export declare type BSONType = typeof BSONType[keyof typeof BSONType];
283
+ export declare type BSONType = (typeof BSONType)[keyof typeof BSONType];
284
284
 
285
285
  /** @public */
286
286
  export declare abstract class BSONValue {
@@ -416,21 +416,45 @@ export declare function deserialize(buffer: Uint8Array, options?: DeserializeOpt
416
416
 
417
417
  /** @public */
418
418
  export declare interface DeserializeOptions {
419
- /** when deserializing a Long will return as a BigInt. */
419
+ /**
420
+ * when deserializing a Long return as a BigInt.
421
+ * @defaultValue `false`
422
+ */
420
423
  useBigInt64?: boolean;
421
- /** when deserializing a Long will fit it into a Number if it's smaller than 53 bits. */
424
+ /**
425
+ * when deserializing a Long will fit it into a Number if it's smaller than 53 bits.
426
+ * @defaultValue `true`
427
+ */
422
428
  promoteLongs?: boolean;
423
- /** when deserializing a Binary will return it as a node.js Buffer instance. */
429
+ /**
430
+ * when deserializing a Binary will return it as a node.js Buffer instance.
431
+ * @defaultValue `false`
432
+ */
424
433
  promoteBuffers?: boolean;
425
- /** when deserializing will promote BSON values to their Node.js closest equivalent types. */
434
+ /**
435
+ * when deserializing will promote BSON values to their Node.js closest equivalent types.
436
+ * @defaultValue `true`
437
+ */
426
438
  promoteValues?: boolean;
427
- /** allow to specify if there what fields we wish to return as unserialized raw buffer. */
439
+ /**
440
+ * allow to specify if there what fields we wish to return as unserialized raw buffer.
441
+ * @defaultValue `null`
442
+ */
428
443
  fieldsAsRaw?: Document;
429
- /** return BSON regular expressions as BSONRegExp instances. */
444
+ /**
445
+ * return BSON regular expressions as BSONRegExp instances.
446
+ * @defaultValue `false`
447
+ */
430
448
  bsonRegExp?: boolean;
431
- /** allows the buffer to be larger than the parsed BSON object. */
449
+ /**
450
+ * allows the buffer to be larger than the parsed BSON object.
451
+ * @defaultValue `false`
452
+ */
432
453
  allowObjectSmallerThanBufferSize?: boolean;
433
- /** Offset into buffer to begin reading document from */
454
+ /**
455
+ * Offset into buffer to begin reading document from
456
+ * @defaultValue `0`
457
+ */
434
458
  index?: number;
435
459
  raw?: boolean;
436
460
  /** Allows for opt-out utf-8 validation for all keys or
@@ -522,11 +546,19 @@ declare function EJSONdeserialize(ejson: Document, options?: EJSONOptions): any;
522
546
 
523
547
  /** @public */
524
548
  export declare type EJSONOptions = {
525
- /** Output using the Extended JSON v1 spec */
549
+ /**
550
+ * Output using the Extended JSON v1 spec
551
+ * @defaultValue `false`
552
+ */
526
553
  legacy?: boolean;
527
- /** Enable Extended JSON's `relaxed` mode, which attempts to return native JS types where possible, rather than BSON types */
554
+ /**
555
+ * Enable Extended JSON's `relaxed` mode, which attempts to return native JS types where possible, rather than BSON types
556
+ * @defaultValue `false` */
528
557
  relaxed?: boolean;
529
- /** Enable native bigint support */
558
+ /**
559
+ * Enable native bigint support
560
+ * @defaultValue `false`
561
+ */
530
562
  useBigInt64?: boolean;
531
563
  };
532
564
 
@@ -1049,14 +1081,27 @@ export declare function serialize(object: Document, options?: SerializeOptions):
1049
1081
 
1050
1082
  /** @public */
1051
1083
  export declare interface SerializeOptions {
1052
- /** the serializer will check if keys are valid. */
1084
+ /**
1085
+ * the serializer will check if keys are valid.
1086
+ * @defaultValue `false`
1087
+ */
1053
1088
  checkKeys?: boolean;
1054
- /** serialize the javascript functions **(default:false)**. */
1089
+ /**
1090
+ * serialize the javascript functions
1091
+ * @defaultValue `false`
1092
+ */
1055
1093
  serializeFunctions?: boolean;
1056
- /** serialize will not emit undefined fields **(default:true)** */
1094
+ /**
1095
+ * serialize will not emit undefined fields
1096
+ * note that the driver sets this to `false`
1097
+ * @defaultValue `true`
1098
+ */
1057
1099
  ignoreUndefined?: boolean;
1058
1100
  /* Excluded from this release type: minInternalBufferSize */
1059
- /** the index in the buffer where we wish to start serializing into */
1101
+ /**
1102
+ * the index in the buffer where we wish to start serializing into
1103
+ * @defaultValue `0`
1104
+ */
1060
1105
  index?: number;
1061
1106
  }
1062
1107
 
@@ -1074,7 +1119,7 @@ export declare function serializeWithBufferAndIndex(object: Document, finalBuffe
1074
1119
  /**
1075
1120
  * Sets the size of the internal serialization buffer.
1076
1121
  *
1077
- * @param size - The desired size for the internal serialization buffer
1122
+ * @param size - The desired size for the internal serialization buffer in bytes
1078
1123
  * @public
1079
1124
  */
1080
1125
  export declare function setInternalBufferSize(size: number): void;
@@ -1107,7 +1152,7 @@ declare function stringify(value: any, replacer?: (number | string)[] | ((this:
1107
1152
  /**
1108
1153
  * @public
1109
1154
  * @category BSONType
1110
- * */
1155
+ */
1111
1156
  export declare class Timestamp extends LongWithoutOverridesClass {
1112
1157
  get _bsontype(): 'Timestamp';
1113
1158
  static readonly MAX_VALUE: Long;
@@ -1168,10 +1213,12 @@ export declare type TimestampOverrides = '_bsontype' | 'toExtendedJSON' | 'fromE
1168
1213
  * @public
1169
1214
  */
1170
1215
  export declare class UUID extends Binary {
1216
+ /** @deprecated Hex string is no longer cached, this control will be removed in a future major release */
1171
1217
  static cacheHexString: boolean;
1172
- /* Excluded from this release type: __id */
1173
1218
  /**
1174
- * Create an UUID type
1219
+ * Create a UUID type
1220
+ *
1221
+ * When the argument to the constructor is omitted a random v4 UUID will be generated.
1175
1222
  *
1176
1223
  * @param input - Can be a 32 or 36 character hex string (dashes excluded/included) or a 16 byte binary Buffer.
1177
1224
  */
@@ -1185,7 +1232,7 @@ export declare class UUID extends Binary {
1185
1232
  /**
1186
1233
  * Returns the UUID id as a 32 or 36 character hex string representation, excluding/including dashes (defaults to 36 character dash separated)
1187
1234
  * @param includeDashes - should the string exclude dash-separators.
1188
- * */
1235
+ */
1189
1236
  toHexString(includeDashes?: boolean): string;
1190
1237
  /**
1191
1238
  * Converts the id into a 36 character (dashes included) hex string, unless a encoding is specified.
@@ -1214,7 +1261,7 @@ export declare class UUID extends Binary {
1214
1261
  * Checks if a value is a valid bson UUID
1215
1262
  * @param input - UUID, string or Buffer to validate.
1216
1263
  */
1217
- static isValid(input: string | Uint8Array | UUID): boolean;
1264
+ static isValid(input: string | Uint8Array | UUID | Binary): boolean;
1218
1265
  /**
1219
1266
  * Creates an UUID from a hex string representation of an UUID.
1220
1267
  * @param hexString - 32 or 36 character hex string (dashes excluded/included).
@@ -1222,6 +1269,8 @@ export declare class UUID extends Binary {
1222
1269
  static createFromHexString(hexString: string): UUID;
1223
1270
  /** Creates an UUID from a base64 string representation of an UUID. */
1224
1271
  static createFromBase64(base64: string): UUID;
1272
+ /* Excluded from this release type: bytesFromString */
1273
+ /* Excluded from this release type: isValidUUIDString */
1225
1274
  inspect(): string;
1226
1275
  }
1227
1276