Skip to content

Commit 19edcb5

Browse files
committed
docs(NODE-4804): Improve migration guide notes
1 parent 491d8b7 commit 19edcb5

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

docs/upgrade-to-v5.md

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
# Changes in v5
22

3+
**TOC**
4+
5+
- [Remove reliance on Node.js Buffer](#remove-reliance-on-nodejs-buffer)
6+
- [APIs impacted](#apis-impacted)
7+
- [Restrict supported encodings in `ObjectId.toString` / `UUID.toString` / `Binary.toString`](#restrict-supported-encodings-in--objectidtostring-----uuidtostring-----binarytostring--to-the-following-encodings----hex-----base64-----utf8--)
8+
- [Migration Example](#migration-example)
9+
- [`serializeFunctions` bug fix](#-serializefunctions--bug-fix)
10+
11+
312
## About
413

514
The following is a detailed collection of the changes in the major v5 release of the bson package
@@ -11,21 +20,33 @@ for nodejs and web platforms.
1120
3. if applicable, an example of suggested syntax change (can be included in (1) )
1221
-->
1322

14-
### Remove reliance on Node.js Buffer
23+
## Remove reliance on Node.js Buffer
1524

1625
> **TL;DR**: Web environments return Uint8Array; Node.js environments return Buffer
1726
1827
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.
1928

2029
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.
2130

22-
### `ObjectId.toString` / `UUID.toString` / `Binary.toString`
31+
### APIs impacted
32+
33+
The following APIs now return Uint8Arrays when the library is loaded in an environment that does not define a global nodejs Buffer.
34+
35+
- `Binary.prototype.buffer`
36+
- `Binary.prototype.read()`
37+
- `Binary.prototype.value()`
38+
- `Decimal128.prototype.bytes`
39+
- `ObjectId.prototype.id`
40+
- `ObjectId.generate()`
41+
- `serialize()`
42+
- `UUID.prototype.id`
43+
- `UUID.generate()`
2344

24-
> **TL;DR**: These `toString` methods only support the following encodings: 'hex', 'base64', 'utf8'
45+
## Restrict supported encodings in `ObjectId.toString` / `UUID.toString` / `Binary.toString` to the following encodings: `'hex' | 'base64' | 'utf8'`
2546

2647
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:
2748

28-
##### Migration Example:
49+
### Migration Example
2950

3051
```typescript
3152
// Given Binary constructed from one of the encodings (using 'utf16le' as an example here)
@@ -39,7 +60,7 @@ bin.value(true).toString('utf16le');
3960
new TextDecoder('utf-16le').decode(bin.value(true));
4061
```
4162

42-
### `serializeFunctions` bug fix
63+
## `serializeFunctions` bug fix
4364

4465
> **TL;DR**: TODO
4566

0 commit comments

Comments
 (0)