@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 +1 -1
- package/dist/index.js.map +1 -1
- package/dist/runtime/middleware.js +1 -1
- package/dist/runtime/middleware.js.map +1 -1
- package/package.json +6 -6
- package/src/runtime/middleware.ts +16 -27
package/README.md
CHANGED
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"names":["
|
|
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
|
|
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","
|
|
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.
|
|
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.
|
|
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.
|
|
32
|
+
"@playwright/test": "^1.49.1",
|
|
33
33
|
"@types/content-type": "^1.1.8",
|
|
34
|
-
"@types/node": "^22.10.
|
|
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.
|
|
40
|
-
"@inox-tools/astro-tests": "^0.2.
|
|
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
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
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
|
-
}
|