@zabaca/zbc 0.13.0 → 0.15.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.
- package/CHANGELOG.md +117 -6
- package/package.json +5 -2
- package/src/commands/add.ts +13 -0
- package/src/commands/apply.ts +33 -9
- package/src/commands/destroy.ts +1 -1
- package/src/commands/init.ts +1 -0
- package/src/engine/apply.ts +110 -22
- package/src/engine/destroy.ts +183 -14
- package/src/engine/discover.ts +1 -1
- package/src/engine/resolve.ts +113 -14
- package/src/utils/copy-template.ts +10 -0
- package/templates/CLAUDE.md +1 -1
- package/templates/apps/inbox/package.json +3 -1
- package/templates/apps/inbox/tsconfig.json +3 -1
- package/templates/apps/inbox/worker/api.ts +197 -0
- package/templates/apps/inbox/worker/commands.ts +299 -0
- package/templates/apps/inbox/worker/inbox-do.ts +53 -10
- package/templates/apps/inbox/worker/index.ts +15 -202
- package/templates/apps/inbox/worker/mcp.ts +49 -100
- package/templates/infra/modules/cloudflare/index.ts +29 -73
- package/templates/infra/modules/cloudflare-access/index.ts +3 -3
- package/templates/infra/modules/cloudflare-api/index.ts +144 -32
- package/templates/infra/modules/cloudflare-api/registry.json +1 -1
- package/templates/infra/modules/cloudflare-email/index.ts +103 -144
- package/templates/infra/modules/cloudflare-email/registry.json +1 -1
- package/templates/infra/modules/cloudflare-token/index.ts +10 -32
- package/templates/infra/modules/cloudflare-token/registry.json +1 -1
- package/templates/infra/modules/cloudflare-tunnel/index.ts +3 -3
- package/templates/infra/modules/cloudflare-zone/index.ts +2 -2
- package/templates/infra/modules/fly/index.ts +17 -42
- package/templates/infra/modules/fly/registry.json +1 -1
- package/templates/infra/modules/host-file/index.ts +6 -7
- package/templates/infra/modules/provision-core/index.ts +21 -15
- package/templates/infra/modules/r2/index.ts +40 -71
- package/templates/infra/modules/r2/registry.json +1 -1
- package/templates/infra/modules/remote-provision/index.ts +1 -3
- package/templates/infra/modules/turso/index.ts +2 -15
- package/templates/infra/modules/vm-provision/index.ts +10 -3
- package/templates/infra/src/context.ts +115 -0
- package/templates/infra/src/define-module.ts +41 -2
- package/templates/infra/src/index.ts +11 -1
- package/templates/infra/src/types.ts +85 -3
- package/src/commands/add-registry.test.ts +0 -92
- package/src/commands/secret.test.ts +0 -717
- package/src/commands/vendor-mode.test.ts +0 -183
- package/src/infra-types.ts +0 -38
- package/src/utils/subtree.test.ts +0 -127
- package/templates/apps/inbox/worker/send-op.ts +0 -174
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// The parts of "talk to the Cloudflare API" that no module should own twice:
|
|
2
|
-
// the base URL, the envelope Cloudflare wraps every response in, and the
|
|
3
|
-
//
|
|
2
|
+
// the base URL, the envelope Cloudflare wraps every response in, and the two
|
|
3
|
+
// names its modules call to read a credential out of an imported instance's
|
|
4
|
+
// outputs (the rule itself is the engine's, in `../../src/context`).
|
|
4
5
|
//
|
|
5
6
|
// Extracted the way `provision-core` was extracted, and for the same reason its
|
|
6
7
|
// header gives. `cloudflare-zone` wrote these first; `cloudflare-tunnel` needed
|
|
@@ -9,8 +10,21 @@
|
|
|
9
10
|
// previous convention was a comment asking a human to notice, and by the time
|
|
10
11
|
// anybody counted the Cloudflare resolver was at four copies.
|
|
11
12
|
//
|
|
12
|
-
// The base URL is the load-bearing one. `
|
|
13
|
-
//
|
|
13
|
+
// The base URL below is the load-bearing one. Its `client/v4` is a version
|
|
14
|
+
// pin, not a protocol constant: the day it moves, it moves once — a promise
|
|
15
|
+
// that holds only while the string is written down in exactly one place, so it
|
|
16
|
+
// is deliberately absent from every comment in `modules/`, this one included.
|
|
17
|
+
//
|
|
18
|
+
// `cloudflare-email`, `cloudflare-token` and `r2` predated the extraction and
|
|
19
|
+
// carried private copies until 2026-09-02. Two of the copies knew things this
|
|
20
|
+
// file did not — that a body is not always JSON, and that a caller sometimes
|
|
21
|
+
// has to branch on the status — so they were folded in here (`CfError`, the
|
|
22
|
+
// non-JSON guard, `!res.ok`) rather than dropped on the way over. The only
|
|
23
|
+
// genuinely per-module part was naming which token scope was missing, and that
|
|
24
|
+
// is `CfOptions.hints`. `c9s/src/cf.ts` is a fifth copy and stays: it lives in
|
|
25
|
+
// a separate package and cannot import a template.
|
|
26
|
+
|
|
27
|
+
import { resolveOutput } from '../../src/context'
|
|
14
28
|
|
|
15
29
|
export const API = 'https://api.cloudflare.com/client/v4'
|
|
16
30
|
|
|
@@ -89,25 +103,129 @@ export function describeCfErrors(errors: unknown, status: number): string {
|
|
|
89
103
|
}
|
|
90
104
|
|
|
91
105
|
/**
|
|
92
|
-
*
|
|
93
|
-
*
|
|
94
|
-
*
|
|
106
|
+
* A failed Cloudflare call, with the two facts a caller may need to branch on:
|
|
107
|
+
* the HTTP status, and every error code the envelope carried.
|
|
108
|
+
*
|
|
109
|
+
* `cloudflare-email` is why this is a class and not a bare `Error`: one of its
|
|
110
|
+
* routing endpoints answers 404 on a shape the module then retries as a POST,
|
|
111
|
+
* and `err.status === 404` is the only way to tell that apart from a real
|
|
112
|
+
* failure. Everything thrown from `cfRaw`/`cf` is a `CfError`, so `instanceof`
|
|
113
|
+
* is a safe narrowing at any call site — including the non-JSON case, where
|
|
114
|
+
* `codes` is empty because there was no envelope to read codes out of.
|
|
115
|
+
*/
|
|
116
|
+
export class CfError extends Error {
|
|
117
|
+
constructor(
|
|
118
|
+
readonly status: number,
|
|
119
|
+
readonly codes: number[],
|
|
120
|
+
message: string,
|
|
121
|
+
) {
|
|
122
|
+
super(message)
|
|
123
|
+
this.name = 'CfError'
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** Did the envelope carry this error code? */
|
|
127
|
+
has(code: number): boolean {
|
|
128
|
+
return this.codes.includes(code)
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** Per-call options for `cf`/`cfRaw`. */
|
|
133
|
+
export interface CfOptions {
|
|
134
|
+
/**
|
|
135
|
+
* Per-code replacements for the thrown message — the module's scope hint.
|
|
136
|
+
*
|
|
137
|
+
* A 10000 "Authentication error" is the same fact for every module and a
|
|
138
|
+
* different instruction for each: `cloudflare-email` needs five token scopes
|
|
139
|
+
* named, `r2` needs one. That naming is the only genuinely per-module part of
|
|
140
|
+
* a Cloudflare failure, so it is the only hook here — the seam still throws
|
|
141
|
+
* `CfError` with the same status and codes, and only the text changes.
|
|
142
|
+
*
|
|
143
|
+
* When an envelope carries several hinted codes, the LOWEST code wins —
|
|
144
|
+
* deliberately, because the alternative is the order Cloudflare happened to
|
|
145
|
+
* list its errors in, and a module's operator-facing text must not depend on
|
|
146
|
+
* that. It also reproduces the copies this replaced, which tested their codes
|
|
147
|
+
* in a fixed order (`cloudflare-email` checked 10000 before 10105).
|
|
148
|
+
*/
|
|
149
|
+
hints?: Record<number, string>
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Every error code the envelope carried, as numbers.
|
|
154
|
+
*
|
|
155
|
+
* `code` is typed `number | string` because Cloudflare sends both spellings; a
|
|
156
|
+
* numeric string is the same code and is coerced, and anything else — a
|
|
157
|
+
* non-numeric string, a missing field, an entry that is not an object at all —
|
|
158
|
+
* is not a code and is dropped rather than surfaced as `NaN`.
|
|
159
|
+
*
|
|
160
|
+
* Pure; exported for tests.
|
|
161
|
+
*/
|
|
162
|
+
export function collectCfCodes(errors: unknown): number[] {
|
|
163
|
+
const entries = Array.isArray(errors) ? errors : []
|
|
164
|
+
const codes: number[] = []
|
|
165
|
+
for (const entry of entries) {
|
|
166
|
+
const raw = (entry as CfApiError | null | undefined)?.code
|
|
167
|
+
const code =
|
|
168
|
+
typeof raw === 'number'
|
|
169
|
+
? raw
|
|
170
|
+
: typeof raw === 'string' && raw.trim() !== ''
|
|
171
|
+
? Number(raw)
|
|
172
|
+
: Number.NaN
|
|
173
|
+
if (Number.isFinite(code)) codes.push(code)
|
|
174
|
+
}
|
|
175
|
+
return codes
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Call the API and return the WHOLE envelope; throw a `CfError` on failure.
|
|
180
|
+
* Callers that paginate need `result_info`, which is why this exists alongside
|
|
181
|
+
* `cf` rather than under it.
|
|
182
|
+
*
|
|
183
|
+
* Two things it refuses to assume, both learned from the private copies this
|
|
184
|
+
* replaced:
|
|
185
|
+
*
|
|
186
|
+
* - **The body is JSON.** A 5xx from Cloudflare's edge is an HTML page, and
|
|
187
|
+
* `res.json()` on it throws a parse error that names neither the call nor the
|
|
188
|
+
* status — the one moment a stack trace is least useful.
|
|
189
|
+
* - **`success` is the whole verdict.** It usually is, which is why the field
|
|
190
|
+
* exists; but a body that says `success: true` under a 4xx did not succeed,
|
|
191
|
+
* and returning its `result` hands the caller a shape it never checked.
|
|
95
192
|
*/
|
|
96
193
|
export async function cfRaw<T>(
|
|
97
194
|
token: string,
|
|
98
195
|
method: string,
|
|
99
196
|
path: string,
|
|
100
197
|
body?: unknown,
|
|
198
|
+
opts?: CfOptions,
|
|
101
199
|
): Promise<CfEnvelope<T>> {
|
|
102
200
|
const res = await fetch(`${API}${path}`, {
|
|
103
201
|
method,
|
|
104
202
|
headers: { Authorization: `Bearer ${token}`, 'Content-Type': 'application/json' },
|
|
105
203
|
...(body === undefined ? {} : { body: JSON.stringify(body) }),
|
|
106
204
|
})
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
205
|
+
let payload: CfEnvelope<T> | null
|
|
206
|
+
try {
|
|
207
|
+
payload = (await res.json()) as CfEnvelope<T> | null
|
|
208
|
+
} catch {
|
|
209
|
+
throw new CfError(
|
|
210
|
+
res.status,
|
|
211
|
+
[],
|
|
212
|
+
`Cloudflare API ${method} ${path}: HTTP ${res.status} (non-JSON body)`,
|
|
213
|
+
)
|
|
214
|
+
}
|
|
215
|
+
if (!res.ok || !payload?.success) {
|
|
216
|
+
const codes = collectCfCodes(payload?.errors)
|
|
217
|
+
// Scanned over the hints rather than over `codes`, so which one wins is the
|
|
218
|
+
// module's own ordering and not the API's — see `CfOptions.hints`.
|
|
219
|
+
const hinted = Object.keys(opts?.hints ?? {})
|
|
220
|
+
.map(Number)
|
|
221
|
+
.find((code) => codes.includes(code))
|
|
222
|
+
const hint = hinted === undefined ? undefined : opts?.hints?.[hinted]
|
|
223
|
+
throw new CfError(
|
|
224
|
+
res.status,
|
|
225
|
+
codes,
|
|
226
|
+
hint === undefined
|
|
227
|
+
? `Cloudflare API ${method} ${path} failed: ${describeCfErrors(payload?.errors, res.status)}`
|
|
228
|
+
: `Cloudflare API ${method} ${path} ${hint}`,
|
|
111
229
|
)
|
|
112
230
|
}
|
|
113
231
|
return payload
|
|
@@ -119,8 +237,9 @@ export async function cf<T>(
|
|
|
119
237
|
method: string,
|
|
120
238
|
path: string,
|
|
121
239
|
body?: unknown,
|
|
240
|
+
opts?: CfOptions,
|
|
122
241
|
): Promise<T> {
|
|
123
|
-
return (await cfRaw<T>(token, method, path, body)).result
|
|
242
|
+
return (await cfRaw<T>(token, method, path, body, opts)).result
|
|
124
243
|
}
|
|
125
244
|
|
|
126
245
|
/**
|
|
@@ -151,30 +270,23 @@ export function resolveApiToken(
|
|
|
151
270
|
* one that says `apiToken` for the third entry of `serviceTokens` sends the
|
|
152
271
|
* reader to the wrong line of an instance file.
|
|
153
272
|
*
|
|
154
|
-
*
|
|
155
|
-
*
|
|
156
|
-
*
|
|
157
|
-
*
|
|
273
|
+
* Both are now one line over `resolveOutput` — the rule and its three messages
|
|
274
|
+
* live in `../../src/context`, and a module inside an `apply`/`destroy` should
|
|
275
|
+
* say `ctx.output(ref, field)` instead, which every Cloudflare module in core
|
|
276
|
+
* now does. These stay because they take a raw imports record rather than a
|
|
277
|
+
* context, which is the only thing a caller outside an apply has, and because
|
|
278
|
+
* removing an exported name from a vendored library breaks whoever wrote their
|
|
279
|
+
* own Cloudflare module against it.
|
|
280
|
+
*
|
|
281
|
+
* Prefer `ctx.output` where you have a context: during a `destroy` it is the
|
|
282
|
+
* call that can still answer. `cloudflare-tunnel`'s teardown read `ctx.imports`
|
|
283
|
+
* through here and reported "which is not in this instance's imports" about an
|
|
284
|
+
* instance that WAS imported — the engine simply had not applied it.
|
|
158
285
|
*/
|
|
159
286
|
export function resolveRef(
|
|
160
287
|
ref: { from?: string; output?: string },
|
|
161
288
|
imports: Record<string, unknown>,
|
|
162
289
|
field: string,
|
|
163
290
|
): string {
|
|
164
|
-
|
|
165
|
-
throw new Error(`${field} must name both an instance (\`from\`) and an output (\`output\`)`)
|
|
166
|
-
}
|
|
167
|
-
const outputs = imports[ref.from]
|
|
168
|
-
if (outputs === undefined) {
|
|
169
|
-
throw new Error(
|
|
170
|
-
`${field} references instance "${ref.from}", which is not in this instance's imports`,
|
|
171
|
-
)
|
|
172
|
-
}
|
|
173
|
-
const value = (outputs as Record<string, unknown> | null)?.[ref.output]
|
|
174
|
-
if (typeof value !== 'string' || value === '') {
|
|
175
|
-
throw new Error(
|
|
176
|
-
`${field} references output "${ref.output}" on instance "${ref.from}", which doesn't emit it`,
|
|
177
|
-
)
|
|
178
|
-
}
|
|
179
|
-
return value
|
|
291
|
+
return resolveOutput(ref, imports, field)
|
|
180
292
|
}
|
|
@@ -8,5 +8,5 @@
|
|
|
8
8
|
],
|
|
9
9
|
"dependencies": {},
|
|
10
10
|
"secrets": [],
|
|
11
|
-
"instructions": "Not a module: it exports no defineModule. It is the library `cloudflare-access`, `cloudflare-tunnel`
|
|
11
|
+
"instructions": "Not a module: it exports no defineModule. It is the library every Cloudflare REST module imports as `../cloudflare-api` — `cloudflare-access`, `cloudflare-email`, `cloudflare-tunnel`, `cloudflare-token`, `cloudflare-zone` and `r2`. It exists because the same envelope-unwrapping had reached four copies in the contributing repo before anybody counted — `success`, `errors`, `result` is not hard, and that is exactly why it gets rewritten slightly differently every time. In COPY mode add it alongside whichever Cloudflare module you copied."
|
|
12
12
|
}
|