@inox-tools/request-state 0.1.3 → 0.1.5-beta.0
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/events.js +1 -4
- package/dist/index.js +1 -7
- package/dist/index.js.map +1 -1
- package/dist/plugin.js +2 -7
- package/dist/plugin.js.map +1 -1
- package/dist/runtime/clientState.js +1 -7
- package/dist/runtime/clientState.js.map +1 -1
- package/dist/runtime/middleware.js +1 -8
- package/dist/runtime/middleware.js.map +1 -1
- package/dist/runtime/serverState.js +1 -7
- package/dist/runtime/serverState.js.map +1 -1
- package/package.json +11 -11
package/dist/events.js
CHANGED
|
@@ -1,5 +1,2 @@
|
|
|
1
|
-
class t extends Event{constructor(r,s,e){super(t.NAME,e);this.previousState=r;this.serverState=s;}static NAME="@it-astro:server-state-loaded"}
|
|
2
|
-
|
|
3
|
-
export { t as ServerStateLoaded };
|
|
4
|
-
//# sourceMappingURL=events.js.map
|
|
1
|
+
class t extends Event{constructor(r,s,e){super(t.NAME,e);this.previousState=r;this.serverState=s;}static NAME="@it-astro:server-state-loaded"}export{t as ServerStateLoaded};//# sourceMappingURL=events.js.map
|
|
5
2
|
//# sourceMappingURL=events.js.map
|
package/dist/index.js
CHANGED
|
@@ -1,8 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { plugin } from './plugin.js';
|
|
3
|
-
|
|
4
|
-
var g=defineIntegration({name:"@inox-tools/request-state",setup(){const{resolve:t}=createResolver(import.meta.url);return {hooks:{"astro:config:setup":e=>{const{addMiddleware:r}=e;e.logger.debug("Adding request-state middleware"),r({order:"pre",entrypoint:t("runtime/middleware.js")}),e.logger.debug("Adding request-state virtual module"),addVitePlugin(e,{warnDuplicated:!0,plugin:plugin()});}}}}});
|
|
5
|
-
|
|
6
|
-
export { g as default };
|
|
7
|
-
//# sourceMappingURL=index.js.map
|
|
1
|
+
import {defineIntegration,createResolver,addVitePlugin}from'astro-integration-kit';import {plugin}from'./plugin.js';var g=defineIntegration({name:"@inox-tools/request-state",setup(){const{resolve:t}=createResolver(import.meta.url);return {hooks:{"astro:config:setup":e=>{const{addMiddleware:r}=e;e.logger.debug("Adding request-state middleware"),r({order:"pre",entrypoint:t("runtime/middleware.js")}),e.logger.debug("Adding request-state virtual module"),addVitePlugin(e,{warnDuplicated:!0,plugin:plugin()});}}}}});export{g as default};//# sourceMappingURL=index.js.map
|
|
8
2
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"names":["src_default","defineIntegration","resolve","createResolver","params","addMiddleware","addVitePlugin","plugin"],"mappings":"
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"names":["src_default","defineIntegration","resolve","createResolver","params","addMiddleware","addVitePlugin","plugin"],"mappings":"oHAGA,IAAOA,CAAQC,CAAAA,iBAAAA,CAAkB,CAChC,IAAM,CAAA,2BAAA,CACN,KAAQ,EAAA,CACP,KAAM,CAAE,OAAA,CAAAC,CAAQ,CAAA,CAAIC,eAAe,MAAY,CAAA,IAAA,CAAA,GAAG,CAElD,CAAA,OAAO,CACN,KAAO,CAAA,CACN,oBAAuBC,CAAAA,CAAAA,EAAW,CACjC,KAAM,CAAE,aAAAC,CAAAA,CAAc,EAAID,CAE1BA,CAAAA,CAAAA,CAAO,MAAO,CAAA,KAAA,CAAM,iCAAiC,CACrDC,CAAAA,CAAAA,CAAc,CACb,KAAA,CAAO,MACP,UAAYH,CAAAA,CAAAA,CAAQ,uBAAuB,CAC5C,CAAC,CAEDE,CAAAA,CAAAA,CAAO,MAAO,CAAA,KAAA,CAAM,qCAAqC,CACzDE,CAAAA,aAAAA,CAAcF,CAAQ,CAAA,CACrB,eAAgB,CAChB,CAAA,CAAA,MAAA,CAAQG,MAAO,EAChB,CAAC,EACF,CACD,CACD,CACD,CACD,CAAC","file":"index.js","sourcesContent":["import { defineIntegration, addVitePlugin, createResolver } from 'astro-integration-kit';\nimport { plugin } from './plugin.js';\n\nexport default defineIntegration({\n\tname: '@inox-tools/request-state',\n\tsetup() {\n\t\tconst { resolve } = createResolver(import.meta.url);\n\n\t\treturn {\n\t\t\thooks: {\n\t\t\t\t'astro:config:setup': (params) => {\n\t\t\t\t\tconst { addMiddleware } = params;\n\n\t\t\t\t\tparams.logger.debug('Adding request-state middleware');\n\t\t\t\t\taddMiddleware({\n\t\t\t\t\t\torder: 'pre',\n\t\t\t\t\t\tentrypoint: resolve('runtime/middleware.js'),\n\t\t\t\t\t});\n\n\t\t\t\t\tparams.logger.debug('Adding request-state virtual module');\n\t\t\t\t\taddVitePlugin(params, {\n\t\t\t\t\t\twarnDuplicated: true,\n\t\t\t\t\t\tplugin: plugin(),\n\t\t\t\t\t});\n\t\t\t\t},\n\t\t\t},\n\t\t};\n\t},\n});\n"]}
|
package/dist/plugin.js
CHANGED
|
@@ -1,10 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
const r="@it-astro:state",s=`\0${r}`,u=()=>{const{resolve:e}=createResolver(import.meta.url);return {name:"@inox-tools/request-state/vite-plugin",resolveId(t){if(t===r)return s},config(t){t.ssr?.external!==!0&&(t.ssr={...t.ssr,external:[...t.ssr?.external??[],"node:async_hooks"]});},load(t,o){if(t!==s)return;const n=o?.ssr?"serverState.js":"clientState.js";return `
|
|
1
|
+
import {createResolver}from'astro-integration-kit';const r="@it-astro:state",s=`\0${r}`,u=()=>{const{resolve:e}=createResolver(import.meta.url);return {name:"@inox-tools/request-state/vite-plugin",resolveId(t){if(t===r)return s},config(t){t.ssr?.external!==!0&&(t.ssr={...t.ssr,external:[...t.ssr?.external??[],"node:async_hooks"]});},load(t,o){if(t!==s)return;const n=o?.ssr?"serverState.js":"clientState.js";return `
|
|
4
2
|
export {setState, getState} from '${e("runtime",n)}';
|
|
5
3
|
export {ServerStateLoaded} from '${e("events.js")}';
|
|
6
|
-
`.trim()}}};
|
|
7
|
-
|
|
8
|
-
export { u as plugin };
|
|
9
|
-
//# sourceMappingURL=plugin.js.map
|
|
4
|
+
`.trim()}}};export{u as plugin};//# sourceMappingURL=plugin.js.map
|
|
10
5
|
//# sourceMappingURL=plugin.js.map
|
package/dist/plugin.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/plugin.ts"],"names":["MODULE_ID","RESOLVED_MODULE_ID","plugin","resolve","createResolver","id","config","options","stateSource"],"mappings":"
|
|
1
|
+
{"version":3,"sources":["../src/plugin.ts"],"names":["MODULE_ID","RESOLVED_MODULE_ID","plugin","resolve","createResolver","id","config","options","stateSource"],"mappings":"mDAGA,MAAMA,EAAY,iBACZC,CAAAA,CAAAA,CAAqB,CAAOD,EAAAA,EAAAA,CAAS,GAE9BE,CAAS,CAAA,IAAc,CACnC,KAAM,CAAE,OAAAC,CAAAA,CAAQ,CAAIC,CAAAA,cAAAA,CAAe,YAAY,GAAG,CAAA,CAElD,OAAO,CACN,KAAM,uCACN,CAAA,SAAA,CAAUC,CAAI,CAAA,CACb,GAAIA,CAAOL,GAAAA,CAAAA,CACV,OAAOC,CAET,EACA,MAAOK,CAAAA,CAAAA,CAAQ,CACVA,CAAAA,CAAO,GAAK,EAAA,QAAA,GAAa,CAE7BA,CAAAA,GAAAA,CAAAA,CAAO,IAAM,CACZ,GAAGA,CAAO,CAAA,GAAA,CACV,SAAU,CAAC,GAAIA,CAAO,CAAA,GAAA,EAAK,UAAY,EAAC,CAAI,kBAAkB,CAC/D,GACD,CACA,CAAA,IAAA,CAAKD,CAAIE,CAAAA,CAAAA,CAAS,CACjB,GAAIF,CAAAA,GAAOJ,CAAoB,CAAA,OAE/B,MAAMO,CAAcD,CAAAA,CAAAA,EAAS,GAAM,CAAA,gBAAA,CAAmB,iBAGtD,OAAO;AAAA,kCAFYJ,EAAAA,CAAAA,CAAQ,SAAWK,CAAAA,CAAW,CAGN,CAAA;AAAA,iCACXL,EAAAA,CAAAA,CAAQ,WAAW,CAAC,CAAA;AAAA,CACrD,CAAA,IAAA,EACA,CACD,CACD","file":"plugin.js","sourcesContent":["import { createResolver } from 'astro-integration-kit';\nimport type { Plugin } from 'vite';\n\nconst MODULE_ID = '@it-astro:state';\nconst RESOLVED_MODULE_ID = `\\x00${MODULE_ID}`;\n\nexport const plugin = (): Plugin => {\n\tconst { resolve } = createResolver(import.meta.url);\n\n\treturn {\n\t\tname: '@inox-tools/request-state/vite-plugin',\n\t\tresolveId(id) {\n\t\t\tif (id === MODULE_ID) {\n\t\t\t\treturn RESOLVED_MODULE_ID;\n\t\t\t}\n\t\t},\n\t\tconfig(config) {\n\t\t\tif (config.ssr?.external === true) return;\n\n\t\t\tconfig.ssr = {\n\t\t\t\t...config.ssr,\n\t\t\t\texternal: [...(config.ssr?.external ?? []), 'node:async_hooks'],\n\t\t\t};\n\t\t},\n\t\tload(id, options) {\n\t\t\tif (id !== RESOLVED_MODULE_ID) return;\n\n\t\t\tconst stateSource = options?.ssr ? 'serverState.js' : 'clientState.js';\n\t\t\tconst importPath = resolve('runtime', stateSource);\n\n\t\t\treturn `\nexport {setState, getState} from '${importPath}';\nexport {ServerStateLoaded} from '${resolve('events.js')}';\n`.trim();\n\t\t},\n\t};\n};\n"]}
|
|
@@ -1,8 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { ServerStateLoaded } from '../events.js';
|
|
3
|
-
|
|
4
|
-
const i={URL:e=>new URL(e),Date:e=>new Date(e),GlobalSymbol:e=>Symbol.for(e),WellKnownSymbol:e=>Symbol[e]},a=e=>{const t=e.getElementById("it-astro-state");if(t?.textContent){const o=parse(t.textContent,i);return t.remove(),o}};let r=a(document);const n=new Map,s=()=>{const e=new ServerStateLoaded(new Map(n),r??new Map);if(document.dispatchEvent(e)){n.clear();for(const[t,o]of e.serverState.entries())n.set(t,o);}};s(),document.addEventListener("astro:before-swap",e=>{r=a(e.newDocument);}),document.addEventListener("astro:after-swap",s);const f=(e,t)=>(n.has(e)||t!==void 0&&n.set(e,t),n.get(e)),p=(e,t)=>{t===void 0?n.delete(e):n.set(e,t);};
|
|
5
|
-
|
|
6
|
-
export { f as getState, p as setState };
|
|
7
|
-
//# sourceMappingURL=clientState.js.map
|
|
1
|
+
import {parse}from'devalue';import {ServerStateLoaded}from'../events.js';const i={URL:e=>new URL(e),Date:e=>new Date(e),GlobalSymbol:e=>Symbol.for(e),WellKnownSymbol:e=>Symbol[e]},a=e=>{const t=e.getElementById("it-astro-state");if(t?.textContent){const o=parse(t.textContent,i);return t.remove(),o}};let r=a(document);const n=new Map,s=()=>{const e=new ServerStateLoaded(new Map(n),r??new Map);if(document.dispatchEvent(e)){n.clear();for(const[t,o]of e.serverState.entries())n.set(t,o);}};s(),document.addEventListener("astro:before-swap",e=>{r=a(e.newDocument);}),document.addEventListener("astro:after-swap",s);const f=(e,t)=>(n.has(e)||t!==void 0&&n.set(e,t),n.get(e)),p=(e,t)=>{t===void 0?n.delete(e):n.set(e,t);};export{f as getState,p as setState};//# sourceMappingURL=clientState.js.map
|
|
8
2
|
//# sourceMappingURL=clientState.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/runtime/clientState.ts"],"names":["revivers","value","loadState","doc","element","state","parse","nextState","applyState","event","ServerStateLoaded","key","getState","valueIfMissing","setState"],"mappings":"
|
|
1
|
+
{"version":3,"sources":["../../src/runtime/clientState.ts"],"names":["revivers","value","loadState","doc","element","state","parse","nextState","applyState","event","ServerStateLoaded","key","getState","valueIfMissing","setState"],"mappings":"yEAGA,MAAMA,CAAgD,CAAA,CACrD,IAAMC,CAAU,EAAA,IAAI,IAAIA,CAAK,CAAA,CAC7B,KAAOA,CAAU,EAAA,IAAI,IAAKA,CAAAA,CAAK,EAC/B,YAAeA,CAAAA,CAAAA,EAAU,MAAO,CAAA,GAAA,CAAIA,CAAK,CACzC,CAAA,eAAA,CAAkBA,CAAU,EAAA,MAAA,CAAOA,CAA4B,CAChE,CAAA,CAEMC,EAAaC,CAAqC,EAAA,CACvD,MAAMC,CAAUD,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M,CAAAA,CAAAA,CAAKV,CAAK,EAEtB","file":"clientState.js","sourcesContent":["import { parse } from 'devalue';\nimport { type State, ServerStateLoaded } from '../events.js';\n\nconst revivers: Record<string, (value: any) => any> = {\n\tURL: (value) => new URL(value),\n\tDate: (value) => new Date(value),\n\tGlobalSymbol: (value) => Symbol.for(value),\n\tWellKnownSymbol: (value) => Symbol[value as keyof typeof Symbol],\n};\n\nconst loadState = (doc: Document): State | undefined => {\n\tconst element = doc.getElementById('it-astro-state');\n\n\tif (element?.textContent) {\n\t\tconst state = parse(element.textContent, revivers);\n\t\telement.remove();\n\t\treturn state;\n\t}\n};\n\nlet nextState = loadState(document);\nconst state = new Map();\n\nconst applyState = () => {\n\tconst event = new ServerStateLoaded(new Map(state), nextState ?? new Map());\n\n\tif (document.dispatchEvent(event)) {\n\t\tstate.clear();\n\t\tfor (const [key, value] of event.serverState.entries()) {\n\t\t\tstate.set(key, value);\n\t\t}\n\t}\n};\n\napplyState();\n\ndocument.addEventListener('astro:before-swap', (event) => {\n\tnextState = loadState(event.newDocument);\n});\ndocument.addEventListener('astro:after-swap', applyState);\n\nexport const getState = (key: string, valueIfMissing?: unknown): unknown => {\n\tif (!state.has(key)) {\n\t\tif (valueIfMissing !== undefined) {\n\t\t\tstate.set(key, valueIfMissing);\n\t\t}\n\t}\n\treturn state.get(key);\n};\n\nexport const setState = (key: string, value: unknown): void => {\n\tif (value === undefined) {\n\t\tstate.delete(key);\n\t} else {\n\t\tstate.set(key, value);\n\t}\n};\n"]}
|
|
@@ -1,9 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { collectState } from './serverState.js';
|
|
3
|
-
import { parse } from 'content-type';
|
|
4
|
-
|
|
5
|
-
const u=defineMiddleware(async(i,n)=>{const{getState:e,result:t}=await collectState(n),r=t.headers.get("Content-Type");if(r===null)return t;const{type:o}=parse(r);if(o!=="text/html"&&!o.startsWith("text/html+"))return t;const s=t.body?.pipeThrough(new TextDecoderStream).pipeThrough(d(e)).pipeThrough(new TextEncoderStream);return new Response(s,t)});function d(i){let n=!1;return new TransformStream({transform(e,t){if(!n){const r=e.indexOf("</body>");if(r>-1){const o=i();if(o){const s=`<script id="it-astro-state" type="application/json+devalue">${o}</script>`;e=e.slice(0,r)+s+e.slice(r);}n=!0;}}t.enqueue(e);}})}
|
|
6
|
-
|
|
7
|
-
export { u as onRequest };
|
|
8
|
-
//# sourceMappingURL=middleware.js.map
|
|
1
|
+
import {defineMiddleware}from'astro/middleware';import {collectState}from'./serverState.js';import {parse}from'content-type';const u=defineMiddleware(async(i,n)=>{const{getState:e,result:t}=await collectState(n),r=t.headers.get("Content-Type");if(r===null)return t;const{type:o}=parse(r);if(o!=="text/html"&&!o.startsWith("text/html+"))return t;const s=t.body?.pipeThrough(new TextDecoderStream).pipeThrough(d(e)).pipeThrough(new TextEncoderStream);return new Response(s,t)});function d(i){let n=!1;return new TransformStream({transform(e,t){if(!n){const r=e.indexOf("</body>");if(r>-1){const o=i();if(o){const s=`<script id="it-astro-state" type="application/json+devalue">${o}</script>`;e=e.slice(0,r)+s+e.slice(r);}n=!0;}}t.enqueue(e);}})}export{u as onRequest};//# sourceMappingURL=middleware.js.map
|
|
9
2
|
//# sourceMappingURL=middleware.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/runtime/middleware.ts"],"names":["onRequest","defineMiddleware","_","next","getState","result","collectState","contentType","mediaType","parse","newBody","injectState","injected","chunk","controller","bodyCloseIndex","state","stateScript"],"mappings":"
|
|
1
|
+
{"version":3,"sources":["../../src/runtime/middleware.ts"],"names":["onRequest","defineMiddleware","_","next","getState","result","collectState","contentType","mediaType","parse","newBody","injectState","injected","chunk","controller","bodyCloseIndex","state","stateScript"],"mappings":"6HAIaA,MAAAA,CAAAA,CAAYC,iBAAiB,MAAOC,CAAAA,CAAGC,CAAS,GAAA,CAC5D,KAAM,CAAE,QAAAC,CAAAA,CAAAA,CAAU,OAAAC,CAAO,CAAA,CAAI,MAAMC,Y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ectState } from './serverState.js';\nimport { parse } from 'content-type';\n\nexport const onRequest = defineMiddleware(async (_, next) => {\n\tconst { getState, result } = await collectState(next);\n\n\tconst contentType = result.headers.get('Content-Type');\n\n\tif (contentType === null) return result;\n\n\tconst { type: mediaType } = parse(contentType);\n\n\tif (mediaType !== 'text/html' && !mediaType.startsWith('text/html+')) return result;\n\n\tconst newBody = result.body\n\t\t?.pipeThrough(new TextDecoderStream())\n\t\t.pipeThrough(injectState(getState))\n\t\t.pipeThrough(new TextEncoderStream());\n\n\treturn new Response(newBody, result);\n});\n\nfunction injectState(getState: () => string | false) {\n\tlet injected = false;\n\treturn new TransformStream({\n\t\ttransform(chunk, controller) {\n\t\t\tif (!injected) {\n\t\t\t\tconst bodyCloseIndex = chunk.indexOf('</body>');\n\t\t\t\tif (bodyCloseIndex > -1) {\n\t\t\t\t\tconst state = getState();\n\t\t\t\t\tif (state) {\n\t\t\t\t\t\tconst stateScript = `<script id=\"it-astro-state\" type=\"application/json+devalue\">${state}</script>`;\n\n\t\t\t\t\t\tchunk = chunk.slice(0, bodyCloseIndex) + stateScript + chunk.slice(bodyCloseIndex);\n\t\t\t\t\t}\n\t\t\t\t\tinjected = true;\n\t\t\t\t}\n\t\t\t}\n\t\t\tcontroller.enqueue(chunk);\n\t\t},\n\t});\n}\n"]}
|
|
@@ -1,8 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { stringify } from 'devalue';
|
|
3
|
-
|
|
4
|
-
const o=new AsyncLocalStorage,l=(t,e)=>{const n=o.getStore();if(n!==void 0)return n.has(t)||e!==void 0&&n.set(t,e),n.get(t)},p=(t,e)=>{const n=o.getStore();e===void 0?n?.delete(t):n?.set(t,e);},i=new Map(Object.entries(Symbol).filter(([t,e])=>typeof e=="symbol"&&typeof t=="string").map(([t,e])=>[e,t])),c={URL:t=>t instanceof URL&&t.href,Date:t=>t instanceof Date&&t.valueOf(),GlobalSymbol:t=>typeof t=="symbol"&&t.description!==void 0&&t===Symbol.for(t.description)&&t.description,WellKnownSymbol:t=>typeof t=="symbol"&&i.get(t)},d=async t=>{const e=new Map;return {result:await o.run(e,t),getState:()=>e.size>0&&stringify(e,c)}};
|
|
5
|
-
|
|
6
|
-
export { d as collectState, l as getState, p as setState };
|
|
7
|
-
//# sourceMappingURL=serverState.js.map
|
|
1
|
+
import {AsyncLocalStorage}from'node:async_hooks';import {stringify}from'devalue';const o=new AsyncLocalStorage,l=(t,e)=>{const n=o.getStore();if(n!==void 0)return n.has(t)||e!==void 0&&n.set(t,e),n.get(t)},p=(t,e)=>{const n=o.getStore();e===void 0?n?.delete(t):n?.set(t,e);},i=new Map(Object.entries(Symbol).filter(([t,e])=>typeof e=="symbol"&&typeof t=="string").map(([t,e])=>[e,t])),c={URL:t=>t instanceof URL&&t.href,Date:t=>t instanceof Date&&t.valueOf(),GlobalSymbol:t=>typeof t=="symbol"&&t.description!==void 0&&t===Symbol.for(t.description)&&t.description,WellKnownSymbol:t=>typeof t=="symbol"&&i.get(t)},d=async t=>{const e=new Map;return {result:await o.run(e,t),getState:()=>e.size>0&&stringify(e,c)}};export{d as collectState,l as getState,p as setState};//# sourceMappingURL=serverState.js.map
|
|
8
2
|
//# sourceMappingURL=serverState.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/runtime/serverState.ts"],"names":["store","AsyncLocalStorage","getState","key","valueIfMissing","state","setState","value","wellKnownSymbols","reducers","collectState","cb","stringify"],"mappings":"
|
|
1
|
+
{"version":3,"sources":["../../src/runtime/serverState.ts"],"names":["store","AsyncLocalStorage","getState","key","valueIfMissing","state","setState","value","wellKnownSymbols","reducers","collectState","cb","stringify"],"mappings":"iFAKA,MAAMA,CAAQ,CAAA,IAAIC,kBAELC,CAAW,CAAA,CAACC,CAAaC,CAAAA,CAAAA,GAAsC,CAC3E,MAAMC,CAAAA,CAAQL,CAAM,CAAA,QAAA,GACpB,GAAIK,CAAAA,GAAU,KAEd,CAAA,CAAA,OAAKA,EAAM,GAAIF,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,GAAA,CAAIK,EAAOM,CAAE,CAAA,CAGvC,QAAU,CAAA,IAAMN,EAAM,IAAO,CAAA,CAAA,EAAKO,UAAUP,CAAOI,CAAAA,CAAQ,CAC5D,CACD","file":"serverState.js","sourcesContent":["import { AsyncLocalStorage } from 'node:async_hooks';\nimport { stringify } from 'devalue';\n\ntype State = Map<string, unknown>;\n\nconst store = new AsyncLocalStorage<State>();\n\nexport const getState = (key: string, valueIfMissing?: unknown): unknown => {\n\tconst state = store.getStore();\n\tif (state === undefined) return;\n\n\tif (!state.has(key)) {\n\t\tif (valueIfMissing !== undefined) {\n\t\t\tstate.set(key, valueIfMissing);\n\t\t}\n\t}\n\treturn state.get(key);\n};\n\nexport const setState = (key: string, value: unknown): void => {\n\tconst state = store.getStore();\n\tif (value === undefined) {\n\t\tstate?.delete(key);\n\t} else {\n\t\tstate?.set(key, value);\n\t}\n};\n\nexport type CollectedState<T> = {\n\tgetState: () => string | false;\n\tresult: T;\n};\n\nconst wellKnownSymbols = new Map(\n\tObject.entries(Symbol)\n\t\t.filter(([key, value]) => typeof value === 'symbol' && typeof key === 'string')\n\t\t.map(([key, value]) => [value, key])\n);\n\nconst reducers: Record<string, (value: any) => any> = {\n\tURL: (value) => value instanceof URL && value.href,\n\tDate: (value) => value instanceof Date && value.valueOf(),\n\tGlobalSymbol: (value) =>\n\t\ttypeof value === 'symbol' &&\n\t\tvalue.description !== undefined &&\n\t\tvalue === Symbol.for(value.description) &&\n\t\tvalue.description,\n\tWellKnownSymbol: (value) => typeof value === 'symbol' && wellKnownSymbols.get(value),\n};\n\nexport const collectState = async <R>(cb: () => Promise<R>): Promise<CollectedState<R>> => {\n\tconst state = new Map();\n\tconst result = await store.run(state, cb);\n\treturn {\n\t\tresult,\n\t\tgetState: () => state.size > 0 && stringify(state, reducers),\n\t};\n};\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inox-tools/request-state",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.5-beta.0",
|
|
4
4
|
"description": "Shared request state between server and client",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"astro-integration",
|
|
@@ -23,24 +23,24 @@
|
|
|
23
23
|
"virtual.d.ts"
|
|
24
24
|
],
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"astro-integration-kit": "~0.
|
|
26
|
+
"astro-integration-kit": "~0.17.0",
|
|
27
27
|
"content-type": "^1.0.5",
|
|
28
28
|
"devalue": "^5.1.1",
|
|
29
|
-
"@inox-tools/utils": "^0.
|
|
29
|
+
"@inox-tools/utils": "^0.3.0-beta.0"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
|
32
|
-
"@playwright/test": "^1.
|
|
32
|
+
"@playwright/test": "^1.48.2",
|
|
33
33
|
"@types/content-type": "^1.1.8",
|
|
34
|
-
"@types/node": "^22.
|
|
35
|
-
"astro": "^
|
|
34
|
+
"@types/node": "^22.8.4",
|
|
35
|
+
"astro": "^5.0.0-beta",
|
|
36
36
|
"jest-extended": "^4.0.2",
|
|
37
|
-
"tsup": "8.
|
|
38
|
-
"typescript": "^5.
|
|
39
|
-
"vite": "^
|
|
40
|
-
"@inox-tools/astro-tests": "^0.
|
|
37
|
+
"tsup": "8.3.5",
|
|
38
|
+
"typescript": "^5.6.3",
|
|
39
|
+
"vite": "^6.0.0-beta",
|
|
40
|
+
"@inox-tools/astro-tests": "^0.2.0-beta.0"
|
|
41
41
|
},
|
|
42
42
|
"peerDependencies": {
|
|
43
|
-
"astro": "^
|
|
43
|
+
"astro": "^5.0.0-beta"
|
|
44
44
|
},
|
|
45
45
|
"scripts": {
|
|
46
46
|
"build": "tsup",
|