@runeditrun/rer 0.2.17 → 0.2.19

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 (45) hide show
  1. package/cli/contract.mjs +3 -0
  2. package/cli/demo-host.mjs +2 -0
  3. package/cli/deploy-command.mjs +1 -1
  4. package/cli/inspect.mjs +4 -2
  5. package/cli/main.mjs +2 -0
  6. package/cli/output.mjs +7 -3
  7. package/cli/parse.mjs +5 -4
  8. package/cli/signin.mjs +41 -0
  9. package/cli/stdio.mjs +12 -0
  10. package/global/CHANGELOG.md +9 -0
  11. package/global/main.mjs +3 -3
  12. package/global/output.mjs +1 -12
  13. package/modules/demo-host/README.md +25 -6
  14. package/modules/demo-host/capacity.mjs +27 -6
  15. package/modules/demo-host/container.mjs +37 -9
  16. package/modules/demo-host/entrypoint.sh +9 -0
  17. package/modules/demo-host/image.mjs +18 -0
  18. package/modules/demo-host/pages.mjs +7 -7
  19. package/modules/demo-host/plan.mjs +1 -1
  20. package/modules/demo-host/prepare.mjs +28 -0
  21. package/modules/demo-host/start-script.mjs +11 -4
  22. package/modules/demo-host/startup.mjs +25 -0
  23. package/modules/demo-host/template.mjs +71 -0
  24. package/modules/demo-host/worker.mjs +74 -31
  25. package/modules/deploy-kit/README.md +2 -0
  26. package/modules/deploy-kit/owner-cli.mjs +5 -2
  27. package/modules/deploy-kit/owner-signin.mjs +34 -0
  28. package/modules/mail/README.md +44 -2
  29. package/modules/mail/src/adapters/rer/contract.mjs +22 -0
  30. package/modules/mail/src/adapters/rer/index.mjs +30 -0
  31. package/modules/mail/src/core/transport.mjs +1 -1
  32. package/modules/mail/src/core/validation.mjs +6 -2
  33. package/modules/mail/src/sender.mjs +2 -1
  34. package/modules/operator-auth/README.md +43 -0
  35. package/modules/operator-auth/accounts/email-change.mjs +34 -0
  36. package/modules/operator-auth/accounts/owner-signin.mjs +50 -0
  37. package/modules/operator-auth/accounts/schema/owner-signin.sql +10 -0
  38. package/modules/operator-auth/identity/email-link/confirmation.mjs +3 -3
  39. package/modules/operator-auth/identity/email-link/index.mjs +1 -1
  40. package/modules/operator-auth/index.d.mts +8 -2
  41. package/modules/operator-auth/owner-signin-grant.mjs +10 -0
  42. package/modules/operator-auth/routes.mjs +11 -1
  43. package/modules/operator-auth/service.mjs +4 -0
  44. package/modules/portability/provider-data.mjs +1 -1
  45. package/package.json +1 -1
