@fabrika/app 0.9.0-dev.1 → 0.9.0-dev.10
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 +61 -10
- 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 +22 -2
- 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 +331 -169
- 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 +226 -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
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
// The Worker bindings a contract application's object boots with, each read structurally: IAM's application
|
|
2
|
+
// entrypoint, the SPA's Workers Assets and the owners' proxy Workers.
|
|
3
|
+
|
|
4
|
+
import { DecodedIamMembership, DecodedIamTerminals } from '@fabrika/auth'
|
|
5
|
+
import type { IamMembershipRpc, IamTerminalRpc, MintFromKeyResult } from '@fabrika/auth-core'
|
|
6
|
+
import { parseJwks } from '@fabrika/jwks'
|
|
7
|
+
import { z } from 'zod/v4'
|
|
8
|
+
import type { IamKeySource } from '../implement/boot.js'
|
|
9
|
+
import { CONNECT_BINDING_PREFIX, type ConnectFetch } from '../implement/connect.js'
|
|
10
|
+
import type { SpaAssets } from '../implement/dispatch.js'
|
|
11
|
+
import type { WorkloadTokenSource } from '../implement/workload.js'
|
|
12
|
+
import { isPageRoute } from '../spa-fallback.js'
|
|
13
|
+
|
|
14
|
+
/** The method `name` of a binding, bound to it, or `null` when it has none. */
|
|
15
|
+
export function method(target: unknown, name: string): ((...args: unknown[]) => unknown) | null {
|
|
16
|
+
if (typeof target !== 'object' || target === null || !(name in target)) {
|
|
17
|
+
return null
|
|
18
|
+
}
|
|
19
|
+
const found: unknown = Reflect.get(target, name)
|
|
20
|
+
return typeof found === 'function' ? (...args) => Reflect.apply(found, target, args) : null
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function bindingKeySource(binding: unknown): IamKeySource {
|
|
24
|
+
const getJwks = method(binding, 'getJwks')
|
|
25
|
+
if (getJwks === null) {
|
|
26
|
+
throw new Error('the IAM binding is missing (env.IAM); the platform binds it to every application Worker')
|
|
27
|
+
}
|
|
28
|
+
return {
|
|
29
|
+
async getJwks() {
|
|
30
|
+
const jwks = parseJwks(await getJwks())
|
|
31
|
+
if (jwks === null) {
|
|
32
|
+
throw new Error('IAM returned no usable key set')
|
|
33
|
+
}
|
|
34
|
+
return jwks
|
|
35
|
+
},
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* IAM's membership operations on the `IAM` binding (`PropustkaApplication`), every answer decoded like the HTTP
|
|
41
|
+
* transport's. A binding without them is no membership transport, and `boot` refuses a contract that needs one.
|
|
42
|
+
*/
|
|
43
|
+
export function bindingMembership(binding: unknown): IamMembershipRpc | undefined {
|
|
44
|
+
const methods = ['requestDelegation', 'createScope', 'assignMember', 'revokeMember', 'listMembers', 'issueLink', 'revokeLink'] as const
|
|
45
|
+
if (!methods.every((name) => method(binding, name) !== null)) {
|
|
46
|
+
return undefined
|
|
47
|
+
}
|
|
48
|
+
return new DecodedIamMembership(async (name, input) => {
|
|
49
|
+
const call = method(binding, name)
|
|
50
|
+
if (call === null) {
|
|
51
|
+
throw new Error(`the IAM binding has no ${name}`)
|
|
52
|
+
}
|
|
53
|
+
return await call(input)
|
|
54
|
+
})
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** The identifications at a terminal over the IAM binding, when its entrypoint serves them (ADR-0067). */
|
|
58
|
+
export function bindingTerminals(binding: unknown): IamTerminalRpc | undefined {
|
|
59
|
+
const methods = ['inspectIdentification', 'assertIdentity', 'cancelIdentification'] as const
|
|
60
|
+
if (!methods.every((name) => method(binding, name) !== null)) {
|
|
61
|
+
return undefined
|
|
62
|
+
}
|
|
63
|
+
return new DecodedIamTerminals(async (name, input) => {
|
|
64
|
+
const call = method(binding, name)
|
|
65
|
+
if (call === null) {
|
|
66
|
+
throw new Error(`the IAM binding has no ${name}`)
|
|
67
|
+
}
|
|
68
|
+
return await call(input)
|
|
69
|
+
})
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const mintFromKeyResultSchema = z.union([
|
|
73
|
+
z.strictObject({ ok: z.literal(true), token: z.string().min(1), expiresAt: z.number() }),
|
|
74
|
+
z.strictObject({ ok: z.literal(false), reason: z.enum(['invalid_key', 'unknown_principal', 'disabled', 'token_too_large']) }),
|
|
75
|
+
])
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* IAM's token exchange on the `IAM` binding (`mintFromKey`), for the keys of the jobs, webhooks and public procedures.
|
|
79
|
+
* An answer that is not IAM's decided outcome throws: "IAM could not be asked".
|
|
80
|
+
*/
|
|
81
|
+
export function bindingWorkloadTokens(binding: unknown): WorkloadTokenSource | undefined {
|
|
82
|
+
if (method(binding, 'mintFromKey') === null) {
|
|
83
|
+
return undefined
|
|
84
|
+
}
|
|
85
|
+
return {
|
|
86
|
+
async exchange(input): Promise<MintFromKeyResult> {
|
|
87
|
+
const mint = method(binding, 'mintFromKey')
|
|
88
|
+
if (mint === null) {
|
|
89
|
+
throw new Error('the IAM binding has no mintFromKey')
|
|
90
|
+
}
|
|
91
|
+
const parsed = mintFromKeyResultSchema.safeParse(await mint({ app: input.app, key: input.key, requestId: crypto.randomUUID() }))
|
|
92
|
+
if (!parsed.success) {
|
|
93
|
+
throw new Error('IAM answered the workload token exchange with something that is not its outcome')
|
|
94
|
+
}
|
|
95
|
+
return parsed.data
|
|
96
|
+
},
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* The SPA and link mounts' files from the Workers Assets binding, whose directory mirrors the request paths: the mount
|
|
102
|
+
* `/admin` reads `admin/…` of it, the root mount the rest (the Cloudflare deploy stages each mount's `dir` there). A page
|
|
103
|
+
* route that names no file (`isPageRoute`) gets the mount's `index.html`, as `diskAssets` serves it: Workers Assets
|
|
104
|
+
* answers `<prefix>/` with it. A missing asset stays 404.
|
|
105
|
+
*/
|
|
106
|
+
export function bindingAssets(binding: unknown): SpaAssets | undefined {
|
|
107
|
+
const fetchAsset = method(binding, 'fetch')
|
|
108
|
+
if (fetchAsset === null) {
|
|
109
|
+
return undefined
|
|
110
|
+
}
|
|
111
|
+
const fetched = async (request: Request): Promise<Response> => {
|
|
112
|
+
const response: unknown = await fetchAsset(request)
|
|
113
|
+
if (!(response instanceof Response)) {
|
|
114
|
+
throw new Error('the ASSETS binding answered something that is not a Response')
|
|
115
|
+
}
|
|
116
|
+
return response
|
|
117
|
+
}
|
|
118
|
+
return {
|
|
119
|
+
async serve(mount, request) {
|
|
120
|
+
const response = await fetched(request)
|
|
121
|
+
if (response.status !== 404 || !isPageRoute(request)) {
|
|
122
|
+
return response
|
|
123
|
+
}
|
|
124
|
+
const index = new URL(request.url)
|
|
125
|
+
index.pathname = mount.prefix === '/' ? '/' : `${mount.prefix}/`
|
|
126
|
+
index.search = ''
|
|
127
|
+
if (index.pathname === new URL(request.url).pathname) {
|
|
128
|
+
return response
|
|
129
|
+
}
|
|
130
|
+
await response.body?.cancel()
|
|
131
|
+
return await fetched(new Request(index, { method: request.method, headers: request.headers }))
|
|
132
|
+
},
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* The service bindings to owners' proxy Workers (`FABRIKA_CONNECT_<IMPORT>`), which `captureEnvironment` keeps from
|
|
138
|
+
* the application because no `FABRIKA_*` value reaches it. A binding routes by itself: the URL it is called with
|
|
139
|
+
* carries the owner's host, which the proxy Worker selects the application by.
|
|
140
|
+
*/
|
|
141
|
+
export function connectBindings(env: Readonly<Record<string, unknown>>): Record<string, ConnectFetch> {
|
|
142
|
+
const bindings: Record<string, ConnectFetch> = {}
|
|
143
|
+
for (const [name, binding] of Object.entries(env)) {
|
|
144
|
+
// Read, not tested with `in`: a binding stub may answer `fetch` without owning the property (Lopata's does not).
|
|
145
|
+
const fetchOwner: unknown = name.startsWith(CONNECT_BINDING_PREFIX) && typeof binding === 'object' && binding !== null
|
|
146
|
+
? Reflect.get(binding, 'fetch')
|
|
147
|
+
: undefined
|
|
148
|
+
if (typeof fetchOwner === 'function') {
|
|
149
|
+
bindings[name] = async (request) => {
|
|
150
|
+
const response: unknown = await Reflect.apply(fetchOwner, binding, [request])
|
|
151
|
+
if (!(response instanceof Response)) {
|
|
152
|
+
throw new Error(`the ${name} binding answered something that is not a Response`)
|
|
153
|
+
}
|
|
154
|
+
return response
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
return bindings
|
|
159
|
+
}
|