@fabrika/check 0.9.0-cloudflare.2 → 0.9.0-cloudflare.3

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fabrika/check",
3
- "version": "0.9.0-cloudflare.2",
3
+ "version": "0.9.0-cloudflare.3",
4
4
  "type": "module",
5
5
  "repository": {
6
6
  "type": "git",
@@ -47,11 +47,11 @@
47
47
  },
48
48
  "dependencies": {
49
49
  "@babel/parser": "8.0.7",
50
- "@fabrika/app": "0.9.0-cloudflare.2",
51
- "@fabrika/auth-core": "0.9.0-cloudflare.2",
52
- "@fabrika/db": "0.9.0-cloudflare.2",
53
- "@fabrika/operations-contract": "0.9.0-cloudflare.2",
54
- "@fabrika/platform-node": "0.9.0-cloudflare.2",
50
+ "@fabrika/app": "0.9.0-cloudflare.3",
51
+ "@fabrika/auth-core": "0.9.0-cloudflare.3",
52
+ "@fabrika/db": "0.9.0-cloudflare.3",
53
+ "@fabrika/operations-contract": "0.9.0-cloudflare.3",
54
+ "@fabrika/platform-node": "0.9.0-cloudflare.3",
55
55
  "zod": "^3.25.76"
56
56
  }
57
57
  }
package/src/check.ts CHANGED
@@ -38,6 +38,7 @@ import {
38
38
  checkCurrentTimestampDefaults,
39
39
  checkFileColumnMigrations,
40
40
  checkMigrationNames,
41
+ checkSqliteFrameworkNames,
41
42
  type ConflictInsert,
42
43
  type FileColumn,
43
44
  } from './rules/migrations'
@@ -291,6 +292,7 @@ export const checkApplication = async (input: CheckInput): Promise<Finding[]> =>
291
292
  if (applicationDialect(tree.files) === 'sqlite') {
292
293
  if (artifact !== null) findings.push(...checkCloudflareContract(artifact))
293
294
  findings.push(...checkCurrentTimestampDefaults(tree.files))
295
+ findings.push(...checkSqliteFrameworkNames(tree.files))
294
296
  }
295
297
  if (declaredTables !== null) {
296
298
  const policies = tablePolicies(declaredTables)
package/src/finding.ts CHANGED
@@ -40,9 +40,10 @@ export const REFUSAL_RULE_IDS = [
40
40
  'layer-import',
41
41
  'generated-test-import',
42
42
  'shared-import',
43
- 'cloudflare-sql',
44
43
  'cloudflare-scan',
44
+ 'cloudflare-job-segment',
45
45
  'cloudflare-current-timestamp',
46
+ 'cloudflare-framework-name',
46
47
  ] as const
47
48
 
48
49
  /** Rules that report a step out of the application architecture. */
@@ -83,7 +83,7 @@ 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 = ['app', 'dimensions', 'actions', 'roles', 'locale', 'displayName', 'mail'] as const
86
+ const VOCABULARY_SECTIONS = ['app', 'dimensions', 'actions', 'roles', 'locale', 'displayName', 'mail', 'members', 'directory', 'terminal'] as const
87
87
 
88
88
  const ALLOWED_VALUE = 'object and array literals, strings, numbers, booleans, null, the c.* and z.* builders, and const imports of such values'
89
89
 
@@ -1,8 +1,9 @@
1
+ import { CLOUDFLARE_MAX_JOB_SEGMENT_MS } from '@fabrika/app/contract'
1
2
  import { type ContractArtifact, type DeclaredEgress, PUBLIC_EGRESS } from '@fabrika/auth-core'
2
3
  import type { Finding } from '../finding'
3
4
  import { CONTRACT_FILE } from '../layout'
4
5
 
5
- const at = (rule: 'cloudflare-sql' | 'cloudflare-scan', message: string, allowed: string): Finding => ({
6
+ const at = (rule: 'cloudflare-scan' | 'cloudflare-job-segment', message: string, allowed: string): Finding => ({
6
7
  file: CONTRACT_FILE,
7
8
  line: 1,
8
9
  column: 1,
@@ -19,19 +20,12 @@ const outsideCalls = (reach: { readonly egress?: DeclaredEgress; readonly connec
19
20
  ]
20
21
 
21
22
  /**
22
- * What an application on Cloudflare cannot declare (ADR-0065): raw SQL, because a Durable Object's SQLite has no role
23
- * under it and the classifier reads Postgres's lexical rules; a file kind that scans, because no scanner runs there.
24
- * And a hint for a mutation that declares an outside call: the object runs one transaction at a time, and the mutation
25
- * holds it while the call waits (backlog 216 item 7).
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).
26
27
  */
27
28
  export const checkCloudflareContract = (artifact: ContractArtifact): Finding[] => [
28
- ...Object.entries(artifact.declared ?? {}).filter(([, reach]) => reach.sql !== null).map(([site]) =>
29
- at(
30
- 'cloudflare-sql',
31
- `${site} declares sql, which an application on Cloudflare cannot run: its SQLite database has no role to bound raw SQL`,
32
- 'ctx.db and its stores, without sql in the site options',
33
- )
34
- ),
35
29
  ...Object.entries(artifact.fileKinds ?? {}).filter(([, kind]) => kind.scan === true).map(([name]) =>
36
30
  at(
37
31
  'cloudflare-scan',
@@ -39,6 +33,13 @@ export const checkCloudflareContract = (artifact: ContractArtifact): Finding[] =
39
33
  `c.files({ types, maxBytes }) without scan for ${name}, and no public procedure that takes its files`,
40
34
  )
41
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
+ ),
42
43
  ...artifact.endpoints.filter((endpoint) => endpoint.kind === 'mutation').flatMap((endpoint): Finding[] => {
43
44
  const reach = artifact.declared?.[endpoint.id]
44
45
  const calls = reach === undefined ? [] : outsideCalls(reach)
@@ -2,11 +2,13 @@ import {
2
2
  afterName,
3
3
  closingParenthesis,
4
4
  isPunct,
5
+ lexSqlite,
5
6
  nameText,
6
7
  objectNameAt,
7
8
  skipWords,
8
9
  splitAtCommas,
9
10
  type SqlToken,
11
+ statementsOf,
10
12
  topLevelStatements,
11
13
  upperWord,
12
14
  } from '@fabrika/platform-node/sql'
@@ -74,6 +76,36 @@ export const checkCurrentTimestampDefaults = (files: ReadonlyMap<string, string>
74
76
  return findings
75
77
  }
76
78
 
79
+ /**
80
+ * On SQLite an application migration runs verbatim, with no name rule, so a view or a trigger over a framework table
81
+ * would reach what `ctx.sql`'s name rule keeps it from. Every `fabrika_*` name is refused (any case, quoted, bracketed,
82
+ * or as a string SQLite may read as a name) but `fabrika_files` right after `REFERENCES`, a file column's foreign key.
83
+ */
84
+ export const checkSqliteFrameworkNames = (files: ReadonlyMap<string, string>): Finding[] => {
85
+ const findings: Finding[] = []
86
+ for (const [path, sql] of files) {
87
+ if (!path.startsWith(MIGRATIONS_DIR) || !path.endsWith('.sql')) continue
88
+ for (const { tokens } of statementsOf(lexSqlite(sql).tokens)) {
89
+ tokens.forEach((token, index) => {
90
+ const text = token.kind === 'word' || token.kind === 'quoted' || token.kind === 'string' ? token.text : undefined
91
+ const name = text?.toLowerCase()
92
+ if (name === undefined || !name.startsWith('fabrika_')) return
93
+ if (name === FILES_TABLE && token.kind !== 'string' && upperWord(tokens[index - 1]) === 'REFERENCES') return
94
+ findings.push({
95
+ file: path,
96
+ line: token.line,
97
+ column: 1,
98
+ severity: 'refusal',
99
+ rule: 'cloudflare-framework-name',
100
+ message: `'${text}' is the framework's: a migration on Cloudflare names no fabrika_* table, which ctx.sql could then reach through it`,
101
+ allowed: `the application's own tables, and REFERENCES ${FILES_TABLE} (id) for a file column`,
102
+ })
103
+ })
104
+ }
105
+ }
106
+ return findings
107
+ }
108
+
77
109
  /** A declared file column: `table.column` holds files of `kind`. */
78
110
  export interface FileColumn {
79
111
  readonly table: string
@@ -33,7 +33,7 @@ const isAssertionCall = (node: AstNode): boolean => {
33
33
  const path = memberPath(callee)
34
34
  const [root] = path ?? []
35
35
  const last = path?.[path.length - 1]
36
- return root !== undefined && root !== 'expect' && (/^assert/.test(root) || (last !== undefined && ASSERTION_NAME.test(last)))
36
+ return root !== undefined && root !== 'expect' && (root.startsWith('assert') || (last !== undefined && ASSERTION_NAME.test(last)))
37
37
  }
38
38
 
39
39
  /** Whether a test body runs an assertion; a nested function counts only where it is called or passed to a call. */
@@ -1,6 +1,6 @@
1
1
  import type { AstNode, Position } from './ast';
2
2
  /** Rules whose finding fails the check: the framework's forms and platform integrity. */
3
- export declare const REFUSAL_RULE_IDS: readonly ["parse-error", "literal", "contract", "forbidden-import", "test-only-import", "unchecked-import", "forbidden-global", "raw-sql", "type-assertion", "any-type", "suppression-comment", "capability-widening", "prototype-assignment", "framework-write", "env-read", "public-env", "cross-app-fetch", "url-endpoint", "on-conflict-index", "key-literal", "client-identity", "sensitive-log", "unsafe-html", "dynamic-code", "migration-name", "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", "cloudflare-sql", "cloudflare-scan", "cloudflare-current-timestamp"];
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", "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", "cloudflare-scan", "cloudflare-job-segment", "cloudflare-current-timestamp", "cloudflare-framework-name"];
4
4
  /** Rules that report a step out of the application architecture. */
5
5
  export declare const STRUCTURE_HINT_RULE_IDS: readonly ["logic-data-access", "cross-slice-queries", "handler-location", "data-access-location"];
6
6
  /**
@@ -9,7 +9,7 @@ export declare const STRUCTURE_HINT_RULE_IDS: readonly ["logic-data-access", "cr
9
9
  * Cloudflare mutation that waits for an outside call inside the object's transaction.
10
10
  */
11
11
  export declare const HINT_RULE_IDS: readonly ["logic-data-access", "cross-slice-queries", "handler-location", "data-access-location", "sql-undeclared", "member-admits-external", "cloudflare-held-call"];
12
- export declare const RULE_IDS: readonly ["parse-error", "literal", "contract", "forbidden-import", "test-only-import", "unchecked-import", "forbidden-global", "raw-sql", "type-assertion", "any-type", "suppression-comment", "capability-widening", "prototype-assignment", "framework-write", "env-read", "public-env", "cross-app-fetch", "url-endpoint", "on-conflict-index", "key-literal", "client-identity", "sensitive-log", "unsafe-html", "dynamic-code", "migration-name", "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", "cloudflare-sql", "cloudflare-scan", "cloudflare-current-timestamp", "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", "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", "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
13
  export type RefusalRuleId = (typeof REFUSAL_RULE_IDS)[number];
14
14
  export type HintRuleId = (typeof HINT_RULE_IDS)[number];
15
15
  export type RuleId = (typeof RULE_IDS)[number];
@@ -1,9 +1,9 @@
1
1
  import { type ContractArtifact } from '@fabrika/auth-core';
2
2
  import type { Finding } from '../finding';
3
3
  /**
4
- * What an application on Cloudflare cannot declare (ADR-0065): raw SQL, because a Durable Object's SQLite has no role
5
- * under it and the classifier reads Postgres's lexical rules; a file kind that scans, because no scanner runs there.
6
- * And a hint for a mutation that declares an outside call: the object runs one transaction at a time, and the mutation
7
- * holds it while the call waits (backlog 216 item 7).
4
+ * What an application on Cloudflare cannot declare (ADR-0065): a file kind that scans, because no scanner runs there; a
5
+ * job longer than one segment, because the object runs it inside an alarm or a request. And a hint for a mutation that
6
+ * declares an outside call: the object runs one transaction at a time, and the mutation holds it while the call waits
7
+ * (backlog 216 item 7).
8
8
  */
9
9
  export declare const checkCloudflareContract: (artifact: ContractArtifact) => Finding[];
@@ -6,6 +6,12 @@ export declare const checkMigrationNames: (paths: readonly string[]) => Finding[
6
6
  * `unexpected_row` (backlog 216 item 13). Refused with or without parentheses, at the line of `CURRENT_TIMESTAMP`.
7
7
  */
8
8
  export declare const checkCurrentTimestampDefaults: (files: ReadonlyMap<string, string>) => Finding[];
9
+ /**
10
+ * On SQLite an application migration runs verbatim, with no name rule, so a view or a trigger over a framework table
11
+ * would reach what `ctx.sql`'s name rule keeps it from. Every `fabrika_*` name is refused (any case, quoted, bracketed,
12
+ * or as a string SQLite may read as a name) but `fabrika_files` right after `REFERENCES`, a file column's foreign key.
13
+ */
14
+ export declare const checkSqliteFrameworkNames: (files: ReadonlyMap<string, string>) => Finding[];
9
15
  /** A declared file column: `table.column` holds files of `kind`. */
10
16
  export interface FileColumn {
11
17
  readonly table: string;