prunelle 0.8.0 → 0.9.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 (184) hide show
  1. package/llms.txt +61 -44
  2. package/package.json +7 -2
  3. package/src/a11y/declaration.ts +6 -10
  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 +88 -134
  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 +20 -10
  15. package/src/build/islands.ts +4 -1
  16. package/src/build/key-types.ts +31 -0
  17. package/src/build/manifest.ts +19 -1
  18. package/src/build/plugin-host.ts +2 -0
  19. package/src/build/prerender.ts +2 -2
  20. package/src/build/reach.ts +14 -1
  21. package/src/build/report.ts +1 -1
  22. package/src/build/route-types.ts +16 -0
  23. package/src/build/routes.ts +24 -15
  24. package/src/build/seed-ids.ts +65 -0
  25. package/src/build/seed-writes.ts +111 -0
  26. package/src/build/static-headers.ts +2 -2
  27. package/src/build/static.ts +1 -1
  28. package/src/cli/check-cache.ts +51 -0
  29. package/src/cli/check-consent.ts +95 -0
  30. package/src/cli/check-forms.ts +2 -2
  31. package/src/cli/check-links-patterns.ts +101 -0
  32. package/src/cli/check-links.ts +16 -100
  33. package/src/cli/check-regions.ts +185 -0
  34. package/src/cli/check-security.ts +50 -6
  35. package/src/cli/check.ts +40 -18
  36. package/src/cli/commands.ts +134 -14
  37. package/src/cli/docs.ts +21 -2
  38. package/src/cli/index.ts +1 -1
  39. package/src/cli/inspect.ts +2 -0
  40. package/src/cli/mcp-tools.ts +2 -2
  41. package/src/cli/new.ts +9 -5
  42. package/src/cli/upgrade-names.ts +14 -2
  43. package/src/cli/upgrade.ts +3 -0
  44. package/src/compiler/analyze.ts +2 -2
  45. package/src/compiler/cacheability.ts +24 -2
  46. package/src/compiler/compliance-contrast.ts +284 -0
  47. package/src/compiler/compliance-empty.ts +24 -0
  48. package/src/compiler/compliance-rgaa-content.ts +95 -0
  49. package/src/compiler/compliance-rgaa-criteria.ts +45 -0
  50. package/src/compiler/compliance-rgaa-pages.ts +140 -0
  51. package/src/compiler/compliance-rgaa.ts +19 -0
  52. package/src/compiler/compliance-third-party.ts +59 -0
  53. package/src/compiler/compliance-urls.ts +52 -0
  54. package/src/compiler/compliance.ts +70 -42
  55. package/src/compiler/cookie-inventory.ts +119 -0
  56. package/src/compiler/foreign-imports.ts +5 -3
  57. package/src/compiler/mistakes.ts +18 -9
  58. package/src/compiler/props-pattern.ts +211 -0
  59. package/src/compiler/props.ts +177 -346
  60. package/src/compiler/ssr-emit.ts +510 -0
  61. package/src/compiler/ssr-helpers.ts +170 -121
  62. package/src/compiler/ssr.ts +25 -440
  63. package/src/context.ts +14 -1
  64. package/src/dev/child.ts +1 -1
  65. package/src/dev/hot-islands.ts +71 -0
  66. package/src/dev/index.ts +31 -18
  67. package/src/dev/protocol.ts +1 -1
  68. package/src/dev/queue.ts +3 -2
  69. package/src/dev/reload.ts +1 -1
  70. package/src/dev/supervisor.ts +3 -1
  71. package/src/dev/watchers.ts +6 -2
  72. package/src/dom/action.ts +97 -14
  73. package/src/dom/extras.ts +2 -1
  74. package/src/dom/field-marks.ts +24 -0
  75. package/src/dom/hot.ts +9 -0
  76. package/src/dom/index.ts +25 -5
  77. package/src/dom/island-hot.ts +29 -0
  78. package/src/dom/island.ts +13 -2
  79. package/src/dom/prefetch.ts +132 -0
  80. package/src/dom/regions.ts +498 -0
  81. package/src/dom/swap.ts +245 -0
  82. package/src/dom/trusted.ts +22 -0
  83. package/src/dom/upload.ts +44 -0
  84. package/src/eco/declaration.ts +39 -40
  85. package/src/eco/index.ts +5 -5
  86. package/src/href.ts +31 -0
  87. package/src/index.ts +9 -3
  88. package/src/plugins/config.ts +1 -1
  89. package/src/plugins/context.ts +7 -1
  90. package/src/plugins/daily.ts +27 -0
  91. package/src/plugins/index.ts +3 -3
  92. package/src/plugins/order.ts +1 -1
  93. package/src/plugins/public.ts +8 -5
  94. package/src/plugins/services.ts +88 -14
  95. package/src/plugins/types.ts +120 -9
  96. package/src/reactivity/hmr.ts +90 -0
  97. package/src/reactivity/index.ts +38 -18
  98. package/src/reactivity/resource.ts +115 -0
  99. package/src/reactivity/seed.ts +39 -0
  100. package/src/reactivity/server-seed.ts +194 -0
  101. package/src/reactivity/store.ts +55 -1
  102. package/src/rgpd/draft.ts +171 -0
  103. package/src/rgpd/forms.ts +85 -0
  104. package/src/rgpd/index.ts +57 -0
  105. package/src/server/action-flash.ts +1 -0
  106. package/src/server/action-registry.ts +6 -8
  107. package/src/server/action.ts +51 -19
  108. package/src/server/app-secret.ts +14 -8
  109. package/src/server/cache-invalidation.ts +96 -0
  110. package/src/server/compress.ts +3 -1
  111. package/src/server/consent.ts +16 -2
  112. package/src/server/cookie-inventory.ts +78 -0
  113. package/src/server/cookies.ts +50 -0
  114. package/src/server/csp-report.ts +86 -0
  115. package/src/server/discard.ts +13 -0
  116. package/src/server/document.ts +2 -2
  117. package/src/server/env.ts +1 -1
  118. package/src/server/errors.ts +9 -2
  119. package/src/server/field-errors.ts +91 -0
  120. package/src/server/form-schema.ts +16 -11
  121. package/src/server/health.ts +33 -12
  122. package/src/server/http.ts +25 -10
  123. package/src/server/index.ts +101 -298
  124. package/src/server/loader.ts +1 -0
  125. package/src/server/page.ts +3 -9
  126. package/src/server/plugin-host.ts +26 -8
  127. package/src/server/public.ts +14 -3
  128. package/src/server/rate-limit.ts +39 -10
  129. package/src/server/region.ts +3 -0
  130. package/src/server/request-handler.ts +370 -0
  131. package/src/server/request-log.ts +94 -0
  132. package/src/server/route-cache.ts +41 -6
  133. package/src/server/route-input.ts +54 -0
  134. package/src/server/route-table.ts +35 -19
  135. package/src/server/routing.ts +12 -11
  136. package/src/server/sanitize-html.ts +1 -1
  137. package/src/server/security.ts +38 -8
  138. package/src/server/serve-handlers.ts +9 -5
  139. package/src/server/shared-store.ts +314 -0
  140. package/src/server/start.ts +12 -5
  141. package/src/server/static-dir.ts +14 -10
  142. package/src/server/stream.ts +2 -1
  143. package/src/server/surface.ts +39 -0
  144. package/src/server/webhook.ts +48 -0
  145. package/src/server/workers.ts +168 -0
  146. package/src/ssr/consent-banner.ts +20 -16
  147. package/src/ssr/consent-texts.ts +40 -0
  148. package/src/ssr/context.ts +26 -3
  149. package/src/ssr/cookie-table.ts +75 -0
  150. package/src/ssr/index.ts +23 -13
  151. package/src/ssr/navigation.ts +161 -14
  152. package/src/test/browser.ts +50 -22
  153. package/src/test/build-reuse.ts +70 -0
  154. package/src/test/index.ts +45 -6
  155. package/src/test/mount.ts +53 -0
  156. package/src/test/outside.ts +94 -0
  157. package/templates/_shared/AGENTS.md +1 -1
  158. package/templates/_shared/Dockerfile +22 -0
  159. package/templates/_shared/dockerignore +9 -0
  160. package/templates/_shared/gitignore +2 -0
  161. package/templates/app/.env.example +3 -1
  162. package/templates/app/compose.yaml +32 -0
  163. package/templates/app/db/migrations/001_notes.sql +7 -0
  164. package/templates/app/db/schema.ts +11 -0
  165. package/templates/app/package.json +1 -0
  166. package/templates/app/prunelle.config.ts +6 -0
  167. package/templates/app/src/actions/notes.ts +26 -0
  168. package/templates/app/src/auth.ts +4 -2
  169. package/templates/app/src/db.ts +5 -0
  170. package/templates/app/src/routes/dashboard/index.tsx +26 -1
  171. package/templates/app/src/routes/privacy.tsx +6 -2
  172. package/templates/app/tests/app.test.ts +14 -3
  173. package/templates/plugin/AGENTS.md +11 -0
  174. package/templates/plugin/CLAUDE.md +1 -0
  175. package/templates/plugin/README.md +27 -0
  176. package/templates/plugin/gitignore +2 -0
  177. package/templates/plugin/llms.txt +19 -0
  178. package/templates/plugin/package.json +33 -0
  179. package/templates/plugin/src/index.ts +40 -0
  180. package/templates/plugin/tests/fixture/prunelle.config.ts +4 -0
  181. package/templates/plugin/tests/fixture/src/routes/index.tsx +11 -0
  182. package/templates/plugin/tests/plugin.test.ts +42 -0
  183. package/templates/plugin/tsconfig.json +19 -0
  184. package/ts-plugin.cjs +3674 -2196
