-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Put Buffer polyfills in a list #1674
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
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -87,27 +87,36 @@ your users will not observe a runtime deprecation warning when running your code | |
| <a id="variant-2"></a> | ||
| ## Variant 2: Use a polyfill | ||
|
|
||
| Utilize [safer-buffer](https://www.npmjs.com/package/safer-buffer) as a polyfill to support older | ||
| Node.js versions. | ||
|
|
||
| You would take exactly the same steps as in [Variant 1](#variant-1), but with a polyfill | ||
| `const Buffer = require('safer-buffer').Buffer` in all files where you use the new `Buffer` API. | ||
|
|
||
| Make sure that you do not use old `new Buffer` API — in any files where the line above is added, | ||
| using old `new Buffer()` API will _throw_. It will be easy to notice that in CI, though. | ||
|
|
||
| Alternatively, you could use [buffer-from](https://www.npmjs.com/package/buffer-from) and/or | ||
| [buffer-alloc](https://www.npmjs.com/package/buffer-alloc) [ponyfills](https://ponyfill.com/) — | ||
| those are great, the only downsides being 4 additional dependencies and slightly more code changes to | ||
| migrate off them (as you would be using e.g. `Buffer.from` under a different name). If you need only | ||
| `Buffer.from` polyfilled — `buffer-from` alone which comes with no extra dependencies. | ||
|
|
||
| _Alternatively, you could use [safe-buffer](https://www.npmjs.com/package/safe-buffer) — it also | ||
| provides a polyfill, but takes a different approach which has | ||
| [its drawbacks](https://github.com/chalker/safer-buffer#why-not-safe-buffer). It will allow you | ||
| to also use the older `new Buffer()` API in your code, though — but that's arguably a benefit, as | ||
| it is problematic, can cause issues in your code, and will start emitting runtime deprecation | ||
| warnings starting with Node.js 10._ | ||
| There are three different polyfills available: | ||
|
|
||
| - **[safer-buffer](https://www.npmjs.com/package/safer-buffer)** is a drop-in replacement for the | ||
| entire `Buffer` API, that will _throw_ when using `new Buffer()`. | ||
|
|
||
| You would take exactly the same steps as in [Variant 1](#variant-1), but with a polyfill | ||
| `const Buffer = require('safer-buffer').Buffer` in all files where you use the new `Buffer` API. | ||
|
|
||
| Make sure that you do not use old `new Buffer` API — in any files where the line above is added, | ||
| using old `new Buffer()` API will _throw_. It will be easy to notice that in CI, though. | ||
|
|
||
| - **[buffer-from](https://www.npmjs.com/package/buffer-from) and/or | ||
| [buffer-alloc](https://www.npmjs.com/package/buffer-alloc)** are | ||
| [ponyfills](https://ponyfill.com/) for their respective part of the `Buffer` API. You only need | ||
| to add the package(s) corresponding to the API you are using. | ||
|
|
||
| You would import the module needed with an appropriate name, e.g. | ||
| `const bufferFrom = require('buffer-from')` and then use that instead of the call to | ||
| `new Buffer`, e.g. `new Buffer('test')` becomes `bufferFrom('test')`. | ||
|
|
||
| A downside with this approach is slightly more code changes to migrate off them (as you would be | ||
| using e.g. `Buffer.from` under a different name). | ||
|
|
||
| - **[safe-buffer](https://www.npmjs.com/package/safe-buffer)** is also a drop in replacement for | ||
|
||
| the entire `Buffer` API, but using `new Buffer()` will still work as before. | ||
|
|
||
| A downside to this approach is that it will allow you to also use the older `new Buffer()` API | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: with this approach |
||
| in your code, which is problematic since it can cause issues in your code, and will start | ||
| emitting runtime deprecation warnings starting with Node.js 10 | ||
| ([read more here](https://github.com/chalker/safer-buffer#why-not-safe-buffer)). | ||
|
|
||
| Note that in either case, it is important that you also remove all calls to the old Buffer | ||
| API manually — just throwing in `safe-buffer` doesn't fix the problem by itself, it just provides | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: