@fabrika/check 0.9.0-dev.3 → 0.9.0-dev.30
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 -7
- package/src/ast.ts +69 -6
- package/src/bun-plugin.ts +4 -27
- package/src/check.ts +157 -44
- package/src/cli.ts +34 -8
- package/src/contract.ts +67 -11
- package/src/dialect.ts +29 -0
- package/src/finding.ts +21 -8
- package/src/graph.ts +49 -12
- package/src/index.ts +4 -12
- package/src/inputs.ts +4 -2
- package/src/layout.ts +41 -12
- package/src/limits.ts +15 -5
- package/src/migration-base.ts +85 -0
- package/src/reader/schema.ts +42 -512
- package/src/reader/surface.ts +48 -7
- package/src/reader/value.ts +41 -8
- package/src/resolution.ts +2 -2
- package/src/rules/cloudflare.ts +59 -0
- package/src/rules/compile.ts +32 -0
- package/src/rules/contract.ts +3 -2
- package/src/rules/framework.ts +2 -2
- package/src/rules/identity.ts +6 -9
- package/src/rules/literal.ts +42 -9
- 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 +172 -19
- package/src/rules/tests.ts +9 -60
- package/src/rules/types.ts +17 -16
- package/src/shell-modules.ts +53 -0
- package/src/slice.ts +6 -5
- package/src/source-maps.ts +2 -0
- package/src/trust.ts +8 -4
- package/src/vite-plugin.ts +65 -0
- package/types/src/ast.d.ts +6 -0
- package/types/src/check.d.ts +3 -0
- package/types/src/contract.d.ts +13 -1
- package/types/src/dialect.d.ts +8 -0
- package/types/src/finding.d.ts +7 -6
- package/types/src/graph.d.ts +12 -2
- package/types/src/index.d.ts +4 -3
- package/types/src/layout.d.ts +25 -10
- package/types/src/limits.d.ts +14 -4
- 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 +3 -3
- 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/literal.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 +18 -4
- package/types/src/rules/tests.d.ts +6 -14
- package/types/src/rules/types.d.ts +2 -0
- package/types/src/shell-modules.d.ts +7 -0
- package/types/src/slice.d.ts +8 -2
- package/types/src/vite-plugin.d.ts +27 -0
- package/src/endpoint-fixtures.ts +0 -221
- package/src/rules/crontab.ts +0 -147
- 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/crontab.d.ts +0 -15
- 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/reader/surface.ts
CHANGED
|
@@ -18,6 +18,7 @@ import {
|
|
|
18
18
|
type ExportAccessDeclaration,
|
|
19
19
|
type FileKind,
|
|
20
20
|
type ImportDeclaration,
|
|
21
|
+
isPath,
|
|
21
22
|
type KitImportDeclaration,
|
|
22
23
|
kitImportsSection,
|
|
23
24
|
kitUnknownOptions,
|
|
@@ -31,11 +32,13 @@ import {
|
|
|
31
32
|
type SpaAccessDeclaration,
|
|
32
33
|
type Surface,
|
|
33
34
|
SURFACE_SECTIONS,
|
|
35
|
+
type TerminalDeclaration,
|
|
34
36
|
type TokenPurpose,
|
|
35
37
|
type TokenPurposes,
|
|
36
38
|
unknownOptions,
|
|
37
39
|
type Vocabulary,
|
|
38
40
|
} from '@fabrika/app/contract'
|
|
41
|
+
import { DataObject } from '@fabrika/app/schema'
|
|
39
42
|
import {
|
|
40
43
|
type CspSource,
|
|
41
44
|
isLocale,
|
|
@@ -79,7 +82,7 @@ const CONTRACT_ALLOWED = 'export const contract = app.contract((c) => ({ … }))
|
|
|
79
82
|
/** Which builders a surface is written with: `defineContract`'s `c`, or the staged kit's (`app.*` and its `c`). */
|
|
80
83
|
type Form = 'surface' | 'kit'
|
|
81
84
|
const ACCESS_ALLOWED =
|
|
82
|
-
|
|
85
|
+
"{ member: true }, { action, over }, { action, in, from }, { action, on, from }, { action, creates }, { action, app: true }, { anyOf: ['<action>', …], app: true }, { capabilities: { <name>: … } } or { public: true }"
|
|
83
86
|
const CAPABILITY_ALLOWED = '{ action, over }, { action, in, from } or { action, on, from }'
|
|
84
87
|
|
|
85
88
|
type Entries = ReadonlyMap<string, LiteralValue>
|
|
@@ -180,7 +183,7 @@ class SurfaceReader {
|
|
|
180
183
|
const [definition] = args(value, 'attachment', 1, 1)
|
|
181
184
|
const options = builderOptions(definition, value, 'attachment')
|
|
182
185
|
const row = this.#schema(options.get('row'), value, 'row')
|
|
183
|
-
if (!(row instanceof z.ZodObject)) {
|
|
186
|
+
if (!(row instanceof z.ZodObject || row instanceof DataObject)) {
|
|
184
187
|
throw new Refused(options.get('row')?.at ?? value.at, 'the row is not an object schema', 'row: z.strictObject({ … })')
|
|
185
188
|
}
|
|
186
189
|
const maxRows = optionalNumber(options.get('maxRows'), 'maxRows')
|
|
@@ -213,13 +216,17 @@ class SurfaceReader {
|
|
|
213
216
|
'mail',
|
|
214
217
|
'members',
|
|
215
218
|
'directory',
|
|
219
|
+
'terminal',
|
|
220
|
+
'relations',
|
|
216
221
|
])
|
|
222
|
+
const relations = fields.get('relations')
|
|
217
223
|
const locale = fields.get('locale')
|
|
218
224
|
const displayName = fields.get('displayName')
|
|
219
225
|
const mail = fields.get('mail')
|
|
220
226
|
const members = fields.get('members')
|
|
221
227
|
const memberFields = members === undefined ? undefined : strictFields(members, 'the vocabulary members', ['manage', 'list'], [])
|
|
222
228
|
const directory = fields.get('directory')
|
|
229
|
+
const terminal = fields.get('terminal')
|
|
223
230
|
return {
|
|
224
231
|
app: text(fields.get('app'), value, 'app'),
|
|
225
232
|
...(locale === undefined ? {} : { locale: localeOf(locale, value) }),
|
|
@@ -229,9 +236,17 @@ class SurfaceReader {
|
|
|
229
236
|
? {}
|
|
230
237
|
: { members: { manage: text(memberFields.get('manage'), value, 'manage'), list: text(memberFields.get('list'), value, 'list') } }),
|
|
231
238
|
...(directory === undefined ? {} : { directory: directoryOf(directory, value) }),
|
|
239
|
+
...(terminal === undefined ? {} : { terminal: terminalOf(terminal) }),
|
|
232
240
|
dimensions: this.#each(entries(fields.get('dimensions'), value, 'dimensions'), (name, dimension) => this.#dimension(name, dimension)),
|
|
233
241
|
actions: this.#each(entries(fields.get('actions'), value, 'actions'), (_, description) => text(description, description, 'an action description')),
|
|
234
242
|
roles: this.#each(entries(fields.get('roles'), value, 'roles'), (name, role) => this.#role(name, role)),
|
|
243
|
+
...(relations === undefined
|
|
244
|
+
? {}
|
|
245
|
+
: {
|
|
246
|
+
relations: this.#each(entries(relations, value, 'relations'), (name, relation) => ({
|
|
247
|
+
dimension: text(strictFields(relation, `relation '${name}'`, ['dimension'], []).get('dimension'), relation, 'dimension'),
|
|
248
|
+
})),
|
|
249
|
+
}),
|
|
235
250
|
}
|
|
236
251
|
}
|
|
237
252
|
|
|
@@ -278,16 +293,18 @@ class SurfaceReader {
|
|
|
278
293
|
}
|
|
279
294
|
|
|
280
295
|
#role(name: string, value: LiteralValue): RoleDef<string, string, string> {
|
|
281
|
-
const fields = strictFields(value, `role '${name}'`, ['grants'], ['member', 'external'])
|
|
296
|
+
const fields = strictFields(value, `role '${name}'`, ['grants'], ['member', 'external', 'via'])
|
|
282
297
|
const member = fields.get('member')
|
|
283
298
|
const memberFields = member === undefined ? undefined : strictFields(member, `role '${name}' member`, ['scope', 'assignableBy'], [])
|
|
284
299
|
const external = fields.get('external')
|
|
300
|
+
const via = fields.get('via')
|
|
285
301
|
return {
|
|
286
302
|
grants: texts(fields.get('grants'), value, 'grants'),
|
|
287
303
|
member: memberFields === undefined
|
|
288
304
|
? undefined
|
|
289
305
|
: { scope: text(memberFields.get('scope'), value, 'scope'), assignableBy: texts(memberFields.get('assignableBy'), value, 'assignableBy') },
|
|
290
306
|
...(external === undefined ? {} : { external: externalOf(external) }),
|
|
307
|
+
...(via === undefined ? {} : { via: this.#each(entries(via, value, 'via'), (_, actions) => texts(actions, value, 'via')) }),
|
|
291
308
|
}
|
|
292
309
|
}
|
|
293
310
|
|
|
@@ -592,7 +609,7 @@ class SurfaceReader {
|
|
|
592
609
|
// ── Sections ────────────────────────────────────────────────────────────────────────────────
|
|
593
610
|
|
|
594
611
|
/**
|
|
595
|
-
* `c.files({ … })`: a kind of stored file. Every option but `types`, `maxBytes` and `
|
|
612
|
+
* `c.files({ … })`: a kind of stored file. Every option but `types`, `maxBytes`, `scan` and `inline` is unknown, as the kit
|
|
596
613
|
* records it, so `buildArtifact` refuses a file collection's `owner` in its own words.
|
|
597
614
|
*/
|
|
598
615
|
#files(value: LiteralValue, form: Form): FileKind {
|
|
@@ -601,11 +618,13 @@ class SurfaceReader {
|
|
|
601
618
|
const [kind] = args(at, 'c.files', 1, 1)
|
|
602
619
|
const options = builderOptions(kind, at, 'c.files')
|
|
603
620
|
const scan = options.get('scan')
|
|
621
|
+
const inline = options.get('inline')
|
|
604
622
|
return {
|
|
605
623
|
kind: 'file-kind',
|
|
606
624
|
types: texts(options.get('types'), at, 'types'),
|
|
607
625
|
maxBytes: numeric(options.get('maxBytes'), at, 'maxBytes'),
|
|
608
626
|
scan: scan === undefined ? undefined : boolean(scan, 'scan'),
|
|
627
|
+
inline: inline === undefined ? undefined : boolean(inline, 'inline'),
|
|
609
628
|
unknownOptions: optionKeys(options, 'fileKind'),
|
|
610
629
|
}
|
|
611
630
|
}
|
|
@@ -794,7 +813,7 @@ function plain(value: LiteralValue | undefined): unknown {
|
|
|
794
813
|
}
|
|
795
814
|
|
|
796
815
|
const DECLARED_ALLOWED =
|
|
797
|
-
"sql: 'read' | 'write', unscoped: { <table>: 'read' | 'write' }, connect: ['<import>'], issues: '<link mount>', mailer: 'installation' | 'own', kicks: ['<cron job>'], binds: '<webhook>', signs: ['<token purpose>'], egress: ['https://<host>']"
|
|
816
|
+
"sql: 'read' | 'write', unscoped: { <table>: 'read' | 'write' }, connect: ['<import>'], issues: '<link mount>', mailer: 'installation' | 'own', kicks: ['<cron job>'], binds: '<webhook>', signs: ['<token purpose>'], egress: ['https://<host>' | 'https://*.<domain>'] | 'public' (a cron job)"
|
|
798
817
|
|
|
799
818
|
/** A builder of the kit read as the `defineContract` builder of the same name, which builds the same declaration. */
|
|
800
819
|
const asSurface = (builder: BuilderValue): LiteralValue => ({
|
|
@@ -814,6 +833,9 @@ interface ReadGroup {
|
|
|
814
833
|
const sameCallers = (left: readonly string[], right: readonly string[]): boolean =>
|
|
815
834
|
left.length === right.length && left.every((caller, index) => caller === right[index])
|
|
816
835
|
|
|
836
|
+
/** `defineApp`'s option fields. `caller` (`ctx.caller`) is the runtime's alone: the artifact carries nothing of it. */
|
|
837
|
+
const KIT_OPTIONS = ['imports', 'tokens', 'caller']
|
|
838
|
+
|
|
817
839
|
/**
|
|
818
840
|
* The kit's imports from `defineApp`'s options: each snapshot's identity, the procedures it calls, its deadline;
|
|
819
841
|
* `undefined` without options, where the contract has no imports section.
|
|
@@ -822,7 +844,7 @@ function kitImports(options: LiteralValue | undefined): Record<string, KitImport
|
|
|
822
844
|
if (options === undefined) {
|
|
823
845
|
return undefined
|
|
824
846
|
}
|
|
825
|
-
const imports = strictFields(options, "defineApp's options", [],
|
|
847
|
+
const imports = strictFields(options, "defineApp's options", [], KIT_OPTIONS).get('imports')
|
|
826
848
|
if (imports === undefined) {
|
|
827
849
|
return undefined
|
|
828
850
|
}
|
|
@@ -846,7 +868,7 @@ function kitImports(options: LiteralValue | undefined): Record<string, KitImport
|
|
|
846
868
|
|
|
847
869
|
/** The token purposes of `defineApp`'s options, each `token(Claims, { maxDays })`; `undefined` without any. */
|
|
848
870
|
function kitTokens(options: LiteralValue | undefined): TokenPurposes | undefined {
|
|
849
|
-
const tokens = options === undefined ? undefined : strictFields(options, "defineApp's options", [],
|
|
871
|
+
const tokens = options === undefined ? undefined : strictFields(options, "defineApp's options", [], KIT_OPTIONS).get('tokens')
|
|
850
872
|
if (options === undefined || tokens === undefined) {
|
|
851
873
|
return undefined
|
|
852
874
|
}
|
|
@@ -936,6 +958,16 @@ function directoryOf(value: LiteralValue, owner: Owner): 'labels' {
|
|
|
936
958
|
return directory
|
|
937
959
|
}
|
|
938
960
|
|
|
961
|
+
/** The vocabulary's `terminal: { identify: '/path' }`; `buildArtifact` checks the path itself. */
|
|
962
|
+
function terminalOf(value: LiteralValue): TerminalDeclaration {
|
|
963
|
+
const identify = strictFields(value, 'the vocabulary terminal', ['identify'], []).get('identify')
|
|
964
|
+
const path = text(identify, value, 'terminal.identify')
|
|
965
|
+
if (!isPath(path)) {
|
|
966
|
+
throw new Refused(identify?.at ?? value.at, `terminal.identify is '${path}'`, "terminal: { identify: '/identify' }")
|
|
967
|
+
}
|
|
968
|
+
return { identify: path }
|
|
969
|
+
}
|
|
970
|
+
|
|
939
971
|
function optionalText(value: LiteralValue | undefined, field: string): string | undefined {
|
|
940
972
|
return value === undefined ? undefined : text(value, value, field)
|
|
941
973
|
}
|
|
@@ -1028,6 +1060,12 @@ function isApp(fields: Entries): boolean {
|
|
|
1028
1060
|
return hasKeys(fields, ['action', 'app']) && app?.kind === 'boolean' && app.value
|
|
1029
1061
|
}
|
|
1030
1062
|
|
|
1063
|
+
/** `{ anyOf: [...], app: true }`: `app` is the literal `true`; `buildArtifact` checks the list. */
|
|
1064
|
+
function isAnyOf(fields: Entries): boolean {
|
|
1065
|
+
const app = fields.get('app')
|
|
1066
|
+
return hasKeys(fields, ['anyOf', 'app']) && app?.kind === 'boolean' && app.value
|
|
1067
|
+
}
|
|
1068
|
+
|
|
1031
1069
|
/** `{ public: true }`: `public` is the literal `true`. */
|
|
1032
1070
|
function isPublic(fields: Entries): boolean {
|
|
1033
1071
|
const open = fields.get('public')
|
|
@@ -1056,6 +1094,9 @@ function accessDeclaration(value: LiteralValue | undefined, at: BuilderValue): A
|
|
|
1056
1094
|
if (isApp(fields)) {
|
|
1057
1095
|
return { action: field('action'), app: true }
|
|
1058
1096
|
}
|
|
1097
|
+
if (isAnyOf(fields)) {
|
|
1098
|
+
return { anyOf: texts(fields.get('anyOf'), value ?? at, 'access.anyOf'), app: true }
|
|
1099
|
+
}
|
|
1059
1100
|
if (isPublic(fields)) {
|
|
1060
1101
|
return { public: true }
|
|
1061
1102
|
}
|
package/src/reader/value.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
// Reads the literal subset of `rules/literal.ts` into values, without executing the application: names resolve through
|
|
2
|
-
// `ModuleGraph`, `z` chains build
|
|
2
|
+
// `ModuleGraph`, `z` chains build schemas held as data (each step checked against zod), `c.*` calls stay unapplied for `surface.ts`.
|
|
3
3
|
|
|
4
4
|
import { file, signedToken, WebhookSecret } from '@fabrika/app/contract'
|
|
5
5
|
import type { z } from 'zod/v4'
|
|
6
6
|
import { type AstNode, child, children, identifierName, isConstAssertion, type ParsedModule, staticKey } from '../ast'
|
|
7
|
-
import type
|
|
7
|
+
import { type Located, type ModuleGraph, unwrapValue } from '../graph'
|
|
8
8
|
import { CONTRACT_LIMITS } from '../limits'
|
|
9
9
|
import {
|
|
10
10
|
BUILDER_IMPORTS,
|
|
@@ -18,7 +18,7 @@ import {
|
|
|
18
18
|
stringStepProblem,
|
|
19
19
|
} from '../rules/literal'
|
|
20
20
|
import type { ReaderProblems } from './problems'
|
|
21
|
-
import { buildSchema } from './schema'
|
|
21
|
+
import { buildSchema, type ReadSchema } from './schema'
|
|
22
22
|
|
|
23
23
|
/**
|
|
24
24
|
* The builder kit a call belongs to: `c` of `defineContract` and `l` of its link's `procedures`; `app`, the kit
|
|
@@ -41,7 +41,7 @@ export type LiteralValue =
|
|
|
41
41
|
/** `nodes` counts the schema with every shared part expanded, as the artifact will hold it; `depth` its nesting. */
|
|
42
42
|
| {
|
|
43
43
|
readonly kind: 'schema'
|
|
44
|
-
readonly schema:
|
|
44
|
+
readonly schema: ReadSchema
|
|
45
45
|
readonly nodes: number
|
|
46
46
|
readonly depth: number
|
|
47
47
|
/** The chain that built it, factory first (`iso` for `z.iso.*`), across a const and the methods applied to it. */
|
|
@@ -83,9 +83,22 @@ type Role = { readonly callback: Kit } | { readonly procedures: Kit } | null
|
|
|
83
83
|
const callbackKit = (kind: BuilderKind): Kit => (kind === 'shell' ? 'shell' : kind === 'endpoints' ? 'endpoints' : 'surface')
|
|
84
84
|
|
|
85
85
|
/** The fields `defineVocabulary` copies from its argument, in its order. */
|
|
86
|
-
const VOCABULARY_SECTIONS = [
|
|
86
|
+
const VOCABULARY_SECTIONS = [
|
|
87
|
+
'app',
|
|
88
|
+
'dimensions',
|
|
89
|
+
'actions',
|
|
90
|
+
'roles',
|
|
91
|
+
'relations',
|
|
92
|
+
'locale',
|
|
93
|
+
'displayName',
|
|
94
|
+
'mail',
|
|
95
|
+
'members',
|
|
96
|
+
'directory',
|
|
97
|
+
'terminal',
|
|
98
|
+
] as const
|
|
87
99
|
|
|
88
100
|
const ALLOWED_VALUE = 'object and array literals, strings, numbers, booleans, null, the c.* and z.* builders, and const imports of such values'
|
|
101
|
+
const ALLOWED_SPREAD = "a spread of a const whose value is an object literal (this module's or a named import), or the entries written out"
|
|
89
102
|
|
|
90
103
|
export class ValueReader {
|
|
91
104
|
readonly #graph: ModuleGraph
|
|
@@ -244,13 +257,19 @@ export class ValueReader {
|
|
|
244
257
|
continue
|
|
245
258
|
}
|
|
246
259
|
const at = { module: located.module, node: property }
|
|
260
|
+
if (property.type === 'SpreadElement') {
|
|
261
|
+
const spread = this.#spread(at, scope)
|
|
262
|
+
if (spread === null) complete = false
|
|
263
|
+
else for (const [key, value] of spread) entries.set(key, value)
|
|
264
|
+
continue
|
|
265
|
+
}
|
|
247
266
|
const key = staticKey(property, 'key')
|
|
248
267
|
const value = child(property, 'value')
|
|
249
268
|
if (
|
|
250
269
|
property.type !== 'Property' || property['computed'] === true || property['kind'] !== 'init' || property['method'] === true
|
|
251
270
|
|| key === undefined || key === '__proto__' || value === undefined
|
|
252
271
|
) {
|
|
253
|
-
this.#problems.add(at, 'a
|
|
272
|
+
this.#problems.add(at, 'a computed key, method or __proto__ key', 'a plain or quoted key with a literal value')
|
|
254
273
|
complete = false
|
|
255
274
|
continue
|
|
256
275
|
}
|
|
@@ -265,6 +284,20 @@ export class ValueReader {
|
|
|
265
284
|
return complete ? { kind: 'object', entries, at: located } : null
|
|
266
285
|
}
|
|
267
286
|
|
|
287
|
+
/** The entries `{ ...x }` copies: those of an object literal, in place or a const's (`rules/literal.ts`' spread). */
|
|
288
|
+
#spread(at: Located, scope: Scope): ReadonlyMap<string, LiteralValue> | null {
|
|
289
|
+
const argument = child(at.node, 'argument')
|
|
290
|
+
const value = argument === undefined ? undefined : unwrapValue(argument)
|
|
291
|
+
const target = value === undefined ? undefined : this.#graph.resolveValue({ module: at.module, node: value })
|
|
292
|
+
const read = value === undefined || target?.node.type !== 'ObjectExpression'
|
|
293
|
+
? undefined
|
|
294
|
+
: this.#read({ module: at.module, node: value }, scope, null)
|
|
295
|
+
if (read === null) return null
|
|
296
|
+
if (read?.kind === 'object') return read.entries
|
|
297
|
+
this.#problems.add(at, 'spread of a value that is not a constant object literal', ALLOWED_SPREAD)
|
|
298
|
+
return null
|
|
299
|
+
}
|
|
300
|
+
|
|
268
301
|
#identifier(located: Located, scope: Scope): LiteralValue | null {
|
|
269
302
|
const name = identifierName(located.node) ?? ''
|
|
270
303
|
const builder = this.#builderOf(located.module, name)
|
|
@@ -541,7 +574,7 @@ export class ValueReader {
|
|
|
541
574
|
return args === null ? null : { kind: 'contract', args, at: located }
|
|
542
575
|
}
|
|
543
576
|
|
|
544
|
-
/** `z.<factory>(…)` or `z.iso.<format>(…)`, then `.<method>(…)` links, each
|
|
577
|
+
/** `z.<factory>(…)` or `z.iso.<format>(…)`, then `.<method>(…)` links, each checked against zod as it is applied. */
|
|
545
578
|
#zodChain(located: Located, links: readonly Link[], scope: Scope): LiteralValue | null {
|
|
546
579
|
const isIso = links[0]?.kind === 'member' && links[0].name === 'iso'
|
|
547
580
|
const steps = this.#steps(located, links, isIso ? 1 : 0)
|
|
@@ -583,7 +616,7 @@ export class ValueReader {
|
|
|
583
616
|
return null
|
|
584
617
|
}
|
|
585
618
|
}
|
|
586
|
-
let schema:
|
|
619
|
+
let schema: ReadSchema | undefined = receiver?.schema
|
|
587
620
|
let size: SchemaSize = receiver === null ? { nodes: 0, depth: 0 } : { nodes: receiver.nodes, depth: receiver.depth }
|
|
588
621
|
for (const step of steps) {
|
|
589
622
|
const at = { module: located.module, node: step.call }
|
package/src/resolution.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { isBuiltin } from 'node:module'
|
|
2
2
|
import { z } from 'zod/v4'
|
|
3
3
|
import { parseJsonc } from './jsonc'
|
|
4
|
-
import {
|
|
4
|
+
import { isRelativeSpecifier, isSkippedPath, joinRelative, resolveRelative } from './layout'
|
|
5
5
|
|
|
6
6
|
/** Pattern → targets. A target is a bare specifier, an absolute path, or `./<path from the root>` (`../…` above it). */
|
|
7
7
|
type MappingTable = ReadonlyMap<string, readonly string[]>
|
|
@@ -139,7 +139,7 @@ export const readImportMappings = (files: ReadonlyMap<string, string>): ImportMa
|
|
|
139
139
|
},
|
|
140
140
|
mayExist: (target) => {
|
|
141
141
|
const path = target.startsWith('/') ? undefined : joinRelative('package.json', target)
|
|
142
|
-
if (path === undefined || path
|
|
142
|
+
if (path === undefined || isSkippedPath(path)) return true
|
|
143
143
|
return resolveRelative((candidate) => files.has(candidate), 'package.json', `./${path}`).kind !== 'missing'
|
|
144
144
|
},
|
|
145
145
|
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { CLOUDFLARE_MAX_JOB_SEGMENT_MS } from '@fabrika/app/contract'
|
|
2
|
+
import { type ContractArtifact, type DeclaredEgress, PUBLIC_EGRESS } from '@fabrika/auth-core'
|
|
3
|
+
import type { Finding } from '../finding'
|
|
4
|
+
import { CONTRACT_FILE } from '../layout'
|
|
5
|
+
|
|
6
|
+
const at = (rule: 'cloudflare-scan' | 'cloudflare-job-segment', message: string, allowed: string): Finding => ({
|
|
7
|
+
file: CONTRACT_FILE,
|
|
8
|
+
line: 1,
|
|
9
|
+
column: 1,
|
|
10
|
+
severity: 'refusal',
|
|
11
|
+
rule,
|
|
12
|
+
message,
|
|
13
|
+
allowed,
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
/** The outside calls a site declares, as the hint names them: its `egress` origins and its `connect` imports. */
|
|
17
|
+
const outsideCalls = (reach: { readonly egress?: DeclaredEgress; readonly connect: readonly string[] }): string[] => [
|
|
18
|
+
...(reach.egress === PUBLIC_EGRESS ? [`egress '${PUBLIC_EGRESS}'`] : (reach.egress ?? []).map((origin) => `egress ${origin}`)),
|
|
19
|
+
...reach.connect.map((name) => `connect ${name}`),
|
|
20
|
+
]
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* What an application on Cloudflare cannot declare (ADR-0065): a file kind that scans, because no scanner runs there; a
|
|
24
|
+
* job longer than one segment, because the object runs it inside an alarm or a request. And a hint for a mutation that
|
|
25
|
+
* declares an outside call: the object runs one transaction at a time, and the mutation holds it while the call waits
|
|
26
|
+
* (backlog 216 item 7).
|
|
27
|
+
*/
|
|
28
|
+
export const checkCloudflareContract = (artifact: ContractArtifact): Finding[] => [
|
|
29
|
+
...Object.entries(artifact.fileKinds ?? {}).filter(([, kind]) => kind.scan === true).map(([name]) =>
|
|
30
|
+
at(
|
|
31
|
+
'cloudflare-scan',
|
|
32
|
+
`the file kind ${name} scans its files (scan: true), which an application on Cloudflare cannot: no scanner runs there`,
|
|
33
|
+
`c.files({ types, maxBytes }) without scan for ${name}, and no public procedure that takes its files`,
|
|
34
|
+
)
|
|
35
|
+
),
|
|
36
|
+
...Object.entries(artifact.jobs).filter(([, job]) => job.timeoutMs > CLOUDFLARE_MAX_JOB_SEGMENT_MS).map(([name, job]) =>
|
|
37
|
+
at(
|
|
38
|
+
'cloudflare-job-segment',
|
|
39
|
+
`the job ${name} runs up to ${job.timeoutMs} ms, and a job segment on Cloudflare runs at most ${CLOUDFLARE_MAX_JOB_SEGMENT_MS} ms`,
|
|
40
|
+
`limits.timeoutMs at most ${CLOUDFLARE_MAX_JOB_SEGMENT_MS} for ${name}: claim a bounded batch per run, and the next run takes the rest; a Workflow for one longer run is planned (backlog 230)`,
|
|
41
|
+
)
|
|
42
|
+
),
|
|
43
|
+
...artifact.endpoints.filter((endpoint) => endpoint.kind === 'mutation').flatMap((endpoint): Finding[] => {
|
|
44
|
+
const reach = artifact.declared?.[endpoint.id]
|
|
45
|
+
const calls = reach === undefined ? [] : outsideCalls(reach)
|
|
46
|
+
if (calls.length === 0) return []
|
|
47
|
+
return [{
|
|
48
|
+
file: CONTRACT_FILE,
|
|
49
|
+
line: 1,
|
|
50
|
+
column: 1,
|
|
51
|
+
severity: 'hint',
|
|
52
|
+
rule: 'cloudflare-held-call',
|
|
53
|
+
message: `${endpoint.id} is a mutation that declares ${
|
|
54
|
+
calls.join(', ')
|
|
55
|
+
}: on Cloudflare it waits for that call inside the object's transaction, which holds the whole environment`,
|
|
56
|
+
allowed: "record a row and ctx.kick('<job>'); the job declares the call and makes it outside ctx.transaction",
|
|
57
|
+
}]
|
|
58
|
+
}),
|
|
59
|
+
]
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { ContractError } from '@fabrika/app/contract'
|
|
2
|
+
import { ContractSourceError, readContractSource, readTablesDefinition } from '../contract'
|
|
3
|
+
import type { Finding } from '../finding'
|
|
4
|
+
import { CONTRACT_FILE } from '../layout'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* What the reader refuses of `src/db/tables.ts`, and a contract that does not compile against its policies: Control
|
|
8
|
+
* reads the commit with the same calls and refuses the deploy. `readable` is false while the literal, trust or
|
|
9
|
+
* contract rules report the sources, so a refusal is not reported twice.
|
|
10
|
+
*/
|
|
11
|
+
export const checkContractCompiles = (files: ReadonlyMap<string, string>, readable: boolean): Finding[] => {
|
|
12
|
+
if (!readable) return []
|
|
13
|
+
try {
|
|
14
|
+
const tables = readTablesDefinition(files)
|
|
15
|
+
if (files.has(CONTRACT_FILE) && tables !== null) readContractSource(files, tables)
|
|
16
|
+
return []
|
|
17
|
+
} catch (error) {
|
|
18
|
+
if (error instanceof ContractSourceError) return [...error.findings]
|
|
19
|
+
if (error instanceof ContractError) {
|
|
20
|
+
return error.problems.map((problem): Finding => ({
|
|
21
|
+
file: CONTRACT_FILE,
|
|
22
|
+
line: 1,
|
|
23
|
+
column: 1,
|
|
24
|
+
severity: 'refusal',
|
|
25
|
+
rule: 'contract',
|
|
26
|
+
message: problem,
|
|
27
|
+
allowed: 'a contract that compiles against src/db/tables.ts',
|
|
28
|
+
}))
|
|
29
|
+
}
|
|
30
|
+
throw error
|
|
31
|
+
}
|
|
32
|
+
}
|
package/src/rules/contract.ts
CHANGED
|
@@ -20,8 +20,9 @@ export const checkContractRead = (graph: ModuleGraph): Finding[] => {
|
|
|
20
20
|
return [...read.findings]
|
|
21
21
|
}
|
|
22
22
|
const module = graph.module(CONTRACT_FILE)
|
|
23
|
-
const binding = module === undefined ? undefined : graph.
|
|
24
|
-
|
|
23
|
+
const binding = module === undefined ? undefined : graph.exported(module, CONTRACT_EXPORT)
|
|
24
|
+
// A contract declared in another module (an import or a re-export) is reported at this file's start.
|
|
25
|
+
const at = binding?.kind === 'const' && binding.module === module && binding.init !== undefined ? positionOf(binding.init) : { line: 1, column: 1 }
|
|
25
26
|
try {
|
|
26
27
|
return memberAdmitsExternal(describeContract(read.contract), at)
|
|
27
28
|
} catch (error) {
|
package/src/rules/framework.ts
CHANGED
|
@@ -79,7 +79,7 @@ export const trustedBindings = (file: string, program: AstNode, mappings: Import
|
|
|
79
79
|
}
|
|
80
80
|
|
|
81
81
|
/**
|
|
82
|
-
* The trusted binding a member chain starts from (`Database.
|
|
82
|
+
* The trusted binding a member chain starts from (`Database.fromEngine`, `db['Database'].fromEngine` → `Database`, `db`),
|
|
83
83
|
* when the name at `ancestors` denotes that declaration and no local that shadows it.
|
|
84
84
|
*/
|
|
85
85
|
const trustedRoot = (
|
|
@@ -135,7 +135,7 @@ const WRITERS = new Set([
|
|
|
135
135
|
|
|
136
136
|
/**
|
|
137
137
|
* A write to a member of a value from `@fabrika/*` or `zod`: an assignment, `delete`, `++`, or a writer call on it. A
|
|
138
|
-
* replaced `
|
|
138
|
+
* replaced `openPostgres` receives the database URL; a patched schema changes what the contract admits. A write
|
|
139
139
|
* through a prototype is `prototype-assignment`'s. A local that shadows such a name is not one.
|
|
140
140
|
*/
|
|
141
141
|
export const checkFrameworkWrite = (node: AstNode, ancestors: readonly AstNode[], bindings: TrustedBindings, report: Report): void => {
|
package/src/rules/identity.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { type AstNode, child, children, type ParsedModule, staticKey, walk } from '../ast'
|
|
2
2
|
import { type Finding, reporter } from '../finding'
|
|
3
|
-
import { type Located, type ModuleGraph, propertyValue } from '../graph'
|
|
3
|
+
import { type Located, type ModuleGraph, objectEntries, propertyValue } from '../graph'
|
|
4
4
|
|
|
5
5
|
const IDENTITY_FIELDS = new Set([
|
|
6
6
|
'userId',
|
|
@@ -35,10 +35,7 @@ const schemaFields = (located: Located, graph: ModuleGraph, depth = 0): Readonly
|
|
|
35
35
|
if (method === undefined || !argument) return fields
|
|
36
36
|
if (SHAPE_METHODS.has(method)) {
|
|
37
37
|
const shape = graph.resolveValue({ module: value.module, node: argument })
|
|
38
|
-
for (const
|
|
39
|
-
const key = property === null ? undefined : staticKey(property, 'key')
|
|
40
|
-
if (key !== undefined) fields.add(key)
|
|
41
|
-
}
|
|
38
|
+
for (const key of shape === undefined ? [] : objectEntries(graph, shape).keys()) fields.add(key)
|
|
42
39
|
} else if (MERGE_METHODS.has(method)) add({ module: value.module, node: argument })
|
|
43
40
|
return fields
|
|
44
41
|
}
|
|
@@ -46,8 +43,8 @@ const schemaFields = (located: Located, graph: ModuleGraph, depth = 0): Readonly
|
|
|
46
43
|
/** The field an `in`/`on` access form takes from the input; the framework removes it and checks the caller's access to it. */
|
|
47
44
|
const checkedField = (access: Located | undefined, graph: ModuleGraph): string | undefined => {
|
|
48
45
|
const value = access === undefined ? undefined : graph.resolveValue(access)
|
|
49
|
-
if (value === undefined || (propertyValue(value, 'in') === undefined && propertyValue(value, 'on') === undefined)) return undefined
|
|
50
|
-
const from = propertyValue(value, 'from')
|
|
46
|
+
if (value === undefined || (propertyValue(graph, value, 'in') === undefined && propertyValue(graph, value, 'on') === undefined)) return undefined
|
|
47
|
+
const from = propertyValue(graph, value, 'from')
|
|
51
48
|
return from === undefined ? undefined : graph.resolveString(from)
|
|
52
49
|
}
|
|
53
50
|
|
|
@@ -61,9 +58,9 @@ export const checkClientIdentity = (contract: ParsedModule, graph: ModuleGraph):
|
|
|
61
58
|
for (const argument of children(node, 'arguments')) {
|
|
62
59
|
if (argument === null) continue
|
|
63
60
|
const options = graph.resolveValue({ module: contract, node: argument })
|
|
64
|
-
const input = options === undefined ? undefined : propertyValue(options, 'input')
|
|
61
|
+
const input = options === undefined ? undefined : propertyValue(graph, options, 'input')
|
|
65
62
|
if (options === undefined || input === undefined) continue
|
|
66
|
-
const checked = checkedField(propertyValue(options, 'access'), graph)
|
|
63
|
+
const checked = checkedField(propertyValue(graph, options, 'access'), graph)
|
|
67
64
|
const at: AstNode = input.module === contract ? input.node : argument
|
|
68
65
|
for (const field of schemaFields(input, graph)) {
|
|
69
66
|
if (!IDENTITY_FIELDS.has(field) || field === checked) continue
|
package/src/rules/literal.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { type AstNode, child, children, identifierName, isConstAssertion, type ParsedModule, staticKey, staticString, stringField } from '../ast'
|
|
2
2
|
import { type Finding, type Report, reporter } from '../finding'
|
|
3
|
-
import type
|
|
3
|
+
import { type Binding, type ModuleGraph, unwrapValue } from '../graph'
|
|
4
4
|
import { isRelativeSpecifier } from '../layout'
|
|
5
5
|
|
|
6
6
|
/**
|
|
@@ -29,6 +29,8 @@ export type BuilderKind =
|
|
|
29
29
|
/** The only imported names a literal module may use: the builders the platform reads without executing them. */
|
|
30
30
|
export const BUILDER_IMPORTS: Readonly<Record<string, Readonly<Record<string, BuilderKind>>>> = {
|
|
31
31
|
'zod/v4': { z: 'zod', '*': 'zod' },
|
|
32
|
+
// The same subset, each schema held as data; the reader reads both alike.
|
|
33
|
+
'@fabrika/app/schema': { z: 'zod', '*': 'zod' },
|
|
32
34
|
'@fabrika/app/contract': {
|
|
33
35
|
defineContract: 'contract',
|
|
34
36
|
defineVocabulary: 'vocabulary',
|
|
@@ -163,8 +165,9 @@ const ALLOWED_VALUE = 'object and array literals, strings, numbers, booleans, nu
|
|
|
163
165
|
const ALLOWED_SCHEMA = 'a schema of the supported z subset (packages/check/CLAUDE.md): strict objects, no transforms, refinements or pipes'
|
|
164
166
|
const ALLOWED_CALLBACK =
|
|
165
167
|
'a function only as a builder callback: defineContract(…, (c) => ({ … })), app.contract((c) => ({ … })), app.group(…, (r) => ({ … })), procedures: (l) => ({ … }) of a link or an export, and the handler of .handle(…)'
|
|
168
|
+
const ALLOWED_SPREAD = "a spread of a const whose value is an object literal (this module's or a named import), or the entries written out"
|
|
166
169
|
const ALLOWED_IMPORT =
|
|
167
|
-
'z from zod/v4, defineContract, defineVocabulary or defineApp from @fabrika/app/contract, defineTables from @fabrika/db/tables, or a literal const of the application'
|
|
170
|
+
'z from zod/v4 or @fabrika/app/schema, defineContract, defineVocabulary or defineApp from @fabrika/app/contract, defineTables from @fabrika/db/tables, or a literal const of the application'
|
|
168
171
|
|
|
169
172
|
export type Link = { readonly kind: 'member'; readonly name: string | undefined; readonly node: AstNode } | {
|
|
170
173
|
readonly kind: 'call'
|
|
@@ -525,7 +528,7 @@ const readModule = (module: ParsedModule, graph: ModuleGraph, findings: Finding[
|
|
|
525
528
|
for (const property of children(node, 'properties')) {
|
|
526
529
|
if (property === null) continue
|
|
527
530
|
if (property.type === 'SpreadElement') {
|
|
528
|
-
|
|
531
|
+
checkObjectSpread(property, scope, dependencies)
|
|
529
532
|
continue
|
|
530
533
|
}
|
|
531
534
|
if (property['computed'] === true) {
|
|
@@ -547,6 +550,23 @@ const readModule = (module: ParsedModule, graph: ModuleGraph, findings: Finding[
|
|
|
547
550
|
}
|
|
548
551
|
}
|
|
549
552
|
|
|
553
|
+
/**
|
|
554
|
+
* `{ ...Base }`: a spread of an object literal, written in place or as a const of this module or an imported one (through
|
|
555
|
+
* `as const`, `satisfies` and parentheses), whose entries the reader copies as evaluating it would.
|
|
556
|
+
*/
|
|
557
|
+
const checkObjectSpread = (spread: AstNode, scope: Scope, dependencies: string[]): void => {
|
|
558
|
+
const argument = child(spread, 'argument')
|
|
559
|
+
const value = argument === undefined ? undefined : unwrapValue(argument)
|
|
560
|
+
if (value?.type === 'ObjectExpression') return checkObject(value, scope, dependencies)
|
|
561
|
+
const name = identifierName(value)
|
|
562
|
+
if (value === undefined || name === undefined) return fail(spread, 'spread of a value that is not a constant object', ALLOWED_SPREAD)
|
|
563
|
+
const before = reported
|
|
564
|
+
checkIdentifier(value, scope, dependencies)
|
|
565
|
+
if (reported !== before) return
|
|
566
|
+
const resolved = graph.resolveValue({ module, node: value })
|
|
567
|
+
if (resolved?.node.type !== 'ObjectExpression') fail(spread, `spread of '${name}', which is not a constant object literal`, ALLOWED_SPREAD)
|
|
568
|
+
}
|
|
569
|
+
|
|
550
570
|
const checkExpression = (node: AstNode, scope: Scope, dependencies: string[]): void => {
|
|
551
571
|
switch (node.type) {
|
|
552
572
|
case 'Literal':
|
|
@@ -615,8 +635,17 @@ const readModule = (module: ParsedModule, graph: ModuleGraph, findings: Finding[
|
|
|
615
635
|
const declaration = statement.type === 'ExportNamedDeclaration' ? (child(statement, 'declaration') ?? statement) : statement
|
|
616
636
|
if (declaration.type === 'TSTypeAliasDeclaration' || declaration.type === 'TSInterfaceDeclaration') continue
|
|
617
637
|
if (declaration.type === 'ExportNamedDeclaration') {
|
|
618
|
-
|
|
619
|
-
|
|
638
|
+
// A relative re-export is checked with the imports (`checkLiteralGraph`), as its binding. Bun erases one whose
|
|
639
|
+
// every specifier is a type, and evaluates the source of `export {} from`, as of `import {} from`.
|
|
640
|
+
const source = staticString(child(declaration, 'source'))
|
|
641
|
+
if (source !== undefined) {
|
|
642
|
+
const specifiers = children(declaration, 'specifiers').filter((item): item is AstNode => item !== null)
|
|
643
|
+
const erased = stringField(declaration, 'exportKind') === 'type'
|
|
644
|
+
|| (specifiers.length > 0 && specifiers.every((item) => stringField(item, 'exportKind') === 'type'))
|
|
645
|
+
if (!erased && specifiers.length === 0) fail(declaration, `side-effect re-export of '${source}'`, 'named imports of literal consts')
|
|
646
|
+
else if (!erased && !isRelativeSpecifier(source)) {
|
|
647
|
+
fail(declaration, `re-export from the package '${source}'`, 'import the builder in the module that uses it')
|
|
648
|
+
}
|
|
620
649
|
continue
|
|
621
650
|
}
|
|
622
651
|
for (const item of children(declaration, 'specifiers')) {
|
|
@@ -663,7 +692,7 @@ export const checkLiteralGraph = (entries: readonly string[], graph: ModuleGraph
|
|
|
663
692
|
for (let module = queue.shift(); module !== undefined; module = queue.shift()) {
|
|
664
693
|
if (summaries.has(module.file)) continue
|
|
665
694
|
summaries.set(module.file, readModule(module, graph, findings))
|
|
666
|
-
for (const binding of graph.
|
|
695
|
+
for (const binding of graph.links(module)) {
|
|
667
696
|
const target = graph.resolve(module.file, binding.specifier)
|
|
668
697
|
if (target !== undefined) queue.push(target)
|
|
669
698
|
}
|
|
@@ -707,7 +736,11 @@ export const checkLiteralGraph = (entries: readonly string[], graph: ModuleGraph
|
|
|
707
736
|
for (const name of summary.consts.keys()) {
|
|
708
737
|
isLiteral(name === 'default' ? graph.exported(summary.module, 'default') : graph.binding(summary.module, name))
|
|
709
738
|
}
|
|
710
|
-
|
|
739
|
+
const index = graph.index(summary.module)
|
|
740
|
+
for (const imported of graph.links(summary.module)) {
|
|
741
|
+
const reexport = index.reexports.get(imported.local) === imported
|
|
742
|
+
// A package re-export is refused at its declaration (`readModule`).
|
|
743
|
+
if (reexport && !isRelativeSpecifier(imported.specifier)) continue
|
|
711
744
|
if (!isRelativeSpecifier(imported.specifier)) {
|
|
712
745
|
const browserSafe = SERVER_ENTRY_OF[imported.specifier]
|
|
713
746
|
if (browserSafe !== undefined && BUILDER_IMPORTS[browserSafe]?.[imported.imported] !== undefined) {
|
|
@@ -739,7 +772,7 @@ export const checkLiteralGraph = (entries: readonly string[], graph: ModuleGraph
|
|
|
739
772
|
)
|
|
740
773
|
continue
|
|
741
774
|
}
|
|
742
|
-
const binding = graph.binding(summary.module, imported.local)
|
|
775
|
+
const binding = reexport ? graph.exported(summary.module, imported.local) : graph.binding(summary.module, imported.local)
|
|
743
776
|
if (binding.kind === 'unresolved') summary.report(imported.node, 'literal', binding.reason, 'a named import of a const the module exports')
|
|
744
777
|
else if (!isLiteral(binding) && !refusedInPlace(binding)) {
|
|
745
778
|
summary.report(
|
|
@@ -759,7 +792,7 @@ const reportImportCycles = (summaries: ReadonlyMap<string, LiteralSummary>, grap
|
|
|
759
792
|
const state = new Map<string, 'visiting' | 'done'>()
|
|
760
793
|
const visit = (summary: LiteralSummary): void => {
|
|
761
794
|
state.set(summary.module.file, 'visiting')
|
|
762
|
-
for (const imported of graph.
|
|
795
|
+
for (const imported of graph.links(summary.module)) {
|
|
763
796
|
const target = graph.resolve(summary.module.file, imported.specifier)
|
|
764
797
|
const next = target === undefined ? undefined : summaries.get(target.file)
|
|
765
798
|
if (next === undefined) continue
|