@runeditrun/rer 0.2.23 → 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 (38) hide show
  1. package/cli/demo-host.mjs +1 -1
  2. package/cli/deploy-command.mjs +7 -5
  3. package/cli/deploy-observation.mjs +20 -2
  4. package/cli/deploy-owner.mjs +12 -7
  5. package/cli/deploy.mjs +5 -4
  6. package/cli/site.mjs +2 -2
  7. package/global/CHANGELOG.md +16 -0
  8. package/modules/container/adapters/d1.mjs +1 -1
  9. package/modules/container/transfer.mjs +0 -1
  10. package/modules/container/worker-engine.mjs +1 -1
  11. package/modules/demo-host/README.md +29 -7
  12. package/modules/demo-host/container.mjs +19 -7
  13. package/modules/demo-host/placement.mjs +19 -5
  14. package/modules/demo-host/plan.mjs +11 -6
  15. package/modules/demo-host/start-script.mjs +2 -1
  16. package/modules/demo-host/startup.mjs +3 -3
  17. package/modules/demo-host/worker.mjs +27 -8
  18. package/modules/deploy-kit/README.md +35 -2
  19. package/modules/deploy-kit/diagnostics.mjs +16 -1
  20. package/modules/deploy-kit/owner-cli.mjs +3 -3
  21. package/modules/deploy-kit/owner-remove.mjs +23 -5
  22. package/modules/deploy-kit/pagination.mjs +99 -0
  23. package/modules/deploy-kit/provider.mjs +30 -70
  24. package/modules/deploy-kit/r2-objects.mjs +6 -17
  25. package/modules/deploy-kit/site-readback.mjs +67 -0
  26. package/modules/deploy-kit/site.mjs +17 -31
  27. package/modules/deploy-kit/uploaded-worker.mjs +9 -3
  28. package/modules/erasure/queue.mjs +0 -2
  29. package/modules/job-queue/cli.mjs +1 -1
  30. package/modules/operator-auth/identity/oidc/jwt.mjs +1 -1
  31. package/modules/portability/bridge.mjs +3 -3
  32. package/modules/portability/catalogue.mjs +1 -1
  33. package/modules/portability/cli-options.mjs +1 -1
  34. package/modules/portability/legacy-files.mjs +1 -1
  35. package/modules/portability/legacy-safety.mjs +3 -3
  36. package/modules/portability/payloads.mjs +1 -1
  37. package/modules/release/source.mjs +1 -1
  38. package/package.json +1 -1
