@fabrika/provider-cloudflare 0.2.0 → 0.3.2

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fabrika/provider-cloudflare",
3
- "version": "0.2.0",
3
+ "version": "0.3.2",
4
4
  "type": "module",
5
5
  "repository": {
6
6
  "type": "git",
@@ -29,6 +29,11 @@
29
29
  "bun": "./src/control.ts",
30
30
  "types": "./src/control.ts",
31
31
  "default": "./src/control.ts"
32
+ },
33
+ "./installation": {
34
+ "bun": "./src/installation/index.ts",
35
+ "types": "./src/installation/index.ts",
36
+ "default": "./src/installation/index.ts"
32
37
  }
33
38
  },
34
39
  "scripts": {
@@ -36,13 +41,12 @@
36
41
  "test": "bun test"
37
42
  },
38
43
  "dependencies": {
39
- "@fabrika/auth": "^0.2.0",
40
- "@fabrika/auth-core": "^0.2.0",
41
- "@fabrika/engine": "^0.2.0",
42
- "@fabrika/proxy-core": "^0.2.0",
43
- "@fabrika/proxy-contract": "^0.2.0",
44
- "@fabrika/provider-contract": "^0.2.0",
45
- "@fabrika/runner-contract": "^0.2.0",
44
+ "zod": "^3.25.76",
45
+ "@fabrika/auth": "^0.3.2",
46
+ "@fabrika/auth-core": "^0.3.2",
47
+ "@fabrika/installation-init": "^0.3.2",
48
+ "@fabrika/proxy-core": "^0.3.2",
49
+ "@fabrika/provider-contract": "^0.3.2",
46
50
  "oblaka-iac": "^0.0.18"
47
51
  }
48
52
  }
package/src/cli.ts CHANGED
@@ -1,8 +1,9 @@
1
1
  #!/usr/bin/env bun
2
2
 
3
- import type { ProviderDeployResult } from '@fabrika/provider-contract'
3
+ import type { DeployStepResult } from '@fabrika/provider-contract'
4
4
  import { parseCloudflareArgs } from './cli-args'
5
5
  import { deployCloudflareConfig } from './command'
6
+ import type { CloudflareDeployResult } from './provider'
6
7
 
