@sigil-dev/grimoire 0.9.29 → 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/bin/sigil.ts
ADDED
package/index.ts
CHANGED
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
// the path the app is served under, and the helper that puts it on a URL
|
|
2
2
|
export { base, withBase } from "./src/base";
|
|
3
|
-
// server loads call depends() to register invalidation keys
|
|
4
|
-
export { depends } from "./src/client/deps";
|
|
5
3
|
export type { Deferred } from "./src/client/deferred";
|
|
6
4
|
export { deferred, onDeferred } from "./src/client/deferred";
|
|
5
|
+
// server loads call depends() to register invalidation keys
|
|
6
|
+
export { depends } from "./src/client/deps";
|
|
7
|
+
export { importConfig } from "./src/config-file";
|
|
8
|
+
// "1h 30m" and friends, for every option that takes a time
|
|
9
|
+
export { type Duration, ms } from "./src/duration";
|
|
7
10
|
export { Head } from "./src/rendering/head";
|
|
11
|
+
export { type Routes, route } from "./src/routing/route";
|
|
8
12
|
// registerSSRPlugin is server-only — import from "@sigil-dev/grimoire/bun"
|
|
9
13
|
export type { RouteFile, RouteTree } from "./src/routing/scanner";
|
|
10
14
|
export type { ErrorResult } from "./src/sentinels/error.ts";
|
|
@@ -13,13 +17,6 @@ export type { FailResult } from "./src/sentinels/fail.ts";
|
|
|
13
17
|
export { fail, isFailResult } from "./src/sentinels/fail.ts";
|
|
14
18
|
export type { RedirectResult } from "./src/sentinels/redirect.ts";
|
|
15
19
|
export { isRedirectResult, redirect } from "./src/sentinels/redirect.ts";
|
|
16
|
-
export type {
|
|
17
|
-
CookieOptions,
|
|
18
|
-
Cookies,
|
|
19
|
-
Handle,
|
|
20
|
-
RequestEvent,
|
|
21
|
-
ResolveFunction,
|
|
22
|
-
} from "./src/server/hooks";
|
|
23
20
|
export {
|
|
24
21
|
type CachedFn,
|
|
25
22
|
type CachedOptions,
|
|
@@ -27,6 +24,13 @@ export {
|
|
|
27
24
|
cachedBy,
|
|
28
25
|
} from "./src/server/cached";
|
|
29
26
|
export { clientAddress } from "./src/server/client-address";
|
|
27
|
+
export type {
|
|
28
|
+
CookieOptions,
|
|
29
|
+
Cookies,
|
|
30
|
+
Handle,
|
|
31
|
+
RequestEvent,
|
|
32
|
+
ResolveFunction,
|
|
33
|
+
} from "./src/server/hooks";
|
|
30
34
|
export { createHooks, sequence } from "./src/server/hooks";
|
|
31
35
|
export type {
|
|
32
36
|
BuiltinWorkerMode,
|
|
@@ -37,14 +41,16 @@ export type {
|
|
|
37
41
|
LayoutServerLoad,
|
|
38
42
|
LoadContext,
|
|
39
43
|
PageServerLoad,
|
|
44
|
+
PluginLocals,
|
|
45
|
+
PluginTables,
|
|
40
46
|
RenderContext,
|
|
41
47
|
RequestHandler,
|
|
42
48
|
RouteInfo,
|
|
43
49
|
TypedLoadContext,
|
|
50
|
+
WorkerContext,
|
|
44
51
|
WorkerDescriptor,
|
|
45
52
|
WorkerEnv,
|
|
46
53
|
WorkerMode,
|
|
47
54
|
WsRouteHandler,
|
|
48
|
-
WorkerContext,
|
|
49
55
|
} from "./src/types";
|
|
50
56
|
export { defineConfig, workerContext } from "./src/types";
|
package/package.json
CHANGED
|
@@ -11,7 +11,10 @@
|
|
|
11
11
|
"module": "index.ts",
|
|
12
12
|
"type": "module",
|
|
13
13
|
"private": false,
|
|
14
|
-
"version": "0.9.
|
|
14
|
+
"version": "0.9.30",
|
|
15
|
+
"bin": {
|
|
16
|
+
"sigil": "bin/sigil.ts"
|
|
17
|
+
},
|
|
15
18
|
"exports": {
|
|
16
19
|
".": "./index.ts",
|
|
17
20
|
"./server": "./server.ts",
|
|
@@ -35,17 +38,21 @@
|
|
|
35
38
|
"test": "bun test"
|
|
36
39
|
},
|
|
37
40
|
"dependencies": {
|
|
38
|
-
"@sigil-dev/
|
|
39
|
-
"@sigil-dev/
|
|
40
|
-
"
|
|
41
|
+
"@sigil-dev/cli": "0.9.30",
|
|
42
|
+
"@sigil-dev/compiler": "0.9.30",
|
|
43
|
+
"@sigil-dev/runtime": "0.9.30"
|
|
41
44
|
},
|
|
42
45
|
"peerDependencies": {
|
|
43
46
|
"typescript": "^5",
|
|
44
|
-
"happy-dom": "*"
|
|
47
|
+
"happy-dom": "*",
|
|
48
|
+
"vite": "^8.0.16"
|
|
45
49
|
},
|
|
46
50
|
"peerDependenciesMeta": {
|
|
47
51
|
"happy-dom": {
|
|
48
52
|
"optional": true
|
|
53
|
+
},
|
|
54
|
+
"vite": {
|
|
55
|
+
"optional": true
|
|
49
56
|
}
|
|
50
57
|
},
|
|
51
58
|
"devDependencies": {
|
|
@@ -55,6 +62,7 @@
|
|
|
55
62
|
"@babel/plugin-syntax-typescript": "^8.0.3",
|
|
56
63
|
"@babel/preset-typescript": "^8.0.7",
|
|
57
64
|
"@babel/types": "^8.0.6",
|
|
58
|
-
"@types/bun": "latest"
|
|
65
|
+
"@types/bun": "latest",
|
|
66
|
+
"vite": "^8.0.16"
|
|
59
67
|
}
|
|
60
68
|
}
|
package/server.ts
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
export { afterResponse } from "./src/server/after-response.ts";
|
|
2
1
|
export { log } from "./src/logger/instance.ts";
|
|
3
2
|
export { scanRoutes } from "./src/routing/scanner";
|
|
4
3
|
export type { GrimoireApp } from "./src/server";
|
|
5
4
|
export { createApp, createServer } from "./src/server";
|
|
6
|
-
export {
|
|
5
|
+
export { afterResponse } from "./src/server/after-response.ts";
|
|
7
6
|
export { buildProject } from "./src/server/build";
|
|
8
7
|
export { parseCookies } from "./src/server/cookie-utils";
|
|
9
8
|
export { startCoordinator } from "./src/server/coordinator.ts";
|
|
@@ -18,5 +17,6 @@ export {
|
|
|
18
17
|
runSerializeLocals,
|
|
19
18
|
runWorkerSpawn,
|
|
20
19
|
} from "./src/server/plugins";
|
|
20
|
+
export { projectAliases } from "./src/server/resolver";
|
|
21
21
|
export type { TypegenConfig } from "./src/typegen";
|
|
22
22
|
export { generateTypes } from "./src/typegen";
|
package/src/adapters/bun.ts
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
import type { Adapter } from "./types";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* The default target, spelled out: a Bun server (`sigil start`, optionally
|
|
5
|
-
* behind the coordinator). It has no build step of its own, so this exists
|
|
6
|
-
* for a config to say what it means rather than to do anything.
|
|
7
|
-
*
|
|
8
|
-
* Not portable to Node today. The request path itself uses Bun APIs —
|
|
9
|
-
* `Bun.serve` and `ServerWebSocket`, `Bun.Glob` in the route scanner, the
|
|
10
|
-
* Bun plugin that compiles route modules for SSR, `Bun.file` for assets — so
|
|
11
|
-
* a Node adapter would be a port of those, not a packaging step.
|
|
12
|
-
*/
|
|
13
|
-
export function adapterBun(): Adapter {
|
|
14
|
-
return { name: "bun" };
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export default adapterBun;
|
|
1
|
+
import type { Adapter } from "./types";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The default target, spelled out: a Bun server (`sigil start`, optionally
|
|
5
|
+
* behind the coordinator). It has no build step of its own, so this exists
|
|
6
|
+
* for a config to say what it means rather than to do anything.
|
|
7
|
+
*
|
|
8
|
+
* Not portable to Node today. The request path itself uses Bun APIs —
|
|
9
|
+
* `Bun.serve` and `ServerWebSocket`, `Bun.Glob` in the route scanner, the
|
|
10
|
+
* Bun plugin that compiles route modules for SSR, `Bun.file` for assets — so
|
|
11
|
+
* a Node adapter would be a port of those, not a packaging step.
|
|
12
|
+
*/
|
|
13
|
+
export function adapterBun(): Adapter {
|
|
14
|
+
return { name: "bun" };
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export default adapterBun;
|
package/src/adapters/static.ts
CHANGED
|
@@ -1,41 +1,41 @@
|
|
|
1
|
-
import { type PrerenderOptions, prerenderSite } from "../server/prerender";
|
|
2
|
-
import type { Adapter } from "./types";
|
|
3
|
-
|
|
4
|
-
export type AdapterStaticOptions = Partial<PrerenderOptions>;
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Build the site to plain files: HTML for every page, the client bundles, and
|
|
8
|
-
* everything from `public/`. Serve the output from any static host, no server.
|
|
9
|
-
*
|
|
10
|
-
* ```ts
|
|
11
|
-
* import { defineConfig } from "@sigil-dev/grimoire";
|
|
12
|
-
* import { adapterStatic } from "@sigil-dev/grimoire/adapters/static";
|
|
13
|
-
*
|
|
14
|
-
* export default defineConfig({ adapter: adapterStatic({ out: "build" }) });
|
|
15
|
-
* ```
|
|
16
|
-
*
|
|
17
|
-
* Pages are found from routes that need no params, from `export const entries
|
|
18
|
-
* = () => [{ slug: "a" }]` on routes that do, and by following links from
|
|
19
|
-
* those pages. `export const prerender = false` on a route, a form action, or
|
|
20
|
-
* a `+server.ts` endpoint cannot exist without a server, so the build fails
|
|
21
|
-
* naming each one rather than shipping a site with holes.
|
|
22
|
-
*
|
|
23
|
-
* Client-side navigation stays instant: each page gets a `__data.json` beside
|
|
24
|
-
* it, which the router reads in place of asking a server.
|
|
25
|
-
*
|
|
26
|
-
* A site served under a path (`config.base = "/repo"`) is built the same way:
|
|
27
|
-
* pages are requested and linked with the base, and the files are written
|
|
28
|
-
* without it, so the output directory is what the host serves at that path.
|
|
29
|
-
*/
|
|
30
|
-
export function adapterStatic(options: AdapterStaticOptions = {}): Adapter {
|
|
31
|
-
return {
|
|
32
|
-
name: "static",
|
|
33
|
-
async adapt(ctx) {
|
|
34
|
-
const out = options.out ?? "build";
|
|
35
|
-
const report = await prerenderSite(ctx, { ...options, out });
|
|
36
|
-
ctx.log.info(`prerendered ${report.pages.length} page(s) to ${out}/`);
|
|
37
|
-
},
|
|
38
|
-
};
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
export default adapterStatic;
|
|
1
|
+
import { type PrerenderOptions, prerenderSite } from "../server/prerender";
|
|
2
|
+
import type { Adapter } from "./types";
|
|
3
|
+
|
|
4
|
+
export type AdapterStaticOptions = Partial<PrerenderOptions>;
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Build the site to plain files: HTML for every page, the client bundles, and
|
|
8
|
+
* everything from `public/`. Serve the output from any static host, no server.
|
|
9
|
+
*
|
|
10
|
+
* ```ts
|
|
11
|
+
* import { defineConfig } from "@sigil-dev/grimoire";
|
|
12
|
+
* import { adapterStatic } from "@sigil-dev/grimoire/adapters/static";
|
|
13
|
+
*
|
|
14
|
+
* export default defineConfig({ adapter: adapterStatic({ out: "build" }) });
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* Pages are found from routes that need no params, from `export const entries
|
|
18
|
+
* = () => [{ slug: "a" }]` on routes that do, and by following links from
|
|
19
|
+
* those pages. `export const prerender = false` on a route, a form action, or
|
|
20
|
+
* a `+server.ts` endpoint cannot exist without a server, so the build fails
|
|
21
|
+
* naming each one rather than shipping a site with holes.
|
|
22
|
+
*
|
|
23
|
+
* Client-side navigation stays instant: each page gets a `__data.json` beside
|
|
24
|
+
* it, which the router reads in place of asking a server.
|
|
25
|
+
*
|
|
26
|
+
* A site served under a path (`config.base = "/repo"`) is built the same way:
|
|
27
|
+
* pages are requested and linked with the base, and the files are written
|
|
28
|
+
* without it, so the output directory is what the host serves at that path.
|
|
29
|
+
*/
|
|
30
|
+
export function adapterStatic(options: AdapterStaticOptions = {}): Adapter {
|
|
31
|
+
return {
|
|
32
|
+
name: "static",
|
|
33
|
+
async adapt(ctx) {
|
|
34
|
+
const out = options.out ?? "build";
|
|
35
|
+
const report = await prerenderSite(ctx, { ...options, out });
|
|
36
|
+
ctx.log.info(`prerendered ${report.pages.length} page(s) to ${out}/`);
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export default adapterStatic;
|
package/src/adapters/types.ts
CHANGED
|
@@ -1,36 +1,36 @@
|
|
|
1
|
-
import type { GrimoireApp } from "../server/index";
|
|
2
|
-
import type { GrimoireConfig, GrimoirePlugin } from "../types";
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* What a deployment target does with a finished build.
|
|
6
|
-
*
|
|
7
|
-
* Without an adapter, `sigil build` leaves the bundles under `public/` and
|
|
8
|
-
* `sigil start` serves them from the Bun server — that is the default and
|
|
9
|
-
* needs nothing. An adapter adds a step after the build: `adapterStatic`
|
|
10
|
-
* prerenders the site to plain files.
|
|
11
|
-
*
|
|
12
|
-
* Deliberately one field on the config rather than a plugin hook. A project
|
|
13
|
-
* has one deployment target, and `onBuildEnd` is handed neither the route
|
|
14
|
-
* tree nor a way to render a page, which is what an adapter needs.
|
|
15
|
-
*/
|
|
16
|
-
export interface Adapter {
|
|
17
|
-
name: string;
|
|
18
|
-
/**
|
|
19
|
-
* Runs after the client bundles are built, with the real request pipeline
|
|
20
|
-
* available as `app.fetch`. Throw to fail the build.
|
|
21
|
-
*/
|
|
22
|
-
adapt?(ctx: AdapterContext): void | Promise<void>;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export interface AdapterContext {
|
|
26
|
-
/** The app as it would run in production, but not listening on a port. */
|
|
27
|
-
app: GrimoireApp;
|
|
28
|
-
config: GrimoireConfig;
|
|
29
|
-
plugins: GrimoirePlugin[];
|
|
30
|
-
/** Project root. */
|
|
31
|
-
root: string;
|
|
32
|
-
log: {
|
|
33
|
-
info(msg: string): void;
|
|
34
|
-
warn(msg: string): void;
|
|
35
|
-
};
|
|
36
|
-
}
|
|
1
|
+
import type { GrimoireApp } from "../server/index";
|
|
2
|
+
import type { GrimoireConfig, GrimoirePlugin } from "../types";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* What a deployment target does with a finished build.
|
|
6
|
+
*
|
|
7
|
+
* Without an adapter, `sigil build` leaves the bundles under `public/` and
|
|
8
|
+
* `sigil start` serves them from the Bun server — that is the default and
|
|
9
|
+
* needs nothing. An adapter adds a step after the build: `adapterStatic`
|
|
10
|
+
* prerenders the site to plain files.
|
|
11
|
+
*
|
|
12
|
+
* Deliberately one field on the config rather than a plugin hook. A project
|
|
13
|
+
* has one deployment target, and `onBuildEnd` is handed neither the route
|
|
14
|
+
* tree nor a way to render a page, which is what an adapter needs.
|
|
15
|
+
*/
|
|
16
|
+
export interface Adapter {
|
|
17
|
+
name: string;
|
|
18
|
+
/**
|
|
19
|
+
* Runs after the client bundles are built, with the real request pipeline
|
|
20
|
+
* available as `app.fetch`. Throw to fail the build.
|
|
21
|
+
*/
|
|
22
|
+
adapt?(ctx: AdapterContext): void | Promise<void>;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface AdapterContext {
|
|
26
|
+
/** The app as it would run in production, but not listening on a port. */
|
|
27
|
+
app: GrimoireApp;
|
|
28
|
+
config: GrimoireConfig;
|
|
29
|
+
plugins: GrimoirePlugin[];
|
|
30
|
+
/** Project root. */
|
|
31
|
+
root: string;
|
|
32
|
+
log: {
|
|
33
|
+
info(msg: string): void;
|
|
34
|
+
warn(msg: string): void;
|
|
35
|
+
};
|
|
36
|
+
}
|
package/src/base.ts
CHANGED
|
@@ -83,7 +83,10 @@ export function outsideBaseMessage(
|
|
|
83
83
|
* The route path for a full pathname, or `null` when the pathname is outside
|
|
84
84
|
* the base. `/repo` and `/repo/` are both `/`.
|
|
85
85
|
*/
|
|
86
|
-
export function stripBase(
|
|
86
|
+
export function stripBase(
|
|
87
|
+
pathname: string,
|
|
88
|
+
under: string = base,
|
|
89
|
+
): string | null {
|
|
87
90
|
if (!under) return pathname;
|
|
88
91
|
if (pathname === under) return "/";
|
|
89
92
|
if (pathname.startsWith(`${under}/`)) return pathname.slice(under.length);
|
package/src/client/csrf.ts
CHANGED
|
@@ -1,47 +1,47 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* CSRF for forms the browser rendered.
|
|
3
|
-
*
|
|
4
|
-
* The server double-submits a token: the `_csrf` cookie, plus a hidden input
|
|
5
|
-
* it injects into every <form> in the SSR HTML (rendering/index.ts). A page
|
|
6
|
-
* reached by client-side navigation is rendered in the browser, so its forms
|
|
7
|
-
* never got that input and every POST from them was refused with 403.
|
|
8
|
-
*
|
|
9
|
-
* ensureCsrf copies the cookie's current token into a form just before it
|
|
10
|
-
* submits. Always overwriting the value also fixes forms that did get an input
|
|
11
|
-
* but are stale, say after the cookie expired and a new one was issued.
|
|
12
|
-
*/
|
|
13
|
-
|
|
14
|
-
export const CSRF_FIELD = "_csrf";
|
|
15
|
-
|
|
16
|
-
function cookieToken(): string | null {
|
|
17
|
-
const m = /(?:^|;\s*)_csrf=([^;]+)/.exec(document.cookie);
|
|
18
|
-
return m ? decodeURIComponent(m[1]) : null;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export function ensureCsrf(form: HTMLFormElement): void {
|
|
22
|
-
const token = cookieToken();
|
|
23
|
-
if (!token) return;
|
|
24
|
-
let input = form.querySelector<HTMLInputElement>(
|
|
25
|
-
`input[name="${CSRF_FIELD}"]`,
|
|
26
|
-
);
|
|
27
|
-
if (!input) {
|
|
28
|
-
input = document.createElement("input");
|
|
29
|
-
input.type = "hidden";
|
|
30
|
-
input.name = CSRF_FIELD;
|
|
31
|
-
input.setAttribute("data-grimoire-csrf", "");
|
|
32
|
-
form.prepend(input);
|
|
33
|
-
}
|
|
34
|
-
input.value = token;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Native (non-enhanced) POST forms: a capture listener on the document runs
|
|
39
|
-
* before any handler on the form itself, so the token is in place for both
|
|
40
|
-
* the browser's own submission and anything that reads FormData.
|
|
41
|
-
*/
|
|
42
|
-
export function onSubmitEnsureCsrf(e: Event): void {
|
|
43
|
-
const form = e.target as HTMLFormElement | null;
|
|
44
|
-
if (form?.tagName !== "FORM") return;
|
|
45
|
-
if ((form.getAttribute("method") ?? "get").toLowerCase() !== "post") return;
|
|
46
|
-
ensureCsrf(form);
|
|
47
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* CSRF for forms the browser rendered.
|
|
3
|
+
*
|
|
4
|
+
* The server double-submits a token: the `_csrf` cookie, plus a hidden input
|
|
5
|
+
* it injects into every <form> in the SSR HTML (rendering/index.ts). A page
|
|
6
|
+
* reached by client-side navigation is rendered in the browser, so its forms
|
|
7
|
+
* never got that input and every POST from them was refused with 403.
|
|
8
|
+
*
|
|
9
|
+
* ensureCsrf copies the cookie's current token into a form just before it
|
|
10
|
+
* submits. Always overwriting the value also fixes forms that did get an input
|
|
11
|
+
* but are stale, say after the cookie expired and a new one was issued.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
export const CSRF_FIELD = "_csrf";
|
|
15
|
+
|
|
16
|
+
function cookieToken(): string | null {
|
|
17
|
+
const m = /(?:^|;\s*)_csrf=([^;]+)/.exec(document.cookie);
|
|
18
|
+
return m ? decodeURIComponent(m[1]) : null;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function ensureCsrf(form: HTMLFormElement): void {
|
|
22
|
+
const token = cookieToken();
|
|
23
|
+
if (!token) return;
|
|
24
|
+
let input = form.querySelector<HTMLInputElement>(
|
|
25
|
+
`input[name="${CSRF_FIELD}"]`,
|
|
26
|
+
);
|
|
27
|
+
if (!input) {
|
|
28
|
+
input = document.createElement("input");
|
|
29
|
+
input.type = "hidden";
|
|
30
|
+
input.name = CSRF_FIELD;
|
|
31
|
+
input.setAttribute("data-grimoire-csrf", "");
|
|
32
|
+
form.prepend(input);
|
|
33
|
+
}
|
|
34
|
+
input.value = token;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Native (non-enhanced) POST forms: a capture listener on the document runs
|
|
39
|
+
* before any handler on the form itself, so the token is in place for both
|
|
40
|
+
* the browser's own submission and anything that reads FormData.
|
|
41
|
+
*/
|
|
42
|
+
export function onSubmitEnsureCsrf(e: Event): void {
|
|
43
|
+
const form = e.target as HTMLFormElement | null;
|
|
44
|
+
if (form?.tagName !== "FORM") return;
|
|
45
|
+
if ((form.getAttribute("method") ?? "get").toLowerCase() !== "post") return;
|
|
46
|
+
ensureCsrf(form);
|
|
47
|
+
}
|
package/src/client/deferred.ts
CHANGED
|
@@ -154,10 +154,7 @@ export function deferred<T>(
|
|
|
154
154
|
* The callback runs once immediately if the value has already landed, so
|
|
155
155
|
* there is no window where it is missed.
|
|
156
156
|
*/
|
|
157
|
-
export function onDeferred<T>(
|
|
158
|
-
apply: (value: T) => void,
|
|
159
|
-
key?: string,
|
|
160
|
-
): void {
|
|
157
|
+
export function onDeferred<T>(apply: (value: T) => void, key?: string): void {
|
|
161
158
|
if (typeof window === "undefined") return;
|
|
162
159
|
|
|
163
160
|
// Fires once, immediately, if the value has already landed — the same
|