@fabrika/check 0.9.0-dev.20 → 0.9.0-dev.22
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/check.ts +28 -19
- package/src/cli.ts +14 -7
- package/src/finding.ts +0 -1
- package/src/index.ts +1 -1
- package/src/layout.ts +20 -2
- package/src/rules/migration-history.ts +1 -1
- package/src/rules/structure.ts +6 -3
- package/src/rules/types.ts +17 -16
- package/src/source-maps.ts +2 -0
- package/types/src/finding.d.ts +2 -2
- package/types/src/index.d.ts +1 -1
- package/types/src/layout.d.ts +7 -1
- package/types/src/rules/structure.d.ts +3 -1
- package/types/src/rules/types.d.ts +2 -0
- package/src/rules/crontab.ts +0 -147
- package/types/src/rules/crontab.d.ts +0 -15
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.22",
|
|
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.22",
|
|
51
|
+
"@fabrika/auth-core": "0.9.0-dev.22",
|
|
52
|
+
"@fabrika/db": "0.9.0-dev.22",
|
|
53
|
+
"@fabrika/operations-contract": "0.9.0-dev.22",
|
|
54
|
+
"@fabrika/platform-node": "0.9.0-dev.22",
|
|
55
55
|
"zod": "^3.25.76"
|
|
56
56
|
}
|
|
57
57
|
}
|
package/src/check.ts
CHANGED
|
@@ -12,6 +12,7 @@ import {
|
|
|
12
12
|
CONTRACT_FILE,
|
|
13
13
|
GENERATED_ENTRY_FILE,
|
|
14
14
|
isCheckedFile,
|
|
15
|
+
isGeneratedModule,
|
|
15
16
|
isIgnoredSegment,
|
|
16
17
|
isJsonFile,
|
|
17
18
|
isServerModule,
|
|
@@ -19,7 +20,6 @@ import {
|
|
|
19
20
|
isTestFile,
|
|
20
21
|
isToolModule,
|
|
21
22
|
TABLES_FILE,
|
|
22
|
-
ZEROPS_FILES,
|
|
23
23
|
} from './layout'
|
|
24
24
|
import type { PackedSet } from './packed'
|
|
25
25
|
import { ReaderProblems } from './reader/problems'
|
|
@@ -29,7 +29,6 @@ import { type ImportMappings, readImportMappings } from './resolution'
|
|
|
29
29
|
import { checkCloudflareContract } from './rules/cloudflare'
|
|
30
30
|
import { checkContractCompiles } from './rules/compile'
|
|
31
31
|
import { checkContractRead } from './rules/contract'
|
|
32
|
-
import { checkCrontab, contractJobs } from './rules/crontab'
|
|
33
32
|
import { checkFrameworkWrite, trustedBindings } from './rules/framework'
|
|
34
33
|
import { checkClientIdentity } from './rules/identity'
|
|
35
34
|
import { checkJsonStrings } from './rules/json'
|
|
@@ -53,7 +52,7 @@ import { checkBackupPlan, checkMigrationClassification } from './rules/platform-
|
|
|
53
52
|
import { checkKeyLiteralNode, checkRuntimeNode, crossAppFetchMessage, rawSqlBindings } from './rules/runtime'
|
|
54
53
|
import { checkStructureNode, checkStructureReferences, checkToolPackages, structurePlace } from './rules/structure'
|
|
55
54
|
import { checkMockModule, checkTestNode, checkTestSuiteConfig } from './rules/tests'
|
|
56
|
-
import { checkComments, checkTypeNode } from './rules/types'
|
|
55
|
+
import { checkCapabilityNode, checkComments, checkTypeNode } from './rules/types'
|
|
57
56
|
import { moduleSlice, sliceFinding } from './slice'
|
|
58
57
|
import { BUNFIG_FILE, checkTrustedResolution } from './trust'
|
|
59
58
|
|
|
@@ -115,27 +114,37 @@ interface ContractDependent {
|
|
|
115
114
|
readonly inserts: ConflictInsert[]
|
|
116
115
|
}
|
|
117
116
|
|
|
117
|
+
/**
|
|
118
|
+
* Whether `type-assertion`, `any-type` and `suppression-comment` (how a module's own source is written) apply: not to
|
|
119
|
+
* a generated module, nor to a test file, which never ships and which `test-only-import` keeps out of shipped code.
|
|
120
|
+
*/
|
|
121
|
+
const isHeldToWriting = (file: string, generated: boolean): boolean => !generated && !isTestFile(file)
|
|
122
|
+
|
|
118
123
|
/**
|
|
119
124
|
* A module under `tools/` runs on a developer's machine, never in the application: it is held to how its source is
|
|
120
|
-
* written (`type-assertion`, `any-type
|
|
121
|
-
* module` of a trusted package and the rules of its tests. Every other module rule guards code that runs in the
|
|
125
|
+
* written (`type-assertion`, `any-type` and `suppression-comment` outside its tests; `capability-widening`), `key-literal`,
|
|
126
|
+
* a `declare module` of a trusted package and the rules of its tests. Every other module rule guards code that runs in the
|
|
122
127
|
* application.
|
|
123
128
|
*/
|
|
124
|
-
const checkToolModule = (module: ParsedModule, findings: Finding[]): void => {
|
|
129
|
+
const checkToolModule = (module: ParsedModule, generated: boolean, findings: Finding[]): void => {
|
|
125
130
|
const report = reporter(module.file, findings)
|
|
126
131
|
const isTest = isTestFile(module.file)
|
|
132
|
+
const writing = isHeldToWriting(module.file, generated)
|
|
127
133
|
walk(module.program, (node, ancestors) => {
|
|
128
134
|
checkKeyLiteralNode(node, report)
|
|
129
|
-
checkTypeNode(node, report)
|
|
135
|
+
if (writing) checkTypeNode(node, report)
|
|
136
|
+
checkCapabilityNode(node, report)
|
|
130
137
|
checkAmbientModule(node, report)
|
|
131
138
|
if (isTest) checkTestNode(node, ancestors, report)
|
|
132
139
|
checkMockModule(node, report)
|
|
133
140
|
})
|
|
134
|
-
checkComments(module.comments, report)
|
|
141
|
+
if (writing) checkComments(module.comments, report)
|
|
135
142
|
}
|
|
136
143
|
|
|
144
|
+
/** A generated module (`isGeneratedModule`) is not held to `type-assertion`, `any-type`, `suppression-comment` and the hints; a test file not to the first three. */
|
|
137
145
|
const checkSourceModule = (
|
|
138
146
|
module: ParsedModule,
|
|
147
|
+
generated: boolean,
|
|
139
148
|
references: readonly ModuleReference[],
|
|
140
149
|
mappings: ImportMappings,
|
|
141
150
|
files: ReadonlyMap<string, string>,
|
|
@@ -146,6 +155,7 @@ const checkSourceModule = (
|
|
|
146
155
|
const report = reporter(module.file, findings)
|
|
147
156
|
const isGeneratedEntry = module.file === GENERATED_ENTRY_FILE
|
|
148
157
|
const isTest = isTestFile(module.file)
|
|
158
|
+
const writing = isHeldToWriting(module.file, generated)
|
|
149
159
|
const deferred = (at: AstNode, rule: 'cross-app-fetch' | 'url-endpoint' | 'on-conflict-index', message: string, allowed: string): Finding[] => {
|
|
150
160
|
const found: Finding[] = []
|
|
151
161
|
reporter(module.file, found)(at, rule, message, allowed)
|
|
@@ -171,17 +181,18 @@ const checkSourceModule = (
|
|
|
171
181
|
},
|
|
172
182
|
}
|
|
173
183
|
const trusted = trustedBindings(module.file, module.program, mappings)
|
|
174
|
-
const place = structurePlace(module.file, isGeneratedEntry)
|
|
184
|
+
const place = structurePlace(module.file, isGeneratedEntry, generated)
|
|
175
185
|
walk(module.program, (node, ancestors) => {
|
|
176
186
|
checkRuntimeNode(node, ancestors, context)
|
|
177
187
|
checkFrameworkWrite(node, ancestors, trusted, report)
|
|
178
|
-
checkTypeNode(node, report)
|
|
188
|
+
if (writing) checkTypeNode(node, report)
|
|
189
|
+
checkCapabilityNode(node, report)
|
|
179
190
|
checkAmbientModule(node, report)
|
|
180
191
|
if (isTest) checkTestNode(node, ancestors, report)
|
|
181
192
|
checkMockModule(node, report)
|
|
182
193
|
checkStructureNode(place, node, report)
|
|
183
194
|
})
|
|
184
|
-
checkComments(module.comments, report)
|
|
195
|
+
if (writing) checkComments(module.comments, report)
|
|
185
196
|
checkModuleReferences(module.file, references, mappings, exists, report, isGeneratedEntry, isTest)
|
|
186
197
|
}
|
|
187
198
|
|
|
@@ -283,6 +294,7 @@ export const checkApplication = async (input: CheckInput): Promise<Finding[]> =>
|
|
|
283
294
|
const findings: Finding[] = []
|
|
284
295
|
const modules = new Map<string, ParsedModule>()
|
|
285
296
|
const references = new Map<string, readonly ModuleReference[]>()
|
|
297
|
+
const generated = new Set<string>()
|
|
286
298
|
const dependent: ContractDependent = { fetches: [], urls: [], inserts: [] }
|
|
287
299
|
const checkedPaths = [...tree.files.keys()].filter(isCheckedFile).sort()
|
|
288
300
|
const mappings = readImportMappings(tree.files)
|
|
@@ -313,12 +325,14 @@ export const checkApplication = async (input: CheckInput): Promise<Finding[]> =>
|
|
|
313
325
|
modules.set(path, parsed.module)
|
|
314
326
|
const referencesOfModule = moduleReferences(parsed.module.program)
|
|
315
327
|
references.set(path, referencesOfModule)
|
|
316
|
-
|
|
317
|
-
|
|
328
|
+
const isGenerated = isGeneratedModule(path, parsed.module)
|
|
329
|
+
if (isGenerated) generated.add(path)
|
|
330
|
+
if (isToolModule(path)) checkToolModule(parsed.module, isGenerated, findings)
|
|
331
|
+
else checkSourceModule(parsed.module, isGenerated, referencesOfModule, mappings, tree.files, findings, dependent)
|
|
318
332
|
}
|
|
319
333
|
const structure = { files: tree.files, mappings, referencesOf: (path: string) => references.get(path) ?? [] }
|
|
320
334
|
for (const path of modules.keys()) {
|
|
321
|
-
checkStructureReferences(structure, structurePlace(path, path === GENERATED_ENTRY_FILE), reporter(path, findings))
|
|
335
|
+
checkStructureReferences(structure, structurePlace(path, path === GENERATED_ENTRY_FILE, generated.has(path)), reporter(path, findings))
|
|
322
336
|
}
|
|
323
337
|
|
|
324
338
|
// The contract rules read each module's shell slice: a colocated handler, and what only it uses, is application
|
|
@@ -365,10 +379,5 @@ export const checkApplication = async (input: CheckInput): Promise<Finding[]> =>
|
|
|
365
379
|
}
|
|
366
380
|
const bunfig = tree.files.get(BUNFIG_FILE)
|
|
367
381
|
if (bunfig !== undefined) findings.push(...checkTestSuiteConfig(bunfig))
|
|
368
|
-
const jobs = contract === undefined ? [] : contractJobs(contract, graph)
|
|
369
|
-
for (const name of ZEROPS_FILES) {
|
|
370
|
-
const text = tree.files.get(name)
|
|
371
|
-
if (text !== undefined) findings.push(...checkCrontab(name, text, jobs))
|
|
372
|
-
}
|
|
373
382
|
return findings.sort(compareFindings)
|
|
374
383
|
}
|
package/src/cli.ts
CHANGED
|
@@ -11,7 +11,7 @@ import type { MigrationBase } from './rules/migration-history'
|
|
|
11
11
|
const USAGE = `fabrika-check — the application checker
|
|
12
12
|
|
|
13
13
|
Usage:
|
|
14
|
-
fabrika-check [--packed <release-manifest.json>] [--base <ref>] [<root>]
|
|
14
|
+
fabrika-check [--hints] [--packed <release-manifest.json>] [--base <ref>] [<root>]
|
|
15
15
|
|
|
16
16
|
Checks the application in <root> (default: the current directory) and prints one line per finding:
|
|
17
17
|
<file>:<line>:<column> <rule> <message> (allowed form: <what to write instead>)
|
|
@@ -24,8 +24,8 @@ In a git work tree, every migration of db/migrations at the merge base of HEAD a
|
|
|
24
24
|
(origin/HEAD, else origin/main or origin/master) must be in the tree unchanged: a deploy refuses a migration that
|
|
25
25
|
changed after it was applied. --base names another ref to compare with.
|
|
26
26
|
|
|
27
|
-
A refusal fails the check. A hint
|
|
28
|
-
|
|
27
|
+
A refusal fails the check. A hint reports a step out of the application architecture and does not fail it. Without
|
|
28
|
+
--hints the checker prints how many there are; with it, it lists them under their own heading after the refusals.
|
|
29
29
|
|
|
30
30
|
Exit status: 0 with no refusals, 1 with a refusal, 2 on a usage or read error.
|
|
31
31
|
There is no suppression syntax; a finding is fixed in the code.
|
|
@@ -39,23 +39,26 @@ interface Arguments {
|
|
|
39
39
|
readonly root: string | undefined
|
|
40
40
|
readonly packed: string | undefined
|
|
41
41
|
readonly base: string | undefined
|
|
42
|
+
readonly hints: boolean
|
|
42
43
|
}
|
|
43
44
|
|
|
44
|
-
/** The root, the `--packed` manifest
|
|
45
|
+
/** The root, the `--packed` manifest, the `--base` ref and `--hints`, or the first argument the checker does not take. */
|
|
45
46
|
const parseArguments = (argv: readonly string[]): Arguments | { readonly unexpected: string } => {
|
|
46
47
|
let root: string | undefined
|
|
47
48
|
let packed: string | undefined
|
|
48
49
|
let base: string | undefined
|
|
50
|
+
let hints = false
|
|
49
51
|
for (let index = 0; index < argv.length; index++) {
|
|
50
52
|
const argument = argv[index] ?? ''
|
|
51
53
|
if (argument === '--packed' && packed === undefined && argv[index + 1] !== undefined) packed = argv[++index]
|
|
52
54
|
else if (argument.startsWith('--packed=') && packed === undefined) packed = argument.slice('--packed='.length)
|
|
53
55
|
else if (argument === '--base' && base === undefined && argv[index + 1] !== undefined) base = argv[++index]
|
|
54
56
|
else if (argument.startsWith('--base=') && base === undefined && argument.length > '--base='.length) base = argument.slice('--base='.length)
|
|
57
|
+
else if (argument === '--hints' && !hints) hints = true
|
|
55
58
|
else if (argument.startsWith('-') || root !== undefined) return { unexpected: argument }
|
|
56
59
|
else root = argument
|
|
57
60
|
}
|
|
58
|
-
return { root, packed, base }
|
|
61
|
+
return { root, packed, base, hints }
|
|
59
62
|
}
|
|
60
63
|
|
|
61
64
|
const main = async (argv: readonly string[]): Promise<number> => {
|
|
@@ -98,8 +101,12 @@ const main = async (argv: readonly string[]): Promise<number> => {
|
|
|
98
101
|
const refusals = findings.filter((finding) => finding.severity === 'refusal')
|
|
99
102
|
const hints = findings.filter((finding) => finding.severity === 'hint')
|
|
100
103
|
for (const finding of refusals) console.log(formatFinding(finding))
|
|
101
|
-
if (hints
|
|
102
|
-
|
|
104
|
+
if (parsed.hints) {
|
|
105
|
+
if (hints.length > 0) console.log(HINTS_HEADING)
|
|
106
|
+
for (const finding of hints) console.log(formatFinding(finding))
|
|
107
|
+
} else if (hints.length > 0) {
|
|
108
|
+
console.log(`${count(hints.length, 'hint')} not listed (hints do not fail the check): fabrika-check --hints lists them`)
|
|
109
|
+
}
|
|
103
110
|
console.error(`fabrika-check: ${count(refusals.length, 'refusal')}, ${count(hints.length, 'hint')}`)
|
|
104
111
|
return refusals.length === 0 ? 0 : 1
|
|
105
112
|
}
|
package/src/finding.ts
CHANGED
package/src/index.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export { type ApplicationTree, checkApplication, type CheckInput, readApplicationTree } from './check'
|
|
2
2
|
export { type ApplicationDialect, applicationDialect } from './dialect'
|
|
3
3
|
export { type Finding, formatFinding, RULE_IDS, type RuleId } from './finding'
|
|
4
|
-
export { CONFIG_FILE, CONTRACT_FILE, GENERATED_ENTRY_FILE, MIGRATIONS_DIR, TABLES_FILE
|
|
4
|
+
export { CONFIG_FILE, CONTRACT_FILE, GENERATED_ENTRY_FILE, MIGRATIONS_DIR, TABLES_FILE } from './layout'
|
|
5
5
|
export { readMigrationBase } from './migration-base'
|
|
6
6
|
export { type PackedSet, type PackedTarball, readPackedSet } from './packed'
|
|
7
7
|
export type { MigrationBase } from './rules/migration-history'
|
package/src/layout.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { children, type ParsedModule, positionOf } from './ast'
|
|
2
|
+
|
|
1
3
|
/** Where an application keeps what the checker reads. Paths are relative to the application root. */
|
|
2
4
|
export const CONTRACT_FILE = 'fabrika.contract.ts'
|
|
3
5
|
/** The provider-authored application config, whose `defineApp` names the provider. */
|
|
@@ -11,7 +13,6 @@ export const GENERATED_ENTRY_FILE = 'fabrika/entry.generated.ts'
|
|
|
11
13
|
/** Where a Cloudflare deploy stages the SPA mounts' built files (`@fabrika/provider-cloudflare`): build output, never source. */
|
|
12
14
|
export const STAGED_ASSETS_DIR = 'fabrika/assets/'
|
|
13
15
|
export const MIGRATIONS_DIR = 'db/migrations/'
|
|
14
|
-
export const ZEROPS_FILES = ['zerops.yaml', 'zerops.yml'] as const
|
|
15
16
|
|
|
16
17
|
/** The application architecture (docs/reference/application-architecture.md): where each layer's code lives. */
|
|
17
18
|
export const VOCABULARY_FILE = 'src/vocabulary.ts'
|
|
@@ -60,12 +61,29 @@ export const isToolModule = (path: string): boolean => path.startsWith(TOOLS_DIR
|
|
|
60
61
|
|
|
61
62
|
export const isJsonFile = (path: string): boolean => path.endsWith('.json')
|
|
62
63
|
|
|
64
|
+
const GENERATED_NAME = /\.(?:gen|generated)\.(?:[mc]?[jt]s|[jt]sx)$/
|
|
65
|
+
const GENERATED_MARKER = /@generated|auto-generated/i
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* A module a generator writes: named `<name>.gen.<ext>` or `<name>.generated.<ext>`, opening with a comment on its first
|
|
69
|
+
* line, before any statement, that says so (`@generated` or `auto-generated`, in any case). The rules about how source
|
|
70
|
+
* is written do not apply to it; every rule about what it does still does.
|
|
71
|
+
*/
|
|
72
|
+
export const isGeneratedModule = (path: string, module: Pick<ParsedModule, 'program' | 'comments'>): boolean => {
|
|
73
|
+
const [header] = module.comments
|
|
74
|
+
if (!GENERATED_NAME.test(path) || header === undefined || header.position.line !== 1 || !GENERATED_MARKER.test(header.text)) return false
|
|
75
|
+
const [statement] = children(module.program, 'body')
|
|
76
|
+
if (statement === undefined || statement === null) return true
|
|
77
|
+
const start = positionOf(statement)
|
|
78
|
+
return header.position.line < start.line || (header.position.line === start.line && header.position.column < start.column)
|
|
79
|
+
}
|
|
80
|
+
|
|
63
81
|
/** Whether the checker reads this file at all. */
|
|
64
82
|
export const isCheckedFile = (path: string): boolean => {
|
|
65
83
|
// The registry configuration and the lockfile decide what a trusted package name installs.
|
|
66
84
|
if (path === '.npmrc' || path === 'bun.lock') return true
|
|
67
85
|
if (isSkippedPath(path)) return false
|
|
68
|
-
return isSourceFile(path) || isJsonFile(path) || path.startsWith(MIGRATIONS_DIR)
|
|
86
|
+
return isSourceFile(path) || isJsonFile(path) || path.startsWith(MIGRATIONS_DIR)
|
|
69
87
|
|| path === 'bunfig.toml'
|
|
70
88
|
}
|
|
71
89
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// A migration that a shared branch already holds, edited, renamed or deleted in the tree being checked. Every runner
|
|
2
2
|
// keeps the SHA-256 of each applied file and refuses a deploy whose file changed after it ran (ADR-0017), and Control
|
|
3
|
-
//
|
|
3
|
+
// refuses it at the run's snapshot; neither sees history, so the checker is where the edit can be named before the push.
|
|
4
4
|
|
|
5
5
|
import type { Finding } from '../finding'
|
|
6
6
|
import { MIGRATIONS_DIR } from '../layout'
|
package/src/rules/structure.ts
CHANGED
|
@@ -57,13 +57,16 @@ export interface StructurePlace {
|
|
|
57
57
|
readonly part: SlicePart | undefined
|
|
58
58
|
/** A test or the generated entrypoint: the structure hints do not apply to it. */
|
|
59
59
|
readonly exempt: boolean
|
|
60
|
+
/** A generated module (`isGeneratedModule`): the hints do not apply to it, the refusals do. */
|
|
61
|
+
readonly generated: boolean
|
|
60
62
|
}
|
|
61
63
|
|
|
62
|
-
export const structurePlace = (file: string, isGeneratedEntry: boolean): StructurePlace => ({
|
|
64
|
+
export const structurePlace = (file: string, isGeneratedEntry: boolean, generated: boolean): StructurePlace => ({
|
|
63
65
|
file,
|
|
64
66
|
slice: sliceOf(file),
|
|
65
67
|
part: slicePart(file),
|
|
66
68
|
exempt: isGeneratedEntry || isTestFile(file),
|
|
69
|
+
generated,
|
|
67
70
|
})
|
|
68
71
|
|
|
69
72
|
/** What the structure rules read beyond one module: every module's references, and how Bun resolves them. */
|
|
@@ -254,7 +257,7 @@ export const checkStructureReferences = (graph: StructureGraph, place: Structure
|
|
|
254
257
|
if (place.file.startsWith(SPA_DIR) && !place.exempt) checkSpaReference(graph, reference, targets, report)
|
|
255
258
|
if (isSharedModule(place.file) && !place.exempt) checkSharedReference(reference, targets, report)
|
|
256
259
|
if (isLayerModule(place.file)) checkLayerReference(graph, place, reference, targets, report)
|
|
257
|
-
if (place.exempt) continue
|
|
260
|
+
if (place.exempt || place.generated) continue
|
|
258
261
|
if (place.part === 'logic') checkLogicReference(reference, targets, report)
|
|
259
262
|
else if (place.slice !== undefined) checkSliceReference(place, reference, targets, report)
|
|
260
263
|
}
|
|
@@ -293,7 +296,7 @@ const dataAccess = (node: AstNode): string | undefined => {
|
|
|
293
296
|
|
|
294
297
|
/** The architecture's hints at one node: a handler outside `endpoints.ts`, data access outside `endpoints.ts`/`queries.ts`. */
|
|
295
298
|
export const checkStructureNode = (place: StructurePlace, node: AstNode, report: Report): void => {
|
|
296
|
-
if (place.exempt) return
|
|
299
|
+
if (place.exempt || place.generated) return
|
|
297
300
|
const handle = handledProcedure(node)?.handle
|
|
298
301
|
if (handle !== undefined && place.part !== 'endpoints') {
|
|
299
302
|
report(
|
package/src/rules/types.ts
CHANGED
|
@@ -17,6 +17,7 @@ const admitsAnyString = (type: AstNode): boolean => {
|
|
|
17
17
|
const referencedName = (typeName: AstNode | undefined): string | undefined =>
|
|
18
18
|
typeName?.type === 'TSQualifiedName' ? identifierName(child(typeName, 'right')) : identifierName(typeName)
|
|
19
19
|
|
|
20
|
+
/** `type-assertion` and `any-type`: how a module's own source is written, which a generated module is not held to. */
|
|
20
21
|
export const checkTypeNode = (node: AstNode, report: Report): void => {
|
|
21
22
|
switch (node.type) {
|
|
22
23
|
case 'TSAsExpression':
|
|
@@ -27,22 +28,22 @@ export const checkTypeNode = (node: AstNode, report: Report): void => {
|
|
|
27
28
|
return
|
|
28
29
|
case 'TSAnyKeyword':
|
|
29
30
|
report(node, 'any-type', '`any` switches type checking off', 'the concrete type, or `unknown` narrowed by a check')
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export const checkCapabilityNode = (node: AstNode, report: Report): void => {
|
|
35
|
+
if (node.type !== 'TSTypeReference') return
|
|
36
|
+
const name = referencedName(child(node, 'typeName'))
|
|
37
|
+
if (name === undefined || !CAPABILITIES.has(name)) return
|
|
38
|
+
const typeArguments = child(node, 'typeArguments')
|
|
39
|
+
const parameters = typeArguments === undefined ? [] : children(typeArguments, 'params')
|
|
40
|
+
if (parameters.some((parameter) => parameter !== null && admitsAnyString(parameter))) {
|
|
41
|
+
report(
|
|
42
|
+
node,
|
|
43
|
+
'capability-widening',
|
|
44
|
+
`${name}<string, …> widens a capability to any dimension or action`,
|
|
45
|
+
`${name}<'<dimension>', '<action>'> with literal names`,
|
|
46
|
+
)
|
|
46
47
|
}
|
|
47
48
|
}
|
|
48
49
|
|
package/src/source-maps.ts
CHANGED
|
@@ -144,6 +144,8 @@ export async function uploadSourceMaps(
|
|
|
144
144
|
},
|
|
145
145
|
body: artifact.body,
|
|
146
146
|
signal: AbortSignal.timeout(UPLOAD_TIMEOUT_MS),
|
|
147
|
+
// The bearer and the map must never reach a redirect target; a 3xx is not ok below.
|
|
148
|
+
redirect: 'manual',
|
|
147
149
|
})
|
|
148
150
|
if (response.ok) uploaded++
|
|
149
151
|
else incomplete = true
|
package/types/src/finding.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { AstNode, Position } from './ast';
|
|
2
2
|
/** Rules whose finding fails the check: the framework's forms and platform integrity. */
|
|
3
|
-
export declare const REFUSAL_RULE_IDS: readonly ["parse-error", "literal", "contract", "forbidden-import", "test-only-import", "unchecked-import", "forbidden-global", "raw-sql", "type-assertion", "any-type", "suppression-comment", "capability-widening", "prototype-assignment", "framework-write", "env-read", "public-env", "cross-app-fetch", "url-endpoint", "on-conflict-index", "access-view-key", "key-literal", "client-identity", "sensitive-log", "unsafe-html", "dynamic-code", "migration-name", "migration-refused", "backup-schema", "migration-edited", "file-column-migration", "
|
|
3
|
+
export declare const REFUSAL_RULE_IDS: readonly ["parse-error", "literal", "contract", "forbidden-import", "test-only-import", "unchecked-import", "forbidden-global", "raw-sql", "type-assertion", "any-type", "suppression-comment", "capability-widening", "prototype-assignment", "framework-write", "env-read", "public-env", "cross-app-fetch", "url-endpoint", "on-conflict-index", "access-view-key", "key-literal", "client-identity", "sensitive-log", "unsafe-html", "dynamic-code", "migration-name", "migration-refused", "backup-schema", "migration-edited", "file-column-migration", "package-replacement", "skipped-test", "test-without-assertion", "mock-module", "spa-server-import", "layer-import", "shared-import", "tool-import", "cloudflare-scan", "cloudflare-job-segment", "cloudflare-current-timestamp", "cloudflare-framework-name", "cloudflare-view-trigger"];
|
|
4
4
|
/** Rules that report a step out of the application architecture. */
|
|
5
5
|
export declare const STRUCTURE_HINT_RULE_IDS: readonly ["logic-data-access", "cross-slice-queries", "handler-location", "data-access-location"];
|
|
6
6
|
/**
|
|
@@ -9,7 +9,7 @@ export declare const STRUCTURE_HINT_RULE_IDS: readonly ["logic-data-access", "cr
|
|
|
9
9
|
* Cloudflare mutation that waits for an outside call inside the object's transaction.
|
|
10
10
|
*/
|
|
11
11
|
export declare const HINT_RULE_IDS: readonly ["logic-data-access", "cross-slice-queries", "handler-location", "data-access-location", "sql-undeclared", "member-admits-external", "cloudflare-held-call"];
|
|
12
|
-
export declare const RULE_IDS: readonly ["parse-error", "literal", "contract", "forbidden-import", "test-only-import", "unchecked-import", "forbidden-global", "raw-sql", "type-assertion", "any-type", "suppression-comment", "capability-widening", "prototype-assignment", "framework-write", "env-read", "public-env", "cross-app-fetch", "url-endpoint", "on-conflict-index", "access-view-key", "key-literal", "client-identity", "sensitive-log", "unsafe-html", "dynamic-code", "migration-name", "migration-refused", "backup-schema", "migration-edited", "file-column-migration", "
|
|
12
|
+
export declare const RULE_IDS: readonly ["parse-error", "literal", "contract", "forbidden-import", "test-only-import", "unchecked-import", "forbidden-global", "raw-sql", "type-assertion", "any-type", "suppression-comment", "capability-widening", "prototype-assignment", "framework-write", "env-read", "public-env", "cross-app-fetch", "url-endpoint", "on-conflict-index", "access-view-key", "key-literal", "client-identity", "sensitive-log", "unsafe-html", "dynamic-code", "migration-name", "migration-refused", "backup-schema", "migration-edited", "file-column-migration", "package-replacement", "skipped-test", "test-without-assertion", "mock-module", "spa-server-import", "layer-import", "shared-import", "tool-import", "cloudflare-scan", "cloudflare-job-segment", "cloudflare-current-timestamp", "cloudflare-framework-name", "cloudflare-view-trigger", "logic-data-access", "cross-slice-queries", "handler-location", "data-access-location", "sql-undeclared", "member-admits-external", "cloudflare-held-call"];
|
|
13
13
|
export type RefusalRuleId = (typeof REFUSAL_RULE_IDS)[number];
|
|
14
14
|
export type HintRuleId = (typeof HINT_RULE_IDS)[number];
|
|
15
15
|
export type RuleId = (typeof RULE_IDS)[number];
|
package/types/src/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export { type ApplicationTree, checkApplication, type CheckInput, readApplicationTree } from './check';
|
|
2
2
|
export { type ApplicationDialect, applicationDialect } from './dialect';
|
|
3
3
|
export { type Finding, formatFinding, RULE_IDS, type RuleId } from './finding';
|
|
4
|
-
export { CONFIG_FILE, CONTRACT_FILE, GENERATED_ENTRY_FILE, MIGRATIONS_DIR, TABLES_FILE
|
|
4
|
+
export { CONFIG_FILE, CONTRACT_FILE, GENERATED_ENTRY_FILE, MIGRATIONS_DIR, TABLES_FILE } from './layout';
|
|
5
5
|
export { readMigrationBase } from './migration-base';
|
|
6
6
|
export { type PackedSet, type PackedTarball, readPackedSet } from './packed';
|
|
7
7
|
export type { MigrationBase } from './rules/migration-history';
|
package/types/src/layout.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { type ParsedModule } from './ast';
|
|
1
2
|
/** Where an application keeps what the checker reads. Paths are relative to the application root. */
|
|
2
3
|
export declare const CONTRACT_FILE = "fabrika.contract.ts";
|
|
3
4
|
/** The provider-authored application config, whose `defineApp` names the provider. */
|
|
@@ -11,7 +12,6 @@ export declare const GENERATED_ENTRY_FILE = "fabrika/entry.generated.ts";
|
|
|
11
12
|
/** Where a Cloudflare deploy stages the SPA mounts' built files (`@fabrika/provider-cloudflare`): build output, never source. */
|
|
12
13
|
export declare const STAGED_ASSETS_DIR = "fabrika/assets/";
|
|
13
14
|
export declare const MIGRATIONS_DIR = "db/migrations/";
|
|
14
|
-
export declare const ZEROPS_FILES: readonly ["zerops.yaml", "zerops.yml"];
|
|
15
15
|
/** The application architecture (docs/reference/application-architecture.md): where each layer's code lives. */
|
|
16
16
|
export declare const VOCABULARY_FILE = "src/vocabulary.ts";
|
|
17
17
|
export declare const APP_KIT_FILE = "src/app-kit.ts";
|
|
@@ -48,6 +48,12 @@ export declare const isServerModule: (path: string) => boolean;
|
|
|
48
48
|
/** A module under `tools/`, tests included. */
|
|
49
49
|
export declare const isToolModule: (path: string) => boolean;
|
|
50
50
|
export declare const isJsonFile: (path: string) => boolean;
|
|
51
|
+
/**
|
|
52
|
+
* A module a generator writes: named `<name>.gen.<ext>` or `<name>.generated.<ext>`, opening with a comment on its first
|
|
53
|
+
* line, before any statement, that says so (`@generated` or `auto-generated`, in any case). The rules about how source
|
|
54
|
+
* is written do not apply to it; every rule about what it does still does.
|
|
55
|
+
*/
|
|
56
|
+
export declare const isGeneratedModule: (path: string, module: Pick<ParsedModule, "program" | "comments">) => boolean;
|
|
51
57
|
/** Whether the checker reads this file at all. */
|
|
52
58
|
export declare const isCheckedFile: (path: string) => boolean;
|
|
53
59
|
/** Joins a relative specifier onto the importing file's directory; `undefined` when it climbs above the root. */
|
|
@@ -12,8 +12,10 @@ export interface StructurePlace {
|
|
|
12
12
|
readonly part: SlicePart | undefined;
|
|
13
13
|
/** A test or the generated entrypoint: the structure hints do not apply to it. */
|
|
14
14
|
readonly exempt: boolean;
|
|
15
|
+
/** A generated module (`isGeneratedModule`): the hints do not apply to it, the refusals do. */
|
|
16
|
+
readonly generated: boolean;
|
|
15
17
|
}
|
|
16
|
-
export declare const structurePlace: (file: string, isGeneratedEntry: boolean) => StructurePlace;
|
|
18
|
+
export declare const structurePlace: (file: string, isGeneratedEntry: boolean, generated: boolean) => StructurePlace;
|
|
17
19
|
/** What the structure rules read beyond one module: every module's references, and how Bun resolves them. */
|
|
18
20
|
export interface StructureGraph {
|
|
19
21
|
readonly files: ReadonlyMap<string, string>;
|
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { type AstNode, type Comment } from '../ast';
|
|
2
2
|
import type { Report } from '../finding';
|
|
3
|
+
/** `type-assertion` and `any-type`: how a module's own source is written, which a generated module is not held to. */
|
|
3
4
|
export declare const checkTypeNode: (node: AstNode, report: Report) => void;
|
|
5
|
+
export declare const checkCapabilityNode: (node: AstNode, report: Report) => void;
|
|
4
6
|
export declare const checkComments: (comments: readonly Comment[], report: Report) => void;
|
package/src/rules/crontab.ts
DELETED
|
@@ -1,147 +0,0 @@
|
|
|
1
|
-
import { z } from 'zod/v4'
|
|
2
|
-
import { type AstNode, child, children, type ParsedModule, staticKey, walk } from '../ast'
|
|
3
|
-
import type { Finding } from '../finding'
|
|
4
|
-
import { type Located, type ModuleGraph, unwrapValue } from '../graph'
|
|
5
|
-
|
|
6
|
-
export interface ContractJob {
|
|
7
|
-
readonly name: string
|
|
8
|
-
/** `undefined` when the contract's schedule is not a static string. */
|
|
9
|
-
readonly schedule: string | undefined
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Every `<name>: c.cron('<schedule>', { … })` in the contract, and every `<name>` there that names a const of the
|
|
14
|
-
* kit's `app.cron('<schedule>', { … })`; the schedule is resolved through literal consts.
|
|
15
|
-
*/
|
|
16
|
-
export const contractJobs = (contract: ParsedModule, graph: ModuleGraph): readonly ContractJob[] => {
|
|
17
|
-
const jobs: ContractJob[] = []
|
|
18
|
-
const cronSchedule = (located: Located): string | undefined => {
|
|
19
|
-
const first = children(located.node, 'arguments')[0]
|
|
20
|
-
return first ? graph.resolveString({ module: located.module, node: first }) : undefined
|
|
21
|
-
}
|
|
22
|
-
walk(contract.program, (node, ancestors) => {
|
|
23
|
-
if (node.type === 'Property') {
|
|
24
|
-
const value = child(node, 'value')
|
|
25
|
-
const name = staticKey(node, 'key')
|
|
26
|
-
const resolved = value !== undefined && unwrapValue(value).type === 'Identifier'
|
|
27
|
-
? graph.resolveValue({ module: contract, node: value })
|
|
28
|
-
: undefined
|
|
29
|
-
if (name !== undefined && resolved !== undefined && isCronCall(resolved.node)) jobs.push({ name, schedule: cronSchedule(resolved) })
|
|
30
|
-
return
|
|
31
|
-
}
|
|
32
|
-
if (!isCronCall(node)) return
|
|
33
|
-
const property = [...ancestors].reverse().find((ancestor) => {
|
|
34
|
-
const value = ancestor.type === 'Property' ? child(ancestor, 'value') : undefined
|
|
35
|
-
return value !== undefined && unwrapValue(value) === node
|
|
36
|
-
})
|
|
37
|
-
const name = property === undefined ? undefined : staticKey(property, 'key')
|
|
38
|
-
if (name !== undefined) jobs.push({ name, schedule: cronSchedule({ module: contract, node }) })
|
|
39
|
-
})
|
|
40
|
-
return jobs
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
const isCronCall = (node: AstNode): boolean => {
|
|
44
|
-
const callee = node.type === 'CallExpression' ? child(node, 'callee') : undefined
|
|
45
|
-
return callee?.type === 'MemberExpression' && staticKey(callee, 'property') === 'cron'
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
const crontabEntrySchema = z.object({
|
|
49
|
-
timing: z.string().optional(),
|
|
50
|
-
command: z.string().optional(),
|
|
51
|
-
allContainers: z.boolean().optional(),
|
|
52
|
-
})
|
|
53
|
-
|
|
54
|
-
const zeropsYamlSchema = z.object({
|
|
55
|
-
zerops: z.array(
|
|
56
|
-
z.object({
|
|
57
|
-
setup: z.string(),
|
|
58
|
-
run: z.object({ start: z.string().optional(), crontab: z.array(crontabEntrySchema).optional() }).optional(),
|
|
59
|
-
}),
|
|
60
|
-
),
|
|
61
|
-
})
|
|
62
|
-
|
|
63
|
-
const JOB_COMMAND = /^fabrika job run (\S+)$/
|
|
64
|
-
/** A lone `&`, `nohup`, `setsid` or `disown` starts a process beside the server, outside every job rule. */
|
|
65
|
-
const BACKGROUND_PROCESS = /(?:^|[^&])&(?:[^&]|$)|\b(?:nohup|setsid|disown)\b/
|
|
66
|
-
|
|
67
|
-
/** The line of the first occurrence of `needle`, or 1. */
|
|
68
|
-
const lineOf = (text: string, needle: string, after = 0): number => {
|
|
69
|
-
const index = text.indexOf(needle, after)
|
|
70
|
-
return index === -1 ? 1 : text.slice(0, index).split('\n').length
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
/** Each setup that runs the application carries exactly the contract's jobs as crontab entries, and nothing else. */
|
|
74
|
-
export const checkCrontab = (file: string, text: string, jobs: readonly ContractJob[]): Finding[] => {
|
|
75
|
-
const findings: Finding[] = []
|
|
76
|
-
const finding = (line: number, message: string, allowed: string) =>
|
|
77
|
-
findings.push({ file, line, column: 1, severity: 'refusal', rule: 'crontab', message, allowed })
|
|
78
|
-
let document: unknown
|
|
79
|
-
try {
|
|
80
|
-
document = Bun.YAML.parse(text)
|
|
81
|
-
} catch {
|
|
82
|
-
finding(1, `${file} is not valid YAML`, 'a zerops.yaml that parses')
|
|
83
|
-
return findings
|
|
84
|
-
}
|
|
85
|
-
const parsed = zeropsYamlSchema.safeParse(document)
|
|
86
|
-
if (!parsed.success) {
|
|
87
|
-
const issue = parsed.error.issues[0]
|
|
88
|
-
finding(
|
|
89
|
-
1,
|
|
90
|
-
`${file} does not have the zerops.yaml shape at '${issue?.path.join('.') ?? ''}'`,
|
|
91
|
-
'zerops: [{ setup, run: { start, crontab: [{ timing, command, allContainers }] } }]',
|
|
92
|
-
)
|
|
93
|
-
return findings
|
|
94
|
-
}
|
|
95
|
-
const byName = new Map(jobs.map((job) => [job.name, job]))
|
|
96
|
-
for (const setup of parsed.data.zerops) {
|
|
97
|
-
const { run } = setup
|
|
98
|
-
if (run === undefined) continue
|
|
99
|
-
const setupLine = lineOf(text, `setup: ${setup.setup}`)
|
|
100
|
-
const crontabLine = lineOf(text, 'crontab:', text.indexOf(`setup: ${setup.setup}`))
|
|
101
|
-
if (run.start !== undefined && BACKGROUND_PROCESS.test(run.start)) {
|
|
102
|
-
finding(
|
|
103
|
-
lineOf(text, run.start),
|
|
104
|
-
`run.start of '${setup.setup}' starts a background process`,
|
|
105
|
-
'one server process in run.start; background work is a job in the contract (c.cron)',
|
|
106
|
-
)
|
|
107
|
-
}
|
|
108
|
-
const scheduled = new Set<string>()
|
|
109
|
-
for (const entry of run.crontab ?? []) {
|
|
110
|
-
const line = entry.command === undefined ? crontabLine : lineOf(text, entry.command)
|
|
111
|
-
const name = JOB_COMMAND.exec(entry.command ?? '')?.[1]
|
|
112
|
-
const job = name === undefined ? undefined : byName.get(name)
|
|
113
|
-
if (job === undefined) {
|
|
114
|
-
finding(
|
|
115
|
-
line,
|
|
116
|
-
`crontab entry '${entry.command ?? ''}' is not a job of the contract`,
|
|
117
|
-
'fabrika job run <name> for a c.cron job declared in fabrika.contract.ts',
|
|
118
|
-
)
|
|
119
|
-
continue
|
|
120
|
-
}
|
|
121
|
-
if (scheduled.has(job.name)) finding(line, `job '${job.name}' has a second crontab entry`, 'one crontab entry per job')
|
|
122
|
-
scheduled.add(job.name)
|
|
123
|
-
if (job.schedule === undefined) {
|
|
124
|
-
finding(line, `the contract's schedule of job '${job.name}' is not a static string`, `c.cron('<cron expression>', { … }) in fabrika.contract.ts`)
|
|
125
|
-
} else if (entry.timing !== job.schedule) {
|
|
126
|
-
finding(
|
|
127
|
-
line,
|
|
128
|
-
`crontab timing '${entry.timing ?? ''}' of job '${job.name}' differs from the contract's '${job.schedule}'`,
|
|
129
|
-
`timing: '${job.schedule}'`,
|
|
130
|
-
)
|
|
131
|
-
}
|
|
132
|
-
if (entry.allContainers !== false) {
|
|
133
|
-
finding(line, `crontab entry of job '${job.name}' does not set allContainers: false`, 'allContainers: false, written explicitly')
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
for (const job of jobs) {
|
|
137
|
-
if (!scheduled.has(job.name)) {
|
|
138
|
-
finding(
|
|
139
|
-
run.crontab === undefined ? setupLine : crontabLine,
|
|
140
|
-
`job '${job.name}' has no crontab entry in '${setup.setup}'`,
|
|
141
|
-
`{ timing: '${job.schedule ?? ''}', command: 'fabrika job run ${job.name}', allContainers: false }`,
|
|
142
|
-
)
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
return findings
|
|
147
|
-
}
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { type ParsedModule } from '../ast';
|
|
2
|
-
import type { Finding } from '../finding';
|
|
3
|
-
import { type ModuleGraph } from '../graph';
|
|
4
|
-
export interface ContractJob {
|
|
5
|
-
readonly name: string;
|
|
6
|
-
/** `undefined` when the contract's schedule is not a static string. */
|
|
7
|
-
readonly schedule: string | undefined;
|
|
8
|
-
}
|
|
9
|
-
/**
|
|
10
|
-
* Every `<name>: c.cron('<schedule>', { … })` in the contract, and every `<name>` there that names a const of the
|
|
11
|
-
* kit's `app.cron('<schedule>', { … })`; the schedule is resolved through literal consts.
|
|
12
|
-
*/
|
|
13
|
-
export declare const contractJobs: (contract: ParsedModule, graph: ModuleGraph) => readonly ContractJob[];
|
|
14
|
-
/** Each setup that runs the application carries exactly the contract's jobs as crontab entries, and nothing else. */
|
|
15
|
-
export declare const checkCrontab: (file: string, text: string, jobs: readonly ContractJob[]) => Finding[];
|