prunelle 0.1.1 → 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.
- package/LICENSE +21 -0
- package/llms.txt +137 -40
- package/package.json +11 -2
- package/src/a11y/declaration.ts +373 -0
- package/src/a11y/index.ts +477 -0
- package/src/a11y/rgaa-map.ts +230 -0
- package/src/attrs.ts +11 -3
- package/src/build/bundle.ts +89 -41
- package/src/build/byte-size.ts +11 -0
- package/src/build/client-entries.ts +3 -2
- package/src/build/client-plugin.ts +11 -5
- package/src/build/client-prune.ts +56 -6
- package/src/build/compile.ts +13 -10
- package/src/build/ecoindex.ts +238 -0
- package/src/build/files.ts +15 -0
- package/src/build/first-load.ts +85 -14
- package/src/build/graph.ts +29 -39
- package/src/build/index.ts +225 -122
- package/src/build/island-promote.ts +311 -0
- package/src/build/islands.ts +14 -203
- package/src/build/manifest.ts +56 -116
- package/src/build/output.ts +3 -6
- package/src/build/plugin-host.ts +115 -41
- package/src/build/precompress.ts +16 -3
- package/src/build/prerender.ts +177 -39
- package/src/build/reach.ts +200 -0
- package/src/build/render-rules.ts +2 -2
- package/src/build/report.ts +48 -5
- package/src/build/resolve.ts +8 -6
- package/src/build/routes.ts +175 -10
- package/src/build/static-headers.ts +112 -0
- package/src/build/static.ts +139 -39
- package/src/build/status-pages.ts +127 -0
- package/src/build/style-bundles.ts +526 -0
- package/src/cli/check-forms.ts +636 -0
- package/src/cli/check-imports.ts +31 -0
- package/src/cli/check-links.ts +632 -0
- package/src/cli/check-security.ts +64 -0
- package/src/cli/check.ts +101 -26
- package/src/cli/commands.ts +120 -11
- package/src/cli/docs.ts +403 -0
- package/src/cli/inspect.ts +105 -15
- package/src/cli/mcp-tools.ts +492 -0
- package/src/cli/mcp.ts +205 -0
- package/src/cli/new.ts +16 -5
- package/src/cli/render.ts +416 -0
- package/src/cli/upgrade-names.ts +73 -13
- package/src/cli/upgrade.ts +156 -22
- package/src/compiler/analyze.ts +87 -54
- package/src/compiler/browser-globals.ts +140 -17
- package/src/compiler/cacheability-private.ts +566 -0
- package/src/compiler/cacheability.ts +335 -0
- package/src/compiler/compliance-eco.ts +71 -0
- package/src/compiler/compliance-rgaa.ts +614 -0
- package/src/compiler/compliance-urls.ts +148 -0
- package/src/compiler/compliance.ts +407 -352
- package/src/compiler/data-keys.ts +199 -192
- package/src/compiler/dev-source.ts +87 -0
- package/src/compiler/dom-attrs.ts +138 -65
- package/src/compiler/dom.ts +517 -442
- package/src/compiler/expr-context.ts +260 -81
- package/src/compiler/foreign-imports.ts +88 -0
- package/src/compiler/mistakes.ts +23 -2
- package/src/compiler/props-body.ts +60 -0
- package/src/compiler/props.ts +87 -36
- package/src/compiler/scope.ts +400 -333
- package/src/compiler/server-only.ts +190 -151
- package/src/compiler/shared.ts +42 -14
- package/src/compiler/signal-reads.ts +6 -2
- package/src/compiler/spread-handlers.ts +295 -0
- package/src/compiler/ssr-prunelle-tags.ts +27 -14
- package/src/compiler/ssr.ts +220 -89
- package/src/context.ts +34 -0
- package/src/dev/error-log.ts +177 -0
- package/src/dev/host-check.ts +32 -0
- package/src/dev/index.ts +94 -34
- package/src/dev/open-editor.ts +228 -0
- package/src/dev/protocol.ts +3 -0
- package/src/dev/reload.ts +5 -3
- package/src/dev/supervisor.ts +50 -25
- package/src/dev/watchers.ts +74 -13
- package/src/dom/action.ts +47 -21
- package/src/dom/attrs.ts +15 -3
- package/src/dom/extras.ts +57 -40
- package/src/dom/index.ts +54 -14
- package/src/dom/island.ts +102 -68
- package/src/dom/loop.ts +66 -32
- package/src/eco/declaration.ts +328 -0
- package/src/eco/index.ts +69 -0
- package/src/index.ts +2 -2
- package/src/jsx-runtime.ts +2 -2
- package/src/plugins/config.ts +18 -12
- package/src/plugins/context.ts +44 -1
- package/src/plugins/index.ts +2 -2
- package/src/plugins/load.ts +30 -10
- package/src/plugins/order.ts +38 -1
- package/src/plugins/public.ts +6 -3
- package/src/plugins/services.ts +84 -0
- package/src/plugins/types.ts +51 -2
- package/src/reactivity/graph.ts +10 -0
- package/src/reactivity/index.ts +24 -16
- package/src/reactivity/selector.ts +31 -4
- package/src/reactivity/store.ts +64 -20
- package/src/server/action-flash.ts +158 -0
- package/src/server/action-protect.ts +199 -0
- package/src/server/action-registry.ts +1 -1
- package/src/server/action.ts +188 -152
- package/src/server/app-secret.ts +63 -0
- package/src/server/assets.ts +34 -2
- package/src/server/compress.ts +96 -10
- package/src/server/consent.ts +59 -22
- package/src/server/cookies.ts +16 -2
- package/src/server/document.ts +25 -10
- package/src/server/env.ts +14 -0
- package/src/server/errors.ts +308 -65
- package/src/server/form-schema.ts +337 -0
- package/src/server/health.ts +275 -0
- package/src/server/http.ts +186 -39
- package/src/server/index.ts +272 -273
- package/src/server/loader.ts +15 -2
- package/src/server/page.ts +15 -0
- package/src/server/param-matchers.ts +112 -0
- package/src/server/plugin-host.ts +125 -88
- package/src/server/public.ts +6 -5
- package/src/server/rate-limit.ts +102 -36
- package/src/server/request-body.ts +1 -1
- package/src/server/route-cache.ts +541 -0
- package/src/server/route-table.ts +356 -11
- package/src/server/router.ts +125 -40
- package/src/server/routing.ts +112 -63
- package/src/server/sanitize-html.ts +309 -0
- package/src/server/security.ts +279 -52
- package/src/server/serve-handlers.ts +31 -7
- package/src/server/standard-schema.ts +17 -0
- package/src/server/start.ts +60 -16
- package/src/server/static-dir.ts +91 -12
- package/src/server/stream.ts +68 -27
- package/src/ssr/consent-banner.ts +58 -0
- package/src/ssr/context.ts +106 -8
- package/src/ssr/index.ts +151 -4
- package/src/ssr/navigation.ts +2 -2
- package/src/ssr/region.ts +40 -19
- package/src/style.ts +64 -1
- package/src/test/browser.ts +28 -13
- package/src/test/index.ts +97 -12
- package/src/ts-plugin/diagnostics.ts +505 -0
- package/src/ts-plugin/fixes.ts +364 -0
- package/src/ts-plugin/index.ts +194 -0
- package/templates/_shared/AGENTS.md +2 -2
- package/templates/_shared/gitignore +5 -0
- package/templates/_shared/mcp.json +11 -0
- package/templates/_shared/src/routes/legal.tsx +15 -1
- package/templates/_shared/tsconfig.json +5 -0
- 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/prunelle.config.ts +1 -1
- package/templates/blog/src/routes/_layout.tsx +6 -1
- package/templates/blog/src/routes/index.tsx +5 -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/ts-plugin.cjs +4277 -0
|
@@ -0,0 +1,335 @@
|
|
|
1
|
+
// Static cacheability detection, `export const cache` AST parsing, and private-read validation for cached routes.
|
|
2
|
+
|
|
3
|
+
import type { ArrowFunctionExpression, Function as OxcFunction, Node, Program } from 'oxc-parser'
|
|
4
|
+
import type { RouteCacheSpec } from '../server/route-cache'
|
|
5
|
+
import type { ModuleAnalysis } from './analyze'
|
|
6
|
+
import type { PrivateGraphView, PrivateReadHit } from './cacheability-private'
|
|
7
|
+
import { buildRouteCacheSpec, parseCacheDuration } from '../server/route-cache'
|
|
8
|
+
import { walk } from './ast'
|
|
9
|
+
import {
|
|
10
|
+
checkHeaderCall,
|
|
11
|
+
findExportedFunction,
|
|
12
|
+
findModulePrivateHits,
|
|
13
|
+
memberPropName,
|
|
14
|
+
PRIVATE_SERVER_IMPORTS,
|
|
15
|
+
propKeyName,
|
|
16
|
+
staticStringOrNumber,
|
|
17
|
+
walkTrackedFunction,
|
|
18
|
+
} from './cacheability-private'
|
|
19
|
+
import { EVENT_ATTR, jsxTagName, resolvePrunelleTag, unwrapTsExpr } from './shared'
|
|
20
|
+
|
|
21
|
+
const CACHE_HELP = 'write a duration such as \'30s\', \'5m\', \'1h\', \'1d\', a positive number of seconds, or { maxAge: \'1h\', staleWhileRevalidate?: \'1d\' }'
|
|
22
|
+
const DYNAMIC_PROPS = new Set(['params', 'url', 'locals', 'cookies', 'headers', 'request', 'consent', 'ip'])
|
|
23
|
+
const DYNAMIC_IMPORTS = new Set(['bun:sqlite', 'node:sqlite', 'sqlite', 'bun:sql'])
|
|
24
|
+
const SYNC_IO_CALLEES = new Set(['readFileSync', 'readdirSync', 'statSync', 'lstatSync', 'existsSync', 'accessSync'])
|
|
25
|
+
const DYNAMIC_TAGS = new Set(['ConsentGate', 'ConsentBanner', 'ConsentPreferences', 'Suspense'])
|
|
26
|
+
const CLIENT_CALLBACK_CALLEES = new Set(['onMount', 'effect', 'onCleanup'])
|
|
27
|
+
|
|
28
|
+
export interface CacheExportResult {
|
|
29
|
+
hasExport: boolean
|
|
30
|
+
spec?: RouteCacheSpec | undefined
|
|
31
|
+
problem?: string | undefined
|
|
32
|
+
offset: number
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface PrivateReadProblem {
|
|
36
|
+
file: string
|
|
37
|
+
offset: number
|
|
38
|
+
message: string
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
interface IslandView {
|
|
42
|
+
readonly targets: ReadonlyMap<string, unknown>
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function parseCacheObjectNode(node: Extract<Node, { type: 'ObjectExpression' }>): { spec: RouteCacheSpec } | { problem: string } {
|
|
46
|
+
let maxAgeMs: number | undefined
|
|
47
|
+
let swrMs: number | undefined
|
|
48
|
+
let swrKeyCount = 0
|
|
49
|
+
for (const prop of node.properties) {
|
|
50
|
+
if (prop.type !== 'Property' || prop.kind !== 'init')
|
|
51
|
+
return { problem: `cache object must use static properties: ${CACHE_HELP}` }
|
|
52
|
+
const key = propKeyName(prop)
|
|
53
|
+
if (key === undefined)
|
|
54
|
+
return { problem: `cache object has a dynamic key: ${CACHE_HELP}` }
|
|
55
|
+
if (key !== 'maxAge' && key !== 'staleWhileRevalidate' && key !== 'swr')
|
|
56
|
+
return { problem: `unknown cache option "${key}": ${CACHE_HELP}` }
|
|
57
|
+
const parsed = staticStringOrNumber(prop.value)
|
|
58
|
+
if (!parsed.ok)
|
|
59
|
+
return { problem: parsed.isLiteral ? `cache.${key} must be a duration string or number of seconds: ${CACHE_HELP}` : `cache.${key} set at runtime: ${CACHE_HELP}` }
|
|
60
|
+
const ms = parseCacheDuration(parsed.value, key !== 'maxAge')
|
|
61
|
+
if (ms === undefined)
|
|
62
|
+
return { problem: `invalid cache.${key} value ${JSON.stringify(parsed.value)}: ${CACHE_HELP}` }
|
|
63
|
+
if (key === 'maxAge') {
|
|
64
|
+
maxAgeMs = ms
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
swrKeyCount++
|
|
68
|
+
swrMs = ms
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
if (maxAgeMs === undefined)
|
|
72
|
+
return { problem: `cache object is missing "maxAge": ${CACHE_HELP}` }
|
|
73
|
+
if (swrKeyCount > 1)
|
|
74
|
+
return { problem: `specify either "staleWhileRevalidate" or "swr", not both: ${CACHE_HELP}` }
|
|
75
|
+
return { spec: buildRouteCacheSpec(maxAgeMs, swrMs ?? maxAgeMs) }
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** Parses and validates `export const cache = ...` (or `const cache = ...; export { cache }`) from a module AST. */
|
|
79
|
+
export function parseCacheExport(program: Program): CacheExportResult {
|
|
80
|
+
const localInits = new Map<string, Node>()
|
|
81
|
+
let exportedLocal: string | undefined
|
|
82
|
+
let exportInit: Node | undefined
|
|
83
|
+
let offset = 0
|
|
84
|
+
let hasExport = false
|
|
85
|
+
|
|
86
|
+
for (const stmt of program.body) {
|
|
87
|
+
if (stmt.type === 'VariableDeclaration') {
|
|
88
|
+
for (const d of stmt.declarations) {
|
|
89
|
+
if (d.id.type === 'Identifier' && d.init !== null)
|
|
90
|
+
localInits.set(d.id.name, d.init)
|
|
91
|
+
}
|
|
92
|
+
continue
|
|
93
|
+
}
|
|
94
|
+
if (stmt.type !== 'ExportNamedDeclaration' || stmt.exportKind === 'type')
|
|
95
|
+
continue
|
|
96
|
+
if (stmt.declaration?.type === 'VariableDeclaration') {
|
|
97
|
+
for (const d of stmt.declaration.declarations) {
|
|
98
|
+
if (d.id.type === 'Identifier' && d.id.name === 'cache') {
|
|
99
|
+
hasExport = true
|
|
100
|
+
offset = d.start
|
|
101
|
+
exportInit = d.init ?? undefined
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
else if (stmt.declaration !== null && 'id' in stmt.declaration && stmt.declaration.id?.type === 'Identifier' && stmt.declaration.id.name === 'cache') {
|
|
106
|
+
return { hasExport: true, offset: stmt.declaration.start, problem: `invalid cache export: ${CACHE_HELP}` }
|
|
107
|
+
}
|
|
108
|
+
for (const spec of stmt.specifiers) {
|
|
109
|
+
const exp = spec.exported.type === 'Identifier' ? spec.exported.name : spec.exported.value
|
|
110
|
+
if (exp === 'cache' && spec.exportKind !== 'type') {
|
|
111
|
+
hasExport = true
|
|
112
|
+
offset = spec.start
|
|
113
|
+
if (stmt.source === null && spec.local.type === 'Identifier')
|
|
114
|
+
exportedLocal = spec.local.name
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
if (!hasExport)
|
|
120
|
+
return { hasExport: false, offset: 0 }
|
|
121
|
+
const init = exportInit ?? (exportedLocal !== undefined ? localInits.get(exportedLocal) : undefined)
|
|
122
|
+
if (init === undefined)
|
|
123
|
+
return { hasExport: true, offset, problem: `cache set at runtime: ${CACHE_HELP}` }
|
|
124
|
+
|
|
125
|
+
const inner = unwrapTsExpr(init)
|
|
126
|
+
if (inner.type === 'ObjectExpression') {
|
|
127
|
+
const res = parseCacheObjectNode(inner)
|
|
128
|
+
return 'spec' in res ? { hasExport: true, offset, spec: res.spec } : { hasExport: true, offset, problem: res.problem }
|
|
129
|
+
}
|
|
130
|
+
const scalar = staticStringOrNumber(inner)
|
|
131
|
+
if (!scalar.ok)
|
|
132
|
+
return { hasExport: true, offset, problem: scalar.isLiteral ? `invalid cache export: ${CACHE_HELP}` : `cache set at runtime: ${CACHE_HELP}` }
|
|
133
|
+
const ms = parseCacheDuration(scalar.value)
|
|
134
|
+
if (ms === undefined)
|
|
135
|
+
return { hasExport: true, offset, problem: `invalid cache duration ${JSON.stringify(scalar.value)}: ${CACHE_HELP}` }
|
|
136
|
+
return { hasExport: true, offset, spec: buildRouteCacheSpec(ms) }
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const unwrapParam = (param: Node): Node => (param.type === 'AssignmentPattern' ? param.left : param)
|
|
140
|
+
|
|
141
|
+
function collectTransitiveDeps(graph: PrivateGraphView, rootFile: string): string[] {
|
|
142
|
+
const visited = new Set<string>([rootFile])
|
|
143
|
+
const queue = [...graph.localDeps(rootFile)]
|
|
144
|
+
const deps: string[] = []
|
|
145
|
+
for (let i = 0; i < queue.length; i++) {
|
|
146
|
+
const dep = queue[i]!
|
|
147
|
+
if (visited.has(dep)) continue
|
|
148
|
+
visited.add(dep)
|
|
149
|
+
deps.push(dep)
|
|
150
|
+
for (const next of graph.localDeps(dep))
|
|
151
|
+
if (!visited.has(next)) queue.push(next)
|
|
152
|
+
}
|
|
153
|
+
return deps
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/** Validates every route exporting `export const cache = ...` against private per-visitor reads in `load()`, the page, ancestor `_layout.tsx` files, and their transitive local dependencies. */
|
|
157
|
+
export function findCachedRoutePrivateReads(options: {
|
|
158
|
+
graph: PrivateGraphView
|
|
159
|
+
pageFiles: readonly string[]
|
|
160
|
+
layoutsFor: (pageFile: string) => readonly string[]
|
|
161
|
+
toRel: (file: string) => string
|
|
162
|
+
tool: 'prunelle check' | 'prunelle build'
|
|
163
|
+
}): PrivateReadProblem[] {
|
|
164
|
+
const { graph, pageFiles, layoutsFor, toRel, tool } = options
|
|
165
|
+
const problems: PrivateReadProblem[] = []
|
|
166
|
+
const pushHits = (file: string, hits: readonly PrivateReadHit[], msg: (what: string) => string): void => {
|
|
167
|
+
for (const hit of hits)
|
|
168
|
+
problems.push({ file, offset: hit.offset, message: msg(hit.what) })
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
for (const pageFile of pageFiles) {
|
|
172
|
+
const analysis = graph.analyses.get(pageFile)
|
|
173
|
+
if (analysis?.cache === undefined)
|
|
174
|
+
continue
|
|
175
|
+
const pageRel = toRel(pageFile)
|
|
176
|
+
const sharedTail = 'cached responses are shared across visitors, so per-visitor data would leak.'
|
|
177
|
+
pushHits(pageFile, findModulePrivateHits(analysis, true, true, pageFile, graph), w => `${tool}: cached route reads ${w}: ${sharedTail} Remove \`export const cache\` or stop reading ${w}.`)
|
|
178
|
+
for (const depFile of collectTransitiveDeps(graph, pageFile)) {
|
|
179
|
+
const dep = graph.analyses.get(depFile)
|
|
180
|
+
if (dep !== undefined)
|
|
181
|
+
pushHits(depFile, findModulePrivateHits(dep, false, false, depFile, graph), w => `${tool}: cached route "${pageRel}" imports module reading ${w}: ${sharedTail} Remove \`export const cache\` from ${pageRel} or stop reading ${w}.`)
|
|
182
|
+
}
|
|
183
|
+
const layoutMsg = (w: string) => `${tool}: cached route "${pageRel}" inherits layout reading ${w}: ${sharedTail} Remove \`export const cache\` from ${pageRel}, move it into a route group with a public layout, or stop reading ${w}.`
|
|
184
|
+
for (const layoutFile of layoutsFor(pageFile)) {
|
|
185
|
+
const layoutAnalysis = graph.analyses.get(layoutFile)
|
|
186
|
+
if (layoutAnalysis === undefined)
|
|
187
|
+
continue
|
|
188
|
+
pushHits(layoutFile, findModulePrivateHits(layoutAnalysis, true, true, layoutFile, graph), layoutMsg)
|
|
189
|
+
for (const depFile of collectTransitiveDeps(graph, layoutFile)) {
|
|
190
|
+
const dep = graph.analyses.get(depFile)
|
|
191
|
+
if (dep !== undefined)
|
|
192
|
+
pushHits(depFile, findModulePrivateHits(dep, false, false, depFile, graph), layoutMsg)
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
return problems
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
function globalObjName(node: Node): string | undefined {
|
|
200
|
+
const obj = unwrapTsExpr(node)
|
|
201
|
+
if (obj.type === 'Identifier')
|
|
202
|
+
return obj.name
|
|
203
|
+
if (obj.type === 'MemberExpression' && obj.object.type === 'Identifier' && obj.object.name === 'globalThis')
|
|
204
|
+
return memberPropName(obj)
|
|
205
|
+
return undefined
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
function isNonDeterministicOrSyncIo(node: Node): boolean {
|
|
209
|
+
if (node.type === 'NewExpression' && node.arguments.length === 0)
|
|
210
|
+
return globalObjName(node.callee) === 'Date'
|
|
211
|
+
if (node.type !== 'CallExpression')
|
|
212
|
+
return false
|
|
213
|
+
const callee = unwrapTsExpr(node.callee)
|
|
214
|
+
if (callee.type === 'Identifier')
|
|
215
|
+
return (callee.name === 'Date' && node.arguments.length === 0) || SYNC_IO_CALLEES.has(callee.name) || callee.name === 'randomUUID'
|
|
216
|
+
if (callee.type !== 'MemberExpression')
|
|
217
|
+
return false
|
|
218
|
+
const prop = memberPropName(callee)
|
|
219
|
+
if (prop === undefined)
|
|
220
|
+
return false
|
|
221
|
+
if (SYNC_IO_CALLEES.has(prop))
|
|
222
|
+
return true
|
|
223
|
+
const owner = globalObjName(callee.object)
|
|
224
|
+
if (owner === 'Bun')
|
|
225
|
+
return prop === 'nanoseconds' || prop === 'randomUUIDv7'
|
|
226
|
+
if (owner === 'Math')
|
|
227
|
+
return prop === 'random'
|
|
228
|
+
if (owner === 'crypto')
|
|
229
|
+
return prop === 'randomUUID' || prop === 'getRandomValues'
|
|
230
|
+
return prop === 'now' && (owner === 'Date' || owner === 'performance')
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
function defaultReadsDynamicProps(fn: OxcFunction | ArrowFunctionExpression): boolean {
|
|
234
|
+
if (fn.params.length === 0 || fn.body === null)
|
|
235
|
+
return false
|
|
236
|
+
const first = unwrapParam(fn.params[0]!)
|
|
237
|
+
if (first.type === 'ObjectPattern')
|
|
238
|
+
return first.properties.some(prop => prop.type === 'RestElement' || !propKeyName(prop) || DYNAMIC_PROPS.has(propKeyName(prop)!))
|
|
239
|
+
if (first.type !== 'Identifier')
|
|
240
|
+
return true
|
|
241
|
+
let readsDynamic = false
|
|
242
|
+
walkTrackedFunction({ fn, trackedProps: DYNAMIC_PROPS, prefix: '', checkHeaders: true, onHit: () => { readsDynamic = true } })
|
|
243
|
+
if (readsDynamic)
|
|
244
|
+
return true
|
|
245
|
+
walk(fn.body, (node) => {
|
|
246
|
+
if (readsDynamic)
|
|
247
|
+
return false
|
|
248
|
+
if ((node.type === 'JSXSpreadAttribute' || node.type === 'SpreadElement') && node.argument.type === 'Identifier' && node.argument.name === first.name) {
|
|
249
|
+
readsDynamic = true
|
|
250
|
+
return false
|
|
251
|
+
}
|
|
252
|
+
return undefined
|
|
253
|
+
})
|
|
254
|
+
return readsDynamic
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
function moduleReadsDynamicData(analysis: ModuleAnalysis, isRouteOrLayout: boolean): boolean {
|
|
258
|
+
if (analysis.imports.some(imp => DYNAMIC_IMPORTS.has(imp.source) || (imp.source === 'prunelle/server' && imp.specifiers.some(s => PRIVATE_SERVER_IMPORTS.has(s.imported)))))
|
|
259
|
+
return true
|
|
260
|
+
let dynamic = false
|
|
261
|
+
walk(analysis.program, (node) => {
|
|
262
|
+
if (dynamic)
|
|
263
|
+
return false
|
|
264
|
+
if (node.type === 'JSXAttribute' && node.name.type === 'JSXIdentifier' && EVENT_ATTR.test(node.name.name))
|
|
265
|
+
return false
|
|
266
|
+
if (node.type === 'CallExpression' && node.callee.type === 'Identifier' && CLIENT_CALLBACK_CALLEES.has(node.callee.name))
|
|
267
|
+
return false
|
|
268
|
+
if (node.type === 'JSXOpeningElement') {
|
|
269
|
+
const tag = resolvePrunelleTag(jsxTagName(node.name), analysis.imports)
|
|
270
|
+
if (tag !== undefined && DYNAMIC_TAGS.has(tag)) {
|
|
271
|
+
dynamic = true
|
|
272
|
+
return false
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
if (isNonDeterministicOrSyncIo(node) || (node.type === 'CallExpression' && checkHeaderCall(node) !== undefined)) {
|
|
276
|
+
dynamic = true
|
|
277
|
+
return false
|
|
278
|
+
}
|
|
279
|
+
return undefined
|
|
280
|
+
})
|
|
281
|
+
if (dynamic)
|
|
282
|
+
return true
|
|
283
|
+
if (isRouteOrLayout) {
|
|
284
|
+
const defaultFn = findExportedFunction(analysis.program, 'default')
|
|
285
|
+
if (defaultFn !== undefined && defaultReadsDynamicProps(defaultFn))
|
|
286
|
+
return true
|
|
287
|
+
}
|
|
288
|
+
return false
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
function isPureIslandFile(file: string, analysis: ModuleAnalysis, islands: IslandView): boolean {
|
|
292
|
+
if (analysis.isInteractive && analysis.staticJsxTags.size === 0)
|
|
293
|
+
return true
|
|
294
|
+
const exported = [...analysis.exports.keys()]
|
|
295
|
+
return exported.length > 0 && exported.every(name => islands.targets.has(`${file}#${name}`))
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* Statically checks whether a page route renders identically on every request:
|
|
300
|
+
* static URL, non-async default component, no `load()`, no `_middleware.ts`, not `prerender = false`,
|
|
301
|
+
* and no request/clock reads across the page, its `_layout.tsx` chain, or local non-island modules they import.
|
|
302
|
+
*/
|
|
303
|
+
export function isRouteCacheable(options: {
|
|
304
|
+
graph: PrivateGraphView
|
|
305
|
+
islands: IslandView
|
|
306
|
+
file: string
|
|
307
|
+
layoutSrcs: readonly string[]
|
|
308
|
+
middlewareCount: number
|
|
309
|
+
routePath: string
|
|
310
|
+
}): boolean {
|
|
311
|
+
const { graph, islands, file, layoutSrcs, middlewareCount, routePath } = options
|
|
312
|
+
if (middlewareCount > 0 || routePath.includes(':') || routePath.includes('*'))
|
|
313
|
+
return false
|
|
314
|
+
const analysis = graph.analyses.get(file)
|
|
315
|
+
if (analysis === undefined || !analysis.exportedNames.includes('default') || analysis.exportedNames.includes('GET'))
|
|
316
|
+
return false
|
|
317
|
+
if (analysis.hasLoadExport || analysis.hasAsyncDefault || analysis.prerenderFalse)
|
|
318
|
+
return false
|
|
319
|
+
|
|
320
|
+
const seen = new Set<string>()
|
|
321
|
+
const walkFile = (cur: string, isRouteOrLayout: boolean): boolean => {
|
|
322
|
+
if (seen.has(cur))
|
|
323
|
+
return true
|
|
324
|
+
seen.add(cur)
|
|
325
|
+
const mod = graph.analyses.get(cur)
|
|
326
|
+
if (mod === undefined)
|
|
327
|
+
return true
|
|
328
|
+
if (!isRouteOrLayout && isPureIslandFile(cur, mod, islands))
|
|
329
|
+
return true
|
|
330
|
+
if ((isRouteOrLayout && mod.hasAsyncDefault) || moduleReadsDynamicData(mod, isRouteOrLayout))
|
|
331
|
+
return false
|
|
332
|
+
return graph.localDeps(cur).every(dep => walkFile(dep, false))
|
|
333
|
+
}
|
|
334
|
+
return layoutSrcs.every(layout => walkFile(layout, true)) && walkFile(file, true)
|
|
335
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
// Static RGESN (eco-design) checks: media that plays on its own, images the browser cannot size, and heavy files in public/.
|
|
2
|
+
|
|
3
|
+
import type { JSXAttribute, JSXElement } from 'oxc-parser'
|
|
4
|
+
import type { CompilerWarning } from './mistakes'
|
|
5
|
+
import { statSync } from 'node:fs'
|
|
6
|
+
import { join } from 'node:path'
|
|
7
|
+
import { relPosix, walkFiles } from '../build/files'
|
|
8
|
+
import { findAttr, hasSpread, staticStringValueOf } from './compliance-rgaa'
|
|
9
|
+
import { formatDiagnostic, unwrapTsExpr } from './shared'
|
|
10
|
+
|
|
11
|
+
export const ECO_JSX_RULES = ['rgesn/autoplay', 'rgesn/img-size', 'rgesn/img-sizes'] as const
|
|
12
|
+
|
|
13
|
+
export type EcoJsxRule = (typeof ECO_JSX_RULES)[number]
|
|
14
|
+
|
|
15
|
+
export interface EcoElementContext {
|
|
16
|
+
filename: string
|
|
17
|
+
isIgnored: (rule: EcoJsxRule, elementStart: number, attrStart?: number) => boolean
|
|
18
|
+
warnings: CompilerWarning[]
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// Past this size a file in public/ is worth a second look: a photo straight from a camera, a video, an uncompressed export.
|
|
22
|
+
export const LARGE_PUBLIC_FILE_BYTES = 1_000_000
|
|
23
|
+
const WIDTH_DESCRIPTOR_RE = /\s\d+w\s*(?:,|$)/
|
|
24
|
+
|
|
25
|
+
// `autoplay`, `autoplay={true}` and `autoplay="..."` turn it on; `autoplay={false}` and a dynamic value are left to the app.
|
|
26
|
+
function isOn(attr: JSXAttribute): boolean {
|
|
27
|
+
if (attr.value === null || attr.value.type === 'Literal')
|
|
28
|
+
return true
|
|
29
|
+
if (attr.value.type !== 'JSXExpressionContainer' || attr.value.expression.type === 'JSXEmptyExpression')
|
|
30
|
+
return false
|
|
31
|
+
const expr = unwrapTsExpr(attr.value.expression)
|
|
32
|
+
return expr.type === 'Literal' && expr.value === true
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function emit(ctx: EcoElementContext, rule: EcoJsxRule, start: number, message: string, attrStart?: number): void {
|
|
36
|
+
if (!ctx.isIgnored(rule, start, attrStart))
|
|
37
|
+
ctx.warnings.push({ rule, start: attrStart ?? start, message })
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function checkEcoElement(element: JSXElement, tag: string, ctx: EcoElementContext): void {
|
|
41
|
+
const attrs = element.openingElement.attributes
|
|
42
|
+
if (tag === 'video' || tag === 'audio') {
|
|
43
|
+
const autoplay = findAttr(attrs, 'autoplay') ?? findAttr(attrs, 'autoPlay')
|
|
44
|
+
if (autoplay !== undefined && isOn(autoplay))
|
|
45
|
+
emit(ctx, 'rgesn/autoplay', element.start, `[rgesn/autoplay] <${tag} autoplay> in ${ctx.filename} downloads and plays its media before anyone asks for it: drop autoplay, let the visitor press play, and set preload="none" (or "metadata").`, autoplay.start)
|
|
46
|
+
return
|
|
47
|
+
}
|
|
48
|
+
if (tag !== 'img' && tag !== 'source')
|
|
49
|
+
return
|
|
50
|
+
const srcset = findAttr(attrs, 'srcset') ?? findAttr(attrs, 'srcSet')
|
|
51
|
+
const candidates = srcset === undefined ? undefined : staticStringValueOf(srcset.value)
|
|
52
|
+
if (srcset !== undefined && candidates !== undefined && WIDTH_DESCRIPTOR_RE.test(candidates) && !hasSpread(attrs) && findAttr(attrs, 'sizes') === undefined)
|
|
53
|
+
emit(ctx, 'rgesn/img-sizes', element.start, `[rgesn/img-sizes] <${tag} srcset> in ${ctx.filename} lists widths but no sizes, so the browser assumes the image fills the viewport and fetches the widest file: add sizes="..." with the width it is shown at (e.g. sizes="(min-width: 60rem) 40rem, 100vw").`, srcset.start)
|
|
54
|
+
if (tag === 'img' && !hasSpread(attrs) && (findAttr(attrs, 'width') === undefined || findAttr(attrs, 'height') === undefined))
|
|
55
|
+
emit(ctx, 'rgesn/img-size', element.start, `[rgesn/img-size] <img> in ${ctx.filename} has no width and height, so the browser cannot reserve its box and the page shifts as it loads: give its intrinsic width and height, or use <Img> from @prunellejs/images, which sets them.`)
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Warns on each file in `publicDir` past 1 MB, which every visitor showing it downloads as is. */
|
|
59
|
+
export function largePublicFileWarnings(root: string, isIgnored: boolean, publicDir = join(root, 'public')): string[] {
|
|
60
|
+
if (isIgnored)
|
|
61
|
+
return []
|
|
62
|
+
const warnings: string[] = []
|
|
63
|
+
for (const file of walkFiles(publicDir, { keep: entry => entry.isFile() }).sort()) {
|
|
64
|
+
const size = statSync(file).size
|
|
65
|
+
if (size > LARGE_PUBLIC_FILE_BYTES) {
|
|
66
|
+
const rel = relPosix(root, file)
|
|
67
|
+
warnings.push(formatDiagnostic(`${rel}:1:1`, `[rgesn/large-public-file] ${rel} weighs ${(size / 1_000_000).toFixed(1)} MB and is served as is: compress it, resize an image with @prunellejs/images, or host a video on a streaming service behind <ConsentGate>.`, 'warning'))
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return warnings
|
|
71
|
+
}
|