@@ -0,0 +1,22 @@
1
+ # The app as one binary on an image holding only glibc and certificates, run by a user without root.
2
+ # docker build -t __APP_NAME__ . && docker run -p 127.0.0.1:3000:3000 __APP_NAME__
3
+
4
+ FROM oven/bun:__BUN_VERSION__ AS build
5
+ WORKDIR /app
6
+ COPY package.json bun.lock ./
7
+ RUN bun install --frozen-lockfile
8
+ COPY . .
9
+ # The image ships the data folder empty, whatever the build opened in it.
10
+ RUN bunx prunelle build --compile server && rm -rf data && mkdir data
11
+
12
+ FROM gcr.io/distroless/base-debian12
13
+ WORKDIR /app
14
+ COPY --from=build /app/server ./server
15
+ # SQLite files go here; a mounted folder must be writable by uid 1000.
16
+ COPY --from=build --chown=1000:1000 /app/data ./data
17
+ USER 1000:1000
18
+ # Inside the container the server listens on every interface; publish its port on 127.0.0.1 and put a proxy in front.
19
+ ENV HOST=0.0.0.0 PORT=3000
20
+ EXPOSE 3000
21
+ HEALTHCHECK --interval=30s --timeout=3s CMD ["./server", "--health"]
22
+ CMD ["./server"]
@@ -0,0 +1,9 @@
1
+ .git
2
+ node_modules
3
+ dist
4
+ .prunelle-cache
5
+ .prunelle-dev
6
+ .env
7
+ .env.*
8
+ data
9
+ data-prod
@@ -6,3 +6,5 @@ dist/
6
6
  .env.*
