@zabaca/zbc 0.12.0 → 0.14.0

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.
Files changed (111) hide show
  1. package/CHANGELOG.md +174 -0
  2. package/package.json +6 -2
  3. package/src/commands/add.ts +13 -0
  4. package/src/commands/init.ts +1 -0
  5. package/src/engine/apply.ts +60 -21
  6. package/src/engine/destroy.ts +183 -14
  7. package/src/engine/discover.ts +1 -1
  8. package/src/engine/resolve.ts +56 -11
  9. package/src/utils/copy-template.ts +10 -0
  10. package/templates/apps/inbox/package.json +4 -2
  11. package/templates/apps/inbox/tsconfig.json +3 -1
  12. package/templates/apps/inbox/worker/api.ts +197 -0
  13. package/templates/apps/inbox/worker/commands.ts +299 -0
  14. package/templates/apps/inbox/worker/inbox-do.ts +53 -10
  15. package/templates/apps/inbox/worker/index.ts +15 -202
  16. package/templates/apps/inbox/worker/mcp.ts +49 -100
  17. package/templates/apps/walgit/CONTEXT.md +135 -0
  18. package/templates/apps/walgit/Dockerfile +40 -18
  19. package/templates/apps/walgit/README.md +518 -55
  20. package/templates/apps/walgit/e2e/README.md +100 -6
  21. package/templates/apps/walgit/e2e/harness.ts +262 -1
  22. package/templates/apps/walgit/e2e/live.ts +588 -0
  23. package/templates/apps/walgit/e2e/scenarios.ts +367 -5
  24. package/templates/apps/walgit/e2e/suite.ts +5 -5
  25. package/templates/apps/walgit/examples/watch.ts +209 -0
  26. package/templates/apps/walgit/package.json +12 -3
  27. package/templates/apps/walgit/registry.json +6 -11
  28. package/templates/apps/walgit/shared/capabilities.ts +189 -0
  29. package/templates/apps/walgit/shared/container-env.ts +114 -0
  30. package/templates/apps/walgit/shared/credentials.ts +81 -0
  31. package/templates/apps/walgit/shared/events.ts +277 -0
  32. package/templates/apps/walgit/shared/landing.ts +1241 -0
  33. package/templates/apps/walgit/shared/llms.ts +463 -0
  34. package/templates/apps/walgit/shared/outbox.ts +266 -0
  35. package/templates/apps/walgit/shared/policy.ts +59 -0
  36. package/templates/apps/walgit/shared/protocol.ts +197 -0
  37. package/templates/apps/walgit/shared/provenance.ts +154 -0
  38. package/templates/apps/walgit/shared/ref-cache.ts +181 -0
  39. package/templates/apps/walgit/shared/telemetry.ts +219 -0
  40. package/templates/apps/walgit/src/announce-main.ts +37 -0
  41. package/templates/apps/walgit/src/announce.ts +98 -0
  42. package/templates/apps/walgit/src/append-only.ts +112 -0
  43. package/templates/apps/walgit/src/cache.ts +172 -0
  44. package/templates/apps/walgit/src/cli.ts +493 -0
  45. package/templates/apps/walgit/src/compact.ts +25 -23
  46. package/templates/apps/walgit/src/delete-repo.ts +237 -0
  47. package/templates/apps/walgit/src/expire.ts +248 -0
  48. package/templates/apps/walgit/src/gc.ts +8 -16
  49. package/templates/apps/walgit/src/git-backend.ts +29 -0
  50. package/templates/apps/walgit/src/git.ts +78 -0
  51. package/templates/apps/walgit/src/hook-main.ts +259 -17
  52. package/templates/apps/walgit/src/hooks.ts +4 -3
  53. package/templates/apps/walgit/src/http.ts +237 -45
  54. package/templates/apps/walgit/src/instructions.ts +240 -0
  55. package/templates/apps/walgit/src/keys.ts +126 -0
  56. package/templates/apps/walgit/src/limits.ts +158 -0
  57. package/templates/apps/walgit/src/materialize.bench.ts +10 -10
  58. package/templates/apps/walgit/src/materialize.ts +48 -40
  59. package/templates/apps/walgit/src/mkdir-lock.ts +66 -0
  60. package/templates/apps/walgit/src/orphans.ts +2 -5
  61. package/templates/apps/walgit/src/pending.ts +201 -0
  62. package/templates/apps/walgit/src/push-cert.ts +254 -0
  63. package/templates/apps/walgit/src/push.ts +233 -46
  64. package/templates/apps/walgit/src/reconcile.ts +5 -8
  65. package/templates/apps/walgit/src/repo.ts +22 -77
  66. package/templates/apps/walgit/src/server.ts +148 -26
  67. package/templates/apps/walgit/src/signers.ts +577 -0
  68. package/templates/apps/walgit/src/store-env.ts +5 -4
  69. package/templates/apps/walgit/src/store.ts +47 -24
  70. package/templates/apps/walgit/src/usage.ts +404 -0
  71. package/templates/apps/walgit/src/verify.ts +171 -0
  72. package/templates/apps/walgit/src/wal-index.ts +189 -11
  73. package/templates/apps/walgit/tsconfig.json +8 -2
  74. package/templates/apps/walgit/tsconfig.worker.json +24 -0
  75. package/templates/apps/walgit/worker/events-do.ts +259 -0
  76. package/templates/apps/walgit/worker/index.ts +572 -0
  77. package/templates/apps/walgit/wrangler.jsonc +95 -0
  78. package/templates/apps/warehouse/package.json +1 -1
  79. package/templates/infra/modules/cloudflare/index.ts +93 -67
  80. package/templates/infra/modules/cloudflare-access/index.ts +3 -3
  81. package/templates/infra/modules/cloudflare-api/index.ts +144 -32
  82. package/templates/infra/modules/cloudflare-api/registry.json +1 -1
  83. package/templates/infra/modules/cloudflare-email/index.ts +103 -144
  84. package/templates/infra/modules/cloudflare-email/registry.json +1 -1
  85. package/templates/infra/modules/cloudflare-token/index.ts +10 -30
  86. package/templates/infra/modules/cloudflare-token/registry.json +1 -1
  87. package/templates/infra/modules/cloudflare-tunnel/index.ts +3 -3
  88. package/templates/infra/modules/cloudflare-zone/index.ts +2 -2
  89. package/templates/infra/modules/fly/index.ts +158 -35
  90. package/templates/infra/modules/host-file/index.ts +6 -7
  91. package/templates/infra/modules/provision-core/index.ts +21 -15
  92. package/templates/infra/modules/r2/index.ts +38 -55
  93. package/templates/infra/modules/r2/registry.json +1 -1
  94. package/templates/infra/modules/remote-provision/index.ts +1 -3
  95. package/templates/infra/modules/turso/index.ts +45 -7
  96. package/templates/infra/modules/vm-provision/index.ts +10 -3
  97. package/templates/infra/package.json +1 -1
  98. package/templates/infra/src/context.ts +115 -0
  99. package/templates/infra/src/define-module.ts +8 -2
  100. package/templates/infra/src/index.ts +11 -1
  101. package/templates/infra/src/types.ts +72 -3
  102. package/src/commands/add-registry.test.ts +0 -92
  103. package/src/commands/secret.test.ts +0 -717
  104. package/src/commands/vendor-mode.test.ts +0 -183
  105. package/src/infra-types.ts +0 -38
  106. package/src/utils/subtree.test.ts +0 -127
  107. package/templates/apps/inbox/worker/send-op.ts +0 -174
  108. package/templates/apps/walgit/entrypoint.sh +0 -59
  109. package/templates/apps/walgit/fly.toml +0 -52
  110. package/templates/apps/walgit/src/ssh-shell.ts +0 -36
  111. package/templates/apps/walgit/sshd_config +0 -38
