prunelle 0.8.0 → 0.10.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/llms.txt +68 -51
- package/package.json +7 -2
- package/src/a11y/declaration.ts +10 -17
- package/src/a11y/index.ts +69 -125
- package/src/animate.ts +127 -0
- package/src/build/asides.ts +51 -0
- package/src/build/bundle.ts +94 -136
- package/src/build/client-entries.ts +6 -6
- package/src/build/client-outputs.ts +75 -0
- package/src/build/client-plugin.ts +9 -4
- package/src/build/ecoindex.ts +4 -2
- package/src/build/files.ts +18 -0
- package/src/build/graph.ts +14 -6
- package/src/build/index.ts +23 -11
- package/src/build/island-promote.ts +42 -2
- package/src/build/islands.ts +63 -9
- package/src/build/key-types.ts +31 -0
- package/src/build/manifest.ts +117 -1
- package/src/build/plugin-host.ts +13 -1
- package/src/build/prerender.ts +8 -3
- package/src/build/reach.ts +18 -4
- package/src/build/report.ts +1 -1
- package/src/build/resolve.ts +5 -1
- package/src/build/route-types.ts +16 -0
- package/src/build/routes.ts +42 -15
- package/src/build/seed-ids.ts +65 -0
- package/src/build/seed-writes.ts +111 -0
- package/src/build/static-headers.ts +2 -2
- package/src/build/static.ts +1 -1
- package/src/cli/check-cache.ts +51 -0
- package/src/cli/check-consent.ts +95 -0
- package/src/cli/check-forms.ts +10 -2
- package/src/cli/check-links-patterns.ts +101 -0
- package/src/cli/check-links.ts +30 -105
- package/src/cli/check-regions.ts +185 -0
- package/src/cli/check-security.ts +50 -6
- package/src/cli/check.ts +49 -21
- package/src/cli/commands.ts +138 -16
- package/src/cli/docs.ts +21 -2
- package/src/cli/form-opaque-children.ts +40 -0
- package/src/cli/index.ts +1 -1
- package/src/cli/inspect.ts +2 -0
- package/src/cli/mcp-tools.ts +2 -2
- package/src/cli/new.ts +9 -5
- package/src/cli/upgrade-names.ts +14 -2
- package/src/cli/upgrade.ts +3 -0
- package/src/compiler/analyze.ts +22 -16
- package/src/compiler/cacheability-private.ts +13 -3
- package/src/compiler/cacheability.ts +24 -2
- package/src/compiler/compliance-contrast.ts +322 -0
- package/src/compiler/compliance-empty.ts +24 -0
- package/src/compiler/compliance-rgaa-content.ts +95 -0
- package/src/compiler/compliance-rgaa-criteria.ts +45 -0
- package/src/compiler/compliance-rgaa-pages.ts +140 -0
- package/src/compiler/compliance-rgaa.ts +19 -0
- package/src/compiler/compliance-rules.ts +72 -0
- package/src/compiler/compliance-third-party.ts +59 -0
- package/src/compiler/compliance-urls.ts +52 -0
- package/src/compiler/compliance.ts +61 -93
- package/src/compiler/cookie-inventory.ts +119 -0
- package/src/compiler/dom-attrs.ts +178 -22
- package/src/compiler/dom.ts +31 -44
- package/src/compiler/emit.ts +17 -6
- package/src/compiler/expr-context.ts +260 -207
- package/src/compiler/foreign-imports.ts +5 -3
- package/src/compiler/mistakes.ts +18 -9
- package/src/compiler/props-body.ts +263 -21
- package/src/compiler/props-pattern.ts +211 -0
- package/src/compiler/props.ts +324 -343
- package/src/compiler/signal-reads.ts +142 -29
- package/src/compiler/spread-handlers.ts +25 -13
- package/src/compiler/ssr-emit.ts +510 -0
- package/src/compiler/ssr-helpers.ts +165 -121
- package/src/compiler/ssr.ts +28 -441
- package/src/context.ts +14 -1
- package/src/dev/child.ts +1 -1
- package/src/dev/host-check.ts +2 -1
- package/src/dev/hot-islands.ts +71 -0
- package/src/dev/index.ts +31 -18
- package/src/dev/protocol.ts +1 -1
- package/src/dev/queue.ts +3 -2
- package/src/dev/reload.ts +5 -2
- package/src/dev/supervisor.ts +3 -1
- package/src/dev/watchers.ts +6 -2
- package/src/dom/action.ts +97 -14
- package/src/dom/attrs.ts +93 -25
- package/src/dom/extras.ts +91 -50
- package/src/dom/field-marks.ts +32 -0
- package/src/dom/hot.ts +9 -0
- package/src/dom/index.ts +44 -18
- package/src/dom/island-hot.ts +29 -0
- package/src/dom/island.ts +13 -2
- package/src/dom/loop.ts +259 -188
- package/src/dom/prefetch.ts +135 -0
- package/src/dom/regions.ts +515 -0
- package/src/dom/swap.ts +246 -0
- package/src/dom/trusted.ts +22 -0
- package/src/dom/upload.ts +44 -0
- package/src/eco/declaration.ts +44 -41
- package/src/eco/index.ts +8 -5
- package/src/href.ts +31 -0
- package/src/index.ts +12 -5
- package/src/plugins/config.ts +1 -1
- package/src/plugins/context.ts +7 -1
- package/src/plugins/daily.ts +27 -0
- package/src/plugins/index.ts +3 -3
- package/src/plugins/order.ts +1 -1
- package/src/plugins/public.ts +12 -5
- package/src/plugins/services.ts +88 -14
- package/src/plugins/types.ts +128 -10
- package/src/reactivity/graph.ts +244 -41
- package/src/reactivity/hmr.ts +101 -0
- package/src/reactivity/index.ts +243 -239
- package/src/reactivity/resource.ts +129 -0
- package/src/reactivity/seed.ts +141 -0
- package/src/reactivity/selector.ts +85 -47
- package/src/reactivity/server-seed.ts +202 -0
- package/src/reactivity/store.ts +331 -97
- package/src/rgpd/draft.ts +171 -0
- package/src/rgpd/forms.ts +85 -0
- package/src/rgpd/index.ts +57 -0
- package/src/server/action-flash.ts +1 -0
- package/src/server/action-protect.ts +3 -0
- package/src/server/action-registry.ts +11 -9
- package/src/server/action.ts +51 -19
- package/src/server/app-secret.ts +14 -8
- package/src/server/cache-invalidation.ts +97 -0
- package/src/server/compress.ts +55 -18
- package/src/server/consent.ts +21 -5
- package/src/server/cookie-inventory.ts +78 -0
- package/src/server/cookies.ts +77 -8
- package/src/server/cors.ts +98 -0
- package/src/server/csp-report.ts +90 -0
- package/src/server/discard.ts +13 -0
- package/src/server/document.ts +2 -2
- package/src/server/env.ts +1 -1
- package/src/server/errors.ts +9 -2
- package/src/server/field-errors.ts +91 -0
- package/src/server/form-schema.ts +16 -11
- package/src/server/health.ts +41 -11
- package/src/server/http.ts +28 -11
- package/src/server/index.ts +122 -307
- package/src/server/loader.ts +2 -0
- package/src/server/page.ts +3 -9
- package/src/server/plugin-host.ts +66 -16
- package/src/server/plugin-route-shadow.ts +24 -0
- package/src/server/public.ts +14 -3
- package/src/server/rate-limit.ts +49 -10
- package/src/server/region.ts +22 -2
- package/src/server/request-handler.ts +406 -0
- package/src/server/request-log.ts +94 -0
- package/src/server/route-cache.ts +55 -8
- package/src/server/route-input.ts +54 -0
- package/src/server/route-table.ts +53 -27
- package/src/server/router.ts +2 -1
- package/src/server/routing.ts +104 -45
- package/src/server/sanitize-html.ts +4 -2
- package/src/server/security.ts +38 -8
- package/src/server/serve-handlers.ts +16 -6
- package/src/server/served-file.ts +19 -14
- package/src/server/shared-store.ts +323 -0
- package/src/server/start.ts +12 -5
- package/src/server/static-dir.ts +34 -12
- package/src/server/stream.ts +2 -1
- package/src/server/surface.ts +39 -0
- package/src/server/webhook.ts +50 -0
- package/src/server/workers.ts +168 -0
- package/src/ssr/consent-banner.ts +20 -16
- package/src/ssr/consent-texts.ts +40 -0
- package/src/ssr/context.ts +174 -53
- package/src/ssr/cookie-table.ts +75 -0
- package/src/ssr/index.ts +63 -58
- package/src/ssr/navigation.ts +161 -14
- package/src/ssr/region.ts +9 -3
- package/src/style.ts +12 -5
- package/src/test/browser.ts +53 -23
- package/src/test/build-reuse.ts +85 -0
- package/src/test/index.ts +51 -8
- package/src/test/mount.ts +53 -0
- package/src/test/outside.ts +94 -0
- package/src/ts-plugin/diagnostics.ts +56 -18
- package/templates/_shared/AGENTS.md +1 -1
- package/templates/_shared/Dockerfile +22 -0
- package/templates/_shared/dockerignore +9 -0
- package/templates/_shared/gitignore +2 -0
- package/templates/app/.env.example +3 -1
- package/templates/app/compose.yaml +32 -0
- package/templates/app/db/migrations/001_notes.sql +7 -0
- package/templates/app/db/schema.ts +11 -0
- package/templates/app/package.json +1 -0
- package/templates/app/prunelle.config.ts +6 -0
- package/templates/app/src/actions/notes.ts +26 -0
- package/templates/app/src/auth.ts +4 -2
- package/templates/app/src/db.ts +5 -0
- package/templates/app/src/routes/dashboard/index.tsx +26 -1
- package/templates/app/src/routes/privacy.tsx +6 -2
- package/templates/app/tests/app.test.ts +14 -3
- package/templates/plugin/AGENTS.md +11 -0
- package/templates/plugin/CLAUDE.md +1 -0
- package/templates/plugin/README.md +27 -0
- package/templates/plugin/gitignore +2 -0
- package/templates/plugin/llms.txt +19 -0
- package/templates/plugin/package.json +33 -0
- package/templates/plugin/src/index.ts +40 -0
- package/templates/plugin/tests/fixture/prunelle.config.ts +4 -0
- package/templates/plugin/tests/fixture/src/routes/index.tsx +11 -0
- package/templates/plugin/tests/plugin.test.ts +42 -0
- package/templates/plugin/tsconfig.json +19 -0
- package/ts-plugin.cjs +4058 -2246
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
// A page re-rendered after a failed zero-JS submit: each field the error names gets `aria-invalid` and is described by the element of its form showing its message (RGAA 11.10, 11.11), as `useAction` does in an island.
|
|
2
|
+
|
|
3
|
+
import type { ActionErrorFlash } from './action'
|
|
4
|
+
|
|
5
|
+
const CONTROLS = new Set(['input', 'select', 'textarea'])
|
|
6
|
+
|
|
7
|
+
interface Seen { index: number, text: string }
|
|
8
|
+
|
|
9
|
+
// Numbers the elements inside the forms posting to `action`, in document order, so both passes name an element the same way.
|
|
10
|
+
function inForms(rewriter: HTMLRewriter, action: string, visit: (el: HTMLRewriterTypes.Element, at: number) => void): HTMLRewriter {
|
|
11
|
+
const path = (url: string): string => new URL(url, 'http://x').pathname
|
|
12
|
+
let index = -1
|
|
13
|
+
let inside = false
|
|
14
|
+
return rewriter.on('*', {
|
|
15
|
+
element(el) {
|
|
16
|
+
if (el.tagName === 'form') {
|
|
17
|
+
const target = el.getAttribute('action')
|
|
18
|
+
inside = target !== null && path(target) === path(action)
|
|
19
|
+
el.onEndTag(() => void (inside = false))
|
|
20
|
+
}
|
|
21
|
+
else if (inside) {
|
|
22
|
+
visit(el, ++index)
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
})
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// The element showing a field's message: the first one after the field whose whole text is the message, else the last one before it, so fields sharing "Required" each get their own.
|
|
29
|
+
function messageOf(seen: readonly Seen[], field: number, text: string): number | undefined {
|
|
30
|
+
const matches = seen.filter(s => s.text === text).map(s => s.index)
|
|
31
|
+
return matches.find(i => i > field) ?? matches.findLast(i => i < field)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** `html` with the fields of the failed form tied to their messages; unchanged when the flash names no field or the page has no form posting to its action. */
|
|
35
|
+
export async function markFlashedFields(html: string, flash: ActionErrorFlash | undefined): Promise<string> {
|
|
36
|
+
const errors = flash?.fieldErrors
|
|
37
|
+
if (flash === undefined || errors === undefined || Object.keys(errors).length === 0)
|
|
38
|
+
return html
|
|
39
|
+
// The page escaped each message as it rendered it, and the rewriter hands text over as written.
|
|
40
|
+
const escaped = new Map(Object.entries(errors).map(([name, message]) => [name, Bun.escapeHTML(message)]))
|
|
41
|
+
|
|
42
|
+
const fields = new Map<number, string>()
|
|
43
|
+
const ids = new Map<number, string | null>()
|
|
44
|
+
const seen: Seen[] = []
|
|
45
|
+
const open: Seen[] = []
|
|
46
|
+
await inForms(new HTMLRewriter(), flash.action, (el, at) => {
|
|
47
|
+
const name = el.getAttribute('name')
|
|
48
|
+
if (CONTROLS.has(el.tagName) && name !== null && escaped.has(name) && !el.hasAttribute('aria-invalid'))
|
|
49
|
+
fields.set(at, name)
|
|
50
|
+
ids.set(at, el.getAttribute('id'))
|
|
51
|
+
if (!el.canHaveContent)
|
|
52
|
+
return
|
|
53
|
+
const entry = { index: at, text: '' }
|
|
54
|
+
open.push(entry)
|
|
55
|
+
el.onEndTag(() => {
|
|
56
|
+
open.pop()
|
|
57
|
+
seen.push({ index: at, text: entry.text.trim() })
|
|
58
|
+
})
|
|
59
|
+
}).onDocument({ text(chunk) {
|
|
60
|
+
for (const entry of open)
|
|
61
|
+
entry.text += chunk.text
|
|
62
|
+
} }).transform(new Response(html)).text()
|
|
63
|
+
if (fields.size === 0)
|
|
64
|
+
return html
|
|
65
|
+
|
|
66
|
+
const describedBy = new Map<number, string>()
|
|
67
|
+
const newIds = new Map<number, string>()
|
|
68
|
+
for (const [at, name] of fields) {
|
|
69
|
+
const shown = messageOf(seen, at, escaped.get(name)!)
|
|
70
|
+
if (shown === undefined)
|
|
71
|
+
continue
|
|
72
|
+
const id = ids.get(shown) ?? newIds.get(shown) ?? `pru-err-${name}`
|
|
73
|
+
newIds.set(shown, id)
|
|
74
|
+
describedBy.set(at, id)
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return inForms(new HTMLRewriter(), flash.action, (el, at) => {
|
|
78
|
+
const id = newIds.get(at)
|
|
79
|
+
if (id !== undefined)
|
|
80
|
+
el.setAttribute('id', id)
|
|
81
|
+
if (!fields.has(at))
|
|
82
|
+
return
|
|
83
|
+
// `data-pru-err` keeps what `aria-describedby` was, so `useAction` undoes these marks at the next submit.
|
|
84
|
+
const before = el.getAttribute('aria-describedby') ?? ''
|
|
85
|
+
el.setAttribute('data-pru-err', before)
|
|
86
|
+
el.setAttribute('aria-invalid', 'true')
|
|
87
|
+
const describe = describedBy.get(at)
|
|
88
|
+
if (describe !== undefined)
|
|
89
|
+
el.setAttribute('aria-describedby', `${before} ${describe}`.trim())
|
|
90
|
+
}).transform(new Response(html)).text()
|
|
91
|
+
}
|
|
@@ -25,6 +25,8 @@ export type ExtractedFormSchema =
|
|
|
25
25
|
| {
|
|
26
26
|
ok: false
|
|
27
27
|
vendor: string
|
|
28
|
+
/** The message of the error the library threw converting the schema, when its converter failed (such as Zod 4 on `z.date()`). */
|
|
29
|
+
reason?: string | undefined
|
|
28
30
|
}
|
|
29
31
|
|
|
30
32
|
interface LegacyJsonSchemaCapable {
|
|
@@ -40,17 +42,20 @@ export function isRecord(value: unknown): value is Record<string, unknown> {
|
|
|
40
42
|
return value !== null && typeof value === 'object' && !Array.isArray(value)
|
|
41
43
|
}
|
|
42
44
|
|
|
43
|
-
|
|
45
|
+
const messageOf = (error: unknown): string => error instanceof Error ? error.message : String(error)
|
|
46
|
+
|
|
47
|
+
function readRawJsonSchema(schema: StandardSchemaV1): { json?: Record<string, unknown>, reason?: string } {
|
|
48
|
+
let reason: string | undefined
|
|
44
49
|
const stdJson = schema['~standard']?.jsonSchema
|
|
45
50
|
if (stdJson !== undefined && typeof stdJson.input === 'function') {
|
|
46
51
|
for (const target of ['draft-2020-12', 'draft-07'] as const) {
|
|
47
52
|
try {
|
|
48
53
|
const out = stdJson.input({ target })
|
|
49
54
|
if (isRecord(out))
|
|
50
|
-
return out
|
|
55
|
+
return { json: out }
|
|
51
56
|
}
|
|
52
|
-
catch {
|
|
53
|
-
|
|
57
|
+
catch (error) {
|
|
58
|
+
reason ??= messageOf(error)
|
|
54
59
|
}
|
|
55
60
|
}
|
|
56
61
|
}
|
|
@@ -59,15 +64,15 @@ function readRawJsonSchema(schema: StandardSchemaV1): Record<string, unknown> |
|
|
|
59
64
|
try {
|
|
60
65
|
const out = candidate.toJSONSchema()
|
|
61
66
|
if (isRecord(out))
|
|
62
|
-
return out
|
|
67
|
+
return { json: out }
|
|
63
68
|
}
|
|
64
|
-
catch {
|
|
65
|
-
return
|
|
69
|
+
catch (error) {
|
|
70
|
+
return { reason: reason ?? messageOf(error) }
|
|
66
71
|
}
|
|
67
72
|
}
|
|
68
73
|
if (isRecord(candidate.jsonSchema))
|
|
69
|
-
return candidate.jsonSchema
|
|
70
|
-
return undefined
|
|
74
|
+
return { json: candidate.jsonSchema }
|
|
75
|
+
return reason === undefined ? {} : { reason }
|
|
71
76
|
}
|
|
72
77
|
|
|
73
78
|
function resolveRef(node: Record<string, unknown>, root: Record<string, unknown>, seen: Set<string>): Record<string, unknown> {
|
|
@@ -318,11 +323,11 @@ export function extractFormSchema(schema: StandardSchemaV1): ExtractedFormSchema
|
|
|
318
323
|
if (cached !== undefined)
|
|
319
324
|
return cached
|
|
320
325
|
|
|
321
|
-
const raw = readRawJsonSchema(schema)
|
|
326
|
+
const { json: raw, reason } = readRawJsonSchema(schema)
|
|
322
327
|
if (raw === undefined) {
|
|
323
328
|
const rawVendor: unknown = schema['~standard']?.vendor
|
|
324
329
|
const vendor = typeof rawVendor === 'string' && rawVendor.trim() !== '' ? rawVendor : 'unknown'
|
|
325
|
-
const result: ExtractedFormSchema = { ok: false, vendor }
|
|
330
|
+
const result: ExtractedFormSchema = { ok: false, vendor, ...(reason === undefined ? {} : { reason }) }
|
|
326
331
|
schemaCache.set(schema, result)
|
|
327
332
|
return result
|
|
328
333
|
}
|
package/src/server/health.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// Server observability helpers: `/_pru/health` probes, fast request IDs (`x-request-id`), and dev
|
|
1
|
+
// Server observability helpers: `/_pru/health` probes, fast request IDs (`x-request-id`), and the phases a request goes through (dev `Server-Timing`, the access log and `onRequestEnd` hooks).
|
|
2
2
|
|
|
3
3
|
import type { Middleware, RequestContext } from './http'
|
|
4
4
|
import type { TrustProxy } from './security'
|
|
@@ -19,11 +19,19 @@ export interface HealthCheckOutcome {
|
|
|
19
19
|
error?: string | undefined
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
-
export
|
|
22
|
+
export type RequestPhaseName = 'middleware' | 'load' | 'render' | 'stream'
|
|
23
|
+
|
|
24
|
+
export interface PhaseTiming {
|
|
23
25
|
middleware?: number | undefined
|
|
24
26
|
load?: number | undefined
|
|
25
27
|
render?: number | undefined
|
|
26
28
|
stream?: number | undefined
|
|
29
|
+
/** When each phase began (`performance.now()`), kept for a request the access log or an `onRequestEnd` hook hears. */
|
|
30
|
+
starts?: Partial<Record<RequestPhaseName, number>> | undefined
|
|
31
|
+
/** Measured for those hooks in production, never sent as `Server-Timing`. */
|
|
32
|
+
headerless?: true | undefined
|
|
33
|
+
/** The route template that answered (`/blog/:slug`), told to those hooks. */
|
|
34
|
+
route?: string | undefined
|
|
27
35
|
}
|
|
28
36
|
|
|
29
37
|
export type RequestIdResolver = (request: Request) => string
|
|
@@ -100,25 +108,45 @@ export function withRequestId(res: Response, requestId: string): Response {
|
|
|
100
108
|
}
|
|
101
109
|
}
|
|
102
110
|
|
|
103
|
-
|
|
111
|
+
// The timings of the requests something listens to, set where the request enters the server and read wherever a phase runs.
|
|
112
|
+
const observedTimings = new WeakMap<Request, PhaseTiming>()
|
|
104
113
|
|
|
105
|
-
|
|
114
|
+
/** Starts measuring the phases of `request`, for the access log or an `onRequestEnd` hook. */
|
|
115
|
+
export function observeTiming(request: Request, isDev: boolean): PhaseTiming {
|
|
116
|
+
const timing: PhaseTiming = isDev ? { starts: {} } : { starts: {}, headerless: true }
|
|
117
|
+
observedTimings.set(request, timing)
|
|
118
|
+
return timing
|
|
119
|
+
}
|
|
106
120
|
|
|
107
|
-
|
|
121
|
+
/** Tells the access log and `onRequestEnd` hooks which route template answered `request`, when one listens. */
|
|
122
|
+
export function noteRoute(request: Request, route: string): void {
|
|
123
|
+
const timing = observedTimings.get(request)
|
|
108
124
|
if (timing !== undefined)
|
|
109
|
-
timing
|
|
125
|
+
timing.route = route
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export const createTiming = (isDev: boolean, request: Request): PhaseTiming | undefined => observedTimings.get(request) ?? (isDev ? {} : undefined)
|
|
129
|
+
|
|
130
|
+
export const startTimer = (timing: PhaseTiming | undefined): number => (timing !== undefined ? performance.now() : 0)
|
|
131
|
+
|
|
132
|
+
export function endTimer(timing: PhaseTiming | undefined, phase: RequestPhaseName, start: number): void {
|
|
133
|
+
if (timing === undefined)
|
|
134
|
+
return
|
|
135
|
+
timing[phase] = performance.now() - start
|
|
136
|
+
if (timing.starts !== undefined)
|
|
137
|
+
timing.starts[phase] = start
|
|
110
138
|
}
|
|
111
139
|
|
|
112
140
|
/** Completes an in-progress `timing.render` start timestamp and formats the `Server-Timing` header value. */
|
|
113
|
-
export function finishRenderTiming(timing:
|
|
141
|
+
export function finishRenderTiming(timing: PhaseTiming): string | undefined {
|
|
114
142
|
if (timing.render !== undefined)
|
|
115
|
-
timing
|
|
143
|
+
endTimer(timing, 'render', timing.render)
|
|
116
144
|
return formatServerTiming(timing)
|
|
117
145
|
}
|
|
118
146
|
|
|
119
147
|
/** Formats dev-only `Server-Timing` phases (returns `undefined` in production or when no phases ran). */
|
|
120
|
-
export function formatServerTiming(timing:
|
|
121
|
-
if (timing === undefined)
|
|
148
|
+
export function formatServerTiming(timing: PhaseTiming | undefined): string | undefined {
|
|
149
|
+
if (timing === undefined || timing.headerless === true)
|
|
122
150
|
return undefined
|
|
123
151
|
const parts: string[] = []
|
|
124
152
|
if (timing.middleware !== undefined)
|
|
@@ -137,7 +165,7 @@ export async function runTimedChain(
|
|
|
137
165
|
ctx: RequestContext,
|
|
138
166
|
middlewares: readonly Middleware[],
|
|
139
167
|
last: () => Response | Promise<Response>,
|
|
140
|
-
timing:
|
|
168
|
+
timing: PhaseTiming | undefined,
|
|
141
169
|
runChain: (ctx: RequestContext, middlewares: readonly Middleware[], last: () => Response | Promise<Response>) => Promise<Response>,
|
|
142
170
|
): Promise<Response> {
|
|
143
171
|
if (timing === undefined || middlewares.length === 0)
|
|
@@ -154,6 +182,8 @@ export async function runTimedChain(
|
|
|
154
182
|
}
|
|
155
183
|
})
|
|
156
184
|
timing.middleware = Math.max(0, performance.now() - chainStart - innerMs)
|
|
185
|
+
if (timing.starts !== undefined)
|
|
186
|
+
timing.starts.middleware = chainStart
|
|
157
187
|
return res
|
|
158
188
|
}
|
|
159
189
|
|
package/src/server/http.ts
CHANGED
|
@@ -5,10 +5,11 @@ import type { PrunelleChild } from '../jsx-runtime'
|
|
|
5
5
|
import type { ServerErrorInfo, ServerRuntimeConfig, ServiceKey } from '../plugins/index'
|
|
6
6
|
import type { NormalizedConsentConfig, RequestConsent } from './consent'
|
|
7
7
|
import type { Cookies } from './cookies'
|
|
8
|
-
import type { PrunelleLocals } from './
|
|
8
|
+
import type { PrunelleLocals, PrunelleRoutes } from './public'
|
|
9
9
|
import type { TrustProxy } from './security'
|
|
10
10
|
import { createRequestConsent } from './consent'
|
|
11
11
|
import { CookieStore } from './cookies'
|
|
12
|
+
import { cancelQuietly } from './discard'
|
|
12
13
|
import { nodeEnv } from './env'
|
|
13
14
|
import { resolveFallbackRequestId } from './health'
|
|
14
15
|
import { requestedRegion } from './region'
|
|
@@ -81,6 +82,9 @@ export type ExtractRouteParams<Path extends string> =
|
|
|
81
82
|
? Record<string, string>
|
|
82
83
|
: ExtractRouteParamsRec<Path>
|
|
83
84
|
|
|
85
|
+
/** A route pattern of the app (`'/posts/[slug]'`), or any string until prunelle has written the app's `prunelle-routes.d.ts`. */
|
|
86
|
+
export type RoutePattern = [keyof PrunelleRoutes] extends [never] ? string : Extract<keyof PrunelleRoutes, string>
|
|
87
|
+
|
|
84
88
|
export type LoaderFn = (...args: never[]) => unknown
|
|
85
89
|
|
|
86
90
|
/** Infers the resolved page `data` type returned by a route's `load()` function, excluding `Response` and `notFound()`. */
|
|
@@ -93,7 +97,7 @@ type InferLoaderParams<T extends LoaderFn> =
|
|
|
93
97
|
|
|
94
98
|
/** Props passed to a Prunelle route page component. Pass `typeof load` to infer `data` (and `params` when `load` uses `RouteContext<Path>`), or a route path string when there is no `load`. */
|
|
95
99
|
export type PageProps<
|
|
96
|
-
TLoadOrPath extends LoaderFn |
|
|
100
|
+
TLoadOrPath extends LoaderFn | RoutePattern | undefined = undefined,
|
|
97
101
|
TPath extends string = string,
|
|
98
102
|
> = {
|
|
99
103
|
params: TLoadOrPath extends string
|
|
@@ -147,6 +151,8 @@ export interface RequestContext<Params = Record<string, string>> {
|
|
|
147
151
|
cookies: Cookies
|
|
148
152
|
consent: RequestConsent
|
|
149
153
|
locals: PrunelleLocals
|
|
154
|
+
/** The parsed value of the route's `export const schema` (its JSON body, or its query for `GET` and `HEAD`): set before the handler runs, `undefined` when the route exports none. */
|
|
155
|
+
input?: unknown
|
|
150
156
|
ip?: string | undefined
|
|
151
157
|
/** Unique identifier for this request, echoed in the `x-request-id` response header and passed to `onError`. */
|
|
152
158
|
readonly requestId: string
|
|
@@ -160,15 +166,22 @@ export interface RequestContext<Params = Record<string, string>> {
|
|
|
160
166
|
}
|
|
161
167
|
|
|
162
168
|
/** `RequestContext` with `params` inferred from a Prunelle route pattern (e.g. `RouteContext<'/notices/[id]'>`). */
|
|
163
|
-
export type RouteContext<Path extends
|
|
169
|
+
export type RouteContext<Path extends RoutePattern = RoutePattern> = RequestContext<RoutePattern extends Path ? Record<string, string> : ExtractRouteParams<Path>>
|
|
164
170
|
|
|
165
171
|
export type Middleware = (ctx: RequestContext, next: () => Promise<Response>) => Response | Promise<Response>
|
|
166
172
|
|
|
167
173
|
export type PrunelleServer = ReturnType<typeof Bun.serve>
|
|
168
174
|
|
|
175
|
+
export interface ServeOptions {
|
|
176
|
+
/** Lets other processes listen on the same port (`SO_REUSEPORT`, Linux), the kernel spreading connections across them: `prunelle start --workers` sets it. */
|
|
177
|
+
reusePort?: boolean | undefined
|
|
178
|
+
}
|
|
179
|
+
|
|
169
180
|
export interface PrunelleApp {
|
|
170
181
|
handle: (request: Request, remoteIp?: string, setIdleTimeout?: (seconds: number) => void) => Promise<Response>
|
|
171
|
-
serve: (port?: number, hostname?: string) => PrunelleServer
|
|
182
|
+
serve: (port?: number, hostname?: string, options?: ServeOptions) => PrunelleServer
|
|
183
|
+
/** @internal What each process of this app keeps alone, one clause each, when several serve it with no `store`: `prunelle start --workers` warns with them. */
|
|
184
|
+
perProcessState?: () => string[]
|
|
172
185
|
/** @internal Swaps this app's handlers into a server another app started, with no dropped connection: `prunelle start` reloads a new build with it. */
|
|
173
186
|
reloadInto?: (server: PrunelleServer) => void
|
|
174
187
|
/** Looks up a server-scoped plugin service registered during `server(api)` (e.g. `JOBS_SERVICE`). */
|
|
@@ -323,7 +336,8 @@ const makeRedirect = (request: Request, trustProxy: TrustProxy, url: URL): Reque
|
|
|
323
336
|
if (!sameOrigin(location, baseOrigin))
|
|
324
337
|
throw new Error(`prunelle: ctx.redirect("${location}") leaves ${baseOrigin}: redirect to a path of this site, or pass { external: true } as the third argument when the app chose that address itself (never one read from the request, which makes an open redirect).`)
|
|
325
338
|
}
|
|
326
|
-
|
|
339
|
+
// A header holds Latin-1 at most, so a Unicode slug is sent percent-encoded.
|
|
340
|
+
const res = new Response(null, { status, headers: { location: location.toWellFormed().replace(/[^\x00-\x7F]+/g, chunk => encodeURI(chunk)) } })
|
|
327
341
|
if (external)
|
|
328
342
|
EXTERNAL_REDIRECTS.add(res)
|
|
329
343
|
return res
|
|
@@ -364,12 +378,18 @@ export function makeRequestContext(
|
|
|
364
378
|
request.headers.get('cookie'),
|
|
365
379
|
opts?.httpsSite === true || isHttpsRequest(request, trustProxy),
|
|
366
380
|
category => getConsent().has(category),
|
|
381
|
+
category => consentConfig?.categorySet.has(category) === true,
|
|
367
382
|
)
|
|
383
|
+
const locals: PrunelleLocals = {}
|
|
384
|
+
const resolveLocale = (): string | undefined => {
|
|
385
|
+
const locale = (locals as { locale?: unknown }).locale
|
|
386
|
+
return typeof locale === 'string' && locale !== '' ? locale : undefined
|
|
387
|
+
}
|
|
368
388
|
const getConsent = (): RequestConsent => {
|
|
369
389
|
const raw = consentConfig !== undefined ? cookieStore.get(consentConfig.cookieName) : undefined
|
|
370
390
|
if (cachedConsent === undefined || raw !== cachedConsentRaw) {
|
|
371
391
|
cachedConsentRaw = raw
|
|
372
|
-
cachedConsent = createRequestConsent(raw, consentConfig)
|
|
392
|
+
cachedConsent = createRequestConsent(raw, consentConfig, resolveLocale)
|
|
373
393
|
}
|
|
374
394
|
return cachedConsent
|
|
375
395
|
}
|
|
@@ -385,7 +405,7 @@ export function makeRequestContext(
|
|
|
385
405
|
get consent() {
|
|
386
406
|
return getConsent()
|
|
387
407
|
},
|
|
388
|
-
locals
|
|
408
|
+
locals,
|
|
389
409
|
requestId,
|
|
390
410
|
get ip() {
|
|
391
411
|
if (!ipComputed) {
|
|
@@ -462,10 +482,7 @@ export async function enforceRequestBodyLimit(
|
|
|
462
482
|
const { value } = read
|
|
463
483
|
total += value.byteLength
|
|
464
484
|
if (total > maxBytes) {
|
|
465
|
-
|
|
466
|
-
await reader.cancel()
|
|
467
|
-
}
|
|
468
|
-
catch {}
|
|
485
|
+
await cancelQuietly(reader)
|
|
469
486
|
return { ok: false }
|
|
470
487
|
}
|
|
471
488
|
chunks.push(value)
|