@runeditrun/rer 0.2.7 → 0.2.9

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
@@ -1,6 +1,6 @@
1
1
  import { cp, mkdir, readFile, rm, writeFile } from 'node:fs/promises';
2
2
  import { createRequire } from 'node:module';
3
- import { join } from 'node:path';
3
+ 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';
@@ -18,11 +18,12 @@ export async function demoHostCommand(project, options, store, signal, out, oper
18
18
  let plan;
19
19
  try { plan = demoPlan({ project: app.project, environment: options.environment ?? 'production', compatibilityDate: app.compatibilityDate, originVar: app.originVar, declaration, turnstileSiteKey: process.env.TURNSTILE_SITE_KEY?.trim() }); }
20
20
  catch (error) { throw invalid(`The demo deploy declaration is invalid (${error.message}). deploy-demo.json holds {"version": 1, "domain": "<run site apex>", "name": "<product name>"}, optionally "maxInstances" and plain "settings".`); }
21
+ if (process.env.RER_DEMO_OPERATOR_TOKEN && (process.env.RER_DEMO_OPERATOR_TOKEN.length < 32 || process.env.RER_DEMO_OPERATOR_TOKEN.length > 512)) throw invalid('RER_DEMO_OPERATOR_TOKEN must contain 32 to 512 characters.');
21
22
  const folder = join(project.root, '.rer', 'demo-host');
22
23
  await rm(folder, { recursive: true, force: true });
23
24
  await mkdir(folder, { recursive: true, mode: 0o700 });
24
25
  await store.step('Building the demo container image (workerd engine)');
25
- await imageFolder(project, operation, app, { output: join(folder, 'image'), engine: 'workerd' });
26
+ await imageFolder(project, operation, { ...app, secrets: [...(app.secrets ?? []), { binding: 'RER_DEMO_OPERATOR_TOKEN' }, { binding: 'TURNSTILE_SECRET_KEY' }] }, { output: join(folder, 'image'), engine: 'workerd' });
26
27
  for (const file of DEMO_FILES) await cp(new URL(`../modules/demo-host/${file}`, import.meta.url), join(folder, file));
27
28
  await writeFile(join(folder, 'wrangler.json'), JSON.stringify(plan.wrangler, null, 2) + '\n');
28
29
  const summary = { worker: plan.worker, origin: plan.origin, hostname: plan.hostname, folder: '.rer/demo-host', maxInstances: plan.maxInstances, settings: plan.settings, instanceType: 'basic', startsPerIpPerMinute: 3, minutes: 60, idleMinutes: 15, enableInternet: false };
@@ -38,9 +39,11 @@ export async function demoHostCommand(project, options, store, signal, out, oper
38
39
  if (names.some(name => !process.env[name]?.trim())) throw missing(`${names.join(', ')} are required; see DEMO.md "Hosting the demo".`);
39
40
  if (!options.json) for (const line of human) out.human(line);
40
41
  const secrets = join(folder, '.secrets.json');
41
- await writeFile(secrets, JSON.stringify({ TURNSTILE_SECRET_KEY: process.env.TURNSTILE_SECRET_KEY.trim() }), { mode: 0o600 });
42
+ await writeFile(secrets, JSON.stringify({ TURNSTILE_SECRET_KEY: process.env.TURNSTILE_SECRET_KEY.trim(), ...(process.env.RER_DEMO_OPERATOR_TOKEN ? { RER_DEMO_OPERATOR_TOKEN: process.env.RER_DEMO_OPERATOR_TOKEN } : {}) }), { mode: 0o600 });
42
43
  try {
43
- const wrangler = createRequire(join(project.root, 'package.json')).resolve('wrangler/bin/wrangler.js');
44
+ const require = createRequire(join(project.root, 'package.json'));
45
+ const manifest = require.resolve('wrangler/package.json');
46
+ const wrangler = join(dirname(manifest), JSON.parse(await readFile(manifest, 'utf8')).bin.wrangler);
44
47
  await store.run('Deploying the demo Worker and its container image', process.execPath, [wrangler, 'deploy', '--config', 'wrangler.json', '--secrets-file', '.secrets.json'], { cwd: folder, signal });
45
48
  } finally { await rm(secrets, { force: true }); }
46
49
  return { dryRun: false, deployed: true, origin: plan.origin, demo: summary };
package/cli/deploy.mjs CHANGED
@@ -109,7 +109,7 @@ function failureText({ error, step, created, again, retry, secretChange, deploye
109
109
  const at = step === 'deploy' ? diagnostic?.step ?? 'prepare deploy' : step;
110
110
  const input = /^missing-secret-([A-Z][A-Z0-9_]*)$/.exec(code)?.[1];
111
111
  if (input) return `Deploy needs ${input}, which the deploy declaration lists as a secret and the Worker does not hold it. Set ${input} in the environment or in an --env-file file (an owned 0600 file of KEY=VALUE lines), then run the same command again. Nothing was uploaded.${created.length ? ` Resources created this attempt: ${created.join(', ')}.` : ''}`;
112
- if (code === 'remove-incomplete') return `Removal stopped with ${error.remaining.join(', ')} still present${error.failed.length ? ` (${error.failed.map(item => `${item.resource}: ${item.code}`).join('; ')})` : ''}. Removed: ${error.removed.join(', ') || 'nothing'}. An R2 bucket must be empty before it can be deleted. Run ${again} --remove --yes again to finish.`;
112
+ if (code === 'remove-incomplete') return `Removal stopped with ${error.remaining.join(', ')} still present${error.failed.length ? ` (${error.failed.map(item => `${item.resource}: ${item.code}`).join('; ')})` : ''}. Removed: ${error.removed.join(', ') || 'nothing'}. Confirmed owned R2 buckets are emptied automatically; unproved buckets are never purged. Run ${again} --remove --yes again to finish.`;
113
113
  if (code === 'nothing-to-remove') return `This checkout has no rer deployment record for this environment, so --remove deletes nothing. Run it from the checkout that deployed it.`;
114
114
  if (code === 'owner-token-retirement-failed') return `The application is deployed at ${deployed?.origin}; owner ${error.owner}; setup token retirement failed (${redact(String(error.cause))}). Run ${again} --unset-secret ${error.tokenKey ?? 'SETUP_TOKEN'} using the same environment credentials, or run ${again} again to finish.`;
115
115
  if (code === 'owner-claim-failed') return `The application is deployed at ${deployed?.origin}, but claiming its owner failed: ${redact(String(error.cause))}. Owner claim and setup token retirement are not confirmed; run ${again} again using the same environment credentials to finish.`;
package/cli/errors.mjs CHANGED
@@ -1,3 +1,5 @@
1
+ import { transferDiagnostic } from '../modules/portability/http-client.mjs';
2
+
1
3
  export class RerError extends Error {
2
4
  constructor(code, message, exitCode, details = {}) { super(message); Object.assign(this, { code, exitCode, ...details }); }
3
5
  }
@@ -7,6 +9,8 @@ export const refuse = message => new RerError('safety_refusal', message, 4);
7
9
 
8
10
  export async function lifecycleError(error, store) {
9
11
  if(error instanceof RerError)return error;
12
+ const transfer=transferDiagnostic(error);
13
+ if(transfer)return new RerError('transfer_failed',transfer,1);
10
14
  const {shared}=await import('./shared.mjs');
11
15
  const {approvedFailure}=await shared('failures.mjs');
12
16
  const approved=approvedFailure(error);
package/cli/transfers.mjs CHANGED
@@ -22,6 +22,7 @@ export async function transfer(project, options, signal, custody) {
22
22
  // A later archive of the same installation carries its latest erasure journal.
23
23
  const journal = options.journal ? await contained(project.root, options.journal, { privateFile: true }) : undefined;
24
24
  await project.adapter.prepareData?.(project.root, custody.store, signal);
25
+ await custody.store.step(`${target === 'remote' ? 'Hosted' : 'Local'} ${direction}`);
25
26
  // Only now can the seed's Vite configuration load in a checkout that never built.
26
27
  await located.unserved(); await legacyOutput?.unserved();
27
28
  const result=await api[direction]({ root: project.root, target, path, journal, origin: options.origin, sessionFile, signal, platform:options.platform, legacyRecovery, ...custody });
@@ -1,5 +1,27 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.2.9
4
+
5
+ - `rer deploy --remove --yes` empties each R2 bucket whose saved creation identity proves rer owns it, deletes the bucket, and verifies removal. Buckets with uncertain or replaced ownership are refused; interrupted deletion retains the deployment record so removal can resume.
6
+ - A failed hosted export names the export step, job ID, HTTP status and returned machine error code. A successful pnpm preparation is no longer blamed for a later export failure.
7
+ - The waiting-for-owner Lobby shows setup-token guidance only for an edition that declares `manualSetupTokenClaim: true`; automatic owner claims need no token from the visitor.
8
+ - Base's shared and exported dependency graphs carry sharp 0.35.5, undici 7.29.1, source-map-js 1.2.2, patched esbuild edges at 0.28.1 or newer, and Playwright 1.63.0.
9
+
10
+ ### Upgrade notes
11
+
12
+ - Removal's existing `--yes` confirmation includes permanently deleting objects in buckets rer proves it created. Preserve an export before removing a deployment whose data you need.
13
+ - A seed using manual setup-token owner claims sets `manualSetupTokenClaim: true` in its edition identity. Seeds receive the shared runtime and dependency updates with their next Base refresh; updating the rer package alone does not refresh seed-owned runtime copies or dependency locks.
14
+
15
+ ## 0.2.8
16
+
17
+ - `rer deploy --demo` resolves Wrangler through its exported package manifest and gives the Cloudflare Container application a valid explicit name. This fixes deployments refused by package export restrictions or an application name containing consecutive dashes.
18
+ - Hosted demos record every readiness probe's timing and status or safe error category, plus available container exit codes. Early exits and stalled probes fail promptly within the existing startup budget. Failed destruction keeps the capacity reservation and schedules cleanup again.
19
+ - An optional host-operator credential grants access to a bounded, redacted failure summary on the existing start page. Visitors retain the generic failure page; their demo session and the diagnostic correlation ID grant no operator access.
20
+
21
+ ### Upgrade notes
22
+
23
+ - Redeploy the demo host to enable the new diagnostics. Set a separate random `RER_DEMO_OPERATOR_TOKEN` to enable the operator page; see `modules/demo-host/README.md`. Container stdout/stderr remains in Cloudflare's authenticated observability service.
24
+
3
25
  ## 0.2.7
4
26
 
5
27
  - `rer deploy` after a custom deploy hook that returned its own kit state file as `state` reads that record for every finalization step (inspect, the setup token, the owner record), so it issues a setup token, claims the hosted owner, retires the token and exits 0, instead of failing with `HTTP 403 (invalid_setup_token)` because it looked only at `.rer/deploy/<environment>.json`. `--set-secret` and `--unset-secret` read the same record. An untouched repeat still writes nothing.
@@ -28,3 +28,29 @@ it with these files and the seed's container image folder (workerd engine) to
28
28
  with its real Durable Objects and rate limit; Turnstile and the container
29
29
  platform are the only fakes. The end-to-end proof (`wrangler dev` with Docker)
30
30
  is in DEMO.md.
31
+
32
+ Startup probes keep the existing 120-second budget, with a separate five-second
33
+ HTTP probe limit. Every attempt logs its timestamp, elapsed time, duration and
34
+ HTTP status or fixed error category, including successful probes. Exit records
35
+ include the numeric platform exit code when supplied. A separate random
36
+ `x-rer-demo-correlation` response header joins these events without disclosing
37
+ the visitor's bearer cookie. Worker observability also enables the platform's
38
+ private Container stdout/stderr logs; historical stdout is not exposed by the
39
+ Container lifecycle API. Read those logs with an account principal granted
40
+ Workers Observability Write, and filter by the application and boot interval.
41
+
42
+ Optionally set a separate random `RER_DEMO_OPERATOR_TOKEN` (32–512 characters)
43
+ when running `rer deploy --demo`. It becomes a Worker secret. A host operator
44
+ can request `GET /__demo/operator/<correlation>` with
45
+ `Authorization: Bearer <RER_DEMO_OPERATOR_TOKEN>` to see the existing start page
46
+ with a bounded, redacted diagnostic line for a failed start. It retains the
47
+ last eight probe/exit records, for at most 15 minutes and 32 failed starts.
48
+ Unauthenticated requests, demo owner sessions and correlation IDs alone grant
49
+ no access. The token never belongs in a URL. Ordinary visitors keep the generic
50
+ failure page. No raw error text, health body, cookies or environment values are
51
+ included in this diagnostic.
52
+
53
+ Failed destruction retains its alarm and capacity reservation for cleanup;
54
+ only confirmed destruction (or an already-stopped container) releases the
55
+ place. A cleanup failure is logged separately and cannot replace the startup
56
+ failure shown to the visitor.
@@ -4,6 +4,20 @@ import { DurableObject } from 'cloudflare:workers';
4
4
  // ends. A start is admitted while fewer than max_instances demos are live; a
5
5
  // demo releases its place when it ends, and a place past its end is free anyway.
6
6
  export class DemoCapacity extends DurableObject {
7
+ async record(correlation, diagnostic) {
8
+ const records = await this.ctx.storage.get('diagnostics') ?? [];
9
+ const now = Date.now();
10
+ const recent = records.filter(record => record.expires > now).slice(-31);
11
+ recent.push({ correlation, expires: now + 15 * 60_000, diagnostic });
12
+ await this.ctx.storage.put('diagnostics', recent);
13
+ await this.ctx.storage.setAlarm(now + 15 * 60_000);
14
+ }
15
+ async diagnostic(correlation) {
16
+ const records = await this.ctx.storage.get('diagnostics') ?? [];
17
+ return records.find(record => record.correlation === correlation && record.expires > Date.now())?.diagnostic ?? null;
18
+ }
19
+ async alarm() { await this.ctx.storage.deleteAll(); }
20
+
7
21
  async acquire(id, ends, max) {
8
22
  const now = Date.now(), places = await this.ctx.storage.list();
9
23
  const over = [...places].filter(([, until]) => until <= now).map(([key]) => key);
@@ -1,5 +1,6 @@
1
1
  import { DurableObject } from 'cloudflare:workers';
2
2
  import { claimKeys, signClaim } from './claim.mjs';
3
+ import { bounded, boundedFetch, category, report } from './diagnostics.mjs';
3
4
  import { endedPage, clearCookie } from './pages.mjs';
4
5
 
5
6
  // One visitor's demo: a Durable Object (newUniqueId) and its container, the
@@ -14,39 +15,80 @@ const inside = url => `http://container${url.pathname}${url.search}`;
14
15
  const random = () => [...crypto.getRandomValues(new Uint8Array(32))].map(byte => byte.toString(16).padStart(2, '0')).join('');
15
16
 
16
17
  export class DemoContainer extends DurableObject {
18
+ constructor(ctx, env) {
19
+ super(ctx, env);
20
+ ctx.blockConcurrencyWhile(async () => {
21
+ const state = await ctx.storage.get(['correlation', 'started']);
22
+ this.correlation = state.get('correlation'); this.started = state.get('started');
23
+ if (this.container.running) this.watch();
24
+ });
25
+ }
26
+ watch() {
27
+ if (!this.watching) {
28
+ this.watching = true;
29
+ this.ctx.waitUntil(this.container.monitor().then(() => this.exited(), error => this.exited(error)));
30
+ }
31
+ }
32
+ records = [];
33
+ record(event, detail = {}) {
34
+ const row = { event, at: new Date().toISOString(), elapsedMs: Date.now() - this.started, ...detail };
35
+ this.records.push(row); if (this.records.length > 8) this.records.shift();
36
+ report(this.correlation, this.started, event, detail);
37
+ }
38
+ diagnostic() { return { correlation: this.correlation, tail: this.records, stdout: 'Provider container logs; not exposed by the lifecycle API.' }; }
17
39
  idle = IDLE;
40
+ startTimeout = 120_000;
41
+ probeTimeout = 5000;
42
+ cleanupRetry = 30_000;
18
43
  get container() { return this.ctx.container; }
19
44
  port() { return this.container.getTcpPort(PORT); }
20
45
 
21
46
  /** Start the container, wait for /health, claim the owner; returns the claim's 303. */
22
- async start({ origin, originVar, ends, endsLocal, settings }) {
47
+ async start({ origin, originVar, ends, endsLocal, settings, correlation }) {
48
+ this.correlation = correlation; this.started = Date.now(); this.stopped = false;
49
+ const deadline = this.started + this.startTimeout;
23
50
  const id = this.ctx.id.toString(), keys = await claimKeys(), now = Date.now();
24
- await this.ctx.storage.put({ ends, seen: now });
51
+ await this.ctx.storage.put({ ends, seen: now, correlation, started: this.started });
25
52
  await this.ctx.storage.setAlarm(Math.min(ends, now + this.idle));
53
+ report(correlation, this.started, 'start');
26
54
  this.container.start({ enableInternet: false, env: { ...settings, RER_DEMO: '1', [originVar]: origin, RER_INGRESS_ORIGIN: origin,
27
55
  SESSION_SECRET: random(), SETUP_TOKEN: random(), OWNER_EMAIL: OWNER, RER_OWNER_CLAIM_ISSUER: origin, RER_OWNER_CLAIM_INSTALLATION_ID: id,
28
56
  RER_OWNER_CLAIM_PUBLIC_KEYS: JSON.stringify(keys.publicKeys), RER_DEMO_EXPIRES: endsLocal } });
29
57
  // A container that stops by itself ends the demo too.
30
- this.container.monitor().then(() => this.end(), () => this.end());
31
- await this.healthy();
58
+ this.watch();
59
+ await this.healthy(deadline);
32
60
  const token = await signClaim(keys, { origin, installationId: id, email: OWNER, now: Date.now() });
33
- const claimed = await this.port().fetch('http://container/auth/owner-claim', { method: 'POST', redirect: 'manual',
34
- headers: { origin, 'content-type': 'application/x-www-form-urlencoded' }, body: new URLSearchParams({ token }) });
35
- await claimed.body?.cancel();
61
+ const claimed = await boundedFetch(this.port(), 'http://container/auth/owner-claim', { method: 'POST', redirect: 'manual',
62
+ headers: { origin, 'content-type': 'application/x-www-form-urlencoded' }, body: new URLSearchParams({ token }) }, Math.max(1, Math.min(10_000, deadline - Date.now())));
63
+ void claimed.body?.cancel().catch(() => {});
64
+ this.record('claim', { status: claimed.status });
65
+ if (this.stopped) throw new Error('container-stopped');
36
66
  if (claimed.status !== 303) throw new Error(`owner claim answered ${claimed.status}`);
37
67
  return { location: claimed.headers.get('location'), cookies: claimed.headers.getSetCookie() };
38
68
  }
39
69
 
40
- async healthy() {
41
- for (const deadline = Date.now() + 120_000; ;) {
70
+ async healthy(deadline) {
71
+ let attempt = 0;
72
+ while (Date.now() < deadline) {
73
+ if (this.stopped) throw new Error('container-stopped');
74
+ const probe = Date.now();
42
75
  try {
43
- const response = await this.port().fetch('http://container/health');
44
- await response.body?.cancel();
45
- if (response.ok) return;
46
- } catch { /* The container is still starting. */ }
47
- if (Date.now() > deadline) throw new Error('demo container never became healthy');
48
- await new Promise(resolve => setTimeout(resolve, 250));
76
+ const response = await boundedFetch(this.port(), 'http://container/health', {}, Math.min(this.probeTimeout, deadline - Date.now()));
77
+ void response.body?.cancel().catch(() => {});
78
+ this.record('health', { attempt: ++attempt, durationMs: Date.now() - probe, status: response.status });
79
+ if (response.ok && !this.stopped) return;
80
+ } catch (error) {
81
+ this.record('health', { attempt: ++attempt, durationMs: Date.now() - probe, error: category(error) });
82
+ }
83
+ await new Promise(resolve => setTimeout(resolve, Math.min(250, Math.max(0, deadline - Date.now()))));
49
84
  }
85
+ throw new Error('health-deadline');
86
+ }
87
+
88
+ async exited(error) {
89
+ this.stopped = true;
90
+ this.record('exit', { outcome: error ? category(error) : 'exited', ...(Number.isInteger(error?.exitCode) ? { exitCode: error.exitCode } : {}) });
91
+ try { await this.end(); } catch (cause) { report(this.correlation, this.started, 'cleanup', { error: category(cause) }); }
50
92
  }
51
93
 
52
94
  /** Every request carrying this demo's cookie: proxied while the demo lives, else the ended page. */
@@ -59,6 +101,7 @@ export class DemoContainer extends DurableObject {
59
101
 
60
102
  async alarm() {
61
103
  const state = await this.ctx.storage.get(['ends', 'seen']), now = Date.now();
104
+ if (await this.ctx.storage.get('stopping')) return this.end();
62
105
  if (!state.get('ends')) return;
63
106
  if (now >= state.get('ends') || now - state.get('seen') >= this.idle) return this.end();
64
107
  await this.ctx.storage.setAlarm(Math.min(state.get('ends'), state.get('seen') + this.idle));
@@ -66,9 +109,20 @@ export class DemoContainer extends DurableObject {
66
109
 
67
110
  /** Destroy the container and delete this demo's state and place; safe to repeat. */
68
111
  async end() {
69
- if (this.container.running) await this.container.destroy().catch(() => { /* Already stopping. */ });
112
+ if (this.ending) return this.ending;
113
+ this.ending = this.cleanup();
114
+ try { await this.ending; } finally { this.ending = null; }
115
+ }
116
+
117
+ async cleanup() {
118
+ this.stopped = true;
119
+ // Keep the alarm and capacity reservation if destruction fails; retry cleanup.
120
+ await this.ctx.storage.put('stopping', true);
121
+ await this.ctx.storage.setAlarm(Date.now() + this.cleanupRetry);
122
+ if (this.container.running) await bounded(this.container.destroy(), 5000);
123
+ await this.env.CAPACITY.get(this.env.CAPACITY.idFromName('capacity')).release(this.ctx.id.toString());
70
124
  await this.ctx.storage.deleteAlarm();
71
125
  await this.ctx.storage.deleteAll();
72
- await this.env.CAPACITY.get(this.env.CAPACITY.idFromName('capacity')).release(this.ctx.id.toString());
126
+ report(this.correlation, this.started, 'ended');
73
127
  }
74
128
  }
@@ -0,0 +1,35 @@
1
+ // Only fixed categories and numeric metadata enter provider logs. No request,
2
+ // container response body, cookie, environment value or raw error is retained.
3
+ export function category(error) {
4
+ const message = String(error?.message ?? '');
5
+ if (error?.name === 'TimeoutError' || error?.name === 'AbortError') return 'probe-timeout';
6
+ if (/connection refused|not listening|port.*not.*ready/i.test(message)) return 'port-not-ready';
7
+ if (/container.*not.*running|container.*stopped/i.test(message)) return 'container-stopped';
8
+ if (/disconnect|connection reset|network connection lost/i.test(message)) return 'connection-lost';
9
+ return error instanceof TypeError ? 'transport-type-error' : 'platform-error';
10
+ }
11
+ export function report(correlation, started, event, detail = {}) {
12
+ console.log(JSON.stringify({ event: `demo_${event}`, correlation, at: new Date().toISOString(), elapsedMs: Date.now() - started, ...detail }));
13
+ }
14
+ export async function boundedFetch(port, url, init, milliseconds) {
15
+ const controller = new AbortController();
16
+ let timer;
17
+ const timeout = new Promise((_, reject) => { timer = setTimeout(() => { controller.abort(); reject(new DOMException('probe deadline', 'TimeoutError')); }, milliseconds); });
18
+ try {
19
+ const request = port.fetch(url, { ...init, signal: controller.signal }).then(response => {
20
+ if (controller.signal.aborted) void response.body?.cancel().catch(() => {});
21
+ return response;
22
+ });
23
+ return await Promise.race([request, timeout]);
24
+ }
25
+ finally { clearTimeout(timer); }
26
+ }
27
+
28
+ export async function bounded(operation, milliseconds) {
29
+ let timer;
30
+ try {
31
+ return await Promise.race([operation, new Promise((_, reject) => {
32
+ timer = setTimeout(() => reject(new DOMException('operation deadline', 'TimeoutError')), milliseconds);
33
+ })]);
34
+ } finally { clearTimeout(timer); }
35
+ }
@@ -0,0 +1,17 @@
1
+ import { startPage } from './pages.mjs';
2
+
3
+ // This credential belongs to the demo HOST operator, never a demo's signed-in
4
+ // owner. It is supplied only as an Authorization header, never a URL/cookie.
5
+ export async function operatorPage(request, env, correlation) {
6
+ const expected = env.RER_DEMO_OPERATOR_TOKEN;
7
+ const authorization = request.headers.get('authorization') ?? '';
8
+ const supplied = authorization.startsWith('Bearer ') ? authorization.slice(7) : '';
9
+ if (!expected || expected.length < 32 || !supplied || supplied.length > 512) return new Response('Unauthorized', { status: 401 });
10
+ const digest = value => crypto.subtle.digest('SHA-256', new TextEncoder().encode(value));
11
+ const [a, b] = await Promise.all([digest(expected), digest(supplied)]);
12
+ let unequal = 0; const av = new Uint8Array(a), bv = new Uint8Array(b);
13
+ for (let i = 0; i < av.length; i++) unequal |= av[i] ^ bv[i];
14
+ if (unequal) return new Response('Unauthorized', { status: 401 });
15
+ const record = await env.CAPACITY.get(env.CAPACITY.idFromName('diagnostics')).diagnostic(correlation);
16
+ return startPage(env, { status: record ? 200 : 404, notice: record ? `Operator diagnostic: ${JSON.stringify(record)}` : 'No retained diagnostic for this start.' });
17
+ }
@@ -33,9 +33,9 @@ function html(env, { page, title, body, status = 200, headers = {}, turnstile =
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 } = {}) {
36
+ export function startPage(env, { status = 200, notice, headers = {} } = {}) {
37
37
  const name = escape(env.DEMO_NAME);
38
- return html(env, { page: 'start', title: `Try ${env.DEMO_NAME}`, status, turnstile: true, body: `<h1 id="title">Try ${name}</h1>
38
+ return html(env, { page: 'start', title: `Try ${env.DEMO_NAME}`, status, headers, turnstile: true, 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
41
  ${notice ? `<p class="notice" role="alert">${escape(notice)}</p>` : ''}<form id="start" method="post" action="/__demo/start"><div class="cf-turnstile" data-sitekey="${escape(env.TURNSTILE_SITE_KEY)}" data-size="flexible" data-callback="${notice ? 'rerDemoReady' : 'rerDemoStart'}"></div>
@@ -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'];
12
+ export const DEMO_FILES = ['worker.mjs', 'container.mjs', 'capacity.mjs', 'claim.mjs', 'pages.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
 
@@ -41,11 +41,12 @@ export function demoPlan({ project, environment, compatibilityDate, originVar, d
41
41
  const wrangler = {
42
42
  name: worker, main: 'worker.mjs', compatibility_date: compatibilityDate, workers_dev: false, preview_urls: false,
43
43
  routes: [{ pattern: hostname, custom_domain: true }],
44
+ observability: { enabled: true, head_sampling_rate: 1, logs: { enabled: true, invocation_logs: false } },
44
45
  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),
45
46
  ...(turnstileSiteKey ? { TURNSTILE_SITE_KEY: turnstileSiteKey } : {}) },
46
47
  durable_objects: { bindings: [{ name: 'DEMO', class_name: 'DemoContainer' }, { name: 'CAPACITY', class_name: 'DemoCapacity' }] },
47
48
  migrations: [{ tag: 'v1', new_sqlite_classes: ['DemoContainer', 'DemoCapacity'] }],
48
- containers: [{ class_name: 'DemoContainer', image: './image/Dockerfile', max_instances: maxInstances, instance_type: 'basic' }],
49
+ containers: [{ name: worker.replace(/-+/g, '-'), class_name: 'DemoContainer', image: './image/Dockerfile', max_instances: maxInstances, instance_type: 'basic' }],
49
50
  ratelimits: [{ name: 'STARTS', namespace_id: namespace, simple: { limit: 3, period: 60 } }],
50
51
  };
51
52
  return { worker, hostname, origin: `https://${hostname}`, maxInstances, settings: Object.keys(settings), wrangler };
@@ -4,6 +4,8 @@
4
4
  // then starts a container of their own, signs its owner in and sets the
5
5
  // rer_demo cookie. Every request with that cookie goes to the visitor's
6
6
  // container; nothing else on this host is reachable.
7
+ import { operatorPage } from './operator.mjs';
8
+ import { bounded, category, report } from './diagnostics.mjs';
7
9
  import { startPage, demoCookie } from './pages.mjs';
8
10
 
9
11
  export { DemoContainer } from './container.mjs';
@@ -46,22 +48,28 @@ async function start(request, env) {
46
48
  if (!await env.CAPACITY.get(env.CAPACITY.idFromName('capacity')).acquire(id.toString(), ends, Number(env.DEMO_MAX_INSTANCES))) {
47
49
  return startPage(env, { status: 503, notice: 'Every demo is in use right now. Try again in a few minutes.' });
48
50
  }
49
- const demo = env.DEMO.get(id);
51
+ const demo = env.DEMO.get(id), correlation = crypto.randomUUID(), started = Date.now();
50
52
  try {
51
- const signedIn = await demo.start({ origin: env.DEMO_ORIGIN, originVar: env.DEMO_ORIGIN_VAR, ends, endsLocal: localTime(ends, request.cf?.timezone), settings: JSON.parse(env.DEMO_SETTINGS) });
52
- const headers = new Headers({ location: signedIn.location, 'cache-control': 'no-store' });
53
+ 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);
54
+ const headers = new Headers({ location: signedIn.location, 'cache-control': 'no-store', 'x-rer-demo-correlation': correlation });
53
55
  for (const cookie of [...signedIn.cookies, demoCookie(env, id.toString())]) headers.append('set-cookie', cookie);
54
56
  return new Response(null, { status: 303, headers });
55
57
  } catch (error) {
56
- console.error('Demo start failed:', error.message);
57
- await demo.end();
58
- return startPage(env, { status: 502, notice: 'The demo did not start. Try again.' });
58
+ const failure = ['health-deadline', 'container-stopped'].includes(error.message) ? error.message : category(error);
59
+ report(correlation, started, 'failed', { error: failure });
60
+ try {
61
+ const diagnostic = { ...await bounded(demo.diagnostic(), 3000), failure };
62
+ await bounded(env.CAPACITY.get(env.CAPACITY.idFromName('diagnostics')).record(correlation, diagnostic), 3000);
63
+ } catch (capture) { report(correlation, started, 'diagnostic', { error: category(capture) }); }
64
+ try { await bounded(demo.end(), 6000); } catch (cleanup) { report(correlation, started, 'cleanup', { error: category(cleanup) }); }
65
+ return startPage(env, { status: 502, headers: { 'x-rer-demo-correlation': correlation }, notice: 'The demo did not start. Try again.' });
59
66
  }
60
67
  }
61
68
 
62
69
  export default {
63
70
  async fetch(request, env) {
64
71
  const url = new URL(request.url);
72
+ if (url.pathname.startsWith('/__demo/operator/')) return operatorPage(request, env, url.pathname.slice('/__demo/operator/'.length));
65
73
  if (url.pathname === '/__demo/start') return request.method === 'POST' ? start(request, env) : home();
66
74
  const id = demoId(request, env);
67
75
  if (id) return env.DEMO.get(id).fetch(request);
@@ -116,7 +116,11 @@ A refused provider call also carries `providerRefusal` on the thrown error: the
116
116
  - **Inputs.** Declared secrets come from the environment, then `--env-file FILE` (an owned 0600 file of `KEY=VALUE` lines), which wins. For a secret the Worker does not hold yet, rer supplies the setup manifest's owner email (`emailField`) from the local setup's private settings, and a fresh random value for each secret field the manifest generates (`generate`), such as the session secret and the setup token; a value the Worker already holds is kept. A still-missing secret stops the run before any write with a message that names it.
117
117
  - **Hosted owner.** After deploy and health pass, rer claims the setup owner on the hosted app: it POSTs the setup token to the seed's standard owner step (`hooks.owner`, by default `/auth/bootstrap`). 303 means claimed; `409 already_initialized` means an owner was already claimed and is kept. Then rer deletes the token secret from the Worker, records the new Worker version in the state file, and lists the token in `retiredSecrets`, so later plans no longer declare it. The build fingerprint still names it, and the recorded claim stays as recorded, so an untouched repeat deploy writes nothing. A Worker that still holds a token rer did not upload in this run (an older deploy) first gets a fresh token through the same recorded secret change. Dependency approvals that need the token run before the claim. A seed with an owner step function, an identity-provider claim, or no declared token secret claims its owner its own way.
118
118
  - **Secret changes.** `rer deploy --set-secret NAME` (value from the environment or `--env-file`, under the declaration's environment name) and `--unset-secret NAME` change one declared secret of the recorded Worker without a build. Before writing, rer records the starting version and the exact live names of all other secrets, never their values. Accepted or ambiguous writes retain that intent until readback confirms the change; explicit refusals clear it. An ambiguous failure, or a later command's preflight, can clear an unapplied intent only when the live deployment still matches the receipt and the newest listed version matches it too. A surviving intent blocks further mutations until consistency returns; retry the same command then. Recovery requires the recorded immediate successor (or the recorded confirming rotation's successor), the receipt's script etag and fingerprint, the intended binding's presence, and an unchanged set of other secret names. Ambiguous rotations require a confirming PUT before reporting success. Older intents without names may recover only from the exact starting version's historical bindings, with its id, etag and fingerprint verified; unavailable or malformed historical bindings refuse adoption. Each historical binding must have an exact provider type supported by this kit; unrecognized types cannot prove that a name was not a secret. Added, removed or renamed other secrets refuse deploy and removal before writes. Values remain opaque: these checks cannot distinguish an otherwise identical outside change to the pending binding.
119
- - **Removal.** `rer deploy --remove --yes` deletes what the state file says rer created for this environment: the Worker (schedules first; its Durable Objects go with it) and each D1, R2, KV and queue that is still the one rer created, after checking all of them before the first delete. A replaced or uncertain resource refuses the whole removal, except a resource whose create answer was lost: under this environment's derived name it is deleted once it is confirmed empty (a D1 with no tables, a KV namespace with no keys, an R2 bucket, which the provider refuses to delete while it holds objects); one holding data is refused as `uncertain-resource-not-empty`, and a queue, whose backlog rer cannot read, as `uncertain-resource-unverifiable` (delete it by hand if it is the empty one from the interrupted run). It then reads back that none is still listed and deletes the state file. An R2 bucket must be empty; a bucket with objects stays, the state file keeps it, and a later `--remove --yes` finishes. Resources rer did not create are never touched.
119
+ - **Removal.** `rer deploy --remove --yes` deletes what the state file says rer created for this environment: the Worker (schedules first; its Durable Objects go with it) and each D1, R2, KV and queue that is still the one rer created, after checking all of them before the first delete. A replaced or uncertain resource refuses the whole removal, except a D1 or KV whose create answer was lost: under this environment's derived name it is deleted once confirmed empty (a D1 with no tables, a KV namespace with no keys); one holding data is refused as `uncertain-resource-not-empty`, and an uncertain queue, whose backlog rer cannot read, as `uncertain-resource-unverifiable` (delete it by hand if it is the empty one from the interrupted run).
120
+
121
+ R2 removal requires a saved `created` identity (the bucket's creation date). Pending buckets or buckets missing that identity are refused as `uncertain-resource-create`, and same-named replacements as `owned-resource-replaced`, even if empty. For a confirmed bucket, removal automatically lists and deletes its objects before deleting the bucket, checking its identity again before every object delete and the bucket delete. Serialize outside bucket deletion and recreation with removal: the provider cannot make a delete conditional on that identity.
122
+
123
+ Removal reads back that no recorded resource remains before deleting the state file. On partial failure, the state file retains the remaining resources so a later `--remove --yes` can finish. Resources rer did not create are never touched.
120
124
 
121
125
  ### If the state file is lost
122
126
 
@@ -42,7 +42,9 @@ export async function main(argv = process.argv.slice(2), env = process.env, {fet
42
42
  try { declaration = JSON.parse(await regularBytes(resolve(root,values.declaration),256 * 1024)); }
43
43
  catch (error) { throw new Error(error.code === 'ENOENT' ? 'missing-owner-declaration' : 'invalid-owner-declaration'); }
44
44
  assert(typeof declaration?.project === 'string','missing-owner-project');
45
- const api = deploymentDiagnostics(cloudflare({accountId:env.CLOUDFLARE_ACCOUNT_ID,token:env.CLOUDFLARE_API_TOKEN,fetch}),{onCreated});
45
+ // Removal reads identity before each object DELETE and may span many pages.
46
+ // Bound each response, not the owner's bucket size; failures retain ownership.
47
+ const api = deploymentDiagnostics(cloudflare({accountId:env.CLOUDFLARE_ACCOUNT_ID,token:env.CLOUDFLARE_API_TOKEN,fetch,...(command === 'remove' ? {maxRequests:Infinity} : {})}),{onCreated});
46
48
  const target = await ownerTarget(api,{project:declaration.project,environment:values.environment});
47
49
  const statePath = values.state ? resolve(root,values.state) : join(root,'.rer','deploy',`${target.environment}.json`);
48
50
  if (command === 'remove') return {ok:true,...await ownerRemove({api,target,statePath}).catch(error => { throw api.annotateFailure(error,'remove'); })};
@@ -1,3 +1,4 @@
1
+ import { r2ObjectPages, r2ObjectPath } from './r2-objects.mjs';
1
2
  import { rm } from 'node:fs/promises';
2
3
  import { assert } from './validation.mjs';
3
4
  import { resourceKinds, findResource } from './resources.mjs';
@@ -9,16 +10,14 @@ import { KINDS, readOwnerState, writeOwnerState, withStateLock, withScopeHints,
9
10
 
10
11
  const remove = {
11
12
  d1: (api, id) => api.request(`/d1/database/${id}`, {method:'DELETE'}),
12
- r2: (api, _id, name) => api.request(`/r2/buckets/${name}`, {method:'DELETE'}),
13
13
  kv: (api, id) => api.request(`/storage/kv/namespaces/${id}`, {method:'DELETE'}),
14
14
  queues: (api, id) => api.request(`/queues/${id}`, {method:'DELETE'}),
15
15
  };
16
16
 
17
17
  /** A resource whose create response was lost (still `pending` in the state file) may be
18
18
  * rer's or someone else's. It is removed only under this environment's derived name and
19
- * once it is confirmed empty: a D1 with no tables, a KV namespace with no keys, an R2
20
- * bucket (the provider refuses to delete one holding objects). A queue's backlog cannot
21
- * be read, so an uncertain queue refuses with its own code. */
19
+ * once it is confirmed empty: a D1 with no tables or a KV namespace with no keys. R2 always
20
+ * requires a confirmed creation identity. A queue's backlog cannot be read, so an uncertain queue refuses with its own code. */
22
21
  async function confirmEmpty(api, worker, kind, name, id) {
23
22
  if (!name.startsWith(`${worker}-`)) throw refuse('uncertain-resource-create', kind, name);
24
23
  if (kind === 'queues') throw refuse('uncertain-resource-unverifiable', kind, name);
@@ -38,7 +37,8 @@ async function confirmEmpty(api, worker, kind, name, id) {
38
37
  * each only while it is still the one rer created. Read back that each is gone, then
39
38
  * drop the state file. Nothing else in the account is touched. Ownership is checked for
40
39
  * everything before the first delete; a replaced or uncertain resource refuses it all,
41
- * except an uncertain one confirmed empty (see confirmEmpty). */
40
+ * except an uncertain one confirmed empty (see confirmEmpty). R2 object purges
41
+ * additionally require the saved creation timestamp and recheck it before each delete. */
42
42
  export async function ownerRemove({api: provider, target, statePath, clock, sleep}) {
43
43
  const api = withSettledLists(withScopeHints(provider));
44
44
  return withStateLock(statePath, async () => {
@@ -64,9 +64,11 @@ export async function ownerRemove({api: provider, target, statePath, clock, slee
64
64
  for (const {name, entry} of owned.filter(item => item.kind === kind)) {
65
65
  const row = findResource(listed[kind], kind, name);
66
66
  if (!row) continue;
67
+ if (kind === 'r2') requireBucketIdentity(name, entry);
67
68
  if (entry.status === 'pending') await confirmEmpty(api, target.worker, kind, name, resourceId(kind, row));
68
69
  else checkResourceIdentity(kind, name, row, entry);
69
- doomed.push({kind, name, id:resourceId(kind, row)});
70
+ if (kind === 'r2' && entry.status === 'created' && entry.id) await checkBucket(api, name, entry);
71
+ doomed.push({kind, name, entry, id:resourceId(kind, row)});
70
72
  }
71
73
  }
72
74
  const removed = [];
@@ -79,7 +81,11 @@ export async function ownerRemove({api: provider, target, statePath, clock, slee
79
81
  if (worker) { await deleteWorker({api, worker, ...(clock ? {clock} : {}), ...(sleep ? {sleep} : {})}); removed.push(`worker:${worker}`); }
80
82
  const failed = [];
81
83
  for (const item of doomed) {
82
- try { await remove[item.kind](api, item.id, item.name); removed.push(`${item.kind}:${item.name}`); }
84
+ try {
85
+ if (item.kind === 'r2') await removeBucket(api, item);
86
+ else await remove[item.kind](api, item.id, item.name);
87
+ removed.push(`${item.kind}:${item.name}`);
88
+ }
83
89
  catch (error) { failed.push({resource:`${item.kind}:${item.name}`, code:error.message}); }
84
90
  }
85
91
  // Read back: nothing this environment created is still listed.
@@ -100,10 +106,34 @@ export async function ownerRemove({api: provider, target, statePath, clock, slee
100
106
  delete state.ownerClaim; delete state.retiredSecrets;
101
107
  }
102
108
  await writeOwnerState(statePath, state);
103
- const error = new Error('remove-incomplete'); error.remaining = remaining; error.failed = failed; error.removed = removed;
109
+ const error = new Error('remove-incomplete'); error.remaining = remaining; error.failed = failed; error.removed = removed.filter(resource => !remaining.includes(resource));
104
110
  throw error;
105
111
  }
106
112
  await rm(statePath, {force:true});
107
113
  return {worker:target.worker, removed, remaining:[]};
108
114
  });
109
115
  }
116
+
117
+ async function checkBucket(api, name, entry) {
118
+ const row = await api.request(`/r2/buckets/${name}`);
119
+ if (row?.name !== name) throw refuse('owned-resource-replaced', 'r2', name);
120
+ checkResourceIdentity('r2', name, row, entry);
121
+ }
122
+ function requireBucketIdentity(name, entry) {
123
+ if (entry.status !== 'created' || typeof entry.id !== 'string' || !entry.id) throw refuse('uncertain-resource-create', 'r2', name);
124
+ }
125
+ async function removeBucket(api, {name, entry}) {
126
+ requireBucketIdentity(name, entry);
127
+ await checkBucket(api, name, entry);
128
+ for await (const rows of r2ObjectPages(api, name)) {
129
+ for (const {key} of rows) {
130
+ // Preflight may precede Worker teardown. Re-read before each destructive
131
+ // request too. The provider has no conditional bucket-identity delete;
132
+ // out-of-band delete/recreate operations must be serialized by the owner.
133
+ await checkBucket(api, name, entry);
134
+ await api.request(r2ObjectPath(name, key), {method:'DELETE'});
135
+ }
136
+ }
137
+ await checkBucket(api, name, entry);
138
+ await api.request(`/r2/buckets/${name}`, {method:'DELETE'});
139
+ }
@@ -1,3 +1,5 @@
1
+ import { deleteR2Object } from './r2-object-transport.mjs';
2
+ import { isR2ObjectDelete } from './r2-objects.mjs';
1
3
  import { recordDeployFailure } from './diagnostics.mjs';
2
4
  import { importD1, importUploadURL } from './d1-import.mjs';
3
5
  import { assert, hexId } from './validation.mjs';
@@ -30,23 +32,23 @@ function transportOutput(error) {
30
32
  return lines.filter(Boolean).join('\n');
31
33
  }
32
34
  /** Only this fixed origin and the explicitly selected account ever receive the token. */
33
- export function cloudflare({ accountId, token, fetch: request = globalThis.fetch, timeoutMs = 30000, maxRequests = 500 }) {
35
+ export function cloudflare({ accountId, token, fetch: request = globalThis.fetch, objectDelete = deleteR2Object, timeoutMs = 30000, maxRequests = 500 }) {
34
36
  assert(hexId(accountId), 'invalid-cloudflare-account-id');
35
37
  assert(typeof token === 'string' && token.trim() && !/[\r\n]/.test(token), 'invalid-cloudflare-api-token');
36
38
  let requests = 0;
37
39
  // `zone` scope: the zone-level API (/zones/...), for a run site's zone lookup and redirect rule.
38
40
  async function envelope(path, { method = 'GET', body, assetToken, zone = false } = {}) {
39
- assert(typeof path === 'string' && /^\/[a-z0-9/_?=&.,%-]+$/i.test(path) && !path.includes('..') && !path.split('?')[0].includes('%'), 'invalid-provider-path');
41
+ assert(typeof path === 'string' && (isR2ObjectDelete(path, method) || /^\/[a-z0-9/_?=&.,%-]+$/i.test(path) && !path.includes('..') && !path.split('?')[0].includes('%')), 'invalid-provider-path');
40
42
  assert(!zone || /^\/zones(?:[/?]|$)/.test(path), 'invalid-provider-path');
41
43
  assert(++requests <= maxRequests, 'provider-request-budget');
42
44
  assert(!assetToken || path === '/workers/assets/upload?base64=true', 'invalid-asset-auth-target');
43
45
  // Asset-session credentials are discovered at runtime, outside the CLI env.
44
46
  // Preserve complete text for the CLI's remaining redaction before truncation.
45
- const redactAuth = text => /\/d1\/database\/[^/]+\/import$/.test(path) ? '[D1 import details omitted]' : [accountId,token,assetToken].filter(Boolean).flatMap(value=>[value,encodeURIComponent(value),JSON.stringify(value).slice(1,-1)]).sort((a,b)=>b.length-a.length).reduce((value,secret)=>value.split(secret).join('[REDACTED]'),text);
47
+ const redactAuth = text => /\/r2\/buckets\/[^/]+\/objects(?:[/?]|$)/.test(path) ? '[R2 object details omitted]' : /\/d1\/database\/[^/]+\/import$/.test(path) ? '[D1 import details omitted]' : [accountId,token,assetToken].filter(Boolean).flatMap(value=>[value,encodeURIComponent(value),JSON.stringify(value).slice(1,-1)]).sort((a,b)=>b.length-a.length).reduce((value,secret)=>value.split(secret).join('[REDACTED]'),text);
46
48
  const transportFailure = cause => recordDeployFailure(new Error('provider-transport-failed'),{output:redactAuth(transportOutput(cause))});
47
49
  const signal = AbortSignal.timeout(timeoutMs);
48
50
  let response;
49
- try { response = await request(zone ? `https://api.cloudflare.com/client/v4${path}` : `https://api.cloudflare.com/client/v4/accounts/${accountId}${path}`, {
51
+ try { response = isR2ObjectDelete(path, method) ? await objectDelete(`/client/v4/accounts/${accountId}${path}`, {headers:{Authorization:`Bearer ${token}`},signal}) : await request(zone ? `https://api.cloudflare.com/client/v4${path}` : `https://api.cloudflare.com/client/v4/accounts/${accountId}${path}`, {
50
52
  method, redirect: 'error', signal, headers: { Authorization: `Bearer ${assetToken ?? token}`, ...(body && !(body instanceof FormData) ? { 'Content-Type':'application/json' } : {}) },
51
53
  ...(body === undefined ? {} : { body: body instanceof FormData ? body : JSON.stringify(body) }),
52
54
  }); } catch (cause) { throw transportFailure(cause); }
@@ -62,7 +64,7 @@ export function cloudflare({ accountId, token, fetch: request = globalThis.fetch
62
64
  const bounded = text => redactAuth(text).replace(/[\x00-\x1f\x7f-\x9f]+/g,' ').trim().slice(0,300);
63
65
  const said = (Array.isArray(payload?.errors) ? payload.errors : []).filter(x => Number.isSafeInteger(x?.code) || typeof x?.message === 'string').slice(0,3)
64
66
  .map(x => ({...(Number.isSafeInteger(x.code) ? {code:x.code} : {}),...(typeof x.message === 'string' ? {message:bounded(x.message)} : {})}));
65
- error.providerRefusal = {operation:`${method} ${path.split('?')[0]}`,errors:said.length || payload !== undefined || !output.trim() ? said : [{message:bounded(output.trim().split('\n')[0])}]};
67
+ error.providerRefusal = {operation:`${method} ${path.split('?')[0].replace(/(\/r2\/buckets\/[^/]+\/objects)\/.*/, '$1/[object]')}`,errors:said.length || payload !== undefined || !output.trim() ? said : [{message:bounded(output.trim().split('\n')[0])}]};
66
68
  throw recordDeployFailure(error,{providerCodes:(Array.isArray(payload?.errors) ? payload.errors : []).filter(x => Number.isSafeInteger(x?.code)).map(x=>x.code).slice(0,8),
67
69
  output:redactAuth(Array.isArray(payload?.errors) ? payload.errors.filter(x=>typeof x?.message==='string').map(x=>x.message).join('\n') : output)});
68
70
  }
@@ -0,0 +1,19 @@
1
+ import https from 'node:https';
2
+ import { Readable } from 'node:stream';
3
+
4
+ // fetch normalizes dot segments, even percent-encoded ones. Object keys are
5
+ // opaque: send the validated account path verbatim through Node's HTTP client.
6
+ // This transport never follows redirects and never selects another origin.
7
+ export function deleteR2Object(path, {headers, signal}) {
8
+ return new Promise((resolve, reject) => {
9
+ const request = https.request({hostname:'api.cloudflare.com', port:443, path, method:'DELETE', headers, signal}, response => {
10
+ try {
11
+ const empty = [204,205,304].includes(response.statusCode);
12
+ if (empty) response.resume();
13
+ resolve(new Response(empty ? null : Readable.toWeb(response), {status:response.statusCode, headers:response.headers}));
14
+ } catch (error) { response.destroy(); reject(error); }
15
+ });
16
+ request.on('error', reject);
17
+ request.end();
18
+ });
19
+ }
@@ -0,0 +1,34 @@
1
+ import { assert } from './validation.mjs';
2
+
3
+ // Cloudflare's object REST API requires literal slashes, with other reserved
4
+ // characters encoded. The provider sends this path without URL normalization.
5
+ export function r2ObjectPath(bucket, key) {
6
+ assert(typeof key === 'string' && key.length > 0, 'unsafe-r2-object-key');
7
+ const encoded = key.split('/').map(part => (part === '.' || part === '..' ? part.replaceAll('.', '%2E') : encodeURIComponent(part)).replace(/[!'()*]/g, c => `%${c.charCodeAt(0).toString(16).toUpperCase()}`)).join('/');
8
+ return `/r2/buckets/${bucket}/objects/${encoded}`;
9
+ }
10
+ export function isR2ObjectDelete(path, method) {
11
+ if (method !== 'DELETE') return false;
12
+ const match = /^\/r2\/buckets\/([a-z0-9][a-z0-9-]{1,62}[a-z0-9])\/objects\/(.+)$/.exec(path);
13
+ if (!match) return false;
14
+ try { return r2ObjectPath(match[1], decodeURIComponent(match[2])) === path; } catch { return false; }
15
+ }
16
+
17
+ // The API returns result: Object[] and result_info.is_truncated/cursor, not the
18
+ // bucket-list shape. Yield bounded pages so removal never collects the bucket.
19
+ export async function* r2ObjectPages(api, bucket) {
20
+ const seen = new Set(); let cursor;
21
+ for (;;) {
22
+ const data = await api.envelope(`/r2/buckets/${bucket}/objects?per_page=1000${cursor ? `&cursor=${encodeURIComponent(cursor)}` : ''}`);
23
+ const rows = data.result, info = data.result_info;
24
+ assert(Array.isArray(rows) && rows.length <= 1000 && rows.every(row => typeof row?.key === 'string' && row.key.length), 'invalid-r2-object-list');
25
+ assert(info && typeof info.is_truncated === 'boolean' && (!info.delimited || Array.isArray(info.delimited) && !info.delimited.length), 'provider-incomplete-pagination');
26
+ assert(new Set(rows.map(row => row.key)).size === rows.length, 'invalid-r2-object-list');
27
+ if (info.is_truncated) {
28
+ assert(rows.length > 0 && typeof info.cursor === 'string' && info.cursor.length > 0 && info.cursor.length <= 4096 && !seen.has(info.cursor), 'provider-invalid-cursor');
29
+ cursor = info.cursor; seen.add(cursor);
30
+ }
31
+ yield rows;
32
+ if (!info.is_truncated) return;
33
+ }
34
+ }
@@ -13,7 +13,8 @@
13
13
  "pnpm": {
14
14
  "overrides": {
15
15
  "sharp": "0.35.5",
16
- "undici": "7.29.1"
16
+ "undici": "7.29.1",
17
+ "source-map-js": "1.2.2"
17
18
  }
18
19
  }
19
20
  }
@@ -7,6 +7,7 @@ settings:
7
7
  overrides:
8
8
  sharp: 0.35.5
9
9
  undici: 7.29.1
10
+ source-map-js: 1.2.2
10
11
 
11
12
  importers:
12
13
 
@@ -436,6 +436,7 @@ modules/portability/import.mjs
436
436
  modules/portability/payloads.mjs
437
437
  modules/portability/journal.mjs
438
438
  modules/portability/http.mjs
439
+ modules/portability/http-client.mjs
439
440
  modules/portability/cli.mjs
440
441
  modules/portability/cli-options.mjs
441
442
  modules/portability/schema/portability.sql
@@ -5,29 +5,23 @@ import { pathToFileURL, fileURLToPath } from 'node:url';
5
5
  import { options, cookieFile, absent, privateFile, checkpoint, promote } from './cli-options.mjs';
6
6
  import { readArchive } from './format.mjs';
7
7
  import { canonicalJson } from './hash.mjs';
8
+ import { transferClient, transferDiagnostic } from './http-client.mjs';
8
9
  export async function main(args) {
9
10
  const opts=options([...args]),cookie=await cookieFile(opts['session-file']);
10
11
  const endpoint=opts.origin+opts.basePath;
11
- async function request(path,method='GET',body) {
12
- let response;
13
- try{response=await fetch(endpoint+'/'+path,{method,redirect:'error',headers:{Cookie:cookie,Origin:opts.origin,...(body===undefined?{}:{'Content-Type':'application/json'})},body:body===undefined?undefined:JSON.stringify(body)});}
14
- catch{throw new Error('The transfer request failed.');}
15
- if(!response.ok) {await response.body?.cancel();throw new Error('The transfer was refused (HTTP '+response.status+').');}
16
- return response;
17
- }
18
- const get=async(...args)=>(await request(...args)).json();
12
+ const client=transferClient({endpoint,origin:opts.origin,cookie}),{get}=client;
19
13
  const shell=value=>"'"+value.replaceAll("'","'\\''")+"'";
20
14
  const resumeCommand=(id)=>'node '+shell(fileURLToPath(import.meta.url))+' '+opts.command+' --origin '+shell(opts.origin)+' --session-file '+shell(opts['session-file'])+' '+(opts.command==='export'?'--out '+shell(opts.out):'--from '+shell(opts.from))+' --resume '+id;
21
15
  if(opts.command==='export') {
22
16
  await absent(opts.out);
23
- const id=opts.resume??(await get('exports','POST',{})).id;
17
+ const id=client.jobId(opts.resume??(await get('exports','POST',{})).id,'exports');
24
18
  await checkpoint(opts.out+'.transfer.json',{version:1,kind:'export',id,origin:opts.origin});
25
19
  process.stdout.write('Job '+id+'\nResume: '+resumeCommand(id)+'\n');
26
- let status=await get('exports/'+id);
27
- while(status.phase==='building')status=await get('exports/'+id+'/advance','POST',{expectedPart:status.completed,maxParts:16});
28
- if(status.phase!=='complete')throw new Error('The export is not complete.');
29
- const response=await request('exports/'+id+'/download'),file=await privateFile(opts.out+'.partial',Boolean(opts.resume));
30
- try{for await(const record of readArchive(response.body))await file.writeFile(canonicalJson(record)+'\n');await file.sync();}
20
+ let path='exports/'+id,status=await get(path);
21
+ while(status.phase==='building'){path='exports/'+id+'/advance';status=await get(path,'POST',{expectedPart:status.completed,maxParts:16});}
22
+ if(status.phase!=='complete')throw client.failedJob(path,status);
23
+ const records=await client.download(id),file=await privateFile(opts.out+'.partial',Boolean(opts.resume));
24
+ try{for await(const record of records)await file.writeFile(canonicalJson(record)+'\n');await file.sync();}
31
25
  finally{await file.close();}
32
26
  await promote(opts.out+'.partial',opts.out);process.stdout.write('Export complete.\n');return;
33
27
  }
@@ -36,7 +30,7 @@ export async function main(args) {
36
30
  // Verify every record even when earlier uploads are already acknowledged.
37
31
  for await(const record of readArchive(source)) {
38
32
  if(record.kind==='manifest') {
39
- id??=(await get('imports','POST',{confirmed:true,manifest:record})).id;
33
+ id=client.jobId(id??(await get('imports','POST',{confirmed:true,manifest:record})).id,'imports');
40
34
  await checkpoint(opts.from+'.transfer.json',{version:1,kind:'import',id,origin:opts.origin});
41
35
  process.stdout.write('Job '+id+'\nResume: '+resumeCommand(id)+'\n');
42
36
  }else if(record.kind==='part')await get('imports/'+id+'/parts/'+record.index,'PUT',record);
@@ -50,7 +44,7 @@ export async function main(args) {
50
44
  if(opts.command==='abort')status=await get(collection+'/'+opts.resume+'/abort','POST',{});
51
45
  process.stdout.write(JSON.stringify(status)+'\n');
52
46
  }
53
- if(process.argv[1] && existsSync(process.argv[1]) && import.meta.url===pathToFileURL(realpathSync(process.argv[1])).href)main(process.argv.slice(2)).catch(()=>{
47
+ if(process.argv[1] && existsSync(process.argv[1]) && import.meta.url===pathToFileURL(realpathSync(process.argv[1])).href)main(process.argv.slice(2)).catch(error=>{
54
48
  // Errors may contain server diagnostics or filesystem paths. Do not echo them.
55
- process.stderr.write('Transfer failed: check the origin, absolute paths, output, session file and job status.\n');process.exitCode=1;
49
+ process.stderr.write((transferDiagnostic(error)??'Transfer failed: check the origin, absolute paths, output, session file and job status.')+'\n');process.exitCode=1;
56
50
  });
@@ -1,6 +1,6 @@
1
1
  import { archiveLines } from './lines.mjs';
2
2
  import { canonicalJson, sha256, StreamHash, unbase64, utf8 } from './hash.mjs';
3
- import { insist } from './storage.mjs';
3
+ import { insist, PortabilityError } from './storage.mjs';
4
4
  import { validateRow } from './catalogue.mjs';
5
5
  export const FORMAT='runeditrun-portability';
6
6
  export const MAX_LINE_BYTES=24*1024*1024;
@@ -107,7 +107,7 @@ async function* verifiedRecords(source,maxLineBytes,partBytes,validateManifest,r
107
107
  let manifest,verifier,archive,index=0,closed=false;
108
108
  for await(const line of archiveLines(source,maxLineBytes)) {
109
109
  insist(!closed,'data_after_closing');
110
- let record;try{record=JSON.parse(line);}catch{throw new Error('archive_json');}
110
+ let record;try{record=JSON.parse(line);}catch{throw new PortabilityError('archive_json');}
111
111
  if(!manifest) {await validateManifest(record);manifest=record;verifier=new ContentVerifier(manifest);archive=new ArchiveVerifier(manifest);yield record;continue;}
112
112
  if(record.kind==='closing') {
113
113
  insist(index===manifest.partCount && record.partCount===index,'part_count');
@@ -0,0 +1,97 @@
1
+ import { readArchive } from './format.mjs';
2
+ import { portabilityErrorCode } from './storage.mjs';
3
+
4
+ const jobPattern = /^[a-zA-Z0-9-]{1,100}$/;
5
+ const stages = new Set(['create', 'status', 'advance', 'download', 'parts', 'finalize', 'abort']);
6
+ const publicValue = (value, cookie) => ![cookie, cookie.slice(cookie.indexOf('=') + 1)].filter(Boolean).some(secret => value.includes(secret));
7
+ const jobId = (value, cookie) => typeof value === 'string' && jobPattern.test(value) && !value.includes('\n') && publicValue(value, cookie) ? value : undefined;
8
+
9
+ // The runner and a seed adapter can import independent maintained copies. Share
10
+ // identity approval within their realm, exposing only the bounded constructor
11
+ // and lookup, never the map or a way to approve an arbitrary Error/message.
12
+ const diagnosticKey = Symbol.for('runeditrun.portability.diagnostics.v1');
13
+ if (!Object.hasOwn(globalThis, diagnosticKey)) Object.defineProperty(globalThis, diagnosticKey, { value: createDiagnostics() });
14
+ const diagnostics = globalThis[diagnosticKey];
15
+ export const transferDiagnostic = error => diagnostics.get(error);
16
+ function createDiagnostics() {
17
+ const approved = new WeakMap();
18
+ function failure(path, status, body, cookie) {
19
+ const [collection, id, action] = path.split('/');
20
+ const operation = collection === 'exports' ? 'export' : 'import';
21
+ const step = stages.has(action) ? action : id ? 'status' : 'create';
22
+ const details = [];
23
+ if (jobId(id, cookie)) details.push('job ' + id);
24
+ if (Number.isInteger(status) && status >= 100 && status <= 599) details.push('HTTP ' + status);
25
+ const raw = body?.code ?? body?.error?.code ?? body?.error;
26
+ // Portability codes may append table, key or row details after a colon.
27
+ // Retain only the bounded machine identifier, never those private details.
28
+ const code = typeof raw === 'string' && publicValue(raw, cookie) ? raw.split(':', 1)[0] : undefined;
29
+ if (code && /^[A-Za-z][A-Za-z0-9_]{0,63}$/.test(code) && !code.includes('\n') && publicValue(code, cookie)) details.push('code ' + code);
30
+ else if (Number.isSafeInteger(raw) && raw >= 0 && publicValue(String(raw), cookie)) details.push('code ' + raw);
31
+ const message = `Hosted ${operation} ${step} failed${details.length ? ' (' + details.join('; ') + ')' : ''}.`;
32
+ const error = new Error(message);
33
+ approved.set(error, message);
34
+ return error;
35
+ }
36
+ return Object.freeze({ failure, get: error => approved.get(error) });
37
+ }
38
+
39
+ export function transferClient({ endpoint, origin, cookie }) {
40
+ const responses = new WeakMap();
41
+ const failure = (path, status, body) => diagnostics.failure(path, status, body, cookie);
42
+ async function request(path, method = 'GET', body) {
43
+ let response;
44
+ try {
45
+ response = await fetch(endpoint + '/' + path, { method, redirect: 'error',
46
+ headers: { Cookie: cookie, Origin: origin, ...(body === undefined ? {} : { 'Content-Type': 'application/json' }) },
47
+ body: body === undefined ? undefined : JSON.stringify(body) });
48
+ } catch { throw failure(path); }
49
+ if (!response.ok) throw failure(path, response.status, await errorBody(response));
50
+ return response;
51
+ }
52
+ return {
53
+ async download(id) {
54
+ const path = 'exports/' + id + '/download', response = await request(path);
55
+ // Consumption and verification happen after the HTTP request succeeds.
56
+ // Preserve that response's context without trusting a stream's Error.
57
+ return (async function* () {
58
+ try { yield* readArchive(response.body); }
59
+ catch (error) { throw failure(path, response.status, { code: portabilityErrorCode(error) }); }
60
+ })();
61
+ },
62
+ async get(...args) {
63
+ const response = await request(...args);
64
+ let body;
65
+ try { body = await response.json(); } catch { throw failure(args[0], response.status); }
66
+ if (!body || typeof body !== 'object' || Array.isArray(body)) throw failure(args[0], response.status);
67
+ responses.set(body, response.status);
68
+ return body;
69
+ },
70
+ jobId(value, collection) {
71
+ if (!jobId(value, cookie)) throw failure(collection);
72
+ return value;
73
+ },
74
+ failedJob(path, body) { return failure(path, responses.get(body), body); },
75
+ };
76
+ }
77
+
78
+ // Error responses can be arbitrary provider HTML or huge private diagnostics.
79
+ // Read a bounded JSON envelope solely to recover its machine code.
80
+ async function errorBody(response) {
81
+ const reader = response.body?.getReader();
82
+ if (!reader) return;
83
+ try {
84
+ let size = 0; const chunks = [];
85
+ for (;;) {
86
+ const { value, done } = await reader.read();
87
+ if (done) break;
88
+ size += value.length;
89
+ if (size > 16384) return;
90
+ chunks.push(value);
91
+ }
92
+ const bytes = new Uint8Array(size); let offset = 0;
93
+ for (const chunk of chunks) { bytes.set(chunk, offset); offset += chunk.length; }
94
+ return JSON.parse(new TextDecoder('utf-8', { fatal: true }).decode(bytes));
95
+ } catch { return; }
96
+ finally { try { await reader.cancel(); } catch {} reader.releaseLock(); }
97
+ }
@@ -1,3 +1,5 @@
1
+ import { PortabilityError } from './storage.mjs';
2
+
1
3
  /** Read one bounded UTF-8 line at a time; never materialize the complete archive. */
2
4
  export async function* archiveLines(source, maxLineBytes) {
3
5
  if (!Number.isSafeInteger(maxLineBytes) || maxLineBytes < 1) throw new Error('Invalid archive line limit.');
@@ -15,7 +17,7 @@ export async function* archiveLines(source, maxLineBytes) {
15
17
  }
16
18
  function line() {
17
19
  // Invalid UTF-8 is malformed archive text: the same constant diagnostic as invalid JSON.
18
- let value; try { value = decoder.decode(buffer.subarray(0, bytes)); } catch { throw new Error('archive_json'); }
20
+ let value; try { value = decoder.decode(buffer.subarray(0, bytes)); } catch { throw new PortabilityError('archive_json'); }
19
21
  bytes = 0;
20
22
  return value.endsWith('\r') ? value.slice(0, -1) : value;
21
23
  }
@@ -1,6 +1,9 @@
1
1
  import { canonicalJson } from './hash.mjs';
2
+ const errorCodes = new WeakMap();
3
+ // A verifier's original machine code remains independent of mutable properties.
4
+ export const portabilityErrorCode = error => errorCodes.get(error);
2
5
  export class PortabilityError extends Error {
3
- constructor(code, status=400) { super(code); this.code=code; this.status=status; }
6
+ constructor(code, status=400) { super(code); this.code=code; this.status=status; errorCodes.set(this,code); }
4
7
  }
5
8
  export function insist(condition, code, status=400) { if (!condition) throw new PortabilityError(code,status); }
6
9
  export const quote = name => '"'+name.replaceAll('"','""')+'"';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@runeditrun/rer",
3
- "version": "0.2.7",
3
+ "version": "0.2.9",
4
4
  "description": "The rer command: install, set up, run and launch Run Edit Run checkouts.",
5
5
  "license": "MIT",
6
6
  "type": "module",