Skip to content

Commit e4c8de6

Browse files
committed
update format of HANDLER
1 parent 4e093a0 commit e4c8de6

File tree

5 files changed

+46
-32
lines changed

5 files changed

+46
-32
lines changed

src/bootstrap.ts

+18-22
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { ChildProcess, spawn } from "child_process";
2-
import which from "which";
32
import { routeEvents } from "./routing";
4-
import { log } from "./log";
3+
import { info, log } from "./log";
54

65
const { _HANDLER, IS_OFFLINE, AWS_LAMBDA_RUNTIME_API } = process.env;
76

@@ -16,34 +15,32 @@ export const bootstrap = async (): Promise<void> => {
1615

1716
log("Bootstraping", { _HANDLER, IS_OFFLINE, AWS_LAMBDA_RUNTIME_API });
1817

19-
let handler: URL | undefined = undefined;
20-
let bin: string;
21-
let endpoint: string | undefined = undefined;
22-
2318
// 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)
2520
// - `http://localhost:{some-port}` (will forward the request to the http server)
2621
// - `{some-bin}` (will forward the event to the bin)
2722

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+
];
3727

3828
let childProcess: ChildProcess | undefined = undefined;
3929

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+
}
4236

43-
endpoint = handler.pathname;
37+
if (bin && endpoint) {
38+
log("Starting child process", { bin });
4439

4540
const subcommand = IS_OFFLINE === "true" ? "dev" : "start";
4641

42+
info(`Running: \`${bin} ${subcommand}\``);
43+
4744
childProcess = spawn(bin, [subcommand], {
4845
detached: true,
4946
stdio: "inherit",
@@ -55,10 +52,9 @@ export const bootstrap = async (): Promise<void> => {
5552
log("Started child process", { bin, subcommand, pid: childProcess.pid });
5653
}
5754

58-
try {
59-
log("Checking if bin is in PATH", { bin });
60-
await which(bin, { all: false });
55+
endpoint = endpoint ? new URL(endpoint) : undefined;
6156

57+
try {
6258
log("Routing events", { bin, endpoint });
6359
await routeEvents(AWS_LAMBDA_RUNTIME_API, bin, endpoint);
6460
} catch (e) {

src/log.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
import packageJson from "../package.json";
22

3+
export const info = (message: any, obj?: Record<string, any>): void => {
4+
console.log(
5+
`[${packageJson.name}@${packageJson.version}] ${message}`,
6+
obj ? JSON.stringify(obj) : undefined
7+
);
8+
};
9+
310
export const log = (message: any, obj?: Record<string, any>): void => {
411
if (!process.env.SLY_DEBUG) return;
512
console.log(
613
`[${packageJson.name}@${packageJson.version}] ${message}`,
7-
JSON.stringify(obj)
14+
obj ? JSON.stringify(obj) : undefined
815
);
916
};

src/proxy.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ function convertHeaders(
3131
}
3232

3333
const waitForEndpoint = async (
34-
endpoint: string,
34+
endpoint: URL,
3535
deadline: number
3636
): Promise<{ deadline: number }> => {
3737
const start = Date.now();
38-
const url = new URL(endpoint);
39-
const hostname = url.hostname;
40-
const port = parseInt(url.port, 10) || (url.protocol === "https:" ? 443 : 80);
38+
const hostname = endpoint.hostname;
39+
const port =
40+
parseInt(endpoint.port, 10) || (endpoint.protocol === "https:" ? 443 : 80);
4141

4242
return new Promise((resolve) => {
4343
const socket = new net.Socket();

src/routing.ts

+15-4
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import { log } from "./log";
55

66
export const routeEvents = async (
77
runtimeApi: string,
8-
bin: string,
9-
endpoint?: string
8+
bin?: string,
9+
endpoint?: URL
1010
): Promise<void> => {
1111
log("Waiting for next event from Lambda Runtime API", { runtimeApi });
1212

@@ -33,7 +33,7 @@ export const routeEvents = async (
3333

3434
let payload: any | undefined = undefined;
3535

36-
if (!endpoint) {
36+
if (bin && !endpoint) {
3737
log("No endpoint specified, executing bin", { bin });
3838

3939
const { execa } = await import("execa");
@@ -45,7 +45,7 @@ export const routeEvents = async (
4545
log("Bin execution complete", { bin, stdout });
4646

4747
payload = JSON.parse(stdout);
48-
} else {
48+
} else if (endpoint) {
4949
log("Endpoint specified, proxying request", { endpoint });
5050

5151
const request: EndpointRequest = {
@@ -58,6 +58,17 @@ export const routeEvents = async (
5858
payload = (await endpointProxy(request)).payload;
5959

6060
log("Proxy request complete", { endpoint, payload });
61+
} else {
62+
throw new Error(
63+
`
64+
Missing bin and handler on _HANDLER: ${process.env._HANDLER}.
65+
Expected format: {bin}@{endpoint} or {bin} or {endpoint}:
66+
- next@http://localhost:3000
67+
- /usr/bin/app@http://localhost:3000
68+
- http://localhost:3000
69+
- /usr/bin/app
70+
`
71+
);
6172
}
6273

6374
await axios.post(

src/types.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export type EndpointRequest = {
22
requestId: string;
3-
endpoint: string;
3+
endpoint: URL;
44
event: any;
55
initialDeadline: number;
66
};

0 commit comments

Comments
 (0)