@fabrika/app 0.9.0-dev.5 → 0.9.0-dev.7
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 +9 -9
- package/skill/SKILL.md +1 -1
- package/skill/references/app-setup.md +8 -8
- package/src/cloudflare/export.ts +5 -25
- package/src/cloudflare/import.ts +104 -28
- package/types/src/cloudflare/import.d.ts +10 -3
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fabrika/app",
|
|
3
|
-
"version": "0.9.0-dev.
|
|
3
|
+
"version": "0.9.0-dev.7",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -109,14 +109,14 @@
|
|
|
109
109
|
"test": "bun test --path-ignore-patterns='src/react/**' && bun test --isolate src/react"
|
|
110
110
|
},
|
|
111
111
|
"dependencies": {
|
|
112
|
-
"@fabrika/auth": "0.9.0-dev.
|
|
113
|
-
"@fabrika/auth-core": "0.9.0-dev.
|
|
114
|
-
"@fabrika/db": "0.9.0-dev.
|
|
115
|
-
"@fabrika/email": "0.9.0-dev.
|
|
116
|
-
"@fabrika/jwks": "0.9.0-dev.
|
|
117
|
-
"@fabrika/platform": "0.9.0-dev.
|
|
118
|
-
"@fabrika/platform-node": "0.9.0-dev.
|
|
119
|
-
"@fabrika/proxy-core": "0.9.0-dev.
|
|
112
|
+
"@fabrika/auth": "0.9.0-dev.7",
|
|
113
|
+
"@fabrika/auth-core": "0.9.0-dev.7",
|
|
114
|
+
"@fabrika/db": "0.9.0-dev.7",
|
|
115
|
+
"@fabrika/email": "0.9.0-dev.7",
|
|
116
|
+
"@fabrika/jwks": "0.9.0-dev.7",
|
|
117
|
+
"@fabrika/platform": "0.9.0-dev.7",
|
|
118
|
+
"@fabrika/platform-node": "0.9.0-dev.7",
|
|
119
|
+
"@fabrika/proxy-core": "0.9.0-dev.7",
|
|
120
120
|
"altcha-lib": "2.6.0",
|
|
121
121
|
"jose": "^5.9.0"
|
|
122
122
|
},
|
package/skill/SKILL.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: fabrika-app
|
|
3
|
-
version: 0.9.0-dev.
|
|
3
|
+
version: 0.9.0-dev.7
|
|
4
4
|
description: >
|
|
5
5
|
How to write and change a Fabrika application: src/vocabulary.ts, db/migrations/, src/db/tables.ts, the
|
|
6
6
|
slices' endpoints.ts with their handlers, fabrika.contract.ts, jobs, files, webhooks, share links, connectors
|
|
@@ -19,20 +19,20 @@ the SPA is added with it.
|
|
|
19
19
|
"test": "bun test --isolate"
|
|
20
20
|
},
|
|
21
21
|
"dependencies": {
|
|
22
|
-
"@fabrika/app": "0.9.0-dev.
|
|
23
|
-
"@fabrika/db": "0.9.0-dev.
|
|
24
|
-
"@fabrika/provider-zerops": "0.9.0-dev.
|
|
25
|
-
"@fabrika/proxy-core": "0.9.0-dev.
|
|
26
|
-
"@fabrika/ui": "0.9.0-dev.
|
|
22
|
+
"@fabrika/app": "0.9.0-dev.7",
|
|
23
|
+
"@fabrika/db": "0.9.0-dev.7",
|
|
24
|
+
"@fabrika/provider-zerops": "0.9.0-dev.7",
|
|
25
|
+
"@fabrika/proxy-core": "0.9.0-dev.7",
|
|
26
|
+
"@fabrika/ui": "0.9.0-dev.7",
|
|
27
27
|
"kysely": "0.29.6",
|
|
28
28
|
"react": "^19.0.0",
|
|
29
29
|
"react-dom": "^19.0.0",
|
|
30
30
|
"zod": "^3.25.76"
|
|
31
31
|
},
|
|
32
32
|
"devDependencies": {
|
|
33
|
-
"@fabrika/check": "0.9.0-dev.
|
|
34
|
-
"@fabrika/cli": "0.9.0-dev.
|
|
35
|
-
"@fabrika/platform": "0.9.0-dev.
|
|
33
|
+
"@fabrika/check": "0.9.0-dev.7",
|
|
34
|
+
"@fabrika/cli": "0.9.0-dev.7",
|
|
35
|
+
"@fabrika/platform": "0.9.0-dev.7",
|
|
36
36
|
"@happy-dom/global-registrator": "^20.11.2",
|
|
37
37
|
"@types/bun": "^1.4.3",
|
|
38
38
|
"@types/react": "^19.0.0",
|
package/src/cloudflare/export.ts
CHANGED
|
@@ -15,12 +15,11 @@ import {
|
|
|
15
15
|
OPS_EMPTY_TABLE_HASH,
|
|
16
16
|
OPS_STALE_STATUS,
|
|
17
17
|
type OpsDataRefusal,
|
|
18
|
-
opsDumpColumnProjection,
|
|
19
18
|
type OpsDumpCursor,
|
|
19
|
+
type OpsDumpPageRow,
|
|
20
20
|
type OpsDumpSchemaEntry,
|
|
21
21
|
type OpsDumpTable,
|
|
22
22
|
type OpsDumpTablePlan,
|
|
23
|
-
type OpsDumpValue,
|
|
24
23
|
opsDumpValueBinding,
|
|
25
24
|
type OpsExportBegin,
|
|
26
25
|
type OpsExportComplete,
|
|
@@ -33,7 +32,7 @@ import {
|
|
|
33
32
|
parseOpsExportCompleteAck,
|
|
34
33
|
parseOpsExportPartAck,
|
|
35
34
|
planOpsDump,
|
|
36
|
-
|
|
35
|
+
readOpsDumpPage,
|
|
37
36
|
sha256Hex,
|
|
38
37
|
} from '@fabrika/auth-core'
|
|
39
38
|
import type { SqliteValue } from '@fabrika/db/sqlite'
|
|
@@ -428,29 +427,10 @@ function rowBounds(storage: Storage, table: OpsDumpTablePlan, after: string | nu
|
|
|
428
427
|
})
|
|
429
428
|
}
|
|
430
429
|
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
readonly values: readonly OpsDumpValue[]
|
|
434
|
-
}
|
|
435
|
-
|
|
436
|
-
/**
|
|
437
|
-
* The next `limit` rows: each its rowid as decimal text, then the rowid and every column through the wire's projection.
|
|
438
|
-
* The column-name list of the common table expression names every result column, which an expression alone does not.
|
|
439
|
-
*/
|
|
440
|
-
function readRows(storage: Storage, table: OpsDumpTablePlan, after: string | null, limit: number): ReadValues[] {
|
|
441
|
-
const keyset = quoted(table.keyset)
|
|
442
|
-
const names = ['k', 'r', ...table.columns.flatMap((_, index) => [`t${index}`, `v${index}`])]
|
|
443
|
-
const projections = [keyset, `CAST(${keyset} AS TEXT)`, ...table.columns.map(opsDumpColumnProjection)]
|
|
430
|
+
/** The next `limit` rows: each its rowid as decimal text, then the rowid and every column through the wire's projection. */
|
|
431
|
+
function readRows(storage: Storage, table: OpsDumpTablePlan, after: string | null, limit: number): OpsDumpPageRow[] {
|
|
444
432
|
const condition = keysetCondition(table, after)
|
|
445
|
-
|
|
446
|
-
ORDER BY ${keyset} LIMIT ${limit}) SELECT ${names.slice(1).join(', ')} FROM page ORDER BY k`
|
|
447
|
-
return storage.sql.exec(query, ...condition.bindings).toArray().map((row) => {
|
|
448
|
-
const rowid = readOpsDumpValue('integer', row['r'])
|
|
449
|
-
if (rowid === null || rowid.type !== 'integer') {
|
|
450
|
-
throw new Error('a rowid reads as an integer')
|
|
451
|
-
}
|
|
452
|
-
return { rowid: rowid.value, values: [rowid, ...table.columns.map((_, index) => readOpsDumpValue(row[`t${index}`], row[`v${index}`]))] }
|
|
453
|
-
})
|
|
433
|
+
return readOpsDumpPage((query) => storage.sql.exec(query, ...condition.bindings).toArray(), table, condition.sql, limit)
|
|
454
434
|
}
|
|
455
435
|
|
|
456
436
|
/** The tables a part finished, with their counts and hashes, and the cursor where the next part starts. */
|
package/src/cloudflare/import.ts
CHANGED
|
@@ -19,10 +19,10 @@ import {
|
|
|
19
19
|
type OpsDataRefusal,
|
|
20
20
|
type OpsDataRefusalReason,
|
|
21
21
|
type OpsDumpBinding,
|
|
22
|
-
opsDumpColumnProjection,
|
|
23
22
|
type OpsDumpCursor,
|
|
24
23
|
type OpsDumpLine,
|
|
25
24
|
type OpsDumpManifest,
|
|
25
|
+
opsDumpRowidSql,
|
|
26
26
|
type OpsDumpSchema,
|
|
27
27
|
type OpsDumpSchemaColumn,
|
|
28
28
|
type OpsDumpSchemaEntry,
|
|
@@ -48,7 +48,7 @@ import {
|
|
|
48
48
|
parseOpsImportVerifyAck,
|
|
49
49
|
planOpsDump,
|
|
50
50
|
readOpsDumpManifest,
|
|
51
|
-
|
|
51
|
+
readOpsDumpPage,
|
|
52
52
|
sha256Hex,
|
|
53
53
|
} from '@fabrika/auth-core'
|
|
54
54
|
import type { SqliteStorage, SqliteValue } from '@fabrika/db/sqlite'
|
|
@@ -330,9 +330,11 @@ async function loadPart(target: ImportTarget, source: OpsDumpSource, manifest: O
|
|
|
330
330
|
/** Writes one row; answers whether it has a deferred key for the link pass to set. */
|
|
331
331
|
function insertRow(storage: SqliteStorage, table: OpsDumpTablePlan, row: readonly OpsDumpValue[]): boolean {
|
|
332
332
|
const [rowid, ...values] = row
|
|
333
|
-
|
|
333
|
+
// Inline, so a row of a table at workerd's column limit binds no more parameters than its columns.
|
|
334
|
+
const key = opsDumpRowidSql(rowid ?? null)
|
|
334
335
|
const columns: string[] = [table.keyset]
|
|
335
|
-
const
|
|
336
|
+
const keys: string[] = [key]
|
|
337
|
+
const bindings: OpsDumpBinding[] = []
|
|
336
338
|
let waits = false
|
|
337
339
|
for (const [index, column] of table.columns.entries()) {
|
|
338
340
|
const value = values[index] ?? null
|
|
@@ -342,16 +344,18 @@ function insertRow(storage: SqliteStorage, table: OpsDumpTablePlan, row: readonl
|
|
|
342
344
|
}
|
|
343
345
|
const deferred = table.deferred.includes(column)
|
|
344
346
|
waits ||= deferred && value !== null
|
|
347
|
+
const binding = opsDumpValueBinding(deferred ? null : value)
|
|
345
348
|
columns.push(column)
|
|
346
|
-
|
|
349
|
+
keys.push(binding.sql)
|
|
350
|
+
bindings.push(binding)
|
|
347
351
|
}
|
|
348
352
|
if (table.name === SQLITE_SEQUENCE) {
|
|
349
353
|
// Loading an AUTOINCREMENT table writes its own counter row; the source's row replaces it, whichever table loads first.
|
|
350
354
|
const name = opsDumpValueBinding(values[0] ?? null)
|
|
351
|
-
storage.sql.exec(`DELETE FROM ${SQLITE_SEQUENCE} WHERE rowid = ${key
|
|
355
|
+
storage.sql.exec(`DELETE FROM ${SQLITE_SEQUENCE} WHERE rowid = ${key} OR name = ${name.sql}`, ...params([name]))
|
|
352
356
|
}
|
|
353
357
|
storage.sql.exec(
|
|
354
|
-
`INSERT INTO ${quote(table.name)} (${columns.map(quote).join(', ')}) VALUES (${
|
|
358
|
+
`INSERT INTO ${quote(table.name)} (${columns.map(quote).join(', ')}) VALUES (${keys.join(', ')})`,
|
|
355
359
|
...params(bindings),
|
|
356
360
|
)
|
|
357
361
|
return waits
|
|
@@ -396,12 +400,11 @@ async function linkPart(target: ImportTarget, source: OpsDumpSource, manifest: O
|
|
|
396
400
|
continue
|
|
397
401
|
}
|
|
398
402
|
const sets = deferred.map(({ column, value }) => ({ column, binding: opsDumpValueBinding(value) }))
|
|
399
|
-
const key = opsDumpValueBinding(rowid ?? null)
|
|
400
403
|
storage.sql.exec(
|
|
401
404
|
`UPDATE ${quote(table.name)} SET ${sets.map(({ column, binding }) => `${quote(column)} = ${binding.sql}`).join(', ')} WHERE ${
|
|
402
405
|
quote(table.keyset)
|
|
403
|
-
} = ${
|
|
404
|
-
...params(
|
|
406
|
+
} = ${opsDumpRowidSql(rowid ?? null)}`,
|
|
407
|
+
...params(sets.map(({ binding }) => binding)),
|
|
405
408
|
)
|
|
406
409
|
linked++
|
|
407
410
|
}
|
|
@@ -495,20 +498,9 @@ function cursorKey(cursor: OpsDumpCursor): string {
|
|
|
495
498
|
|
|
496
499
|
/** One keyset page of `table` after the rowid `after`: each row its rowid, then its columns, as the producer reads them. */
|
|
497
500
|
function readPage(storage: SqliteStorage, table: OpsDumpTablePlan, after: string | null): (readonly OpsDumpValue[])[] {
|
|
498
|
-
const
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
const select = [keyset, `CAST(${keyset} AS TEXT)`, ...table.columns.map((column) => opsDumpColumnProjection(column))].join(', ')
|
|
502
|
-
const where = after === null ? '' : ` WHERE ${keyset} > CAST(? AS INTEGER)`
|
|
503
|
-
// Qualified, so a table named `page` is not the CTE itself.
|
|
504
|
-
const query =
|
|
505
|
-
`WITH page(r, k, ${names.join(', ')}) AS (SELECT ${select} FROM main.${quote(table.name)}${where} ORDER BY ${keyset} LIMIT ${OPS_DUMP_PAGE_ROWS}) `
|
|
506
|
-
+ `SELECT k, ${names.join(', ')} FROM page ORDER BY r`
|
|
507
|
-
const rows = after === null ? storage.sql.exec(query).toArray() : storage.sql.exec(query, after).toArray()
|
|
508
|
-
return rows.map((row) => [
|
|
509
|
-
readOpsDumpValue('integer', row['k']),
|
|
510
|
-
...table.columns.map((_, index) => readOpsDumpValue(row[`t${index}`], row[`v${index}`])),
|
|
511
|
-
])
|
|
501
|
+
const condition = after === null ? '' : `WHERE ${quote(table.keyset)} > CAST(? AS INTEGER)`
|
|
502
|
+
const query = (sql: string) => after === null ? storage.sql.exec(sql).toArray() : storage.sql.exec(sql, after).toArray()
|
|
503
|
+
return readOpsDumpPage(query, table, condition, OPS_DUMP_PAGE_ROWS).map((row) => row.values)
|
|
512
504
|
}
|
|
513
505
|
|
|
514
506
|
/**
|
|
@@ -770,9 +762,9 @@ function sameDdl(left: readonly OpsDumpSchemaEntry[], right: readonly OpsDumpSch
|
|
|
770
762
|
}
|
|
771
763
|
|
|
772
764
|
/**
|
|
773
|
-
* The schema `planOpsDump` reads, from the PRAGMAs workerd admits, of every table a dump holds.
|
|
774
|
-
*
|
|
775
|
-
*
|
|
765
|
+
* The schema `planOpsDump` reads, from the PRAGMAs workerd admits, of every table a dump holds. An `INITIALLY DEFERRED`
|
|
766
|
+
* clause is read from the table's SQL outside its quotes and comments, and marks every foreign key of its table; the
|
|
767
|
+
* names a `CHECK` reads come from the same SQL (`checkNamesOf`).
|
|
776
768
|
*/
|
|
777
769
|
export function describeDumpSchema(storage: SqliteStorage): OpsDumpSchema {
|
|
778
770
|
const tables: OpsDumpSchemaTable[] = []
|
|
@@ -789,7 +781,7 @@ export function describeDumpSchema(storage: SqliteStorage): OpsDumpSchema {
|
|
|
789
781
|
name,
|
|
790
782
|
virtual,
|
|
791
783
|
withoutRowid: flag(row['wr']),
|
|
792
|
-
|
|
784
|
+
checkNames: typeof sql === 'string' ? checkNamesOf(sql) : null,
|
|
793
785
|
// A virtual table is refused whatever it holds, and its module may not be loaded to describe it.
|
|
794
786
|
columns: virtual ? [] : describeColumns(storage, name),
|
|
795
787
|
indexes: virtual ? [] : describeIndexes(storage, name),
|
|
@@ -849,6 +841,90 @@ function describeForeignKeys(storage: SqliteStorage, table: string, initiallyDef
|
|
|
849
841
|
})
|
|
850
842
|
}
|
|
851
843
|
|
|
844
|
+
type SqlToken =
|
|
845
|
+
| { readonly kind: 'name'; readonly text: string; readonly quoted: boolean }
|
|
846
|
+
| { readonly kind: 'open' | 'close' | 'other' }
|
|
847
|
+
|
|
848
|
+
const WORD_CHAR = /[A-Za-z0-9_$\u0080-\uffff]/
|
|
849
|
+
|
|
850
|
+
/**
|
|
851
|
+
* The names the `CHECK` clauses of a table's `sql` read: every word and quoted identifier between a `CHECK` keyword's
|
|
852
|
+
* parentheses, ASCII-lowercase, keywords and numbers included, so it lists at least every column a `CHECK` reads. A
|
|
853
|
+
* string literal and a comment name nothing; a double-quoted string, which SQLite may read as either, counts as a name.
|
|
854
|
+
* `null` when the text does not scan (an unclosed quote, comment or parenthesis), which counts as naming every column.
|
|
855
|
+
*/
|
|
856
|
+
export function checkNamesOf(sql: string): string[] | null {
|
|
857
|
+
const tokens = sqlTokens(sql)
|
|
858
|
+
if (tokens === null) return null
|
|
859
|
+
const names: string[] = []
|
|
860
|
+
for (let index = 0; index < tokens.length; index++) {
|
|
861
|
+
const token = tokens[index]
|
|
862
|
+
if (token?.kind !== 'name' || token.quoted || asciiLower(token.text) !== 'check' || tokens[index + 1]?.kind !== 'open') continue
|
|
863
|
+
let depth = 0
|
|
864
|
+
let end = index + 1
|
|
865
|
+
for (; end < tokens.length; end++) {
|
|
866
|
+
const inner = tokens[end]
|
|
867
|
+
if (inner?.kind === 'open') depth++
|
|
868
|
+
else if (inner?.kind === 'close' && --depth === 0) break
|
|
869
|
+
else if (inner?.kind === 'name') names.push(asciiLower(inner.text))
|
|
870
|
+
}
|
|
871
|
+
if (end === tokens.length) return null
|
|
872
|
+
index = end
|
|
873
|
+
}
|
|
874
|
+
return names
|
|
875
|
+
}
|
|
876
|
+
|
|
877
|
+
/** `sql` as words, quoted identifiers and parentheses; string literals, comments and whitespace dropped. */
|
|
878
|
+
function sqlTokens(sql: string): SqlToken[] | null {
|
|
879
|
+
const tokens: SqlToken[] = []
|
|
880
|
+
let index = 0
|
|
881
|
+
while (index < sql.length) {
|
|
882
|
+
const char = sql.charAt(index)
|
|
883
|
+
if (sql.startsWith('--', index)) {
|
|
884
|
+
const end = sql.indexOf('\n', index)
|
|
885
|
+
index = end < 0 ? sql.length : end + 1
|
|
886
|
+
} else if (sql.startsWith('/*', index)) {
|
|
887
|
+
const end = sql.indexOf('*/', index + 2)
|
|
888
|
+
if (end < 0) return null
|
|
889
|
+
index = end + 2
|
|
890
|
+
} else if (char === "'" || char === '"' || char === '`' || char === '[') {
|
|
891
|
+
const close = char === '[' ? ']' : char
|
|
892
|
+
let end = index + 1
|
|
893
|
+
let text = ''
|
|
894
|
+
for (;;) {
|
|
895
|
+
const next = sql.indexOf(close, end)
|
|
896
|
+
if (next < 0) return null
|
|
897
|
+
text += sql.slice(end, next)
|
|
898
|
+
if (close !== ']' && sql.charAt(next + 1) === close) {
|
|
899
|
+
text += close
|
|
900
|
+
end = next + 2
|
|
901
|
+
continue
|
|
902
|
+
}
|
|
903
|
+
end = next + 1
|
|
904
|
+
break
|
|
905
|
+
}
|
|
906
|
+
if (char !== "'") tokens.push({ kind: 'name', text, quoted: true })
|
|
907
|
+
index = end
|
|
908
|
+
} else if (WORD_CHAR.test(char)) {
|
|
909
|
+
let end = index + 1
|
|
910
|
+
while (end < sql.length && WORD_CHAR.test(sql.charAt(end))) end++
|
|
911
|
+
tokens.push({ kind: 'name', text: sql.slice(index, end), quoted: false })
|
|
912
|
+
index = end
|
|
913
|
+
} else {
|
|
914
|
+
if (char === '(') tokens.push({ kind: 'open' })
|
|
915
|
+
else if (char === ')') tokens.push({ kind: 'close' })
|
|
916
|
+
else if (!/\s/.test(char)) tokens.push({ kind: 'other' })
|
|
917
|
+
index++
|
|
918
|
+
}
|
|
919
|
+
}
|
|
920
|
+
return tokens
|
|
921
|
+
}
|
|
922
|
+
|
|
923
|
+
/** SQLite compares identifiers case-insensitively in ASCII only. */
|
|
924
|
+
function asciiLower(value: string): string {
|
|
925
|
+
return value.replace(/[A-Z]/g, (letter) => letter.toLowerCase())
|
|
926
|
+
}
|
|
927
|
+
|
|
852
928
|
/** `sql` with its string literals, quoted identifiers and comments blanked, so a keyword search finds only clauses. */
|
|
853
929
|
function unquotedSql(sql: string): string {
|
|
854
930
|
let out = ''
|
|
@@ -44,8 +44,15 @@ export declare function importInProgress(storage: {
|
|
|
44
44
|
*/
|
|
45
45
|
export declare function importStep(target: ImportTarget, barrier: MaintenanceBarrier, step: OpsImportRequest): Promise<Response>;
|
|
46
46
|
/**
|
|
47
|
-
* The schema `planOpsDump` reads, from the PRAGMAs workerd admits, of every table a dump holds.
|
|
48
|
-
*
|
|
49
|
-
*
|
|
47
|
+
* The schema `planOpsDump` reads, from the PRAGMAs workerd admits, of every table a dump holds. An `INITIALLY DEFERRED`
|
|
48
|
+
* clause is read from the table's SQL outside its quotes and comments, and marks every foreign key of its table; the
|
|
49
|
+
* names a `CHECK` reads come from the same SQL (`checkNamesOf`).
|
|
50
50
|
*/
|
|
51
51
|
export declare function describeDumpSchema(storage: SqliteStorage): OpsDumpSchema;
|
|
52
|
+
/**
|
|
53
|
+
* The names the `CHECK` clauses of a table's `sql` read: every word and quoted identifier between a `CHECK` keyword's
|
|
54
|
+
* parentheses, ASCII-lowercase, keywords and numbers included, so it lists at least every column a `CHECK` reads. A
|
|
55
|
+
* string literal and a comment name nothing; a double-quoted string, which SQLite may read as either, counts as a name.
|
|
56
|
+
* `null` when the text does not scan (an unclosed quote, comment or parenthesis), which counts as naming every column.
|
|
57
|
+
*/
|
|
58
|
+
export declare function checkNamesOf(sql: string): string[] | null;
|