prunelle 0.1.1 → 0.5.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/llms.txt +23 -19
- package/package.json +8 -2
- package/src/a11y/declaration.ts +335 -0
- package/src/a11y/index.ts +446 -0
- package/src/a11y/rgaa-map.ts +184 -0
- package/src/attrs.ts +11 -3
- package/src/build/bundle.ts +80 -36
- package/src/build/client-entries.ts +3 -2
- package/src/build/client-plugin.ts +8 -4
- package/src/build/client-prune.ts +56 -6
- package/src/build/compile.ts +11 -9
- package/src/build/ecoindex.ts +229 -0
- package/src/build/index.ts +174 -108
- package/src/build/islands.ts +2 -2
- package/src/build/manifest.ts +66 -5
- package/src/build/output.ts +2 -2
- package/src/build/plugin-host.ts +91 -33
- package/src/build/precompress.ts +15 -2
- package/src/build/prerender.ts +128 -46
- package/src/build/report.ts +23 -2
- package/src/build/resolve.ts +8 -6
- package/src/build/routes.ts +175 -10
- package/src/build/static.ts +137 -39
- package/src/build/status-pages.ts +127 -0
- package/src/build/style-bundles.ts +526 -0
- package/src/cli/check-forms.ts +561 -0
- package/src/cli/check-links.ts +632 -0
- package/src/cli/check.ts +75 -20
- package/src/cli/commands.ts +85 -4
- package/src/cli/docs.ts +402 -0
- package/src/cli/inspect.ts +103 -14
- package/src/cli/mcp-tools.ts +490 -0
- package/src/cli/mcp.ts +205 -0
- package/src/cli/new.ts +2 -2
- package/src/cli/render.ts +421 -0
- package/src/cli/upgrade-names.ts +73 -13
- package/src/cli/upgrade.ts +137 -16
- package/src/compiler/analyze.ts +42 -9
- package/src/compiler/browser-globals.ts +140 -17
- package/src/compiler/cacheability.ts +636 -0
- package/src/compiler/compliance-rgaa.ts +508 -0
- package/src/compiler/compliance.ts +241 -289
- 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 +167 -57
- package/src/compiler/mistakes.ts +4 -1
- 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 +40 -12
- package/src/compiler/signal-reads.ts +6 -2
- package/src/compiler/ssr-prunelle-tags.ts +27 -14
- package/src/compiler/ssr.ts +218 -87
- package/src/dev/error-log.ts +151 -0
- package/src/dev/index.ts +84 -34
- package/src/dev/open-editor.ts +204 -0
- package/src/dev/protocol.ts +1 -0
- package/src/dev/reload.ts +5 -3
- package/src/dev/supervisor.ts +42 -25
- package/src/dev/watchers.ts +47 -11
- package/src/dom/action.ts +43 -20
- package/src/dom/attrs.ts +15 -3
- package/src/dom/extras.ts +28 -12
- package/src/dom/index.ts +26 -8
- package/src/dom/island.ts +35 -28
- package/src/dom/loop.ts +10 -9
- package/src/plugins/config.ts +8 -7
- package/src/plugins/index.ts +2 -2
- package/src/plugins/load.ts +26 -7
- package/src/plugins/order.ts +38 -1
- package/src/plugins/public.ts +5 -2
- package/src/plugins/services.ts +62 -0
- package/src/plugins/types.ts +32 -0
- 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 +45 -13
- package/src/server/action-protect.ts +198 -0
- package/src/server/action-registry.ts +1 -1
- package/src/server/action.ts +82 -27
- package/src/server/assets.ts +34 -2
- package/src/server/compress.ts +91 -9
- package/src/server/document.ts +25 -10
- package/src/server/errors.ts +303 -65
- package/src/server/form-schema.ts +343 -0
- package/src/server/health.ts +244 -0
- package/src/server/http.ts +128 -31
- package/src/server/index.ts +230 -260
- package/src/server/loader.ts +5 -0
- package/src/server/param-matchers.ts +112 -0
- package/src/server/plugin-host.ts +123 -88
- package/src/server/public.ts +3 -3
- package/src/server/rate-limit.ts +58 -35
- package/src/server/request-body.ts +1 -1
- package/src/server/route-cache.ts +423 -0
- package/src/server/route-table.ts +339 -8
- package/src/server/router.ts +125 -40
- package/src/server/routing.ts +196 -63
- package/src/server/security.ts +208 -32
- package/src/server/serve-handlers.ts +21 -7
- package/src/server/start.ts +60 -16
- package/src/server/static-dir.ts +61 -8
- package/src/server/stream.ts +41 -22
- package/src/ssr/context.ts +85 -3
- package/src/ssr/index.ts +130 -3
- package/src/ssr/region.ts +31 -19
- 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/mcp.json +11 -0
- package/templates/_shared/src/routes/legal.tsx +5 -0
- package/templates/_shared/tsconfig.json +5 -0
- package/templates/blog/prunelle.config.ts +1 -1
- package/templates/blog/src/routes/index.tsx +4 -0
- package/ts-plugin.cjs +4277 -0
|
@@ -0,0 +1,636 @@
|
|
|
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 { visitorKeys } from 'oxc-parser'
|
|
7
|
+
import { buildRouteCacheSpec, parseCacheDuration } from '../server/route-cache'
|
|
8
|
+
import { walk } from './ast'
|
|
9
|
+
import { collectPatternNames, EVENT_ATTR, jsxTagName, resolvePrunelleTag, staticLiteral, unwrapTsExpr } from './shared'
|
|
10
|
+
|
|
11
|
+
const CACHE_HELP = 'write a duration such as \'30s\', \'5m\', \'1h\', \'1d\', a positive number of seconds, or { maxAge: \'1h\', staleWhileRevalidate?: \'1d\' }'
|
|
12
|
+
const DYNAMIC_PROPS = new Set(['params', 'url', 'locals', 'cookies', 'headers', 'request', 'consent', 'ip'])
|
|
13
|
+
const PRIVATE_CTX_PROPS = new Set(['cookies', 'locals', 'consent', 'ip'])
|
|
14
|
+
const PRIVATE_PAGE_PROPS = new Set(['locals', 'cookies', 'consent', 'ip'])
|
|
15
|
+
const PRIVATE_SERVER_IMPORTS = new Set(['getActionError', 'currentRequest'])
|
|
16
|
+
const PRIVATE_HEADERS = new Set(['cookie', 'authorization'])
|
|
17
|
+
const DYNAMIC_IMPORTS = new Set(['bun:sqlite', 'node:sqlite', 'sqlite', 'bun:sql'])
|
|
18
|
+
const SYNC_IO_CALLEES = new Set(['readFileSync', 'readdirSync', 'statSync', 'lstatSync', 'existsSync', 'accessSync'])
|
|
19
|
+
const DYNAMIC_TAGS = new Set(['ConsentGate', 'Suspense'])
|
|
20
|
+
const PRIVATE_TAGS = new Set(['ConsentGate'])
|
|
21
|
+
const CLIENT_CALLBACK_CALLEES = new Set(['onMount', 'effect', 'onCleanup'])
|
|
22
|
+
const TS_EXPR_WRAPPERS = new Set(['TSAsExpression', 'TSNonNullExpression', 'TSSatisfiesExpression', 'TSTypeAssertion', 'TSInstantiationExpression'])
|
|
23
|
+
|
|
24
|
+
export interface CacheExportResult {
|
|
25
|
+
hasExport: boolean
|
|
26
|
+
spec?: RouteCacheSpec | undefined
|
|
27
|
+
problem?: string | undefined
|
|
28
|
+
offset: number
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface PrivateReadProblem {
|
|
32
|
+
file: string
|
|
33
|
+
offset: number
|
|
34
|
+
message: string
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
interface PrivateReadHit {
|
|
38
|
+
what: string
|
|
39
|
+
offset: number
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
interface GraphView {
|
|
43
|
+
readonly analyses: ReadonlyMap<string, ModuleAnalysis>
|
|
44
|
+
localDeps(file: string): readonly string[]
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
interface IslandView {
|
|
48
|
+
readonly targets: ReadonlyMap<string, unknown>
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function staticStringOrNumber(node: Node): { ok: true, value: string | number } | { ok: false, isLiteral: boolean } {
|
|
52
|
+
const inner = unwrapTsExpr(node)
|
|
53
|
+
if (inner.type === 'TemplateLiteral' && inner.expressions.length === 0 && inner.quasis.length === 1)
|
|
54
|
+
return { ok: true, value: inner.quasis[0]!.value.cooked ?? inner.quasis[0]!.value.raw }
|
|
55
|
+
const lit = staticLiteral(inner)
|
|
56
|
+
if (!lit.ok)
|
|
57
|
+
return { ok: false, isLiteral: false }
|
|
58
|
+
return typeof lit.value === 'string' || typeof lit.value === 'number'
|
|
59
|
+
? { ok: true, value: lit.value }
|
|
60
|
+
: { ok: false, isLiteral: true }
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function propKeyName(prop: Node): string | undefined {
|
|
64
|
+
if (prop.type !== 'Property')
|
|
65
|
+
return undefined
|
|
66
|
+
if (!prop.computed && prop.key.type === 'Identifier')
|
|
67
|
+
return prop.key.name
|
|
68
|
+
return prop.key.type === 'Literal' && typeof prop.key.value === 'string' ? prop.key.value : undefined
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function memberPropName(node: Extract<Node, { type: 'MemberExpression' }>): string | undefined {
|
|
72
|
+
if (!node.computed && node.property.type === 'Identifier')
|
|
73
|
+
return node.property.name
|
|
74
|
+
return node.computed && node.property.type === 'Literal' && typeof node.property.value === 'string' ? node.property.value : undefined
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function parseCacheObjectNode(node: Extract<Node, { type: 'ObjectExpression' }>): { spec: RouteCacheSpec } | { problem: string } {
|
|
78
|
+
let maxAgeMs: number | undefined
|
|
79
|
+
let swrMs: number | undefined
|
|
80
|
+
let swrKeyCount = 0
|
|
81
|
+
for (const prop of node.properties) {
|
|
82
|
+
if (prop.type !== 'Property' || prop.kind !== 'init')
|
|
83
|
+
return { problem: `cache object must use static properties: ${CACHE_HELP}` }
|
|
84
|
+
const key = propKeyName(prop)
|
|
85
|
+
if (key === undefined)
|
|
86
|
+
return { problem: `cache object has a dynamic key: ${CACHE_HELP}` }
|
|
87
|
+
if (key !== 'maxAge' && key !== 'staleWhileRevalidate' && key !== 'swr')
|
|
88
|
+
return { problem: `unknown cache option "${key}": ${CACHE_HELP}` }
|
|
89
|
+
const parsed = staticStringOrNumber(prop.value)
|
|
90
|
+
if (!parsed.ok)
|
|
91
|
+
return { problem: parsed.isLiteral ? `cache.${key} must be a duration string or number of seconds: ${CACHE_HELP}` : `cache.${key} set at runtime: ${CACHE_HELP}` }
|
|
92
|
+
const ms = parseCacheDuration(parsed.value, key !== 'maxAge')
|
|
93
|
+
if (ms === undefined)
|
|
94
|
+
return { problem: `invalid cache.${key} value ${JSON.stringify(parsed.value)}: ${CACHE_HELP}` }
|
|
95
|
+
if (key === 'maxAge') {
|
|
96
|
+
maxAgeMs = ms
|
|
97
|
+
}
|
|
98
|
+
else {
|
|
99
|
+
swrKeyCount++
|
|
100
|
+
swrMs = ms
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
if (maxAgeMs === undefined)
|
|
104
|
+
return { problem: `cache object is missing "maxAge": ${CACHE_HELP}` }
|
|
105
|
+
if (swrKeyCount > 1)
|
|
106
|
+
return { problem: `specify either "staleWhileRevalidate" or "swr", not both: ${CACHE_HELP}` }
|
|
107
|
+
return { spec: buildRouteCacheSpec(maxAgeMs, swrMs ?? maxAgeMs) }
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** Parses and validates `export const cache = ...` (or `const cache = ...; export { cache }`) from a module AST. */
|
|
111
|
+
export function parseCacheExport(program: Program): CacheExportResult {
|
|
112
|
+
const localInits = new Map<string, Node>()
|
|
113
|
+
let exportedLocal: string | undefined
|
|
114
|
+
let exportInit: Node | undefined
|
|
115
|
+
let offset = 0
|
|
116
|
+
let hasExport = false
|
|
117
|
+
|
|
118
|
+
for (const stmt of program.body) {
|
|
119
|
+
if (stmt.type === 'VariableDeclaration') {
|
|
120
|
+
for (const d of stmt.declarations) {
|
|
121
|
+
if (d.id.type === 'Identifier' && d.init !== null)
|
|
122
|
+
localInits.set(d.id.name, d.init)
|
|
123
|
+
}
|
|
124
|
+
continue
|
|
125
|
+
}
|
|
126
|
+
if (stmt.type !== 'ExportNamedDeclaration' || stmt.exportKind === 'type')
|
|
127
|
+
continue
|
|
128
|
+
if (stmt.declaration?.type === 'VariableDeclaration') {
|
|
129
|
+
for (const d of stmt.declaration.declarations) {
|
|
130
|
+
if (d.id.type === 'Identifier' && d.id.name === 'cache') {
|
|
131
|
+
hasExport = true
|
|
132
|
+
offset = d.start
|
|
133
|
+
exportInit = d.init ?? undefined
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
else if (stmt.declaration !== null && 'id' in stmt.declaration && stmt.declaration.id?.type === 'Identifier' && stmt.declaration.id.name === 'cache') {
|
|
138
|
+
return { hasExport: true, offset: stmt.declaration.start, problem: `invalid cache export: ${CACHE_HELP}` }
|
|
139
|
+
}
|
|
140
|
+
for (const spec of stmt.specifiers) {
|
|
141
|
+
const exp = spec.exported.type === 'Identifier' ? spec.exported.name : spec.exported.value
|
|
142
|
+
if (exp === 'cache' && spec.exportKind !== 'type') {
|
|
143
|
+
hasExport = true
|
|
144
|
+
offset = spec.start
|
|
145
|
+
if (stmt.source === null && spec.local.type === 'Identifier')
|
|
146
|
+
exportedLocal = spec.local.name
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
if (!hasExport)
|
|
152
|
+
return { hasExport: false, offset: 0 }
|
|
153
|
+
const init = exportInit ?? (exportedLocal !== undefined ? localInits.get(exportedLocal) : undefined)
|
|
154
|
+
if (init === undefined)
|
|
155
|
+
return { hasExport: true, offset, problem: `cache set at runtime: ${CACHE_HELP}` }
|
|
156
|
+
|
|
157
|
+
const inner = unwrapTsExpr(init)
|
|
158
|
+
if (inner.type === 'ObjectExpression') {
|
|
159
|
+
const res = parseCacheObjectNode(inner)
|
|
160
|
+
return 'spec' in res ? { hasExport: true, offset, spec: res.spec } : { hasExport: true, offset, problem: res.problem }
|
|
161
|
+
}
|
|
162
|
+
const scalar = staticStringOrNumber(inner)
|
|
163
|
+
if (!scalar.ok)
|
|
164
|
+
return { hasExport: true, offset, problem: scalar.isLiteral ? `invalid cache export: ${CACHE_HELP}` : `cache set at runtime: ${CACHE_HELP}` }
|
|
165
|
+
const ms = parseCacheDuration(scalar.value)
|
|
166
|
+
if (ms === undefined)
|
|
167
|
+
return { hasExport: true, offset, problem: `invalid cache duration ${JSON.stringify(scalar.value)}: ${CACHE_HELP}` }
|
|
168
|
+
return { hasExport: true, offset, spec: buildRouteCacheSpec(ms) }
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function collectTopLevelFns(program: Program): { fns: Map<string, OxcFunction>, inits: Map<string, Node> } {
|
|
172
|
+
const fns = new Map<string, OxcFunction>()
|
|
173
|
+
const inits = new Map<string, Node>()
|
|
174
|
+
const add = (node: Node): void => {
|
|
175
|
+
if (node.type === 'FunctionDeclaration' && node.id !== null)
|
|
176
|
+
fns.set(node.id.name, node)
|
|
177
|
+
else if (node.type === 'VariableDeclaration')
|
|
178
|
+
for (const d of node.declarations) if (d.id.type === 'Identifier' && d.init !== null) inits.set(d.id.name, d.init)
|
|
179
|
+
}
|
|
180
|
+
for (const stmt of program.body)
|
|
181
|
+
add(stmt.type === 'ExportNamedDeclaration' && stmt.declaration !== null ? stmt.declaration : stmt)
|
|
182
|
+
return { fns, inits }
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function unwrapFn(node: Node | undefined, fns: ReadonlyMap<string, OxcFunction>, inits: ReadonlyMap<string, Node>, depth = 0): OxcFunction | ArrowFunctionExpression | undefined {
|
|
186
|
+
if (node === undefined || depth > 4)
|
|
187
|
+
return undefined
|
|
188
|
+
const inner = unwrapTsExpr(node)
|
|
189
|
+
if (inner.type === 'FunctionDeclaration' || inner.type === 'FunctionExpression' || inner.type === 'ArrowFunctionExpression')
|
|
190
|
+
return inner
|
|
191
|
+
return inner.type === 'Identifier' ? fns.get(inner.name) ?? unwrapFn(inits.get(inner.name), fns, inits, depth + 1) : undefined
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
function findExportedFunction(program: Program, exportName: 'default' | 'load'): OxcFunction | ArrowFunctionExpression | undefined {
|
|
195
|
+
const { fns, inits } = collectTopLevelFns(program)
|
|
196
|
+
for (const stmt of program.body) {
|
|
197
|
+
if (exportName === 'default' && stmt.type === 'ExportDefaultDeclaration')
|
|
198
|
+
return unwrapFn(stmt.declaration, fns, inits)
|
|
199
|
+
if (stmt.type !== 'ExportNamedDeclaration' || stmt.exportKind === 'type')
|
|
200
|
+
continue
|
|
201
|
+
if (stmt.declaration?.type === 'FunctionDeclaration' && stmt.declaration.id?.name === exportName)
|
|
202
|
+
return stmt.declaration
|
|
203
|
+
if (stmt.declaration?.type === 'VariableDeclaration') {
|
|
204
|
+
for (const d of stmt.declaration.declarations) {
|
|
205
|
+
if (d.id.type === 'Identifier' && d.id.name === exportName)
|
|
206
|
+
return unwrapFn(d.init ?? undefined, fns, inits)
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
for (const spec of stmt.specifiers) {
|
|
210
|
+
const exp = spec.exported.type === 'Identifier' ? spec.exported.name : spec.exported.value
|
|
211
|
+
if (exp === exportName && stmt.source === null && spec.local.type === 'Identifier')
|
|
212
|
+
return fns.get(spec.local.name) ?? unwrapFn(inits.get(spec.local.name), fns, inits)
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
return undefined
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
const unwrapParam = (param: Node): Node => (param.type === 'AssignmentPattern' ? param.left : param)
|
|
219
|
+
|
|
220
|
+
function isPrivateHeaderCall(node: Extract<Node, { type: 'CallExpression' }>, headersAliases?: ReadonlySet<string>): string | undefined {
|
|
221
|
+
const callee = unwrapTsExpr(node.callee)
|
|
222
|
+
if (callee.type !== 'MemberExpression')
|
|
223
|
+
return undefined
|
|
224
|
+
const method = memberPropName(callee)
|
|
225
|
+
const firstArg = node.arguments[0]
|
|
226
|
+
if ((method !== 'get' && method !== 'has') || firstArg === undefined || firstArg.type === 'SpreadElement')
|
|
227
|
+
return undefined
|
|
228
|
+
const arg = staticStringOrNumber(firstArg)
|
|
229
|
+
const header = arg.ok && typeof arg.value === 'string' ? arg.value.toLowerCase() : undefined
|
|
230
|
+
if (header === undefined || !PRIVATE_HEADERS.has(header))
|
|
231
|
+
return undefined
|
|
232
|
+
const obj = unwrapTsExpr(callee.object)
|
|
233
|
+
const isHeadersObj = (obj.type === 'MemberExpression' && memberPropName(obj) === 'headers')
|
|
234
|
+
|| (obj.type === 'Identifier' && (obj.name === 'headers' || headersAliases?.has(obj.name) === true))
|
|
235
|
+
if (!isHeadersObj)
|
|
236
|
+
return undefined
|
|
237
|
+
const owner = obj.type === 'MemberExpression' && obj.object.type === 'Identifier' && obj.object.name === 'ctx' ? 'ctx' : 'request'
|
|
238
|
+
return `${owner}.headers.${method}('${header}')`
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
interface TrackedContext {
|
|
242
|
+
paramName: string | undefined
|
|
243
|
+
trackedProps: ReadonlySet<string>
|
|
244
|
+
prefix: string
|
|
245
|
+
checkHeaders: boolean
|
|
246
|
+
requestAliases: Set<string>
|
|
247
|
+
headersAliases: Set<string>
|
|
248
|
+
onHit: (hit: PrivateReadHit) => void
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
function collectHeaderAlias(pattern: Extract<Node, { type: 'ObjectPattern' }>, headersAliases: Set<string>): void {
|
|
252
|
+
for (const prop of pattern.properties) {
|
|
253
|
+
if (prop.type === 'Property' && propKeyName(prop) === 'headers') {
|
|
254
|
+
const hv = unwrapParam(prop.value)
|
|
255
|
+
if (hv.type === 'Identifier')
|
|
256
|
+
headersAliases.add(hv.name)
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
function scanObjectPattern(pattern: Extract<Node, { type: 'ObjectPattern' }>, ctx: TrackedContext, prefix: string): void {
|
|
262
|
+
for (const prop of pattern.properties) {
|
|
263
|
+
if (prop.type !== 'Property')
|
|
264
|
+
continue
|
|
265
|
+
const key = propKeyName(prop)
|
|
266
|
+
if (key !== undefined && ctx.trackedProps.has(key))
|
|
267
|
+
ctx.onHit({ what: `${prefix}${key}`, offset: prop.start })
|
|
268
|
+
if (!ctx.checkHeaders || key !== 'request')
|
|
269
|
+
continue
|
|
270
|
+
const val = unwrapParam(prop.value)
|
|
271
|
+
if (val.type === 'Identifier')
|
|
272
|
+
ctx.requestAliases.add(val.name)
|
|
273
|
+
else if (val.type === 'ObjectPattern')
|
|
274
|
+
collectHeaderAlias(val, ctx.headersAliases)
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
function trackDeclarator(ast: Extract<Node, { type: 'VariableDeclarator' }>, ctx: TrackedContext, paramActive: boolean): void {
|
|
279
|
+
if (ast.init === null)
|
|
280
|
+
return
|
|
281
|
+
const init = unwrapTsExpr(ast.init)
|
|
282
|
+
if (paramActive && init.type === 'Identifier' && init.name === ctx.paramName && ast.id.type === 'ObjectPattern')
|
|
283
|
+
scanObjectPattern(ast.id, ctx, ctx.prefix)
|
|
284
|
+
if (!ctx.checkHeaders)
|
|
285
|
+
return
|
|
286
|
+
if (init.type === 'MemberExpression' && ast.id.type === 'Identifier') {
|
|
287
|
+
const key = memberPropName(init)
|
|
288
|
+
const obj = unwrapTsExpr(init.object)
|
|
289
|
+
if (key === 'request' && paramActive && obj.type === 'Identifier' && obj.name === ctx.paramName)
|
|
290
|
+
ctx.requestAliases.add(ast.id.name)
|
|
291
|
+
if (key === 'headers')
|
|
292
|
+
ctx.headersAliases.add(ast.id.name)
|
|
293
|
+
}
|
|
294
|
+
else if (init.type === 'Identifier' && ctx.requestAliases.has(init.name) && ast.id.type === 'ObjectPattern') {
|
|
295
|
+
collectHeaderAlias(ast.id, ctx.headersAliases)
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
function blockScopeNames(block: Extract<Node, { type: 'BlockStatement' }>): Set<string> {
|
|
300
|
+
const scope = new Set<string>()
|
|
301
|
+
for (const stmt of block.body) {
|
|
302
|
+
if (stmt.type === 'VariableDeclaration')
|
|
303
|
+
for (const d of stmt.declarations) collectPatternNames(d.id, scope)
|
|
304
|
+
else if (stmt.type === 'FunctionDeclaration' && stmt.id !== null)
|
|
305
|
+
scope.add(stmt.id.name)
|
|
306
|
+
}
|
|
307
|
+
return scope
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
function walkTrackedFunction(
|
|
311
|
+
fn: OxcFunction | ArrowFunctionExpression,
|
|
312
|
+
trackedProps: ReadonlySet<string>,
|
|
313
|
+
prefix: string,
|
|
314
|
+
checkHeaders: boolean,
|
|
315
|
+
onHit: (hit: PrivateReadHit) => void,
|
|
316
|
+
): void {
|
|
317
|
+
if (fn.body === null || fn.params.length === 0)
|
|
318
|
+
return
|
|
319
|
+
const first = unwrapParam(fn.params[0]!)
|
|
320
|
+
const ctx: TrackedContext = {
|
|
321
|
+
paramName: first.type === 'Identifier' ? first.name : undefined,
|
|
322
|
+
trackedProps,
|
|
323
|
+
prefix,
|
|
324
|
+
checkHeaders,
|
|
325
|
+
requestAliases: new Set(),
|
|
326
|
+
headersAliases: new Set(),
|
|
327
|
+
onHit,
|
|
328
|
+
}
|
|
329
|
+
if (first.type === 'ObjectPattern')
|
|
330
|
+
scanObjectPattern(first, ctx, '')
|
|
331
|
+
|
|
332
|
+
const scopes: Set<string>[] = [new Set()]
|
|
333
|
+
const isParamActive = (): boolean => ctx.paramName !== undefined && !scopes.some(s => s.has(ctx.paramName!))
|
|
334
|
+
|
|
335
|
+
const visit = (node: unknown): void => {
|
|
336
|
+
if (node === null || typeof node !== 'object')
|
|
337
|
+
return
|
|
338
|
+
if (Array.isArray(node)) {
|
|
339
|
+
for (let i = 0; i < node.length; i++) visit(node[i])
|
|
340
|
+
return
|
|
341
|
+
}
|
|
342
|
+
const ast = node as Node
|
|
343
|
+
if (typeof ast.type !== 'string')
|
|
344
|
+
return
|
|
345
|
+
if (ast.type.startsWith('TS')) {
|
|
346
|
+
if (TS_EXPR_WRAPPERS.has(ast.type)) visit((ast as { expression?: unknown }).expression)
|
|
347
|
+
return
|
|
348
|
+
}
|
|
349
|
+
if (ast !== fn && (ast.type === 'FunctionDeclaration' || ast.type === 'FunctionExpression' || ast.type === 'ArrowFunctionExpression')) {
|
|
350
|
+
const inner = ast as OxcFunction | ArrowFunctionExpression
|
|
351
|
+
const scope = new Set<string>()
|
|
352
|
+
if (inner.type !== 'ArrowFunctionExpression' && inner.id !== null) scope.add(inner.id.name)
|
|
353
|
+
for (const p of inner.params) collectPatternNames(p, scope)
|
|
354
|
+
scopes.push(scope)
|
|
355
|
+
if (inner.body !== null) visit(inner.body)
|
|
356
|
+
scopes.pop()
|
|
357
|
+
return
|
|
358
|
+
}
|
|
359
|
+
if (ast.type === 'BlockStatement' && ast !== fn.body) {
|
|
360
|
+
scopes.push(blockScopeNames(ast))
|
|
361
|
+
for (const stmt of ast.body) visit(stmt)
|
|
362
|
+
scopes.pop()
|
|
363
|
+
return
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
const active = isParamActive()
|
|
367
|
+
if (ast.type === 'VariableDeclarator') {
|
|
368
|
+
trackDeclarator(ast, ctx, active)
|
|
369
|
+
}
|
|
370
|
+
else if (ast.type === 'MemberExpression' && active) {
|
|
371
|
+
const obj = unwrapTsExpr(ast.object)
|
|
372
|
+
const key = obj.type === 'Identifier' && obj.name === ctx.paramName ? memberPropName(ast) : undefined
|
|
373
|
+
if (key !== undefined && ctx.trackedProps.has(key))
|
|
374
|
+
ctx.onHit({ what: `${ctx.prefix}${key}`, offset: ast.start })
|
|
375
|
+
}
|
|
376
|
+
else if (ast.type === 'CallExpression' && ctx.checkHeaders) {
|
|
377
|
+
const headerCall = isPrivateHeaderCall(ast, ctx.headersAliases)
|
|
378
|
+
if (headerCall !== undefined)
|
|
379
|
+
ctx.onHit({ what: headerCall, offset: ast.start })
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
const keys = visitorKeys[ast.type]
|
|
383
|
+
if (keys === undefined)
|
|
384
|
+
return
|
|
385
|
+
const rec = ast as unknown as Record<string, unknown>
|
|
386
|
+
for (let i = 0; i < keys.length; i++) {
|
|
387
|
+
const val = rec[keys[i]!]
|
|
388
|
+
if (val !== null && typeof val === 'object') visit(val)
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
visit(fn.body)
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
function findModulePrivateHits(analysis: ModuleAnalysis, includeLoad: boolean, isPageOrLayout = true): PrivateReadHit[] {
|
|
396
|
+
const hits: PrivateReadHit[] = []
|
|
397
|
+
const seenOffsets = new Set<number>()
|
|
398
|
+
const push = (hit: PrivateReadHit): void => {
|
|
399
|
+
if (!seenOffsets.has(hit.offset)) {
|
|
400
|
+
seenOffsets.add(hit.offset)
|
|
401
|
+
hits.push(hit)
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
for (const imp of analysis.imports) {
|
|
405
|
+
if (imp.source === 'prunelle/server') {
|
|
406
|
+
for (const spec of imp.specifiers) {
|
|
407
|
+
if (PRIVATE_SERVER_IMPORTS.has(spec.imported))
|
|
408
|
+
push({ what: `${spec.imported}()`, offset: imp.start })
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
walk(analysis.program, (node) => {
|
|
413
|
+
if (!isPageOrLayout && node.type === 'JSXAttribute' && node.name.type === 'JSXIdentifier' && EVENT_ATTR.test(node.name.name))
|
|
414
|
+
return false
|
|
415
|
+
if (!isPageOrLayout && node.type === 'CallExpression' && node.callee.type === 'Identifier' && CLIENT_CALLBACK_CALLEES.has(node.callee.name))
|
|
416
|
+
return false
|
|
417
|
+
if (node.type === 'JSXOpeningElement') {
|
|
418
|
+
const tag = resolvePrunelleTag(jsxTagName(node.name), analysis.imports)
|
|
419
|
+
if (tag !== undefined && PRIVATE_TAGS.has(tag))
|
|
420
|
+
push({ what: `<${tag}>`, offset: node.start })
|
|
421
|
+
}
|
|
422
|
+
else if (!isPageOrLayout && node.type === 'CallExpression') {
|
|
423
|
+
const headerCall = isPrivateHeaderCall(node)
|
|
424
|
+
if (headerCall !== undefined)
|
|
425
|
+
push({ what: headerCall, offset: node.start })
|
|
426
|
+
}
|
|
427
|
+
return undefined
|
|
428
|
+
})
|
|
429
|
+
if (includeLoad) {
|
|
430
|
+
const loadFn = findExportedFunction(analysis.program, 'load')
|
|
431
|
+
if (loadFn !== undefined)
|
|
432
|
+
walkTrackedFunction(loadFn, PRIVATE_CTX_PROPS, 'ctx.', true, push)
|
|
433
|
+
}
|
|
434
|
+
if (isPageOrLayout) {
|
|
435
|
+
const defaultFn = findExportedFunction(analysis.program, 'default')
|
|
436
|
+
if (defaultFn !== undefined)
|
|
437
|
+
walkTrackedFunction(defaultFn, PRIVATE_PAGE_PROPS, '', true, push)
|
|
438
|
+
}
|
|
439
|
+
return hits
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
function collectTransitiveDeps(graph: GraphView, rootFile: string): string[] {
|
|
443
|
+
const visited = new Set<string>([rootFile])
|
|
444
|
+
const queue = [...graph.localDeps(rootFile)]
|
|
445
|
+
const deps: string[] = []
|
|
446
|
+
for (let i = 0; i < queue.length; i++) {
|
|
447
|
+
const dep = queue[i]!
|
|
448
|
+
if (visited.has(dep)) continue
|
|
449
|
+
visited.add(dep)
|
|
450
|
+
deps.push(dep)
|
|
451
|
+
for (const next of graph.localDeps(dep))
|
|
452
|
+
if (!visited.has(next)) queue.push(next)
|
|
453
|
+
}
|
|
454
|
+
return deps
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
/** 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. */
|
|
458
|
+
export function findCachedRoutePrivateReads(options: {
|
|
459
|
+
graph: GraphView
|
|
460
|
+
pageFiles: readonly string[]
|
|
461
|
+
layoutsFor: (pageFile: string) => readonly string[]
|
|
462
|
+
toRel: (file: string) => string
|
|
463
|
+
tool: 'prunelle check' | 'prunelle build'
|
|
464
|
+
}): PrivateReadProblem[] {
|
|
465
|
+
const { graph, pageFiles, layoutsFor, toRel, tool } = options
|
|
466
|
+
const problems: PrivateReadProblem[] = []
|
|
467
|
+
const pushHits = (file: string, hits: readonly PrivateReadHit[], msg: (what: string) => string): void => {
|
|
468
|
+
for (const hit of hits)
|
|
469
|
+
problems.push({ file, offset: hit.offset, message: msg(hit.what) })
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
for (const pageFile of pageFiles) {
|
|
473
|
+
const analysis = graph.analyses.get(pageFile)
|
|
474
|
+
if (analysis?.cache === undefined)
|
|
475
|
+
continue
|
|
476
|
+
const pageRel = toRel(pageFile)
|
|
477
|
+
const sharedTail = 'cached responses are shared across visitors, so per-visitor data would leak.'
|
|
478
|
+
pushHits(pageFile, findModulePrivateHits(analysis, true, true), w => `${tool}: cached route reads ${w}: ${sharedTail} Remove \`export const cache\` or stop reading ${w}.`)
|
|
479
|
+
for (const depFile of collectTransitiveDeps(graph, pageFile)) {
|
|
480
|
+
const dep = graph.analyses.get(depFile)
|
|
481
|
+
if (dep !== undefined)
|
|
482
|
+
pushHits(depFile, findModulePrivateHits(dep, false, false), w => `${tool}: cached route "${pageRel}" imports module reading ${w}: ${sharedTail} Remove \`export const cache\` from ${pageRel} or stop reading ${w}.`)
|
|
483
|
+
}
|
|
484
|
+
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}.`
|
|
485
|
+
for (const layoutFile of layoutsFor(pageFile)) {
|
|
486
|
+
const layoutAnalysis = graph.analyses.get(layoutFile)
|
|
487
|
+
if (layoutAnalysis === undefined)
|
|
488
|
+
continue
|
|
489
|
+
pushHits(layoutFile, findModulePrivateHits(layoutAnalysis, true, true), layoutMsg)
|
|
490
|
+
for (const depFile of collectTransitiveDeps(graph, layoutFile)) {
|
|
491
|
+
const dep = graph.analyses.get(depFile)
|
|
492
|
+
if (dep !== undefined)
|
|
493
|
+
pushHits(depFile, findModulePrivateHits(dep, false, false), layoutMsg)
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
return problems
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
function globalObjName(node: Node): string | undefined {
|
|
501
|
+
const obj = unwrapTsExpr(node)
|
|
502
|
+
if (obj.type === 'Identifier')
|
|
503
|
+
return obj.name
|
|
504
|
+
if (obj.type === 'MemberExpression' && obj.object.type === 'Identifier' && obj.object.name === 'globalThis')
|
|
505
|
+
return memberPropName(obj)
|
|
506
|
+
return undefined
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
function isNonDeterministicOrSyncIo(node: Node): boolean {
|
|
510
|
+
if (node.type === 'NewExpression' && node.arguments.length === 0)
|
|
511
|
+
return globalObjName(node.callee) === 'Date'
|
|
512
|
+
if (node.type !== 'CallExpression')
|
|
513
|
+
return false
|
|
514
|
+
const callee = unwrapTsExpr(node.callee)
|
|
515
|
+
if (callee.type === 'Identifier')
|
|
516
|
+
return (callee.name === 'Date' && node.arguments.length === 0) || SYNC_IO_CALLEES.has(callee.name) || callee.name === 'randomUUID'
|
|
517
|
+
if (callee.type !== 'MemberExpression')
|
|
518
|
+
return false
|
|
519
|
+
const prop = memberPropName(callee)
|
|
520
|
+
if (prop === undefined)
|
|
521
|
+
return false
|
|
522
|
+
if (SYNC_IO_CALLEES.has(prop))
|
|
523
|
+
return true
|
|
524
|
+
const owner = globalObjName(callee.object)
|
|
525
|
+
if (owner === 'Bun')
|
|
526
|
+
return prop === 'nanoseconds' || prop === 'randomUUIDv7'
|
|
527
|
+
if (owner === 'Math')
|
|
528
|
+
return prop === 'random'
|
|
529
|
+
if (owner === 'crypto')
|
|
530
|
+
return prop === 'randomUUID' || prop === 'getRandomValues'
|
|
531
|
+
return prop === 'now' && (owner === 'Date' || owner === 'performance')
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
function defaultReadsDynamicProps(fn: OxcFunction | ArrowFunctionExpression): boolean {
|
|
535
|
+
if (fn.params.length === 0 || fn.body === null)
|
|
536
|
+
return false
|
|
537
|
+
const first = unwrapParam(fn.params[0]!)
|
|
538
|
+
if (first.type === 'ObjectPattern')
|
|
539
|
+
return first.properties.some(prop => prop.type === 'RestElement' || !propKeyName(prop) || DYNAMIC_PROPS.has(propKeyName(prop)!))
|
|
540
|
+
if (first.type !== 'Identifier')
|
|
541
|
+
return true
|
|
542
|
+
let readsDynamic = false
|
|
543
|
+
walkTrackedFunction(fn, DYNAMIC_PROPS, '', true, () => { readsDynamic = true })
|
|
544
|
+
if (readsDynamic)
|
|
545
|
+
return true
|
|
546
|
+
walk(fn.body, (node) => {
|
|
547
|
+
if (readsDynamic)
|
|
548
|
+
return false
|
|
549
|
+
if ((node.type === 'JSXSpreadAttribute' || node.type === 'SpreadElement') && node.argument.type === 'Identifier' && node.argument.name === first.name) {
|
|
550
|
+
readsDynamic = true
|
|
551
|
+
return false
|
|
552
|
+
}
|
|
553
|
+
return undefined
|
|
554
|
+
})
|
|
555
|
+
return readsDynamic
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
function moduleReadsDynamicData(analysis: ModuleAnalysis, isRouteOrLayout: boolean): boolean {
|
|
559
|
+
if (analysis.imports.some(imp => DYNAMIC_IMPORTS.has(imp.source) || (imp.source === 'prunelle/server' && imp.specifiers.some(s => PRIVATE_SERVER_IMPORTS.has(s.imported)))))
|
|
560
|
+
return true
|
|
561
|
+
let dynamic = false
|
|
562
|
+
walk(analysis.program, (node) => {
|
|
563
|
+
if (dynamic)
|
|
564
|
+
return false
|
|
565
|
+
if (node.type === 'JSXAttribute' && node.name.type === 'JSXIdentifier' && EVENT_ATTR.test(node.name.name))
|
|
566
|
+
return false
|
|
567
|
+
if (node.type === 'CallExpression' && node.callee.type === 'Identifier' && CLIENT_CALLBACK_CALLEES.has(node.callee.name))
|
|
568
|
+
return false
|
|
569
|
+
if (node.type === 'JSXOpeningElement') {
|
|
570
|
+
const tag = resolvePrunelleTag(jsxTagName(node.name), analysis.imports)
|
|
571
|
+
if (tag !== undefined && DYNAMIC_TAGS.has(tag)) {
|
|
572
|
+
dynamic = true
|
|
573
|
+
return false
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
if (isNonDeterministicOrSyncIo(node) || (node.type === 'CallExpression' && isPrivateHeaderCall(node) !== undefined)) {
|
|
577
|
+
dynamic = true
|
|
578
|
+
return false
|
|
579
|
+
}
|
|
580
|
+
return undefined
|
|
581
|
+
})
|
|
582
|
+
if (dynamic)
|
|
583
|
+
return true
|
|
584
|
+
if (isRouteOrLayout) {
|
|
585
|
+
const defaultFn = findExportedFunction(analysis.program, 'default')
|
|
586
|
+
if (defaultFn !== undefined && defaultReadsDynamicProps(defaultFn))
|
|
587
|
+
return true
|
|
588
|
+
}
|
|
589
|
+
return false
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
function isPureIslandFile(file: string, analysis: ModuleAnalysis, islands: IslandView): boolean {
|
|
593
|
+
if (analysis.isInteractive && analysis.staticJsxTags.size === 0)
|
|
594
|
+
return true
|
|
595
|
+
const exported = [...analysis.exports.keys()]
|
|
596
|
+
return exported.length > 0 && exported.every(name => islands.targets.has(`${file}#${name}`))
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
/**
|
|
600
|
+
* Statically checks whether a page route renders identically on every request:
|
|
601
|
+
* static URL, non-async default component, no `load()`, no `_middleware.ts`, not `prerender = false`,
|
|
602
|
+
* and no request/clock reads across the page, its `_layout.tsx` chain, or local non-island modules they import.
|
|
603
|
+
*/
|
|
604
|
+
export function isRouteCacheable(options: {
|
|
605
|
+
graph: GraphView
|
|
606
|
+
islands: IslandView
|
|
607
|
+
file: string
|
|
608
|
+
layoutSrcs: readonly string[]
|
|
609
|
+
middlewareCount: number
|
|
610
|
+
routePath: string
|
|
611
|
+
}): boolean {
|
|
612
|
+
const { graph, islands, file, layoutSrcs, middlewareCount, routePath } = options
|
|
613
|
+
if (middlewareCount > 0 || routePath.includes(':') || routePath.includes('*'))
|
|
614
|
+
return false
|
|
615
|
+
const analysis = graph.analyses.get(file)
|
|
616
|
+
if (analysis === undefined || !analysis.exportedNames.includes('default') || analysis.exportedNames.includes('GET'))
|
|
617
|
+
return false
|
|
618
|
+
if (analysis.hasLoadExport || analysis.hasAsyncDefault || analysis.prerenderFalse)
|
|
619
|
+
return false
|
|
620
|
+
|
|
621
|
+
const seen = new Set<string>()
|
|
622
|
+
const walkFile = (cur: string, isRouteOrLayout: boolean): boolean => {
|
|
623
|
+
if (seen.has(cur))
|
|
624
|
+
return true
|
|
625
|
+
seen.add(cur)
|
|
626
|
+
const mod = graph.analyses.get(cur)
|
|
627
|
+
if (mod === undefined)
|
|
628
|
+
return true
|
|
629
|
+
if (!isRouteOrLayout && isPureIslandFile(cur, mod, islands))
|
|
630
|
+
return true
|
|
631
|
+
if ((isRouteOrLayout && mod.hasAsyncDefault) || moduleReadsDynamicData(mod, isRouteOrLayout))
|
|
632
|
+
return false
|
|
633
|
+
return graph.localDeps(cur).every(dep => walkFile(dep, false))
|
|
634
|
+
}
|
|
635
|
+
return layoutSrcs.every(layout => walkFile(layout, true)) && walkFile(file, true)
|
|
636
|
+
}
|