@runeditrun/rer 0.2.21 → 0.2.22

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/README.md +7 -0
  2. package/cli/demo-host.mjs +5 -5
  3. package/cli/deploy-command.mjs +2 -2
  4. package/cli/deploy.mjs +3 -1
  5. package/cli/export.mjs +1 -1
  6. package/cli/managed-server.mjs +5 -2
  7. package/cli/production-auth.mjs +19 -5
  8. package/modules/container/README.md +10 -0
  9. package/modules/container/build.mjs +7 -11
  10. package/modules/container/host.mjs +3 -1
  11. package/modules/container/ingress.mjs +15 -4
  12. package/modules/container/migration-inventory.mjs +33 -0
  13. package/modules/container/migrations.mjs +16 -2
  14. package/modules/delivery-producer/migrations.mjs +61 -0
  15. package/modules/demo/effects.mjs +4 -0
  16. package/modules/demo-host/README.md +27 -17
  17. package/modules/demo-host/capacity.mjs +1 -1
  18. package/modules/demo-host/container.mjs +65 -37
  19. package/modules/demo-host/pages.mjs +1 -1
  20. package/modules/demo-host/plan.mjs +19 -6
  21. package/modules/demo-host/remove.mjs +9 -7
  22. package/modules/demo-host/start-script.mjs +10 -1
  23. package/modules/demo-host/startup.mjs +4 -2
  24. package/modules/demo-host/worker.mjs +19 -2
  25. package/modules/dependencies/approval.mjs +3 -2
  26. package/modules/dependencies/index.mjs +2 -2
  27. package/modules/deploy-kit/README.md +4 -2
  28. package/modules/deploy-kit/diagnostics.mjs +9 -0
  29. package/modules/deploy-kit/provider-transport.mjs +56 -0
  30. package/modules/deploy-kit/provider.mjs +22 -20
  31. package/modules/operator-auth/README.md +4 -0
  32. package/modules/operator-auth/accounts/credentials.mjs +2 -2
  33. package/modules/operator-auth/accounts/email-change.mjs +2 -2
  34. package/modules/operator-auth/accounts/provider-control.mjs +3 -3
  35. package/modules/operator-auth/accounts/store.mjs +9 -8
  36. package/modules/operator-auth/dev/cli-session.mjs +5 -4
  37. package/modules/operator-auth/dev/index.mjs +5 -5
  38. package/modules/operator-auth/dev-gate.mjs +4 -2
  39. package/modules/operator-auth/identity/email-link/index.mjs +2 -1
  40. package/modules/operator-auth/signin/index.mjs +10 -2
  41. package/onboarding/adapter.mjs +11 -3
  42. package/onboarding/operator-installer.mjs +2 -2
  43. package/onboarding/operator-migrations.mjs +48 -0
  44. package/onboarding/process-table.mjs +45 -19
  45. package/package.json +1 -1
@@ -4,6 +4,7 @@ import { bounded, boundedFetch, category, report } from './diagnostics.mjs';
4
4
  import { endedPage, clearCookie } from './pages.mjs';
5
5
  import { finishStart } from './startup.mjs';
6
6
  import { placeContainer } from './placement.mjs';
7
+ import { fingerprint } from './capacity.mjs';
7
8
 
8
9
  // One visitor's demo: a Durable Object (newUniqueId) and its container, the
9
10
  // seed's image from `rer build --platform container` on the workerd engine.
@@ -11,11 +12,9 @@ import { placeContainer } from './placement.mjs';
11
12
  // external effects; the engine also refuses direct mail/SMS transports. The
12
13
  // front signs the owner in through the existing signed owner claim. The demo ends at its hour or after
13
14
  // 15 idle minutes: the alarm destroys the container and deletes everything.
14
- // The platform stops a container about two minutes after its Durable Object's
15
- // last event, when the object is evicted, and the exit watcher goes with it.
16
- // So the alarm stays in flight until the demo's next check and then arms the
17
- // next at once, as the Containers class keeps its object alive; a container
18
- // that is gone anyway answers the ended page and frees its place.
15
+ // Container lifetime is independent of the object's JS lifetime. Configure the
16
+ // platform's inactivity grace on every object instance; persisted alarms and
17
+ // request-time checks enforce the visitor's exact idle and absolute deadlines.
19
18
  const PORT = 8080, IDLE = 15 * 60_000, OWNER = 'owner@demo.invalid';
20
19
  // The container's port speaks plain HTTP; the app takes its public origin from RER_INGRESS_ORIGIN.
21
20
  const inside = url => `http://container${url.pathname}${url.search}`;
@@ -30,9 +29,12 @@ export class DemoContainer extends DurableObject {
30
29
  if (state.get('startup')?.phase === 'starting') {
31
30
  // An interrupted start has lost its private claim key and cannot be
32
31
  // resumed after object eviction. End it instead of polling forever.
33
- await ctx.storage.put('startup', { phase: 'failed' });
32
+ await ctx.storage.put('startup', { phase: 'starting', failure: state.get('startup').failure ?? { phase: 'failed' } });
34
33
  ctx.waitUntil(this.end().catch(cause => report(this.correlation, this.started, 'cleanup', { error: category(cause) })));
35
- } else if (this.container.running) this.watch();
34
+ } else if (this.container.running) {
35
+ await this.container.setInactivityTimeout(this.idle);
36
+ this.watch();
37
+ }
36
38
  });
37
39
  }
