@fabrika/app 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 +9 -9
- package/skill/SKILL.md +20 -18
- package/skill/references/app-setup.md +29 -16
- package/skill/references/endpoint.md +24 -22
- package/skill/references/errors.md +3 -2
- package/skill/references/jobs.md +13 -3
- package/skill/references/tests.md +7 -0
- package/src/bun.ts +159 -6
- package/src/cloudflare/application.ts +543 -73
- package/src/cloudflare/export.ts +621 -0
- package/src/cloudflare/import.ts +1097 -0
- package/src/cloudflare/maintenance.ts +518 -0
- package/src/cloudflare/ops.ts +147 -18
- package/src/cloudflare/restore.ts +167 -0
- package/src/cloudflare/tracker.ts +192 -0
- package/src/cloudflare.ts +3 -2
- package/src/contract/artifact-schema.ts +7 -1
- package/src/contract/build.ts +6 -2
- package/src/contract/context.ts +26 -9
- package/src/contract/defaults.ts +22 -2
- package/src/contract/define.ts +1 -1
- package/src/contract/group.ts +1 -1
- package/src/files/kinds.ts +54 -24
- package/src/implement/anonymous.ts +1 -10
- package/src/implement/boot.ts +21 -9
- package/src/implement/dispatch.ts +171 -33
- package/src/implement/egress.ts +34 -8
- package/src/implement/invocation.ts +50 -10
- package/src/implement/workload.ts +18 -3
- package/src/jobs/runner.ts +639 -96
- package/src/testing/kit.ts +60 -0
- package/types/src/bun.d.ts +34 -1
- package/types/src/cloudflare/application.d.ts +36 -5
- package/types/src/cloudflare/export.d.ts +16 -0
- package/types/src/cloudflare/import.d.ts +58 -0
- package/types/src/cloudflare/maintenance.d.ts +117 -0
- package/types/src/cloudflare/ops.d.ts +39 -4
- package/types/src/cloudflare/restore.d.ts +9 -0
- package/types/src/cloudflare/tracker.d.ts +29 -0
- package/types/src/cloudflare.d.ts +2 -1
- package/types/src/contract/artifact-schema.d.ts +2 -0
- package/types/src/contract/context.d.ts +24 -9
- package/types/src/contract/defaults.d.ts +19 -1
- package/types/src/contract/define.d.ts +1 -1
- package/types/src/contract/group.d.ts +1 -1
- package/types/src/files/kinds.d.ts +17 -6
- package/types/src/implement/anonymous.d.ts +0 -4
- package/types/src/implement/boot.d.ts +11 -0
- package/types/src/implement/dispatch.d.ts +31 -0
- package/types/src/implement/invocation.d.ts +17 -7
- package/types/src/implement/workload.d.ts +5 -0
- package/types/src/jobs/runner.d.ts +60 -2
- package/types/src/testing/kit.d.ts +16 -0
package/src/contract/build.ts
CHANGED
|
@@ -67,7 +67,7 @@ import {
|
|
|
67
67
|
import { isPolicyOrigin, MAX_CSP_ORIGINS, WORKER_BLOB_SOURCE } from '@fabrika/proxy-core/headers'
|
|
68
68
|
import { z } from 'zod/v4'
|
|
69
69
|
import { CONTRACT_APP_ID_PATTERN as APP_ID } from './app-id'
|
|
70
|
-
import { contractArtifactSchema } from './artifact-schema'
|
|
70
|
+
import { contractArtifactSchema, RESERVED_FILE_KIND_PREFIX } from './artifact-schema'
|
|
71
71
|
import {
|
|
72
72
|
type AccessDeclaration,
|
|
73
73
|
type Builder,
|
|
@@ -1714,6 +1714,10 @@ class ArtifactBuilder {
|
|
|
1714
1714
|
const kinds: Record<string, FileKindArtifact> = {}
|
|
1715
1715
|
for (const [name, declared] of Object.entries(this.#contract.files)) {
|
|
1716
1716
|
const where = `files '${name}'`
|
|
1717
|
+
if (name.startsWith(RESERVED_FILE_KIND_PREFIX)) {
|
|
1718
|
+
this.#problem(where, `is a file kind name the framework reserves (${RESERVED_FILE_KIND_PREFIX}*)`)
|
|
1719
|
+
continue
|
|
1720
|
+
}
|
|
1717
1721
|
if (!this.#name(where, name) || !this.#kind(where, declared, 'file-kind', 'c.files({ types, maxBytes, scan? })')) {
|
|
1718
1722
|
continue
|
|
1719
1723
|
}
|
|
@@ -2308,7 +2312,7 @@ class ArtifactBuilder {
|
|
|
2308
2312
|
/**
|
|
2309
2313
|
* The HTTPS origins a site's code calls: a credentialed procedure of an RPC mount, a webhook or a job, never a public,
|
|
2310
2314
|
* export or link procedure. Each is an exact origin with no path or wildcard, once. Or `'public'`, every public host
|
|
2311
|
-
* (ADR-
|
|
2315
|
+
* (ADR-0071), which only a cron job declares: it calls outside any transaction, and its schedule, not a caller,
|
|
2312
2316
|
* decides when. `null` when it declares none or after a problem.
|
|
2313
2317
|
*/
|
|
2314
2318
|
#egress(where: string, value: unknown, place: DeclaringSite): DeclaredEgress | null {
|
package/src/contract/context.ts
CHANGED
|
@@ -118,20 +118,31 @@ export type SqlValue = string | number | boolean | bigint | Date | null | readon
|
|
|
118
118
|
export interface SqlTag<Ix extends AnyDbIndex = AnyDbIndex, M extends SqlMode = SqlMode> {
|
|
119
119
|
<Row extends Schema>(row: Row): (text: TemplateStringsArray, ...values: readonly (SqlValue | SqlScope)[]) => Promise<z.output<Row>[]>
|
|
120
120
|
/**
|
|
121
|
-
* The capability's whole predicate on `table
|
|
122
|
-
*
|
|
123
|
-
* `
|
|
124
|
-
*
|
|
125
|
-
*
|
|
126
|
-
*
|
|
121
|
+
* The capability's whole predicate on `table`: the object's key and every link of its chain for a `Ref`, what
|
|
122
|
+
* `ctx.db.within` matches for a `Within`, every row for `ctx.app`. Interpolated as
|
|
123
|
+
* `where ${ctx.sql.scope(ctx.target, 'tasks', { alias: 't' })}`, its values are bound parameters. `alias` is the
|
|
124
|
+
* table's name in the statement (the table's own by default). `use` states what the statement does with the table:
|
|
125
|
+
* `'read'` (the default) needs an action the table reads with, `'update'` or `'delete'` (a `'write'` site only) that
|
|
126
|
+
* operation's action, checked when the fragment is built. The statement is not read for where the fragment stands:
|
|
127
|
+
* using it as stated is the author's and the approver's to check. A table the capability reaches for no use of this
|
|
128
|
+
* site is a type error here.
|
|
127
129
|
*/
|
|
128
130
|
scope<C extends Ref<string, string> | Within<string, string> | AppCapability<string>, T extends FragmentTable<Ix, C, M>>(
|
|
129
131
|
capability: C,
|
|
130
132
|
table: T,
|
|
131
|
-
|
|
133
|
+
options?: SqlScopeOptions,
|
|
132
134
|
): SqlScope
|
|
133
135
|
}
|
|
134
136
|
|
|
137
|
+
/**
|
|
138
|
+
* The third argument of `ctx.sql.scope`: the table's alias in the statement, and what the statement does with it. A
|
|
139
|
+
* `'read'` site's `ctx.sql` is refused `'update'` and `'delete'` at run time; a `'write'` site's stays assignable to it.
|
|
140
|
+
*/
|
|
141
|
+
export interface SqlScopeOptions {
|
|
142
|
+
readonly alias?: string
|
|
143
|
+
readonly use?: 'read' | 'update' | 'delete'
|
|
144
|
+
}
|
|
145
|
+
|
|
135
146
|
declare const sqlScope: unique symbol
|
|
136
147
|
|
|
137
148
|
/** What `ctx.sql.scope(…)` builds: only the framework makes one, and only its own tag takes it. */
|
|
@@ -420,13 +431,19 @@ export type PublicContext<K extends KitTypes, Kind extends DataKind, Acts extend
|
|
|
420
431
|
|
|
421
432
|
/**
|
|
422
433
|
* `ctx.kick('<job>')` of a site that declares `kicks`: the job runs in the background after the site's transaction
|
|
423
|
-
* committed, once however often it is kicked, and not at all on a rollback.
|
|
434
|
+
* committed, once however often it is kicked, and not at all on a rollback. `ctx.kick('<job>', { at })` runs it at `at`
|
|
435
|
+
* instead. Either way the kick is a durable wake written in the site's transaction, and it carries nothing.
|
|
424
436
|
*/
|
|
425
437
|
export type KickContext<Ks> = [Ks] extends [never]
|
|
426
438
|
? { readonly kick: Unavailable<"declare kicks: ['<cron job>'] on this site to kick a job after its commit"> }
|
|
427
|
-
: Ks extends readonly (infer N extends string)[] ? { kick(job: N): void }
|
|
439
|
+
: Ks extends readonly (infer N extends string)[] ? { kick(job: N, options?: KickOptions): void }
|
|
428
440
|
: never
|
|
429
441
|
|
|
442
|
+
/** When a kicked job runs: at `at`, a `Date`; without options, as soon as the site committed. */
|
|
443
|
+
export interface KickOptions {
|
|
444
|
+
readonly at: Date
|
|
445
|
+
}
|
|
446
|
+
|
|
430
447
|
/** `ctx.tokens.sign('<purpose>', claims, { days })` of a site that declares `signs`: a token of one of those purposes. */
|
|
431
448
|
export type TokensContext<K extends KitTypes, Sg> = [Sg] extends [never]
|
|
432
449
|
? { readonly tokens: Unavailable<"declare signs: ['<token purpose>'] on this site to sign its tokens"> }
|
package/src/contract/defaults.ts
CHANGED
|
@@ -29,6 +29,17 @@ export const PROCEDURE_LIMITS = {
|
|
|
29
29
|
maxPartHeaderBytes: 8 * 1024,
|
|
30
30
|
} as const
|
|
31
31
|
|
|
32
|
+
/**
|
|
33
|
+
* The slowest body a procedure or a webhook waits for: its deadline is `maxBytes` at `bytesPerSecond`, `minMs` at least.
|
|
34
|
+
* The handler's own `timeoutMs` starts after the last byte, so the approval diff names both.
|
|
35
|
+
*/
|
|
36
|
+
export const BODY_DEADLINE = { bytesPerSecond: 16 * 1024, minMs: 30_000 } as const
|
|
37
|
+
|
|
38
|
+
/** How long a body of at most `maxBytes` may take to arrive. */
|
|
39
|
+
export function bodyDeadlineMs(maxBytes: number): number {
|
|
40
|
+
return Math.max(BODY_DEADLINE.minMs, Math.ceil((maxBytes / BODY_DEADLINE.bytesPerSecond) * 1000))
|
|
41
|
+
}
|
|
42
|
+
|
|
32
43
|
/**
|
|
33
44
|
* A procedure with file fields is `multipart/form-data`: its JSON input travels in the first part, named `input`, and
|
|
34
45
|
* each file in a part named after its field. A field of several files sends one part per file, under its name.
|
|
@@ -46,9 +57,18 @@ export const WEBHOOK_LIMITS = {
|
|
|
46
57
|
timeoutMs: PROCEDURE_LIMITS.timeoutMs,
|
|
47
58
|
} as const
|
|
48
59
|
|
|
49
|
-
/**
|
|
60
|
+
/**
|
|
61
|
+
* The longest job segment on Cloudflare: setup, one attempt and its finish. A framework policy inside the 15 minutes
|
|
62
|
+
* `waitUntil` keeps an object alive, not a platform guarantee; longer work chunks through claims.
|
|
63
|
+
*/
|
|
64
|
+
export const CLOUDFLARE_MAX_JOB_SEGMENT_MS = 14 * 60_000
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* A job attempt's time limit (`limits.timeoutMs`) and its ceiling, which keeps a lease and a timer within a day. The
|
|
68
|
+
* default is one Cloudflare segment, so a job that declares no limit deploys on either provider.
|
|
69
|
+
*/
|
|
50
70
|
export const JOB_LIMITS = {
|
|
51
|
-
timeoutMs:
|
|
71
|
+
timeoutMs: CLOUDFLARE_MAX_JOB_SEGMENT_MS,
|
|
52
72
|
maxTimeoutMs: 24 * 60 * 60_000,
|
|
53
73
|
} as const
|
|
54
74
|
|
package/src/contract/define.ts
CHANGED
|
@@ -31,7 +31,7 @@ export function isPath(value: string): value is Path {
|
|
|
31
31
|
return value.startsWith('/')
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
-
/** A job attempt's time limit:
|
|
34
|
+
/** A job attempt's time limit: 14 minutes by default, at most a day. A job reads no body, so it has no `maxBytes`. */
|
|
35
35
|
export interface JobLimitsOption {
|
|
36
36
|
readonly timeoutMs?: number
|
|
37
37
|
}
|
package/src/contract/group.ts
CHANGED
|
@@ -120,7 +120,7 @@ export interface EgressField {
|
|
|
120
120
|
|
|
121
121
|
/**
|
|
122
122
|
* A cron job's `egress`: its exact HTTPS origins, or `'public'`, every public host over HTTPS or HTTP on its default
|
|
123
|
-
* port, for a job that calls the websites its operators name (ADR-
|
|
123
|
+
* port, for a job that calls the websites its operators name (ADR-0071). The guard refuses IP addresses, local and
|
|
124
124
|
* internal names and the installation's own hosts, checks each redirect, and bounds each request's time and body.
|
|
125
125
|
*/
|
|
126
126
|
export interface JobEgressField {
|
package/src/files/kinds.ts
CHANGED
|
@@ -2,7 +2,8 @@
|
|
|
2
2
|
// the table's policy is its authority. This module stages a file (an incoming part or a `ctx.files.put`): its pending
|
|
3
3
|
// row first, on a connection of its own, then its object, sniffed and bounded by the kind. It serves and opens a file
|
|
4
4
|
// only through a reference the work holds and may read, and only a live one, by its scanned version. The sweep
|
|
5
|
-
// deletes the files no declared column references, and
|
|
5
|
+
// deletes the files no declared column references, and their objects once past the runtime's retention window, and the
|
|
6
|
+
// scan job decides about the quarantined ones (`scan.ts`).
|
|
6
7
|
|
|
7
8
|
import type { ContractArtifact, FileKindArtifact } from '@fabrika/auth-core'
|
|
8
9
|
import {
|
|
@@ -17,6 +18,7 @@ import {
|
|
|
17
18
|
type StoredFiles,
|
|
18
19
|
} from '@fabrika/db/framework'
|
|
19
20
|
import { type FileObjectStore, type StoredObject, uuidv7 } from '@fabrika/platform'
|
|
21
|
+
import { RESERVED_FILE_KIND_PREFIX } from '../contract/artifact-schema'
|
|
20
22
|
import type { FileRef } from '../contract/file'
|
|
21
23
|
import { HttpError, NotFoundError } from '../errors'
|
|
22
24
|
import { CapabilityRefusedError } from '../implement/capability'
|
|
@@ -33,6 +35,11 @@ export { FILE_SWEEP_JOB, FILE_SWEEP_SCHEDULE } from './job-names'
|
|
|
33
35
|
export const FILE_SWEEP_TIMEOUT_MS = 10 * 60_000
|
|
34
36
|
/** How long a staged file waits for its first reference before the sweep may delete it. */
|
|
35
37
|
export const FILE_PENDING_GRACE_MS = 60 * 60 * 1000
|
|
38
|
+
/**
|
|
39
|
+
* How long a removed file's object outlives its row on Cloudflare, so the rows of a restored backup still find their
|
|
40
|
+
* files: the 30 days backups are kept, which never exceeds this window.
|
|
41
|
+
*/
|
|
42
|
+
export const FILE_TOMBSTONE_RETENTION_MS = 30 * 24 * 60 * 60 * 1000
|
|
36
43
|
/** How many files one sweep batch handles; the next batch, or the next run, takes the rest. */
|
|
37
44
|
const SWEEP_BATCH = 20
|
|
38
45
|
|
|
@@ -96,12 +103,16 @@ export class StoredFileKinds {
|
|
|
96
103
|
readonly #store: FileObjectStore
|
|
97
104
|
readonly #files: StoredFiles
|
|
98
105
|
readonly #columns: readonly FileColumnRef[]
|
|
106
|
+
readonly #retentionMs: number
|
|
107
|
+
/** Serializes each object deletion of the sweep with a cancel, so no deletion is in flight once a cancel resolves. */
|
|
108
|
+
#deletions: Promise<void> = Promise.resolve()
|
|
99
109
|
|
|
100
|
-
constructor(kinds: readonly Kind[], store: FileObjectStore, files: StoredFiles, columns: readonly FileColumnRef[]) {
|
|
110
|
+
constructor(kinds: readonly Kind[], store: FileObjectStore, files: StoredFiles, columns: readonly FileColumnRef[], retentionMs: number) {
|
|
101
111
|
this.#kinds = new Map(kinds.map((kind) => [kind.name, kind]))
|
|
102
112
|
this.#store = store
|
|
103
113
|
this.#files = files
|
|
104
114
|
this.#columns = columns
|
|
115
|
+
this.#retentionMs = retentionMs
|
|
105
116
|
}
|
|
106
117
|
|
|
107
118
|
/** Whether the contract declares kind `name`. */
|
|
@@ -316,41 +327,54 @@ export class StoredFileKinds {
|
|
|
316
327
|
return new FileDownload({ body: object.body, size: file.size, contentType: file.type, name: file.name })
|
|
317
328
|
}
|
|
318
329
|
|
|
330
|
+
#serialized<T>(work: () => Promise<T>): Promise<T> {
|
|
331
|
+
const result = this.#deletions.then(work)
|
|
332
|
+
this.#deletions = result.then(() => undefined, () => undefined)
|
|
333
|
+
return result
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* Cancels the tombstones of `objectKeys`, so their objects stay: a restore cancels every key its restored rows
|
|
338
|
+
* reference. Once it resolves, no deletion of this sweep is in flight. Answers how many it cancelled.
|
|
339
|
+
*/
|
|
340
|
+
cancelDeletions(objectKeys: readonly string[]): Promise<number> {
|
|
341
|
+
return this.#serialized(() => this.#files.cancelDeletions(objectKeys))
|
|
342
|
+
}
|
|
343
|
+
|
|
319
344
|
/**
|
|
320
|
-
* The sweep: it
|
|
321
|
-
*
|
|
322
|
-
*
|
|
345
|
+
* The sweep: it adopts the migrated files a declared column references, then removes, batch by batch, every file past
|
|
346
|
+
* the grace that no declared column references: its row and the record of its object in one transaction (the foreign
|
|
347
|
+
* key keeps a referenced one). Last it claims each recorded object past the retention window, then deletes it.
|
|
323
348
|
*/
|
|
324
349
|
async sweep(signal: AbortSignal): Promise<void> {
|
|
325
350
|
let step = 'rows_read_failed'
|
|
326
351
|
try {
|
|
327
|
-
for (let keys = await this.#files.unfinishedDeletions(SWEEP_BATCH); keys.length > 0 && !signal.aborted;) {
|
|
328
|
-
for (const key of keys) {
|
|
329
|
-
step = 'object_delete_failed'
|
|
330
|
-
await this.#store.delete(key)
|
|
331
|
-
step = 'deletion_record_failed'
|
|
332
|
-
await this.#files.forgetDeletion(key)
|
|
333
|
-
}
|
|
334
|
-
step = 'rows_read_failed'
|
|
335
|
-
keys = keys.length === SWEEP_BATCH ? await this.#files.unfinishedDeletions(SWEEP_BATCH) : []
|
|
336
|
-
}
|
|
337
352
|
await this.#files.adoptReferenced(this.#columns)
|
|
338
353
|
for (let more = true; more && !signal.aborted;) {
|
|
339
354
|
step = 'rows_read_failed'
|
|
340
355
|
const batch = await this.#files.unreferenced(this.#columns, SWEEP_BATCH, FILE_PENDING_GRACE_MS)
|
|
341
356
|
for (const id of batch) {
|
|
342
357
|
step = 'row_delete_failed'
|
|
343
|
-
|
|
344
|
-
if (key === null) {
|
|
345
|
-
continue
|
|
346
|
-
}
|
|
347
|
-
step = 'object_delete_failed'
|
|
348
|
-
await this.#store.delete(key)
|
|
349
|
-
step = 'deletion_record_failed'
|
|
350
|
-
await this.#files.forgetDeletion(key)
|
|
358
|
+
await this.#files.remove(id, FILE_PENDING_GRACE_MS)
|
|
351
359
|
}
|
|
352
360
|
more = batch.length === SWEEP_BATCH
|
|
353
361
|
}
|
|
362
|
+
const cutoff = new Date(Date.now() - this.#retentionMs)
|
|
363
|
+
for (let more = true; more && !signal.aborted;) {
|
|
364
|
+
step = 'rows_read_failed'
|
|
365
|
+
const keys = await this.#files.dueDeletions(SWEEP_BATCH, cutoff)
|
|
366
|
+
for (const key of keys) {
|
|
367
|
+
await this.#serialized(async () => {
|
|
368
|
+
step = 'deletion_claim_failed'
|
|
369
|
+
// A crash or a failed delete after the claim leaks this object's bytes; it loses no data.
|
|
370
|
+
if (await this.#files.claimDeletion(key, cutoff)) {
|
|
371
|
+
step = 'object_delete_failed'
|
|
372
|
+
await this.#store.delete(key)
|
|
373
|
+
}
|
|
374
|
+
})
|
|
375
|
+
}
|
|
376
|
+
more = keys.length === SWEEP_BATCH
|
|
377
|
+
}
|
|
354
378
|
} catch {
|
|
355
379
|
console.error(`fabrika: stored files sweep: ${step}`)
|
|
356
380
|
throw new ServerFault('file_sweep_failed', 'the sweep of stored files failed; the next run retries it')
|
|
@@ -430,13 +454,15 @@ function fileColumns(artifact: ContractArtifact): FileColumnRef[] {
|
|
|
430
454
|
|
|
431
455
|
/**
|
|
432
456
|
* The file kinds of `artifact`, or `null` when it declares none. Every reason a kind cannot be served goes into
|
|
433
|
-
* `problems`, so `boot` refuses to start rather than take a file it cannot check.
|
|
457
|
+
* `problems`, so `boot` refuses to start rather than take a file it cannot check. A removed file's object outlives its
|
|
458
|
+
* row by `retentionMs`.
|
|
434
459
|
*/
|
|
435
460
|
export function storedFileKinds(
|
|
436
461
|
artifact: ContractArtifact,
|
|
437
462
|
database: ImplementationDatabase | null,
|
|
438
463
|
store: FileObjectStore | undefined,
|
|
439
464
|
problems: string[],
|
|
465
|
+
retentionMs: number,
|
|
440
466
|
): StoredFileKinds | null {
|
|
441
467
|
const entries = Object.entries(artifact.fileKinds ?? {})
|
|
442
468
|
if (entries.length === 0) {
|
|
@@ -450,6 +476,9 @@ export function storedFileKinds(
|
|
|
450
476
|
return null
|
|
451
477
|
}
|
|
452
478
|
for (const [name, kind] of entries) {
|
|
479
|
+
if (name.startsWith(RESERVED_FILE_KIND_PREFIX)) {
|
|
480
|
+
problems.push(`files '${name}' is a file kind name the framework reserves (${RESERVED_FILE_KIND_PREFIX}*)`)
|
|
481
|
+
}
|
|
453
482
|
for (const type of kind.types.filter((candidate) => !SNIFFABLE_TYPES.includes(candidate))) {
|
|
454
483
|
problems.push(`files '${name}' admits '${type}', whose bytes the framework cannot verify (allowed: ${SNIFFABLE_TYPES.join(', ')})`)
|
|
455
484
|
}
|
|
@@ -462,5 +491,6 @@ export function storedFileKinds(
|
|
|
462
491
|
store,
|
|
463
492
|
frameworkTablesOf(database).files,
|
|
464
493
|
fileColumns(artifact),
|
|
494
|
+
retentionMs,
|
|
465
495
|
)
|
|
466
496
|
}
|
|
@@ -2,19 +2,10 @@
|
|
|
2
2
|
// body, so the framework enforces it) and the share SPA's fragment bootstrap. A form's admission is
|
|
3
3
|
// `form-admission.ts`, a webhook's signature `verifiers.ts`.
|
|
4
4
|
|
|
5
|
-
import { LINK_BOOTSTRAP_FILE, SHARE_KEY_GLOBAL } from '../contract/defaults'
|
|
5
|
+
import { bodyDeadlineMs, LINK_BOOTSTRAP_FILE, SHARE_KEY_GLOBAL } from '../contract/defaults'
|
|
6
6
|
import { HttpError } from '../errors'
|
|
7
7
|
import { deadline, readBoundedBytes } from './bounded'
|
|
8
8
|
|
|
9
|
-
/** The slowest body a procedure waits for, in bytes per second: its deadline is `maxBytes` at this rate, 30 s at least. */
|
|
10
|
-
export const BODY_RATE_FLOOR = 16 * 1024
|
|
11
|
-
const MIN_BODY_DEADLINE_MS = 30_000
|
|
12
|
-
|
|
13
|
-
/** How long a body of at most `maxBytes` may take to arrive. */
|
|
14
|
-
export function bodyDeadlineMs(maxBytes: number): number {
|
|
15
|
-
return Math.max(MIN_BODY_DEADLINE_MS, Math.ceil((maxBytes / BODY_RATE_FLOOR) * 1000))
|
|
16
|
-
}
|
|
17
|
-
|
|
18
9
|
/**
|
|
19
10
|
* The body as bytes, read until `maxBytes`; a longer one stops the read and is a 413. A body whose stream fails before
|
|
20
11
|
* its end (the client closed the connection) is the sender's 400 `aborted`, never a failure of the application.
|
package/src/implement/boot.ts
CHANGED
|
@@ -31,6 +31,7 @@ import {
|
|
|
31
31
|
type FrameworkTables,
|
|
32
32
|
frameworkTablesOf,
|
|
33
33
|
isScopedDatabase,
|
|
34
|
+
type Session,
|
|
34
35
|
} from '@fabrika/db/framework'
|
|
35
36
|
import type { DataKind } from '@fabrika/db/tables'
|
|
36
37
|
import type { EmailProviderRegistry } from '@fabrika/email'
|
|
@@ -99,6 +100,11 @@ export interface BootOptions {
|
|
|
99
100
|
readonly requireProxySecret?: boolean
|
|
100
101
|
/** The object storage of the stored files, which the Bun adapter opens. Without it, a contract with file kinds does not boot. */
|
|
101
102
|
readonly files?: FileObjectStore
|
|
103
|
+
/**
|
|
104
|
+
* How long a removed file's object outlives its row, so a restored backup's rows find their files:
|
|
105
|
+
* `FILE_TOMBSTONE_RETENTION_MS` on Cloudflare. Zero when omitted, as on Bun.
|
|
106
|
+
*/
|
|
107
|
+
readonly fileRetentionMs?: number
|
|
102
108
|
/**
|
|
103
109
|
* The malware scanner of the scan job of `scan: true` file kinds, which the Bun adapter supplies to a job run
|
|
104
110
|
* (`clamscanScanner`). Without it the scan job leaves every quarantined file quarantined and reports.
|
|
@@ -168,6 +174,11 @@ export interface BootOptions {
|
|
|
168
174
|
* then `null`.
|
|
169
175
|
*/
|
|
170
176
|
readonly clientAddress?: 'vouched'
|
|
177
|
+
/**
|
|
178
|
+
* Runs in the transaction of every change of a job's wakes or lease (`DataLayerRuntime.projectWakes`): the Cloudflare
|
|
179
|
+
* object arms its alarm there. Bun supplies none.
|
|
180
|
+
*/
|
|
181
|
+
readonly projectWakes?: (transaction: Session) => Promise<void>
|
|
171
182
|
}
|
|
172
183
|
|
|
173
184
|
/** The pool size a runtime adapter that names none is assumed to have: the Bun adapter's default. */
|
|
@@ -280,16 +291,11 @@ function readProxySecret(framework: Readonly<Record<string, string>>, required:
|
|
|
280
291
|
return proxySecretDigest(secret)
|
|
281
292
|
}
|
|
282
293
|
|
|
283
|
-
/** What an application's SQLite database (a Durable Object's, ADR-0065) cannot serve:
|
|
294
|
+
/** What an application's SQLite database (a Durable Object's, ADR-0065) cannot serve: a scanning file kind. */
|
|
284
295
|
function sqliteProblems(artifact: ContractArtifact, database: Database | undefined, problems: string[]): void {
|
|
285
296
|
if (database?.dialect !== 'sqlite') {
|
|
286
297
|
return
|
|
287
298
|
}
|
|
288
|
-
for (const [site, reach] of Object.entries(artifact.declared ?? {})) {
|
|
289
|
-
if (reach.sql !== null) {
|
|
290
|
-
problems.push(`'${site}' declares sql: '${reach.sql}'; ctx.sql runs only on Postgres, and this application's database is SQLite (Cloudflare)`)
|
|
291
|
-
}
|
|
292
|
-
}
|
|
293
299
|
for (const [name, kind] of Object.entries(artifact.fileKinds ?? {})) {
|
|
294
300
|
if (kind.scan === true) {
|
|
295
301
|
problems.push(`files '${name}' declares scan: true; no malware scanner runs on Cloudflare, so a scanning file kind is not served there`)
|
|
@@ -397,6 +403,7 @@ function contractDatabase(
|
|
|
397
403
|
database: ImplementationDatabase | null,
|
|
398
404
|
connection: Database | undefined,
|
|
399
405
|
capabilities: CapabilityAuthority,
|
|
406
|
+
projectWakes: ((transaction: Session) => Promise<void>) | undefined,
|
|
400
407
|
problems: string[],
|
|
401
408
|
): BoundDatabase | null {
|
|
402
409
|
if (database === null) {
|
|
@@ -414,7 +421,12 @@ function contractDatabase(
|
|
|
414
421
|
return null
|
|
415
422
|
}
|
|
416
423
|
try {
|
|
417
|
-
const { locate, release } = bindScopedDatabaseProvisionally(database, {
|
|
424
|
+
const { locate, release } = bindScopedDatabaseProvisionally(database, {
|
|
425
|
+
database: connection,
|
|
426
|
+
capabilities,
|
|
427
|
+
readOnly: requestIsReadOnly,
|
|
428
|
+
...(projectWakes === undefined ? {} : { projectWakes }),
|
|
429
|
+
})
|
|
418
430
|
return { database: { tables: database.tables, locate }, framework: frameworkTablesOf(database), data: dataAccessOf(database), release }
|
|
419
431
|
} catch {
|
|
420
432
|
problems.push('the data layer was bound before boot; only the framework binds it, so remove @fabrika/db/framework from the application')
|
|
@@ -730,8 +742,8 @@ function bootOnce(app: ContractImplementation, options: BootOptions, binding: Bi
|
|
|
730
742
|
sqliteProblems(artifact, options.database, problems)
|
|
731
743
|
// This runtime's own: what another `boot` call mints, application code's included, its stores refuse.
|
|
732
744
|
const issuer = createCapabilityIssuer()
|
|
733
|
-
const fileKinds = storedFileKinds(artifact, app.database, objects, problems)
|
|
734
|
-
const bound = contractDatabase(app.database, options.database, issuer.authority, problems)
|
|
745
|
+
const fileKinds = storedFileKinds(artifact, app.database, objects, problems, options.fileRetentionMs ?? 0)
|
|
746
|
+
const bound = contractDatabase(app.database, options.database, issuer.authority, options.projectWakes, problems)
|
|
735
747
|
binding.release = bound?.release ?? null
|
|
736
748
|
const framework = bound?.framework ?? null
|
|
737
749
|
frameworkProblems(artifact, framework, problems)
|