@fabrika/check 0.9.0-dev.13 → 0.9.0-dev.15
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/package.json +6 -6
- package/src/ast.ts +69 -6
- package/src/check.ts +41 -8
- package/src/contract.ts +41 -6
- package/src/finding.ts +1 -0
- package/src/inputs.ts +4 -2
- package/src/layout.ts +4 -1
- package/src/limits.ts +13 -3
- package/src/reader/schema.ts +42 -512
- package/src/reader/surface.ts +3 -2
- package/src/reader/value.ts +5 -5
- package/src/rules/cloudflare.ts +3 -3
- package/src/rules/literal.ts +3 -1
- package/src/rules/migrations.ts +155 -10
- package/src/rules/modules.ts +6 -0
- package/src/rules/platform-migrations.ts +10 -4
- package/src/rules/runtime.ts +6 -3
- package/src/rules/structure.ts +7 -6
- package/types/src/ast.d.ts +6 -0
- package/types/src/finding.d.ts +2 -2
- package/types/src/layout.d.ts +4 -1
- package/types/src/limits.d.ts +12 -2
- package/types/src/reader/schema.d.ts +5 -2
- package/types/src/reader/value.d.ts +2 -2
- package/types/src/rules/cloudflare.d.ts +1 -1
- package/types/src/rules/migrations.d.ts +12 -0
- package/types/src/rules/platform-migrations.d.ts +4 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fabrika/check",
|
|
3
|
-
"version": "0.9.0-dev.
|
|
3
|
+
"version": "0.9.0-dev.15",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -47,11 +47,11 @@
|
|
|
47
47
|
},
|
|
48
48
|
"dependencies": {
|
|
49
49
|
"@babel/parser": "8.0.7",
|
|
50
|
-
"@fabrika/app": "0.9.0-dev.
|
|
51
|
-
"@fabrika/auth-core": "0.9.0-dev.
|
|
52
|
-
"@fabrika/db": "0.9.0-dev.
|
|
53
|
-
"@fabrika/operations-contract": "0.9.0-dev.
|
|
54
|
-
"@fabrika/platform-node": "0.9.0-dev.
|
|
50
|
+
"@fabrika/app": "0.9.0-dev.15",
|
|
51
|
+
"@fabrika/auth-core": "0.9.0-dev.15",
|
|
52
|
+
"@fabrika/db": "0.9.0-dev.15",
|
|
53
|
+
"@fabrika/operations-contract": "0.9.0-dev.15",
|
|
54
|
+
"@fabrika/platform-node": "0.9.0-dev.15",
|
|
55
55
|
"zod": "^3.25.76"
|
|
56
56
|
}
|
|
57
57
|
}
|
package/src/ast.ts
CHANGED
|
@@ -167,17 +167,61 @@ export const isValueReference = (node: AstNode, parent: AstNode | undefined): bo
|
|
|
167
167
|
}
|
|
168
168
|
}
|
|
169
169
|
|
|
170
|
+
/**
|
|
171
|
+
* A node's line and column (from 1), as the parser counts them, for a tree `parseModule` returns: its `loc` is the
|
|
172
|
+
* start of its line (`lineLocations`). A tree parsed any other way has no such `loc` and reads as 1:1.
|
|
173
|
+
*/
|
|
170
174
|
export const positionOf = (node: AstNode): Position => {
|
|
171
175
|
const loc = node['loc']
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
return { line: start['line'], column: start['column'] + 1 }
|
|
176
|
-
}
|
|
176
|
+
const start = node['start']
|
|
177
|
+
if (isRecord(loc) && typeof loc['line'] === 'number' && typeof loc['offset'] === 'number' && typeof start === 'number') {
|
|
178
|
+
return { line: loc['line'], column: start - loc['offset'] + 1 }
|
|
177
179
|
}
|
|
178
180
|
return { line: 1, column: 1 }
|
|
179
181
|
}
|
|
180
182
|
|
|
183
|
+
/** Where a line starts: a line break is the parser's (`\r\n`, `\n`, `\r`, U+2028, U+2029). */
|
|
184
|
+
interface LineStart {
|
|
185
|
+
readonly line: number
|
|
186
|
+
readonly offset: number
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
const LINE_BREAK = /\r\n|[\n\r\u2028\u2029]/g
|
|
190
|
+
|
|
191
|
+
const isMutableNode = (value: unknown): value is Record<string, unknown> =>
|
|
192
|
+
typeof value === 'object' && value !== null && !Array.isArray(value) && 'type' in value && typeof value.type === 'string'
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Replaces the parser's location of every node below `roots` (an object and two positions per node) with the start of
|
|
196
|
+
* the node's line, one object per line, which `positionOf` reads with the node's offset. The syntax trees of a
|
|
197
|
+
* contract are most of the memory its reader holds, and these locations were the largest part of them.
|
|
198
|
+
*/
|
|
199
|
+
function lineLocations(text: string, roots: readonly unknown[]): void {
|
|
200
|
+
const lines: LineStart[] = [{ line: 1, offset: 0 }]
|
|
201
|
+
for (const match of text.matchAll(LINE_BREAK)) lines.push({ line: lines.length + 1, offset: match.index + match[0].length })
|
|
202
|
+
const lineAt = (offset: number): LineStart | undefined => {
|
|
203
|
+
let low = 0
|
|
204
|
+
let high = lines.length - 1
|
|
205
|
+
while (low < high) {
|
|
206
|
+
const middle = (low + high + 1) >> 1
|
|
207
|
+
if ((lines[middle]?.offset ?? 0) <= offset) low = middle
|
|
208
|
+
else high = middle - 1
|
|
209
|
+
}
|
|
210
|
+
return lines[low]
|
|
211
|
+
}
|
|
212
|
+
const pending: unknown[] = [...roots]
|
|
213
|
+
while (pending.length > 0) {
|
|
214
|
+
const value = pending.pop()
|
|
215
|
+
if (Array.isArray(value)) {
|
|
216
|
+
for (const item of value) pending.push(item)
|
|
217
|
+
} else if (isMutableNode(value)) {
|
|
218
|
+
const start = value['start']
|
|
219
|
+
value['loc'] = typeof start === 'number' ? lineAt(start) : undefined
|
|
220
|
+
for (const [key, child] of Object.entries(value)) if (!NON_CHILD_KEYS.has(key) && typeof child === 'object') pending.push(child)
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
181
225
|
/** Visits every node below `root`, depth first, with the chain of its ancestors (nearest last); `'skip'` prunes a subtree. */
|
|
182
226
|
export const walk = (root: AstNode, visit: (node: AstNode, ancestors: readonly AstNode[]) => 'skip' | undefined | void): void => {
|
|
183
227
|
const ancestors: AstNode[] = []
|
|
@@ -243,11 +287,30 @@ export const nodeDepth = (root: AstNode): number => {
|
|
|
243
287
|
return deepest
|
|
244
288
|
}
|
|
245
289
|
|
|
290
|
+
/** The number of syntax nodes in the tree below `root`, `root` included, counted without recursion. */
|
|
291
|
+
export const nodeCount = (root: AstNode): number => {
|
|
292
|
+
let count = 0
|
|
293
|
+
const pending: AstNode[] = [root]
|
|
294
|
+
for (let node = pending.pop(); node !== undefined; node = pending.pop()) {
|
|
295
|
+
count++
|
|
296
|
+
for (const [key, value] of Object.entries(node)) {
|
|
297
|
+
if (NON_CHILD_KEYS.has(key)) continue
|
|
298
|
+
if (isNode(value)) pending.push(value)
|
|
299
|
+
else if (Array.isArray(value)) {
|
|
300
|
+
for (const item of value) if (isNode(item)) pending.push(item)
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
return count
|
|
305
|
+
}
|
|
306
|
+
|
|
246
307
|
export const parseModule = (file: string, text: string): ParseResult => {
|
|
247
308
|
try {
|
|
248
|
-
|
|
309
|
+
// Nothing reads a comment from the node it is attached to; `ast.comments` lists them all.
|
|
310
|
+
const ast = parse(text, { sourceType: 'module', sourceFilename: file, plugins: pluginsFor(file), attachComment: false })
|
|
249
311
|
const program: unknown = ast.program
|
|
250
312
|
if (!isNode(program)) return { ok: false, message: 'the parser returned no program', position: { line: 1, column: 1 } }
|
|
313
|
+
lineLocations(text, [program, ast.comments])
|
|
251
314
|
return { ok: true, module: { file, program, comments: readComments(ast.comments), bytes: utf8Bytes(text) } }
|
|
252
315
|
} catch (error) {
|
|
253
316
|
const message = error instanceof Error ? (error.message.split('\n')[0] ?? error.message) : 'unparseable source'
|
package/src/check.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type AnyContract, compileContract, ContractError } from '@fabrika/app/contract'
|
|
2
|
-
import { type ContractArtifact, egressAdmits, type EgressEntry, parseEgressEntry } from '@fabrika/auth-core'
|
|
2
|
+
import { type ContractArtifact, egressAdmits, type EgressEntry, parseEgressEntry, PUBLIC_EGRESS, publicEgressRefusal } from '@fabrika/auth-core'
|
|
3
3
|
import { accessTables, type TablePolicies, tablePolicies, type Tables } from '@fabrika/db/tables'
|
|
4
4
|
import { readdir, readFile } from 'node:fs/promises'
|
|
5
5
|
import { join } from 'node:path'
|
|
@@ -36,6 +36,8 @@ import { checkJsonStrings } from './rules/json'
|
|
|
36
36
|
import { checkLiteralGraph } from './rules/literal'
|
|
37
37
|
import { checkMigrationHistory, type MigrationBase } from './rules/migration-history'
|
|
38
38
|
import {
|
|
39
|
+
type AccessKey,
|
|
40
|
+
checkAccessViewKeys,
|
|
39
41
|
checkConflictIndexes,
|
|
40
42
|
checkCurrentTimestampDefaults,
|
|
41
43
|
checkFileColumnMigrations,
|
|
@@ -55,6 +57,15 @@ import { checkComments, checkTypeNode } from './rules/types'
|
|
|
55
57
|
import { moduleSlice, sliceFinding } from './slice'
|
|
56
58
|
import { BUNFIG_FILE, checkTrustedResolution } from './trust'
|
|
57
59
|
|
|
60
|
+
/** Every table that names an access view, with its key (ADR-0076). */
|
|
61
|
+
const accessKeys = (policies: TablePolicies): AccessKey[] =>
|
|
62
|
+
Object.entries(policies).flatMap(([table, policy]) => {
|
|
63
|
+
const views = Object.values(policy.access ?? {}).map((access) => access.view)
|
|
64
|
+
const scope = policy.scope
|
|
65
|
+
const key = scope.mode === 'dimension' || scope.mode === 'child' || scope.mode === 'app' ? scope.key : undefined
|
|
66
|
+
return views.length === 0 || key === undefined ? [] : [{ table, key, views: [...new Set(views)].sort() }]
|
|
67
|
+
})
|
|
68
|
+
|
|
58
69
|
/** Every file column the table policies declare, with its kind. */
|
|
59
70
|
const fileColumns = (policies: TablePolicies): FileColumn[] =>
|
|
60
71
|
Object.entries(policies).flatMap(([table, policy]) => Object.entries(policy.files ?? {}).map(([column, kind]) => ({ table, column, kind })))
|
|
@@ -191,6 +202,8 @@ const URL_ENDPOINT_ALLOWED = "the id of a procedure of the contract: ctx.url('<m
|
|
|
191
202
|
interface CompiledReach {
|
|
192
203
|
readonly procedures: ReadonlySet<string>
|
|
193
204
|
readonly egress: readonly EgressEntry[]
|
|
205
|
+
/** Some cron job declares `egress: 'public'`. */
|
|
206
|
+
readonly publicEgress: boolean
|
|
194
207
|
}
|
|
195
208
|
|
|
196
209
|
/** The contract's artifact, or `null` when it does not compile, which its own rule reports. */
|
|
@@ -211,22 +224,27 @@ const compiledReach = (artifact: ContractArtifact): CompiledReach => {
|
|
|
211
224
|
return {
|
|
212
225
|
procedures: new Set(procedures.map((row) => row.id)),
|
|
213
226
|
egress: Object.values(artifact.declared ?? {}).flatMap((reach) =>
|
|
214
|
-
(reach.egress
|
|
227
|
+
(reach.egress === undefined || reach.egress === PUBLIC_EGRESS ? [] : reach.egress).flatMap((value) => {
|
|
215
228
|
const parsed = parseEgressEntry(value)
|
|
216
229
|
return parsed.ok ? [parsed.entry] : []
|
|
217
230
|
})
|
|
218
231
|
),
|
|
232
|
+
publicEgress: Object.values(artifact.declared ?? {}).some((reach) => reach.egress === PUBLIC_EGRESS),
|
|
219
233
|
}
|
|
220
234
|
}
|
|
221
235
|
|
|
222
236
|
/**
|
|
223
237
|
* A `fetch` of an absolute URL stands when no site declares its origin (a static prefix that ends inside the host is
|
|
224
|
-
* not accepted, nor an interpolated label a pattern does not cover)
|
|
225
|
-
*
|
|
226
|
-
*
|
|
238
|
+
* not accepted, nor an interpolated label a pattern does not cover) and, where a cron job declares `egress: 'public'`,
|
|
239
|
+
* when public egress would refuse it: another scheme, or a host its URL alone refuses (an IP address, a local or
|
|
240
|
+
* internal name, a port). A prefix that ends inside the host is then the runtime's to judge. Every one stands when the
|
|
241
|
+
* contract does not compile. A `ctx.url` stands when the contract compiles and names no such procedure. The runtime
|
|
242
|
+
* guard on `fetch` still checks the site that runs.
|
|
227
243
|
*/
|
|
228
244
|
const contractDependentFindings = (dependent: ContractDependent, reach: CompiledReach | null): Finding[] => [
|
|
229
|
-
...dependent.fetches.filter((fetch) =>
|
|
245
|
+
...dependent.fetches.filter((fetch) =>
|
|
246
|
+
reach === null || (!isDeclaredFetch(fetch, reach.egress) && !(reach.publicEgress && isPublicTarget(fetch.target)))
|
|
247
|
+
).map(({ finding }) => finding),
|
|
230
248
|
...(reach === null ? [] : dependent.urls.filter(({ endpoint }) => !reach.procedures.has(endpoint)).map(({ finding }) => finding)),
|
|
231
249
|
]
|
|
232
250
|
|
|
@@ -249,6 +267,19 @@ const urlOf = (value: string): URL | null => {
|
|
|
249
267
|
}
|
|
250
268
|
}
|
|
251
269
|
|
|
270
|
+
/** A static prefix public egress may reach: http(s), and a host that is not yet complete or that its URL admits. */
|
|
271
|
+
const isPublicTarget = (target: string): boolean => {
|
|
272
|
+
const scheme = /^(https?):\/\//i.exec(target)
|
|
273
|
+
if (scheme === null) return false
|
|
274
|
+
const rest = target.slice(scheme[0].length)
|
|
275
|
+
if (!/[/?#]/.test(rest)) return true
|
|
276
|
+
try {
|
|
277
|
+
return publicEgressRefusal(new URL(target)) === null
|
|
278
|
+
} catch {
|
|
279
|
+
return false
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
|
|
252
283
|
/** Runs every rule over one application and returns the findings in file, line and column order. */
|
|
253
284
|
export const checkApplication = async (input: CheckInput): Promise<Finding[]> => {
|
|
254
285
|
const tree: ApplicationTree = input.files === undefined ? await readApplicationTree(input.root) : { files: input.files, links: [] }
|
|
@@ -318,10 +349,11 @@ export const checkApplication = async (input: CheckInput): Promise<Finding[]> =>
|
|
|
318
349
|
const declaredTables = tables !== undefined && literal.length === 0 ? readTablesValue(graph, tables, new ReaderProblems()) : null
|
|
319
350
|
const artifact = read?.ok === true ? compiledArtifact(read.contract, declaredTables) : null
|
|
320
351
|
findings.push(...contractDependentFindings(dependent, artifact === null ? null : compiledReach(artifact)))
|
|
352
|
+
const dialect = applicationDialect(tree.files)
|
|
321
353
|
if (artifact !== null) {
|
|
322
|
-
findings.push(...checkMigrationClassification(tree.files, artifact.app, declaredTables === null ? [] : accessTables(declaredTables)))
|
|
354
|
+
findings.push(...checkMigrationClassification(tree.files, artifact.app, declaredTables === null ? [] : accessTables(declaredTables), dialect))
|
|
323
355
|
}
|
|
324
|
-
if (
|
|
356
|
+
if (dialect === 'sqlite') {
|
|
325
357
|
if (artifact !== null) findings.push(...checkCloudflareContract(artifact))
|
|
326
358
|
findings.push(...checkCurrentTimestampDefaults(tree.files))
|
|
327
359
|
findings.push(...checkSqliteFrameworkNames(tree.files))
|
|
@@ -332,6 +364,7 @@ export const checkApplication = async (input: CheckInput): Promise<Finding[]> =>
|
|
|
332
364
|
const policies = tablePolicies(declaredTables)
|
|
333
365
|
findings.push(...checkFileColumnMigrations(tree.files, fileColumns(policies)))
|
|
334
366
|
findings.push(...checkConflictIndexes(tree.files, new Set(Object.keys(policies)), dependent.inserts))
|
|
367
|
+
findings.push(...checkAccessViewKeys(tree.files, accessKeys(policies)))
|
|
335
368
|
}
|
|
336
369
|
const bunfig = tree.files.get(BUNFIG_FILE)
|
|
337
370
|
if (bunfig !== undefined) findings.push(...checkTestSuiteConfig(bunfig))
|
package/src/contract.ts
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
import { type AnyContract, compileContract } from '@fabrika/app/contract'
|
|
6
6
|
import type { ContractArtifact, TablePolicyArtifact } from '@fabrika/auth-core'
|
|
7
7
|
import { type TablePolicies, tablePolicies, type Tables } from '@fabrika/db/tables'
|
|
8
|
-
import { importedSpecifiers, nodeDepth, type ParsedModule, utf8Bytes } from './ast'
|
|
8
|
+
import { child, children, importedSpecifiers, nodeCount, nodeDepth, type ParsedModule, utf8Bytes } from './ast'
|
|
9
9
|
import { type Finding, formatFinding } from './finding'
|
|
10
10
|
import { ModuleGraph } from './graph'
|
|
11
11
|
import { CONTRACT_EXPORT, CONTRACT_FILE, isRelativeSpecifier, isSourceFile, resolveRelative, TABLES_FILE } from './layout'
|
|
@@ -161,21 +161,34 @@ export function contractModules(graph: ModuleGraph, entry: ParsedModule): Parsed
|
|
|
161
161
|
return [...reached.values()]
|
|
162
162
|
}
|
|
163
163
|
|
|
164
|
-
/** The source and syntax
|
|
164
|
+
/** The source and syntax limits of `CONTRACT_LIMITS` over the modules of one entry (the contract or the tables). */
|
|
165
165
|
function sourceBudget(modules: readonly ParsedModule[], entry = CONTRACT_FILE): Finding[] {
|
|
166
166
|
const findings: Finding[] = []
|
|
167
167
|
const bytes = modules.reduce((total, module) => total + module.bytes, 0)
|
|
168
168
|
if (bytes > CONTRACT_LIMITS.sourceBytes) {
|
|
169
169
|
findings.push(overBudget(entry, `${entry} and its modules are ${bytes} bytes, over ${CONTRACT_LIMITS.sourceBytes}`))
|
|
170
170
|
}
|
|
171
|
+
let nodes = 0
|
|
171
172
|
for (const module of modules) {
|
|
173
|
+
if (module.bytes > CONTRACT_LIMITS.moduleBytes) {
|
|
174
|
+
findings.push(overBudget(module.file, moduleTooLarge(module.bytes)))
|
|
175
|
+
}
|
|
172
176
|
if (nodeDepth(module.program) > CONTRACT_LIMITS.astDepth) {
|
|
173
177
|
findings.push(overBudget(module.file, `the syntax nests deeper than ${CONTRACT_LIMITS.astDepth} levels`))
|
|
174
178
|
}
|
|
179
|
+
nodes += heldNodes(module)
|
|
180
|
+
}
|
|
181
|
+
if (nodes > CONTRACT_LIMITS.astNodes) {
|
|
182
|
+
findings.push(overBudget(entry, `${entry} and its modules hold ${nodes} syntax nodes, over the budget of ${CONTRACT_LIMITS.astNodes}`))
|
|
175
183
|
}
|
|
176
184
|
return findings
|
|
177
185
|
}
|
|
178
186
|
|
|
187
|
+
const moduleTooLarge = (bytes: number): string => `the module is ${bytes} bytes, over ${CONTRACT_LIMITS.moduleBytes} for one module`
|
|
188
|
+
|
|
189
|
+
/** The syntax nodes the reader holds of a module: all but its top-level type declarations. */
|
|
190
|
+
const heldNodes = (module: ParsedModule): number => nodeCount(withoutTypeDeclarations(module).program)
|
|
191
|
+
|
|
179
192
|
function overBudget(file: string, message: string): Finding {
|
|
180
193
|
return {
|
|
181
194
|
file,
|
|
@@ -193,8 +206,8 @@ type ParsedGraph = { readonly ok: true; readonly graph: ModuleGraph } | { readon
|
|
|
193
206
|
/**
|
|
194
207
|
* Parses `entry` (`fabrika.contract.ts` or `src/db/tables.ts`) and the modules its imports reach among `files`, and
|
|
195
208
|
* nothing else; each module as its shell slice, so an import only a handler uses is never followed. Every candidate of an import is parsed, so that `rules/literal.ts` sees an ambiguous or directory
|
|
196
|
-
* import; the
|
|
197
|
-
* walks it.
|
|
209
|
+
* import; the byte limits hold before a module is parsed, and the depth and node limits right after, before any
|
|
210
|
+
* recursive rule walks it.
|
|
198
211
|
*/
|
|
199
212
|
function parseSourceGraph(files: ReadonlyMap<string, string>, entry: string): ParsedGraph {
|
|
200
213
|
const modules = new Map<string, ParsedModule>()
|
|
@@ -203,13 +216,18 @@ function parseSourceGraph(files: ReadonlyMap<string, string>, entry: string): Pa
|
|
|
203
216
|
return { ok: false, findings: [missingFile(entry)] }
|
|
204
217
|
}
|
|
205
218
|
let bytes = 0
|
|
219
|
+
let nodes = 0
|
|
206
220
|
const queue = [entry]
|
|
207
221
|
for (let path = queue.shift(); path !== undefined; path = queue.shift()) {
|
|
208
222
|
const text = files.get(path)
|
|
209
223
|
if (modules.has(path) || text === undefined || !isSourceFile(path)) {
|
|
210
224
|
continue
|
|
211
225
|
}
|
|
212
|
-
|
|
226
|
+
const moduleBytes = utf8Bytes(text)
|
|
227
|
+
if (moduleBytes > CONTRACT_LIMITS.moduleBytes) {
|
|
228
|
+
return { ok: false, findings: [overBudget(path, moduleTooLarge(moduleBytes))] }
|
|
229
|
+
}
|
|
230
|
+
bytes += moduleBytes
|
|
213
231
|
if (bytes > CONTRACT_LIMITS.sourceBytes) {
|
|
214
232
|
return { ok: false, findings: [overBudget(path, `${entry} and its modules are over ${CONTRACT_LIMITS.sourceBytes} bytes`)] }
|
|
215
233
|
}
|
|
@@ -229,7 +247,12 @@ function parseSourceGraph(files: ReadonlyMap<string, string>, entry: string): Pa
|
|
|
229
247
|
if (nodeDepth(parsed.module.program) > CONTRACT_LIMITS.astDepth) {
|
|
230
248
|
return { ok: false, findings: [overBudget(path, `the syntax nests deeper than ${CONTRACT_LIMITS.astDepth} levels`)] }
|
|
231
249
|
}
|
|
232
|
-
|
|
250
|
+
const held = withoutTypeDeclarations(parsed.module)
|
|
251
|
+
nodes += nodeCount(held.program)
|
|
252
|
+
if (nodes > CONTRACT_LIMITS.astNodes) {
|
|
253
|
+
return { ok: false, findings: [overBudget(path, `${entry} and its modules hold over ${CONTRACT_LIMITS.astNodes} syntax nodes`)] }
|
|
254
|
+
}
|
|
255
|
+
modules.set(path, held)
|
|
233
256
|
for (const specifier of importedSpecifiers(parsed.module)) {
|
|
234
257
|
const resolution = isRelativeSpecifier(specifier) ? resolveRelative((candidate) => files.has(candidate), path, specifier) : undefined
|
|
235
258
|
if (resolution?.kind === 'file') {
|
|
@@ -242,6 +265,18 @@ function parseSourceGraph(files: ReadonlyMap<string, string>, entry: string): Pa
|
|
|
242
265
|
return findings.length > 0 ? { ok: false, findings } : { ok: true, graph: new ModuleGraph(modules) }
|
|
243
266
|
}
|
|
244
267
|
|
|
268
|
+
/** Top-level type declarations: the module index binds no name to them, and the literal rule passes over them. */
|
|
269
|
+
const TYPE_DECLARATIONS = new Set(['TSTypeAliasDeclaration', 'TSInterfaceDeclaration'])
|
|
270
|
+
|
|
271
|
+
/** The module without its top-level type declarations, which the reader would otherwise hold until it is done. */
|
|
272
|
+
function withoutTypeDeclarations(module: ParsedModule): ParsedModule {
|
|
273
|
+
const body = children(module.program, 'body').filter((statement) => {
|
|
274
|
+
const declaration = statement?.type === 'ExportNamedDeclaration' ? child(statement, 'declaration') : statement
|
|
275
|
+
return declaration === null || declaration === undefined || !TYPE_DECLARATIONS.has(declaration.type)
|
|
276
|
+
})
|
|
277
|
+
return { ...module, program: { ...module.program, body } }
|
|
278
|
+
}
|
|
279
|
+
|
|
245
280
|
function missingFile(file: string): Finding {
|
|
246
281
|
return {
|
|
247
282
|
file,
|
package/src/finding.ts
CHANGED
package/src/inputs.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
// Which files of an application the static reader needs. Control reads a commit file by file, and the reader cannot
|
|
2
2
|
// tell a missing file from one it was not given, so the caller asks here until nothing is missing. Worker-safe.
|
|
3
3
|
|
|
4
|
-
import { importedSpecifiers } from './ast'
|
|
4
|
+
import { importedSpecifiers, utf8Bytes } from './ast'
|
|
5
5
|
import { parseJsonc } from './jsonc'
|
|
6
6
|
import { CONTRACT_FILE, isRelativeSpecifier, isSourceFile, joinRelative, resolveRelative, TABLES_FILE } from './layout'
|
|
7
|
+
import { CONTRACT_LIMITS } from './limits'
|
|
7
8
|
import { parseShellModule } from './slice'
|
|
8
9
|
import { BUNFIG_FILE } from './trust'
|
|
9
10
|
|
|
@@ -49,7 +50,8 @@ export function contractReaderInputs(files: ReadonlyMap<string, string>, exists:
|
|
|
49
50
|
want(path)
|
|
50
51
|
continue
|
|
51
52
|
}
|
|
52
|
-
|
|
53
|
+
// A module over the limit is not parsed here: the reader refuses it, and its own parse would be the peak.
|
|
54
|
+
const parsed = isSourceFile(path) && utf8Bytes(text) <= CONTRACT_LIMITS.moduleBytes ? parseShellModule(path, text).parsed : undefined
|
|
53
55
|
if (parsed?.ok !== true) continue
|
|
54
56
|
for (const specifier of importedSpecifiers(parsed.module)) {
|
|
55
57
|
if (!isRelativeSpecifier(specifier)) continue
|
package/src/layout.ts
CHANGED
|
@@ -26,7 +26,10 @@ export const APP_KIT_FILE = 'src/app-kit.ts'
|
|
|
26
26
|
/** The shared zod schemas the shell and the SPA import: one module, or a directory of them once it grows. */
|
|
27
27
|
export const SCHEMAS_FILE = 'src/schemas.ts'
|
|
28
28
|
export const SCHEMAS_DIR = 'src/schemas/'
|
|
29
|
-
/**
|
|
29
|
+
/**
|
|
30
|
+
* Pure code the SPA and every slice may import: it loads nothing but `zod/v4`, `@fabrika/app/schema` and other modules of
|
|
31
|
+
* its own directory.
|
|
32
|
+
*/
|
|
30
33
|
export const SHARED_DIR = 'src/shared/'
|
|
31
34
|
/** `src/db/`: the tables, the generated schema and their generated tests; never a slice. */
|
|
32
35
|
export const DATABASE_DIR = 'src/db/'
|
package/src/limits.ts
CHANGED
|
@@ -4,12 +4,22 @@
|
|
|
4
4
|
* step runs.
|
|
5
5
|
*/
|
|
6
6
|
export const CONTRACT_LIMITS = {
|
|
7
|
-
/**
|
|
8
|
-
|
|
7
|
+
/**
|
|
8
|
+
* UTF-8 bytes of `fabrika.contract.ts` and every module it imports, together. Control reads them in a Cloudflare
|
|
9
|
+
* Worker, whose isolate has 128 MB; handler bodies are sliced out before the parse, so they cost bytes, not nodes.
|
|
10
|
+
*/
|
|
11
|
+
sourceBytes: 4 * 1024 * 1024,
|
|
12
|
+
/** UTF-8 bytes of one module: its own parse is the peak while it is read, and a dense one needs up to 340 times it. */
|
|
13
|
+
moduleBytes: 512 * 1024,
|
|
14
|
+
/**
|
|
15
|
+
* Syntax nodes the reader holds over each graph (the contract's, the tables'), top-level type declarations left out.
|
|
16
|
+
* At this, `sourceBytes` and `schemaNodes` together, Control's read needs a 95 MB heap of its 128 MB isolate.
|
|
17
|
+
*/
|
|
18
|
+
astNodes: 400_000,
|
|
9
19
|
/** Nesting of syntax nodes in one module; the contracts in this repository reach 25. */
|
|
10
20
|
astDepth: 128,
|
|
11
21
|
/** Schema nodes once every shared schema is expanded where the contract uses it, over the whole contract. */
|
|
12
|
-
schemaNodes:
|
|
22
|
+
schemaNodes: 60_000,
|
|
13
23
|
/** Nesting of one schema: objects, arrays, unions and wrappers. */
|
|
14
24
|
schemaDepth: 32,
|
|
15
25
|
} as const
|