@runeditrun/rer 0.2.17 → 0.2.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/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 };
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/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
+ }
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
+ }
@@ -0,0 +1,71 @@
1
+ import { createHash } from 'node:crypto';
2
+ import { cp, lstat, mkdir, readdir, readFile, rename, writeFile } from 'node:fs/promises';
3
+ import { join } from 'node:path';
4
+
5
+ const descriptor = '.rer-demo-template.json';
6
+ const initialized = '.rer-demo-initialized.json';
7
+ const copying = '.rer-demo-initializing.json';
8
+
9
+ async function inventory(directory, prefix = '') {
10
+ const entries = [];
11
+ for (const entry of (await readdir(directory, { withFileTypes: true })).sort((a, b) => a.name.localeCompare(b.name))) {
12
+ if (!entry.isFile() && !entry.isDirectory()) throw new Error('demo-template-invalid-entry');
13
+ const path = prefix + entry.name;
14
+ entries.push({ path, directory: entry.isDirectory() });
15
+ if (entry.isDirectory()) entries.push(...await inventory(join(directory, entry.name), path + '/'));
16
+ }
17
+ return entries;
18
+ }
19
+
20
+ /** Called only after the engine has closed its database and persistence files. */
21
+ export async function sealTemplate(template, manifest) {
22
+ const entries = await inventory(template);
23
+ if (entries.some(entry => ['host.lock', descriptor, initialized, copying].includes(entry.path))) throw new Error('demo-template-not-fresh');
24
+ const marker = { version: 1, manifest: createHash('sha256').update(JSON.stringify(manifest)).digest('hex'), entries };
25
+ await writeFile(join(template, descriptor), JSON.stringify(marker) + '\n', { flag: 'wx', mode: 0o600 });
26
+ return marker;
27
+ }
28
+
29
+ async function present(directory, entries) {
30
+ for (const entry of entries) {
31
+ let stat;
32
+ try { stat = await lstat(join(directory, entry.path)); }
33
+ catch (error) { if (error.code === 'ENOENT') return false; throw error; }
34
+ if (entry.directory ? !stat.isDirectory() : !stat.isFile()) return false;
35
+ }
36
+ return true;
37
+ }
38
+
39
+ /** Caller holds data/host.lock until the host exits. Never overlays live data. */
40
+ export async function copyTemplate(template, data) {
41
+ const raw = await readFile(join(template, descriptor), 'utf8'), marker = JSON.parse(raw);
42
+ if (marker.version !== 1 || !/^[a-f0-9]{64}$/.test(marker.manifest ?? '') || !Array.isArray(marker.entries)
43
+ || marker.entries.some(entry => typeof entry.path !== 'string' || !entry.path || entry.path.startsWith('/')
44
+ || entry.path.split('/').some(part => ['', '.', '..'].includes(part)) || typeof entry.directory !== 'boolean'
45
+ || [descriptor, initialized, copying, 'host.lock'].includes(entry.path))) throw new Error('demo-template-invalid');
46
+ await mkdir(data, { recursive: true, mode: 0o700 });
47
+ const existing = await readdir(data);
48
+ if (existing.includes('host.lock') && !(await lstat(join(data, 'host.lock'))).isFile()) throw new Error('demo-volume-invalid-lock');
49
+ if (existing.includes(copying)) throw new Error('demo-volume-incomplete');
50
+ if (existing.includes(initialized)) {
51
+ if (!(await lstat(join(data, initialized))).isFile()) throw new Error('demo-volume-incomplete');
52
+ if (await readFile(join(data, initialized), 'utf8') !== raw) throw new Error('demo-volume-template-mismatch');
53
+ // SQLite may remove its transient WAL/SHM files after a later clean close.
54
+ if (!await present(data, marker.entries.filter(entry => !/-(wal|shm)$/.test(entry.path)))) throw new Error('demo-volume-incomplete');
55
+ return { copied: false };
56
+ }
57
+ if (existing.some(name => name !== 'host.lock')) throw new Error('demo-volume-not-fresh');
58
+ if (!await present(template, marker.entries)) throw new Error('demo-template-incomplete');
59
+ await writeFile(join(data, copying), raw, { flag: 'wx', mode: 0o600 });
60
+ for (const entry of marker.entries.filter(entry => !entry.path.includes('/'))) {
61
+ await cp(join(template, entry.path), join(data, entry.path), { recursive: true, force: false, errorOnExist: true });
62
+ }
63
+ if (!await present(data, marker.entries)) throw new Error('demo-volume-incomplete');
64
+ await rename(join(data, copying), join(data, initialized));
65
+ return { copied: true };
66
+ }
67
+
68
+ if (process.argv[1] === import.meta.filename) {
69
+ try { await copyTemplate(process.argv[2] ?? '/template', process.argv[3] ?? '/data'); }
70
+ catch (error) { console.error('Demo volume initialization failed:', error.message); process.exitCode = 1; }
71
+ }
@@ -7,7 +7,7 @@
7
7
  // container; nothing else on this host is reachable.