package/cli/demo-host.mjs CHANGED
@@ -20,7 +20,7 @@ export async function demoHostCommand(project, options, store, signal, out, oper
20
20
  if (!(app.platforms ?? ['workers']).includes('container')) throw refuse('A hosted demo runs the seed\'s container image: declare "container" in deploy.json platforms first.');
21
21
  let plan;
22
22
  try { plan = demoPlan({ project: app.project, environment: options.environment ?? 'production', compatibilityDate: app.compatibilityDate, originVar: app.originVar, hostVariables: app.container?.hostVariables, declaration, turnstileSiteKey: process.env.TURNSTILE_SITE_KEY?.trim() }); }
23
- catch (error) { throw invalid(`The demo deploy declaration is invalid (${error.message}). deploy-demo.json holds {"version": 1, "domain": "<run site apex>", "name": "<product name>"}, optionally "maxInstances", "instanceType" (${INSTANCE_TYPES.join(', ')}; default basic) and plain "settings".`); }
23
+ catch (error) { throw invalid(`The demo deploy declaration is invalid (${error.message}). deploy-demo.json holds {"version": 1, "domain": "<run site apex>", "name": "<product name>"}, optionally "maxInstances", "instanceType" (${INSTANCE_TYPES.join(', ')}; default basic), "ownerEmail" (the demo owner's address; default owner@demo.invalid) and plain "settings".`); }
24
24
  if (options.remove) return removeDemo(plan, store, signal);
25
25
  if (process.env.RER_DEMO_OPERATOR_TOKEN && (process.env.RER_DEMO_OPERATOR_TOKEN.length < 32 || process.env.RER_DEMO_OPERATOR_TOKEN.length > 512)) throw invalid('RER_DEMO_OPERATOR_TOKEN must contain 32 to 512 characters.');
26
26
  const folder = join(project.root, '.rer', 'demo-host');
@@ -8,14 +8,14 @@ import { deployEnvironment, deployAgain } from './deploy-inputs.mjs';
8
8
  import { claimHostedOwner } from './deploy-owner.mjs';
9
9
  import { checkHostedSetup, hostedSetupFailure } from './deploy-setup.mjs';
10
10
  import { mutationReadinessFailure } from './deploy-readiness.mjs';
11
- import { observeOwner } from './deploy-observation.mjs';
11
+ import { observeOwner, ownerExchangeFailure } from './deploy-observation.mjs';
12
12
  import { committedSource } from './deploy-source.mjs';
13
13
  import { hookOrigin, waitForSource } from './deploy-hook.mjs';
14
14
  import { rememberHookState, hookState } from './deploy-hook-record.mjs';
15
15
  import { main as owner } from '../modules/deploy-kit/owner-cli.mjs';
16
16
  import { localOnlyBinding, ownerWorkerName } from '../modules/deploy-kit/owner.mjs';
17
17
  import { refuseLocalModels } from '../modules/deploy-kit/local-only.mjs';
18
- import { providerRefusal, providerTransport } from '../modules/deploy-kit/diagnostics.mjs';
18
+ import { providerFailure } from '../modules/deploy-kit/diagnostics.mjs';
19
19
  import { shared } from './shared.mjs';
20
20
 
21
21
  export function validateDeployOptions(options) {
@@ -108,7 +108,7 @@ export async function deployCommand(context) {
108
108
  // current version independently of the original upload's spent deadline.
109
109
  const state = await inspect(origin);
110
110
  if ((state?.origin ?? origin) !== origin || claims.some(item => item.origin !== origin)) throw new Error('owner-claim-origin-mismatch');
111
- if (claims.some(item => !item.outcome)) throw new Error('owner-claim-failed');
111
+ if (claims.some(item => !item.outcome)) { const error = new Error('owner-claim-failed'); error.ownerExchange = claims.find(item => !item.outcome); throw error; }
112
112
  claimed = claims.at(-1)?.outcome;
113
113
  step = 'wait for the deployed source';
114
114
  await waitForSource({ origin, sourceSha, versionId: state?.versionId ?? (!claims.length && !deployed.owner ? uploaded?.versionId : undefined), again: deployAgain(options) });
@@ -136,7 +136,9 @@ export async function deployCommand(context) {
136
136
  if (setup) throw commandFailure(redact(setup));
137
137
  const readiness = mutationReadinessFailure(error, { again, redact });
138
138
  if (readiness) throw commandFailure(readiness);
139
- const refused = providerRefusal(error) ?? providerTransport(error);
140
- throw commandFailure(redact(`${state} Failed at ${step}: ${error.message}${refused ? ` (${refused})` : ''}${error.cause ? ` (${error.cause})` : ''}. Fix the cause and run ${again} again using the same environment credentials. ${outcome && recorded ? `To retire the token without rebuilding: ${again} --unset-secret ${setupManifest?.steps?.find(item => item.type === 'owner-claim')?.tokenField ?? 'SETUP_TOKEN'}.` : ''}`));
139
+ const refused = providerFailure(error,redact);
140
+ const exchange = error.ownerExchange ?? (error.message === 'owner-claim-failed' ? observedClaims.lastExchange ?? observedClaims.at(-1) : undefined);
141
+ const ownerCause = ownerExchangeFailure(exchange);
142
+ throw commandFailure(redact(`${state} Failed at ${step}: ${error.message}${refused ? ` (${refused})` : ''}${ownerCause ? ` (${ownerCause})` : ''}${error.cause ? ` (${error.cause})` : ''}. Fix the cause and run ${again} again using the same environment credentials. ${outcome && recorded ? `To retire the token without rebuilding: ${again} --unset-secret ${setupManifest?.steps?.find(item => item.type === 'owner-claim')?.tokenField ?? 'SETUP_TOKEN'}.` : ''}`));
141
143
  }
142
144
  }
@@ -12,7 +12,11 @@ export async function observeOwner({ paths, inspect, beforeClaim, addSecret, dep
12
12
  if (paths.includes(url.pathname) && (init.method ?? (input instanceof Request ? input.method : 'GET')).toUpperCase() === 'POST') {
13
13
  try { body = JSON.parse(typeof init.body === 'string' ? init.body : input instanceof Request ? await input.clone().text() : 'null'); } catch {}
14
14
  }
15
- if (typeof body?.token !== 'string' || !body.token) return fetch(input, init);
15
+ if (typeof body?.token !== 'string' || !body.token) {
16
+ const response = await fetch(input,init);
17
+ if (claims.some(item => item.origin === url.origin)) claims.lastExchange = await exchange(response,url,init.method ?? 'GET');
18
+ return response;
19
+ }
16
20
  addSecret(body.token);
17
21
  const deployed = await inspect(url.origin);
18
22
  if (deployed && url.origin !== deployed.origin) throw new Error('owner-claim-origin-mismatch');
@@ -24,9 +28,23 @@ export async function observeOwner({ paths, inspect, beforeClaim, addSecret, dep
24
28
  try { code = (await response.clone().json()).error; } catch {}
25
29
  }
26
30
  const outcome = response.status === 303 ? 'claimed' : response.status === 409 && code === 'already_initialized' ? 'kept' : undefined;
27
- claims.push({ origin: url.origin, outcome, status: response.status });
31
+ const observed = {...await exchange(response,url,'POST'),outcome};
32
+ claims.push(observed); claims.lastExchange = observed;
28
33
  return response;
29
34
  };
30
35
  try { return await observeReadiness(() => run(claims)); }
31
36
  finally { globalThis.fetch = fetch; }
32
37
  }
38
+
39
+ async function exchange(response,url,method) {
40
+ let code;
41
+ if (response.status >= 400) { try { code = (await response.clone().json()).error; } catch {} }
42
+ return {origin:url.origin,path:url.pathname,method,status:response.status,
43
+ ...(typeof code === 'string' && /^[a-zA-Z0-9_-]{1,80}$/.test(code) ? {code} : {}),
44
+ ...(response.status === 303 && !response.headers.has('set-cookie') ? {missingCookie:true} : {})};
45
+ }
46
+
47
+ export function ownerExchangeFailure(exchange) {
48
+ if (!exchange || typeof exchange.path !== 'string' || !Number.isInteger(exchange.status)) return undefined;
49
+ return `${exchange.method === 'GET' ? 'GET' : 'POST'} ${exchange.path.split('?')[0]} returned HTTP ${exchange.status}${exchange.code ? ` (${exchange.code})` : ''}${exchange.missingCookie ? ' (no Set-Cookie header)' : ''}`;
50
+ }
@@ -1,5 +1,5 @@
1
1
  import { ownerClaimRequest } from './deploy-claim-request.mjs';
2
- import { observeOwner } from './deploy-observation.mjs';
2
+ import { observeOwner, ownerExchangeFailure } from './deploy-observation.mjs';
3
3
  import { randomBytes } from 'node:crypto';
4
4
 
5
5
  // After a healthy deploy, the hosted app's first owner is the setup owner: rer
@@ -28,7 +28,7 @@ export async function claimHostedOwner({ hook, setupManifest, declaration, deplo
28
28
  try { if (!deployed.ownerClaim) await recordClaim?.(outcome); await secret(['--unset', tokenKey], env); }
29
29
  catch (cause) {
30
30
  if (cause.secretChange) { cause.owner = outcome; cause.tokenKey = tokenKey; cause.ownerFinalization = true; throw cause; }
31
- const error = new Error('owner-token-retirement-failed'); error.owner = outcome; error.tokenKey = tokenKey; error.cause = cause.message; throw error; }
31
+ const error = new Error('owner-token-retirement-failed'); error.owner = outcome; error.tokenKey = tokenKey; error.cause = cause; throw error; }
32
32
  return outcome;
33
33
  };
34
34
  // Only a recorded successful bootstrap proves ownership; --unset-secret alone
@@ -53,11 +53,16 @@ export async function claimHostedOwner({ hook, setupManifest, declaration, deplo
53
53
  }
54
54
  if (typeof hook === 'function') {
55
55
  const context = { origin: deployed.origin, vars: { ...env, [tokenKey]: token }, input: {}, store, signal, ownerClaim: claim, fail: () => ownerFailure('the seed owner hook failed') };
56
- let observed;
56
+ let observed, exchanges = [];
57
57
  try {
58
58
  if (deployed.observedOwner) await hook(context);
59
- else await observeOwner({ paths: ['/auth/bootstrap', claim?.path].filter(Boolean), inspect, addSecret, deployed: () => deployed, tokenKey }, async claims => { await hook(context); observed = claims.at(-1)?.outcome; });
60
- } catch (cause) { if (cause.message === 'owner-claim-failed') throw cause; throw ownerFailure(cause.message); }
59
+ else await observeOwner({ paths: ['/auth/bootstrap', claim?.path].filter(Boolean), inspect, addSecret, deployed: () => deployed, tokenKey }, async claims => { exchanges = claims; await hook(context); observed = claims.at(-1)?.outcome; });
60
+ } catch (cause) {
61
+ const exchange = exchanges.lastExchange ?? exchanges.at(-1);
62
+ const error = cause.message === 'owner-claim-failed' ? cause : ownerFailure(cause.message);
63
+ if (exchange) { error.ownerExchange = exchange; error.cause = ownerExchangeFailure(exchange); }
64
+ throw error;
65
+ }
61
66
  // Function hooks can keep existing owners without returning a value. Observe
62
67
  // their actual response, instead of assuming that a resolved promise claimed.
63
68
  const outcome = context.ownerClaimed ?? observed ?? deployed.observedOwner?.();
@@ -69,12 +74,12 @@ export async function claimHostedOwner({ hook, setupManifest, declaration, deplo
69
74
  try {
70
75
  response = await ownerClaimRequest(fetch, new URL(hook.path ?? '/auth/bootstrap', deployed.origin), { method: 'POST',
71
76
  headers: { Origin: deployed.origin, 'Content-Type': 'application/json' }, body: JSON.stringify({ token, displayName, returnTo: hook.returnTo ?? '/' }) }, {deployed, tokenKey, signal});
72
- } catch (error) { throw ownerFailure(['owner-claim-origin-mismatch','owner-claim-redirected','owner-claim-readiness-timeout'].includes(error.message) ? error.message : 'the hosted application did not answer'); }
77
+ } catch (error) { throw ownerFailure(`POST ${new URL(hook.path ?? '/auth/bootstrap',deployed.origin).pathname}: ` + (['owner-claim-origin-mismatch','owner-claim-redirected','owner-claim-readiness-timeout'].includes(error.message) ? error.message : 'the hosted application did not answer')); }
73
78
  if (response.status !== 303) { try { const body = await response.json(); if (typeof body?.error === 'string' && /^[a-z_]{1,64}$/.test(body.error)) code = body.error; } catch {} }
74
79
  else await response.body?.cancel();
75
80
  const outcome = response.status === 303 ? 'claimed' : response.status === 409 && code === 'already_initialized' ? 'kept' : undefined;
76
81
  const advice = code !== 'invalid_setup_token' ? '' : !secret ? `; the Worker holds another setup token: have the deploy hook return the one it deployed as setupToken, or set it as ${item.environment} with --env-file` : given ? `; the ${item.environment} you supplied is not the deployed setup token: leave it unset and rer issues a fresh one` : '';
77
- if (!outcome) throw ownerFailure(`HTTP ${response.status} (${code})${advice}`);
82
+ if (!outcome) throw ownerFailure(`POST ${new URL(hook.path ?? '/auth/bootstrap',deployed.origin).pathname} returned HTTP ${response.status} (${code})${advice}`);
78
83
  return retire(outcome);
79
84
  }
80
85
 
package/cli/deploy.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  import { requireProductionAuth } from './production-auth.mjs';
2
2
  import { readFile } from 'node:fs/promises';
3
- import { deployDiagnostic, providerTransport } from '../modules/deploy-kit/diagnostics.mjs';
3
+ import { deployDiagnostic, providerFailure } from '../modules/deploy-kit/diagnostics.mjs';
4
4
  import { shared } from './shared.mjs';
5
5
  import { contained } from './project.mjs';
6
6
  import { missing, invalid, refuse, RerError } from './errors.mjs';
@@ -119,14 +119,14 @@ function failureText({ error, step, created, again, retry, secretChange, deploye
119
119
  const at = step === 'deploy' ? diagnostic?.step ?? 'prepare deploy' : step;
120
120
  const input = /^missing-secret-([A-Z][A-Z0-9_]*)$/.exec(code)?.[1];
121
121
  if (input) return `Deploy needs ${input}, which the deploy declaration lists as a secret and the Worker does not hold it. Set ${input} in the environment or in an --env-file file (an owned 0600 file of KEY=VALUE lines), then run the same command again. Nothing was uploaded.${created.length ? ` Resources created this attempt: ${created.join(', ')}.` : ''}`;
122
- if (code === 'remove-incomplete') return `Removal stopped with ${error.remaining.join(', ')} still present${error.failed.length ? ` (${error.failed.map(item => `${item.resource}: ${item.code}`).join('; ')})` : ''}. Removed: ${error.removed.join(', ') || 'nothing'}. Confirmed owned R2 buckets are emptied automatically; unproved buckets are never purged. Run ${again} --remove --yes again to finish.`;
122
+ if (code === 'remove-incomplete') return `Removal stopped with ${error.remaining.join(', ')} still present${error.failed.length ? ` (${error.failed.map(item => `${item.resource}: ${providerFailure(item.error, redact) ?? item.code}`).join('; ')})` : ''}. Removed: ${error.removed.join(', ') || 'nothing'}. Confirmed owned R2 buckets are emptied automatically; unproved buckets are never purged. Run ${again} --remove --yes again to finish.`;
123
123
  if (code === 'nothing-to-remove') return `The deployment record selected by ${again} has no resources that rer owns, so --remove deletes nothing.`;
124
- if (code === 'owner-token-retirement-failed') return `The application is deployed at ${deployed?.origin}; owner ${error.owner}; setup token retirement failed (${redact(String(error.cause))}). Run ${again} --unset-secret ${error.tokenKey ?? 'SETUP_TOKEN'} using the same environment credentials, or run ${again} again to finish.`;
124
+ if (code === 'owner-token-retirement-failed') return `The application is deployed at ${deployed?.origin}; owner ${error.owner}; setup token retirement failed (${redact(String(error.cause))}${providerFailure(error,redact) ? `; ${providerFailure(error,redact)}` : ''}). Run ${again} --unset-secret ${error.tokenKey ?? 'SETUP_TOKEN'} using the same environment credentials, or run ${again} again to finish.`;
125
125
  if (code === 'owner-claim-failed') return `The application is deployed at ${deployed?.origin}, but claiming its owner failed: ${redact(String(error.cause))}. Owner claim and setup token retirement are not confirmed; run ${again} again using the same environment credentials to finish.`;
126
126
  const resource = diagnostic?.resource ?? (error.resource ? redact(String(error.resource)) : undefined);
127
127
  if (code === 'resource-not-listed') return `Deploy stopped at ${at}: ${resource} was created, but Cloudflare's account listing still does not show it after two minutes. Nothing was uploaded.\nResources created this attempt: ${created.join(', ') || 'none'}.\nRun ${again} again once the listing catches up: it reuses ${resource}. To delete what this environment created instead: ${again} --remove --yes.`;
128
128
  const lines = [`Deploy failed at ${at}: ${code}${resource ? ` (${resource})` : ''}${diagnostic?.providerCodes?.length ? ` (provider ${diagnostic.providerCodes.join(', ')})` : ''}.`];
129
- const transport = providerTransport(error, redact);
129
+ const transport = providerFailure(error, redact);
130
130
  if (transport) lines.push(`${transport}.`);
131
131
  if (diagnostic?.output && diagnostic.output !== '(no provider output)') lines.push(`First redacted output lines:\n${diagnostic.output}`);
132
132
  lines.push(`Resources created this attempt: ${created.join(', ') || 'none'}.`);
@@ -136,6 +136,7 @@ function failureText({ error, step, created, again, retry, secretChange, deploye
136
136
 
137
137
  function recovery({ code, at, resource, again, retry, secretChange }) {
138
138
  const remove = `${again} --remove --yes`, removing = retry === remove;
139
+ if (code === 'provider-incomplete-pagination') return `Cloudflare did not supply a complete list. Inspect the named GET call in Cloudflare, resolve its missing continuation, then run ${retry} again with the same account and environment. Keep the deployment record so rer can reuse its resources.`;
139
140
  if (code === 'owned-resource-replaced' || code === 'resource-name-collision') return `rer refuses to change a resource it did not create or that was replaced after it did; nothing was changed. Choose another --environment name, or remove the other resource yourself.`;
140
141
  // A lost create answer: the resource may exist, and rer cannot prove it made it.
141
142
  if (resource && (code === 'uncertain-resource-create' || code === 'provider-transport-failed' && at.startsWith('create '))) {
package/cli/site.mjs CHANGED
@@ -113,7 +113,7 @@ export async function siteCommand(project, options, store, signal, out) {
113
113
  if (!options.json) for (const line of human) out.human(line);
114
114
  const { cloudflare } = await import('../modules/deploy-kit/provider.mjs');
115
115
  const { collectFiles } = await import('../modules/deploy-kit/artifact.mjs');
116
- const { siteDeploy } = await import('../modules/deploy-kit/site.mjs');
116
+ const { siteDeploy, siteReadbackSummary } = await import('../modules/deploy-kit/site.mjs');
117
117
  const environment = options.environment ?? 'production';
118
118
  try {
119
119
  const api = cloudflare({ accountId: process.env.CLOUDFLARE_ACCOUNT_ID, token: process.env.CLOUDFLARE_API_TOKEN });
@@ -122,7 +122,7 @@ export async function siteCommand(project, options, store, signal, out) {
122
122
  return { dryRun: false, deployed: true, origin: result.origin, site: summary };
123
123
  } catch (error) {
124
124
  if (error instanceof RerError) throw error;
125
- throw new RerError('deploy_refused', `Run site deploy stopped: ${/^[a-z0-9-]{1,80}$/.test(error.message) ? error.message : 'site-deploy-failed'}${error.resource ? ` (${error.resource})` : ''}.`, 1);
125
+ throw new RerError('deploy_refused', `Run site deploy stopped: ${/^[a-z0-9-]{1,80}$/.test(error.message) ? error.message : 'site-deploy-failed'}${error.resource ? ` (${error.resource})` : ''}.${siteReadbackSummary(error)}`, 1);
126
126
  }
127
127
  }
128
128
 
@@ -1,5 +1,21 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.2.24
4
+
5
+ - Hosted demos stay up through redeploys. The demo's Container application keeps a live demo's instance for an hour (`rollout_active_grace_period`), so a deploy that changes the image, instance type or limit no longer ends idle visitors' demos ("This demo has ended" within two minutes before).
6
+ - Hosted demos wait out Cloudflare placement. A fresh application keeps 7 prepared instances; the eighth concurrent demo waited 16-37 s for Cloudflare to place another. Placement waits now renew the 45-second boot budget up to 90 seconds and keep the visitor's reservation. The application's own `max_instances` is twice `maxInstances`, so instances Cloudflare still counts after demos end never refuse an admitted visitor; admission still holds at `maxInstances`.
7
+ - A new demo copy that takes over two seconds to answer is a pending start instead of a 502, and a slow status read stays pending instead of a 503.
8
+ - The start page signs in about half a second sooner: each status poll is held until the start changes, and the reservation is extended alongside the owner claim.
9
+ - `deploy-demo.json` takes an optional `ownerEmail`, the address the demo's owner signs in as and seed screens show (`owner@demo.invalid` unless set).
10
+ - Each demo admission logs `demo_admitted` with the user agent and network, so a held place can be joined to whoever started it. On test hosts, automated browsers pass Turnstile's always-pass testing keys and hold real places.
11
+ - Run-site deploys wait through Cloudflare propagation after a fresh upload, within a 90-second window, and name the serving and newest versions on a timeout.
12
+ - Every Cloudflare list read shares one pagination reader; an incomplete list stops with `provider-incomplete-pagination` instead of treating a resource as absent. Environment removal detaches queue consumers before deleting queues and confirms their absence.
13
+
14
+ ### Upgrade notes
15
+
16
+ - Redeploy the hosted demo (`rer deploy --demo`) once to apply the hour-long rollout grace and the doubled provider limit. The deploy that first sets the grace may itself still replace live demos, so run it when no visitor is mid-demo.
17
+ - Add `"ownerEmail"` to `deploy-demo.json` for a plausible owner address on demo screens.
18
+
3
19
  ## 0.2.23
4
20
 
5
21
  - Managed mail (`rer` provider) works from Cloudflare Workers. Mail transport always fetches with `redirect: 'manual'`, because Workers reject `redirect: 'error'`, which made every relay send end as `transport_uncertain` without a request. A relay 3xx is never followed and is reported as `rejected` (`provider_redirected`).
@@ -14,7 +14,7 @@ export class D1 {
14
14
  if(message.started){pending.timer=setTimeout(()=>{void this.worker.terminate();this.fail(new Error('D1_ERROR: query timed out: SQLITE_INTERRUPT'));},30000);return;}
15
15
  clearTimeout(pending.timer);this.pending.delete(message.id);message.error?pending.reject(new Error(message.error)):pending.resolve(message.result);
16
16
  });
17
- this.worker.on('error',cause=>this.fail(cause));this.worker.on('exit',code=>{this.worker=null;if(this.pending.size)this.fail(new Error('D1_ERROR: database worker stopped'));});
17
+ this.worker.on('error',cause=>this.fail(cause));this.worker.on('exit',()=>{this.worker=null;if(this.pending.size)this.fail(new Error('D1_ERROR: database worker stopped'));});
18
18
  }
19
19
  fail(cause){for(const pending of this.pending.values()){clearTimeout(pending.timer);pending.reject(cause);}this.pending.clear();}
20
20
  call(operation,statements,sql){if(!this.worker)this.start();const id=++this.next;return new Promise((resolve,reject)=>{this.pending.set(id,{resolve,reject});this.worker.postMessage({id,operation,statements,sql});});}
@@ -1,5 +1,4 @@
1
1
  import { readFile } from 'node:fs/promises';
2
- import { join } from 'node:path';
3
2
  import { DatabaseSync } from 'node:sqlite';
4
3
  import { D1 } from './adapters/d1.mjs';
5
4
  import { Objects } from './adapters/objects.mjs';
@@ -1,6 +1,6 @@
1
1
  import { Miniflare, convertV4MiniflareOptions, coupleWebSocket } from 'miniflare';
2
2
  import { readdir } from 'node:fs/promises';
3
- import { join, relative } from 'node:path';
3
+ import { join } from 'node:path';
4
4
  import { rootCertificates } from 'node:tls';
5
5
  import { ensurePortabilitySchema } from './portability-schema.mjs';
6
6
  import { migrate } from './migrations.mjs';
@@ -3,7 +3,8 @@
3
3
  The front Worker `rer deploy --demo` deploys at `demo.<domain>` for a seed
4
4
  that adopted demo mode (`withDemo`, export/DEMO.md). `plan.mjs` turns `deploy-demo.json`
5
5
  (optionally `instanceType`, one of Cloudflare's predefined Container instance
6
- types, `basic` unless set) into the Worker's Wrangler configuration; the CLI (`cli/demo-host.mjs`) writes
6
+ types, `basic` unless set, and `ownerEmail`, the address the demo's owner signs
7
+ in as, `owner@demo.invalid` unless set) into the Worker's Wrangler configuration; the CLI (`cli/demo-host.mjs`) writes
7
8
  it with these files and the seed's container image folder (workerd engine) to
8
9
  `.rer/demo-host/` and runs `wrangler deploy` there. Declared
9
10
  `container.hostVariables` resolve from demo settings to the primary hostname or
@@ -20,14 +21,20 @@ start page.
20
21
  202 progress and a `rer_demo` cookie; `GET /__demo/status` polls that same
21
22
  copy until it can relay the owner claim's 303 (JSON `{ location }` for the
22
23
  page script) and session cookie. Every external admission step is bounded:
23
- the start answers within 12 seconds, and status within two seconds. A request
24
+ the start answers within 12 seconds. A new copy that takes over two seconds
25
+ to answer is still a pending start, not a failure. Each status request is
26
+ held (at most 1.5 seconds) until the start changes, so the page learns of the
27
+ sign-in at once; a slow status read answers pending. Each admission logs
28
+ `demo_admitted` with the request's user agent and network owner, so a held
29
+ place can be joined to whoever started it (automated browsers pass
30
+ Turnstile's always-pass testing keys on test hosts). A request
24
31
  with that cookie goes to the visitor's Durable Object; nothing else reaches
25
32
  a container.
26
33
  - `container.mjs`: `DemoContainer`, one per visitor (`newUniqueId`, checked
27
34
  with `idFromString`). It starts the container with public internet enabled,
28
35
  `RER_DEMO=1`, the fixed origin (`RER_INGRESS_ORIGIN` and the seed's origin
29
36
  variable), fresh `SESSION_SECRET` and `SETUP_TOKEN`,
30
- `OWNER_EMAIL=owner@demo.invalid` and the owner claim's issuer, installation
37
+ `OWNER_EMAIL` (`DEMO_OWNER_EMAIL`, else `owner@demo.invalid`) and the owner claim's issuer, installation
31
38
  id and public key; waits for `/health`; and posts a five-minute ES256 claim
32
39
  (`claim.mjs`) to `/auth/owner-claim`. Its alarm ends the demo at its hour or
33
40
  after 15 idle minutes: `destroy()`, `deleteAll()` and the place released. A
@@ -82,7 +89,9 @@ Chromium (default-seed's Playwright) through waiting, failure, Try again,
82
89
  working and the signed-in landing. The end-to-end proof (`wrangler dev` with Docker)
83
90
  is in DEMO.md.
84
91
 
85
- Startup has a 45-second budget, with a separate five-second HTTP probe limit.
92
+ Startup has a 45-second boot budget, with a separate five-second HTTP probe
93
+ limit; waiting for Cloudflare to place the container renews it, up to 90
94
+ seconds from the start (see placement below).
86
95
  The Durable Object owns that work and cleanup while the page polls; a browser
87
96
  that leaves cannot abandon it. Retrying a pending copy resumes the same
88
97
  reservation; an explicit Try again cancels it before creating a replacement. Failed starts release their place before returning the retry state, retain the bounded operator diagnostic and destroy
@@ -116,12 +125,25 @@ container is already stopped); a terminal start failure releases it immediately.
116
125
  A cleanup failure is logged separately and cannot replace the startup failure
117
126
  shown to the visitor.
118
127
 
119
- Cloudflare may briefly report that it has no prepared container instance even
120
- when the application is below `max_instances`. `placement.mjs` keeps the same
128
+ Cloudflare keeps a small prepared pool per application (7 instances on fresh
129
+ test hosts, measured in October 2026, whatever `max_instances` is) and places a
130
+ new instance only on demand: a start beyond the pool gets "no container instance
131
+ that can be provided" until Cloudflare has placed one (16 to 37 seconds
132
+ measured, after which boot was normal). `placement.mjs` keeps the same
121
133
  reservation and launch options, requests placement again only for that specific
122
- provider response, and stays within the original startup deadline. Boot, health
134
+ provider response, and renews the 45-second boot budget after each wait, up to
135
+ 90 seconds from the start, extending the reservation to match. Boot, health
123
136
  and claim errors are not retried. Placement exhaustion answers with the normal
124
137
  failed-start retry state and releases the reservation; the capacity notice is
125
138
  reserved for actual reservation exhaustion. Cleanup keeps only that terminal
126
139
  answer and correlation for one minute, so a status poll after object recovery
127
140
  still receives the right answer; its alarm then deletes the remaining metadata.
141
+
142
+ `DemoCapacity` alone admits visitors (`maxInstances` live demos). The Container
143
+ application's own `max_instances` is twice that, so instances Cloudflare still
144
+ counts after a demo ends or a start fails never refuse a place admission gave.
145
+ Its `rollout_active_grace_period` is an hour, a demo's longest life: a redeploy
146
+ that rolls the application out (a new image, instance type or limit) replaces
147
+ idle prepared instances at once and leaves every live demo running until it
148
+ ends. With no grace period a rollout replaced live demos' containers within two
149
+ minutes, and their visitors saw "This demo has ended".
@@ -52,10 +52,12 @@ export class DemoContainer extends DurableObject {
52
52
  diagnostic() { return { correlation: this.correlation, tail: this.records, stdout: 'Provider container logs; not exposed by the lifecycle API.' }; }
53
53
  idle = IDLE;
54
54
  startTimeout = 45_000;
55
+ placementTimeout = 90_000;
55
56
  probeTimeout = 5000;
56
57
  cleanupRetry = 30_000;
57
58
  now() { return Date.now(); }
58
59
  get container() { return this.ctx.container; }
60
+ get owner() { return this.env.DEMO_OWNER_EMAIL || OWNER; }
59
61
  port() { return this.container.getTcpPort(PORT); }
60
62
 
61
63
  async begin(options) {
@@ -66,31 +68,41 @@ export class DemoContainer extends DurableObject {
66
68
  return this.status();
67
69
  }
68
70
 
69
- async status() {
70
- const startup = await this.ctx.storage.get('startup');
71
+ /** The start's state; `wait` holds a pending answer until it changes (at most 1.5 s), so the page learns of the sign-in without its own polling delay.
72
+ * A poll can also reach a new copy before its slow begin has landed. */
73
+ async status(wait = 0) {
74
+ const until = Date.now() + Math.min(wait, 1500);
75
+ let startup = await this.ctx.storage.get('startup');
76
+ while ((!startup || (startup.phase === 'starting' && !startup.failure)) && Date.now() < until) {
77
+ await new Promise(resolve => setTimeout(resolve, 100));
78
+ startup = await this.ctx.storage.get('startup');
79
+ }
71
80
  return { ...(startup ?? { phase: 'failed' }), correlation: this.correlation };
72
81
  }
73
82
 
74
83
  /** Start the container, wait for /health, claim the owner; returns the claim's 303. */
75
84
  async start({ origin, originVar, ends, endsLocal, settings, correlation }) {
76
85
  this.correlation = correlation; this.started = this.now(); this.stopped = false; this.cancelled = false;
77
- const deadline = this.started + this.startTimeout;
86
+ let deadline = this.started + this.startTimeout;
78
87
  const id = this.ctx.id.toString(), keys = await claimKeys(), now = this.now();
79
88
  await this.ctx.storage.put({ ends, seen: now, correlation, started: this.started, startDeadline: deadline });
80
89
  report(correlation, this.started, 'start');
81
90
  if (this.stopped) throw new Error('container-stopped');
82
- await placeContainer(this, { enableInternet: true, env: { ...settings, RER_DEMO: '1', [originVar]: origin, RER_INGRESS_ORIGIN: origin, RER_INGRESS_ORIGINS: JSON.stringify([origin, ...JSON.parse(this.env.DEMO_SECONDARY_ORIGINS ?? '[]')]),
83
- SESSION_SECRET: random(), SETUP_TOKEN: random(), OWNER_EMAIL: OWNER, RER_OWNER_CLAIM_ISSUER: origin, RER_OWNER_CLAIM_INSTALLATION_ID: id,
91
+ deadline = await placeContainer(this, { enableInternet: true, env: { ...settings, RER_DEMO: '1', [originVar]: origin, RER_INGRESS_ORIGIN: origin, RER_INGRESS_ORIGINS: JSON.stringify([origin, ...JSON.parse(this.env.DEMO_SECONDARY_ORIGINS ?? '[]')]),
92
+ SESSION_SECRET: random(), SETUP_TOKEN: random(), OWNER_EMAIL: this.owner, RER_OWNER_CLAIM_ISSUER: origin, RER_OWNER_CLAIM_INSTALLATION_ID: id,
84
93
  RER_OWNER_CLAIM_PUBLIC_KEYS: JSON.stringify(keys.publicKeys), RER_DEMO_EXPIRES: endsLocal } }, deadline);
85
94
  await this.container.setInactivityTimeout(this.idle);
86
95
  await this.ctx.storage.setAlarm(deadline);
87
- const token = await signClaim(keys, { origin, installationId: id, email: OWNER, now: this.now() });
96
+ const token = await signClaim(keys, { origin, installationId: id, email: this.owner, now: this.now() });
97
+ // Hold the place to the demo's end while the owner claim runs; a failed claim releases it in cleanup.
98
+ const held = bounded(this.env.CAPACITY.get(this.env.CAPACITY.idFromName('capacity')).extend(id, ends), 2000).catch(() => false);
88
99
  const claimed = await boundedFetch(this.port(), 'http://container/auth/owner-claim', { method: 'POST', redirect: 'manual',
89
100
  headers: { origin, 'content-type': 'application/x-www-form-urlencoded' }, body: new URLSearchParams({ token }) }, Math.max(1, Math.min(10_000, deadline - this.now())));
90
101
  void claimed.body?.cancel().catch(() => {});
91
102
  this.record('claim', { status: claimed.status });
92
103
  if (this.stopped) throw new Error('container-stopped');
93
104
  if (claimed.status !== 303) throw new Error(`owner claim answered ${claimed.status}`);
105
+ if (!await held) throw new Error('start-reservation-expired');
94
106
  await this.ctx.storage.put('seen', this.now());
95
107
  await this.ctx.storage.setAlarm(Math.min(ends, this.now() + this.idle));
96
108
  return { location: claimed.headers.get('location'), cookies: claimed.headers.getSetCookie() };
@@ -109,7 +121,7 @@ export class DemoContainer extends DurableObject {
109
121
  } catch (error) {
110
122
  this.record('health', { attempt: ++attempt, durationMs: this.now() - probe, error: category(error) });
111
123
  }
112
- await new Promise(resolve => setTimeout(resolve, Math.min(250, Math.max(0, deadline - this.now()))));
124
+ await new Promise(resolve => setTimeout(resolve, Math.min(100, Math.max(0, deadline - this.now()))));
113
125
  }
114
126
  throw new Error('health-deadline');
115
127
  }
@@ -1,8 +1,15 @@
1
- // Cloudflare can exhaust its prepared instances before reaching max_instances.
2
- // Keep this visitor's reservation while that pool grows. Only the provider's
3
- // explicit no-instance response permits another placement attempt; boot, health
4
- // and claim failures still terminate the start.
1
+ import { bounded } from './diagnostics.mjs';
2
+
3
+ // Cloudflare keeps a small prepared pool per application (7 instances measured
4
+ // on fresh hosts) and prepares more only on demand: a start beyond the pool
5
+ // waits while Cloudflare places a new instance (16-37 s measured), then boots
6
+ // normally. Keep this visitor's reservation through that wait. Only the
7
+ // provider's explicit no-instance response permits another placement attempt,
8
+ // and it renews the boot budget up to placementTimeout from the start; boot,
9
+ // health and claim failures still terminate the start. Returns the deadline the
10
+ // placed container must become healthy and claimed by.
5
11
  export async function placeContainer(demo, options, deadline) {
12
+ const limit = demo.started + demo.placementTimeout, capacity = demo.env.CAPACITY.get(demo.env.CAPACITY.idFromName('capacity'));
6
13
  let attempt = 0;
7
14
  while (Date.now() < deadline && !demo.cancelled) {
8
15
  demo.stopped = false;
@@ -12,10 +19,17 @@ export async function placeContainer(demo, options, deadline) {
12
19
  await demo.ctx.storage.setAlarm(deadline);
13
20
  try {
14
21
  await demo.healthy(deadline);
15
- return;
22
+ return deadline;
16
23
  } catch (error) {
17
24
  if (demo.cancelled || demo.stopReason !== 'placement-unavailable') throw error;
18
25
  demo.record('placement', { attempt: ++attempt, outcome: 'waiting' });
26
+ const renewed = Math.min(limit, Date.now() + demo.startTimeout);
27
+ if (renewed > deadline) {
28
+ // The reservation outlives the renewed budget, or the place is no longer this visitor's.
29
+ if (!await bounded(capacity.extend(demo.ctx.id.toString(), renewed + 15_000), 2000)) throw new Error('start-reservation-expired');
30
+ deadline = renewed;
31
+ await demo.ctx.storage.put('startDeadline', deadline);
32
+ }
19
33
  await new Promise(resolve => setTimeout(resolve, Math.min(1000, Math.max(0, deadline - Date.now()))));
20
34
  }
21
35
  }
@@ -18,29 +18,31 @@ export const INSTANCE_TYPES = ['lite', 'basic', 'standard-1', 'standard-2', 'sta
18
18
  /**
19
19
  * deploy-demo.json: `{ "version": 1, "domain": "run-lamp.com", "name": "Lamp" }`, plus optionally
20
20
  * `maxInstances` (concurrent demos, default 10), `instanceType` (one of INSTANCE_TYPES, default
21
- * "basic") and `settings`, the seed's plain demo settings (such as MAIL_PROVIDER: "development").
21
+ * "basic"), `ownerEmail` (the address the demo's owner signs in as, default owner@demo.invalid) and `settings`, the seed's plain demo settings (such as MAIL_PROVIDER: "development").
22
22
  * Settings never carry secrets or what the front sets. The app declaration's
23
23
  * container.hostVariables select hostname settings: the primary demo hostname
24
24
  * or its descendants, routed through the same visitor isolation.
25
25
  */
26
26
  export function demoDeclaration(value, originVar) {
27
- assert(value && typeof value === 'object' && !Array.isArray(value) && value.version === 1 && Object.keys(value).every(key => ['version', 'domain', 'name', 'maxInstances', 'instanceType', 'settings'].includes(key)), 'invalid-demo-declaration');
27
+ assert(value && typeof value === 'object' && !Array.isArray(value) && value.version === 1 && Object.keys(value).every(key => ['version', 'domain', 'name', 'maxInstances', 'instanceType', 'ownerEmail', 'settings'].includes(key)), 'invalid-demo-declaration');
28
28
  siteDeclaration({ version: 1, domain: value.domain });
29
29
  assert(typeof value.name === 'string' && value.name.trim() === value.name && value.name.length > 0 && value.name.length <= 60 && !/[\p{Cc}\p{Cf}]/u.test(value.name), 'invalid-demo-name');
30
30
  const maxInstances = value.maxInstances ?? 10;
31
31
  assert(Number.isSafeInteger(maxInstances) && maxInstances >= 1 && maxInstances <= 100, 'invalid-demo-max-instances');
32
32
  const instanceType = value.instanceType ?? 'basic';
33
33
  assert(INSTANCE_TYPES.includes(instanceType), 'invalid-demo-instance-type');
34
+ const ownerEmail = value.ownerEmail;
35
+ assert(ownerEmail === undefined || (typeof ownerEmail === 'string' && ownerEmail.length <= 254 && /^[^\s@<>"]+@[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?(?:\.[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?)+$/i.test(ownerEmail)), 'invalid-demo-owner-email');
34
36
  const settings = value.settings ?? {};
35
37
  assert(settings && typeof settings === 'object' && !Array.isArray(settings) && Object.entries(settings).every(([name, setting]) =>
36
38
  /^[A-Z][A-Z0-9_]{0,63}$/.test(name) && !FRONT_SET.test(name) && !SECRET.test(name) && name !== originVar && typeof setting === 'string' && setting.length <= 1024), 'invalid-demo-settings');
37
- return { domain: value.domain, name: value.name, maxInstances, instanceType, settings };
39
+ return { domain: value.domain, name: value.name, maxInstances, instanceType, ownerEmail, settings };
38
40
  }
39
41
 
40
42
  /** The demo Worker's name and its Wrangler configuration; no provider request. */
41
43
  export function demoPlan({ project, environment, compatibilityDate, originVar, hostVariables = [], declaration, turnstileSiteKey }) {
42
44
  assert(/^\d{4}-\d{2}-\d{2}$/.test(compatibilityDate ?? '') && /^[A-Z][A-Z0-9_]{0,63}$/.test(originVar ?? ''), 'invalid-plan');
43
- const { domain, name, maxInstances, instanceType, settings } = demoDeclaration(declaration, originVar);
45
+ const { domain, name, maxInstances, instanceType, ownerEmail, settings } = demoDeclaration(declaration, originVar);
44
46
  const worker = `${ownerWorkerName(project, environment)}--demo`, hostname = `demo.${domain}`, container = worker.replace(/-+/g, '-');
45
47
  assert(resourceName(worker), 'owner-name-too-long');
46
48
  assert(Array.isArray(hostVariables) && hostVariables.every(variable => typeof variable === 'string' && /^[A-Z][A-Z0-9_]{0,63}$/.test(variable)), 'invalid-demo-host-variables');
@@ -60,10 +62,13 @@ export function demoPlan({ project, environment, compatibilityDate, originVar, h
60
62
  routes: hostnames.map(pattern => ({ pattern, custom_domain: true })),
61
63
  observability: { enabled: true, head_sampling_rate: 1, logs: { enabled: true, invocation_logs: false } },
62
64
  vars: { DEMO_ORIGIN: `https://${hostname}`, DEMO_SITE: `https://${domain}`, DEMO_NAME: name, DEMO_ORIGIN_VAR: originVar, DEMO_MAX_INSTANCES: String(maxInstances), DEMO_SETTINGS: JSON.stringify(settings), DEMO_SECONDARY_ORIGINS: JSON.stringify(secondaryHosts.map(host => `https://${host}`)),
63
- ...(turnstileSiteKey ? { TURNSTILE_SITE_KEY: turnstileSiteKey } : {}) },
65
+ ...(ownerEmail ? { DEMO_OWNER_EMAIL: ownerEmail } : {}), ...(turnstileSiteKey ? { TURNSTILE_SITE_KEY: turnstileSiteKey } : {}) },
64
66
  durable_objects: { bindings: [{ name: 'DEMO', class_name: 'DemoContainer' }, { name: 'CAPACITY', class_name: 'DemoCapacity' }] },
65
67
  migrations: [{ tag: 'v1', new_sqlite_classes: ['DemoContainer', 'DemoCapacity'] }],
66
- containers: [{ name: container, class_name: 'DemoContainer', image: './image/Dockerfile', max_instances: maxInstances, instance_type: instanceType }],
68
+ // Admission (DemoCapacity) holds live demos at maxInstances. The provider's
69
+ // own limit keeps headroom for instances it still counts after a demo ends
70
+ // or a start fails, and rollouts leave a live demo (at most an hour old) alone.
71
+ containers: [{ name: container, class_name: 'DemoContainer', image: './image/Dockerfile', max_instances: maxInstances * 2, instance_type: instanceType, rollout_active_grace_period: 3600 }],
67
72
  ratelimits: [{ name: 'STARTS', namespace_id: namespace, simple: { limit: 3, period: 60 } }],
68
73
  };
69
74
  return { worker, hostname, hostnames, container, record: demoRecord({ worker, hostname, hostnames, container }), origin: `https://${hostname}`, maxInstances, instanceType, settings: Object.keys(settings), wrangler };
@@ -33,7 +33,8 @@ async function go(resume=false){
33
33
  let response=await fetch(resume?'/__demo/status':p.form.action,{...(resume?{}:{method:'POST',body:new URLSearchParams(new FormData(p.form))}),headers:{accept:'application/json'},signal:controller.signal});
34
34
  let result=await response.json().catch(()=>({}));
35
35
  while(response.status===202&&result.pending===true){
36
- await new Promise(resolve=>setTimeout(resolve,500));
36
+ // The server holds each pending answer until the start changes.
37
+ await new Promise(resolve=>setTimeout(resolve,100));
37
38
  if(attempt!==controller)return;
38
39
  response=await fetch('/__demo/status',{headers:{accept:'application/json'},signal:controller.signal});
39
40
  result=await response.json().catch(()=>({}));
@@ -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, ends } = options, id = demo.ctx.id.toString();
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.startTimeout + 1000);
12
- if (!await bounded(demo.env.CAPACITY.get(demo.env.CAPACITY.idFromName('capacity')).extend(id, ends), 2000) || demo.stopped) throw new Error('start-reservation-expired');
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
- try { return progress(request, env, id, await bounded(env.DEMO.get(id).status(), 2000)); }
71
- 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);
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 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);
101
- return progress(request, env, id, { ...state, correlation });
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
- try {
138
- const state = await bounded(env.DEMO.get(id).status(), 2000);
139
- if (state.phase === 'starting') return progress(request, env, id, state);
140
- } 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);
141
160
  }
142
161
  return env.DEMO.get(id).fetch(request);
143
162
  }