prunelle 0.0.0-stage → 0.7.2

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 (222) hide show
  1. package/LICENSE +21 -0
  2. package/llms.txt +245 -0
  3. package/package.json +62 -4
  4. package/src/a11y/declaration.ts +373 -0
  5. package/src/a11y/index.ts +477 -0
  6. package/src/a11y/rgaa-map.ts +230 -0
  7. package/src/ambient.d.ts +6 -0
  8. package/src/attrs.ts +43 -0
  9. package/src/build/bundle.ts +661 -0
  10. package/src/build/byte-size.ts +11 -0
  11. package/src/build/carry.ts +57 -0
  12. package/src/build/client-entries.ts +107 -0
  13. package/src/build/client-plugin.ts +175 -0
  14. package/src/build/client-prune.ts +168 -0
  15. package/src/build/compile.ts +130 -0
  16. package/src/build/ecoindex.ts +238 -0
  17. package/src/build/files.ts +171 -0
  18. package/src/build/first-load.ts +235 -0
  19. package/src/build/graph.ts +237 -0
  20. package/src/build/index.ts +678 -0
  21. package/src/build/is-inside.ts +12 -0
  22. package/src/build/island-promote.ts +311 -0
  23. package/src/build/islands.ts +452 -0
  24. package/src/build/manifest.ts +204 -0
  25. package/src/build/output.ts +81 -0
  26. package/src/build/plugin-host.ts +294 -0
  27. package/src/build/precompress.ts +139 -0
  28. package/src/build/prerender.ts +288 -0
  29. package/src/build/reach.ts +200 -0
  30. package/src/build/render-rules.ts +205 -0
  31. package/src/build/report.ts +290 -0
  32. package/src/build/resolve.ts +52 -0
  33. package/src/build/routes.ts +342 -0
  34. package/src/build/server-entry.ts +38 -0
  35. package/src/build/static-headers.ts +112 -0
  36. package/src/build/static.ts +209 -0
  37. package/src/build/status-pages.ts +127 -0
  38. package/src/build/style-bundles.ts +526 -0
  39. package/src/cli/check-forms.ts +636 -0
  40. package/src/cli/check-imports.ts +31 -0
  41. package/src/cli/check-links.ts +632 -0
  42. package/src/cli/check-security.ts +64 -0
  43. package/src/cli/check.ts +409 -0
  44. package/src/cli/commands.ts +372 -0
  45. package/src/cli/docs.ts +403 -0
  46. package/src/cli/index.ts +70 -0
  47. package/src/cli/inspect.ts +406 -0
  48. package/src/cli/mcp-tools.ts +492 -0
  49. package/src/cli/mcp.ts +205 -0
  50. package/src/cli/new.ts +52 -0
  51. package/src/cli/render.ts +416 -0
  52. package/src/cli/upgrade-names.ts +209 -0
  53. package/src/cli/upgrade.ts +367 -0
  54. package/src/compiler/analyze.ts +651 -0
  55. package/src/compiler/ast.ts +82 -0
  56. package/src/compiler/browser-globals.ts +250 -0
  57. package/src/compiler/cacheability-private.ts +566 -0
  58. package/src/compiler/cacheability.ts +335 -0
  59. package/src/compiler/compliance-eco.ts +71 -0
  60. package/src/compiler/compliance-rgaa.ts +614 -0
  61. package/src/compiler/compliance-urls.ts +148 -0
  62. package/src/compiler/compliance.ts +653 -0
  63. package/src/compiler/data-keys.ts +302 -0
  64. package/src/compiler/dev-source.ts +87 -0
  65. package/src/compiler/dom-attrs.ts +452 -0
  66. package/src/compiler/dom.ts +641 -0
  67. package/src/compiler/emit.ts +131 -0
  68. package/src/compiler/expr-context.ts +587 -0
  69. package/src/compiler/foreign-imports.ts +88 -0
  70. package/src/compiler/index.ts +8 -0
  71. package/src/compiler/interactivity.ts +366 -0
  72. package/src/compiler/mistakes.ts +520 -0
  73. package/src/compiler/node-env.ts +50 -0
  74. package/src/compiler/patch.ts +97 -0
  75. package/src/compiler/props-body.ts +60 -0
  76. package/src/compiler/props.ts +511 -0
  77. package/src/compiler/scope.ts +635 -0
  78. package/src/compiler/server-only.ts +365 -0
  79. package/src/compiler/shared.ts +397 -0
  80. package/src/compiler/signal-reads.ts +246 -0
  81. package/src/compiler/slots.ts +227 -0
  82. package/src/compiler/spread-handlers.ts +295 -0
  83. package/src/compiler/ssr-helpers.ts +555 -0
  84. package/src/compiler/ssr-island.ts +90 -0
  85. package/src/compiler/ssr-prunelle-tags.ts +69 -0
  86. package/src/compiler/ssr.ts +525 -0
  87. package/src/context.ts +199 -0
  88. package/src/dev/child.ts +30 -0
  89. package/src/dev/error-log.ts +177 -0
  90. package/src/dev/host-check.ts +32 -0
  91. package/src/dev/index.ts +268 -0
  92. package/src/dev/modules.ts +68 -0
  93. package/src/dev/open-editor.ts +228 -0
  94. package/src/dev/protocol.ts +24 -0
  95. package/src/dev/queue.ts +79 -0
  96. package/src/dev/reload.ts +159 -0
  97. package/src/dev/supervisor.ts +212 -0
  98. package/src/dev/watchers.ts +276 -0
  99. package/src/dom/action.ts +187 -0
  100. package/src/dom/attrs.ts +84 -0
  101. package/src/dom/claim.ts +120 -0
  102. package/src/dom/extras.ts +579 -0
  103. package/src/dom/index.ts +329 -0
  104. package/src/dom/island.ts +318 -0
  105. package/src/dom/loop.ts +484 -0
  106. package/src/dom/revive.ts +36 -0
  107. package/src/eco/declaration.ts +328 -0
  108. package/src/eco/index.ts +69 -0
  109. package/src/index.ts +22 -0
  110. package/src/jsx-runtime.ts +566 -0
  111. package/src/plugins/config.ts +92 -0
  112. package/src/plugins/context.ts +109 -0
  113. package/src/plugins/index.ts +14 -0
  114. package/src/plugins/load.ts +90 -0
  115. package/src/plugins/order.ts +148 -0
  116. package/src/plugins/public.ts +15 -0
  117. package/src/plugins/requires.ts +47 -0
  118. package/src/plugins/services.ts +119 -0
  119. package/src/plugins/types.ts +278 -0
  120. package/src/reactivity/graph.ts +259 -0
  121. package/src/reactivity/index.ts +541 -0
  122. package/src/reactivity/selector.ts +78 -0
  123. package/src/reactivity/store.ts +252 -0
  124. package/src/server/action-flash.ts +158 -0
  125. package/src/server/action-protect.ts +199 -0
  126. package/src/server/action-registry.ts +78 -0
  127. package/src/server/action.ts +511 -0
  128. package/src/server/app-entry.ts +23 -0
  129. package/src/server/app-secret.ts +63 -0
  130. package/src/server/assets.ts +168 -0
  131. package/src/server/compress.ts +646 -0
  132. package/src/server/consent.ts +290 -0
  133. package/src/server/cookies.ts +153 -0
  134. package/src/server/document.ts +286 -0
  135. package/src/server/env.ts +47 -0
  136. package/src/server/errors.ts +396 -0
  137. package/src/server/form-schema.ts +337 -0
  138. package/src/server/health.ts +275 -0
  139. package/src/server/http.ts +588 -0
  140. package/src/server/index.ts +615 -0
  141. package/src/server/lifecycle.ts +75 -0
  142. package/src/server/loader.ts +131 -0
  143. package/src/server/page.ts +122 -0
  144. package/src/server/param-matchers.ts +112 -0
  145. package/src/server/plugin-host.ts +267 -0
  146. package/src/server/prerendered.ts +71 -0
  147. package/src/server/public.ts +20 -0
  148. package/src/server/rate-limit.ts +199 -0
  149. package/src/server/region.ts +96 -0
  150. package/src/server/request-body.ts +27 -0
  151. package/src/server/request-path.ts +73 -0
  152. package/src/server/request-scope.ts +22 -0
  153. package/src/server/route-cache.ts +541 -0
  154. package/src/server/route-table.ts +424 -0
  155. package/src/server/router.ts +261 -0
  156. package/src/server/routing.ts +388 -0
  157. package/src/server/sanitize-html.ts +309 -0
  158. package/src/server/security.ts +550 -0
  159. package/src/server/serve-handlers.ts +93 -0
  160. package/src/server/served-file.ts +128 -0
  161. package/src/server/standard-schema.ts +104 -0
  162. package/src/server/start.ts +143 -0
  163. package/src/server/static-dir.ts +246 -0
  164. package/src/server/stream.ts +233 -0
  165. package/src/ssr/consent-banner.ts +58 -0
  166. package/src/ssr/context.ts +399 -0
  167. package/src/ssr/index.ts +624 -0
  168. package/src/ssr/navigation.ts +96 -0
  169. package/src/ssr/region.ts +192 -0
  170. package/src/style.ts +96 -0
  171. package/src/test/browser.ts +201 -0
  172. package/src/test/index.ts +262 -0
  173. package/src/ts-plugin/diagnostics.ts +505 -0
  174. package/src/ts-plugin/fixes.ts +364 -0
  175. package/src/ts-plugin/index.ts +194 -0
  176. package/src/unrenderable.ts +35 -0
  177. package/templates/_shared/AGENTS.md +10 -0
  178. package/templates/_shared/CLAUDE.md +1 -0
  179. package/templates/_shared/gitignore +8 -0
  180. package/templates/_shared/mcp.json +11 -0
  181. package/templates/_shared/public/robots.txt +2 -0
  182. package/templates/_shared/src/routes/legal.tsx +22 -0
  183. package/templates/_shared/tsconfig.json +25 -0
  184. package/templates/app/.env.example +8 -0
  185. package/templates/app/package.json +22 -0
  186. package/templates/app/prunelle.config.ts +13 -0
  187. package/templates/app/src/auth.ts +12 -0
  188. package/templates/app/src/mail.ts +4 -0
  189. package/templates/app/src/routes/_layout.tsx +40 -0
  190. package/templates/app/src/routes/dashboard/_middleware.ts +3 -0
  191. package/templates/app/src/routes/dashboard/index.tsx +47 -0
  192. package/templates/app/src/routes/index.tsx +15 -0
  193. package/templates/app/src/routes/login.tsx +53 -0
  194. package/templates/app/src/routes/privacy.tsx +25 -0
  195. package/templates/app/src/styles/app.css +14 -0
  196. package/templates/app/tests/app.test.ts +73 -0
  197. package/templates/blog/content/posts/welcome.md +9 -0
  198. package/templates/blog/package.json +22 -0
  199. package/templates/blog/prunelle.config.ts +23 -0
  200. package/templates/blog/src/posts.ts +26 -0
  201. package/templates/blog/src/routes/_layout.tsx +25 -0
  202. package/templates/blog/src/routes/blog/[slug].tsx +31 -0
  203. package/templates/blog/src/routes/index.tsx +33 -0
  204. package/templates/blog/src/styles/app.css +15 -0
  205. package/templates/fullstack/package.json +19 -0
  206. package/templates/fullstack/prunelle.config.ts +6 -0
  207. package/templates/fullstack/src/actions/greet.ts +19 -0
  208. package/templates/fullstack/src/components/Counter.tsx +10 -0
  209. package/templates/fullstack/src/components/GreetForm.tsx +28 -0
  210. package/templates/fullstack/src/routes/_layout.tsx +18 -0
  211. package/templates/fullstack/src/routes/api/health.ts +5 -0
  212. package/templates/fullstack/src/routes/index.tsx +13 -0
  213. package/templates/fullstack/src/styles/app.css +39 -0
  214. package/templates/fullstack/tests/app.test.ts +37 -0
  215. package/templates/minimal/package.json +19 -0
  216. package/templates/minimal/prunelle.config.ts +6 -0
  217. package/templates/minimal/src/components/Counter.tsx +10 -0
  218. package/templates/minimal/src/routes/_layout.tsx +18 -0
  219. package/templates/minimal/src/routes/index.tsx +11 -0
  220. package/templates/minimal/src/styles/app.css +32 -0
  221. package/ts-plugin.cjs +4277 -0
  222. package/README.md +0 -3
