@sigil-dev/grimoire 0.9.28 → 0.9.30
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/bin/sigil.ts +4 -0
- package/index.ts +16 -10
- package/package.json +14 -6
- package/server.ts +2 -2
- package/src/adapters/bun.ts +17 -17
- package/src/adapters/static.ts +41 -41
- package/src/adapters/types.ts +36 -36
- package/src/base.ts +4 -1
- package/src/client/csrf.ts +47 -47
- package/src/client/deferred.ts +1 -4
- package/src/client/enhance.ts +160 -160
- package/src/client/router.ts +927 -926
- package/src/config-file.ts +84 -0
- package/src/dev/compile-module.ts +199 -193
- package/src/dev/hmr-client.ts +229 -229
- package/src/dev/hmr-server.ts +274 -266
- package/src/dev/loader.ts +144 -144
- package/src/dev/runtime-bundle.ts +5 -3
- package/src/duration.ts +46 -0
- package/src/env/modules.ts +62 -62
- package/src/env/required.ts +42 -42
- package/src/env/typegen.ts +61 -61
- package/src/headers.ts +105 -105
- package/src/logger/transports.ts +117 -117
- package/src/plugin-order.ts +88 -0
- package/src/rendering/head.ts +49 -49
- package/src/rendering/hydrate.ts +162 -162
- package/src/rendering/images.ts +43 -43
- package/src/rendering/preload.ts +22 -8
- package/src/rendering/ssrPlugin.ts +107 -107
- package/src/rendering/timing.ts +40 -40
- package/src/routing/route-ids.ts +65 -0
- package/src/routing/route.ts +65 -0
- package/src/routing/router.ts +84 -32
- package/src/routing/scanner.ts +18 -5
- package/src/routing/transform-routes.ts +102 -102
- package/src/server/after-response.ts +206 -206
- package/src/server/asset-cache.ts +57 -57
- package/src/server/build.ts +304 -295
- package/src/server/cached.ts +114 -114
- package/src/server/client-address.ts +82 -82
- package/src/server/client-dir.ts +63 -63
- package/src/server/client-entry.ts +44 -44
- package/src/server/context.ts +18 -10
- package/src/server/cookie-utils.ts +80 -80
- package/src/server/coordinator.ts +476 -476
- package/src/server/hooks.ts +122 -122
- package/src/server/index.ts +8 -0
- package/src/server/plugins.ts +172 -172
- package/src/server/prerender.ts +473 -473
- package/src/server/reserved.ts +17 -17
- package/src/server/resolver.ts +149 -123
- package/src/server/rewrite.ts +28 -28
- package/src/server/warm.ts +9 -3
- package/src/server/with-headers.ts +27 -27
- package/src/server/worker.ts +2 -6
- package/src/server-only.ts +78 -0
- package/src/testing/dom.ts +73 -73
- package/src/testing/index.ts +169 -169
- package/src/testing/server.ts +1 -0
- package/src/typegen/index.ts +97 -2
- package/src/types.ts +140 -19
- package/test/after-response.test.ts +132 -132
- package/test/asset-cache-policy.test.ts +6 -2
- package/test/base.test.ts +20 -6
- package/test/cached.test.ts +126 -126
- package/test/client-address.test.ts +78 -78
- package/test/client-csrf.test.ts +61 -61
- package/test/client-dir.test.ts +101 -101
- package/test/client-router.test.ts +37 -5
- package/test/config-file.test.ts +84 -0
- package/test/coordinator.test.ts +9 -2
- package/test/deferred.test.ts +4 -2
- package/test/dev-loader.test.ts +30 -30
- package/test/env.test.ts +171 -171
- package/test/force-production.test.ts +7 -2
- package/test/images.test.ts +20 -8
- package/test/locals-header.test.ts +61 -61
- package/test/middleware.test.ts +243 -243
- package/test/parser-plugins.test.ts +98 -92
- package/test/plugin-handle.test.ts +62 -62
- package/test/plugin-order.test.ts +98 -0
- package/test/plugin-routes.test.ts +47 -42
- package/test/plugin-services-context.test.ts +7 -2
- package/test/preload.test.ts +4 -1
- package/test/prerender.test.ts +3 -1
- package/test/rendering.test.ts +3 -2
- package/test/required-env.test.ts +32 -32
- package/test/resolver-windows.test.ts +22 -23
- package/test/rewrite-html.test.ts +90 -90
- package/test/route-group-layouts.test.ts +38 -36
- package/test/route.test.ts +103 -0
- package/test/routing.test.ts +225 -197
- package/test/scanning.test.ts +53 -33
- package/test/server-context.test.ts +22 -0
- package/test/server-only.test.ts +110 -0
- package/test/server.test.ts +640 -640
- package/test/services.test.ts +3 -3
- package/test/streaming.test.ts +11 -6
- package/test/testing-helpers.test.ts +166 -166
- package/test/typegen.test.ts +209 -112
- package/test/warm.test.ts +96 -96
- package/test/worker-context.test.ts +59 -59
- package/test/workspace-files.test.ts +26 -10
- package/test/workspace-versions.test.ts +146 -131
package/src/client/enhance.ts
CHANGED
|
@@ -1,160 +1,160 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Client-side form action handler.
|
|
3
|
-
* Wraps form submission with fetch, handles fail() responses, and updates state.
|
|
4
|
-
*
|
|
5
|
-
* Usage in +page.tsx:
|
|
6
|
-
*
|
|
7
|
-
* import { enhance } from "@sigil-dev/grimoire/client";
|
|
8
|
-
* import { createSignal } from "@sigil-dev/runtime";
|
|
9
|
-
*
|
|
10
|
-
* const [errors, setErrors] = createSignal({});
|
|
11
|
-
*
|
|
12
|
-
* <form use={[enhance, { action: "/api/login", onSuccess: () => { ... }, onFail: (data) => setErrors(data) }]}>
|
|
13
|
-
* ...
|
|
14
|
-
* </form>
|
|
15
|
-
*/
|
|
16
|
-
import { ensureCsrf } from "./csrf.ts";
|
|
17
|
-
|
|
18
|
-
export interface EnhanceOptions {
|
|
19
|
-
/** URL to submit to (defaults to form's action) */
|
|
20
|
-
action?: string;
|
|
21
|
-
/**
|
|
22
|
-
* Called when the action succeeds, before navigating to `redirect` (the
|
|
23
|
-
* page itself, unless the action redirected). `data` is whatever the
|
|
24
|
-
* action returned.
|
|
25
|
-
*/
|
|
26
|
-
onSuccess?: (result: { redirect: string; data?: unknown }) => void;
|
|
27
|
-
/**
|
|
28
|
-
* Called when the action returns fail(). The page has already been shown
|
|
29
|
-
* again with that data as its `form` prop, so most forms need no handler.
|
|
30
|
-
*/
|
|
31
|
-
onFail?: (data: Record<string, unknown>) => void;
|
|
32
|
-
/** Called on network or unexpected errors, and for error() */
|
|
33
|
-
onError?: (error: Error) => void;
|
|
34
|
-
/** Whether to reset the form on success (default: true) */
|
|
35
|
-
reset?: boolean;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
type Outcome =
|
|
39
|
-
| { fail: true; status: number; data: Record<string, unknown> }
|
|
40
|
-
| { error: true; status: number; message: string }
|
|
41
|
-
| { type: "success"; location: string; data: unknown }
|
|
42
|
-
| { type: "redirect"; status: number; location: string };
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Go where the action sent us through the client router, so the destination's
|
|
46
|
-
* loads run again. Landing back on the same page replaces the history entry
|
|
47
|
-
* rather than adding one, and the router keeps the scroll position.
|
|
48
|
-
*/
|
|
49
|
-
function follow(location: string): void {
|
|
50
|
-
const navigate = (
|
|
51
|
-
globalThis as {
|
|
52
|
-
__grimoire_navigate__?: (path: string, replace?: boolean) => void;
|
|
53
|
-
}
|
|
54
|
-
).__grimoire_navigate__;
|
|
55
|
-
const target = new URL(location, window.location.href);
|
|
56
|
-
if (target.origin !== window.location.origin || !navigate) {
|
|
57
|
-
window.location.href = target.href;
|
|
58
|
-
return;
|
|
59
|
-
}
|
|
60
|
-
const path = target.pathname + target.search + target.hash;
|
|
61
|
-
const here = window.location.pathname + window.location.search;
|
|
62
|
-
navigate(path, target.pathname + target.search === here);
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* use:enhance directive for progressive form enhancement.
|
|
67
|
-
* Attaches to a <form> element and intercepts submit events.
|
|
68
|
-
*
|
|
69
|
-
* Returns a cleanup function.
|
|
70
|
-
*/
|
|
71
|
-
export function enhance(
|
|
72
|
-
form: HTMLFormElement,
|
|
73
|
-
options: EnhanceOptions = {},
|
|
74
|
-
): () => void {
|
|
75
|
-
const { action, onSuccess, onFail, onError, reset = true } = options;
|
|
76
|
-
|
|
77
|
-
async function handleSubmit(e: Event) {
|
|
78
|
-
e.preventDefault();
|
|
79
|
-
|
|
80
|
-
// client-rendered forms have no SSR-injected token; the server's CSRF
|
|
81
|
-
// check covers fetch submissions too
|
|
82
|
-
ensureCsrf(form);
|
|
83
|
-
const formData = new FormData(form);
|
|
84
|
-
// the button that submitted the form sends its own name=value, just
|
|
85
|
-
// as a native submission would
|
|
86
|
-
const submitter = (e as SubmitEvent).submitter as
|
|
87
|
-
| HTMLButtonElement
|
|
88
|
-
| HTMLInputElement
|
|
89
|
-
| null;
|
|
90
|
-
if (submitter?.name) formData.append(submitter.name, submitter.value);
|
|
91
|
-
const resolved =
|
|
92
|
-
action ?? form.getAttribute("action") ?? window.location.pathname;
|
|
93
|
-
// SvelteKit-style named action: "?/create" submits to the current path
|
|
94
|
-
const url = resolved.startsWith("?/")
|
|
95
|
-
? window.location.pathname + resolved
|
|
96
|
-
: resolved;
|
|
97
|
-
|
|
98
|
-
try {
|
|
99
|
-
const res = await fetch(url, {
|
|
100
|
-
method: "POST",
|
|
101
|
-
headers: {
|
|
102
|
-
"X-Grimoire-Form": "1",
|
|
103
|
-
Accept: "application/json",
|
|
104
|
-
},
|
|
105
|
-
body: formData,
|
|
106
|
-
});
|
|
107
|
-
|
|
108
|
-
const contentType = res.headers.get("content-type") ?? "";
|
|
109
|
-
if (!contentType.includes("application/json")) {
|
|
110
|
-
if (!res.ok) throw new Error(`Form submission failed: ${res.status}`);
|
|
111
|
-
// something that isn't a page action answered (a proxy, a plain
|
|
112
|
-
// endpoint): count it as done and stay put
|
|
113
|
-
if (reset) form.reset();
|
|
114
|
-
onSuccess?.({ redirect: window.location.pathname });
|
|
115
|
-
return;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
const json = (await res.json()) as Outcome | Record<string, unknown>;
|
|
119
|
-
if ("fail" in json && json.fail) {
|
|
120
|
-
const data = (json as { data: Record<string, unknown> }).data;
|
|
121
|
-
// the page that posted shows the failure as its `form` prop, as
|
|
122
|
-
// it would after a submission without JavaScript
|
|
123
|
-
if (
|
|
124
|
-
new URL(url, window.location.href).pathname ===
|
|
125
|
-
window.location.pathname
|
|
126
|
-
) {
|
|
127
|
-
await (
|
|
128
|
-
globalThis as {
|
|
129
|
-
__grimoire_form__?: (form: unknown) => Promise<void>;
|
|
130
|
-
}
|
|
131
|
-
).__grimoire_form__?.(data);
|
|
132
|
-
}
|
|
133
|
-
onFail?.(data);
|
|
134
|
-
return;
|
|
135
|
-
}
|
|
136
|
-
if ("error" in json && json.error) {
|
|
137
|
-
throw new Error(String((json as { message?: unknown }).message));
|
|
138
|
-
}
|
|
139
|
-
if (
|
|
140
|
-
"type" in json &&
|
|
141
|
-
(json.type === "success" || json.type === "redirect")
|
|
142
|
-
) {
|
|
143
|
-
const outcome = json as { location: string; data?: unknown };
|
|
144
|
-
if (reset) form.reset();
|
|
145
|
-
onSuccess?.({ redirect: outcome.location, data: outcome.data });
|
|
146
|
-
follow(outcome.location);
|
|
147
|
-
return;
|
|
148
|
-
}
|
|
149
|
-
// JSON from a +server.ts endpoint (action: "/api/…")
|
|
150
|
-
if (!res.ok) throw new Error(`Form submission failed: ${res.status}`);
|
|
151
|
-
if (reset) form.reset();
|
|
152
|
-
onSuccess?.({ redirect: window.location.pathname, data: json });
|
|
153
|
-
} catch (err) {
|
|
154
|
-
onError?.(err instanceof Error ? err : new Error(String(err)));
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
form.addEventListener("submit", handleSubmit);
|
|
159
|
-
return () => form.removeEventListener("submit", handleSubmit);
|
|
160
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Client-side form action handler.
|
|
3
|
+
* Wraps form submission with fetch, handles fail() responses, and updates state.
|
|
4
|
+
*
|
|
5
|
+
* Usage in +page.tsx:
|
|
6
|
+
*
|
|
7
|
+
* import { enhance } from "@sigil-dev/grimoire/client";
|
|
8
|
+
* import { createSignal } from "@sigil-dev/runtime";
|
|
9
|
+
*
|
|
10
|
+
* const [errors, setErrors] = createSignal({});
|
|
11
|
+
*
|
|
12
|
+
* <form use={[enhance, { action: "/api/login", onSuccess: () => { ... }, onFail: (data) => setErrors(data) }]}>
|
|
13
|
+
* ...
|
|
14
|
+
* </form>
|
|
15
|
+
*/
|
|
16
|
+
import { ensureCsrf } from "./csrf.ts";
|
|
17
|
+
|
|
18
|
+
export interface EnhanceOptions {
|
|
19
|
+
/** URL to submit to (defaults to form's action) */
|
|
20
|
+
action?: string;
|
|
21
|
+
/**
|
|
22
|
+
* Called when the action succeeds, before navigating to `redirect` (the
|
|
23
|
+
* page itself, unless the action redirected). `data` is whatever the
|
|
24
|
+
* action returned.
|
|
25
|
+
*/
|
|
26
|
+
onSuccess?: (result: { redirect: string; data?: unknown }) => void;
|
|
27
|
+
/**
|
|
28
|
+
* Called when the action returns fail(). The page has already been shown
|
|
29
|
+
* again with that data as its `form` prop, so most forms need no handler.
|
|
30
|
+
*/
|
|
31
|
+
onFail?: (data: Record<string, unknown>) => void;
|
|
32
|
+
/** Called on network or unexpected errors, and for error() */
|
|
33
|
+
onError?: (error: Error) => void;
|
|
34
|
+
/** Whether to reset the form on success (default: true) */
|
|
35
|
+
reset?: boolean;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
type Outcome =
|
|
39
|
+
| { fail: true; status: number; data: Record<string, unknown> }
|
|
40
|
+
| { error: true; status: number; message: string }
|
|
41
|
+
| { type: "success"; location: string; data: unknown }
|
|
42
|
+
| { type: "redirect"; status: number; location: string };
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Go where the action sent us through the client router, so the destination's
|
|
46
|
+
* loads run again. Landing back on the same page replaces the history entry
|
|
47
|
+
* rather than adding one, and the router keeps the scroll position.
|
|
48
|
+
*/
|
|
49
|
+
function follow(location: string): void {
|
|
50
|
+
const navigate = (
|
|
51
|
+
globalThis as {
|
|
52
|
+
__grimoire_navigate__?: (path: string, replace?: boolean) => void;
|
|
53
|
+
}
|
|
54
|
+
).__grimoire_navigate__;
|
|
55
|
+
const target = new URL(location, window.location.href);
|
|
56
|
+
if (target.origin !== window.location.origin || !navigate) {
|
|
57
|
+
window.location.href = target.href;
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
const path = target.pathname + target.search + target.hash;
|
|
61
|
+
const here = window.location.pathname + window.location.search;
|
|
62
|
+
navigate(path, target.pathname + target.search === here);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* use:enhance directive for progressive form enhancement.
|
|
67
|
+
* Attaches to a <form> element and intercepts submit events.
|
|
68
|
+
*
|
|
69
|
+
* Returns a cleanup function.
|
|
70
|
+
*/
|
|
71
|
+
export function enhance(
|
|
72
|
+
form: HTMLFormElement,
|
|
73
|
+
options: EnhanceOptions = {},
|
|
74
|
+
): () => void {
|
|
75
|
+
const { action, onSuccess, onFail, onError, reset = true } = options;
|
|
76
|
+
|
|
77
|
+
async function handleSubmit(e: Event) {
|
|
78
|
+
e.preventDefault();
|
|
79
|
+
|
|
80
|
+
// client-rendered forms have no SSR-injected token; the server's CSRF
|
|
81
|
+
// check covers fetch submissions too
|
|
82
|
+
ensureCsrf(form);
|
|
83
|
+
const formData = new FormData(form);
|
|
84
|
+
// the button that submitted the form sends its own name=value, just
|
|
85
|
+
// as a native submission would
|
|
86
|
+
const submitter = (e as SubmitEvent).submitter as
|
|
87
|
+
| HTMLButtonElement
|
|
88
|
+
| HTMLInputElement
|
|
89
|
+
| null;
|
|
90
|
+
if (submitter?.name) formData.append(submitter.name, submitter.value);
|
|
91
|
+
const resolved =
|
|
92
|
+
action ?? form.getAttribute("action") ?? window.location.pathname;
|
|
93
|
+
// SvelteKit-style named action: "?/create" submits to the current path
|
|
94
|
+
const url = resolved.startsWith("?/")
|
|
95
|
+
? window.location.pathname + resolved
|
|
96
|
+
: resolved;
|
|
97
|
+
|
|
98
|
+
try {
|
|
99
|
+
const res = await fetch(url, {
|
|
100
|
+
method: "POST",
|
|
101
|
+
headers: {
|
|
102
|
+
"X-Grimoire-Form": "1",
|
|
103
|
+
Accept: "application/json",
|
|
104
|
+
},
|
|
105
|
+
body: formData,
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
const contentType = res.headers.get("content-type") ?? "";
|
|
109
|
+
if (!contentType.includes("application/json")) {
|
|
110
|
+
if (!res.ok) throw new Error(`Form submission failed: ${res.status}`);
|
|
111
|
+
// something that isn't a page action answered (a proxy, a plain
|
|
112
|
+
// endpoint): count it as done and stay put
|
|
113
|
+
if (reset) form.reset();
|
|
114
|
+
onSuccess?.({ redirect: window.location.pathname });
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const json = (await res.json()) as Outcome | Record<string, unknown>;
|
|
119
|
+
if ("fail" in json && json.fail) {
|
|
120
|
+
const data = (json as { data: Record<string, unknown> }).data;
|
|
121
|
+
// the page that posted shows the failure as its `form` prop, as
|
|
122
|
+
// it would after a submission without JavaScript
|
|
123
|
+
if (
|
|
124
|
+
new URL(url, window.location.href).pathname ===
|
|
125
|
+
window.location.pathname
|
|
126
|
+
) {
|
|
127
|
+
await (
|
|
128
|
+
globalThis as {
|
|
129
|
+
__grimoire_form__?: (form: unknown) => Promise<void>;
|
|
130
|
+
}
|
|
131
|
+
).__grimoire_form__?.(data);
|
|
132
|
+
}
|
|
133
|
+
onFail?.(data);
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
if ("error" in json && json.error) {
|
|
137
|
+
throw new Error(String((json as { message?: unknown }).message));
|
|
138
|
+
}
|
|
139
|
+
if (
|
|
140
|
+
"type" in json &&
|
|
141
|
+
(json.type === "success" || json.type === "redirect")
|
|
142
|
+
) {
|
|
143
|
+
const outcome = json as { location: string; data?: unknown };
|
|
144
|
+
if (reset) form.reset();
|
|
145
|
+
onSuccess?.({ redirect: outcome.location, data: outcome.data });
|
|
146
|
+
follow(outcome.location);
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
// JSON from a +server.ts endpoint (action: "/api/…")
|
|
150
|
+
if (!res.ok) throw new Error(`Form submission failed: ${res.status}`);
|
|
151
|
+
if (reset) form.reset();
|
|
152
|
+
onSuccess?.({ redirect: window.location.pathname, data: json });
|
|
153
|
+
} catch (err) {
|
|
154
|
+
onError?.(err instanceof Error ? err : new Error(String(err)));
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
form.addEventListener("submit", handleSubmit);
|
|
159
|
+
return () => form.removeEventListener("submit", handleSubmit);
|
|
160
|
+
}
|