Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit 2f1c201

Browse files
authored
Add support for devtools inspector protocol (#426)
Adds a new `inspectorPort` option, passed to `workerd`'s `--inspector-addr`. Also disables `--verbose` by default, unless the `verbose: true` option is set, as we don't want logging by default if we could use the inspector for this instead.
1 parent 3e17af7 commit 2f1c201

File tree

3 files changed

+55
-31
lines changed

3 files changed

+55
-31
lines changed

packages/tre/src/index.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import {
3131
Config,
3232
Runtime,
3333
RuntimeConstructor,
34+
RuntimeOptions,
3435
Service,
3536
Socket,
3637
Worker_Binding,
@@ -229,11 +230,16 @@ export class Miniflare {
229230
const loopbackPort = address.port;
230231

231232
// Start runtime
232-
const host = this.#sharedOpts.core.host ?? "127.0.0.1";
233-
const port = this.#sharedOpts.core.port ?? (await getPort({ port: 8787 }));
234-
this.#runtime = new this.#runtimeConstructor(host, port, loopbackPort);
233+
const opts: RuntimeOptions = {
234+
entryHost: this.#sharedOpts.core.host ?? "127.0.0.1",
235+
entryPort: this.#sharedOpts.core.port ?? (await getPort({ port: 8787 })),
236+
loopbackPort,
237+
inspectorPort: this.#sharedOpts.core.inspectorPort,
238+
verbose: this.#sharedOpts.core.verbose,
239+
};
240+
this.#runtime = new this.#runtimeConstructor(opts);
235241
this.#removeRuntimeExitHook = exitHook(() => void this.#runtime?.dispose());
236-
this.#runtimeEntryURL = new URL(`http://127.0.0.1:${port}`);
242+
this.#runtimeEntryURL = new URL(`http://127.0.0.1:${opts.entryPort}`);
237243

238244
const config = await this.#assembleConfig();
239245
assert(config !== undefined);

packages/tre/src/plugins/core/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,17 @@ export const CoreOptionsSchema = z.object({
4949
wasmBindings: z.record(z.string()).optional(),
5050
textBlobBindings: z.record(z.string()).optional(),
5151
dataBlobBindings: z.record(z.string()).optional(),
52+
// TODO: add support for workerd network/external/disk services here
5253
serviceBindings: z.record(z.union([z.string(), ServiceFetch])).optional(),
5354
});
5455

5556
export const CoreSharedOptionsSchema = z.object({
5657
host: z.string().optional(),
5758
port: z.number().optional(),
59+
60+
inspectorPort: z.number().optional(),
61+
verbose: z.boolean().optional(),
62+
5863
// TODO: add back validation of cf object
5964
cf: z.union([z.boolean(), z.string(), z.record(z.any())]).optional(),
6065
});

packages/tre/src/runtime/index.ts

Lines changed: 40 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,39 +10,49 @@ import workerdPath, {
1010
import { SERVICE_LOOPBACK, SOCKET_ENTRY } from "../plugins";
1111
import { Awaitable, MiniflareCoreError } from "../shared";
1212

13+
export interface RuntimeOptions {
14+
entryHost: string;
15+
entryPort: number;
16+
loopbackPort: number;
17+
inspectorPort?: number;
18+
verbose?: boolean;
19+
}
20+
1321
export abstract class Runtime {
14-
constructor(
15-
protected readonly entryHost: string,
16-
protected readonly entryPort: number,
17-
protected readonly loopbackPort: number
18-
) {}
22+
constructor(protected readonly opts: RuntimeOptions) {}
1923

2024
abstract updateConfig(configBuffer: Buffer): Awaitable<void>;
2125
abstract get exitPromise(): Promise<void> | undefined;
2226
abstract dispose(): Awaitable<void>;
27+
28+
protected getCommonArgs(): string[] {
29+
const args: string[] = [
30+
"serve",
31+
// Required to use binary capnp config
32+
"--binary",
33+
// Required to use compatibility flags without a default-on date,
34+
// (e.g. "streams_enable_constructors"), see https://github.com/cloudflare/workerd/pull/21
35+
"--experimental",
36+
];
37+
if (this.opts.inspectorPort !== undefined) {
38+
// Required to enable the V8 inspector
39+
args.push(`--inspector-addr=127.0.0.1:${this.opts.inspectorPort}`);
40+
}
41+
if (this.opts.verbose) {
42+
args.push("--verbose");
43+
}
44+
return args;
45+
}
2346
}
2447

2548
export interface RuntimeConstructor {
26-
new (entryHost: string, entryPort: number, loopbackPort: number): Runtime;
49+
new (opts: RuntimeOptions): Runtime;
2750

2851
isSupported(): boolean;
2952
supportSuggestion: string;
3053
description: string;
3154
}
3255

33-
const COMMON_RUNTIME_ARGS = [
34-
"serve",
35-
// Required to use binary capnp config
36-
"--binary",
37-
// Required to display `console.log()` output
38-
"--verbose",
39-
// Required to use compatibility flags without a default-on date,
40-
// (e.g. "streams_enable_constructors"), see https://github.com/cloudflare/workerd/pull/21
41-
"--experimental",
42-
];
43-
// `__dirname` relative to bundled output `dist/src/index.js`
44-
const RESTART_PATH = path.resolve(__dirname, "..", "..", "lib", "restart.sh");
45-
4656
function waitForExit(process: childProcess.ChildProcess): Promise<void> {
4757
return new Promise((resolve) => {
4858
process.once("exit", () => resolve());
@@ -84,8 +94,8 @@ class NativeRuntime extends Runtime {
8494
#process?: childProcess.ChildProcess;
8595
#processExitPromise?: Promise<void>;
8696

87-
constructor(entryHost: string, entryPort: number, loopbackPort: number) {
88-
super(entryHost, entryPort, loopbackPort);
97+
constructor(opts: RuntimeOptions) {
98+
super(opts);
8999
const [command, ...args] = this.getCommand();
90100
this.#command = command;
91101
this.#args = args;
@@ -94,9 +104,9 @@ class NativeRuntime extends Runtime {
94104
getCommand(): string[] {
95105
return [
96106
workerdPath,
97-
...COMMON_RUNTIME_ARGS,
98-
`--socket-addr=${SOCKET_ENTRY}=${this.entryHost}:${this.entryPort}`,
99-
`--external-addr=${SERVICE_LOOPBACK}=127.0.0.1:${this.loopbackPort}`,
107+
...this.getCommonArgs(),
108+
`--socket-addr=${SOCKET_ENTRY}=${this.opts.entryHost}:${this.opts.entryPort}`,
109+
`--external-addr=${SERVICE_LOOPBACK}=127.0.0.1:${this.opts.loopbackPort}`,
100110
// TODO: consider adding support for unix sockets?
101111
// `--socket-fd=${SOCKET_ENTRY}=${this.entryPort}`,
102112
// `--external-addr=${SERVICE_LOOPBACK}=${this.loopbackPort}`,
@@ -150,6 +160,9 @@ class WSLRuntime extends NativeRuntime {
150160
}
151161
}
152162

163+
// `__dirname` relative to bundled output `dist/src/index.js`
164+
const RESTART_PATH = path.resolve(__dirname, "..", "..", "lib", "restart.sh");
165+
153166
class DockerRuntime extends Runtime {
154167
static isSupported() {
155168
const result = childProcess.spawnSync("docker", ["--version"]); // TODO: check daemon running too?
@@ -191,13 +204,13 @@ class DockerRuntime extends Runtime {
191204
`--volume=${RESTART_PATH}:/restart.sh`,
192205
`--volume=${workerdPath}:/runtime`,
193206
`--volume=${this.#configPath}:/miniflare-config.bin`,
194-
`--publish=${this.entryHost}:${this.entryPort}:8787`,
207+
`--publish=${this.opts.entryHost}:${this.opts.entryPort}:8787`,
195208
"debian:bullseye-slim",
196209
"/restart.sh",
197210
"/runtime",
198-
...COMMON_RUNTIME_ARGS,
211+
...this.getCommonArgs(),
199212
`--socket-addr=${SOCKET_ENTRY}=*:8787`,
200-
`--external-addr=${SERVICE_LOOPBACK}=host.docker.internal:${this.loopbackPort}`,
213+
`--external-addr=${SERVICE_LOOPBACK}=host.docker.internal:${this.opts.loopbackPort}`,
201214
"/miniflare-config.bin",
202215
],
203216
{

0 commit comments

Comments
 (0)