bun-plugin-stx 0.2.73 → 0.2.75

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.
Files changed (2) hide show
  1. package/dist/serve.js +53 -7
  2. package/package.json +1 -1
package/dist/serve.js CHANGED
@@ -9018,6 +9018,10 @@ After installing: bun add bun-plugin-stx`);
9018
9018
  const extraProtectedPrefixes = authConfig?.protectedPaths ?? [];
9019
9019
  let activeServeLocale = null;
9020
9020
  let activeServeSearch = "";
9021
+ let activeServeHost = "";
9022
+ let activeServeCookies = {};
9023
+ let activeServeCookieHeader = "";
9024
+ let activeServeIp = "";
9021
9025
  function injectServeLocaleContext(context) {
9022
9026
  if (!activeServeLocale)
9023
9027
  return;
@@ -9029,17 +9033,24 @@ After installing: bun add bun-plugin-stx`);
9029
9033
  };
9030
9034
  }
9031
9035
  function injectServeRequestContext(context) {
9032
- if (!activeServeSearch)
9033
- return;
9034
- context.__stxServeSearch = activeServeSearch;
9035
- globalThis.__stxServeSearch = activeServeSearch;
9036
+ if (activeServeSearch) {
9037
+ context.__stxServeSearch = activeServeSearch;
9038
+ globalThis.__stxServeSearch = activeServeSearch;
9039
+ }
9040
+ if (activeServeHost)
9041
+ context.host = activeServeHost;
9042
+ context.cookies = activeServeCookies;
9043
+ if (activeServeIp)
9044
+ context.ip = activeServeIp;
9036
9045
  }
9037
9046
  function htmlCacheKey(filePath) {
9038
9047
  const loc = i18nConfig ? activeServeLocale ?? i18nConfig.defaultLocale : "";
9039
9048
  const search = activeServeSearch || "";
9040
- if (!loc && !search)
9049
+ const host = activeServeHost || "";
9050
+ const cookies = activeServeCookieHeader;
9051
+ if (!loc && !search && !host && !cookies)
9041
9052
  return filePath;
9042
- return `${filePath}\x00${loc}\x00${search}`;
9053
+ return `${filePath}\x00${loc}\x00${search}\x00${host}\x00${cookies}`;
9043
9054
  }
9044
9055
  const builtInMiddleware = authConfig === null ? {} : {
9045
9056
  auth: (_req, ctx) => {
@@ -9354,6 +9365,7 @@ After installing: bun add bun-plugin-stx`);
9354
9365
  app: stxConfig.app || {},
9355
9366
  ..."strict" in stxConfig && { strict: stxConfig.strict },
9356
9367
  ..."router" in stxConfig && { router: stxConfig.router },
9368
+ ..."debug" in stxConfig && { debug: stxConfig.debug },
9357
9369
  ..."skipDefaultSeoTags" in stxConfig && { skipDefaultSeoTags: stxConfig.skipDefaultSeoTags },
9358
9370
  ..."defaultTitle" in stxConfig && { defaultTitle: stxConfig.defaultTitle },
9359
9371
  ..."defaultDescription" in stxConfig && { defaultDescription: stxConfig.defaultDescription },
@@ -9521,6 +9533,7 @@ After installing: bun add bun-plugin-stx`);
9521
9533
  app: stxConfig.app || {},
9522
9534
  ..."strict" in stxConfig && { strict: stxConfig.strict },
9523
9535
  ..."router" in stxConfig && { router: stxConfig.router },
9536
+ ..."debug" in stxConfig && { debug: stxConfig.debug },
9524
9537
  ..."skipDefaultSeoTags" in stxConfig && { skipDefaultSeoTags: stxConfig.skipDefaultSeoTags },
9525
9538
  ..."defaultTitle" in stxConfig && { defaultTitle: stxConfig.defaultTitle },
9526
9539
  ..."defaultDescription" in stxConfig && { defaultDescription: stxConfig.defaultDescription },
@@ -9676,9 +9689,34 @@ ${html}`;
9676
9689
  _server = bunServe({
9677
9690
  port: actualPort,
9678
9691
  idleTimeout: 0,
9679
- async fetch(req) {
9692
+ async fetch(req, server) {
9680
9693
  const url = new URL(req.url);
9681
9694
  let path = url.pathname;
9695
+ const proxyRules = stxConfig.server?.proxy;
9696
+ if (proxyRules) {
9697
+ for (const [prefix, rule] of Object.entries(proxyRules)) {
9698
+ if (path !== prefix && !path.startsWith(prefix))
9699
+ continue;
9700
+ const ruleCfg = typeof rule === "string" ? { target: rule } : rule;
9701
+ const outPath = ruleCfg.rewrite ? ruleCfg.rewrite(path) : path;
9702
+ const target = new URL(ruleCfg.target);
9703
+ const outUrl = new URL(outPath + url.search, target);
9704
+ const headers = new Headers(req.headers);
9705
+ if (ruleCfg.changeOrigin !== false)
9706
+ headers.set("host", target.host);
9707
+ try {
9708
+ return await fetch(outUrl, {
9709
+ method: req.method,
9710
+ headers,
9711
+ body: req.body,
9712
+ redirect: "manual",
9713
+ ...req.body ? { duplex: "half" } : {}
9714
+ });
9715
+ } catch (err) {
9716
+ return new Response(`[stx dev proxy] ${prefix} \u2192 ${ruleCfg.target} failed: ${err instanceof Error ? err.message : String(err)}`, { status: 502 });
9717
+ }
9718
+ }
9719
+ }
9682
9720
  let i18nLocale = null;
9683
9721
  if (i18nConfig && !url.pathname.startsWith("/api/")) {
9684
9722
  const stripped = localeFromPath(url.pathname);
@@ -9699,6 +9737,10 @@ ${html}`;
9699
9737
  const _i18nResp = await (async () => {
9700
9738
  activeServeLocale = i18nLocale;
9701
9739
  activeServeSearch = url.search;
9740
+ activeServeHost = req.headers.get("host") || "";
9741
+ activeServeCookieHeader = req.headers.get("cookie") || "";
9742
+ activeServeCookies = parseCookies(req);
9743
+ activeServeIp = server.requestIP(req)?.address || (req.headers.get("x-forwarded-for") || "").split(",")[0].trim();
9702
9744
  try {
9703
9745
  if (req.method === "OPTIONS") {
9704
9746
  return new Response(null, { headers: corsHeaders });
@@ -10134,6 +10176,10 @@ ${pageSetupScripts.join(`
10134
10176
  } finally {
10135
10177
  activeServeLocale = null;
10136
10178
  activeServeSearch = "";
10179
+ activeServeHost = "";
10180
+ activeServeCookieHeader = "";
10181
+ activeServeCookies = {};
10182
+ activeServeIp = "";
10137
10183
  }
10138
10184
  })();
10139
10185
  return applyI18nToResponse(_i18nResp, i18nLocale ?? (i18nConfig?.defaultLocale ?? "en"), path);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "bun-plugin-stx",
3
3
  "type": "module",
4
- "version": "0.2.73",
4
+ "version": "0.2.75",
5
5
  "description": "A Bun plugin that allows for using Laravel Blade-like syntax.",
6
6
  "author": "Chris Breuer <chris@stacksjs.org>",
7
7
  "license": "MIT",