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
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.
11
20
3. if applicable, an example of suggested syntax change (can be included in (1) )
12
21
-->
13
22
14
-
###Remove reliance on Node.js Buffer
23
+
## Remove reliance on Node.js Buffer
15
24
16
25
> **TL;DR**: Web environments return Uint8Array; Node.js environments return Buffer
17
26
18
27
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.
19
28
20
29
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.
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()`
23
44
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'`
25
46
26
47
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:
27
48
28
-
#####Migration Example:
49
+
### Migration Example
29
50
30
51
```typescript
31
52
// Given Binary constructed from one of the encodings (using 'utf16le' as an example here)
0 commit comments