-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebServer.ts
More file actions
269 lines (240 loc) · 8.07 KB
/
Copy pathwebServer.ts
File metadata and controls
269 lines (240 loc) · 8.07 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import { spawn } from "node:child_process";
import fs from "node:fs";
import http from "node:http";
import https from "node:https";
import path from "node:path";
import { URL } from "node:url";
import type { WebServerArgs } from "./args";
import { createUserError } from "../internal/userError";
const WEB_SERVER_POLL_INTERVAL_MS = 250;
function shouldSpawnInShell(command: string): boolean {
// If the command includes whitespace or quotes, users are likely passing something like:
// - `npm run dev`
// - `"C:\\Program Files\\My App\\app.exe" --flag`
// In these cases, using the OS shell preserves existing behavior, but it also means the
// string is interpreted by a shell. Treat it as trusted configuration.
return /[\s"'`]/.test(command.trim());
}
function resolveWindowsCommand(command: string): string {
// Avoid `shell: true` by resolving `npm` -> `npm.cmd`, etc.
const hasPathSep = command.includes("\\") || command.includes("/") || command.includes(":");
const ext = path.extname(command);
const rawPathEnv = process.env.PATH ?? process.env.Path ?? "";
const pathEntries = rawPathEnv
.split(";")
.map((entry) => entry.trim())
.filter(Boolean);
const pathext = (process.env.PATHEXT ?? ".COM;.EXE;.BAT;.CMD")
.split(";")
.map((e) => e.trim())
.filter(Boolean);
const tryResolve = (fullPath: string): string | undefined => {
// If command has no extension, prefer PATHEXT matches even if an extensionless
// file exists (e.g. Node installs `npm` + `npm.cmd`, but only `.cmd` is runnable).
if (!ext) {
for (const extension of pathext) {
const candidate = `${fullPath}${extension.toLowerCase()}`;
if (fs.existsSync(candidate)) return candidate;
}
}
if (fs.existsSync(fullPath)) return fullPath;
return undefined;
};
if (hasPathSep) {
const fullPath = path.isAbsolute(command) ? command : path.resolve(command);
return tryResolve(fullPath) ?? command;
}
// Approximate Windows command resolution order: check CWD first, then PATH.
const fromCwd = tryResolve(path.join(process.cwd(), command));
if (fromCwd) return fromCwd;
for (const entry of pathEntries) {
const resolved = tryResolve(path.join(entry, command));
if (resolved) return resolved;
}
return command;
}
function sleep(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}
function quoteCmdArg(value: string): string {
// Basic quoting for Windows `cmd.exe`. This is intentionally conservative.
if (value.length === 0) return "\"\"";
if (!/[ \t"]/g.test(value)) return value;
return `"${value.replace(/"/g, "\"\"")}"`;
}
async function killProcessTree(pid: number): Promise<void> {
if (!Number.isFinite(pid) || pid <= 0) return;
if (process.platform === "win32") {
const runTaskkill = async (
args: readonly string[],
timeoutMs: number,
): Promise<void> => {
await new Promise<void>((resolve) => {
const taskkill = spawn("taskkill", args, {
stdio: "ignore",
windowsHide: true,
});
const timeout = setTimeout(() => {
taskkill.kill();
resolve();
}, timeoutMs);
taskkill.on("error", () => {
clearTimeout(timeout);
resolve();
});
taskkill.on("exit", () => {
clearTimeout(timeout);
resolve();
});
});
};
const isPidAlive = (): boolean => {
try {
process.kill(pid, 0);
return true;
} catch {
return false;
}
};
// Try graceful termination first to allow cleanup; fall back to force-kill.
await runTaskkill(["/PID", String(pid), "/T"], 2_000);
await sleep(300);
if (isPidAlive()) await runTaskkill(["/PID", String(pid), "/T", "/F"], 5_000);
return;
}
// POSIX: negative PID kills the process group when spawned with `detached: true`.
try {
process.kill(-pid, "SIGINT");
} catch {
// ignore
}
await sleep(300);
try {
process.kill(-pid, "SIGKILL");
} catch {
// ignore
}
}
async function isUrlReachable(url: string): Promise<boolean> {
const parsed = new URL(url);
const client = parsed.protocol === "https:" ? https : http;
const isLocalhost =
parsed.hostname === "localhost" ||
parsed.hostname === "127.0.0.1" ||
parsed.hostname === "::1";
return new Promise((resolve) => {
const req = client.get(
{
protocol: parsed.protocol,
hostname: parsed.hostname,
port: parsed.port,
path: `${parsed.pathname}${parsed.search}`,
timeout: 1_000,
...(parsed.protocol === "https:" && isLocalhost
? { rejectUnauthorized: false }
: undefined),
},
(res) => {
res.resume();
const code = res.statusCode ?? 0;
resolve(code >= 200 && code < 500);
},
);
req.on("error", () => resolve(false));
req.on("timeout", () => {
req.destroy();
resolve(false);
});
});
}
async function waitForUrlOrExit(options: {
url: string;
timeoutMs: number;
child: ReturnType<typeof spawn>;
getSpawnError?: () => unknown;
}): Promise<void> {
const start = Date.now();
while (Date.now() - start < options.timeoutMs) {
const spawnError = options.getSpawnError?.();
if (spawnError) {
const message = spawnError instanceof Error ? spawnError.message : String(spawnError);
throw createUserError(`Failed to start web server: ${message}`);
}
if (options.child.exitCode !== null) {
throw createUserError(
`Web server exited with code ${options.child.exitCode} before becoming reachable at ${options.url}`,
);
}
// eslint-disable-next-line no-await-in-loop
const ok = await isUrlReachable(options.url);
if (ok) return;
// eslint-disable-next-line no-await-in-loop
await sleep(WEB_SERVER_POLL_INTERVAL_MS);
}
if (options.child.exitCode !== null) {
throw createUserError(
`Web server exited with code ${options.child.exitCode} before becoming reachable at ${options.url}`,
);
}
throw createUserError(`Timed out waiting for web server URL: ${options.url}`);
}
export async function withWebServer<T>(
webServer: WebServerArgs | undefined,
fn: () => Promise<T>,
): Promise<T> {
if (!webServer) return fn();
if (webServer.reuseExisting) {
const reachable = await isUrlReachable(webServer.url);
if (reachable) return fn();
}
let useShell = shouldSpawnInShell(webServer.command);
let command = webServer.command;
let forceQuotedCommandLine = false;
if (!useShell && process.platform === "win32") {
const resolved = resolveWindowsCommand(webServer.command);
const ext = path.extname(resolved).toLowerCase();
command = resolved;
// `.cmd` / `.bat` require `cmd.exe` (shell) to execute reliably.
if (ext === ".cmd" || ext === ".bat") {
useShell = true;
forceQuotedCommandLine = true;
}
}
const commandForSpawn = forceQuotedCommandLine
? [quoteCmdArg(command), ...webServer.args.map(quoteCmdArg)].join(" ")
: command;
const argsForSpawn = forceQuotedCommandLine ? [] : webServer.args;
const envForSpawn = webServer.env ? { ...process.env, ...webServer.env } : process.env;
let child: ReturnType<typeof spawn>;
try {
child = spawn(commandForSpawn, argsForSpawn, {
stdio: "inherit",
shell: useShell,
env: envForSpawn,
detached: process.platform !== "win32",
windowsHide: true,
});
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
throw createUserError(`Failed to start web server: ${message}`);
}
let spawnError: unknown;
child.once("error", (error) => {
spawnError = error;
});
try {
if (spawnError) {
const message = spawnError instanceof Error ? spawnError.message : String(spawnError);
throw createUserError(`Failed to start web server: ${message}`);
}
await waitForUrlOrExit({
url: webServer.url,
timeoutMs: webServer.timeoutMs,
child,
getSpawnError: () => spawnError,
});
return await fn();
} finally {
await killProcessTree(child.pid ?? 0);
}
}