@fabrika/provider-cloudflare 0.9.0-dev.3 → 0.9.0-dev.5
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 +11 -10
- package/src/authoring.ts +56 -6
- package/src/bounds.ts +145 -4
- package/src/cli.ts +14 -0
- package/src/command.ts +29 -4
- package/src/contract-support.ts +235 -0
- package/src/control.ts +55 -118
- package/src/deploy-order.ts +35 -0
- package/src/index.ts +19 -3
- package/src/installation/init.ts +11 -18
- package/src/installation/installation.ts +2 -4
- package/src/installation/scaffold.ts +29 -2
- package/src/installation/templates/README.md +29 -11
- package/src/installation/templates/platform.yml +9 -5
- package/src/managed-secrets.ts +61 -0
- package/src/plan.ts +13 -3
- package/src/provider.ts +455 -64
- package/src/proxy-worker.ts +40 -16
- package/src/proxy.ts +48 -16
- package/src/runner-protocol.ts +35 -2
- package/src/spa-assets.ts +176 -0
- package/types/src/authoring.d.ts +27 -5
- package/types/src/bounds.d.ts +19 -0
- package/types/src/cli.d.ts +6 -0
- package/types/src/command.d.ts +8 -3
- package/types/src/contract-support.d.ts +80 -0
- package/types/src/control.d.ts +108 -69
- package/types/src/deploy-order.d.ts +8 -0
- package/types/src/index.d.ts +3 -3
- package/types/src/managed-secrets.d.ts +19 -0
- package/types/src/plan.d.ts +2 -1
- package/types/src/provider.d.ts +38 -1
- package/types/src/proxy-worker.d.ts +1 -0
- package/types/src/proxy.d.ts +21 -0
- package/types/src/runner-protocol.d.ts +32 -0
- package/types/src/spa-assets.d.ts +28 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fabrika/provider-cloudflare",
|
|
3
|
-
"version": "0.9.0-dev.
|
|
3
|
+
"version": "0.9.0-dev.5",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -43,15 +43,16 @@
|
|
|
43
43
|
},
|
|
44
44
|
"dependencies": {
|
|
45
45
|
"zod": "^3.25.76",
|
|
46
|
-
"@fabrika/app": "0.9.0-dev.
|
|
47
|
-
"@fabrika/auth": "0.9.0-dev.
|
|
48
|
-
"@fabrika/auth-core": "0.9.0-dev.
|
|
49
|
-
"@fabrika/check": "0.9.0-dev.
|
|
50
|
-
"@fabrika/email": "0.9.0-dev.
|
|
51
|
-
"@fabrika/iam-contract": "0.9.0-dev.
|
|
52
|
-
"@fabrika/installation-init": "0.9.0-dev.
|
|
53
|
-
"@fabrika/proxy-core": "0.9.0-dev.
|
|
54
|
-
"@fabrika/provider-contract": "0.9.0-dev.
|
|
46
|
+
"@fabrika/app": "0.9.0-dev.5",
|
|
47
|
+
"@fabrika/auth": "0.9.0-dev.5",
|
|
48
|
+
"@fabrika/auth-core": "0.9.0-dev.5",
|
|
49
|
+
"@fabrika/check": "0.9.0-dev.5",
|
|
50
|
+
"@fabrika/email": "0.9.0-dev.5",
|
|
51
|
+
"@fabrika/iam-contract": "0.9.0-dev.5",
|
|
52
|
+
"@fabrika/installation-init": "0.9.0-dev.5",
|
|
53
|
+
"@fabrika/proxy-core": "0.9.0-dev.5",
|
|
54
|
+
"@fabrika/provider-contract": "0.9.0-dev.5",
|
|
55
|
+
"ignore": "^5.3.2",
|
|
55
56
|
"oblaka-iac": "^0.0.18"
|
|
56
57
|
}
|
|
57
58
|
}
|
package/src/authoring.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
+
import { GENERATED_ENTRY_FILE } from '@fabrika/app/build'
|
|
1
2
|
import { APP_PROVIDER, type AppConfigBase, isProviderAuthoredApp, type ProviderAuthoredApp } from '@fabrika/provider-contract'
|
|
2
|
-
import
|
|
3
|
+
import { Worker } from 'oblaka-iac'
|
|
4
|
+
import { iamServiceReference } from './proxy'
|
|
3
5
|
|
|
4
6
|
/** Inputs used to materialize one environment's Cloudflare resource graph. */
|
|
5
7
|
export interface ResourceContext {
|
|
@@ -7,20 +9,68 @@ export interface ResourceContext {
|
|
|
7
9
|
readonly domain?: string
|
|
8
10
|
}
|
|
9
11
|
|
|
12
|
+
/** What an author may set on the application Worker `defineApp` builds; everything else is fixed by the platform. */
|
|
13
|
+
export interface ApplicationWorkerOptions {
|
|
14
|
+
/** The application's own bindings, beside `IAM`, which is always the platform's. */
|
|
15
|
+
readonly bindings?: Worker['options']['bindings']
|
|
16
|
+
readonly compatibility_date?: string
|
|
17
|
+
readonly compatibility_flags?: Worker['options']['compatibility_flags']
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** A Cloudflare app config as `fabrika.config.ts` writes it. */
|
|
21
|
+
export type CloudflareAppConfigInput =
|
|
22
|
+
& AppConfigBase
|
|
23
|
+
& (
|
|
24
|
+
| {
|
|
25
|
+
/**
|
|
26
|
+
* The application Worker, for a config that composes its own. The deployer builds the proxy Worker in front of it
|
|
27
|
+
* from the contract (ADR-0048); never return one.
|
|
28
|
+
*/
|
|
29
|
+
readonly resources: (ctx: ResourceContext) => Worker
|
|
30
|
+
readonly worker?: never
|
|
31
|
+
}
|
|
32
|
+
| {
|
|
33
|
+
readonly resources?: never
|
|
34
|
+
/** Without `resources`, `defineApp` builds the application Worker from these and the platform's fixed values. */
|
|
35
|
+
readonly worker?: ApplicationWorkerOptions
|
|
36
|
+
}
|
|
37
|
+
)
|
|
38
|
+
|
|
10
39
|
/** A Cloudflare app config loaded from `fabrika.config.ts` inside the runner checkout. */
|
|
11
|
-
export interface
|
|
12
|
-
/** The application Worker. The deployer builds the proxy Worker in front of it from the contract (ADR-0048)
|
|
40
|
+
export interface CloudflareAppConfig extends AppConfigBase, ProviderAuthoredApp<'cloudflare'> {
|
|
41
|
+
/** The application Worker. The deployer builds the proxy Worker in front of it from the contract (ADR-0048). */
|
|
13
42
|
readonly resources: (ctx: ResourceContext) => Worker
|
|
14
43
|
}
|
|
15
44
|
|
|
16
|
-
|
|
45
|
+
/**
|
|
46
|
+
* The application Worker of a config without `resources`: the values the bounds require of every application Worker
|
|
47
|
+
* (the generated entry, no route, no public URL, IAM at its application entrypoint) and the Worker name `<app>`, so its
|
|
48
|
+
* remote name is the deployment's own `<env>-<app>`.
|
|
49
|
+
*/
|
|
50
|
+
const applicationWorker = (id: string, options: ApplicationWorkerOptions = {}): Worker =>
|
|
51
|
+
new Worker({
|
|
52
|
+
dir: '.',
|
|
53
|
+
name: id,
|
|
54
|
+
main: `./${GENERATED_ENTRY_FILE}`,
|
|
55
|
+
compatibility_date: options.compatibility_date ?? '2025-10-01',
|
|
56
|
+
compatibility_flags: [...(options.compatibility_flags ?? ['nodejs_compat_v2'])],
|
|
57
|
+
observability: { enabled: true },
|
|
58
|
+
workers_dev: false,
|
|
59
|
+
preview_urls: false,
|
|
60
|
+
routes: [],
|
|
61
|
+
bindings: { ...options.bindings, IAM: iamServiceReference() },
|
|
62
|
+
})
|
|
17
63
|
|
|
18
|
-
/**
|
|
64
|
+
/**
|
|
65
|
+
* Validate the app id and fill in the application Worker when the config composes none. The result always carries
|
|
66
|
+
* `resources`, because a runner narrows a loaded config with `isCloudflareAppConfig`, which requires it.
|
|
67
|
+
*/
|
|
19
68
|
export function defineApp(config: CloudflareAppConfigInput): CloudflareAppConfig {
|
|
20
69
|
if (typeof config.id !== 'string' || config.id.trim() === '') {
|
|
21
70
|
throw new Error('defineApp: `id` is required and must be a non-empty string')
|
|
22
71
|
}
|
|
23
|
-
|
|
72
|
+
const { resources, worker, ...rest } = config
|
|
73
|
+
return { ...rest, resources: resources ?? (() => applicationWorker(config.id, worker)), [APP_PROVIDER]: 'cloudflare' }
|
|
24
74
|
}
|
|
25
75
|
|
|
26
76
|
/** Narrow an imported module default without trusting code from the checkout. */
|
package/src/bounds.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { CLOUDFLARE_APPLICATION_BINDING, CLOUDFLARE_APPLICATION_CLASS, CLOUDFLARE_FILES_BINDING, GENERATED_ENTRY_FILE } from '@fabrika/app/build'
|
|
2
|
+
import { type AppEnvironment, appEnvironmentId, FRAMEWORK_VARIABLE_NAMES, isFrameworkVariableName, parseAppEnvironmentId } from '@fabrika/auth-core'
|
|
2
3
|
import { isProxyHost } from '@fabrika/proxy-core/manifest'
|
|
3
|
-
import { resolve } from 'node:path'
|
|
4
|
+
import { dirname, resolve } from 'node:path'
|
|
4
5
|
import type { GeneratedConfig, Worker } from 'oblaka-iac'
|
|
5
6
|
import { CloudflareClient } from 'oblaka-iac/src/client'
|
|
6
7
|
import { ResourceProcessor } from 'oblaka-iac/src/commands/resource-processor'
|
|
7
8
|
import { PLATFORM_RESOURCE_NAMES } from './plan'
|
|
8
|
-
import { CLOUDFLARE_PROXY_WORKER_DIR, IAM_APPLICATION_ENTRYPOINT, IAM_SERVICE } from './proxy'
|
|
9
|
+
import { CLOUDFLARE_PROXY_WORKER_DIR, IAM_APPLICATION_ENTRYPOINT, IAM_SERVICE, PROXY_MANIFEST_DEFINE } from './proxy'
|
|
9
10
|
|
|
10
11
|
type WranglerConfig = GeneratedConfig['config']
|
|
11
12
|
|
|
@@ -38,8 +39,34 @@ export interface DeploymentBounds {
|
|
|
38
39
|
readonly application: Readonly<Record<string, string>>
|
|
39
40
|
readonly proxy: Readonly<Record<string, string>> | null
|
|
40
41
|
}
|
|
42
|
+
/** The `define` literal of the manifest the deployer baked into its proxy Worker, or null when it built none. */
|
|
43
|
+
readonly proxyManifest: string | null
|
|
44
|
+
/** What the platform gives a contract application's Worker (ADR-0065), or null outside one. */
|
|
45
|
+
readonly application: ApplicationRuntime | null
|
|
41
46
|
}
|
|
42
47
|
|
|
48
|
+
/** The Durable Object, files bucket and cron trigger of a contract application's Worker; the application authors none of them. */
|
|
49
|
+
export interface ApplicationRuntime {
|
|
50
|
+
/** The remote name of the environment's files bucket. */
|
|
51
|
+
readonly filesBucket: string
|
|
52
|
+
/**
|
|
53
|
+
* The cron triggers the Worker carries when the contract has a job or a file kind, else an empty list: the hourly repair
|
|
54
|
+
* tick of a release that serves alarms, otherwise the minute tick.
|
|
55
|
+
*/
|
|
56
|
+
readonly crons: readonly string[]
|
|
57
|
+
/**
|
|
58
|
+
* The absolute path of the entry the deploy regenerates, which the Worker's `main` must name (`pinsGeneratedEntry`), or
|
|
59
|
+
* null for a release whose Worker keeps its authored `main`.
|
|
60
|
+
*/
|
|
61
|
+
readonly entry: string | null
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* The one migration of the application's object. Its tag is fixed: Cloudflare records the last tag a script applied, so
|
|
66
|
+
* a tag that moved would migrate the class twice. The deployer binds the object first, so oblaka numbers it `v0001`.
|
|
67
|
+
*/
|
|
68
|
+
const APPLICATION_OBJECT_MIGRATION = { tag: 'v0001', new_sqlite_classes: [CLOUDFLARE_APPLICATION_CLASS] } as const
|
|
69
|
+
|
|
43
70
|
/** The framework variables among a Worker's `vars`, as the deployer wrote them. */
|
|
44
71
|
export const frameworkValues = (vars: Readonly<Record<string, unknown>> | undefined): Record<string, string> => {
|
|
45
72
|
const values: Record<string, string> = {}
|
|
@@ -79,6 +106,9 @@ const CONFIG_KEYS: ReadonlySet<string> = new Set([
|
|
|
79
106
|
'workflows',
|
|
80
107
|
])
|
|
81
108
|
|
|
109
|
+
/** The platform proxy's config also carries the `define` that bakes its manifest into the bundle. */
|
|
110
|
+
const PROXY_CONFIG_KEYS: ReadonlySet<string> = new Set([...CONFIG_KEYS, 'define'])
|
|
111
|
+
|
|
82
112
|
/** Everything wrangler reads out of `assets`. `directory` is the only one that names a path. */
|
|
83
113
|
const ASSETS_KEYS: ReadonlySet<string> = new Set(['binding', 'directory', 'html_handling', 'not_found_handling', 'run_worker_first'])
|
|
84
114
|
|
|
@@ -226,12 +256,29 @@ const assertServices = (worker: string, config: WranglerConfig, names: Applicati
|
|
|
226
256
|
`cloudflare: Worker \`${worker}\` binds \`${service}\` to ${bound}; an application binds \`${IAM_APPLICATION_ENTRYPOINT}\` (use \`iamServiceReference()\`)`,
|
|
227
257
|
)
|
|
228
258
|
}
|
|
259
|
+
assertIamIdentity(worker, binding, field(entry, 'props'), names)
|
|
229
260
|
continue
|
|
230
261
|
}
|
|
231
262
|
assertOwnRemote(`Worker \`${worker}\` binds service`, service, names)
|
|
232
263
|
}
|
|
233
264
|
}
|
|
234
265
|
|
|
266
|
+
/**
|
|
267
|
+
* IAM reads a binding's `props.app` as the calling application (ADR-0067 §3), so an IAM binding names exactly this
|
|
268
|
+
* deployment's IAM application id and carries nothing else. The deployer writes the `IAM` binding that way.
|
|
269
|
+
*/
|
|
270
|
+
const assertIamIdentity = (worker: string, binding: unknown, props: unknown, names: ApplicationNames): void => {
|
|
271
|
+
const app = appEnvironmentId(names.self.app, names.self.env)
|
|
272
|
+
const keys = typeof props === 'object' && props !== null ? Object.keys(props) : []
|
|
273
|
+
if (keys.length !== 1 || field(props, 'app') !== app) {
|
|
274
|
+
throw new Error(
|
|
275
|
+
`cloudflare: Worker \`${worker}\` binds IAM as \`${
|
|
276
|
+
String(binding)
|
|
277
|
+
}\` without naming its own application \`${app}\`; only the deployer's \`IAM\` binding reaches IAM`,
|
|
278
|
+
)
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
|
|
235
282
|
/** Every remote name a config's bindings carry, beside the services. */
|
|
236
283
|
const assertResourceBindings = (worker: string, config: WranglerConfig, names: ApplicationNames): void => {
|
|
237
284
|
const binds = (kind: string, remote: unknown): void => assertOwnRemote(`Worker \`${worker}\` binds ${kind}`, remote, names)
|
|
@@ -341,6 +388,95 @@ const assertFrameworkVariables = (worker: string, vars: unknown, expected: Reado
|
|
|
341
388
|
}
|
|
342
389
|
}
|
|
343
390
|
|
|
391
|
+
/** The proxy's `define` is exactly the manifest the deployer baked: nothing else is substituted into its bundle. */
|
|
392
|
+
const assertBakedManifest = (worker: string, define: unknown, expected: string | null): void => {
|
|
393
|
+
const names = typeof define === 'object' && define !== null && !Array.isArray(define) ? Object.keys(define) : null
|
|
394
|
+
const exact = expected === null
|
|
395
|
+
? define === undefined
|
|
396
|
+
: names?.length === 1 && names[0] === PROXY_MANIFEST_DEFINE && field(define, PROXY_MANIFEST_DEFINE) === expected
|
|
397
|
+
if (!exact) {
|
|
398
|
+
throw new Error(`cloudflare: proxy Worker \`${worker}\` does not carry exactly the manifest Fabrika baked into it (\`define\`)`)
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
const isApplicationMigration = (migration: unknown): boolean => {
|
|
403
|
+
const keys = typeof migration === 'object' && migration !== null ? Object.keys(migration).sort() : []
|
|
404
|
+
const classes = field(migration, 'new_sqlite_classes')
|
|
405
|
+
return keys.join(',') === 'new_sqlite_classes,tag' && field(migration, 'tag') === APPLICATION_OBJECT_MIGRATION.tag
|
|
406
|
+
&& Array.isArray(classes) && classes.length === 1 && classes[0] === CLOUDFLARE_APPLICATION_CLASS
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
const namesApplicationClass = (migration: unknown): boolean => JSON.stringify(migration).includes(JSON.stringify(CLOUDFLARE_APPLICATION_CLASS))
|
|
410
|
+
|
|
411
|
+
const isPlatformTriggers = (triggers: unknown, crons: readonly string[]): boolean => {
|
|
412
|
+
const keys = typeof triggers === 'object' && triggers !== null ? Object.keys(triggers) : []
|
|
413
|
+
const written = field(triggers, 'crons')
|
|
414
|
+
return keys.length === 1 && Array.isArray(written) && written.length === crons.length && crons.every((cron, index) => written[index] === cron)
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
/**
|
|
418
|
+
* A contract application's Worker binds its own object (`FABRIKA_APPLICATION`, the class the generated entry exports)
|
|
419
|
+
* under the one fixed migration, the environment's files bucket, and exactly the platform's cron triggers.
|
|
420
|
+
*/
|
|
421
|
+
const assertApplicationRuntime = (worker: string, config: WranglerConfig, runtime: ApplicationRuntime): void => {
|
|
422
|
+
const objects = entries(config.durable_objects?.bindings, worker, 'durable_objects.bindings').filter((entry) =>
|
|
423
|
+
field(entry, 'name') === CLOUDFLARE_APPLICATION_BINDING || field(entry, 'class_name') === CLOUDFLARE_APPLICATION_CLASS
|
|
424
|
+
)
|
|
425
|
+
const object = objects[0]
|
|
426
|
+
if (
|
|
427
|
+
objects.length !== 1 || field(object, 'name') !== CLOUDFLARE_APPLICATION_BINDING || field(object, 'class_name') !== CLOUDFLARE_APPLICATION_CLASS
|
|
428
|
+
|| field(object, 'script_name') !== undefined
|
|
429
|
+
) {
|
|
430
|
+
throw new Error(
|
|
431
|
+
`cloudflare: application Worker \`${worker}\` must bind \`${CLOUDFLARE_APPLICATION_BINDING}\` once, to its own \`${CLOUDFLARE_APPLICATION_CLASS}\`; the platform writes that binding`,
|
|
432
|
+
)
|
|
433
|
+
}
|
|
434
|
+
const migrations = entries(config.migrations, worker, 'migrations')
|
|
435
|
+
if (!isApplicationMigration(migrations[0]) || migrations.slice(1).some(namesApplicationClass)) {
|
|
436
|
+
throw new Error(
|
|
437
|
+
`cloudflare: application Worker \`${worker}\` must open its migrations with \`${APPLICATION_OBJECT_MIGRATION.tag}\` creating \`${CLOUDFLARE_APPLICATION_CLASS}\` and name the class nowhere else; a committed \`wrangler.jsonc\` with a history of its own cannot be deployed`,
|
|
438
|
+
)
|
|
439
|
+
}
|
|
440
|
+
const files = entries(config.r2_buckets, worker, 'r2_buckets').filter((entry) => field(entry, 'binding') === CLOUDFLARE_FILES_BINDING)
|
|
441
|
+
if (files.length !== 1 || field(files[0], 'bucket_name') !== runtime.filesBucket) {
|
|
442
|
+
throw new Error(`cloudflare: application Worker \`${worker}\` must bind \`${CLOUDFLARE_FILES_BINDING}\` once, to \`${runtime.filesBucket}\``)
|
|
443
|
+
}
|
|
444
|
+
if (!isPlatformTriggers(config.triggers, runtime.crons)) {
|
|
445
|
+
throw new Error(`cloudflare: application Worker \`${worker}\` sets \`triggers\` the platform did not write; its cron triggers are the platform's`)
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
/**
|
|
450
|
+
* A pinned release's Worker runs the entry the deploy regenerates, and nothing that entry does not export: a hand-written
|
|
451
|
+
* `main` would skip the contract digest check, the hop secret and the framework's scheduling.
|
|
452
|
+
*/
|
|
453
|
+
const assertGeneratedEntry = (worker: string, generated: GeneratedConfig, cwd: string, entry: string): void => {
|
|
454
|
+
const config = generated.config
|
|
455
|
+
const main: unknown = config.main
|
|
456
|
+
if (typeof main !== 'string' || resolve(dirname(resolve(cwd, generated.path)), main) !== entry) {
|
|
457
|
+
throw new Error(
|
|
458
|
+
`cloudflare: application Worker \`${worker}\` runs \`${
|
|
459
|
+
typeof main === 'string' ? main : '(not a string)'
|
|
460
|
+
}\`; on its framework release it runs \`${GENERATED_ENTRY_FILE}\` beside its config, which the deploy regenerates`,
|
|
461
|
+
)
|
|
462
|
+
}
|
|
463
|
+
for (const key of ['workflows', 'containers'] as const) {
|
|
464
|
+
if (entries(config[key], worker, key).length > 0) {
|
|
465
|
+
throw new Error(`cloudflare: application Worker \`${worker}\` declares \`${key}\`, which the generated entry does not export`)
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
const object = entries(config.durable_objects?.bindings, worker, 'durable_objects.bindings').find((binding) =>
|
|
469
|
+
field(binding, 'name') !== CLOUDFLARE_APPLICATION_BINDING
|
|
470
|
+
)
|
|
471
|
+
if (object !== undefined) {
|
|
472
|
+
throw new Error(
|
|
473
|
+
`cloudflare: application Worker \`${worker}\` binds the Durable Object \`${
|
|
474
|
+
String(field(object, 'name'))
|
|
475
|
+
}\`; the generated entry exports only \`${CLOUDFLARE_APPLICATION_CLASS}\``,
|
|
476
|
+
)
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
|
|
344
480
|
const isPlatformProxy = (generated: GeneratedConfig, bounds: DeploymentBounds): boolean =>
|
|
345
481
|
bounds.proxy !== null && generated.config.name === bounds.proxy && resolve(bounds.cwd, generated.path) === PROXY_CONFIG_PATH
|
|
346
482
|
|
|
@@ -366,8 +502,9 @@ export const assertGeneratedConfigs = (configs: readonly GeneratedConfig[], boun
|
|
|
366
502
|
for (const generated of configs) {
|
|
367
503
|
const config = generated.config
|
|
368
504
|
const worker = config.name ?? generated.path
|
|
505
|
+
const keys = isPlatformProxy(generated, bounds) ? PROXY_CONFIG_KEYS : CONFIG_KEYS
|
|
369
506
|
for (const key of Object.keys(config)) {
|
|
370
|
-
if (!
|
|
507
|
+
if (!keys.has(key)) {
|
|
371
508
|
throw new Error(`cloudflare: Worker \`${worker}\` sets \`${key}\`, which an app may not configure; declare resources in \`bindings\``)
|
|
372
509
|
}
|
|
373
510
|
}
|
|
@@ -378,6 +515,7 @@ export const assertGeneratedConfigs = (configs: readonly GeneratedConfig[], boun
|
|
|
378
515
|
if (isPlatformProxy(generated, bounds)) {
|
|
379
516
|
assertRoutedHere(worker, config, bounds)
|
|
380
517
|
assertFrameworkVariables(worker, config.vars, bounds.frameworkVariables.proxy ?? {})
|
|
518
|
+
assertBakedManifest(worker, config.define, bounds.proxyManifest)
|
|
381
519
|
continue
|
|
382
520
|
}
|
|
383
521
|
if (resolve(bounds.cwd, generated.path) === PROXY_CONFIG_PATH) {
|
|
@@ -385,6 +523,9 @@ export const assertGeneratedConfigs = (configs: readonly GeneratedConfig[], boun
|
|
|
385
523
|
}
|
|
386
524
|
assertUnexposed(worker, config)
|
|
387
525
|
assertFrameworkVariables(worker, config.vars, bounds.frameworkVariables.application)
|
|
526
|
+
if (bounds.application === null) continue
|
|
527
|
+
assertApplicationRuntime(worker, config, bounds.application)
|
|
528
|
+
if (bounds.application.entry !== null) assertGeneratedEntry(worker, generated, bounds.cwd, bounds.application.entry)
|
|
388
529
|
}
|
|
389
530
|
}
|
|
390
531
|
|
package/src/cli.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
#!/usr/bin/env bun
|
|
2
2
|
|
|
3
3
|
import type { DeployStepResult } from '@fabrika/provider-contract'
|
|
4
|
+
import type { AppCli } from '@fabrika/provider-contract/app-cli'
|
|
4
5
|
import { parseCloudflareArgs } from './cli-args'
|
|
5
6
|
import { deployCloudflareConfig } from './command'
|
|
6
7
|
import type { CloudflareDeployResult } from './provider'
|
|
@@ -76,6 +77,19 @@ export const runCloudflareCli = async (argv: readonly string[]): Promise<void> =
|
|
|
76
77
|
)
|
|
77
78
|
}
|
|
78
79
|
|
|
80
|
+
/**
|
|
81
|
+
* What `fabrika app build` and `app deploy` run for an application on Cloudflare. A contract application's build is its
|
|
82
|
+
* generated Worker entry, which the `fabrika` command writes; there is nothing else to build.
|
|
83
|
+
*/
|
|
84
|
+
export const appCli: AppCli = {
|
|
85
|
+
provider: 'cloudflare',
|
|
86
|
+
entryRuntime: 'cloudflare',
|
|
87
|
+
build: async (_argv, { contract }) => {
|
|
88
|
+
if (!contract) throw new Error('Cloudflare app tooling does not support `app build` for an application without fabrika.contract.ts')
|
|
89
|
+
},
|
|
90
|
+
deploy: (argv) => runCloudflareCli(['deploy', ...argv]),
|
|
91
|
+
}
|
|
92
|
+
|
|
79
93
|
if (import.meta.main) {
|
|
80
94
|
await runCloudflareCli(process.argv.slice(2)).catch((error: unknown) => {
|
|
81
95
|
console.error(`fabrika-cloudflare-executor: ${error instanceof Error ? error.message : 'unknown error'}`)
|
package/src/command.ts
CHANGED
|
@@ -1,13 +1,16 @@
|
|
|
1
|
-
import { type AppEnvironment, parseAppEnvironmentId } from '@fabrika/auth-core'
|
|
1
|
+
import { type AppEnvironment, type ApplicationObjects, parseAppEnvironmentId, parseManagedEnvironment } from '@fabrika/auth-core'
|
|
2
2
|
import { readFile } from 'node:fs/promises'
|
|
3
3
|
import { z } from 'zod'
|
|
4
4
|
import type { CloudflareAppConfig } from './authoring'
|
|
5
5
|
import { loadCloudflareConfig } from './collaborators'
|
|
6
6
|
import { type CloudflareDeployResult, createCloudflareDeployer, type PlatformIamTarget } from './provider'
|
|
7
7
|
import {
|
|
8
|
+
APPLICATION_OBJECTS_ENV,
|
|
9
|
+
applicationObjectsSchema,
|
|
8
10
|
APPROVED_ACCESS_FILE_ENV,
|
|
9
11
|
CROSS_ORIGINS_ENV,
|
|
10
12
|
INSTALLATION_APPS_ENV,
|
|
13
|
+
OPERATIONS_ORIGIN_ENV,
|
|
11
14
|
type RunnerApprovedAccess,
|
|
12
15
|
runnerApprovedAccessSchema,
|
|
13
16
|
} from './runner-protocol'
|
|
@@ -42,8 +45,8 @@ const requiredValues = (names: readonly string[] | undefined): Record<string, st
|
|
|
42
45
|
}
|
|
43
46
|
|
|
44
47
|
/**
|
|
45
|
-
* Decode `FABRIKA_IAM_RETURN_ORIGINS`, the comma-separated set the
|
|
46
|
-
* deploy
|
|
48
|
+
* Decode `FABRIKA_IAM_RETURN_ORIGINS`, the comma-separated set the installation's workflow hands the
|
|
49
|
+
* platform's own deploy (ADR-0022). Undefined for an absent or empty value, so a deploy with nothing to
|
|
47
50
|
* project leaves IAM's registry alone rather than clearing it. Part of the executor's environment
|
|
48
51
|
* contract, exported for the test that pins it; not part of the package's public surface.
|
|
49
52
|
*/
|
|
@@ -72,6 +75,23 @@ export const parseCrossOrigins = (raw: string | undefined): readonly string[] |
|
|
|
72
75
|
return origins.data.length === 0 ? undefined : origins.data
|
|
73
76
|
}
|
|
74
77
|
|
|
78
|
+
/**
|
|
79
|
+
* Decode `FABRIKA_RUNNER_APPLICATION_OBJECTS`, the pointer Control recorded, as the runner writes it. Undefined when
|
|
80
|
+
* absent, and the deployer writes its defaults; anything malformed fails the deploy rather than move ordinary traffic.
|
|
81
|
+
*/
|
|
82
|
+
export const parseApplicationObjectsEnvironment = (raw: string | undefined): ApplicationObjects | undefined => {
|
|
83
|
+
if (raw === undefined || raw === '') return undefined
|
|
84
|
+
let decoded: unknown
|
|
85
|
+
try {
|
|
86
|
+
decoded = JSON.parse(raw)
|
|
87
|
+
} catch {
|
|
88
|
+
throw new Error(`${APPLICATION_OBJECTS_ENV} is not JSON`)
|
|
89
|
+
}
|
|
90
|
+
const objects = applicationObjectsSchema.safeParse(decoded)
|
|
91
|
+
if (!objects.success) throw new Error(`${APPLICATION_OBJECTS_ENV} is not a serving object and its object list`)
|
|
92
|
+
return objects.data
|
|
93
|
+
}
|
|
94
|
+
|
|
75
95
|
/**
|
|
76
96
|
* Decode `FABRIKA_INSTALLATION_APPS`, the Cloudflare deployments Control registered, as the runner writes it.
|
|
77
97
|
* Undefined when absent; anything malformed fails the deploy rather than dropping the refusal it feeds.
|
|
@@ -155,20 +175,25 @@ export const deployCloudflareConfig = async (options: CloudflareCommandDeployOpt
|
|
|
155
175
|
const returnOrigins = parseReturnOrigins(environmentValue('FABRIKA_IAM_RETURN_ORIGINS'))
|
|
156
176
|
const installationApps = parseInstallationApps(environmentValue(INSTALLATION_APPS_ENV))
|
|
157
177
|
const crossOrigins = parseCrossOrigins(environmentValue(CROSS_ORIGINS_ENV))
|
|
178
|
+
const operationsOrigin = environmentValue(OPERATIONS_ORIGIN_ENV)
|
|
179
|
+
const applicationObjects = parseApplicationObjectsEnvironment(environmentValue(APPLICATION_OBJECTS_ENV))
|
|
158
180
|
return deploy({
|
|
159
181
|
appId: loaded.config.id,
|
|
160
182
|
env: options.env,
|
|
161
183
|
...(domain === undefined ? {} : { domain }),
|
|
162
184
|
...(crossOrigins === undefined ? {} : { crossOrigins }),
|
|
185
|
+
...(operationsOrigin === undefined ? {} : { operationsOrigin }),
|
|
163
186
|
...(returnOrigins === undefined ? {} : { returnOrigins }),
|
|
164
187
|
...(installationApps === undefined ? {} : { installationApps }),
|
|
188
|
+
...(applicationObjects === undefined ? {} : { applicationObjects }),
|
|
165
189
|
...approved,
|
|
166
190
|
cwd: loaded.cwd,
|
|
167
191
|
configPath: loaded.absolutePath,
|
|
168
192
|
...(options.stateNamespace === undefined ? {} : { stateNamespace: options.stateNamespace }),
|
|
169
193
|
secrets: declaredValues(loaded.config.pipeline?.secrets),
|
|
170
194
|
vars: declaredValues(loaded.config.pipeline?.vars),
|
|
171
|
-
|
|
195
|
+
// The runner hands what Control handed its job; parsed here, where it enters the executor.
|
|
196
|
+
managedEnvironment: parseManagedEnvironment(requiredValues(options.managedVarNames)),
|
|
172
197
|
dryRun: options.dryRun ?? false,
|
|
173
198
|
signal: options.signal ?? new AbortController().signal,
|
|
174
199
|
log: options.log ?? ((line) => console.log(line)),
|