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