7
8
  const USAGE = `Internal Cloudflare app executor
8
9
 
@@ -23,32 +24,30 @@ const die = (message: string): never => {
23
24
  throw new Error(message)
24
25
  }
25
26
 
26
- const formatDuration = (step: ProviderDeployResult['steps'][number]): string => {
27
+ const formatDuration = (step: DeployStepResult): string => {
27
28
  if (step.startedAt === undefined || step.finishedAt === undefined) {
28
29
  return ''
29
30
  }
30
31
  return ` (${step.finishedAt - step.startedAt}ms)`
31
32
  }
32
33
 
33
- const ICON: Readonly<Record<string, string>> = {
34
- pending: '·',
35
- running: '…',
34
+ const ICON: Readonly<Record<DeployStepResult['status'], string>> = {
36
35
  succeeded: '✓',
37
36
  failed: '✗',
38
37
  skipped: '∅',
39
38
  }
40
39
 
41
- const printResult = (result: ProviderDeployResult): void => {
40
+ const printResult = (result: CloudflareDeployResult): void => {
42
41
  console.log(`\n${result.appId} → ${result.env}: ${result.status}`)
43
42
  for (const step of result.steps) {
44
- console.log(` ${ICON[step.status] ?? '?'} ${step.spec.id} — ${step.status}${formatDuration(step)}`)
43
+ console.log(` ${ICON[step.status]} ${step.id} — ${step.status}${formatDuration(step)}`)
45
44
  if (step.error !== undefined) {
46
45
  console.log(` ${step.error}`)
47
46
  }
48
47
  }
49
48
  }
50
49
 
51
- const requireSuccessful = (result: ProviderDeployResult): void => {
50
+ const requireSuccessful = (result: CloudflareDeployResult): void => {
52
51
  printResult(result)
53
52
  if (result.status === 'failed') {
54
53
  throw new Error('Cloudflare deployment failed')
package/src/codec.ts CHANGED
@@ -1,70 +1,17 @@
1
- import type { JsonValue, ProviderCodec } from '@fabrika/provider-contract'
2
-
3
- /** Ephemeral credentials and handles used by one Cloudflare deploy. IAM's key is never among them (ADR-0022). */
4
- export interface CloudflareTarget {
5
- readonly accountId: string
6
- readonly apiToken: string
7
- readonly stateNamespace?: string
8
- }
1
+ import type { ProviderCodec } from '@fabrika/provider-contract'
2
+ import { z } from 'zod'
9
3
 
10
4
  /** A checkout recipe. The Oblaka graph stays executable code in the referenced config module. */
11
5
  export interface CloudflareArtifact {
12
6
  readonly configPath: string
13
7
  }
14
8
 
15
- const objectProperty = (value: JsonValue, property: string): JsonValue => {
16
- if (typeof value !== 'object' || value === null || Array.isArray(value)) {
17
- throw new Error('expected an object')
18
- }
19
- const propertyValue = value[property]
20
- if (propertyValue === undefined) {
21
- throw new Error(`missing ${property}`)
22
- }
23
- return propertyValue
24
- }
25
-
26
- const stringProperty = (value: JsonValue, property: string): string => {
27
- const propertyValue = objectProperty(value, property)
28
- if (typeof propertyValue !== 'string' || propertyValue === '') {
29
- throw new Error(`${property} must be a non-empty string`)
30
- }
31
- return propertyValue
32
- }
33
-
34
- const optionalStringProperty = (value: JsonValue, property: string): string | undefined => {
35
- if (typeof value !== 'object' || value === null || Array.isArray(value)) {
36
- throw new Error('expected an object')
37
- }
38
- const propertyValue = value[property]
39
- if (propertyValue === undefined) {
40
- return undefined
41
- }
42
- if (typeof propertyValue !== 'string' || propertyValue === '') {
43
- throw new Error(`${property} must be a non-empty string`)
44
- }
45
- return propertyValue
46
- }
47
-
48
- export const cloudflareTargetCodec: ProviderCodec<CloudflareTarget> = {
49
- version: 1,
50
- encode: (target) => ({
51
- accountId: target.accountId,
52
- apiToken: target.apiToken,
53
- ...(target.stateNamespace === undefined ? {} : { stateNamespace: target.stateNamespace }),
54
- }),
55
- decode: (payload) => ({
56
- accountId: stringProperty(payload, 'accountId'),
57
- apiToken: stringProperty(payload, 'apiToken'),
58
- ...(optionalStringProperty(payload, 'stateNamespace') === undefined
59
- ? {}
60
- : { stateNamespace: optionalStringProperty(payload, 'stateNamespace') }),
61
- }),
62
- }
9
+ const artifactSchema = z.object({ configPath: z.string().min(1) })
63
10
 
64
11
  export const cloudflareArtifactCodec: ProviderCodec<CloudflareArtifact> = {
65
12
  version: 1,
66
13
  encode: (artifact) => ({ configPath: artifact.configPath }),
67
- decode: (payload) => ({ configPath: stringProperty(payload, 'configPath') }),
14
+ decode: (payload) => artifactSchema.parse(payload),
68
15
  }
69
16
 
70
17
  /** Build the default checkout recipe used by the Cloudflare runner. */
@@ -1,9 +1,9 @@
1
1
  import type { SchemaReconciler } from '@fabrika/provider-contract'
2
- import { RUNNER_OUTCOME_FILE_ENV, type RunnerOutcomeFile } from '@fabrika/runner-contract'
3
2
  import { writeFile } from 'node:fs/promises'
4
3
  import { resolve } from 'node:path'
5
4
  import { type Definition, deploy as oblakaDeploy, type DeployResult as OblakaDeployResult } from 'oblaka-iac'
6
5
  import { type CloudflareAppConfig, isCloudflareAppConfig } from './authoring'
6
+ import { RUNNER_OUTCOME_FILE_ENV, type RunnerOutcomeFile } from './runner-protocol'
7
7
 
8
8
  export interface CommandSpec {
9
9
  readonly command: string
@@ -53,13 +53,18 @@ export interface CloudflareCollaborators {
53
53
  readonly writeOutcome: OutcomeWriter
54
54
  }
55
55
 
56
- const defaultLoadConfig: CloudflareConfigLoader = async (cwd, configPath) => {
57
- const absolute = resolve(cwd, configPath)
58
- const loaded: unknown = await import(absolute)
56
+ export const loadCloudflareConfig = async (cwd: string, configPath: string): Promise<LoadedCloudflareConfig & { readonly absolutePath: string }> => {
57
+ const absolutePath = resolve(cwd, configPath)
58
+ const loaded: unknown = await import(absolutePath)
59
59
  if (typeof loaded !== 'object' || loaded === null || !('default' in loaded) || !isCloudflareAppConfig(loaded.default)) {
60
- throw new Error(`Cloudflare config at ${absolute} must export a valid default defineApp(...) value`)
60
+ throw new Error(`Cloudflare config at ${absolutePath} must export a valid default defineApp(...) value`)
61
61
  }
62
- return { config: loaded.default, cwd: resolve(absolute, '..') }
62
+ return { config: loaded.default, absolutePath, cwd: resolve(absolutePath, '..') }
63
+ }
64
+
65
+ const defaultLoadConfig: CloudflareConfigLoader = async (cwd, configPath) => {
66
+ const loaded = await loadCloudflareConfig(cwd, configPath)
67
+ return { config: loaded.config, cwd: loaded.cwd }
63
68
  }
64
69
 
65
70
  const childEnvironment = (spec: CommandSpec): Record<string, string | undefined> => {
package/src/command.ts CHANGED
@@ -1,10 +1,6 @@
1
- import { deploy } from '@fabrika/engine'
2
- import type { ProviderDeployResult, ProviderRunEvents, RuntimeProviderRun } from '@fabrika/provider-contract'
3
- import { resolve } from 'node:path'
4
- import { type CloudflareAppConfig, isCloudflareAppConfig } from './authoring'
5
- import { cloudflareArtifact } from './codec'
6
- import { defaultCloudflareCollaborators } from './collaborators'
7
- import { createCloudflareProvider } from './provider'
1
+ import type { CloudflareAppConfig } from './authoring'
2
+ import { loadCloudflareConfig } from './collaborators'
3
+ import { type CloudflareDeployResult, createCloudflareDeployer } from './provider'
8
4
 
9
5
  const requiredEnvironmentValue = (name: string): string => {
10
6
  const value = environmentValue(name)
@@ -37,7 +33,7 @@ const requiredValues = (names: readonly string[] | undefined): Record<string, st
37
33
 
38
34
  /**
39
35
  * Decode `FABRIKA_IAM_RETURN_ORIGINS`, the comma-separated set the control plane projects into the
40
- * deploy environment (ADR-0021). Undefined for an absent or empty value, so a deploy with nothing to
36
+ * deploy environment (ADR-0022). Undefined for an absent or empty value, so a deploy with nothing to
41
37
  * project leaves IAM's registry alone rather than clearing it. Part of the executor's environment
42
38
  * contract, exported for the test that pins it; not part of the package's public surface.
43
39
  */
@@ -52,14 +48,8 @@ export interface LoadedCloudflareCommandConfig {
52
48
  readonly cwd: string
53
49
  }
54
50
 
55
- export const loadCloudflareCommandConfig = async (configPath: string, cwd: string): Promise<LoadedCloudflareCommandConfig> => {
56
- const absolutePath = resolve(cwd, configPath)
57
- const loaded: unknown = await import(absolutePath)
58
- if (typeof loaded !== 'object' || loaded === null || !('default' in loaded) || !isCloudflareAppConfig(loaded.default)) {
59
- throw new Error(`Cloudflare config at ${absolutePath} must export a valid default defineApp(...) value`)
60
- }
61
- return { config: loaded.default, absolutePath, cwd: resolve(absolutePath, '..') }
62
- }
51
+ export const loadCloudflareCommandConfig = async (configPath: string, cwd: string): Promise<LoadedCloudflareCommandConfig> =>
52
+ loadCloudflareConfig(cwd, configPath)
63
53
 
64
54
  export interface CloudflareCommandDeployOptions {
65
55
  readonly env: string
@@ -74,38 +64,30 @@ export interface CloudflareCommandDeployOptions {
74
64
  readonly log?: (line: string) => void
75
65
  }
76
66
 
77
- /** Build an opaque provider run from one config and execute it through the neutral engine. */
78
- export const deployCloudflareConfig = async (options: CloudflareCommandDeployOptions): Promise<ProviderDeployResult> => {
67
+ /** Deploy one config with the account credentials, declared secrets and vars read from the environment. */
68
+ export const deployCloudflareConfig = async (options: CloudflareCommandDeployOptions): Promise<CloudflareDeployResult> => {
79
69
  const cwd = options.cwd ?? process.cwd()
80
70
  const loaded = await loadCloudflareCommandConfig(options.configPath ?? './fabrika.config.ts', cwd)
81
- const events: ProviderRunEvents = {
82
- log: options.log ?? ((line) => console.log(line)),
83
- externalId: async () => {},
84
- checkpoint: async () => {},
85
- }
86
- const provider = createCloudflareProvider(defaultCloudflareCollaborators, {
71
+ const deploy = createCloudflareDeployer({
72
+ accountId: requiredEnvironmentValue('CLOUDFLARE_ACCOUNT_ID'),
73
+ apiToken: requiredEnvironmentValue('CLOUDFLARE_API_TOKEN'),
87
74
  platformResources: options.platformResources ?? 'refuse',
88
75
  })
89
76
  const domain = environmentValue('FABRIKA_CONTROL_DOMAIN')
90
77
  const returnOrigins = parseReturnOrigins(environmentValue('FABRIKA_IAM_RETURN_ORIGINS'))
91
- const run: RuntimeProviderRun = {
78
+ return deploy({
92
79
  appId: loaded.config.id,
93
80
  env: options.env,
94
81
  ...(domain === undefined ? {} : { domain }),
95
82
  ...(returnOrigins === undefined ? {} : { returnOrigins }),
96
83
  cwd: loaded.cwd,
84
+ configPath: loaded.absolutePath,
85
+ ...(options.stateNamespace === undefined ? {} : { stateNamespace: options.stateNamespace }),
97
86
  secrets: declaredValues(loaded.config.pipeline?.secrets),
98
87
  vars: declaredValues(loaded.config.pipeline?.vars),
99
88
  managedEnvironment: requiredValues(options.managedVarNames),
100
89
  dryRun: options.dryRun ?? false,
101
90
  signal: options.signal ?? new AbortController().signal,
102
- events,
103
- target: provider.encodeTarget({
104
- accountId: requiredEnvironmentValue('CLOUDFLARE_ACCOUNT_ID'),
105
- apiToken: requiredEnvironmentValue('CLOUDFLARE_API_TOKEN'),
106
- ...(options.stateNamespace === undefined ? {} : { stateNamespace: options.stateNamespace }),
107
- }),
108
- artifact: provider.encodeArtifact(cloudflareArtifact(loaded.absolutePath)),
109
- }
110
- return deploy(provider.runtime, run)
91
+ log: options.log ?? ((line) => console.log(line)),
92
+ })
111
93
  }