@fabrika/check 0.9.0-dev.1 → 0.9.0-dev.10
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 +12 -11
- package/src/check.ts +58 -9
- package/src/cli.ts +23 -4
- package/src/dialect.ts +29 -0
- package/src/endpoint-fixtures.ts +99 -13
- package/src/finding.ts +12 -3
- package/src/index.ts +4 -1
- package/src/layout.ts +16 -1
- package/src/migration-base.ts +85 -0
- package/src/reader/surface.ts +15 -0
- package/src/reader/value.ts +1 -1
- package/src/resolution.ts +2 -2
- package/src/rules/cloudflare.ts +59 -0
- package/src/rules/framework.ts +2 -2
- package/src/rules/isolation.ts +5 -1
- package/src/rules/migration-history.ts +70 -0
- package/src/rules/migrations.ts +63 -0
- package/src/rules/modules.ts +3 -3
- package/src/rules/platform-migrations.ts +93 -0
- package/src/rules/runtime.ts +13 -7
- package/src/rules/structure.ts +26 -1
- package/src/rules/tests.ts +1 -1
- package/src/scaffold.ts +323 -61
- package/src/trust.ts +8 -4
- package/types/src/ast.d.ts +65 -0
- package/types/src/bun-plugin.d.ts +12 -0
- package/types/src/check.d.ts +22 -0
- package/types/src/cli.d.ts +2 -0
- package/types/src/contract.d.ts +58 -0
- package/types/src/dialect.d.ts +8 -0
- package/types/src/endpoint-fixtures.d.ts +54 -0
- package/types/src/finding.d.ts +39 -0
- package/types/src/graph.d.ts +68 -0
- package/types/src/index.d.ts +9 -0
- package/types/src/inputs.d.ts +9 -0
- package/types/src/jsonc.d.ts +2 -0
- package/types/src/layout.d.ts +76 -0
- package/types/src/limits.d.ts +15 -0
- package/types/src/lockfile.d.ts +19 -0
- package/types/src/migration-base.d.ts +9 -0
- package/types/src/packed.d.ts +15 -0
- package/types/src/reader/problems.d.ts +7 -0
- package/types/src/reader/schema.d.ts +18 -0
- package/types/src/reader/surface.d.ts +5 -0
- package/types/src/reader/tables.d.ts +6 -0
- package/types/src/reader/value.d.ts +113 -0
- package/types/src/references.d.ts +15 -0
- package/types/src/resolution.d.ts +20 -0
- package/types/src/rules/cloudflare.d.ts +9 -0
- package/types/src/rules/contract.d.ts +8 -0
- package/types/src/rules/crontab.d.ts +15 -0
- package/types/src/rules/framework.d.ts +19 -0
- package/types/src/rules/identity.d.ts +5 -0
- package/types/src/rules/isolation.d.ts +18 -0
- package/types/src/rules/json.d.ts +3 -0
- package/types/src/rules/literal.d.ts +63 -0
- package/types/src/rules/migration-history.d.ts +12 -0
- package/types/src/rules/migrations.d.ts +40 -0
- package/types/src/rules/modules.d.ts +12 -0
- package/types/src/rules/packages.d.ts +7 -0
- package/types/src/rules/platform-migrations.d.ts +13 -0
- package/types/src/rules/runtime.d.ts +34 -0
- package/types/src/rules/structure.d.ts +41 -0
- package/types/src/rules/tests.d.ts +22 -0
- package/types/src/rules/types.d.ts +4 -0
- package/types/src/scaffold-sample.d.ts +8 -0
- package/types/src/scaffold.d.ts +36 -0
- package/types/src/scope.d.ts +6 -0
- package/types/src/slice.d.ts +30 -0
- package/types/src/source-maps-cli.d.ts +4 -0
- package/types/src/source-maps.d.ts +32 -0
- package/types/src/toml.d.ts +30 -0
- package/types/src/trust.d.ts +15 -0
- package/types/src/version.d.ts +2 -0
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/** An ESTree node as `@babel/parser` returns it with the `estree` plugin, read structurally. */
|
|
2
|
+
export interface AstNode {
|
|
3
|
+
readonly type: string;
|
|
4
|
+
readonly [key: string]: unknown;
|
|
5
|
+
}
|
|
6
|
+
export interface Position {
|
|
7
|
+
readonly line: number;
|
|
8
|
+
readonly column: number;
|
|
9
|
+
}
|
|
10
|
+
export interface Comment {
|
|
11
|
+
readonly text: string;
|
|
12
|
+
readonly position: Position;
|
|
13
|
+
}
|
|
14
|
+
export interface ParsedModule {
|
|
15
|
+
readonly file: string;
|
|
16
|
+
readonly program: AstNode;
|
|
17
|
+
readonly comments: readonly Comment[];
|
|
18
|
+
/** The UTF-8 size of the source text. */
|
|
19
|
+
readonly bytes: number;
|
|
20
|
+
}
|
|
21
|
+
export type ParseResult = {
|
|
22
|
+
readonly ok: true;
|
|
23
|
+
readonly module: ParsedModule;
|
|
24
|
+
} | {
|
|
25
|
+
readonly ok: false;
|
|
26
|
+
readonly message: string;
|
|
27
|
+
readonly position: Position;
|
|
28
|
+
};
|
|
29
|
+
export declare const isNode: (value: unknown) => value is AstNode;
|
|
30
|
+
/** Whether the source wraps a node in parentheses, which the parser records beside the node rather than as one. */
|
|
31
|
+
export declare const isParenthesized: (node: AstNode) => boolean;
|
|
32
|
+
export declare const child: (node: AstNode, key: string) => AstNode | undefined;
|
|
33
|
+
/** The nodes of an array property; holes (`[a, , b]`) come back as `null`. */
|
|
34
|
+
export declare const children: (node: AstNode, key: string) => readonly (AstNode | null)[];
|
|
35
|
+
export declare const stringField: (node: AstNode, key: string) => string | undefined;
|
|
36
|
+
/** The name of an `Identifier`, or `undefined` for any other node. */
|
|
37
|
+
export declare const identifierName: (node: AstNode | undefined) => string | undefined;
|
|
38
|
+
/** The text of a node whose value is a fixed string: a string `Literal`, or a template with no `${…}`. */
|
|
39
|
+
export declare const staticString: (node: AstNode | undefined) => string | undefined;
|
|
40
|
+
/** A `TemplateElement`'s source text and its decoded value (`undefined` for an invalid escape in a tagged template). */
|
|
41
|
+
export declare const templateText: (element: AstNode) => {
|
|
42
|
+
readonly raw: string;
|
|
43
|
+
readonly cooked: string | undefined;
|
|
44
|
+
};
|
|
45
|
+
/** A property or member key read statically: `a.b`, `a['b']`, ``a[`b`]``, `{ b: … }`, `{ 'b': … }`. */
|
|
46
|
+
export declare const staticKey: (node: AstNode, keyField: "property" | "key") => string | undefined;
|
|
47
|
+
/** The static property names at the end of a member chain, whatever its root: `load().db.sql.raw` → `['db', 'sql', 'raw']`. */
|
|
48
|
+
export declare const memberSuffix: (node: AstNode) => readonly string[];
|
|
49
|
+
/** `as const` and `<const>x` fix a literal's type; they assert nothing. */
|
|
50
|
+
export declare const isConstAssertion: (node: AstNode) => boolean;
|
|
51
|
+
/** Strips the wrappers that do not change which value an expression denotes. */
|
|
52
|
+
export declare const unwrap: (node: AstNode) => AstNode;
|
|
53
|
+
/** `a.b.c` as `['a', 'b', 'c']`; `undefined` when a link is computed or the root is not an identifier. */
|
|
54
|
+
export declare const memberPath: (node: AstNode) => readonly string[] | undefined;
|
|
55
|
+
/** Whether an `Identifier` denotes a runtime value here, rather than a property name, a key, a label or a type. */
|
|
56
|
+
export declare const isValueReference: (node: AstNode, parent: AstNode | undefined) => boolean;
|
|
57
|
+
export declare const positionOf: (node: AstNode) => Position;
|
|
58
|
+
/** Visits every node below `root`, depth first, with the chain of its ancestors (nearest last); `'skip'` prunes a subtree. */
|
|
59
|
+
export declare const walk: (root: AstNode, visit: (node: AstNode, ancestors: readonly AstNode[]) => "skip" | undefined | void) => void;
|
|
60
|
+
export declare const utf8Bytes: (text: string) => number;
|
|
61
|
+
/** The nesting depth of the tree below `root`, measured without recursion, so a deep tree cannot exhaust the stack. */
|
|
62
|
+
export declare const nodeDepth: (root: AstNode) => number;
|
|
63
|
+
export declare const parseModule: (file: string, text: string) => ParseResult;
|
|
64
|
+
/** The sources of a module's import and re-export declarations; `rules/literal.ts` decides which of them are allowed. */
|
|
65
|
+
export declare const importedSpecifiers: (module: ParsedModule) => string[];
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { BunPlugin } from 'bun';
|
|
2
|
+
export interface ShellSliceOptions {
|
|
3
|
+
/** The application's root, where `fabrika.contract.ts` lies; the working directory by default. */
|
|
4
|
+
readonly root?: string;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Loads every module of the application's shell graph as its slice: no `.handle(…)`, and no import only a handler
|
|
8
|
+
* used. The build fails with the reader's findings when the static reader refuses the shell.
|
|
9
|
+
*/
|
|
10
|
+
export declare function shellSlicePlugin(options?: ShellSliceOptions): BunPlugin;
|
|
11
|
+
declare const _default: BunPlugin;
|
|
12
|
+
export default _default;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { type Finding } from './finding';
|
|
2
|
+
import type { PackedSet } from './packed';
|
|
3
|
+
import { type MigrationBase } from './rules/migration-history';
|
|
4
|
+
export interface CheckInput {
|
|
5
|
+
/** The application's root directory. */
|
|
6
|
+
readonly root: string;
|
|
7
|
+
/** The application's files keyed by path relative to `root` with `/` separators; read from disk under `root` when omitted. */
|
|
8
|
+
readonly files?: ReadonlyMap<string, string>;
|
|
9
|
+
/** A local pack whose tarballs `package-replacement` accepts (`fabrika-check --packed`); a deploy build gives none. */
|
|
10
|
+
readonly packed?: PackedSet;
|
|
11
|
+
/** The migrations of a shared branch the tree must still hold unchanged (`fabrika-check`, from git); a deploy build gives none. */
|
|
12
|
+
readonly migrationBase?: MigrationBase;
|
|
13
|
+
}
|
|
14
|
+
export interface ApplicationTree {
|
|
15
|
+
readonly files: ReadonlyMap<string, string>;
|
|
16
|
+
/** Symbolic links outside the skipped directories; the checker does not follow them. */
|
|
17
|
+
readonly links: readonly string[];
|
|
18
|
+
}
|
|
19
|
+
/** Every file the checker reads, by relative path, and the symbolic links it refuses to follow. */
|
|
20
|
+
export declare const readApplicationTree: (root: string) => Promise<ApplicationTree>;
|
|
21
|
+
/** Runs every rule over one application and returns the findings in file, line and column order. */
|
|
22
|
+
export declare const checkApplication: (input: CheckInput) => Promise<Finding[]>;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { type AnyContract } from '@fabrika/app/contract';
|
|
2
|
+
import type { ContractArtifact, TablePolicyArtifact } from '@fabrika/auth-core';
|
|
3
|
+
import { type TablePolicies, type Tables } from '@fabrika/db/tables';
|
|
4
|
+
import { type ParsedModule } from './ast';
|
|
5
|
+
import { type Finding } from './finding';
|
|
6
|
+
import { ModuleGraph } from './graph';
|
|
7
|
+
export { ContractError } from '@fabrika/app/contract';
|
|
8
|
+
export { type Finding, formatFinding } from './finding';
|
|
9
|
+
export { contractReaderInputs } from './inputs';
|
|
10
|
+
export { CONTRACT_FILE, MIGRATIONS_DIR, TABLES_FILE } from './layout';
|
|
11
|
+
export { CONTRACT_LIMITS } from './limits';
|
|
12
|
+
export { lockedRegistryVersion, LOCKFILE } from './lockfile';
|
|
13
|
+
export { CHECKER_VERSION } from './version';
|
|
14
|
+
/** Every form the reader refuses, each with its file, line, column and the allowed form. */
|
|
15
|
+
export declare class ContractSourceError extends Error {
|
|
16
|
+
readonly name = "ContractSourceError";
|
|
17
|
+
readonly findings: readonly Finding[];
|
|
18
|
+
constructor(findings: readonly Finding[]);
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* The contract artifact of an application, read from the text of its files (path relative to the application root →
|
|
22
|
+
* text): `fabrika.contract.ts`, every module it imports and the configuration that applies to them (`readContract`),
|
|
23
|
+
* and `src/db/tables.ts` with the modules it imports (`readTablesDefinition`). `tables`, when given, replaces what
|
|
24
|
+
* `src/db/tables.ts` declares: its parsed policies, or the artifact form of each policy.
|
|
25
|
+
*
|
|
26
|
+
* Throws `ContractSourceError` for a form the reader refuses, and `ContractError` with the same problems
|
|
27
|
+
* `compileContract` reports for a contract that does not compile.
|
|
28
|
+
*/
|
|
29
|
+
export declare function readContractSource(files: ReadonlyMap<string, string>, tables?: Tables<unknown, unknown> | Readonly<Record<string, TablePolicyArtifact>>): ContractArtifact;
|
|
30
|
+
/**
|
|
31
|
+
* The table policies of `src/db/tables.ts` in their artifact form (`readTablesDefinition`); none when `files` has no
|
|
32
|
+
* such file. Throws `ContractSourceError`.
|
|
33
|
+
*/
|
|
34
|
+
export declare function readTablesSource(files: ReadonlyMap<string, string>): TablePolicies;
|
|
35
|
+
/**
|
|
36
|
+
* The table policies of `src/db/tables.ts`, read from source and parsed by `@fabrika/db`'s own policy parse, as
|
|
37
|
+
* `defineTables` returns them; `null` when `files` has no such file. `files` carries its imports (the generated
|
|
38
|
+
* `src/db/schema.generated.ts`) and the configuration `readContract` names. Throws `ContractSourceError`.
|
|
39
|
+
*/
|
|
40
|
+
export declare function readTablesDefinition(files: ReadonlyMap<string, string>): Tables<unknown, unknown> | null;
|
|
41
|
+
/**
|
|
42
|
+
* The contract `defineContract` returns in the evaluated module, read from source; throws `ContractSourceError`.
|
|
43
|
+
* Besides the contract's modules, `files` carries every `package.json`, `tsconfig.json`, `jsconfig.json` and
|
|
44
|
+
* `bunfig.toml` that applies to them (the root and each directory of a contract module): one of them can redirect the
|
|
45
|
+
* packages the reader trusts by name, and the reader cannot tell a missing file from an absent one.
|
|
46
|
+
*/
|
|
47
|
+
export declare function readContract(files: ReadonlyMap<string, string>): AnyContract;
|
|
48
|
+
export type ContractRead = {
|
|
49
|
+
readonly ok: true;
|
|
50
|
+
readonly contract: AnyContract;
|
|
51
|
+
} | {
|
|
52
|
+
readonly ok: false;
|
|
53
|
+
readonly findings: readonly Finding[];
|
|
54
|
+
};
|
|
55
|
+
/** Reads the `contract` export of a graph whose contract modules already pass the literal rule. */
|
|
56
|
+
export declare function readContractModule(graph: ModuleGraph): ContractRead;
|
|
57
|
+
/** The contract module and every module its value imports reach. */
|
|
58
|
+
export declare function contractModules(graph: ModuleGraph, entry: ParsedModule): ParsedModule[];
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** The database an application's data layer runs on: SQLite in its Durable Object on Cloudflare (ADR-0065), else Postgres. */
|
|
2
|
+
export type ApplicationDialect = 'postgres' | 'sqlite';
|
|
3
|
+
/**
|
|
4
|
+
* Read statically from `fabrika.config.ts`: SQLite when its default export is a call of the `defineApp` that
|
|
5
|
+
* `@fabrika/provider-cloudflare` exports, the config every Cloudflare application writes. Postgres otherwise,
|
|
6
|
+
* including without a config or when the call's origin is not static.
|
|
7
|
+
*/
|
|
8
|
+
export declare const applicationDialect: (files: ReadonlyMap<string, string>) => ApplicationDialect;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import type { Finding } from './finding';
|
|
2
|
+
import type { Sample } from './scaffold-sample';
|
|
3
|
+
export declare const FIXTURES_EXPORT = "fixtures";
|
|
4
|
+
export declare const SEEDS_EXPORT = "seeds";
|
|
5
|
+
export declare const MEMBERS_EXPORT = "members";
|
|
6
|
+
export declare const INVITATIONS_EXPORT = "invitations";
|
|
7
|
+
export declare const LINKS_EXPORT = "links";
|
|
8
|
+
export declare const ROWS_EXPORT = "rows";
|
|
9
|
+
export declare const APP_ROWS_EXPORT = "appRows";
|
|
10
|
+
export declare const FILES_EXPORT = "files";
|
|
11
|
+
export declare const REFUSALS_EXPORT = "refusals";
|
|
12
|
+
export declare const PINS_EXPORT = "pins";
|
|
13
|
+
type ById = Readonly<Record<string, Readonly<Record<string, Sample>>>>;
|
|
14
|
+
export interface EndpointFixtures {
|
|
15
|
+
/** The input each endpoint's cases send, by endpoint id. */
|
|
16
|
+
readonly inputs: ById;
|
|
17
|
+
/** The columns of the row each endpoint addresses, as both scopes' rows are seeded, by endpoint id. */
|
|
18
|
+
readonly seeds: ById;
|
|
19
|
+
/** The input fields that name a member of the endpoint's member directory, by endpoint id. */
|
|
20
|
+
readonly members: Readonly<Record<string, readonly string[]>>;
|
|
21
|
+
/** The input fields that name an invitation the endpoint cancels, by endpoint id. */
|
|
22
|
+
readonly invitations: Readonly<Record<string, readonly string[]>>;
|
|
23
|
+
/** The input fields that name a share link the endpoint revokes, by endpoint id. */
|
|
24
|
+
readonly links: Readonly<Record<string, readonly string[]>>;
|
|
25
|
+
/** The input fields that name a row lying in the endpoint's object, with that row's table, by endpoint id. */
|
|
26
|
+
readonly rows: Readonly<Record<string, Readonly<Record<string, string>>>>;
|
|
27
|
+
/** The rows of application tables the endpoint's own case reads, by table, each with its columns, by endpoint id. */
|
|
28
|
+
readonly appRows: Readonly<Record<string, Readonly<Record<string, Readonly<Record<string, Sample>>>>>>;
|
|
29
|
+
/** The file each case sends in a file field, a path relative to the application's root, by input field, by endpoint id. */
|
|
30
|
+
readonly files: Readonly<Record<string, Readonly<Record<string, string>>>>;
|
|
31
|
+
/** The error types of the 400s an endpoint's handler may answer its own object with, by endpoint id. */
|
|
32
|
+
readonly refusals: Readonly<Record<string, readonly string[]>>;
|
|
33
|
+
/** The own scope's value of each pinned text column of the row the endpoint addresses, by endpoint id. */
|
|
34
|
+
readonly pins: Readonly<Record<string, Readonly<Record<string, string>>>>;
|
|
35
|
+
}
|
|
36
|
+
export declare const NO_FIXTURES: EndpointFixtures;
|
|
37
|
+
/**
|
|
38
|
+
* Whether `path` may name a file fixture: relative to the application's root, `/`-separated, and each segment of
|
|
39
|
+
* letters, digits, `_`, `-` and `.` that starts with neither `.` nor `-`, so it never leaves the root, names no dotfile and
|
|
40
|
+
* reads the same as a URL path.
|
|
41
|
+
*/
|
|
42
|
+
export declare const isFixtureFilePath: (path: string) => boolean;
|
|
43
|
+
export declare function isSampleObject(value: Sample): value is {
|
|
44
|
+
readonly [key: string]: Sample;
|
|
45
|
+
};
|
|
46
|
+
/** The fixtures of `files`, by endpoint id; none without the file. */
|
|
47
|
+
export declare function readEndpointFixtures(files: ReadonlyMap<string, string>): {
|
|
48
|
+
readonly ok: true;
|
|
49
|
+
readonly fixtures: EndpointFixtures;
|
|
50
|
+
} | {
|
|
51
|
+
readonly ok: false;
|
|
52
|
+
readonly findings: readonly Finding[];
|
|
53
|
+
};
|
|
54
|
+
export {};
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { AstNode, Position } from './ast';
|
|
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", "key-literal", "client-identity", "sensitive-log", "unsafe-html", "dynamic-code", "migration-name", "migration-refused", "backup-schema", "migration-edited", "file-column-migration", "crontab", "package-replacement", "skipped-test", "test-without-assertion", "generated-test-hash", "mock-module", "endpoint-fixture", "spa-server-import", "layer-import", "generated-test-import", "shared-import", "tool-import", "cloudflare-scan", "cloudflare-job-segment", "cloudflare-current-timestamp", "cloudflare-framework-name"];
|
|
4
|
+
/** Rules that report a step out of the application architecture. */
|
|
5
|
+
export declare const STRUCTURE_HINT_RULE_IDS: readonly ["logic-data-access", "cross-slice-queries", "handler-location", "data-access-location"];
|
|
6
|
+
/**
|
|
7
|
+
* Rules whose findings never fail the check: the architecture's, a `ctx.sql` its site does not declare, a
|
|
8
|
+
* `{ member: true }` endpoint of a contract whose external role admits people from outside the organization, and a
|
|
9
|
+
* Cloudflare mutation that waits for an outside call inside the object's transaction.
|
|
10
|
+
*/
|
|
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", "key-literal", "client-identity", "sensitive-log", "unsafe-html", "dynamic-code", "migration-name", "migration-refused", "backup-schema", "migration-edited", "file-column-migration", "crontab", "package-replacement", "skipped-test", "test-without-assertion", "generated-test-hash", "mock-module", "endpoint-fixture", "spa-server-import", "layer-import", "generated-test-import", "shared-import", "tool-import", "cloudflare-scan", "cloudflare-job-segment", "cloudflare-current-timestamp", "cloudflare-framework-name", "logic-data-access", "cross-slice-queries", "handler-location", "data-access-location", "sql-undeclared", "member-admits-external", "cloudflare-held-call"];
|
|
13
|
+
export type RefusalRuleId = (typeof REFUSAL_RULE_IDS)[number];
|
|
14
|
+
export type HintRuleId = (typeof HINT_RULE_IDS)[number];
|
|
15
|
+
export type RuleId = (typeof RULE_IDS)[number];
|
|
16
|
+
export type Severity = 'refusal' | 'hint';
|
|
17
|
+
interface FindingFields {
|
|
18
|
+
/** Relative to the application root, with `/` separators. */
|
|
19
|
+
readonly file: string;
|
|
20
|
+
readonly line: number;
|
|
21
|
+
readonly column: number;
|
|
22
|
+
readonly message: string;
|
|
23
|
+
/** What to write instead; every finding names it, because the reader is often an agent with no other feedback. */
|
|
24
|
+
readonly allowed: string;
|
|
25
|
+
}
|
|
26
|
+
/** A finding's severity is its rule's: a refusal rule never reports a hint, nor the reverse. */
|
|
27
|
+
export type Finding = (FindingFields & {
|
|
28
|
+
readonly severity: 'refusal';
|
|
29
|
+
readonly rule: RefusalRuleId;
|
|
30
|
+
}) | (FindingFields & {
|
|
31
|
+
readonly severity: 'hint';
|
|
32
|
+
readonly rule: HintRuleId;
|
|
33
|
+
});
|
|
34
|
+
export declare const isHintRule: (rule: RuleId) => rule is HintRuleId;
|
|
35
|
+
export type Report = (at: AstNode | Position, rule: RuleId, message: string, allowed: string) => void;
|
|
36
|
+
export declare const reporter: (file: string, findings: Finding[]) => Report;
|
|
37
|
+
export declare const formatFinding: (finding: Finding) => string;
|
|
38
|
+
export declare const compareFindings: (left: Finding, right: Finding) => number;
|
|
39
|
+
export {};
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { type AstNode, type ParsedModule } from './ast';
|
|
2
|
+
import { type Resolution } from './layout';
|
|
3
|
+
export interface Located {
|
|
4
|
+
readonly module: ParsedModule;
|
|
5
|
+
readonly node: AstNode;
|
|
6
|
+
}
|
|
7
|
+
export interface ImportBinding {
|
|
8
|
+
readonly local: string;
|
|
9
|
+
readonly specifier: string;
|
|
10
|
+
/** `default`, `*` or the imported name. */
|
|
11
|
+
readonly imported: string;
|
|
12
|
+
/** The specifier node, where a finding about this import is reported. */
|
|
13
|
+
readonly node: AstNode;
|
|
14
|
+
}
|
|
15
|
+
/** What a name denotes, followed through imports and exports to its declaration. */
|
|
16
|
+
export type Binding = {
|
|
17
|
+
readonly kind: 'const';
|
|
18
|
+
readonly module: ParsedModule;
|
|
19
|
+
readonly name: string;
|
|
20
|
+
readonly init: AstNode | undefined;
|
|
21
|
+
} | {
|
|
22
|
+
readonly kind: 'default';
|
|
23
|
+
readonly module: ParsedModule;
|
|
24
|
+
readonly expression: AstNode;
|
|
25
|
+
} | {
|
|
26
|
+
readonly kind: 'package';
|
|
27
|
+
readonly specifier: string;
|
|
28
|
+
readonly imported: string;
|
|
29
|
+
} | {
|
|
30
|
+
readonly kind: 'other';
|
|
31
|
+
readonly module: ParsedModule;
|
|
32
|
+
readonly name: string;
|
|
33
|
+
} | {
|
|
34
|
+
readonly kind: 'unresolved';
|
|
35
|
+
readonly reason: string;
|
|
36
|
+
};
|
|
37
|
+
interface ModuleIndex {
|
|
38
|
+
readonly consts: ReadonlyMap<string, AstNode | undefined>;
|
|
39
|
+
/** Top-level names that are not consts: `let`, `var`, functions, classes. */
|
|
40
|
+
readonly others: ReadonlySet<string>;
|
|
41
|
+
readonly imports: ReadonlyMap<string, ImportBinding>;
|
|
42
|
+
/** Exported name → the local name it exports, or the default export's expression. */
|
|
43
|
+
readonly exports: ReadonlyMap<string, string | AstNode>;
|
|
44
|
+
}
|
|
45
|
+
/** Erased wrappers: `x satisfies T`, `x as const`, parentheses. */
|
|
46
|
+
export declare const unwrapValue: (node: AstNode) => AstNode;
|
|
47
|
+
/** The application's modules, read statically: what each name denotes, without executing anything. */
|
|
48
|
+
export declare class ModuleGraph {
|
|
49
|
+
#private;
|
|
50
|
+
constructor(modules: ReadonlyMap<string, ParsedModule>);
|
|
51
|
+
module(file: string): ParsedModule | undefined;
|
|
52
|
+
index(module: ParsedModule): ModuleIndex;
|
|
53
|
+
/** How a relative specifier resolves among the checked modules. */
|
|
54
|
+
resolution(from: string, specifier: string): Resolution;
|
|
55
|
+
/** The checked module a relative specifier names; none when two candidates compete, the index of a directory import. */
|
|
56
|
+
resolve(from: string, specifier: string): ParsedModule | undefined;
|
|
57
|
+
/** What `name` denotes in `module`; an import is followed to the exporting module's declaration. */
|
|
58
|
+
binding(module: ParsedModule, name: string, seen?: ReadonlySet<string>): Binding;
|
|
59
|
+
/** What an exported name of `module` denotes. */
|
|
60
|
+
exported(module: ParsedModule, name: string, seen?: ReadonlySet<string>): Binding;
|
|
61
|
+
/** Follows identifiers and erased wrappers to the expression a value is written as; `undefined` when it is not static. */
|
|
62
|
+
resolveValue(located: Located, depth?: number): Located | undefined;
|
|
63
|
+
/** The fixed string a value resolves to. */
|
|
64
|
+
resolveString(located: Located): string | undefined;
|
|
65
|
+
}
|
|
66
|
+
/** The value of `key` in an object literal, read statically; spreads and computed keys are not followed. */
|
|
67
|
+
export declare const propertyValue: (located: Located, key: string) => Located | undefined;
|
|
68
|
+
export {};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export { type ApplicationTree, checkApplication, type CheckInput, readApplicationTree } from './check';
|
|
2
|
+
export { type ApplicationDialect, applicationDialect } from './dialect';
|
|
3
|
+
export { type Finding, formatFinding, RULE_IDS, type RuleId } from './finding';
|
|
4
|
+
export { CONFIG_FILE, CONTRACT_FILE, GENERATED_ENTRY_FILE, MIGRATIONS_DIR, TABLES_FILE, ZEROPS_FILES } from './layout';
|
|
5
|
+
export { readMigrationBase } from './migration-base';
|
|
6
|
+
export { type PackedSet, type PackedTarball, readPackedSet } from './packed';
|
|
7
|
+
export type { MigrationBase } from './rules/migration-history';
|
|
8
|
+
export { GENERATED_TEST_HEADER, generatedTestHeader } from './rules/tests';
|
|
9
|
+
export { ENDPOINT_ACCESS_TEST, ENDPOINT_FIXTURES_FILE, type GeneratedTest, generatedTests, missingEndpointFixtures, POLICY_TESTS_DIR, policyTestPath, scaffoldInputs, } from './scaffold';
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The paths `readContractSource` reads that `files` does not hold yet, among the paths that `exists` at the commit:
|
|
3
|
+
* `fabrika.contract.ts` and `src/db/tables.ts`, every module the shell slice of each imports (an import only a
|
|
4
|
+
* colocated handler uses is never followed) with every candidate of an import, the
|
|
5
|
+
* `package.json`, `tsconfig.json` and `jsconfig.json` of each such module's directories, a root `bunfig.toml`, and the
|
|
6
|
+
* files a `tsconfig.json` extends. A module is followed only once its text is in `files`, so the caller reads the answer,
|
|
7
|
+
* adds it to `files` and asks again until the answer is empty.
|
|
8
|
+
*/
|
|
9
|
+
export declare function contractReaderInputs(files: ReadonlyMap<string, string>, exists: (path: string) => boolean): string[];
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/** Where an application keeps what the checker reads. Paths are relative to the application root. */
|
|
2
|
+
export declare const CONTRACT_FILE = "fabrika.contract.ts";
|
|
3
|
+
/** The provider-authored application config, whose `defineApp` names the provider. */
|
|
4
|
+
export declare const CONFIG_FILE = "fabrika.config.ts";
|
|
5
|
+
/** The named export of the contract module that holds `defineContract(…)`. */
|
|
6
|
+
export declare const CONTRACT_EXPORT = "contract";
|
|
7
|
+
export declare const TABLES_FILE = "src/db/tables.ts";
|
|
8
|
+
/** The named export of the tables module that holds `defineTables(…)(…)`. */
|
|
9
|
+
export declare const TABLES_EXPORT = "tables";
|
|
10
|
+
export declare 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 declare const STAGED_ASSETS_DIR = "fabrika/assets/";
|
|
13
|
+
export declare const MIGRATIONS_DIR = "db/migrations/";
|
|
14
|
+
/** Where `fabrika db test-scaffold` writes the policy isolation tests, `<table>.policy-isolation.test.ts`. */
|
|
15
|
+
export declare const POLICY_TESTS_DIR = "src/db/__tests__/";
|
|
16
|
+
export declare const POLICY_TEST_SUFFIX = ".policy-isolation.test.ts";
|
|
17
|
+
/** The generated endpoint access tests; they import `src/app.ts`, whose default export they boot. */
|
|
18
|
+
export declare const ENDPOINT_ACCESS_TEST = "src/__tests__/endpoint-access.test.ts";
|
|
19
|
+
/** The inputs the author gives the endpoint access tests where the scaffold cannot sample one; read, never run. */
|
|
20
|
+
export declare const ENDPOINT_FIXTURES_FILE = "src/__tests__/endpoint-access.fixtures.ts";
|
|
21
|
+
export declare const ZEROPS_FILES: readonly ["zerops.yaml", "zerops.yml"];
|
|
22
|
+
/** The application architecture (docs/reference/application-architecture.md): where each layer's code lives. */
|
|
23
|
+
export declare const VOCABULARY_FILE = "src/vocabulary.ts";
|
|
24
|
+
export declare const APP_KIT_FILE = "src/app-kit.ts";
|
|
25
|
+
/** The shared zod schemas the shell and the SPA import: one module, or a directory of them once it grows. */
|
|
26
|
+
export declare const SCHEMAS_FILE = "src/schemas.ts";
|
|
27
|
+
export declare const SCHEMAS_DIR = "src/schemas/";
|
|
28
|
+
/** Pure code the SPA and every slice may import: it loads nothing but `zod/v4` and other modules of its own directory. */
|
|
29
|
+
export declare const SHARED_DIR = "src/shared/";
|
|
30
|
+
/** `src/db/`: the tables, the generated schema and their generated tests; never a slice. */
|
|
31
|
+
export declare const DATABASE_DIR = "src/db/";
|
|
32
|
+
/** A slice is a directory `src/<slice>/` that holds these modules, each optional. */
|
|
33
|
+
export declare const SLICE_ENDPOINTS_FILE = "endpoints.ts";
|
|
34
|
+
export declare const SLICE_LOGIC_FILE = "logic.ts";
|
|
35
|
+
export declare const SLICE_QUERIES_FILE = "queries.ts";
|
|
36
|
+
export declare const SPA_DIR = "web/";
|
|
37
|
+
/** The server half of an application: its modules run in the application's process. */
|
|
38
|
+
export declare const SERVER_DIR = "src/";
|
|
39
|
+
/**
|
|
40
|
+
* A developer's tools, such as a command line over the contract: code that runs on a developer's machine and never in
|
|
41
|
+
* the application, so the rules for code that runs there do not apply, and no module outside it may load one.
|
|
42
|
+
*/
|
|
43
|
+
export declare const TOOLS_DIR = "tools/";
|
|
44
|
+
/** Build output, installed packages and tool state are not application source and are never read. */
|
|
45
|
+
export declare const isIgnoredSegment: (segment: string) => boolean;
|
|
46
|
+
/** Whether the checker skips `path` (relative to the root): under a skipped segment, or the staged SPA files. */
|
|
47
|
+
export declare const isSkippedPath: (path: string) => boolean;
|
|
48
|
+
export declare const isSourceFile: (path: string) => boolean;
|
|
49
|
+
export declare const isTestFile: (path: string) => boolean;
|
|
50
|
+
/** A module that runs in the application's server process: under `src/`, not a test. */
|
|
51
|
+
export declare const isServerModule: (path: string) => boolean;
|
|
52
|
+
/** A module under `tools/`, tests included. */
|
|
53
|
+
export declare const isToolModule: (path: string) => boolean;
|
|
54
|
+
export declare const isJsonFile: (path: string) => boolean;
|
|
55
|
+
/** Whether the checker reads this file at all. */
|
|
56
|
+
export declare const isCheckedFile: (path: string) => boolean;
|
|
57
|
+
/** Joins a relative specifier onto the importing file's directory; `undefined` when it climbs above the root. */
|
|
58
|
+
export declare const joinRelative: (fromFile: string, specifier: string) => string | undefined;
|
|
59
|
+
/**
|
|
60
|
+
* How a relative import resolves among the files that exist. Only an exact name, or exactly one extension candidate,
|
|
61
|
+
* is a `file`: with two candidates (`m.ts` and `m.tsx`) or a directory, Bun's choice is not one the checker repeats.
|
|
62
|
+
*/
|
|
63
|
+
export type Resolution = {
|
|
64
|
+
readonly kind: 'file';
|
|
65
|
+
readonly path: string;
|
|
66
|
+
} | {
|
|
67
|
+
readonly kind: 'ambiguous';
|
|
68
|
+
readonly paths: readonly string[];
|
|
69
|
+
} | {
|
|
70
|
+
readonly kind: 'directory';
|
|
71
|
+
readonly paths: readonly string[];
|
|
72
|
+
} | {
|
|
73
|
+
readonly kind: 'missing';
|
|
74
|
+
};
|
|
75
|
+
export declare const resolveRelative: (exists: (path: string) => boolean, fromFile: string, specifier: string) => Resolution;
|
|
76
|
+
export declare const isRelativeSpecifier: (specifier: string) => boolean;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The resource budget of reading a contract from source. Control reads an application's commit, so a contract may
|
|
3
|
+
* not make the reader or `compileContract` do unbounded work; each limit is enforced while reading, before the next
|
|
4
|
+
* step runs.
|
|
5
|
+
*/
|
|
6
|
+
export declare const CONTRACT_LIMITS: {
|
|
7
|
+
/** UTF-8 bytes of `fabrika.contract.ts` and every module it imports, together. */
|
|
8
|
+
readonly sourceBytes: number;
|
|
9
|
+
/** Nesting of syntax nodes in one module; the contracts in this repository reach 25. */
|
|
10
|
+
readonly astDepth: 128;
|
|
11
|
+
/** Schema nodes once every shared schema is expanded where the contract uses it, over the whole contract. */
|
|
12
|
+
readonly schemaNodes: 20000;
|
|
13
|
+
/** Nesting of one schema: objects, arrays, unions and wrappers. */
|
|
14
|
+
readonly schemaDepth: 32;
|
|
15
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export declare const LOCKFILE = "bun.lock";
|
|
2
|
+
/** An exact version, as `bun install` records one resolved from the registry. */
|
|
3
|
+
export declare const EXACT_VERSION: RegExp;
|
|
4
|
+
/** One `packages` entry: `[<name>@<version>, <registry>, <metadata>, <integrity>]`; a tarball has no registry. */
|
|
5
|
+
export interface LockfileEntry {
|
|
6
|
+
/** What follows `<name>@` in the resolution, or empty when the resolution names another package. */
|
|
7
|
+
readonly version: string;
|
|
8
|
+
readonly registry: unknown;
|
|
9
|
+
readonly integrity: unknown;
|
|
10
|
+
}
|
|
11
|
+
/** The `packages` of a `bun.lock` by key; null when the text is not a lockfile bun writes. */
|
|
12
|
+
export declare function lockfilePackages(text: string): Readonly<Record<string, unknown>> | null;
|
|
13
|
+
export declare function lockfileEntry(name: string, entry: unknown): LockfileEntry;
|
|
14
|
+
/**
|
|
15
|
+
* The registry version the lockfile resolves the package `name` to for the application itself: its top-level entry,
|
|
16
|
+
* not one nested under another package. Null when the text is not a lockfile, holds no such entry, or resolves it from
|
|
17
|
+
* anywhere but the npm registry.
|
|
18
|
+
*/
|
|
19
|
+
export declare function lockedRegistryVersion(text: string, name: string): string | null;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { MigrationBase } from './rules/migration-history';
|
|
2
|
+
/**
|
|
3
|
+
* The migrations at the merge base of HEAD and `ref` (the remote's default branch when omitted), compared against
|
|
4
|
+
* the merge base so that migrations the default branch added since the branch forked are not read as deleted.
|
|
5
|
+
* `null` when `root` is in no git work tree; `{ skipped }` when it is but the base cannot be read.
|
|
6
|
+
*/
|
|
7
|
+
export declare const readMigrationBase: (root: string, ref?: string) => Promise<MigrationBase | {
|
|
8
|
+
readonly skipped: string;
|
|
9
|
+
} | null>;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/** One package of the set: the tarball `release:pack` wrote and the sha512 integrity of its bytes. */
|
|
2
|
+
export interface PackedTarball {
|
|
3
|
+
readonly filename: string;
|
|
4
|
+
readonly integrity: string;
|
|
5
|
+
}
|
|
6
|
+
export interface PackedSet {
|
|
7
|
+
/** The version the pack gave every package of the set. */
|
|
8
|
+
readonly version: string;
|
|
9
|
+
/** Each package's tarball, by package name. */
|
|
10
|
+
readonly packages: ReadonlyMap<string, PackedTarball>;
|
|
11
|
+
/** The sha512 integrity of every file under the root named like one of the tarballs, by path relative to the root. */
|
|
12
|
+
readonly tarballs: ReadonlyMap<string, string>;
|
|
13
|
+
}
|
|
14
|
+
/** The packed set `manifestText` (a `release-manifest.json`) describes, with the tarballs found under `root`. */
|
|
15
|
+
export declare const readPackedSet: (root: string, manifestText: string) => Promise<PackedSet>;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { type Finding } from '../finding';
|
|
2
|
+
import type { Located } from '../graph';
|
|
3
|
+
/** Every form the reader refuses, as findings of the `contract` rule with where it is and the allowed form. */
|
|
4
|
+
export declare class ReaderProblems {
|
|
5
|
+
readonly findings: Finding[];
|
|
6
|
+
add(at: Located, message: string, allowed: string): void;
|
|
7
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { z } from 'zod/v4';
|
|
2
|
+
import type { LiteralValue } from './value';
|
|
3
|
+
export type SchemaStep = {
|
|
4
|
+
readonly factory: string;
|
|
5
|
+
readonly iso: boolean;
|
|
6
|
+
} | {
|
|
7
|
+
readonly method: string;
|
|
8
|
+
readonly receiver: z.ZodType | undefined;
|
|
9
|
+
};
|
|
10
|
+
export type BuiltSchema = {
|
|
11
|
+
readonly ok: true;
|
|
12
|
+
readonly schema: z.ZodType;
|
|
13
|
+
} | {
|
|
14
|
+
readonly ok: false;
|
|
15
|
+
readonly message: string;
|
|
16
|
+
readonly allowed: string;
|
|
17
|
+
};
|
|
18
|
+
export declare function buildSchema(step: SchemaStep, args: readonly LiteralValue[]): BuiltSchema;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { type AnyContract } from '@fabrika/app/contract';
|
|
2
|
+
import type { ReaderProblems } from './problems';
|
|
3
|
+
import { type LiteralValue } from './value';
|
|
4
|
+
/** Reads a `defineContract(…)` value into the contract `defineContract` returns; `null` after recording why not. */
|
|
5
|
+
export declare function readContractValue(value: LiteralValue, problems: ReaderProblems): AnyContract | null;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { type Tables } from '@fabrika/db/tables';
|
|
2
|
+
import { type ParsedModule } from '../ast';
|
|
3
|
+
import type { ModuleGraph } from '../graph';
|
|
4
|
+
import type { ReaderProblems } from './problems';
|
|
5
|
+
/** The policies of `src/db/tables.ts`, as `defineTables` returns them; `null` after recording why not. */
|
|
6
|
+
export declare function readTablesValue(graph: ModuleGraph, module: ParsedModule, problems: ReaderProblems): Tables<unknown, unknown> | null;
|