@@ -10,39 +10,49 @@ import workerdPath, {
1010import { SERVICE_LOOPBACK , SOCKET_ENTRY } from "../plugins" ;
1111import { 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+
1321export 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
2548export 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-
4656function 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+
153166class 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