@inflector/aura 0.6.8 → 0.7.1
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/dist/bin.d.ts +1 -0
- package/dist/bin.d.ts.map +1 -1
- package/dist/bin.js +153 -2
- package/package.json +1 -1
package/dist/bin.d.ts
CHANGED
package/dist/bin.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bin.d.ts","sourceRoot":"","sources":["../src/bin.ts"],"names":[],"mappings":";AAmLA,wBAAgB,KAAK,CAAC,MAAM,UAAQ,QAgDnC;
|
|
1
|
+
{"version":3,"file":"bin.d.ts","sourceRoot":"","sources":["../src/bin.ts"],"names":[],"mappings":";AAmLA,wBAAgB,KAAK,CAAC,MAAM,UAAQ,QAgDnC;AAwJD,wBAAsB,KAAK,CAAC,IAAI,EAAE,KAAK,GAAG,MAAM,iBAqL/C;AAmRD,wBAAsB,GAAG,kBAiFxB"}
|
package/dist/bin.js
CHANGED
|
@@ -321,6 +321,157 @@ async function pull() {
|
|
|
321
321
|
log.error(err.message);
|
|
322
322
|
}
|
|
323
323
|
}
|
|
324
|
+
export async function serve(mode) {
|
|
325
|
+
let Config;
|
|
326
|
+
try {
|
|
327
|
+
Config = JSON.parse(fs.readFileSync("./Aura/config.json", "utf-8"));
|
|
328
|
+
}
|
|
329
|
+
catch {
|
|
330
|
+
log.error("Config file not found. Run `aura init` first.");
|
|
331
|
+
return;
|
|
332
|
+
}
|
|
333
|
+
const upstreamUrl = Config.Url.replace(/\/$/, "");
|
|
334
|
+
const AURA_KEY = process.env.AURA_KEY;
|
|
335
|
+
const proxyRequest = async (req) => {
|
|
336
|
+
const url = new URL(req.url);
|
|
337
|
+
const targetUrl = `${upstreamUrl}${url.pathname}${url.search}`;
|
|
338
|
+
const headers = new Headers();
|
|
339
|
+
// @ts-ignore
|
|
340
|
+
for (const [key, value] of req.headers.entries()) {
|
|
341
|
+
if (["host", "accept-encoding", "connection", "transfer-encoding"].includes(key.toLowerCase()))
|
|
342
|
+
continue;
|
|
343
|
+
headers.set(key, value);
|
|
344
|
+
}
|
|
345
|
+
headers.set("x-forwarded-host", req.headers.get("host") ?? "");
|
|
346
|
+
headers.set("x-forwarded-proto", req.headers.get("x-forwarded-proto") ?? "http");
|
|
347
|
+
headers.set("x-forwarded-for", req.headers.get("x-forwarded-for") ?? "");
|
|
348
|
+
if (AURA_KEY)
|
|
349
|
+
headers.set("Authorization", `Bearer ${AURA_KEY}`);
|
|
350
|
+
try {
|
|
351
|
+
const upstream = await fetch(targetUrl, {
|
|
352
|
+
method: req.method,
|
|
353
|
+
headers,
|
|
354
|
+
body: req.method === "GET" || req.method === "HEAD" ? undefined : req.body,
|
|
355
|
+
signal: req.signal,
|
|
356
|
+
});
|
|
357
|
+
const resHeaders = new Headers();
|
|
358
|
+
// @ts-ignore
|
|
359
|
+
for (const [key, value] of upstream.headers.entries()) {
|
|
360
|
+
if (["content-encoding", "content-length", "transfer-encoding"].includes(key.toLowerCase()))
|
|
361
|
+
continue;
|
|
362
|
+
resHeaders.append(key, value);
|
|
363
|
+
}
|
|
364
|
+
return new Response(upstream.body, {
|
|
365
|
+
status: upstream.status,
|
|
366
|
+
statusText: upstream.statusText,
|
|
367
|
+
headers: resHeaders,
|
|
368
|
+
});
|
|
369
|
+
}
|
|
370
|
+
catch (err) {
|
|
371
|
+
if (err.name === "AbortError" || req.signal?.aborted) {
|
|
372
|
+
return new Response(null, { status: 499 });
|
|
373
|
+
}
|
|
374
|
+
return new Response(JSON.stringify({ error: "Upstream Proxy Failed" }), {
|
|
375
|
+
status: 502,
|
|
376
|
+
headers: { "Content-Type": "application/json" },
|
|
377
|
+
});
|
|
378
|
+
}
|
|
379
|
+
};
|
|
380
|
+
if (mode === "dev") {
|
|
381
|
+
const { createServer, mergeConfig, loadConfigFromFile } = await import("vite");
|
|
382
|
+
const configFile = path.resolve(process.cwd(), "vite.config.ts");
|
|
383
|
+
const configResult = await loadConfigFromFile({ command: "serve", mode: "development" }, configFile);
|
|
384
|
+
if (!configResult?.config) {
|
|
385
|
+
log.error("Could not load vite.config.ts");
|
|
386
|
+
return;
|
|
387
|
+
}
|
|
388
|
+
const vite = await createServer(mergeConfig(configResult.config, {
|
|
389
|
+
configFile: false,
|
|
390
|
+
server: {
|
|
391
|
+
proxy: {
|
|
392
|
+
"/api": {
|
|
393
|
+
target: upstreamUrl,
|
|
394
|
+
changeOrigin: false, // don't rewrite origin — breaks cookies
|
|
395
|
+
secure: false,
|
|
396
|
+
ws: true,
|
|
397
|
+
configure(proxy) {
|
|
398
|
+
proxy.on("proxyReq", (proxyReq, req) => {
|
|
399
|
+
// Rewrite the path using the raw unmodified URL from the request
|
|
400
|
+
proxyReq.path = req.url; // ← keeps the original encoding intact
|
|
401
|
+
proxyReq.setHeader("connection", "close");
|
|
402
|
+
if (AURA_KEY)
|
|
403
|
+
proxyReq.setHeader("Authorization", `Bearer ${AURA_KEY}`);
|
|
404
|
+
if (req.headers.cookie)
|
|
405
|
+
proxyReq.setHeader("cookie", req.headers.cookie);
|
|
406
|
+
});
|
|
407
|
+
proxy.on("proxyRes", (proxyRes) => {
|
|
408
|
+
// ensure set-cookie is not stripped
|
|
409
|
+
if (proxyRes.headers["set-cookie"]) {
|
|
410
|
+
proxyRes.headers["set-cookie"] = proxyRes.headers["set-cookie"].map((c) => c
|
|
411
|
+
.replace(/; secure/gi, "")
|
|
412
|
+
.replace(/; samesite=none/gi, ""));
|
|
413
|
+
}
|
|
414
|
+
});
|
|
415
|
+
},
|
|
416
|
+
},
|
|
417
|
+
},
|
|
418
|
+
},
|
|
419
|
+
}));
|
|
420
|
+
if (vite.bindCLIShortcuts)
|
|
421
|
+
vite.bindCLIShortcuts({ print: true });
|
|
422
|
+
await vite.listen();
|
|
423
|
+
vite.printUrls();
|
|
424
|
+
return;
|
|
425
|
+
}
|
|
426
|
+
// prod
|
|
427
|
+
const configFile = path.resolve(process.cwd(), "vite.config.ts");
|
|
428
|
+
const configResult = await loadConfigFromFile({ command: "serve", mode: "development" }, configFile);
|
|
429
|
+
const port = configResult?.config?.preview?.port ?? 80;
|
|
430
|
+
const getMimeType = (p) => {
|
|
431
|
+
if (p.endsWith(".js"))
|
|
432
|
+
return "application/javascript";
|
|
433
|
+
if (p.endsWith(".css"))
|
|
434
|
+
return "text/css";
|
|
435
|
+
if (p.endsWith(".html"))
|
|
436
|
+
return "text/html";
|
|
437
|
+
if (p.endsWith(".json"))
|
|
438
|
+
return "application/json";
|
|
439
|
+
if (p.endsWith(".png"))
|
|
440
|
+
return "image/png";
|
|
441
|
+
if (p.endsWith(".jpg") || p.endsWith(".jpeg"))
|
|
442
|
+
return "image/jpeg";
|
|
443
|
+
if (p.endsWith(".svg"))
|
|
444
|
+
return "image/svg+xml";
|
|
445
|
+
return "application/octet-stream";
|
|
446
|
+
};
|
|
447
|
+
Bun.serve({
|
|
448
|
+
hostname: "0.0.0.0",
|
|
449
|
+
port,
|
|
450
|
+
idleTimeout: 0,
|
|
451
|
+
async fetch(req) {
|
|
452
|
+
const url = new URL(req.url);
|
|
453
|
+
if (url.pathname.startsWith("/api"))
|
|
454
|
+
return proxyRequest(req);
|
|
455
|
+
const filePath = `./dist${url.pathname}`;
|
|
456
|
+
const file = Bun.file(filePath);
|
|
457
|
+
if (await file.exists()) {
|
|
458
|
+
return new Response(file, {
|
|
459
|
+
headers: { "Content-Type": getMimeType(filePath) },
|
|
460
|
+
});
|
|
461
|
+
}
|
|
462
|
+
const index = Bun.file("./dist/index.html");
|
|
463
|
+
if (await index.exists()) {
|
|
464
|
+
return new Response(index, {
|
|
465
|
+
headers: { "Content-Type": "text/html" },
|
|
466
|
+
});
|
|
467
|
+
}
|
|
468
|
+
return new Response("Not Found", { status: 404 });
|
|
469
|
+
},
|
|
470
|
+
});
|
|
471
|
+
log.success(`Server running on http://localhost:${port}`);
|
|
472
|
+
log.dim(` • Proxying /api → ${upstreamUrl}`);
|
|
473
|
+
log.dim(` • Serving static → ./dist`);
|
|
474
|
+
}
|
|
324
475
|
function init(c = "vite") {
|
|
325
476
|
log.info("Initializing Aura project...");
|
|
326
477
|
const filesContent = {
|
|
@@ -732,9 +883,9 @@ async function main() {
|
|
|
732
883
|
await login();
|
|
733
884
|
break;
|
|
734
885
|
case "dev":
|
|
735
|
-
return await dev
|
|
886
|
+
return await serve("dev");
|
|
736
887
|
case "start":
|
|
737
|
-
return await
|
|
888
|
+
return await serve("prod");
|
|
738
889
|
case undefined:
|
|
739
890
|
log.info("Available commands: init, pull, push, build, login, ping");
|
|
740
891
|
break;
|