bson 5.3.0 → 6.0.0-alpha

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.
Files changed (73) hide show
  1. package/README.md +51 -43
  2. package/bson.d.ts +10 -23
  3. package/lib/bson.bundle.js +73 -130
  4. package/lib/bson.bundle.js.map +1 -1
  5. package/lib/bson.cjs +73 -130
  6. package/lib/bson.cjs.map +1 -1
  7. package/lib/bson.mjs +73 -130
  8. package/lib/bson.mjs.map +1 -1
  9. package/lib/bson.rn.cjs +4051 -0
  10. package/lib/bson.rn.cjs.map +1 -0
  11. package/package.json +26 -24
  12. package/src/binary.ts +19 -53
  13. package/src/bson.ts +2 -2
  14. package/src/code.ts +1 -1
  15. package/src/constants.ts +1 -1
  16. package/src/decimal128.ts +35 -67
  17. package/src/objectid.ts +20 -37
  18. package/src/parser/deserializer.ts +8 -8
  19. package/src/symbol.ts +1 -1
  20. package/src/timestamp.ts +7 -5
  21. package/src/utils/byte_utils.ts +2 -2
  22. package/src/utils/node_byte_utils.ts +3 -3
  23. package/src/utils/web_byte_utils.ts +2 -2
  24. package/vendor/base64/LICENSE-MIT.txt +20 -0
  25. package/vendor/base64/README.md +112 -0
  26. package/vendor/base64/base64.js +157 -0
  27. package/vendor/base64/package.json +43 -0
  28. package/vendor/text-encoding/LICENSE.md +237 -0
  29. package/vendor/text-encoding/README.md +111 -0
  30. package/vendor/text-encoding/index.js +9 -0
  31. package/vendor/text-encoding/lib/encoding-indexes.js +47 -0
  32. package/vendor/text-encoding/lib/encoding.js +3301 -0
  33. package/vendor/text-encoding/package.json +37 -0
  34. package/lib/binary.d.ts +0 -182
  35. package/lib/binary.d.ts.map +0 -1
  36. package/lib/bson.d.ts +0 -97
  37. package/lib/bson.d.ts.map +0 -1
  38. package/lib/bson_value.d.ts +0 -10
  39. package/lib/bson_value.d.ts.map +0 -1
  40. package/lib/code.d.ts +0 -32
  41. package/lib/code.d.ts.map +0 -1
  42. package/lib/constants.d.ts +0 -107
  43. package/lib/constants.d.ts.map +0 -1
  44. package/lib/db_ref.d.ts +0 -40
  45. package/lib/db_ref.d.ts.map +0 -1
  46. package/lib/decimal128.d.ts +0 -34
  47. package/lib/decimal128.d.ts.map +0 -1
  48. package/lib/double.d.ts +0 -35
  49. package/lib/double.d.ts.map +0 -1
  50. package/lib/error.d.ts +0 -50
  51. package/lib/error.d.ts.map +0 -1
  52. package/lib/extended_json.d.ts +0 -82
  53. package/lib/extended_json.d.ts.map +0 -1
  54. package/lib/index.d.ts +0 -4
  55. package/lib/index.d.ts.map +0 -1
  56. package/lib/int_32.d.ts +0 -35
  57. package/lib/int_32.d.ts.map +0 -1
  58. package/lib/long.d.ts +0 -323
  59. package/lib/long.d.ts.map +0 -1
  60. package/lib/max_key.d.ts +0 -19
  61. package/lib/max_key.d.ts.map +0 -1
  62. package/lib/min_key.d.ts +0 -19
  63. package/lib/min_key.d.ts.map +0 -1
  64. package/lib/objectid.d.ts +0 -96
  65. package/lib/objectid.d.ts.map +0 -1
  66. package/lib/regexp.d.ts +0 -36
  67. package/lib/regexp.d.ts.map +0 -1
  68. package/lib/symbol.d.ts +0 -28
  69. package/lib/symbol.d.ts.map +0 -1
  70. package/lib/timestamp.d.ts +0 -66
  71. package/lib/timestamp.d.ts.map +0 -1
  72. package/lib/validate_utf8.d.ts +0 -10
  73. package/lib/validate_utf8.d.ts.map +0 -1
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
@@ -188,23 +203,16 @@ try {
188
203
 
189
204
  ## React Native
190
205
 
191
- 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:
206
+ 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:
207
+
192
208
  ```sh
193
- npm install --save react-native-get-random-values text-encoding-polyfill base-64
209
+ npm install --save react-native-get-random-values
194
210
  ```
195
211
 
196
- 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`.
197
213
 
198
214
  ```typescript
199
215
  // Required Polyfills For ReactNative
200
- import {encode, decode} from 'base-64';
201
- if (global.btoa == null) {
202
- global.btoa = encode;
203
- }
204
- if (global.atob == null) {
205
- global.atob = decode;
206
- }
207
- import 'text-encoding-polyfill';
208
216
  import 'react-native-get-random-values';
209
217
  ```
210
218
 
@@ -214,11 +222,11 @@ Finally, import the `BSON` library like so:
214
222
  import { BSON, EJSON } from 'bson';
215
223
  ```
216
224
 
217
- 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).)
225
+ 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).)
218
226
 
219
227
  ### Technical Note about React Native module import
220
228
 
221
- 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/).
222
230
 
223
231
  ## FAQ
224
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
  *