38
40
  watch() {
@@ -43,7 +45,7 @@ export class DemoContainer extends DurableObject {
43
45
  }
44
46
  records = [];
45
47
  record(event, detail = {}) {
46
- const row = { event, at: new Date().toISOString(), elapsedMs: Date.now() - this.started, ...detail };
48
+ const row = { event, at: new Date().toISOString(), elapsedMs: this.now() - this.started, ...detail };
47
49
  this.records.push(row); if (this.records.length > 8) this.records.shift();
48
50
  report(this.correlation, this.started, event, detail);
49
51
  }
@@ -52,7 +54,7 @@ export class DemoContainer extends DurableObject {
52
54
  startTimeout = 45_000;
53
55
  probeTimeout = 5000;
54
56
  cleanupRetry = 30_000;
55
- hold = 3 * 60_000;
57
+ now() { return Date.now(); }
56
58
  get container() { return this.ctx.container; }
57
59
  port() { return this.container.getTcpPort(PORT); }
58
60
 
@@ -71,40 +73,43 @@ export class DemoContainer extends DurableObject {
71
73
 
72
74
  /** Start the container, wait for /health, claim the owner; returns the claim's 303. */
73
75
  async start({ origin, originVar, ends, endsLocal, settings, correlation }) {
74
- this.correlation = correlation; this.started = Date.now(); this.stopped = false; this.cancelled = false;
76
+ this.correlation = correlation; this.started = this.now(); this.stopped = false; this.cancelled = false;
75
77
  const deadline = this.started + this.startTimeout;
76
- const id = this.ctx.id.toString(), keys = await claimKeys(), now = Date.now();
78
+ const id = this.ctx.id.toString(), keys = await claimKeys(), now = this.now();
77
79
  await this.ctx.storage.put({ ends, seen: now, correlation, started: this.started, startDeadline: deadline });
78
80
  report(correlation, this.started, 'start');
79
81
  if (this.stopped) throw new Error('container-stopped');
80
- await placeContainer(this, { enableInternet: true, env: { ...settings, RER_DEMO: '1', [originVar]: origin, RER_INGRESS_ORIGIN: origin,
82
+ await placeContainer(this, { enableInternet: true, env: { ...settings, RER_DEMO: '1', [originVar]: origin, RER_INGRESS_ORIGIN: origin, RER_INGRESS_ORIGINS: JSON.stringify([origin, ...JSON.parse(this.env.DEMO_SECONDARY_ORIGINS ?? '[]')]),
81
83
  SESSION_SECRET: random(), SETUP_TOKEN: random(), OWNER_EMAIL: OWNER, RER_OWNER_CLAIM_ISSUER: origin, RER_OWNER_CLAIM_INSTALLATION_ID: id,
82
84
  RER_OWNER_CLAIM_PUBLIC_KEYS: JSON.stringify(keys.publicKeys), RER_DEMO_EXPIRES: endsLocal } }, deadline);
83
- await this.ctx.storage.setAlarm(Date.now());
84
- const token = await signClaim(keys, { origin, installationId: id, email: OWNER, now: Date.now() });
85
+ await this.container.setInactivityTimeout(this.idle);
86
+ await this.ctx.storage.setAlarm(deadline);
87
+ const token = await signClaim(keys, { origin, installationId: id, email: OWNER, now: this.now() });
85
88
  const claimed = await boundedFetch(this.port(), 'http://container/auth/owner-claim', { method: 'POST', redirect: 'manual',
86
- headers: { origin, 'content-type': 'application/x-www-form-urlencoded' }, body: new URLSearchParams({ token }) }, Math.max(1, Math.min(10_000, deadline - Date.now())));
89
+ headers: { origin, 'content-type': 'application/x-www-form-urlencoded' }, body: new URLSearchParams({ token }) }, Math.max(1, Math.min(10_000, deadline - this.now())));
87
90
  void claimed.body?.cancel().catch(() => {});
88
91
  this.record('claim', { status: claimed.status });
89
92
  if (this.stopped) throw new Error('container-stopped');
90
93
  if (claimed.status !== 303) throw new Error(`owner claim answered ${claimed.status}`);
94
+ await this.ctx.storage.put('seen', this.now());
95
+ await this.ctx.storage.setAlarm(Math.min(ends, this.now() + this.idle));
91
96
  return { location: claimed.headers.get('location'), cookies: claimed.headers.getSetCookie() };
92
97
  }
93
98
 
94
99
  async healthy(deadline) {
95
100
  let attempt = 0;
96
- while (Date.now() < deadline) {
101
+ while (this.now() < deadline) {
97
102
  if (this.stopped) throw new Error('container-stopped');
98
- const probe = Date.now();
103
+ const probe = this.now();
99
104
  try {
100
- const response = await boundedFetch(this.port(), 'http://container/health', {}, Math.min(this.probeTimeout, deadline - Date.now()));
105
+ const response = await boundedFetch(this.port(), 'http://container/health', {}, Math.min(this.probeTimeout, deadline - this.now()));
101
106
  void response.body?.cancel().catch(() => {});
102
- this.record('health', { attempt: ++attempt, durationMs: Date.now() - probe, status: response.status });
107
+ this.record('health', { attempt: ++attempt, durationMs: this.now() - probe, status: response.status });
103
108
  if (response.ok && !this.stopped) return;
104
109
  } catch (error) {
105
- this.record('health', { attempt: ++attempt, durationMs: Date.now() - probe, error: category(error) });
110
+ this.record('health', { attempt: ++attempt, durationMs: this.now() - probe, error: category(error) });
106
111
  }
107
- await new Promise(resolve => setTimeout(resolve, Math.min(250, Math.max(0, deadline - Date.now()))));
112
+ await new Promise(resolve => setTimeout(resolve, Math.min(250, Math.max(0, deadline - this.now()))));
108
113
  }
109
114
  throw new Error('health-deadline');
110
115
  }
@@ -120,11 +125,20 @@ export class DemoContainer extends DurableObject {
120
125
 
121
126
  /** Every request carrying this demo's cookie: proxied while the demo lives, else the ended page. */
122
127
  async fetch(request) {
123
- const state = await this.ctx.storage.get(['ends', 'seen']), now = Date.now();
124
- if (!(state.get('ends') > now)) return this.ended();
128
+ const state = await this.ctx.storage.get(['ends', 'seen']), now = this.now();
129
+ if (!state.get('ends') || now >= Math.min(state.get('ends'), state.get('seen') + this.idle)) {
130
+ try { await this.end(); }
131
+ catch (cause) { report(this.correlation, this.started, 'cleanup', { error: category(cause) }); }
132
+ return this.ended();
133
+ }
125
134
  if (!this.container.running) return this.gone();
126
- if (now - state.get('seen') > 60_000) await this.ctx.storage.put('seen', now);
127
- try { return await this.port().fetch(new Request(inside(new URL(request.url)), request)); }
135
+ await this.ctx.storage.put('seen', now);
136
+ await this.ctx.storage.setAlarm(Math.min(state.get('ends'), now + this.idle));
137
+ const forwarded = new Request(inside(new URL(request.url)), request);
138
+ // The front admits only declared hosts. Never let a visitor choose the
139
+ // container's public origin through a transport header.
140
+ forwarded.headers.set('x-rer-ingress-origin', new URL(request.url).origin);
141
+ try { return await this.port().fetch(forwarded); }
128
142
  catch (error) { if (this.container.running && !this.stopped) throw error; return this.gone(); }
129
143
  }
130
144
 
@@ -138,7 +152,7 @@ export class DemoContainer extends DurableObject {
138
152
  }
139
153
 
140
154
  async alarm() {
141
- const state = await this.ctx.storage.get(['ends', 'seen', 'startDeadline']), now = Date.now();
155
+ const state = await this.ctx.storage.get(['ends', 'seen', 'startDeadline']), now = this.now();
142
156
  if (await this.ctx.storage.get('stopping')) return this.end();
143
157
  if (!state.get('ends')) return this.ctx.storage.deleteAll();
144
158
  const due = Math.min(state.get('ends'), state.get('seen') + this.idle, state.get('startDeadline') ?? Infinity);
@@ -146,8 +160,7 @@ export class DemoContainer extends DurableObject {
146
160
  if ((await this.ctx.storage.get('startup'))?.phase === 'starting') { this.stopped = true; return; }
147
161
  return this.end();
148
162
  }
149
- await new Promise(resolve => { this.wake = resolve; setTimeout(resolve, Math.min(due - now, this.hold)); });
150
- if (!this.stopped) await this.ctx.storage.setAlarm(Date.now());
163
+ await this.ctx.storage.setAlarm(due);
151
164
  }
152
165
 
153
166
  /** Destroy the container and delete this demo's state and place; safe to repeat. */
@@ -158,24 +171,39 @@ export class DemoContainer extends DurableObject {
158
171
  }
159
172
 
160
173
  async cleanup() {
161
- this.cancelled = true; this.stopped = true; this.wake?.();
174
+ this.cancelled = true; this.stopped = true;
162
175
  const startup = await this.ctx.storage.get('startup');
163
- // Keep the alarm and capacity reservation if destruction fails; retry cleanup.
176
+ const failure = startup?.failure ?? (startup?.phase === 'failed' ? startup : null);
177
+ // A failed start has no live visitor. Release it even if destruction needs
178
+ // another alarm; a live demo keeps its place until destruction succeeds.
164
179
  await this.ctx.storage.put('stopping', true);
165
- await this.ctx.storage.setAlarm(Date.now() + this.cleanupRetry);
180
+ await this.ctx.storage.setAlarm(this.now() + this.cleanupRetry);
181
+ if (failure) {
182
+ await this.releasePlace();
183
+ await this.ctx.storage.put('startup', failure);
184
+ }
166
185
  if (this.container.running) {
167
- try { await bounded(this.env.CAPACITY.get(this.env.CAPACITY.idFromName('capacity')).extend(this.ctx.id.toString(), Date.now() + 60_000), 2000); }
168
- catch (cause) { report(this.correlation, this.started, 'cleanup-reservation', { error: category(cause) }); }
186
+ if (!failure) {
187
+ try { await bounded(this.env.CAPACITY.get(this.env.CAPACITY.idFromName('capacity')).extend(this.ctx.id.toString(), this.now() + 60_000), 2000); }
188
+ catch (cause) { report(this.correlation, this.started, 'cleanup-reservation', { error: category(cause) }); }
189
+ }
169
190
  await bounded(this.container.destroy(), 5000);
170
191
  }
171
- await bounded(this.env.CAPACITY.get(this.env.CAPACITY.idFromName('capacity')).release(this.ctx.id.toString()), 2000);
192
+ if (!failure) await this.releasePlace();
172
193
  await this.ctx.storage.deleteAlarm();
173
194
  await this.ctx.storage.deleteAll();
174
195
  // Retain only the terminal answer briefly, so polling survives a fresh DO.
175
- if (startup?.phase === 'failed') {
176
- await this.ctx.storage.put({ startup, correlation: this.correlation, started: this.started });
177
- await this.ctx.storage.setAlarm(Date.now() + 60_000);
196
+ if (failure) {
197
+ await this.ctx.storage.put({ startup: failure, correlation: this.correlation, started: this.started });
198
+ await this.ctx.storage.setAlarm(this.now() + 60_000);
178
199
  }
179
200
  report(this.correlation, this.started, 'ended');
180
201
  }
202
+
203
+ async releasePlace() {
204
+ const id = this.ctx.id.toString();
205
+ if (await bounded(this.env.CAPACITY.get(this.env.CAPACITY.idFromName('capacity')).release(id), 2000)) {
206
+ report(this.correlation, this.started, 'released', { releasedId: await fingerprint(id) });
207
+ }
208
+ }
181
209
  }
@@ -49,7 +49,7 @@ export function endedPage(env, { status = 410, headers = {} } = {}) {
49
49
  const name = escape(env.DEMO_NAME);
50
50
  return html(env, { page: 'ended', title: 'This demo has ended', status, headers, body: `<h1 id="title">This demo has ended</h1>
51
51
  <p class="intro">Demos last an hour, or until 15 minutes pass with nothing happening. Its copy of ${name} and everything in it have been deleted.</p>
52
- <a class="button" href="/">Start a new demo</a><p class="secondary"><a href="${escape(new URL(env.DEMO_SITE).origin)}/run">Run ${name} yourself</a></p>` });
52
+ <a class="button" href="${escape(env.DEMO_ORIGIN)}/">Start a new demo</a><p class="secondary"><a href="${escape(new URL(env.DEMO_SITE).origin)}/run">Run ${name} yourself</a></p>` });
53
53
  }
54
54
 
55
55
  const cookie = (env, value, seconds) => `rer_demo=${value}; Domain=${new URL(env.DEMO_ORIGIN).hostname}; Path=/; Max-Age=${seconds}; HttpOnly; Secure; SameSite=Lax`;
@@ -19,7 +19,9 @@ export const INSTANCE_TYPES = ['lite', 'basic', 'standard-1', 'standard-2', 'sta
19
19
  * deploy-demo.json: `{ "version": 1, "domain": "run-lamp.com", "name": "Lamp" }`, plus optionally
20
20
  * `maxInstances` (concurrent demos, default 10), `instanceType` (one of INSTANCE_TYPES, default
21
21
  * "basic") and `settings`, the seed's plain demo settings (such as MAIL_PROVIDER: "development").
22
- * Settings never carry secrets or what the front sets.
22
+ * Settings never carry secrets or what the front sets. The app declaration's
23
+ * container.hostVariables select hostname settings: the primary demo hostname
24
+ * or its descendants, routed through the same visitor isolation.
23
25
  */
24
26
  export function demoDeclaration(value, originVar) {
25
27
  assert(value && typeof value === 'object' && !Array.isArray(value) && value.version === 1 && Object.keys(value).every(key => ['version', 'domain', 'name', 'maxInstances', 'instanceType', 'settings'].includes(key)), 'invalid-demo-declaration');
@@ -36,28 +38,39 @@ export function demoDeclaration(value, originVar) {
36
38
  }
37
39
 
38
40
  /** The demo Worker's name and its Wrangler configuration; no provider request. */
39
- export function demoPlan({ project, environment, compatibilityDate, originVar, declaration, turnstileSiteKey }) {
41
+ export function demoPlan({ project, environment, compatibilityDate, originVar, hostVariables = [], declaration, turnstileSiteKey }) {
40
42
  assert(/^\d{4}-\d{2}-\d{2}$/.test(compatibilityDate ?? '') && /^[A-Z][A-Z0-9_]{0,63}$/.test(originVar ?? ''), 'invalid-plan');
41
43
  const { domain, name, maxInstances, instanceType, settings } = demoDeclaration(declaration, originVar);
42
44
  const worker = `${ownerWorkerName(project, environment)}--demo`, hostname = `demo.${domain}`, container = worker.replace(/-+/g, '-');
43
45
  assert(resourceName(worker), 'owner-name-too-long');
46
+ assert(Array.isArray(hostVariables) && hostVariables.every(variable => typeof variable === 'string' && /^[A-Z][A-Z0-9_]{0,63}$/.test(variable)), 'invalid-demo-host-variables');
47
+ const declaredHosts = hostVariables.map(variable => {
48
+ const host = settings[variable];
49
+ assert(Object.hasOwn(settings, variable) && typeof host === 'string' && host.length <= 253 &&
50
+ /^(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?$/.test(host) &&
51
+ (host === hostname || host.endsWith(`.${hostname}`)), 'invalid-demo-host-setting');
52
+ return host;
53
+ });
54
+ const secondaryHosts = [...new Set(declaredHosts)].filter(host => host !== hostname).sort();
55
+ const hostnames = [hostname, ...secondaryHosts];
44
56
  // Rate limit namespaces are account-wide integers: one per demo Worker.
45
57
  const namespace = String(1000 + parseInt(createHash('sha256').update(worker).digest('hex').slice(0, 8), 16) % 1_000_000_000);
46
58
  const wrangler = {
47
59
  name: worker, main: 'worker.mjs', compatibility_date: compatibilityDate, workers_dev: false, preview_urls: false,
48
- routes: [{ pattern: hostname, custom_domain: true }],
60
+ routes: hostnames.map(pattern => ({ pattern, custom_domain: true })),
49
61
  observability: { enabled: true, head_sampling_rate: 1, logs: { enabled: true, invocation_logs: false } },
50
- vars: { DEMO_ORIGIN: `https://${hostname}`, DEMO_SITE: `https://${domain}`, DEMO_NAME: name, DEMO_ORIGIN_VAR: originVar, DEMO_MAX_INSTANCES: String(maxInstances), DEMO_SETTINGS: JSON.stringify(settings),
62
+ vars: { DEMO_ORIGIN: `https://${hostname}`, DEMO_SITE: `https://${domain}`, DEMO_NAME: name, DEMO_ORIGIN_VAR: originVar, DEMO_MAX_INSTANCES: String(maxInstances), DEMO_SETTINGS: JSON.stringify(settings), DEMO_SECONDARY_ORIGINS: JSON.stringify(secondaryHosts.map(host => `https://${host}`)),
51
63
  ...(turnstileSiteKey ? { TURNSTILE_SITE_KEY: turnstileSiteKey } : {}) },
52
64
  durable_objects: { bindings: [{ name: 'DEMO', class_name: 'DemoContainer' }, { name: 'CAPACITY', class_name: 'DemoCapacity' }] },
53
65
  migrations: [{ tag: 'v1', new_sqlite_classes: ['DemoContainer', 'DemoCapacity'] }],
54
66
  containers: [{ name: container, class_name: 'DemoContainer', image: './image/Dockerfile', max_instances: maxInstances, instance_type: instanceType }],
55
67
  ratelimits: [{ name: 'STARTS', namespace_id: namespace, simple: { limit: 3, period: 60 } }],
56
68
  };
57
- return { worker, hostname, container, record: demoRecord({ worker, hostname, container }), origin: `https://${hostname}`, maxInstances, instanceType, settings: Object.keys(settings), wrangler };
69
+ return { worker, hostname, hostnames, container, record: demoRecord({ worker, hostname, hostnames, container }), origin: `https://${hostname}`, maxInstances, instanceType, settings: Object.keys(settings), wrangler };
58
70
  }
59
71
 
60
72
  /** The demo's record of what `rer deploy --demo` creates, kept on the provider so a
61
73
  * fresh checkout (a CI runner) can remove it: the deploy writes it as the Worker
62
74
  * version's message, and only a Worker serving it is taken for the demo's own. */
63
- export const demoRecord = ({ worker, hostname, container }) => `rer-demo:${hash({ worker, hostname, container })}`;
75
+ export const demoRecord = ({ worker, hostname, hostnames = [hostname], container }) =>
76
+ `rer-demo:${hash({ worker, hostname, container, ...(hostnames.length > 1 ? { hostnames } : {}) })}`;
@@ -6,8 +6,8 @@ import { deleteWorker } from '../deploy-kit/teardown.mjs';
6
6
  /* `rer deploy --demo --remove --yes`: remove the hosted demo `rer deploy --demo`
7
7
  * made for this environment, by exact provider ids, and nothing else. What the
8
8
  * deploy made: the front Worker (its Durable Object namespaces go with it), the
9
- * Workers Custom Domain demo.<domain> (Cloudflare adds and removes its DNS
10
- * record with it) and the Container application `wrangler deploy` created for
9
+ * Workers Custom Domains on the primary demo hostname and declared descendants
10
+ * (Cloudflare adds and removes their DNS records with them), and the Container application `wrangler deploy` created for
11
11
  * the Worker's DemoContainer namespace. It makes no D1, KV, R2, queue or DNS
12
12
  * record of its own: the domain's record is Cloudflare's, and goes with it.
13
13
  *
@@ -24,17 +24,18 @@ const rows = value => { assert(Array.isArray(value), 'provider-invalid-list'); r
24
24
 
25
25
  /** What exists of the demo now, each with its exact id; refuses whatever is not provably the demo's. */
26
26
  async function inventory(api, plan) {
27
- const named = rows(await api.request(`/workers/domains?hostname=${plan.hostname}`)).filter(row => row?.hostname === plan.hostname);
27
+ const named = (await Promise.all(plan.hostnames.map(async hostname =>
28
+ rows(await api.request(`/workers/domains?hostname=${hostname}`)).filter(row => row?.hostname === hostname)))).flat();
28
29
  const attached = rows(await api.request(`/workers/domains?service=${plan.worker}`)).filter(row => row?.service === plan.worker);
29
30
  const apps = rows(await api.request('/containers/applications')).filter(row => row?.name === plan.container);
30
31
  const namespaces = rows(await api.list('/workers/durable_objects/namespaces')).filter(row => row?.script === plan.worker);
31
32
  const worker = await workerSettings(api, plan.worker) !== null;
32
- for (const row of named) if (row.service !== plan.worker) throw refuse('resource-name-collision', `domain:${plan.hostname}`);
33
+ for (const row of named) if (row.service !== plan.worker) throw refuse('resource-name-collision', `domain:${row.hostname}`);
33
34
  if (worker) {
34
35
  const served = await servedVersion(api, plan.worker);
35
36
  if (served.annotation !== plan.record) throw refuse('resource-name-collision', `worker:${plan.worker}`);
36
37
  if (!served.current) throw refuse('owned-resource-replaced', `worker:${plan.worker}`);
37
- for (const row of attached) if (row.hostname !== plan.hostname) throw refuse('resource-not-in-record', `domain:${row.hostname}`);
38
+ for (const row of attached) if (!plan.hostnames.includes(row.hostname)) throw refuse('resource-not-in-record', `domain:${row.hostname}`);
38
39
  } else if (named.length || attached.length || apps.length) {
39
40
  // Without the Worker nothing proves these are the demo's.
40
41
  throw refuse('uncertain-resource-create', named[0] || attached[0] ? `domain:${(named[0] ?? attached[0]).hostname}` : `container:${plan.container}`);
@@ -50,7 +51,7 @@ export async function demoRemove({ api, plan, sleep }) {
50
51
  const removed = [];
51
52
  for (const domain of found.domains) {
52
53
  await api.request(`/workers/domains/${domain.id}`, { method: 'DELETE' });
53
- removed.push(`domain:${plan.hostname}`);
54
+ removed.push(`domain:${domain.hostname}`);
54
55
  }
55
56
  for (const app of found.apps) { await api.request(`/containers/applications/${app.id}`, { method: 'DELETE' }); removed.push(`container:${plan.container}`); }
56
57
  await deleteWorker({ api, worker: plan.worker, ...sleep ? { sleep } : {} });
@@ -59,7 +60,8 @@ export async function demoRemove({ api, plan, sleep }) {
59
60
  const remaining = [];
60
61
  if (await workerSettings(api, plan.worker) !== null) remaining.push(`worker:${plan.worker}`);
61
62
  for (const row of rows(await api.list('/workers/durable_objects/namespaces'))) if (row?.script === plan.worker) remaining.push(`durable-object:${row.class}`);
62
- if (rows(await api.request(`/workers/domains?hostname=${plan.hostname}`)).some(row => row?.hostname === plan.hostname)) remaining.push(`domain:${plan.hostname}`);
63
+ const domains = await Promise.all(plan.hostnames.map(async hostname => ({ hostname, rows: rows(await api.request(`/workers/domains?hostname=${hostname}`)) })));
64
+ for (const { hostname, rows: listed } of domains) if (listed.some(row => row?.hostname === hostname)) remaining.push(`domain:${hostname}`);
63
65
  const apps = rows(await api.request('/containers/applications'));
64
66
  for (const app of found.apps) if (apps.some(row => row?.id === app.id)) remaining.push(`container:${plan.container}`);
65
67
  if (remaining.length) { const error = new Error('remove-incomplete'); error.remaining = remaining; error.removed = removed.filter(item => !remaining.includes(item)); throw error; }
@@ -52,8 +52,17 @@ async function go(resume=false){
52
52
  show('failed',{label:'Try again',note:'',live:'',notice:'The demo could not be reached. Check your connection, then try again.'});
53
53
  }
54
54
  }
55
- function again(){
55
+ async function again(){
56
56
  attempt?.abort();attempt=null;stop();
57
+ el().auto=false;
58
+ show('working',{label:'Trying again…',note:'Your new demo will start after the human check.',live:'Trying again.',notice:''});
59
+ try{
60
+ const response=await fetch('/__demo/cancel',{method:'POST',headers:{accept:'application/json'},signal:AbortSignal.timeout(8000)});
61
+ if(!response.ok)throw new Error('cancel-failed');
62
+ }catch{
63
+ show('failed',{label:'Try again',note:'',live:'',notice:'The previous demo could not be ended. Try again.'});
64
+ return;
65
+ }
57
66
  el().auto=true;
58
67
  show('waiting',{label:'Waiting for the human check…',note:'Running the check again. The demo starts by itself once it passes.',live:'Running the human check again.',notice:''});
59
68
  turnstile.reset(el().form.querySelector('.cf-turnstile'));
@@ -15,11 +15,13 @@ export async function finishStart(demo, options) {
15
15
  } catch (error) {
16
16
  const failure = ['health-deadline', 'container-stopped', 'start-reservation-expired'].includes(error.message) ? error.message : category(error);
17
17
  demo.record('failed', { error: failure });
18
+ // Release before publishing the retry state, diagnostic capture or a slow
19
+ // destruction retry. This copy never became a live visitor's demo.
20
+ await demo.ctx.storage.put('startup', { phase: 'starting', failure: { phase: 'failed' } });
21
+ try { await demo.end(); } catch (cleanup) { report(correlation, demo.started, 'cleanup', { error: category(cleanup) }); }
18
22
  try {
19
23
  const diagnostic = await bounded(demo.diagnostic(), 3000);
20
24
  await bounded(demo.env.CAPACITY.get(demo.env.CAPACITY.idFromName('diagnostics')).record(correlation, { ...diagnostic, failure }), 3000);
21
25
  } catch (capture) { report(correlation, demo.started, 'diagnostic', { error: category(capture) }); }
22
- await demo.ctx.storage.put('startup', { phase: 'failed', capacity: failure === 'placement-unavailable' });
23
- try { await demo.end(); } catch (cleanup) { report(correlation, demo.started, 'cleanup', { error: category(cleanup) }); }
24
26
  }
25
27
  }
@@ -47,7 +47,7 @@ function refusal(request, env, status, notice, headers = {}) {
47
47
  function progress(request, env, id, state) {
48
48
  const headers = new Headers({ 'cache-control': 'no-store' });
49
49
  if (state.correlation) headers.set('x-rer-demo-correlation', state.correlation);
50
- if (state.phase === 'failed') return refusal(request, env, state.capacity ? 503 : 502, state.capacity ? 'Every demo is in use right now. Try again in a few minutes.' : 'The demo did not start. Try again.', { ...Object.fromEntries(headers), 'set-cookie': clearCookie(env) });
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
51
  headers.append('set-cookie', demoCookie(env, id.toString()));
52
52
  if (state.phase === 'starting') {
53
53
  headers.set('retry-after', '1');
@@ -103,17 +103,33 @@ async function start(request, env, ctx) {
103
103
  }
104
104
  }
105
105
 
106
+ // The visitor explicitly abandons this copy before running a fresh human check.
107
+ // Wait for destruction and place release; a failed cleanup must retain the cookie
108
+ // so another click can finish it instead of silently leaking a reservation.
109
+ async function cancel(request, env) {
110
+ if (request.headers.get('origin') !== env.DEMO_ORIGIN) return refusal(request, env, 403, 'Start the demo from this page.');
111
+ const id = demoId(request, env);
112
+ try { if (id) await bounded(env.DEMO.get(id).end(), 6000); }
113
+ catch { return refusal(request, env, 503, 'The previous demo could not be ended. Try again.'); }
114
+ return new Response(null, { status: 204, headers: { 'cache-control': 'no-store', 'set-cookie': clearCookie(env) } });
115
+ }
116
+
106
117
  export default {
107
118
  async fetch(request, env, ctx) {
108
119
  const url = new URL(request.url);
120
+ const primary = url.origin === env.DEMO_ORIGIN;
121
+ const secondary = JSON.parse(env.DEMO_SECONDARY_ORIGINS ?? '[]').includes(url.origin);
122
+ if (!primary && !secondary) return new Response('Not found.', { status: 404 });
123
+ if (!primary && url.pathname.startsWith('/__demo/')) return new Response('Start the demo on its primary host.', { status: 403 });
109
124
  if (url.pathname.startsWith('/__demo/operator/')) return operatorPage(request, env, url.pathname.slice('/__demo/operator/'.length));
110
125
  if (url.pathname === '/__demo/start') return request.method === 'POST' ? start(request, env, ctx) : home();
111
126
  if (url.pathname === '/__demo/status') return request.method === 'GET' ? status(request, env) : home();
127
+ if (url.pathname === '/__demo/cancel') return request.method === 'POST' ? cancel(request, env) : home();
112
128
  const id = demoId(request, env);
113
129
  if (id) {
114
130
  // The JSON start leaves the browser on /. Reloading it must keep the
115
131
  // pending copy on the progress page until health and owner claim finish.
116
- if (url.pathname === '/' && ['GET', 'HEAD'].includes(request.method)) {
132
+ if (primary && url.pathname === '/' && ['GET', 'HEAD'].includes(request.method)) {
117
133
  try {
118
134
  const state = await bounded(env.DEMO.get(id).status(), 2000);
119
135
  if (state.phase === 'starting') return progress(request, env, id, state);
@@ -122,6 +138,7 @@ export default {
122
138
  return env.DEMO.get(id).fetch(request);
123
139
  }
124
140
  if (!['GET', 'HEAD'].includes(request.method)) return new Response('Start a demo first.', { status: 403 });
141
+ if (!primary) return new Response(null, { status: 303, headers: { location: env.DEMO_ORIGIN + '/' } });
125
142
  if (url.pathname !== '/') return home();
126
143
  return startPage(env);
127
144
  },
@@ -17,8 +17,9 @@ export async function approveSetup({db,auth,request,input,contract,callbacks,clo
17
17
  }
18
18
  export async function approveOwner({store,actor,input,contract,callbacks,clock,native}) {
19
19
  const fence=native?await native.ownerFence(actor,clock()):ownerFence(actor,clock());
20
- const approval=await store.run(`INSERT INTO rer_dependency_pending(interface,issuer,approved_by,expires_at,approved_session,approved_version,callbacks,attempt_id) SELECT ?,?,?,?,?,?,?,? WHERE ${fence.sql} ON CONFLICT(interface) DO UPDATE SET issuer=excluded.issuer,approved_by=excluded.approved_by,expires_at=excluded.expires_at,approved_session=excluded.approved_session,approved_version=excluded.approved_version,callbacks=excluded.callbacks,attempt_id=excluded.attempt_id`,contract,input.issuer,actor.operator.id,clock()+600000,actor.id,actor.operator.version,callbacks,crypto.randomUUID(),...fence.args);
21
- insist(approval.meta.changes===1,'authority_changed',409);
20
+ const attempt=crypto.randomUUID();
21
+ const approval=await store.first(`INSERT INTO rer_dependency_pending(interface,issuer,approved_by,expires_at,approved_session,approved_version,callbacks,attempt_id) SELECT ?,?,?,?,?,?,?,? WHERE ${fence.sql} ON CONFLICT(interface) DO UPDATE SET issuer=excluded.issuer,approved_by=excluded.approved_by,expires_at=excluded.expires_at,approved_session=excluded.approved_session,approved_version=excluded.approved_version,callbacks=excluded.callbacks,attempt_id=excluded.attempt_id RETURNING interface,attempt_id`,contract,input.issuer,actor.operator.id,clock()+600000,actor.id,actor.operator.version,callbacks,attempt,...fence.args);
22
+ insist(approval?.interface===contract && approval.attempt_id===attempt,'authority_changed',409);
22
23
  }
23
24
  // Snapshot the attempt before network I/O. Every authority and exact attempt
24
25
  // predicate is executed again in the transaction that installs the link.
@@ -36,8 +36,8 @@ export function createDependencies({db,origin,name,provider,interfaces=[{interfa
36
36
  insist(await store.first(`SELECT 1 WHERE ${initialApproval.sql}`,...initialApproval.args),'local_owner_proof_required',403);
37
37
  const metadata=await providerMetadata(expected,fetcher,contract),installation=await store.installation(contract);
38
38
  const codeDigest=await digest(expected+'\0'+data.code);
39
- const consumed=await store.run('INSERT INTO rer_dependency_codes(digest,created_at) VALUES(?,?) ON CONFLICT DO NOTHING',codeDigest,clock());
40
- insist(consumed.meta.changes===1,'link_code_used',409);
39
+ const consumed=await store.first('INSERT INTO rer_dependency_codes(digest,created_at) VALUES(?,?) ON CONFLICT DO NOTHING RETURNING digest',codeDigest,clock());
40
+ insist(consumed?.digest===codeDigest,'link_code_used',409);
41
41
  const now=Math.floor(clock()/1000), proof=await assertion(installation.key,{iss:origin,sub:origin,aud:metadata.link.redeem,iat:now,exp:now+60,jti:crypto.randomUUID(),code:data.code,interface:contract,sector_id:installation.sector_id,callback:origin+CALLBACK});
42
42
  const result=await readJson(fetcher,metadata.link.redeem,NETWORK,{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({code:data.code,interface:contract,origin,callback:origin+CALLBACK,client_assertion_type:'urn:ietf:params:oauth:client-assertion-type:jwt-bearer',client_assertion:proof})});
43
43
  insist(result.issuer===expected && result.interface===contract && result.sector_id===installation.sector_id && result.origin===origin && result.callback===origin+CALLBACK && typeof result.client_id==='string' && result.client_id.length>0 && result.client_id.length<=512,'invalid_link_grant',403);
@@ -52,7 +52,7 @@ Cron reads require the documented `result: {schedules: [...]}` envelope, includi
52
52
 
53
53
  `workerConsumerScript(consumer)` (also exported from `index.mjs`) decodes Worker consumer identities from either documented `script_name` or the observed GET response string `script`. It returns the exact name using the plan's resource-name grammar, or `null` for missing, malformed, conflicting, or non-Worker identities. If both fields are present, each must be valid and both must agree; a malformed alias is never ignored. Reconciliation checks every consumer on queues with a requested consumer before any trigger write and rejects foreign or duplicate consumers. Writes continue to use the documented `script_name` request field. Read-only health and retained-receipt repeats accept either response variant without repairs.
54
54
 
55
- D1/Queue/DO account listings fully paginate with bounded page/request budgets. R2 follows provider cursors. Missing pagination evidence, duplicate names, permission errors, partial success, redirects and transport failures fail visibly. Requests go only to Cloudflare's exact account API. Every read has a 30-second timeout and bounded response size; no request is retried. Asset upload tokens are used only on the documented same-account assets endpoint.
55
+ D1/Queue/DO account listings fully paginate with bounded page/request budgets. R2 follows provider cursors. Missing pagination evidence, duplicate names, permission errors, partial success, redirects and transport failures fail visibly. Requests go only to Cloudflare's exact account API. Requests have a 30-second timeout and bounded response size. Before any HTTP status, GET/HEAD reads and writes that provably failed before connecting (DNS failure, connection refusal or connect timeout) may retry once, sharing the original timeout and request budget. The first read reserves half the timeout for a retry; caller deadlines and cancellation still apply. HTTP responses, failed response-body reads and ambiguous write failures are never replayed. Asset upload tokens are used only on the documented same-account assets endpoint.
56
56
 
57
57
  The first receipt binds source commit SHA and tree, artifact/config SHA-256, effective deployment fingerprint, account/Worker, actual resource IDs, migration hashes, secret names and active provider deployment/version/etag. It contains no secret value. The CLI creates it exclusively at mode 0600 before the first health request; it is an identity receipt, not a claim that health passed (health attempts are added afterwards, see below). A failed health request still exits nonzero, while the retained identity allows the next explicit invocation to authenticate state without another upload. For the second deployment use the **same artifact, run/attempt and receipt**. Current provider state must match the prior receipt. The kit performs no resource creation, Worker/asset upload, SQL mutation, Cron/consumer update, or secret rewrite when unchanged. Read-only D1 queries use the provider's POST query transport; a POST is not by itself a database mutation. The receipt remains byte-identical on a repeat. Missing resources, drift or stale state fail without repair during this repeat.
58
58
 
@@ -109,6 +109,8 @@ Ownership lives in a state file, by default `.rer/deploy/<environment>.json` und
109
109
  The CLI prints JSON: refusal codes and resource names only, never inputs, tokens or provider bodies.
110
110
  A refused provider call also carries `providerRefusal` on the thrown error: the call (`PUT /workers/scripts/<name>`) and Cloudflare's own codes and messages, with the account id and token removed and each message cut to 300 characters. `rer deploy` names it when a custom deploy hook fails on one.
111
111
 
112
+ A transport failure carries `providerTransport`: the operation, sanitized endpoint, attempt count, whether an HTTP status was received and a structural network reason. Both standard and custom-hook `rer deploy` failures name the call and retain the same-input recovery command. Endpoint details omit URL credentials, queries, account ids and R2 object keys; signed D1 import destinations are withheld.
113
+
112
114
  ### rer deploy: inputs, the hosted owner, secret changes and removal
113
115
 
114
116
  `rer deploy` runs these steps for a seed whose adapter uses `ownerDeploy` from `cli/deploy.mjs`.
@@ -261,7 +263,7 @@ without metadata fails closed. Present metadata must supply a valid positive
261
263
  Cursors are encoded, validated and checked for cycles. Any supplied count or
262
264
  total count must agree with returned rows and completion, and totals cannot
263
265
  change between pages. Request, page, row and response-size budgets still apply.
264
- No metadata is fabricated and no request is retried. Generic `pagination:'cursor'`
266
+ No metadata is fabricated; only the bounded pre-status transport retry above applies. Generic `pagination:'cursor'`
265
267
  retains its existing contract: omitted metadata fails, and its R2 null-metadata
266
268
  exception accepts only a short page. D1/Queue/DO page pagination is unchanged.
267
269
  The bounded strategy is covered with local provider fixtures; this change does
@@ -24,6 +24,15 @@ export function providerRefusal(error, redact = String) {
24
24
  const said = refusal.errors.map(item => [item?.code, typeof item?.message === 'string' ? redact(item.message).slice(0,300) : undefined].filter(part => part !== undefined && part !== '').join(' ')).filter(Boolean).slice(0,3);
25
25
  return `Cloudflare refused ${redact(refusal.operation).slice(0,200)}${said.length ? `: ${said.join('; ')}` : ''}`;
26
26
  }
27
+ /** Safe call identity travels with errors from independently vendored kits. */
28
+ export function providerTransport(error, redact = String) {
29
+ const detail = error?.providerTransport;
30
+ if (typeof detail?.operation !== 'string' || typeof detail.endpoint !== 'string') return undefined;
31
+ const clean = value => redact(value).replace(/[\x00-\x1f\x7f-\x9f]+/g,' ').replace(/\b[a-f0-9]{32}\b/gi,'[REDACTED]').slice(0,300);
32
+ const attempts = detail.attempts === 2 ? ' after 2 attempts' : '';
33
+ const status = Number.isInteger(detail.status) ? `while reading HTTP ${detail.status}` : 'before any HTTP status';
34
+ return `Cloudflare transport failed for ${clean(detail.operation)} at ${clean(detail.endpoint)} ${status}${attempts}${typeof detail.reason === 'string' ? `: ${clean(detail.reason)}` : ''}`;
35
+ }
27
36
  function operation(path, options = {}) {
28
37
  const write = options.method && options.method !== 'GET';
29
38
  if (/\/d1\/database\/[^/]+\/query/.test(path)) return 'migrate D1';
@@ -0,0 +1,56 @@
1
+ // A missing HTTP status does not prove that a write never reached Cloudflare.
2
+ // Only these connection-establishment failures make replaying a write safe.
3
+ const UNSENT = new Set(['ENOTFOUND', 'EAI_AGAIN', 'ECONNREFUSED', 'UND_ERR_CONNECT_TIMEOUT']);
4
+ function unsent(error, seen = new Set()) {
5
+ if (!error || seen.has(error) || seen.size >= 8) return false;
6
+ seen.add(error);
7
+ if (Array.isArray(error.errors)) return error.errors.length > 0 && error.errors.every(cause => unsent(cause, seen));
8
+ if (typeof error.code === 'string') return UNSENT.has(error.code);
9
+ return unsent(error.cause, seen);
10
+ }
11
+
12
+ /** Retry only before a response, once, sharing the original deadline and budget. */
13
+ export async function providerResponse({send, method, timeoutMs, callerSignal, reserveRetry, failure}) {
14
+ const timeout = AbortSignal.timeout(timeoutMs);
15
+ const signal = callerSignal ? AbortSignal.any([callerSignal, timeout]) : timeout;
16
+ const read = method === 'GET' || method === 'HEAD';
17
+ for (let attempt = 1; ; attempt++) {
18
+ // Leave time for a second read when the first one times out. Caller deadlines
19
+ // remain authoritative and are never reset by a retry.
20
+ const firstRead = read && attempt === 1 ? new AbortController() : undefined;
21
+ const timer = firstRead ? setTimeout(() => firstRead.abort(new DOMException('Provider read timed out','TimeoutError')), Math.max(1, Math.ceil(timeoutMs / 2))) : undefined;
22
+ timer?.unref();
23
+ const attemptSignal = firstRead ? AbortSignal.any([signal, firstRead.signal]) : signal;
24
+ try { return {response:await send(attemptSignal),attempts:attempt}; }
25
+ catch (cause) {
26
+ if (attempt === 1 && !signal.aborted && (read || unsent(cause)) && reserveRetry()) continue;
27
+ throw failure(cause, attempt);
28
+ }
29
+ // Headers end the retry window; body consumption keeps the total deadline.
30
+ finally { clearTimeout(timer); }
31
+ }
32
+ }
33
+
34
+ export function transportOutput(error) {
35
+ const lines = [], seen = new Set();
36
+ for (let cause = error; cause && !seen.has(cause) && seen.size < 4; cause = cause.cause) {
37
+ seen.add(cause);
38
+ lines.push([typeof cause.code === 'string' ? cause.code : '', typeof cause.message === 'string' ? cause.message : ''].filter(Boolean).join(': '));
39
+ }
40
+ // Network errors can echo signed URLs. Keep their destination, never URL
41
+ // credentials, query parameters or fragments, before later secret redaction.
42
+ return lines.filter(Boolean).join('\n').replace(/\bhttps?:\/\/[^\s<>"']+/gi, value => {
43
+ try { const url = new URL(value); url.username = ''; url.password = ''; url.search = ''; url.hash = ''; return url.href; }
44
+ catch { return '[URL omitted]'; }
45
+ });
46
+ }
47
+
48
+ export function transportReason(error) {
49
+ const seen = new Set();
50
+ for (let cause = error; cause && !seen.has(cause) && seen.size < 4; cause = cause.cause) {
51
+ seen.add(cause);
52
+ if (typeof cause.code === 'string' && /^[A-Z][A-Z0-9_]{1,63}$/.test(cause.code)) return cause.code;
53
+ if (cause.name === 'TimeoutError' || cause.name === 'AbortError') return cause.name;
54
+ }
55
+ return 'network error';
56
+ }