prunelle 0.1.1 → 0.5.1

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.
Files changed (88) hide show
  1. package/llms.txt +23 -19
  2. package/package.json +8 -2
  3. package/src/a11y/declaration.ts +335 -0
  4. package/src/a11y/index.ts +446 -0
  5. package/src/a11y/rgaa-map.ts +184 -0
  6. package/src/attrs.ts +11 -3
  7. package/src/build/bundle.ts +3 -2
  8. package/src/build/client-entries.ts +3 -2
  9. package/src/build/client-plugin.ts +8 -4
  10. package/src/build/client-prune.ts +11 -2
  11. package/src/build/compile.ts +2 -1
  12. package/src/build/ecoindex.ts +229 -0
  13. package/src/build/index.ts +121 -44
  14. package/src/build/manifest.ts +66 -5
  15. package/src/build/output.ts +2 -2
  16. package/src/build/plugin-host.ts +91 -33
  17. package/src/build/precompress.ts +15 -2
  18. package/src/build/prerender.ts +2 -2
  19. package/src/build/report.ts +23 -2
  20. package/src/build/resolve.ts +4 -3
  21. package/src/build/routes.ts +175 -10
  22. package/src/build/static.ts +71 -17
  23. package/src/build/status-pages.ts +127 -0
  24. package/src/build/style-bundles.ts +338 -0
  25. package/src/cli/check-forms.ts +654 -0
  26. package/src/cli/check-links.ts +619 -0
  27. package/src/cli/check.ts +52 -5
  28. package/src/cli/commands.ts +86 -4
  29. package/src/cli/docs.ts +402 -0
  30. package/src/cli/inspect.ts +31 -2
  31. package/src/cli/mcp-tools.ts +480 -0
  32. package/src/cli/mcp.ts +205 -0
  33. package/src/cli/new.ts +2 -2
  34. package/src/cli/render.ts +261 -0
  35. package/src/cli/upgrade-names.ts +73 -13
  36. package/src/cli/upgrade.ts +137 -16
  37. package/src/compiler/analyze.ts +17 -3
  38. package/src/compiler/cacheability.ts +592 -0
  39. package/src/compiler/compliance-rgaa.ts +502 -0
  40. package/src/compiler/compliance.ts +120 -221
  41. package/src/compiler/dev-source.ts +87 -0
  42. package/src/compiler/dom.ts +7 -7
  43. package/src/compiler/ssr.ts +135 -37
  44. package/src/dev/error-log.ts +151 -0
  45. package/src/dev/index.ts +60 -29
  46. package/src/dev/open-editor.ts +204 -0
  47. package/src/dev/reload.ts +5 -3
  48. package/src/dev/supervisor.ts +3 -0
  49. package/src/dom/action.ts +4 -1
  50. package/src/dom/island.ts +11 -10
  51. package/src/plugins/config.ts +4 -4
  52. package/src/plugins/index.ts +2 -2
  53. package/src/plugins/public.ts +5 -2
  54. package/src/plugins/services.ts +62 -0
  55. package/src/plugins/types.ts +32 -0
  56. package/src/server/action-protect.ts +198 -0
  57. package/src/server/action-registry.ts +1 -1
  58. package/src/server/action.ts +82 -27
  59. package/src/server/errors.ts +291 -62
  60. package/src/server/form-schema.ts +343 -0
  61. package/src/server/health.ts +244 -0
  62. package/src/server/http.ts +86 -19
  63. package/src/server/index.ts +158 -219
  64. package/src/server/param-matchers.ts +112 -0
  65. package/src/server/plugin-host.ts +40 -14
  66. package/src/server/public.ts +3 -3
  67. package/src/server/rate-limit.ts +58 -35
  68. package/src/server/request-body.ts +1 -1
  69. package/src/server/route-cache.ts +365 -0
  70. package/src/server/route-table.ts +302 -6
  71. package/src/server/router.ts +125 -40
  72. package/src/server/routing.ts +187 -61
  73. package/src/server/security.ts +208 -32
  74. package/src/server/serve-handlers.ts +21 -7
  75. package/src/ssr/context.ts +49 -0
  76. package/src/ssr/index.ts +120 -1
  77. package/src/test/browser.ts +6 -0
  78. package/src/test/index.ts +17 -3
  79. package/src/ts-plugin/diagnostics.ts +378 -0
  80. package/src/ts-plugin/fixes.ts +364 -0
  81. package/src/ts-plugin/index.ts +194 -0
  82. package/templates/_shared/AGENTS.md +2 -2
  83. package/templates/_shared/mcp.json +11 -0
  84. package/templates/_shared/src/routes/legal.tsx +5 -0
  85. package/templates/_shared/tsconfig.json +5 -0
  86. package/templates/blog/prunelle.config.ts +1 -1
  87. package/templates/blog/src/routes/index.tsx +4 -0
  88. package/ts-plugin.cjs +4277 -0
@@ -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
- type SegmentParam<Seg extends string> =
35
- Seg extends `[...${infer Name}]`
36
- ? { [K in Name]: string }
37
- : Seg extends `[${infer Name}]`
38
- ? { [K in Name]: string }
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) => Response.json({ ok: false, status, error: message, fieldErrors }, { status })
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
@@ -302,8 +354,15 @@ export function makeRequestContext(
302
354
 
303
355
  /** True when the client sent or asked for JSON: such requests get JSON errors, not pages. */
304
356
  export function wantsJson(request: Request): boolean {
305
- return (request.headers.get('accept') ?? '').includes('application/json')
306
- || (request.headers.get('content-type') ?? '').includes('application/json')
357
+ return (request.headers.get('accept') ?? '').toLowerCase().includes('application/json')
358
+ || (request.headers.get('content-type') ?? '').toLowerCase().includes('application/json')
359
+ }
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') ?? '').toLowerCase().includes('text/html')
307
366
  }
308
367
 
309
368
  /** Enforces `maxBytes` on incoming request bodies (both Content-Length and streaming body reads). */
@@ -396,14 +455,18 @@ export function applyHstsHeader(
396
455
  }
397
456
  }
398
457
 
399
- /** Adds the pending cookies (when any) and the security headers the response does not set itself. */
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
- if (!hasCookies && entries.length === 0)
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 {