@inox-tools/request-state 0.7.2 → 0.8.2
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,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 m=new TextEncoder,S=defineMiddleware(async(x,d)=>{const{getState:l,result:t}=await collectState(d),o=t.headers.get("Content-Type");if(o===null)return t;const{type:a}=parse(o);if(a!=="text/html"&&!a.startsWith("text/html+"))return t;const e=await t.text(),r=l(),n=r?`<script class="it-astro-state" type="application/json+devalue">${r}</script>`:null;if(!n)return new Response(e,t);const s=e.indexOf("</head>"),p=s>-1?e.slice(0,s)+n+e.slice(s):n+e,c=m.encode(p),u=c.byteLength,i=new Headers(t.headers);return i.set("Content-Length",u.toString()),new Response(c,{status:t.status,statusText:t.statusText,headers:i})});export{S 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","originalBody","state","stateScript","headCloseIndex"],"mappings":"
|
|
1
|
+
{"version":3,"sources":["../../src/runtime/middleware.ts"],"names":["encoder","onRequest","defineMiddleware","_","next","getState","result","collectState","contentType","mediaType","parse","originalBody","state","stateScript","headCloseIndex","finalBody","encodedBody","contentLength","headers"],"mappings":"6HAIA,MAAMA,CAAAA,CAAU,IAAI,WAAA,CAEPC,CAAAA,CAAYC,gBAAAA,CAAiB,MAAOC,CAAAA,CAAGC,CAAAA,GAAS,CAC5D,KAAM,CAAE,QAAA,CAAAC,CAAAA,CAAU,MAAA,CAAAC,CAAO,CAAA,CAAI,MAAMC,YAAAA,CAAaH,CAAI,EAE9CI,C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collectState } from './serverState.js';\nimport { parse } from 'content-type';\n\nconst encoder = new TextEncoder();\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 state = getState();\n\tconst stateScript = state\n\t\t? `<script class=\"it-astro-state\" type=\"application/json+devalue\">${state}</script>`\n\t\t: null;\n\n\tif (!stateScript) {\n\t\treturn new Response(originalBody, result);\n\t}\n\n\tconst headCloseIndex = originalBody.indexOf('</head>');\n\tconst finalBody =\n\t\theadCloseIndex > -1\n\t\t\t? originalBody.slice(0, headCloseIndex) + stateScript + originalBody.slice(headCloseIndex)\n\t\t\t: stateScript + originalBody;\n\n\t// TextEncoder is also supported in CloudFlare Workers, so this should work in all environments Astro supports.\n\tconst encodedBody = encoder.encode(finalBody);\n\tconst contentLength = encodedBody.byteLength;\n\n\tconst headers = new Headers(result.headers);\n\theaders.set('Content-Length', contentLength.toString());\n\n\treturn new Response(encodedBody, {\n\t\tstatus: result.status,\n\t\tstatusText: result.statusText,\n\t\theaders,\n\t});\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.8.2",
|
|
4
4
|
"description": "Shared request state between server and client",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"astro-integration",
|
|
@@ -8,6 +8,9 @@
|
|
|
8
8
|
"astro",
|
|
9
9
|
"utils"
|
|
10
10
|
],
|
|
11
|
+
"repository": {
|
|
12
|
+
"url": "https://github.com/Fryuni/inox-tools"
|
|
13
|
+
},
|
|
11
14
|
"license": "MIT",
|
|
12
15
|
"author": "Luiz Ferraz <luiz@lferraz.com>",
|
|
13
16
|
"type": "module",
|
|
@@ -23,21 +26,21 @@
|
|
|
23
26
|
"virtual.d.ts"
|
|
24
27
|
],
|
|
25
28
|
"dependencies": {
|
|
26
|
-
"astro-integration-kit": "~0.19.
|
|
29
|
+
"astro-integration-kit": "~0.19.1",
|
|
27
30
|
"content-type": "^1.0.5",
|
|
28
|
-
"devalue": "^5.
|
|
29
|
-
"@inox-tools/utils": "^
|
|
31
|
+
"devalue": "^5.6.2",
|
|
32
|
+
"@inox-tools/utils": "^1.1.0"
|
|
30
33
|
},
|
|
31
34
|
"devDependencies": {
|
|
32
|
-
"@playwright/test": "^1.
|
|
35
|
+
"@playwright/test": "^1.58.2",
|
|
33
36
|
"@types/content-type": "^1.1.9",
|
|
34
|
-
"@types/node": "^
|
|
35
|
-
"astro": "^5.
|
|
36
|
-
"jest-extended": "^
|
|
37
|
-
"tsup": "8.5.
|
|
38
|
-
"typescript": "^5.9.
|
|
39
|
-
"vite": "^7.1
|
|
40
|
-
"@inox-tools/astro-tests": "^0.
|
|
37
|
+
"@types/node": "^25.2.2",
|
|
38
|
+
"astro": "^5.17.1",
|
|
39
|
+
"jest-extended": "^7.0.0",
|
|
40
|
+
"tsup": "8.5.1",
|
|
41
|
+
"typescript": "^5.9.3",
|
|
42
|
+
"vite": "^7.3.1",
|
|
43
|
+
"@inox-tools/astro-tests": "^0.9.1"
|
|
41
44
|
},
|
|
42
45
|
"peerDependencies": {
|
|
43
46
|
"astro": "^5"
|
|
@@ -2,6 +2,8 @@ import { defineMiddleware } from 'astro/middleware';
|
|
|
2
2
|
import { collectState } from './serverState.js';
|
|
3
3
|
import { parse } from 'content-type';
|
|
4
4
|
|
|
5
|
+
const encoder = new TextEncoder();
|
|
6
|
+
|
|
5
7
|
export const onRequest = defineMiddleware(async (_, next) => {
|
|
6
8
|
const { getState, result } = await collectState(next);
|
|
7
9
|
|
|
@@ -19,17 +21,27 @@ export const onRequest = defineMiddleware(async (_, next) => {
|
|
|
19
21
|
const stateScript = state
|
|
20
22
|
? `<script class="it-astro-state" type="application/json+devalue">${state}</script>`
|
|
21
23
|
: null;
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
return new Response(
|
|
26
|
-
originalBody.slice(0, headCloseIndex) + stateScript + originalBody.slice(headCloseIndex),
|
|
27
|
-
result
|
|
28
|
-
);
|
|
29
|
-
} else {
|
|
30
|
-
return new Response(stateScript + originalBody, result);
|
|
31
|
-
}
|
|
24
|
+
|
|
25
|
+
if (!stateScript) {
|
|
26
|
+
return new Response(originalBody, result);
|
|
32
27
|
}
|
|
33
28
|
|
|
34
|
-
|
|
29
|
+
const headCloseIndex = originalBody.indexOf('</head>');
|
|
30
|
+
const finalBody =
|
|
31
|
+
headCloseIndex > -1
|
|
32
|
+
? originalBody.slice(0, headCloseIndex) + stateScript + originalBody.slice(headCloseIndex)
|
|
33
|
+
: stateScript + originalBody;
|
|
34
|
+
|
|
35
|
+
// TextEncoder is also supported in CloudFlare Workers, so this should work in all environments Astro supports.
|
|
36
|
+
const encodedBody = encoder.encode(finalBody);
|
|
37
|
+
const contentLength = encodedBody.byteLength;
|
|
38
|
+
|
|
39
|
+
const headers = new Headers(result.headers);
|
|
40
|
+
headers.set('Content-Length', contentLength.toString());
|
|
41
|
+
|
|
42
|
+
return new Response(encodedBody, {
|
|
43
|
+
status: result.status,
|
|
44
|
+
statusText: result.statusText,
|
|
45
|
+
headers,
|
|
46
|
+
});
|
|
35
47
|
});
|