@@ -0,0 +1,262 @@
1
+ // prunelle/test: in-process testing for pages, middleware, cookies, and actions — zero port binding.
2
+
3
+ import type { BuildManifest, BuildOptions } from '../build/index'
4
+ import type { PrunelleApp, ServerAction, ServerRuntimeConfig } from '../server/index'
5
+ import { existsSync, readdirSync, rmSync } from 'node:fs'
6
+ import { dirname, join, resolve } from 'node:path'
7
+ import { build } from '../build/index'
8
+ import { relPosix } from '../build/files'
9
+ import { processSuffix, removeDeadProcessDirs } from '../build/output'
10
+ import { ensureCacheGitignore } from '../build/precompress'
11
+ import { resolveServerConfig } from '../server/index'
12
+ import { FORM_TIMESTAMP_FIELD, honeypotFieldName, signFormTimestamp } from '../server/action-protect'
13
+ import { startBuiltApp } from '../server/loader'
14
+
15
+ const TEST_DIR_PREFIX = 'test-'
16
+ const ABSOLUTE_URL_RE = /^https?:\/\//i
17
+ const INTEGER_RE = /^-?\d+$/
18
+
19
+ function safeDecodeCookieValue(val: string): string {
20
+ try {
21
+ return decodeURIComponent(val)
22
+ }
23
+ catch {
24
+ return val
25
+ }
26
+ }
27
+
28
+ function isExpiredSetCookie(raw: string): boolean {
29
+ const parts = raw.split(';')
30
+ let hasMaxAge = false
31
+ let maxAgeExpired = false
32
+ let expiresExpired = false
33
+ for (let i = 1; i < parts.length; i++) {
34
+ const seg = parts[i]!.trim()
35
+ const eq = seg.indexOf('=')
36
+ if (eq <= 0)
37
+ continue
38
+ const attrName = seg.slice(0, eq).trim().toLowerCase()
39
+ const attrVal = seg.slice(eq + 1).trim()
40
+ if (attrName === 'max-age' && INTEGER_RE.test(attrVal)) {
41
+ hasMaxAge = true
42
+ maxAgeExpired = Number(attrVal) <= 0
43
+ }
44
+ else if (attrName === 'expires') {
45
+ const ts = Date.parse(attrVal)
46
+ if (Number.isFinite(ts))
47
+ expiresExpired = ts <= Date.now()
48
+ }
49
+ }
50
+ return hasMaxAge ? maxAgeExpired : expiresExpired
51
+ }
52
+
53
+ function defaultOriginFor(target: string): string {
54
+ if (ABSOLUTE_URL_RE.test(target)) {
55
+ try {
56
+ return new URL(target).origin
57
+ }
58
+ catch {
59
+ return 'http://localhost'
60
+ }
61
+ }
62
+ return 'http://localhost'
63
+ }
64
+
65
+ export interface TestAppOptions extends Omit<BuildOptions, 'compile' | 'static'>, Partial<ServerRuntimeConfig> {
66
+ prebuilt?: boolean | undefined
67
+ }
68
+
69
+ export interface TestApp {
70
+ readonly app: PrunelleApp
71
+ readonly manifest: BuildManifest | undefined
72
+ readonly cookies: Map<string, string>
73
+ /** The unhandled route, action and plugin errors seen since the app started, as `route GET /path: message`; a response that is a 500 has one here. */
74
+ readonly errors: string[]
75
+ fetch(pathOrReq: string | Request, init?: RequestInit): Promise<Response>
76
+ get(path: string, headers?: HeadersInit): Promise<Response>
77
+ post(path: string, body?: unknown, headers?: HeadersInit): Promise<Response>
78
+ /** The app's own bundled copy of a route, layout, middleware, action file or `prunelle.config.ts`, by path from the root. */
79
+ import(path: string): Promise<Record<string, unknown>>
80
+ /** Posts `fields` as a form to an action, given as one from `import()` or as its URL. */
81
+ submit(action: ServerAction<never, unknown> | string, fields: Record<string, string>, headers?: HeadersInit): Promise<Response>
82
+ clearCookies(): void
83
+ /** Runs plugin shutdown hooks and cleans up app resources (idempotent). */
84
+ dispose(): Promise<void>
85
+ [Symbol.asyncDispose](): Promise<void>
86
+ }
87
+
88
+ /** The folder of the latest testApp() build of each app root in this process, which `prebuilt: true` reuses. */
89
+ const latestOutDirs = new Map<string, string>()
90
+ const ownOutDirs: string[] = []
91
+
92
+ /** A folder under `.prunelle-cache/` for one testApp() build, removed when the process exits, so tests never replace the app's deployable dist/. */
93
+ function ownOutDir(root: string): string {
94
+ const cacheDir = join(root, '.prunelle-cache')
95
+ removeDeadProcessDirs(cacheDir, TEST_DIR_PREFIX)
96
+ ensureCacheGitignore(cacheDir)
97
+ if (ownOutDirs.length === 0) {
98
+ process.once('exit', () => {
99
+ for (const dir of ownOutDirs) {
100
+ rmSync(dir, { recursive: true, force: true })
101
+ const parent = dirname(dir)
102
+ if (!existsSync(parent))
103
+ continue
104
+ const remaining = readdirSync(parent)
105
+ if (remaining.length === 0 || (remaining.length === 1 && remaining[0] === '.gitignore'))
106
+ rmSync(parent, { recursive: true, force: true })
107
+ }
108
+ })
109
+ }
110
+ const dir = join(cacheDir, `${TEST_DIR_PREFIX}${processSuffix()}`)
111
+ ownOutDirs.push(dir)
112
+ latestOutDirs.set(root, dir)
113
+ return dir
114
+ }
115
+
116
+ export async function testApp(options: TestAppOptions): Promise<TestApp> {
117
+ const root = resolve(options.root)
118
+ const outDir = options.outDir ?? (options.prebuilt ? latestOutDirs.get(root) : ownOutDir(root))
119
+ const manifest = options.prebuilt ? undefined : await build({
120
+ root: options.root,
121
+ routesDir: options.routesDir,
122
+ outDir,
123
+ publicDir: options.publicDir,
124
+ minify: options.minify ?? false,
125
+ assetPrefix: options.assetPrefix,
126
+ site: options.site,
127
+ plugins: options.plugins,
128
+ cache: options.cache,
129
+ analyze: options.analyze,
130
+ analyzeJson: options.analyzeJson,
131
+ onAnalyze: options.onAnalyze,
132
+ maxJs: options.maxJs,
133
+ prerender: options.prerender,
134
+ autoPrerender: options.autoPrerender,
135
+ copyPublic: options.copyPublic,
136
+ dev: options.dev,
137
+ nodeEnv: options.nodeEnv,
138
+ })
139
+
140
+ const errors: string[] = []
141
+ const { app, entry } = await startBuiltApp({
142
+ root: options.root,
143
+ outDir,
144
+ dev: options.dev,
145
+ nodeEnv: options.nodeEnv,
146
+ ...resolveServerConfig(undefined, options),
147
+ errorSink: ({ error, request, source }) => {
148
+ errors.push(`${source} ${request.method} ${new URL(request.url).pathname}: ${error instanceof Error ? error.message : String(error)}`)
149
+ },
150
+ })
151
+
152
+ const cookies = new Map<string, string>()
153
+
154
+ function cookieHeader(): string | undefined {
155
+ if (cookies.size === 0)
156
+ return undefined
157
+ return [...cookies.entries()].map(([k, v]) => `${k}=${encodeURIComponent(v)}`).join('; ')
158
+ }
159
+
160
+ function storeSetCookies(res: Response): void {
161
+ const list = res.headers.getSetCookie?.() ?? []
162
+ for (const raw of list) {
163
+ const first = raw.split(';')[0] ?? ''
164
+ const eq = first.indexOf('=')
165
+ if (eq <= 0)
166
+ continue
167
+ const name = first.slice(0, eq).trim()
168
+ const val = first.slice(eq + 1).trim()
169
+ if (isExpiredSetCookie(raw) || val === '')
170
+ cookies.delete(name)
171
+ else
172
+ cookies.set(name, safeDecodeCookieValue(val))
173
+ }
174
+ }
175
+
176
+ async function doFetch(pathOrReq: string | Request, init?: RequestInit): Promise<Response> {
177
+ const req = typeof pathOrReq === 'string'
178
+ ? new Request(ABSOLUTE_URL_RE.test(pathOrReq) ? pathOrReq : `http://localhost${pathOrReq}`, init)
179
+ : (init !== undefined ? new Request(pathOrReq, init) : pathOrReq)
180
+
181
+ const existing = cookieHeader()
182
+ let withCookies = req
183
+ if (existing !== undefined && !req.headers.has('cookie')) {
184
+ const headers = new Headers(req.headers)
185
+ headers.set('cookie', existing)
186
+ withCookies = new Request(req, { headers })
187
+ }
188
+
189
+ const res = await app.handle(withCookies)
190
+ storeSetCookies(res)
191
+ return res
192
+ }
193
+
194
+ let disposed = false
195
+ async function dispose(): Promise<void> {
196
+ if (disposed)
197
+ return
198
+ disposed = true
199
+ await app.dispose?.()
200
+ }
201
+
202
+ return {
203
+ app,
204
+ manifest,
205
+ cookies,
206
+ errors,
207
+ fetch: doFetch,
208
+ get(path, headers) {
209
+ return doFetch(path, headers !== undefined ? { method: 'GET', headers } : { method: 'GET' })
210
+ },
211
+ post(path, body, headers) {
212
+ const h = new Headers(headers)
213
+ if (!h.has('origin'))
214
+ h.set('origin', defaultOriginFor(path))
215
+ if (body instanceof FormData)
216
+ return doFetch(path, { method: 'POST', body, headers: h })
217
+ if (!h.has('content-type'))
218
+ h.set('content-type', 'application/json')
219
+ if (!h.has('accept'))
220
+ h.set('accept', 'application/json')
221
+ return doFetch(path, {
222
+ method: 'POST',
223
+ body: body === undefined ? '{}' : JSON.stringify(body),
224
+ headers: h,
225
+ })
226
+ },
227
+ async import(path) {
228
+ const source = relPosix(options.root, resolve(options.root, path))
229
+ const mod = entry?.sources[source]
230
+ if (mod === undefined)
231
+ throw new Error(`prunelle: testApp().import('${path}') found no bundled module. Only routes, layouts, middlewares, action files and prunelle.config.ts are bundled entries; reach ${source} through one of them.`)
232
+ return mod
233
+ },
234
+ submit(action, fields, headers) {
235
+ const actionUrl = typeof action === 'string' ? action : action.url
236
+ if (actionUrl === '')
237
+ throw new Error('prunelle: testApp().submit() got an action with no URL. Pass one from `await app.import(path)`, not one the test file imported itself.')
238
+ const fd = new FormData()
239
+ for (const [k, v] of Object.entries(fields))
240
+ fd.append(k, v)
241
+ if (typeof action !== 'string' && action.protect !== undefined) {
242
+ const hp = honeypotFieldName(action.protect)
243
+ if (hp !== undefined && !Object.hasOwn(fields, hp))
244
+ fd.append(hp, '')
245
+ if (action.protect.minTime !== undefined && !Object.hasOwn(fields, FORM_TIMESTAMP_FIELD))
246
+ fd.append(FORM_TIMESTAMP_FIELD, signFormTimestamp(Date.now() - Math.max(0, action.protect.minTime) - 1000, actionUrl))
247
+ }
248
+ const h = new Headers(headers)
249
+ if (!h.has('origin'))
250
+ h.set('origin', defaultOriginFor(actionUrl))
251
+ return doFetch(actionUrl, { method: 'POST', body: fd, headers: h })
252
+ },
253
+ clearCookies() {
254
+ cookies.clear()
255
+ },
256
+ dispose,
257
+ [Symbol.asyncDispose]: dispose,
258
+ }
259
+ }
260
+
261
+ export { testBrowser } from './browser'
262
+ export type { BrowserPage, BrowserTest, BrowserTestOptions } from './browser'