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-
41import { hrtime as UnenvHrTime } from "unenv/node/internal/process/hrtime" ;
52import { Process as UnenvProcess } from "unenv/node/internal/process/process" ;
63
@@ -19,47 +16,92 @@ export const getBuiltinModule: NodeJS.Process["getBuiltinModule"] =
1916
2017const workerdProcess = getBuiltinModule ( "node:process" ) ;
2118
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+
2229const unenvProcess = new UnenvProcess ( {
2330 env : globalProcess . env ,
24- hrtime : UnenvHrTime ,
31+ // `hrtime` is only available from workerd process v2
32+ hrtime : isWorkerdProcessV2 ? workerdProcess . hrtime : UnenvHrTime ,
2533 // `nextTick` is available from workerd process v1
2634 nextTick : workerdProcess . nextTick ,
2735} ) ;
2836
29- // APIs implemented by workerd process in both v1 and v2
37+ // APIs implemented by workerd module in both v1 and v2
3038// Note that `env`, `hrtime` and `nextTick` are always retrieved from `unenv`
3139export const { exit, features, platform } = workerdProcess ;
3240
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`.
3354export const {
3455 _channel,
35- _debugEnd,
36- _debugProcess,
3756 _disconnect,
3857 _events,
3958 _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
4076 _exiting,
77+ // @ts -expect-error `_fatalException` is missing typings
4178 _fatalException,
79+ // @ts -expect-error `_getActiveHandles` is missing typings
4280 _getActiveHandles,
81+ // @ts -expect-error `_getActiveRequests` is missing typings
4382 _getActiveRequests,
44- _handleQueue ,
83+ // @ts -expect-error `_kill` is missing typings
4584 _kill,
85+ // @ts -expect-error `_linkedBinding` is missing typings
4686 _linkedBinding,
47- _maxListeners,
48- _pendingMessage,
87+ // @ts -expect-error `_preload_modules` is missing typings
4988 _preload_modules,
89+ // @ts -expect-error `_rawDebug` is missing typings
5090 _rawDebug,
51- _send ,
91+ // @ts -expect-error `_startProfilerIdleNotifier` is missing typings
5292 _startProfilerIdleNotifier,
93+ // @ts -expect-error `_stopProfilerIdleNotifier` is missing typings
5394 _stopProfilerIdleNotifier,
95+ // @ts -expect-error `_tickCallback` is missing typings
5496 _tickCallback,
5597 abort,
5698 addListener,
5799 allowedNodeEnvironmentFlags,
58100 arch,
59101 argv,
60102 argv0,
61- assert,
62103 availableMemory,
104+ // @ts -expect-error `binding` is missing typings
63105 binding,
64106 channel,
65107 chdir,
@@ -69,12 +111,11 @@ export const {
69111 cpuUsage,
70112 cwd,
71113 debugPort,
72- disconnect,
73114 dlopen,
115+ // @ts -expect-error `domain` is missing typings
74116 domain,
75117 emit,
76118 emitWarning,
77- env,
78119 eventNames,
79120 execArgv,
80121 execPath,
@@ -88,26 +129,27 @@ export const {
88129 getMaxListeners,
89130 getuid,
90131 hasUncaughtExceptionCaptureCallback,
91- hrtime ,
132+ // @ts -expect-error `initgroups` is missing typings
92133 initgroups,
93134 kill,
94135 listenerCount,
95136 listeners,
96137 loadEnvFile,
97- mainModule,
98138 memoryUsage,
139+ // @ts -expect-error `moduleLoadList` is missing typings
99140 moduleLoadList,
100- nextTick,
101141 off,
102142 on,
103143 once,
144+ // @ts -expect-error `openStdin` is missing typings
104145 openStdin,
105146 permission,
106147 pid,
107148 ppid,
108149 prependListener,
109150 prependOnceListener,
110151 rawListeners,
152+ // @ts -expect-error `reallyExit` is missing typings
111153 reallyExit,
112154 ref,
113155 release,
@@ -136,7 +178,7 @@ export const {
136178 uptime,
137179 version,
138180 versions,
139- } = unenvProcess ;
181+ } = isWorkerdProcessV2 ? workerdProcess : unenvProcess ;
140182
141183const _process = {
142184 abort,
0 commit comments