Skip to content

Commit 62fd118

Browse files
feat(unenv-preset): add support for native node:inspector module (#11733)
* feat(unenv-preset): add support for native node:inspector module Co-Authored-By: pbacondarwin@cloudflare.com <pete@bacondarwin.com> * fix: require experimental flag for inspector module enablement Co-Authored-By: pbacondarwin@cloudflare.com <pete@bacondarwin.com> --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
1 parent dd66dcd commit 62fd118

File tree

4 files changed

+105
-0
lines changed

4 files changed

+105
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
"@cloudflare/unenv-preset": minor
3+
---
4+
5+
Add support for native `node:inspector` module when the `enable_nodejs_inspector_module` compatibility flag is enabled. This feature is currently experimental and requires both the `enable_nodejs_inspector_module` and `experimental` compatibility flags to be set.
6+
7+
To enable the native inspector module, add the following to your `wrangler.jsonc`:
8+
9+
```jsonc
10+
{
11+
"compatibility_flags": ["experimental", "enable_nodejs_inspector_module"],
12+
}
13+
```
14+
15+
Then you can import and use the inspector module in your Worker:
16+
17+
```javascript
18+
import inspector from "node:inspector";
19+
20+
// Access inspector APIs (note: workerd's implementation is a non-functional stub)
21+
inspector.url(); // returns undefined
22+
inspector.close(); // no-op
23+
```

packages/unenv-preset/src/preset.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ export function getCloudflarePreset({
7979
const wasiOverrides = getWasiOverrides(compat);
8080
const consoleOverrides = getConsoleOverrides(compat);
8181
const vmOverrides = getVmOverrides(compat);
82+
const inspectorOverrides = getInspectorOverrides(compat);
8283

8384
// "dynamic" as they depend on the compatibility date and flags
8485
const dynamicNativeModules = [
@@ -94,6 +95,7 @@ export function getCloudflarePreset({
9495
...wasiOverrides.nativeModules,
9596
...consoleOverrides.nativeModules,
9697
...vmOverrides.nativeModules,
98+
...inspectorOverrides.nativeModules,
9799
];
98100

99101
// "dynamic" as they depend on the compatibility date and flags
@@ -110,6 +112,7 @@ export function getCloudflarePreset({
110112
...wasiOverrides.hybridModules,
111113
...consoleOverrides.hybridModules,
112114
...vmOverrides.hybridModules,
115+
...inspectorOverrides.hybridModules,
113116
];
114117

115118
return {
@@ -596,3 +599,39 @@ function getVmOverrides({
596599
hybridModules: [],
597600
};
598601
}
602+
603+
/**
604+
* Returns the overrides for `node:inspector` (unenv or workerd)
605+
*
606+
* The native inspector implementation:
607+
* - is experimental and has no default enable date
608+
* - can be enabled with the "enable_nodejs_inspector_module" flag
609+
* - can be disabled with the "disable_nodejs_inspector_module" flag
610+
*/
611+
function getInspectorOverrides({
612+
compatibilityFlags,
613+
}: {
614+
compatibilityDate: string;
615+
compatibilityFlags: string[];
616+
}): { nativeModules: string[]; hybridModules: string[] } {
617+
const disabledByFlag = compatibilityFlags.includes(
618+
"disable_nodejs_inspector_module"
619+
);
620+
621+
const enabledByFlag =
622+
compatibilityFlags.includes("enable_nodejs_inspector_module") &&
623+
compatibilityFlags.includes("experimental");
624+
625+
const enabled = enabledByFlag && !disabledByFlag;
626+
627+
// When enabled, use the native `inspector` module from workerd
628+
return enabled
629+
? {
630+
nativeModules: ["inspector"],
631+
hybridModules: [],
632+
}
633+
: {
634+
nativeModules: [],
635+
hybridModules: [],
636+
};
637+
}

packages/wrangler/e2e/unenv-preset/preset.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,25 @@ const localTestConfigs: TestConfig[] = [
410410
},
411411
},
412412
],
413+
// node:inspector (experimental, no default enable date)
414+
[
415+
{
416+
name: "inspector enabled by flag",
417+
compatibilityDate: "2024-09-23",
418+
compatibilityFlags: ["enable_nodejs_inspector_module", "experimental"],
419+
expectRuntimeFlags: {
420+
enable_nodejs_inspector_module: true,
421+
},
422+
},
423+
{
424+
name: "inspector disabled by flag",
425+
compatibilityDate: "2024-09-23",
426+
compatibilityFlags: ["disable_nodejs_inspector_module", "experimental"],
427+
expectRuntimeFlags: {
428+
enable_nodejs_inspector_module: false,
429+
},
430+
},
431+
],
413432
].flat() as TestConfig[];
414433

415434
describe.each(localTestConfigs)(

packages/wrangler/e2e/unenv-preset/worker/index.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,30 @@ export const WorkerdTests: Record<string, () => void> = {
728728
createScript: "function",
729729
});
730730
},
731+
732+
async testInspector() {
733+
const inspector = await import("node:inspector");
734+
735+
assertTypeOfProperties(inspector, {
736+
Session: "function",
737+
close: "function",
738+
console: "object",
739+
open: "function",
740+
url: "function",
741+
waitForDebugger: "function",
742+
Network: "object",
743+
});
744+
745+
assertTypeOfProperties(inspector.default, {
746+
Session: "function",
747+
close: "function",
748+
console: "object",
749+
open: "function",
750+
url: "function",
751+
waitForDebugger: "function",
752+
Network: "object",
753+
});
754+
},
731755
};
732756

733757
/**

0 commit comments

Comments
 (0)