prunelle 0.1.1 → 0.7.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/LICENSE +21 -0
- package/llms.txt +137 -40
- package/package.json +11 -2
- package/src/a11y/declaration.ts +369 -0
- package/src/a11y/index.ts +466 -0
- package/src/a11y/rgaa-map.ts +230 -0
- package/src/attrs.ts +11 -3
- package/src/build/bundle.ts +88 -40
- package/src/build/byte-size.ts +11 -0
- package/src/build/client-entries.ts +3 -2
- package/src/build/client-plugin.ts +11 -5
- package/src/build/client-prune.ts +56 -6
- package/src/build/compile.ts +13 -10
- package/src/build/ecoindex.ts +230 -0
- package/src/build/files.ts +6 -0
- package/src/build/first-load.ts +83 -11
- package/src/build/graph.ts +29 -39
- package/src/build/index.ts +218 -113
- package/src/build/island-promote.ts +229 -0
- package/src/build/islands.ts +11 -203
- package/src/build/manifest.ts +56 -116
- package/src/build/output.ts +2 -2
- package/src/build/plugin-host.ts +113 -41
- package/src/build/precompress.ts +16 -3
- package/src/build/prerender.ts +173 -38
- package/src/build/reach.ts +136 -0
- package/src/build/render-rules.ts +2 -2
- package/src/build/report.ts +42 -5
- package/src/build/resolve.ts +8 -6
- package/src/build/routes.ts +175 -10
- package/src/build/static-headers.ts +93 -0
- package/src/build/static.ts +139 -39
- package/src/build/status-pages.ts +127 -0
- package/src/build/style-bundles.ts +526 -0
- package/src/cli/check-forms.ts +610 -0
- package/src/cli/check-imports.ts +25 -0
- package/src/cli/check-links.ts +632 -0
- package/src/cli/check-security.ts +64 -0
- package/src/cli/check.ts +107 -26
- package/src/cli/commands.ts +120 -11
- package/src/cli/docs.ts +403 -0
- package/src/cli/inspect.ts +105 -15
- package/src/cli/mcp-tools.ts +492 -0
- package/src/cli/mcp.ts +205 -0
- package/src/cli/new.ts +16 -5
- package/src/cli/render.ts +416 -0
- package/src/cli/upgrade-names.ts +73 -13
- package/src/cli/upgrade.ts +156 -18
- package/src/compiler/analyze.ts +68 -14
- package/src/compiler/browser-globals.ts +140 -17
- package/src/compiler/cacheability.ts +636 -0
- package/src/compiler/compliance-eco.ts +71 -0
- package/src/compiler/compliance-rgaa.ts +614 -0
- package/src/compiler/compliance-urls.ts +145 -0
- package/src/compiler/compliance.ts +355 -351
- package/src/compiler/data-keys.ts +199 -192
- package/src/compiler/dev-source.ts +87 -0
- package/src/compiler/dom-attrs.ts +138 -65
- package/src/compiler/dom.ts +517 -442
- package/src/compiler/expr-context.ts +167 -57
- package/src/compiler/foreign-imports.ts +88 -0
- package/src/compiler/mistakes.ts +23 -2
- package/src/compiler/props-body.ts +52 -0
- package/src/compiler/props.ts +87 -36
- package/src/compiler/scope.ts +400 -333
- package/src/compiler/server-only.ts +190 -151
- package/src/compiler/shared.ts +42 -14
- package/src/compiler/signal-reads.ts +6 -2
- package/src/compiler/ssr-prunelle-tags.ts +27 -14
- package/src/compiler/ssr.ts +220 -89
- package/src/context.ts +34 -0
- package/src/dev/error-log.ts +177 -0
- package/src/dev/host-check.ts +26 -0
- package/src/dev/index.ts +91 -34
- package/src/dev/open-editor.ts +208 -0
- package/src/dev/protocol.ts +3 -0
- package/src/dev/reload.ts +5 -3
- package/src/dev/supervisor.ts +50 -25
- package/src/dev/watchers.ts +74 -13
- package/src/dom/action.ts +47 -21
- package/src/dom/attrs.ts +15 -3
- package/src/dom/extras.ts +29 -14
- package/src/dom/index.ts +39 -10
- package/src/dom/island.ts +54 -35
- package/src/dom/loop.ts +33 -23
- package/src/eco/declaration.ts +257 -0
- package/src/eco/index.ts +65 -0
- package/src/index.ts +2 -2
- package/src/jsx-runtime.ts +2 -2
- package/src/plugins/config.ts +8 -7
- package/src/plugins/context.ts +16 -1
- package/src/plugins/index.ts +2 -2
- package/src/plugins/load.ts +30 -10
- package/src/plugins/order.ts +38 -1
- package/src/plugins/public.ts +6 -3
- package/src/plugins/services.ts +84 -0
- package/src/plugins/types.ts +51 -2
- package/src/reactivity/graph.ts +10 -0
- package/src/reactivity/index.ts +24 -16
- package/src/reactivity/selector.ts +31 -4
- package/src/reactivity/store.ts +45 -13
- package/src/server/action-flash.ts +158 -0
- package/src/server/action-protect.ts +199 -0
- package/src/server/action-registry.ts +1 -1
- package/src/server/action.ts +56 -117
- package/src/server/app-secret.ts +63 -0
- package/src/server/assets.ts +34 -2
- package/src/server/compress.ts +96 -10
- package/src/server/consent.ts +59 -22
- package/src/server/cookies.ts +16 -2
- package/src/server/document.ts +25 -10
- package/src/server/env.ts +14 -0
- package/src/server/errors.ts +305 -65
- package/src/server/form-schema.ts +343 -0
- package/src/server/health.ts +275 -0
- package/src/server/http.ts +177 -38
- package/src/server/index.ts +264 -272
- package/src/server/loader.ts +11 -2
- package/src/server/param-matchers.ts +112 -0
- package/src/server/plugin-host.ts +125 -88
- package/src/server/public.ts +6 -5
- package/src/server/rate-limit.ts +102 -36
- package/src/server/request-body.ts +1 -1
- package/src/server/route-cache.ts +445 -0
- package/src/server/route-table.ts +344 -9
- package/src/server/router.ts +125 -40
- package/src/server/routing.ts +112 -63
- package/src/server/sanitize-html.ts +309 -0
- package/src/server/security.ts +277 -50
- package/src/server/serve-handlers.ts +31 -7
- package/src/server/start.ts +60 -16
- package/src/server/static-dir.ts +87 -11
- package/src/server/stream.ts +68 -27
- package/src/ssr/consent-banner.ts +58 -0
- package/src/ssr/context.ts +105 -7
- package/src/ssr/index.ts +148 -3
- package/src/ssr/navigation.ts +2 -2
- package/src/ssr/region.ts +31 -19
- package/src/style.ts +32 -1
- package/src/test/browser.ts +28 -13
- package/src/test/index.ts +97 -12
- package/src/ts-plugin/diagnostics.ts +505 -0
- package/src/ts-plugin/fixes.ts +364 -0
- package/src/ts-plugin/index.ts +194 -0
- package/templates/_shared/AGENTS.md +2 -2
- package/templates/_shared/gitignore +5 -0
- package/templates/_shared/mcp.json +11 -0
- package/templates/_shared/src/routes/legal.tsx +15 -1
- package/templates/_shared/tsconfig.json +5 -0
- package/templates/app/.env.example +4 -0
- package/templates/app/package.json +2 -1
- package/templates/app/prunelle.config.ts +2 -0
- package/templates/app/src/auth.ts +4 -6
- package/templates/app/src/mail.ts +4 -0
- package/templates/app/src/routes/_layout.tsx +9 -1
- package/templates/app/src/routes/dashboard/index.tsx +42 -8
- package/templates/app/src/routes/login.tsx +49 -30
- package/templates/app/src/routes/privacy.tsx +25 -0
- package/templates/app/src/styles/app.css +8 -1
- package/templates/app/tests/app.test.ts +44 -3
- package/templates/blog/package.json +1 -1
- package/templates/blog/prunelle.config.ts +1 -1
- package/templates/blog/src/routes/_layout.tsx +6 -1
- package/templates/blog/src/routes/index.tsx +5 -1
- package/templates/blog/src/styles/app.css +6 -1
- package/templates/fullstack/package.json +1 -1
- package/templates/fullstack/src/components/GreetForm.tsx +2 -1
- package/templates/fullstack/src/routes/_layout.tsx +4 -0
- package/templates/fullstack/src/styles/app.css +11 -1
- package/templates/minimal/package.json +1 -1
- package/templates/minimal/src/routes/_layout.tsx +4 -0
- package/templates/minimal/src/styles/app.css +11 -1
- package/ts-plugin.cjs +4277 -0
package/src/server/errors.ts
CHANGED
|
@@ -1,34 +1,47 @@
|
|
|
1
|
-
// Errors
|
|
1
|
+
// Errors and status pages: `onError` hooks, nested `_404`/`_error`/`_401`/`_403`/`_429`/`_503` lookup, and HTML `ctx.deny()` rendering.
|
|
2
2
|
|
|
3
|
-
import type { BuildManifest } from '../build/index'
|
|
3
|
+
import type { BuildManifest, StatusPageKind, StatusPageManifestEntry } from '../build/index'
|
|
4
4
|
import type { Html } from '../jsx-runtime'
|
|
5
5
|
import type { PrunellePlugin, ServerErrorInfo } from '../plugins/index'
|
|
6
6
|
import type { DocumentOptions } from '../ssr/index'
|
|
7
7
|
import type { CookieStore } from './cookies'
|
|
8
8
|
import type { DocumentHooks, HtmlHeaders } from './document'
|
|
9
|
-
import type { RequestContext } from './http'
|
|
9
|
+
import type { DeniedInfo, RequestContext } from './http'
|
|
10
|
+
import type { ParamMatcherMap } from './param-matchers'
|
|
10
11
|
import { createRenderContext, runWithRenderContext } from '../ssr/context'
|
|
11
12
|
import { raw, renderDocument, ssrChild } from '../ssr/index'
|
|
12
13
|
import { addedInlineStyleHashes, headTagStyleHashes, mergeStyleHashes, resolveRenderedIslands, revivePreload } from './document'
|
|
13
14
|
import { wantsJson } from './http'
|
|
15
|
+
import { BUILTIN_PARAM_MATCHERS } from './param-matchers'
|
|
16
|
+
import { decodeRequestPath, pathPrefix } from './request-path'
|
|
17
|
+
import { Router } from './router'
|
|
14
18
|
import { styleHashesOf } from './security'
|
|
15
19
|
|
|
16
|
-
export type NotifyError = (error: unknown, request: Request, source: ServerErrorInfo['source']) => void
|
|
20
|
+
export type NotifyError = (error: unknown, request: Request, source: ServerErrorInfo['source'], requestId?: string) => void
|
|
17
21
|
|
|
18
22
|
/** The text of an error a dev response shows: its stack, which starts with its message. */
|
|
19
23
|
export const errorDetail = (error: unknown): string => error instanceof Error ? error.stack ?? error.message : String(error)
|
|
20
24
|
|
|
21
25
|
const errorMessage = (error: unknown): string => error instanceof Error ? error.message : String(error)
|
|
22
26
|
|
|
27
|
+
const HTML_ESCAPE_RE = /[&<>"']/g
|
|
28
|
+
const HTML_ESCAPES: Record<string, string> = { '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }
|
|
29
|
+
const escapeHtml = (value: string): string => value.replace(HTML_ESCAPE_RE, ch => HTML_ESCAPES[ch]!)
|
|
30
|
+
|
|
31
|
+
const STATUS_KINDS = new Set<string>(['401', '403', '404', '429', '503'])
|
|
32
|
+
|
|
23
33
|
/** What a dev server answers with an unhandled error: its message and stack, as JSON when the request wants it. */
|
|
24
34
|
export function devErrorResponse(error: unknown, request: Request): Response {
|
|
25
|
-
if (wantsJson(request))
|
|
35
|
+
if (wantsJson(request)) {
|
|
36
|
+
if (request.method === 'HEAD')
|
|
37
|
+
return new Response(null, { status: 500, headers: { 'content-type': 'application/json; charset=utf-8' } })
|
|
26
38
|
return Response.json({ ok: false, status: 500, error: errorMessage(error), stack: errorDetail(error) }, { status: 500 })
|
|
27
|
-
|
|
39
|
+
}
|
|
40
|
+
return new Response(request.method === 'HEAD' ? null : errorDetail(error), { status: 500, headers: { 'content-type': 'text/plain; charset=utf-8' } })
|
|
28
41
|
}
|
|
29
42
|
|
|
30
43
|
export interface ErrorNotifierOptions {
|
|
31
|
-
/** With no `onError` hook to hear an unhandled error, `console.error` does. */
|
|
44
|
+
/** With no `onError` hook to hear an unhandled error, `console.error` does (unless a `sink` captures it outside dev, as `testApp` and prerender do). */
|
|
32
45
|
dev?: boolean | undefined
|
|
33
46
|
/** Hears every unhandled error, hooks or not: how `testApp().errors` fills. */
|
|
34
47
|
sink?: ((info: ServerErrorInfo) => void) | undefined
|
|
@@ -42,13 +55,16 @@ export function createErrorNotifier(onError: ((info: ServerErrorInfo) => void) |
|
|
|
42
55
|
if (p.onError !== undefined)
|
|
43
56
|
errorHooks.push({ owner: p.name, hook: p.onError })
|
|
44
57
|
}
|
|
45
|
-
return (error, request, source) => {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
58
|
+
return (error, request, source, requestId) => {
|
|
59
|
+
const info: ServerErrorInfo = requestId !== undefined ? { error, request, source, requestId } : { error, request, source }
|
|
60
|
+
options.sink?.(info)
|
|
61
|
+
if (errorHooks.length === 0 && (options.dev === true || options.sink === undefined)) {
|
|
62
|
+
const idSuffix = requestId !== undefined ? ` [${requestId}]` : ''
|
|
63
|
+
console.error(`prunelle: unhandled error in ${source} ${request.method} ${new URL(request.url).pathname}${idSuffix}`, error)
|
|
64
|
+
}
|
|
49
65
|
for (const { owner, hook } of errorHooks) {
|
|
50
66
|
try {
|
|
51
|
-
hook(
|
|
67
|
+
hook(info)
|
|
52
68
|
}
|
|
53
69
|
catch (hookError) {
|
|
54
70
|
console.error(`prunelle: the onError hook of ${owner} threw; the other hooks still run.`, hookError)
|
|
@@ -61,17 +77,167 @@ interface LayoutModule {
|
|
|
61
77
|
default: (props: { children: Html, url?: URL, params?: Record<string, string>, locals?: object }) => Html | string | Promise<Html | string>
|
|
62
78
|
}
|
|
63
79
|
|
|
64
|
-
interface
|
|
65
|
-
default
|
|
80
|
+
interface StatusPageModule {
|
|
81
|
+
default?: (props: Record<string, unknown>) => Html | string | Promise<Html | string>
|
|
66
82
|
}
|
|
67
83
|
|
|
68
|
-
interface
|
|
69
|
-
|
|
84
|
+
interface CompiledStatusPage {
|
|
85
|
+
entry: StatusPageManifestEntry
|
|
86
|
+
render: (props: Record<string, unknown>) => Html | string | Promise<Html | string>
|
|
87
|
+
layouts: LayoutModule[]
|
|
88
|
+
doc: DocumentOptions
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
interface StatusPageLookup {
|
|
92
|
+
find: (kind: StatusPageKind, url?: URL, routeFile?: string) => { page: CompiledStatusPage, params?: Record<string, string> } | undefined
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function dirnamePrefix(file: string): string {
|
|
96
|
+
const slash = file.lastIndexOf('/')
|
|
97
|
+
return slash === -1 ? '' : file.slice(0, slash + 1)
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function parentDirPrefix(dir: string): string | undefined {
|
|
101
|
+
if (dir === '')
|
|
102
|
+
return undefined
|
|
103
|
+
const trimmed = dir.endsWith('/') ? dir.slice(0, -1) : dir
|
|
104
|
+
const slash = trimmed.lastIndexOf('/')
|
|
105
|
+
return slash === -1 ? '' : trimmed.slice(0, slash + 1)
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function legacyStatusEntries(manifest: BuildManifest, routesPrefix: string): StatusPageManifestEntry[] {
|
|
109
|
+
const layouts = manifest.rootLayoutFile !== undefined ? [manifest.rootLayoutFile] : []
|
|
110
|
+
const styles = manifest.globalStyles ?? manifest.styles
|
|
111
|
+
const entries: StatusPageManifestEntry[] = []
|
|
112
|
+
if (manifest.errorFile !== undefined)
|
|
113
|
+
entries.push({ kind: 'error', dirPattern: '/', dirPrefix: routesPrefix, file: manifest.errorFile, layouts, styles, islandChunks: [] })
|
|
114
|
+
if (manifest.notFoundFile !== undefined)
|
|
115
|
+
entries.push({ kind: '404', dirPattern: '/', dirPrefix: routesPrefix, file: manifest.notFoundFile, layouts, styles, islandChunks: [] })
|
|
116
|
+
return entries
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function buildStatusPageLookup(
|
|
120
|
+
manifest: BuildManifest,
|
|
121
|
+
modules: Record<string, unknown>,
|
|
122
|
+
baseDoc: DocumentOptions,
|
|
123
|
+
routesPrefix: string,
|
|
124
|
+
paramMatchers: ParamMatcherMap,
|
|
125
|
+
): StatusPageLookup {
|
|
126
|
+
const rawEntries = manifest.statusPages ?? legacyStatusEntries(manifest, routesPrefix)
|
|
127
|
+
const byDirPrefix = new Map<StatusPageKind, Map<string, CompiledStatusPage>>()
|
|
128
|
+
const byUrlRouter = new Map<StatusPageKind, Router<CompiledStatusPage>>()
|
|
129
|
+
const groupCandidates = new Map<StatusPageKind, Map<string, CompiledStatusPage[]>>()
|
|
130
|
+
|
|
131
|
+
for (const entry of rawEntries) {
|
|
132
|
+
const mod = modules[entry.file] as StatusPageModule | undefined
|
|
133
|
+
if (typeof mod?.default !== 'function')
|
|
134
|
+
continue
|
|
135
|
+
const layouts = entry.layouts
|
|
136
|
+
.map(key => modules[key] as LayoutModule | undefined)
|
|
137
|
+
.filter((l): l is LayoutModule => typeof l?.default === 'function')
|
|
138
|
+
const compiled: CompiledStatusPage = {
|
|
139
|
+
entry,
|
|
140
|
+
render: mod.default,
|
|
141
|
+
layouts,
|
|
142
|
+
doc: { ...baseDoc, styles: entry.styles },
|
|
143
|
+
}
|
|
144
|
+
let dirMap = byDirPrefix.get(entry.kind)
|
|
145
|
+
if (dirMap === undefined) {
|
|
146
|
+
dirMap = new Map()
|
|
147
|
+
byDirPrefix.set(entry.kind, dirMap)
|
|
148
|
+
}
|
|
149
|
+
dirMap.set(entry.dirPrefix, compiled)
|
|
150
|
+
|
|
151
|
+
if (entry.inGroup === true) {
|
|
152
|
+
let byPattern = groupCandidates.get(entry.kind)
|
|
153
|
+
if (byPattern === undefined) {
|
|
154
|
+
byPattern = new Map()
|
|
155
|
+
groupCandidates.set(entry.kind, byPattern)
|
|
156
|
+
}
|
|
157
|
+
const list = byPattern.get(entry.dirPattern) ?? []
|
|
158
|
+
list.push(compiled)
|
|
159
|
+
byPattern.set(entry.dirPattern, list)
|
|
160
|
+
}
|
|
161
|
+
else {
|
|
162
|
+
let router = byUrlRouter.get(entry.kind)
|
|
163
|
+
if (router === undefined) {
|
|
164
|
+
router = new Router<CompiledStatusPage>(paramMatchers)
|
|
165
|
+
byUrlRouter.set(entry.kind, router)
|
|
166
|
+
}
|
|
167
|
+
router.add(entry.dirPattern, compiled)
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const groupUrlRouter = new Map<StatusPageKind, Router<CompiledStatusPage>>()
|
|
172
|
+
for (const [kind, byPattern] of groupCandidates) {
|
|
173
|
+
const router = new Router<CompiledStatusPage>(paramMatchers)
|
|
174
|
+
let added = false
|
|
175
|
+
for (const [pattern, list] of byPattern) {
|
|
176
|
+
if (list.length === 1) {
|
|
177
|
+
router.add(pattern, list[0]!)
|
|
178
|
+
added = true
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
if (added)
|
|
182
|
+
groupUrlRouter.set(kind, router)
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
return {
|
|
186
|
+
find(kind, url, routeFile) {
|
|
187
|
+
if (routeFile !== undefined) {
|
|
188
|
+
const dirMap = byDirPrefix.get(kind)
|
|
189
|
+
if (dirMap !== undefined) {
|
|
190
|
+
let dir: string | undefined = dirnamePrefix(routeFile)
|
|
191
|
+
while (dir !== undefined) {
|
|
192
|
+
const hit = dirMap.get(dir)
|
|
193
|
+
if (hit !== undefined)
|
|
194
|
+
return { page: hit }
|
|
195
|
+
if (dir === routesPrefix || !dir.startsWith(routesPrefix))
|
|
196
|
+
break
|
|
197
|
+
dir = parentDirPrefix(dir)
|
|
198
|
+
}
|
|
199
|
+
const rootHit = dirMap.get(routesPrefix)
|
|
200
|
+
if (rootHit !== undefined)
|
|
201
|
+
return { page: rootHit }
|
|
202
|
+
}
|
|
203
|
+
return undefined
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
const decoded = decodeRequestPath(url?.pathname ?? '/')
|
|
207
|
+
if (decoded === undefined)
|
|
208
|
+
return undefined
|
|
209
|
+
const urlRouter = byUrlRouter.get(kind)
|
|
210
|
+
if (urlRouter !== undefined) {
|
|
211
|
+
for (let i = decoded.segments.length; i >= 0; i--) {
|
|
212
|
+
const matched = urlRouter.match(pathPrefix(decoded, i))
|
|
213
|
+
if (matched !== undefined)
|
|
214
|
+
return { page: matched.data, params: matched.params }
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
const fallbackGroup = groupUrlRouter.get(kind)
|
|
218
|
+
if (fallbackGroup !== undefined) {
|
|
219
|
+
for (let i = decoded.segments.length; i >= 0; i--) {
|
|
220
|
+
const matched = fallbackGroup.match(pathPrefix(decoded, i))
|
|
221
|
+
if (matched !== undefined)
|
|
222
|
+
return { page: matched.data, params: matched.params }
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
return undefined
|
|
226
|
+
},
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
function minimalStatusHtml(status: number, message: string, lang: string): string {
|
|
231
|
+
const safeLang = escapeHtml(lang)
|
|
232
|
+
const safeMsg = escapeHtml(message)
|
|
233
|
+
return `<!DOCTYPE html><html lang="${safeLang}"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>${status}</title></head><body><main><h1>${status}</h1><p>${safeMsg}</p></main></body></html>`
|
|
70
234
|
}
|
|
71
235
|
|
|
72
236
|
export interface FallbackPagesOptions {
|
|
73
237
|
manifest: BuildManifest
|
|
74
238
|
modules: Record<string, unknown>
|
|
239
|
+
routesPrefix?: string | undefined
|
|
240
|
+
paramMatchers?: ParamMatcherMap | undefined
|
|
75
241
|
lang: string
|
|
76
242
|
alwaysLoadRuntime: boolean
|
|
77
243
|
navigation?: string | undefined
|
|
@@ -79,18 +245,17 @@ export interface FallbackPagesOptions {
|
|
|
79
245
|
document: DocumentHooks
|
|
80
246
|
htmlHeaders: HtmlHeaders
|
|
81
247
|
notifyError: NotifyError
|
|
82
|
-
finish: (res: Response, request: Request, cookieStore?: CookieStore) => Response
|
|
248
|
+
finish: (res: Response, request: Request, cookieStore?: CookieStore, requestId?: string, serverTiming?: string) => Response
|
|
83
249
|
}
|
|
84
250
|
|
|
85
251
|
export function createFallbackPages(options: FallbackPagesOptions) {
|
|
86
252
|
const { manifest, modules, document, htmlHeaders, notifyError, finish } = options
|
|
87
|
-
const
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
const
|
|
93
|
-
const fallbackDoc: DocumentOptions = {
|
|
253
|
+
const routesPrefix = options.routesPrefix ?? (
|
|
254
|
+
manifest.rootLayoutFile ?? manifest.errorFile ?? manifest.notFoundFile
|
|
255
|
+
? dirnamePrefix((manifest.rootLayoutFile ?? manifest.errorFile ?? manifest.notFoundFile)!)
|
|
256
|
+
: 'server/'
|
|
257
|
+
)
|
|
258
|
+
const baseDoc: DocumentOptions = {
|
|
94
259
|
body: '',
|
|
95
260
|
headTags: manifest.headTags,
|
|
96
261
|
styles: manifest.globalStyles ?? manifest.styles,
|
|
@@ -100,54 +265,129 @@ export function createFallbackPages(options: FallbackPagesOptions) {
|
|
|
100
265
|
navigation: options.navigation,
|
|
101
266
|
lang: options.lang,
|
|
102
267
|
}
|
|
103
|
-
const fallbackHeadStyleHashes = headTagStyleHashes(
|
|
268
|
+
const fallbackHeadStyleHashes = headTagStyleHashes(baseDoc.headTags)
|
|
269
|
+
const lookup = buildStatusPageLookup(manifest, modules, baseDoc, routesPrefix, options.paramMatchers ?? BUILTIN_PARAM_MATCHERS)
|
|
104
270
|
|
|
105
|
-
async function
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
271
|
+
async function renderCompiledStatusPage(
|
|
272
|
+
page: CompiledStatusPage,
|
|
273
|
+
status: number,
|
|
274
|
+
props: Record<string, unknown>,
|
|
275
|
+
effUrl: URL,
|
|
276
|
+
params: Record<string, string>,
|
|
277
|
+
req?: Request,
|
|
278
|
+
ctx?: RequestContext,
|
|
279
|
+
): Promise<Response | undefined> {
|
|
280
|
+
try {
|
|
281
|
+
const locals = ctx?.locals ?? {}
|
|
282
|
+
const rctx = createRenderContext(undefined, undefined, ctx?.consent, req !== undefined ? error => notifyError(error, req, 'route', ctx?.requestId) : undefined)
|
|
283
|
+
const body = await runWithRenderContext(rctx, async () => {
|
|
284
|
+
const inner = ssrChild(await page.render(props) as Html)
|
|
285
|
+
let wrapped = inner
|
|
286
|
+
try {
|
|
287
|
+
for (let i = page.layouts.length - 1; i >= 0; i--)
|
|
288
|
+
wrapped = ssrChild(await page.layouts[i]!.default({ children: raw(wrapped), url: effUrl, params, locals }) as Html)
|
|
289
|
+
}
|
|
290
|
+
catch (layoutErr) {
|
|
291
|
+
if (req !== undefined)
|
|
292
|
+
notifyError(layoutErr, req, 'route', ctx?.requestId)
|
|
114
293
|
return inner
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
294
|
+
}
|
|
295
|
+
return wrapped
|
|
296
|
+
})
|
|
297
|
+
const { hasIslands, eagerChunks } = resolveRenderedIslands(rctx, body)
|
|
298
|
+
const docHead = await document.resolveDocumentHead(rctx, manifest.headTags, effUrl, req, ctx)
|
|
299
|
+
const docHtml = renderDocument({
|
|
300
|
+
...page.doc,
|
|
301
|
+
body,
|
|
302
|
+
head: docHead.head,
|
|
303
|
+
lang: docHead.lang,
|
|
304
|
+
htmlAttrs: docHead.htmlAttrs,
|
|
305
|
+
preloadChunks: [...eagerChunks, ...revivePreload(rctx, manifest.reviveScript)],
|
|
306
|
+
hasIslands,
|
|
307
|
+
})
|
|
308
|
+
const html = await document.applyHtmlTransforms(docHtml, effUrl, req, ctx)
|
|
309
|
+
const styleHashes = mergeStyleHashes(fallbackHeadStyleHashes, docHead.styleHashes, styleHashesOf(rctx.inlineStyles), addedInlineStyleHashes(docHtml, html))
|
|
310
|
+
return new Response(req?.method === 'HEAD' ? null : html, { status, headers: htmlHeaders(hasIslands, false, true, styleHashes) })
|
|
311
|
+
}
|
|
312
|
+
catch (err) {
|
|
313
|
+
if (req !== undefined)
|
|
314
|
+
notifyError(err, req, 'route', ctx?.requestId)
|
|
315
|
+
return undefined
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
async function buildErrorResponse(error: unknown, url?: URL, req?: Request, ctx?: RequestContext, routeFile?: string): Promise<Response> {
|
|
320
|
+
const effUrl = url ?? new URL('http://localhost/500')
|
|
321
|
+
const found = lookup.find('error', effUrl, routeFile)
|
|
322
|
+
if (found !== undefined) {
|
|
323
|
+
const params = ctx !== undefined && Object.keys(ctx.params).length > 0 ? ctx.params : (found.params ?? {})
|
|
324
|
+
// Outside dev the page gets a bare error: a thrown message can name a host, a table or a credential, and it lands in HTML anyone can load.
|
|
325
|
+
const shown = options.dev ? error : Object.assign(new Error('Internal Server Error'), { stack: 'Error: Internal Server Error' })
|
|
326
|
+
const rendered = await renderCompiledStatusPage(
|
|
327
|
+
found.page,
|
|
328
|
+
500,
|
|
329
|
+
{ error: shown, status: 500, message: errorMessage(shown), url: effUrl, params, locals: ctx?.locals ?? {} },
|
|
330
|
+
effUrl,
|
|
331
|
+
params,
|
|
332
|
+
req,
|
|
333
|
+
ctx,
|
|
334
|
+
)
|
|
335
|
+
if (rendered !== undefined)
|
|
336
|
+
return rendered
|
|
135
337
|
}
|
|
136
|
-
return new Response(
|
|
338
|
+
return new Response(req?.method === 'HEAD' ? null : (options.dev ? errorDetail(error) : 'Internal Server Error'), { status: 500, headers: { 'content-type': 'text/plain; charset=utf-8' } })
|
|
137
339
|
}
|
|
138
340
|
|
|
139
|
-
|
|
140
|
-
|
|
341
|
+
async function buildNotFoundResponse(url?: URL, req?: Request, ctx?: RequestContext, routeFile?: string): Promise<Response> {
|
|
342
|
+
const effUrl = url ?? new URL('http://localhost/404')
|
|
343
|
+
const found = lookup.find('404', effUrl, routeFile)
|
|
344
|
+
if (found !== undefined) {
|
|
345
|
+
const params = ctx !== undefined && Object.keys(ctx.params).length > 0 ? ctx.params : (found.params ?? {})
|
|
346
|
+
const rendered = await renderCompiledStatusPage(
|
|
347
|
+
found.page,
|
|
348
|
+
404,
|
|
349
|
+
{ status: 404, message: 'Not Found', url: effUrl, params, locals: ctx?.locals ?? {} },
|
|
350
|
+
effUrl,
|
|
351
|
+
params,
|
|
352
|
+
req,
|
|
353
|
+
ctx,
|
|
354
|
+
)
|
|
355
|
+
if (rendered !== undefined)
|
|
356
|
+
return rendered
|
|
357
|
+
}
|
|
358
|
+
return new Response(req?.method === 'HEAD' ? null : 'Not Found', { status: 404, headers: { 'content-type': 'text/plain; charset=utf-8' } })
|
|
359
|
+
}
|
|
141
360
|
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
361
|
+
async function renderDenied(denied: DeniedInfo, url: URL, req: Request, ctx?: RequestContext, routeFile?: string): Promise<Response> {
|
|
362
|
+
const statusKey = String(denied.status)
|
|
363
|
+
const statusHit = STATUS_KINDS.has(statusKey) ? lookup.find(statusKey as StatusPageKind, url, routeFile) : undefined
|
|
364
|
+
const found = statusHit ?? lookup.find('error', url, routeFile)
|
|
365
|
+
if (found !== undefined) {
|
|
366
|
+
const params = ctx !== undefined && Object.keys(ctx.params).length > 0 ? ctx.params : (found.params ?? {})
|
|
367
|
+
const props = statusHit !== undefined
|
|
368
|
+
? { status: denied.status, message: denied.message, url, params, locals: ctx?.locals ?? {} }
|
|
369
|
+
: { error: new Error(denied.message), status: denied.status, message: denied.message, url, params, locals: ctx?.locals ?? {} }
|
|
370
|
+
const rendered = await renderCompiledStatusPage(found.page, denied.status, props, url, params, req, ctx)
|
|
371
|
+
if (rendered !== undefined)
|
|
372
|
+
return rendered
|
|
373
|
+
}
|
|
374
|
+
return new Response(req.method === 'HEAD' ? null : minimalStatusHtml(denied.status, denied.message, options.lang), {
|
|
375
|
+
status: denied.status,
|
|
376
|
+
headers: htmlHeaders(false, false, true, fallbackHeadStyleHashes),
|
|
377
|
+
})
|
|
378
|
+
}
|
|
145
379
|
|
|
146
|
-
async function renderError(error: unknown, cookieStore: CookieStore | undefined, url: URL, request: Request, ctx?: RequestContext): Promise<Response> {
|
|
147
|
-
if (wantsJson(request))
|
|
148
|
-
|
|
149
|
-
|
|
380
|
+
async function renderError(error: unknown, cookieStore: CookieStore | undefined, url: URL, request: Request, ctx?: RequestContext, routeFile?: string): Promise<Response> {
|
|
381
|
+
if (wantsJson(request)) {
|
|
382
|
+
const res = options.dev
|
|
383
|
+
? devErrorResponse(error, request)
|
|
384
|
+
: (request.method === 'HEAD'
|
|
385
|
+
? new Response(null, { status: 500, headers: { 'content-type': 'application/json; charset=utf-8' } })
|
|
386
|
+
: Response.json({ ok: false, status: 500, error: 'Internal Server Error' }, { status: 500 }))
|
|
387
|
+
return finish(res, request, cookieStore, ctx?.requestId)
|
|
388
|
+
}
|
|
389
|
+
return finish(await buildErrorResponse(error, url, request, ctx, routeFile), request, cookieStore, ctx?.requestId)
|
|
150
390
|
}
|
|
151
391
|
|
|
152
|
-
return { buildNotFoundResponse, renderError }
|
|
392
|
+
return { buildNotFoundResponse, renderError, renderDenied }
|
|
153
393
|
}
|