@runeditrun/rer 0.2.6 → 0.2.8
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/README.md +5 -3
- package/cli/demo-host.mjs +7 -4
- package/cli/deploy-command.mjs +9 -4
- package/cli/deploy.mjs +3 -3
- package/global/CHANGELOG.md +18 -0
- package/modules/demo-host/README.md +26 -0
- package/modules/demo-host/capacity.mjs +14 -0
- package/modules/demo-host/container.mjs +71 -17
- package/modules/demo-host/diagnostics.mjs +35 -0
- package/modules/demo-host/operator.mjs +17 -0
- package/modules/demo-host/pages.mjs +2 -2
- package/modules/demo-host/plan.mjs +3 -2
- package/modules/demo-host/worker.mjs +14 -6
- package/package.json +1 -1
package/cli/README.md
CHANGED
|
@@ -282,9 +282,11 @@ sign-in address; a hook that reports no deployment, or whose app never names
|
|
|
282
282
|
the commit, fails with what happened and claims nothing. When the hook deployed
|
|
283
283
|
through the kit's standard deployment record, rer also records successful
|
|
284
284
|
ownership separately from retired secrets and removes the setup token before
|
|
285
|
-
returning success
|
|
286
|
-
that
|
|
287
|
-
|
|
285
|
+
returning success. A hook that deploys with the kit under its own state file
|
|
286
|
+
returns that file as `state` (`cli/deploy-hook-record.mjs` keeps the pointer):
|
|
287
|
+
every later step (inspect, setup token, owner record, `--set-secret`,
|
|
288
|
+
`--unset-secret`, `--remove --yes`) reads that record, so rer claims and retires
|
|
289
|
+
the token the same way; a hook that keeps an unnamed record keeps its token. An owner
|
|
288
290
|
already claimed by the hook is kept without another bootstrap. An existing owner
|
|
289
291
|
is explicitly reported as kept. Function owner hooks run their complete
|
|
290
292
|
bootstrap flow; setup manifest paths do not replace it. A missing or ambiguous
|
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
|
|
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-command.mjs
CHANGED
|
@@ -8,7 +8,7 @@ import { mutationReadinessFailure } from './deploy-readiness.mjs';
|
|
|
8
8
|
import { observeOwner } from './deploy-observation.mjs';
|
|
9
9
|
import { committedSource } from './deploy-source.mjs';
|
|
10
10
|
import { hookOrigin, waitForSource } from './deploy-hook.mjs';
|
|
11
|
-
import { rememberHookState } from './deploy-hook-record.mjs';
|
|
11
|
+
import { rememberHookState, hookState } from './deploy-hook-record.mjs';
|
|
12
12
|
import { main as owner } from '../modules/deploy-kit/owner-cli.mjs';
|
|
13
13
|
import { localOnlyBinding, ownerWorkerName } from '../modules/deploy-kit/owner.mjs';
|
|
14
14
|
import { refuseLocalModels } from '../modules/deploy-kit/local-only.mjs';
|
|
@@ -55,9 +55,13 @@ export async function deployCommand(context) {
|
|
|
55
55
|
const redaction = createRedaction();
|
|
56
56
|
const addSecret = value => { redaction.addSecrets([value]); store.addSecrets?.([value]); };
|
|
57
57
|
for (const value of [...credentialValues(env), env.CLOUDFLARE_ACCOUNT_ID, ...(declaration.secrets ?? []).map(item => env[item.environment])].filter(Boolean)) addSecret(value);
|
|
58
|
-
const
|
|
58
|
+
const environment = options.environment ?? 'production';
|
|
59
|
+
// A hook keeping the kit's record in its own state file named it; every kit
|
|
60
|
+
// step after the hook (inspect, secret, record-owner) reads that record.
|
|
61
|
+
let hookRecord = await hookState(root, environment);
|
|
62
|
+
const command = (verb, extra = [], values = env) => owner([verb, '--declaration', path, '--environment', environment, ...(hookRecord ? ['--state', hookRecord] : []), ...extra], values, { cwd: root, localOnlyKeys });
|
|
59
63
|
// A hook that deployed through the kit's standard record lets rer issue and
|
|
60
|
-
// retire the setup token; a hook keeping its own
|
|
64
|
+
// retire the setup token; a hook keeping an unnamed record of its own keeps its token.
|
|
61
65
|
let recorded;
|
|
62
66
|
const inspect = async () => {
|
|
63
67
|
try { return recorded = await command('inspect'); }
|
|
@@ -78,7 +82,8 @@ export async function deployCommand(context) {
|
|
|
78
82
|
Object.assign(process.env, saved);
|
|
79
83
|
}
|
|
80
84
|
if (options.dryRun) return deployed;
|
|
81
|
-
await rememberHookState(root,
|
|
85
|
+
await rememberHookState(root, environment, deployed?.state);
|
|
86
|
+
hookRecord = await hookState(root, environment);
|
|
82
87
|
const origin = hookOrigin(deployed, deployAgain(options));
|
|
83
88
|
step = 'wait for the deployed source';
|
|
84
89
|
await waitForSource({ origin, sourceSha, again: deployAgain(options) });
|
package/cli/deploy.mjs
CHANGED
|
@@ -57,10 +57,10 @@ export function ownerDeploy(declarationPath, lifecycle) {
|
|
|
57
57
|
const created = [];
|
|
58
58
|
let step = 'deploy', deployed;
|
|
59
59
|
try {
|
|
60
|
+
// A custom hook's own state file, when its deploy named one.
|
|
61
|
+
const state = await hookState(root, environment);
|
|
60
62
|
if (options.remove) {
|
|
61
63
|
step = 'remove';
|
|
62
|
-
// A custom hook's own state file, when its deploy named one.
|
|
63
|
-
const state = await hookState(root, environment);
|
|
64
64
|
const result = await owner('remove', state ? ['--state', state] : []);
|
|
65
65
|
await forgetHookState(root, environment);
|
|
66
66
|
store.report(`Removed ${result.removed.join(', ') || 'nothing (already gone)'}; read back: none of them remains.`);
|
|
@@ -69,7 +69,7 @@ export function ownerDeploy(declarationPath, lifecycle) {
|
|
|
69
69
|
if (change.length) {
|
|
70
70
|
step = 'secrets';
|
|
71
71
|
const binding = options.setSecret ?? options.unsetSecret;
|
|
72
|
-
await owner('secret', [options.setSecret ? '--set' : '--unset', binding]);
|
|
72
|
+
await owner('secret', [options.setSecret ? '--set' : '--unset', binding, ...(state ? ['--state', state] : [])]);
|
|
73
73
|
store.report(`${options.setSecret ? `Set ${binding}` : `Removed ${binding}`} on this environment's Worker, recorded its new version and verified hosted readiness.`);
|
|
74
74
|
return { binding, present: Boolean(options.setSecret), deployed: false };
|
|
75
75
|
}
|
package/global/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,23 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.2.8
|
|
4
|
+
|
|
5
|
+
- `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.
|
|
6
|
+
- 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.
|
|
7
|
+
- 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.
|
|
8
|
+
|
|
9
|
+
### Upgrade notes
|
|
10
|
+
|
|
11
|
+
- 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.
|
|
12
|
+
|
|
13
|
+
## 0.2.7
|
|
14
|
+
|
|
15
|
+
- `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.
|
|
16
|
+
|
|
17
|
+
### Upgrade notes
|
|
18
|
+
|
|
19
|
+
- None: a hook that already returns `state` (0.2.6 upgrade note) gets its owner claimed on its next `rer deploy`.
|
|
20
|
+
|
|
3
21
|
## 0.2.6
|
|
4
22
|
|
|
5
23
|
- A job-queue client no longer crashes with an unhandled `write EPIPE` when a process probing its identity or policy socket closes before the answer is written (a probe that timed out under load); the prober reports its own timeout or reset. This stopped Orbit's release producer twice.
|
|
@@ -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.
|
|
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()
|
|
34
|
-
headers: { origin, 'content-type': 'application/x-www-form-urlencoded' }, body: new URLSearchParams({ token }) });
|
|
35
|
-
|
|
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
|
-
|
|
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()
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
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.
|
|
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
|
-
|
|
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
|
-
|
|
57
|
-
|
|
58
|
-
|
|
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);
|