1+ // The polyfill is only used with the process v1 native implementation
2+ // process v2 implements all the APIs from workerd v1.20250924.0
3+
14import { hrtime as UnenvHrTime } from "unenv/node/internal/process/hrtime" ;
25import { Process as UnenvProcess } from "unenv/node/internal/process/process" ;
36
@@ -16,92 +19,47 @@ export const getBuiltinModule: NodeJS.Process["getBuiltinModule"] =
1619
1720const workerdProcess = getBuiltinModule ( "node:process" ) ;
1821
19- // Workerd has 2 different implementation for `node:process`
20- //
21- // See:
22- // - [workerd `process` v1](https://github.com/cloudflare/workerd/blob/main/src/node/internal/legacy_process.ts)
23- // - [workerd `process` v2](https://github.com/cloudflare/workerd/blob/main/src/node/internal/public_process.ts)
24- // - [`enable_nodejs_process_v2` flag](https://github.com/cloudflare/workerd/blob/main/src/workerd/io/compatibility-date.capnp)
25- // eslint-disable-next-line @typescript-eslint/no-explicit-any
26- const isWorkerdProcessV2 = ( globalThis as any ) . Cloudflare . compatibilityFlags
27- . enable_nodejs_process_v2 ;
28-
2922const unenvProcess = new UnenvProcess ( {
3023 env : globalProcess . env ,
31- // `hrtime` is only available from workerd process v2
32- hrtime : isWorkerdProcessV2 ? workerdProcess . hrtime : UnenvHrTime ,
24+ hrtime : UnenvHrTime ,
3325 // `nextTick` is available from workerd process v1
3426 nextTick : workerdProcess . nextTick ,
3527} ) ;
3628
37- // APIs implemented by workerd module in both v1 and v2
29+ // APIs implemented by workerd process in both v1 and v2
3830// Note that `env`, `hrtime` and `nextTick` are always retrieved from `unenv`
3931export const { exit, features, platform } = workerdProcess ;
4032
41- // APIs that can be implemented by either `unenv` or `workerd`.
42- // They are always retrieved from `unenv` which might use their `workerd` implementation.
43- export const {
44- // Always implemented by workerd
45- env,
46- // Only implemented in workerd v2
47- hrtime,
48- // Always implemented by workerd
49- nextTick,
50- } = unenvProcess ;
51-
52- // APIs that are not implemented by `workerd` (whether v1 or v2)
53- // They are retrieved from `unenv`.
5433export const {
5534 _channel,
35+ _debugEnd,
36+ _debugProcess,
5637 _disconnect,
5738 _events,
5839 _eventsCount,
59- _handleQueue,
60- _maxListeners,
61- _pendingMessage,
62- _send,
63- assert,
64- disconnect,
65- mainModule,
66- } = unenvProcess ;
67-
68- // API that are only implemented starting from v2 of workerd process
69- // They are retrieved from unenv when process v1 is used
70- export const {
71- // @ts -expect-error `_debugEnd` is missing typings
72- _debugEnd,
73- // @ts -expect-error `_debugProcess` is missing typings
74- _debugProcess,
75- // @ts -expect-error `_exiting` is missing typings
7640 _exiting,
77- // @ts -expect-error `_fatalException` is missing typings
7841 _fatalException,
79- // @ts -expect-error `_getActiveHandles` is missing typings
8042 _getActiveHandles,
81- // @ts -expect-error `_getActiveRequests` is missing typings
8243 _getActiveRequests,
83- // @ts -expect-error `_kill` is missing typings
44+ _handleQueue ,
8445 _kill,
85- // @ts -expect-error `_linkedBinding` is missing typings
8646 _linkedBinding,
87- // @ts -expect-error `_preload_modules` is missing typings
47+ _maxListeners,
48+ _pendingMessage,
8849 _preload_modules,
89- // @ts -expect-error `_rawDebug` is missing typings
9050 _rawDebug,
91- // @ts -expect-error `_startProfilerIdleNotifier` is missing typings
51+ _send ,
9252 _startProfilerIdleNotifier,
93- // @ts -expect-error `_stopProfilerIdleNotifier` is missing typings
9453 _stopProfilerIdleNotifier,
95- // @ts -expect-error `_tickCallback` is missing typings
9654 _tickCallback,
9755 abort,
9856 addListener,
9957 allowedNodeEnvironmentFlags,
10058 arch,
10159 argv,
10260 argv0,
61+ assert,
10362 availableMemory,
104- // @ts -expect-error `binding` is missing typings
10563 binding,
10664 channel,
10765 chdir,
@@ -111,11 +69,12 @@ export const {
11169 cpuUsage,
11270 cwd,
11371 debugPort,
72+ disconnect,
11473 dlopen,
115- // @ts -expect-error `domain` is missing typings
11674 domain,
11775 emit,
11876 emitWarning,
77+ env,
11978 eventNames,
12079 execArgv,
12180 execPath,
@@ -129,27 +88,26 @@ export const {
12988 getMaxListeners,
13089 getuid,
13190 hasUncaughtExceptionCaptureCallback,
132- // @ts -expect-error `initgroups` is missing typings
91+ hrtime ,
13392 initgroups,
13493 kill,
13594 listenerCount,
13695 listeners,
13796 loadEnvFile,
97+ mainModule,
13898 memoryUsage,
139- // @ts -expect-error `moduleLoadList` is missing typings
14099 moduleLoadList,
100+ nextTick,
141101 off,
142102 on,
143103 once,
144- // @ts -expect-error `openStdin` is missing typings
145104 openStdin,
146105 permission,
147106 pid,
148107 ppid,
149108 prependListener,
150109 prependOnceListener,
151110 rawListeners,
152- // @ts -expect-error `reallyExit` is missing typings
153111 reallyExit,
154112 ref,
155113 release,
@@ -178,7 +136,7 @@ export const {
178136 uptime,
179137 version,
180138 versions,
181- } = isWorkerdProcessV2 ? workerdProcess : unenvProcess ;
139+ } = unenvProcess ;
182140
183141const _process = {
184142 abort,
0 commit comments