@inox-tools/request-state 0.8.1 → 1.0.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/chunk-BAVYTS2V.js +5 -0
- package/dist/chunk-BAVYTS2V.js.map +1 -0
- package/dist/chunk-GHTUQIMX.js +2 -0
- package/dist/chunk-GHTUQIMX.js.map +1 -0
- package/dist/chunk-MP5FTYJK.js +2 -0
- package/dist/chunk-MP5FTYJK.js.map +1 -0
- package/dist/chunk-RFBBAUMM.js +2 -0
- package/dist/chunk-RFBBAUMM.js.map +1 -0
- package/dist/events.js +1 -1
- package/dist/events.js.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/plugin.js +1 -4
- package/dist/plugin.js.map +1 -1
- package/dist/runtime/clientState.js +1 -1
- package/dist/runtime/clientState.js.map +1 -1
- package/dist/runtime/middleware.js +94 -1
- package/dist/runtime/middleware.js.map +1 -1
- package/dist/runtime/serverState.js +1 -1
- package/dist/runtime/serverState.js.map +1 -1
- package/package.json +9 -9
- package/src/runtime/middleware.ts +23 -11
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
export{d as collectState,a as getState,b as hasState,c as setState}from'../chunk-GHTUQIMX.js';//# sourceMappingURL=serverState.js.map
|
|
2
2
|
//# sourceMappingURL=serverState.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":[
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":"","file":"serverState.js"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inox-tools/request-state",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"description": "Shared request state between server and client",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"astro-integration",
|
|
@@ -26,24 +26,24 @@
|
|
|
26
26
|
"virtual.d.ts"
|
|
27
27
|
],
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"astro-integration-kit": "~0.
|
|
30
|
-
"
|
|
31
|
-
"
|
|
32
|
-
"@inox-tools/utils": "^1.0.0"
|
|
29
|
+
"astro-integration-kit": "~0.20.0",
|
|
30
|
+
"devalue": "^5.6.3",
|
|
31
|
+
"@inox-tools/utils": "^1.1.1"
|
|
33
32
|
},
|
|
34
33
|
"devDependencies": {
|
|
35
34
|
"@playwright/test": "^1.58.2",
|
|
36
35
|
"@types/content-type": "^1.1.9",
|
|
37
|
-
"@types/node": "^25.
|
|
38
|
-
"astro": "^
|
|
36
|
+
"@types/node": "^25.3.3",
|
|
37
|
+
"astro": "^6.0.8",
|
|
38
|
+
"content-type": "^1.0.5",
|
|
39
39
|
"jest-extended": "^7.0.0",
|
|
40
40
|
"tsup": "8.5.1",
|
|
41
41
|
"typescript": "^5.9.3",
|
|
42
42
|
"vite": "^7.3.1",
|
|
43
|
-
"@inox-tools/astro-tests": "^0.
|
|
43
|
+
"@inox-tools/astro-tests": "^1.0.0"
|
|
44
44
|
},
|
|
45
45
|
"peerDependencies": {
|
|
46
|
-
"astro": "^
|
|
46
|
+
"astro": "^6"
|
|
47
47
|
},
|
|
48
48
|
"scripts": {
|
|
49
49
|
"build": "tsup",
|
|
@@ -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
|
});
|