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
+
1
4
import { hrtime as UnenvHrTime } from "unenv/node/internal/process/hrtime" ;
2
5
import { Process as UnenvProcess } from "unenv/node/internal/process/process" ;
3
6
@@ -16,92 +19,47 @@ export const getBuiltinModule: NodeJS.Process["getBuiltinModule"] =
16
19
17
20
const workerdProcess = getBuiltinModule ( "node:process" ) ;
18
21
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
-
29
22
const unenvProcess = new UnenvProcess ( {
30
23
env : globalProcess . env ,
31
- // `hrtime` is only available from workerd process v2
32
- hrtime : isWorkerdProcessV2 ? workerdProcess . hrtime : UnenvHrTime ,
24
+ hrtime : UnenvHrTime ,
33
25
// `nextTick` is available from workerd process v1
34
26
nextTick : workerdProcess . nextTick ,
35
27
} ) ;
36
28
37
- // APIs implemented by workerd module in both v1 and v2
29
+ // APIs implemented by workerd process in both v1 and v2
38
30
// Note that `env`, `hrtime` and `nextTick` are always retrieved from `unenv`
39
31
export const { exit, features, platform } = workerdProcess ;
40
32
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`.
54
33
export const {
55
34
_channel,
35
+ _debugEnd,
36
+ _debugProcess,
56
37
_disconnect,
57
38
_events,
58
39
_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
76
40
_exiting,
77
- // @ts -expect-error `_fatalException` is missing typings
78
41
_fatalException,
79
- // @ts -expect-error `_getActiveHandles` is missing typings
80
42
_getActiveHandles,
81
- // @ts -expect-error `_getActiveRequests` is missing typings
82
43
_getActiveRequests,
83
- // @ts -expect-error `_kill` is missing typings
44
+ _handleQueue ,
84
45
_kill,
85
- // @ts -expect-error `_linkedBinding` is missing typings
86
46
_linkedBinding,
87
- // @ts -expect-error `_preload_modules` is missing typings
47
+ _maxListeners,
48
+ _pendingMessage,
88
49
_preload_modules,
89
- // @ts -expect-error `_rawDebug` is missing typings
90
50
_rawDebug,
91
- // @ts -expect-error `_startProfilerIdleNotifier` is missing typings
51
+ _send ,
92
52
_startProfilerIdleNotifier,
93
- // @ts -expect-error `_stopProfilerIdleNotifier` is missing typings
94
53
_stopProfilerIdleNotifier,
95
- // @ts -expect-error `_tickCallback` is missing typings
96
54
_tickCallback,
97
55
abort,
98
56
addListener,
99
57
allowedNodeEnvironmentFlags,
100
58
arch,
101
59
argv,
102
60
argv0,
61
+ assert,
103
62
availableMemory,
104
- // @ts -expect-error `binding` is missing typings
105
63
binding,
106
64
channel,
107
65
chdir,
@@ -111,11 +69,12 @@ export const {
111
69
cpuUsage,
112
70
cwd,
113
71
debugPort,
72
+ disconnect,
114
73
dlopen,
115
- // @ts -expect-error `domain` is missing typings
116
74
domain,
117
75
emit,
118
76
emitWarning,
77
+ env,
119
78
eventNames,
120
79
execArgv,
121
80
execPath,
@@ -129,27 +88,26 @@ export const {
129
88
getMaxListeners,
130
89
getuid,
131
90
hasUncaughtExceptionCaptureCallback,
132
- // @ts -expect-error `initgroups` is missing typings
91
+ hrtime ,
133
92
initgroups,
134
93
kill,
135
94
listenerCount,
136
95
listeners,
137
96
loadEnvFile,
97
+ mainModule,
138
98
memoryUsage,
139
- // @ts -expect-error `moduleLoadList` is missing typings
140
99
moduleLoadList,
100
+ nextTick,
141
101
off,
142
102
on,
143
103
once,
144
- // @ts -expect-error `openStdin` is missing typings
145
104
openStdin,
146
105
permission,
147
106
pid,
148
107
ppid,
149
108
prependListener,
150
109
prependOnceListener,
151
110
rawListeners,
152
- // @ts -expect-error `reallyExit` is missing typings
153
111
reallyExit,
154
112
ref,
155
113
release,
@@ -178,7 +136,7 @@ export const {
178
136
uptime,
179
137
version,
180
138
versions,
181
- } = isWorkerdProcessV2 ? workerdProcess : unenvProcess ;
139
+ } = unenvProcess ;
182
140
183
141
const _process = {
184
142
abort,
0 commit comments