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.
Files changed (209) hide show
  1. package/llms.txt +68 -51
  2. package/package.json +7 -2
  3. package/src/a11y/declaration.ts +10 -17
  4. package/src/a11y/index.ts +69 -125
  5. package/src/animate.ts +127 -0
  6. package/src/build/asides.ts +51 -0
  7. package/src/build/bundle.ts +94 -136
  8. package/src/build/client-entries.ts +6 -6
  9. package/src/build/client-outputs.ts +75 -0
  10. package/src/build/client-plugin.ts +9 -4
  11. package/src/build/ecoindex.ts +4 -2
  12. package/src/build/files.ts +18 -0
  13. package/src/build/graph.ts +14 -6
  14. package/src/build/index.ts +23 -11
  15. package/src/build/island-promote.ts +42 -2
  16. package/src/build/islands.ts +63 -9
  17. package/src/build/key-types.ts +31 -0
  18. package/src/build/manifest.ts +117 -1
  19. package/src/build/plugin-host.ts +13 -1
  20. package/src/build/prerender.ts +8 -3
  21. package/src/build/reach.ts +18 -4
  22. package/src/build/report.ts +1 -1
  23. package/src/build/resolve.ts +5 -1
  24. package/src/build/route-types.ts +16 -0
  25. package/src/build/routes.ts +42 -15
  26. package/src/build/seed-ids.ts +65 -0
  27. package/src/build/seed-writes.ts +111 -0
  28. package/src/build/static-headers.ts +2 -2
  29. package/src/build/static.ts +1 -1
  30. package/src/cli/check-cache.ts +51 -0
  31. package/src/cli/check-consent.ts +95 -0
  32. package/src/cli/check-forms.ts +10 -2
  33. package/src/cli/check-links-patterns.ts +101 -0
  34. package/src/cli/check-links.ts +30 -105
  35. package/src/cli/check-regions.ts +185 -0
  36. package/src/cli/check-security.ts +50 -6
  37. package/src/cli/check.ts +49 -21
  38. package/src/cli/commands.ts +138 -16
  39. package/src/cli/docs.ts +21 -2
  40. package/src/cli/form-opaque-children.ts +40 -0
  41. package/src/cli/index.ts +1 -1
  42. package/src/cli/inspect.ts +2 -0
  43. package/src/cli/mcp-tools.ts +2 -2
  44. package/src/cli/new.ts +9 -5
  45. package/src/cli/upgrade-names.ts +14 -2
  46. package/src/cli/upgrade.ts +3 -0
  47. package/src/compiler/analyze.ts +22 -16
  48. package/src/compiler/cacheability-private.ts +13 -3
  49. package/src/compiler/cacheability.ts +24 -2
  50. package/src/compiler/compliance-contrast.ts +322 -0
  51. package/src/compiler/compliance-empty.ts +24 -0
  52. package/src/compiler/compliance-rgaa-content.ts +95 -0
  53. package/src/compiler/compliance-rgaa-criteria.ts +45 -0
  54. package/src/compiler/compliance-rgaa-pages.ts +140 -0
  55. package/src/compiler/compliance-rgaa.ts +19 -0
  56. package/src/compiler/compliance-rules.ts +72 -0
  57. package/src/compiler/compliance-third-party.ts +59 -0
  58. package/src/compiler/compliance-urls.ts +52 -0
  59. package/src/compiler/compliance.ts +61 -93
  60. package/src/compiler/cookie-inventory.ts +119 -0
  61. package/src/compiler/dom-attrs.ts +178 -22
  62. package/src/compiler/dom.ts +31 -44
  63. package/src/compiler/emit.ts +17 -6
  64. package/src/compiler/expr-context.ts +260 -207
  65. package/src/compiler/foreign-imports.ts +5 -3
  66. package/src/compiler/mistakes.ts +18 -9
  67. package/src/compiler/props-body.ts +263 -21
  68. package/src/compiler/props-pattern.ts +211 -0
  69. package/src/compiler/props.ts +324 -343
  70. package/src/compiler/signal-reads.ts +142 -29
  71. package/src/compiler/spread-handlers.ts +25 -13
  72. package/src/compiler/ssr-emit.ts +510 -0
  73. package/src/compiler/ssr-helpers.ts +165 -121
  74. package/src/compiler/ssr.ts +28 -441
  75. package/src/context.ts +14 -1
  76. package/src/dev/child.ts +1 -1
  77. package/src/dev/host-check.ts +2 -1
  78. package/src/dev/hot-islands.ts +71 -0
  79. package/src/dev/index.ts +31 -18
  80. package/src/dev/protocol.ts +1 -1
  81. package/src/dev/queue.ts +3 -2
  82. package/src/dev/reload.ts +5 -2
  83. package/src/dev/supervisor.ts +3 -1
  84. package/src/dev/watchers.ts +6 -2
  85. package/src/dom/action.ts +97 -14
  86. package/src/dom/attrs.ts +93 -25
  87. package/src/dom/extras.ts +91 -50
  88. package/src/dom/field-marks.ts +32 -0
  89. package/src/dom/hot.ts +9 -0
  90. package/src/dom/index.ts +44 -18
  91. package/src/dom/island-hot.ts +29 -0
  92. package/src/dom/island.ts +13 -2
  93. package/src/dom/loop.ts +259 -188
  94. package/src/dom/prefetch.ts +135 -0
  95. package/src/dom/regions.ts +515 -0
  96. package/src/dom/swap.ts +246 -0
  97. package/src/dom/trusted.ts +22 -0
  98. package/src/dom/upload.ts +44 -0
  99. package/src/eco/declaration.ts +44 -41
  100. package/src/eco/index.ts +8 -5
  101. package/src/href.ts +31 -0
  102. package/src/index.ts +12 -5
  103. package/src/plugins/config.ts +1 -1
  104. package/src/plugins/context.ts +7 -1
  105. package/src/plugins/daily.ts +27 -0
  106. package/src/plugins/index.ts +3 -3
  107. package/src/plugins/order.ts +1 -1
  108. package/src/plugins/public.ts +12 -5
  109. package/src/plugins/services.ts +88 -14
  110. package/src/plugins/types.ts +128 -10
  111. package/src/reactivity/graph.ts +244 -41
  112. package/src/reactivity/hmr.ts +101 -0
  113. package/src/reactivity/index.ts +243 -239
  114. package/src/reactivity/resource.ts +129 -0
  115. package/src/reactivity/seed.ts +141 -0
  116. package/src/reactivity/selector.ts +85 -47
  117. package/src/reactivity/server-seed.ts +202 -0
  118. package/src/reactivity/store.ts +331 -97
  119. package/src/rgpd/draft.ts +171 -0
  120. package/src/rgpd/forms.ts +85 -0
  121. package/src/rgpd/index.ts +57 -0
  122. package/src/server/action-flash.ts +1 -0
  123. package/src/server/action-protect.ts +3 -0
  124. package/src/server/action-registry.ts +11 -9
  125. package/src/server/action.ts +51 -19
  126. package/src/server/app-secret.ts +14 -8
  127. package/src/server/cache-invalidation.ts +97 -0
  128. package/src/server/compress.ts +55 -18
  129. package/src/server/consent.ts +21 -5
  130. package/src/server/cookie-inventory.ts +78 -0
  131. package/src/server/cookies.ts +77 -8
  132. package/src/server/cors.ts +98 -0
  133. package/src/server/csp-report.ts +90 -0
  134. package/src/server/discard.ts +13 -0
  135. package/src/server/document.ts +2 -2
  136. package/src/server/env.ts +1 -1
  137. package/src/server/errors.ts +9 -2
  138. package/src/server/field-errors.ts +91 -0
  139. package/src/server/form-schema.ts +16 -11
  140. package/src/server/health.ts +41 -11
  141. package/src/server/http.ts +28 -11
  142. package/src/server/index.ts +122 -307
  143. package/src/server/loader.ts +2 -0
  144. package/src/server/page.ts +3 -9
  145. package/src/server/plugin-host.ts +66 -16
  146. package/src/server/plugin-route-shadow.ts +24 -0
  147. package/src/server/public.ts +14 -3
  148. package/src/server/rate-limit.ts +49 -10
  149. package/src/server/region.ts +22 -2
  150. package/src/server/request-handler.ts +406 -0
  151. package/src/server/request-log.ts +94 -0
  152. package/src/server/route-cache.ts +55 -8
  153. package/src/server/route-input.ts +54 -0
  154. package/src/server/route-table.ts +53 -27
  155. package/src/server/router.ts +2 -1
  156. package/src/server/routing.ts +104 -45
  157. package/src/server/sanitize-html.ts +4 -2
  158. package/src/server/security.ts +38 -8
  159. package/src/server/serve-handlers.ts +16 -6
  160. package/src/server/served-file.ts +19 -14
  161. package/src/server/shared-store.ts +323 -0
  162. package/src/server/start.ts +12 -5
  163. package/src/server/static-dir.ts +34 -12
  164. package/src/server/stream.ts +2 -1
  165. package/src/server/surface.ts +39 -0
  166. package/src/server/webhook.ts +50 -0
  167. package/src/server/workers.ts +168 -0
  168. package/src/ssr/consent-banner.ts +20 -16
  169. package/src/ssr/consent-texts.ts +40 -0
  170. package/src/ssr/context.ts +174 -53
  171. package/src/ssr/cookie-table.ts +75 -0
  172. package/src/ssr/index.ts +63 -58
  173. package/src/ssr/navigation.ts +161 -14
  174. package/src/ssr/region.ts +9 -3
  175. package/src/style.ts +12 -5
  176. package/src/test/browser.ts +53 -23
  177. package/src/test/build-reuse.ts +85 -0
  178. package/src/test/index.ts +51 -8
  179. package/src/test/mount.ts +53 -0
  180. package/src/test/outside.ts +94 -0
  181. package/src/ts-plugin/diagnostics.ts +56 -18
  182. package/templates/_shared/AGENTS.md +1 -1
  183. package/templates/_shared/Dockerfile +22 -0
  184. package/templates/_shared/dockerignore +9 -0
  185. package/templates/_shared/gitignore +2 -0
  186. package/templates/app/.env.example +3 -1
  187. package/templates/app/compose.yaml +32 -0
  188. package/templates/app/db/migrations/001_notes.sql +7 -0
  189. package/templates/app/db/schema.ts +11 -0
  190. package/templates/app/package.json +1 -0
  191. package/templates/app/prunelle.config.ts +6 -0
  192. package/templates/app/src/actions/notes.ts +26 -0
  193. package/templates/app/src/auth.ts +4 -2
  194. package/templates/app/src/db.ts +5 -0
  195. package/templates/app/src/routes/dashboard/index.tsx +26 -1
  196. package/templates/app/src/routes/privacy.tsx +6 -2
  197. package/templates/app/tests/app.test.ts +14 -3
  198. package/templates/plugin/AGENTS.md +11 -0
  199. package/templates/plugin/CLAUDE.md +1 -0
  200. package/templates/plugin/README.md +27 -0
  201. package/templates/plugin/gitignore +2 -0
  202. package/templates/plugin/llms.txt +19 -0
  203. package/templates/plugin/package.json +33 -0
  204. package/templates/plugin/src/index.ts +40 -0
  205. package/templates/plugin/tests/fixture/prunelle.config.ts +4 -0
  206. package/templates/plugin/tests/fixture/src/routes/index.tsx +11 -0
  207. package/templates/plugin/tests/plugin.test.ts +42 -0
  208. package/templates/plugin/tsconfig.json +19 -0
  209. package/ts-plugin.cjs +4058 -2246
