-
-
Notifications
You must be signed in to change notification settings - Fork 113
Expand file tree
/
Copy pathserve.ts
More file actions
172 lines (143 loc) · 4.19 KB
/
serve.ts
File metadata and controls
172 lines (143 loc) · 4.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import { parseArgs } from "./deps/cli.ts";
/**
* This module implements an HTTP server that starts a Lume server
* and proxies requests to it.
*
* You use it by running: deno serve -A lume/serve.ts
*/
// Capture flags to pass to the server
const flags = parseArgs(Deno.args, {
string: ["port", "hostname", "location"],
default: {
port: "3000",
hostname: "localhost",
showTerminal: false,
},
});
export function getServeHandler(): Deno.ServeHandler {
const { port, hostname } = flags;
let process:
| { process: Deno.ChildProcess; ready: boolean; error: boolean }
| undefined;
let timeout: number | undefined;
return async function (request: Request): Promise<Response> {
const url = new URL(request.url);
// Start the server on the first request
if (!process?.ready) {
return startServer(url);
}
// Close the server after 2 hours of inactivity
clearTimeout(timeout);
timeout = setTimeout(closeServerProcess, 2 * 60 * 60 * 1000);
// Forward the request to the server
url.port = port;
const headers = new Headers(request.headers);
headers.set("host", url.host);
headers.set("origin", url.origin);
if (headers.get("upgrade") === "websocket") {
return proxyWebSocket(request);
}
const response = await fetch(url, {
redirect: "manual",
headers,
method: request.method,
body: request.body,
});
// Close the server if the response header tells us to
if (response.headers.get("X-Lume-CMS") === "reload") {
await closeServerProcess();
const url = response.headers.get("X-Lume-Location") ||
response.headers.get("Location") || request.url;
return startServer(new URL(url, request.url));
}
return response;
};
async function startServer(url: URL): Promise<Response> {
await startProcess(url);
if (process?.error) {
process = undefined;
return new Response("Error starting the server", { status: 500 });
}
const body = `<head><meta http-equiv="refresh" content="0"></head>`;
return new Response(body, {
status: 200,
headers: {
"Content-Type": "text/html",
},
});
}
// Start the server process
async function startProcess(location: URL): Promise<void> {
if (process?.ready === false) {
return;
}
console.log(`Start proxied server on port ${port}`);
const command = new Deno.Command(Deno.execPath(), {
env: Deno.env.toObject(),
stdout: "inherit",
stderr: "inherit",
args: [
"task",
"lume",
"--serve",
`--port=${port}`,
`--hostname=${hostname}`,
`--location=${flags.location || location.origin}`,
],
});
process = {
process: command.spawn(),
ready: false,
error: false,
};
process.process.status.then((status) => {
if (process && status.success === false && status.signal !== "SIGTERM") {
process!.error = true;
} else {
closeServerProcess();
}
});
// Wait for the server to start
const timeout = 1000;
while (true) {
if (process.error) {
return;
}
try {
await fetch(`http://${hostname}:${port}`);
process.ready = true;
break;
} catch {
await new Promise((resolve) => setTimeout(resolve, timeout));
}
}
}
// Close the server process
async function closeServerProcess() {
try {
if (process) {
process.process.kill();
await process.process.output();
}
} catch {
// The process is already dead
}
process = undefined;
}
// Proxy the WebSocket connection
function proxyWebSocket(request: Request) {
const { socket, response } = Deno.upgradeWebSocket(request);
const { pathname } = new URL(request.url);
const origin = new WebSocket(`ws://${hostname}:${port}${pathname}`);
origin.onopen = () => {
socket.onmessage = (event) => origin.send(event.data);
origin.onmessage = (event) => socket.send(event.data);
socket.onclose = () => origin.close();
origin.onclose = () => socket.close();
};
return response;
}
}
export default {
fetch: getServeHandler(),
};