@fabrika/app 0.9.0-dev.1 → 0.9.0-dev.11
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 +32 -26
- package/skill/SKILL.md +46 -15
- package/skill/references/app-setup.md +43 -12
- package/skill/references/dev.md +5 -2
- package/skill/references/endpoint.md +39 -28
- package/skill/references/errors.md +50 -37
- package/skill/references/files.md +8 -4
- package/skill/references/jobs.md +39 -7
- package/skill/references/table-and-migration.md +47 -3
- package/skill/references/tests.md +73 -16
- package/skill/references/webhooks-links-connectors.md +1 -1
- package/src/build-recipe.ts +198 -0
- package/src/build.ts +60 -4
- package/src/bun.ts +181 -13
- package/src/cloudflare/application.ts +822 -0
- package/src/cloudflare/bindings.ts +159 -0
- package/src/cloudflare/export.ts +621 -0
- package/src/cloudflare/import.ts +888 -0
- package/src/cloudflare/maintenance.ts +518 -0
- package/src/cloudflare/names.ts +16 -0
- package/src/cloudflare/ops.ts +375 -0
- package/src/cloudflare/r2-files.ts +172 -0
- package/src/cloudflare/restore.ts +167 -0
- package/src/cloudflare/tracker.ts +192 -0
- package/src/cloudflare.ts +100 -146
- package/src/contract/app-id.ts +5 -0
- package/src/contract/artifact-schema.ts +9 -1
- package/src/contract/build.ts +40 -2
- package/src/contract/capability-cases.ts +30 -0
- package/src/contract/compile.ts +1 -0
- package/src/contract/containers.ts +19 -0
- package/src/contract/context.ts +35 -9
- package/src/contract/declaration.ts +2 -0
- package/src/contract/defaults.ts +27 -4
- package/src/contract/define.ts +29 -3
- package/src/contract/index.ts +1 -0
- package/src/contract/kit.ts +4 -1
- package/src/contract/platform.ts +3 -2
- package/src/contract/vocabulary.ts +26 -8
- package/src/dev/changes.ts +5 -43
- package/src/dev/contract-digest.ts +17 -0
- package/src/dev/index.ts +1 -1
- package/src/dev/launcher.ts +197 -41
- package/src/dev/output.ts +13 -3
- package/src/dev/wire.ts +11 -1
- package/src/disk-assets.ts +8 -3
- package/src/files/archive.ts +2 -2
- package/src/files/handle.ts +2 -1
- package/src/files/job-names.ts +12 -0
- package/src/files/kinds.ts +74 -32
- package/src/files/scan.ts +3 -4
- package/src/files/streams.ts +3 -1
- package/src/implement/anonymous.ts +12 -11
- package/src/implement/boot.ts +84 -27
- package/src/implement/bounded.ts +21 -2
- package/src/implement/capability.ts +16 -4
- package/src/implement/dispatch.ts +423 -178
- package/src/implement/failures.ts +59 -4
- package/src/implement/faults.ts +5 -1
- package/src/implement/index.ts +1 -0
- package/src/implement/invocation.ts +62 -11
- package/src/implement/principal.ts +25 -2
- package/src/implement/terminals.ts +93 -0
- package/src/implement/tokens.ts +4 -2
- package/src/implement/types.ts +22 -4
- package/src/implement/workload.ts +42 -16
- package/src/jobs/mail.ts +8 -0
- package/src/jobs/report.ts +5 -1
- package/src/jobs/runner.ts +702 -126
- package/src/jobs/schedule.ts +7 -1
- package/src/jobs/scrub.ts +14 -1
- package/src/jobs/types.ts +6 -0
- package/src/spa-fallback.ts +12 -0
- package/src/testing/endpoint-access.ts +493 -202
- package/src/testing/index.ts +1 -1
- package/src/testing/isolation.ts +8 -1
- package/src/testing/kit.ts +86 -4
- package/src/testing/policy-isolation.ts +2 -2
- package/src/testing/proxy.ts +15 -10
- package/types/src/agent-skill.d.ts +29 -0
- package/types/src/build-recipe.d.ts +70 -0
- package/types/src/build.d.ts +29 -0
- package/types/src/bun.d.ts +99 -0
- package/types/src/client/caller.d.ts +19 -0
- package/types/src/client/contract.d.ts +131 -0
- package/types/src/client/error-reporting.d.ts +66 -0
- package/types/src/client/index.d.ts +4 -0
- package/types/src/client/rpc.d.ts +14 -0
- package/types/src/client/share.d.ts +2 -0
- package/types/src/client/transport.d.ts +42 -0
- package/types/src/cloudflare/application.d.ts +73 -0
- package/types/src/cloudflare/bindings.d.ts +33 -0
- package/types/src/cloudflare/export.d.ts +16 -0
- package/types/src/cloudflare/import.d.ts +47 -0
- package/types/src/cloudflare/maintenance.d.ts +117 -0
- package/types/src/cloudflare/names.d.ts +11 -0
- package/types/src/cloudflare/ops.d.ts +81 -0
- package/types/src/cloudflare/r2-files.d.ts +9 -0
- package/types/src/cloudflare/restore.d.ts +9 -0
- package/types/src/cloudflare/tracker.d.ts +29 -0
- package/types/src/cloudflare.d.ts +43 -0
- package/types/src/contract/app-id.d.ts +2 -0
- package/types/src/contract/artifact-schema.d.ts +10 -0
- package/types/src/contract/attachment.d.ts +41 -0
- package/types/src/contract/build.d.ts +12 -0
- package/types/src/contract/capability-cases.d.ts +9 -0
- package/types/src/contract/compile.d.ts +11 -0
- package/types/src/contract/connect.d.ts +51 -0
- package/types/src/contract/containers.d.ts +3 -0
- package/types/src/contract/context.d.ts +525 -0
- package/types/src/contract/declaration.d.ts +307 -0
- package/types/src/contract/defaults.d.ts +228 -0
- package/types/src/contract/define.d.ts +376 -0
- package/types/src/contract/file.d.ts +44 -0
- package/types/src/contract/group.d.ts +274 -0
- package/types/src/contract/handled.d.ts +18 -0
- package/types/src/contract/index.d.ts +20 -0
- package/types/src/contract/kit.d.ts +211 -0
- package/types/src/contract/platform.d.ts +9 -0
- package/types/src/contract/problems.d.ts +14 -0
- package/types/src/contract/redirect.d.ts +25 -0
- package/types/src/contract/schema-from-shape.d.ts +10 -0
- package/types/src/contract/schema-shape.d.ts +5 -0
- package/types/src/contract/snapshot-source.d.ts +15 -0
- package/types/src/contract/token.d.ts +58 -0
- package/types/src/contract/vocabulary.d.ts +32 -0
- package/types/src/contract/webhook-binding.d.ts +24 -0
- package/types/src/contract/webhook-scheme.d.ts +23 -0
- package/types/src/dev/changes.d.ts +7 -0
- package/types/src/dev/contract-digest.d.ts +1 -0
- package/types/src/dev/index.d.ts +2 -0
- package/types/src/dev/launcher.d.ts +32 -0
- package/types/src/dev/output.d.ts +28 -0
- package/types/src/dev/wire.d.ts +126 -0
- package/types/src/disk-assets.d.ts +6 -0
- package/types/src/errors.d.ts +46 -0
- package/types/src/fetch.d.ts +7 -0
- package/types/src/files/archive.d.ts +26 -0
- package/types/src/files/clamscan.d.ts +32 -0
- package/types/src/files/handle.d.ts +107 -0
- package/types/src/files/job-names.d.ts +9 -0
- package/types/src/files/kinds.d.ts +112 -0
- package/types/src/files/scan.d.ts +78 -0
- package/types/src/files/sniff.d.ts +7 -0
- package/types/src/files/streams.d.ts +17 -0
- package/types/src/files/treatment.d.ts +18 -0
- package/types/src/from-row.d.ts +20 -0
- package/types/src/honeypot.d.ts +2 -0
- package/types/src/implement/anonymous.d.ts +24 -0
- package/types/src/implement/attachment.d.ts +14 -0
- package/types/src/implement/boot.d.ts +128 -0
- package/types/src/implement/bounded.d.ts +32 -0
- package/types/src/implement/browser-config.d.ts +19 -0
- package/types/src/implement/capability.d.ts +142 -0
- package/types/src/implement/challenge.d.ts +75 -0
- package/types/src/implement/client-address.d.ts +11 -0
- package/types/src/implement/connect.d.ts +67 -0
- package/types/src/implement/crashes.d.ts +31 -0
- package/types/src/implement/dispatch.d.ts +251 -0
- package/types/src/implement/egress.d.ts +14 -0
- package/types/src/implement/environment.d.ts +14 -0
- package/types/src/implement/failures.d.ts +58 -0
- package/types/src/implement/faults.d.ts +48 -0
- package/types/src/implement/headers.d.ts +7 -0
- package/types/src/implement/implement.d.ts +58 -0
- package/types/src/implement/index.d.ts +25 -0
- package/types/src/implement/invocation.d.ts +136 -0
- package/types/src/implement/membership.d.ts +201 -0
- package/types/src/implement/multipart.d.ts +36 -0
- package/types/src/implement/params.d.ts +7 -0
- package/types/src/implement/principal.d.ts +57 -0
- package/types/src/implement/project.d.ts +11 -0
- package/types/src/implement/public.d.ts +61 -0
- package/types/src/implement/serve.d.ts +25 -0
- package/types/src/implement/terminals.d.ts +33 -0
- package/types/src/implement/tokens.d.ts +49 -0
- package/types/src/implement/types.d.ts +263 -0
- package/types/src/implement/urls.d.ts +15 -0
- package/types/src/implement/verifiers.d.ts +19 -0
- package/types/src/implement/webhook.d.ts +55 -0
- package/types/src/implement/workload.d.ts +61 -0
- package/types/src/implement/xlsx.d.ts +6 -0
- package/types/src/index.d.ts +12 -0
- package/types/src/jobs/exception.d.ts +40 -0
- package/types/src/jobs/mail.d.ts +67 -0
- package/types/src/jobs/report.d.ts +56 -0
- package/types/src/jobs/runner.d.ts +218 -0
- package/types/src/jobs/schedule.d.ts +17 -0
- package/types/src/jobs/scrub.d.ts +12 -0
- package/types/src/jobs/types.d.ts +98 -0
- package/types/src/react/caller.d.ts +30 -0
- package/types/src/react/errors.d.ts +40 -0
- package/types/src/react/hooks.d.ts +110 -0
- package/types/src/react/index.d.ts +5 -0
- package/types/src/react/provider.d.ts +58 -0
- package/types/src/react/store.d.ts +68 -0
- package/types/src/react/testing.d.ts +28 -0
- package/types/src/react/types.d.ts +33 -0
- package/types/src/rpc/builder.d.ts +52 -0
- package/types/src/rpc/contract.d.ts +23 -0
- package/types/src/rpc/dispatcher.d.ts +11 -0
- package/types/src/rpc/types.d.ts +62 -0
- package/types/src/service/define.d.ts +42 -0
- package/types/src/service/index.d.ts +3 -0
- package/types/src/service/serve.d.ts +28 -0
- package/types/src/spa-fallback.d.ts +2 -0
- package/types/src/standard-schema.d.ts +55 -0
- package/types/src/testing/caller.d.ts +95 -0
- package/types/src/testing/challenge.d.ts +11 -0
- package/types/src/testing/dev.d.ts +97 -0
- package/types/src/testing/endpoint-access.d.ts +132 -0
- package/types/src/testing/index.d.ts +12 -0
- package/types/src/testing/isolation.d.ts +3 -0
- package/types/src/testing/kit.d.ts +207 -0
- package/types/src/testing/membership.d.ts +132 -0
- package/types/src/testing/policy-isolation.d.ts +113 -0
- package/types/src/testing/preload.d.ts +1 -0
- package/types/src/testing/proxy.d.ts +40 -0
- package/types/src/testing/runner.d.ts +8 -0
- package/types/src/testing/sample-file.d.ts +5 -0
- package/types/src/testing/status.d.ts +4 -0
- package/types/src/undumpable.d.ts +5 -0
|
@@ -16,6 +16,7 @@ import {
|
|
|
16
16
|
type EndpointRow,
|
|
17
17
|
type ExportDescription,
|
|
18
18
|
FORM_URLENCODED_TYPE,
|
|
19
|
+
type IamTerminalRpc,
|
|
19
20
|
isAttachmentShape,
|
|
20
21
|
isFileShape,
|
|
21
22
|
isRedirectShape,
|
|
@@ -35,15 +36,18 @@ import {
|
|
|
35
36
|
sha256Hex,
|
|
36
37
|
type WebhookScheme,
|
|
37
38
|
} from '@fabrika/auth-core'
|
|
39
|
+
import { DatabaseError } from '@fabrika/db'
|
|
38
40
|
import { type CapabilityAuthority, type ContainerGrant, MAX_WITHIN_VALUES } from '@fabrika/db/capability'
|
|
39
41
|
import type { DataAccess, FileBinding, FrameworkTables } from '@fabrika/db/framework'
|
|
40
42
|
import type { DataKind } from '@fabrika/db/tables'
|
|
41
43
|
import type { TokenVerifier } from '@fabrika/jwks'
|
|
42
44
|
import { applyAnswerCache, withResponseHeaders } from '@fabrika/proxy-core/headers'
|
|
45
|
+
import { AsyncLocalStorage } from 'node:async_hooks'
|
|
43
46
|
import { createHash, timingSafeEqual } from 'node:crypto'
|
|
44
47
|
import { z } from 'zod/v4'
|
|
45
48
|
import { fileFieldsOf } from '../contract/build'
|
|
46
49
|
import {
|
|
50
|
+
bodyDeadlineMs,
|
|
47
51
|
BROWSER_CONFIG_FILE,
|
|
48
52
|
CHALLENGE_FIELD,
|
|
49
53
|
FILE_INPUT_PART,
|
|
@@ -67,9 +71,9 @@ import {
|
|
|
67
71
|
import type { RequestExecutionContext } from '../fetch'
|
|
68
72
|
import { FileWork, type StoredFileKinds, storedFilesHandle } from '../files/kinds'
|
|
69
73
|
import { FileDownload, fileResponse, withFileTreatment } from '../files/treatment'
|
|
70
|
-
import { abortedBody,
|
|
74
|
+
import { abortedBody, bootstrapPath, bootstrapResponse, readBodyInTime, withBootstrap } from './anonymous'
|
|
71
75
|
import { attachmentResponse } from './attachment'
|
|
72
|
-
import { boundedStream, handlerSignal, readBoundedBytes } from './bounded'
|
|
76
|
+
import { boundedStream, type Deadline, deadline, handlerSignal, readBoundedBytes } from './bounded'
|
|
73
77
|
import type { BrowserConfig } from './browser-config'
|
|
74
78
|
import {
|
|
75
79
|
type AppCapability,
|
|
@@ -88,7 +92,17 @@ import type { FailureReports } from './failures'
|
|
|
88
92
|
import { ServerFault } from './faults'
|
|
89
93
|
import { type HeaderPolicy, withHeaderPolicy } from './headers'
|
|
90
94
|
import type { ContractDatabase } from './implement'
|
|
91
|
-
import {
|
|
95
|
+
import {
|
|
96
|
+
boundTo,
|
|
97
|
+
boundWebhookApp,
|
|
98
|
+
endpointData,
|
|
99
|
+
Invocation,
|
|
100
|
+
type KickRecorder,
|
|
101
|
+
kickRecorder,
|
|
102
|
+
siteData,
|
|
103
|
+
type SiteDeclaration,
|
|
104
|
+
undeclaredSql,
|
|
105
|
+
} from './invocation'
|
|
92
106
|
import {
|
|
93
107
|
applicationDirectory,
|
|
94
108
|
createScope,
|
|
@@ -104,6 +118,7 @@ import { paramsObject, urlParams } from './params'
|
|
|
104
118
|
import { pageCallerOf, presentedBy, type Principal } from './principal'
|
|
105
119
|
import { project, ProjectionRefusedError } from './project'
|
|
106
120
|
import { bucketsOf, DEFAULT_ADMISSION, type InFlightShare, type ProcedureAdmission, requestClient } from './public'
|
|
121
|
+
import { type TerminalIdentification, terminalIdentification } from './terminals'
|
|
107
122
|
import type { SignedTokens } from './tokens'
|
|
108
123
|
import type { AppUrl } from './urls'
|
|
109
124
|
import { githubAccounts, verifyDelivery } from './verifiers'
|
|
@@ -119,6 +134,76 @@ import {
|
|
|
119
134
|
} from './webhook'
|
|
120
135
|
import type { WorkloadCredential } from './workload'
|
|
121
136
|
|
|
137
|
+
/** The classes of invocation an object's maintenance barrier admits per mode, counts and drains (ADR-0069 §3). */
|
|
138
|
+
export type InvocationKind = 'read' | 'write' | 'webhook' | 'kick' | 'job' | 'framework-job'
|
|
139
|
+
|
|
140
|
+
/** One admitted invocation, counted until it settles. */
|
|
141
|
+
export interface InvocationAdmission {
|
|
142
|
+
/** Aborts when a drain stops the invocation. */
|
|
143
|
+
readonly signal: AbortSignal
|
|
144
|
+
/** Counts `work` the invocation handed on until it settles, past the invocation's own end. */
|
|
145
|
+
track(work: Promise<unknown>): void
|
|
146
|
+
/** Ends the invocation's own count; called once it settled. */
|
|
147
|
+
release(): void
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** What a Cloudflare object's barrier puts around each of its events; `null` refuses an invocation. */
|
|
151
|
+
export interface InvocationGate {
|
|
152
|
+
admit(kind: InvocationKind): InvocationAdmission | null
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const invocationGate = new AsyncLocalStorage<InvocationGate>()
|
|
156
|
+
|
|
157
|
+
const UNGATED: InvocationAdmission = Object.freeze({ signal: new AbortController().signal, track: () => {}, release: () => {} })
|
|
158
|
+
|
|
159
|
+
/** Runs `work` with `gate` admitting every invocation it starts, the background work it hands on included. */
|
|
160
|
+
export function runGated<T>(gate: InvocationGate, work: () => T): T {
|
|
161
|
+
return invocationGate.run(gate, work)
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/** Admits an invocation of `kind` at the current event's gate; outside an object every invocation is admitted, uncounted. */
|
|
165
|
+
export function admitInvocation(kind: InvocationKind): InvocationAdmission | null {
|
|
166
|
+
const gate = invocationGate.getStore()
|
|
167
|
+
return gate === undefined ? UNGATED : gate.admit(kind)
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** How long a refused caller waits before it tries again, in seconds. */
|
|
171
|
+
const MAINTENANCE_RETRY_AFTER_S = 30
|
|
172
|
+
|
|
173
|
+
/** The answer while maintenance refuses an invocation: 503 with `Retry-After`, the fixed body of every 503. */
|
|
174
|
+
export function maintenanceRefusal(): Response {
|
|
175
|
+
const response = serverError(503)
|
|
176
|
+
response.headers.set('Retry-After', String(MAINTENANCE_RETRY_AFTER_S))
|
|
177
|
+
return response
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* A write the object's write fence refused: the invocation was admitted before a restrictive transition (ADR-0069 §3).
|
|
182
|
+
* A `DatabaseError`, so `@fabrika/db` passes it through its normalization of statement and commit failures unchanged;
|
|
183
|
+
* its fixed text names no statement or value. Dispatch answers it as `maintenanceRefusal()`.
|
|
184
|
+
*/
|
|
185
|
+
export class MaintenanceFenceError extends DatabaseError {
|
|
186
|
+
override name = 'MaintenanceFenceError'
|
|
187
|
+
|
|
188
|
+
constructor() {
|
|
189
|
+
super(null)
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/** The class an endpoint's invocation counts in: a writing endpoint, a webhook, or one that only reads. */
|
|
194
|
+
function invocationKind(served: ServedEndpoint): InvocationKind {
|
|
195
|
+
switch (served.kind) {
|
|
196
|
+
case 'webhook':
|
|
197
|
+
return 'webhook'
|
|
198
|
+
case 'procedure':
|
|
199
|
+
return served.site?.kind === 'mutation' && served.readOnly !== true ? 'write' : 'read'
|
|
200
|
+
case 'public':
|
|
201
|
+
return served.site.kind === 'mutation' ? 'write' : 'read'
|
|
202
|
+
default:
|
|
203
|
+
return 'read'
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
122
207
|
/** SHA-256 of a proxy secret: 32 bytes whatever the secret's length, as `timingSafeEqual` needs. */
|
|
123
208
|
export function proxySecretDigest(secret: string): Uint8Array {
|
|
124
209
|
return createHash('sha256').update(secret).digest()
|
|
@@ -241,6 +326,8 @@ export interface RuntimeParts {
|
|
|
241
326
|
readonly assets: SpaAssets | null
|
|
242
327
|
/** IAM's membership operations; `boot` refuses a contract that needs them when the adapter supplies none. */
|
|
243
328
|
readonly membership: IamMembership | null
|
|
329
|
+
/** IAM's identification operations, where the contract declares a terminal; `boot` refuses one without them. */
|
|
330
|
+
readonly terminals: IamTerminalRpc | null
|
|
244
331
|
/** The framework's own writes (admission buckets, webhook tables); `boot` refuses a webhook or a bucket without them. */
|
|
245
332
|
readonly framework: FrameworkTables | null
|
|
246
333
|
/** This runtime's capability mint, which `boot` alone holds, and the authority its stores are bound to. */
|
|
@@ -339,6 +426,7 @@ export class ContractRuntime {
|
|
|
339
426
|
|
|
340
427
|
async #answer(request: Request, plan: EndpointPlan, exec: RequestExecutionContext | undefined): Promise<Response | FileDownload> {
|
|
341
428
|
let where = 'request'
|
|
429
|
+
let selected = false
|
|
342
430
|
try {
|
|
343
431
|
const served = plan.outcome === 'endpoint' ? this.#parts.endpoints.get(plan.endpoint.id) : undefined
|
|
344
432
|
// Before any other answer: a direct dial learns nothing, not even whether its path exists.
|
|
@@ -352,40 +440,26 @@ export class ContractRuntime {
|
|
|
352
440
|
throw new NotFoundError(`No endpoint serves ${request.method} ${rawRequestTarget(request.url).pathname}`)
|
|
353
441
|
}
|
|
354
442
|
where = served.row.id
|
|
443
|
+
selected = true
|
|
355
444
|
this.#checkEndpoint(served, request)
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
case 'spa-prefix':
|
|
366
|
-
return new Response(null, { status: 308, headers: { Location: `${served.prefix}/${new URL(request.url).search}` } })
|
|
367
|
-
case 'link-spa':
|
|
368
|
-
if (rawRequestTarget(request.url).pathname === bootstrapPath(served.prefix)) {
|
|
369
|
-
return bootstrapResponse()
|
|
370
|
-
}
|
|
371
|
-
return await withBootstrap(await this.#asset(served, request), served.prefix)
|
|
372
|
-
case 'procedure':
|
|
373
|
-
return await this.#procedure(served, request, exec)
|
|
374
|
-
case 'public':
|
|
375
|
-
return await this.#public(served, request, exec)
|
|
376
|
-
case 'challenge':
|
|
377
|
-
return await this.#issueChallenge(served, request)
|
|
378
|
-
case 'webhook':
|
|
379
|
-
return await this.#webhook(served, request, plan.params, exec)
|
|
380
|
-
case 'description':
|
|
381
|
-
await this.#authorize(served.row, request)
|
|
382
|
-
await readInput({ row: served.row, input: null }, request)
|
|
383
|
-
return jsonResponse({ result: served.description }, 200)
|
|
445
|
+
// Counted from here, while it authorizes and uploads too, until it settled.
|
|
446
|
+
const admission = admitInvocation(invocationKind(served))
|
|
447
|
+
if (admission === null) {
|
|
448
|
+
return maintenanceRefusal()
|
|
449
|
+
}
|
|
450
|
+
try {
|
|
451
|
+
return await this.#served(served, request, plan, exec)
|
|
452
|
+
} finally {
|
|
453
|
+
admission.release()
|
|
384
454
|
}
|
|
385
455
|
} catch (error) {
|
|
456
|
+
if (error instanceof MaintenanceFenceError) {
|
|
457
|
+
// An expected refusal, not a failure: nothing it wrote was kept, and the caller retries.
|
|
458
|
+
return maintenanceRefusal()
|
|
459
|
+
}
|
|
386
460
|
const response = errorResponse(error, where)
|
|
387
461
|
if (response.status >= 500) {
|
|
388
|
-
const failure = { endpoint: where, requestId: request.headers.get(REQUEST_ID_HEADER), status: response.status, error }
|
|
462
|
+
const failure = { endpoint: where, selected, requestId: request.headers.get(REQUEST_ID_HEADER), status: response.status, error }
|
|
389
463
|
const delivery = this.#parts.failures.record(failure)
|
|
390
464
|
exec?.waitUntil(delivery)
|
|
391
465
|
}
|
|
@@ -393,6 +467,43 @@ export class ContractRuntime {
|
|
|
393
467
|
}
|
|
394
468
|
}
|
|
395
469
|
|
|
470
|
+
async #served(
|
|
471
|
+
served: ServedEndpoint,
|
|
472
|
+
request: Request,
|
|
473
|
+
plan: Extract<EndpointPlan, { outcome: 'endpoint' }>,
|
|
474
|
+
exec: RequestExecutionContext | undefined,
|
|
475
|
+
): Promise<Response | FileDownload> {
|
|
476
|
+
switch (served.kind) {
|
|
477
|
+
case 'health':
|
|
478
|
+
return jsonResponse({ status: 'ok' }, 200)
|
|
479
|
+
case 'spa': {
|
|
480
|
+
const caller = await this.#authorize(served.row, request)
|
|
481
|
+
return await this.#spaFile(served, request, caller)
|
|
482
|
+
}
|
|
483
|
+
case 'public-spa':
|
|
484
|
+
return await this.#spaFile(served, request, null)
|
|
485
|
+
case 'spa-prefix':
|
|
486
|
+
return new Response(null, { status: 308, headers: { Location: `${served.prefix}/${new URL(request.url).search}` } })
|
|
487
|
+
case 'link-spa':
|
|
488
|
+
if (rawRequestTarget(request.url).pathname === bootstrapPath(served.prefix)) {
|
|
489
|
+
return bootstrapResponse()
|
|
490
|
+
}
|
|
491
|
+
return await withBootstrap(await this.#asset(served, request), served.prefix)
|
|
492
|
+
case 'procedure':
|
|
493
|
+
return await this.#procedure(served, request, exec)
|
|
494
|
+
case 'public':
|
|
495
|
+
return await this.#public(served, request, exec)
|
|
496
|
+
case 'challenge':
|
|
497
|
+
return await this.#issueChallenge(served, request)
|
|
498
|
+
case 'webhook':
|
|
499
|
+
return await this.#webhook(served, request, plan.params, exec)
|
|
500
|
+
case 'description':
|
|
501
|
+
await this.#authorize(served.row, request)
|
|
502
|
+
await readInput({ row: served.row, input: null }, request)
|
|
503
|
+
return jsonResponse({ result: served.description }, 200)
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
|
|
396
507
|
/**
|
|
397
508
|
* An SPA mount's files, at `<prefix>/__fabrika-config.json` its browser config (`browser-config.ts`), and at
|
|
398
509
|
* `<prefix>/__fabrika-me.json` the caller document of the person the mount admitted. A public mount admits no one:
|
|
@@ -518,6 +629,22 @@ export class ContractRuntime {
|
|
|
518
629
|
const admission = served.admission ?? DEFAULT_ADMISSION
|
|
519
630
|
await this.#admit(served.row, admission, request, caller.subject)
|
|
520
631
|
const body = await procedureBody(served, request, this.#parts.fileKinds)
|
|
632
|
+
try {
|
|
633
|
+
return await this.#procedureInvocation(served, request, exec, caller, admission, body)
|
|
634
|
+
} finally {
|
|
635
|
+
// Whatever refuses the request once the body is open, a multipart body's stream and deadline end here.
|
|
636
|
+
await body.multipart?.close()
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
async #procedureInvocation(
|
|
641
|
+
served: Extract<ServedEndpoint, { kind: 'procedure' }>,
|
|
642
|
+
request: Request,
|
|
643
|
+
exec: RequestExecutionContext | undefined,
|
|
644
|
+
caller: Caller,
|
|
645
|
+
admission: ProcedureAdmission,
|
|
646
|
+
body: Awaited<ReturnType<typeof procedureBody>>,
|
|
647
|
+
): Promise<Response | FileDownload> {
|
|
521
648
|
// Signed tokens are verified as the input is parsed; the texts they carry are what the request may redirect to.
|
|
522
649
|
const carried = new Set<string>()
|
|
523
650
|
const fields = this.#parts.tokens.verifyFields(served.row.input, body.multipart === null ? body.input : body.multipart.json, carried)
|
|
@@ -528,58 +655,72 @@ export class ContractRuntime {
|
|
|
528
655
|
const invocation = site === undefined ? null : new Invocation(frame, this.#data(), site.kind === 'query' || served.readOnly === true)
|
|
529
656
|
const kicks = kickRecorder(`'${served.row.id}'`, site?.declaration.kicks ?? [])
|
|
530
657
|
let outcome: 'open' | 'committing' = 'open'
|
|
658
|
+
let budget: Deadline | null = null
|
|
531
659
|
try {
|
|
532
660
|
const input = body.multipart === null
|
|
533
661
|
? fields
|
|
534
662
|
: this.#parts.tokens.verifyFields(served.row.input, await this.#fileInput(served, body.multipart, work), carried)
|
|
535
663
|
// The handler's time starts after the last body byte, so a slow upload does not eat it.
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
result
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
? {}
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
664
|
+
budget = handlerSignal(request, admission.timeoutMs)
|
|
665
|
+
const { signal } = budget
|
|
666
|
+
const answer = await enclosedAnswer(invocation, this.#hold(served.row.id, exec), async () => {
|
|
667
|
+
let result: unknown
|
|
668
|
+
try {
|
|
669
|
+
result = await runInRequest(frame, async () => {
|
|
670
|
+
const membership = this.#membershipRequest(served.row, caller, request, frame)
|
|
671
|
+
const access = await this.#access(served.row, caller, input, frame, membership)
|
|
672
|
+
const binding = this.#parts.fileKinds?.binding(work)
|
|
673
|
+
const ctx = Object.freeze({
|
|
674
|
+
principal: caller.principal,
|
|
675
|
+
requestId: frame.id,
|
|
676
|
+
signal,
|
|
677
|
+
client: requestClient(request, this.#parts.clientVouched),
|
|
678
|
+
url: this.#parts.url,
|
|
679
|
+
...access.context,
|
|
680
|
+
...this.#facilities(served.facilities, served.row, membership, access.context['target'], {
|
|
681
|
+
credential: caller.token,
|
|
682
|
+
requestId: request.headers.get(REQUEST_ID_HEADER) ?? frame.id,
|
|
683
|
+
}),
|
|
684
|
+
...this.#terminals(caller.token, request.headers.get(REQUEST_ID_HEADER) ?? frame.id),
|
|
685
|
+
...(served.connect === undefined ? {} : { connect: connectContext(served.connect, frame) }),
|
|
686
|
+
...(site === undefined || invocation === null ? {} : siteContext(served.row, site, invocation, caller.subject, access.context, binding)),
|
|
687
|
+
// An export's consumer reaches its rows only, never the files of their owners.
|
|
688
|
+
...(site === undefined || invocation === null || served.readOnly === true
|
|
689
|
+
? {}
|
|
690
|
+
: { files: this.#files(served.row.id, invocation, site.kind, work) }),
|
|
691
|
+
...(served.binds === undefined || invocation === null
|
|
692
|
+
? {}
|
|
693
|
+
: { webhooks: this.#webhookBindings(served.row.id, served.binds, invocation, caller.subject) }),
|
|
694
|
+
...this.#signing(`'${served.row.id}'`, site?.declaration.signs ?? []),
|
|
695
|
+
kick: kicks.kick,
|
|
696
|
+
})
|
|
697
|
+
const args = { input: access.input, ctx, ...(query === undefined ? {} : { query }) }
|
|
698
|
+
const egress = served.egress
|
|
699
|
+
return egress === undefined
|
|
700
|
+
? await served.handler(args)
|
|
701
|
+
: await runAsSite(`'${served.row.id}'`, egress, async () => await served.handler(args))
|
|
565
702
|
})
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
outcome = 'committing'
|
|
577
|
-
await commitAnswer(invocation, signal, answer)
|
|
703
|
+
} finally {
|
|
704
|
+
frame.end()
|
|
705
|
+
}
|
|
706
|
+
// Before the answer opens a file's stream: a failed write then has nothing to cancel.
|
|
707
|
+
await this.#recordKicks(invocation, kicks)
|
|
708
|
+
const answer = await this.#answerOf(served, result, request, work, carried)
|
|
709
|
+
outcome = 'committing'
|
|
710
|
+
await commitAnswer(invocation, signal, answer)
|
|
711
|
+
return answer
|
|
712
|
+
})
|
|
578
713
|
this.#kickAll(kicks.kicked(), exec)
|
|
579
714
|
return answer
|
|
715
|
+
} catch (error) {
|
|
716
|
+
// The fence refuses inside the transaction, before its COMMIT: a known rollback, so what it staged goes now.
|
|
717
|
+
if (error instanceof MaintenanceFenceError) {
|
|
718
|
+
outcome = 'open'
|
|
719
|
+
}
|
|
720
|
+
throw error
|
|
580
721
|
} finally {
|
|
722
|
+
budget?.clear()
|
|
581
723
|
await invocation?.rollback()
|
|
582
|
-
await body.multipart?.close()
|
|
583
724
|
// Only a known rollback deletes what the request staged; after a commit that may have happened, the sweep decides.
|
|
584
725
|
if (outcome === 'open') {
|
|
585
726
|
await this.#parts.fileKinds?.release(work)
|
|
@@ -671,6 +812,22 @@ export class ContractRuntime {
|
|
|
671
812
|
return data
|
|
672
813
|
}
|
|
673
814
|
|
|
815
|
+
/** Where an invocation of `site` reports a long hold of its transaction: `FailureReports`, its delivery kept alive like a failure's. */
|
|
816
|
+
#hold(site: string, exec: RequestExecutionContext | undefined): Hold {
|
|
817
|
+
return {
|
|
818
|
+
site,
|
|
819
|
+
held: (heldMs) => {
|
|
820
|
+
// The transaction already settled: a report that cannot be made, kept or sent never changes the answer.
|
|
821
|
+
try {
|
|
822
|
+
const delivery = this.#parts.failures.held(site, heldMs).catch(() => undefined)
|
|
823
|
+
exec?.waitUntil(delivery)
|
|
824
|
+
} catch {
|
|
825
|
+
// Dropped, like a delivery that fails.
|
|
826
|
+
}
|
|
827
|
+
},
|
|
828
|
+
}
|
|
829
|
+
}
|
|
830
|
+
|
|
674
831
|
/** The caller's authority at IAM for this request, when the endpoint's access form names a dimension and an action. */
|
|
675
832
|
#membershipRequest(row: CredentialedEndpoint, caller: Caller, request: Request, frame: RequestFrame): MembershipRequest | null {
|
|
676
833
|
const iam = this.#parts.membership
|
|
@@ -687,6 +844,12 @@ export class ContractRuntime {
|
|
|
687
844
|
}
|
|
688
845
|
}
|
|
689
846
|
|
|
847
|
+
/** `ctx.terminals`, with the credential IAM takes the request's application from: the caller's, or the workload's. */
|
|
848
|
+
#terminals(credential: string, requestId: string): { readonly terminals?: TerminalIdentification } {
|
|
849
|
+
const rpc = this.#parts.terminals
|
|
850
|
+
return rpc === null ? {} : { terminals: terminalIdentification(rpc, { app: this.#parts.audience, credential, requestId }) }
|
|
851
|
+
}
|
|
852
|
+
|
|
690
853
|
/** `ctx.members` and `ctx.links`, for exactly the endpoints the artifact gives them. */
|
|
691
854
|
#facilities(
|
|
692
855
|
facilities: Facilities,
|
|
@@ -755,17 +918,21 @@ export class ContractRuntime {
|
|
|
755
918
|
}
|
|
756
919
|
}
|
|
757
920
|
|
|
758
|
-
/**
|
|
921
|
+
/**
|
|
922
|
+
* The values of `dimension` in which the caller holds `action`, directly or through a container, and its grants on the
|
|
923
|
+
* dimensions inside it, so a list returns every row a single-object read admits (S4); all for a global grant.
|
|
924
|
+
*/
|
|
759
925
|
#within(caller: Caller, action: string, dimension: string, frame: RequestFrame): Within<string, string> {
|
|
760
926
|
const direct = scopedValues(caller.perms, action, dimension)
|
|
761
927
|
if (direct === null) {
|
|
762
928
|
return this.#parts.mint.within(frame, dimension, action, caller.subject, 'all')
|
|
763
929
|
}
|
|
764
930
|
const held = this.#heldContainers(caller.perms, action, dimension)
|
|
765
|
-
|
|
931
|
+
const inside = this.#heldContained(caller.perms, action, dimension)
|
|
932
|
+
if (direct.length === 0 && held.length === 0 && inside.length === 0) {
|
|
766
933
|
throw new ForbiddenError(`The caller holds '${action}' in no ${dimension}`)
|
|
767
934
|
}
|
|
768
|
-
return this.#parts.mint.within(frame, dimension, action, caller.subject, direct, held)
|
|
935
|
+
return this.#parts.mint.within(frame, dimension, action, caller.subject, direct, held, inside)
|
|
769
936
|
}
|
|
770
937
|
|
|
771
938
|
/** A scope value the caller acts in: without a grant on it or on a container of it, 403 whether it exists or not. */
|
|
@@ -796,6 +963,7 @@ export class ContractRuntime {
|
|
|
796
963
|
/**
|
|
797
964
|
* Several capabilities, each minted as its single access form mints it, in their declared order, so the first one
|
|
798
965
|
* refused answers. Each reads its value from the input as the caller sent it; the fields they read are removed after.
|
|
966
|
+
* `HandlerInput` (`types.ts`) removes the same fields from the handler's input type.
|
|
799
967
|
*/
|
|
800
968
|
async #capabilities(
|
|
801
969
|
requirement: CapabilitiesRequirement,
|
|
@@ -908,7 +1076,7 @@ export class ContractRuntime {
|
|
|
908
1076
|
admission.repeatableToken,
|
|
909
1077
|
)
|
|
910
1078
|
}
|
|
911
|
-
return await this.#publicInvocation(served, request, body, fields, query, carried, authority
|
|
1079
|
+
return await this.#publicInvocation(served, request, body, fields, query, carried, authority, exec)
|
|
912
1080
|
} finally {
|
|
913
1081
|
await multipart?.close()
|
|
914
1082
|
release()
|
|
@@ -933,8 +1101,24 @@ export class ContractRuntime {
|
|
|
933
1101
|
}
|
|
934
1102
|
|
|
935
1103
|
/**
|
|
936
|
-
*
|
|
937
|
-
*
|
|
1104
|
+
* Writes each kick's wake in the invocation's transaction, after the handler and before the commit: a rollback leaves
|
|
1105
|
+
* none, and a crash between the commit and the kick's start leaves the wake to the scheduler. A kick due now is due by
|
|
1106
|
+
* the database's clock, which takes the lease and marks the wakes a run owns.
|
|
1107
|
+
*/
|
|
1108
|
+
async #recordKicks(invocation: Invocation | null, kicks: KickRecorder): Promise<void> {
|
|
1109
|
+
const framework = this.#parts.framework
|
|
1110
|
+
if (invocation === null || framework === null || !kicks.recorded()) {
|
|
1111
|
+
return
|
|
1112
|
+
}
|
|
1113
|
+
const transaction = await invocation.frameworkSite('ctx.kick').session()
|
|
1114
|
+
for (const wake of kicks.wakes(await framework.wakes.now(transaction))) {
|
|
1115
|
+
await framework.wakes.schedule(wake, transaction)
|
|
1116
|
+
}
|
|
1117
|
+
}
|
|
1118
|
+
|
|
1119
|
+
/**
|
|
1120
|
+
* Sends each job kicked without `at` once, after the commit: in the background, never awaited and never rejecting.
|
|
1121
|
+
* Without an execution context a kick runs detached.
|
|
938
1122
|
*/
|
|
939
1123
|
#kickAll(jobs: readonly string[], exec: RequestExecutionContext | undefined): void {
|
|
940
1124
|
for (const job of new Set(jobs)) {
|
|
@@ -974,9 +1158,10 @@ export class ContractRuntime {
|
|
|
974
1158
|
fields: Readonly<Record<string, unknown>>,
|
|
975
1159
|
query: Readonly<Record<string, unknown>> | undefined,
|
|
976
1160
|
carried: Set<string>,
|
|
977
|
-
perms: readonly PermissionEntry[],
|
|
1161
|
+
authority: { readonly perms: readonly PermissionEntry[]; readonly token: string },
|
|
978
1162
|
exec: RequestExecutionContext | undefined,
|
|
979
1163
|
): Promise<Response | FileDownload> {
|
|
1164
|
+
const { perms } = authority
|
|
980
1165
|
const kicks = kickRecorder(`'${served.row.id}'`, served.site.declaration.kicks)
|
|
981
1166
|
const frame = new RequestFrame(crypto.randomUUID(), { readOnly: served.site.kind === 'query', endpoint: served.row.id })
|
|
982
1167
|
const work = new FileWork(frame, frame.id)
|
|
@@ -995,55 +1180,69 @@ export class ContractRuntime {
|
|
|
995
1180
|
return this.#parts.mint.app(frame, action, subject)
|
|
996
1181
|
}
|
|
997
1182
|
let outcome: 'open' | 'committing' = 'open'
|
|
1183
|
+
let budget: Deadline | null = null
|
|
998
1184
|
try {
|
|
999
1185
|
// The file parts come after the challenge, so a refused visitor stores nothing.
|
|
1000
1186
|
const input = body.multipart === null
|
|
1001
1187
|
? fields
|
|
1002
1188
|
: this.#parts.tokens.verifyFields(served.row.input, await this.#fileInput(served, body.multipart, work), carried)
|
|
1003
1189
|
// The handler's time starts after the last body byte, so a slow upload does not eat it.
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
result
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1190
|
+
budget = handlerSignal(request, served.admission.timeoutMs)
|
|
1191
|
+
const { signal } = budget
|
|
1192
|
+
const answer = await enclosedAnswer(invocation, this.#hold(served.row.id, exec), async () => {
|
|
1193
|
+
let result: unknown
|
|
1194
|
+
try {
|
|
1195
|
+
result = await runInRequest(frame, async () => {
|
|
1196
|
+
const handles = invocation.handles(
|
|
1197
|
+
served.site.kind,
|
|
1198
|
+
served.site.declaration,
|
|
1199
|
+
subject,
|
|
1200
|
+
{ carried },
|
|
1201
|
+
this.#parts.fileKinds?.binding(work),
|
|
1202
|
+
)
|
|
1203
|
+
const where = `'${served.row.id}'`
|
|
1204
|
+
const ctx = Object.freeze({
|
|
1205
|
+
principal: Object.freeze({ kind: 'public', endpoint: served.row.id }),
|
|
1206
|
+
requestId: frame.id,
|
|
1207
|
+
signal,
|
|
1208
|
+
client: requestClient(request, this.#parts.clientVouched),
|
|
1209
|
+
url: this.#parts.url,
|
|
1210
|
+
app,
|
|
1211
|
+
db: Object.freeze({ app: (action: string) => handles.app(app(action)) }),
|
|
1212
|
+
...this.#signing(`'${served.row.id}'`, served.site.declaration.signs),
|
|
1213
|
+
files: storedFilesHandle(this.#parts.fileKinds, work, {
|
|
1214
|
+
where,
|
|
1215
|
+
check: boundTo(frame, () => false, 'ctx.files'),
|
|
1216
|
+
writes: served.site.kind !== 'query',
|
|
1217
|
+
}),
|
|
1218
|
+
...this.#terminals(authority.token, request.headers.get(REQUEST_ID_HEADER) ?? frame.id),
|
|
1219
|
+
kick: kicks.kick,
|
|
1220
|
+
})
|
|
1221
|
+
const args = { input, ctx, ...(query === undefined ? {} : { query }) }
|
|
1222
|
+
return await runAsSite(`'${served.row.id}'`, served.site.declaration.egress, async () => await served.handler(args))
|
|
1031
1223
|
})
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
}
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1224
|
+
} finally {
|
|
1225
|
+
frame.end()
|
|
1226
|
+
}
|
|
1227
|
+
await this.#recordKicks(invocation, kicks)
|
|
1228
|
+
const output = served.row.output
|
|
1229
|
+
const answer = output !== null && isFileShape(output)
|
|
1230
|
+
? await this.#fileKinds(served.row.id).download(work, served.row.id, output.kind, result)
|
|
1231
|
+
: outputResponse(served.row, served.output, result, request, carried)
|
|
1232
|
+
outcome = 'committing'
|
|
1233
|
+
await commitAnswer(invocation, signal, answer)
|
|
1234
|
+
return answer
|
|
1235
|
+
})
|
|
1044
1236
|
this.#kickAll(kicks.kicked(), exec)
|
|
1045
1237
|
return answer
|
|
1238
|
+
} catch (error) {
|
|
1239
|
+
// The fence refuses inside the transaction, before its COMMIT: a known rollback, so what it staged goes now.
|
|
1240
|
+
if (error instanceof MaintenanceFenceError) {
|
|
1241
|
+
outcome = 'open'
|
|
1242
|
+
}
|
|
1243
|
+
throw error
|
|
1046
1244
|
} finally {
|
|
1245
|
+
budget?.clear()
|
|
1047
1246
|
await invocation.rollback()
|
|
1048
1247
|
if (outcome === 'open') {
|
|
1049
1248
|
await this.#parts.fileKinds?.release(work)
|
|
@@ -1066,7 +1265,7 @@ export class ContractRuntime {
|
|
|
1066
1265
|
): Promise<Response> {
|
|
1067
1266
|
admitContentType(served.row, request)
|
|
1068
1267
|
const limit = maxBytesOf(served.row)
|
|
1069
|
-
const body = await
|
|
1268
|
+
const body = await readBodyInTime(request, limit)
|
|
1070
1269
|
const tables = this.#framework().webhooks
|
|
1071
1270
|
const bindingId = params[WEBHOOK_BINDING_PARAMETER] ?? ''
|
|
1072
1271
|
const binding = BINDING_ID.test(bindingId) ? await tables.binding(served.webhook, bindingId) : null
|
|
@@ -1130,65 +1329,70 @@ export class ContractRuntime {
|
|
|
1130
1329
|
// The claim stays outside the delivery's transaction, and its completion commits inside it, with the work.
|
|
1131
1330
|
const invocation = site === undefined ? null : new Invocation(frame, this.#data(), false)
|
|
1132
1331
|
const kicks = kickRecorder(`webhook '${served.webhook}'`, site?.kicks ?? [])
|
|
1133
|
-
const
|
|
1332
|
+
const budget = handlerSignal(request, budgetMs)
|
|
1333
|
+
const { signal } = budget
|
|
1134
1334
|
let committing = false
|
|
1135
1335
|
try {
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1336
|
+
await enclosed(invocation, this.#hold(served.row.id, exec), async () => {
|
|
1337
|
+
try {
|
|
1338
|
+
await runInRequest(frame, async () => {
|
|
1339
|
+
const subject = webhookSubject(binding.id)
|
|
1340
|
+
const reach = this.#webhookReach(served, frame, subject, binding.scope.value, chain)
|
|
1341
|
+
const principal: WebhookPrincipal = Object.freeze({ kind: 'webhook', bindingId: binding.id })
|
|
1342
|
+
const delivery = Object.freeze({ key })
|
|
1343
|
+
const handles = site === undefined || invocation === null
|
|
1344
|
+
? null
|
|
1345
|
+
: invocation.handles('mutation', site, subject, 'declared', this.#parts.fileKinds?.binding(work))
|
|
1346
|
+
const data = site === undefined || invocation === null || handles === null
|
|
1347
|
+
? {}
|
|
1348
|
+
: {
|
|
1349
|
+
db: siteData(handles, (action) => handles.app(reach.app(action))),
|
|
1350
|
+
sql: site.sql === null ? undeclaredSql(served.row.id, 'mutation') : invocation.sql(site.sql),
|
|
1351
|
+
}
|
|
1352
|
+
const client = requestClient(request, this.#parts.clientVouched)
|
|
1353
|
+
const args = {
|
|
1354
|
+
input,
|
|
1355
|
+
ctx: Object.freeze({
|
|
1356
|
+
principal,
|
|
1357
|
+
requestId: frame.id,
|
|
1358
|
+
signal,
|
|
1359
|
+
client,
|
|
1360
|
+
url: this.#parts.url,
|
|
1361
|
+
...reach.context,
|
|
1362
|
+
delivery,
|
|
1363
|
+
files: storedFilesHandle(this.#parts.fileKinds, work, {
|
|
1364
|
+
where: `webhook '${served.webhook}'`,
|
|
1365
|
+
check: boundTo(frame, () => false, 'ctx.files'),
|
|
1366
|
+
writes: true,
|
|
1367
|
+
}),
|
|
1368
|
+
...this.#signing(`webhook '${served.webhook}'`, site?.signs ?? []),
|
|
1369
|
+
kick: kicks.kick,
|
|
1370
|
+
...data,
|
|
1166
1371
|
}),
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
}
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
}
|
|
1185
|
-
committing = true
|
|
1186
|
-
await invocation?.commit(signal)
|
|
1372
|
+
}
|
|
1373
|
+
if (site === undefined) {
|
|
1374
|
+
await served.handler(args)
|
|
1375
|
+
} else {
|
|
1376
|
+
await runAsSite(`webhook '${served.webhook}'`, site.egress, async () => await served.handler(args))
|
|
1377
|
+
}
|
|
1378
|
+
})
|
|
1379
|
+
} finally {
|
|
1380
|
+
frame.end()
|
|
1381
|
+
}
|
|
1382
|
+
const using = invocation === null ? undefined : await invocation.frameworkSite('the delivery claim').session()
|
|
1383
|
+
if (!(await tables.completeDelivery(claim, using))) {
|
|
1384
|
+
throw new ServerFault('webhook_claim_lost', `the claim of a delivery to '${served.webhook}' expired and another copy took it over`)
|
|
1385
|
+
}
|
|
1386
|
+
await this.#recordKicks(invocation, kicks)
|
|
1387
|
+
committing = true
|
|
1388
|
+
await invocation?.commit(signal)
|
|
1389
|
+
})
|
|
1187
1390
|
this.#kickAll(kicks.kicked(), exec)
|
|
1188
1391
|
} catch (error) {
|
|
1189
1392
|
await invocation?.rollback()
|
|
1190
1393
|
// Only a known rollback deletes what the delivery staged; after a commit that may have happened, the sweep decides.
|
|
1191
|
-
|
|
1394
|
+
// The fence refuses inside the transaction, before its COMMIT, so its refusal is a known rollback.
|
|
1395
|
+
if (!committing || error instanceof MaintenanceFenceError) {
|
|
1192
1396
|
await this.#parts.fileKinds?.release(work)
|
|
1193
1397
|
}
|
|
1194
1398
|
// The provider redelivers a failed delivery; giving the claim back lets the redelivery run at once. The unsigned
|
|
@@ -1196,6 +1400,8 @@ export class ContractRuntime {
|
|
|
1196
1400
|
console.error(`fabrika: ${served.row.id}: delivery_failed; the provider's redelivery runs it again`)
|
|
1197
1401
|
await tables.releaseDelivery(claim)
|
|
1198
1402
|
throw error
|
|
1403
|
+
} finally {
|
|
1404
|
+
budget.clear()
|
|
1199
1405
|
}
|
|
1200
1406
|
return jsonResponse({ result: { received: true } }, 200)
|
|
1201
1407
|
}
|
|
@@ -1257,6 +1463,14 @@ export class ContractRuntime {
|
|
|
1257
1463
|
return values.length === 0 ? [] : [{ dimension: container, values }]
|
|
1258
1464
|
})
|
|
1259
1465
|
}
|
|
1466
|
+
|
|
1467
|
+
/** The caller's grants of `action` on every dimension that lies inside `dimension`. */
|
|
1468
|
+
#heldContained(perms: PermissionEntry[], action: string, dimension: string): ContainerGrant[] {
|
|
1469
|
+
return [...this.#parts.containers].flatMap(([inner, containers]) => {
|
|
1470
|
+
const values = containers.includes(dimension) ? scopedValues(perms, action, inner) ?? [] : []
|
|
1471
|
+
return values.length === 0 ? [] : [{ dimension: inner, values }]
|
|
1472
|
+
})
|
|
1473
|
+
}
|
|
1260
1474
|
}
|
|
1261
1475
|
|
|
1262
1476
|
/** `ctx.db` (by the access form) and `ctx.sql` of a staged-kit endpoint: the declared tag, or one that refuses. */
|
|
@@ -1475,7 +1689,7 @@ async function readInput(
|
|
|
1475
1689
|
const limit = served.row.admission?.maxBytes ?? null
|
|
1476
1690
|
const text = limit === null
|
|
1477
1691
|
? await request.text()
|
|
1478
|
-
: new TextDecoder().decode(await
|
|
1692
|
+
: new TextDecoder().decode(await readBodyInTime(request, limit))
|
|
1479
1693
|
if (mediaType(request.headers.get('Content-Type')) === FORM_URLENCODED_TYPE) {
|
|
1480
1694
|
body = paramsObject(new URLSearchParams(text), served.row.input)
|
|
1481
1695
|
} else if (text.trim() !== '') {
|
|
@@ -1547,16 +1761,17 @@ async function openMultipart(
|
|
|
1547
1761
|
if (Number.isFinite(declared) && declared > bodyBytes) {
|
|
1548
1762
|
throw tooLarge()
|
|
1549
1763
|
}
|
|
1550
|
-
const
|
|
1764
|
+
const timer = deadline(bodyDeadlineMs(maxBytes), request.signal)
|
|
1551
1765
|
const body = boundedStream(request.body ?? new Blob([]).stream(), bodyBytes)
|
|
1552
1766
|
const parts = multipartParts(body.stream, boundary, { maxHeaderBytes: PROCEDURE_LIMITS.maxPartHeaderBytes })
|
|
1553
|
-
const failure = (error: unknown) => (body.exceeded() ? tooLarge() : multipartFailure(error, parts,
|
|
1767
|
+
const failure = (error: unknown) => (body.exceeded() ? tooLarge() : multipartFailure(error, parts, timer.signal, request))
|
|
1554
1768
|
const stop = () => {
|
|
1555
1769
|
parts.cancel().catch(() => {})
|
|
1556
1770
|
}
|
|
1557
|
-
|
|
1771
|
+
timer.signal.addEventListener('abort', stop, { once: true })
|
|
1558
1772
|
const close = async () => {
|
|
1559
|
-
|
|
1773
|
+
timer.signal.removeEventListener('abort', stop)
|
|
1774
|
+
timer.clear()
|
|
1560
1775
|
await parts.cancel()
|
|
1561
1776
|
}
|
|
1562
1777
|
let json: Readonly<Record<string, unknown>>
|
|
@@ -1617,6 +1832,36 @@ async function openMultipart(
|
|
|
1617
1832
|
}
|
|
1618
1833
|
}
|
|
1619
1834
|
|
|
1835
|
+
/** The site an invocation's long hold is logged under, and where it is reported. */
|
|
1836
|
+
interface Hold {
|
|
1837
|
+
readonly site: string
|
|
1838
|
+
readonly held: (heldMs: number) => void
|
|
1839
|
+
}
|
|
1840
|
+
|
|
1841
|
+
/** Runs `work` in the invocation's transaction (`Invocation.enclose`), or as is without one. */
|
|
1842
|
+
function enclosed<T>(invocation: Invocation | null, hold: Hold, work: () => Promise<T>): Promise<T> {
|
|
1843
|
+
return invocation === null ? work() : invocation.enclose(work, hold.site, hold.held)
|
|
1844
|
+
}
|
|
1845
|
+
|
|
1846
|
+
/**
|
|
1847
|
+
* An answer's work, from the handler to its commit, in the invocation's transaction. Where the transaction encloses
|
|
1848
|
+
* the work (SQLite) its COMMIT runs after `commitAnswer`, so a failed one cancels a file's object stream here.
|
|
1849
|
+
*/
|
|
1850
|
+
async function enclosedAnswer<A extends Response | FileDownload>(invocation: Invocation | null, hold: Hold, work: () => Promise<A>): Promise<A> {
|
|
1851
|
+
let answer: A | undefined
|
|
1852
|
+
try {
|
|
1853
|
+
return await enclosed(invocation, hold, async () => {
|
|
1854
|
+
answer = await work()
|
|
1855
|
+
return answer
|
|
1856
|
+
})
|
|
1857
|
+
} catch (error) {
|
|
1858
|
+
if (answer instanceof FileDownload) {
|
|
1859
|
+
await answer.body.cancel()
|
|
1860
|
+
}
|
|
1861
|
+
throw error
|
|
1862
|
+
}
|
|
1863
|
+
}
|
|
1864
|
+
|
|
1620
1865
|
/**
|
|
1621
1866
|
* Commits the invocation of an answer that is ready: a file's object stream was opened before the commit, so a failed
|
|
1622
1867
|
* commit cancels it and nothing of it is sent.
|