@@ -0,0 +1,115 @@
1
+ // The two questions every module used to answer for itself.
2
+ //
3
+ // "What is this secret" was written seventeen times, in two spellings that
4
+ // disagree about what absent means (`!value`, which calls an empty string
5
+ // missing, and `=== undefined`, which does not). "What did the instance I
6
+ // imported emit" was written six times, and the copies differed in whether a
7
+ // non-string counted, whether an empty string counted, and which of the three
8
+ // distinct failures they could tell apart.
9
+ //
10
+ // Both live here now, and `ApplyContext` is how a module reaches them. The
11
+ // wording of the three `output` failures is `cloudflare-api`'s `resolveRef`
12
+ // verbatim: a missing import and a missing output are both silently
13
+ // `undefined` otherwise, and an apply that carries `Bearer undefined` fails
14
+ // much later, at a Cloudflare 403 that reads like a permissions problem.
15
+ //
16
+ // Runs under the CLI and under a consumer's own bun. Pure — no I/O, no
17
+ // runtime-specific globals — which is what lets it be tested as a value.
18
+
19
+ import type {
20
+ ApplyContext,
21
+ ApplyContextInput,
22
+ OutputOptions,
23
+ OutputRef,
24
+ SecretOptions,
25
+ } from './types'
26
+
27
+ /** `ctx.secret`, over a plain secrets record. */
28
+ export function resolveSecret(
29
+ secrets: Record<string, string>,
30
+ key: string,
31
+ opts: SecretOptions = {},
32
+ ): string {
33
+ const raw = secrets[key]
34
+ const subject =
35
+ opts.field === undefined ? `Secret "${key}"` : `${opts.field} needs secret "${key}", which`
36
+ if (raw === undefined) {
37
+ throw new Error(`${subject} is missing from this environment's secrets.yaml`)
38
+ }
39
+ // `KEY:` with nothing after it parses to null, and that is the most natural
40
+ // spelling of the intentionally blank placeholder `allowBlank` exists for —
41
+ // so it means what `KEY: ""` means rather than a different failure. The
42
+ // coercion also keeps the return type honest: without it, a module that asked
43
+ // for a string gets `null` and finds out somewhere much further downstream.
44
+ const value = raw === null ? '' : raw
45
+ if (value === '' && !opts.allowBlank) {
46
+ throw new Error(`${subject} is empty in this environment's secrets.yaml`)
47
+ }
48
+ return value
49
+ }
50
+
51
+ /**
52
+ * `ctx.output`, over a plain imports record.
53
+ *
54
+ * The three messages are the whole value of this function: `field` says which
55
+ * line of the instance file to look at, and the three cases are told apart
56
+ * because they have three different fixes.
57
+ *
58
+ * `ref` is typed loosely on purpose. A zod-inferred config marks both halves
59
+ * optional in some spellings, and the checks have to run at runtime regardless
60
+ * — a `from` naming an instance that is not imported is a real failure mode and
61
+ * nothing in the type system is catching it here.
62
+ */
63
+ export function resolveOutput(
64
+ ref: OutputRef,
65
+ imports: Record<string, unknown>,
66
+ field: string,
67
+ opts: OutputOptions = {},
68
+ ): string {
69
+ if (!ref.from || !ref.output) {
70
+ throw new Error(`${field} must name both an instance (\`from\`) and an output (\`output\`)`)
71
+ }
72
+ const outputs = imports[ref.from]
73
+ if (outputs === undefined) {
74
+ throw new Error(
75
+ `${field} references instance "${ref.from}", which is not in this instance's imports`,
76
+ )
77
+ }
78
+ const value = (outputs as Record<string, unknown> | null)?.[ref.output]
79
+ if (typeof value !== 'string' || (value === '' && !opts.allowBlank)) {
80
+ throw new Error(
81
+ `${field} references output "${ref.output}" on instance "${ref.from}", which doesn't emit it`,
82
+ )
83
+ }
84
+ return value
85
+ }
86
+
87
+ /** The context the engine hands a module: the three fields plus the two rules. */
88
+ export function createApplyContext(input: ApplyContextInput): ApplyContext {
89
+ return {
90
+ secrets: input.secrets,
91
+ imports: input.imports,
92
+ projectRoot: input.projectRoot,
93
+ secret(key, opts) {
94
+ return resolveSecret(input.secrets, key, opts)
95
+ },
96
+ output(ref, field, opts) {
97
+ return resolveOutput(ref, input.imports, field, opts)
98
+ },
99
+ }
100
+ }
101
+
102
+ /**
103
+ * Upgrade whatever a caller passed into a full context, leaving one that is
104
+ * already full — the engine's destroy context resolves imports on demand, and
105
+ * rebuilding it here would throw that away.
106
+ *
107
+ * `defineModule` calls this on every `apply`/`destroy`, so a module body may
108
+ * assume the methods exist no matter who invoked it.
109
+ */
110
+ export function ensureApplyContext(ctx: ApplyContextInput | ApplyContext): ApplyContext {
111
+ const candidate = ctx as ApplyContext
112
+ return typeof candidate.secret === 'function' && typeof candidate.output === 'function'
113
+ ? candidate
114
+ : createApplyContext(ctx)
115
+ }
@@ -1,4 +1,5 @@
1
1
  import type { z } from 'zod'
