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

tools: add timers functions to the list of restricted globals #42013

Merged
merged 3 commits into from Feb 18, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions lib/.eslintrc.yaml
Expand Up @@ -79,6 +79,12 @@ rules:
message: Use `const { atob } = require('buffer');` instead of the global.
- name: btoa
message: Use `const { btoa } = require('buffer');` instead of the global.
- name: clearImmediate
message: Use `const { clearImmediate } = require('timers');` instead of the global.
- name: clearInterval
message: Use `const { clearInterval } = require('timers');` instead of the global.
- name: clearTimeout
message: Use `const { clearTimeout } = require('timers');` instead of the global.
- name: crypto
message: Use `const { crypto } = require('internal/crypto/webcrypto');` instead of the global.
- name: Crypto
Expand All @@ -93,6 +99,12 @@ rules:
message: Use `const { performance } = require('perf_hooks');` instead of the global.
- name: queueMicrotask
message: Use `const { queueMicrotask } = require('internal/process/task_queues');` instead of the global.
- name: setImmediate
message: Use `const { setImmediate } = require('timers');` instead of the global.
- name: setInterval
message: Use `const { setInterval } = require('timers');` instead of the global.
- name: setTimeout
message: Use `const { setTimeout } = require('timers');` instead of the global.
- name: structuredClone
message: Use `const { structuredClone } = require('internal/structured_clone');` instead of the global.
- name: SubtleCrypto
Expand Down
10 changes: 6 additions & 4 deletions lib/timers/promises.js
Expand Up @@ -7,13 +7,19 @@ const {
ReflectConstruct,
SafePromisePrototypeFinally,
Symbol,
globalThis,
} = primordials;

const {
Timeout,
Immediate,
insert
} = require('internal/timers');
const {
clearImmediate,
clearInterval,
clearTimeout,
} = globalThis;
aduh95 marked this conversation as resolved.
Show resolved Hide resolved

const {
AbortError,
Expand Down Expand Up @@ -73,7 +79,6 @@ function setTimeout(after, value, options = {}) {
insert(timeout, timeout._idleTimeout);
if (signal) {
oncancel = FunctionPrototypeBind(cancelListenerHandler,
// eslint-disable-next-line no-undef
timeout, clearTimeout, reject, signal);
signal.addEventListener('abort', oncancel);
}
Expand Down Expand Up @@ -117,7 +122,6 @@ function setImmediate(value, options = {}) {
if (!ref) immediate.unref();
if (signal) {
oncancel = FunctionPrototypeBind(cancelListenerHandler,
// eslint-disable-next-line no-undef
immediate, clearImmediate, reject,
signal);
signal.addEventListener('abort', oncancel);
Expand Down Expand Up @@ -153,7 +157,6 @@ async function* setInterval(after, value, options = {}) {
insert(interval, interval._idleTimeout);
if (signal) {
onCancel = () => {
// eslint-disable-next-line no-undef
clearInterval(interval);
if (callback) {
callback(
Expand All @@ -175,7 +178,6 @@ async function* setInterval(after, value, options = {}) {
}
throw new AbortError(undefined, { cause: signal?.reason });
} finally {
// eslint-disable-next-line no-undef
clearInterval(interval);
signal?.removeEventListener('abort', onCancel);
}
Expand Down