@fabrika/check 0.9.0-dev.27 → 0.9.0-dev.29
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/contract.ts +23 -3
- package/src/limits.ts +2 -2
- package/src/shell-modules.ts +26 -5
- package/src/slice.ts +6 -5
- package/src/vite-plugin.ts +3 -2
- package/types/src/contract.d.ts +11 -0
- package/types/src/limits.d.ts +2 -2
- package/types/src/shell-modules.d.ts +1 -1
- package/types/src/slice.d.ts +8 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fabrika/check",
|
|
3
|
-
"version": "0.9.0-dev.
|
|
3
|
+
"version": "0.9.0-dev.29",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -52,11 +52,11 @@
|
|
|
52
52
|
},
|
|
53
53
|
"dependencies": {
|
|
54
54
|
"@babel/parser": "8.0.7",
|
|
55
|
-
"@fabrika/app": "0.9.0-dev.
|
|
56
|
-
"@fabrika/auth-core": "0.9.0-dev.
|
|
57
|
-
"@fabrika/db": "0.9.0-dev.
|
|
58
|
-
"@fabrika/operations-contract": "0.9.0-dev.
|
|
59
|
-
"@fabrika/platform-node": "0.9.0-dev.
|
|
55
|
+
"@fabrika/app": "0.9.0-dev.29",
|
|
56
|
+
"@fabrika/auth-core": "0.9.0-dev.29",
|
|
57
|
+
"@fabrika/db": "0.9.0-dev.29",
|
|
58
|
+
"@fabrika/operations-contract": "0.9.0-dev.29",
|
|
59
|
+
"@fabrika/platform-node": "0.9.0-dev.29",
|
|
60
60
|
"zod": "^3.25.76"
|
|
61
61
|
}
|
|
62
62
|
}
|
package/src/contract.ts
CHANGED
|
@@ -8,14 +8,14 @@ import { type TablePolicies, tablePolicies, type Tables } from '@fabrika/db/tabl
|
|
|
8
8
|
import { child, children, importedSpecifiers, nodeCount, nodeDepth, type ParsedModule, utf8Bytes } from './ast'
|
|
9
9
|
import { type Finding, formatFinding } from './finding'
|
|
10
10
|
import { ModuleGraph } from './graph'
|
|
11
|
-
import { CONTRACT_EXPORT, CONTRACT_FILE, isRelativeSpecifier, isSourceFile, resolveRelative, TABLES_FILE } from './layout'
|
|
11
|
+
import { CONTRACT_EXPORT, CONTRACT_FILE, isRelativeSpecifier, isSourceFile, resolveRelative, TABLES_EXPORT, TABLES_FILE } from './layout'
|
|
12
12
|
import { CONTRACT_LIMITS } from './limits'
|
|
13
13
|
import { ReaderProblems } from './reader/problems'
|
|
14
14
|
import { readContractValue } from './reader/surface'
|
|
15
15
|
import { readTablesValue } from './reader/tables'
|
|
16
16
|
import { ValueReader } from './reader/value'
|
|
17
17
|
import { checkLiteralGraph } from './rules/literal'
|
|
18
|
-
import { parseShellModule } from './slice'
|
|
18
|
+
import { offsets, parseShellModule, type Range } from './slice'
|
|
19
19
|
import { checkTrustedResolution } from './trust'
|
|
20
20
|
|
|
21
21
|
export { ContractError } from '@fabrika/app/contract'
|
|
@@ -70,6 +70,20 @@ export function readTablesSource(files: ReadonlyMap<string, string>): TablePolic
|
|
|
70
70
|
* `src/db/schema.generated.ts`) and the configuration `readContract` names. Throws `ContractSourceError`.
|
|
71
71
|
*/
|
|
72
72
|
export function readTablesDefinition(files: ReadonlyMap<string, string>): Tables<unknown, unknown> | null {
|
|
73
|
+
return readTablesDeclaration(files)?.tables ?? null
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** The policies `readTablesDefinition` reads, and where the `defineTables(…)(…)` call that declares them stands. */
|
|
77
|
+
export interface TablesDeclaration {
|
|
78
|
+
readonly tables: Tables<unknown, unknown>
|
|
79
|
+
/** The module of the call: `src/db/tables.ts`, or the module it re-exports `tables` from. */
|
|
80
|
+
readonly file: string
|
|
81
|
+
/** The call's offsets in that module's text, the same in its shell slice. */
|
|
82
|
+
readonly call: Range
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** `readTablesDefinition` with the place of the call; `null` when `files` has no `src/db/tables.ts`. */
|
|
86
|
+
export function readTablesDeclaration(files: ReadonlyMap<string, string>): TablesDeclaration | null {
|
|
73
87
|
if (!files.has(TABLES_FILE)) {
|
|
74
88
|
return null
|
|
75
89
|
}
|
|
@@ -95,7 +109,13 @@ export function readTablesDefinition(files: ReadonlyMap<string, string>): Tables
|
|
|
95
109
|
if (tables === null || problems.findings.length > 0) {
|
|
96
110
|
throw new ContractSourceError(problems.findings)
|
|
97
111
|
}
|
|
98
|
-
|
|
112
|
+
// readTablesValue read the policies from this binding's initializer, so it is a const with one.
|
|
113
|
+
const binding = parsed.graph.exported(module, TABLES_EXPORT)
|
|
114
|
+
const call = binding.kind === 'const' && binding.init !== undefined ? offsets(binding.init) : undefined
|
|
115
|
+
if (binding.kind !== 'const' || call === undefined) {
|
|
116
|
+
throw new Error(`${TABLES_FILE}: the policies were read, but not where their call stands`)
|
|
117
|
+
}
|
|
118
|
+
return { tables, file: binding.module.file, call }
|
|
99
119
|
}
|
|
100
120
|
|
|
101
121
|
/**
|
package/src/limits.ts
CHANGED
|
@@ -20,6 +20,6 @@ export const CONTRACT_LIMITS = {
|
|
|
20
20
|
astDepth: 128,
|
|
21
21
|
/** Schema nodes once every shared schema is expanded where the contract uses it, over the whole contract. */
|
|
22
22
|
schemaNodes: 60_000,
|
|
23
|
-
/** Nesting of one schema: objects, arrays, unions and wrappers. */
|
|
24
|
-
schemaDepth:
|
|
23
|
+
/** Nesting of one schema: objects, arrays, unions and wrappers; it bounds the recursion of compiling and diffing one. */
|
|
24
|
+
schemaDepth: 64,
|
|
25
25
|
} as const
|
package/src/shell-modules.ts
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
// The application's shell graph read from disk as Control reads it from a commit, each module as its slice: what both
|
|
2
|
-
// SPA build plugins (`bun-plugin.ts`, `vite-plugin.ts`) load in place of the files.
|
|
2
|
+
// SPA build plugins (`bun-plugin.ts`, `vite-plugin.ts`) load in place of the files. The SPA's slice of the module that
|
|
3
|
+
// declares the table policies has `tables = {}`: the client never reads them, and their parse would load with them.
|
|
3
4
|
|
|
4
5
|
import { readFileSync, statSync } from 'node:fs'
|
|
5
6
|
import { join } from 'node:path'
|
|
6
|
-
import {
|
|
7
|
+
import { parseModule } from './ast'
|
|
8
|
+
import { contractReaderInputs, readContract, readTablesDeclaration, type TablesDeclaration } from './contract'
|
|
7
9
|
import { CONTRACT_FILE, isSourceFile } from './layout'
|
|
8
|
-
import { shellSlice } from './slice'
|
|
10
|
+
import { moduleSlice, shellSlice } from './slice'
|
|
9
11
|
|
|
10
12
|
const SOURCE_EXTENSION = String.raw`\.(?:[mc]?[jt]s|[jt]sx)$`
|
|
11
13
|
|
|
@@ -17,7 +19,7 @@ const isFile = (path: string): boolean => statSync(path, { throwIfNoEntry: false
|
|
|
17
19
|
|
|
18
20
|
/**
|
|
19
21
|
* The shell modules under `root`, by absolute path, as their slices; none without a `fabrika.contract.ts` there.
|
|
20
|
-
* Throws the reader's refusal when the static reader refuses the shell.
|
|
22
|
+
* Throws the reader's refusal when the static reader refuses the shell or the table policies.
|
|
21
23
|
*/
|
|
22
24
|
export function shellModules(root: string): ReadonlyMap<string, string> {
|
|
23
25
|
if (!isFile(join(root, CONTRACT_FILE))) return new Map()
|
|
@@ -28,5 +30,24 @@ export function shellModules(root: string): ReadonlyMap<string, string> {
|
|
|
28
30
|
for (const path of missing) files.set(path, readFileSync(join(root, path), 'utf8'))
|
|
29
31
|
}
|
|
30
32
|
readContract(files)
|
|
31
|
-
|
|
33
|
+
// The policies are refused here as the platform refuses them, instead of by `defineTables` in the browser.
|
|
34
|
+
const tables = readTablesDeclaration(files)
|
|
35
|
+
return new Map(
|
|
36
|
+
[...files]
|
|
37
|
+
.filter(([path]) => isSourceFile(path))
|
|
38
|
+
.map(([path, text]) => [join(root, path), path === tables?.file ? withoutPolicies(path, text, tables) : shellSlice(path, text).text]),
|
|
39
|
+
)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* The slice of the module that declares the policies, its `defineTables(…)(…)` call and what only the call imports
|
|
44
|
+
* blanked, `{}` in the call's place. `defineApp` reads of `tables` only that it is given.
|
|
45
|
+
*/
|
|
46
|
+
function withoutPolicies(path: string, text: string, tables: TablesDeclaration): string {
|
|
47
|
+
const slice = shellSlice(path, text).text
|
|
48
|
+
const parsed = parseModule(path, slice)
|
|
49
|
+
// The reader has just read the policies from this slice.
|
|
50
|
+
if (!parsed.ok) throw new Error(`${path}: the slice the policies were read from does not parse`)
|
|
51
|
+
const blanked = moduleSlice(parsed.module, slice, [tables.call]).text
|
|
52
|
+
return `${blanked.slice(0, tables.call.start)}{}${blanked.slice(tables.call.start + 2)}`
|
|
32
53
|
}
|
package/src/slice.ts
CHANGED
|
@@ -26,14 +26,14 @@ import type { Finding } from './finding'
|
|
|
26
26
|
/** Builders whose result takes `.handle(handler)`: an endpoint of a group, a link or an export, a job and a webhook. */
|
|
27
27
|
const HANDLED_BUILDERS = new Set(['query', 'mutation', 'cron', 'webhook'])
|
|
28
28
|
|
|
29
|
-
interface Range {
|
|
29
|
+
export interface Range {
|
|
30
30
|
readonly start: number
|
|
31
31
|
readonly end: number
|
|
32
32
|
}
|
|
33
33
|
|
|
34
34
|
const isFunction = (node: AstNode | null | undefined): boolean => node?.type === 'ArrowFunctionExpression' || node?.type === 'FunctionExpression'
|
|
35
35
|
|
|
36
|
-
const offsets = (node: AstNode): Range | undefined => {
|
|
36
|
+
export const offsets = (node: AstNode): Range | undefined => {
|
|
37
37
|
const start = node['start']
|
|
38
38
|
const end = node['end']
|
|
39
39
|
return typeof start === 'number' && typeof end === 'number' ? { start, end } : undefined
|
|
@@ -245,10 +245,11 @@ export interface ShellSlice {
|
|
|
245
245
|
|
|
246
246
|
/**
|
|
247
247
|
* The slice of one parsed module: `module`'s text with every `.handle(…)` and everything only the handlers used
|
|
248
|
-
* blanked. A module without a handler comes back unchanged.
|
|
248
|
+
* blanked. A module without a handler comes back unchanged. Each range of `removed` goes as a handler does, with what
|
|
249
|
+
* only it uses.
|
|
249
250
|
*/
|
|
250
|
-
export function moduleSlice(module: ParsedModule, text: string): ShellSlice {
|
|
251
|
-
const handles: Range[] = []
|
|
251
|
+
export function moduleSlice(module: ParsedModule, text: string, removed: readonly Range[] = []): ShellSlice {
|
|
252
|
+
const handles: Range[] = [...removed]
|
|
252
253
|
const refusals: SliceRefusal[] = []
|
|
253
254
|
const visit = (node: AstNode): 'skip' | undefined => {
|
|
254
255
|
const site = handleSite(node)
|
package/src/vite-plugin.ts
CHANGED
|
@@ -53,8 +53,9 @@ export function shellSlicePlugin(options: ShellSliceOptions = {}): ShellSliceVit
|
|
|
53
53
|
throw new Error(`${path} is not the contract of the plugin's root ${root}: pass shellSlicePlugin({ root }) the application's root`)
|
|
54
54
|
}
|
|
55
55
|
if (code === undefined) return null
|
|
56
|
-
// Vite watches only its root and the files it loads itself; the shell lies beside `web
|
|
57
|
-
|
|
56
|
+
// Vite watches only its root and the files it loads itself; the shell lies beside `web/`, and the SPA loads
|
|
57
|
+
// no module only the table policies import, which the next read checks again.
|
|
58
|
+
for (const file of shell.keys()) this.addWatchFile(file)
|
|
58
59
|
return { code }
|
|
59
60
|
},
|
|
60
61
|
watchChange() {
|
package/types/src/contract.d.ts
CHANGED
|
@@ -4,6 +4,7 @@ import { type TablePolicies, type Tables } from '@fabrika/db/tables';
|
|
|
4
4
|
import { type ParsedModule } from './ast';
|
|
5
5
|
import { type Finding } from './finding';
|
|
6
6
|
import { ModuleGraph } from './graph';
|
|
7
|
+
import { type Range } from './slice';
|
|
7
8
|
export { ContractError } from '@fabrika/app/contract';
|
|
8
9
|
export { accessTables } from '@fabrika/db/tables';
|
|
9
10
|
export { type Finding, formatFinding } from './finding';
|
|
@@ -39,6 +40,16 @@ export declare function readTablesSource(files: ReadonlyMap<string, string>): Ta
|
|
|
39
40
|
* `src/db/schema.generated.ts`) and the configuration `readContract` names. Throws `ContractSourceError`.
|
|
40
41
|
*/
|
|
41
42
|
export declare function readTablesDefinition(files: ReadonlyMap<string, string>): Tables<unknown, unknown> | null;
|
|
43
|
+
/** The policies `readTablesDefinition` reads, and where the `defineTables(…)(…)` call that declares them stands. */
|
|
44
|
+
export interface TablesDeclaration {
|
|
45
|
+
readonly tables: Tables<unknown, unknown>;
|
|
46
|
+
/** The module of the call: `src/db/tables.ts`, or the module it re-exports `tables` from. */
|
|
47
|
+
readonly file: string;
|
|
48
|
+
/** The call's offsets in that module's text, the same in its shell slice. */
|
|
49
|
+
readonly call: Range;
|
|
50
|
+
}
|
|
51
|
+
/** `readTablesDefinition` with the place of the call; `null` when `files` has no `src/db/tables.ts`. */
|
|
52
|
+
export declare function readTablesDeclaration(files: ReadonlyMap<string, string>): TablesDeclaration | null;
|
|
42
53
|
/**
|
|
43
54
|
* The contract `defineContract` returns in the evaluated module, read from source; throws `ContractSourceError`.
|
|
44
55
|
* Besides the contract's modules, `files` carries every `package.json`, `tsconfig.json`, `jsconfig.json` and
|
package/types/src/limits.d.ts
CHANGED
|
@@ -20,6 +20,6 @@ export declare const CONTRACT_LIMITS: {
|
|
|
20
20
|
readonly astDepth: 128;
|
|
21
21
|
/** Schema nodes once every shared schema is expanded where the contract uses it, over the whole contract. */
|
|
22
22
|
readonly schemaNodes: 60000;
|
|
23
|
-
/** Nesting of one schema: objects, arrays, unions and wrappers. */
|
|
24
|
-
readonly schemaDepth:
|
|
23
|
+
/** Nesting of one schema: objects, arrays, unions and wrappers; it bounds the recursion of compiling and diffing one. */
|
|
24
|
+
readonly schemaDepth: 64;
|
|
25
25
|
};
|
|
@@ -2,6 +2,6 @@
|
|
|
2
2
|
export declare const applicationModules: (root: string) => RegExp;
|
|
3
3
|
/**
|
|
4
4
|
* The shell modules under `root`, by absolute path, as their slices; none without a `fabrika.contract.ts` there.
|
|
5
|
-
* Throws the reader's refusal when the static reader refuses the shell.
|
|
5
|
+
* Throws the reader's refusal when the static reader refuses the shell or the table policies.
|
|
6
6
|
*/
|
|
7
7
|
export declare function shellModules(root: string): ReadonlyMap<string, string>;
|
package/types/src/slice.d.ts
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
import { type AstNode, type ParsedModule, type ParseResult } from './ast';
|
|
2
2
|
import type { Finding } from './finding';
|
|
3
|
+
export interface Range {
|
|
4
|
+
readonly start: number;
|
|
5
|
+
readonly end: number;
|
|
6
|
+
}
|
|
7
|
+
export declare const offsets: (node: AstNode) => Range | undefined;
|
|
3
8
|
/** A `.handle(…)` the slice cannot keep out of the shell, at its `handle`; the reader and the SPA build refuse it. */
|
|
4
9
|
export interface SliceRefusal {
|
|
5
10
|
readonly node: AstNode;
|
|
@@ -13,9 +18,10 @@ export interface ShellSlice {
|
|
|
13
18
|
}
|
|
14
19
|
/**
|
|
15
20
|
* The slice of one parsed module: `module`'s text with every `.handle(…)` and everything only the handlers used
|
|
16
|
-
* blanked. A module without a handler comes back unchanged.
|
|
21
|
+
* blanked. A module without a handler comes back unchanged. Each range of `removed` goes as a handler does, with what
|
|
22
|
+
* only it uses.
|
|
17
23
|
*/
|
|
18
|
-
export declare function moduleSlice(module: ParsedModule, text: string): ShellSlice;
|
|
24
|
+
export declare function moduleSlice(module: ParsedModule, text: string, removed?: readonly Range[]): ShellSlice;
|
|
19
25
|
/**
|
|
20
26
|
* The slice of a module's text. Text that does not parse comes back unchanged, so the reader reports the parse error
|
|
21
27
|
* where it is.
|