8
8
  import { operatorPage } from './operator.mjs';
9
9
  import { bounded, category, report } from './diagnostics.mjs';
10
- import { startPage, demoCookie } from './pages.mjs';
10
+ import { startPage, demoCookie, clearCookie } from './pages.mjs';
11
11
 
12
12
  export { DemoContainer } from './container.mjs';
13
13
  export { DemoCapacity } from './capacity.mjs';
@@ -33,51 +33,94 @@ async function human(env, token, ip) {
33
33
  if (typeof token !== 'string' || !token || token.length > 2048) return false;
34
34
  const body = new URLSearchParams({ secret: env.TURNSTILE_SECRET_KEY, response: token });
35
35
  if (ip) body.set('remoteip', ip);
36
- const response = await fetch('https://challenges.cloudflare.com/turnstile/v0/siteverify', { method: 'POST', body });
36
+ const response = await fetch('https://challenges.cloudflare.com/turnstile/v0/siteverify', { method: 'POST', body, signal: AbortSignal.timeout(5000) });
37
37
  const result = response.ok ? await response.json() : {};
38
38
  if (result.success !== true) console.error('Turnstile refused a start:', response.status, JSON.stringify(result['error-codes'] ?? []));
39
39
  return result.success === true;
40
40
  }
41
41
 
