@runeditrun/rer 0.2.22 → 0.2.24

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 (72) hide show
  1. package/cli/README.md +19 -0
  2. package/cli/bounded.mjs +5 -1
  3. package/cli/delivery-declarations.mjs +2 -2
  4. package/cli/demo-host.mjs +1 -1
  5. package/cli/deploy-command.mjs +7 -5
  6. package/cli/deploy-inputs.mjs +4 -1
  7. package/cli/deploy-observation.mjs +20 -2
  8. package/cli/deploy-owner.mjs +12 -7
  9. package/cli/deploy.mjs +9 -6
  10. package/cli/deployment-record.mjs +52 -0
  11. package/cli/local.mjs +11 -3
  12. package/cli/managed-server.mjs +2 -1
  13. package/cli/output.mjs +1 -1
  14. package/cli/production-auth.mjs +7 -27
  15. package/cli/production-mail.mjs +72 -0
  16. package/cli/signin.mjs +11 -6
  17. package/cli/site.mjs +2 -2
  18. package/global/CHANGELOG.md +25 -0
  19. package/modules/container/README.md +8 -3
  20. package/modules/container/adapters/d1.mjs +1 -1
  21. package/modules/container/build.mjs +1 -1
  22. package/modules/container/host.mjs +7 -4
  23. package/modules/container/transfer.mjs +0 -1
  24. package/modules/container/websockets.mjs +54 -0
  25. package/modules/container/worker-engine.mjs +3 -3
  26. package/modules/demo/README.md +4 -0
  27. package/modules/demo/label.mjs +7 -4
  28. package/modules/demo/responses.mjs +1 -1
  29. package/modules/demo-host/README.md +34 -9
  30. package/modules/demo-host/capacity.mjs +2 -2
  31. package/modules/demo-host/container.mjs +19 -7
  32. package/modules/demo-host/placement.mjs +19 -5
  33. package/modules/demo-host/plan.mjs +11 -6
  34. package/modules/demo-host/start-script.mjs +2 -1
  35. package/modules/demo-host/startup.mjs +3 -3
  36. package/modules/demo-host/worker.mjs +36 -13
  37. package/modules/deploy-kit/README.md +35 -2
  38. package/modules/deploy-kit/diagnostics.mjs +16 -1
  39. package/modules/deploy-kit/owner-cli.mjs +3 -3
  40. package/modules/deploy-kit/owner-remove.mjs +23 -5
  41. package/modules/deploy-kit/owner-signin.mjs +1 -1
  42. package/modules/deploy-kit/pagination.mjs +99 -0
  43. package/modules/deploy-kit/provider.mjs +30 -70
  44. package/modules/deploy-kit/r2-objects.mjs +6 -17
  45. package/modules/deploy-kit/site-readback.mjs +67 -0
  46. package/modules/deploy-kit/site.mjs +17 -31
  47. package/modules/deploy-kit/uploaded-worker.mjs +9 -3
  48. package/modules/erasure/queue.mjs +0 -2
  49. package/modules/job-queue/cli.mjs +1 -1
  50. package/modules/mail/README.md +14 -5
  51. package/modules/mail/src/adapters/rer/contract.mjs +33 -5
  52. package/modules/mail/src/adapters/rer/index.mjs +4 -1
  53. package/modules/mail/src/core/transport.mjs +9 -3
  54. package/modules/operator-auth/README.md +5 -0
  55. package/modules/operator-auth/identity/email-link/index.mjs +5 -0
  56. package/modules/operator-auth/identity/oidc/jwt.mjs +1 -1
  57. package/modules/portability/bridge.mjs +3 -3
  58. package/modules/portability/catalogue.mjs +1 -1
  59. package/modules/portability/cli-options.mjs +1 -1
  60. package/modules/portability/legacy-files.mjs +1 -1
  61. package/modules/portability/legacy-safety.mjs +3 -3
  62. package/modules/portability/payloads.mjs +1 -1
  63. package/modules/release/source.mjs +1 -1
  64. package/onboarding/local/operation.mjs +4 -2
  65. package/onboarding/local/owner.mjs +9 -3
  66. package/onboarding/local/state-files.mjs +17 -0
  67. package/onboarding/process-diagnostics.mjs +25 -5
  68. package/package.json +1 -1
  69. package/scripts/publish-check/README.md +5 -2
  70. package/scripts/publish-check/content_identities.py +1 -1
  71. package/scripts/publish-check/json_identity.py +10 -1
  72. package/scripts/publish-check/public_hygiene.py +30 -4