2
+ import { ensureApplyContext } from './context'
2
3
  import type { ApplyFn, DestroyFn, ModuleDefinition, ModuleInstance, InstanceOptions } from './types'
3
4
 
4
5
  interface DefineModuleOptions<TConfig extends z.ZodType, TOutputs extends z.ZodType> {
@@ -16,8 +17,13 @@ export function defineModule<TConfig extends z.ZodType, TOutputs extends z.ZodTy
16
17
  name: opts.name,
17
18
  configSchema: opts.configSchema,
18
19
  outputsSchema: opts.outputs,
19
- apply: opts.apply,
20
- destroy: opts.destroy,
20
+ // The context is normalized HERE rather than in the engine, so a module
21
+ // body may assume `ctx.secret`/`ctx.output` exist whoever called it — the
22
+ // engine, another module's test, a consumer's script. A caller that
23
+ // already holds a full context (the destroy path's on-demand one) is
24
+ // passed through untouched.
25
+ apply: (config, ctx) => opts.apply(config, ensureApplyContext(ctx)),
26
+ destroy: opts.destroy && ((config, ctx) => opts.destroy!(config, ensureApplyContext(ctx))),
21
27
  instance(instanceOpts: InstanceOptions<TConfig>): ModuleInstance<TOutputs> {
22
28
  return {
23
29
  name: instanceOpts.name,
@@ -1,3 +1,13 @@
1
1
  export { defineModule } from './define-module'
2
2
  export { defineConfig } from './config'
3
- export type { ModuleDefinition, ModuleInstance, ApplyContext, InstanceOptions } from './types'
3
+ export { createApplyContext, ensureApplyContext, resolveOutput, resolveSecret } from './context'
4
+ export type {
5
+ ModuleDefinition,
6
+ ModuleInstance,
7
+ ApplyContext,
8
+ ApplyContextInput,
9
+ OutputRef,
10
+ OutputOptions,
11
+ SecretOptions,
12
+ InstanceOptions,
13
+ } from './types'
@@ -4,18 +4,87 @@ export type ApplyFn<TConfig, TOutputs> = (config: TConfig, ctx: ApplyContext) =>
4
4
 
5
5
  export type DestroyFn<TConfig> = (config: TConfig, ctx: ApplyContext) => Promise<void>
6
6
 
7
- export interface ApplyContext {
7
+ /**
8
+ * The three raw fields a caller has to supply. `defineModule` turns one of
9
+ * these into a full `ApplyContext` before the module body sees it (see
10
+ * `context.ts`), which is why the engine — and a test — may hand `apply` a
11
+ * plain object and the module still gets `secret()` and `output()`.
12
+ */
13
+ export interface ApplyContextInput {
8
14
  secrets: Record<string, string>
9
15
  imports: Record<string, unknown>
10
16
  projectRoot: string
11
17
  }
12
18
 
19
+ /** A `{ from, output }` reference into an imported instance's outputs. */
20
+ export interface OutputRef {
21
+ from?: string
22
+ output?: string
23
+ }
24
+
25
+ export interface SecretOptions {
26
+ /**
27
+ * What wanted the secret, for the error message — e.g.
28
+ * `flySecrets entry "WALGIT_S3_ACCESS_KEY_ID"`. With an alias the env var
29
+ * name and the secrets.yaml key are different strings, and a message naming
30
+ * only one sends the reader to the wrong file.
31
+ */
32
+ field?: string
33
+ /**
34
+ * Presence is the contract rather than non-emptiness. An intentionally blank
35
+ * placeholder — `KEY:` with no value, or `KEY: ""` — lets a script no-op the
36
+ * step it gates until the real value is filled in.
37
+ */
38
+ allowBlank?: boolean
39
+ }
40
+
41
+ export interface OutputOptions {
42
+ /** Accept an emitted empty string — for outputs where "nothing to do" is a real answer. */
43
+ allowBlank?: boolean
44
+ }
45
+
46
+ /**
47
+ * What a module's `apply`/`destroy` receives.
48
+ *
49
+ * The two methods are the engine's answer to the same two questions every
50
+ * module used to answer itself: "what is this secret" and "what did the
51
+ * instance I imported emit". Six copies of the second and seventeen of the
52
+ * first is how they failed differently for the same cause.
53
+ */
54
+ export interface ApplyContext extends ApplyContextInput {
55
+ /**
56
+ * A value from this environment's secrets.yaml. Throws naming the key (and
57
+ * `field`, when given) if the key is absent or blank; `allowBlank` accepts a
58
+ * key that is present but intentionally empty.
59
+ */
60
+ secret(key: string, opts?: SecretOptions): string
61
+ /**
62
+ * An imported instance's output. Throws naming `field` when the ref is
63
+ * incomplete, when the instance is not among this instance's imports, or
64
+ * when it does not emit that output. `allowBlank` accepts an emitted empty
65
+ * string — "nothing to do" is a real answer for some outputs.
66
+ */
67
+ output(ref: OutputRef, field: string, opts?: OutputOptions): string
68
+ }
69
+
70
+ /**
71
+ * The shape `defineModule` publishes: it accepts the raw three fields, because
72
+ * it normalizes them itself. A caller holding a full `ApplyContext` may pass
73
+ * that instead — it is already one of these.
74
+ */
75
+ export type BoundApplyFn<TConfig, TOutputs> = (
76
+ config: TConfig,
77
+ ctx: ApplyContextInput,
78
+ ) => Promise<TOutputs>
79
+
80
+ export type BoundDestroyFn<TConfig> = (config: TConfig, ctx: ApplyContextInput) => Promise<void>
81
+
13
82
  export interface ModuleDefinition<TConfig extends z.ZodType, TOutputs extends z.ZodType> {
14
83
  name: string
15
84
  configSchema: TConfig
16
85
  outputsSchema: TOutputs
17
- apply: ApplyFn<z.infer<TConfig>, z.infer<TOutputs>>
18
- destroy?: DestroyFn<z.infer<TConfig>>
86
+ apply: BoundApplyFn<z.infer<TConfig>, z.infer<TOutputs>>
87
+ destroy?: BoundDestroyFn<z.infer<TConfig>>
19
88
  instance: (opts: InstanceOptions<TConfig>) => ModuleInstance<TOutputs>
20
89
  }
21
90
 
@@ -1,92 +0,0 @@
1
- import { afterEach, expect, test } from 'bun:test'
2
- import { spawnSync } from 'node:child_process'
3
- import * as fs from 'node:fs'
4
- import * as os from 'node:os'
5
- import * as path from 'node:path'
6
-
7
- /**
8
- * `zbc add <unknown>` tells the caller what it could have asked for, and that
9
- * sentence is the only place the built-in registry is enumerated for a human.
10
- *
11
- * It was a hand-written literal, and it went stale the first time modules were
12
- * added to `templates/infra/modules/` — the four host primitives contributed
13
- * contributed later resolved perfectly well and were named nowhere, so
14
- * the only way to discover them was to already know they existed. Every other
15
- * "Available:" in this CLI computes its list (`apply.ts`, `destroy.ts`,
16
- * `engine/resolve.ts` all join a live array); this one is now the same shape,
17
- * and this file is what stops it drifting back.
18
- *
19
- * The census is walked here with `readdirSync`, where the implementation globs
20
- * through `Bun.file(...).exists()`. Two different mechanisms on purpose: a test
21
- * that recomputed the implementation would agree with it while both were wrong.
22
- */
23
-
24
- const CLI = path.resolve(import.meta.dir, '../index.ts')
25
- const MODULES = path.resolve(import.meta.dir, '../../templates/infra/modules')
26
- const APPS = path.resolve(import.meta.dir, '../../templates/apps')
27
-
28
- const roots: string[] = []
29
- afterEach(() => {
30
- for (const r of roots.splice(0)) fs.rmSync(r, { recursive: true, force: true })
31
- })
32
-
33
- /** Directories holding a registry.json — what `zbc add` can actually resolve. */
34
- function census(dir: string): string[] {
35
- return fs
36
- .readdirSync(dir, { withFileTypes: true })
37
- .filter((e) => e.isDirectory() && fs.existsSync(path.join(dir, e.name, 'registry.json')))
38
- .map((e) => e.name)
39
- .toSorted()
40
- }
41
-
42
- /** The smallest tree `zbc add` will get as far as resolution in. */
43
- function project(): string {
44
- const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'zbc-add-registry-'))
45
- roots.push(dir)
46
- fs.writeFileSync(
47
- path.join(dir, 'zbc.config.ts'),
48
- "export default { project: 'p', environments: ['production'] }\n",
49
- )
50
- fs.mkdirSync(path.join(dir, 'packages/infra'), { recursive: true })
51
- fs.writeFileSync(path.join(dir, 'packages/infra/package.json'), '{"name":"@p/infra"}\n')
52
- return dir
53
- }
54
-
55
- /** citty wraps and colours its error line, so flatten before matching — a
56
- * line-oriented match cannot see a claim that wrapped. */
57
- function addUnknown(): string {
58
- const res = spawnSync('bun', [CLI, 'add', 'no-such-module', '--no-prompt'], {
59
- cwd: project(),
60
- encoding: 'utf8',
61
- })
62
- expect(res.status).not.toBe(0)
63
- return `${res.stdout}\n${res.stderr}`.replace(/\s+/g, ' ')
64
- }
65
-
66
- test('the built-in registry census is non-empty and holds the promoted host primitives', () => {
67
- const modules = census(MODULES)
68
- const apps = census(APPS)
69
- // Floors, because every assertion below is vacuously true against an empty
70
- // walk — a mistyped MODULES would otherwise pass silently.
71
- expect(modules.length).toBeGreaterThanOrEqual(12)
72
- expect(apps.length).toBeGreaterThanOrEqual(3)
73
- // Named rather than counted: the group this file exists because of.
74
- expect(modules).toContain('host-dir')
75
- expect(modules).toContain('host-exec')
76
- expect(modules).toContain('host-symlink')
77
- expect(modules).toContain('systemd-mask')
78
- })
79
-
80
- test('`zbc add <unknown>` names every module and app it could have resolved', () => {
81
- const expected = [...census(MODULES), ...census(APPS)].toSorted()
82
- const listed = addUnknown()
83
- .match(/Available: ([^.]+)\./)?.[1]
84
- .split(',')
85
- .map((s) => s.trim())
86
- .toSorted()
87
-
88
- // Set equality, not substring containment: "cloudflare" is a prefix of
89
- // "cloudflare-token", so a `toContain` sweep passes on a list missing the
90
- // short name entirely.
91
- expect(listed).toEqual(expected)
92
- })