@stacksjs/server 0.75.707 → 0.75.709

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.
@@ -30,8 +30,9 @@ export declare function loadRouteRedirects(file?: string): Promise<RedirectConfi
30
30
  /*': 'https://dash.example.com'` lands on
31
31
  * `https://dash.example.com/events/42`. No regular expressions, no captures,
32
32
  * no ordering to reason about.
33
+ * External fragment navigations use the stx browser handoff header.
33
34
  */
34
- export declare function resolveRedirect(url: URL, rules?: RedirectRules): Response | undefined;
35
+ export declare function resolveRedirect(url: URL, rules?: RedirectRules, requestHeaders?: Headers): Response | undefined;
35
36
  /**
36
37
  * A one-line summary for the dev server's boot output.
37
38
  *
package/dist/redirects.js CHANGED
@@ -1 +1 @@
1
- import{existsSync}from"node:fs";import{path as p}from"@stacksjs/path";const PROTECTED_PREFIX="/api/";export const DEFAULT_REDIRECT_STATUS=301;function normalizePath(value){const path=String(value).trim();if(!path.startsWith("/"))return"";return path.length>1&&path.endsWith("/")?path.replace(/\/+$/,""):path}export function resolveRedirectRules(input={}){const rules=new Map;for(const[rawFrom,rawTarget]of Object.entries(input??{})){const subtree=rawFrom.endsWith("/*");const from=normalizePath(subtree?rawFrom.slice(0,-2)||"/":rawFrom);if(!from)continue;if(from===PROTECTED_PREFIX.slice(0,-1)||from.startsWith(PROTECTED_PREFIX))continue;const target=typeof rawTarget==="string"?{to:rawTarget}:rawTarget;const to=String(target?.to??"").trim();if(!to)continue;if(!/^https?:\/\//i.test(to)&&normalizePath(to)===from)continue;rules.set(subtree?`${from}/*`:from,{from,to,status:Number(target?.status)||DEFAULT_REDIRECT_STATUS,preserveQuery:target?.preserveQuery!==false,subtree})}return rules}export async function loadRouteRedirects(file=p.storagePath("framework/auto-imports/routes.ts")){if(!existsSync(file))return{};try{const generated=await import(file);const redirects={};for(const[from,target]of Object.entries(generated.routeRedirects??{})){if(from.includes("{")||!target?.to)continue;redirects[from]={to:target.to,status:target.status}}return redirects}catch{return{}}}export function resolveRedirect(url,rules){if(!rules?.size)return;const pathname=normalizePath(url.pathname);const rule=rules.get(pathname)??matchSubtree(pathname,rules);if(!rule)return;let location=rule.to;if(rule.subtree){const remainder=pathname.slice(rule.from.length);if(remainder)location=location.replace(/\/$/,"")+remainder}if(rule.preserveQuery&&url.search){location+=location.includes("?")?`&${url.search.slice(1)}`:url.search}return new Response(null,{status:rule.status,headers:{Location:location,"Cache-Control":rule.status===301||rule.status===308?"public, max-age=3600":"no-store"}})}function matchSubtree(pathname,rules){let best;for(const rule of rules.values()){if(!rule.subtree)continue;if(pathname!==rule.from&&!pathname.startsWith(`${rule.from}/`))continue;if(!best||rule.from.length>best.from.length)best=rule}return best}export function describeRedirectRules(rules){if(!rules.size)return"none";const shown=[...rules.values()].slice(0,3).map((rule)=>`${rule.from} → ${rule.to}`);const rest=rules.size-shown.length;return rest>0?`${shown.join(", ")} (+${rest} more)`:shown.join(", ")}
1
+ import{existsSync}from"node:fs";import{path as p}from"@stacksjs/path";const PROTECTED_PREFIX="/api/";export const DEFAULT_REDIRECT_STATUS=301;function normalizePath(value){const path=String(value).trim();if(!path.startsWith("/"))return"";return path.length>1&&path.endsWith("/")?path.replace(/\/+$/,""):path}export function resolveRedirectRules(input={}){const rules=new Map;for(const[rawFrom,rawTarget]of Object.entries(input??{})){const subtree=rawFrom.endsWith("/*");const from=normalizePath(subtree?rawFrom.slice(0,-2)||"/":rawFrom);if(!from)continue;if(from===PROTECTED_PREFIX.slice(0,-1)||from.startsWith(PROTECTED_PREFIX))continue;const target=typeof rawTarget==="string"?{to:rawTarget}:rawTarget;const to=String(target?.to??"").trim();if(!to)continue;if(!/^https?:\/\//i.test(to)&&normalizePath(to)===from)continue;rules.set(subtree?`${from}/*`:from,{from,to,status:Number(target?.status)||DEFAULT_REDIRECT_STATUS,preserveQuery:target?.preserveQuery!==false,subtree})}return rules}export async function loadRouteRedirects(file=p.storagePath("framework/auto-imports/routes.ts")){if(!existsSync(file))return{};try{const generated=await import(file);const redirects={};for(const[from,target]of Object.entries(generated.routeRedirects??{})){if(from.includes("{")||!target?.to)continue;redirects[from]={to:target.to,status:target.status}}return redirects}catch{return{}}}export function resolveRedirect(url,rules,requestHeaders){if(!rules?.size)return;const pathname=normalizePath(url.pathname);const rule=rules.get(pathname)??matchSubtree(pathname,rules);if(!rule)return;let location=rule.to;if(rule.subtree){const remainder=pathname.slice(rule.from.length);if(remainder)location=location.replace(/\/$/,"")+remainder}if(rule.preserveQuery&&url.search){location+=location.includes("?")?`&${url.search.slice(1)}`:url.search}const target=URL.parse(location,url);const external=target&&(target.protocol==="http:"||target.protocol==="https:")&&target.origin!==url.origin;if(external&&requestHeaders?.get("X-STX-Router")==="true"){return new Response(null,{headers:{"X-STX-Location":location,"Cache-Control":"no-store",Vary:"X-STX-Router"}})}return new Response(null,{status:rule.status,headers:{Location:location,...external&&{Vary:"X-STX-Router"},"Cache-Control":rule.status===301||rule.status===308?"public, max-age=3600":"no-store"}})}function matchSubtree(pathname,rules){let best;for(const rule of rules.values()){if(!rule.subtree)continue;if(pathname!==rule.from&&!pathname.startsWith(`${rule.from}/`))continue;if(!best||rule.from.length>best.from.length)best=rule}return best}export function describeRedirectRules(rules){if(!rules.size)return"none";const shown=[...rules.values()].slice(0,3).map((rule)=>`${rule.from} → ${rule.to}`);const rest=rules.size-shown.length;return rest>0?`${shown.join(", ")} (+${rest} more)`:shown.join(", ")}
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@stacksjs/server",
3
3
  "type": "module",
4
4
  "sideEffects": false,
5
- "version": "0.75.707",
5
+ "version": "0.75.709",
6
6
  "description": "Local development and production-ready.",
7
7
  "author": "Chris Breuer",
8
8
  "contributors": [
@@ -58,22 +58,22 @@
58
58
  "prepublishOnly": "bun run build"
59
59
  },
60
60
  "dependencies": {
61
- "@stacksjs/auth": "0.75.707",
61
+ "@stacksjs/auth": "0.75.709",
62
62
  "@stacksjs/bun-router": "^0.1.24",
63
- "@stacksjs/config": "0.75.707",
64
- "@stacksjs/events": "0.75.707",
65
- "@stacksjs/i18n": "0.75.707",
66
- "@stacksjs/logging": "0.75.707",
67
- "@stacksjs/orm": "0.75.707",
68
- "@stacksjs/path": "0.75.707",
69
- "@stacksjs/payments": "0.75.707",
70
- "@stacksjs/router": "0.75.707",
71
- "@stacksjs/storage": "0.75.707",
72
- "@stacksjs/stx": "^0.2.957",
63
+ "@stacksjs/config": "0.75.709",
64
+ "@stacksjs/events": "0.75.709",
65
+ "@stacksjs/i18n": "0.75.709",
66
+ "@stacksjs/logging": "0.75.709",
67
+ "@stacksjs/orm": "0.75.709",
68
+ "@stacksjs/path": "0.75.709",
69
+ "@stacksjs/payments": "0.75.709",
70
+ "@stacksjs/router": "0.75.709",
71
+ "@stacksjs/storage": "0.75.709",
72
+ "@stacksjs/stx": "^0.2.960",
73
73
  "bun-plugin-auto-imports": "^0.4.4"
74
74
  },
75
75
  "devDependencies": {
76
- "@stacksjs/validation": "0.75.707",
76
+ "@stacksjs/validation": "0.75.709",
77
77
  "better-dx": "^0.2.39"
78
78
  }
79
79
  }