@stacksjs/router 0.75.544 → 0.75.551

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.
@@ -143,9 +143,11 @@ export declare const DEFAULT_ROUTE_BUNDLES: readonly ['auth', 'dashboard', 'deli
143
143
  * configured a provider (stacksjs/stacks#2276). Naming one in
144
144
  * `STACKS_DEFAULT_ROUTES` mounts it like any other; bootstrap additionally
145
145
  * mounts `social` on its own when a provider is actually configured, so the
146
- * env var is only needed to force it on or off.
146
+ * env var is only needed to force it on or off. `licensing` is the same
147
+ * shape: its routes answer nothing useful without a license signing key, so
148
+ * bootstrap mounts it when `LICENSE_SIGNING_KEY` is set.
147
149
  */
148
- export declare const OPT_IN_ROUTE_BUNDLES: readonly ['social'];
150
+ export declare const OPT_IN_ROUTE_BUNDLES: readonly ['social', 'licensing'];
149
151
  /** Where {@link loadFrameworkRoutes} leaves the selection for bootstrap.ts. */
150
152
  export declare const DEFAULT_ROUTE_BUNDLES_KEY: '__stacksDefaultRouteBundles';
151
153
  /**
@@ -1 +1 @@
1
- import{existsSync}from"node:fs";import{log}from"@stacksjs/logging/runtime";import{route,withFallbackRouteNames}from"./stacks-router";let rootMountedAppRoutes;export function listRootMountedAppRoutes(){return rootMountedAppRoutes??=[]}export const DEFAULT_ROUTE_REGISTRY={api:"api"};export async function appRouteRegistry(){const{projectPath}=await import("@stacksjs/path");const file=projectPath("app/Routes.ts");if(!existsSync(file))return DEFAULT_ROUTE_REGISTRY;const registry=(await import(file)).default;if(!registry||typeof registry!=="object"||Object.keys(registry).length===0){log.debug("[route-loader] app/Routes.ts exports no registry; using the default");return DEFAULT_ROUTE_REGISTRY}return registry}export async function loadRoutes(registry){for(const[key,definition]of Object.entries(registry)){const config=normalizeDefinition(definition);const prefix=config.prefix!==undefined?config.prefix?config.prefix.startsWith("/")?config.prefix:`/${config.prefix}`:undefined:key==="web"?undefined:`/${key}`;const middleware=normalizeMiddleware(config.middleware);log.debug(`[route-loader] Loading: ${config.path} prefix=${prefix||"/"} middleware=[${middleware.join(", ")}]`);try{if(prefix||middleware.length>0){await route.group({prefix,middleware:middleware.length>0?middleware:undefined},async()=>{await importRouteFile(config.path)})}else{const before=registeredRouteKeys();await importRouteFile(config.path);for(const entry of currentRouteTable()){const method=String(entry.method??"").toUpperCase();const path=String(entry.path??"");if(!method||!path||before.has(`${method} ${path}`))continue;const routes=rootMountedAppRoutes??=[];routes.push({method,path,file:`routes/${config.path}.ts`})}}}catch(error){const message=error instanceof Error?error.message:String(error);console.error(`[Routes] Failed to load route file '${config.path}': ${message}`);throw new Error(`Route loading failed for '${config.path}': ${message}`)}}await loadFrameworkRoutes()}export const DEFAULT_ROUTE_BUNDLES=["auth","dashboard","delivery","email","forms","payments","storefront"];export const OPT_IN_ROUTE_BUNDLES=["social"];export const DEFAULT_ROUTE_BUNDLES_KEY="__stacksDefaultRouteBundles";export function resolveDefaultRouteBundles(env=process.env){return resolveDefaultRouteBundlesWithDiagnostics(env).bundles}export function resolveDefaultRouteBundlesWithDiagnostics(env=process.env){const all=()=>new Set(DEFAULT_ROUTE_BUNDLES);const raw=env.STACKS_DEFAULT_ROUTES;if(raw!==undefined){const names=raw.split(",").map((name)=>name.trim().toLowerCase()).filter(Boolean);if(names.length===0||names.includes("none"))return{bundles:new Set,unknown:[],explicit:true};if(names.includes("all"))return{bundles:all(),unknown:[],explicit:true};const bundles=new Set;const unknown=[];for(const name of names){if(DEFAULT_ROUTE_BUNDLES.includes(name)||OPT_IN_ROUTE_BUNDLES.includes(name)){bundles.add(name)}else{unknown.push(name)}}return{bundles,unknown,explicit:true}}if(env.STACKS_SKIP_DEFAULT_ROUTES==="1")return{bundles:new Set,unknown:[],explicit:true};return{bundles:all(),unknown:[],explicit:false}}export const DEFAULT_ROUTE_BUNDLE_FEATURES={auth:"dashboard",dashboard:"dashboard",delivery:"commerce",email:"email",forms:"forms",payments:"commerce",storefront:"commerce"};export function bundleMounts(selection,bundle,featureEnabled){if(selection&&!selection.bundles.has(bundle))return false;return selection?.explicit?true:featureEnabled}export function mountedDefaultRouteBundles(featureEnabled,env=process.env){const selection=resolveDefaultRouteBundlesWithDiagnostics(env);const mounted=new Set;for(const bundle of DEFAULT_ROUTE_BUNDLES){if(bundleMounts(selection,bundle,featureEnabled(DEFAULT_ROUTE_BUNDLE_FEATURES[bundle])))mounted.add(bundle)}return mounted}async function loadFrameworkRoutes(){const{bundles,unknown,explicit}=resolveDefaultRouteBundlesWithDiagnostics();for(const name of unknown)log.warn(`[Routes] STACKS_DEFAULT_ROUTES lists unknown bundle "${name}" - known bundles are ${DEFAULT_ROUTE_BUNDLES.join(", ")}.`);if(bundles.size===0)return;globalThis[DEFAULT_ROUTE_BUNDLES_KEY]={bundles,explicit};try{try{const{overridesReady}=await import("@stacksjs/config");await overridesReady}catch{}const{frameworkPath}=await import("@stacksjs/path");const bootstrapPath=frameworkPath("defaults/bootstrap.ts");if(await Bun.file(bootstrapPath).exists()){await withFallbackRouteNames(()=>import(bootstrapPath))}}catch(error){const message=error instanceof Error?error.message:String(error);if(!message.includes("Cannot find module")&&!message.includes("MODULE_NOT_FOUND")){console.error(`[Routes] Failed to load framework bootstrap: ${message}`)}}}function assertSafeRouteName(routeName){if(typeof routeName!=="string"||routeName.length===0)throw new Error(`[route-loader] Invalid route path: empty or non-string`);if(routeName.includes("\x00"))throw new Error(`[route-loader] Invalid route path: null byte`);let decoded;try{decoded=decodeURIComponent(routeName)}catch{throw new Error(`[route-loader] Invalid route path: malformed URL encoding`)}const cleanPath=decoded.replace(/\.ts$/,"");if(cleanPath.startsWith("/")||/^[A-Za-z]:[\\/]/.test(cleanPath))throw new Error(`[route-loader] Invalid route path: absolute paths not allowed (${cleanPath})`);const segments=cleanPath.split(/[/\\]/);if(segments.some((s)=>s===".."))throw new Error(`[route-loader] Invalid route path: '..' segment not allowed (${cleanPath})`);return cleanPath}async function importRouteFile(routeName){const cleanPath=assertSafeRouteName(routeName);const{isCompiledBinary,projectPath}=await import("@stacksjs/path");if(isCompiledBinary()){throw new Error(`[route-loader] Cannot load routes/${cleanPath} from a compiled binary: route files are read from disk at runtime, and a disk file cannot resolve the framework packages it imports from inside the bundle. Import the route file statically (\`import '../routes/${cleanPath}'\`) so it is bundled, or run the app with \`bun\` instead of compiling it.`)}await import(projectPath(`routes/${cleanPath}`))}function normalizeDefinition(def){if(typeof def==="string"){return{path:def}}return def}function normalizeMiddleware(middleware){if(!middleware)return[];if(typeof middleware==="string")return[middleware];return middleware}function currentRouteTable(){return route.routes??[]}function registeredRouteKeys(){const keys=new Set;for(const entry of currentRouteTable())keys.add(`${String(entry.method??"").toUpperCase()} ${String(entry.path??"")}`);return keys}
1
+ import{existsSync}from"node:fs";import{log}from"@stacksjs/logging/runtime";import{route,withFallbackRouteNames}from"./stacks-router";let rootMountedAppRoutes;export function listRootMountedAppRoutes(){return rootMountedAppRoutes??=[]}export const DEFAULT_ROUTE_REGISTRY={api:"api"};export async function appRouteRegistry(){const{projectPath}=await import("@stacksjs/path");const file=projectPath("app/Routes.ts");if(!existsSync(file))return DEFAULT_ROUTE_REGISTRY;const registry=(await import(file)).default;if(!registry||typeof registry!=="object"||Object.keys(registry).length===0){log.debug("[route-loader] app/Routes.ts exports no registry; using the default");return DEFAULT_ROUTE_REGISTRY}return registry}export async function loadRoutes(registry){for(const[key,definition]of Object.entries(registry)){const config=normalizeDefinition(definition);const prefix=config.prefix!==undefined?config.prefix?config.prefix.startsWith("/")?config.prefix:`/${config.prefix}`:undefined:key==="web"?undefined:`/${key}`;const middleware=normalizeMiddleware(config.middleware);log.debug(`[route-loader] Loading: ${config.path} prefix=${prefix||"/"} middleware=[${middleware.join(", ")}]`);try{if(prefix||middleware.length>0){await route.group({prefix,middleware:middleware.length>0?middleware:undefined},async()=>{await importRouteFile(config.path)})}else{const before=registeredRouteKeys();await importRouteFile(config.path);for(const entry of currentRouteTable()){const method=String(entry.method??"").toUpperCase();const path=String(entry.path??"");if(!method||!path||before.has(`${method} ${path}`))continue;const routes=rootMountedAppRoutes??=[];routes.push({method,path,file:`routes/${config.path}.ts`})}}}catch(error){const message=error instanceof Error?error.message:String(error);console.error(`[Routes] Failed to load route file '${config.path}': ${message}`);throw new Error(`Route loading failed for '${config.path}': ${message}`)}}await loadFrameworkRoutes()}export const DEFAULT_ROUTE_BUNDLES=["auth","dashboard","delivery","email","forms","payments","storefront"];export const OPT_IN_ROUTE_BUNDLES=["social","licensing"];export const DEFAULT_ROUTE_BUNDLES_KEY="__stacksDefaultRouteBundles";export function resolveDefaultRouteBundles(env=process.env){return resolveDefaultRouteBundlesWithDiagnostics(env).bundles}export function resolveDefaultRouteBundlesWithDiagnostics(env=process.env){const all=()=>new Set(DEFAULT_ROUTE_BUNDLES);const raw=env.STACKS_DEFAULT_ROUTES;if(raw!==undefined){const names=raw.split(",").map((name)=>name.trim().toLowerCase()).filter(Boolean);if(names.length===0||names.includes("none"))return{bundles:new Set,unknown:[],explicit:true};if(names.includes("all"))return{bundles:all(),unknown:[],explicit:true};const bundles=new Set;const unknown=[];for(const name of names){if(DEFAULT_ROUTE_BUNDLES.includes(name)||OPT_IN_ROUTE_BUNDLES.includes(name)){bundles.add(name)}else{unknown.push(name)}}return{bundles,unknown,explicit:true}}if(env.STACKS_SKIP_DEFAULT_ROUTES==="1")return{bundles:new Set,unknown:[],explicit:true};return{bundles:all(),unknown:[],explicit:false}}export const DEFAULT_ROUTE_BUNDLE_FEATURES={auth:"dashboard",dashboard:"dashboard",delivery:"commerce",email:"email",forms:"forms",payments:"commerce",storefront:"commerce"};export function bundleMounts(selection,bundle,featureEnabled){if(selection&&!selection.bundles.has(bundle))return false;return selection?.explicit?true:featureEnabled}export function mountedDefaultRouteBundles(featureEnabled,env=process.env){const selection=resolveDefaultRouteBundlesWithDiagnostics(env);const mounted=new Set;for(const bundle of DEFAULT_ROUTE_BUNDLES){if(bundleMounts(selection,bundle,featureEnabled(DEFAULT_ROUTE_BUNDLE_FEATURES[bundle])))mounted.add(bundle)}return mounted}async function loadFrameworkRoutes(){const{bundles,unknown,explicit}=resolveDefaultRouteBundlesWithDiagnostics();for(const name of unknown)log.warn(`[Routes] STACKS_DEFAULT_ROUTES lists unknown bundle "${name}" - known bundles are ${DEFAULT_ROUTE_BUNDLES.join(", ")}.`);if(bundles.size===0)return;globalThis[DEFAULT_ROUTE_BUNDLES_KEY]={bundles,explicit};try{try{const{overridesReady}=await import("@stacksjs/config");await overridesReady}catch{}const{frameworkPath}=await import("@stacksjs/path");const bootstrapPath=frameworkPath("defaults/bootstrap.ts");if(await Bun.file(bootstrapPath).exists()){await withFallbackRouteNames(()=>import(bootstrapPath))}}catch(error){const message=error instanceof Error?error.message:String(error);if(!message.includes("Cannot find module")&&!message.includes("MODULE_NOT_FOUND")){console.error(`[Routes] Failed to load framework bootstrap: ${message}`)}}}function assertSafeRouteName(routeName){if(typeof routeName!=="string"||routeName.length===0)throw new Error(`[route-loader] Invalid route path: empty or non-string`);if(routeName.includes("\x00"))throw new Error(`[route-loader] Invalid route path: null byte`);let decoded;try{decoded=decodeURIComponent(routeName)}catch{throw new Error(`[route-loader] Invalid route path: malformed URL encoding`)}const cleanPath=decoded.replace(/\.ts$/,"");if(cleanPath.startsWith("/")||/^[A-Za-z]:[\\/]/.test(cleanPath))throw new Error(`[route-loader] Invalid route path: absolute paths not allowed (${cleanPath})`);const segments=cleanPath.split(/[/\\]/);if(segments.some((s)=>s===".."))throw new Error(`[route-loader] Invalid route path: '..' segment not allowed (${cleanPath})`);return cleanPath}async function importRouteFile(routeName){const cleanPath=assertSafeRouteName(routeName);const{isCompiledBinary,projectPath}=await import("@stacksjs/path");if(isCompiledBinary()){throw new Error(`[route-loader] Cannot load routes/${cleanPath} from a compiled binary: route files are read from disk at runtime, and a disk file cannot resolve the framework packages it imports from inside the bundle. Import the route file statically (\`import '../routes/${cleanPath}'\`) so it is bundled, or run the app with \`bun\` instead of compiling it.`)}await import(projectPath(`routes/${cleanPath}`))}function normalizeDefinition(def){if(typeof def==="string"){return{path:def}}return def}function normalizeMiddleware(middleware){if(!middleware)return[];if(typeof middleware==="string")return[middleware];return middleware}function currentRouteTable(){return route.routes??[]}function registeredRouteKeys(){const keys=new Set;for(const entry of currentRouteTable())keys.add(`${String(entry.method??"").toUpperCase()} ${String(entry.path??"")}`);return keys}
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@stacksjs/router",
3
3
  "type": "module",
4
4
  "sideEffects": false,
5
- "version": "0.75.544",
5
+ "version": "0.75.551",
6
6
  "description": "The Stacks framework router.",
7
7
  "author": "Chris Breuer",
8
8
  "contributors": [
@@ -68,27 +68,27 @@
68
68
  },
69
69
  "dependencies": {
70
70
  "@stacksjs/bun-router": "^0.1.24",
71
- "@stacksjs/cache": "0.75.544",
72
- "@stacksjs/collections": "0.75.544",
73
- "@stacksjs/config": "0.75.544",
74
- "@stacksjs/error-handling": "0.75.544",
75
- "@stacksjs/logging": "0.75.544",
76
- "@stacksjs/pagination": "0.75.544",
77
- "@stacksjs/path": "0.75.544",
78
- "@stacksjs/security": "0.75.544",
79
- "@stacksjs/storage": "0.75.544",
71
+ "@stacksjs/cache": "0.75.551",
72
+ "@stacksjs/collections": "0.75.551",
73
+ "@stacksjs/config": "0.75.551",
74
+ "@stacksjs/error-handling": "0.75.551",
75
+ "@stacksjs/logging": "0.75.551",
76
+ "@stacksjs/pagination": "0.75.551",
77
+ "@stacksjs/path": "0.75.551",
78
+ "@stacksjs/security": "0.75.551",
79
+ "@stacksjs/storage": "0.75.551",
80
80
  "ts-rate-limiter": "^0.4.10"
81
81
  },
82
82
  "devDependencies": {
83
- "@stacksjs/actions": "0.75.544",
84
- "@stacksjs/types": "0.75.544",
83
+ "@stacksjs/actions": "0.75.551",
84
+ "@stacksjs/types": "0.75.551",
85
85
  "better-dx": "^0.2.24"
86
86
  },
87
87
  "peerDependencies": {
88
- "@stacksjs/api": "0.75.544",
89
- "@stacksjs/auth": "0.75.544",
90
- "@stacksjs/database": "0.75.544",
91
- "@stacksjs/validation": "0.75.544"
88
+ "@stacksjs/api": "0.75.551",
89
+ "@stacksjs/auth": "0.75.551",
90
+ "@stacksjs/database": "0.75.551",
91
+ "@stacksjs/validation": "0.75.551"
92
92
  },
93
93
  "peerDependenciesMeta": {
94
94
  "@stacksjs/api": {