Skip to content
File

Blob: src/node/internal/internal_timers_global_override.ts

typescript28 lines
1// Copyright (c) 2017-2025 Cloudflare, Inc.
2// Licensed under the Apache 2.0 license found in the LICENSE file or at:
3// https://opensource.org/licenses/Apache-2.0
4 
5// This module overrides globalThis timer functions with Node.js-compatible versions
6// when loaded. It is loaded by worker.c++ when enable_nodejs_global_timers compat
7// flag is enabled.
8//
9// After loading:
10// - globalThis.setTimeout and globalThis.setInterval return Timeout objects
11// with methods like refresh(), ref(), unref(), and hasRef() instead of numeric IDs
12// - globalThis.setImmediate and globalThis.clearImmediate are available
13 
14import {
15 setTimeout,
16 setInterval,
17 clearTimeout,
18 clearInterval,
19} from 'node-internal:internal_timers';
20 
21globalThis.setTimeout = setTimeout as unknown as typeof globalThis.setTimeout;
22globalThis.setInterval =
23 setInterval as unknown as typeof globalThis.setInterval;
24globalThis.clearTimeout =
25 clearTimeout as unknown as typeof globalThis.clearTimeout;
26globalThis.clearInterval =
27 clearInterval as unknown as typeof globalThis.clearInterval;