bson 5.4.0 → 6.0.0-alpha.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
@@ -4,6 +4,7 @@ BSON is short for "Binary JSON," and is the binary-encoded serialization of JSON
4
4
  You can learn more about it in [the specification](http://bsonspec.org).
5
5
 
6
6
  ### Table of Contents
7
+
7
8
  - [Usage](#usage)
8
9
  - [Bugs/Feature Requests](#bugs--feature-requests)
9
10
  - [Installation](#installation)
@@ -18,7 +19,7 @@ Think you've found a bug? Want to see a new feature in `bson`? Please open a cas
18
19
  2. Navigate to the NODE project: [jira.mongodb.org/browse/NODE](https://jira.mongodb.org/browse/NODE)
19
20
  3. Click **Create Issue** - Please provide as much information as possible about the issue and how to reproduce it.
20
21
 
21
- Bug reports in JIRA for all driver projects (i.e. NODE, PYTHON, CSHARP, JAVA) and the Core Server (i.e. SERVER) project are **public**.
22
+ Bug reports in JIRA for the NODE driver project are **public**.
22
23
 
23
24
  ## Usage
24
25
 
@@ -67,9 +68,20 @@ If you are working directly in the browser without a bundler please use the `.mj
67
68
  npm install bson
68
69
  ```
69
70
 
71
+ ### MongoDB Node.js Driver Version Compatibility
72
+
73
+ Only the following version combinations with the [MongoDB Node.js Driver](https://github.com/mongodb/node-mongodb-native) are considered stable.
74
+
75
+ | | `bson@1.x` | `bson@4.x` | `bson@5.x` | `bson@6.x` |
76
+ | ------------- | ---------- | ---------- | ---------- | ---------- |
77
+ | `mongodb@6.x` | N/A | N/A | N/A | ✓ |
78
+ | `mongodb@5.x` | N/A | N/A | ✓ | N/A |
79
+ | `mongodb@4.x` | N/A | ✓ | N/A | N/A |
80
+ | `mongodb@3.x` | ✓ | N/A | N/A | N/A |
81
+
70
82
  ## Documentation
71
83
 
72
- ### BSON
84
+ ### BSON
73
85
 
74
86
  [API documentation](https://mongodb.github.io/node-mongodb-native/Next/modules/BSON.html)
75
87
 
@@ -77,31 +89,31 @@ npm install bson
77
89
 
78
90
  ### EJSON
79
91
 
80
- * [EJSON](#EJSON)
81
-
82
- * [.parse(text, [options])](#EJSON.parse)
92
+ - [EJSON](#EJSON)
83
93
 
84
- * [.stringify(value, [replacer], [space], [options])](#EJSON.stringify)
94
+ - [.parse(text, [options])](#EJSON.parse)
85
95
 
86
- * [.serialize(bson, [options])](#EJSON.serialize)
96
+ - [.stringify(value, [replacer], [space], [options])](#EJSON.stringify)
87
97
 
88
- * [.deserialize(ejson, [options])](#EJSON.deserialize)
98
+ - [.serialize(bson, [options])](#EJSON.serialize)
89
99
 
100
+ - [.deserialize(ejson, [options])](#EJSON.deserialize)
90
101
 
91
102
  <a name="EJSON.parse"></a>
92
103
 
93
- #### *EJSON*.parse(text, [options])
104
+ #### _EJSON_.parse(text, [options])
94
105
 
95
- | Param | Type | Default | Description |
96
- | --- | --- | --- | --- |
97
- | text | <code>string</code> | | |
98
- | [options] | <code>object</code> | | Optional settings |
106
+ | Param | Type | Default | Description |
107
+ | ----------------- | -------------------- | ----------------- | ---------------------------------------------------------------------------------- |
108
+ | text | <code>string</code> | | |
109
+ | [options] | <code>object</code> | | Optional settings |
99
110
  | [options.relaxed] | <code>boolean</code> | <code>true</code> | Attempt to return native JS types where possible, rather than BSON types (if true) |
100
111
 
101
112
  Parse an Extended JSON string, constructing the JavaScript value or object described by that
102
113
  string.
103
114
 
104
115
  **Example**
116
+
105
117
  ```js
106
118
  const { EJSON } = require('bson');
107
119
  const text = '{ "int32": { "$numberInt": "10" } }';
@@ -112,24 +124,26 @@ console.log(EJSON.parse(text, { relaxed: false }));
112
124
  // prints { int32: 10 }
113
125
  console.log(EJSON.parse(text));
114
126
  ```
127
+
115
128
  <a name="EJSON.stringify"></a>
116
129
 
117
- #### *EJSON*.stringify(value, [replacer], [space], [options])
130
+ #### _EJSON_.stringify(value, [replacer], [space], [options])
118
131
 
119
- | Param | Type | Default | Description |
120
- | --- | --- | --- | --- |
121
- | value | <code>object</code> | | The value to convert to extended JSON |
122
- | [replacer] | <code>function</code> \| <code>array</code> | | A function that alters the behavior of the stringification process, or an array of String and Number objects that serve as a whitelist for selecting/filtering the properties of the value object to be included in the JSON string. If this value is null or not provided, all properties of the object are included in the resulting JSON string |
123
- | [space] | <code>string</code> \| <code>number</code> | | A String or Number object that's used to insert white space into the output JSON string for readability purposes. |
124
- | [options] | <code>object</code> | | Optional settings |
125
- | [options.relaxed] | <code>boolean</code> | <code>true</code> | Enabled Extended JSON's `relaxed` mode |
126
- | [options.legacy] | <code>boolean</code> | <code>true</code> | Output in Extended JSON v1 |
132
+ | Param | Type | Default | Description |
133
+ | ----------------- | ------------------------------------------- | ----------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
134
+ | value | <code>object</code> | | The value to convert to extended JSON |
135
+ | [replacer] | <code>function</code> \| <code>array</code> | | A function that alters the behavior of the stringification process, or an array of String and Number objects that serve as a whitelist for selecting/filtering the properties of the value object to be included in the JSON string. If this value is null or not provided, all properties of the object are included in the resulting JSON string |
136
+ | [space] | <code>string</code> \| <code>number</code> | | A String or Number object that's used to insert white space into the output JSON string for readability purposes. |
137
+ | [options] | <code>object</code> | | Optional settings |
138
+ | [options.relaxed] | <code>boolean</code> | <code>true</code> | Enabled Extended JSON's `relaxed` mode |
139
+ | [options.legacy] | <code>boolean</code> | <code>true</code> | Output in Extended JSON v1 |
127
140
 
128
141
  Converts a BSON document to an Extended JSON string, optionally replacing values if a replacer
129
142
  function is specified or optionally including only the specified properties if a replacer array
130
143
  is specified.
131
144
 
132
145
  **Example**
146
+
133
147
  ```js
134
148
  const { EJSON } = require('bson');
135
149
  const Int32 = require('mongodb').Int32;
@@ -141,24 +155,25 @@ console.log(EJSON.stringify(doc, { relaxed: false }));
141
155
  // prints '{"int32":10}'
142
156
  console.log(EJSON.stringify(doc));
143
157
  ```
158
+
144
159
  <a name="EJSON.serialize"></a>
145
160
 
146
- #### *EJSON*.serialize(bson, [options])
161
+ #### _EJSON_.serialize(bson, [options])
147
162
 
148
- | Param | Type | Description |
149
- | --- | --- | --- |
150
- | bson | <code>object</code> | The object to serialize |
163
+ | Param | Type | Description |
164
+ | --------- | ------------------- | ---------------------------------------------------- |
165
+ | bson | <code>object</code> | The object to serialize |
151
166
  | [options] | <code>object</code> | Optional settings passed to the `stringify` function |
152
167
 
153
168
  Serializes an object to an Extended JSON string, and reparse it as a JavaScript object.
154
169
 
155
170
  <a name="EJSON.deserialize"></a>
156
171
 
157
- #### *EJSON*.deserialize(ejson, [options])
172
+ #### _EJSON_.deserialize(ejson, [options])
158
173
 
159
- | Param | Type | Description |
160
- | --- | --- | --- |
161
- | ejson | <code>object</code> | The Extended JSON object to deserialize |
174
+ | Param | Type | Description |
175
+ | --------- | ------------------- | -------------------------------------------- |
176
+ | ejson | <code>object</code> | The Extended JSON object to deserialize |
162
177
  | [options] | <code>object</code> | Optional settings passed to the parse method |
163
178
 
164
179
  Deserializes an Extended JSON object into a plain JavaScript object with native/BSON types
@@ -194,7 +209,7 @@ BSON vendors the required polyfills for `TextEncoder`, `TextDecoder`, `atob`, `b
194
209
  npm install --save react-native-get-random-values
195
210
  ```
196
211
 
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`.
212
+ 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`.
198
213
 
199
214
  ```typescript
200
215
  // Required Polyfills For ReactNative
@@ -211,7 +226,7 @@ This will cause React Native to import the `node_modules/bson/lib/bson.rn.cjs` b
211
226
 
212
227
  ### Technical Note about React Native module import
213
228
 
214
- The `"exports"` definition in our `package.json` will result in BSON's CommonJS bundle being imported in a React Native project instead of the ES module bundle. Importing the CommonJS bundle is necessary because BSON's ES module bundle of BSON uses top-level await, which is not supported syntax in [React Native's runtime hermes](https://hermesengine.dev/).
229
+ The `"exports"` definition in our `package.json` will result in BSON's CommonJS bundle being imported in a React Native project instead of the ES module bundle. Importing the CommonJS bundle is necessary because BSON's ES module bundle of BSON uses top-level await, which is not supported syntax in [React Native's runtime hermes](https://hermesengine.dev/).
215
230
 
216
231
  ## FAQ
217
232
 
package/bson.d.ts CHANGED
@@ -31,16 +31,10 @@ export declare class Binary extends BSONValue {
31
31
  position: number;
32
32
  /**
33
33
  * Create a new Binary instance.
34
- *
35
- * This constructor can accept a string as its first argument. In this case,
36
- * this string will be encoded using ISO-8859-1, **not** using UTF-8.
37
- * This is almost certainly not what you want. Use `new Binary(Buffer.from(string))`
38
- * instead to convert the string to a Buffer using UTF-8 first.
39
- *
40
34
  * @param buffer - a buffer object containing the binary data.
41
35
  * @param subType - the option binary type.
42
36
  */
43
- constructor(buffer?: string | BinarySequence, subType?: number);
37
+ constructor(buffer?: BinarySequence, subType?: number);
44
38
  /**
45
39
  * Updates this binary with byte_value.
46
40
  *
@@ -48,12 +42,12 @@ export declare class Binary extends BSONValue {
48
42
  */
49
43
  put(byteValue: string | number | Uint8Array | number[]): void;
50
44
  /**
51
- * Writes a buffer or string to the binary.
45
+ * Writes a buffer to the binary.
52
46
  *
53
47
  * @param sequence - a string or buffer to be written to the Binary BSON object.
54
48
  * @param offset - specify the binary of where to write the content.
55
49
  */
56
- write(sequence: string | BinarySequence, offset: number): void;
50
+ write(sequence: BinarySequence, offset: number): void;
57
51
  /**
58
52
  * Reads **length** bytes starting at **position**.
59
53
  *
@@ -61,13 +55,8 @@ export declare class Binary extends BSONValue {
61
55
  * @param length - the number of bytes to read.
62
56
  */
63
57
  read(position: number, length: number): BinarySequence;
64
- /**
65
- * Returns the value of this binary as a string.
66
- * @param asRaw - Will skip converting to a string
67
- * @remarks
68
- * This is handy when calling this function conditionally for some key value pairs and not others
69
- */
70
- value(asRaw?: boolean): string | BinarySequence;
58
+ /** returns a view of the binary value as a Uint8Array */
59
+ value(): Uint8Array;
71
60
  /** the length of the binary sequence */
72
61
  length(): number;
73
62
  toJSON(): string;
@@ -990,7 +979,7 @@ export declare class ObjectId extends BSONValue {
990
979
  */
991
980
  get id(): Uint8Array;
992
981
  set id(value: Uint8Array);
993
- /** Returns the ObjectId id as a 24 character hex string representation */
982
+ /** Returns the ObjectId id as a 24 lowercase character hex string representation */
994
983
  toHexString(): string;
995
984
  /* Excluded from this release type: getInc */
996
985
  /**
@@ -1006,12 +995,13 @@ export declare class ObjectId extends BSONValue {
1006
995
  toString(encoding?: 'hex' | 'base64'): string;
1007
996
  /** Converts to its JSON the 24 character hex string representation. */
1008
997
  toJSON(): string;
998
+ /* Excluded from this release type: is */
1009
999
  /**
1010
1000
  * Compares the equality of this ObjectId with `otherID`.
1011
1001
  *
1012
1002
  * @param otherId - ObjectId instance to compare against.
1013
1003
  */
1014
- equals(otherId: string | ObjectId | ObjectIdLike): boolean;
1004
+ equals(otherId: string | ObjectId | ObjectIdLike | undefined | null): boolean;
1015
1005
  /** Returns the generation date (accurate up to the second) that this ID was generated. */
1016
1006
  getTimestamp(): Date;
1017
1007
  /* Excluded from this release type: createPk */
@@ -1030,9 +1020,8 @@ export declare class ObjectId extends BSONValue {
1030
1020
  /** Creates an ObjectId instance from a base64 string */
1031
1021
  static createFromBase64(base64: string): ObjectId;
1032
1022
  /**
1033
- * Checks if a value is a valid bson ObjectId
1034
- *
1035
- * @param id - ObjectId instance to validate.
1023
+ * Checks if a value can be used to create a valid bson ObjectId
1024
+ * @param id - any JS value
1036
1025
  */
1037
1026
  static isValid(id: string | number | ObjectId | ObjectIdLike | Uint8Array): boolean;
1038
1027
  /* Excluded from this release type: toExtendedJSON */
@@ -1213,8 +1202,6 @@ export declare type TimestampOverrides = '_bsontype' | 'toExtendedJSON' | 'fromE
1213
1202
  * @public
1214
1203
  */
1215
1204
  export declare class UUID extends Binary {
1216
- /** @deprecated Hex string is no longer cached, this control will be removed in a future major release */
1217
- static cacheHexString: boolean;
1218
1205
  /**
1219
1206
  * Create a UUID type
1220
1207
  *
@@ -17,7 +17,7 @@ function isDate(d) {
17
17
  return Object.prototype.toString.call(d) === '[object Date]';
18
18
  }
19
19
 
20
- const BSON_MAJOR_VERSION = 5;
20
+ const BSON_MAJOR_VERSION = 6;
21
21
  const BSON_INT32_MAX = 0x7fffffff;
22
22
  const BSON_INT32_MIN = -0x80000000;
23
23
  const BSON_INT64_MAX = Math.pow(2, 63) - 1;
@@ -312,11 +312,11 @@ class Binary extends BSONValue {
312
312
  constructor(buffer, subType) {
313
313
  super();
314
314
  if (!(buffer == null) &&
315
- !(typeof buffer === 'string') &&
315
+ typeof buffer === 'string' &&
316
316
  !ArrayBuffer.isView(buffer) &&
317
- !(buffer instanceof ArrayBuffer) &&
317
+ !isAnyArrayBuffer(buffer) &&
318
318
  !Array.isArray(buffer)) {
319
- throw new BSONError('Binary can only be constructed from string, Buffer, TypedArray, or Array<number>');
319
+ throw new BSONError('Binary can only be constructed from Uint8Array or number[]');
320
320
  }
321
321
  this.sub_type = subType ?? Binary.BSON_BINARY_SUBTYPE_DEFAULT;
322
322
  if (buffer == null) {
@@ -324,15 +324,9 @@ class Binary extends BSONValue {
324
324
  this.position = 0;
325
325
  }
326
326
  else {
327
- if (typeof buffer === 'string') {
328
- this.buffer = ByteUtils.fromISO88591(buffer);
329
- }
330
- else if (Array.isArray(buffer)) {
331
- this.buffer = ByteUtils.fromNumberArray(buffer);
332
- }
333
- else {
334
- this.buffer = ByteUtils.toLocalBufferType(buffer);
335
- }
327
+ this.buffer = Array.isArray(buffer)
328
+ ? ByteUtils.fromNumberArray(buffer)
329
+ : ByteUtils.toLocalBufferType(buffer);
336
330
  this.position = this.buffer.byteLength;
337
331
  }
338
332
  }
@@ -378,25 +372,17 @@ class Binary extends BSONValue {
378
372
  offset + sequence.byteLength > this.position ? offset + sequence.length : this.position;
379
373
  }
380
374
  else if (typeof sequence === 'string') {
381
- const bytes = ByteUtils.fromISO88591(sequence);
382
- this.buffer.set(bytes, offset);
383
- this.position =
384
- offset + sequence.length > this.position ? offset + sequence.length : this.position;
375
+ throw new BSONError('input cannot be string');
385
376
  }
386
377
  }
387
378
  read(position, length) {
388
379
  length = length && length > 0 ? length : this.position;
389
380
  return this.buffer.slice(position, position + length);
390
381
  }
391
- value(asRaw) {
392
- asRaw = !!asRaw;
393
- if (asRaw && this.buffer.length === this.position) {
394
- return this.buffer;
395
- }
396
- if (asRaw) {
397
- return this.buffer.slice(0, this.position);
398
- }
399
- return ByteUtils.toISO88591(this.buffer.subarray(0, this.position));
382
+ value() {
383
+ return this.buffer.length === this.position
384
+ ? this.buffer
385
+ : this.buffer.subarray(0, this.position);
400
386
  }
401
387
  length() {
402
388
  return this.position;
@@ -597,7 +583,6 @@ class UUID extends Binary {
597
583
  return `new UUID("${this.toHexString()}")`;
598
584
  }
599
585
  }
600
- UUID.cacheHexString = false;
601
586
 
602
587
  class Code extends BSONValue {
603
588
  get _bsontype() {
@@ -1991,20 +1976,11 @@ class ObjectId extends BSONValue {
1991
1976
  this[kId] = ByteUtils.toLocalBufferType(workingId);
1992
1977
  }
1993
1978
  else if (typeof workingId === 'string') {
1994
- if (workingId.length === 12) {
1995
- const bytes = ByteUtils.fromUTF8(workingId);
1996
- if (bytes.byteLength === 12) {
1997
- this[kId] = bytes;
1998
- }
1999
- else {
2000
- throw new BSONError('Argument passed in must be a string of 12 bytes');
2001
- }
2002
- }
2003
- else if (workingId.length === 24 && checkForHexRegExp.test(workingId)) {
1979
+ if (workingId.length === 24 && checkForHexRegExp.test(workingId)) {
2004
1980
  this[kId] = ByteUtils.fromHex(workingId);
2005
1981
  }
2006
1982
  else {
2007
- throw new BSONError('Argument passed in must be a string of 12 bytes or a string of 24 hex characters or an integer');
1983
+ throw new BSONError('input must be a 24 character hex string, 12 byte Uint8Array, or an integer');
2008
1984
  }
2009
1985
  }
2010
1986
  else {
@@ -2066,30 +2042,25 @@ class ObjectId extends BSONValue {
2066
2042
  toJSON() {
2067
2043
  return this.toHexString();
2068
2044
  }
2045
+ static is(variable) {
2046
+ return (variable != null &&
2047
+ typeof variable === 'object' &&
2048
+ '_bsontype' in variable &&
2049
+ variable._bsontype === 'ObjectId');
2050
+ }
2069
2051
  equals(otherId) {
2070
2052
  if (otherId === undefined || otherId === null) {
2071
2053
  return false;
2072
2054
  }
2073
- if (otherId instanceof ObjectId) {
2055
+ if (ObjectId.is(otherId)) {
2074
2056
  return this[kId][11] === otherId[kId][11] && ByteUtils.equals(this[kId], otherId[kId]);
2075
2057
  }
2076
- if (typeof otherId === 'string' &&
2077
- ObjectId.isValid(otherId) &&
2078
- otherId.length === 12 &&
2079
- isUint8Array(this.id)) {
2080
- return ByteUtils.equals(this.id, ByteUtils.fromISO88591(otherId));
2081
- }
2082
- if (typeof otherId === 'string' && ObjectId.isValid(otherId) && otherId.length === 24) {
2058
+ if (typeof otherId === 'string') {
2083
2059
  return otherId.toLowerCase() === this.toHexString();
2084
2060
  }
2085
- if (typeof otherId === 'string' && ObjectId.isValid(otherId) && otherId.length === 12) {
2086
- return ByteUtils.equals(ByteUtils.fromUTF8(otherId), this.id);
2087
- }
2088
- if (typeof otherId === 'object' &&
2089
- 'toHexString' in otherId &&
2090
- typeof otherId.toHexString === 'function') {
2061
+ if (typeof otherId === 'object' && typeof otherId.toHexString === 'function') {
2091
2062
  const otherIdString = otherId.toHexString();
2092
- const thisIdString = this.toHexString().toLowerCase();
2063
+ const thisIdString = this.toHexString();
2093
2064
  return typeof otherIdString === 'string' && otherIdString.toLowerCase() === thisIdString;
2094
2065
  }
2095
2066
  return false;