7
7
  !.env.example
8
8
  data/
9
+ data-prod/
10
+ src/prunelle-*.d.ts
@@ -1,7 +1,9 @@
1
1
  # Generate a 32+ byte secret for production, e.g.:
2
2
  # openssl rand -base64 32
3
3
  AUTH_SECRET=
4
- AUTH_DB=./data/auth.sqlite
4
+ DATABASE=./data/app.db
5
+ # The address the app is served at, e.g. https://example.com
6
+ SITE=
5
7
 
6
8
  # The SMTP server and the sender of sign-up confirmations and sign-in links, e.g. smtp://user:pass@smtp.example.com:587 and hello@example.com
7
9
  MAIL_URL=
@@ -0,0 +1,32 @@
1
+ # docker compose up -d --build, after `mkdir data-prod && sudo chown 1000:1000 data-prod` and the four settings in .env.
2
+ # Expected server layout:
3
+ # compose.yaml
4
+ # .env AUTH_SECRET, MAIL_URL, MAIL_FROM, SITE
5
+ # data-prod/ the SQLite database, owned by uid 1000
6
+
7
+ services:
8
+ app:
9
+ build: .
10
+ image: __APP_NAME__
11
+ restart: unless-stopped
12
+ environment:
13
+ AUTH_SECRET: ${AUTH_SECRET:?set AUTH_SECRET in .env (openssl rand -base64 32)}
14
+ MAIL_URL: ${MAIL_URL:?set MAIL_URL in .env}
15
+ MAIL_FROM: ${MAIL_FROM:?set MAIL_FROM in .env}
16
+ SITE: ${SITE:?set SITE in .env to the address the app is served at, e.g. https://example.com}
17
+ DATABASE: data/app.db
18
+ ports:
19
+ # Loopback only: the proxy in front ends TLS and is the one thing on a public interface.
20
+ - '127.0.0.1:${PORT:-3000}:3000'
21
+ volumes:
22
+ - type: bind
23
+ source: ./data-prod
24
+ target: /app/data
25
+ bind: { create_host_path: false }
26
+ read_only: true
27
+ tmpfs: [/tmp]
28
+ cap_drop: [ALL]
29
+ security_opt: [no-new-privileges:true]
30
+ logging:
31
+ driver: json-file
32
+ options: { max-size: 10m, max-file: '3' }
@@ -0,0 +1,7 @@
1
+ create table notes (
2
+ id integer primary key,
3
+ user_id text not null,
4
+ body text not null,
5
+ created_at bigint not null
6
+ );
7
+ create index notes_user on notes (user_id, created_at);
@@ -0,0 +1,11 @@
1
+ import { defineSchema } from '@prunellejs/db'
2
+
3
+ // What SQL cannot say: which columns are personal and why, and whose rows they are. Auth exports and erases a user's notes with their account, and `prunelle rgpd --draft` lists them.
4
+ export default defineSchema({
5
+ notes: {
6
+ owner: 'user_id',
7
+ personal: { body: 'le texte des notes' },
8
+ purpose: 'Garder les notes que vous écrivez.',
9
+ legalBasis: 'Exécution du contrat (RGPD art. 6.1.b).',
10
+ },
11
+ })
@@ -12,6 +12,7 @@
12
12
  "dependencies": {
13
13
  "prunelle": "__PRUNELLE_VERSION__",
14
14
  "@prunellejs/auth": "__PRUNELLE_VERSION__",
15
+ "@prunellejs/db": "__PRUNELLE_VERSION__",
15
16
  "@prunellejs/mail": "__PRUNELLE_VERSION__",
16
17
  "@prunellejs/ui": "__PRUNELLE_VERSION__"
17
18
  },
@@ -1,13 +1,19 @@
1
1
  import { ui } from '@prunellejs/ui/plugin'
2
2
  import { defineConfig } from 'prunelle/config'
3
3
  import { auth } from './src/auth'
4
+ import { db } from './src/db'
4
5
  import { mail } from './src/mail'
5
6
 
6
7
  export default defineConfig({
7
8
  lang: 'en',
9
+ // The address the app is served at, for the links of mails sent outside a request, such as the warning before an inactive account is deleted.
10
+ site: process.env.SITE || undefined,
11
+ // The database of every process: accounts, notes and rate limits. A relative path lands under the app's folder, out of dist/.
12
+ store: process.env.DATABASE || 'data/app.db',
8
13
  plugins: [
9
14
  ui({ grammar: 'editorial' }),
10
15
  mail,
11
16
  auth,
17
+ db,
12
18
  ],
13
19
  })
@@ -0,0 +1,26 @@
1
+ import type { StandardSchemaV1 } from 'prunelle/server'
2
+ import { action } from 'prunelle/server'
3
+ import { db } from '../db'
4
+
5
+ const noteSchema: StandardSchemaV1<{ body: string }> = {
6
+ '~standard': {
7
+ version: 1,
8
+ vendor: 'prunelle',
9
+ // Read by prunelle check, and by the page, which gets `required` and `maxlength` on the field from it.
10
+ jsonSchema: { input: () => ({ type: 'object', properties: { body: { type: 'string', minLength: 1, maxLength: 2000 } }, required: ['body'] }) },
11
+ validate(raw: unknown) {
12
+ const body = String((raw as { body?: unknown })?.body ?? '').trim()
13
+ if (body === '' || body.length > 2000)
14
+ return { issues: [{ message: 'Write a note of 1 to 2000 characters', path: ['body'] }] }
15
+ return { value: { body } }
16
+ },
17
+ },
18
+ }
19
+
20
+ export const addNote = action(noteSchema, async ({ body }, ctx) => {
21
+ const user = ctx.locals.user
22
+ if (!user)
23
+ return ctx.deny(401, 'Sign in to write a note')
24
+ await db.sql`insert into notes (user_id, body, created_at) values (${user.id}, ${body}, ${Date.now()})`
25
+ return ctx.redirect('/dashboard')
26
+ })
@@ -4,9 +4,11 @@ import { mail } from './mail'
4
4
 
5
5
  // Set AUTH_SECRET (at least 32 bytes) in production — see .env.example.
6
6
  export const auth = createAuth({
7
- // A relative path lands under the app's folder, out of dist/.
8
- store: sqliteAuthStore(process.env.AUTH_DB || 'data/auth.sqlite'),
7
+ // The `store` of prunelle.config.ts, opened here too so a test or a script reaches the accounts.
8
+ store: sqliteAuthStore(process.env.DATABASE || 'data/app.db'),
9
9
  secret: process.env.AUTH_SECRET || undefined,
10
10
  defaultRedirect: '/dashboard',
11
11
  mailer: mail,
12
+ // An account nobody signs in to for 2 years is deleted, its owner mailed 30 days before.
13
+ inactivity: true,
12
14
  })
@@ -0,0 +1,5 @@
1
+ import { createDb } from '@prunellejs/db'
2
+ import schema from '../db/schema'
3
+
4
+ // Queries the `store` of prunelle.config.ts; the files of db/migrations apply at start, or with `prunelle db migrate`.
5
+ export const db = createDb({ schema })
@@ -1,12 +1,19 @@
1
1
  import type { PageProps } from 'prunelle'
2
- import { Button, Card, CardBody, CardHeader, Checkbox, Cluster, Notice, Stack } from '@prunellejs/ui'
2
+ import { Button, Card, CardBody, CardHeader, Checkbox, Cluster, Field, Notice, Stack } from '@prunellejs/ui'
3
3
  import { Head } from 'prunelle'
4
+ import { getActionError } from 'prunelle/server'
5
+ import { addNote } from '../../actions/notes'
4
6
  import { auth } from '../../auth'
7
+ import { db } from '../../db'
8
+
9
+ interface Note { id: number, body: string }
5
10
 
6
11
  export default async function DashboardPage({ locals, url }: PageProps) {
7
12
  // A refused export or deletion comes back here with a code naming why.
8
13
  const message = auth.formMessage(url)
9
14
  const refusal = message?.form === 'account' ? message : undefined
15
+ const notes: Note[] = await db.sql`select id, body from notes where user_id = ${locals.user!.id} order by created_at desc, id desc`
16
+ const noteError = getActionError(locals)?.fieldErrors?.body
10
17
 
11
18
  return (
12
19
  <Stack gap="lg">
@@ -17,6 +24,24 @@ export default async function DashboardPage({ locals, url }: PageProps) {
17
24
  <p class="m-0">Signed in as <strong>{locals.user?.email}</strong>.</p>
18
25
  </CardBody>
19
26
  </Card>
27
+ <Card>
28
+ <CardHeader><h2 class="card-title">Your notes</h2></CardHeader>
29
+ <CardBody>
30
+ <Stack gap="md">
31
+ <form action={addNote} method="post">
32
+ <Stack gap="sm">
33
+ <Field id="note-body" label="New note" error={noteError} required><textarea id="note-body" name="body" rows="3" /></Field>
34
+ <Cluster>
35
+ <Button type="submit">Add the note</Button>
36
+ </Cluster>
37
+ </Stack>
38
+ </form>
39
+ {notes.length === 0
40
+ ? <p class="m-0">No notes yet.</p>
41
+ : <ul>{notes.map(note => <li>{note.body}</li>)}</ul>}
42
+ </Stack>
43
+ </CardBody>
44
+ </Card>
20
45
  <Card>
21
46
  <CardHeader><h2 class="card-title">Your data</h2></CardHeader>
22
47
  <CardBody>
@@ -1,4 +1,4 @@
1
- import { Head } from 'prunelle'
1
+ import { CookieTable, Head } from 'prunelle'
2
2
 
3
3
  // What the RGPD (art. 13) asks to tell people when their data is collected, written for what @prunellejs/auth stores by default; `prunelle check` warns until every placeholder below is replaced.
4
4
  // Change it when the app collects more, or when you change the auth plugin's storeIp, storeUserAgent or sessionTtlSeconds.
@@ -13,9 +13,13 @@ export default function Privacy() {
13
13
  <p>__APP_NAME__ is run by [to fill in: your name or your company's, as on the legal notice]. For any question about your data, write to [to fill in: a contact email].</p>
14
14
  <h2>What we collect and why</h2>
15
15
  <p>To create your account and sign you in: your email address and a hash of your password, never the password itself. This is needed to provide the service you sign up for (RGPD art. 6.1.b).</p>
16
+ <p>To keep the notes you write: their text, for the same reason.</p>
16
17
  <p>To keep you signed in and protect your account: a session cookie, which the site cannot work without and which therefore needs no consent, and for each session the name of your browser and a shortened IP address with its last part removed.</p>
17
18
  <h2>How long we keep it</h2>
18
- <p>A session ends 30 days after your last visit, or when you sign out, and is then deleted. An account whose email address is not confirmed within 7 days is deleted. Your account is kept until you delete it from your dashboard.</p>
19
+ <p>A session ends 30 days after your last visit, or when you sign out, and is then deleted. An account whose email address is not confirmed within 7 days is deleted. Your account and your notes are kept until you delete the account from your dashboard.</p>
20
+ <h2>Cookies</h2>
21
+ <p>Every cookie this site sets is essential to it, so none waits for your consent:</p>
22
+ <CookieTable />
19
23
  <h2>Who receives it</h2>
20
24
  <p>Nobody else: the data stays with our host, [to fill in: the host's name], and is not shared or sold.</p>
21
25
  <h2>Your rights</h2>
@@ -1,5 +1,6 @@
1
1
  import type { TestApp } from 'prunelle/test'
2
2
  import { beforeAll, describe, expect, it } from 'bun:test'
3
+ import { Database } from 'bun:sqlite'
3
4
  import { mkdtempSync } from 'node:fs'
4
5
  import { tmpdir } from 'node:os'
5
6
  import { join, resolve } from 'node:path'
@@ -7,7 +8,7 @@ import { testApp } from 'prunelle/test'
7
8
 
8
9
  const root = resolve(import.meta.dir, '..')
9
10
  // A file the app and this test both open, so a user the test creates can sign in to the app.
10
- process.env.AUTH_DB ??= join(mkdtempSync(join(tmpdir(), '__APP_NAME__-')), 'auth.sqlite')
11
+ process.env.DATABASE ??= join(mkdtempSync(join(tmpdir(), '__APP_NAME__-')), 'app.db')
11
12
  const { auth } = await import('../src/auth')
12
13
 
13
14
  describe('__APP_NAME__', () => {
@@ -30,6 +31,7 @@ describe('__APP_NAME__', () => {
30
31
 
31
32
  const privacy = await app.get('/privacy')
32
33
  expect(privacy.status).toBe(200)
34
+ expect(await privacy.text()).toContain('<td><code>pru_session</code></td><td>Keeps you signed in.</td><td>Essential</td><td>30 days</td>')
33
35
  expect(app.errors).toEqual([])
34
36
  })
35
37
 
@@ -41,7 +43,7 @@ describe('__APP_NAME__', () => {
41
43
  expect(page).toContain('Invalid email or password')
42
44
  })
43
45
 
44
- it('lets a signed-in user download their data and delete their account from the dashboard', async () => {
46
+ it('lets a signed-in user write notes, download them with their data and delete them with their account', async () => {
45
47
  const credentials = { email: 'ada@example.com', password: 'correct-horse-battery' }
46
48
  await auth.createUser({ ...credentials, emailVerified: true })
47
49
  const fromLogin = { referer: 'http://localhost/login' }
@@ -55,12 +57,21 @@ describe('__APP_NAME__', () => {
55
57
  expect(refused).toContain('Sign in again to confirm it is you')
56
58
  expect(refused).toContain('<a href="/login">Sign in</a>')
57
59
 
60
+ const { addNote } = await app.import('src/actions/notes.ts') as typeof import('../src/actions/notes')
61
+ expect((await app.submit(addNote, { body: 'Buy milk' }, fromDashboard)).headers.get('location')).toBe('/dashboard')
62
+ expect(await (await app.get('/dashboard')).text()).toContain('<li>Buy milk</li>')
63
+
58
64
  const dump = await app.get('/auth/me/export', fromDashboard)
59
65
  expect(dump.status).toBe(200)
60
- expect((await dump.json() as { user: { email: string } }).user.email).toBe('ada@example.com')
66
+ const exported = await dump.json() as { user: { email: string }, data: { notes: { body: string }[] } }
67
+ expect(exported.user.email).toBe('ada@example.com')
68
+ expect(exported.data.notes.map(note => note.body)).toEqual(['Buy milk'])
61
69
 
62
70
  const deleted = await app.submit('/auth/me/delete', { confirm: 'on', redirect: '/' }, fromDashboard)
63
71
  expect(deleted.headers.get('location')).toBe('/')
72
+ const database = new Database(process.env.DATABASE)
73
+ expect(database.query('select count(*) as n from notes').get()).toEqual({ n: 0 })
74
+ database.close()
64
75
  expect((await app.get('/dashboard')).status).toBe(302)
65
76
  expect((await app.submit('/auth/signin', credentials, fromLogin)).headers.get('location')).toBe('/login?error=signin.invalid-credentials')
66
77
  })
@@ -0,0 +1,11 @@
1
+ # __APP_NAME__
2
+
3
+ A prunelle plugin. Read `node_modules/prunelle/llms.txt` before writing code: it holds prunelle's whole API, the `PrunellePlugin` hooks included, for the version installed here.
4
+
5
+ ## Workflow
6
+
7
+ - **Verify changes**: `bun test` (the plugin mounted on `tests/fixture` with `testApp` from `prunelle/test`) and `bun run check` (TypeScript).
8
+ - **Keep `llms.txt` current**: it is what an agent working on an app reads to use this plugin, and `prunelle docs` searches it. A change to an option or an export updates it in the same commit.
9
+ - **Report mistakes early**: a misused option throws from the factory, what may be intended is a `ctx.warn()` in `check()`, and what production cannot run without is a message from `requires()`. Each names the option and its fix.
10
+
11
+ @node_modules/prunelle/llms.txt
@@ -0,0 +1 @@
1
+ @AGENTS.md
@@ -0,0 +1,27 @@
1
+ # __APP_NAME__
2
+
3
+ A [prunelle](https://prunelle.kaynooo.fr) plugin serving `/.well-known/security.txt`.
4
+
5
+ ```sh
6
+ bun add __APP_NAME__
7
+ ```
8
+
9
+ ```ts
10
+ // prunelle.config.ts
11
+ import { defineConfig } from 'prunelle/config'
12
+ import { securityTxt } from '__APP_NAME__'
13
+
14
+ export default defineConfig({ plugins: [securityTxt({ contact: 'mailto:security@example.com', expires: '2027-06-30' })] })
15
+ ```
16
+
17
+ `llms.txt` documents the options for coding agents, and `prunelle docs` searches it in any app that installs the plugin.
18
+
19
+ ## Developing
20
+
21
+ ```sh
22
+ bun install
23
+ bun test
24
+ bun run check
25
+ ```
26
+
27
+ The tests mount the plugin on the app in `tests/fixture` with `testApp` from `prunelle/test`, in-process and with no port bound. [Writing a plugin](https://prunelle.kaynooo.fr/docs/production/writing-a-plugin) covers each hook and how to publish.
@@ -0,0 +1,2 @@
1
+ node_modules/
2
+ .prunelle-cache/
@@ -0,0 +1,19 @@
1
+ # __APP_NAME__ Reference
2
+
3
+ > A prunelle plugin serving `/.well-known/security.txt` (RFC 9116): where to report a vulnerability, and until when that address holds.
4
+
5
+ ## Imports
6
+
7
+ - `__APP_NAME__`: `securityTxt`; type `SecurityTxtOptions`.
8
+
9
+ ## Setup (`securityTxt(options)`)
10
+
11
+ ```ts
12
+ import { defineConfig } from 'prunelle/config'
13
+ import { securityTxt } from '__APP_NAME__'
14
+
15
+ export default defineConfig({ plugins: [securityTxt({ contact: 'mailto:security@example.com', expires: '2027-06-30' })] })
16
+ ```
17
+
18
+ - `contact`: a `mailto:` or `https://` URL; anything else throws as the config loads.
19
+ - `expires`: an ISO 8601 date, served in full (`2027-06-30T00:00:00.000Z`). One that is not a date throws; one more than a year ahead is a `prunelle check` warning; once it has passed, a production start refuses through `requires()`.
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "__APP_NAME__",
3
+ "version": "0.1.0",
4
+ "description": "A prunelle plugin serving /.well-known/security.txt",
5
+ "type": "module",
6
+ "sideEffects": false,
7
+ "files": [
8
+ "src",
9
+ "llms.txt"
10
+ ],
11
+ "exports": {
12
+ ".": "./src/index.ts"
13
+ },
14
+ "keywords": [
15
+ "prunelle",
16
+ "prunelle-plugin"
17
+ ],
18
+ "engines": {
19
+ "bun": ">=1.4.0"
20
+ },
21
+ "scripts": {
22
+ "check": "tsc",
23
+ "test": "bun test"
24
+ },
25
+ "peerDependencies": {
26
+ "prunelle": "__PRUNELLE_VERSION__"
27
+ },
28
+ "devDependencies": {
29
+ "@types/bun": "^1.4.4",
30
+ "prunelle": "__PRUNELLE_VERSION__",
31
+ "typescript": "^6.0.3"
32
+ }
33
+ }
@@ -0,0 +1,40 @@
1
+ // A prunelle plugin serving /.well-known/security.txt (RFC 9116): where to report a vulnerability, and until when that address holds.
2
+
3
+ import type { PrunellePlugin } from 'prunelle/config'
4
+ import { definePlugin } from 'prunelle/config'
5
+
6
+ export interface SecurityTxtOptions {
7
+ /** Where to report a vulnerability: a `mailto:` or `https://` URL. */
8
+ contact: string
9
+ /** When the file goes stale, as an ISO 8601 date such as `2027-06-30`: RFC 9116 asks for less than a year ahead. */
10
+ expires: string
11
+ }
12
+
13
+ const CONTACT_RE = /^(?:mailto:|https:\/\/)\S+$/
14
+ const YEAR_MS = 365 * 24 * 60 * 60 * 1000
15
+
16
+ export function securityTxt(options: SecurityTxtOptions): PrunellePlugin {
17
+ // A misuse throws as the config loads, the earliest a plugin can report it, naming the option and its fix.
18
+ if (!CONTACT_RE.test(options.contact))
19
+ throw new Error(`__APP_NAME__: contact "${options.contact}" is neither a mailto: nor an https:// URL: give the address reports go to, such as mailto:security@example.com.`)
20
+ const expires = Date.parse(options.expires)
21
+ if (Number.isNaN(expires))
22
+ throw new Error(`__APP_NAME__: expires "${options.expires}" is not a date: give one such as 2027-06-30.`)
23
+ const body = `Contact: ${options.contact}\nExpires: ${new Date(expires).toISOString()}\n`
24
+
25
+ return definePlugin({
26
+ name: '__APP_NAME__',
27
+ server(api) {
28
+ api.route('GET', '/.well-known/security.txt', () => new Response(body, { headers: { 'content-type': 'text/plain; charset=utf-8' } }))
29
+ },
30
+ // What may be intended: `prunelle check` warns, and fails only under --strict.
31
+ check(ctx) {
32
+ if (expires - Date.now() > YEAR_MS)
33
+ ctx.warn(`__APP_NAME__: expires ${options.expires} is more than a year ahead, and RFC 9116 asks for less so a stale contact does not linger: set an earlier date.`)
34
+ },
35
+ // What production cannot run without: a production start refuses while any message is returned, and dev, tests and check warn, each after the plugin's name.
36
+ requires() {
37
+ return expires <= Date.now() ? [`security.txt expired on ${options.expires}, so reporters are told not to trust it: set \`expires\` to a date less than a year ahead.`] : []
38
+ },
39
+ })
40
+ }
@@ -0,0 +1,4 @@
1
+ import { defineConfig } from 'prunelle/config'
2
+
3
+ // No legal notice: this app exists only for the tests, and is never published.
4
+ export default defineConfig({ lang: 'en', compliance: { legalNotice: false } })
@@ -0,0 +1,11 @@
1
+ import { Head } from 'prunelle'
2
+
3
+ // The smallest app the tests mount the plugin on: what they read comes from the plugin.
4
+ export default function Home() {
5
+ return (
6
+ <>
7
+ <Head><title>Fixture</title></Head>
8
+ <h1>Fixture</h1>
9
+ </>
10
+ )
11
+ }
@@ -0,0 +1,42 @@
1
+ import { describe, expect, it } from 'bun:test'
2
+ import { join } from 'node:path'
3
+ import { testApp } from 'prunelle/test'
4
+ import { securityTxt } from '../src/index'
5
+
6
+ // The app the plugin is mounted on: `testApp` builds it in-process, with no port bound.
7
+ const root = join(import.meta.dir, 'fixture')
8
+ const DAY_MS = 24 * 60 * 60 * 1000
9
+ const daysFromNow = (days: number): string => new Date(Date.now() + days * DAY_MS).toISOString()
10
+
11
+ function checkWarnings(plugin: ReturnType<typeof securityTxt>): string[] {
12
+ const warnings: string[] = []
13
+ plugin.check?.({ root, routesDir: join(root, 'src/routes'), error: message => warnings.push(`error: ${message}`), warn: message => warnings.push(message) })
14
+ return warnings
15
+ }
16
+
17
+ describe('__APP_NAME__', () => {
18
+ it('serves the contact and the expiry date, written in full as RFC 9116 asks, at /.well-known/security.txt', async () => {
19
+ const day = daysFromNow(30).slice(0, 10)
20
+ await using app = await testApp({ root, plugins: [securityTxt({ contact: 'mailto:security@example.com', expires: day })] })
21
+ const res = await app.get('/.well-known/security.txt')
22
+ expect(res.status).toBe(200)
23
+ expect(res.headers.get('content-type')).toBe('text/plain; charset=utf-8')
24
+ expect(await res.text()).toBe(`Contact: mailto:security@example.com\nExpires: ${day}T00:00:00.000Z\n`)
25
+ })
26
+
27
+ it('refuses a contact that is not a mailto: or https:// URL, and an expiry that is not a date', () => {
28
+ expect(() => securityTxt({ contact: 'security@example.com', expires: '2027-06-30' })).toThrow('is neither a mailto: nor an https:// URL')
29
+ expect(() => securityTxt({ contact: 'https://example.com/report', expires: 'next year' })).toThrow('is not a date')
30
+ })
31
+
32
+ it('warns in prunelle check when the expiry is more than a year ahead', () => {
33
+ expect(checkWarnings(securityTxt({ contact: 'mailto:security@example.com', expires: daysFromNow(400) }))).toEqual([expect.stringContaining('is more than a year ahead')])
34
+ expect(checkWarnings(securityTxt({ contact: 'mailto:security@example.com', expires: daysFromNow(300) }))).toEqual([])
35
+ })
36
+
37
+ it('stops a production start once the file has expired', async () => {
38
+ const context = { root, site: undefined, consentCategories: [] }
39
+ expect(await securityTxt({ contact: 'mailto:security@example.com', expires: daysFromNow(-1) }).requires?.(context)).toEqual([expect.stringContaining('security.txt expired on')])
40
+ expect(await securityTxt({ contact: 'mailto:security@example.com', expires: daysFromNow(1) }).requires?.(context)).toEqual([])
41
+ })
42
+ })
@@ -0,0 +1,19 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ESNext",
4
+ "module": "ESNext",
5
+ "moduleResolution": "bundler",
6
+ "jsx": "react-jsx",
7
+ "jsxImportSource": "prunelle",
8
+ "strict": true,
9
+ "noEmit": true,
10
+ "skipLibCheck": true,
11
+ "types": [
12
+ "bun"
13
+ ]
14
+ },
15
+ "include": [
16
+ "src",
17
+ "tests"
18
+ ]
19
+ }