@runeditrun/rer 0.2.23 → 0.2.25
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/README.md +16 -7
- package/cli/browser-runtime.mjs +65 -0
- package/cli/demo-host.mjs +1 -1
- package/cli/deploy-claim-request.mjs +5 -4
- package/cli/deploy-command.mjs +7 -5
- package/cli/deploy-inputs.mjs +2 -1
- package/cli/deploy-observation.mjs +20 -2
- package/cli/deploy-owner.mjs +12 -7
- package/cli/deploy.mjs +19 -6
- package/cli/export.mjs +1 -1
- package/cli/hydration/browser.mjs +3 -9
- package/cli/inspect.mjs +3 -1
- package/cli/lifecycle.mjs +4 -1
- package/cli/site.mjs +2 -2
- package/global/CHANGELOG.md +37 -0
- package/global/README.md +16 -7
- package/global/browser-runtime.mjs +45 -0
- package/global/cli/layout.mjs +8 -4
- package/global/main.mjs +13 -3
- package/install.sh +56 -3
- package/modules/container/README.md +6 -0
- package/modules/container/adapters/d1.mjs +1 -1
- package/modules/container/build.mjs +2 -1
- package/modules/container/image-variables.mjs +27 -0
- package/modules/container/transfer.mjs +0 -1
- package/modules/container/worker-engine.mjs +1 -1
- package/modules/delivery-producer/declarations.mjs +6 -1
- package/modules/delivery-producer/mail.mjs +66 -0
- package/modules/demo/label.mjs +5 -2
- package/modules/demo-host/README.md +29 -7
- package/modules/demo-host/container.mjs +19 -7
- package/modules/demo-host/placement.mjs +19 -5
- package/modules/demo-host/plan.mjs +11 -6
- package/modules/demo-host/start-script.mjs +2 -1
- package/modules/demo-host/startup.mjs +3 -3
- package/modules/demo-host/worker.mjs +27 -8
- package/modules/deploy-kit/README.md +41 -6
- package/modules/deploy-kit/asset-health.mjs +28 -0
- package/modules/deploy-kit/d1-import.mjs +22 -4
- package/modules/deploy-kit/diagnostics.mjs +46 -1
- package/modules/deploy-kit/health.mjs +2 -23
- package/modules/deploy-kit/migration-ledger.mjs +6 -2
- package/modules/deploy-kit/migration-retry.mjs +91 -0
- package/modules/deploy-kit/migrations.mjs +9 -5
- package/modules/deploy-kit/owner-cli.mjs +9 -6
- package/modules/deploy-kit/owner-remove.mjs +104 -64
- package/modules/deploy-kit/pagination.mjs +99 -0
- package/modules/deploy-kit/provider-transport.mjs +28 -5
- package/modules/deploy-kit/provider.mjs +38 -75
- package/modules/deploy-kit/r2-objects.mjs +6 -17
- package/modules/deploy-kit/site-readback.mjs +67 -0
- package/modules/deploy-kit/site.mjs +17 -31
- package/modules/deploy-kit/uploaded-worker.mjs +9 -3
- package/modules/deploy-kit/worker.mjs +5 -2
- package/modules/erasure/queue.mjs +0 -2
- package/modules/job-queue/cli.mjs +1 -1
- package/modules/operator-auth/README.md +10 -2
- package/modules/operator-auth/config.mjs +1 -0
- package/modules/operator-auth/conformance/presentation.mjs +2 -2
- package/modules/operator-auth/identity/email-link/confirmation.mjs +2 -1
- package/modules/operator-auth/identity/email-link/expiry.mjs +8 -0
- package/modules/operator-auth/identity/email-link/index.mjs +2 -1
- package/modules/operator-auth/identity/email-link/message.mjs +3 -2
- package/modules/operator-auth/identity/oidc/jwt.mjs +1 -1
- package/modules/operator-auth/index.d.mts +2 -0
- package/modules/portability/README.md +7 -2
- package/modules/portability/bridge.mjs +3 -3
- package/modules/portability/catalogue.mjs +1 -1
- package/modules/portability/cli-options.mjs +1 -1
- package/modules/portability/import-staging.mjs +45 -0
- package/modules/portability/import.mjs +8 -8
- package/modules/portability/legacy-files.mjs +1 -1
- package/modules/portability/legacy-safety.mjs +3 -3
- package/modules/portability/payloads.mjs +1 -1
- package/modules/portability/storage.mjs +5 -2
- package/modules/release/source.mjs +1 -1
- package/package.json +1 -1
|
@@ -5,11 +5,11 @@ import { fingerprint } from './capacity.mjs';
|
|
|
5
5
|
// responses. A dropped browser request cannot abandon cleanup or hold a place
|
|
6
6
|
// until the demo's hour expires.
|
|
7
7
|
export async function finishStart(demo, options) {
|
|
8
|
-
const { correlation
|
|
8
|
+
const { correlation } = options, id = demo.ctx.id.toString();
|
|
9
9
|
try {
|
|
10
10
|
report(correlation, Date.now(), 'reservation', { heldId: await fingerprint(id) });
|
|
11
|
-
const result = await bounded(demo.start(options), demo.
|
|
12
|
-
if (
|
|
11
|
+
const result = await bounded(demo.start(options), demo.placementTimeout + 1000);
|
|
12
|
+
if (demo.stopped) throw new Error('container-stopped');
|
|
13
13
|
await demo.ctx.storage.put('startup', { phase: 'ready', ...result });
|
|
14
14
|
await demo.ctx.storage.delete('startDeadline');
|
|
15
15
|
} catch (error) {
|
|
@@ -64,11 +64,16 @@ function progress(request, env, id, state) {
|
|
|
64
64
|
return new Response(null, { status: 303, headers });
|
|
65
65
|
}
|
|
66
66
|
|
|
67
|
+
// The page asks again as soon as this answers: the object holds a pending
|
|
68
|
+
// answer until the start changes. A slow answer is still pending; the start's
|
|
69
|
+
// own budget decides failure.
|
|
67
70
|
async function status(request, env) {
|
|
68
71
|
const id = demoId(request, env);
|
|
69
72
|
if (!id) return refusal(request, env, 410, 'This demo has ended. Start a new demo.');
|
|
70
|
-
|
|
71
|
-
|
|
73
|
+
let state;
|
|
74
|
+
try { state = await bounded(env.DEMO.get(id).status(1500), 3500); }
|
|
75
|
+
catch { state = { phase: 'starting' }; }
|
|
76
|
+
return progress(request, env, id, state);
|
|
72
77
|
}
|
|
73
78
|
|
|
74
79
|
async function start(request, env, ctx) {
|
|
@@ -96,9 +101,23 @@ async function start(request, env, ctx) {
|
|
|
96
101
|
if (!await bounded(capacity.acquire(id.toString(), Date.now() + 60_000, Number(env.DEMO_MAX_INSTANCES), correlation), 2000)) {
|
|
97
102
|
return refuse(503, 'Every demo is in use right now. Try again in a few minutes.');
|
|
98
103
|
}
|
|
104
|
+
// Who holds the place: automated browsers pass Turnstile's testing keys.
|
|
105
|
+
report(correlation, started, 'admitted', { agent: (request.headers.get('user-agent') ?? '').slice(0, 160), network: request.cf?.asOrganization ?? null });
|
|
99
106
|
demo = env.DEMO.get(id);
|
|
100
|
-
const
|
|
101
|
-
|
|
107
|
+
const begun = demo.begin({ correlation, origin: env.DEMO_ORIGIN, originVar: env.DEMO_ORIGIN_VAR, ends, endsLocal: localTime(ends, request.cf?.timezone), settings: JSON.parse(env.DEMO_SETTINGS) });
|
|
108
|
+
// A new object can take seconds to answer. That is a slow start, not a
|
|
109
|
+
// failed one: the page polls this copy, and a begin that later fails ends
|
|
110
|
+
// it and releases its place.
|
|
111
|
+
let slow;
|
|
112
|
+
const state = await Promise.race([begun, new Promise(resolve => { slow = setTimeout(resolve, 2000, null); })]).finally(() => clearTimeout(slow));
|
|
113
|
+
if (!state) {
|
|
114
|
+
report(correlation, started, 'begin-pending');
|
|
115
|
+
ctx.waitUntil(begun.catch(error => {
|
|
116
|
+
report(correlation, started, 'failed', { error: category(error) });
|
|
117
|
+
return bounded(demo.end(), 6000);
|
|
118
|
+
}).catch(cleanup => report(correlation, started, 'cleanup', { error: category(cleanup) })));
|
|
119
|
+
}
|
|
120
|
+
return progress(request, env, id, { ...(state ?? { phase: 'starting' }), correlation });
|
|
102
121
|
} catch (error) {
|
|
103
122
|
report(correlation, started, 'failed', { error: category(error) });
|
|
104
123
|
// Cleanup runs independently of the response: retries never wait on it.
|
|
@@ -134,10 +153,10 @@ export default {
|
|
|
134
153
|
// The JSON start leaves the browser on /. Reloading it must keep the
|
|
135
154
|
// pending copy on the progress page until health and owner claim finish.
|
|
136
155
|
if (primary && url.pathname === '/' && ['GET', 'HEAD'].includes(request.method)) {
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
156
|
+
// A recovering object may answer slowly; its own fetch below still
|
|
157
|
+
// serves the demo or its ended page.
|
|
158
|
+
const state = await bounded(env.DEMO.get(id).status(), 2000).catch(() => null);
|
|
159
|
+
if (state?.phase === 'starting') return progress(request, env, id, state);
|
|
141
160
|
}
|
|
142
161
|
return env.DEMO.get(id).fetch(request);
|
|
143
162
|
}
|
|
@@ -40,7 +40,7 @@ The maintained default-seed `/health` returns the existing `status: 'ok'` plus t
|
|
|
40
40
|
} }
|
|
41
41
|
```
|
|
42
42
|
|
|
43
|
-
Assets upload with `run_worker_first: true` (`RUN_WORKER_FIRST` in `render.mjs`). Every request reaches the Worker first, so the seed's routing, origin admission, `no-store` document policy and private-path refusals decide what is served, and the Base delivery producer accepts no other setting. A built file therefore reaches the browser only through the Worker: seed entries call `staticAssetResponse(request, env)` from `foundation/runtime/static-assets.ts` first. After the deploy identity passes, health requests one built file (the first artifact asset that is neither an HTML document nor under a dot path)
|
|
43
|
+
Assets upload with `run_worker_first: true` (`RUN_WORKER_FIRST` in `render.mjs`). Every request reaches the Worker first, so the seed's routing, origin admission, `no-store` document policy and private-path refusals decide what is served, and the Base delivery producer accepts no other setting. A built file therefore reaches the browser only through the Worker: seed entries call `staticAssetResponse(request, env)` from `foundation/runtime/static-assets.ts` first. After the deploy identity passes, health requests one built file (the first artifact asset that is neither an HTML document nor under a dot path) without credentials, retries a 5xx or transport failure once within the existing readiness deadline, and fails with `asset-http-failed` unless it answers 200; the result is recorded as `health.asset`. `rer verify` serves the local build with the same setting (`cli/hosted-output.mjs`).
|
|
44
44
|
|
|
45
45
|
The controller supplies the two public variables and Cloudflare version-metadata binding. The parent owns default-seed source/types, package scripts, shared manifests and workflows. Keep the staged-deploy descriptor blocked until the three-stage isolation and actual build/health integration pass; the original deploy-then-health executor cannot safely run installation/build with its credentials.
|
|
46
46
|
|
|
@@ -52,7 +52,7 @@ Cron reads require the documented `result: {schedules: [...]}` envelope, includi
|
|
|
52
52
|
|
|
53
53
|
`workerConsumerScript(consumer)` (also exported from `index.mjs`) decodes Worker consumer identities from either documented `script_name` or the observed GET response string `script`. It returns the exact name using the plan's resource-name grammar, or `null` for missing, malformed, conflicting, or non-Worker identities. If both fields are present, each must be valid and both must agree; a malformed alias is never ignored. Reconciliation checks every consumer on queues with a requested consumer before any trigger write and rejects foreign or duplicate consumers. Writes continue to use the documented `script_name` request field. Read-only health and retained-receipt repeats accept either response variant without repairs.
|
|
54
54
|
|
|
55
|
-
D1/Queue/DO account listings fully paginate with bounded page/request budgets. R2 follows provider cursors. Missing pagination evidence, duplicate names, permission errors, partial success, redirects and transport failures fail visibly. Requests go only to Cloudflare's exact account API. Requests have a 30-second timeout and bounded response size. Before any HTTP status, GET/HEAD reads and writes that provably failed before connecting (DNS failure, connection refusal or connect timeout) may retry once, sharing the original timeout and request budget. The first read reserves half the timeout for a retry; caller deadlines and cancellation still apply. HTTP responses, failed response-body reads and ambiguous write failures are
|
|
55
|
+
D1/Queue/DO account listings fully paginate with bounded page/request budgets. R2 follows provider cursors. Missing pagination evidence, duplicate names, permission errors, partial success, redirects and transport failures fail visibly. Requests go only to Cloudflare's exact account API. Requests have a 30-second timeout and bounded response size. Before any HTTP status, GET/HEAD reads and writes that provably failed before connecting (DNS failure, connection refusal or connect timeout) may retry once, sharing the original timeout and request budget. The first read reserves half the timeout for a retry; caller deadlines and cancellation still apply. Other HTTP responses, failed response-body reads and ambiguous write failures are not replayed. Two scoped exceptions share the original request timeout and budget: content-addressed asset session/upload requests retry a 5xx or transport failure (including a broken response body) once; owner-account removal retries GET/DELETE 5xx responses up to three attempts, with bounded backoff, and treats a retried DELETE 404 as absent before the normal absence readback. Final asset failures name the built path and HTTP status (or missing status). Removal failures name the count and resources still present or not yet confirmed absent, retain their ownership proof, and give the same removal command to finish. Asset upload tokens are used only on the documented same-account assets endpoint.
|
|
56
56
|
|
|
57
57
|
The first receipt binds source commit SHA and tree, artifact/config SHA-256, effective deployment fingerprint, account/Worker, actual resource IDs, migration hashes, secret names and active provider deployment/version/etag. It contains no secret value. The CLI creates it exclusively at mode 0600 before the first health request; it is an identity receipt, not a claim that health passed (health attempts are added afterwards, see below). A failed health request still exits nonzero, while the retained identity allows the next explicit invocation to authenticate state without another upload. For the second deployment use the **same artifact, run/attempt and receipt**. Current provider state must match the prior receipt. The kit performs no resource creation, Worker/asset upload, SQL mutation, Cron/consumer update, or secret rewrite when unchanged. Read-only D1 queries use the provider's POST query transport; a POST is not by itself a database mutation. The receipt remains byte-identical on a repeat. Missing resources, drift or stale state fail without repair during this repeat.
|
|
58
58
|
|
|
@@ -186,7 +186,7 @@ No zone ruleset is read or written. Every request now invokes the Worker: one pa
|
|
|
186
186
|
- the Worker is the kit's when its latest deployment serves, at 100%, the Worker's newest version, and that version's message annotation is the kit's `rer-site:<fingerprint>` (every site upload sets it);
|
|
187
187
|
- a Custom Domain is the kit's when its service is the kit's Worker.
|
|
188
188
|
|
|
189
|
-
A read-only preflight reads the zone (it must be in this account, else `site-zone-missing`), the Worker and the zone's Custom Domains, and refuses before any write: `resource-name-collision` for a Worker of the name whose live version the kit did not upload, or for a hostname attached to another service (or to a Worker that is not the kit's); `owned-resource-replaced` for a kit Worker whose latest deployment is not the kit's upload (a dashboard rollback or a split deployment). It then uploads the assets and the site Worker, checks that the new version is live, turns workers.dev off, attaches each missing hostname, and reads the Worker and both domains back. A repeat attaches nothing new. `.rer/deploy/site-<environment>.json` receives a receipt for the person; it is never read.
|
|
189
|
+
A read-only preflight reads the zone (it must be in this account, else `site-zone-missing`), the Worker and the zone's Custom Domains, and refuses before any write: `resource-name-collision` for a Worker of the name whose live version the kit did not upload, or for a hostname attached to another service (or to a Worker that is not the kit's); `owned-resource-replaced` for a kit Worker whose latest deployment is not the kit's upload (a dashboard rollback or a split deployment). It then uploads the assets and the site Worker, checks that the new version is live, turns workers.dev off, attaches each missing hostname, and reads the Worker and both domains back. Both post-upload Worker checks share a 90-second upload window: missing visibility or the exact preflight version waits with backoff until the upload response's version serves alone as newest with its expected fingerprint. A foreign version, changed fingerprint or split deployment refuses immediately. A timeout names the last serving/newest versions and fingerprint. A repeat attaches nothing new. `.rer/deploy/site-<environment>.json` receives a receipt for the person; it is never read. Provider propagation cases are tested against the fake Cloudflare API in `tests/site-provider.mjs`, where every zone ruleset path answers 403.
|
|
190
190
|
|
|
191
191
|
The token needs, beyond the owner-account permissions above:
|
|
192
192
|
|
|
@@ -200,7 +200,9 @@ No Single Redirect (dynamic redirect ruleset) permission is needed. Base's refer
|
|
|
200
200
|
|
|
201
201
|
## Migration safety
|
|
202
202
|
|
|
203
|
-
SQL files have ordered unique names and SHA-256 identities. `d1_migrations` holds controller history with ordered IDs, names, timestamps, SHA-256 hashes and states. Native archives already exclude this installation table. Standard Wrangler name-only inserts remain possible, but their missing hashes/states block subsequent Base deployment pending reviewed adoption. Applied entries must be an unchanged prefix of the desired set. A new migration gets an `applying` claim before its SQL and completion marker are submitted together through the [Cloudflare D1 SQL import API](https://developers.cloudflare.com/api/resources/d1/subresources/database/methods/import/), using the same whole-file protocol as Wrangler to preserve compound trigger bodies and atomic SQL/completion. Uploads use bounded HTTPS R2 destinations, without account credentials; polling is bounded and signed URLs
|
|
203
|
+
SQL files have ordered unique names and SHA-256 identities. `d1_migrations` holds controller history with ordered IDs, names, timestamps, SHA-256 hashes and states. Native archives already exclude this installation table. Standard Wrangler name-only inserts remain possible, but their missing hashes/states block subsequent Base deployment pending reviewed adoption. Applied entries must be an unchanged prefix of the desired set. A new migration gets an `applying` claim before its SQL and completion marker are submitted together through the [Cloudflare D1 SQL import API](https://developers.cloudflare.com/api/resources/d1/subresources/database/methods/import/), using the same whole-file protocol as Wrangler to preserve compound trigger bodies and atomic SQL/completion. Uploads use bounded HTTPS R2 destinations, without account credentials; polling is bounded and signed URLs are never diagnostic output. A failed import keeps Cloudflare's own codes and messages on the error (`providerRefusal`, which `rer deploy` prints, with credentials and URLs removed and each message cut to 300 characters) and names the migration it was running.
|
|
204
|
+
|
|
205
|
+
An import that did not complete leaves its `applying` claim. The next deploy of the same migration bytes runs it again only when it proves none of it applied (`migration-retry.mjs`): the import is atomic, so the migration's effects that would make a second run fail (a CREATE without IF NOT EXISTS, ADD COLUMN, RENAME) and that last to the end of the file must all be absent. Then no attempt committed, and one that commits late makes the re-run fail whole instead of applying twice. A migration with no such effect, or with any of them present, stays refused as `uncertain-migration-claim`; the message names the migration, what is present, and the exact step: undo what is present and deploy again, or confirm by hand and delete or complete its claim with `wrangler d1 execute`. The kit never re-runs a migration whose effects may be present, rolls back user data, or marks an unknown outcome successful. Any other unconfirmed or changed history is still `changed-or-uncertain-migration-history`. Readback checks every final identity.
|
|
204
206
|
|
|
205
207
|
A seed that had to rewrite a migration it already released declares the rewrite
|
|
206
208
|
in `migrations/rewrites.json`: `{ "version": 1, "rewrites": [{ "migration":
|
|
@@ -221,7 +223,7 @@ without treating quoted semicolons as terminators or changing migration hashes.
|
|
|
221
223
|
|
|
222
224
|
A lexical guard rejects standalone transaction/resource control statements (`BEGIN`, `COMMIT`, `END`, `ROLLBACK`, `SAVEPOINT`, `RELEASE`, `ATTACH`, `DETACH`, `VACUUM`) at statement boundaries, including after a trigger. Bare `ROLLBACK` is also refused in conflict clauses and `RAISE`, which can roll back a batch without a standalone control statement. SQLite `CREATE [TEMP|TEMPORARY] TRIGGER` bodies and nested `CASE ... END` expressions are supported; quoted text and comments cannot create statement boundaries. Both ledger names are reserved in bare, double-quoted, backtick and bracket identifiers, including schema-qualified references. [SQLite also accepts single-quoted identifiers](https://www.sqlite.org/lang_keywords.html), so exact controller ledger names are reserved in every token, including data strings, relation names, virtual-table options and introspection arguments. Comments and longer descriptive strings remain allowed. This intentionally conservative policy does not attempt to authorize arbitrary SQLite name grammars. Incomplete quotes, block comments, parentheses, CASE expressions or trigger bodies and NUL bytes fail closed; trigger header expressions must close before BEGIN and body expressions before their statement semicolon. This guard protects controller custody; SQLite still validates application SQL syntax.
|
|
223
225
|
|
|
224
|
-
The old kit `_rer_deploy_migrations` ledger is moved atomically into `d1_migrations` only after every applied name/hash/state proves an exact desired-history prefix. An ordinary deploy performs this one-time metadata transition even for unchanged code; subsequent redeploys write nothing, and health remains read-only. Existing Wrangler `d1_migrations` records lack content hashes. Automatic adoption would fabricate historical certainty, so those installations require separately reviewed history/hash adoption before this kit proceeds unless their ordered rows exactly match a prefix already attested by the old kit ledger. That transition preserves their IDs and timestamps. The kit can extend its own previous migrations while retaining existing application rows. Local SQLite tests prove failure rollback in the test transport; the provider's actual failure atomicity, retained data, and two-deploy no-mutation behavior remain live proof obligations.
|
|
226
|
+
The old kit `_rer_deploy_migrations` ledger is moved atomically into `d1_migrations` only after every applied name/hash/state proves an exact desired-history prefix. An ordinary deploy performs this one-time metadata transition even for unchanged code; subsequent redeploys write nothing, and health remains read-only. Existing Wrangler `d1_migrations` records lack content hashes. Automatic adoption would fabricate historical certainty, so those installations require separately reviewed history/hash adoption before this kit proceeds unless their ordered rows exactly match a prefix already attested by the old kit ledger. That transition preserves their IDs and timestamps. The kit can extend its own previous migrations while retaining existing application rows. Local SQLite tests prove failure rollback in the test transport; the provider's actual failure atomicity, retained data, and two-deploy no-mutation behavior remain live proof obligations. An `applying` claim, including one left by a lost remote response, is re-run only on the proof above.
|
|
225
227
|
|
|
226
228
|
## Local operations and source templates
|
|
227
229
|
|
|
@@ -290,7 +292,11 @@ budget, and no request starts at the deadline. An unchanged repeat uses the
|
|
|
290
292
|
existing library window and does not record first-upload readiness attempts.
|
|
291
293
|
The standalone CLI `health` command sends a single request; only an answer from
|
|
292
294
|
another Worker version (an edge still converging after a secret change) is asked
|
|
293
|
-
again, within 30 seconds.
|
|
295
|
+
again, within 30 seconds. Immediate post-upload readback also waits through an
|
|
296
|
+
HTTPS workers.dev 404 whose complete body is `error code: 1042`, including after
|
|
297
|
+
a changed-source redeploy, within the original upload deadline and the health
|
|
298
|
+
command's 30-second bound. This does not grant first-creation setup-token replay
|
|
299
|
+
permission to a redeploy. Library `verifyHealth`
|
|
294
300
|
retains the existing window, retrying only 404, 503 and network failures for
|
|
295
301
|
30 seconds by default.
|
|
296
302
|
|
|
@@ -407,3 +413,32 @@ narrowing where Cloudflare omits service-binding `props` from settings/version
|
|
|
407
413
|
readback. Ordinary `allowedHosts` still maps to `props` and retains strict
|
|
408
414
|
readback; missing props are never silently accepted. See the
|
|
409
415
|
[egress service guide](../egress-service/README.md).
|
|
416
|
+
|
|
417
|
+
### List completion and deployment failures
|
|
418
|
+
|
|
419
|
+
Every Cloudflare list read uses `pagination.mjs`, including reads through
|
|
420
|
+
`request` (Worker versions, deployments, secrets and schedules, consumers,
|
|
421
|
+
domains, zones, routes and Container applications), account inventories and streaming R2 object pages.
|
|
422
|
+
The endpoint's strategy chooses request parameters; the shared reader decides
|
|
423
|
+
completion. Missing/null `result_info` on a short requested-limit page is
|
|
424
|
+
terminal. Missing/empty cursors and reached `total_pages` are terminal unless
|
|
425
|
+
other metadata explicitly advertises unread rows. Continuation cursors and page
|
|
426
|
+
counts are followed. Contradictory totals, repeated cursors, malformed metadata,
|
|
427
|
+
request limits and a truncated reply with no continuation still refuse.
|
|
428
|
+
|
|
429
|
+
`provider-incomplete-pagination` identifies a list with insufficient continuation
|
|
430
|
+
information, rather than a Cloudflare error code. CLI failures name its GET path;
|
|
431
|
+
Cloudflare error codes/messages are printed when the provider supplied them, and
|
|
432
|
+
otherwise explicitly identified as unavailable. Resolve the named call's cause
|
|
433
|
+
and rerun the printed command with the same account and environment; preserve
|
|
434
|
+
the ownership record. Removal detaches all consumers of an owned queue before
|
|
435
|
+
queue deletion and polls its listing for absence without repeating the DELETE.
|
|
436
|
+
|
|
437
|
+
`owner-claim-failed` is the hosted bootstrap failure, after an app may already
|
|
438
|
+
have been deployed. The CLI retains the observed POST path, HTTP status and
|
|
439
|
+
application error code even when a function hook throws a generic failure.
|
|
440
|
+
Correct owner authorization or token inputs and rerun deploy; unknown supplied
|
|
441
|
+
tokens should be left unset so rer issues a fresh one. The existing owner is
|
|
442
|
+
kept, and a successful claim retires the one-time token. A hook that fails
|
|
443
|
+
without an HTTP exchange can only supply its own cause. Seeds copy this guide
|
|
444
|
+
and `RUN-LOCALLY.md` for these recovery instructions.
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { nameAssetFailure } from './diagnostics.mjs';
|
|
2
|
+
|
|
3
|
+
/** Probe a real built file after identity health. A transient read gets one replay;
|
|
4
|
+
* a fresh edge missing a hashed file may settle within the existing route window. */
|
|
5
|
+
export async function servedAsset(origin, path, request, timeoutMs, {window, windowMs = 0, clock = () => performance.now(), sleep = ms => new Promise(resolve => setTimeout(resolve, ms))} = {}) {
|
|
6
|
+
const start = clock(), budget = Math.min(windowMs || timeoutMs, window?.remaining() ?? Infinity);
|
|
7
|
+
const deadline = AbortSignal.timeout(Math.max(1, Math.ceil(budget)));
|
|
8
|
+
let transient = 0, failure;
|
|
9
|
+
const fail = (code, status) => nameAssetFailure(new Error(code), [path], status);
|
|
10
|
+
for (;;) {
|
|
11
|
+
const remaining = Math.min(budget - (clock() - start), window?.remaining() ?? Infinity);
|
|
12
|
+
if (remaining <= 0 || deadline.aborted) throw failure ?? fail('asset-readiness-timeout');
|
|
13
|
+
let response;
|
|
14
|
+
try { response = await request(`${origin}/${path.split('/').map(encodeURIComponent).join('/')}`, {redirect:'manual',signal:AbortSignal.any([deadline,AbortSignal.timeout(Math.max(1, Math.ceil(Math.min(timeoutMs,remaining / (transient ? 1 : 2)))))]),headers:{'Cache-Control':'no-cache'}}); }
|
|
15
|
+
catch { failure = fail('asset-transport-failed'); }
|
|
16
|
+
if (response) {
|
|
17
|
+
await response.body?.cancel().catch(() => {});
|
|
18
|
+
if (response.status === 200 && !response.redirected) return {path,status:200};
|
|
19
|
+
failure = fail('asset-http-failed', response.status);
|
|
20
|
+
if (response.redirected || response.status !== 404 && (response.status < 500 || response.status > 599)) throw failure;
|
|
21
|
+
}
|
|
22
|
+
const propagating = response?.status === 404 && clock() - start + 2000 <= windowMs;
|
|
23
|
+
if (!propagating && (++transient > 1 || response?.status === 404)) throw failure;
|
|
24
|
+
const rest = Math.min(budget - (clock() - start), window?.remaining() ?? Infinity);
|
|
25
|
+
if (rest <= 0 || deadline.aborted) throw failure;
|
|
26
|
+
await sleep(Math.min(2000,rest));
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -1,9 +1,27 @@
|
|
|
1
1
|
import { createHash } from 'node:crypto';
|
|
2
2
|
import { assert, uuid } from './validation.mjs';
|
|
3
|
+
import { recordDeployFailure } from './diagnostics.mjs';
|
|
3
4
|
|
|
4
5
|
// Wrangler's remote --file protocol. D1 imports execute the whole file atomically,
|
|
5
6
|
// including trigger bodies; the query endpoint's semicolon splitting cannot.
|
|
6
|
-
|
|
7
|
+
// Cloudflare's own reason for an import that failed or did not finish, kept on the
|
|
8
|
+
// error as providerRefusal (rer deploy names it) and in the deploy diagnostic: its
|
|
9
|
+
// codes and messages without credentials, URLs (signed upload destinations) or
|
|
10
|
+
// control characters, each cut to 300 characters.
|
|
11
|
+
function importFailure(code, id, response, redact) {
|
|
12
|
+
const clean = text => redact(text).replace(/\bhttps?:\/\/\S+/gi, '[URL omitted]').replace(/[\x00-\x1f\x7f-\x9f]+/g, ' ').trim().slice(0, 300);
|
|
13
|
+
const said = [response?.error, ...(Array.isArray(response?.errors) ? response.errors : [])]
|
|
14
|
+
.map(item => typeof item === 'string' ? { message:item } : item)
|
|
15
|
+
.filter(item => Number.isSafeInteger(item?.code) || typeof item?.message === 'string' && item.message.trim()).slice(0, 3)
|
|
16
|
+
.map(item => ({ ...(Number.isSafeInteger(item.code) ? { code:item.code } : {}), ...(typeof item.message === 'string' ? { message:clean(item.message) } : {}) }));
|
|
17
|
+
const status = typeof response?.status === 'string' && /^[a-z_]{1,40}$/.test(response.status) ? response.status : undefined;
|
|
18
|
+
const errors = said.length ? said : status ? [{ message:`import status ${status}` }] : [];
|
|
19
|
+
const error = new Error(code);
|
|
20
|
+
error.providerRefusal = { operation:`POST /d1/database/${id}/import`, errors };
|
|
21
|
+
return recordDeployFailure(error, { providerCodes:errors.filter(item => item.code !== undefined).map(item => item.code), output:errors.map(item => item.message).filter(Boolean).join('\n') });
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export async function importD1({ envelope, upload, redact = String }, id, sql) {
|
|
7
25
|
assert(uuid(id) && typeof sql === 'string' && Buffer.byteLength(sql) <= 4 * 1024 * 1024, 'invalid-d1-import');
|
|
8
26
|
const bytes = Buffer.from(sql), etag = createHash('md5').update(bytes).digest('hex');
|
|
9
27
|
const post = async body => (await envelope(`/d1/database/${id}/import`, { method:'POST', body })).result;
|
|
@@ -15,12 +33,12 @@ export async function importD1({ envelope, upload }, id, sql) {
|
|
|
15
33
|
}
|
|
16
34
|
for (let polls = 0; polls < 100; polls++) {
|
|
17
35
|
// REST places errors on the envelope; validate nested errors when supplied.
|
|
18
|
-
|
|
36
|
+
if (!(response?.success === true && response.type === 'import' && response.status !== 'error' && (!Object.hasOwn(response, 'errors') || Array.isArray(response.errors) && !response.errors.length))) throw importFailure('d1-import-failed', id, response, redact);
|
|
19
37
|
if (response.status === 'complete') {
|
|
20
|
-
|
|
38
|
+
if (!(Number.isSafeInteger(response.result?.num_queries) && response.result.num_queries > 0 && typeof response.result.final_bookmark === 'string' && response.result.final_bookmark.length > 0)) throw importFailure('d1-import-invalid-result', id, response, redact);
|
|
21
39
|
return;
|
|
22
40
|
}
|
|
23
|
-
|
|
41
|
+
if (!(response.status === 'active' && typeof response.at_bookmark === 'string' && response.at_bookmark.length > 0 && response.at_bookmark.length <= 1024)) throw importFailure('d1-import-uncertain', id, response, redact);
|
|
24
42
|
response = await post({ action:'poll', current_bookmark:response.at_bookmark });
|
|
25
43
|
}
|
|
26
44
|
throw new Error('d1-import-poll-limit');
|
|
@@ -14,6 +14,7 @@ export function deployDiagnostic(error, redact) {
|
|
|
14
14
|
return {code,step:detail.step ?? 'deploy',created:(detail.created ?? []).map(clean),
|
|
15
15
|
...(typeof error.resource==='string' && /^(?:d1|r2|kv|queues|worker|service):[a-z0-9-]+$/.test(error.resource) ? {resource:clean(error.resource)} : {}),
|
|
16
16
|
...(detail.providerCodes?.length ? {providerCodes:detail.providerCodes} : {}),
|
|
17
|
+
...(redact && providerFailure(error,redact) ? {provider:providerFailure(error,redact)} : {}),
|
|
17
18
|
output:(detail.output ? (redact ? clean(detail.output) : '(provider output withheld; rer deploy provides redacted details)') : '(no provider output)').split(/\r?\n/).slice(0,8).map(line=>line.slice(0,500)).join('\n')};
|
|
18
19
|
}
|
|
19
20
|
/** One line naming the provider call that was refused and what Cloudflare said, from
|
|
@@ -24,6 +25,24 @@ export function providerRefusal(error, redact = String) {
|
|
|
24
25
|
const said = refusal.errors.map(item => [item?.code, typeof item?.message === 'string' ? redact(item.message).slice(0,300) : undefined].filter(part => part !== undefined && part !== '').join(' ')).filter(Boolean).slice(0,3);
|
|
25
26
|
return `Cloudflare refused ${redact(refusal.operation).slice(0,200)}${said.length ? `: ${said.join('; ')}` : ''}`;
|
|
26
27
|
}
|
|
28
|
+
/** What a migration that failed or stays refused means for the next run, from the
|
|
29
|
+
* error's own `migration` or `migrationClaim` (set by any copy of the kit) and its
|
|
30
|
+
* `d1:<name>` resource; undefined otherwise. */
|
|
31
|
+
export function migrationFailure(error, again = 'rer deploy') {
|
|
32
|
+
const safe = value => String(value).replace(/[^\w. -]/g,'?').slice(0,120);
|
|
33
|
+
const database = /^d1:([a-z0-9-]+)$/.exec(error?.resource ?? '')?.[1];
|
|
34
|
+
const claim = error?.migrationClaim;
|
|
35
|
+
if (typeof claim?.name === 'string') {
|
|
36
|
+
const name = safe(claim.name).replaceAll(' ','?');
|
|
37
|
+
const run = sql => `npx wrangler d1 execute ${database ?? '<database>'} --remote --command "${sql} WHERE name='${name}' AND state='applying'"`;
|
|
38
|
+
const present = (Array.isArray(claim.present) ? claim.present : []).slice(0,5).map(safe);
|
|
39
|
+
return `Migration ${name} in ${database ? `D1 ${database}` : 'its D1 database'} was started but never confirmed, and rer runs it again only when it can prove none of it applied. ${present.length
|
|
40
|
+
? `Some of its changes are present (${present.join(', ')}). Undo them so the database is as it was before ${name}, then run ${again} again: rer checks again and runs it.`
|
|
41
|
+
: `It creates no table, index, view, trigger or column, so rer cannot check whether it applied. If none of its changes are present, delete its claim with ${run('DELETE FROM d1_migrations')}, then run ${again} again.`} If it did apply in full, record that instead with ${run("UPDATE d1_migrations SET state='applied'")}.`;
|
|
42
|
+
}
|
|
43
|
+
if (typeof error?.migration === 'string') return `Migration ${safe(error.migration)} did not apply; the next ${again} runs it again once rer proves none of it applied.`;
|
|
44
|
+
return undefined;
|
|
45
|
+
}
|
|
27
46
|
/** Safe call identity travels with errors from independently vendored kits. */
|
|
28
47
|
export function providerTransport(error, redact = String) {
|
|
29
48
|
const detail = error?.providerTransport;
|
|
@@ -50,10 +69,11 @@ const CREATED_KINDS = {D1:'d1',R2:'r2',KV:'kv',queue:'queues'};
|
|
|
50
69
|
export function deploymentDiagnostics(api, {onCreated} = {}) {
|
|
51
70
|
const created = [];
|
|
52
71
|
const recordCreated = resource => { created.push(resource); onCreated?.(resource); };
|
|
53
|
-
let currentStep = 'prepare deploy';
|
|
72
|
+
let currentStep = 'prepare deploy', currentCall;
|
|
54
73
|
const guard = method => async (path, options) => {
|
|
55
74
|
const step = operation(path,options);
|
|
56
75
|
currentStep = step;
|
|
76
|
+
currentCall = `${method === 'list' ? 'GET' : options?.method ?? 'GET'} ${path.split('?')[0].replace(/(\/r2\/buckets\/[^/]+\/objects)\/.*/, '$1/[object]')}`;
|
|
57
77
|
try {
|
|
58
78
|
const result = await api[method](path,options);
|
|
59
79
|
if (step.startsWith('create ')) recordCreated(`${step.slice(7)}:${options.body.name ?? options.body.queue_name ?? options.body.title}`);
|
|
@@ -61,6 +81,7 @@ export function deploymentDiagnostics(api, {onCreated} = {}) {
|
|
|
61
81
|
} catch (error) {
|
|
62
82
|
// A failed create names the resource it asked for: a lost answer may still have created it.
|
|
63
83
|
if (step.startsWith('create ') && error.resource === undefined) error.resource = `${CREATED_KINDS[step.slice(7)]}:${options.body.name ?? options.body.queue_name ?? options.body.title}`;
|
|
84
|
+
error.providerCall ??= {operation:currentCall,reason:error.message};
|
|
64
85
|
throw recordDeployFailure(error,{step,created:[...created]});
|
|
65
86
|
}
|
|
66
87
|
};
|
|
@@ -72,6 +93,7 @@ export function deploymentDiagnostics(api, {onCreated} = {}) {
|
|
|
72
93
|
},
|
|
73
94
|
annotateFailure(error, step = currentStep) {
|
|
74
95
|
const prior = failures.get(error);
|
|
96
|
+
if (currentCall) error.providerCall ??= {operation:currentCall,reason:error.message};
|
|
75
97
|
// Node filesystem/CLI codes are structural; their messages can hold paths
|
|
76
98
|
// and inputs, so retain them privately for the caller's redaction boundary.
|
|
77
99
|
const cliCode = typeof error.code === 'string' && /^(?:E[A-Z0-9_]{1,63}|health-[a-z-]+)$/.test(error.code) ? error.code : undefined;
|
|
@@ -80,3 +102,26 @@ export function deploymentDiagnostics(api, {onCreated} = {}) {
|
|
|
80
102
|
return error;
|
|
81
103
|
}};
|
|
82
104
|
}
|
|
105
|
+
|
|
106
|
+
/** All provider failure paths share the same safe, portable rendering. */
|
|
107
|
+
export function providerFailure(error, redact = String) {
|
|
108
|
+
const specific = providerRefusal(error,redact) ?? providerTransport(error,redact);
|
|
109
|
+
if (!specific && error?.cause instanceof Error) return providerFailure(error.cause,redact);
|
|
110
|
+
if (specific) return specific;
|
|
111
|
+
const call = error?.providerCall;
|
|
112
|
+
if (typeof call?.operation !== 'string') return undefined;
|
|
113
|
+
const clean = value => redact(String(value)).replace(/[\x00-\x1f\x7f-\x9f]+/g,' ').replace(/\b[a-f0-9]{32}\b/gi,'[REDACTED]').slice(0,300);
|
|
114
|
+
return `Cloudflare call ${clean(call.operation)}: ${clean(call.reason ?? error.message)} (no Cloudflare error code or message supplied)`;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// Asset paths are build inputs; the CLI still redacts them before display.
|
|
118
|
+
export function nameAssetFailure(error, paths, status = error.status ?? error.providerTransport?.status) {
|
|
119
|
+
error.assetFailure = {paths, status};
|
|
120
|
+
return error;
|
|
121
|
+
}
|
|
122
|
+
export function assetFailure(error, redact = String) {
|
|
123
|
+
const detail = error?.assetFailure;
|
|
124
|
+
if (!Array.isArray(detail?.paths)) return undefined;
|
|
125
|
+
const names = detail.paths.slice(0,8).map(path => redact(String(path)).replace(/[\x00-\x1f\x7f-\x9f]+/g,' ').slice(0,300));
|
|
126
|
+
return `Asset ${names.join(', ')}${detail.paths.length > 8 ? ` (+${detail.paths.length - 8} more)` : ''}: ${Number.isInteger(detail.status) ? `HTTP ${detail.status}` : 'no HTTP status (transport failure)'}`;
|
|
127
|
+
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { servedAsset } from './asset-health.mjs';
|
|
1
2
|
import { readinessAPI } from './readiness-window.mjs';
|
|
2
3
|
import { verifyServiceGrants } from './service-grants.mjs';
|
|
3
4
|
import { assert, stable } from './validation.mjs';
|
|
@@ -47,32 +48,10 @@ export async function waitForDeploymentHealth(url, {
|
|
|
47
48
|
throw healthError(`Deployment did not become healthy at ${url} within ${timeoutMs}ms; last result: ${lastResult}.`, lastResult === 'network error' ? 'health-transport-failed' : 'health-http-failed', lastResult);
|
|
48
49
|
}
|
|
49
50
|
|
|
50
|
-
/** An unauthenticated GET of a built file; anything but 200 means the browser gets no assets.
|
|
51
|
-
* After an upload (`windowMs`), an edge still serving the previous version answers 404 for
|
|
52
|
-
* the new build's hashed files: 404, 5xx and transport failures are retried within the window. */
|
|
53
|
-
async function servedAsset(origin, path, request, timeoutMs, {window, windowMs = 0, clock = () => performance.now(), sleep = ms => new Promise(resolve => setTimeout(resolve, ms))} = {}) {
|
|
54
|
-
const start = clock();
|
|
55
|
-
for (;;) {
|
|
56
|
-
let response, failure;
|
|
57
|
-
const remaining = window?.remaining() ?? timeoutMs;
|
|
58
|
-
assert(remaining > 0, 'asset-readiness-timeout');
|
|
59
|
-
try { response = await request(`${origin}/${path.split('/').map(encodeURIComponent).join('/')}`,{redirect:'manual',signal:AbortSignal.timeout(Math.max(1, Math.ceil(Math.min(timeoutMs,remaining)))),headers:{'Cache-Control':'no-cache'}}); }
|
|
60
|
-
catch { failure = 'asset-transport-failed'; }
|
|
61
|
-
if (response) {
|
|
62
|
-
await response.body?.cancel().catch(() => {});
|
|
63
|
-
if (response.status === 200 && !response.redirected) return {path,status:response.status};
|
|
64
|
-
failure = 'asset-http-failed';
|
|
65
|
-
if (response.redirected || response.status !== 404 && response.status < 500) throw new Error(failure);
|
|
66
|
-
}
|
|
67
|
-
if (clock() - start + 2000 > windowMs) throw new Error(failure);
|
|
68
|
-
await sleep(Math.min(2000,window?.remaining() ?? 2000));
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
|
|
72
51
|
/** Authenticated provider readback, then the unauthenticated account-derived /health.
|
|
73
52
|
* Without `readiness`, /health gets the single bounded window of waitForDeploymentHealth.
|
|
74
53
|
* With `readiness` (deploy after an upload) it gets the recorded 90-second wait of health-request.mjs.
|
|
75
|
-
* `asset`, a built file's path,
|
|
54
|
+
* `asset`, a built file's path, must answer 200 after at most one transient replay. */
|
|
76
55
|
export async function verifyHealth({api,plan,receipt,fetch:request=globalThis.fetch,timeoutMs=30000,intervalMs=2000,now,sleep,readiness,asset}) {
|
|
77
56
|
if (readiness?.window) api = readinessAPI(api, readiness.window);
|
|
78
57
|
assert(receipt?.version === 1 && receipt.accountId === api.accountId && receipt.worker === plan.worker,'receipt-target-mismatch');
|
|
@@ -47,7 +47,7 @@ const same = (a, b) => a.length === b.length && a.every((row, index) => row.name
|
|
|
47
47
|
export async function readLedger(api, id, migrations, query) {
|
|
48
48
|
const read = async sql => (await query(api, id, sql))[0].results;
|
|
49
49
|
const tables = (await read(`SELECT name FROM sqlite_master WHERE type='table' AND name IN ('${legacy}','${ledger}')`)).map(row => row.name);
|
|
50
|
-
let old = [], applied = [], transition = '';
|
|
50
|
+
let old = [], applied = [], transition = '', claimed = false;
|
|
51
51
|
if (tables.includes(legacy)) {
|
|
52
52
|
const columns = await read(`SELECT name FROM pragma_table_info('${legacy}')`);
|
|
53
53
|
assert(columns.length === 3 && columns.every(row => ['name','sha256','state'].includes(row.name)), 'invalid-migration-ledger');
|
|
@@ -59,6 +59,10 @@ export async function readLedger(api, id, migrations, query) {
|
|
|
59
59
|
assert(['id','name','applied_at'].every(name => columns.includes(name)), 'invalid-migration-ledger');
|
|
60
60
|
if (columns.includes('sha256') && columns.includes('state')) {
|
|
61
61
|
applied = await read(`SELECT name, sha256, state FROM ${ledger} ORDER BY id`);
|
|
62
|
+
// The last row may claim this build's next migration, exact bytes, whose import
|
|
63
|
+
// did not complete; migrate re-runs it only on proof (migration-retry.mjs).
|
|
64
|
+
const last = applied.at(-1);
|
|
65
|
+
if (!tables.includes(legacy) && last?.state === 'applying' && last.name === migrations[applied.length - 1]?.name && last.sha256 === migrations[applied.length - 1].sha256) claimed = Boolean(applied.pop());
|
|
62
66
|
assert(validPrefix(applied, migrations), 'changed-or-uncertain-migration-history');
|
|
63
67
|
if (tables.includes(legacy)) assert(same(applied, old), 'changed-or-uncertain-migration-history');
|
|
64
68
|
} else {
|
|
@@ -78,7 +82,7 @@ export async function readLedger(api, id, migrations, query) {
|
|
|
78
82
|
applied = old;
|
|
79
83
|
}
|
|
80
84
|
if (tables.includes(legacy)) transition += `DROP TABLE ${legacy};`;
|
|
81
|
-
return { applied, transition, exists:tables.includes(ledger) || tables.includes(legacy) };
|
|
85
|
+
return { applied, transition, claimed, exists:tables.includes(ledger) || tables.includes(legacy) };
|
|
82
86
|
}
|
|
83
87
|
function insert(row) {
|
|
84
88
|
return `INSERT INTO ${ledger}(name,sha256,state) VALUES (${literal(row.name)},${literal(row.sha256)},'applied');\n`;
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { sqlTokens } from './sql-tokens.mjs';
|
|
2
|
+
import { literal } from './migration-ledger.mjs';
|
|
3
|
+
|
|
4
|
+
// A migration claimed `applying` whose import never completed is re-run only on
|
|
5
|
+
// proof that none of it applied. D1 imports the SQL and its completion marker as
|
|
6
|
+
// one atomic file, so one statement that fails when its effect already exists
|
|
7
|
+
// (CREATE without IF NOT EXISTS, ADD COLUMN, RENAME) and whose effect lasts to the
|
|
8
|
+
// end of the file proves it: its effect absent now means no attempt committed, and
|
|
9
|
+
// an earlier attempt that commits late makes the re-run fail whole instead of
|
|
10
|
+
// applying twice. Every such effect must be absent; any present one is refused.
|
|
11
|
+
|
|
12
|
+
const word = token => token?.kind === 'word' ? token.value.toUpperCase() : '';
|
|
13
|
+
const lower = token => token?.kind === 'word' || token?.kind === 'identifier' ? token.value.toLowerCase() : undefined;
|
|
14
|
+
const symbol = (token, value) => token?.kind === 'symbol' && token.value === value;
|
|
15
|
+
|
|
16
|
+
// Trigger bodies split here too; their parts are DML and add or remove no guard.
|
|
17
|
+
function statements(sql) {
|
|
18
|
+
const list = [[]];
|
|
19
|
+
for (const token of sqlTokens(sql)) symbol(token, ';') ? list.push([]) : list.at(-1).push(token);
|
|
20
|
+
return list.filter(statement => statement.length);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// [main.]name at index; anything in another schema is not checked.
|
|
24
|
+
function named(statement, index) {
|
|
25
|
+
if (!symbol(statement[index + 1], '.')) return { name:lower(statement[index]), next:index + 1 };
|
|
26
|
+
return { name:lower(statement[index]) === 'main' ? lower(statement[index + 2]) : undefined, next:index + 3 };
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function create(statement) {
|
|
30
|
+
let index = 1;
|
|
31
|
+
if (['TEMP', 'TEMPORARY'].includes(word(statement[index]))) return undefined;
|
|
32
|
+
if (['UNIQUE', 'VIRTUAL'].includes(word(statement[index]))) index++;
|
|
33
|
+
const type = word(statement[index++]).toLowerCase();
|
|
34
|
+
if (!['table', 'index', 'view', 'trigger'].includes(type) || word(statement[index]) === 'IF') return undefined;
|
|
35
|
+
const target = named(statement, index);
|
|
36
|
+
if (!target.name) return undefined;
|
|
37
|
+
if (type === 'table' || type === 'view') return { type, object:target.name, table:target.name };
|
|
38
|
+
const on = statement.findIndex((token, at) => at >= target.next && word(token) === 'ON');
|
|
39
|
+
const table = on < 0 ? undefined : named(statement, on + 1).name;
|
|
40
|
+
return table && { type, object:target.name, table };
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** The effects of a migration whose absence proves it never applied. */
|
|
44
|
+
export function retryGuards(sql) {
|
|
45
|
+
let guards = [];
|
|
46
|
+
for (const statement of statements(sql)) {
|
|
47
|
+
const first = word(statement[0]);
|
|
48
|
+
if (first === 'CREATE') {
|
|
49
|
+
const guard = create(statement);
|
|
50
|
+
if (guard) guards.push(guard);
|
|
51
|
+
} else if (first === 'ALTER' && word(statement[1]) === 'TABLE') {
|
|
52
|
+
const { name:table, next } = named(statement, 2);
|
|
53
|
+
let index = next;
|
|
54
|
+
const action = word(statement[index++]);
|
|
55
|
+
if (word(statement[index]) === 'COLUMN') index++;
|
|
56
|
+
const column = lower(statement[index]);
|
|
57
|
+
if (!table) guards = [];
|
|
58
|
+
else if (action === 'ADD' && column) guards.push({ table, column });
|
|
59
|
+
else if (action === 'RENAME' && word(statement[next + 1]) === 'TO') {
|
|
60
|
+
const to = named(statement, next + 2).name;
|
|
61
|
+
guards = guards.filter(guard => guard.table !== table);
|
|
62
|
+
if (to) guards.push({ type:'table', object:to, table:to });
|
|
63
|
+
} else if (action === 'RENAME' && column && word(statement[index + 1]) === 'TO' && lower(statement[index + 2])) {
|
|
64
|
+
guards = guards.filter(guard => !(guard.table === table && guard.column === column));
|
|
65
|
+
guards.push({ table, column:lower(statement[index + 2]) });
|
|
66
|
+
} else if (action === 'DROP' && column) guards = guards.filter(guard => !(guard.table === table && guard.column === column));
|
|
67
|
+
else guards = [];
|
|
68
|
+
} else if (first === 'DROP') {
|
|
69
|
+
const type = word(statement[1]);
|
|
70
|
+
const { name } = named(statement, word(statement[2]) === 'IF' ? 4 : 2);
|
|
71
|
+
// A later drop removes the effect it would prove; an unreadable one proves nothing.
|
|
72
|
+
guards = name ? guards.filter(guard => guard.object !== name && !(['TABLE', 'VIEW'].includes(type) && guard.table === name)) : [];
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return guards;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const label = guard => guard.column === undefined ? `${guard.type} ${guard.object}` : `column ${guard.table}.${guard.column}`;
|
|
79
|
+
|
|
80
|
+
/** Refuse unless the claimed migration's effects are provably absent: it then
|
|
81
|
+
* may run again. `read(sql)` returns one SELECT's rows. */
|
|
82
|
+
export async function assertRetryable(read, migration) {
|
|
83
|
+
const guards = retryGuards(migration.sql), present = [];
|
|
84
|
+
for (let start = 0; start < guards.length; start += 50) {
|
|
85
|
+
const part = guards.slice(start, start + 50).map((guard, index) => guard.column === undefined
|
|
86
|
+
? `SELECT ${index} AS guard FROM sqlite_master WHERE type=${literal(guard.type)} AND lower(name)=${literal(guard.object)}`
|
|
87
|
+
: `SELECT ${index} AS guard FROM pragma_table_info(${literal(guard.table)}) WHERE lower(name)=${literal(guard.column)}`);
|
|
88
|
+
for (const row of await read(part.join(' UNION ALL '))) present.push(label(guards[start + row.guard]));
|
|
89
|
+
}
|
|
90
|
+
if (!guards.length || present.length) throw Object.assign(new Error('uncertain-migration-claim'), { migrationClaim:{ name:migration.name, present:[...new Set(present)] } });
|
|
91
|
+
}
|
|
@@ -4,6 +4,7 @@ import { regularBytes } from './artifact.mjs';
|
|
|
4
4
|
import { assertMigrationSqlSafe } from './sql-safety.mjs';
|
|
5
5
|
import { sqlTokens } from './sql-tokens.mjs';
|
|
6
6
|
import { ledger, literal, createLedger, readLedger, recorded } from './migration-ledger.mjs';
|
|
7
|
+
import { assertRetryable } from './migration-retry.mjs';
|
|
7
8
|
export function validateMigrations(migrations) {
|
|
8
9
|
let previous = '';
|
|
9
10
|
for (const migration of migrations) {
|
|
@@ -54,7 +55,7 @@ export async function query(api, id, sql) {
|
|
|
54
55
|
}
|
|
55
56
|
export async function migrate(api, id, migrations, { apply = true, upgrade = apply } = {}) {
|
|
56
57
|
validateMigrations(migrations);
|
|
57
|
-
const { applied, transition, exists } = await readLedger(api, id, migrations, query);
|
|
58
|
+
const { applied, transition, exists, claimed } = await readLedger(api, id, migrations, query);
|
|
58
59
|
const pending = migrations.slice(applied.length);
|
|
59
60
|
assert(apply || pending.length === 0,'pending-migrations');
|
|
60
61
|
if (transition && upgrade) {
|
|
@@ -63,14 +64,17 @@ export async function migrate(api, id, migrations, { apply = true, upgrade = app
|
|
|
63
64
|
assert(!verified.transition && verified.applied.length === applied.length, 'migration-readback-mismatch');
|
|
64
65
|
}
|
|
65
66
|
if (pending.length && !exists) await query(api,id,createLedger);
|
|
66
|
-
for (const migration of pending) {
|
|
67
|
-
// Claim before execution. A
|
|
68
|
-
|
|
67
|
+
for (const [index, migration] of pending.entries()) {
|
|
68
|
+
// Claim before execution. A claim left by an import that did not complete runs
|
|
69
|
+
// again only when its effects are provably absent; otherwise it stays refused.
|
|
70
|
+
if (claimed && index === 0) await assertRetryable(async sql => (await query(api,id,sql))[0].results, migration);
|
|
71
|
+
else await query(api,id,`INSERT INTO ${ledger}(name,sha256,state) VALUES (${literal(migration.name)},${literal(migration.sha256)},'applying')`);
|
|
69
72
|
const last = sqlTokens(migration.sql).at(-1);
|
|
70
73
|
// A second terminator after trailing comments creates an empty D1 statement.
|
|
71
74
|
// Keep the newline even when terminated: a final line comment must end.
|
|
72
75
|
const separator = last?.kind === 'symbol' && last.value === ';' ? '' : '; ';
|
|
73
|
-
await api.importSql(id,`${migration.sql}\n${separator}UPDATE ${ledger} SET state='applied' WHERE name=${literal(migration.name)} AND sha256=${literal(migration.sha256)};`)
|
|
76
|
+
await api.importSql(id,`${migration.sql}\n${separator}UPDATE ${ledger} SET state='applied' WHERE name=${literal(migration.name)} AND sha256=${literal(migration.sha256)};`)
|
|
77
|
+
.catch(error => { error.migration ??= migration.name; throw error; });
|
|
74
78
|
}
|
|
75
79
|
if (pending.length) {
|
|
76
80
|
const final = (await query(api,id,`SELECT name, sha256, state FROM ${ledger} ORDER BY id`))[0].results;
|
|
@@ -8,7 +8,7 @@ import { assert, hexId } from './validation.mjs';
|
|
|
8
8
|
import { regularBytes } from './artifact.mjs';
|
|
9
9
|
import { packageBuild } from './prepare.mjs';
|
|
10
10
|
import { cloudflare } from './provider.mjs';
|
|
11
|
-
import { ownerTarget, ownerPlan, ownerDeploy, ownerHealth, settleOwnerState, sameDeployment, localOnlyBinding, withStateLock, readOwnerState, writeOwnerState } from './owner.mjs';
|
|
11
|
+
import { ownerTarget, ownerWorkerName, ownerPlan, ownerDeploy, ownerHealth, settleOwnerState, sameDeployment, localOnlyBinding, withStateLock, readOwnerState, writeOwnerState } from './owner.mjs';
|
|
12
12
|
import { activeWorker } from './worker.mjs';
|
|
13
13
|
import { ownerSecret } from './owner-secret.mjs';
|
|
14
14
|
import { ownerRemove } from './owner-remove.mjs';
|
|
@@ -46,7 +46,7 @@ export async function main(argv = process.argv.slice(2), env = process.env, {fet
|
|
|
46
46
|
assert(typeof declaration?.project === 'string','missing-owner-project');
|
|
47
47
|
// Removal reads identity before each object DELETE and may span many pages.
|
|
48
48
|
// Bound each response, not the owner's bucket size; failures retain ownership.
|
|
49
|
-
let requestWindow = propagation?.
|
|
49
|
+
let requestWindow = propagation?.uploaded ? propagation.window : undefined;
|
|
50
50
|
const request = fetch;
|
|
51
51
|
fetch = (input, init = {}) => {
|
|
52
52
|
if (!requestWindow?.active) return request(input, init);
|
|
@@ -55,8 +55,11 @@ export async function main(argv = process.argv.slice(2), env = process.env, {fet
|
|
|
55
55
|
const deadline = AbortSignal.timeout(Math.max(1, Math.ceil(remaining)));
|
|
56
56
|
return request(input, {...init,signal:init.signal ? AbortSignal.any([init.signal,deadline]) : deadline});
|
|
57
57
|
};
|
|
58
|
-
const api = deploymentDiagnostics(cloudflare({accountId:env.CLOUDFLARE_ACCOUNT_ID,token:env.CLOUDFLARE_API_TOKEN,fetch,...(command === 'remove' ? {maxRequests:Infinity} : {})}),{onCreated});
|
|
59
|
-
|
|
58
|
+
const api = deploymentDiagnostics(cloudflare({accountId:env.CLOUDFLARE_ACCOUNT_ID,token:env.CLOUDFLARE_API_TOKEN,fetch,...(command === 'remove' ? {maxRequests:Infinity,removal:true} : {})}),{onCreated});
|
|
59
|
+
// Removal needs the recorded account/name identity, not a workers.dev route.
|
|
60
|
+
const target = command === 'remove'
|
|
61
|
+
? {accountId:api.accountId,project:declaration.project,environment:values.environment,worker:ownerWorkerName(declaration.project,values.environment)}
|
|
62
|
+
: await ownerTarget(api,{project:declaration.project,environment:values.environment});
|
|
60
63
|
const statePath = values.state ? resolve(root,values.state) : join(root,'.rer','deploy',`${target.environment}.json`);
|
|
61
64
|
if (command === 'remove') return {ok:true,...await ownerRemove({api,target,statePath}).catch(error => { throw api.annotateFailure(error,'remove'); })};
|
|
62
65
|
if (command === 'secret') {
|
|
@@ -94,7 +97,7 @@ export async function main(argv = process.argv.slice(2), env = process.env, {fet
|
|
|
94
97
|
if (command === 'health') {
|
|
95
98
|
const readiness = {...(healthOptions?.now ? {clock:healthOptions.now} : {}), ...(healthOptions?.sleep ? {sleep:healthOptions.sleep} : {}),
|
|
96
99
|
...healthOptions?.readiness,single:true,totalMs:30000,
|
|
97
|
-
...(propagation ? {window:propagation.window,freshUpload:propagation.
|
|
100
|
+
...(propagation ? {window:propagation.window,freshUpload:propagation.uploaded && propagation.versionId === state.receipt?.provider.versionId} : {})};
|
|
98
101
|
await ownerHealth({api,target,plan,statePath,fetch,healthOptions:{...healthOptions,readiness}}).catch(error=>{throw api.annotateFailure(error,'health');});
|
|
99
102
|
return {ok:true,worker:target.worker,origin:target.origin};
|
|
100
103
|
}
|
|
@@ -110,7 +113,7 @@ export async function main(argv = process.argv.slice(2), env = process.env, {fet
|
|
|
110
113
|
});
|
|
111
114
|
const result = {ok:true,changed:receipt.changed,worker:target.worker,origin:receipt.origin,versionId:receipt.provider.versionId,created,sentSecrets,retiredSecrets,...(state.ownerClaim ? {ownerClaim:state.ownerClaim} : {}),state:statePath};
|
|
112
115
|
const sentTokens = new Map(sentSecrets.map(binding => [binding,env[declaration.secrets.find(item => item.binding === binding)?.environment]]));
|
|
113
|
-
Object.defineProperty(result, 'propagation', {value: {window,freshUpload,sentSecrets,versionId:receipt.provider.versionId,
|
|
116
|
+
Object.defineProperty(result, 'propagation', {value: {window,uploaded:receipt.changed,freshUpload,sentSecrets,versionId:receipt.provider.versionId,
|
|
114
117
|
matchesToken:(binding,token) => sentTokens.has(binding) && sentTokens.get(binding) === token}});
|
|
115
118
|
rememberReadiness(result);
|
|
116
119
|
return result;
|