@alecsibilia/ui-kit 0.0.0-stage → 0.1.0-alpha.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/CHANGELOG.md +7 -0
- package/LICENSE +201 -0
- package/README.md +233 -2
- package/bin/ui-kit.js +12 -0
- package/dist/alchemy/app.d.ts +34 -0
- package/dist/alchemy/app.js +88 -0
- package/dist/alchemy/index.d.ts +11 -0
- package/dist/alchemy/index.js +11 -0
- package/dist/alchemy/live-rooms.d.ts +21 -0
- package/dist/alchemy/live-rooms.js +31 -0
- package/dist/alchemy/migrations.d.ts +28 -0
- package/dist/alchemy/migrations.js +56 -0
- package/dist/alchemy/stage.d.ts +20 -0
- package/dist/alchemy/stage.js +20 -0
- package/dist/alchemy/wrangler-config.d.ts +76 -0
- package/dist/alchemy/wrangler-config.js +82 -0
- package/dist/build/app-modules.d.ts +11 -0
- package/dist/build/app-modules.js +40 -0
- package/dist/build/check.d.ts +4 -0
- package/dist/build/check.js +170 -0
- package/dist/build/cli.d.ts +1 -0
- package/dist/build/cli.js +79 -0
- package/dist/build/feature.d.ts +8 -0
- package/dist/build/feature.js +24 -0
- package/dist/build/index.d.ts +13 -0
- package/dist/build/index.js +13 -0
- package/dist/build/measure-cpu.d.ts +2 -0
- package/dist/build/measure-cpu.js +196 -0
- package/dist/build/prerender.d.ts +5 -0
- package/dist/build/prerender.js +109 -0
- package/dist/build/preview.d.ts +7 -0
- package/dist/build/preview.js +41 -0
- package/dist/build/seo-files.d.ts +27 -0
- package/dist/build/seo-files.js +24 -0
- package/dist/client/head-swap.d.ts +45 -0
- package/dist/client/head-swap.js +183 -0
- package/dist/client/hydration-pass.d.ts +56 -0
- package/dist/client/hydration-pass.js +186 -0
- package/dist/client/hydration-reset.d.ts +82 -0
- package/dist/client/hydration-reset.js +154 -0
- package/dist/client/index.d.ts +37 -0
- package/dist/client/index.js +65 -0
- package/dist/client/nav-client.d.ts +35 -0
- package/dist/client/nav-client.js +371 -0
- package/dist/live/collab/doc-provider.d.ts +74 -0
- package/dist/live/collab/doc-provider.js +412 -0
- package/dist/live/collab/index.d.ts +51 -0
- package/dist/live/collab/index.jsx +142 -0
- package/dist/live/collab-status.d.ts +25 -0
- package/dist/live/collab-status.js +17 -0
- package/dist/live/cursors/cursor-engine.d.ts +57 -0
- package/dist/live/cursors/cursor-engine.js +243 -0
- package/dist/live/cursors/index.d.ts +46 -0
- package/dist/live/cursors/index.jsx +110 -0
- package/dist/live/declaration.d.ts +113 -0
- package/dist/live/declaration.js +94 -0
- package/dist/live/index.d.ts +8 -0
- package/dist/live/index.js +8 -0
- package/dist/live/protocol.d.ts +224 -0
- package/dist/live/protocol.js +272 -0
- package/dist/live/server/doc-store.d.ts +68 -0
- package/dist/live/server/doc-store.js +187 -0
- package/dist/live/server/index.d.ts +60 -0
- package/dist/live/server/index.js +77 -0
- package/dist/live/server/room-presence.d.ts +24 -0
- package/dist/live/server/room-presence.js +123 -0
- package/dist/live/server/room-sockets.d.ts +65 -0
- package/dist/live/server/room-sockets.js +97 -0
- package/dist/live/server/room.d.ts +20 -0
- package/dist/live/server/room.js +348 -0
- package/dist/live/server/route.d.ts +56 -0
- package/dist/live/server/route.js +159 -0
- package/dist/live/server/types.d.ts +163 -0
- package/dist/live/server/types.js +2 -0
- package/dist/logic/agent-actions.d.ts +133 -0
- package/dist/logic/agent-actions.js +150 -0
- package/dist/logic/command-types.d.ts +53 -0
- package/dist/logic/command-types.js +0 -0
- package/dist/logic/dev.d.ts +9 -0
- package/dist/logic/dev.js +10 -0
- package/dist/logic/draft-rebase.d.ts +59 -0
- package/dist/logic/draft-rebase.js +71 -0
- package/dist/logic/form-state.d.ts +105 -0
- package/dist/logic/form-state.js +93 -0
- package/dist/logic/head.d.ts +145 -0
- package/dist/logic/head.js +140 -0
- package/dist/logic/index.d.ts +27 -0
- package/dist/logic/index.js +25 -0
- package/dist/logic/island-def.d.ts +89 -0
- package/dist/logic/island-def.js +59 -0
- package/dist/logic/kit-commands.d.ts +104 -0
- package/dist/logic/kit-commands.js +72 -0
- package/dist/logic/kit-errors.d.ts +25 -0
- package/dist/logic/kit-errors.js +6 -0
- package/dist/logic/listen.d.ts +99 -0
- package/dist/logic/listen.js +92 -0
- package/dist/logic/mutation-def.d.ts +65 -0
- package/dist/logic/mutation-def.js +43 -0
- package/dist/logic/navigation-data.d.ts +50 -0
- package/dist/logic/navigation-data.js +55 -0
- package/dist/logic/navigation-policy.d.ts +74 -0
- package/dist/logic/navigation-policy.js +127 -0
- package/dist/logic/page-model-def.d.ts +108 -0
- package/dist/logic/page-model-def.js +31 -0
- package/dist/logic/path.d.ts +31 -0
- package/dist/logic/path.js +48 -0
- package/dist/logic/query-def.d.ts +56 -0
- package/dist/logic/query-def.js +37 -0
- package/dist/logic/query-key.d.ts +24 -0
- package/dist/logic/query-key.js +35 -0
- package/dist/logic/query-types.d.ts +28 -0
- package/dist/logic/query-types.js +2 -0
- package/dist/logic/resources.d.ts +163 -0
- package/dist/logic/resources.js +62 -0
- package/dist/logic/result.d.ts +30 -0
- package/dist/logic/result.js +23 -0
- package/dist/logic/route-model.d.ts +139 -0
- package/dist/logic/route-model.js +207 -0
- package/dist/logic/route-params.d.ts +80 -0
- package/dist/logic/route-params.js +114 -0
- package/dist/logic/route-table.d.ts +308 -0
- package/dist/logic/route-table.js +206 -0
- package/dist/logic/seo.d.ts +61 -0
- package/dist/logic/seo.js +101 -0
- package/dist/logic/site.d.ts +12 -0
- package/dist/logic/site.js +0 -0
- package/dist/logic/subscriptions.d.ts +177 -0
- package/dist/logic/subscriptions.js +152 -0
- package/dist/render/index.d.ts +32 -0
- package/dist/render/index.js +52 -0
- package/dist/runtime/island-wake.d.ts +38 -0
- package/dist/runtime/island-wake.js +147 -0
- package/dist/runtime/query-scope.d.ts +224 -0
- package/dist/runtime/query-scope.js +222 -0
- package/dist/runtime/room-hub.d.ts +107 -0
- package/dist/runtime/room-hub.js +307 -0
- package/dist/runtime/subscription-runner.d.ts +71 -0
- package/dist/runtime/subscription-runner.js +240 -0
- package/dist/server/bindings.d.ts +41 -0
- package/dist/server/bindings.js +74 -0
- package/dist/server/browser.d.ts +9 -0
- package/dist/server/browser.js +9 -0
- package/dist/server/edge-cache.d.ts +28 -0
- package/dist/server/edge-cache.js +35 -0
- package/dist/server/errors.d.ts +45 -0
- package/dist/server/errors.js +36 -0
- package/dist/server/form-actions-setup.d.ts +1 -0
- package/dist/server/form-actions-setup.js +15 -0
- package/dist/server/form-actions.d.ts +121 -0
- package/dist/server/form-actions.js +211 -0
- package/dist/server/index.d.ts +36 -0
- package/dist/server/index.js +35 -0
- package/dist/server/inputs.d.ts +45 -0
- package/dist/server/inputs.js +77 -0
- package/dist/server/rate-limit.d.ts +29 -0
- package/dist/server/rate-limit.js +32 -0
- package/dist/server/read-cache.d.ts +74 -0
- package/dist/server/read-cache.js +58 -0
- package/dist/server/request-info.d.ts +17 -0
- package/dist/server/request-info.js +11 -0
- package/dist/server/request.d.ts +52 -0
- package/dist/server/request.js +73 -0
- package/dist/server/response-cookies.d.ts +23 -0
- package/dist/server/response-cookies.js +24 -0
- package/dist/server/run-effect.d.ts +42 -0
- package/dist/server/run-effect.js +92 -0
- package/dist/server/run.d.ts +63 -0
- package/dist/server/run.js +66 -0
- package/dist/server/session.d.ts +125 -0
- package/dist/server/session.js +247 -0
- package/dist/test/fake-bindings.d.ts +32 -0
- package/dist/test/fake-bindings.js +108 -0
- package/dist/test/fake-room.d.ts +66 -0
- package/dist/test/fake-room.js +216 -0
- package/dist/test/index.d.ts +19 -0
- package/dist/test/index.js +44 -0
- package/dist/test/probe.d.ts +11 -0
- package/dist/test/probe.js +11 -0
- package/dist/test/solid-jsx.d.ts +23 -0
- package/dist/test/solid-jsx.js +28 -0
- package/dist/test/solid-preload.d.ts +1 -0
- package/dist/test/solid-preload.js +29 -0
- package/dist/ui/code-editor/index.d.ts +21 -0
- package/dist/ui/code-editor/index.jsx +286 -0
- package/dist/ui/collab-text/index.d.ts +29 -0
- package/dist/ui/collab-text/index.jsx +96 -0
- package/dist/ui/components/controls.d.ts +69 -0
- package/dist/ui/components/controls.jsx +81 -0
- package/dist/ui/components/feedback.d.ts +26 -0
- package/dist/ui/components/feedback.jsx +80 -0
- package/dist/ui/components/layout.d.ts +74 -0
- package/dist/ui/components/layout.jsx +146 -0
- package/dist/ui/components/lists.d.ts +69 -0
- package/dist/ui/components/lists.jsx +133 -0
- package/dist/ui/components/navigation.d.ts +73 -0
- package/dist/ui/components/navigation.jsx +112 -0
- package/dist/ui/components/text.d.ts +34 -0
- package/dist/ui/components/text.jsx +163 -0
- package/dist/ui/index.d.ts +7 -0
- package/dist/ui/index.js +7 -0
- package/dist/ui/jsx-runtime.d.ts +19 -0
- package/dist/ui/jsx-runtime.js +0 -0
- package/dist/ui/list-keys.d.ts +31 -0
- package/dist/ui/list-keys.js +45 -0
- package/dist/ui/styles/base.css +38 -0
- package/dist/ui/styles/components.css +608 -0
- package/dist/ui/styles/tokens.css +27 -0
- package/dist/ui/styles/transitions.css +23 -0
- package/dist/ui/styles/workbench.css +428 -0
- package/dist/ui/styles.css +9 -0
- package/dist/view/agent-actions-view.d.ts +30 -0
- package/dist/view/agent-actions-view.js +27 -0
- package/dist/view/agent-surface.d.ts +42 -0
- package/dist/view/agent-surface.js +123 -0
- package/dist/view/app-scope.d.ts +51 -0
- package/dist/view/app-scope.js +75 -0
- package/dist/view/boundary.d.ts +18 -0
- package/dist/view/boundary.jsx +26 -0
- package/dist/view/command-runner.d.ts +16 -0
- package/dist/view/command-runner.js +245 -0
- package/dist/view/form.d.ts +82 -0
- package/dist/view/form.jsx +109 -0
- package/dist/view/head-view.d.ts +31 -0
- package/dist/view/head-view.js +46 -0
- package/dist/view/index.d.ts +41 -0
- package/dist/view/index.js +40 -0
- package/dist/view/island-root.d.ts +30 -0
- package/dist/view/island-root.js +36 -0
- package/dist/view/island.d.ts +102 -0
- package/dist/view/island.jsx +154 -0
- package/dist/view/match.d.ts +41 -0
- package/dist/view/match.js +111 -0
- package/dist/view/model.d.ts +92 -0
- package/dist/view/model.js +162 -0
- package/dist/view/navigation-head.d.ts +29 -0
- package/dist/view/navigation-head.jsx +38 -0
- package/dist/view/page-model.d.ts +46 -0
- package/dist/view/page-model.js +146 -0
- package/dist/view/query-effects.d.ts +87 -0
- package/dist/view/query-effects.js +147 -0
- package/dist/view/query-ok.d.ts +45 -0
- package/dist/view/query-ok.js +96 -0
- package/dist/view/query.d.ts +115 -0
- package/dist/view/query.jsx +179 -0
- package/dist/view/result-view.d.ts +14 -0
- package/dist/view/result-view.jsx +15 -0
- package/dist/view/route-pages.d.ts +43 -0
- package/dist/view/route-pages.js +59 -0
- package/dist/view/router.d.ts +44 -0
- package/dist/view/router.js +123 -0
- package/dist/vite/dev-middleware.d.ts +12 -0
- package/dist/vite/dev-middleware.js +39 -0
- package/dist/vite/dev-objects.d.ts +25 -0
- package/dist/vite/dev-objects.js +127 -0
- package/dist/vite/dev-platform.d.ts +19 -0
- package/dist/vite/dev-platform.js +77 -0
- package/dist/vite/dev-seo-files.d.ts +11 -0
- package/dist/vite/dev-seo-files.js +40 -0
- package/dist/vite/index.d.ts +53 -0
- package/dist/vite/index.js +148 -0
- package/dist/vite/local-config.d.ts +34 -0
- package/dist/vite/local-config.js +72 -0
- package/dist/vite/worker-build.d.ts +24 -0
- package/dist/vite/worker-build.js +84 -0
- package/dist/worker/create-worker.d.ts +52 -0
- package/dist/worker/create-worker.js +90 -0
- package/dist/worker/index.d.ts +12 -0
- package/dist/worker/index.js +12 -0
- package/dist/worker/page-routes.d.ts +88 -0
- package/dist/worker/page-routes.js +100 -0
- package/dist/worker/page-status.d.ts +41 -0
- package/dist/worker/page-status.js +23 -0
- package/dist/worker/request-prelude.d.ts +69 -0
- package/dist/worker/request-prelude.js +107 -0
- package/dist/worker/security.d.ts +76 -0
- package/dist/worker/security.js +137 -0
- package/dist/worker/server-app.d.ts +54 -0
- package/dist/worker/server-app.js +76 -0
- package/guards/browser-only.js +5 -0
- package/guards/node-only.js +6 -0
- package/guards/server-only.js +5 -0
- package/migrations/0001-sessions.sql +22 -0
- package/package.json +233 -5
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { routeEntryFor } from '../logic/route-table.js';
|
|
2
|
+
/**
|
|
3
|
+
* Which paths the Worker renders, and how: read from the app's route table
|
|
4
|
+
* (path, render mode, lazy page, NotFound). The Worker, `ui-kit prerender`
|
|
5
|
+
* and the router all read that one table.
|
|
6
|
+
*
|
|
7
|
+
* Unknown paths are a 404 two ways (`classifyPath`): a page-shaped path
|
|
8
|
+
* renders the table's `NotFound` page with status 404 (the app's own page,
|
|
9
|
+
* no data queries); an asset-shaped path (a file extension, `/assets/`), a
|
|
10
|
+
* non-GET request, or an unknown `/_server/*` id gets a short static 404
|
|
11
|
+
* with no render. The dev server uses the same function, so `/nope`
|
|
12
|
+
* behaves the same on `vite dev` and workerd.
|
|
13
|
+
*/
|
|
14
|
+
/** How a page is produced (the table's `render`). */
|
|
15
|
+
export const RenderMode = {
|
|
16
|
+
/** Rendered once at build time into a static asset (`ui-kit prerender`). */
|
|
17
|
+
Prerender: 'prerender',
|
|
18
|
+
/** Rendered per request by the Worker (whole by default, streamed when the route opts in; nonce CSP). */
|
|
19
|
+
Ssr: 'ssr',
|
|
20
|
+
};
|
|
21
|
+
/** Solid's server-function endpoint (`/_server`, and its data paths). */
|
|
22
|
+
export const SERVER_FUNCTION_PATH = '/_server';
|
|
23
|
+
/** The page at `pathname`, or `undefined` (the table's NotFound: a 404). */
|
|
24
|
+
export function pageRoute(routes, pathname) {
|
|
25
|
+
const entry = routeEntryFor(routes, pathname);
|
|
26
|
+
if (entry.path === null)
|
|
27
|
+
return undefined;
|
|
28
|
+
return {
|
|
29
|
+
tag: entry.tag,
|
|
30
|
+
mode: entry.render,
|
|
31
|
+
auth: entry.auth ?? 'public',
|
|
32
|
+
ssr: entry.ssr,
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
/** What `signIn` defaults to: the `SignIn` route and `?next=`. */
|
|
36
|
+
export const DEFAULT_SIGN_IN = {
|
|
37
|
+
route: 'SignIn',
|
|
38
|
+
nextParam: 'next',
|
|
39
|
+
};
|
|
40
|
+
/**
|
|
41
|
+
* Where an unauthenticated request for `url` goes: the sign-in page with
|
|
42
|
+
* the original path and search as `?next=` (same-origin path only).
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```ts
|
|
46
|
+
* signInLocation(routes, DEFAULT_SIGN_IN, new URL('https://x.example/account?tab=1'))
|
|
47
|
+
* // "/sign-in?next=%2Faccount%3Ftab%3D1"
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
export function signInLocation(routes, signIn, url) {
|
|
51
|
+
const path = routes[signIn.route]?.path;
|
|
52
|
+
if (typeof path !== 'string') {
|
|
53
|
+
throw new Error(`The route table has no "${signIn.route}" route with a static path (the sign-in page)`);
|
|
54
|
+
}
|
|
55
|
+
const next = new URLSearchParams({
|
|
56
|
+
[signIn.nextParam]: `${url.pathname}${url.search}`,
|
|
57
|
+
});
|
|
58
|
+
return `${path}?${next}`;
|
|
59
|
+
}
|
|
60
|
+
/** Whether `pathname` is Solid's server-function endpoint. */
|
|
61
|
+
export function isServerFunctionPath(pathname) {
|
|
62
|
+
return (pathname === SERVER_FUNCTION_PATH ||
|
|
63
|
+
pathname.startsWith(`${SERVER_FUNCTION_PATH}/`));
|
|
64
|
+
}
|
|
65
|
+
/** Hashed build output lives here; anything under it is an asset, never a page. */
|
|
66
|
+
const ASSET_PREFIX = '/assets/';
|
|
67
|
+
/** A last path segment with a file extension (`/favicon.png`): an asset, not a page. */
|
|
68
|
+
const FILE_EXTENSION = /\/[^/]*\.[a-z0-9]+$/i;
|
|
69
|
+
/**
|
|
70
|
+
* Classifies a request path for the Worker and the dev server.
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* ```ts
|
|
74
|
+
* classifyPath(routes, '/nope', 'GET') // { _tag: "NotFoundPage" }
|
|
75
|
+
* classifyPath(routes, '/assets/x.js', 'GET') // { _tag: "NotFoundStatic" }
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
78
|
+
export function classifyPath(routes, pathname, method) {
|
|
79
|
+
if (isServerFunctionPath(pathname))
|
|
80
|
+
return { _tag: 'ServerFunction' };
|
|
81
|
+
const route = pageRoute(routes, pathname);
|
|
82
|
+
if (route)
|
|
83
|
+
return { _tag: 'Page', route };
|
|
84
|
+
const renders = method === 'GET' || method === 'HEAD';
|
|
85
|
+
if (!renders ||
|
|
86
|
+
pathname.startsWith(ASSET_PREFIX) ||
|
|
87
|
+
FILE_EXTENSION.test(pathname)) {
|
|
88
|
+
return { _tag: 'NotFoundStatic' };
|
|
89
|
+
}
|
|
90
|
+
return { _tag: 'NotFoundPage' };
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Solid's render mode for a classified request: streaming only for pages
|
|
94
|
+
* that opt into `ssr: "stream"`; everything else is sent whole (`"async"`).
|
|
95
|
+
*/
|
|
96
|
+
export function renderModeFor(kind) {
|
|
97
|
+
return kind._tag === 'Page' && kind.route.ssr === 'stream'
|
|
98
|
+
? 'stream'
|
|
99
|
+
: 'async';
|
|
100
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { Effect } from 'effect';
|
|
2
|
+
import type { PathKind } from './page-routes.ts';
|
|
3
|
+
import { type RouteTable } from '../logic/route-table.ts';
|
|
4
|
+
/**
|
|
5
|
+
* The HTTP status of a page whose primary data may not exist
|
|
6
|
+
* (`/plans/<unknown-id>` must be a 404, not a 200 with an error panel).
|
|
7
|
+
*
|
|
8
|
+
* Why a probe before render, not the render itself: a streamed page sends
|
|
9
|
+
* its status with the shell, before any `<Query>` has resolved. So the
|
|
10
|
+
* Worker asks a cheap question first: does the page's primary record
|
|
11
|
+
* exist? One indexed read, awaited (I/O, not CPU) before the render; the
|
|
12
|
+
* page still renders normally (its `<Query>` shows the typed not-found
|
|
13
|
+
* error), only the status changes.
|
|
14
|
+
*
|
|
15
|
+
* A route with a probe (the server app's `status`) gets one; every other
|
|
16
|
+
* page is 200. A probe that fails says 200: a wrong 404 would be worse.
|
|
17
|
+
*/
|
|
18
|
+
type Params = Readonly<Record<string, string>>;
|
|
19
|
+
/** Per route tag: does the page's primary record exist? */
|
|
20
|
+
export type StatusProbes<R> = {
|
|
21
|
+
readonly [tag: string]: (params: Params) => Effect.Effect<boolean, never, R>;
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* 404 when `kind` is a page with a probe and its primary record is missing,
|
|
25
|
+
* else 200.
|
|
26
|
+
*
|
|
27
|
+
* @param run - runs a probe over `env` (the app runner's `runWithEnv`)
|
|
28
|
+
*/
|
|
29
|
+
export declare function pageStatus<R>({ kind, url, routes, probes, run, }: {
|
|
30
|
+
kind: PathKind;
|
|
31
|
+
url: string;
|
|
32
|
+
routes: RouteTable;
|
|
33
|
+
probes: StatusProbes<R> | undefined;
|
|
34
|
+
run: (program: Effect.Effect<boolean, never, R>) => Promise<{
|
|
35
|
+
readonly _tag: 'Ok';
|
|
36
|
+
readonly value: boolean;
|
|
37
|
+
} | {
|
|
38
|
+
readonly _tag: 'Err';
|
|
39
|
+
}>;
|
|
40
|
+
}): Promise<200 | 404>;
|
|
41
|
+
export {};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { routeEntryFor } from '../logic/route-table.js';
|
|
2
|
+
/**
|
|
3
|
+
* 404 when `kind` is a page with a probe and its primary record is missing,
|
|
4
|
+
* else 200.
|
|
5
|
+
*
|
|
6
|
+
* @param run - runs a probe over `env` (the app runner's `runWithEnv`)
|
|
7
|
+
*/
|
|
8
|
+
export async function pageStatus({ kind, url, routes, probes, run, }) {
|
|
9
|
+
if (kind._tag !== 'Page')
|
|
10
|
+
return 200;
|
|
11
|
+
const probe = probes?.[kind.route.tag];
|
|
12
|
+
if (probe === undefined)
|
|
13
|
+
return 200;
|
|
14
|
+
const match = routeEntryFor(routes, url);
|
|
15
|
+
const params = 'params' in match.route ? match.route.params : {};
|
|
16
|
+
try {
|
|
17
|
+
const exists = await run(probe(params));
|
|
18
|
+
return exists._tag === 'Ok' && !exists.value ? 404 : 200;
|
|
19
|
+
}
|
|
20
|
+
catch {
|
|
21
|
+
return 200;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import type { PathKind } from './page-routes.ts';
|
|
2
|
+
import type { D1Binding } from '../logic/resources.ts';
|
|
3
|
+
import { type FormFlash } from '../server/form-actions.ts';
|
|
4
|
+
import { type Sessions, type SessionSlot } from '../server/session.ts';
|
|
5
|
+
/**
|
|
6
|
+
* What the Worker (and the dev middleware) does for a request before Solid
|
|
7
|
+
* sees it, and to the page response after:
|
|
8
|
+
*
|
|
9
|
+
* - puts the session on `locals.session` as a lazy lookup: no cookie,
|
|
10
|
+
* `SignedOut` and no D1; with a cookie, D1 is read only when a program
|
|
11
|
+
* reads the `Session` service;
|
|
12
|
+
* - for `auth: "required"` routes: resolves it now, and with no session
|
|
13
|
+
* redirects to the sign-in page with `?next=`, before any render;
|
|
14
|
+
* - for page renders: reads Solid's no-JS flash cookie (a form post's
|
|
15
|
+
* outcome) onto `locals.formFlash` and clears it on the response;
|
|
16
|
+
* - sets the page's `Cache-Control`: SSR HTML carries a per-request CSP
|
|
17
|
+
* nonce and may carry a session or a flash, so it is never shared-cached.
|
|
18
|
+
* With Workers Cache on, a response without `Cache-Control` would be
|
|
19
|
+
* cached by the edge's heuristics, so every page says so explicitly.
|
|
20
|
+
*
|
|
21
|
+
* One function for both surfaces, so dev and workerd agree.
|
|
22
|
+
*/
|
|
23
|
+
/** `Cache-Control` of SSR pages (never shared: nonce, session, flash). */
|
|
24
|
+
export declare const PAGE_CACHE_CONTROL: {
|
|
25
|
+
/** A session cookie, or replaying a form outcome: not even the browser keeps it. */
|
|
26
|
+
readonly PERSONAL: 'private, no-store';
|
|
27
|
+
/** Anonymous: the browser may keep it for back/forward but revalidates. */
|
|
28
|
+
readonly ANONYMOUS: 'private, no-cache';
|
|
29
|
+
};
|
|
30
|
+
/** What the request carries into the render. */
|
|
31
|
+
export type Prelude =
|
|
32
|
+
/** Answer with this instead of rendering (an auth redirect). */
|
|
33
|
+
{
|
|
34
|
+
readonly _tag: 'Respond';
|
|
35
|
+
readonly response: Response;
|
|
36
|
+
} | {
|
|
37
|
+
readonly _tag: 'Render';
|
|
38
|
+
/** `locals.session`: resolved or a lazy lookup. */
|
|
39
|
+
readonly session: SessionSlot;
|
|
40
|
+
/** The form outcome to replay on this page, if any. */
|
|
41
|
+
readonly formFlash: FormFlash | undefined;
|
|
42
|
+
/** The request had a flash cookie: clear it on the response. */
|
|
43
|
+
readonly clearFlash: boolean;
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* Runs before render / dispatch for a classified request.
|
|
47
|
+
*
|
|
48
|
+
* @param sessions - the app's sessions (none: every request is signed out)
|
|
49
|
+
* @param db - the sessions database, called only when the lookup runs
|
|
50
|
+
* @param signInLocation - where an unauthenticated request for a URL goes
|
|
51
|
+
*/
|
|
52
|
+
export declare function prepareRequest({ request, kind, nowMs, sessions, db, signInLocation, }: {
|
|
53
|
+
request: Request;
|
|
54
|
+
kind: PathKind;
|
|
55
|
+
nowMs: number;
|
|
56
|
+
sessions: Sessions<unknown> | undefined;
|
|
57
|
+
db: () => D1Binding;
|
|
58
|
+
signInLocation: (url: URL) => string;
|
|
59
|
+
}): Promise<Prelude>;
|
|
60
|
+
/** The redirect for an unauthenticated request to an `auth: "required"` page. */
|
|
61
|
+
export declare function signInRedirect(location: string): Response;
|
|
62
|
+
/** The page response's cache header and flash clearing (mutates the fresh response's headers). */
|
|
63
|
+
export declare function finishPage(response: Response, prelude: Prelude): Response;
|
|
64
|
+
/**
|
|
65
|
+
* Safety net for anything else the Worker answers: a response with no
|
|
66
|
+
* `Cache-Control` gets `no-store`, so Workers Cache never applies its
|
|
67
|
+
* heuristic TTL to it.
|
|
68
|
+
*/
|
|
69
|
+
export declare function ensureCacheControl(response: Response): Response;
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { BASE_HEADERS } from './security.js';
|
|
2
|
+
import { clearFormFlash, readFormFlash, } from '../server/form-actions.js';
|
|
3
|
+
import { resolveSlot, SIGNED_OUT, } from '../server/session.js';
|
|
4
|
+
/**
|
|
5
|
+
* What the Worker (and the dev middleware) does for a request before Solid
|
|
6
|
+
* sees it, and to the page response after:
|
|
7
|
+
*
|
|
8
|
+
* - puts the session on `locals.session` as a lazy lookup: no cookie,
|
|
9
|
+
* `SignedOut` and no D1; with a cookie, D1 is read only when a program
|
|
10
|
+
* reads the `Session` service;
|
|
11
|
+
* - for `auth: "required"` routes: resolves it now, and with no session
|
|
12
|
+
* redirects to the sign-in page with `?next=`, before any render;
|
|
13
|
+
* - for page renders: reads Solid's no-JS flash cookie (a form post's
|
|
14
|
+
* outcome) onto `locals.formFlash` and clears it on the response;
|
|
15
|
+
* - sets the page's `Cache-Control`: SSR HTML carries a per-request CSP
|
|
16
|
+
* nonce and may carry a session or a flash, so it is never shared-cached.
|
|
17
|
+
* With Workers Cache on, a response without `Cache-Control` would be
|
|
18
|
+
* cached by the edge's heuristics, so every page says so explicitly.
|
|
19
|
+
*
|
|
20
|
+
* One function for both surfaces, so dev and workerd agree.
|
|
21
|
+
*/
|
|
22
|
+
/** `Cache-Control` of SSR pages (never shared: nonce, session, flash). */
|
|
23
|
+
export const PAGE_CACHE_CONTROL = {
|
|
24
|
+
/** A session cookie, or replaying a form outcome: not even the browser keeps it. */
|
|
25
|
+
PERSONAL: 'private, no-store',
|
|
26
|
+
/** Anonymous: the browser may keep it for back/forward but revalidates. */
|
|
27
|
+
ANONYMOUS: 'private, no-cache',
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* Runs before render / dispatch for a classified request.
|
|
31
|
+
*
|
|
32
|
+
* @param sessions - the app's sessions (none: every request is signed out)
|
|
33
|
+
* @param db - the sessions database, called only when the lookup runs
|
|
34
|
+
* @param signInLocation - where an unauthenticated request for a URL goes
|
|
35
|
+
*/
|
|
36
|
+
export async function prepareRequest({ request, kind, nowMs, sessions, db, signInLocation, }) {
|
|
37
|
+
if (kind._tag === 'NotFoundStatic') {
|
|
38
|
+
return {
|
|
39
|
+
_tag: 'Render',
|
|
40
|
+
session: SIGNED_OUT,
|
|
41
|
+
formFlash: undefined,
|
|
42
|
+
clearFlash: false,
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
let session = sessions
|
|
46
|
+
? sessions.lazy(request, db, nowMs)
|
|
47
|
+
: SIGNED_OUT;
|
|
48
|
+
if (kind._tag === 'Page' && kind.route.auth === 'required') {
|
|
49
|
+
session = await resolveSlot(session);
|
|
50
|
+
if (session._tag === 'SignedOut') {
|
|
51
|
+
return {
|
|
52
|
+
_tag: 'Respond',
|
|
53
|
+
response: signInRedirect(signInLocation(new URL(request.url))),
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
if (kind._tag === 'ServerFunction') {
|
|
58
|
+
return {
|
|
59
|
+
_tag: 'Render',
|
|
60
|
+
session,
|
|
61
|
+
formFlash: undefined,
|
|
62
|
+
clearFlash: false,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
const flash = await readFormFlash(request);
|
|
66
|
+
return {
|
|
67
|
+
_tag: 'Render',
|
|
68
|
+
session,
|
|
69
|
+
formFlash: flash.flash,
|
|
70
|
+
clearFlash: flash.present,
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
/** The redirect for an unauthenticated request to an `auth: "required"` page. */
|
|
74
|
+
export function signInRedirect(location) {
|
|
75
|
+
return new Response(null, {
|
|
76
|
+
status: 303,
|
|
77
|
+
headers: {
|
|
78
|
+
...BASE_HEADERS,
|
|
79
|
+
Location: location,
|
|
80
|
+
'Cache-Control': 'no-store',
|
|
81
|
+
},
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
/** The page response's cache header and flash clearing (mutates the fresh response's headers). */
|
|
85
|
+
export function finishPage(response, prelude) {
|
|
86
|
+
if (prelude._tag === 'Respond')
|
|
87
|
+
return response;
|
|
88
|
+
// Judged by the cookie, not the lookup: a page that never read the
|
|
89
|
+
// session still may not be kept when a session cookie came with it.
|
|
90
|
+
const personal = prelude.session._tag !== 'SignedOut' || prelude.clearFlash;
|
|
91
|
+
response.headers.set('Cache-Control', personal ? PAGE_CACHE_CONTROL.PERSONAL : PAGE_CACHE_CONTROL.ANONYMOUS);
|
|
92
|
+
if (prelude.clearFlash)
|
|
93
|
+
clearFormFlash(response);
|
|
94
|
+
return response;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Safety net for anything else the Worker answers: a response with no
|
|
98
|
+
* `Cache-Control` gets `no-store`, so Workers Cache never applies its
|
|
99
|
+
* heuristic TTL to it.
|
|
100
|
+
*/
|
|
101
|
+
export function ensureCacheControl(response) {
|
|
102
|
+
if (response.headers.has('Cache-Control') || response.status === 101)
|
|
103
|
+
return response;
|
|
104
|
+
const copy = new Response(response.body, response);
|
|
105
|
+
copy.headers.set('Cache-Control', 'no-store');
|
|
106
|
+
return copy;
|
|
107
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Security headers for the Worker's responses and the prerendered pages'
|
|
3
|
+
* `_headers`. Pure functions: `createWorker` and `ui-kit prerender` share
|
|
4
|
+
* them.
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Third-party hosts a page may load scripts from and connect to. The kit's
|
|
8
|
+
* default is Cloudflare Web Analytics only (the beacon script and where it
|
|
9
|
+
* reports). An app lists any other host in one place (its server app's
|
|
10
|
+
* `cspHosts`), and only with the owner's approval.
|
|
11
|
+
*/
|
|
12
|
+
export interface CspHosts {
|
|
13
|
+
readonly script: readonly string[];
|
|
14
|
+
readonly connect: readonly string[];
|
|
15
|
+
}
|
|
16
|
+
/** Cloudflare Web Analytics: the only third party by default. */
|
|
17
|
+
export declare const DEFAULT_CSP_HOSTS: CspHosts;
|
|
18
|
+
/** The asset file a prerendered path is written to (`/docs` → `docs.html`). */
|
|
19
|
+
export declare function prerenderedFile(path: string): string;
|
|
20
|
+
/** A fresh base64 nonce (128 bits) for one request's CSP. */
|
|
21
|
+
export declare function makeNonce(): string;
|
|
22
|
+
/** What a page's inline scripts and styles are allowed by. */
|
|
23
|
+
export type InlinePolicy =
|
|
24
|
+
/** A per-request SSR page: one nonce for `<script>` and `<style>`. */
|
|
25
|
+
{
|
|
26
|
+
readonly _tag: 'Nonce';
|
|
27
|
+
readonly nonce: string;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* A prerendered page: its inline scripts' hashes. Styles there are
|
|
31
|
+
* `'unsafe-inline'`: CodeMirror injects `<style>` elements on
|
|
32
|
+
* a prerendered page and a static page has no nonce to give it.
|
|
33
|
+
*/
|
|
34
|
+
| {
|
|
35
|
+
readonly _tag: 'Hashes';
|
|
36
|
+
readonly scriptHashes: readonly string[];
|
|
37
|
+
};
|
|
38
|
+
/**
|
|
39
|
+
* The CSP for one page. Never `'unsafe-inline'` for scripts. On SSR pages
|
|
40
|
+
* `style-src` takes the nonce and `style-src-attr 'unsafe-inline'` allows
|
|
41
|
+
* the UI kit's `style` attributes (BarChart widths), which nonces and
|
|
42
|
+
* hashes can't cover.
|
|
43
|
+
*/
|
|
44
|
+
export declare function contentSecurityPolicy(policy: InlinePolicy, hosts?: CspHosts): string;
|
|
45
|
+
/**
|
|
46
|
+
* Hashed build output (`/assets/<name>-<hash>.js|css`): a new build gets
|
|
47
|
+
* new names, so browsers and the edge may keep these forever.
|
|
48
|
+
*/
|
|
49
|
+
export declare const IMMUTABLE_ASSET_HEADERS: Readonly<Record<string, string>>;
|
|
50
|
+
/** `sitemap.xml` / `robots.txt` (rebuilt each deploy): an hour at the edge and in browsers. */
|
|
51
|
+
export declare const SEO_FILE_HEADERS: Readonly<Record<string, string>>;
|
|
52
|
+
/** Headers every response carries, Worker or static asset. */
|
|
53
|
+
export declare const BASE_HEADERS: Readonly<Record<string, string>>;
|
|
54
|
+
/**
|
|
55
|
+
* The bodies of the HTML's inline, executable `<script>` elements (no
|
|
56
|
+
* `src`, not a JSON data block), which a hash-based CSP must list.
|
|
57
|
+
*/
|
|
58
|
+
export declare function inlineScripts(html: string): string[];
|
|
59
|
+
/** `'sha256-…'` CSP sources for each distinct inline script in `html`. */
|
|
60
|
+
export declare function scriptHashes(html: string): Promise<string[]>;
|
|
61
|
+
/** Copies `response` with `headers` added (Worker responses' headers may be immutable). */
|
|
62
|
+
export declare function withHeaders(response: Response, headers: Readonly<Record<string, string>>): Response;
|
|
63
|
+
/** The same response with another status (the body still streams). */
|
|
64
|
+
export declare function withStatus(response: Response, status: number): Response;
|
|
65
|
+
/**
|
|
66
|
+
* Response headers the deployed Worker drops: Solid's runtime writes
|
|
67
|
+
* `Server-Timing` (`solid-shell`, `solid-boundary`, and `solid-invocation`
|
|
68
|
+
* naming each server function by id) for the dev performance tracks.
|
|
69
|
+
* Production shouldn't tell visitors its server function ids and timings.
|
|
70
|
+
* `vite dev` keeps them.
|
|
71
|
+
*/
|
|
72
|
+
export declare const PRODUCTION_STRIPPED_HEADERS: readonly string[];
|
|
73
|
+
/** Drops {@link PRODUCTION_STRIPPED_HEADERS} from a (mutable) response's headers. */
|
|
74
|
+
export declare function stripDevHeaders(response: Response): Response;
|
|
75
|
+
/** The Worker's short static 404 (unknown asset paths, non-GET): no render, a short static body, the base headers. */
|
|
76
|
+
export declare function notFoundResponse(): Response;
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Security headers for the Worker's responses and the prerendered pages'
|
|
3
|
+
* `_headers`. Pure functions: `createWorker` and `ui-kit prerender` share
|
|
4
|
+
* them.
|
|
5
|
+
*/
|
|
6
|
+
/** Cloudflare Web Analytics: the only third party by default. */
|
|
7
|
+
export const DEFAULT_CSP_HOSTS = {
|
|
8
|
+
script: ['https://static.cloudflareinsights.com'],
|
|
9
|
+
connect: ['https://cloudflareinsights.com'],
|
|
10
|
+
};
|
|
11
|
+
/** The asset file a prerendered path is written to (`/docs` → `docs.html`). */
|
|
12
|
+
export function prerenderedFile(path) {
|
|
13
|
+
return `${path.slice(1)}.html`;
|
|
14
|
+
}
|
|
15
|
+
/** A fresh base64 nonce (128 bits) for one request's CSP. */
|
|
16
|
+
export function makeNonce() {
|
|
17
|
+
const bytes = crypto.getRandomValues(new Uint8Array(16));
|
|
18
|
+
let binary = '';
|
|
19
|
+
for (const byte of bytes)
|
|
20
|
+
binary += String.fromCharCode(byte);
|
|
21
|
+
return btoa(binary);
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* The CSP for one page. Never `'unsafe-inline'` for scripts. On SSR pages
|
|
25
|
+
* `style-src` takes the nonce and `style-src-attr 'unsafe-inline'` allows
|
|
26
|
+
* the UI kit's `style` attributes (BarChart widths), which nonces and
|
|
27
|
+
* hashes can't cover.
|
|
28
|
+
*/
|
|
29
|
+
export function contentSecurityPolicy(policy, hosts = DEFAULT_CSP_HOSTS) {
|
|
30
|
+
const scriptSources = policy._tag === 'Nonce'
|
|
31
|
+
? [`'nonce-${policy.nonce}'`]
|
|
32
|
+
: policy.scriptHashes;
|
|
33
|
+
const styleDirectives = policy._tag === 'Nonce'
|
|
34
|
+
? [
|
|
35
|
+
`style-src 'self' 'nonce-${policy.nonce}'`,
|
|
36
|
+
"style-src-attr 'unsafe-inline'",
|
|
37
|
+
]
|
|
38
|
+
: ["style-src 'self' 'unsafe-inline'"];
|
|
39
|
+
return [
|
|
40
|
+
"default-src 'self'",
|
|
41
|
+
"base-uri 'none'",
|
|
42
|
+
"object-src 'none'",
|
|
43
|
+
"frame-ancestors 'none'",
|
|
44
|
+
"form-action 'self'",
|
|
45
|
+
["script-src 'self'", ...scriptSources, ...hosts.script].join(' '),
|
|
46
|
+
...styleDirectives,
|
|
47
|
+
"img-src 'self' data:",
|
|
48
|
+
"font-src 'self'",
|
|
49
|
+
["connect-src 'self'", ...hosts.connect].join(' '),
|
|
50
|
+
].join('; ');
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Hashed build output (`/assets/<name>-<hash>.js|css`): a new build gets
|
|
54
|
+
* new names, so browsers and the edge may keep these forever.
|
|
55
|
+
*/
|
|
56
|
+
export const IMMUTABLE_ASSET_HEADERS = {
|
|
57
|
+
'Cache-Control': 'public, max-age=31536000, immutable',
|
|
58
|
+
};
|
|
59
|
+
/** `sitemap.xml` / `robots.txt` (rebuilt each deploy): an hour at the edge and in browsers. */
|
|
60
|
+
export const SEO_FILE_HEADERS = {
|
|
61
|
+
'Cache-Control': 'public, max-age=3600',
|
|
62
|
+
};
|
|
63
|
+
/** Headers every response carries, Worker or static asset. */
|
|
64
|
+
export const BASE_HEADERS = {
|
|
65
|
+
'Referrer-Policy': 'no-referrer',
|
|
66
|
+
'X-Content-Type-Options': 'nosniff',
|
|
67
|
+
};
|
|
68
|
+
const INLINE_SCRIPT = /<script\b([^>]*)>([\s\S]*?)<\/script>/gi;
|
|
69
|
+
/**
|
|
70
|
+
* The bodies of the HTML's inline, executable `<script>` elements (no
|
|
71
|
+
* `src`, not a JSON data block), which a hash-based CSP must list.
|
|
72
|
+
*/
|
|
73
|
+
export function inlineScripts(html) {
|
|
74
|
+
const scripts = [];
|
|
75
|
+
for (const match of html.matchAll(INLINE_SCRIPT)) {
|
|
76
|
+
const attributes = match[1] ?? '';
|
|
77
|
+
const body = match[2] ?? '';
|
|
78
|
+
if (/\bsrc\s*=/i.test(attributes))
|
|
79
|
+
continue;
|
|
80
|
+
if (/\btype\s*=\s*["']?application\/(ld\+)?json/i.test(attributes))
|
|
81
|
+
continue;
|
|
82
|
+
if (body.length === 0)
|
|
83
|
+
continue;
|
|
84
|
+
scripts.push(body);
|
|
85
|
+
}
|
|
86
|
+
return scripts;
|
|
87
|
+
}
|
|
88
|
+
/** `'sha256-…'` CSP sources for each distinct inline script in `html`. */
|
|
89
|
+
export async function scriptHashes(html) {
|
|
90
|
+
const hashes = new Set();
|
|
91
|
+
for (const body of inlineScripts(html)) {
|
|
92
|
+
const digest = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(body));
|
|
93
|
+
let binary = '';
|
|
94
|
+
for (const byte of new Uint8Array(digest))
|
|
95
|
+
binary += String.fromCharCode(byte);
|
|
96
|
+
hashes.add(`'sha256-${btoa(binary)}'`);
|
|
97
|
+
}
|
|
98
|
+
return [...hashes];
|
|
99
|
+
}
|
|
100
|
+
/** Copies `response` with `headers` added (Worker responses' headers may be immutable). */
|
|
101
|
+
export function withHeaders(response, headers) {
|
|
102
|
+
const copy = new Response(response.body, response);
|
|
103
|
+
for (const [name, value] of Object.entries(headers)) {
|
|
104
|
+
copy.headers.set(name, value);
|
|
105
|
+
}
|
|
106
|
+
return copy;
|
|
107
|
+
}
|
|
108
|
+
/** The same response with another status (the body still streams). */
|
|
109
|
+
export function withStatus(response, status) {
|
|
110
|
+
return new Response(response.body, { status, headers: response.headers });
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Response headers the deployed Worker drops: Solid's runtime writes
|
|
114
|
+
* `Server-Timing` (`solid-shell`, `solid-boundary`, and `solid-invocation`
|
|
115
|
+
* naming each server function by id) for the dev performance tracks.
|
|
116
|
+
* Production shouldn't tell visitors its server function ids and timings.
|
|
117
|
+
* `vite dev` keeps them.
|
|
118
|
+
*/
|
|
119
|
+
export const PRODUCTION_STRIPPED_HEADERS = ['Server-Timing'];
|
|
120
|
+
/** Drops {@link PRODUCTION_STRIPPED_HEADERS} from a (mutable) response's headers. */
|
|
121
|
+
export function stripDevHeaders(response) {
|
|
122
|
+
for (const name of PRODUCTION_STRIPPED_HEADERS)
|
|
123
|
+
response.headers.delete(name);
|
|
124
|
+
return response;
|
|
125
|
+
}
|
|
126
|
+
/** The Worker's short static 404 (unknown asset paths, non-GET): no render, a short static body, the base headers. */
|
|
127
|
+
export function notFoundResponse() {
|
|
128
|
+
return new Response('<!doctype html><meta charset="utf-8"><title>Not found</title><h1>Not found</h1><p><a href="/">Back to the home page</a></p>', {
|
|
129
|
+
status: 404,
|
|
130
|
+
headers: {
|
|
131
|
+
...BASE_HEADERS,
|
|
132
|
+
'Content-Type': 'text/html; charset=utf-8',
|
|
133
|
+
'Content-Security-Policy': "default-src 'none'; base-uri 'none'; frame-ancestors 'none'",
|
|
134
|
+
'Cache-Control': 'public, max-age=300',
|
|
135
|
+
},
|
|
136
|
+
});
|
|
137
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { type PathKind, type SignInSpec } from './page-routes.ts';
|
|
2
|
+
import { type StatusProbes } from './page-status.ts';
|
|
3
|
+
import { type Prelude } from './request-prelude.ts';
|
|
4
|
+
import type { CspHosts } from './security.ts';
|
|
5
|
+
import { type ResourcesDecl } from '../logic/resources.ts';
|
|
6
|
+
import type { RouteTable } from '../logic/route-table.ts';
|
|
7
|
+
import type { CallServices, Runner } from '../server/run.ts';
|
|
8
|
+
import type { Sessions } from '../server/session.ts';
|
|
9
|
+
/**
|
|
10
|
+
* The app's server side as one value (`src/server/layer.ts`): the tables
|
|
11
|
+
* the Worker reads, the runner, sessions, page status probes and jobs.
|
|
12
|
+
* `createWorker` serves it in workerd, and the kit's Vite plugin serves the
|
|
13
|
+
* same value under `vite dev`, so both answer every request the same way.
|
|
14
|
+
*/
|
|
15
|
+
export interface ServerApp<R> {
|
|
16
|
+
readonly routes: RouteTable;
|
|
17
|
+
readonly resources: ResourcesDecl;
|
|
18
|
+
readonly runner: Runner<R>;
|
|
19
|
+
/** The app's sessions (`createSessions`); none: every request is signed out. */
|
|
20
|
+
readonly sessions?: Sessions<unknown>;
|
|
21
|
+
/** The sign-in page for `auth: "required"` redirects (default: route `SignIn`, `?next=`). */
|
|
22
|
+
readonly signIn?: SignInSpec;
|
|
23
|
+
/** Page status probes by route tag (a missing primary record makes the page a 404). */
|
|
24
|
+
readonly status?: StatusProbes<R | CallServices>;
|
|
25
|
+
/** Third-party hosts for the CSP (default: Cloudflare Web Analytics only). */
|
|
26
|
+
readonly cspHosts?: CspHosts;
|
|
27
|
+
/** Cron jobs (`resources.crons`), each run with the Worker `env`. */
|
|
28
|
+
readonly jobs?: readonly {
|
|
29
|
+
readonly cron: string;
|
|
30
|
+
readonly run: (env: unknown) => Promise<void>;
|
|
31
|
+
}[];
|
|
32
|
+
}
|
|
33
|
+
/** Declares the app's server side (returns it unchanged, typed). */
|
|
34
|
+
export declare function defineServerApp<R>(app: ServerApp<R>): ServerApp<R>;
|
|
35
|
+
/** Expired sessions removed per cleanup batch. */
|
|
36
|
+
export declare const SESSION_CLEANUP_BATCH = 500;
|
|
37
|
+
/** Most cleanup batches per cron run. */
|
|
38
|
+
export declare const SESSION_CLEANUP_MAX_BATCHES = 20;
|
|
39
|
+
/** What a request gets before its render (shared by the Worker and the dev middleware). */
|
|
40
|
+
export interface PreparedRequest {
|
|
41
|
+
readonly kind: PathKind;
|
|
42
|
+
readonly prelude: Prelude;
|
|
43
|
+
readonly status: 200 | 404;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Classifies a request, runs the prelude (session, auth redirect, form
|
|
47
|
+
* flash) and the page status probe.
|
|
48
|
+
*/
|
|
49
|
+
export declare function prepareAppRequest<R>(app: ServerApp<R>, request: Request, env: unknown): Promise<PreparedRequest>;
|
|
50
|
+
/**
|
|
51
|
+
* The Worker's `scheduled`: the sessions cleanup on its cron (expired
|
|
52
|
+
* sessions in bounded batches) and the app's own jobs on theirs.
|
|
53
|
+
*/
|
|
54
|
+
export declare function runScheduled<R>(app: ServerApp<R>, cron: string, env: unknown): Promise<void>;
|