Skip to content

Conversation

@jimmywarting
Copy link

buffer.alloc exist natively now

const Readable = require('stream').Readable
const bufferAlloc = require('buffer-alloc')
const { Readable } = require('stream')
const { Buffer } = require('buffer')
Copy link
Collaborator

Choose a reason for hiding this comment

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

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')

Copy link
Author

@jimmywarting jimmywarting Sep 22, 2021

Choose a reason for hiding this comment

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

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-undefined or "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 use require('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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants