@inox-tools/request-state 0.2.0 → 0.3.1

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/README.md CHANGED
@@ -16,7 +16,7 @@ This library provides a solution for those problems:
16
16
 
17
17
  ## Installing the dependency
18
18
 
19
- ```js
19
+ ```sh
20
20
  npm i @inox-tools/request-state
21
21
  ```
22
22
 
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":"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"]}
1
+ {"version":3,"sources":["../src/index.ts"],"names":["index_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"]}
@@ -1,2 +1,2 @@
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
1
+ import {defineMiddleware}from'astro/middleware';import {collectState}from'./serverState.js';import {parse}from'content-type';const h=defineMiddleware(async(f,r)=>{const{getState:a,result:t}=await collectState(r),s=t.headers.get("Content-Type");if(s===null)return t;const{type:o}=parse(s);if(o!=="text/html"&&!o.startsWith("text/html+"))return t;const e=await t.text(),n=e.indexOf("</head>");if(n>-1){const i=a();if(i){const c=`<script id="it-astro-state" type="application/json+devalue">${i}</script>`;return new Response(e.slice(0,n)+c+e.slice(n),t)}}return new Response(e,t)});export{h as onRequest};//# sourceMappingURL=middleware.js.map
2
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":"6HAIaA,MAAAA,CAAAA,CAAYC,iBAAiB,MAAOC,CAAAA,CAAGC,CAAS,GAAA,CAC5D,KAAM,CAAE,QAAAC,CAAAA,CAAAA,CAAU,OAAAC,CAAO,CAAA,CAAI,MAAMC,YAAAA,CAAaH,CAAI,CAE9CI,CAAAA,CAAAA,CAAcF,CAAO,CAAA,OAAA,CAAQ,IAAI,cAAc,CAAA,CAErD,GAAIE,CAAAA,GAAgB,IAAM,CAAA,OAAOF,CAEjC,CAAA,KAAM,CAAE,IAAMG,CAAAA,CAAU,CAAIC,CAAAA,KAAAA,CAAMF,CAAW,CAE7C,CAAA,GAAIC,CAAc,GAAA,WAAA,EAAe,CAACA,CAAU,CAAA,UAAA,CAAW,YAAY,CAAA,CAAG,OAAOH,CAAAA,CAE7E,MAAMK,CAAAA,CAAUL,EAAO,IACpB,EAAA,WAAA,CAAY,IAAI,iBAAmB,EACpC,WAAYM,CAAAA,CAAAA,CAAYP,CAAQ,CAAC,EACjC,WAAY,CAAA,IAAI,iBAAmB,CAAA,CAErC,OAAO,IAAI,QAASM,CAAAA,CAAAA,CAASL,CAAM,CACpC,CAAC,EAED,SAASM,CAAYP,CAAAA,CAAAA,CAAgC,CACpD,IAAIQ,EAAW,CACf,CAAA,CAAA,OAAO,IAAI,eAAA,CAAgB,CAC1B,SAAA,CAAUC,CAAOC,CAAAA,CAAAA,CAAY,CAC5B,GAAI,CAACF,CAAU,CAAA,CACd,MAAMG,CAAiBF,CAAAA,CAAAA,CAAM,OAAQ,CAAA,SAAS,EAC9C,GAAIE,CAAAA,CAAiB,CAAI,CAAA,CAAA,CACxB,MAAMC,CAAAA,CAAQZ,CAAS,EAAA,CACvB,GAAIY,CAAO,CAAA,CACV,MAAMC,CAAAA,CAAc,+DAA+DD,CAAK,CAAA,SAAA,CAAA,CAExFH,CAAQA,CAAAA,CAAAA,CAAM,MAAM,CAAGE,CAAAA,CAAc,CAAIE,CAAAA,CAAAA,CAAcJ,CAAM,CAAA,KAAA,CAAME,CAAc,EAClF,CACAH,CAAW,CAAA,CAAA,EACZ,CACD,CACAE,EAAW,OAAQD,CAAAA,CAAK,EACzB,CACD,CAAC,CACF","file":"middleware.js","sourcesContent":["import { defineMiddleware } from 'astro/middleware';\nimport { collectState } 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
+ {"version":3,"sources":["../../src/runtime/middleware.ts"],"names":["onRequest","defineMiddleware","_","next","getState","result","collectState","contentType","mediaType","parse","originalBody","headCloseIndex","state","stateScript"],"mappings":"6HAIO,MAAMA,EAAYC,gBAAiB,CAAA,MAAOC,CAAGC,CAAAA,CAAAA,GAAS,CAC5D,KAAM,CAAE,QAAAC,CAAAA,CAAAA,CAAU,MAAAC,CAAAA,CAAO,CAAI,CAAA,MAAMC,aAAaH,CAAI,CAAA,CAE9CI,CAAcF,CAAAA,CAAAA,CAAO,OAAQ,CAAA,GAAA,CAAI,cAAc,CAErD,CAAA,GAAIE,CAAgB,GAAA,IAAA,CAAM,OAAOF,CAAAA,CAEjC,KAAM,CAAE,IAAA,CAAMG,CAAU,CAAA,CAAIC,KAAMF,CAAAA,CAAW,EAE7C,GAAIC,CAAAA,GAAc,WAAe,EAAA,CAACA,CAAU,CAAA,UAAA,CAAW,YAAY,CAAG,CAAA,OAAOH,CAE7E,CAAA,MAAMK,CAAe,CAAA,MAAML,EAAO,IAAK,EAAA,CAEjCM,CAAiBD,CAAAA,CAAAA,CAAa,OAAQ,CAAA,SAAS,EACrD,GAAIC,CAAAA,CAAiB,CAAI,CAAA,CAAA,CACxB,MAAMC,CAAAA,CAAQR,GACd,CAAA,GAAIQ,CAAO,CAAA,CACV,MAAMC,CAAAA,CAAc,+DAA+DD,CAAK,CAAA,SAAA,CAAA,CAExF,OAAO,IAAI,QACVF,CAAAA,CAAAA,CAAa,MAAM,CAAGC,CAAAA,CAAc,CAAIE,CAAAA,CAAAA,CAAcH,CAAa,CAAA,KAAA,CAAMC,CAAc,CACvFN,CAAAA,CACD,CACD,CACD,CAEA,OAAO,IAAI,QAASK,CAAAA,CAAAA,CAAcL,CAAM,CACzC,CAAC","file":"middleware.js","sourcesContent":["import { defineMiddleware } from 'astro/middleware';\nimport { collectState } 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 originalBody = await result.text();\n\n\tconst headCloseIndex = originalBody.indexOf('</head>');\n\tif (headCloseIndex > -1) {\n\t\tconst state = getState();\n\t\tif (state) {\n\t\t\tconst stateScript = `<script id=\"it-astro-state\" type=\"application/json+devalue\">${state}</script>`;\n\n\t\t\treturn new Response(\n\t\t\t\toriginalBody.slice(0, headCloseIndex) + stateScript + originalBody.slice(headCloseIndex),\n\t\t\t\tresult\n\t\t\t);\n\t\t}\n\t}\n\n\treturn new Response(originalBody, result);\n});\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inox-tools/request-state",
3
- "version": "0.2.0",
3
+ "version": "0.3.1",
4
4
  "description": "Shared request state between server and client",
5
5
  "keywords": [
6
6
  "astro-integration",
@@ -23,21 +23,21 @@
23
23
  "virtual.d.ts"
24
24
  ],
25
25
  "dependencies": {
26
- "astro-integration-kit": "~0.17.0",
26
+ "astro-integration-kit": "~0.18.0",
27
27
  "content-type": "^1.0.5",
28
28
  "devalue": "^5.1.1",
29
29
  "@inox-tools/utils": "^0.3.0"
30
30
  },
31
31
  "devDependencies": {
32
- "@playwright/test": "^1.49.0",
32
+ "@playwright/test": "^1.49.1",
33
33
  "@types/content-type": "^1.1.8",
34
- "@types/node": "^22.10.1",
34
+ "@types/node": "^22.10.3",
35
35
  "astro": "^5",
36
36
  "jest-extended": "^4.0.2",
37
37
  "tsup": "8.3.5",
38
38
  "typescript": "^5.7.2",
39
- "vite": "^6.0.2",
40
- "@inox-tools/astro-tests": "^0.2.1"
39
+ "vite": "^6.0.6",
40
+ "@inox-tools/astro-tests": "^0.2.2"
41
41
  },
42
42
  "peerDependencies": {
43
43
  "astro": "^5"
@@ -13,31 +13,20 @@ export const onRequest = defineMiddleware(async (_, next) => {
13
13
 
14
14
  if (mediaType !== 'text/html' && !mediaType.startsWith('text/html+')) return result;
15
15
 
16
- const newBody = result.body
17
- ?.pipeThrough(new TextDecoderStream())
18
- .pipeThrough(injectState(getState))
19
- .pipeThrough(new TextEncoderStream());
20
-
21
- return new Response(newBody, result);
16
+ const originalBody = await result.text();
17
+
18
+ const headCloseIndex = originalBody.indexOf('</head>');
19
+ if (headCloseIndex > -1) {
20
+ const state = getState();
21
+ if (state) {
22
+ const stateScript = `<script id="it-astro-state" type="application/json+devalue">${state}</script>`;
23
+
24
+ return new Response(
25
+ originalBody.slice(0, headCloseIndex) + stateScript + originalBody.slice(headCloseIndex),
26
+ result
27
+ );
28
+ }
29
+ }
30
+
31
+ return new Response(originalBody, result);
22
32
  });
23
-
24
- function injectState(getState: () => string | false) {
25
- let injected = false;
26
- return new TransformStream({
27
- transform(chunk, controller) {
28
- if (!injected) {
29
- const bodyCloseIndex = chunk.indexOf('</body>');
30
- if (bodyCloseIndex > -1) {
31
- const state = getState();
32
- if (state) {
33
- const stateScript = `<script id="it-astro-state" type="application/json+devalue">${state}</script>`;
34
-
35
- chunk = chunk.slice(0, bodyCloseIndex) + stateScript + chunk.slice(bodyCloseIndex);
36
- }
37
- injected = true;
38
- }
39
- }
40
- controller.enqueue(chunk);
41
- },
42
- });
43
- }