42
- async function start(request, env) {
43
- const json = request.headers.get('accept') === 'application/json';
44
- const refuse = (status, notice, headers = {}) => json ? Response.json({ notice }, { status, headers: { 'cache-control': 'no-store', ...headers } }) : startPage(env, { status, notice, headers });
45
- if (request.headers.get('origin') !== env.DEMO_ORIGIN) return refuse(403, 'Start the demo from this page.');
46
- const ip = request.headers.get('cf-connecting-ip') ?? '';
47
- if (!(await env.STARTS.limit({ key: ip })).success) return refuse(429, 'Too many demos started from your network. Try again in a minute.');
48
- const form = await request.formData();
49
- if (!await human(env, form.get('cf-turnstile-response'), ip)) return refuse(403, 'The check did not pass. Try again.');
50
- const id = env.DEMO.newUniqueId(), ends = Date.now() + HOUR;
51
- if (!await env.CAPACITY.get(env.CAPACITY.idFromName('capacity')).acquire(id.toString(), ends, Number(env.DEMO_MAX_INSTANCES))) {
52
- return refuse(503, 'Every demo is in use right now. Try again in a few minutes.');
42
+ const wantsJSON = request => request.headers.get('accept') === 'application/json';
43
+ function refusal(request, env, status, notice, headers = {}) {
44
+ return wantsJSON(request) ? Response.json({ notice }, { status, headers: { 'cache-control': 'no-store', ...headers } }) : startPage(env, { status, notice, headers });
45
+ }
46
+
47
+ function progress(request, env, id, state) {
48
+ const headers = new Headers({ 'cache-control': 'no-store' });
49
+ if (state.correlation) headers.set('x-rer-demo-correlation', state.correlation);
50
+ if (state.phase === 'failed') return refusal(request, env, 502, 'The demo did not start. Try again.', { ...Object.fromEntries(headers), 'set-cookie': clearCookie(env) });
51
+ headers.append('set-cookie', demoCookie(env, id.toString()));
52
+ if (state.phase === 'starting') {
53
+ headers.set('retry-after', '1');
54
+ if (wantsJSON(request)) return Response.json({ pending: true }, { status: 202, headers });
55
+ // A plain form keeps the same visitor reservation while the copy starts.
56
+ return startPage(env, { status: 202, pending: true, headers: Object.fromEntries(headers) });
53
57
  }
54
- const demo = env.DEMO.get(id), correlation = crypto.randomUUID(), started = Date.now();
58
+ for (const cookie of state.cookies) headers.append('set-cookie', cookie);
59
+ if (wantsJSON(request)) return Response.json({ location: state.location }, { headers });
60
+ headers.set('location', state.location);
61
+ return new Response(null, { status: 303, headers });
62
+ }
63
+
64
+ async function status(request, env) {
65
+ const id = demoId(request, env);
66
+ if (!id) return refusal(request, env, 410, 'This demo has ended. Start a new demo.');
67
+ try { return progress(request, env, id, await bounded(env.DEMO.get(id).status(), 2000)); }
68
+ catch { return refusal(request, env, 503, 'The demo could not be reached. Try again.'); }
69
+ }
70
+
71
+ async function start(request, env, ctx) {
72
+ const refuse = (status, notice, headers = {}) => refusal(request, env, status, notice, headers);
73
+ if (request.headers.get('origin') !== env.DEMO_ORIGIN) return refuse(403, 'Start the demo from this page.');
74
+ let demo, correlation;
75
+ const started = Date.now();
55
76
  try {
56
- const signedIn = await bounded(demo.start({ correlation, origin: env.DEMO_ORIGIN, originVar: env.DEMO_ORIGIN_VAR, ends, endsLocal: localTime(ends, request.cf?.timezone), settings: JSON.parse(env.DEMO_SETTINGS) }), 121_000);
57
- const headers = new Headers({ 'cache-control': 'no-store', 'x-rer-demo-correlation': correlation });
58
- for (const cookie of [...signedIn.cookies, demoCookie(env, id.toString())]) headers.append('set-cookie', cookie);
59
- if (json) return Response.json({ location: signedIn.location }, { headers });
60
- headers.set('location', signedIn.location);
61
- return new Response(null, { status: 303, headers });
77
+ const ip = request.headers.get('cf-connecting-ip') ?? '';
78
+ if (!(await bounded(env.STARTS.limit({ key: ip }), 1000)).success) return refuse(429, 'Too many demos started from your network. Try again in a minute.');
79
+ const form = await bounded(request.formData(), 1000);
80
+ if (!await bounded(human(env, form.get('cf-turnstile-response'), ip), 5000)) return refuse(403, 'The check did not pass. Try again.');
81
+ // A retry while the original start is pending resumes that copy, not a
82
+ // second reservation that the visitor can never use.
83
+ const prior = demoId(request, env);
84
+ if (prior) {
85
+ const state = await bounded(env.DEMO.get(prior).status(), 1000);
86
+ if (state.phase === 'starting') return progress(request, env, prior, state);
87
+ }
88
+ const id = env.DEMO.newUniqueId(), ends = Date.now() + HOUR;
89
+ const capacity = env.CAPACITY.get(env.CAPACITY.idFromName('capacity'));
90
+ // A lost handoff occupies a place for at most a minute. The object extends
91
+ // it to the demo's end only after health and owner admission succeed.
92
+ if (!await bounded(capacity.acquire(id.toString(), Date.now() + 60_000, Number(env.DEMO_MAX_INSTANCES)), 2000)) {
93
+ return refuse(503, 'Every demo is in use right now. Try again in a few minutes.');
94
+ }
95
+ demo = env.DEMO.get(id); correlation = crypto.randomUUID();
96
+ const state = await bounded(demo.begin({ correlation, origin: env.DEMO_ORIGIN, originVar: env.DEMO_ORIGIN_VAR, ends, endsLocal: localTime(ends, request.cf?.timezone), settings: JSON.parse(env.DEMO_SETTINGS) }), 2000);
97
+ return progress(request, env, id, { ...state, correlation });
62
98
  } catch (error) {
63
- const failure = ['health-deadline', 'container-stopped'].includes(error.message) ? error.message : category(error);
64
- report(correlation, started, 'failed', { error: failure });
65
- try {
66
- const diagnostic = { ...await bounded(demo.diagnostic(), 3000), failure };
67
- await bounded(env.CAPACITY.get(env.CAPACITY.idFromName('diagnostics')).record(correlation, diagnostic), 3000);
68
- } catch (capture) { report(correlation, started, 'diagnostic', { error: category(capture) }); }
69
- try { await bounded(demo.end(), 6000); } catch (cleanup) { report(correlation, started, 'cleanup', { error: category(cleanup) }); }
70
- return refuse(502, 'The demo did not start. Try again.', { 'x-rer-demo-correlation': correlation });
99
+ report(correlation, started, 'failed', { error: category(error) });
100
+ // Cleanup runs independently of the response: retries never wait on it.
101
+ if (demo) ctx.waitUntil(bounded(demo.end(), 6000).catch(cleanup => report(correlation, started, 'cleanup', { error: category(cleanup) })));
102
+ return refuse(502, 'The demo did not start. Try again.', correlation ? { 'x-rer-demo-correlation': correlation } : {});
71
103
  }
72
104
  }
73
105
 
74
106
  export default {
75
- async fetch(request, env) {
107
+ async fetch(request, env, ctx) {
76
108
  const url = new URL(request.url);
77
109
  if (url.pathname.startsWith('/__demo/operator/')) return operatorPage(request, env, url.pathname.slice('/__demo/operator/'.length));
78
- if (url.pathname === '/__demo/start') return request.method === 'POST' ? start(request, env) : home();
110
+ if (url.pathname === '/__demo/start') return request.method === 'POST' ? start(request, env, ctx) : home();
111
+ if (url.pathname === '/__demo/status') return request.method === 'GET' ? status(request, env) : home();
79
112
  const id = demoId(request, env);
80
- if (id) return env.DEMO.get(id).fetch(request);
113
+ if (id) {
114
+ // The JSON start leaves the browser on /. Reloading it must keep the
115
+ // pending copy on the progress page until health and owner claim finish.
116
+ if (url.pathname === '/' && ['GET', 'HEAD'].includes(request.method)) {
117
+ try {
118
+ const state = await bounded(env.DEMO.get(id).status(), 2000);
119
+ if (state.phase === 'starting') return progress(request, env, id, state);
120
+ } catch { return refusal(request, env, 503, 'The demo could not be reached. Try again.'); }
121
+ }
122
+ return env.DEMO.get(id).fetch(request);
123
+ }
81
124
  if (!['GET', 'HEAD'].includes(request.method)) return new Response('Start a demo first.', { status: 403 });
82
125
  if (url.pathname !== '/') return home();
83
126
  return startPage(env);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@runeditrun/rer",
3
- "version": "0.2.17",
3
+ "version": "0.2.18",
4
4
  "description": "The rer command: install, set up, run and launch Run Edit Run checkouts.",
5
5
  "license": "MIT",
6
6
  "type": "module",