@stacksjs/router 0.74.37 → 0.74.39

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.
@@ -1 +1 @@
1
- import process from"node:process";import{AsyncLocalStorage}from"node:async_hooks";import{log}from"@stacksjs/logging";const REQUEST_STORAGE_KEY=Symbol.for("stacks.router.requestStorage"),requestStorage=globalThis[REQUEST_STORAGE_KEY]??=new AsyncLocalStorage,TRACE_STORAGE_KEY=Symbol.for("stacks.router.traceStorage"),traceStorage=globalThis[TRACE_STORAGE_KEY]??=new AsyncLocalStorage;export function getTraceId(){const explicit=traceStorage.getStore();if(explicit)return explicit;return requestStorage.getStore()?._requestId}export function withTraceId(id,fn){return traceStorage.run(id,fn)}const REQUEST_QUERY_CACHE_KEY=Symbol.for("stacks.requestQueryCache");function getRequestCache(){const req=requestStorage.getStore();if(!req)return;let cache=req[REQUEST_QUERY_CACHE_KEY];if(!cache){cache={map:new Map};req[REQUEST_QUERY_CACHE_KEY]=cache}return cache}export function cacheRequestQuery(key,fetcher){try{const cache=getRequestCache(),existing=cache?.map.get(key);if(existing)return existing.then();return startQuery(cache,key,fetcher)}catch(error){return Promise.reject(error)}}async function startQuery(cache,key,fetcher){if(!cache)return fetcher();const promise=Promise.resolve().then(()=>fetcher());cache.map.set(key,promise);promise.catch(()=>cache.map.delete(key));return promise}export function setCurrentRequest(req){log.debug(`[request] ${req.method} ${new URL(req.url).pathname}`);requestStorage.enterWith(req)}export function clearCurrentRequest(){requestStorage.enterWith(void 0)}export function runWithRequest(req,fn){return requestStorage.run(req,fn)}export function runWithRequestArgument(req,fn,arg){return requestStorage.run(req,fn,arg)}export function runWithRequestArguments(req,fn,first,second,third){return requestStorage.run(req,fn,first,second,third)}export function getCurrentRequest(){return requestStorage.getStore()}export const request=new Proxy({},{get(_target,prop){const currentRequest=getCurrentRequest();if(!currentRequest){if(process.env.NODE_ENV!=="production")console.warn(`[RequestContext] Accessing request.${String(prop)} outside of request context`);if(prop==="bearerToken")return()=>null;if(prop==="user"||prop==="userToken")return async()=>{return};if(prop==="tokenCan"||prop==="tokenCant")return async()=>!1;if(prop==="headers")return new Headers;if(prop==="url")return"";if(prop==="method")return"GET";return}const value=currentRequest[prop];if(typeof value==="function")return value.bind(currentRequest);return value}});
1
+ import process from"node:process";import{AsyncLocalStorage}from"node:async_hooks";import{log}from"@stacksjs/logging";const REQUEST_STORAGE_KEY=Symbol.for("stacks.router.requestStorage"),requestStorage=globalThis[REQUEST_STORAGE_KEY]??=new AsyncLocalStorage,TRACE_STORAGE_KEY=Symbol.for("stacks.router.traceStorage"),traceStorage=globalThis[TRACE_STORAGE_KEY]??=new AsyncLocalStorage;export function getTraceId(){const explicit=traceStorage.getStore();if(explicit)return explicit;return requestStorage.getStore()?._requestId}export function withTraceId(id,fn){return traceStorage.run(id,fn)}const REQUEST_QUERY_CACHE_KEY=Symbol.for("stacks.requestQueryCache");function getRequestCache(){const req=requestStorage.getStore();if(!req)return;let cache=req[REQUEST_QUERY_CACHE_KEY];if(!cache){cache={map:new Map};req[REQUEST_QUERY_CACHE_KEY]=cache}return cache}export function cacheRequestQuery(key,fetcher){try{const cache=getRequestCache(),existing=cache?.map.get(key);if(existing)return existing.then();return startQuery(cache,key,fetcher)}catch(error){return Promise.reject(error)}}function startQuery(cache,key,fetcher){if(!cache)return uncachedQuery(fetcher);const promise=Promise.resolve().then(()=>fetcher());cache.map.set(key,promise);promise.catch(()=>cache.map.delete(key));return promise.then()}async function uncachedQuery(fetcher){return fetcher()}export function setCurrentRequest(req){log.debug(`[request] ${req.method} ${new URL(req.url).pathname}`);requestStorage.enterWith(req)}export function clearCurrentRequest(){requestStorage.enterWith(void 0)}export function runWithRequest(req,fn){return requestStorage.run(req,fn)}export function runWithRequestArgument(req,fn,arg){return requestStorage.run(req,fn,arg)}export function runWithRequestArguments(req,fn,first,second,third){return requestStorage.run(req,fn,first,second,third)}export function getCurrentRequest(){return requestStorage.getStore()}export const request=new Proxy({},{get(_target,prop){const currentRequest=getCurrentRequest();if(!currentRequest){if(process.env.NODE_ENV!=="production")console.warn(`[RequestContext] Accessing request.${String(prop)} outside of request context`);if(prop==="bearerToken")return()=>null;if(prop==="user"||prop==="userToken")return async()=>{return};if(prop==="tokenCan"||prop==="tokenCant")return async()=>!1;if(prop==="headers")return new Headers;if(prop==="url")return"";if(prop==="method")return"GET";return}const value=currentRequest[prop];if(typeof value==="function")return value.bind(currentRequest);return value}});
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@stacksjs/router",
3
3
  "type": "module",
