@fabrika/check 0.9.0-dev.2 → 0.9.0-dev.20
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 +7 -7
- package/src/ast.ts +69 -6
- package/src/check.ts +135 -32
- package/src/cli.ts +23 -4
- package/src/contract.ts +42 -6
- package/src/dialect.ts +29 -0
- package/src/finding.ts +14 -6
- package/src/index.ts +4 -12
- package/src/inputs.ts +4 -2
- package/src/layout.ts +21 -10
- package/src/limits.ts +13 -3
- package/src/migration-base.ts +85 -0
- package/src/reader/schema.ts +42 -512
- package/src/reader/surface.ts +43 -5
- package/src/reader/value.ts +18 -6
- package/src/resolution.ts +2 -2
- package/src/rules/cloudflare.ts +59 -0
- package/src/rules/compile.ts +32 -0
- package/src/rules/framework.ts +2 -2
- package/src/rules/literal.ts +3 -1
- package/src/rules/migration-history.ts +70 -0
- package/src/rules/migrations.ts +248 -9
- package/src/rules/modules.ts +9 -3
- package/src/rules/platform-migrations.ts +100 -0
- package/src/rules/runtime.ts +45 -14
- package/src/rules/structure.ts +32 -15
- package/src/rules/tests.ts +9 -60
- package/src/trust.ts +8 -4
- package/types/src/ast.d.ts +6 -0
- package/types/src/check.d.ts +3 -0
- package/types/src/contract.d.ts +1 -0
- package/types/src/dialect.d.ts +8 -0
- package/types/src/finding.d.ts +6 -5
- package/types/src/index.d.ts +4 -3
- package/types/src/layout.d.ts +18 -9
- package/types/src/limits.d.ts +12 -2
- package/types/src/migration-base.d.ts +9 -0
- 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 +9 -0
- package/types/src/rules/compile.d.ts +7 -0
- package/types/src/rules/framework.d.ts +1 -1
- package/types/src/rules/migration-history.d.ts +12 -0
- package/types/src/rules/migrations.d.ts +28 -0
- package/types/src/rules/platform-migrations.d.ts +15 -0
- package/types/src/rules/runtime.d.ts +7 -2
- package/types/src/rules/structure.d.ts +8 -3
- package/types/src/rules/tests.d.ts +6 -14
- package/src/endpoint-fixtures.ts +0 -221
- package/src/rules/isolation.ts +0 -79
- package/src/scaffold-sample.ts +0 -111
- package/src/scaffold.ts +0 -650
- package/types/src/endpoint-fixtures.d.ts +0 -36
- package/types/src/rules/isolation.d.ts +0 -18
- package/types/src/scaffold-sample.d.ts +0 -8
- package/types/src/scaffold.d.ts +0 -28
package/src/finding.ts
CHANGED
|
@@ -22,34 +22,42 @@ export const REFUSAL_RULE_IDS = [
|
|
|
22
22
|
'cross-app-fetch',
|
|
23
23
|
'url-endpoint',
|
|
24
24
|
'on-conflict-index',
|
|
25
|
+
'access-view-key',
|
|
25
26
|
'key-literal',
|
|
26
27
|
'client-identity',
|
|
27
28
|
'sensitive-log',
|
|
28
29
|
'unsafe-html',
|
|
29
30
|
'dynamic-code',
|
|
30
31
|
'migration-name',
|
|
32
|
+
'migration-refused',
|
|
33
|
+
'backup-schema',
|
|
34
|
+
'migration-edited',
|
|
31
35
|
'file-column-migration',
|
|
32
36
|
'crontab',
|
|
33
37
|
'package-replacement',
|
|
34
38
|
'skipped-test',
|
|
35
39
|
'test-without-assertion',
|
|
36
|
-
'generated-test-hash',
|
|
37
40
|
'mock-module',
|
|
38
|
-
'endpoint-fixture',
|
|
39
41
|
'spa-server-import',
|
|
40
42
|
'layer-import',
|
|
41
|
-
'generated-test-import',
|
|
42
43
|
'shared-import',
|
|
44
|
+
'tool-import',
|
|
45
|
+
'cloudflare-scan',
|
|
46
|
+
'cloudflare-job-segment',
|
|
47
|
+
'cloudflare-current-timestamp',
|
|
48
|
+
'cloudflare-framework-name',
|
|
49
|
+
'cloudflare-view-trigger',
|
|
43
50
|
] as const
|
|
44
51
|
|
|
45
52
|
/** Rules that report a step out of the application architecture. */
|
|
46
53
|
export const STRUCTURE_HINT_RULE_IDS = ['logic-data-access', 'cross-slice-queries', 'handler-location', 'data-access-location'] as const
|
|
47
54
|
|
|
48
55
|
/**
|
|
49
|
-
* Rules whose findings never fail the check: the architecture's, a `ctx.sql` its site does not declare,
|
|
50
|
-
* `{ member: true }` endpoint of a contract whose external role admits people from outside the organization
|
|
56
|
+
* Rules whose findings never fail the check: the architecture's, a `ctx.sql` its site does not declare, a
|
|
57
|
+
* `{ member: true }` endpoint of a contract whose external role admits people from outside the organization, and a
|
|
58
|
+
* Cloudflare mutation that waits for an outside call inside the object's transaction.
|
|
51
59
|
*/
|
|
52
|
-
export const HINT_RULE_IDS = [...STRUCTURE_HINT_RULE_IDS, 'sql-undeclared', 'member-admits-external'] as const
|
|
60
|
+
export const HINT_RULE_IDS = [...STRUCTURE_HINT_RULE_IDS, 'sql-undeclared', 'member-admits-external', 'cloudflare-held-call'] as const
|
|
53
61
|
|
|
54
62
|
export const RULE_IDS = [...REFUSAL_RULE_IDS, ...HINT_RULE_IDS] as const
|
|
55
63
|
|
package/src/index.ts
CHANGED
|
@@ -1,15 +1,7 @@
|
|
|
1
1
|
export { type ApplicationTree, checkApplication, type CheckInput, readApplicationTree } from './check'
|
|
2
|
+
export { type ApplicationDialect, applicationDialect } from './dialect'
|
|
2
3
|
export { type Finding, formatFinding, RULE_IDS, type RuleId } from './finding'
|
|
3
|
-
export { CONTRACT_FILE, GENERATED_ENTRY_FILE, MIGRATIONS_DIR, TABLES_FILE, ZEROPS_FILES } from './layout'
|
|
4
|
+
export { CONFIG_FILE, CONTRACT_FILE, GENERATED_ENTRY_FILE, MIGRATIONS_DIR, TABLES_FILE, ZEROPS_FILES } from './layout'
|
|
5
|
+
export { readMigrationBase } from './migration-base'
|
|
4
6
|
export { type PackedSet, type PackedTarball, readPackedSet } from './packed'
|
|
5
|
-
export {
|
|
6
|
-
export {
|
|
7
|
-
ENDPOINT_ACCESS_TEST,
|
|
8
|
-
ENDPOINT_FIXTURES_FILE,
|
|
9
|
-
type GeneratedTest,
|
|
10
|
-
generatedTests,
|
|
11
|
-
missingEndpointFixtures,
|
|
12
|
-
POLICY_TESTS_DIR,
|
|
13
|
-
policyTestPath,
|
|
14
|
-
scaffoldInputs,
|
|
15
|
-
} from './scaffold'
|
|
7
|
+
export type { MigrationBase } from './rules/migration-history'
|
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
|
@@ -1,19 +1,16 @@
|
|
|
1
1
|
/** Where an application keeps what the checker reads. Paths are relative to the application root. */
|
|
2
2
|
export const CONTRACT_FILE = 'fabrika.contract.ts'
|
|
3
|
+
/** The provider-authored application config, whose `defineApp` names the provider. */
|
|
4
|
+
export const CONFIG_FILE = 'fabrika.config.ts'
|
|
3
5
|
/** The named export of the contract module that holds `defineContract(…)`. */
|
|
4
6
|
export const CONTRACT_EXPORT = 'contract'
|
|
5
7
|
export const TABLES_FILE = 'src/db/tables.ts'
|
|
6
8
|
/** The named export of the tables module that holds `defineTables(…)(…)`. */
|
|
7
9
|
export const TABLES_EXPORT = 'tables'
|
|
8
10
|
export const GENERATED_ENTRY_FILE = 'fabrika/entry.generated.ts'
|
|
11
|
+
/** Where a Cloudflare deploy stages the SPA mounts' built files (`@fabrika/provider-cloudflare`): build output, never source. */
|
|
12
|
+
export const STAGED_ASSETS_DIR = 'fabrika/assets/'
|
|
9
13
|
export const MIGRATIONS_DIR = 'db/migrations/'
|
|
10
|
-
/** Where `fabrika db test-scaffold` writes the policy isolation tests, `<table>.policy-isolation.test.ts`. */
|
|
11
|
-
export const POLICY_TESTS_DIR = 'src/db/__tests__/'
|
|
12
|
-
export const POLICY_TEST_SUFFIX = '.policy-isolation.test.ts'
|
|
13
|
-
/** The generated endpoint access tests; they import `src/app.ts`, whose default export they boot. */
|
|
14
|
-
export const ENDPOINT_ACCESS_TEST = 'src/__tests__/endpoint-access.test.ts'
|
|
15
|
-
/** The inputs the author gives the endpoint access tests where the scaffold cannot sample one; read, never run. */
|
|
16
|
-
export const ENDPOINT_FIXTURES_FILE = 'src/__tests__/endpoint-access.fixtures.ts'
|
|
17
14
|
export const ZEROPS_FILES = ['zerops.yaml', 'zerops.yml'] as const
|
|
18
15
|
|
|
19
16
|
/** The application architecture (docs/reference/application-architecture.md): where each layer's code lives. */
|
|
@@ -22,9 +19,12 @@ export const APP_KIT_FILE = 'src/app-kit.ts'
|
|
|
22
19
|
/** The shared zod schemas the shell and the SPA import: one module, or a directory of them once it grows. */
|
|
23
20
|
export const SCHEMAS_FILE = 'src/schemas.ts'
|
|
24
21
|
export const SCHEMAS_DIR = 'src/schemas/'
|
|
25
|
-
/**
|
|
22
|
+
/**
|
|
23
|
+
* Pure code the SPA and every slice may import: it loads nothing but `zod/v4`, `@fabrika/app/schema` and other modules of
|
|
24
|
+
* its own directory.
|
|
25
|
+
*/
|
|
26
26
|
export const SHARED_DIR = 'src/shared/'
|
|
27
|
-
/** `src/db/`: the tables
|
|
27
|
+
/** `src/db/`: the tables and the generated schema; never a slice. */
|
|
28
28
|
export const DATABASE_DIR = 'src/db/'
|
|
29
29
|
/** A slice is a directory `src/<slice>/` that holds these modules, each optional. */
|
|
30
30
|
export const SLICE_ENDPOINTS_FILE = 'endpoints.ts'
|
|
@@ -33,6 +33,11 @@ export const SLICE_QUERIES_FILE = 'queries.ts'
|
|
|
33
33
|
export const SPA_DIR = 'web/'
|
|
34
34
|
/** The server half of an application: its modules run in the application's process. */
|
|
35
35
|
export const SERVER_DIR = 'src/'
|
|
36
|
+
/**
|
|
37
|
+
* A developer's tools, such as a command line over the contract: code that runs on a developer's machine and never in
|
|
38
|
+
* the application, so the rules for code that runs there do not apply, and no module outside it may load one.
|
|
39
|
+
*/
|
|
40
|
+
export const TOOLS_DIR = 'tools/'
|
|
36
41
|
|
|
37
42
|
const SOURCE_EXTENSION = /\.(?:[mc]?[jt]s|[jt]sx)$/
|
|
38
43
|
const TEST_FILE = /(?:^|\/)__tests__\/|\.(?:test|spec)\.[mc]?[jt]sx?$/
|
|
@@ -40,6 +45,9 @@ const TEST_FILE = /(?:^|\/)__tests__\/|\.(?:test|spec)\.[mc]?[jt]sx?$/
|
|
|
40
45
|
/** Build output, installed packages and tool state are not application source and are never read. */
|
|
41
46
|
export const isIgnoredSegment = (segment: string): boolean => segment === 'node_modules' || segment === 'dist' || segment.startsWith('.')
|
|
42
47
|
|
|
48
|
+
/** Whether the checker skips `path` (relative to the root): under a skipped segment, or the staged SPA files. */
|
|
49
|
+
export const isSkippedPath = (path: string): boolean => path.split('/').some(isIgnoredSegment) || path.startsWith(STAGED_ASSETS_DIR)
|
|
50
|
+
|
|
43
51
|
export const isSourceFile = (path: string): boolean => SOURCE_EXTENSION.test(path)
|
|
44
52
|
|
|
45
53
|
export const isTestFile = (path: string): boolean => TEST_FILE.test(path)
|
|
@@ -47,13 +55,16 @@ export const isTestFile = (path: string): boolean => TEST_FILE.test(path)
|
|
|
47
55
|
/** A module that runs in the application's server process: under `src/`, not a test. */
|
|
48
56
|
export const isServerModule = (path: string): boolean => path.startsWith(SERVER_DIR) && !isTestFile(path)
|
|
49
57
|
|
|
58
|
+
/** A module under `tools/`, tests included. */
|
|
59
|
+
export const isToolModule = (path: string): boolean => path.startsWith(TOOLS_DIR)
|
|
60
|
+
|
|
50
61
|
export const isJsonFile = (path: string): boolean => path.endsWith('.json')
|
|
51
62
|
|
|
52
63
|
/** Whether the checker reads this file at all. */
|
|
53
64
|
export const isCheckedFile = (path: string): boolean => {
|
|
54
65
|
// The registry configuration and the lockfile decide what a trusted package name installs.
|
|
55
66
|
if (path === '.npmrc' || path === 'bun.lock') return true
|
|
56
|
-
if (path
|
|
67
|
+
if (isSkippedPath(path)) return false
|
|
57
68
|
return isSourceFile(path) || isJsonFile(path) || path.startsWith(MIGRATIONS_DIR) || ZEROPS_FILES.some((name) => name === path)
|
|
58
69
|
|| path === 'bunfig.toml'
|
|
59
70
|
}
|
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
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
// The migrations `migration-edited` compares the tree with: those of the merge base of HEAD and the remote's default
|
|
2
|
+
// branch, read with git. Only the bin reads them; a caller with the files of a commit (Control, a deploy build) has no
|
|
3
|
+
// history to compare with, and the runner's checksum stays the real check.
|
|
4
|
+
|
|
5
|
+
import { MIGRATIONS_DIR } from './layout'
|
|
6
|
+
import type { MigrationBase } from './rules/migration-history'
|
|
7
|
+
|
|
8
|
+
/** What the default base is looked up as, in order, when the bin is given no `--base`. */
|
|
9
|
+
const DEFAULT_REFS = ['origin/main', 'origin/master']
|
|
10
|
+
|
|
11
|
+
interface GitResult {
|
|
12
|
+
readonly ok: boolean
|
|
13
|
+
readonly stdout: Uint8Array
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const git = async (root: string, args: readonly string[], stdin?: string): Promise<GitResult> => {
|
|
17
|
+
const input = stdin === undefined ? 'ignore' : new TextEncoder().encode(stdin)
|
|
18
|
+
const child = Bun.spawn(['git', '-C', root, ...args], { stdin: input, stdout: 'pipe', stderr: 'ignore' })
|
|
19
|
+
const [code, stdout] = await Promise.all([child.exited, new Response(child.stdout).arrayBuffer()])
|
|
20
|
+
return { ok: code === 0, stdout: new Uint8Array(stdout) }
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const text = (bytes: Uint8Array): string => new TextDecoder().decode(bytes).trim()
|
|
24
|
+
|
|
25
|
+
/** The remote's default branch: `origin/HEAD` where the clone recorded it, else the first of `DEFAULT_REFS` that exists. */
|
|
26
|
+
const defaultRef = async (root: string): Promise<string | null> => {
|
|
27
|
+
const head = await git(root, ['symbolic-ref', '-q', '--short', 'refs/remotes/origin/HEAD'])
|
|
28
|
+
if (head.ok && text(head.stdout) !== '') return text(head.stdout)
|
|
29
|
+
for (const ref of DEFAULT_REFS) {
|
|
30
|
+
if ((await git(root, ['rev-parse', '--verify', '-q', `${ref}^{commit}`])).ok) return ref
|
|
31
|
+
}
|
|
32
|
+
return null
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Each regular file directly in `db/migrations/` at `commit`, by its blob id. */
|
|
36
|
+
const migrationBlobs = async (root: string, commit: string): Promise<Map<string, string> | null> => {
|
|
37
|
+
const listed = await git(root, ['ls-tree', '-z', commit, '--', MIGRATIONS_DIR])
|
|
38
|
+
if (!listed.ok) return null
|
|
39
|
+
const blobs = new Map<string, string>()
|
|
40
|
+
for (const entry of new TextDecoder().decode(listed.stdout).split('\0')) {
|
|
41
|
+
const match = /^(100644|100755) blob ([0-9a-f]+)\t(.+)$/.exec(entry)
|
|
42
|
+
if (match?.[2] !== undefined && match[3] !== undefined && match[3].endsWith('.sql')) blobs.set(match[3], match[2])
|
|
43
|
+
}
|
|
44
|
+
return blobs
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** The blobs' texts, through one `git cat-file --batch`: each answer is `<id> blob <size>\n<bytes>\n`. */
|
|
48
|
+
const blobTexts = async (root: string, blobs: ReadonlyMap<string, string>): Promise<Map<string, string> | null> => {
|
|
49
|
+
if (blobs.size === 0) return new Map()
|
|
50
|
+
const ids = [...blobs.values()]
|
|
51
|
+
const batch = await git(root, ['cat-file', '--batch'], ids.map((id) => `${id}\n`).join(''))
|
|
52
|
+
if (!batch.ok) return null
|
|
53
|
+
const decoder = new TextDecoder()
|
|
54
|
+
const texts = new Map<string, string>()
|
|
55
|
+
let offset = 0
|
|
56
|
+
for (const id of ids) {
|
|
57
|
+
const end = batch.stdout.indexOf(10, offset)
|
|
58
|
+
const header = /^([0-9a-f]+) blob (\d+)$/.exec(decoder.decode(batch.stdout.subarray(offset, end)))
|
|
59
|
+
if (end < 0 || header?.[1] !== id || header[2] === undefined) return null
|
|
60
|
+
const size = Number(header[2])
|
|
61
|
+
texts.set(id, decoder.decode(batch.stdout.subarray(end + 1, end + 1 + size)))
|
|
62
|
+
offset = end + 1 + size + 1
|
|
63
|
+
}
|
|
64
|
+
return new Map([...blobs].map(([path, id]) => [path, texts.get(id) ?? '']))
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* The migrations at the merge base of HEAD and `ref` (the remote's default branch when omitted), compared against
|
|
69
|
+
* the merge base so that migrations the default branch added since the branch forked are not read as deleted.
|
|
70
|
+
* `null` when `root` is in no git work tree; `{ skipped }` when it is but the base cannot be read.
|
|
71
|
+
*/
|
|
72
|
+
export const readMigrationBase = async (root: string, ref?: string): Promise<MigrationBase | { readonly skipped: string } | null> => {
|
|
73
|
+
if (!(await git(root, ['rev-parse', '--is-inside-work-tree'])).ok) return null
|
|
74
|
+
const named = ref ?? (await defaultRef(root))
|
|
75
|
+
if (named === null) return { skipped: `no origin/HEAD, ${DEFAULT_REFS.join(' or ')} to compare the migrations with; pass --base <ref>` }
|
|
76
|
+
if (named.startsWith('-') || !(await git(root, ['rev-parse', '--verify', '-q', `${named}^{commit}`])).ok) {
|
|
77
|
+
return { skipped: `${named} names no commit` }
|
|
78
|
+
}
|
|
79
|
+
const mergeBase = await git(root, ['merge-base', 'HEAD', named])
|
|
80
|
+
if (!mergeBase.ok) return { skipped: `HEAD and ${named} share no commit this clone holds` }
|
|
81
|
+
const blobs = await migrationBlobs(root, text(mergeBase.stdout))
|
|
82
|
+
const files = blobs === null ? null : await blobTexts(root, blobs)
|
|
83
|
+
if (files === null) return { skipped: `the migrations of ${named} cannot be read` }
|
|
84
|
+
return { ref: named, files }
|
|
85
|
+
}
|