prunelle 0.1.1 → 0.2.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 +12 -12
- package/package.json +1 -1
- package/src/build/client-entries.ts +3 -2
- package/src/build/compile.ts +2 -1
- package/src/build/index.ts +68 -29
- package/src/build/manifest.ts +18 -1
- package/src/build/output.ts +2 -2
- package/src/build/precompress.ts +15 -2
- package/src/build/prerender.ts +2 -2
- package/src/build/routes.ts +171 -10
- package/src/build/static.ts +71 -17
- package/src/build/status-pages.ts +127 -0
- package/src/cli/check-forms.ts +654 -0
- package/src/cli/check-links.ts +619 -0
- package/src/cli/check.ts +37 -4
- package/src/cli/commands.ts +5 -3
- package/src/cli/inspect.ts +21 -2
- package/src/cli/upgrade-names.ts +73 -13
- package/src/cli/upgrade.ts +137 -16
- package/src/compiler/compliance-rgaa.ts +502 -0
- package/src/compiler/compliance.ts +119 -220
- package/src/compiler/ssr.ts +121 -24
- package/src/dom/action.ts +4 -1
- package/src/dom/island.ts +5 -5
- package/src/plugins/config.ts +1 -1
- package/src/plugins/types.ts +13 -0
- package/src/server/action-protect.ts +186 -0
- package/src/server/action.ts +33 -10
- package/src/server/errors.ts +291 -62
- package/src/server/form-schema.ts +343 -0
- package/src/server/health.ts +244 -0
- package/src/server/http.ts +84 -17
- package/src/server/index.ts +162 -91
- package/src/server/param-matchers.ts +112 -0
- package/src/server/plugin-host.ts +40 -14
- package/src/server/public.ts +3 -3
- package/src/server/route-table.ts +5 -2
- package/src/server/router.ts +125 -40
- package/src/server/routing.ts +95 -57
- package/src/server/security.ts +171 -22
- package/src/server/serve-handlers.ts +21 -7
- package/src/ssr/context.ts +49 -0
- package/src/ssr/index.ts +120 -1
- package/src/test/browser.ts +6 -0
- package/src/test/index.ts +17 -3
- package/templates/_shared/src/routes/legal.tsx +5 -0
- package/templates/blog/prunelle.config.ts +1 -1
- package/templates/blog/src/routes/index.tsx +4 -0
package/src/server/http.ts
CHANGED
|
@@ -10,6 +10,7 @@ import type { TrustProxy } from './security'
|
|
|
10
10
|
import { createRequestConsent } from './consent'
|
|
11
11
|
import { CookieStore } from './cookies'
|
|
12
12
|
import { nodeEnv } from './env'
|
|
13
|
+
import { resolveFallbackRequestId } from './health'
|
|
13
14
|
import { requestedRegion } from './region'
|
|
14
15
|
import { getClientIp, isHttpsRequest } from './security'
|
|
15
16
|
|
|
@@ -31,21 +32,41 @@ export function isNotFound(value: unknown): value is NotFoundResult {
|
|
|
31
32
|
return typeof value === 'object' && value !== null && (value as Record<symbol, unknown>)[NOT_FOUND_SYMBOL] === true
|
|
32
33
|
}
|
|
33
34
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
: Seg extends `*${infer Name}`
|
|
40
|
-
? { [K in Name extends '' ? 'rest' : Name]: string }
|
|
41
|
-
: Seg extends `:${infer Name}`
|
|
42
|
-
? { [K in Name]: string }
|
|
43
|
-
: unknown
|
|
44
|
-
|
|
45
|
-
type MergeParams<A, B> = {
|
|
46
|
-
[K in keyof A | keyof B]: K extends keyof B ? B[K] : K extends keyof A ? A[K] : never
|
|
35
|
+
/** Maps built-in (and augmented) route param matcher names to their TypeScript param value types. */
|
|
36
|
+
export interface ParamMatcherTypes {
|
|
37
|
+
integer: `${number}`
|
|
38
|
+
uuid: `${string}-${string}-${string}-${string}-${string}`
|
|
39
|
+
slug: string
|
|
47
40
|
}
|
|
48
41
|
|
|
42
|
+
type MatcherValue<Name extends string> = Name extends keyof ParamMatcherTypes ? ParamMatcherTypes[Name] : string
|
|
43
|
+
|
|
44
|
+
type ParseParamSpec<Spec extends string> =
|
|
45
|
+
Spec extends `${infer Name}=${infer Matcher}`
|
|
46
|
+
? [Name, MatcherValue<Matcher>]
|
|
47
|
+
: [Spec, string]
|
|
48
|
+
|
|
49
|
+
type SegmentParam<Seg extends string> =
|
|
50
|
+
Seg extends `(${string})`
|
|
51
|
+
? unknown
|
|
52
|
+
: Seg extends `[[${infer Spec}]]`
|
|
53
|
+
? { [K in ParseParamSpec<Spec>[0]]?: ParseParamSpec<Spec>[1] | undefined }
|
|
54
|
+
: Seg extends `[...${infer Spec}]`
|
|
55
|
+
? { [K in ParseParamSpec<Spec>[0]]: string }
|
|
56
|
+
: Seg extends `[${infer Spec}]`
|
|
57
|
+
? { [K in ParseParamSpec<Spec>[0]]: ParseParamSpec<Spec>[1] }
|
|
58
|
+
: Seg extends `*${infer Spec}`
|
|
59
|
+
? { [K in ParseParamSpec<Spec>[0] extends '' ? 'rest' : ParseParamSpec<Spec>[0]]: string }
|
|
60
|
+
: Seg extends `:${infer Spec}?`
|
|
61
|
+
? { [K in ParseParamSpec<Spec>[0]]?: ParseParamSpec<Spec>[1] | undefined }
|
|
62
|
+
: Seg extends `:${infer Spec}`
|
|
63
|
+
? { [K in ParseParamSpec<Spec>[0]]: ParseParamSpec<Spec>[1] }
|
|
64
|
+
: unknown
|
|
65
|
+
|
|
66
|
+
type MergeParams<A, B> = unknown extends B
|
|
67
|
+
? A
|
|
68
|
+
: { [K in keyof (Omit<A, keyof B> & B)]: (Omit<A, keyof B> & B)[K] }
|
|
69
|
+
|
|
49
70
|
type ExtractRouteParamsRec<Path extends string, Acc = {}> =
|
|
50
71
|
Path extends `${infer Seg}/${infer Rest}`
|
|
51
72
|
? ExtractRouteParamsRec<Rest, Seg extends '' ? Acc : MergeParams<Acc, SegmentParam<Seg>>>
|
|
@@ -53,7 +74,7 @@ type ExtractRouteParamsRec<Path extends string, Acc = {}> =
|
|
|
53
74
|
? Acc
|
|
54
75
|
: MergeParams<Acc, SegmentParam<Path>>
|
|
55
76
|
|
|
56
|
-
/** Extracts typed route params from a Prunelle route path (`'/notices/[id]'` -> `{ id: string }`, `'/docs/[...rest]'` -> `{ rest: string }`). */
|
|
77
|
+
/** Extracts typed route params from a Prunelle route path (`'/notices/[id=integer]'` -> `{ id: `${number}` }`, `'/[[lang]]/about'` -> `{ lang?: string | undefined }`, `'/docs/[...rest]'` -> `{ rest: string }`). */
|
|
57
78
|
export type ExtractRouteParams<Path extends string> =
|
|
58
79
|
string extends Path
|
|
59
80
|
? Record<string, string>
|
|
@@ -91,6 +112,15 @@ export interface LayoutProps<Path extends string = string> {
|
|
|
91
112
|
url: URL
|
|
92
113
|
}
|
|
93
114
|
|
|
115
|
+
/** Props passed to a Prunelle status page (`_401.tsx`, `_403.tsx`, `_404.tsx`, `_429.tsx`, `_503.tsx`). */
|
|
116
|
+
export interface StatusPageProps<Path extends string = string> {
|
|
117
|
+
status: number
|
|
118
|
+
message: string
|
|
119
|
+
params: ExtractRouteParams<Path>
|
|
120
|
+
locals: PrunelleLocals
|
|
121
|
+
url: URL
|
|
122
|
+
}
|
|
123
|
+
|
|
94
124
|
export interface ServerOptions extends ServerRuntimeConfig {
|
|
95
125
|
root: string
|
|
96
126
|
outDir?: string | undefined
|
|
@@ -113,6 +143,8 @@ export interface RequestContext<Params = Record<string, string>> {
|
|
|
113
143
|
consent: RequestConsent
|
|
114
144
|
locals: PrunelleLocals
|
|
115
145
|
ip?: string | undefined
|
|
146
|
+
/** Unique identifier for this request, echoed in the `x-request-id` response header and passed to `onError`. */
|
|
147
|
+
readonly requestId: string
|
|
116
148
|
/** The region a `data-pru-target` link or form asked for alone (`x-pru-region`): `load()` can skip the data the others show. */
|
|
117
149
|
readonly region?: string | undefined
|
|
118
150
|
json: (data: unknown, init?: ResponseInit) => Response
|
|
@@ -210,6 +242,7 @@ export interface RequestContextOptions {
|
|
|
210
242
|
/** The parsed `request.url`, when the caller already has it. */
|
|
211
243
|
url?: URL | undefined
|
|
212
244
|
consent?: NormalizedConsentConfig | undefined
|
|
245
|
+
requestId?: string | undefined
|
|
213
246
|
}
|
|
214
247
|
|
|
215
248
|
// None of these read the request, so every context shares them.
|
|
@@ -230,8 +263,25 @@ const text: RequestContext['text'] = (body, init) => {
|
|
|
230
263
|
headers.set('content-length', String(Buffer.byteLength(body, 'utf8')))
|
|
231
264
|
return new Response(body, { ...init, headers })
|
|
232
265
|
}
|
|
266
|
+
export interface DeniedInfo {
|
|
267
|
+
status: number
|
|
268
|
+
message: string
|
|
269
|
+
fieldErrors?: Record<string, string> | undefined
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
const DENIED_RESPONSES = new WeakMap<Response, DeniedInfo>()
|
|
273
|
+
|
|
274
|
+
/** Metadata attached to a `Response` produced by `ctx.deny()`. */
|
|
275
|
+
export function getDeniedInfo(res: Response): DeniedInfo | undefined {
|
|
276
|
+
return DENIED_RESPONSES.get(res)
|
|
277
|
+
}
|
|
278
|
+
|
|
233
279
|
const redirect: RequestContext['redirect'] = (location, status = 302) => new Response(null, { status, headers: { location } })
|
|
234
|
-
const deny: RequestContext['deny'] = (status, message, fieldErrors) =>
|
|
280
|
+
const deny: RequestContext['deny'] = (status, message, fieldErrors) => {
|
|
281
|
+
const res = Response.json({ ok: false, status, error: message, fieldErrors }, { status })
|
|
282
|
+
DENIED_RESPONSES.set(res, { status, message, fieldErrors })
|
|
283
|
+
return res
|
|
284
|
+
}
|
|
235
285
|
|
|
236
286
|
/** RFC 9110 weak ETag comparison supporting `*`, comma-separated lists, and `W/` prefixes. */
|
|
237
287
|
export function etagMatches(ifNoneMatch: string | null, etag: string): boolean {
|
|
@@ -267,6 +317,7 @@ export function makeRequestContext(
|
|
|
267
317
|
cachedConsent ??= createRequestConsent(consentConfig !== undefined ? cookieStore.get(consentConfig.cookieName) : undefined, consentConfig)
|
|
268
318
|
return cachedConsent
|
|
269
319
|
}
|
|
320
|
+
const requestId = opts?.requestId ?? resolveFallbackRequestId(request, trustProxy)
|
|
270
321
|
let cachedIp: string | undefined
|
|
271
322
|
let ipComputed = false
|
|
272
323
|
const ctx: RequestContext = {
|
|
@@ -278,6 +329,7 @@ export function makeRequestContext(
|
|
|
278
329
|
return getConsent()
|
|
279
330
|
},
|
|
280
331
|
locals: {},
|
|
332
|
+
requestId,
|
|
281
333
|
get ip() {
|
|
282
334
|
if (!ipComputed) {
|
|
283
335
|
ipComputed = true
|
|
@@ -306,6 +358,13 @@ export function wantsJson(request: Request): boolean {
|
|
|
306
358
|
|| (request.headers.get('content-type') ?? '').includes('application/json')
|
|
307
359
|
}
|
|
308
360
|
|
|
361
|
+
/** True when the client explicitly accepts HTML and does not ask for JSON (e.g. browser navigation). */
|
|
362
|
+
export function acceptsHtml(request: Request): boolean {
|
|
363
|
+
if (wantsJson(request))
|
|
364
|
+
return false
|
|
365
|
+
return (request.headers.get('accept') ?? '').includes('text/html')
|
|
366
|
+
}
|
|
367
|
+
|
|
309
368
|
/** Enforces `maxBytes` on incoming request bodies (both Content-Length and streaming body reads). */
|
|
310
369
|
export async function enforceRequestBodyLimit(
|
|
311
370
|
request: Request,
|
|
@@ -396,14 +455,18 @@ export function applyHstsHeader(
|
|
|
396
455
|
}
|
|
397
456
|
}
|
|
398
457
|
|
|
399
|
-
/** Adds the pending cookies (when any)
|
|
458
|
+
/** Adds the pending cookies (when any), security headers, `x-request-id`, and dev `Server-Timing`. */
|
|
400
459
|
export function finalizeResponse(
|
|
401
460
|
res: Response,
|
|
402
461
|
cookieStore: CookieStore | undefined,
|
|
403
462
|
entries: readonly [string, string][],
|
|
463
|
+
requestId?: string,
|
|
464
|
+
serverTiming?: string,
|
|
404
465
|
): Response {
|
|
405
466
|
const hasCookies = cookieStore?.hasChanges === true
|
|
406
|
-
|
|
467
|
+
const hasRequestId = requestId !== undefined
|
|
468
|
+
const hasTiming = serverTiming !== undefined && serverTiming !== ''
|
|
469
|
+
if (!hasCookies && entries.length === 0 && !hasRequestId && !hasTiming)
|
|
407
470
|
return res
|
|
408
471
|
|
|
409
472
|
const apply = (h: Headers): void => {
|
|
@@ -413,6 +476,10 @@ export function finalizeResponse(
|
|
|
413
476
|
if (!h.has(k))
|
|
414
477
|
h.set(k, v)
|
|
415
478
|
}
|
|
479
|
+
if (hasRequestId && !h.has('x-request-id'))
|
|
480
|
+
h.set('x-request-id', requestId)
|
|
481
|
+
if (hasTiming)
|
|
482
|
+
h.set('server-timing', serverTiming)
|
|
416
483
|
}
|
|
417
484
|
// Mutate in place (no copy in Bun); a response with immutable headers (e.g. a proxied upstream fetch) is rebuilt.
|
|
418
485
|
try {
|