1
1
import { ChildProcess , spawn } from "child_process" ;
2
- import which from "which" ;
3
2
import { routeEvents } from "./routing" ;
4
- import { log } from "./log" ;
3
+ import { info , log } from "./log" ;
5
4
6
5
const { _HANDLER , IS_OFFLINE , AWS_LAMBDA_RUNTIME_API } = process . env ;
7
6
@@ -16,34 +15,32 @@ export const bootstrap = async (): Promise<void> => {
16
15
17
16
log ( "Bootstraping" , { _HANDLER , IS_OFFLINE , AWS_LAMBDA_RUNTIME_API } ) ;
18
17
19
- let handler : URL | undefined = undefined ;
20
- let bin : string ;
21
- let endpoint : string | undefined = undefined ;
22
-
23
18
// handler is in the format of
24
- // - `{some-bin}: http://localhost:{the-bins-port} (will start some-bin, and forward requests to the http server)
19
+ // - `{some-bin}@ http ://localhost:{the-bins-port} (will start some-bin, and forward requests to the http server)
25
20
// - `http://localhost:{some-port}` (will forward the request to the http server)
26
21
// - `{some-bin}` (will forward the event to the bin)
27
22
28
- try {
29
- handler = new URL ( _HANDLER ) ;
30
- bin = handler . protocol . slice ( 0 , - 1 ) ;
31
- endpoint = handler . toString ( ) ;
32
- log ( "Found protocol in handler" , { bin, endpoint } ) ;
33
- } catch ( e ) {
34
- log ( "No protocol found in handler" , { _HANDLER } ) ;
35
- bin = _HANDLER ;
36
- }
23
+ let [ bin , endpoint ] = _HANDLER . split ( / (?< = ^ [ ^ @ ] * ) @ / ) as [
24
+ string | undefined ,
25
+ string | undefined | URL
26
+ ] ;
37
27
38
28
let childProcess : ChildProcess | undefined = undefined ;
39
29
40
- if ( handler && bin !== "http" && bin !== "https" ) {
41
- log ( "Starting child process" , { bin, endpoint } ) ;
30
+ if ( bin && ! endpoint ) {
31
+ try {
32
+ endpoint = new URL ( bin ) . toString ( ) ;
33
+ bin = undefined ;
34
+ } catch ( e ) { }
35
+ }
42
36
43
- endpoint = handler . pathname ;
37
+ if ( bin && endpoint ) {
38
+ log ( "Starting child process" , { bin } ) ;
44
39
45
40
const subcommand = IS_OFFLINE === "true" ? "dev" : "start" ;
46
41
42
+ info ( `Running: \`${ bin } ${ subcommand } \`` ) ;
43
+
47
44
childProcess = spawn ( bin , [ subcommand ] , {
48
45
detached : true ,
49
46
stdio : "inherit" ,
@@ -55,10 +52,9 @@ export const bootstrap = async (): Promise<void> => {
55
52
log ( "Started child process" , { bin, subcommand, pid : childProcess . pid } ) ;
56
53
}
57
54
58
- try {
59
- log ( "Checking if bin is in PATH" , { bin } ) ;
60
- await which ( bin , { all : false } ) ;
55
+ endpoint = endpoint ? new URL ( endpoint ) : undefined ;
61
56
57
+ try {
62
58
log ( "Routing events" , { bin, endpoint } ) ;
63
59
await routeEvents ( AWS_LAMBDA_RUNTIME_API , bin , endpoint ) ;
64
60
} catch ( e ) {
0 commit comments