@@ -1,13 +1,18 @@
1
1
  // Per-request SSR context for <Head> metadata, streaming <Suspense>, rendered island tracking,
2
2
  // universal createContext/useContext, and deterministic hydration-safe useId().
3
3
 
4
- import type { CatchProps, ConsentBannerProps, ConsentGateProps, ConsentTexts } from '../context'
4
+ import type { CatchProps, ConsentBannerProps, ConsentGateProps, ConsentTexts, CookieTableProps } from '../context'
5
5
  import type { Html, PrunelleChild } from '../jsx-runtime'
6
+ import type { RequestSeeds } from '../reactivity/index'
6
7
  import type { RequestConsent } from '../server/consent'
7
8
  import type { FieldConstraint } from '../server/form-schema'
8
9
  import { AsyncLocalStorage } from 'node:async_hooks'
9
10
  import { registerServerContextBridge } from '../context'
11
+ import { getAppContext } from '../plugins/context'
12
+ import { COOKIE_INVENTORY } from '../server/cookie-inventory'
13
+ import { currentRequest } from '../server/request-scope'
10
14
  import { renderConsentBanner, renderConsentPreferences } from './consent-banner'
15
+ import { renderCookieTable } from './cookie-table'
11
16
  import { raw, ssrChild } from './index'