@@ -41,7 +41,10 @@ async function human(env, token, ip) {
41
41
 
42
42
  const wantsJSON = request => request.headers.get('accept') === 'application/json';
43
43
  function refusal(request, env, status, notice, headers = {}) {
44
- return wantsJSON(request) ? Response.json({ notice }, { status, headers: { 'cache-control': 'no-store', ...headers } }) : startPage(env, { status, notice, headers });
44
+ const correlation = headers['x-rer-demo-correlation'] ?? crypto.randomUUID();
45
+ headers = { 'cache-control': 'no-store', ...headers, 'x-rer-demo-correlation': correlation };
46
+ report(correlation, Date.now(), 'refused', { status });
47
+ return wantsJSON(request) ? Response.json({ notice }, { status, headers }) : startPage(env, { status, notice, headers });
45
48
  }
46
49
 
47
50
  function progress(request, env, id, state) {
@@ -61,17 +64,23 @@ function progress(request, env, id, state) {
61
64
  return new Response(null, { status: 303, headers });
62
65
  }
63
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.
64
70
  async function status(request, env) {
65
71
  const id = demoId(request, env);
66
72
  if (!id) return refusal(request, env, 410, 'This demo has ended. Start a new demo.');
67
- try { return progress(request, env, id, await bounded(env.DEMO.get(id).status(), 2000)); }
68
- catch { return refusal(request, env, 503, 'The demo could not be reached. Try again.'); }
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);
69
77
  }
70
78
 
71
79
  async function start(request, env, ctx) {
72
- const refuse = (status, notice, headers = {}) => refusal(request, env, status, notice, headers);
80
+ const correlation = crypto.randomUUID();
81
+ const refuse = (status, notice, headers = {}) => refusal(request, env, status, notice, { 'x-rer-demo-correlation': correlation, ...headers });
73
82
  if (request.headers.get('origin') !== env.DEMO_ORIGIN) return refuse(403, 'Start the demo from this page.');
74
- let demo, correlation;
83
+ let demo;
75
84
  const started = Date.now();
76
85
  try {
77
86
  const ip = request.headers.get('cf-connecting-ip') ?? '';
@@ -89,12 +98,26 @@ async function start(request, env, ctx) {
89
98
  const capacity = env.CAPACITY.get(env.CAPACITY.idFromName('capacity'));
90
99
  // A lost handoff occupies a place for at most a minute. The object extends
91
100
  // it to the demo's end only after health and owner admission succeed.
92
- if (!await bounded(capacity.acquire(id.toString(), Date.now() + 60_000, Number(env.DEMO_MAX_INSTANCES)), 2000)) {
101
+ if (!await bounded(capacity.acquire(id.toString(), Date.now() + 60_000, Number(env.DEMO_MAX_INSTANCES), correlation), 2000)) {
93
102
  return refuse(503, 'Every demo is in use right now. Try again in a few minutes.');
94
103
  }
95
- demo = env.DEMO.get(id); correlation = crypto.randomUUID();
96
- const state = await bounded(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) }), 2000);
97
- return progress(request, env, id, { ...state, correlation });
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 });
106
+ demo = env.DEMO.get(id);
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 });
98
121
  } catch (error) {
99
122
  report(correlation, started, 'failed', { error: category(error) });
100
123
  // Cleanup runs independently of the response: retries never wait on it.
@@ -130,10 +153,10 @@ export default {
130
153
  // The JSON start leaves the browser on /. Reloading it must keep the
131
154
  // pending copy on the progress page until health and owner claim finish.
132
155
  if (primary && url.pathname === '/' && ['GET', 'HEAD'].includes(request.method)) {
133
- try {
134
- const state = await bounded(env.DEMO.get(id).status(), 2000);
135
- if (state.phase === 'starting') return progress(request, env, id, state);
136
- } catch { return refusal(request, env, 503, 'The demo could not be reached. Try again.'); }
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);
137
160
  }
138
161
  return env.DEMO.get(id).fetch(request);
139
162
  }
@@ -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. The real path is tested only against the fake Cloudflare API in `tests/site-provider.mjs`, where every zone ruleset path answers 403; it has not run against a live account.
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
 
@@ -290,7 +290,11 @@ budget, and no request starts at the deadline. An unchanged repeat uses the
290
290
  existing library window and does not record first-upload readiness attempts.
291
291
  The standalone CLI `health` command sends a single request; only an answer from
292
292
  another Worker version (an edge still converging after a secret change) is asked
293
- again, within 30 seconds. Library `verifyHealth`
293
+ again, within 30 seconds. Immediate post-upload readback also waits through an
294
+ HTTPS workers.dev 404 whose complete body is `error code: 1042`, including after
295
+ a changed-source redeploy, within the original upload deadline and the health
296
+ command's 30-second bound. This does not grant first-creation setup-token replay
297
+ permission to a redeploy. Library `verifyHealth`
294
298
  retains the existing window, retrying only 404, 503 and network failures for
295
299
  30 seconds by default.
296
300
 
@@ -407,3 +411,32 @@ narrowing where Cloudflare omits service-binding `props` from settings/version
407
411
  readback. Ordinary `allowedHosts` still maps to `props` and retains strict
408
412
  readback; missing props are never silently accepted. See the
409
413
  [egress service guide](../egress-service/README.md).
