Open
Conversation
LinusU
reviewed
Sep 22, 2021
| const Readable = require('stream').Readable | ||
| const bufferAlloc = require('buffer-alloc') | ||
| const { Readable } = require('stream') | ||
| const { Buffer } = require('buffer') |
Collaborator
There was a problem hiding this comment.
Buffer is a global in Node.js, I haven't seen much code in the wild that imports it? Is there any upside to that 🤔
Suggested change
| const { Buffer } = require('buffer') |
Author
There was a problem hiding this comment.
I know...
ever since xo started doing it i thought "ok, why not do it?" what could possible be the reason for it?
- If you don't require/import Buffer then you have the rule that
no-undefinedor "no global" will throw an error for in many cases/projectsd. - it makes it harder for other compiles to "walk" the AST and figure out if something is using some global stuff and then polyfill it.
- it just makes something as simple as Ryan dhal stated: just include that little extra extension for imports, it makes it much easier for the resolver to figure out what to include
- You also got the Blob at the buffer module
import { Blob, } from 'buffer'so why not userequire('buffer').Buffer for the same reason?It's such a small thing that you can include that otherwise makes some task so simple for compilers solve in complex ways. you don't see things like global windows stuff being included into node (cuz it's always node first and DOM second)- make it explicitly and you will know what you depend upon
- i don't know if it can help make some stuff friendlier for Deno also
- another reason could possible be to use sourcemaps to override what is imported
on 2nd note i think it's better to use Uint8Array + DataView, TextEncoder/Decoder instead :P
better cross comp
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
buffer.alloc exist natively now