prunelle 0.6.0 → 0.7.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/LICENSE +21 -0
- package/llms.txt +48 -43
- package/package.json +4 -1
- package/src/a11y/declaration.ts +106 -72
- package/src/a11y/index.ts +59 -39
- package/src/a11y/rgaa-map.ts +95 -49
- package/src/build/bundle.ts +8 -4
- package/src/build/byte-size.ts +11 -0
- package/src/build/client-plugin.ts +3 -1
- package/src/build/compile.ts +2 -1
- package/src/build/ecoindex.ts +15 -14
- package/src/build/files.ts +6 -0
- package/src/build/first-load.ts +83 -11
- package/src/build/graph.ts +29 -39
- package/src/build/index.ts +57 -18
- package/src/build/island-promote.ts +229 -0
- package/src/build/islands.ts +9 -201
- package/src/build/manifest.ts +4 -125
- package/src/build/plugin-host.ts +21 -8
- package/src/build/precompress.ts +1 -1
- package/src/build/prerender.ts +97 -44
- package/src/build/reach.ts +136 -0
- package/src/build/render-rules.ts +2 -2
- package/src/build/report.ts +19 -3
- package/src/build/static-headers.ts +93 -0
- package/src/build/static.ts +2 -0
- package/src/cli/check-forms.ts +97 -48
- package/src/cli/check-imports.ts +25 -0
- package/src/cli/check-links.ts +2 -2
- package/src/cli/check-security.ts +64 -0
- package/src/cli/check.ts +33 -7
- package/src/cli/commands.ts +35 -7
- package/src/cli/docs.ts +4 -3
- package/src/cli/inspect.ts +2 -1
- package/src/cli/mcp-tools.ts +7 -5
- package/src/cli/mcp.ts +1 -1
- package/src/cli/new.ts +14 -3
- package/src/cli/render.ts +14 -19
- package/src/cli/upgrade.ts +22 -5
- package/src/compiler/analyze.ts +26 -5
- package/src/compiler/cacheability.ts +2 -2
- package/src/compiler/compliance-eco.ts +71 -0
- package/src/compiler/compliance-rgaa.ts +110 -4
- package/src/compiler/compliance-urls.ts +145 -0
- package/src/compiler/compliance.ts +168 -116
- package/src/compiler/foreign-imports.ts +88 -0
- package/src/compiler/mistakes.ts +19 -1
- package/src/compiler/props-body.ts +52 -0
- package/src/compiler/shared.ts +2 -2
- package/src/compiler/ssr.ts +2 -2
- package/src/context.ts +34 -0
- package/src/dev/error-log.ts +90 -64
- package/src/dev/host-check.ts +26 -0
- package/src/dev/index.ts +12 -5
- package/src/dev/open-editor.ts +15 -11
- package/src/dev/protocol.ts +2 -0
- package/src/dev/supervisor.ts +8 -0
- package/src/dev/watchers.ts +27 -2
- package/src/dom/action.ts +4 -1
- package/src/dom/extras.ts +1 -2
- package/src/dom/index.ts +13 -2
- package/src/dom/island.ts +22 -10
- package/src/dom/loop.ts +25 -16
- package/src/eco/declaration.ts +257 -0
- package/src/eco/index.ts +65 -0
- package/src/index.ts +2 -2
- package/src/jsx-runtime.ts +2 -2
- package/src/plugins/config.ts +1 -1
- package/src/plugins/context.ts +16 -1
- package/src/plugins/load.ts +4 -3
- package/src/plugins/public.ts +2 -2
- package/src/plugins/services.ts +3 -0
- package/src/plugins/types.ts +17 -2
- package/src/server/action-flash.ts +158 -0
- package/src/server/action-protect.ts +38 -37
- package/src/server/action.ts +11 -127
- package/src/server/app-secret.ts +63 -0
- package/src/server/compress.ts +5 -1
- package/src/server/consent.ts +59 -22
- package/src/server/cookies.ts +16 -2
- package/src/server/env.ts +14 -0
- package/src/server/errors.ts +3 -1
- package/src/server/form-schema.ts +1 -1
- package/src/server/health.ts +46 -15
- package/src/server/http.ts +47 -7
- package/src/server/index.ts +52 -30
- package/src/server/loader.ts +6 -2
- package/src/server/plugin-host.ts +3 -1
- package/src/server/public.ts +4 -3
- package/src/server/rate-limit.ts +52 -9
- package/src/server/route-cache.ts +26 -4
- package/src/server/route-table.ts +7 -3
- package/src/server/routing.ts +3 -87
- package/src/server/sanitize-html.ts +309 -0
- package/src/server/security.ts +71 -20
- package/src/server/serve-handlers.ts +14 -4
- package/src/server/start.ts +1 -1
- package/src/server/static-dir.ts +27 -4
- package/src/server/stream.ts +28 -6
- package/src/ssr/consent-banner.ts +58 -0
- package/src/ssr/context.ts +20 -4
- package/src/ssr/index.ts +18 -0
- package/src/ssr/navigation.ts +2 -2
- package/src/style.ts +32 -1
- package/templates/_shared/gitignore +5 -0
- package/templates/_shared/src/routes/legal.tsx +10 -1
- package/templates/app/.env.example +4 -0
- package/templates/app/package.json +2 -1
- package/templates/app/prunelle.config.ts +2 -0
- package/templates/app/src/auth.ts +4 -6
- package/templates/app/src/mail.ts +4 -0
- package/templates/app/src/routes/_layout.tsx +9 -1
- package/templates/app/src/routes/dashboard/index.tsx +42 -8
- package/templates/app/src/routes/login.tsx +49 -30
- package/templates/app/src/routes/privacy.tsx +25 -0
- package/templates/app/src/styles/app.css +8 -1
- package/templates/app/tests/app.test.ts +44 -3
- package/templates/blog/package.json +1 -1
- package/templates/blog/src/routes/_layout.tsx +6 -1
- package/templates/blog/src/routes/index.tsx +1 -1
- package/templates/blog/src/styles/app.css +6 -1
- package/templates/fullstack/package.json +1 -1
- package/templates/fullstack/src/components/GreetForm.tsx +2 -1
- package/templates/fullstack/src/routes/_layout.tsx +4 -0
- package/templates/fullstack/src/styles/app.css +11 -1
- package/templates/minimal/package.json +1 -1
- package/templates/minimal/src/routes/_layout.tsx +4 -0
- package/templates/minimal/src/styles/app.css +11 -1
package/src/server/static-dir.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// Standalone `--static` directory server (used when `outDir` holds exported HTML without `manifest.json`): every file is listed once at startup, so a request is a map lookup.
|
|
2
2
|
|
|
3
3
|
import type { FSWatcher } from 'node:fs'
|
|
4
|
-
import type { PrunellePlugin } from '../plugins/index'
|
|
4
|
+
import type { PrunellePlugin, ServerErrorInfo } from '../plugins/index'
|
|
5
5
|
import type { NavigationConfig } from '../ssr/navigation'
|
|
6
6
|
import type { PrunelleApp, PrunelleServer } from './http'
|
|
7
7
|
import type { SecurityHeadersOptions, TrustProxy } from './security'
|
|
@@ -12,6 +12,7 @@ import { isInside } from '../build/is-inside'
|
|
|
12
12
|
import { resolvePlugins } from '../plugins/index'
|
|
13
13
|
import { createNavigation, cspSha256 } from '../ssr/navigation'
|
|
14
14
|
import { CompressionCache, isCompressibleType } from './compress'
|
|
15
|
+
import { createErrorNotifier } from './errors'
|
|
15
16
|
import { applyHstsHeader, MIME } from './http'
|
|
16
17
|
import { decodeRequestPath } from './request-path'
|
|
17
18
|
import { buildSecurityHeaderEntries, inlineStyleHashes, PRUNELLE_ISLAND_STYLE_HASH, resolveCspPolicy, resolveHstsValue, withCspHashes } from './security'
|
|
@@ -30,6 +31,9 @@ const EXTRA_STATIC_MIME: Record<string, string> = {
|
|
|
30
31
|
export interface StaticDirExtraOptions {
|
|
31
32
|
navigation?: NavigationConfig | undefined
|
|
32
33
|
plugins?: readonly PrunellePlugin[] | undefined
|
|
34
|
+
/** The app's `onError` hook; plugin `onError` hooks run too. With no hook the error is logged. */
|
|
35
|
+
onError?: ((info: ServerErrorInfo) => void) | undefined
|
|
36
|
+
errorSink?: ((info: ServerErrorInfo) => void) | undefined
|
|
33
37
|
}
|
|
34
38
|
|
|
35
39
|
function findNotFound(rel: string | undefined, byDir: ReadonlyMap<string, ServedFile>): ServedFile | undefined {
|
|
@@ -117,7 +121,20 @@ export function createStaticDirServer(
|
|
|
117
121
|
const withHsts = hstsValue === undefined || isDev
|
|
118
122
|
? (res: Response) => res
|
|
119
123
|
: (res: Response, req: Request) => applyHstsHeader(res, req, hstsValue, trustProxy, isDev)
|
|
120
|
-
const
|
|
124
|
+
const serveFileUnguarded = createFileServer({ compressEnabled, compressCache, withHsts })
|
|
125
|
+
const notifyError = createErrorNotifier(extra?.onError, plugins, { dev: isDev, sink: extra?.errorSink })
|
|
126
|
+
// A file that fails to open answers here, where the request is known: gone since the listing is a 404, anything else is reported.
|
|
127
|
+
const serveFile = async (req: Request, file: ServedFile, status?: number): Promise<Response> => {
|
|
128
|
+
try {
|
|
129
|
+
return await serveFileUnguarded(req, file, status)
|
|
130
|
+
}
|
|
131
|
+
catch (err) {
|
|
132
|
+
if (err instanceof Error && 'code' in err && err.code === 'ENOENT')
|
|
133
|
+
return withHsts(new Response('Not Found', { status: 404, headers: { 'cache-control': 'no-store', ...securityRecord } }), req)
|
|
134
|
+
notifyError(err, req, 'route')
|
|
135
|
+
return error()
|
|
136
|
+
}
|
|
137
|
+
}
|
|
121
138
|
|
|
122
139
|
function entryFor(rel: string, path: string, all: ReadonlyMap<string, string>): ServedFile {
|
|
123
140
|
const ext = extname(rel).toLowerCase()
|
|
@@ -203,14 +220,20 @@ export function createStaticDirServer(
|
|
|
203
220
|
status: 500,
|
|
204
221
|
headers: { 'content-type': 'text/plain; charset=utf-8', ...securityRecord },
|
|
205
222
|
})
|
|
223
|
+
// What escapes `serveFile` (a body stream failing after its headers) reaches `Bun.serve` without a request: logged, never silent.
|
|
224
|
+
const lastResort = (err: Error): Response => {
|
|
225
|
+
console.error('prunelle start: unhandled error while serving a file', err)
|
|
226
|
+
return error()
|
|
227
|
+
}
|
|
206
228
|
|
|
207
229
|
return {
|
|
208
230
|
handle,
|
|
231
|
+
securityHeaders: securityRecord,
|
|
209
232
|
serve(port = 3000, hostname = '127.0.0.1') {
|
|
210
|
-
return Bun.serve({ port, hostname, idleTimeout: 30, development: false, fetch: handle, error })
|
|
233
|
+
return Bun.serve({ port, hostname, idleTimeout: 30, development: false, fetch: handle, error: lastResort })
|
|
211
234
|
},
|
|
212
235
|
reloadInto(server: PrunelleServer) {
|
|
213
|
-
server.reload({ routes: {}, fetch: handle, error })
|
|
236
|
+
server.reload({ routes: {}, fetch: handle, error: lastResort })
|
|
214
237
|
},
|
|
215
238
|
async dispose() {
|
|
216
239
|
clearTimeout(timer)
|
package/src/server/stream.ts
CHANGED
|
@@ -59,6 +59,7 @@ export function streamDocument(
|
|
|
59
59
|
streamTimeout: number,
|
|
60
60
|
onBoundaryError: (error: unknown) => void,
|
|
61
61
|
transformChunk?: (html: string) => string | Promise<string>,
|
|
62
|
+
tracker?: Pick<StreamTracker, 'trackPage'>,
|
|
62
63
|
): ReadableStream<Uint8Array> {
|
|
63
64
|
let splitAt = shell.lastIndexOf(STREAM_TAIL_MARK)
|
|
64
65
|
let tailOffset = STREAM_TAIL_MARK.length
|
|
@@ -73,9 +74,11 @@ export function streamDocument(
|
|
|
73
74
|
const abortCtrl = new AbortController()
|
|
74
75
|
let closed = false
|
|
75
76
|
const deadline = Date.now() + streamTimeout
|
|
77
|
+
// Shutdown stops waiting for the pending boundaries; the document still gets its tail.
|
|
78
|
+
let endedEarly = false
|
|
76
79
|
|
|
77
80
|
const safeEnqueue = (controller: ReadableStreamDefaultController<Uint8Array>, data: Uint8Array): void => {
|
|
78
|
-
if (closed || abortCtrl.signal.aborted)
|
|
81
|
+
if (closed || (abortCtrl.signal.aborted && !endedEarly))
|
|
79
82
|
return
|
|
80
83
|
try {
|
|
81
84
|
controller.enqueue(data)
|
|
@@ -89,6 +92,10 @@ export function streamDocument(
|
|
|
89
92
|
|
|
90
93
|
return new ReadableStream<Uint8Array>({
|
|
91
94
|
async start(controller) {
|
|
95
|
+
const untrack = tracker?.trackPage(() => {
|
|
96
|
+
endedEarly = true
|
|
97
|
+
abortCtrl.abort()
|
|
98
|
+
})
|
|
92
99
|
try {
|
|
93
100
|
safeEnqueue(controller, textEncoder.encode(openingHtml))
|
|
94
101
|
// A resolved boundary can render boundaries of its own, so each flush also drains the ones it added.
|
|
@@ -114,13 +121,14 @@ export function streamDocument(
|
|
|
114
121
|
}))
|
|
115
122
|
}
|
|
116
123
|
await flushPending()
|
|
117
|
-
if (!closed && !abortCtrl.signal.aborted) {
|
|
124
|
+
if (!closed && (endedEarly || !abortCtrl.signal.aborted)) {
|
|
118
125
|
safeEnqueue(controller, textEncoder.encode(tailHtml))
|
|
119
126
|
closed = true
|
|
120
127
|
safeClose(controller)
|
|
121
128
|
}
|
|
122
129
|
}
|
|
123
130
|
finally {
|
|
131
|
+
untrack?.()
|
|
124
132
|
rctx.aborted = true
|
|
125
133
|
}
|
|
126
134
|
},
|
|
@@ -197,14 +205,28 @@ function trackEventStream(res: Response, active: Set<() => void>, disableTimeout
|
|
|
197
205
|
return new Response(body, { status: res.status, statusText: res.statusText, headers: res.headers })
|
|
198
206
|
}
|
|
199
207
|
|
|
200
|
-
|
|
201
|
-
|
|
208
|
+
export interface StreamTracker {
|
|
209
|
+
track: (res: Response, disableTimeout?: () => void) => Response
|
|
210
|
+
/** Registers a streamed page's way to end early; the returned function drops it once the page is done. */
|
|
211
|
+
trackPage: (end: () => void) => () => void
|
|
212
|
+
closeAll: () => Promise<void>
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/** Tracks active SSE streams and streamed pages so graceful shutdown can end them before `server.stop()` drains in-flight requests. */
|
|
216
|
+
export function createStreamTracker(): StreamTracker {
|
|
202
217
|
const active = new Set<() => void>()
|
|
218
|
+
const pages = new Set<() => void>()
|
|
203
219
|
return {
|
|
204
|
-
track: (res
|
|
205
|
-
|
|
220
|
+
track: (res, disableTimeout) => trackEventStream(res, active, disableTimeout),
|
|
221
|
+
trackPage: (end) => {
|
|
222
|
+
pages.add(end)
|
|
223
|
+
return () => { pages.delete(end) }
|
|
224
|
+
},
|
|
225
|
+
closeAll: () => {
|
|
206
226
|
for (const close of [...active])
|
|
207
227
|
close()
|
|
228
|
+
for (const end of [...pages])
|
|
229
|
+
end()
|
|
208
230
|
return Promise.resolve()
|
|
209
231
|
},
|
|
210
232
|
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
// The markup of <ConsentBanner> and <ConsentPreferences>: one form posting to the consent endpoint, with no script.
|
|
2
|
+
|
|
3
|
+
import type { ConsentBannerProps, ConsentTexts } from '../context'
|
|
4
|
+
import type { RequestConsent } from '../server/consent'
|
|
5
|
+
import { esc, escAttr, ssrChild } from './index'
|
|
6
|
+
|
|
7
|
+
function requireCategories(consent: RequestConsent, tag: string): void {
|
|
8
|
+
if (consent.categories.length === 0)
|
|
9
|
+
throw new Error(`prunelle: <${tag}> has no consent category to ask about: declare them in prunelle.config.ts, such as consent: { categories: { analytics: 'Audience measurement' } }.`)
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function checkboxes(consent: RequestConsent, legend: string | undefined): string {
|
|
13
|
+
const boxes = consent.categories.map((category) => {
|
|
14
|
+
const checked = consent.has(category) ? ' checked' : ''
|
|
15
|
+
return `<label data-part="choice"><input type="checkbox" name="categories" value="${escAttr(category)}"${checked}> ${esc(consent.labels[category] ?? category)}</label>`
|
|
16
|
+
})
|
|
17
|
+
return `<fieldset data-part="choices"><legend>${esc(legend ?? 'Cookies by purpose')}</legend>${boxes.join('')}</fieldset>`
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// Refuse comes first and looks the same as Accept: the CNIL asks for a refusal as easy as a consent.
|
|
21
|
+
function decisionButtons(texts: ConsentTexts): string {
|
|
22
|
+
return `<button type="submit" name="action" value="reject-all" data-ui="button">${esc(texts.refuse ?? 'Refuse all')}</button>`
|
|
23
|
+
+ `<button type="submit" name="action" value="accept-all" data-ui="button">${esc(texts.accept ?? 'Accept all')}</button>`
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function saveButton(texts: ConsentTexts): string {
|
|
27
|
+
return `<button type="submit" name="action" value="save" data-ui="button">${esc(texts.save ?? 'Save my choices')}</button>`
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function renderConsentBanner(consent: RequestConsent | undefined, props: ConsentBannerProps): string {
|
|
31
|
+
if (consent === undefined)
|
|
32
|
+
return ''
|
|
33
|
+
requireCategories(consent, 'ConsentBanner')
|
|
34
|
+
if (consent.decided)
|
|
35
|
+
return ''
|
|
36
|
+
const labels = consent.categories.map(category => consent.labels[category] ?? category).join(', ')
|
|
37
|
+
const body = props.children !== undefined
|
|
38
|
+
? ssrChild(props.children)
|
|
39
|
+
: `<p>This site would like to set cookies for: ${esc(labels)}. You can accept or refuse them, and change your mind at any time.</p>`
|
|
40
|
+
const policy = props.policyHref !== undefined ? `<p><a href="${escAttr(props.policyHref)}">Cookie policy</a></p>` : ''
|
|
41
|
+
return `<section data-ui="consent" aria-labelledby="pru-consent-title">`
|
|
42
|
+
+ `<h2 id="pru-consent-title" data-part="title">${esc(props.title ?? 'Cookies')}</h2>`
|
|
43
|
+
+ `<div data-part="body">${body}${policy}</div>`
|
|
44
|
+
+ `<form method="post" action="${escAttr(consent.endpoint)}" data-part="form">`
|
|
45
|
+
+ `<div data-part="actions">${decisionButtons(props)}</div>`
|
|
46
|
+
+ `<details data-part="details"><summary>${esc(props.choose ?? 'Choose')}</summary>${checkboxes(consent, props.legend)}${saveButton(props)}</details>`
|
|
47
|
+
+ `</form></section>`
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function renderConsentPreferences(consent: RequestConsent | undefined, texts: ConsentTexts): string {
|
|
51
|
+
if (consent === undefined)
|
|
52
|
+
return ''
|
|
53
|
+
requireCategories(consent, 'ConsentPreferences')
|
|
54
|
+
return `<form method="post" action="${escAttr(consent.endpoint)}" data-ui="consent" data-part="preferences">`
|
|
55
|
+
+ `${checkboxes(consent, texts.legend)}`
|
|
56
|
+
+ `<div data-part="actions">${saveButton(texts)}${decisionButtons(texts)}</div>`
|
|
57
|
+
+ `</form>`
|
|
58
|
+
}
|
package/src/ssr/context.ts
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
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, ConsentGateProps } from '../context'
|
|
4
|
+
import type { CatchProps, ConsentBannerProps, ConsentGateProps, ConsentTexts } from '../context'
|
|
5
5
|
import type { Html, PrunelleChild } from '../jsx-runtime'
|
|
6
|
+
import type { RequestConsent } from '../server/consent'
|
|
6
7
|
import type { FieldConstraint } from '../server/form-schema'
|
|
7
8
|
import { AsyncLocalStorage } from 'node:async_hooks'
|
|
8
9
|
import { registerServerContextBridge } from '../context'
|
|
10
|
+
import { renderConsentBanner, renderConsentPreferences } from './consent-banner'
|
|
9
11
|
import { raw, ssrChild } from './index'
|
|
10
12
|
import { Region } from './region'
|
|
11
13
|
|
|
@@ -60,8 +62,8 @@ export interface RenderContext {
|
|
|
60
62
|
warnedStreamStyle: boolean
|
|
61
63
|
/** True once the stream/region finished, timed out, was aborted, or was discarded (e.g. HEAD). */
|
|
62
64
|
aborted: boolean
|
|
63
|
-
/** Request consent state used by `<ConsentGate>`. */
|
|
64
|
-
consent:
|
|
65
|
+
/** Request consent state used by `<ConsentGate>`, `<ConsentBanner>` and `<ConsentPreferences>`. */
|
|
66
|
+
consent: RequestConsent | undefined
|
|
65
67
|
/** Reports an error a `<Catch>` rendered its fallback for, to the app's error hook. */
|
|
66
68
|
onCaught: ((error: unknown) => void) | undefined
|
|
67
69
|
/** Actions referenced via `useAction(fn).action` during this request, keyed by action URL. */
|
|
@@ -180,6 +182,12 @@ registerServerContextBridge({
|
|
|
180
182
|
consentGate(props) {
|
|
181
183
|
return ConsentGate(props)
|
|
182
184
|
},
|
|
185
|
+
consentBanner(props) {
|
|
186
|
+
return ConsentBanner(props)
|
|
187
|
+
},
|
|
188
|
+
consentPreferences(props) {
|
|
189
|
+
return ConsentPreferences(props)
|
|
190
|
+
},
|
|
183
191
|
head(props) {
|
|
184
192
|
return Head(props)
|
|
185
193
|
},
|
|
@@ -194,7 +202,7 @@ registerServerContextBridge({
|
|
|
194
202
|
export function createRenderContext(
|
|
195
203
|
region?: string,
|
|
196
204
|
ancestors: ReadonlySet<string> = new Set(),
|
|
197
|
-
consent?:
|
|
205
|
+
consent?: RequestConsent,
|
|
198
206
|
onCaught?: (error: unknown) => void,
|
|
199
207
|
): RenderContext {
|
|
200
208
|
return {
|
|
@@ -296,6 +304,14 @@ export function ConsentGate(props: ConsentGateProps): Html {
|
|
|
296
304
|
return raw(ssrChild(granted ? props.children : props.fallback))
|
|
297
305
|
}
|
|
298
306
|
|
|
307
|
+
export function ConsentBanner(props: ConsentBannerProps): Html {
|
|
308
|
+
return raw(renderConsentBanner(renderStorage.getStore()?.consent, props))
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
export function ConsentPreferences(props: ConsentTexts): Html {
|
|
312
|
+
return raw(renderConsentPreferences(renderStorage.getStore()?.consent, props))
|
|
313
|
+
}
|
|
314
|
+
|
|
299
315
|
/** Collects document <head> tags during SSR; innermost <title> wins. */
|
|
300
316
|
export function Head(props: { children?: PrunelleChild }): Html {
|
|
301
317
|
const ctx = renderStorage.getStore()
|
package/src/ssr/index.ts
CHANGED
|
@@ -100,6 +100,16 @@ export function escAttr(value: unknown): string {
|
|
|
100
100
|
.replaceAll('>', '>')
|
|
101
101
|
}
|
|
102
102
|
|
|
103
|
+
// What XML 1.0 forbids in a document: C0 controls but tab, LF and CR, U+FFFE, U+FFFF and lone surrogates.
|
|
104
|
+
const XML_FORBIDDEN_RE = /[\0-\x08\v\f\x0E-\x1F\uFFFE\uFFFF\uD800-\uDFFF]/gu
|
|
105
|
+
const XML_ESCAPES: Record<string, string> = { '&': '&', '<': '<', '>': '>', '"': '"', '\'': ''' }
|
|
106
|
+
const XML_SPECIAL_RE = /[&<>"']/g
|
|
107
|
+
|
|
108
|
+
/** Text for an XML document (a feed, a sitemap): escaped, and stripped of the characters that would make the whole file invalid. */
|
|
109
|
+
export function escXml(value: unknown): string {
|
|
110
|
+
return String(value).replace(XML_FORBIDDEN_RE, '').replace(XML_SPECIAL_RE, c => XML_ESCAPES[c]!)
|
|
111
|
+
}
|
|
112
|
+
|
|
103
113
|
// `<`, then the two line separators, built from their code points so the source shows no invisible character.
|
|
104
114
|
const SCRIPT_JSON_RE = new RegExp(`[<${String.fromCharCode(0x2028, 0x2029)}]`, 'g')
|
|
105
115
|
|
|
@@ -547,6 +557,11 @@ export interface DocumentOptions {
|
|
|
547
557
|
navigation?: string | undefined
|
|
548
558
|
}
|
|
549
559
|
|
|
560
|
+
// A tab icon link (`icon`, `shortcut icon`); an `apple-touch-icon` alone still leaves the browser asking for /favicon.ico.
|
|
561
|
+
const TAB_ICON_RE = /<link\b[^>]*\brel=["']?(?:[^"'>]*\s)?icon(?=[\s"'>])/i
|
|
562
|
+
// Named in a page with no icon of its own: the browser takes it as the icon and never requests /favicon.ico, which most apps answer with a 404.
|
|
563
|
+
const EMPTY_ICON = '<link rel="icon" href="data:,">'
|
|
564
|
+
|
|
550
565
|
export function renderDocument(options: DocumentOptions): string {
|
|
551
566
|
const { body, head = '', headTags, lang = 'en', htmlAttrs, runtimeScript, alwaysLoadRuntime, regionScript, preloadChunks, chunkImports, styles, hasIslands, hasSuspense, navigation } = options
|
|
552
567
|
let headExtra = head
|
|
@@ -556,6 +571,9 @@ export function renderDocument(options: DocumentOptions): string {
|
|
|
556
571
|
headExtra += tag
|
|
557
572
|
}
|
|
558
573
|
|
|
574
|
+
if (!TAB_ICON_RE.test(headExtra))
|
|
575
|
+
headExtra += EMPTY_ICON
|
|
576
|
+
|
|
559
577
|
if (styles !== undefined) {
|
|
560
578
|
for (const href of styles)
|
|
561
579
|
headExtra += `<link rel="stylesheet" href="${escAttr(href)}">`
|
package/src/ssr/navigation.ts
CHANGED
|
@@ -10,7 +10,7 @@ export type Eagerness = 'conservative' | 'moderate' | 'eager'
|
|
|
10
10
|
export interface NavigationConfig {
|
|
11
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. */
|
|
12
12
|
viewTransitions?: boolean | undefined
|
|
13
|
-
/** When the browser fetches the page a same-origin link points at, through Speculation Rules: `'
|
|
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
|
|
15
15
|
/** Renders the page a link points at before it is followed, which runs its `load()`: off by default, `true` is `'moderate'`. */
|
|
16
16
|
prerender?: Eagerness | boolean | undefined
|
|
@@ -64,7 +64,7 @@ export function createNavigation(config: NavigationConfig | undefined): Navigati
|
|
|
64
64
|
html += `<style>${VIEW_TRANSITION_STYLE}</style>`
|
|
65
65
|
styleHashes.push(cspSha256(VIEW_TRANSITION_STYLE))
|
|
66
66
|
}
|
|
67
|
-
const prefetch = config?.prefetch === undefined ? '
|
|
67
|
+
const prefetch = config?.prefetch === undefined ? 'conservative' : config.prefetch
|
|
68
68
|
const prerender = config?.prerender === true ? 'moderate' : config?.prerender || undefined
|
|
69
69
|
if (prefetch !== false || prerender !== undefined) {
|
|
70
70
|
const where = rulesWhere()
|
package/src/style.ts
CHANGED
|
@@ -20,10 +20,41 @@ function cssName(key: string): string {
|
|
|
20
20
|
return MS_PREFIX.test(key) ? `-${name}` : name
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
+
const PROPERTY_NAME = /^-{0,2}[a-z][\w-]*$/i
|
|
24
|
+
|
|
25
|
+
// Whether `value` stays one declaration value: a `;`, `{` or `}` outside quotes and parentheses, a comment, a raw newline, or a quote or parenthesis left open would end it and let the rest add declarations.
|
|
26
|
+
function isOneValue(value: string): boolean {
|
|
27
|
+
let depth = 0
|
|
28
|
+
let quote = ''
|
|
29
|
+
for (let i = 0; i < value.length; i++) {
|
|
30
|
+
const c = value[i]!
|
|
31
|
+
if (c === '\\')
|
|
32
|
+
i++
|
|
33
|
+
else if (c === '\n' || c === '\r' || c === '\f')
|
|
34
|
+
return false
|
|
35
|
+
else if (quote !== '')
|
|
36
|
+
quote = c === quote ? '' : quote
|
|
37
|
+
else if (c === '"' || c === '\'')
|
|
38
|
+
quote = c
|
|
39
|
+
else if (c === '/' && value[i + 1] === '*')
|
|
40
|
+
return false
|
|
41
|
+
else if (c === '(')
|
|
42
|
+
depth++
|
|
43
|
+
else if (c === ')' && --depth < 0)
|
|
44
|
+
return false
|
|
45
|
+
else if (depth === 0 && (c === ';' || c === '{' || c === '}'))
|
|
46
|
+
return false
|
|
47
|
+
}
|
|
48
|
+
return quote === '' && depth === 0
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** The CSS text of a style object. A declaration that could not stay one is dropped, as a browser drops an invalid one: a key that is not a property name, a non-finite number, a string value `isOneValue` refuses. */
|
|
23
52
|
export function styleText(style: Record<string, unknown>): string {
|
|
24
53
|
let out = ''
|
|
25
54
|
for (const [key, value] of Object.entries(style)) {
|
|
26
|
-
if (value == null || value === '' || value === false)
|
|
55
|
+
if (value == null || value === '' || value === false || !PROPERTY_NAME.test(key))
|
|
56
|
+
continue
|
|
57
|
+
if (typeof value === 'number' ? !Number.isFinite(value) : !isOneValue(String(value)))
|
|
27
58
|
continue
|
|
28
59
|
const name = cssName(key)
|
|
29
60
|
const px = typeof value === 'number' && !name.startsWith('--') && !UNITLESS.has(name)
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { Head } from 'prunelle'
|
|
2
2
|
|
|
3
|
+
// French law (LCEN art. 1-1) asks every site for these details; `prunelle check` warns until every placeholder below is replaced.
|
|
4
|
+
// A private individual publishing without a business may give only the host's details, the host keeping their identity.
|
|
3
5
|
export default function LegalNotice() {
|
|
4
6
|
return (
|
|
5
7
|
<section>
|
|
@@ -7,7 +9,14 @@ export default function LegalNotice() {
|
|
|
7
9
|
<title>Legal Notice</title>
|
|
8
10
|
</Head>
|
|
9
11
|
<h1>Legal Notice</h1>
|
|
10
|
-
<
|
|
12
|
+
<h2>Publisher</h2>
|
|
13
|
+
<p>__APP_NAME__ is published by [to fill in: your full name, or your company's name, legal form and share capital].</p>
|
|
14
|
+
<p>Address: [to fill in: your home address, or your company's registered office]</p>
|
|
15
|
+
<p>Phone: [to fill in] / Email: [to fill in]</p>
|
|
16
|
+
<p>Registration: [to fill in: RCS or RNE number and VAT number, or remove this line if you have none]</p>
|
|
17
|
+
<p>Publication director: [to fill in: full name]</p>
|
|
18
|
+
<h2>Host</h2>
|
|
19
|
+
<p>[to fill in: the host's name, address and phone number]</p>
|
|
11
20
|
</section>
|
|
12
21
|
)
|
|
13
22
|
}
|
|
@@ -12,10 +12,11 @@
|
|
|
12
12
|
"dependencies": {
|
|
13
13
|
"prunelle": "__PRUNELLE_VERSION__",
|
|
14
14
|
"@prunellejs/auth": "__PRUNELLE_VERSION__",
|
|
15
|
+
"@prunellejs/mail": "__PRUNELLE_VERSION__",
|
|
15
16
|
"@prunellejs/ui": "__PRUNELLE_VERSION__"
|
|
16
17
|
},
|
|
17
18
|
"devDependencies": {
|
|
18
|
-
"@types/bun": "
|
|
19
|
+
"@types/bun": "^1.4.4",
|
|
19
20
|
"typescript": "^6.0.3"
|
|
20
21
|
}
|
|
21
22
|
}
|
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import { ui } from '@prunellejs/ui/plugin'
|
|
2
2
|
import { defineConfig } from 'prunelle/config'
|
|
3
3
|
import { auth } from './src/auth'
|
|
4
|
+
import { mail } from './src/mail'
|
|
4
5
|
|
|
5
6
|
export default defineConfig({
|
|
6
7
|
lang: 'en',
|
|
7
8
|
plugins: [
|
|
8
9
|
ui({ grammar: 'editorial' }),
|
|
10
|
+
mail,
|
|
9
11
|
auth,
|
|
10
12
|
],
|
|
11
13
|
})
|
|
@@ -1,14 +1,12 @@
|
|
|
1
|
-
import { join } from 'node:path'
|
|
2
1
|
import { createAuth } from '@prunellejs/auth'
|
|
3
2
|
import { sqliteAuthStore } from '@prunellejs/auth/sqlite'
|
|
3
|
+
import { mail } from './mail'
|
|
4
4
|
|
|
5
5
|
// Set AUTH_SECRET (at least 32 bytes) in production — see .env.example.
|
|
6
|
-
const defaultDb = import.meta.dir.startsWith('/$bunfs')
|
|
7
|
-
? './data/auth.sqlite'
|
|
8
|
-
: join(import.meta.dir, '../data/auth.sqlite')
|
|
9
|
-
|
|
10
6
|
export const auth = createAuth({
|
|
11
|
-
|
|
7
|
+
// A relative path lands under the app's folder, out of dist/.
|
|
8
|
+
store: sqliteAuthStore(process.env.AUTH_DB || 'data/auth.sqlite'),
|
|
12
9
|
secret: process.env.AUTH_SECRET || undefined,
|
|
13
10
|
defaultRedirect: '/dashboard',
|
|
11
|
+
mailer: mail,
|
|
14
12
|
})
|
|
@@ -10,6 +10,7 @@ export default function Layout({ children, locals }: LayoutProps) {
|
|
|
10
10
|
<Head>
|
|
11
11
|
<title>__APP_NAME__</title>
|
|
12
12
|
</Head>
|
|
13
|
+
<a href="#main" class="skip-link">Skip to content</a>
|
|
13
14
|
<div class="wrap">
|
|
14
15
|
<header data-layout="split" class="site-header">
|
|
15
16
|
<a href="/" class="site-brand">__APP_NAME__</a>
|
|
@@ -24,7 +25,14 @@ export default function Layout({ children, locals }: LayoutProps) {
|
|
|
24
25
|
)
|
|
25
26
|
: <Button href="/login" variant="outline" size="sm">Sign in</Button>}
|
|
26
27
|
</header>
|
|
27
|
-
<main>{children}</main>
|
|
28
|
+
<main id="main">{children}</main>
|
|
29
|
+
<footer class="site-footer">
|
|
30
|
+
<Cluster>
|
|
31
|
+
<a href="/legal">Legal notice</a>
|
|
32
|
+
<a href="/privacy">Privacy</a>
|
|
33
|
+
<a href="/accessibilite" lang="fr">Accessibilité : non conforme</a>
|
|
34
|
+
</Cluster>
|
|
35
|
+
</footer>
|
|
28
36
|
</div>
|
|
29
37
|
<Toaster />
|
|
30
38
|
</>
|
|
@@ -1,13 +1,47 @@
|
|
|
1
1
|
import type { PageProps } from 'prunelle'
|
|
2
|
-
import { Card, CardBody, CardHeader } from '@prunellejs/ui'
|
|
2
|
+
import { Button, Card, CardBody, CardHeader, Checkbox, Cluster, Notice, Stack } from '@prunellejs/ui'
|
|
3
|
+
import { Head } from 'prunelle'
|
|
4
|
+
import { auth } from '../../auth'
|
|
5
|
+
|
|
6
|
+
export default async function DashboardPage({ locals, url }: PageProps) {
|
|
7
|
+
// A refused export or deletion comes back here with a code naming why.
|
|
8
|
+
const message = auth.formMessage(url)
|
|
9
|
+
const refusal = message?.form === 'account' ? message : undefined
|
|
3
10
|
|
|
4
|
-
export default async function DashboardPage({ locals }: PageProps) {
|
|
5
11
|
return (
|
|
6
|
-
<
|
|
7
|
-
<
|
|
8
|
-
<
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
+
<Stack gap="lg">
|
|
13
|
+
<Head><title>Dashboard - __APP_NAME__</title></Head>
|
|
14
|
+
<h1 class="page-title">Dashboard</h1>
|
|
15
|
+
<Card>
|
|
16
|
+
<CardBody>
|
|
17
|
+
<p class="m-0">Signed in as <strong>{locals.user?.email}</strong>.</p>
|
|
18
|
+
</CardBody>
|
|
19
|
+
</Card>
|
|
20
|
+
<Card>
|
|
21
|
+
<CardHeader><h2 class="card-title">Your data</h2></CardHeader>
|
|
22
|
+
<CardBody>
|
|
23
|
+
<Stack gap="md">
|
|
24
|
+
{refusal && (
|
|
25
|
+
<Notice tone="danger" role="alert">
|
|
26
|
+
{refusal.text}
|
|
27
|
+
{refusal.code === 'account.reauth-required' && <>. <a href="/login">Sign in</a></>}
|
|
28
|
+
</Notice>
|
|
29
|
+
)}
|
|
30
|
+
<Cluster>
|
|
31
|
+
<Button href="/auth/me/export" variant="outline">Download my data</Button>
|
|
32
|
+
</Cluster>
|
|
33
|
+
<form action="/auth/me/delete" method="post">
|
|
34
|
+
<Stack gap="sm">
|
|
35
|
+
<input type="hidden" name="redirect" value="/" />
|
|
36
|
+
<Checkbox name="confirm" label="Delete my account and everything kept with it, for good" required />
|
|
37
|
+
<Cluster>
|
|
38
|
+
<Button type="submit" tone="danger" variant="outline">Delete my account</Button>
|
|
39
|
+
</Cluster>
|
|
40
|
+
</Stack>
|
|
41
|
+
</form>
|
|
42
|
+
</Stack>
|
|
43
|
+
</CardBody>
|
|
44
|
+
</Card>
|
|
45
|
+
</Stack>
|
|
12
46
|
)
|
|
13
47
|
}
|
|
@@ -1,34 +1,53 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { PageProps } from 'prunelle'
|
|
2
|
+
import { Button, Card, CardBody, CardHeader, Field, Grid, Notice, Stack } from '@prunellejs/ui'
|
|
3
|
+
import { Head } from 'prunelle'
|
|
4
|
+
import { auth } from '../auth'
|
|
5
|
+
|
|
6
|
+
export default function LoginPage({ url }: PageProps) {
|
|
7
|
+
// A failed sign-in or sign-up comes back here with a code naming its form and, when it is about one, its field.
|
|
8
|
+
const message = auth.formMessage(url)
|
|
9
|
+
const fieldError = (form: 'signin' | 'signup', field: 'email' | 'password') =>
|
|
10
|
+
message?.form === form && message.field === field ? message.text : undefined
|
|
11
|
+
const formNotice = (form: 'signin' | 'signup') =>
|
|
12
|
+
message?.form === form && message.field === undefined
|
|
13
|
+
? <Notice tone={message.tone} role={message.tone === 'danger' ? 'alert' : 'status'}>{message.text}</Notice>
|
|
14
|
+
: undefined
|
|
2
15
|
|
|
3
|
-
export default function LoginPage() {
|
|
4
16
|
return (
|
|
5
|
-
<
|
|
6
|
-
<
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
<
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
<
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
17
|
+
<Stack gap="lg">
|
|
18
|
+
<Head><title>Sign in - __APP_NAME__</title></Head>
|
|
19
|
+
<h1 class="page-title">Sign in or create an account</h1>
|
|
20
|
+
<Grid min="md" gap="lg">
|
|
21
|
+
<Card>
|
|
22
|
+
<CardHeader><h2 class="card-title">Sign in</h2></CardHeader>
|
|
23
|
+
<CardBody>
|
|
24
|
+
<form action="/auth/signin" method="post">
|
|
25
|
+
<Stack gap="sm">
|
|
26
|
+
{formNotice('signin')}
|
|
27
|
+
<input type="hidden" name="redirect" value="/dashboard" />
|
|
28
|
+
<Field id="in-email" label="Email" required><input id="in-email" name="email" type="email" autocomplete="email" required /></Field>
|
|
29
|
+
<Field id="in-pass" label="Password" error={fieldError('signin', 'password')} required><input id="in-pass" name="password" type="password" autocomplete="current-password" required /></Field>
|
|
30
|
+
<Button type="submit" tone="accent">Sign in</Button>
|
|
31
|
+
</Stack>
|
|
32
|
+
</form>
|
|
33
|
+
</CardBody>
|
|
34
|
+
</Card>
|
|
35
|
+
<Card>
|
|
36
|
+
<CardHeader><h2 class="card-title">Create account</h2></CardHeader>
|
|
37
|
+
<CardBody>
|
|
38
|
+
<form action="/auth/signup" method="post">
|
|
39
|
+
<Stack gap="sm">
|
|
40
|
+
{formNotice('signup')}
|
|
41
|
+
<input type="hidden" name="redirect" value="/dashboard" />
|
|
42
|
+
<Field id="up-email" label="Email" error={fieldError('signup', 'email')} required><input id="up-email" name="email" type="email" autocomplete="email" required /></Field>
|
|
43
|
+
<Field id="up-pass" label="Password" hint="Min 12 characters" error={fieldError('signup', 'password')} required><input id="up-pass" name="password" type="password" autocomplete="new-password" minlength="12" required /></Field>
|
|
44
|
+
<p class="form-note">Your email is used only to run your account. <a href="/privacy">How your data is handled</a></p>
|
|
45
|
+
<Button type="submit" tone="accent">Create account</Button>
|
|
46
|
+
</Stack>
|
|
47
|
+
</form>
|
|
48
|
+
</CardBody>
|
|
49
|
+
</Card>
|
|
50
|
+
</Grid>
|
|
51
|
+
</Stack>
|
|
33
52
|
)
|
|
34
53
|
}
|