package/cli/contract.mjs CHANGED
@@ -23,6 +23,9 @@ export const verbs = Object.fromEntries(Object.entries(definitions).map(([name,
23
23
  // `rer demo` is `rer dev --demo`; describe lists it as a verb with the dev
24
24
  // flags a demo takes whenever the seed's dev takes --demo.
25
25
  export const demoVerb = Object.freeze({ flags: [...commonFlags, '--port', '--host', '--idle-timeout', '--stop', '--detach', '--foreground', '--no-queue'], effects: 'A throwaway demo of this seed (rer dev --demo): fresh data in .rer/demo/START (deleted when it stops: --stop, Ctrl-C or its end, one hour by default or --idle-timeout), the saved owner signed in, the seed\'s demo data, a Demo bar, and nothing sent (RER_DEMO=1; the checkout\'s own data is never touched).' });
26
+ // `rer signin` is rer's own, not a seed operation: any seed rer deploys, it can
27
+ // sign the owner in to, so no seed declares it and check does not ask for it.
28
+ export const signinVerb = Object.freeze({ flags: [...commonFlags, '--environment', '--env-file'], effects: 'A one-time sign-in link for the hosted owner, without mail. Uses the Cloudflare credentials that deployed --environment (default production; from the environment or --env-file) to write a single-use grant for the recorded owner into that deployment\'s auth database, then prints the link once to this terminal; it is never saved. The link works once, only for that owner, expires within 15 minutes, and its use is recorded in the owner\'s audit log. Signed in this way, the owner can correct a wrong email in settings.' });
26
29
  export const aliases = Object.fromEntries([...Object.keys(verbs).filter(v => v !== 'install'), 'release', 'deps', 'rer'].map(name => [name, name === 'deps' ? 'install' : name === 'rer' ? null : name]));
27
30
  export const releaseFlags = ['--repo', '--project', '--repository', '--output', '--ref', '--dry-run', '--tag', '--clean-history', '--onto', '--env-file', '--no-queue'];
28
31
  export const requiredHooks = ['preview', 'dependencies', 'build', 'migrate', 'start', 'health', 'owner', 'signIn', 'verify', 'deploy', 'portability', 'state', 'settings', 'input'];
package/cli/demo-host.mjs CHANGED
@@ -4,6 +4,7 @@ import { dirname, join } from 'node:path';
4
4
  import { contained } from './project.mjs';
5
5
  import { invalid, missing, refuse, RerError } from './errors.mjs';
6
6
  import { imageFolder } from './container.mjs';
7
+ import { prepareDemoImage } from '../modules/demo-host/image.mjs';
7
8
  import { DEMO_FILES, INSTANCE_TYPES, demoPlan } from '../modules/demo-host/plan.mjs';
8
9
 
9
10
  // `rer deploy --demo [--dry-run]`: the seed's hosted demo at demo.<domain>
@@ -27,6 +28,7 @@ export async function demoHostCommand(project, options, store, signal, out, oper
27
28
  await mkdir(folder, { recursive: true, mode: 0o700 });
28
29
  await store.step('Building the demo container image (workerd engine)');
29
30
  await imageFolder(project, operation, { ...app, secrets: [...(app.secrets ?? []), { binding: 'RER_DEMO_OPERATOR_TOKEN' }, { binding: 'TURNSTILE_SECRET_KEY' }] }, { output: join(folder, 'image'), engine: 'workerd' });
31
+ await prepareDemoImage(join(folder, 'image'));
30
32
  for (const file of DEMO_FILES) await cp(new URL(`../modules/demo-host/${file}`, import.meta.url), join(folder, file));
31
33
  await writeFile(join(folder, 'wrangler.json'), JSON.stringify(plan.wrangler, null, 2) + '\n');
32
34
  const summary = { worker: plan.worker, origin: plan.origin, hostname: plan.hostname, folder: '.rer/demo-host', maxInstances: plan.maxInstances, settings: plan.settings, instanceType: plan.instanceType, startsPerIpPerMinute: 3, minutes: 60, idleMinutes: 15, enableInternet: false };
@@ -25,7 +25,7 @@ export function validateDeployOptions(options) {
25
25
 
26
26
  // An explicit adapter declaration wins. Older hooks use a conventional root
27
27
  // declaration; discover it by its schema, never by seed identity or hook source.
28
- async function declarationFor(root, adapter) {
28
+ export async function declarationFor(root, adapter) {
29
29
  const declared = adapter.deployDeclaration ?? adapter.deploy?.declaration;
30
30
  const paths = declared ? [declared] : (await readdir(root)).filter(path => path.endsWith('.json') && path !== adapter.site?.declaration);
31
31
  const matches = [];
package/cli/inspect.mjs CHANGED
@@ -3,7 +3,7 @@ import { inventory } from './export.mjs';
3
3
  import { lstat, readFile } from 'node:fs/promises';
4
4
  import { createHash } from 'node:crypto';
5
5
  import { delimiter, dirname, resolve } from 'node:path';
6
- import { aliases, demoVerb, pins, requiredHooks, serverDefaults, verbs, releaseFlags } from './contract.mjs';
6
+ import { aliases, demoVerb, pins, requiredHooks, serverDefaults, signinVerb, verbs, releaseFlags } from './contract.mjs';
7
7
  import { runningVersion } from './version.mjs';
8
8
  import { contained, readPackage } from './project.mjs';
9
9
  import { shared } from './shared.mjs';
@@ -41,7 +41,9 @@ export async function describe(project) {
41
41
  ? operationsEffects
42
42
  : verbs[verb]?.effects ?? 'Produce releases; keygen writes a public anchor and, on success, its private key to non-terminal stdout; sign writes a detached signature and prints (but does not execute) the GitHub upload command; receipt verifies against local anchors offline.';
43
43
  return [verb, { flags: (verbs[verb]?.flags ?? releaseFlags).filter(flag => capability.flags?.includes(flag)), effects }];
44
- }).concat(capabilities.dev?.flags?.includes('--demo') ? [['demo', { flags: demoVerb.flags.filter(flag => capabilities.dev.flags.includes(flag)), effects: demoVerb.effects }]] : [])),
44
+ }).concat(capabilities.dev?.flags?.includes('--demo') ? [['demo', { flags: demoVerb.flags.filter(flag => capabilities.dev.flags.includes(flag)), effects: demoVerb.effects }]] : [])
45
+ // rer signin is rer's own: any seed it can deploy, it can sign the owner in to.
46
+ .concat(capabilities.deploy ? [['signin', { flags: signinVerb.flags, effects: signinVerb.effects }]] : [])),
45
47
  aliases: Object.fromEntries(Object.entries(aliases).filter(([alias, verb]) => pkg.scripts?.[alias] === commandLine(verb, declaredPackage(pkg) !== undefined))),
46
48
  local: { state: project.adapter?.state, settings: project.adapter?.settings ?? [], installerState: '.seed/installer', outbox: '/__dev/outbox', ...await localState(project), origin: await savedOrigin(project), ...(project.adapter?.devIdentity ? { devIdentity: true, devKey: '.rer/local/dev-key.env' } : {}) },
47
49
  // This pin serves a checkout through the machine's front door (rer door).
package/cli/main.mjs CHANGED
@@ -39,6 +39,8 @@ export async function main(argv = process.argv.slice(2)) {
39
39
  if (!options.json) out.human(JSON.stringify(result, null, 2));
40
40
  out.result(result.ok, result.ok ? 0 : 3, { diagnostics: result }); return result.ok ? 0 : 3;
41
41
  }
42
+ // The owner link is printed once to this terminal: never through the logged lifecycle.
43
+ if (options.verb === 'signin') { out.result(true, 0, await (await import('./signin.mjs')).signinCommand(project, options, out)); return 0; }
42
44
  // A checkout without a declared run site refuses --site before any effect.
43
45
  if (options.site) (await import('./site.mjs')).declaredSite(project);
44
46
  const bounded=await (await import('./bounded.mjs')).boundedCommand(project,options,argv,out);
package/cli/output.mjs CHANGED
@@ -1,10 +1,14 @@
1
- import { writeSync } from 'node:fs';
1
+ import { handleBrokenPipes } from './stdio.mjs';
2
2
  export function output(options) {
3
- let closed = false, finished = false;
3
+ handleBrokenPipes();
4
+ let finished = false;
4
5
  // A bounded child retains the caller's human progress but sends its closing
5
6
  // outcome as data, so only the drained outer workflow renders the result.
6
7
  const structuredResult = options.json || process.env.RER_BOUNDED_OUTPUT === '1';
7
- const write = (fd, text) => { if (closed && fd === 1) return; try { writeSync(fd, text); } catch (error) { if (error.code === 'EPIPE') closed = true; else if (!['EAGAIN', 'EWOULDBLOCK'].includes(error.code)) throw error; } };
8
+ // Pipes can accept only part of a synchronous write (512 bytes on macOS).
9
+ // Writable streams queue every byte under backpressure; the entry points set
10
+ // exitCode so Node drains that queue before exiting naturally.
11
+ const write = (fd, text) => { const stream = fd === 1 ? process.stdout : process.stderr; if (!stream.destroyed) stream.write(text); };
8
12
  return {
9
13
  event(event, details = {}) { write(1, JSON.stringify({ version: 1, verb: options.verb ?? 'unknown', event, ...details }) + '\n'); },
10
14
  human(text) { write(1, text + '\n'); },
package/cli/parse.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  import { resolve } from 'node:path';
2
- import { commonFlags, serverDefaults, verbs } from './contract.mjs';
2
+ import { commonFlags, serverDefaults, signinVerb, verbs } from './contract.mjs';
3
3
  import { invalid, refuse } from './errors.mjs';
4
4
  import { isLoopbackHost } from '../modules/operator-auth/loopback.mjs';
5
5
  const booleans = new Set(['--help', '--json', '--non-interactive', '--yes', '--dry-run', '--open', '--stop', '--detach', '--foreground', '--no-queue', '--login', '--legacy-v1', '--acknowledge-unverified-metadata', '--door', '--direct', '--site', '--remove', '--demo']);
@@ -22,7 +22,7 @@ export function parse(argv, cwd = process.cwd(), { independent = false, deferTai
22
22
  // Independent projects own verb names; only the common wrapper flags apply
23
23
  // to unknown names. Profile resolution rejects undeclared commands later.
24
24
  if (!/^[a-z][a-z0-9-]*$/.test(verb)) throw invalid('Unknown rer command. Run rer --help.');
25
- const allowed = new Set(independent ? [...commonFlags,...(['install','setup','build','verify','deploy','release','export','import','dev','preview'].includes(verb)?['--no-queue']:[])] : verbs[verb]?.flags ?? commonFlags), options = { verb, cwd: resolve(cwd), forwarded: [], supplied: new Set() };
25
+ const allowed = new Set(independent ? [...commonFlags,...(['install','setup','build','verify','deploy','release','export','import','dev','preview'].includes(verb)?['--no-queue']:[])] : (verbs[verb] ?? (verb === 'signin' ? signinVerb : undefined))?.flags ?? commonFlags), options = { verb, cwd: resolve(cwd), forwarded: [], supplied: new Set() };
26
26
  let separator = false;
27
27
  for (let i = 0; i < args.length; i++) {
28
28
  const flag = args[i];
@@ -89,8 +89,9 @@ export function parse(argv, cwd = process.cwd(), { independent = false, deferTai
89
89
  }
90
90
 
91
91
  export function help(verb) {
92
- if (!verb) return `Usage: rer VERB [FLAGS]\nCommands: ${Object.keys(verbs).join(', ')}, release\nRun rer VERB --help for flags.\n`;
93
- return `Usage: rer ${verb} [FLAGS]\nFlags: ${(verbs[verb]?.flags ?? commonFlags).join(', ')}\n${verbs[verb]?.effects ?? 'Execute the declared OPERATIONS v2 argv with explicit arguments after --.'}\n`;
92
+ if (!verb) return `Usage: rer VERB [FLAGS]\nCommands: ${Object.keys(verbs).join(', ')}, signin, release\nRun rer VERB --help for flags.\n`;
93
+ const known = verbs[verb] ?? (verb === 'signin' ? signinVerb : undefined);
94
+ return `Usage: rer ${verb} [FLAGS]\nFlags: ${(known?.flags ?? commonFlags).join(', ')}\n${known?.effects ?? 'Execute the declared OPERATIONS v2 argv with explicit arguments after --.'}\n`;
94
95
  }
95
96
 
96
97
  function distance(a,b){let row=Array.from({length:b.length+1},(_,i)=>i);for(let i=1;i<=a.length;i++){const next=[i];for(let j=1;j<=b.length;j++)next[j]=Math.min(next[j-1]+1,row[j]+1,row[j-1]+(a[i-1]===b[j-1]?0:1));row=next;}return row[b.length];}
package/cli/signin.mjs ADDED
@@ -0,0 +1,41 @@
1
+ import { declarationFor } from './deploy-command.mjs';
2
+ import { deployEnvironment, deployAgain } from './deploy-inputs.mjs';
3
+ import { hookState } from './deploy-hook-record.mjs';
4
+ import { missing, refuse, RerError } from './errors.mjs';
5
+ import { shared } from './shared.mjs';
6
+ import { main as owner } from '../modules/deploy-kit/owner-cli.mjs';
7
+ import { deployDiagnostic, providerRefusal } from '../modules/deploy-kit/diagnostics.mjs';
8
+
9
+ // rer signin: the owner's way into a hosted install without mail. With the same
10
+ // Cloudflare credentials that deployed it, rer writes a one-time grant (only its
11
+ // SHA-256) into the deployment's auth D1 and prints the link once to this terminal.
12
+ // It never reaches a log, receipt or the deploy record; the Worker admits it only
13
+ // for the recorded owner, once, within 15 minutes, and logs its use.
14
+ export async function signinCommand(project, options, out, { fetch } = {}) {
15
+ if (project.profile !== 'first-party' || typeof project.adapter?.deploy !== 'function') throw refuse('rer signin needs a seed with an owner-account deploy declaration.');
16
+ const root = project.root, environment = options.environment ?? 'production', again = deployAgain(options).replace(/^rer deploy/, 'rer signin');
17
+ const { path, value: declaration } = await declarationFor(root, project.adapter);
18
+ const { env } = await deployEnvironment({ root, options, declaration, localOnlyKeys: project.adapter.localOnlyKeys ?? [] });
19
+ if (['CLOUDFLARE_ACCOUNT_ID', 'CLOUDFLARE_API_TOKEN'].some(name => !env[name]?.trim())) throw missing('rer signin uses the Cloudflare credentials that deployed this environment: set CLOUDFLARE_ACCOUNT_ID and CLOUDFLARE_API_TOKEN in the environment or an --env-file, then run it again.');
20
+ const { createRedaction, credentialValues } = await shared('redaction.mjs');
21
+ const redaction = createRedaction();
22
+ redaction.addSecrets([...credentialValues(env), env.CLOUDFLARE_ACCOUNT_ID]);
23
+ const state = await hookState(root, environment);
24
+ let result;
25
+ try { result = await owner(['signin', '--declaration', path, '--environment', environment, ...(state ? ['--state', state] : [])], env, { cwd: root, localOnlyKeys: project.adapter.localOnlyKeys ?? [], ...(fetch ? { fetch } : {}) }); }
26
+ catch (error) { throw failure(error, { environment, again, redact: redaction.redact }); }
27
+ const minutes = Math.round((result.expiresAt - Date.now()) / 60000);
28
+ if (!options.json) out.human(`One-time sign-in link for the owner ${result.email}. It works once, only for that owner, and expires in ${minutes} minutes. rer does not save it; open it now:\n${result.link}`);
29
+ return { owner: result.email, expiresAt: result.expiresAt, ...(options.json ? { link: result.link } : {}) };
30
+ }
31
+
32
+ function failure(error, { environment, again, redact }) {
33
+ const code = deployDiagnostic(error, redact)?.code ?? error.message;
34
+ if (code === 'missing-owner-receipt') return refuse(`This checkout has no completed rer deployment of the ${environment} environment. Run rer signin from the checkout that deployed it (rer deploy --environment ${environment}).`);
35
+ if (code === 'owned-resource-replaced') return refuse(`The ${environment} Worker is no longer the one this checkout deployed, so rer will not sign anyone in to it. Run rer deploy --environment ${environment} first.`);
36
+ if (code === 'owner-signin-unsupported') return refuse(`The ${environment} deployment's database has no owner sign-in table. Update the seed to a rer that includes operator-auth's accounts/schema/owner-signin.sql migration, run rer deploy --environment ${environment}, then ${again}.`);
37
+ if (code === 'owner-signin-ambiguous') return refuse(`More than one D1 database of the ${environment} deployment holds an owner sign-in table, so rer cannot tell which one signs in the owner. Nothing was written.`);
38
+ if (code === 'owner-not-claimed') return refuse(`No owner has claimed the ${environment} deployment yet. Run rer deploy --environment ${environment}: it claims the owner. Nothing was written.`);
39
+ const refusal = providerRefusal(error, redact);
40
+ return new RerError('command_failed', `rer signin failed: ${/^[a-zA-Z0-9_-]{1,120}$/.test(code) ? redact(code) : 'signin-failed'}${refusal ? ` (${refusal})` : ''}. No sign-in link was issued; run ${again} again.`, 1);
41
+ }
package/cli/stdio.mjs ADDED
@@ -0,0 +1,12 @@
1
+ let installed = false;
2
+
3
+ export function handleBrokenPipes() {
4
+ if (installed) return;
5
+ installed = true;
6
+ for (const stream of [process.stdout, process.stderr]) {
7
+ stream.on('error', error => {
8
+ if (error.code === 'EPIPE') return;
9
+ throw error;
10
+ });
11
+ }
12
+ }
@@ -1,5 +1,14 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.2.19
4
+
5
+ - `rer signin [--environment NAME]` uses the credentials that deployed a seed to print a one-time owner sign-in link, valid for 15 minutes. It requires no email delivery, refuses non-owners and used or expired links, and records successful use in the auth audit log.
6
+ - A freshly signed-in owner can correct their sign-in email in Operator access. The global command installs the checkout's pinned rer package before running `signin`.
7
+
8
+ ### Upgrade notes
9
+
10
+ - Update the exact rer package pin or refresh vendored commands and operator-auth. Apply `modules/operator-auth/accounts/schema/owner-signin.sql` as a new numbered migration, then redeploy. The reference seed includes the migration and README instructions. Run `rer signin` from the checkout that deployed the environment with its Cloudflare credentials.
11
+
3
12
  ## 0.2.17
4
13
 
5
14
  - Container builds check the app's reachable runtime imports from Wrangler main and fresh server output. Dormant source files and ordinary calls containing cloudflare-looking strings no longer fail the boundary; reachable Node-engine restrictions remain.
package/global/main.mjs CHANGED
@@ -30,13 +30,13 @@ pins (installed first by any verb when node_modules holds another), else its ven
30
30
  Run COMMAND once this computer's job queue admits it (heavy package scripts
31
31
  of a checkout that pins the rer package use it).
32
32
 
33
- Checkout verbs: install, setup, dev, demo, preview, build, verify, reset, deploy, export, import,
34
- outbox, doctor, describe, check, release. Run rer VERB --help in a checkout for its flags.
33
+ Checkout verbs: install, setup, dev, demo, preview, build, verify, reset, deploy, signin, export,
34
+ import, outbox, doctor, describe, check, release. Run rer VERB --help in a checkout for its flags.
35
35
  `;
36
36
 
37
37
  // The checkout verbs that install the pinned package before they run when
38
38
  // node_modules holds another version or none (dev installs it in its launch).
39
- const installsFirst = new Set([...Object.keys(verbs), 'release'].filter(verb => verb !== 'dev'));
39
+ const installsFirst = new Set([...Object.keys(verbs), 'signin', 'release'].filter(verb => verb !== 'dev'));
40
40
 
41
41
  export async function main(argv) {
42
42
  handleBrokenPipes();
package/global/output.mjs CHANGED
@@ -1,12 +1 @@
1
- let installed = false;
2
-
3
- export function handleBrokenPipes() {
4
- if (installed) return;
5
- installed = true;
6
- for (const stream of [process.stdout, process.stderr]) {
7
- stream.on('error', error => {
8
- if (error.code === 'EPIPE') return;
9
- throw error;
10
- });
11
- }
12
- }
1
+ export { handleBrokenPipes } from '../cli/stdio.mjs';
@@ -9,8 +9,11 @@ it with these files and the seed's container image folder (workerd engine) to
9
9
 
10
10
  - `worker.mjs`: `GET /` is the start page; `POST /__demo/start` checks the
11
11
  exact Origin, the `STARTS` rate limit (3 per IP per 60 s), Turnstile's
12
- siteverify and capacity, then starts the visitor's demo and answers with the
13
- owner claim's 303, its session cookie and the `rer_demo` cookie. A request
12
+ siteverify and capacity, then starts the visitor's demo. It answers with
13
+ 202 progress and a `rer_demo` cookie; `GET /__demo/status` polls that same
14
+ copy until it can relay the owner claim's 303 (JSON `{ location }` for the
15
+ page script) and session cookie. Every external admission step is bounded:
16
+ the start answers within 12 seconds, and status within two seconds. A request
14
17
  with that cookie goes to the visitor's Durable Object; nothing else reaches
15
18
  a container.
16
19
  - `container.mjs`: `DemoContainer`, one per visitor (`newUniqueId`, checked
@@ -28,11 +31,23 @@ it with these files and the seed's container image folder (workerd engine) to
28
31
  minutes, and arms the next at once, as the Containers class keeps its own
29
32
  object alive. A request or alarm that finds the container gone ends the demo
30
33
  and answers the ended page (410), never a 5xx.
31
- - `capacity.mjs`: `DemoCapacity` admits at most `max_instances` live demos.
34
+ - `capacity.mjs`: `DemoCapacity` counts unexpired reservation rows inside a
35
+ storage transaction, excluding diagnostics and expired rows. A lost start
36
+ handoff expires after one minute; a successful claim extends the reservation
37
+ to the demo's end. Refusals log the held ids as SHA-256 fingerprints (also
38
+ logged with each start's correlation), never visitor bearer cookies.
39
+ - `image.mjs`, `prepare.mjs`, `template.mjs`, `entrypoint.sh`: the demo image
40
+ build migrates fresh workerd persistence with the packaged engine and closes
41
+ it into `/template`, with no network and no app requests. The entrypoint
42
+ copies that state into a fresh `/data` under the host's existing flock;
43
+ partial or foreign volumes are refused. Startup validates the existing
44
+ migration hashes instead of replaying SQL. Owners, sessions and the seed's
45
+ owner-dependent sample data are still created separately for each visitor.
32
46
  - `pages.mjs`: the start and ended pages (12ui designs), strict CSP.
33
47
  `start-script.mjs` is the start page's script: once Turnstile passes it
34
48
  posts the form with `Accept: application/json` (the Worker then answers
35
- `{ location }` with the cookies, or `{ notice }` with the refusal's status)
49
+ 202 `{ pending: true }`, `{ location }` with the cookies, or `{ notice }`
50
+ with the refusal's status)
36
51
  and shows each step: the check, the private copy starting, the sign-in. A
37
52
  refusal, a network failure or a start past 25 seconds is shown with Try
38
53
  again, which runs a new check; progress is announced through a
@@ -55,8 +70,12 @@ Chromium (default-seed's Playwright) through waiting, failure, Try again,
55
70
  working and the signed-in landing. The end-to-end proof (`wrangler dev` with Docker)
56
71
  is in DEMO.md.
57
72
 
58
- Startup probes keep the existing 120-second budget, with a separate five-second
59
- HTTP probe limit. Every attempt logs its timestamp, elapsed time, duration and
73
+ Startup has a 45-second budget, with a separate five-second HTTP probe limit.
74
+ The Durable Object owns that work and cleanup while the page polls; a browser
75
+ that leaves cannot abandon it. Retrying a pending copy resumes the same
76
+ reservation. Failed starts retain the bounded operator diagnostic and destroy
77
+ the container; cleanup retries keep a reservation only while destruction is
78
+ unconfirmed. Every attempt logs its timestamp, elapsed time, duration and
60
79
  HTTP status or fixed error category, including successful probes. Exit records
61
80
  include the numeric platform exit code when supplied. A separate random
62
81
  `x-rer-demo-correlation` response header joins these events without disclosing
@@ -19,15 +19,36 @@ export class DemoCapacity extends DurableObject {
19
19
  async alarm() { await this.ctx.storage.deleteAll(); }
20
20
 
21
21
  async acquire(id, ends, max) {
22
- const now = Date.now(), places = await this.ctx.storage.list();
23
- const over = [...places].filter(([, until]) => until <= now).map(([key]) => key);
24
- if (over.length) await this.ctx.storage.delete(over);
25
- if (places.size - over.length >= max) return false;
26
- await this.ctx.storage.put(id, ends);
27
- return true;
22
+ return this.ctx.storage.transaction(async storage => {
23
+ const now = Date.now(), places = [...await storage.list()]
24
+ .filter(([key, until]) => /^[0-9a-f]{64}$/.test(key) && typeof until === 'number');
25
+ const expired = places.filter(([, until]) => until <= now).map(([key]) => key);
26
+ if (expired.length) await storage.delete(expired);
27
+ const held = places.filter(([key, until]) => key !== id && until > now);
28
+ if (held.length >= max) {
29
+ // Fingerprints identify held reservations without logging bearer cookies.
30
+ const heldIds = await Promise.all(held.map(async ([key, until]) => ({ id: await fingerprint(key), ends: until })));
31
+ console.log(JSON.stringify({ event: 'demo_capacity_refused', max, held: heldIds }));
32
+ return false;
33
+ }
34
+ await storage.put(id, ends);
35
+ return true;
36
+ });
37
+ }
38
+
39
+ async extend(id, ends) {
40
+ return this.ctx.storage.transaction(async storage => {
41
+ if (!((await storage.get(id)) > Date.now())) return false;
42
+ await storage.put(id, ends);
43
+ return true;
44
+ });
28
45
  }
29
46
 
30
47
  async release(id) {
31
48
  await this.ctx.storage.delete(id);
32
49
  }
33
50
  }
51
+
52
+ export async function fingerprint(id) {
53
+ return [...new Uint8Array(await crypto.subtle.digest('SHA-256', new TextEncoder().encode(id)))].slice(0, 12).map(byte => byte.toString(16).padStart(2, '0')).join('');
54
+ }
@@ -2,6 +2,7 @@ import { DurableObject } from 'cloudflare:workers';
2
2
  import { claimKeys, signClaim } from './claim.mjs';
3
3
  import { bounded, boundedFetch, category, report } from './diagnostics.mjs';
4
4
  import { endedPage, clearCookie } from './pages.mjs';
5
+ import { finishStart } from './startup.mjs';
5
6
 
6
7
  // One visitor's demo: a Durable Object (newUniqueId) and its container, the
7
8
  // seed's image from `rer build --platform container` on the workerd engine.
@@ -23,9 +24,14 @@ export class DemoContainer extends DurableObject {
23
24
  constructor(ctx, env) {
24
25
  super(ctx, env);
25
26
  ctx.blockConcurrencyWhile(async () => {
26
- const state = await ctx.storage.get(['correlation', 'started']);
27
+ const state = await ctx.storage.get(['correlation', 'started', 'startup']);
27
28
  this.correlation = state.get('correlation'); this.started = state.get('started');
28
- if (this.container.running) this.watch();
29
+ if (state.get('startup')?.phase === 'starting') {
30
+ // An interrupted start has lost its private claim key and cannot be
31
+ // resumed after object eviction. End it instead of polling forever.
32
+ await ctx.storage.put('startup', { phase: 'failed' });
33
+ ctx.waitUntil(this.end().catch(cause => report(this.correlation, this.started, 'cleanup', { error: category(cause) })));
34
+ } else if (this.container.running) this.watch();
29
35
  });
30
36
  }
31
37
  watch() {
@@ -42,20 +48,34 @@ export class DemoContainer extends DurableObject {
42
48
  }
43
49
  diagnostic() { return { correlation: this.correlation, tail: this.records, stdout: 'Provider container logs; not exposed by the lifecycle API.' }; }
44
50
  idle = IDLE;
45
- startTimeout = 120_000;
51
+ startTimeout = 45_000;
46
52
  probeTimeout = 5000;
47
53
  cleanupRetry = 30_000;
48
54
  hold = 3 * 60_000;
49
55
  get container() { return this.ctx.container; }
50
56
  port() { return this.container.getTcpPort(PORT); }
51
57
 
58
+ async begin(options) {
59
+ if (!await this.ctx.storage.get('startup')) {
60
+ await this.ctx.storage.put('startup', { phase: 'starting' });
61
+ this.ctx.waitUntil(finishStart(this, options));
62
+ }
63
+ return this.status();
64
+ }
65
+
66
+ async status() {
67
+ const startup = await this.ctx.storage.get('startup');
68
+ return { ...(startup ?? { phase: 'failed' }), correlation: this.correlation };
69
+ }
70
+
52
71
  /** Start the container, wait for /health, claim the owner; returns the claim's 303. */
53
72
  async start({ origin, originVar, ends, endsLocal, settings, correlation }) {
54
73
  this.correlation = correlation; this.started = Date.now(); this.stopped = false;
55
74
  const deadline = this.started + this.startTimeout;
56
75
  const id = this.ctx.id.toString(), keys = await claimKeys(), now = Date.now();
57
- await this.ctx.storage.put({ ends, seen: now, correlation, started: this.started });
76
+ await this.ctx.storage.put({ ends, seen: now, correlation, started: this.started, startDeadline: deadline });
58
77
  report(correlation, this.started, 'start');
78
+ if (this.stopped) throw new Error('container-stopped');
59
79
  this.container.start({ enableInternet: false, env: { ...settings, RER_DEMO: '1', [originVar]: origin, RER_INGRESS_ORIGIN: origin,
60
80
  SESSION_SECRET: random(), SETUP_TOKEN: random(), OWNER_EMAIL: OWNER, RER_OWNER_CLAIM_ISSUER: origin, RER_OWNER_CLAIM_INSTALLATION_ID: id,
61
81
  RER_OWNER_CLAIM_PUBLIC_KEYS: JSON.stringify(keys.publicKeys), RER_DEMO_EXPIRES: endsLocal } });
@@ -94,6 +114,7 @@ export class DemoContainer extends DurableObject {
94
114
  async exited(error) {
95
115
  this.stopped = true;
96
116
  this.record('exit', { outcome: error ? category(error) : 'exited', ...(Number.isInteger(error?.exitCode) ? { exitCode: error.exitCode } : {}) });
117
+ if ((await this.ctx.storage.get('startup'))?.phase === 'starting') return;
97
118
  try { await this.end(); } catch (cause) { report(this.correlation, this.started, 'cleanup', { error: category(cause) }); }
98
119
  }
99
120
 
@@ -117,11 +138,14 @@ export class DemoContainer extends DurableObject {
117
138
  }
118
139
 
119
140
  async alarm() {
120
- const state = await this.ctx.storage.get(['ends', 'seen']), now = Date.now();
141
+ const state = await this.ctx.storage.get(['ends', 'seen', 'startDeadline']), now = Date.now();
121
142
  if (await this.ctx.storage.get('stopping')) return this.end();
122
143
  if (!state.get('ends')) return;
123
- const due = Math.min(state.get('ends'), state.get('seen') + this.idle);
124
- if (now >= due || !this.container.running) return this.end();
144
+ const due = Math.min(state.get('ends'), state.get('seen') + this.idle, state.get('startDeadline') ?? Infinity);
145
+ if (now >= due || !this.container.running) {
146
+ if ((await this.ctx.storage.get('startup'))?.phase === 'starting') { this.stopped = true; return; }
147
+ return this.end();
148
+ }
125
149
  await new Promise(resolve => { this.wake = resolve; setTimeout(resolve, Math.min(due - now, this.hold)); });
126
150
  if (!this.stopped) await this.ctx.storage.setAlarm(Date.now());
127
151
  }
@@ -138,8 +162,12 @@ export class DemoContainer extends DurableObject {
138
162
  // Keep the alarm and capacity reservation if destruction fails; retry cleanup.
139
163
  await this.ctx.storage.put('stopping', true);
140
164
  await this.ctx.storage.setAlarm(Date.now() + this.cleanupRetry);
141
- if (this.container.running) await bounded(this.container.destroy(), 5000);
142
- await this.env.CAPACITY.get(this.env.CAPACITY.idFromName('capacity')).release(this.ctx.id.toString());
165
+ if (this.container.running) {
166
+ try { await bounded(this.env.CAPACITY.get(this.env.CAPACITY.idFromName('capacity')).extend(this.ctx.id.toString(), Date.now() + 60_000), 2000); }
167
+ catch (cause) { report(this.correlation, this.started, 'cleanup-reservation', { error: category(cause) }); }
168
+ await bounded(this.container.destroy(), 5000);
169
+ }
170
+ await bounded(this.env.CAPACITY.get(this.env.CAPACITY.idFromName('capacity')).release(this.ctx.id.toString()), 2000);
143
171
  await this.ctx.storage.deleteAlarm();
144
172
  await this.ctx.storage.deleteAll();
145
173
  report(this.correlation, this.started, 'ended');
@@ -0,0 +1,9 @@
1
+ #!/bin/sh
2
+ set -eu
3
+ umask 077
4
+ mkdir -p /data
5
+ # Keep this same lock inode held through template copying and the host lifetime.
6
+ exec flock --nonblock --no-fork /data/host.lock sh -ec '
7
+ node /runtime/modules/demo-host/template.mjs /template /data
8
+ exec node /runtime/modules/container/host.mjs "$@"
9
+ ' sh "$@"
@@ -0,0 +1,18 @@
1
+ import { cp, mkdir, readFile, writeFile } from 'node:fs/promises';
2
+ import { join } from 'node:path';
3
+
4
+ /** Add demo-only preparation to an ordinary workerd image build folder. */
5
+ export async function prepareDemoImage(image) {
6
+ const manifest = JSON.parse(await readFile(join(image, 'app/container.json'), 'utf8'));
7
+ if (manifest.engine !== 'workerd') throw new Error('demo-template-requires-workerd');
8
+ const path = join(image, 'Dockerfile'), dockerfile = await readFile(path, 'utf8');
9
+ const entrypoint = 'ENTRYPOINT ["sh", "/runtime/modules/container/entrypoint.sh"]';
10
+ if (!dockerfile.endsWith(entrypoint + '\n')) throw new Error('demo-image-entrypoint-changed');
11
+ const runtime = join(image, 'runtime/modules/demo-host');
12
+ await mkdir(runtime, { recursive: true });
13
+ for (const file of ['prepare.mjs', 'template.mjs', 'entrypoint.sh']) await cp(new URL(file, import.meta.url), join(runtime, file));
14
+ await writeFile(path, dockerfile.slice(0, -entrypoint.length - 1)
15
+ + 'RUN --network=none node /runtime/modules/demo-host/prepare.mjs\n'
16
+ + 'ENTRYPOINT ["sh", "/runtime/modules/demo-host/entrypoint.sh"]\n');
17
+ return { output: image };
18
+ }
@@ -24,24 +24,24 @@ ol.steps{display:none;list-style:none;margin:22px auto 0;padding:0;width:max-con
24
24
  @media(min-width:600px) and (max-width:1023px){main{width:calc(100% - 64px);max-width:666px;margin:100px auto 48px}.card{padding:36px}h1{font-size:38px;line-height:46px}.intro{font-size:18px;line-height:27px}}
25
25
  @media(max-width:599px){main,.ended main{width:calc(100% - 32px);margin:32px auto}.ended .card{padding:26px 22px}.ended h1{font-size:29px;line-height:36px}.ended .intro{font-size:17px;line-height:25px;margin-bottom:28px}.card{padding:26px 22px;border-radius:16px}.brand{font-size:16px}h1{font-size:34px;line-height:41px;letter-spacing:-.8px}.intro{font-size:17px;line-height:25px}ul{font-size:16px;line-height:25px;margin:22px 0 26px}li{padding-left:22px;margin-bottom:9px}li:before{left:2px;top:9px}.button{font-size:18px;margin-top:22px}.note{font-size:13px;line-height:20px}.steps{font-size:15px}.back{margin-top:24px;font-size:14px}}`;
26
26
 
27
- function html(env, { page, title, body, status = 200, headers = {}, turnstile = false }) {
27
+ function html(env, { page, title, body, status = 200, headers = {}, turnstile = false, refresh = false }) {
28
28
  const nonce = crypto.randomUUID().replaceAll('-', ''), site = new URL(env.DEMO_SITE);
29
29
  const policy = `default-src 'none'; style-src 'nonce-${nonce}'; img-src 'self'; form-action 'self'; base-uri 'none'; frame-ancestors 'none'${turnstile ? `; script-src 'nonce-${nonce}' https://challenges.cloudflare.com; frame-src https://challenges.cloudflare.com; connect-src 'self' https://challenges.cloudflare.com` : ''}`;
30
- return new Response(`<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><meta name="robots" content="noindex"><title>${escape(title)}</title><style nonce="${nonce}">${STYLE}</style>${turnstile ? `<noscript><style nonce="${nonce}">#note{display:none}</style></noscript><script nonce="${nonce}">${START_SCRIPT}</script><script nonce="${nonce}" src="https://challenges.cloudflare.com/turnstile/v0/api.js" async defer></script>` : ''}</head>
30
+ return new Response(`<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><meta name="robots" content="noindex"><title>${escape(title)}</title>${refresh ? '<noscript><meta http-equiv="refresh" content="2;url=/__demo/status"></noscript>' : ''}<style nonce="${nonce}">${STYLE}</style>${turnstile ? `<noscript><style nonce="${nonce}">#note{display:none}</style></noscript><script nonce="${nonce}">${START_SCRIPT}</script><script nonce="${nonce}" src="https://challenges.cloudflare.com/turnstile/v0/api.js" async defer></script>` : ''}</head>
31
31
  <body class="${page}"><main><section class="card" aria-labelledby="title"><div class="mark" aria-hidden="true"></div><p class="brand">${escape(env.DEMO_NAME)}</p>${body}</section><nav class="back" aria-label="Return"><a href="${escape(site.origin)}/">Back to ${escape(site.host)}</a></nav></main></body></html>`,
32
32
  { status, headers: { 'content-type': 'text/html; charset=utf-8', 'cache-control': 'no-store', 'content-security-policy': policy, 'referrer-policy': 'strict-origin', 'x-content-type-options': 'nosniff', ...headers } });
33
33
  }
34
34
 
35
35
  /** GET /: the visitor proves they are human and their demo starts. `notice` explains a refused start. */
36
- export function startPage(env, { status = 200, notice, headers = {} } = {}) {
36
+ export function startPage(env, { status = 200, notice, pending = false, headers = {} } = {}) {
37
37
  const name = escape(env.DEMO_NAME);
38
- return html(env, { page: 'start', title: `Try ${env.DEMO_NAME}`, status, headers, turnstile: true, body: `<h1 id="title">Try ${name}</h1>
38
+ return html(env, { page: 'start', title: `Try ${env.DEMO_NAME}`, status, headers, turnstile: true, refresh: pending, body: `<h1 id="title">Try ${name}</h1>
39
39
  <p class="intro">Your own copy of ${name}, signed in as its owner, with sample data.</p>
40
40
  <ul><li>Nothing is sent: mail stays in an outbox.</li><li>It ends after an hour, or 15 idle minutes.</li><li>Then it is deleted. Nobody else can see it.</li></ul>
41
- <p class="notice" id="notice" role="alert"${notice ? '' : ' hidden'}>${notice ? escape(notice) : ''}</p><form id="start" method="post" action="/__demo/start" data-auto="${notice ? '0' : '1'}"><div class="cf-turnstile" data-sitekey="${escape(env.TURNSTILE_SITE_KEY)}" data-size="flexible" data-callback="rerDemoVerified" data-before-interactive-callback="rerDemoInteractive" data-after-interactive-callback="rerDemoChecking" data-expired-callback="rerDemoExpired" data-error-callback="rerDemoCheckFailed"></div>
42
- <button class="button" type="submit"><span class="spinner" aria-hidden="true"></span><span class="label">Start the demo</span></button>
41
+ <p class="notice" id="notice" role="alert"${notice ? '' : ' hidden'}>${notice ? escape(notice) : ''}</p><form id="start" method="post" action="/__demo/start" data-auto="${notice || pending ? '0' : '1'}"${pending ? ' data-resume="1" data-state="working"' : ''}><div class="cf-turnstile" data-sitekey="${escape(env.TURNSTILE_SITE_KEY)}" data-size="flexible" data-callback="rerDemoVerified" data-before-interactive-callback="rerDemoInteractive" data-after-interactive-callback="rerDemoChecking" data-expired-callback="rerDemoExpired" data-error-callback="rerDemoCheckFailed"></div>
42
+ <button class="button" type="submit"${pending ? ' disabled' : ''}><span class="spinner" aria-hidden="true"></span><span class="label">${pending ? 'Starting your demo…' : 'Start the demo'}</span></button>
43
43
  <ol class="steps" aria-hidden="true"><li data-at="done">Checking you're human</li><li data-at="now">Starting a private copy</li><li data-at="next">Signing you in</li></ol>
44
- <p class="note" id="note">Complete the human check to start your demo.</p><noscript><p class="note">The human check needs JavaScript. Turn it on to start the demo.</p></noscript><p class="visually-hidden" id="status" role="status"></p></form>` });
44
+ <p class="note" id="note">${pending ? 'Your demo will open automatically when ready.' : 'Complete the human check to start your demo.'}</p><noscript><p class="note">The human check needs JavaScript. Turn it on to start the demo.</p></noscript><p class="visually-hidden" id="status" role="status"></p></form>` });
45
45
  }
46
46
 
47
47
  /** A demo whose hour or idle time is up: its container and data are gone. */
@@ -9,7 +9,7 @@ import { siteDeclaration } from '../deploy-kit/site.mjs';
9
9
  * running the seed's own image (`rer build --platform container`, workerd
10
10
  * engine), deployed by `wrangler deploy` with the image beside it. */
11
11
 
12
- export const DEMO_FILES = ['worker.mjs', 'container.mjs', 'capacity.mjs', 'claim.mjs', 'pages.mjs', 'start-script.mjs', 'diagnostics.mjs', 'operator.mjs'];
12
+ export const DEMO_FILES = ['worker.mjs', 'container.mjs', 'startup.mjs', 'capacity.mjs', 'claim.mjs', 'pages.mjs', 'start-script.mjs', 'diagnostics.mjs', 'operator.mjs'];
13
13
  const FRONT_SET = /^(?:RER_.*|SESSION_SECRET|SETUP_TOKEN|OWNER_EMAIL)$/;
14
14
  const SECRET = /SECRET|TOKEN|PASSWORD|_KEY$/;
15
15
  // Cloudflare Containers' predefined instance types, smallest first (developers.cloudflare.com/containers/platform-details/limits).
@@ -0,0 +1,28 @@
1
+ import { mkdir, readFile } from 'node:fs/promises';
2
+ import { join } from 'node:path';
3
+ import { workerEngine } from '../container/worker-engine.mjs';
4
+ import { sealTemplate } from './template.mjs';
5
+
6
+ /** Migrate with the packaged engine; never dispatch an app request or claim. */
7
+ export async function prepareTemplate({ root = '/app', data = '/template' } = {}) {
8
+ const manifest = JSON.parse(await readFile(join(root, 'container.json'), 'utf8'));
9
+ if (manifest.engine !== 'workerd') throw new Error('demo-template-requires-workerd');
10
+ await mkdir(data, { mode: 0o700 });
11
+ let engine;
12
+ try {
13
+ engine = await workerEngine({ root, data, manifest, settings: { RER_DEMO: '1' } });
14
+ } finally {
15
+ await engine?.close();
16
+ }
17
+ return sealTemplate(data, manifest);
18
+ }
19
+
20
+ if (process.argv[1] === import.meta.filename) {
21
+ try { await prepareTemplate(); }
22
+ catch (error) {
23
+ // A failed engine constructor may still own workerd handles. The build must
24
+ // fail immediately; there is no usable template to keep alive or recover.
25
+ console.error('Demo template preparation failed:', error.name);
26
+ process.exit(1);
27
+ }
28
+ }
@@ -24,14 +24,20 @@ function waiting(){
24
24
  show('waiting',{label:'Waiting for the human check…',note:auto?'Your demo will open automatically when ready.':'Once the check passes, press Start the demo.'});
25
25
  }
26
26
  function stop(){timers.forEach(clearTimeout);timers=[]}
27
- async function go(){
27
+ async function go(resume=false){
28
28
  const p=el(),controller=attempt=new AbortController();
29
29
  show('working',{label:'Starting your demo…',note:'Your demo will open automatically when ready.',live:'Check passed. Starting a private copy.',step:1,notice:''});
30
30
  timers.push(setTimeout(()=>p.note.textContent='Taking a little longer than usual. Still starting…',LONG));
31
31
  timers.push(setTimeout(()=>show('slow',{label:'Try again',note:'Still starting. It will open by itself if it finishes.',notice:'This is taking longer than it should. Keep waiting, or try again.'}),SLOW));
32
32
  try{
33
- const response=await fetch(p.form.action,{method:'POST',body:new URLSearchParams(new FormData(p.form)),headers:{accept:'application/json'},signal:controller.signal});
34
- const result=await response.json().catch(()=>({}));
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
+ let result=await response.json().catch(()=>({}));
35
+ while(response.status===202&&result.pending===true){
36
+ await new Promise(resolve=>setTimeout(resolve,500));
37
+ if(attempt!==controller)return;
38
+ response=await fetch('/__demo/status',{headers:{accept:'application/json'},signal:controller.signal});
39
+ result=await response.json().catch(()=>({}));
40
+ }
35
41
  if(attempt!==controller)return;
36
42
  stop();
37
43
  if(response.ok&&typeof result.location==='string'){
@@ -60,7 +66,8 @@ window.rerDemoChecking=()=>checking()&&waiting();
60
66
  window.rerDemoExpired=()=>checking()&&waiting();
61
67
  window.rerDemoCheckFailed=()=>checking()&&show('failed',{label:'Try again',note:'',live:'',notice:'The human check did not load. Try again.'});
62
68
  addEventListener('DOMContentLoaded',()=>{
63
- if(!el().form.dataset.state)waiting();
69
+ if(el().form.dataset.resume==='1')go(true);
70
+ else if(!el().form.dataset.state)waiting();
64
71
  el().form.addEventListener('submit',event=>{
65
72
  event.preventDefault();
66
73
  const state=el().form.dataset.state;
@@ -0,0 +1,25 @@
1
+ import { bounded, category, report } from './diagnostics.mjs';
2
+ import { fingerprint } from './capacity.mjs';
3
+
4
+ // Kept by the visitor's Durable Object while the front returns short progress
5
+ // responses. A dropped browser request cannot abandon cleanup or hold a place
6
+ // until the demo's hour expires.
7
+ export async function finishStart(demo, options) {
8
+ const { correlation, ends } = options, id = demo.ctx.id.toString();
9
+ try {
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');
13
+ await demo.ctx.storage.put('startup', { phase: 'ready', ...result });
14
+ await demo.ctx.storage.delete('startDeadline');
15
+ } catch (error) {
16
+ const failure = ['health-deadline', 'container-stopped', 'start-reservation-expired'].includes(error.message) ? error.message : category(error);
17
+ demo.record('failed', { error: failure });
18
+ try {
19
+ const diagnostic = await bounded(demo.diagnostic(), 3000);
20
+ await bounded(demo.env.CAPACITY.get(demo.env.CAPACITY.idFromName('diagnostics')).record(correlation, { ...diagnostic, failure }), 3000);
21
+ } catch (capture) { report(correlation, demo.started, 'diagnostic', { error: category(capture) }); }
22
+ await demo.ctx.storage.put('startup', { phase: 'failed' });
23
+ try { await demo.end(); } catch (cleanup) { report(correlation, demo.started, 'cleanup', { error: category(cleanup) }); }
24
+ }
25
+ }