prunelle 0.7.3 → 0.7.7
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 +19 -17
- package/package.json +1 -1
- package/src/a11y/index.ts +9 -2
- package/src/build/index.ts +30 -31
- package/src/build/output.ts +1 -1
- package/src/build/prerender.ts +29 -13
- package/src/build/routes.ts +19 -1
- package/src/build/static.ts +24 -2
- package/src/cli/check-forms.ts +16 -6
- package/src/cli/check-links.ts +12 -9
- package/src/cli/check-security.ts +87 -12
- package/src/cli/check.ts +1 -1
- package/src/cli/commands.ts +3 -3
- package/src/cli/upgrade.ts +36 -3
- package/src/compiler/compliance.ts +2 -0
- package/src/compiler/dom.ts +41 -15
- package/src/compiler/ssr-helpers.ts +45 -21
- package/src/compiler/ssr.ts +32 -16
- package/src/context.ts +2 -0
- package/src/dev/error-log.ts +1 -1
- package/src/dev/index.ts +1 -1
- package/src/dev/supervisor.ts +2 -2
- package/src/dom/attrs.ts +1 -1
- package/src/dom/island.ts +30 -4
- package/src/jsx-runtime.ts +1 -1
- package/src/plugins/context.ts +18 -1
- package/src/plugins/services.ts +1 -0
- package/src/reactivity/index.ts +14 -2
- package/src/server/action.ts +26 -18
- package/src/server/assets.ts +22 -4
- package/src/server/consent.ts +7 -2
- package/src/server/cookies.ts +8 -1
- package/src/server/errors.ts +57 -26
- package/src/server/http.ts +1 -1
- package/src/server/index.ts +18 -16
- package/src/server/page.ts +4 -0
- package/src/server/rate-limit.ts +36 -12
- package/src/server/route-cache.ts +9 -10
- package/src/server/route-table.ts +38 -13
- package/src/server/router.ts +4 -4
- package/src/server/sanitize-html.ts +21 -7
- package/src/server/security.ts +13 -3
- package/src/server/serve-handlers.ts +3 -3
- package/src/server/start.ts +2 -2
- package/src/server/static-dir.ts +3 -10
- package/src/server/stream.ts +4 -4
- package/src/ssr/consent-banner.ts +3 -1
- package/src/ssr/context.ts +85 -2
- package/src/ssr/index.ts +16 -4
- package/src/ssr/navigation.ts +1 -1
- package/src/style.ts +9 -7
- package/src/unrenderable.ts +15 -5
package/src/ssr/context.ts
CHANGED
|
@@ -27,6 +27,7 @@ export interface PendingChunk {
|
|
|
27
27
|
|
|
28
28
|
export interface RenderContext {
|
|
29
29
|
headParts: string[]
|
|
30
|
+
headKeys: Set<string>
|
|
30
31
|
title: string | undefined
|
|
31
32
|
pending: PendingChunk[]
|
|
32
33
|
nextId: number
|
|
@@ -58,8 +59,12 @@ export interface RenderContext {
|
|
|
58
59
|
inlineStyles: string[] | undefined
|
|
59
60
|
/** True once HTTP headers (and CSP) have been flushed for a streamed response. */
|
|
60
61
|
headersFlushed: boolean
|
|
62
|
+
/** True once `<head>` has been collected for the document shell (subsequent `<Head>` calls in `<Suspense>` are dropped). */
|
|
63
|
+
headFlushed: boolean
|
|
61
64
|
/** True once the post-flush inline `<style>` warning has been emitted for this request. */
|
|
62
65
|
warnedStreamStyle: boolean
|
|
66
|
+
/** True once the post-flush `<Head>` warning has been emitted for this request. */
|
|
67
|
+
warnedStreamHead: boolean
|
|
63
68
|
/** True once the stream/region finished, timed out, was aborted, or was discarded (e.g. HEAD). */
|
|
64
69
|
aborted: boolean
|
|
65
70
|
/** Request consent state used by `<ConsentGate>`, `<ConsentBanner>` and `<ConsentPreferences>`. */
|
|
@@ -76,6 +81,7 @@ interface RenderScope {
|
|
|
76
81
|
outerContexts: ReadonlyMap<symbol, unknown> | undefined
|
|
77
82
|
island: string | undefined
|
|
78
83
|
inTarget: boolean
|
|
84
|
+
inSuspense?: boolean
|
|
79
85
|
formFields: ReadonlyMap<string, FieldConstraint> | undefined
|
|
80
86
|
}
|
|
81
87
|
|
|
@@ -207,6 +213,7 @@ export function createRenderContext(
|
|
|
207
213
|
): RenderContext {
|
|
208
214
|
return {
|
|
209
215
|
headParts: [],
|
|
216
|
+
headKeys: new Set(),
|
|
210
217
|
title: undefined,
|
|
211
218
|
pending: [],
|
|
212
219
|
nextId: 0,
|
|
@@ -228,7 +235,9 @@ export function createRenderContext(
|
|
|
228
235
|
inTarget: false,
|
|
229
236
|
inlineStyles: undefined,
|
|
230
237
|
headersFlushed: false,
|
|
238
|
+
headFlushed: false,
|
|
231
239
|
warnedStreamStyle: false,
|
|
240
|
+
warnedStreamHead: false,
|
|
232
241
|
aborted: false,
|
|
233
242
|
consent,
|
|
234
243
|
onCaught,
|
|
@@ -274,6 +283,53 @@ export function currentRenderContext(): RenderContext | undefined {
|
|
|
274
283
|
}
|
|
275
284
|
|
|
276
285
|
const TITLE_RE = /<title\b[^>]*>([\s\S]*?)<\/title>/gi
|
|
286
|
+
const HEAD_DEDUPE_TAG_RE = /<(meta|link)\b((?:"[^"]*"|'[^']*'|[^>"'])*)>/gi
|
|
287
|
+
const HEAD_ATTR_RE = /([a-zA-Z_:][-a-zA-Z0-9_:.]*)(?:\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s"'=<>`]+)))?/g
|
|
288
|
+
|
|
289
|
+
function headTagDedupeKey(tag: string, attrs: string): string | undefined {
|
|
290
|
+
let name = ''
|
|
291
|
+
let property = ''
|
|
292
|
+
let rel = ''
|
|
293
|
+
HEAD_ATTR_RE.lastIndex = 0
|
|
294
|
+
let m: RegExpExecArray | null
|
|
295
|
+
while ((m = HEAD_ATTR_RE.exec(attrs)) !== null) {
|
|
296
|
+
const attrName = m[1]!.toLowerCase()
|
|
297
|
+
const attrVal = (m[2] ?? m[3] ?? m[4] ?? '').trim().toLowerCase()
|
|
298
|
+
if (attrName === 'name')
|
|
299
|
+
name = attrVal
|
|
300
|
+
else if (attrName === 'property')
|
|
301
|
+
property = attrVal
|
|
302
|
+
else if (attrName === 'rel')
|
|
303
|
+
rel = attrVal
|
|
304
|
+
}
|
|
305
|
+
if (tag === 'meta') {
|
|
306
|
+
if (name === 'description' || name.startsWith('og:') || name.startsWith('twitter:'))
|
|
307
|
+
return `meta:${name}`
|
|
308
|
+
if (property.startsWith('og:') || property.startsWith('twitter:'))
|
|
309
|
+
return `meta:${property}`
|
|
310
|
+
return undefined
|
|
311
|
+
}
|
|
312
|
+
if (tag === 'link' && rel.split(/\s+/).includes('canonical'))
|
|
313
|
+
return 'link:canonical'
|
|
314
|
+
return undefined
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
function dedupeHeadTags(html: string, seen: Set<string>): string {
|
|
318
|
+
const added: string[] = []
|
|
319
|
+
HEAD_DEDUPE_TAG_RE.lastIndex = 0
|
|
320
|
+
const out = html.replace(HEAD_DEDUPE_TAG_RE, (full, rawTag: string, attrs: string) => {
|
|
321
|
+
const key = headTagDedupeKey(rawTag.toLowerCase(), attrs)
|
|
322
|
+
if (key === undefined)
|
|
323
|
+
return full
|
|
324
|
+
if (seen.has(key))
|
|
325
|
+
return ''
|
|
326
|
+
added.push(key)
|
|
327
|
+
return full
|
|
328
|
+
})
|
|
329
|
+
for (const key of added)
|
|
330
|
+
seen.add(key)
|
|
331
|
+
return out
|
|
332
|
+
}
|
|
277
333
|
|
|
278
334
|
/** Renders `children`, or `fallback` when they throw or reject; the compiler hands it `children` as a getter, so they render inside the try. */
|
|
279
335
|
export function Catch(props: CatchProps): Html | Promise<Html> {
|
|
@@ -312,12 +368,37 @@ export function ConsentPreferences(props: ConsentTexts): Html {
|
|
|
312
368
|
return raw(renderConsentPreferences(renderStorage.getStore()?.consent, props))
|
|
313
369
|
}
|
|
314
370
|
|
|
315
|
-
|
|
371
|
+
const STACK_FRAME_RE = /^\s*at\s+(?:async\s+)?([A-Za-z_$][\w$]*)\s+\(/
|
|
372
|
+
const INTERNAL_HEAD_FRAME_RE = /^(?:callerComponentName|Head\d*|head|ssrChild|settle|runWithRenderContext|run)$/
|
|
373
|
+
|
|
374
|
+
function callerComponentName(): string | undefined {
|
|
375
|
+
const stack = new Error().stack
|
|
376
|
+
if (stack === undefined)
|
|
377
|
+
return undefined
|
|
378
|
+
for (const line of stack.split('\n').slice(1)) {
|
|
379
|
+
const match = STACK_FRAME_RE.exec(line)
|
|
380
|
+
if (match !== null && !INTERNAL_HEAD_FRAME_RE.test(match[1]!))
|
|
381
|
+
return match[1]!
|
|
382
|
+
}
|
|
383
|
+
return undefined
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
/** Collects document <head> tags during SSR; innermost <title>, description, og:*, twitter:* and canonical win. */
|
|
316
387
|
export function Head(props: { children?: PrunelleChild }): Html {
|
|
317
388
|
const ctx = renderStorage.getStore()
|
|
318
389
|
if (ctx === undefined)
|
|
319
390
|
return raw('')
|
|
320
391
|
|
|
392
|
+
if (ctx.headersFlushed || ctx.headFlushed || activeScope(ctx).inSuspense === true) {
|
|
393
|
+
if (!ctx.warnedStreamHead) {
|
|
394
|
+
ctx.warnedStreamHead = true
|
|
395
|
+
const component = callerComponentName()
|
|
396
|
+
const where = component !== undefined ? ` in ${component}` : ''
|
|
397
|
+
console.warn(`prunelle: <Head>${where} was rendered inside a streamed <Suspense> boundary after <head> was already sent, so its tags were dropped; move <Head> outside <Suspense> (into the page or layout).`)
|
|
398
|
+
}
|
|
399
|
+
return raw('')
|
|
400
|
+
}
|
|
401
|
+
|
|
321
402
|
let html = ssrChild(props.children)
|
|
322
403
|
TITLE_RE.lastIndex = 0
|
|
323
404
|
const titleMatch = TITLE_RE.exec(html)
|
|
@@ -327,6 +408,8 @@ export function Head(props: { children?: PrunelleChild }): Html {
|
|
|
327
408
|
TITLE_RE.lastIndex = 0
|
|
328
409
|
html = html.replace(TITLE_RE, '')
|
|
329
410
|
}
|
|
411
|
+
if (html !== '')
|
|
412
|
+
html = dedupeHeadTags(html, ctx.headKeys)
|
|
330
413
|
if (html !== '')
|
|
331
414
|
ctx.headParts.push(html)
|
|
332
415
|
|
|
@@ -348,7 +431,7 @@ export function Suspense(props: { fallback?: PrunelleChild, error?: PrunelleChil
|
|
|
348
431
|
return raw(ssrChild(props.fallback))
|
|
349
432
|
|
|
350
433
|
const id = ctx.nextId++
|
|
351
|
-
const scope = activeScope(ctx)
|
|
434
|
+
const scope: RenderScope = { ...activeScope(ctx), inSuspense: true }
|
|
352
435
|
// Capture RenderContext and RenderScope so timeout / error fallback renders still see outer Context.Provider and <Region> state.
|
|
353
436
|
const renderError = (error: unknown): string => {
|
|
354
437
|
try {
|
package/src/ssr/index.ts
CHANGED
|
@@ -10,7 +10,7 @@ import { extractFormSchema, lookupFieldConstraint } from '../server/form-schema'
|
|
|
10
10
|
import { styleText } from '../style'
|
|
11
11
|
import { assertRenderable } from '../unrenderable'
|
|
12
12
|
import { currentFormFields, currentRenderContext, lookupFormAction, PRUNELLE_STREAM_SCRIPT, renderIsland, runWithFormFields } from './context'
|
|
13
|
-
import { keepScrollTag } from './navigation'
|
|
13
|
+
import { KEEP_SCROLL_TAG, keepScrollTag } from './navigation'
|
|
14
14
|
export { ssrStyleBlock } from './context'
|
|
15
15
|
|
|
16
16
|
export const PRUNELLE_ISLAND_STYLE = 'pru-island,pru-slot{display:contents}'
|
|
@@ -122,8 +122,19 @@ export function raw(html: string): Html {
|
|
|
122
122
|
return { __html: html, [RAW_HTML]: true } as unknown as Html
|
|
123
123
|
}
|
|
124
124
|
|
|
125
|
+
/** HTML strips one leading newline after `<textarea>` and `<pre>`; double it so the parsed text keeps it. */
|
|
126
|
+
export function ssrLeadNl(html: string): string {
|
|
127
|
+
return html.startsWith('\n') || html.startsWith('\r') ? `\n${html}` : html
|
|
128
|
+
}
|
|
129
|
+
|
|
125
130
|
export function ssrAttr(name: string, value: unknown, tag?: string): string {
|
|
126
|
-
if (value == null || isBlockedAttr(name))
|
|
131
|
+
if (value == null || isBlockedAttr(name) || name === 'indeterminate')
|
|
132
|
+
return ''
|
|
133
|
+
if (name === 'checked' || name === 'selected' || name === 'muted') {
|
|
134
|
+
assertRenderable(value, name)
|
|
135
|
+
return value ? ` ${name}` : ''
|
|
136
|
+
}
|
|
137
|
+
if (name === 'value' && typeof value === 'boolean')
|
|
127
138
|
return ''
|
|
128
139
|
if (typeof value === 'boolean') {
|
|
129
140
|
if (isStringBoolAttr(name))
|
|
@@ -165,7 +176,7 @@ export function ssrSpread(props: Record<string, unknown> | null | undefined, tag
|
|
|
165
176
|
if (rawKey === 'dangerouslySetInnerHTML')
|
|
166
177
|
throw new TypeError(SPREAD_DANGEROUS_HTML)
|
|
167
178
|
const name = rawKey === 'className' ? 'class' : rawKey === 'htmlFor' ? 'for' : rawKey
|
|
168
|
-
if (isBlockedAttr(name))
|
|
179
|
+
if (isBlockedAttr(name) || ((tag === 'textarea' || tag === 'select') && name === 'value'))
|
|
169
180
|
continue
|
|
170
181
|
if (trackForm && value !== undefined) {
|
|
171
182
|
const bit = FORM_ATTR_BITS[name.toLowerCase()]
|
|
@@ -620,5 +631,6 @@ export function renderDocument(options: DocumentOptions): string {
|
|
|
620
631
|
}
|
|
621
632
|
}
|
|
622
633
|
|
|
623
|
-
|
|
634
|
+
const scrollTag = keepScrollTag(body) || (hasSuspense ? KEEP_SCROLL_TAG : '')
|
|
635
|
+
return `<!DOCTYPE html><html${attrStr}><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">${headExtra}</head><body>${body}${scrollTag}</body></html>`
|
|
624
636
|
}
|
package/src/ssr/navigation.ts
CHANGED
|
@@ -8,7 +8,7 @@ export const NO_PREFETCH_ATTR = 'data-pru-no-prefetch'
|
|
|
8
8
|
export type Eagerness = 'conservative' | 'moderate' | 'eager'
|
|
9
9
|
|
|
10
10
|
export interface NavigationConfig {
|
|
11
|
-
/** Fades one page into the next with a cross-document view transition (`@view-transition { navigation: auto }`), off under `prefers-reduced-motion`. On by default, `false` leaves the browser's plain repaint. */
|
|
11
|
+
/** Fades one page into the next with a cross-document view transition (`@view-transition { navigation: auto }`), off under `prefers-reduced-motion`. On by default, `false` leaves the browser's plain repaint, for `@prunellejs/spa` swaps too. */
|
|
12
12
|
viewTransitions?: boolean | undefined
|
|
13
13
|
/** When the browser fetches the page a same-origin link points at, through Speculation Rules: `'conservative'` (the default) on press, `'moderate'` on hover (and, on phones, as a link scrolls into view), `'eager'` as soon as it can, `false` never. A link, or an ancestor of it, with `data-pru-no-prefetch` is never fetched; neither is a `download` or `rel="nofollow"` link, a prunelle route (actions among them) or another origin's page. */
|
|
14
14
|
prefetch?: Eagerness | false | undefined
|
package/src/style.ts
CHANGED
|
@@ -3,14 +3,16 @@
|
|
|
3
3
|
// Properties that take a bare number; any other number gets `px`.
|
|
4
4
|
const UNITLESS = new Set([
|
|
5
5
|
'animation-iteration-count', 'aspect-ratio', 'border-image-outset', 'border-image-slice', 'border-image-width',
|
|
6
|
-
'
|
|
7
|
-
'
|
|
8
|
-
'
|
|
9
|
-
'
|
|
10
|
-
'
|
|
6
|
+
'box-flex', 'box-flex-group', 'box-ordinal-group', 'column-count', 'columns', 'fill-opacity', 'flex',
|
|
7
|
+
'flex-grow', 'flex-negative', 'flex-order', 'flex-positive', 'flex-shrink', 'flood-opacity', 'font-weight',
|
|
8
|
+
'grid-area', 'grid-column', 'grid-column-end', 'grid-column-span', 'grid-column-start', 'grid-row',
|
|
9
|
+
'grid-row-end', 'grid-row-span', 'grid-row-start', 'line-clamp', 'line-height', 'opacity', 'order', 'orphans',
|
|
10
|
+
'scale', 'stop-opacity', 'stroke-dasharray', 'stroke-dashoffset', 'stroke-miterlimit', 'stroke-opacity',
|
|
11
|
+
'stroke-width', 'tab-size', 'widows', 'z-index', 'zoom',
|
|
11
12
|
])
|
|
12
13
|
const UPPER = /[A-Z]/g
|
|
13
14
|
const MS_PREFIX = /^ms[A-Z]/
|
|
15
|
+
const VENDOR = /^-(?:webkit|moz|ms|o)-/
|
|
14
16
|
|
|
15
17
|
// `backgroundColor` becomes `background-color` and `msTransform` `-ms-transform`; a custom property keeps its case.
|
|
16
18
|
function cssName(key: string): string {
|
|
@@ -20,7 +22,7 @@ function cssName(key: string): string {
|
|
|
20
22
|
return MS_PREFIX.test(key) ? `-${name}` : name
|
|
21
23
|
}
|
|
22
24
|
|
|
23
|
-
const PROPERTY_NAME =
|
|
25
|
+
const PROPERTY_NAME = /^(?:--[\w-\u00A0-\uFFFF]+|-?[a-z][\w-]*)$/i
|
|
24
26
|
|
|
25
27
|
const NL = /[\n\r\f]/
|
|
26
28
|
const URL_FN = /(?:^|[^\w-])url$/i
|
|
@@ -89,7 +91,7 @@ export function styleText(style: Record<string, unknown>): string {
|
|
|
89
91
|
if (typeof value === 'number' ? !Number.isFinite(value) : !isOneValue(String(value)))
|
|
90
92
|
continue
|
|
91
93
|
const name = cssName(key)
|
|
92
|
-
const px = typeof value === 'number' && !name.startsWith('--') && !UNITLESS.has(name)
|
|
94
|
+
const px = typeof value === 'number' && !name.startsWith('--') && !UNITLESS.has(name.replace(VENDOR, ''))
|
|
93
95
|
out += `${name}:${value}${px ? 'px' : ''};`
|
|
94
96
|
}
|
|
95
97
|
return out
|
package/src/unrenderable.ts
CHANGED
|
@@ -6,7 +6,7 @@ function hasOwnToString(value: object): boolean {
|
|
|
6
6
|
return typeof toString === 'function' && toString !== Function.prototype.toString && toString !== Object.prototype.toString && toString !== Array.prototype.toString
|
|
7
7
|
}
|
|
8
8
|
|
|
9
|
-
function describe(value: unknown,
|
|
9
|
+
function describe(value: unknown, place: string): [string, string] | undefined {
|
|
10
10
|
if (typeof value === 'function')
|
|
11
11
|
return hasOwnToString(value) ? undefined : ['a function', 'call it, or pass it to an event prop']
|
|
12
12
|
if (typeof value !== 'object' || value === null)
|
|
@@ -17,19 +17,29 @@ function describe(value: unknown, asClass: boolean): [string, string] | undefine
|
|
|
17
17
|
return ['a signal', 'read its .value']
|
|
18
18
|
if ('__html' in value)
|
|
19
19
|
return ['a { __html } object', 'pass the string to innerHTML, or wrap trusted HTML in raw()']
|
|
20
|
-
if (
|
|
21
|
-
return ['an array', 'join the names into one string']
|
|
20
|
+
if (Array.isArray(value))
|
|
21
|
+
return place === 'class' ? ['an array', 'join the names into one string'] : undefined
|
|
22
|
+
if (value instanceof Date)
|
|
23
|
+
return ['a Date', 'format it with .toISOString() or Intl.DateTimeFormat']
|
|
24
|
+
if (value instanceof Set)
|
|
25
|
+
return ['a Set', 'spread it with [...set] and render each item']
|
|
26
|
+
if (value instanceof Map)
|
|
27
|
+
return ['a Map', 'iterate its entries and render each item']
|
|
22
28
|
if (hasOwnToString(value))
|
|
23
29
|
return undefined
|
|
30
|
+
if (place === 'text' && typeof Node !== 'undefined' && value instanceof Node)
|
|
31
|
+
return undefined
|
|
24
32
|
const proto = Object.getPrototypeOf(value)
|
|
25
33
|
if (proto === Object.prototype || proto === null)
|
|
26
34
|
return ['a plain object', 'render one of its fields']
|
|
27
|
-
|
|
35
|
+
const name = (proto as { constructor?: { name?: unknown } })?.constructor?.name
|
|
36
|
+
const label = typeof name === 'string' && name !== '' ? `a ${name} instance` : 'an object'
|
|
37
|
+
return [label, 'render one of its fields or define toString()']
|
|
28
38
|
}
|
|
29
39
|
|
|
30
40
|
/** Throws when `value` would print as `[object Object]`, `[object Promise]` or a function's source; `place` is `text` or an attribute name. */
|
|
31
41
|
export function assertRenderable(value: unknown, place: string): void {
|
|
32
|
-
const found = describe(value, place
|
|
42
|
+
const found = describe(value, place)
|
|
33
43
|
if (found !== undefined)
|
|
34
44
|
throw new TypeError(`prunelle: cannot render ${found[0]} as ${place === 'text' ? 'text' : `the ${place} attribute`}; ${found[1]}.`)
|
|
35
45
|
}
|