414
+
415
+ ### List completion and deployment failures
416
+
417
+ Every Cloudflare list read uses `pagination.mjs`, including reads through
418
+ `request` (Worker versions, deployments, secrets and schedules, consumers,
419
+ domains, zones, routes and Container applications), account inventories and streaming R2 object pages.
420
+ The endpoint's strategy chooses request parameters; the shared reader decides
421
+ completion. Missing/null `result_info` on a short requested-limit page is
422
+ terminal. Missing/empty cursors and reached `total_pages` are terminal unless
423
+ other metadata explicitly advertises unread rows. Continuation cursors and page
424
+ counts are followed. Contradictory totals, repeated cursors, malformed metadata,
425
+ request limits and a truncated reply with no continuation still refuse.
426
+
427
+ `provider-incomplete-pagination` identifies a list with insufficient continuation
428
+ information, rather than a Cloudflare error code. CLI failures name its GET path;
429
+ Cloudflare error codes/messages are printed when the provider supplied them, and
430
+ otherwise explicitly identified as unavailable. Resolve the named call's cause
431
+ and rerun the printed command with the same account and environment; preserve
432
+ the ownership record. Removal detaches all consumers of an owned queue before
433
+ queue deletion and polls its listing for absence without repeating the DELETE.
434
+
435
+ `owner-claim-failed` is the hosted bootstrap failure, after an app may already
436
+ have been deployed. The CLI retains the observed POST path, HTTP status and
437
+ application error code even when a function hook throws a generic failure.
438
+ Correct owner authorization or token inputs and rerun deploy; unknown supplied
439
+ tokens should be left unset so rer issues a fresh one. The existing owner is
440
+ kept, and a successful claim retires the one-time token. A hook that fails
441
+ without an HTTP exchange can only supply its own cause. Seeds copy this guide
442
+ and `RUN-LOCALLY.md` for these recovery instructions.
@@ -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
@@ -50,10 +51,11 @@ const CREATED_KINDS = {D1:'d1',R2:'r2',KV:'kv',queue:'queues'};
50
51
  export function deploymentDiagnostics(api, {onCreated} = {}) {
51
52
  const created = [];
52
53
  const recordCreated = resource => { created.push(resource); onCreated?.(resource); };
53
- let currentStep = 'prepare deploy';
54
+ let currentStep = 'prepare deploy', currentCall;
54
55
  const guard = method => async (path, options) => {
55
56
  const step = operation(path,options);
56
57
  currentStep = step;
58
+ currentCall = `${method === 'list' ? 'GET' : options?.method ?? 'GET'} ${path.split('?')[0].replace(/(\/r2\/buckets\/[^/]+\/objects)\/.*/, '$1/[object]')}`;
57
59
  try {
58
60
  const result = await api[method](path,options);
59
61
  if (step.startsWith('create ')) recordCreated(`${step.slice(7)}:${options.body.name ?? options.body.queue_name ?? options.body.title}`);
@@ -61,6 +63,7 @@ export function deploymentDiagnostics(api, {onCreated} = {}) {
61
63
  } catch (error) {
62
64
  // A failed create names the resource it asked for: a lost answer may still have created it.
63
65
  if (step.startsWith('create ') && error.resource === undefined) error.resource = `${CREATED_KINDS[step.slice(7)]}:${options.body.name ?? options.body.queue_name ?? options.body.title}`;
66
+ error.providerCall ??= {operation:currentCall,reason:error.message};
64
67
  throw recordDeployFailure(error,{step,created:[...created]});
65
68
  }
66
69
  };
@@ -72,6 +75,7 @@ export function deploymentDiagnostics(api, {onCreated} = {}) {
72
75
  },
73
76
  annotateFailure(error, step = currentStep) {
74
77
  const prior = failures.get(error);
78
+ if (currentCall) error.providerCall ??= {operation:currentCall,reason:error.message};
75
79
  // Node filesystem/CLI codes are structural; their messages can hold paths
76
80
  // and inputs, so retain them privately for the caller's redaction boundary.
77
81
  const cliCode = typeof error.code === 'string' && /^(?:E[A-Z0-9_]{1,63}|health-[a-z-]+)$/.test(error.code) ? error.code : undefined;
@@ -80,3 +84,14 @@ export function deploymentDiagnostics(api, {onCreated} = {}) {
80
84
  return error;
81
85
  }};
82
86
  }
87
+
88
+ /** All provider failure paths share the same safe, portable rendering. */
89
+ export function providerFailure(error, redact = String) {
90
+ const specific = providerRefusal(error,redact) ?? providerTransport(error,redact);
91
+ if (!specific && error?.cause instanceof Error) return providerFailure(error.cause,redact);
92
+ if (specific) return specific;
93
+ const call = error?.providerCall;
94
+ if (typeof call?.operation !== 'string') return undefined;
95
+ const clean = value => redact(String(value)).replace(/[\x00-\x1f\x7f-\x9f]+/g,' ').replace(/\b[a-f0-9]{32}\b/gi,'[REDACTED]').slice(0,300);
96
+ return `Cloudflare call ${clean(call.operation)}: ${clean(call.reason ?? error.message)} (no Cloudflare error code or message supplied)`;
97
+ }
@@ -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?.freshUpload ? propagation.window : undefined;
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);
@@ -94,7 +94,7 @@ export async function main(argv = process.argv.slice(2), env = process.env, {fet
94
94
  if (command === 'health') {
95
95
  const readiness = {...(healthOptions?.now ? {clock:healthOptions.now} : {}), ...(healthOptions?.sleep ? {sleep:healthOptions.sleep} : {}),
96
96
  ...healthOptions?.readiness,single:true,totalMs:30000,
97
- ...(propagation ? {window:propagation.window,freshUpload:propagation.freshUpload && propagation.versionId === state.receipt?.provider.versionId} : {})};
97
+ ...(propagation ? {window:propagation.window,freshUpload:propagation.uploaded && propagation.versionId === state.receipt?.provider.versionId} : {})};
98
98
  await ownerHealth({api,target,plan,statePath,fetch,healthOptions:{...healthOptions,readiness}}).catch(error=>{throw api.annotateFailure(error,'health');});
99
99
  return {ok:true,worker:target.worker,origin:target.origin};
100
100
  }
@@ -110,7 +110,7 @@ export async function main(argv = process.argv.slice(2), env = process.env, {fet
110
110
  });
111
111
  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
112
  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,
113
+ Object.defineProperty(result, 'propagation', {value: {window,uploaded:receipt.changed,freshUpload,sentSecrets,versionId:receipt.provider.versionId,
114
114
  matchesToken:(binding,token) => sentTokens.has(binding) && sentTokens.get(binding) === token}});
115
115
  rememberReadiness(result);
116
116
  return result;
@@ -1,10 +1,9 @@
1
1
  import { r2ObjectPages, r2ObjectPath } from './r2-objects.mjs';
2
2
  import { rm } from 'node:fs/promises';
3
- import { assert } from './validation.mjs';
3
+ import { assert, hexId } from './validation.mjs';
4
4
  import { resourceKinds, findResource } from './resources.mjs';
5
5
  import { activeWorker } from './worker.mjs';
6
6
  import { deleteWorker } from './teardown.mjs';
7
- import { workerConsumerScript } from './queue-consumer.mjs';
8
7
  import { query } from './migrations.mjs';
9
8
  import { KINDS, readOwnerState, writeOwnerState, withStateLock, withScopeHints, withSettledLists, workerSettings, sameDeployment, resourceId, checkResourceIdentity, refuse, settlePendingSecret } from './owner.mjs';
10
9
 
@@ -75,7 +74,8 @@ export async function ownerRemove({api: provider, target, statePath, clock, slee
75
74
  // A Worker that consumes one of these queues is released first.
76
75
  for (const queue of doomed.filter(item => item.kind === 'queues')) {
77
76
  for (const consumer of await api.list(`/queues/${queue.id}/consumers`, {pagination:'single'})) {
78
- if (workerConsumerScript(consumer) === target.worker) await api.request(`/queues/${queue.id}/consumers/${consumer.consumer_id}`, {method:'DELETE'});
77
+ assert(hexId(consumer.consumer_id), 'invalid-consumer-id');
78
+ await api.request(`/queues/${queue.id}/consumers/${consumer.consumer_id}`, {method:'DELETE'});
79
79
  }
80
80
  }
81
81
  if (worker) { await deleteWorker({api, worker, ...(clock ? {clock} : {}), ...(sleep ? {sleep} : {})}); removed.push(`worker:${worker}`); }
@@ -83,10 +83,13 @@ export async function ownerRemove({api: provider, target, statePath, clock, slee
83
83
  for (const item of doomed) {
84
84
  try {
85
85
  if (item.kind === 'r2') await removeBucket(api, item);
86
- else await remove[item.kind](api, item.id, item.name);
86
+ else {
87
+ await remove[item.kind](api, item.id, item.name);
88
+ if (item.kind === 'queues') await waitForQueueRemoval(api,item,{clock,sleep});
89
+ }
87
90
  removed.push(`${item.kind}:${item.name}`);
88
91
  }
89
- catch (error) { failed.push({resource:`${item.kind}:${item.name}`, code:error.message}); }
92
+ catch (error) { failed.push({resource:`${item.kind}:${item.name}`, code:error.message, error}); }
90
93
  }
91
94
  // Read back: nothing this environment created is still listed.
92
95
  const remaining = [];
@@ -137,3 +140,18 @@ async function removeBucket(api, {name, entry}) {
137
140
  await checkBucket(api, name, entry);
138
141
  await api.request(`/r2/buckets/${name}`, {method:'DELETE'});
139
142
  }
143
+
144
+ // Queue deletion and its account listing can settle separately. Finish the
145
+ // readback within this removal invocation, without replaying the DELETE.
146
+ async function waitForQueueRemoval(api, item, {clock = () => performance.now(), sleep = ms => new Promise(resolve => setTimeout(resolve,ms))}) {
147
+ const start = clock();
148
+ for (;;) {
149
+ if (!(await resourceKinds.queues.list(api)).some(row => row.queue_id === item.id)) return;
150
+ if (clock() - start >= 60000) {
151
+ const error = new Error('queue-removal-not-confirmed');
152
+ error.providerCall = {operation:'GET /queues',reason:'Cloudflare still lists the deleted queue after 60 seconds'};
153
+ throw error;
154
+ }
155
+ await sleep(500);
156
+ }
157
+ }
@@ -18,7 +18,7 @@ export async function ownerSignin({ api, target, statePath, clock = Date.now })
18
18
  const ids = Object.values(state.owned.d1).filter(entry => entry.status === 'created' && entry.id).map(entry => entry.id);
19
19
  const holding = [];
20
20
  for (const id of ids) if ((await query(api, id, "SELECT name FROM sqlite_master WHERE type='table' AND name='oa_owner_signins'"))[0].results.length) holding.push(id);
21
- assert(holding.length > 0, 'owner-signin-unsupported');
21
+ if (!holding.length) throw Object.assign(new Error('owner-signin-unsupported'), { origin: target.origin });
22
22
  assert(holding.length === 1, 'owner-signin-ambiguous');
23
23
  const token = randomBytes(32).toString('base64url'), id = randomUUID(), now = clock();
24
24
  // The recorded owner: the claimed (bootstrap) owner first, as operator-auth's own owner() reads it.
@@ -0,0 +1,99 @@
1
+ import { assert } from './validation.mjs';
2
+
3
+ // Request strategies describe the wire parameters only. Every endpoint uses
4
+ // this reader to decide whether a reply actually has another page.
5
+ export async function* providerPages(envelope, path, {field, pagination = 'page', zone = false, signal} = {}) {
6
+ assert(['page','cursor','r2-bounded','single'].includes(pagination), 'invalid-list-pagination');
7
+ assert(pagination !== 'r2-bounded' || path === '/r2/buckets' && field === 'buckets', 'invalid-r2-bounded-list');
8
+ const cursorMode = pagination === 'cursor' || pagination === 'r2-bounded';
9
+ const url = new URL(path, 'https://api.cloudflare.com');
10
+ const perPage = Number(url.searchParams.get('per_page') ?? url.searchParams.get('limit') ?? (cursorMode ? 1000 : pagination === 'single' ? 10000 : 100));
11
+ assert(Number.isSafeInteger(perPage) && perPage > 0, 'invalid-list-pagination');
12
+ let cursor, total, count = 0, pageMode = false;
13
+ const seen = new Set();
14
+ for (let page = 1; page <= 100; page++) {
15
+ const query = new URLSearchParams(url.searchParams);
16
+ if (cursor) {
17
+ query.delete('page');
18
+ if (pagination !== 'single' && !query.has('limit')) query.set('per_page',String(perPage));
19
+ query.set('cursor',cursor);
20
+ }
21
+ else if ((pageMode || !cursorMode) && (pagination !== 'single' || page > 1)) query.set('page',String(page));
22
+ if (pagination !== 'single' && !query.has('limit')) query.set('per_page',String(perPage));
23
+ const requestPath = url.pathname + (query.size ? `?${query}` : '');
24
+ let data, rows, next;
25
+ try {
26
+ data = await envelope(requestPath, {zone,signal});
27
+ rows = field ? data.result?.[field] : data.result;
28
+ assert(Array.isArray(rows), 'provider-invalid-list');
29
+ assert(rows.length <= perPage, 'provider-partial-list');
30
+ count += rows.length;
31
+ const info = data.result_info;
32
+ if (info == null) {
33
+ assert(rows.length < perPage, 'provider-incomplete-pagination');
34
+ assert(total === undefined || count === total, 'provider-partial-list');
35
+ } else {
36
+ assert(typeof info === 'object' && !Array.isArray(info), 'provider-incomplete-pagination');
37
+ if (info.per_page !== undefined) {
38
+ assert(Number.isSafeInteger(info.per_page) && info.per_page > 0 && info.per_page <= perPage, 'provider-incomplete-pagination');
39
+ assert(rows.length <= info.per_page, 'provider-partial-list');
40
+ }
41
+ if (info.count !== undefined) assert(info.count === rows.length, 'provider-partial-list');
42
+ if (info.page !== undefined) assert(info.page === page, 'provider-incomplete-pagination');
43
+ if (info.total_count !== undefined) {
44
+ assert(Number.isSafeInteger(info.total_count) && info.total_count >= 0, 'provider-incomplete-pagination');
45
+ total ??= info.total_count;
46
+ assert(total === info.total_count, 'provider-pagination-changed');
47
+ assert(count <= total, 'provider-partial-list');
48
+ }
49
+ if (info.total_pages !== undefined) assert(Number.isSafeInteger(info.total_pages) && info.total_pages >= 0 && (info.total_pages > 0 || count === 0), 'provider-incomplete-pagination');
50
+ if (info.is_truncated !== undefined) assert(typeof info.is_truncated === 'boolean', 'provider-incomplete-pagination');
51
+ assert(!info.delimited || Array.isArray(info.delimited) && !info.delimited.length, 'provider-incomplete-pagination');
52
+ assert(info.cursor == null || typeof info.cursor === 'string', 'provider-invalid-cursor');
53
+ const morePages = info.total_pages !== undefined && page < info.total_pages;
54
+ const moreRows = total !== undefined && count < total;
55
+ if (info.cursor) {
56
+ assert(info.is_truncated !== false, 'provider-invalid-cursor');
57
+ assert((info.total_pages === undefined || morePages) && (total === undefined || moreRows), 'provider-partial-list');
58
+ assert(info.cursor.length <= 4096 && /^[a-zA-Z0-9_+\/=-]+$/.test(info.cursor) && !seen.has(info.cursor), 'provider-invalid-cursor');
59
+ seen.add(info.cursor); next = info.cursor;
60
+ } else if (info.is_truncated === true) {
61
+ throw new Error('provider-incomplete-pagination');
62
+ } else if (cursorMode && moreRows && !morePages) {
63
+ throw new Error('provider-partial-list');
64
+ } else if (morePages || moreRows) {
65
+ assert(info.total_pages === undefined || morePages, 'provider-partial-list');
66
+ next = true;
67
+ } else {
68
+ assert(total === undefined || count === total, 'provider-partial-list');
69
+ }
70
+ if (next) assert(rows.length > 0 || morePages || typeof next === 'string', 'provider-partial-list');
71
+ }
72
+ } catch (error) {
73
+ // No response bodies, query values, account IDs or cursors in diagnostics.
74
+ error.providerCall ??= {operation:`GET ${url.pathname}`,reason:error.message};
75
+ throw error;
76
+ }
77
+ yield {rows,data};
78
+ if (!next) return;
79
+ cursor = typeof next === 'string' ? next : undefined;
80
+ pageMode = next === true;
81
+ }
82
+ const error = new Error('provider-pagination-limit');
83
+ error.providerCall = {operation:`GET ${url.pathname}`,reason:'Cloudflare continued advertising pages beyond the request limit'};
84
+ throw error;
85
+ }
86
+
87
+ // Keep result wrappers (versions/items, schedules, deployments) for existing kit
88
+ // callers while ensuring their GETs cannot bypass pagination. Query filters stay
89
+ // on every request; writes never pass through the reader.
90
+ export function providerListShape(path) {
91
+ const base = path.split('?')[0];
92
+ if (base === '/r2/buckets') return {field:'buckets',pagination:'cursor'};
93
+ if (/^\/r2\/buckets\/[^/]+\/objects$/.test(base) || /^\/storage\/kv\/namespaces\/[^/]+\/keys$/.test(base)) return {pagination:'cursor'};
94
+ const field = /\/workers\/scripts\/[^/]+\/(versions|deployments|schedules)$/.exec(base)?.[1];
95
+ if (field) return {field:field === 'versions' ? 'items' : field,...(field === 'schedules' ? {pagination:'single'} : {})};
96
+ if (['/d1/database','/storage/kv/namespaces','/queues','/workers/durable_objects/namespaces','/workers/domains','/zones','/containers/applications'].includes(base)) return {};
97
+ if (['/workers/scripts','/d1/database','/storage/kv/namespaces','/queues','/workers/durable_objects/namespaces','/workers/domains','/zones'].includes(base) || /^\/queues\/[^/]+\/consumers$/.test(base) || /^\/workers\/scripts\/[^/]+\/secrets$/.test(base) || /^\/zones\/[^/]+\/workers\/routes$/.test(base)) return {pagination:'single'};
98
+ return undefined;
99
+ }
@@ -1,3 +1,4 @@
1
+ import { providerPages, providerListShape } from './pagination.mjs';
1
2
  import { deleteR2Object } from './r2-object-transport.mjs';
2
3
  import { isR2ObjectDelete } from './r2-objects.mjs';
3
4
  import { recordDeployFailure } from './diagnostics.mjs';
@@ -30,7 +31,7 @@ export function cloudflare({ accountId, token, fetch: request = globalThis.fetch
30
31
  assert(typeof token === 'string' && token.trim() && !/[\r\n]/.test(token), 'invalid-cloudflare-api-token');
31
32
  let requests = 0;
32
33
  // `zone` scope: the zone-level API (/zones/...), for a run site's zone lookup and redirect rule.
33
- async function envelope(path, { method = 'GET', body, assetToken, zone = false, signal: callerSignal } = {}) {
34
+ async function rawEnvelope(path, { method = 'GET', body, assetToken, zone = false, signal: callerSignal } = {}) {
34
35
  assert(typeof path === 'string' && (isR2ObjectDelete(path, method) || /^\/[a-z0-9/_?=&.,%-]+$/i.test(path) && !path.includes('..') && !path.split('?')[0].includes('%')), 'invalid-provider-path');
35
36
  assert(!zone || /^\/zones(?:[/?]|$)/.test(path), 'invalid-provider-path');
36
37
  assert(++requests <= maxRequests, 'provider-request-budget');
@@ -69,7 +70,7 @@ export function cloudflare({ accountId, token, fetch: request = globalThis.fetch
69
70
  const bounded = text => redactAuth(text).replace(/[\x00-\x1f\x7f-\x9f]+/g,' ').trim().slice(0,300);
70
71
  const said = (Array.isArray(payload?.errors) ? payload.errors : []).filter(x => Number.isSafeInteger(x?.code) || typeof x?.message === 'string').slice(0,3)
71
72
  .map(x => ({...(Number.isSafeInteger(x.code) ? {code:x.code} : {}),...(typeof x.message === 'string' ? {message:bounded(x.message)} : {})}));
72
- error.providerRefusal = {operation:`${method} ${path.split('?')[0].replace(/(\/r2\/buckets\/[^/]+\/objects)\/.*/, '$1/[object]')}`,errors:said.length || payload !== undefined || !output.trim() ? said : [{message:bounded(output.trim().split('\n')[0])}]};
73
+ error.providerRefusal = {operation:redactCredentials(operation),errors:said.length || payload !== undefined || !output.trim() ? said : [{message:bounded(output.trim().split('\n')[0])}]};
73
74
  throw recordDeployFailure(error,{providerCodes:(Array.isArray(payload?.errors) ? payload.errors : []).filter(x => Number.isSafeInteger(x?.code)).map(x=>x.code).slice(0,8),
74
75
  output:redactAuth(Array.isArray(payload?.errors) ? payload.errors.filter(x=>typeof x?.message==='string').map(x=>x.message).join('\n') : output)});
75
76
  }
@@ -80,79 +81,38 @@ export function cloudflare({ accountId, token, fetch: request = globalThis.fetch
80
81
  // D1 can fail inside a successful HTTP/API envelope. Read only typed
81
82
  // error fields; query results and metadata are never diagnostic output.
82
83
  const errors = failed.flatMap(row => [typeof row.error === 'string' ? {message:row.error} : row.error, ...(Array.isArray(row.errors) ? row.errors : [])]);
83
- throw recordDeployFailure(new Error('d1-query-failed'),{providerCodes:errors.filter(error=>Number.isSafeInteger(error?.code)).map(error=>error.code).slice(0,8),
84
+ const error = new Error('d1-query-failed');
85
+ error.providerRefusal = {operation:redactCredentials(operation),errors:errors.filter(item => Number.isSafeInteger(item?.code) || typeof item?.message === 'string').map(item => ({code:item.code,message:typeof item.message === 'string' ? redactAuth(item.message).slice(0,300) : undefined})).slice(0,3)};
86
+ throw recordDeployFailure(error,{providerCodes:errors.filter(error=>Number.isSafeInteger(error?.code)).map(error=>error.code).slice(0,8),
84
87
  output:redactAuth(errors.filter(error=>typeof error?.message==='string').map(error=>error.message).join('\n'))});
85
88
  }
86
89
  }
87
90
  return payload;
88
91
  }
89
- async function list(path, { field, pagination = 'page' } = {}) {
90
- if (pagination === 'single') {
91
- assert(path === '/workers/scripts' || /^\/queues\/[a-f0-9]{32}\/consumers$/.test(path),'invalid-single-list');
92
- const data = await envelope(path);
93
- assert(Array.isArray(data.result) && data.result.length <= 10000,'provider-invalid-list');
94
- const info = data.result_info;
95
- if (info != null) {
96
- assert(typeof info === 'object' && !Array.isArray(info),'provider-incomplete-pagination');
97
- for (const key of ['count','total_count']) if (Object.hasOwn(info,key)) assert(info[key] === data.result.length,'provider-partial-list');
98
- assert([undefined,null,''].includes(info.cursor) &&
99
- (info.total_pages === undefined || info.total_pages === 1 || info.total_pages === 0 && data.result.length === 0) &&
100
- (info.page === undefined || info.page === 1),'provider-incomplete-pagination');
101
- }
102
- return data.result;
92
+ async function envelope(path, options = {}) {
93
+ try { return await rawEnvelope(path,options); }
94
+ catch (error) {
95
+ error.providerCall ??= {operation:`${options.method ?? 'GET'} ${path.split('?')[0].replace(/(\/r2\/buckets\/[^/]+\/objects)\/.*/, '$1/[object]')}`,reason:error.message};
96
+ throw error;
103
97
  }
104
- assert(['page','cursor','r2-bounded'].includes(pagination),'invalid-list-pagination');
105
- const r2Bounded = pagination === 'r2-bounded';
106
- assert(!r2Bounded || path === '/r2/buckets' && field === 'buckets', 'invalid-r2-bounded-list');
107
- const result = []; const seen = new Set(); let cursor; let total;
108
- for (let page = 1; page <= 100; page++) {
109
- const suffix = r2Bounded || pagination === 'cursor' ? (path === '/r2/buckets' ? `?per_page=1000${cursor ? `&cursor=${encodeURIComponent(cursor)}` : ''}` : (cursor ? `?cursor=${encodeURIComponent(cursor)}` : '')) : `?page=${page}&per_page=100`;
110
- const data = await envelope(path + suffix); const rows = field ? data.result?.[field] : data.result;
111
- assert(Array.isArray(rows), 'provider-invalid-list');
112
- result.push(...rows); assert(result.length <= 10000, 'provider-list-too-large');
113
- if (r2Bounded) {
114
- const info = data.result_info;
115
- assert(rows.length <= 1000, 'provider-partial-list');
116
- // Only this endpoint's explicit bounded strategy uses the requested
117
- // limit when metadata is absent. Never invent metadata for other modes.
118
- if (info === undefined || info === null) {
119
- assert(rows.length < 1000, 'provider-incomplete-pagination');
120
- assert(total === undefined || result.length === total, 'provider-partial-list');
121
- return result;
122
- }
123
- assert(typeof info === 'object' && !Array.isArray(info) && Number.isSafeInteger(info.per_page) && info.per_page > 0 && info.per_page <= 1000, 'provider-incomplete-pagination');
124
- assert(rows.length <= info.per_page, 'provider-partial-list');
125
- if (Object.hasOwn(info, 'count')) assert(info.count === rows.length, 'provider-partial-list');
126
- if (Object.hasOwn(info, 'total_count')) {
127
- assert(Number.isSafeInteger(info.total_count) && info.total_count >= 0, 'provider-incomplete-pagination');
128
- total ??= info.total_count; assert(total === info.total_count, 'provider-pagination-changed');
129
- }
130
- assert(total === undefined || result.length <= total, 'provider-partial-list');
131
- assert(info.cursor === undefined || typeof info.cursor === 'string', 'provider-invalid-cursor');
132
- if (!info.cursor) {
133
- assert(rows.length < info.per_page && (total === undefined || result.length === total), 'provider-partial-list');
134
- return result;
135
- }
136
- assert(total === undefined || result.length < total, 'provider-partial-list');
137
- cursor = info.cursor; assert(/^[a-zA-Z0-9_+\/=-]+$/.test(cursor) && !seen.has(cursor), 'provider-invalid-cursor'); seen.add(cursor);
138
- } else if (pagination === 'cursor') {
139
- const info = data.result_info;
140
- // R2's terminal short page may explicitly report null pagination. The
141
- // requested documented limit supplies the bound; a full page is ambiguous.
142
- if (path === '/r2/buckets' && info === null) { assert(rows.length < 1000, 'provider-incomplete-pagination'); return result; }
143
- assert(info && Number.isSafeInteger(info.per_page) && info.per_page > 0, 'provider-incomplete-pagination');
144
- if (!info.cursor) { assert(rows.length < info.per_page, 'provider-partial-list'); return result; }
145
- cursor = info.cursor; assert(typeof cursor === 'string' && /^[a-zA-Z0-9_+\/=-]+$/.test(cursor) && !seen.has(cursor), 'provider-invalid-cursor'); seen.add(cursor);
146
- } else {
147
- const info = data.result_info;
148
- assert(info && info.page === page && Number.isSafeInteger(info.total_count) && info.total_count >= 0, 'provider-incomplete-pagination');
149
- total ??= info.total_count; assert(total === info.total_count, 'provider-pagination-changed');
150
- assert(result.length <= total, 'provider-partial-list');
151
- if (result.length === total) return result;
152
- assert(rows.length > 0 && (info.total_pages === undefined || page < info.total_pages), 'provider-partial-list');
153
- }
98
+ }
99
+ async function collect(path, options) {
100
+ let result, rows = [];
101
+ for await (const page of providerPages(envelope, path, options)) {
102
+ result ??= page.data.result;
103
+ rows.push(...page.rows);
104
+ assert(rows.length <= 10000, 'provider-list-too-large');
154
105
  }
155
- throw new Error('provider-pagination-limit');
106
+ return options?.field ? {...result,[options.field]:rows} : rows;
107
+ }
108
+ async function list(path, options = {}) {
109
+ const shape = {...providerListShape(path),...options};
110
+ const result = await collect(path,shape);
111
+ return shape.field ? result[shape.field] : result;
112
+ }
113
+ async function requestResult(path, options = {}) {
114
+ const shape = (!options.method || options.method === 'GET') && providerListShape(path);
115
+ return shape ? collect(path,{...shape,zone:options.zone,signal:options.signal}) : (await envelope(path,options)).result;
156
116
  }
157
117
  async function upload(url, bytes, etag) {
158
118
  const destination = importUploadURL(url);
@@ -167,6 +127,6 @@ export function cloudflare({ accountId, token, fetch: request = globalThis.fetch
167
127
  assert(response.status === 200, 'd1-import-upload-failed');
168
128
  assert(response.headers.get('etag')?.replace(/^"|"$/g, '') === etag, 'd1-import-upload-etag-mismatch');
169
129
  }
170
- return { accountId, envelope, list, importSql(id, sql) { return importD1({envelope,upload}, id, sql); }, async request(path, options) { return (await envelope(path, options)).result; },
171
- async zoneRequest(path, options = {}) { return (await envelope(path, { ...options, zone: true })).result; } };
130
+ return { accountId, envelope, list, importSql(id, sql) { return importD1({envelope,upload}, id, sql); }, request:requestResult,
131
+ zoneRequest(path, options = {}) { return requestResult(path, { ...options, zone: true }); } };
172
132
  }
@@ -1,3 +1,4 @@
1
+ import { providerPages } from './pagination.mjs';
1
2
  import { assert } from './validation.mjs';
2
3
 
3
4
  // Cloudflare's object REST API requires literal slashes, with other reserved
@@ -17,24 +18,12 @@ export function isR2ObjectDelete(path, method) {
17
18
  // The API returns result: Object[] and result_info.is_truncated/cursor, not the
18
19
  // bucket-list shape. Yield bounded pages so removal never collects the bucket.
19
20
  export async function* r2ObjectPages(api, bucket) {
20
- const seen = new Set(); let cursor;
21
- for (;;) {
22
- const data = await api.envelope(`/r2/buckets/${bucket}/objects?per_page=1000${cursor ? `&cursor=${encodeURIComponent(cursor)}` : ''}`);
23
- const rows = data.result, info = data.result_info;
24
- assert(Array.isArray(rows) && rows.length <= 1000 && rows.every(row => typeof row?.key === 'string' && row.key.length), 'invalid-r2-object-list');
21
+ try { for await (const {rows} of providerPages(api.envelope, `/r2/buckets/${bucket}/objects`, {pagination:'cursor'})) {
22
+ assert(rows.length <= 1000 && rows.every(row => typeof row?.key === 'string' && row.key.length), 'invalid-r2-object-list');
25
23
  assert(new Set(rows.map(row => row.key)).size === rows.length, 'invalid-r2-object-list');
26
- // Cloudflare may omit pagination (or return null) on any terminal page
27
- // shorter than the requested limit. A full page cannot prove completion.
28
- if (info == null) {
29
- assert(rows.length < 1000, 'provider-incomplete-pagination');
30
- yield rows; return;
31
- }
32
- assert(typeof info.is_truncated === 'boolean' && (!info.delimited || Array.isArray(info.delimited) && !info.delimited.length), 'provider-incomplete-pagination');
33
- if (info.is_truncated) {
34
- assert(rows.length > 0 && typeof info.cursor === 'string' && info.cursor.length > 0 && info.cursor.length <= 4096 && !seen.has(info.cursor), 'provider-invalid-cursor');
35
- cursor = info.cursor; seen.add(cursor);
36
- } else assert(info.cursor == null || info.cursor === '', 'provider-invalid-cursor');
37
24
  yield rows;
38
- if (!info.is_truncated) return;
25
+ } } catch (error) {
26
+ error.providerCall ??= {operation:`GET /r2/buckets/${bucket}/objects`,reason:error.message};
27
+ throw error;
39
28
  }
40
29
  }