@stacksjs/config 0.75.572 → 0.75.575
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/request-context.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{AsyncLocalStorage}from"node:async_hooks";export function parseCookieHeader(header){const out={};if(!header)return out;for(const part of header.split(";")){const trimmed=part.trim();const eq=trimmed.indexOf("=");if(eq===-1)continue;const key=trimmed.slice(0,eq).trim();if(!key)continue;const value=trimmed.slice(eq+1).trim();try{out[key]=decodeURIComponent(value)}catch{out[key]=value}}return out}export function createRequestContext(read){const snapshot=()=>read()??{};const searchOf=()=>{const direct=snapshot().search;if(direct)return direct;const url=snapshot().url??"";const mark=url.indexOf("?");return mark===-1?"":url.slice(mark)};return{cookie:(name)=>snapshot().cookies?.[name]??null,cookies:()=>snapshot().cookies??{},url:()=>snapshot().url??"",path:()=>{const direct=snapshot().path;if(direct)return direct;const url=snapshot().url??"";if(!url)return"";try{return new URL(url).pathname}catch{const mark=url.indexOf("?");return mark===-1?url:url.slice(0,mark)}},search:searchOf,query:()=>{const out={};const search=searchOf();if(!search)return out;new URLSearchParams(search.startsWith("?")?search.slice(1):search).forEach((value,key)=>{out[key]=value});return out},params:()=>snapshot().params??{},locale:()=>snapshot().locale??"en",ip:()=>snapshot().ip??"",host:()=>snapshot().host??"",site:()=>snapshot().site??null}}export function installRequestContext(read){const context=createRequestContext(read);globalThis.requestContext=context;return context}const SCOPE_STORAGE_KEY=Symbol.for("stacks.config.requestScope");const scopeStorage=globalThis[SCOPE_STORAGE_KEY]??=new AsyncLocalStorage;export function enterRequestScope(request){const url=new URL(request.url);const own={cookies:parseCookieHeader(request.headers.get("cookie")),url:request.url,path:url.pathname,search:url.search,host:request.headers.get("host")??url.host,site:null};scopeStorage.enterWith({own});return own}export function scopedRequestSnapshot(){const scope=scopeStorage.getStore();if(!scope)return;const{own,published}=scope;if(!published)return own;
|
|
1
|
+
import{AsyncLocalStorage}from"node:async_hooks";export function parseCookieHeader(header){const out={};if(!header)return out;for(const part of header.split(";")){const trimmed=part.trim();const eq=trimmed.indexOf("=");if(eq===-1)continue;const key=trimmed.slice(0,eq).trim();if(!key)continue;const value=trimmed.slice(eq+1).trim();try{out[key]=decodeURIComponent(value)}catch{out[key]=value}}return out}export function createRequestContext(read){const snapshot=()=>read()??{};const searchOf=()=>{const direct=snapshot().search;if(direct)return direct;const url=snapshot().url??"";const mark=url.indexOf("?");return mark===-1?"":url.slice(mark)};return{cookie:(name)=>snapshot().cookies?.[name]??null,cookies:()=>snapshot().cookies??{},url:()=>snapshot().url??"",path:()=>{const direct=snapshot().path;if(direct)return direct;const url=snapshot().url??"";if(!url)return"";try{return new URL(url).pathname}catch{const mark=url.indexOf("?");return mark===-1?url:url.slice(0,mark)}},search:searchOf,query:()=>{const out={};const search=searchOf();if(!search)return out;new URLSearchParams(search.startsWith("?")?search.slice(1):search).forEach((value,key)=>{out[key]=value});return out},params:()=>snapshot().params??{},locale:()=>snapshot().locale??"en",ip:()=>snapshot().ip??"",host:()=>snapshot().host??"",site:()=>snapshot().site??null}}export function installRequestContext(read){const context=createRequestContext(read);globalThis.requestContext=context;return context}const SCOPE_STORAGE_KEY=Symbol.for("stacks.config.requestScope");const scopeStorage=globalThis[SCOPE_STORAGE_KEY]??=new AsyncLocalStorage;export function enterRequestScope(request){const url=new URL(request.url);const own={cookies:parseCookieHeader(request.headers.get("cookie")),url:request.url,path:url.pathname,search:url.search,host:request.headers.get("host")??url.host,site:null};scopeStorage.enterWith({own});return own}export function scopedRequestSnapshot(){const scope=scopeStorage.getStore();if(!scope)return;const{own,published}=scope;if(!published)return own.siteHost?{...own,host:own.siteHost}:own;const merged={...own,...published,site:own.site??published.site??null};return own.siteHost?{...merged,host:own.siteHost}:merged}const STX_SERVE_CONTEXT="__stxServeContext";export function scopeStxServeContext(){const existing=Object.getOwnPropertyDescriptor(globalThis,STX_SERVE_CONTEXT);if(existing?.get)return;let unscoped=existing?.value;Object.defineProperty(globalThis,STX_SERVE_CONTEXT,{configurable:true,enumerable:false,get:()=>{const scope=scopeStorage.getStore();return scope?scope.published:unscoped},set:(value)=>{const scope=scopeStorage.getStore();if(scope)scope.published=value;else unscoped=value}})}export function installRequestScope(){scopeStxServeContext();return installRequestContext(scopedRequestSnapshot)}export function useRequestEvent(){const context=globalThis.requestContext??createRequestContext(()=>{return});return{url:context.url(),path:context.path(),search:context.search(),query:context.query(),cookies:context.cookies(),params:context.params(),locale:context.locale(),ip:context.ip(),host:context.host(),site:context.site()}}
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@stacksjs/config",
|
|
3
3
|
"type": "module",
|
|
4
4
|
"sideEffects": false,
|
|
5
|
-
"version": "0.75.
|
|
5
|
+
"version": "0.75.575",
|
|
6
6
|
"description": "The Stacks config helper methods.",
|
|
7
7
|
"author": "Chris Breuer",
|
|
8
8
|
"contributors": [
|
|
@@ -55,16 +55,16 @@
|
|
|
55
55
|
"prepublishOnly": "bun run build"
|
|
56
56
|
},
|
|
57
57
|
"dependencies": {
|
|
58
|
-
"@stacksjs/events": "0.75.
|
|
59
|
-
"@stacksjs/features": "0.75.
|
|
60
|
-
"@stacksjs/path": "0.75.
|
|
61
|
-
"@stacksjs/tunnel": "0.75.
|
|
58
|
+
"@stacksjs/events": "0.75.575",
|
|
59
|
+
"@stacksjs/features": "0.75.575",
|
|
60
|
+
"@stacksjs/path": "0.75.575",
|
|
61
|
+
"@stacksjs/tunnel": "0.75.575",
|
|
62
62
|
"ts-pantry": "^0.11.198"
|
|
63
63
|
},
|
|
64
64
|
"devDependencies": {
|
|
65
|
-
"@stacksjs/alias": "0.75.
|
|
66
|
-
"@stacksjs/env": "0.75.
|
|
67
|
-
"@stacksjs/types": "0.75.
|
|
68
|
-
"better-dx": "^0.2.
|
|
65
|
+
"@stacksjs/alias": "0.75.575",
|
|
66
|
+
"@stacksjs/env": "0.75.575",
|
|
67
|
+
"@stacksjs/types": "0.75.575",
|
|
68
|
+
"better-dx": "^0.2.39"
|
|
69
69
|
}
|
|
70
70
|
}
|