Skip to content

Global timer function do not match Node API in nodejs_compat mode #5662

@vicb

Description

@vicb

As discussed when the Node timers were implemented in January 25 (on this PR), the global timer functions (setTimeout, setInterval) do not implement the Node API but the Web Platform API:

// This code works fine in Node but crashes in workerd  
const timeout = /*globalThis.*/setTimeout(...);
timeout.refresh(); // crashes, timeout is a number

// However this works fine in both Node and Workerd
import nodeTimers from `node:timers`;
const timeout = nodeTimers.setTimeout(...);
timeout.refresh();

This has been discussed with @jasnell and @danlapid

One of the problem here is that the timers module calls in the global functions which make it quite complex to polyfill the functions without creating an infinite loop.

One workaround is to inject the following code at the top of the generated JS bundle:

import {setInterval, clearInterval, setTimeout, clearTimeout, setImmediate, clearImmediate} from "node:timers";

But this has downside, see i.e. opennextjs/opennextjs-cloudflare#1036

Could we please implement Noce compatible global timer functions behind a compatibility flag?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions