Skip to content

Commit 2594130

Browse files
authored
Revert 10805 (Drop process polyfill when v2 is available) (#10860)
* Revert "Drop `node:process` polyfill when v2 is available (#10805)" This reverts commit d0801b1. * fixup! updates * fixup! lock file * fixup! changeset
1 parent 862034c commit 2594130

File tree

5 files changed

+70
-129
lines changed

5 files changed

+70
-129
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@cloudflare/unenv-preset": patch
3+
---
4+
5+
Revert "Drop `node:process` polyfill when v2 is available"

packages/unenv-preset/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
},
5050
"peerDependencies": {
5151
"unenv": "2.0.0-rc.21",
52-
"workerd": "^1.20250924.0"
52+
"workerd": "^1.20250927.0"
5353
},
5454
"peerDependenciesMeta": {
5555
"workerd": {

packages/unenv-preset/src/preset.ts

Lines changed: 2 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ const nativeModules = [
4747
];
4848

4949
// Modules implemented via a mix of workerd APIs and polyfills.
50-
const hybridModules = ["console"];
50+
const hybridModules = ["console", "process"];
5151

5252
/**
5353
* Creates the Cloudflare preset for the given compatibility date and compatibility flags
@@ -72,7 +72,6 @@ export function getCloudflarePreset({
7272
const http2Overrides = getHttp2Overrides(compat);
7373
const osOverrides = getOsOverrides(compat);
7474
const fsOverrides = getFsOverrides(compat);
75-
const processOverrides = getProcessOverrides(compat);
7675

7776
// "dynamic" as they depend on the compatibility date and flags
7877
const dynamicNativeModules = [
@@ -81,7 +80,6 @@ export function getCloudflarePreset({
8180
...http2Overrides.nativeModules,
8281
...osOverrides.nativeModules,
8382
...fsOverrides.nativeModules,
84-
...processOverrides.nativeModules,
8583
];
8684

8785
// "dynamic" as they depend on the compatibility date and flags
@@ -91,7 +89,6 @@ export function getCloudflarePreset({
9189
...http2Overrides.hybridModules,
9290
...osOverrides.hybridModules,
9391
...fsOverrides.hybridModules,
94-
...processOverrides.hybridModules,
9592
];
9693

9794
return {
@@ -125,7 +122,7 @@ export function getCloudflarePreset({
125122
clearImmediate: false,
126123
setImmediate: false,
127124
console: "@cloudflare/unenv-preset/node/console",
128-
...processOverrides.inject,
125+
process: "@cloudflare/unenv-preset/node/process",
129126
},
130127
polyfill: ["@cloudflare/unenv-preset/polyfill/performance"],
131128
external: dynamicNativeModules.flatMap((p) => [p, `node:${p}`]),
@@ -308,48 +305,3 @@ function getFsOverrides({
308305
hybridModules: [],
309306
};
310307
}
311-
312-
/**
313-
* Returns the overrides for `node:process` and `node:fs/promises`
314-
*
315-
* The native process v2 implementation:
316-
* - is enabled starting from 2025-09-15
317-
* - can be enabled with the "enable_nodejs_process_v2" flag
318-
* - can be disabled with the "disable_nodejs_process_v2" flag
319-
*/
320-
function getProcessOverrides({
321-
compatibilityDate,
322-
compatibilityFlags,
323-
}: {
324-
compatibilityDate: string;
325-
compatibilityFlags: string[];
326-
}): {
327-
nativeModules: string[];
328-
hybridModules: string[];
329-
inject: { process: string | false };
330-
} {
331-
const disabledV2ByFlag = compatibilityFlags.includes(
332-
"disable_nodejs_process_v2"
333-
);
334-
335-
const enabledV2ByFlag = compatibilityFlags.includes(
336-
"enable_nodejs_process_v2"
337-
);
338-
const enabledV2ByDate = compatibilityDate >= "2025-09-15";
339-
340-
const isV2 = (enabledV2ByFlag || enabledV2ByDate) && !disabledV2ByFlag;
341-
342-
return isV2
343-
? {
344-
nativeModules: ["process"],
345-
hybridModules: [],
346-
// We can use the native global, return `false` to drop the unenv default
347-
inject: { process: false },
348-
}
349-
: {
350-
nativeModules: [],
351-
hybridModules: ["process"],
352-
// Use the module default export as the global `process`
353-
inject: { process: "@cloudflare/unenv-preset/node/process" },
354-
};
355-
}

packages/unenv-preset/src/runtime/node/process.ts

Lines changed: 60 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
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-
41
import { hrtime as UnenvHrTime } from "unenv/node/internal/process/hrtime";
52
import { Process as UnenvProcess } from "unenv/node/internal/process/process";
63

@@ -19,47 +16,92 @@ export const getBuiltinModule: NodeJS.Process["getBuiltinModule"] =
1916

2017
const 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+
2229
const 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`
3139
export 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`.
3354
export 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

141183
const _process = {
142184
abort,

0 commit comments

Comments
 (0)