Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/node/internal/internal_timers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
// USE OR OTHER DEALINGS IN THE SOFTWARE.

import { validateFunction } from 'node-internal:validators';
import { default as timersUtil } from 'node-internal:timers';

let clearTimeoutImpl: (obj: Timeout) => void;

Expand Down Expand Up @@ -134,9 +135,9 @@ export function clearTimeout(timer: unknown): void {
}
}

export const setImmediate = globalThis.setImmediate.bind(globalThis);
export const setImmediate = timersUtil.setImmediate.bind(timersUtil);

export const clearImmediate = globalThis.clearImmediate.bind(globalThis);
export const clearImmediate = timersUtil.clearImmediate.bind(timersUtil);

export function setInterval(
callback: (...args: unknown[]) => void,
Expand Down
4 changes: 2 additions & 2 deletions src/node/internal/internal_timers_promises.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,13 @@ export async function setImmediate<T>(

const { promise, resolve, reject } = Promise.withResolvers<T>();

const timer = globalThis.setImmediate(() => {
const timer = timers.setImmediate(() => {
resolve(value as T);
});

if (signal) {
function onCancel(): void {
globalThis.clearImmediate(timer);
timers.clearImmediate(timer);
signal?.removeEventListener('abort', onCancel);
reject(new AbortError(undefined, { cause: signal?.reason }));
}
Expand Down
7 changes: 7 additions & 0 deletions src/node/internal/timers.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type {
setImmediate as setImmediateImpl,
clearImmediate as clearImmediateImpl,
} from 'node:timers';

export const setImmediate: typeof setImmediateImpl;
export const clearImmediate: typeof clearImmediateImpl;
2 changes: 2 additions & 0 deletions src/workerd/api/node/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ wd_cc_library(
"crypto-keys.c++",
"diagnostics-channel.c++",
"dns.c++",
"timers.c++",
"zlib-util.c++",
],
hdrs = [
"crypto.h",
"diagnostics-channel.h",
"dns.h",
"node.h",
"timers.h",
"zlib-util.h",
],
implementation_deps = [
Expand Down
6 changes: 4 additions & 2 deletions src/workerd/api/node/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <workerd/api/node/buffer.h>
#include <workerd/api/node/dns.h>
#include <workerd/api/node/module.h>
#include <workerd/api/node/timers.h>
#include <workerd/api/node/url.h>
#include <workerd/api/node/util.h>
#include <workerd/io/compatibility-date.h>
Expand Down Expand Up @@ -52,7 +53,8 @@ class CompatibilityFlags: public jsg::Object {
V(DiagnosticsChannelModule, "node-internal:diagnostics_channel") \
V(ZlibUtil, "node-internal:zlib") \
V(UrlUtil, "node-internal:url") \
V(DnsUtil, "node-internal:dns")
V(DnsUtil, "node-internal:dns") \
V(TimersUtil, "node-internal:timers")

// Add to the NODEJS_MODULES_EXPERIMENTAL list any currently in-development
// node.js compat C++ modules that should be guarded by the experimental compat
Expand Down Expand Up @@ -144,4 +146,4 @@ kj::Own<jsg::modules::ModuleBundle> getExternalNodeJsCompatModuleBundle(auto fea
api::node::CompatibilityFlags, EW_NODE_BUFFER_ISOLATE_TYPES, EW_NODE_CRYPTO_ISOLATE_TYPES, \
EW_NODE_DIAGNOSTICCHANNEL_ISOLATE_TYPES, EW_NODE_ASYNCHOOKS_ISOLATE_TYPES, \
EW_NODE_UTIL_ISOLATE_TYPES, EW_NODE_ZLIB_ISOLATE_TYPES, EW_NODE_URL_ISOLATE_TYPES, \
EW_NODE_MODULE_ISOLATE_TYPES, EW_NODE_DNS_ISOLATE_TYPES\
EW_NODE_MODULE_ISOLATE_TYPES, EW_NODE_DNS_ISOLATE_TYPES, EW_NODE_TIMERS_ISOLATE_TYPES\
2 changes: 1 addition & 1 deletion src/workerd/api/node/tests/timers-nodejs-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export const testSetImmediate = {

{
const { promise, resolve } = Promise.withResolvers();
globalThis.setImmediate(
timers.setImmediate(
(...args) => {
deepStrictEqual(args, [1, 2, 3]);
resolve();
Expand Down
2 changes: 1 addition & 1 deletion src/workerd/api/node/tests/timers-nodejs-test.wd-test
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const unitTests :Workerd.Config = (
(name = "worker", esModule = embed "timers-nodejs-test.js")
],
compatibilityDate = "2025-01-09",
compatibilityFlags = ["nodejs_compat"],
compatibilityFlags = ["nodejs_compat", "no_nodejs_compat_v2"],
)
),
],
Expand Down
27 changes: 27 additions & 0 deletions src/workerd/api/node/timers.c++
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include "timers.h"

#include <workerd/api/global-scope.h>
#include <workerd/jsg/jsg.h>

namespace workerd::api::node {

Comment thread
anonrig marked this conversation as resolved.
// The setImmediate/clearImmediate methods are only exposed on globalThis if the
// node_compat_v2 flag is set. However, we want them exposed via `node:timers`
// generally when just the original node_compat is enabled. Therefore, we provide
// this alternative route to the implementations on ServiceWorkerGlobalScope.
jsg::Ref<Immediate> TimersUtil::setImmediate(jsg::Lock& js,
jsg::Function<void(jsg::Arguments<jsg::Value>)> function,
jsg::Arguments<jsg::Value> args) {
auto context = js.v8Context();
auto& global =
jsg::extractInternalPointer<ServiceWorkerGlobalScope, true>(context, context->Global());
return global.setImmediate(js, kj::mv(function), kj::mv(args));
}

void TimersUtil::clearImmediate(jsg::Lock& js, kj::Maybe<jsg::Ref<Immediate>> maybeImmediate) {
auto context = js.v8Context();
auto& global =
jsg::extractInternalPointer<ServiceWorkerGlobalScope, true>(context, context->Global());
global.clearImmediate(kj::mv(maybeImmediate));
}
} // namespace workerd::api::node
34 changes: 34 additions & 0 deletions src/workerd/api/node/timers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (c) 2017-2022 Cloudflare, Inc.
// Licensed under the Apache 2.0 license found in the LICENSE file or at:
// https://opensource.org/licenses/Apache-2.0
#pragma once

#include <workerd/jsg/jsg.h>

#include <kj/string.h>

namespace workerd::api {

class Immediate;

namespace node {
class TimersUtil final: public jsg::Object {
public:
TimersUtil() = default;
TimersUtil(jsg::Lock&, const jsg::Url&) {}

jsg::Ref<Immediate> setImmediate(jsg::Lock& js,
jsg::Function<void(jsg::Arguments<jsg::Value>)> function,
jsg::Arguments<jsg::Value> args);
void clearImmediate(jsg::Lock& js, kj::Maybe<jsg::Ref<Immediate>> immediate);

JSG_RESOURCE_TYPE(TimersUtil) {
JSG_METHOD(setImmediate);
JSG_METHOD(clearImmediate);
}
};

#define EW_NODE_TIMERS_ISOLATE_TYPES api::node::TimersUtil
} // namespace node

} // namespace workerd::api