@inflector/aura 0.6.7 → 0.7.0

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 CHANGED
@@ -1,4 +1,5 @@
1
1
  #!/usr/bin/env bun
2
2
  export declare function build(silent?: boolean): void;
3
+ export declare function serve(mode: "dev" | "prod"): Promise<void>;
3
4
  export declare function dev(): Promise<void>;
4
5
  //# sourceMappingURL=bin.d.ts.map
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;AA0aD,wBAAsB,GAAG,kBAqExB"}
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,iBAiJ/C;AAmRD,wBAAsB,GAAG,kBAiFxB"}
package/dist/bin.js CHANGED
@@ -321,6 +321,150 @@ 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
+ configure(proxy) {
397
+ proxy.on("proxyReq", (proxyReq, req) => {
398
+ proxyReq.setHeader("connection", "close");
399
+ if (AURA_KEY)
400
+ proxyReq.setHeader("Authorization", `Bearer ${AURA_KEY}`);
401
+ // preserve cookies
402
+ if (req.headers.cookie)
403
+ proxyReq.setHeader("cookie", req.headers.cookie);
404
+ });
405
+ proxy.on("proxyRes", (proxyRes) => {
406
+ // ensure set-cookie is not stripped
407
+ if (proxyRes.headers["set-cookie"]) {
408
+ proxyRes.headers["set-cookie"] = proxyRes.headers["set-cookie"]
409
+ .map((c) => c.replace(/; secure/gi, "").replace(/; samesite=none/gi, ""));
410
+ }
411
+ });
412
+ },
413
+ },
414
+ },
415
+ },
416
+ }));
417
+ if (vite.bindCLIShortcuts)
418
+ vite.bindCLIShortcuts({ print: true });
419
+ await vite.listen();
420
+ vite.printUrls();
421
+ return;
422
+ }
423
+ // prod
424
+ const configFile = path.resolve(process.cwd(), "vite.config.ts");
425
+ const configResult = await loadConfigFromFile({ command: "serve", mode: "development" }, configFile);
426
+ const port = configResult?.config?.preview?.port ?? 80;
427
+ const getMimeType = (p) => {
428
+ if (p.endsWith(".js"))
429
+ return "application/javascript";
430
+ if (p.endsWith(".css"))
431
+ return "text/css";
432
+ if (p.endsWith(".html"))
433
+ return "text/html";
434
+ if (p.endsWith(".json"))
435
+ return "application/json";
436
+ if (p.endsWith(".png"))
437
+ return "image/png";
438
+ if (p.endsWith(".jpg") || p.endsWith(".jpeg"))
439
+ return "image/jpeg";
440
+ if (p.endsWith(".svg"))
441
+ return "image/svg+xml";
442
+ return "application/octet-stream";
443
+ };
444
+ Bun.serve({
445
+ hostname: "0.0.0.0",
446
+ port,
447
+ idleTimeout: 0,
448
+ async fetch(req) {
449
+ const url = new URL(req.url);
450
+ if (url.pathname.startsWith("/api"))
451
+ return proxyRequest(req);
452
+ const filePath = `./dist${url.pathname}`;
453
+ const file = Bun.file(filePath);
454
+ if (await file.exists()) {
455
+ return new Response(file, { headers: { "Content-Type": getMimeType(filePath) } });
456
+ }
457
+ const index = Bun.file("./dist/index.html");
458
+ if (await index.exists()) {
459
+ return new Response(index, { headers: { "Content-Type": "text/html" } });
460
+ }
461
+ return new Response("Not Found", { status: 404 });
462
+ },
463
+ });
464
+ log.success(`Server running on http://localhost:${port}`);
465
+ log.dim(` • Proxying /api → ${upstreamUrl}`);
466
+ log.dim(` • Serving static → ./dist`);
467
+ }
324
468
  function init(c = "vite") {
325
469
  log.info("Initializing Aura project...");
326
470
  const filesContent = {
@@ -582,11 +726,17 @@ export async function dev() {
582
726
  changeOrigin: true,
583
727
  secure: false,
584
728
  configure(proxy, _options) {
585
- proxy.on("proxyReq", (proxyReq) => {
729
+ proxy.on("proxyReq", (proxyReq, req, res) => {
586
730
  proxyReq.setHeader("Connection", "close");
587
731
  if (AURA_KEY) {
588
732
  proxyReq.setHeader("Authorization", `Bearer ${AURA_KEY}`);
589
733
  }
734
+ // Abort upstream request if client disconnects
735
+ req.on("close", () => {
736
+ if (!res.writableEnded) {
737
+ proxyReq.destroy();
738
+ }
739
+ });
590
740
  });
591
741
  },
592
742
  },
@@ -643,6 +793,7 @@ async function start() {
643
793
  body: req.method === "GET" || req.method === "HEAD"
644
794
  ? undefined
645
795
  : req.body,
796
+ signal: req.signal,
646
797
  });
647
798
  const proxyHeaders = new Headers(proxiedResp.headers);
648
799
  proxyHeaders.delete("content-encoding");
@@ -653,7 +804,11 @@ async function start() {
653
804
  headers: proxyHeaders,
654
805
  });
655
806
  }
656
- catch {
807
+ catch (err) {
808
+ // Client disconnected — no point sending a response
809
+ if (err.name === "AbortError" || req.signal.aborted) {
810
+ return new Response(null, { status: 499 });
811
+ }
657
812
  return new Response(JSON.stringify({ error: "Upstream Proxy Failed" }), { status: 502, headers: { "Content-Type": "application/json" } });
658
813
  }
659
814
  }
@@ -721,9 +876,9 @@ async function main() {
721
876
  await login();
722
877
  break;
723
878
  case "dev":
724
- return await dev();
879
+ return await serve("dev");
725
880
  case "start":
726
- return await start();
881
+ return await serve("prod");
727
882
  case undefined:
728
883
  log.info("Available commands: init, pull, push, build, login, ping");
729
884
  break;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inflector/aura",
3
- "version": "0.6.7",
3
+ "version": "0.7.0",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",