bini-server 2.0.1 → 2.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +115 -197
- package/dist/cli.js +124 -179
- package/dist/cli.js.map +1 -1
- package/dist/index.js +148 -203
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/cli.js
CHANGED
|
@@ -27,23 +27,17 @@ import { Readable } from "stream";
|
|
|
27
27
|
import { pipeline } from "stream/promises";
|
|
28
28
|
import { loadEnvFiles, envFilesFor } from "bini-env/node";
|
|
29
29
|
function getEnvVar(key, defaultValue) {
|
|
30
|
-
|
|
31
|
-
if (process.env[biniKey] !== void 0) return process.env[biniKey];
|
|
32
|
-
const viteKey = `VITE_${key}`;
|
|
33
|
-
if (process.env[viteKey] !== void 0) return process.env[viteKey];
|
|
34
|
-
if (process.env[key] !== void 0) return process.env[key];
|
|
35
|
-
return defaultValue;
|
|
30
|
+
return process.env[`BINI_${key}`] ?? process.env[`VITE_${key}`] ?? process.env[key] ?? defaultValue;
|
|
36
31
|
}
|
|
37
32
|
function getEnvVarNumber(key, defaultValue) {
|
|
38
|
-
const
|
|
39
|
-
if (
|
|
40
|
-
const
|
|
41
|
-
return isNaN(
|
|
33
|
+
const raw = getEnvVar(key);
|
|
34
|
+
if (raw === void 0) return defaultValue;
|
|
35
|
+
const n = parseInt(raw, 10);
|
|
36
|
+
return isNaN(n) ? defaultValue : n;
|
|
42
37
|
}
|
|
43
38
|
function readConfig() {
|
|
44
39
|
const distDir = path.join(process.cwd(), getEnvVar("DIST_DIR") ?? "dist");
|
|
45
40
|
const apiDir = path.join(distDir, "api");
|
|
46
|
-
const apiEntry = path.join(apiDir, "index.mjs");
|
|
47
41
|
const rawBase = (getEnvVar("BASE") ?? "").trim();
|
|
48
42
|
const base = !rawBase || rawBase === "/" || rawBase === "." || rawBase === "./" ? "" : "/" + rawBase.replace(/^\/+|\/+$/g, "");
|
|
49
43
|
return {
|
|
@@ -54,7 +48,7 @@ function readConfig() {
|
|
|
54
48
|
distDir,
|
|
55
49
|
compiledApiDir: apiDir + path.sep,
|
|
56
50
|
apiDir,
|
|
57
|
-
apiEntry,
|
|
51
|
+
apiEntry: path.join(apiDir, "index.mjs"),
|
|
58
52
|
base,
|
|
59
53
|
isProd: process.env.NODE_ENV === "production"
|
|
60
54
|
};
|
|
@@ -66,82 +60,75 @@ function resolveApiEntry() {
|
|
|
66
60
|
return fs.existsSync(config.apiEntry) ? config.apiEntry : null;
|
|
67
61
|
}
|
|
68
62
|
function stripBase(pathname) {
|
|
69
|
-
const base = config
|
|
63
|
+
const { base } = config;
|
|
70
64
|
if (!base) return pathname || "/";
|
|
71
65
|
if (pathname === base || pathname === base + "/") return "/";
|
|
72
66
|
if (pathname.startsWith(base + "/")) return pathname.slice(base.length) || "/";
|
|
73
67
|
return null;
|
|
74
68
|
}
|
|
69
|
+
function toApiPath(pathname) {
|
|
70
|
+
let p = pathname;
|
|
71
|
+
const { base } = config;
|
|
72
|
+
if (base) {
|
|
73
|
+
if (p === base || p.startsWith(base + "/")) p = p.slice(base.length) || "/";
|
|
74
|
+
else return null;
|
|
75
|
+
}
|
|
76
|
+
if (p === "/api") return "/";
|
|
77
|
+
if (p.startsWith("/api/")) return p.slice(4);
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
function isApiRequest(url) {
|
|
81
|
+
if (!url) return false;
|
|
82
|
+
try {
|
|
83
|
+
return toApiPath(new URL(url, "http://localhost").pathname) !== null;
|
|
84
|
+
} catch {
|
|
85
|
+
return false;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
75
88
|
function scanShellRoutes() {
|
|
76
|
-
const
|
|
77
|
-
|
|
78
|
-
|
|
89
|
+
const routes = /* @__PURE__ */ new Set();
|
|
90
|
+
function walk(dir, prefix) {
|
|
91
|
+
let entries;
|
|
79
92
|
try {
|
|
80
|
-
|
|
81
|
-
for (const entry of entries) {
|
|
82
|
-
if (!entry.isDirectory()) continue;
|
|
83
|
-
if (entry.name.startsWith(".") || entry.name === "node_modules" || entry.name === "api") continue;
|
|
84
|
-
const fullPath = path.join(dir, entry.name);
|
|
85
|
-
const routePath = currentPath ? `${currentPath}/${entry.name}` : entry.name;
|
|
86
|
-
if (entry.name.startsWith("[") && entry.name.endsWith("]")) {
|
|
87
|
-
const indexPath = path.join(fullPath, "index.html");
|
|
88
|
-
if (fs.existsSync(indexPath)) {
|
|
89
|
-
shellRoutes.add(`/${routePath}`);
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
walkDir(fullPath, routePath);
|
|
93
|
-
}
|
|
93
|
+
entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
94
94
|
} catch {
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
for (const entry of entries) {
|
|
98
|
+
if (!entry.isDirectory()) continue;
|
|
99
|
+
if (entry.name.startsWith(".") || entry.name === "node_modules" || entry.name === "api") continue;
|
|
100
|
+
const fullPath = path.join(dir, entry.name);
|
|
101
|
+
const routePath = prefix ? `${prefix}/${entry.name}` : entry.name;
|
|
102
|
+
if (entry.name.startsWith("[") && entry.name.endsWith("]") && fs.existsSync(path.join(fullPath, "index.html"))) {
|
|
103
|
+
routes.add(`/${routePath}`);
|
|
104
|
+
}
|
|
105
|
+
walk(fullPath, routePath);
|
|
95
106
|
}
|
|
96
107
|
}
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
} catch {
|
|
100
|
-
}
|
|
101
|
-
return shellRoutes;
|
|
108
|
+
walk(config.distDir, "");
|
|
109
|
+
return routes;
|
|
102
110
|
}
|
|
103
111
|
function getShellRoutes() {
|
|
104
|
-
|
|
105
|
-
shellRouteCache = scanShellRoutes();
|
|
106
|
-
}
|
|
107
|
-
return shellRouteCache;
|
|
112
|
+
return shellRouteCache ??= scanShellRoutes();
|
|
108
113
|
}
|
|
109
114
|
function findMatchingShellRoute(pathname) {
|
|
110
|
-
const shellRoutes = getShellRoutes();
|
|
111
115
|
const pathSegments = pathname.replace(/^\/|\/$/g, "").split("/").filter(Boolean);
|
|
112
|
-
for (const shellRoute of
|
|
116
|
+
for (const shellRoute of getShellRoutes()) {
|
|
113
117
|
const shellSegments = shellRoute.replace(/^\/|\/$/g, "").split("/").filter(Boolean);
|
|
114
118
|
if (shellSegments.length === 0) continue;
|
|
115
119
|
const last = shellSegments[shellSegments.length - 1];
|
|
116
120
|
const isRequiredCatchAll = last.startsWith("[...") && !last.startsWith("[[...");
|
|
117
|
-
const isOptionalCatchAll = last.startsWith("[[
|
|
118
|
-
|
|
119
|
-
if (isCatchAll) {
|
|
121
|
+
const isOptionalCatchAll = last.startsWith("[[") && last.endsWith("]]");
|
|
122
|
+
if (isRequiredCatchAll || isOptionalCatchAll) {
|
|
120
123
|
const prefix = shellSegments.slice(0, -1);
|
|
121
124
|
if (pathSegments.length < prefix.length) continue;
|
|
122
125
|
if (isRequiredCatchAll && pathSegments.length === prefix.length) continue;
|
|
123
|
-
|
|
124
|
-
for (let i = 0; i < prefix.length; i++) {
|
|
125
|
-
const seg = prefix[i];
|
|
126
|
-
if (seg.startsWith("[") && seg.endsWith("]")) continue;
|
|
127
|
-
if (seg !== pathSegments[i]) {
|
|
128
|
-
matches2 = false;
|
|
129
|
-
break;
|
|
130
|
-
}
|
|
131
|
-
}
|
|
126
|
+
const matches2 = prefix.every((seg, i) => seg.startsWith("[") && seg.endsWith("]") || seg === pathSegments[i]);
|
|
132
127
|
if (matches2) return shellRoute;
|
|
133
128
|
continue;
|
|
134
129
|
}
|
|
135
130
|
if (shellSegments.length !== pathSegments.length) continue;
|
|
136
|
-
|
|
137
|
-
for (let i = 0; i < shellSegments.length; i++) {
|
|
138
|
-
const seg = shellSegments[i];
|
|
139
|
-
if (seg.startsWith("[") && seg.endsWith("]")) continue;
|
|
140
|
-
if (seg !== pathSegments[i]) {
|
|
141
|
-
matches = false;
|
|
142
|
-
break;
|
|
143
|
-
}
|
|
144
|
-
}
|
|
131
|
+
const matches = shellSegments.every((seg, i) => seg.startsWith("[") && seg.endsWith("]") || seg === pathSegments[i]);
|
|
145
132
|
if (matches) return shellRoute;
|
|
146
133
|
}
|
|
147
134
|
return null;
|
|
@@ -149,6 +136,7 @@ function findMatchingShellRoute(pathname) {
|
|
|
149
136
|
function getNetworkIp() {
|
|
150
137
|
const ifaces = os.networkInterfaces();
|
|
151
138
|
const SKIP = /docker|veth|br-|lo|loopback|vmnet|vbox|utun|tun|tap/i;
|
|
139
|
+
const candidates = [];
|
|
152
140
|
for (const name in ifaces) {
|
|
153
141
|
if (SKIP.test(name)) continue;
|
|
154
142
|
for (const iface of ifaces[name] ?? []) {
|
|
@@ -157,15 +145,10 @@ function getNetworkIp() {
|
|
|
157
145
|
if (a === 169 && b === 254) continue;
|
|
158
146
|
const isPrivate = a === 10 || a === 172 && b >= 16 && b <= 31 || a === 192 && b === 168;
|
|
159
147
|
if (isPrivate) return iface.address;
|
|
148
|
+
candidates.push(iface.address);
|
|
160
149
|
}
|
|
161
150
|
}
|
|
162
|
-
|
|
163
|
-
if (SKIP.test(name)) continue;
|
|
164
|
-
for (const iface of ifaces[name] ?? []) {
|
|
165
|
-
if (!iface.internal && iface.family === "IPv4") return iface.address;
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
return null;
|
|
151
|
+
return candidates[0] ?? null;
|
|
169
152
|
}
|
|
170
153
|
function printBanner(port) {
|
|
171
154
|
const envFiles2 = envFilesFor(process.cwd(), process.env.NODE_ENV ?? "production");
|
|
@@ -186,7 +169,7 @@ function printBanner(port) {
|
|
|
186
169
|
console.log(` ${C.DIM}${C.GREEN}\u279C${C.RESET} ${C.DIM}press ${C.RESET}h + enter${C.DIM} to show help${C.RESET}`);
|
|
187
170
|
console.log("");
|
|
188
171
|
}
|
|
189
|
-
|
|
172
|
+
function isPortFree(port) {
|
|
190
173
|
return new Promise((resolve) => {
|
|
191
174
|
const server = net.createServer();
|
|
192
175
|
server.once("error", () => resolve(false));
|
|
@@ -196,9 +179,7 @@ async function isPortFree(port) {
|
|
|
196
179
|
}
|
|
197
180
|
async function findFreePort(start3) {
|
|
198
181
|
for (let port = start3; port < start3 + 100; port++) {
|
|
199
|
-
if (await isPortFree(port))
|
|
200
|
-
return port;
|
|
201
|
-
}
|
|
182
|
+
if (await isPortFree(port)) return port;
|
|
202
183
|
console.log(`Port ${port} is in use, trying another one...`);
|
|
203
184
|
}
|
|
204
185
|
throw new Error(`No free port found in range ${start3}\u2013${start3 + 99}`);
|
|
@@ -263,64 +244,57 @@ function normalizeHeaders(raw) {
|
|
|
263
244
|
}
|
|
264
245
|
return out;
|
|
265
246
|
}
|
|
266
|
-
function
|
|
267
|
-
|
|
268
|
-
const
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
if (p.startsWith("/api/")) return p.slice(4);
|
|
275
|
-
return null;
|
|
247
|
+
async function toWebRequest(req, host) {
|
|
248
|
+
const method = (req.method ?? "GET").toUpperCase();
|
|
249
|
+
const body = method !== "GET" && method !== "HEAD" ? await readBody(req) : void 0;
|
|
250
|
+
return new Request(`http://${host}${req.url ?? "/"}`, {
|
|
251
|
+
method,
|
|
252
|
+
headers: normalizeHeaders(req.headers),
|
|
253
|
+
body: body ? new Uint8Array(body) : null
|
|
254
|
+
});
|
|
276
255
|
}
|
|
277
|
-
function
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
256
|
+
function noApiFetch() {
|
|
257
|
+
return async () => new Response(
|
|
258
|
+
JSON.stringify({ error: "No API routes compiled" }),
|
|
259
|
+
{ status: 503, headers: { "content-type": "application/json" } }
|
|
260
|
+
);
|
|
261
|
+
}
|
|
262
|
+
function failedApiFetch() {
|
|
263
|
+
return async () => new Response(
|
|
264
|
+
JSON.stringify({ error: "API failed to load" }),
|
|
265
|
+
{ status: 500, headers: { "content-type": "application/json" } }
|
|
266
|
+
);
|
|
284
267
|
}
|
|
285
268
|
async function loadApiFetch() {
|
|
286
269
|
if (apiFetch) return apiFetch;
|
|
287
270
|
if (apiLoadPromise) return apiLoadPromise;
|
|
288
271
|
apiLoadPromise = (async () => {
|
|
272
|
+
const entry = resolveApiEntry();
|
|
273
|
+
if (!entry) {
|
|
274
|
+
try {
|
|
275
|
+
await import(pathToFileURL(config.apiEntry).href);
|
|
276
|
+
} catch (e) {
|
|
277
|
+
const tag = process.stdout.isTTY ? `\x1B[33m[WARN]\x1B[0m` : `[WARN]`;
|
|
278
|
+
console.warn(`
|
|
279
|
+
${tag} failed to load handler for /api:`, e);
|
|
280
|
+
}
|
|
281
|
+
apiFetch = noApiFetch();
|
|
282
|
+
return apiFetch;
|
|
283
|
+
}
|
|
289
284
|
try {
|
|
290
|
-
const entry = resolveApiEntry();
|
|
291
|
-
if (!entry) throw new Error(`${config.apiEntry} not found`);
|
|
292
285
|
const mod = await import(pathToFileURL(entry).href);
|
|
293
|
-
if (typeof mod.fetch
|
|
294
|
-
|
|
286
|
+
if (typeof mod.fetch === "function") {
|
|
287
|
+
apiFetch = mod.fetch;
|
|
288
|
+
} else {
|
|
289
|
+
apiFetch = failedApiFetch();
|
|
295
290
|
}
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
const msg = err instanceof Error ? err.message : String(err);
|
|
299
|
-
console.warn(
|
|
300
|
-
`
|
|
301
|
-
${C.YELLOW}\u26A0${C.RESET} No compiled API in ${config.apiDir}.
|
|
302
|
-
${C.DIM}${msg}${C.RESET}
|
|
303
|
-
API requests will return 404. Run ${C.CYAN}npm run build${C.RESET} to compile it.
|
|
304
|
-
`
|
|
305
|
-
);
|
|
306
|
-
apiFetch = async () => new Response(
|
|
307
|
-
JSON.stringify({ error: "No API routes compiled" }),
|
|
308
|
-
{ status: 404, headers: { "content-type": "application/json" } }
|
|
309
|
-
);
|
|
291
|
+
} catch {
|
|
292
|
+
apiFetch = failedApiFetch();
|
|
310
293
|
}
|
|
311
294
|
return apiFetch;
|
|
312
295
|
})();
|
|
313
296
|
return apiLoadPromise;
|
|
314
297
|
}
|
|
315
|
-
async function toWebRequest(req, host) {
|
|
316
|
-
const method = (req.method ?? "GET").toUpperCase();
|
|
317
|
-
const body = method !== "GET" && method !== "HEAD" ? await readBody(req) : void 0;
|
|
318
|
-
return new Request(`http://${host}${req.url ?? "/"}`, {
|
|
319
|
-
method,
|
|
320
|
-
headers: normalizeHeaders(req.headers),
|
|
321
|
-
body: body ? new Uint8Array(body) : null
|
|
322
|
-
});
|
|
323
|
-
}
|
|
324
298
|
function sendJson(res, status, body, extra = {}) {
|
|
325
299
|
res.writeHead(status, { "content-type": "application/json", ...extra });
|
|
326
300
|
res.end(JSON.stringify(body));
|
|
@@ -363,8 +337,10 @@ async function handleApiRequest(req, res) {
|
|
|
363
337
|
} catch {
|
|
364
338
|
return sendJson(res, 400, { error: "Bad request URL" });
|
|
365
339
|
}
|
|
366
|
-
if (toApiPath(parsedUrl.pathname) === null)
|
|
367
|
-
|
|
340
|
+
if (toApiPath(parsedUrl.pathname) === null) {
|
|
341
|
+
return sendJson(res, 404, { error: "Not Found" });
|
|
342
|
+
}
|
|
343
|
+
await loadApiFetch();
|
|
368
344
|
let webReq;
|
|
369
345
|
try {
|
|
370
346
|
webReq = await toWebRequest(req, host);
|
|
@@ -372,20 +348,20 @@ async function handleApiRequest(req, res) {
|
|
|
372
348
|
const message = e?.message ?? "Bad request";
|
|
373
349
|
return sendJson(res, message.includes("too large") ? 413 : 408, { error: message });
|
|
374
350
|
}
|
|
375
|
-
const ctx = {
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
}
|
|
380
|
-
};
|
|
351
|
+
const ctx = { waitUntil(p) {
|
|
352
|
+
p.catch(() => {
|
|
353
|
+
});
|
|
354
|
+
} };
|
|
381
355
|
try {
|
|
382
356
|
const webRes = await withTimeout(
|
|
383
|
-
|
|
357
|
+
apiFetch(webReq, process.env, ctx),
|
|
384
358
|
config.handlerTimeout
|
|
385
359
|
);
|
|
386
360
|
await sendWebResponse(res, webRes, method);
|
|
387
361
|
} catch (e) {
|
|
388
|
-
|
|
362
|
+
const tag = process.stdout.isTTY ? `\x1B[33m[WARN]\x1B[0m` : `[WARN]`;
|
|
363
|
+
console.error(`
|
|
364
|
+
${tag} failed to handle ${method} ${parsedUrl.pathname}:`, e);
|
|
389
365
|
if (!res.headersSent) sendJson(res, 500, { error: publicErrorMessage(e) });
|
|
390
366
|
else res.end();
|
|
391
367
|
}
|
|
@@ -395,7 +371,6 @@ function getETag(stat) {
|
|
|
395
371
|
}
|
|
396
372
|
async function sendFile(filePath, req, res, status = 200) {
|
|
397
373
|
const ext = path.extname(filePath).toLowerCase();
|
|
398
|
-
const mimeType = MIME_TYPES[ext] ?? "application/octet-stream";
|
|
399
374
|
const isAsset = filePath.includes(`${path.sep}assets${path.sep}`);
|
|
400
375
|
let stat;
|
|
401
376
|
try {
|
|
@@ -412,7 +387,7 @@ async function sendFile(filePath, req, res, status = 200) {
|
|
|
412
387
|
return;
|
|
413
388
|
}
|
|
414
389
|
res.writeHead(status, {
|
|
415
|
-
"Content-Type":
|
|
390
|
+
"Content-Type": MIME_TYPES[ext] ?? "application/octet-stream",
|
|
416
391
|
"Content-Length": stat.size,
|
|
417
392
|
"Cache-Control": isAsset ? "public, max-age=31536000, immutable" : "no-cache",
|
|
418
393
|
"ETag": etag,
|
|
@@ -427,65 +402,39 @@ async function sendFile(filePath, req, res, status = 200) {
|
|
|
427
402
|
} catch {
|
|
428
403
|
}
|
|
429
404
|
}
|
|
405
|
+
async function trySendFile(filePath, req, res, status = 200) {
|
|
406
|
+
try {
|
|
407
|
+
const stat = await fs.promises.stat(filePath);
|
|
408
|
+
if (!stat.isFile()) return false;
|
|
409
|
+
await sendFile(filePath, req, res, status);
|
|
410
|
+
return true;
|
|
411
|
+
} catch {
|
|
412
|
+
return false;
|
|
413
|
+
}
|
|
414
|
+
}
|
|
430
415
|
async function serveStatic(req, res, next) {
|
|
431
416
|
const rawPathname = new URL(req.url ?? "/", "http://localhost").pathname;
|
|
432
417
|
if (toApiPath(rawPathname) !== null) return next();
|
|
433
418
|
const pathname = stripBase(rawPathname);
|
|
434
419
|
if (pathname === null) return next();
|
|
435
|
-
if (pathname.includes("\0") || pathname.split("/").some((s) => s === ".."))
|
|
436
|
-
return next();
|
|
437
|
-
}
|
|
420
|
+
if (pathname.includes("\0") || pathname.split("/").some((s) => s === "..")) return next();
|
|
438
421
|
const hasExtension = path.extname(pathname).length > 0;
|
|
439
422
|
if (hasExtension) {
|
|
440
423
|
const filePath = path.join(config.distDir, pathname);
|
|
441
424
|
if (filePath.startsWith(config.compiledApiDir)) return next();
|
|
442
|
-
|
|
443
|
-
const stat = await fs.promises.stat(filePath);
|
|
444
|
-
if (stat.isFile()) {
|
|
445
|
-
await sendFile(filePath, req, res);
|
|
446
|
-
return;
|
|
447
|
-
}
|
|
448
|
-
} catch {
|
|
449
|
-
}
|
|
425
|
+
if (await trySendFile(filePath, req, res)) return;
|
|
450
426
|
return next();
|
|
451
427
|
}
|
|
452
|
-
const
|
|
453
|
-
|
|
454
|
-
const stat = await fs.promises.stat(staticFilePath);
|
|
455
|
-
if (stat.isFile()) {
|
|
456
|
-
await sendFile(staticFilePath, req, res);
|
|
457
|
-
return;
|
|
458
|
-
}
|
|
459
|
-
} catch {
|
|
460
|
-
}
|
|
428
|
+
const staticIndex = path.join(config.distDir, pathname === "/" ? "" : pathname, "index.html");
|
|
429
|
+
if (await trySendFile(staticIndex, req, res)) return;
|
|
461
430
|
const matchingShell = findMatchingShellRoute(pathname);
|
|
462
431
|
if (matchingShell) {
|
|
463
|
-
const
|
|
464
|
-
|
|
465
|
-
const stat = await fs.promises.stat(shellPath);
|
|
466
|
-
if (stat.isFile()) {
|
|
467
|
-
await sendFile(shellPath, req, res);
|
|
468
|
-
return;
|
|
469
|
-
}
|
|
470
|
-
} catch {
|
|
471
|
-
}
|
|
472
|
-
}
|
|
473
|
-
const notFoundPath = path.join(config.distDir, "404.html");
|
|
474
|
-
try {
|
|
475
|
-
const stat = await fs.promises.stat(notFoundPath);
|
|
476
|
-
if (stat.isFile()) {
|
|
477
|
-
await sendFile(notFoundPath, req, res, 404);
|
|
478
|
-
return;
|
|
479
|
-
}
|
|
480
|
-
} catch {
|
|
481
|
-
}
|
|
482
|
-
const indexPath = path.join(config.distDir, "index.html");
|
|
483
|
-
try {
|
|
484
|
-
await fs.promises.access(indexPath);
|
|
485
|
-
await sendFile(indexPath, req, res);
|
|
486
|
-
} catch {
|
|
487
|
-
next();
|
|
432
|
+
const shellIndex = path.join(config.distDir, matchingShell, "index.html");
|
|
433
|
+
if (await trySendFile(shellIndex, req, res)) return;
|
|
488
434
|
}
|
|
435
|
+
if (await trySendFile(path.join(config.distDir, "404.html"), req, res, 404)) return;
|
|
436
|
+
if (await trySendFile(path.join(config.distDir, "index.html"), req, res)) return;
|
|
437
|
+
next();
|
|
489
438
|
}
|
|
490
439
|
function compose(middlewares) {
|
|
491
440
|
return async (req, res) => {
|
|
@@ -515,7 +464,6 @@ async function start() {
|
|
|
515
464
|
`);
|
|
516
465
|
process.exit(1);
|
|
517
466
|
}
|
|
518
|
-
await loadApiFetch();
|
|
519
467
|
getShellRoutes();
|
|
520
468
|
const port = await findFreePort(config.defaultPort);
|
|
521
469
|
const handler = compose([
|
|
@@ -542,9 +490,7 @@ async function start() {
|
|
|
542
490
|
server.headersTimeout = 66e3;
|
|
543
491
|
server.maxHeadersCount = 2e3;
|
|
544
492
|
startKeyboardLoop(port);
|
|
545
|
-
server.listen(port, "0.0.0.0", () =>
|
|
546
|
-
printBanner(port);
|
|
547
|
-
});
|
|
493
|
+
server.listen(port, "0.0.0.0", () => printBanner(port));
|
|
548
494
|
const shutdown = () => {
|
|
549
495
|
server.close(() => process.exit(0));
|
|
550
496
|
setTimeout(() => process.exit(1), 1e4).unref();
|
|
@@ -563,7 +509,6 @@ var init_src = __esm({
|
|
|
563
509
|
GREEN: "\x1B[32m",
|
|
564
510
|
RED: "\x1B[31m",
|
|
565
511
|
YELLOW: "\x1B[33m",
|
|
566
|
-
BOLD: "\x1B[1m",
|
|
567
512
|
DIM: "\x1B[2m"
|
|
568
513
|
};
|
|
569
514
|
shellRouteCache = null;
|