You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
-[TS "target" set to es2020](#ts-target-set-to-es2020)
13
13
14
14
## About
15
15
16
-
The following is a detailed collection of the changes in the major v5 release of the bson package
17
-
for Node.js and web platforms.
16
+
The following is a detailed collection of the changes in the major v5 release of the BSON package for Node.js and web platforms.
18
17
19
18
<!--
20
19
1. a brief statement of what is breaking (brief as in "x will now return y instead of z", or "x is no longer supported, use y instead", etc
@@ -26,15 +25,15 @@ for Node.js and web platforms.
26
25
27
26
### Remove reliance on Node.js Buffer
28
27
29
-
> **TL;DR**: Impacted APIs now return Uint8Array in web environments; Node.js environments unaffected
28
+
> **TL;DR**: Impacted APIs now return `Uint8Array` in web environments; Node.js environments are unaffected
30
29
31
-
For those that use the BSON library on Node.js, there is no change - the BSON APIs will still return and accept instances of Node.js Buffer. Since we no longer depend on the Buffer web shim for compatibility with browsers, in non-Node.js environments a Uint8Array will be returned instead.
30
+
For those that use the BSON library on Node.js, there is no change - the BSON APIs will still return and accept instances of Node.js `Buffer`. Since we no longer depend on the `Buffer` web shim for compatibility with browsers, in non-Node.js environments a `Uint8Array` will be returned instead.
32
31
33
-
This allows the BSON library to be better at platform independence while keeping its behavior consistent cross platform. The Buffer shim served the library well but brought in more than was necessary for the concerns of the code here.
32
+
This allows the BSON library to be more platform independent while keeping its behavior consistent cross platform.
34
33
35
34
#### APIs impacted
36
35
37
-
The following APIs now return Uint8Arrays when the library is loaded in an environment that does not define a global Node.js Buffer.
36
+
The following APIs now return `Uint8Arrays` when the library is loaded in an environment that does not define a global Node.js `Buffer`.
38
37
39
38
-`Binary.prototype.buffer`
40
39
-`Binary.prototype.read()`
@@ -50,7 +49,7 @@ The following APIs now return Uint8Arrays when the library is loaded in an envir
50
49
51
50
> **TL;DR**: The only supported encodings are: `'hex' | 'base64' | 'utf8'`
52
51
53
-
The methods: `ObjectId.toString`, `UUID.toString`, and `Binary.toString` took encodings that were passed through to the Node.js Buffer API. As a result of no longer relying on the presence of `Buffer` we can no longer support [every encoding that Node.js does](https://nodejs.org/dist/latest-v16.x/docs/api/buffer.html#buffers-and-character-encodings). We continue to support `'hex'` and `'base64'` on all three methods and additionally `'utf-8' | 'utf8'` on `Binary.toString`. If any of the other encodings are desired the underlying buffer for all these classes are publicly accessible and while in Node.js will be stored as a Node.js buffer:
52
+
The methods: `ObjectId.toString`, `UUID.toString`, and `Binary.toString` took encodings that were passed through to the Node.js `Buffer` API. As a result of no longer relying on the presence of `Buffer` we can no longer support [every encoding that Node.js does](https://nodejs.org/dist/latest-v16.x/docs/api/buffer.html#buffers-and-character-encodings). We continue to support `'hex'` and `'base64'` on all three methods and additionally `'utf-8' | 'utf8'` on `Binary.toString`. If any of the other encodings are desired the underlying buffer for all these classes are publicly accessible and while in Node.js will be stored as a Node.js buffer:
54
53
55
54
#### Migration Example
56
55
@@ -70,34 +69,36 @@ new TextDecoder('utf-16le').decode(bin.value(true));
70
69
71
70
### TS "target" set to es2020
72
71
73
-
We have set our typescript compilation target to `es2020` which aligns with our minimum supported Node.js version 14+. The following is from the typescript release notes on es2020 support, so it's some of the syntax that can be expected to be preserved after compilation:
72
+
We have set our TypeScript compilation target to `es2020` which aligns with our minimum supported Node.js version 14+. The following is from the [TypeScript release notes on `es2020` support](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-8.html#es2020-for-target-and-module), so it's some of the syntax that can be expected to be preserved after compilation:
74
73
75
74
> This will preserve newer ECMAScript 2020 features like optional chaining, nullish coalescing, export \* as ns, and dynamic import(...) syntax. It also means bigint literals now have a stable target below esnext.
76
75
77
-
### serializeFunctions bug fix
76
+
### `serializeFunctions` bug fix
78
77
79
-
If serializeFunctions was enabled and the functions being serialized had a name that is outside of [Controls and Basic Latin](https://en.wikibooks.org/wiki/Unicode/Character_reference/0000-0FFF) character ranges (a.k.a utf8 bytes: 0x00-0x7F) they would be incorrectly serialized.
78
+
If `serializeFunctions` was enabled and the functions being serialized had a name that is outside of [Controls and Basic Latin](https://en.wikibooks.org/wiki/Unicode/Character_reference/0000-0FFF) character ranges (a.k.a utf8 bytes: `0x00-0x7F`) they would be incorrectly serialized.
80
79
81
80
### Remove `Map` export
82
81
83
-
This library no longer polyfills [ES Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) and the export "Map" was removed. Users should migrate to using the global Map constructor available in all supported JS environments.
82
+
This library no longer polyfills [ES Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) and the export "`Map`" was removed. Users should migrate to using the global `Map` constructor available in all supported JS environments.
84
83
85
84
### `Decimal128``toObject()` mapper support removed
86
85
87
-
`Decimal128` can no longer have a `toObject()` method added on to its prototype for mapping to a custom value. This feature was undocumented and inconsistent with the rest of our BSON types. At this time there is no direct migration: cursors in the driver support transformations via `.map`, otherwise the `Decimal128` instances will require manual transformation. There is a plan to provide a better mechanism for consistently transforming BSON values tracked in [NODE-4680](https://jira.mongodb.org/browse/NODE-4680), please feel free to add a vote or comment with a use case to help us land the feature in the most useful form.
86
+
`Decimal128` can no longer have a `toObject()` method added on to its prototype for mapping to a custom value. This feature was undocumented and inconsistent with the rest of our BSON types.
88
87
89
-
### Remove deprecated ObjectId methods
88
+
At this time there is no direct migration. Cursors in the driver support transformations via `.map`, otherwise the `Decimal128` instances will require manual transformation. There is a plan to provide a better mechanism for consistently transforming BSON values tracked in [NODE-4680](https://jira.mongodb.org/browse/NODE-4680). Please feel free to add a vote or comment with a use case to help us land the feature in the most useful form.
89
+
90
+
### Remove deprecated `ObjectId` methods
90
91
91
92
The following deprecated methods have been removed:
92
93
93
94
-`ObjectId.prototype.generate`
94
-
- Instead, generate a new ObjectId with the constructor: `new ObjectId()` or using the `static generate(time?: number)` method.
95
+
- Instead, generate a new `ObjectId` with the constructor: `new ObjectId()` or using the `static generate(time?: number)` method.
95
96
-`ObjectId.prototype.generationTime`
96
97
- Instead, use `static createFromTime()` and `getTimestamp()` to set and inspect these values on an `ObjectId()`
97
98
-`ObjectId.prototype.getInc`
98
99
-`ObjectId.prototype.get_inc`
99
100
-`ObjectId.get_inc`
100
-
- The `static getInc()` is private since invoking it increments the next `ObjectId` index, so invoking would impact the creation of subsequent ObjectIds.
101
+
- The `static getInc()` is private since invoking it increments the next `ObjectId` index, so invoking would impact the creation of subsequent `ObjectId`s.
101
102
102
103
### BSON Element names are now fetched only from object's own properties
For clarity the deprecated and duplicate export `ObjectID` has been removed. `ObjectId` matches the class name and is equal in every way to the capital "D" export.
135
136
136
137
### Timestamp constructor validation
137
138
138
-
The `Timestamp` type no longer accepts two number arguments for the low and high bits of the int64 value.
139
+
The `Timestamp` type no longer accepts two number arguments for the low and high bits of the `int64` value.
139
140
140
141
Supported constructors are as follows:
141
142
@@ -147,7 +148,7 @@ class Timestamp {
147
148
}
148
149
```
149
150
150
-
Any code that use the two number argument style of constructing a Timestamp will need to be migrated to one of the supported constructors. We recommend using the `{ t: number; i: number }` style input, representing the timestamp and increment respectively.
151
+
Any code that uses the two number argument style of constructing a `Timestamp` will need to be migrated to one of the supported constructors. We recommend using the `{ t: number; i: number }` style input, representing the timestamp and increment respectively.
151
152
152
153
```typescript
153
154
// in 4.x BSON
@@ -156,7 +157,7 @@ new Timestamp(1, 2); // as an int64: 8589934593
156
157
newTimestamp({ t: 2, i: 1 }); // as an int64: 8589934593
157
158
```
158
159
159
-
Additionally, the `t` and `i` fields of `{ t: number; i: number }` are now validated more strictly to ensure your Timestamps are being constructed as expected.
160
+
Additionally, the `t` and `i` fields of `{ t: number; i: number }` are now validated more strictly to ensure your timestamps are being constructed as expected.
160
161
161
162
For example:
162
163
```typescript
@@ -183,8 +184,8 @@ EJSON.stringify({}, { strict: false }); /* migrate to */ EJSON.stringify({}, { r
183
184
184
185
### The BSON default export has been removed.
185
186
186
-
* If you import BSON commonjs style `const BSON = require('bson')` then the `BSON.default` property is no longer present.
187
-
* If you import BSON esmodule style `import BSON from 'bson'` then this code will crash upon loading.**TODO: This is not the case right now but it will be after NODE-4713.**
187
+
* If you import BSON `commonjs` style `const BSON = require('bson')` then the `BSON.default` property is no longer present.
188
+
* If you import BSON `esmodule` style `import BSON from 'bson'` then this code will crash upon loading.
188
189
* This error will throw: `SyntaxError: The requested module 'bson' does not provide an export named 'default'`.
189
190
190
191
### `class Code` always converts `.code` to string
@@ -201,9 +202,10 @@ const myCode = new Code(function iLoveJavascript() { console.log('I love javascr
201
202
### `BSON.deserialize()` only returns `Code` instances
202
203
203
204
The deserialize options: `evalFunctions`, `cacheFunctions`, and `cacheFunctionsCrc32` have been removed.
204
-
The `evalFunctions` option, when enabled, would return BSON Code typed values as eval-ed javascript functions, now it will always return Code instances.
205
+
The `evalFunctions` option, when enabled, would return BSON `Code` typed values as eval-ed JavaScript functions, now it will always return `Code` instances.
205
206
206
207
See the following snippet for how to migrate:
208
+
207
209
```typescript
208
210
const bsonBytes =BSON.serialize(
209
211
{ iLoveJavascript: function () { console.log('I love javascript') } },
@@ -221,7 +223,7 @@ iLoveJavascript();
221
223
### `BSON.serialize()` validation
222
224
223
225
The BSON format does not support encoding arrays as the **root** object.
224
-
However, in javascript arrays are just objects where the keys are numeric (and a magic `length` property), so round tripping an array (ex. `[1, 2]`) though BSON would return `{ '0': 1, '1': 2 }`.
226
+
However, in JavaScript arrays are just objects where the keys are numeric (and a magic `length` property), so round tripping an array (ex. `[1, 2]`) though BSON would return `{ '0': 1, '1': 2 }`.
225
227
226
228
`BSON.serialize()` now validates input types, the input to serialize must be an object or a `Map`, arrays will now cause an error.
227
229
@@ -230,7 +232,7 @@ BSON.serialize([1, 2, 3])
230
232
// BSONError: serialize does not support an array as the root input
231
233
```
232
234
233
-
if the functionality of turning arrays into an object with numeric keys is useful see the following example:
235
+
If the functionality of turning arrays into an object with numeric keys is useful, see the following example:
234
236
235
237
```typescript
236
238
// Migration example:
@@ -242,20 +244,21 @@ BSON.deserialize(result)
242
244
### Exports and available bundles
243
245
244
246
Most users should be unaffected by these changes, Node.js `require()` / Node.js `import` will fetch the corresponding BSON library as expected.
245
-
And for folks using bundlers like, webpack or rollup a tree shakable es module bundle will be pulled in because of the settings in our package.json.
247
+
And for folks using bundlers like webpack or rollup, a tree shakable ES module bundle will be pulled in because of the settings in our `package.json`.
246
248
247
-
Our package.json defines the following `"exports"` settings.
249
+
Our `package.json` defines the following `"exports"` settings.
248
250
```json
249
251
{
250
252
"main": "./lib/bson.cjs",
251
253
"module": "./lib/bson.mjs",
252
-
"browser": "./lib/bson.mjs",
253
254
"exports": {
254
-
"browser": "./lib/bson.mjs",
255
255
"import": "./lib/bson.mjs",
256
-
"require": "./lib/bson.cjs"
257
-
}
256
+
"require": "./lib/bson.cjs",
257
+
"react-native": "./lib/bson.cjs",
258
+
"browser": "./lib/bson.mjs"
259
+
},
258
260
}
261
+
259
262
```
260
263
261
264
You can now find compiled bundles of the BSON library in 3 common formats in the `lib` directory.
@@ -267,14 +270,16 @@ You can now find compiled bundles of the BSON library in 3 common formats in the
267
270
268
271
### `BSONTypeError` removed and `BSONError` offers filtering functionality with `static isBSONError()`
269
272
270
-
`BSONTypeError` has been removed because it was not a subclass of BSONError so would not return true for an `instanceof` check against `BSONError`. To learn more about our expectations of error handling see [this section of the mongodb driver's readme](https://github.com/mongodb/node-mongodb-native/tree/main#error-handling).
273
+
`BSONTypeError` has been removed because it was not a subclass of BSONError so would not return true for an `instanceof` check against `BSONError`. To learn more about our expectations of error handling see [this section of the MongoDB Node.js Driver's README](https://github.com/mongodb/node-mongodb-native/tree/main#error-handling).
271
274
272
275
273
276
A `BSONError` can be thrown from deep within a library that relies on BSON, having one error super class for the library helps with programmatic filtering of an error's origin.
277
+
274
278
Since BSON can be used in environments where instances may originate from across realms, `BSONError` has a static `isBSONError()` method that helps with determining if an object is a `BSONError` instance (much like [Array.isArray](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray)).
279
+
275
280
It is our recommendation to use `isBSONError()` checks on errors and to avoid relying on parsing `error.message` and `error.name` strings in your code. We guarantee `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.
276
281
277
-
Hypothetical example: A collection in our Db has an issue with UTF-8 data:
282
+
Hypothetical example: A collection in our database contains invalid UTF-8 data:
Starting with v5.0.0 of the BSON library instances of types from previous versions will throw an error when passed to the serializer.
295
-
This is to ensure that types are always serialized correctly and that there is no unexpected silent BSON serialization mistakes that could occur when mixing versions.
299
+
Starting with v5.0.0 of the BSON library instances of types from previous versions will throw an error when passed to the serializer. This is to ensure that types are always serialized correctly and that there is no unexpected silent BSON serialization mistakes that could occur when mixing versions.
300
+
296
301
It's unexpected for any applications to have more than one version of the BSON library but with nested dependencies and re-exporting, this new error will illuminate those incorrect combinations.
0 commit comments