Skip to content

feat!(NODE-4802): Refactor BSON to work with cross platform JS APIs #518

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 22 commits into from
Nov 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
},
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 2017,
"ecmaVersion": 2020,
"project": [
"./tsconfig.json"
]
Expand Down
23 changes: 11 additions & 12 deletions .evergreen/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -122,17 +122,16 @@ tasks:
- func: run tests
vars:
TEST_TARGET: node
# TODO(NODE-3555): Karma tests pass locally still after these changes, rhel80 is just missing chrome
# - name: browser-tests
# tags: ["browser"]
# commands:
# - func: fetch source
# vars:
# NODE_MAJOR_VERSION: 16
# - func: install dependencies
# - func: run tests
# vars:
# TEST_TARGET: browser
- name: web-tests
tags: ["web"]
commands:
- func: fetch source
vars:
NODE_MAJOR_VERSION: 18
- func: install dependencies
- func: run tests
vars:
TEST_TARGET: web
- name: run-checks
tags:
- run-checks
Expand Down Expand Up @@ -177,7 +176,7 @@ buildvariants:
- name: linux
display_name: RHEL 8.0
run_on: rhel80-small
tasks: [".node"]
tasks: [".node", ".web"]
- name: lint
display_name: lint
run_on: rhel80-small
Expand Down
6 changes: 3 additions & 3 deletions .evergreen/run-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ case $1 in
"node")
npm run check:coverage
;;
"browser")
# TODO(NODE-3555): remove explicit browser tests
npm run test-browser
"web")
export WEB="true"
npm run check:web
;;
*)
npm test
Expand Down
3 changes: 1 addition & 2 deletions .mocharc.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
"$schema": "https://raw.githubusercontent.com/SchemaStore/schemastore/master/src/schemas/json/mocharc.json",
"require": [
"source-map-support/register",
"ts-node/register",
"./node_modules/chai/register-expect"
"ts-node/register"
],
"extension": [
"js",
Expand Down
11 changes: 7 additions & 4 deletions .nycrc.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
{
"extends": "@istanbuljs/nyc-config-typescript",
"include": [
"src/**/*"
],
"reporter": [
"lcovonly",
"text-summary",
"html"
],
"statements": 53,
"branches": 38,
"functions": 52,
"lines": 54
"statements": 81,
"branches": 73,
"functions": 74,
"lines": 83
}
46 changes: 46 additions & 0 deletions docs/upgrade-to-v5.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Changes in v5

## About

The following is a detailed collection of the changes in the major v5 release of the bson package
for nodejs and web platforms.

<!--
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
2. a brief statement of why we are breaking it (bug, not useful, inconsistent behavior, better alternative, etc)
3. if applicable, an example of suggested syntax change (can be included in (1) )
-->

### Remove reliance on Node.js Buffer

> **TL;DR**: Web environments return Uint8Array; Node.js environments return Buffer

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.

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.

### `ObjectId.toString` / `UUID.toString` / `Binary.toString`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
### `ObjectId.toString` / `UUID.toString` / `Binary.toString`
### Restrict supported encodings in `ObjectId.toString` / `UUID.toString` / `Binary.toString`


> **TL;DR**: These `toString` methods only support the following encodings: 'hex', 'base64', 'utf8'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if the 'utf8' encoding isn't supported on ObjectId.toString or UUID.toString, then this summary is incorrect

I actually think that what you currently have as a "tl;dr" can be simply the sub-heading for the corresponding set of changes. I think it would also make the table of contents easier to generate. In the case of this TL;DR, it can be removed entirely if you update L22 the way I am suggesting; then the paragraph underneath can elaborate on the specifics

Then the TL;DR of the section above (L16) can be promoted to a subheading, with both of these sections being under the title "Remove reliance on Node.js buffer", which would be the grouping heading.

So in this guide, the grouping headings will indicate the themes of the changes, and the subheadings in the groups will be the actual breaking change from the user's perspective. (What I mean is, "remove reliance on nodejs buffer" isn't the breaking change, it's the cause of the breaking changes whose summaries follow)

As for the 3rd example, I think we can group it under "Other" for now


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:

##### Migration Example:

```typescript
// Given Binary constructed from one of the encodings (using 'utf16le' as an example here)
// no longer supported directly by the Binary.toString method
const bin = new Binary(Buffer.from('abc', 'utf16le'), 0);
// To obtain the original translation of bytes to string
// We can access the underlying buffer and on Node.js it will be an instanceof Buffer
// so it will support the translation to the specified encoding.
bin.value(true).toString('utf16le');
// In web environments (and Node.js) the same can be accomplished with TextDecoder
new TextDecoder('utf-16le').decode(bin.value(true));
```

### `serializeFunctions` bug fix

> **TL;DR**: TODO

TODO(NODE-4771): serializeFunctions bug fix makes function names outside the ascii range get serialized correctly
91 changes: 0 additions & 91 deletions karma.conf.js

This file was deleted.

Loading