4
4
  "sideEffects": false,
5
- "version": "0.74.37",
5
+ "version": "0.74.39",
6
6
  "description": "The Stacks framework router.",
7
7
  "author": "Chris Breuer",
8
8
  "contributors": [
@@ -61,27 +61,27 @@
61
61
  },
62
62
  "dependencies": {
63
63
  "@stacksjs/bun-router": "^0.1.15",
64
- "@stacksjs/cache": "0.74.37",
65
- "@stacksjs/collections": "0.74.37",
66
- "@stacksjs/config": "0.74.37",
67
- "@stacksjs/error-handling": "0.74.37",
68
- "@stacksjs/logging": "0.74.37",
69
- "@stacksjs/pagination": "0.74.37",
70
- "@stacksjs/path": "0.74.37",
71
- "@stacksjs/security": "0.74.37",
72
- "@stacksjs/storage": "0.74.37",
64
+ "@stacksjs/cache": "0.74.39",
65
+ "@stacksjs/collections": "0.74.39",
66
+ "@stacksjs/config": "0.74.39",
67
+ "@stacksjs/error-handling": "0.74.39",
68
+ "@stacksjs/logging": "0.74.39",
69
+ "@stacksjs/pagination": "0.74.39",
70
+ "@stacksjs/path": "0.74.39",
71
+ "@stacksjs/security": "0.74.39",
72
+ "@stacksjs/storage": "0.74.39",
73
73
  "ts-rate-limiter": "^0.4.10"
74
74
  },
75
75
  "devDependencies": {
76
- "@stacksjs/actions": "0.74.37",
77
- "@stacksjs/types": "0.74.37",
76
+ "@stacksjs/actions": "0.74.39",
77
+ "@stacksjs/types": "0.74.39",
78
78
  "better-dx": "^0.2.24"
79
79
  },
80
80
  "peerDependencies": {
81
- "@stacksjs/api": "0.74.37",
82
- "@stacksjs/auth": "0.74.37",
83
- "@stacksjs/database": "0.74.37",
84
- "@stacksjs/validation": "0.74.37"
81
+ "@stacksjs/api": "0.74.39",
82
+ "@stacksjs/auth": "0.74.39",
83
+ "@stacksjs/database": "0.74.39",
84
+ "@stacksjs/validation": "0.74.39"
85
85
  },
86
86
  "peerDependenciesMeta": {
87
87
  "@stacksjs/api": {