12
17
  import { Region } from './region'
13
18
 
@@ -27,21 +32,27 @@ export interface PendingChunk {
27
32
 
28
33
  export interface RenderContext {
29
34
  headParts: string[]
30
- headKeys: Set<string>
35
+ headKeys: Set<string> | undefined
31
36
  title: string | undefined
32
37
  pending: PendingChunk[]
33
38
  nextId: number
34
39
  islandDepth: number
35
40
  islandSeq: number
36
41
  idPrefix: string
42
+ /** Starts every `useId` and island prefix of a region answer, so its ids differ from the page's and from the other answers' already in the document. */
43
+ idScope: string
37
44
  idSeq: number
38
45
  contexts: Map<symbol, unknown>
39
46
  /** While an island renders, the contexts provided outside it, which the browser never sees. */
40
47
  outerContexts: Map<symbol, unknown> | undefined
41
48
  /** The island rendering, as its tag. */
42
49
  island: string | undefined
50
+ /** What the request's islands wrote to module state, which each island's server render starts from. */
51
+ seeds: RequestSeeds | undefined
43
52
  /** Maps island chunk URL -> eager (true if hydrate is undefined/'load'). */
44
53
  renderedIslands: Map<string, boolean>
54
+ /** True on fast-static routes whose island preload chunks were already precomputed into the document shell. */
55
+ skipIslandMap: boolean
45
56
  hasIslands: boolean
46
57
  /** True once an island's props hold a tagged value (a Date, Map, Set…), whose page preloads the revivers. */
47
58
  revives: boolean
@@ -50,6 +61,8 @@ export interface RenderContext {
50
61
  region: string | undefined
51
62
  /** The requested region's HTML, once the page rendered it. */
52
63
  regionHtml: string | undefined
64
+ /** The requested region's own attributes as its tag renders them (` class="a"`), sent so a swap keeps the element as the new page has it. */
65
+ regionAttrs: string
53
66
  regionNames: Set<string>
54
67
  /** The regions around the requested one: rendered through, their own HTML dropped. */
55
68
  regionAncestors: ReadonlySet<string>
@@ -116,6 +129,93 @@ function activeScope(ctx: RenderContext): RenderScope {
116
129
  }
117
130
  }
118
131
 
132
+ const EMPTY_SET: ReadonlySet<string> = new Set()
133
+ const EMPTY_STRINGS: string[] = Object.freeze([]) as unknown as string[]
134
+ const EMPTY_PENDING: PendingChunk[] = Object.freeze([]) as unknown as PendingChunk[]
135
+ const EMPTY_CONTEXTS: Map<symbol, unknown> = new Map()
136
+ const EMPTY_ISLANDS: Map<string, boolean> = new Map()
137
+ const EMPTY_HTML_ATTRS: Record<string, string> = Object.freeze({})
138
+
139
+ class RenderContextImpl implements RenderContext {
140
+ headParts: string[] = EMPTY_STRINGS
141
+ headKeys: Set<string> | undefined = undefined
142
+ title: string | undefined = undefined
143
+ pending: PendingChunk[] = EMPTY_PENDING
144
+ nextId = 0
145
+ islandDepth = 0
146
+ islandSeq = 0
147
+ idPrefix: string
148
+ idScope: string
149
+ idSeq = 0
150
+ contexts: Map<symbol, unknown> = EMPTY_CONTEXTS
151
+ outerContexts: Map<symbol, unknown> | undefined = undefined
152
+ island: string | undefined = undefined
153
+ seeds: RequestSeeds | undefined = undefined
154
+ renderedIslands: Map<string, boolean> = EMPTY_ISLANDS
155
+ skipIslandMap: boolean
156
+ hasIslands = false
157
+ revives = false
158
+ htmlAttrs: Record<string, string> = EMPTY_HTML_ATTRS
159
+ region: string | undefined
160
+ regionHtml: string | undefined = undefined
161
+ regionAttrs = ''
162
+ #regionNames: Set<string> | undefined = undefined
163
+ regionAncestors: ReadonlySet<string>
164
+ inTarget = false
165
+ inlineStyles: string[] | undefined = undefined
166
+ headersFlushed = false
167
+ headFlushed = false
168
+ warnedStreamStyle = false
169
+ warnedStreamHead = false
170
+ aborted = false
171
+ consent: RequestConsent | undefined
172
+ onCaught: ((error: unknown) => void) | undefined
173
+ formActions: Map<string, unknown> | undefined = undefined
174
+
175
+ constructor(
176
+ region: string | undefined,
177
+ ancestors: ReadonlySet<string>,
178
+ consent: RequestConsent | undefined,
179
+ onCaught: ((error: unknown) => void) | undefined,
180
+ skipIslandMap: boolean,
181
+ ) {
182
+ // A region answer is swapped or appended into a document that already holds ids from its own render, so it gets a scope of its own.
183
+ const idScope = region === undefined ? '' : `r${Math.random().toString(36).slice(2, 8)}-`
184
+ this.idPrefix = idScope
185
+ this.idScope = idScope
186
+ this.region = region
187
+ this.regionAncestors = ancestors
188
+ this.consent = consent
189
+ this.onCaught = onCaught
190
+ this.skipIslandMap = skipIslandMap
191
+ }
192
+
193
+ get regionNames(): Set<string> {
194
+ return this.#regionNames ??= new Set()
195
+ }
196
+
197
+ set regionNames(value: Set<string>) {
198
+ this.#regionNames = value
199
+ }
200
+
201
+ hasRenderedRegions(): boolean {
202
+ return this.#regionNames !== undefined && this.#regionNames.size > 0
203
+ }
204
+ }
205
+
206
+ /** True when `<Region>` actually rendered during this request, without materializing an empty `Set`. */
207
+ export function hasRenderedRegions(ctx: RenderContext): boolean {
208
+ const probe = ctx as RenderContext & { hasRenderedRegions?: () => boolean }
209
+ return typeof probe.hasRenderedRegions === 'function' ? probe.hasRenderedRegions() : ctx.regionNames.size > 0
210
+ }
211
+
212
+ /** Returns a mutable per-request island map, replacing the shared empty sentinel on first write. */
213
+ export function ensureRenderedIslands(ctx: RenderContext): Map<string, boolean> {
214
+ if (ctx.renderedIslands.size === 0)
215
+ ctx.renderedIslands = new Map()
216
+ return ctx.renderedIslands
217
+ }
218
+
119
219
  export function lookupFormAction(url: string): unknown {
120
220
  const ctx = renderStorage.getStore()
121
221
  if (ctx !== undefined)
@@ -125,7 +225,10 @@ export function lookupFormAction(url: string): unknown {
125
225
 
126
226
  export function currentFormFields(): ReadonlyMap<string, FieldConstraint> | undefined {
127
227
  const ctx = renderStorage.getStore()
128
- return ctx !== undefined ? activeScope(ctx).formFields : fallbackFormFields
228
+ if (ctx === undefined)
229
+ return fallbackFormFields
230
+ const scope = scopeStorage.getStore()
231
+ return scope !== undefined && scope.owner === ctx ? scope.formFields : undefined
129
232
  }
130
233
 
131
234
  export function runWithFormFields<T>(fields: ReadonlyMap<string, FieldConstraint>, fn: () => T): T {
@@ -148,7 +251,8 @@ export function runWithFormFields<T>(fields: ReadonlyMap<string, FieldConstraint
148
251
  }
149
252
 
150
253
  export function isInTarget(ctx: RenderContext): boolean {
151
- return activeScope(ctx).inTarget
254
+ const scope = scopeStorage.getStore()
255
+ return scope !== undefined && scope.owner === ctx ? scope.inTarget : ctx.inTarget
152
256
  }
153
257
 
154
258
  export function runInTarget<T>(ctx: RenderContext, fn: () => T): T {
@@ -160,11 +264,14 @@ registerServerContextBridge({
160
264
  const ctx = renderStorage.getStore()
161
265
  if (ctx === undefined)
162
266
  return undefined
163
- const scope = activeScope(ctx)
164
- if (scope.contexts.has(key))
165
- return { found: true, value: scope.contexts.get(key) }
166
- if (scope.outerContexts?.has(key) === true)
167
- throw new Error(`prunelle: <${scope.island}> reads a context provided outside the island, which the browser never sees; pass its value as a prop.`)
267
+ const scope = scopeStorage.getStore()
268
+ const own = scope !== undefined && scope.owner === ctx
269
+ const contexts = own ? scope.contexts : ctx.contexts
270
+ if (contexts.has(key))
271
+ return { found: true, value: contexts.get(key) }
272
+ const outer = own ? scope.outerContexts : ctx.outerContexts
273
+ if (outer?.has(key) === true)
274
+ throw new Error(`prunelle: <${own ? scope.island : ctx.island}> reads a context provided outside the island, which the browser never sees; pass its value as a prop.`)
168
275
  return undefined
169
276
  },
170
277
  runWithContext<T>(key: symbol, value: unknown, fn: () => T): T {
@@ -182,6 +289,9 @@ registerServerContextBridge({
182
289
  return undefined
183
290
  return `${ctx.idPrefix}${ctx.idSeq++}`
184
291
  },
292
+ requestLocale() {
293
+ return requestLocale()
294
+ },
185
295
  region(props) {
186
296
  return Region(props)
187
297
  },
@@ -194,6 +304,9 @@ registerServerContextBridge({
194
304
  consentPreferences(props) {
195
305
  return ConsentPreferences(props)
196
306
  },
307
+ cookieTable(props) {
308
+ return CookieTable(props)
309
+ },
197
310
  head(props) {
198
311
  return Head(props)
199
312
  },
@@ -207,42 +320,12 @@ registerServerContextBridge({
207
320
 
208
321
  export function createRenderContext(
209
322
  region?: string,
210
- ancestors: ReadonlySet<string> = new Set(),
323
+ ancestors: ReadonlySet<string> = EMPTY_SET,
211
324
  consent?: RequestConsent,
212
325
  onCaught?: (error: unknown) => void,
326
+ skipIslandMap = false,
213
327
  ): RenderContext {
214
- return {
215
- headParts: [],
216
- headKeys: new Set(),
217
- title: undefined,
218
- pending: [],
219
- nextId: 0,
220
- islandDepth: 0,
221
- islandSeq: 0,
222
- idPrefix: '',
223
- idSeq: 0,
224
- contexts: new Map(),
225
- outerContexts: undefined,
226
- island: undefined,
227
- renderedIslands: new Map(),
228
- hasIslands: false,
229
- revives: false,
230
- htmlAttrs: {},
231
- region,
232
- regionHtml: undefined,
233
- regionNames: new Set(),
234
- regionAncestors: ancestors,
235
- inTarget: false,
236
- inlineStyles: undefined,
237
- headersFlushed: false,
238
- headFlushed: false,
239
- warnedStreamStyle: false,
240
- warnedStreamHead: false,
241
- aborted: false,
242
- consent,
243
- onCaught,
244
- formActions: undefined,
245
- }
328
+ return new RenderContextImpl(region, ancestors, consent, onCaught, skipIslandMap)
246
329
  }
247
330
 
248
331
  /** Records a compiler-emitted inline `<style>` block's CSS in the active RenderContext for CSP hashing. */
@@ -267,10 +350,30 @@ export function ssrStyleBlock(css: string): string {
267
350
  return css
268
351
  }
269
352
 
353
+ /** True when an island needs a fresh `scopeStorage` entry to isolate outer `Context.Provider` values. */
354
+ export function needsIslandScope(ctx: RenderContext): boolean {
355
+ const scope = scopeStorage.getStore()
356
+ const cur = scope !== undefined && scope.owner === ctx ? scope : undefined
357
+ const contexts = cur !== undefined ? cur.contexts : ctx.contexts
358
+ return contexts.size > 0 || (cur !== undefined ? cur.outerContexts : ctx.outerContexts) !== undefined
359
+ }
360
+
270
361
  // An island sees only the contexts provided inside it, as in the browser.
271
362
  export function renderIsland(ctx: RenderContext, name: string, render: () => string): string {
272
- const cur = activeScope(ctx)
273
- return scopeStorage.run({ ...cur, contexts: new Map(), outerContexts: cur.contexts, island: name }, render)
363
+ const scope = scopeStorage.getStore()
364
+ const cur = scope !== undefined && scope.owner === ctx ? scope : undefined
365
+ const contexts = cur !== undefined ? cur.contexts : ctx.contexts
366
+ if (contexts.size === 0 && (cur !== undefined ? cur.outerContexts : ctx.outerContexts) === undefined)
367
+ return render()
368
+ const base = cur ?? {
369
+ owner: ctx,
370
+ contexts,
371
+ outerContexts: ctx.outerContexts,
372
+ island: ctx.island,
373
+ inTarget: ctx.inTarget,
374
+ formFields: undefined,
375
+ }
376
+ return scopeStorage.run({ ...base, contexts: EMPTY_CONTEXTS, outerContexts: contexts, island: name }, render)
274
377
  }
275
378
 
276
379
 
@@ -345,7 +448,7 @@ export function Catch(props: CatchProps): Html | Promise<Html> {
345
448
  try {
346
449
  const children = props.children as PrunelleChild | Promise<PrunelleChild>
347
450
  if (children instanceof Promise)
348
- return children.then(resolved => raw(ssrChild(resolved)), fail)
451
+ return children.then(resolved => raw(ssrChild(resolved))).catch(fail)
349
452
  return raw(ssrChild(children))
350
453
  }
351
454
  catch (error) {
@@ -360,12 +463,21 @@ export function ConsentGate(props: ConsentGateProps): Html {
360
463
  return raw(ssrChild(granted ? props.children : props.fallback))
361
464
  }
362
465
 
466
+ function requestLocale(): string | undefined {
467
+ const locale = (currentRequest()?.ctx.locals as { locale?: unknown } | undefined)?.locale
468
+ return typeof locale === 'string' && locale !== '' ? locale : undefined
469
+ }
470
+
363
471
  export function ConsentBanner(props: ConsentBannerProps): Html {
364
- return raw(renderConsentBanner(renderStorage.getStore()?.consent, props))
472
+ return raw(renderConsentBanner(renderStorage.getStore()?.consent, props, requestLocale()))
365
473
  }
366
474
 
367
475
  export function ConsentPreferences(props: ConsentTexts): Html {
368
- return raw(renderConsentPreferences(renderStorage.getStore()?.consent, props))
476
+ return raw(renderConsentPreferences(renderStorage.getStore()?.consent, props, requestLocale()))
477
+ }
478
+
479
+ export function CookieTable(props: CookieTableProps): Html {
480
+ return raw(renderCookieTable(getAppContext()?.inject(COOKIE_INVENTORY), props, requestLocale()))
369
481
  }
370
482
 
371
483
  const STACK_FRAME_RE = /^\s*at\s+(?:async\s+)?([A-Za-z_$][\w$]*)\s+\(/
@@ -389,7 +501,8 @@ export function Head(props: { children?: PrunelleChild }): Html {
389
501
  if (ctx === undefined)
390
502
  return raw('')
391
503
 
392
- if (ctx.headersFlushed || ctx.headFlushed || activeScope(ctx).inSuspense === true) {
504
+ const scope = scopeStorage.getStore()
505
+ if (ctx.headersFlushed || ctx.headFlushed || (scope !== undefined && scope.owner === ctx && scope.inSuspense === true)) {
393
506
  if (!ctx.warnedStreamHead) {
394
507
  ctx.warnedStreamHead = true
395
508
  const component = callerComponentName()
@@ -409,9 +522,13 @@ export function Head(props: { children?: PrunelleChild }): Html {
409
522
  html = html.replace(TITLE_RE, '')
410
523
  }
411
524
  if (html !== '')
412
- html = dedupeHeadTags(html, ctx.headKeys)
413
- if (html !== '')
414
- ctx.headParts.push(html)
525
+ html = dedupeHeadTags(html, ctx.headKeys ??= new Set())
526
+ if (html !== '') {
527
+ if (ctx.headParts.length === 0)
528
+ ctx.headParts = [html]
529
+ else
530
+ ctx.headParts.push(html)
531
+ }
415
532
 
416
533
  return raw('')
417
534
  }
@@ -443,12 +560,16 @@ export function Suspense(props: { fallback?: PrunelleChild, error?: PrunelleChil
443
560
  }
444
561
  let discarded = false
445
562
  const isAborted = (): boolean => ctx.aborted || discarded
446
- ctx.pending.push({
563
+ const entry: PendingChunk = {
447
564
  id,
448
565
  settled: settle(ctx, scope, child, renderError, isAborted),
449
566
  renderError,
450
567
  discard: () => { discarded = true },
451
- })
568
+ }
569
+ if (ctx.pending.length === 0)
570
+ ctx.pending = [entry]
571
+ else
572
+ ctx.pending.push(entry)
452
573
 
453
574
  const fallbackHtml = ssrChild(props.fallback)
454
575
  return raw(`<pru-s id="k-s-${id}">${fallbackHtml}</pru-s>`)
@@ -0,0 +1,75 @@
1
+ // The markup of <CookieTable>: one row per cookie the site sets, in the app's language, with no script.
2
+
3
+ import type { CookieTableProps } from '../context'
4
+ import type { CookieInventory, CookieRow } from '../server/cookie-inventory'
5
+ import { esc } from './index'
6
+
7
+ interface CookieTableTexts {
8
+ caption: string
9
+ name: string
10
+ purpose: string
11
+ category: string
12
+ lifetime: string
13
+ essential: string
14
+ session: string
15
+ /** A field the source does not give, such as `[to fill in: purpose]`. */
16
+ missing: (field: string) => string
17
+ }
18
+
19
+ const ENGLISH: CookieTableTexts = {
20
+ caption: 'Cookies this site sets',
21
+ name: 'Name',
22
+ purpose: 'Purpose',
23
+ category: 'Category',
24
+ lifetime: 'Lifetime',
25
+ essential: 'Essential',
26
+ session: 'Browser session',
27
+ missing: field => `[to fill in: ${field}]`,
28
+ }
29
+
30
+ const FRENCH: CookieTableTexts = {
31
+ caption: 'Cookies déposés par ce site',
32
+ name: 'Nom',
33
+ purpose: 'Finalité',
34
+ category: 'Catégorie',
35
+ lifetime: 'Durée de conservation',
36
+ essential: 'Nécessaire',
37
+ session: 'Session du navigateur',
38
+ missing: field => `[à compléter : ${field}]`,
39
+ }
40
+
41
+ const FRENCH_FIELDS: Readonly<Record<string, string>> = { name: 'nom', purpose: 'finalité', category: 'catégorie', lifetime: 'durée' }
42
+
43
+ // The largest unit the lifetime is a whole number of, a month counted as 30 days and a year as 365; 30 days stays 30 days rather than 1 month.
44
+ const UNITS = [['year', 31_536_000, 1], ['month', 2_592_000, 2], ['day', 86_400, 1], ['hour', 3600, 1], ['minute', 60, 1]] as const
45
+
46
+ function formatLifetime(seconds: number, lang: string): string {
47
+ const [unit, size] = UNITS.find(([, size, least]) => seconds >= size * least && seconds % size === 0) ?? ['second', 1]
48
+ return new Intl.NumberFormat(lang, { style: 'unit', unit, unitDisplay: 'long' }).format(seconds / size)
49
+ }
50
+
51
+ function purposeIn(purpose: CookieRow['purpose'], lang: string): string | undefined {
52
+ if (purpose === undefined || typeof purpose === 'string')
53
+ return purpose
54
+ return purpose[lang] ?? purpose[lang.split('-', 1)[0]!] ?? purpose.en ?? Object.values(purpose)[0]
55
+ }
56
+
57
+ export function renderCookieTable(inventory: CookieInventory | undefined, props: CookieTableProps, lang?: string): string {
58
+ if (inventory === undefined)
59
+ return ''
60
+ const activeLang = lang ?? inventory.lang
61
+ const french = activeLang.split('-', 1)[0]!.toLowerCase() === 'fr'
62
+ const texts = french ? FRENCH : ENGLISH
63
+ const missing = (field: string): string => esc(texts.missing(french ? FRENCH_FIELDS[field]! : field))
64
+ const rows = inventory.rows.map((row) => {
65
+ const purpose = purposeIn(row.purpose, activeLang)
66
+ const category = row.category === 'essential' ? texts.essential : row.category === undefined ? undefined : inventory.labels[row.category] ?? row.category
67
+ const lifetime = row.lifetime === 'session' ? texts.session : row.lifetime === undefined ? undefined : formatLifetime(row.lifetime, activeLang)
68
+ return `<tr><td>${row.name === undefined ? missing('name') : `<code>${esc(row.name)}</code>`}</td>`
69
+ + `<td>${purpose === undefined ? missing('purpose') : esc(purpose)}</td>`
70
+ + `<td>${category === undefined ? missing('category') : esc(category)}</td>`
71
+ + `<td>${lifetime === undefined ? missing('lifetime') : esc(lifetime)}</td></tr>`
72
+ })
73
+ const head = [texts.name, texts.purpose, texts.category, texts.lifetime].map(label => `<th scope="col">${esc(label)}</th>`).join('')
74
+ return `<table data-ui="table"><caption>${esc(props.caption ?? texts.caption)}</caption><thead><tr>${head}</tr></thead><tbody>${rows.join('')}</tbody></table>`
75
+ }