-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5ee472c
commit 30900ab
Showing
4 changed files
with
105 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
--- | ||
title: Goodbye, Node.js Buffer | ||
description: It's time to move from Buffer to Uint8Array. | ||
pubDate: 2023-10-24 | ||
tags: | ||
- nodejs | ||
- javascript | ||
- open-source | ||
--- | ||
|
||
The [`Buffer`](https://nodejs.org/api/buffer.html) type has been the cornerstone for binary data handling in Node.js since the beginning. However, these days we have [`Uint8Array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array), which is a native JavaScript type and works cross-platform. While `Buffer` is an instance of `Uint8Array`, it introduces numerous methods that are not available in other JavaScript environments. Consequently, code leveraging Buffer-specific methods needs polyfilling, preventing many valuable packages from being browser-compatible. | ||
|
||
`Buffer` also comes with additional caveats. For instance, `buffer.slice()` creates a mutable segment linked to the original `Buffer`, while `uint8array.slice()` creates an immutable copy, resulting in possible unpredictable behavior. Furthermore, [buffers expose private information](https://github.com/nodejs/node/issues/41588#issuecomment-1016269584) through global variables, a potential security risk. | ||
|
||
It's time to move on. | ||
|
||
## The Plan | ||
|
||
I intend to move [all my packages](https://github.com/search?q=owner%3Asindresorhus+%22node%3Abuffer%22&type=code) from using `Buffer` to `Uint8Array`. If you are a maintainer of an npm package, I encourage you to do the same. | ||
|
||
`Buffer` will never be removed, and probably never even deprecated, but at least the community can slowly move away from it. My hope is that the Node.js team will at least start discouraging the use of `Buffer`. | ||
|
||
## How | ||
|
||
First, make yourself familiar with the [subtle incompatibilities](https://nodejs.org/api/buffer.html#buffers-and-typedarrays) between `Uint8Array` and `Buffer`. | ||
|
||
I have made the [`uint8array-extras` package](https://github.com/sindresorhus/uint8array-extras) to make the transition easier. Pull requests are welcome for additional utilities. | ||
|
||
If your code accepts a `Buffer` and don't use any `Buffer`-specific methods, you can simply update your docs and types to `Uint8Array`. Changing the input type from `Buffer` to `Uint8Array` is a non-breaking change since `Buffer` is an instance of `Uint8Array`. | ||
|
||
Changing the return type from `Buffer` to `Uint8Array` is a breaking change. | ||
|
||
If you really have to convert to a `Buffer`, you can use `Buffer.from(uint8Array)`. However, there's usually a better way. | ||
|
||
## Enforcement | ||
|
||
I recommend enforcing `Uint8Array` over `Buffer` with linting. | ||
|
||
Add this to your ESLint config: | ||
|
||
```js | ||
{ | ||
'no-restricted-globals': [ | ||
'error', | ||
{ | ||
name: 'Buffer', | ||
message: 'Use Uint8Array instead.' | ||
} | ||
], | ||
'no-restricted-imports': [ | ||
'error', | ||
{ | ||
name: 'buffer', | ||
message: 'Use Uint8Array instead.' | ||
}, | ||
{ | ||
name: 'node:buffer', | ||
message: 'Use Uint8Array instead.' | ||
} | ||
] | ||
} | ||
``` | ||
|
||
And if you use TypeScript, add this: | ||
|
||
```js | ||
{ | ||
'@typescript-eslint/ban-types': [ | ||
'error', | ||
{ | ||
types: { | ||
Buffer: { | ||
message: 'Use Uint8Array instead.', | ||
suggest: [ | ||
'Uint8Array' | ||
] | ||
} | ||
} | ||
} | ||
] | ||
} | ||
``` | ||
|
||
If you use [XO](https://github.com/xojs/xo), it will soon come with this config by default. | ||
|
||
## How Can I Help? | ||
|
||
[Voice your support](https://github.com/nodejs/node/issues/41588) for Node.js using `Uint8Array` for new APIs. | ||
|
||
Help me move [my packages](https://github.com/search?q=owner%3Asindresorhus+%22node%3Abuffer%22&type=code) to `Uint8Array`. Pick one and give it a go. | ||
|
||
Help us make a [lint rule](https://github.com/sindresorhus/eslint-plugin-unicorn/issues/1808) to prevent the use of `Buffer` methods. | ||
|
||
## The End | ||
|
||
Let's make the JavaScript package ecosystem more cross-platform. Thanks for reading. | ||
|
||
[Discuss](https://github.com/sindresorhus/meta/discussions/22) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters