Skip to content
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

refactor: import timers, process, and Buffer from node: #7157

Merged
merged 4 commits into from Dec 28, 2021

Conversation

kyranet
Copy link
Member

@kyranet kyranet commented Dec 27, 2021

Please describe the changes this PR makes and why it should be merged:

Following this Tweet, this PR imports process, Buffer, and timers (setTimeout, setInterval, and setImmediate) from their respective modules.

But why?

tl;dr: if Node.js recommends a way to do something over another, we abide to that, also known as following best practices.

This has been asked several times in the server, while I do not know the very exact reason (Node.js developers aren't sharing many details in their PRs), I have read the same thing:

The imports tell bundlers (and other tools) where certain globals come from, and what kind they are, for example, Node.js's setTimeout isn't a compatible implementation of the DOM one, one returns a NodeJS.Timer and the other returns a number, however, bundlers can't tell if a package needs one or another. Importing timers from node:timers is a sensible approach to this, since it tells the bundler we're using Node.js's.

And now my theories...

It's possible that Node.js might look into deprecating some of the globals (specially those that aren't used in other platforms) in favour of importing from a package, this remains to be confirmed, but it's likely.

Also, there's a possibility they also recommend this change to protect libraries from any kind of global mutation, it only takes an inexperienced developer writing process = something to trigger a quite funny error to debug. Since the imports are const-assigned, we keep a reference of those global objects during the entire runtime, and as such, this is far less likely to happen, also with ESM, developers won't be able to change the exports of modules, so that's one problem that'll be fixed in the (far) future.

For the performance kids, because the globals are now const assigned, it's faster than pulling from the global, since the value cannot change.

Status and versioning classification:

  • Code changes have been tested against the Discord API, or there are no code changes
  • I know how to update typings and have done so, or typings don't need updating

test/webhooktest.js Outdated Show resolved Hide resolved
Co-authored-by: muchnameless <12682826+muchnameless@users.noreply.github.com>
@almeidx
Copy link
Member

almeidx commented Dec 27, 2021

Might be worth configuring the no-restricted-globals eslint rule to disallow the usage of the global process, Buffer, etc.

.eslintrc.json Outdated Show resolved Hide resolved
.eslintrc.json Outdated Show resolved Hide resolved
.eslintrc.json Outdated Show resolved Hide resolved
.eslintrc.json Outdated Show resolved Hide resolved
.eslintrc.json Outdated Show resolved Hide resolved
Co-authored-by: Almeida <almeidx@pm.me>
@blattersturm
Copy link

blattersturm commented Dec 31, 2021

I'm still missing clearTimeout and clearInterval being imported here in files such as GuildMemberManager.js. This may work well in pure Node runtimes, but elsewhere this may lead to a mismatch.

@kyranet
Copy link
Member Author

kyranet commented Dec 31, 2021

Oh. Good catch, I'll make a PR in a bit.

@kyranet kyranet deleted the refactor/import-globals-from-modules branch December 31, 2021 10:43
@blattersturm
Copy link

@kyranet have you managed to get this fixed yet? People are still hitting issues in our embedded runtime.

@kyranet
Copy link
Member Author

kyranet commented Jan 14, 2022

Yes, I just opened #7269 to fix that, sorry for the delay, I have been busy with exams 😅

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

Successfully merging this pull request may close these issues.

None yet

7 participants