@runeditrun/rer 0.2.29 → 0.2.30
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/contract.mjs +1 -1
- package/cli/demo-build.mjs +23 -0
- package/cli/demo-host.mjs +10 -70
- package/cli/demo-native.mjs +74 -0
- package/cli/lifecycle.mjs +1 -1
- package/cli/parse.mjs +1 -1
- package/global/CHANGELOG.md +13 -0
- package/modules/container/README.md +2 -15
- package/modules/container/host.mjs +1 -3
- package/modules/container/ingress.mjs +1 -15
- package/modules/container/worker-engine.mjs +2 -3
- package/modules/demo-host/README.md +23 -156
- package/modules/demo-host/build.mjs +49 -0
- package/modules/demo-host/capacity.mjs +8 -2
- package/modules/demo-host/claim.mjs +2 -2
- package/modules/demo-host/diagnostics.mjs +1 -18
- package/modules/demo-host/native/alarms.mjs +51 -0
- package/modules/demo-host/native/bindings.mjs +24 -0
- package/modules/demo-host/native/bridge.mjs +17 -0
- package/modules/demo-host/native/config.mjs +35 -0
- package/modules/demo-host/native/cron.mjs +31 -0
- package/modules/demo-host/native/d1.mjs +37 -0
- package/modules/demo-host/native/deploy-r2.mjs +43 -0
- package/modules/demo-host/native/isolate.mjs +36 -0
- package/modules/demo-host/native/loader.mjs +30 -0
- package/modules/demo-host/native/namespace.mjs +35 -0
- package/modules/demo-host/native/prepare.mjs +35 -0
- package/modules/demo-host/native/queues.mjs +62 -0
- package/modules/demo-host/native/r2-client.mjs +37 -0
- package/modules/demo-host/native/r2.mjs +43 -0
- package/modules/demo-host/native/resources.mjs +37 -0
- package/modules/demo-host/native/seed-object.mjs +22 -0
- package/modules/demo-host/native/sql.mjs +17 -0
- package/modules/demo-host/native/visitor.mjs +147 -0
- package/modules/demo-host/native/worker.mjs +22 -0
- package/modules/demo-host/pages.mjs +1 -1
- package/modules/demo-host/plan.mjs +9 -30
- package/modules/demo-host/remove.mjs +13 -23
- package/modules/demo-host/upgrade.mjs +41 -0
- package/modules/demo-host/worker.mjs +3 -4
- package/package.json +1 -1
- package/modules/demo-host/container.mjs +0 -221
- package/modules/demo-host/entrypoint.sh +0 -9
- package/modules/demo-host/image.mjs +0 -18
- package/modules/demo-host/placement.mjs +0 -37
- package/modules/demo-host/prepare.mjs +0 -28
- package/modules/demo-host/startup.mjs +0 -27
- package/modules/demo-host/template.mjs +0 -71
package/cli/contract.mjs
CHANGED
|
@@ -11,7 +11,7 @@ const definitions = {
|
|
|
11
11
|
build: [['--site'], 'Production build with private local settings excluded and restored. --site instead writes the static run site (homepage, /run, docs) to its declared folder and prints that folder.'],
|
|
12
12
|
verify: [[], 'Run the complete internal verification target.'],
|
|
13
13
|
reset: [['--yes'], 'Confirmed removal of the declared local inventory only.'],
|
|
14
|
-
deploy: [['--environment', '--dry-run', '--site', '--demo', '--host', '--env-file', '--image', '--bind-address', '--http-port', '--https-port', '--remove', '--yes', '--set-secret', '--unset-secret'], 'Owner-account deployment and health; dry run performs local validation/build only. Declared secrets come from the environment or --env-file (an owned 0600 KEY=VALUE file); a new Worker gets the local setup owner and fresh generated secrets when they are not given. After a healthy deploy rer claims the setup owner on the hosted app with the one-time setup token, then removes the token. --set-secret NAME (value from the environment or --env-file) and --unset-secret NAME change one secret of the deployed Worker without a build and record its new version. --remove --yes deletes what this environment\'s deploy record says rer created (Worker, D1, R2, KV, queues), reads back that each is gone, and touches nothing else. --site deploys the run site instead: an assets-only Worker on its custom domain with www redirected; with --dry-run it builds the site and prints the plan with no provider request. --demo deploys the hosted demo instead (deploy-demo.json): a front Worker on demo.<domain> giving each visitor their own signed-in, throwaway
|
|
14
|
+
deploy: [['--environment', '--dry-run', '--site', '--demo', '--host', '--env-file', '--image', '--bind-address', '--http-port', '--https-port', '--remove', '--yes', '--set-secret', '--unset-secret'], 'Owner-account deployment and health; dry run performs local validation/build only. Declared secrets come from the environment or --env-file (an owned 0600 KEY=VALUE file); a new Worker gets the local setup owner and fresh generated secrets when they are not given. After a healthy deploy rer claims the setup owner on the hosted app with the one-time setup token, then removes the token. --set-secret NAME (value from the environment or --env-file) and --unset-secret NAME change one secret of the deployed Worker without a build and record its new version. --remove --yes deletes what this environment\'s deploy record says rer created (Worker, D1, R2, KV, queues), reads back that each is gone, and touches nothing else. --site deploys the run site instead: an assets-only Worker on its custom domain with www redirected; with --dry-run it builds the site and prints the plan with no provider request. --demo deploys the hosted demo instead (deploy-demo.json): a front Worker on demo.<domain> giving each visitor their own signed-in, throwaway native Worker isolate of this seed with real public reads/checks and simulated external writes; --dry-run writes it to .rer/demo-host with no provider request; --demo --remove --yes deletes that demo (its Worker, demo.<domain> Custom Domain, each only while the record its deploy left proves it the demo\'s), reads back that each is gone, and is a no-op once it is.'],
|
|
15
15
|
export: [['--target', '--output', '--origin', '--session-file'], 'Complete supported installation archive with integrity and exclusions.'],
|
|
16
16
|
import: [['--target', '--input', '--journal', '--origin', '--session-file', '--yes', '--legacy-v1', '--acknowledge-unverified-metadata', '--output'], 'Confirmed atomic restore into an empty destination; --journal LATER_ARCHIVE also fences rows that installation erased since. Backup-only local v1 recovery requires --legacy-v1 --acknowledge-unverified-metadata and a new --output v2 archive; --journal requires normal v2 import.'],
|
|
17
17
|
doctor: [[], 'Read-only installed tool, configuration and state diagnostics.'],
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
|
+
import { readFile } from 'node:fs/promises';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
import { fileURLToPath } from 'node:url';
|
|
5
|
+
import { shared } from './shared.mjs';
|
|
6
|
+
|
|
7
|
+
export async function demoBundle(project, operation, declaration, output) {
|
|
8
|
+
const { root } = project;
|
|
9
|
+
const { withoutLocalVariables } = await shared('local/build.mjs');
|
|
10
|
+
const { buildEnvironment } = await shared('local/environment.mjs');
|
|
11
|
+
const seed = await readFile(join(root, 'seed.json'), 'utf8').then(JSON.parse).catch(() => ({}));
|
|
12
|
+
const declared = Array.isArray(seed.env) ? seed.env.map(item => item.name) : Object.keys(seed.env ?? {});
|
|
13
|
+
await withoutLocalVariables(root, async () => {
|
|
14
|
+
const builder = fileURLToPath(new URL('../modules/demo-host/build.mjs', import.meta.url));
|
|
15
|
+
const child = spawn(process.execPath, [builder, root, output, JSON.stringify(declaration)], {
|
|
16
|
+
cwd: root, stdio: 'inherit', env: buildEnvironment([...declared, ...(declaration.secrets ?? []).flatMap(item => [item.binding, item.environment])]),
|
|
17
|
+
});
|
|
18
|
+
const abort = () => child.kill('SIGTERM');
|
|
19
|
+
process.once('SIGTERM', abort); process.once('SIGINT', abort);
|
|
20
|
+
try { await new Promise((resolve, reject) => { child.once('error', reject); child.once('exit', code => code === 0 ? resolve() : reject(new Error('Demo build failed'))); }); }
|
|
21
|
+
finally { process.off('SIGTERM', abort); process.off('SIGINT', abort); if (child.exitCode === null && child.signalCode === null) child.kill('SIGTERM'); }
|
|
22
|
+
}, { nested: operation.token, settings: [...(project.adapter?.settings ?? []), ...(project.privateInputs ?? [])] });
|
|
23
|
+
}
|
package/cli/demo-host.mjs
CHANGED
|
@@ -1,83 +1,23 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { randomBytes } from 'node:crypto';
|
|
3
|
-
import { createRequire } from 'node:module';
|
|
4
|
-
import { dirname, join } from 'node:path';
|
|
1
|
+
import { readFile } from 'node:fs/promises';
|
|
5
2
|
import { contained } from './project.mjs';
|
|
6
|
-
import { invalid, missing,
|
|
7
|
-
import {
|
|
8
|
-
import { prepareDemoImage } from '../modules/demo-host/image.mjs';
|
|
9
|
-
import { DEMO_FILES, INSTANCE_TYPES, demoPlan } from '../modules/demo-host/plan.mjs';
|
|
10
|
-
import { awaitCertificates } from '../modules/demo-host/certificate.mjs';
|
|
11
|
-
import { MEASUREMENT_COOKIE, MEASUREMENT_HEADER, TESTING_SECRET } from '../modules/demo-host/measurement.mjs';
|
|
3
|
+
import { invalid, missing, RerError } from './errors.mjs';
|
|
4
|
+
import { demoPlan } from '../modules/demo-host/plan.mjs';
|
|
12
5
|
|
|
13
|
-
// `rer deploy --demo [--dry-run]`: the seed's hosted demo at demo.<domain>
|
|
14
|
-
// (modules/demo-host), from deploy-demo.json. It writes .rer/demo-host/: the
|
|
15
|
-
// front Worker, its wrangler.json and the container image folder (workerd
|
|
16
|
-
// engine). --dry-run stops there, so `wrangler dev` can serve that folder with
|
|
17
|
-
// Docker; a deploy runs `wrangler deploy` on it with the Turnstile secret and
|
|
18
|
-
// the demo's record as the version message, then waits for every demo host's
|
|
19
|
-
// certificate. On Turnstile's testing secret (a test host) it also sets the
|
|
20
|
-
// measurement token starts must carry, and says where it is. `--remove --yes` removes
|
|
21
|
-
// what that record proves is the demo's (modules/demo-host/remove.mjs).
|
|
22
6
|
export async function demoHostCommand(project, options, store, signal, out, operation) {
|
|
23
7
|
const read = async path => { try { return JSON.parse(await readFile(await contained(project.root, path), 'utf8')); } catch (error) { if (error instanceof RerError) throw error; throw invalid(`Missing or malformed ${path}.`); } };
|
|
24
8
|
const app = await read(project.adapter.deploy?.declaration ?? 'deploy.json'), declaration = await read('deploy-demo.json');
|
|
25
|
-
|
|
9
|
+
// Old releases wrote sizing and mode selectors. Consume their removal here
|
|
10
|
+
// so pinning this release needs no per-seed declaration edit.
|
|
11
|
+
const { instanceType: retiredSize, mode: retiredMode, ...nativeDeclaration } = declaration;
|
|
12
|
+
if (retiredSize !== undefined || retiredMode !== undefined) store.report('Hosted demos now use the native Loader host; remove the retired instanceType and mode keys from deploy-demo.json.');
|
|
26
13
|
let plan;
|
|
27
|
-
try { plan = demoPlan({ project: app.project, environment: options.environment ?? 'production', compatibilityDate: app.compatibilityDate, originVar: app.originVar, hostVariables: app.container?.hostVariables, declaration, turnstileSiteKey: process.env.TURNSTILE_SITE_KEY?.trim() }); }
|
|
28
|
-
catch (error) { throw invalid(`The demo deploy declaration is invalid (${error.message}). deploy-demo.json holds
|
|
14
|
+
try { plan = demoPlan({ project: app.project, environment: options.environment ?? 'production', compatibilityDate: app.compatibilityDate, originVar: app.originVar, hostVariables: app.container?.hostVariables, declaration: nativeDeclaration, turnstileSiteKey: process.env.TURNSTILE_SITE_KEY?.trim() }); }
|
|
15
|
+
catch (error) { throw invalid(`The demo deploy declaration is invalid (${error.message}). deploy-demo.json holds version, domain, name, and optionally maxInstances, ownerEmail and plain settings.`); }
|
|
29
16
|
if (options.remove) return removeDemo(plan, store, signal);
|
|
30
17
|
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.');
|
|
31
18
|
const supplied = process.env.RER_DEMO_MEASUREMENT_TOKEN?.trim();
|
|
32
19
|
if (supplied && (supplied.length < 32 || supplied.length > 512 || /[\s;,]/.test(supplied))) throw invalid('RER_DEMO_MEASUREMENT_TOKEN must contain 32 to 512 characters, with no spaces, semicolons or commas.');
|
|
33
|
-
|
|
34
|
-
await rm(folder, { recursive: true, force: true });
|
|
35
|
-
await mkdir(folder, { recursive: true, mode: 0o700 });
|
|
36
|
-
await store.step('Building the demo container image (workerd engine)');
|
|
37
|
-
await imageFolder(project, operation, { ...app, secrets: [...(app.secrets ?? []), { binding: 'RER_DEMO_OPERATOR_TOKEN' }, { binding: 'RER_DEMO_MEASUREMENT_TOKEN' }, { binding: 'TURNSTILE_SECRET_KEY' }] }, { output: join(folder, 'image'), engine: 'workerd' });
|
|
38
|
-
await prepareDemoImage(join(folder, 'image'));
|
|
39
|
-
for (const file of DEMO_FILES) await cp(new URL(`../modules/demo-host/${file}`, import.meta.url), join(folder, file));
|
|
40
|
-
await writeFile(join(folder, 'wrangler.json'), JSON.stringify(plan.wrangler, null, 2) + '\n');
|
|
41
|
-
const summary = { worker: plan.worker, origin: plan.origin, hostname: plan.hostname, hostnames: plan.hostnames, routes: plan.wrangler.routes, folder: '.rer/demo-host', maxInstances: plan.maxInstances, settings: plan.settings, instanceType: plan.instanceType, startsPerIpPerMinute: 3, minutes: 60, idleMinutes: 15, enableInternet: true };
|
|
42
|
-
const human = [`Demo plan: Worker ${summary.worker} on ${summary.hostnames.join(', ')} (custom domains; workers.dev off), written to ${summary.folder}`,
|
|
43
|
-
` one container per visitor (${summary.instanceType}, at most ${summary.maxInstances} at once, public internet, RER_DEMO=1), 3 starts per IP per minute behind Turnstile`,
|
|
44
|
-
` each demo ends after 60 minutes or 15 idle minutes; its container is destroyed and its state deleted`];
|
|
45
|
-
if (options.dryRun) {
|
|
46
|
-
if (options.json) out.event('demo', summary); else for (const line of human) out.human(line);
|
|
47
|
-
store.report('deploy --demo: dry run complete; no deployment occurred and no provider request was made.');
|
|
48
|
-
return { dryRun: true, deployed: false, demo: summary };
|
|
49
|
-
}
|
|
50
|
-
const names = ['CLOUDFLARE_ACCOUNT_ID', 'CLOUDFLARE_API_TOKEN', 'TURNSTILE_SITE_KEY', 'TURNSTILE_SECRET_KEY'];
|
|
51
|
-
if (names.some(name => !process.env[name]?.trim())) throw missing(`${names.join(', ')} are required; see DEMO.md "Hosting the demo".`);
|
|
52
|
-
if (!options.json) for (const line of human) out.human(line);
|
|
53
|
-
// Production hosts keep the real human check and never take a measurement token.
|
|
54
|
-
const measurement = TESTING_SECRET.test(process.env.TURNSTILE_SECRET_KEY.trim()) ? supplied || randomBytes(32).toString('base64url') : null;
|
|
55
|
-
const secrets = join(folder, '.secrets.json');
|
|
56
|
-
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 } : {}),
|
|
57
|
-
...(measurement ? { RER_DEMO_MEASUREMENT_TOKEN: measurement } : {}) }), { mode: 0o600 });
|
|
58
|
-
try {
|
|
59
|
-
const require = createRequire(join(project.root, 'package.json'));
|
|
60
|
-
const manifest = require.resolve('wrangler/package.json');
|
|
61
|
-
const wrangler = join(dirname(manifest), JSON.parse(await readFile(manifest, 'utf8')).bin.wrangler);
|
|
62
|
-
await store.run('Deploying the demo Worker and its container image', process.execPath, [wrangler, 'deploy', '--config', 'wrangler.json', '--secrets-file', '.secrets.json', '--message', plan.record], { cwd: folder, signal });
|
|
63
|
-
} finally { await rm(secrets, { force: true }); }
|
|
64
|
-
await store.step(`Waiting for the certificate on ${plan.hostnames.join(', ')}`);
|
|
65
|
-
try { await awaitCertificates({ hostnames: plan.hostnames, report: line => store.report(line) }); }
|
|
66
|
-
catch (error) {
|
|
67
|
-
if (error.message !== 'demo-certificate-timeout') throw error;
|
|
68
|
-
throw new RerError('deploy_refused', `The demo is deployed, but its certificate is not active 10 minutes later (${error.hosts.join('; ')}). Run the deploy again to wait again; nothing needs removing.`, 1);
|
|
69
|
-
}
|
|
70
|
-
let measured;
|
|
71
|
-
if (measurement) {
|
|
72
|
-
// Output redaction masks credentials, so a token rer made goes to a private file.
|
|
73
|
-
const file = '.rer/demo-host/measurement-token';
|
|
74
|
-
if (!supplied) await writeFile(join(project.root, file), measurement + '\n', { mode: 0o600 });
|
|
75
|
-
measured = { cookie: MEASUREMENT_COOKIE, header: MEASUREMENT_HEADER, token: supplied ? 'RER_DEMO_MEASUREMENT_TOKEN' : file };
|
|
76
|
-
const how = `send it as the cookie ${MEASUREMENT_COOKIE}=<token> on ${plan.hostname} or the header ${MEASUREMENT_HEADER}: <token>`;
|
|
77
|
-
if (options.json) out.event('demo-measurement', measured);
|
|
78
|
-
else out.human(`Test host: it starts demos only with the measurement token in ${supplied ? 'RER_DEMO_MEASUREMENT_TOKEN' : file}; ${how}.`);
|
|
79
|
-
}
|
|
80
|
-
return { dryRun: false, deployed: true, origin: plan.origin, demo: summary, ...(measured ? { measurement: measured } : {}) };
|
|
20
|
+
return (await import('./demo-native.mjs')).nativeDemoCommand(project, options, store, signal, out, operation, app, plan);
|
|
81
21
|
}
|
|
82
22
|
|
|
83
23
|
async function removeDemo(plan, store, signal) {
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { cp, mkdir, writeFile, rm, readFile } from 'node:fs/promises';
|
|
2
|
+
import { join, dirname } from 'node:path';
|
|
3
|
+
import { createRequire } from 'node:module';
|
|
4
|
+
import { randomBytes } from 'node:crypto';
|
|
5
|
+
import { demoBundle } from './demo-build.mjs';
|
|
6
|
+
import { upgradeDemo } from '../modules/demo-host/upgrade.mjs';
|
|
7
|
+
import { missing, invalid } from './errors.mjs';
|
|
8
|
+
import { DEMO_FILES } from '../modules/demo-host/plan.mjs';
|
|
9
|
+
import { cloudflare } from '../modules/deploy-kit/provider.mjs';
|
|
10
|
+
import { demoBucketName, prepareBucket } from '../modules/demo-host/native/deploy-r2.mjs';
|
|
11
|
+
import { RUNTIME_FILES } from '../modules/demo-host/native/config.mjs';
|
|
12
|
+
import { prepareNative } from '../modules/demo-host/native/prepare.mjs';
|
|
13
|
+
import { TESTING_SECRET } from '../modules/demo-host/measurement.mjs';
|
|
14
|
+
import { awaitCertificates } from '../modules/demo-host/certificate.mjs';
|
|
15
|
+
|
|
16
|
+
export async function nativeDemoCommand(project, options, store, signal, out, operation, app, plan) {
|
|
17
|
+
const folder = join(project.root, '.rer/demo-host');
|
|
18
|
+
await rm(folder, { recursive: true, force: true });
|
|
19
|
+
await mkdir(join(folder, 'modules/demo-host/native'), { recursive: true, mode: 0o700 });
|
|
20
|
+
await store.step('Building the native demo Worker bundle and static assets');
|
|
21
|
+
await demoBundle(project, operation, app, join(folder, 'bundle'));
|
|
22
|
+
for (const file of DEMO_FILES) await cp(new URL(`../modules/demo-host/${file}`, import.meta.url), join(folder, 'modules/demo-host', file));
|
|
23
|
+
for (const file of RUNTIME_FILES) await cp(new URL(`../modules/demo-host/native/${file}.mjs`, import.meta.url), join(folder, `modules/demo-host/native/${file}.mjs`));
|
|
24
|
+
for (const directory of ['deploy-kit']) await cp(new URL(`../modules/${directory}/`, import.meta.url), join(folder, 'modules', directory), { recursive: true });
|
|
25
|
+
await mkdir(join(folder, 'modules/container'), { recursive: true });
|
|
26
|
+
for (const file of ['migrations.mjs', 'demo-egress.mjs']) await cp(new URL(`../modules/container/${file}`, import.meta.url), join(folder, 'modules/container', file));
|
|
27
|
+
// Runtime imports use the same module topology as the maintained source.
|
|
28
|
+
const bundle = await prepareNative(folder);
|
|
29
|
+
const config = { ...plan.wrangler, main: 'modules/demo-host/native/worker.mjs', compatibility_flags: ['nodejs_compat'],
|
|
30
|
+
assets: { directory: './assets', binding: 'ASSETS', run_worker_first: true }, worker_loaders: [{ binding: 'LOADER' }],
|
|
31
|
+
};
|
|
32
|
+
if (bundle.bindings.buckets.length) config.r2_buckets = [{ binding: 'NATIVE_BUCKET', bucket_name: demoBucketName(plan.worker) }];
|
|
33
|
+
config.vars.NATIVE_SOURCE_DIGEST = bundle.digest;
|
|
34
|
+
if (process.env.RER_NATIVE_TEST_TTL_MS && TESTING_SECRET.test(process.env.TURNSTILE_SECRET_KEY ?? '')) {
|
|
35
|
+
if (!/^[1-9][0-9]{2,6}$/.test(process.env.RER_NATIVE_TEST_TTL_MS)) throw invalid('RER_NATIVE_TEST_TTL_MS must be positive milliseconds');
|
|
36
|
+
config.vars.NATIVE_TEST_TTL_MS = process.env.RER_NATIVE_TEST_TTL_MS;
|
|
37
|
+
}
|
|
38
|
+
await writeFile(join(folder, 'wrangler.json'), JSON.stringify(config, null, 2) + '\n');
|
|
39
|
+
const summary = { worker: plan.worker, origin: plan.origin, mode: 'loader', digest: bundle.digest, modules: Object.keys(bundle.modules).length, minutes: 60, idleMinutes: 15 };
|
|
40
|
+
if (options.dryRun) {
|
|
41
|
+
if (options.json) out.event('demo', { ...summary, hostnames: plan.hostnames, routes: config.routes });
|
|
42
|
+
else out.human(`Native demo plan: ${plan.origin} (Worker Loader), written to .rer/demo-host`);
|
|
43
|
+
return { dryRun: true, deployed: false, demo: summary };
|
|
44
|
+
}
|
|
45
|
+
const required = ['CLOUDFLARE_ACCOUNT_ID', 'CLOUDFLARE_API_TOKEN', 'TURNSTILE_SITE_KEY', 'TURNSTILE_SECRET_KEY'];
|
|
46
|
+
if (required.some(name => !process.env[name]?.trim())) throw missing(`${required.join(', ')} are required`);
|
|
47
|
+
const api = cloudflare({ accountId: process.env.CLOUDFLARE_ACCOUNT_ID, token: process.env.CLOUDFLARE_API_TOKEN });
|
|
48
|
+
config.migrations = await upgradeDemo({ api, plan });
|
|
49
|
+
if (bundle.bindings.buckets.length) {
|
|
50
|
+
const statePath = join(project.root, '.rer/demo-buckets', plan.worker + '.json');
|
|
51
|
+
const pending = await readFile(statePath, 'utf8').then(JSON.parse).catch(error => { if (error.code === 'ENOENT') return null; throw error; });
|
|
52
|
+
const bucket = await prepareBucket(api, plan, pending);
|
|
53
|
+
await mkdir(dirname(statePath), { recursive: true, mode: 0o700 });
|
|
54
|
+
await writeFile(statePath, JSON.stringify({ record: plan.record, bucket }), { mode: 0o600 });
|
|
55
|
+
config.vars.NATIVE_BUCKET_CREATED = bucket.creation_date;
|
|
56
|
+
await writeFile(join(folder, 'wrangler.json'), JSON.stringify(config, null, 2) + '\n');
|
|
57
|
+
}
|
|
58
|
+
await writeFile(join(folder, 'wrangler.json'), JSON.stringify(config, null, 2) + '\n');
|
|
59
|
+
const testing = TESTING_SECRET.test(process.env.TURNSTILE_SECRET_KEY);
|
|
60
|
+
const measurement = testing ? process.env.RER_DEMO_MEASUREMENT_TOKEN || randomBytes(32).toString('base64url') : undefined;
|
|
61
|
+
const secrets = { TURNSTILE_SECRET_KEY: process.env.TURNSTILE_SECRET_KEY, ...(measurement ? { RER_DEMO_MEASUREMENT_TOKEN: measurement } : {}),
|
|
62
|
+
...(process.env.RER_DEMO_OPERATOR_TOKEN ? { RER_DEMO_OPERATOR_TOKEN: process.env.RER_DEMO_OPERATOR_TOKEN } : {}) };
|
|
63
|
+
const secretFile = join(folder, '.secrets.json');
|
|
64
|
+
await writeFile(secretFile, JSON.stringify(secrets), { mode: 0o600 });
|
|
65
|
+
try {
|
|
66
|
+
const require = createRequire(join(project.root, 'package.json')), manifest = require.resolve('wrangler/package.json');
|
|
67
|
+
const wrangler = join(dirname(manifest), JSON.parse(await readFile(manifest, 'utf8')).bin.wrangler);
|
|
68
|
+
await store.run('Deploying the native demo host', process.execPath, [wrangler, 'deploy', '--config', 'wrangler.json', '--secrets-file', '.secrets.json', '--message', plan.record], { cwd: folder, signal });
|
|
69
|
+
} finally { await rm(secretFile, { force: true }); }
|
|
70
|
+
if (measurement) await writeFile(join(folder, 'measurement-token'), measurement, { mode: 0o600 });
|
|
71
|
+
await awaitCertificates({ hostnames: plan.hostnames, report: line => store.report(line) });
|
|
72
|
+
out.human(`Native demo deployed: ${plan.origin} (Worker Loader, one SQLite Durable Object per visitor; edge static assets).`);
|
|
73
|
+
return { deployed: true, demo: summary };
|
|
74
|
+
}
|
package/cli/lifecycle.mjs
CHANGED
|
@@ -86,7 +86,7 @@ async function runLifecycle(project, options, out, deployRedaction) {
|
|
|
86
86
|
const adapter = project.adapter;
|
|
87
87
|
// The run site: static pages built from edition identity, never the app build.
|
|
88
88
|
if (options.site) return await (await import('./site.mjs')).siteCommand(project, options, store, signal, out);
|
|
89
|
-
// The hosted demo: a
|
|
89
|
+
// The hosted demo: a native Worker Loader host on demo.<domain>.
|
|
90
90
|
if (options.verb === 'deploy' && options.demo) return await (await import('./demo-host.mjs')).demoHostCommand(project, options, store, signal, out, operation);
|
|
91
91
|
if (options.verb === 'install') {
|
|
92
92
|
const { dependencyEnvironment } = await shared('dependency-environment.mjs');
|
package/cli/parse.mjs
CHANGED
|
@@ -85,7 +85,7 @@ export function parse(argv, cwd = process.cwd(), { independent = false, deferTai
|
|
|
85
85
|
if (options.environment !== undefined && !/^[a-z][a-z0-9-]{0,39}$/.test(options.environment)) throw invalid('Provide a valid deployment environment name.');
|
|
86
86
|
for (const key of ['setSecret', 'unsetSecret']) if (options[key] !== undefined && !/^[A-Z][A-Z0-9_]{0,63}$/.test(options[key])) throw invalid('Name a declared secret binding, such as SESSION_SECRET.');
|
|
87
87
|
if(verb==='deploy'&&options.demo&&(options.site||['platform','image','bindAddress','httpPort','httpsPort','envFile','host','setSecret','unsetSecret'].some(key=>options[key]!==undefined)))throw invalid('--demo takes only --environment, --dry-run and --remove --yes: the hosted demo is its own Worker on demo.<domain>.');
|
|
88
|
-
if(verb==='deploy'&&options.demo&&options.remove&&(options.dryRun||!options.yes))throw invalid(options.dryRun?'Use --remove and --dry-run on separate runs.':'deploy --demo --remove deletes the hosted demo\'s Worker, its demo.<domain> Custom Domain
|
|
88
|
+
if(verb==='deploy'&&options.demo&&options.remove&&(options.dryRun||!options.yes))throw invalid(options.dryRun?'Use --remove and --dry-run on separate runs.':'deploy --demo --remove deletes the hosted demo\'s Worker, its demo.<domain> Custom Domain. Add --yes to confirm.');
|
|
89
89
|
if ((options.remove || options.setSecret || options.unsetSecret) && options.site) throw invalid('--remove, --set-secret and --unset-secret act on the application Worker, not the run site.');
|
|
90
90
|
if (options.origin !== undefined) {
|
|
91
91
|
let url; try { url = new URL(options.origin); } catch { throw invalid('Provide a valid credential-free origin.'); }
|
package/global/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,18 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.2.30
|
|
4
|
+
|
|
5
|
+
- **Hosted demos run as native Workers.** `rer deploy --demo` gives each visitor their own Worker Loader isolate of the seed's ordinary production bundle, with SQLite Durable Object storage and static files served at the edge, instead of a Cloudflare Container. Docker and the Containers permission are no longer needed for a new demo host. Portable container hosting (`rer build`/`rer deploy --platform container`, Node/workerd and SSH) is unchanged; only the hosted-demo image, runtime and placement code are removed.
|
|
6
|
+
- **Seed bindings in native demos.** The seed's Wrangler configuration supplies every D1, R2 bucket, queue and local Durable Object class: D1s and seed DOs get separate SQLite facets per visitor, R2 keys are prefixed by visitor and binding, and alarms deliver UTC crons, seed DO alarms and durable queue batches (ack/retry, delays, dead letters). Own-host reads keep the visitor's context, and a narrow resource bridge keeps seeds away from front admission and diagnostics. Unsupported bindings are refused by name at demo build.
|
|
7
|
+
- **Demo egress guard.** Native visitors' outbound requests go through the same `demo-egress` guard as before: direct mail/SMS provider APIs (SendGrid, Resend, Twilio, Mailgun, Postmark) and SMTP ports are refused, and reads of the demo's own hosts stay inside that visitor's copy.
|
|
8
|
+
- **Removal.** `rer deploy --demo --remove --yes` empties and removes the demo-owned R2 bucket and deletes the demo's Worker and Custom Domains covered by its ownership record, reading back their absence; an existing legacy Container application is removed only when its namespace proves ownership.
|
|
9
|
+
|
|
10
|
+
### Upgrade notes
|
|
11
|
+
|
|
12
|
+
- Pin rer 0.2.30 and run the same demo workflow. The Worker id and `demo.<domain>` stay the same. The first deploy verifies the previous version's ownership record, removes its Container application by exact id and migrates `DemoContainer` to `NativeDemo` (keeping `DemoCapacity`). Visitor copies that are running end during the upgrade. That one deploy's token still needs Containers: Edit; later deploys do not.
|
|
13
|
+
- `deploy.json` no longer needs `"container"` in `platforms` for a hosted demo. Retired sizing and mode keys in `deploy-demo.json` (such as `instanceType`) are consumed with a migration notice; remove them when next editing the file.
|
|
14
|
+
- Worker Loader/Facets access on Workers Paid is required; no Workers for Platforms subscription is needed.
|
|
15
|
+
|
|
3
16
|
## 0.2.29
|
|
4
17
|
|
|
5
18
|
- **Placeholder mark (SC-14).** While a seed's logo, favicon or app mark is byte-equal to Base's placeholder mark, `rer check --profile first-party`, `rer build --site` and `rer deploy --site` print one warning naming the files (a `placeholder_mark` warning event under `--json`) and still succeed. `rer release` refuses the placeholder unless `--allow-placeholder-mark` is given; the receipt then records `placeholderMark: { allowed: true, files }`. Base's own projects are exempt.
|
|
@@ -33,13 +33,7 @@ The host forwards WebSocket upgrades through the same ingress and app session
|
|
|
33
33
|
checks as HTTP. An app response with status 101 and a `WebSocketPair` is bridged
|
|
34
34
|
in both directions, preserving text and binary messages, selected protocols,
|
|
35
35
|
cookies and close codes. Refused upgrades retain their HTTP response; other
|
|
36
|
-
upgrade protocols receive 400.
|
|
37
|
-
container before forwarding the upgrade, including on declared secondary hosts.
|
|
38
|
-
In a hosted demo (`RER_DEMO=1` with `RER_INGRESS_ORIGIN`), the workerd demo
|
|
39
|
-
egress serves the app's own fetch of any of its ingress origins through this
|
|
40
|
-
container's ingress, never the network: the front admits only the visitor's
|
|
41
|
-
cookie, so this is how the app reads its own public pages. Other destinations
|
|
42
|
-
keep the demo egress and its refusals.
|
|
36
|
+
upgrade protocols receive 400.
|
|
43
37
|
|
|
44
38
|
The workerd source boundary permits Durable Object definitions, including export
|
|
45
39
|
aliases and indirect inheritance. Unbound definitions create no namespace. The
|
|
@@ -77,7 +71,7 @@ Managed mail's `MAIL_PROVIDER`, `MAIL_FROM` and `MAIL_RELAY_URL` are public
|
|
|
77
71
|
configuration. Keep them in the mail spec's public variables and Wrangler `vars`,
|
|
78
72
|
and remove them from `deploy.json` secrets. For migration, the image builder
|
|
79
73
|
accepts those legacy secret declarations when the managed-mail spec declares them public,
|
|
80
|
-
and prints that correction.
|
|
74
|
+
and prints that correction. Runtime settings override their image defaults.
|
|
81
75
|
API keys, tokens and other secrets in image variables remain refused.
|
|
82
76
|
|
|
83
77
|
`rer deploy --platform container --host ssh://user@host --env-file FILE` uses
|
|
@@ -90,13 +84,6 @@ headers from other peers are discarded. `RER_SSH_IDENTITY` and
|
|
|
90
84
|
are logged. Local loopback origins support a disposable SSH Docker host for
|
|
91
85
|
qualification. Owner hosting/account setup remains an owner step.
|
|
92
86
|
|
|
93
|
-
An isolated front can set `RER_INGRESS_ORIGIN` to its primary public origin and
|
|
94
|
-
`RER_INGRESS_ORIGINS` to a JSON array of allowed HTTPS origins. It must overwrite
|
|
95
|
-
`x-rer-ingress-origin` on each forwarded request with its validated public
|
|
96
|
-
origin. The container accepts that header only with a fixed primary origin and
|
|
97
|
-
an exact allowlist match, then strips it before the app. Requests without the
|
|
98
|
-
header, including health probes and owner claims, use the primary origin.
|
|
99
|
-
|
|
100
87
|
A seed can package its own scoped Node runner with a declaration such as:
|
|
101
88
|
|
|
102
89
|
```json
|
|
@@ -12,13 +12,11 @@ export async function startHost({ root = '/app', data = '/data', settings = proc
|
|
|
12
12
|
const release = await recordVolumeOwner(data);
|
|
13
13
|
let engine, server, timer, idleTimer, runner, closing, closeWebSockets, lastMinute = -1, lastRequest=Date.now();
|
|
14
14
|
try {
|
|
15
|
-
const origins = settings.RER_INGRESS_ORIGINS === undefined ? undefined : JSON.parse(settings.RER_INGRESS_ORIGINS);
|
|
16
|
-
if (origins !== undefined && (!Array.isArray(origins) || origins.some(value => { try { const url = new URL(value); return typeof value !== 'string' || url.protocol !== 'https:' || url.origin !== value; } catch { return true; } }))) throw new Error('Invalid ingress origins');
|
|
17
15
|
const create = manifest.engine === 'node' ? (await import('./node-engine.mjs')).nodeEngine : (await import('./worker-engine.mjs')).workerEngine;
|
|
18
16
|
engine = await create({ root,data,manifest,settings,edge });
|
|
19
17
|
const schedules = (manifest.config.triggers?.crons ?? []).map(cron => [cron,parseCron(cron)]);
|
|
20
18
|
const tick = async time => { for (const [cron, expression] of schedules) if (cronMatches(expression, new Date(time))) await engine.scheduled({ cron,scheduledTime:time }); await engine.drain(); };
|
|
21
|
-
const requestFrom = req => incoming(req, { trustedProxy:settings.TRUSTED_PROXY,
|
|
19
|
+
const requestFrom = req => incoming(req, { trustedProxy:settings.TRUSTED_PROXY, maxRequestBytes:manifest.maxRequestBytes });
|
|
22
20
|
server = createServer(async (req, res) => {
|
|
23
21
|
try {
|
|
24
22
|
lastRequest=Date.now();
|
|
@@ -1,26 +1,12 @@
|
|
|
1
1
|
import { Readable } from 'node:stream';
|
|
2
2
|
import { lookup } from 'node:dns/promises';
|
|
3
3
|
import { isIP } from 'node:net';
|
|
4
|
-
|
|
5
|
-
// peer is its own front, such as a hosted demo's Durable Object. That front can
|
|
6
|
-
// select another explicitly allowed HTTPS origin with x-rer-ingress-origin.
|
|
7
|
-
export async function incoming(req, { trustedProxy, origin, origins, maxRequestBytes = 25 * 1024 * 1024 } = {}) {
|
|
4
|
+
export async function incoming(req, { trustedProxy, maxRequestBytes = 25 * 1024 * 1024 } = {}) {
|
|
8
5
|
const headers = new Headers(); for (const [name, value] of Object.entries(req.headers)) if (value !== undefined) headers.set(name, Array.isArray(value) ? value.join(',') : value);
|
|
9
6
|
const peer = req.socket.remoteAddress?.replace('::ffff:', '') ?? '';
|
|
10
7
|
const trusted = trustedProxy && (isIP(trustedProxy) ? peer===trustedProxy : (await lookup(trustedProxy,{all:true})).some(item=>item.address.replace('::ffff:','')===peer));
|
|
11
8
|
let host = req.headers.host, proto = 'http', address = peer;
|
|
12
9
|
if (trusted) { host = req.headers['x-forwarded-host'] ?? host; proto = req.headers['x-forwarded-proto'] ?? proto; address = req.headers['x-forwarded-for']?.split(',').at(-1)?.trim() ?? peer; }
|
|
13
|
-
if (origin) {
|
|
14
|
-
const requested = req.headers['x-rer-ingress-origin'];
|
|
15
|
-
let fixed = new URL(origin);
|
|
16
|
-
if (requested !== undefined) {
|
|
17
|
-
let selected;
|
|
18
|
-
try { selected = new URL(requested); } catch {}
|
|
19
|
-
if (!Array.isArray(origins) || !origins.includes(requested) || selected?.protocol !== 'https:' || selected.origin !== requested) throw Object.assign(new Error('Invalid ingress origin'), { status: 400 });
|
|
20
|
-
fixed = selected;
|
|
21
|
-
}
|
|
22
|
-
host = fixed.host; proto = fixed.protocol.slice(0, -1);
|
|
23
|
-
}
|
|
24
10
|
for (const name of ['cf-connecting-ip','x-forwarded-host','x-forwarded-proto','x-forwarded-for','forwarded','x-rer-original-url','x-rer-ingress-origin']) headers.delete(name);
|
|
25
11
|
if (!['http', 'https'].includes(proto) || typeof host !== 'string' || !/^(?:[a-zA-Z0-9.-]+|\[[0-9a-fA-F:]+\])(?::[0-9]+)?$/.test(host)) throw new Error('Invalid ingress origin');
|
|
26
12
|
headers.set('cf-connecting-ip', address); headers.set('host', host);
|
|
@@ -19,9 +19,8 @@ export async function workerEngine({ root, data, manifest, settings, edge }) {
|
|
|
19
19
|
};
|
|
20
20
|
const mf = new Miniflare(convertV4MiniflareOptions({ host:'127.0.0.1',port:0,cf:false,resourcePersistencePath:data,unsafeLocalExplorer:false,
|
|
21
21
|
workers:[{ name:'ingress',modules:true,script:`export default {fetch(request,env){const observed=request.headers.get('x-rer-original-url');if(!observed)return new Response(null,{status:400});const url=new URL(observed),headers=new Headers(request.headers);headers.delete('x-rer-original-url');headers.set('host',url.host);return env.APP.fetch(new Request(url,{method:request.method,headers,redirect:'manual',...(!['GET','HEAD'].includes(request.method)?{body:request.body}:{})}))}}`,serviceBindings:{ APP:'app' } }, app,
|
|
22
|
-
{ name:'egress',modules:
|
|
23
|
-
|
|
24
|
-
serviceBindings:{ SELF:'ingress', NETWORK:{ network:{ allow:['public'],tlsOptions:{ trustBrowserCas:true,trustedCertificates:[...rootCertificates] } } } } }],
|
|
22
|
+
{ name:'egress',modules:true,script:'export default {fetch(request,env){return env.NETWORK.fetch(request)}}',
|
|
23
|
+
serviceBindings:{ NETWORK:{ network:{ allow:['public'],tlsOptions:{ trustBrowserCas:true,trustedCertificates:[...rootCertificates] } } } } }],
|
|
25
24
|
}));
|
|
26
25
|
await mf.ready; const databases = {}, objects = {};
|
|
27
26
|
for (const [binding, plan] of Object.entries(manifest.migrations)) { const db = await mf.getD1Database(binding, 'app'); databases[binding] = db; await migrate(db, plan); await ensurePortabilitySchema(db, manifest.portabilityMigrations); }
|
|
@@ -1,156 +1,23 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
- `
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
the start answers within 12 seconds. A new copy that takes over two seconds
|
|
25
|
-
to answer is still a pending start, not a failure. Each status request is
|
|
26
|
-
held (at most 1.5 seconds) until the start changes, so the page learns of the
|
|
27
|
-
sign-in at once; a slow status read answers pending. Each admission logs
|
|
28
|
-
`demo_admitted` with the request's user agent and network owner, so a held
|
|
29
|
-
place can be joined to whoever started it. On Turnstile's testing secret (a
|
|
30
|
-
test host) a start must also carry the deploy's measurement token
|
|
31
|
-
(`measurement.mjs`: cookie `rer_demo_measure` or header
|
|
32
|
-
`x-rer-demo-measurement`), since automated browsers pass those keys; a
|
|
33
|
-
test host without a token admits nobody. A request
|
|
34
|
-
with that cookie goes to the visitor's Durable Object; nothing else reaches
|
|
35
|
-
a container.
|
|
36
|
-
- `container.mjs`: `DemoContainer`, one per visitor (`newUniqueId`, checked
|
|
37
|
-
with `idFromString`). It starts the container with public internet enabled,
|
|
38
|
-
`RER_DEMO=1`, the fixed origin (`RER_INGRESS_ORIGIN` and the seed's origin
|
|
39
|
-
variable), fresh `SESSION_SECRET` and `SETUP_TOKEN`,
|
|
40
|
-
`OWNER_EMAIL` (`DEMO_OWNER_EMAIL`, else `owner@demo.invalid`) and the owner claim's issuer, installation
|
|
41
|
-
id and public key; waits for `/health`; and posts a five-minute ES256 claim
|
|
42
|
-
(`claim.mjs`) to `/auth/owner-claim`. Its alarm ends the demo at its hour or
|
|
43
|
-
after 15 idle minutes: `destroy()`, `deleteAll()` and the place released. A
|
|
44
|
-
container that stops by itself ends its demo too.
|
|
45
|
-
Public internet reads and checks run against real services. The app's demo
|
|
46
|
-
guard simulates effects on people and third-party accounts, while the
|
|
47
|
-
container egress independently refuses direct mail/SMS provider transports
|
|
48
|
-
and SMTP ports. Public-address filtering and TLS remain enforced.
|
|
49
|
-
The platform inactivity timeout is set after start and reapplied when an
|
|
50
|
-
object recovers an already-running container. Persisted alarms enforce the
|
|
51
|
-
exact idle/absolute deadline without keeping a timer in flight. Requests
|
|
52
|
-
check both deadlines before refreshing activity. A container found stopped
|
|
53
|
-
ends the demo and answers the ended page (410).
|
|
54
|
-
- `certificate.mjs`: after `wrangler deploy`, the CLI waits for each demo
|
|
55
|
-
hostname's certificate, a TLS handshake that verifies for that name at the
|
|
56
|
-
address public DNS over HTTPS gives (10 minutes at most), and logs each
|
|
57
|
-
attempt. A failed or unverified handshake never counts as active.
|
|
58
|
-
- `capacity.mjs`: `DemoCapacity` counts unexpired reservation rows inside a
|
|
59
|
-
storage transaction, excluding diagnostics and expired rows. A lost start
|
|
60
|
-
handoff expires after one minute; a successful claim extends the reservation
|
|
61
|
-
to the demo's end. Refusals log the held ids as SHA-256 fingerprints (also
|
|
62
|
-
logged with each start's correlation), never visitor bearer cookies. The
|
|
63
|
-
refusal's response correlation also identifies that held-place snapshot.
|
|
64
|
-
- `image.mjs`, `prepare.mjs`, `template.mjs`, `entrypoint.sh`: the demo image
|
|
65
|
-
build migrates fresh workerd persistence with the packaged engine and closes
|
|
66
|
-
it into `/template`, with no network and no app requests. The entrypoint
|
|
67
|
-
copies that state into a fresh `/data` under the host's existing flock;
|
|
68
|
-
partial or foreign volumes are refused. Startup validates the existing
|
|
69
|
-
migration hashes instead of replaying SQL. Owners, sessions and the seed's
|
|
70
|
-
owner-dependent sample data are still created separately for each visitor.
|
|
71
|
-
- `pages.mjs`: the start and ended pages (12ui designs), strict CSP.
|
|
72
|
-
`start-script.mjs` is the start page's script: once Turnstile passes it
|
|
73
|
-
posts the form with `Accept: application/json` (the Worker then answers
|
|
74
|
-
202 `{ pending: true }`, `{ location }` with the cookies, or `{ notice }`
|
|
75
|
-
with the refusal's status)
|
|
76
|
-
and shows each step: the check, the private copy starting, the sign-in. A
|
|
77
|
-
refusal, a network failure or a start past 25 seconds is shown with Try
|
|
78
|
-
again, which first destroys the abandoned copy and releases its place through
|
|
79
|
-
`POST /__demo/cancel`, then runs a new check; progress is announced through a
|
|
80
|
-
`role="status"` region and failures through `role="alert"`. Without
|
|
81
|
-
JavaScript the form posts as before and gets the 303 or the page.
|
|
82
|
-
- `remove.mjs`: `rer deploy --demo --remove --yes`. The deploy passes the
|
|
83
|
-
demo's record (`plan.record`) to `wrangler deploy --message`; removal takes
|
|
84
|
-
the Worker for the demo's only while it serves that version, the Custom
|
|
85
|
-
Domain only while it routes to that Worker and the Container application
|
|
86
|
-
only while it belongs to that Worker's DemoContainer namespace, refuses
|
|
87
|
-
anything else before the first delete, deletes by exact id (the Worker
|
|
88
|
-
last) and reads back that each is gone (export/DEMO.md "Removing a hosted
|
|
89
|
-
demo"). `cli/demo-remove.test.mjs` runs it through the CLI against a fake
|
|
90
|
-
Cloudflare account (`tests/demo-account.mjs`).
|
|
91
|
-
|
|
92
|
-
`node --test modules/demo-host/tests/*.test.mjs` runs the Worker in workerd
|
|
93
|
-
with its real Durable Objects and rate limit; Turnstile and the container
|
|
94
|
-
platform are the only fakes. `start-page.test.mjs` drives the start page in
|
|
95
|
-
Chromium (default-seed's Playwright) through waiting, failure, Try again,
|
|
96
|
-
working and the signed-in landing. The end-to-end proof (`wrangler dev` with Docker)
|
|
97
|
-
is in DEMO.md.
|
|
98
|
-
|
|
99
|
-
Startup has a 45-second boot budget, with a separate five-second HTTP probe
|
|
100
|
-
limit; waiting for Cloudflare to place the container renews it, up to 90
|
|
101
|
-
seconds from the start (see placement below).
|
|
102
|
-
The Durable Object owns that work and cleanup while the page polls; a browser
|
|
103
|
-
that leaves cannot abandon it. Retrying a pending copy resumes the same
|
|
104
|
-
reservation; an explicit Try again cancels it before creating a replacement. Failed starts release their place before returning the retry state, retain the bounded operator diagnostic and destroy
|
|
105
|
-
the container. Destruction retries for failed starts never extend or reclaim the
|
|
106
|
-
released place. Each release logs the id's SHA-256 fingerprint with the start's
|
|
107
|
-
correlation. Every attempt logs its timestamp, elapsed time, duration and
|
|
108
|
-
HTTP status or fixed error category, including successful probes. Exit records
|
|
109
|
-
include the numeric platform exit code when supplied. A separate random
|
|
110
|
-
`x-rer-demo-correlation` response header joins these events without disclosing
|
|
111
|
-
the visitor's bearer cookie. Early refusals, including capacity, rate-limit,
|
|
112
|
-
Origin and human-check refusals, also carry a correlation and log their status.
|
|
113
|
-
Worker observability also enables the platform's
|
|
114
|
-
private Container stdout/stderr logs; historical stdout is not exposed by the
|
|
115
|
-
Container lifecycle API. Read those logs with an account principal granted
|
|
116
|
-
Workers Observability Write, and filter by the application and boot interval.
|
|
117
|
-
|
|
118
|
-
Optionally set a separate random `RER_DEMO_OPERATOR_TOKEN` (32–512 characters)
|
|
119
|
-
when running `rer deploy --demo`. It becomes a Worker secret. A host operator
|
|
120
|
-
can request `GET /__demo/operator/<correlation>` with
|
|
121
|
-
`Authorization: Bearer <RER_DEMO_OPERATOR_TOKEN>` to see the existing start page
|
|
122
|
-
with a bounded, redacted diagnostic line for a failed start. It retains the
|
|
123
|
-
last eight probe/exit records, for at most 15 minutes and 32 failed starts.
|
|
124
|
-
Unauthenticated requests, demo owner sessions and correlation IDs alone grant
|
|
125
|
-
no access. The token never belongs in a URL. Ordinary visitors keep the generic
|
|
126
|
-
failure page. No raw error text, health body, cookies or environment values are
|
|
127
|
-
included in this diagnostic.
|
|
128
|
-
|
|
129
|
-
Failed destruction retains its alarm for cleanup. A live visitor's demo also
|
|
130
|
-
retains its capacity reservation until destruction is confirmed (or the
|
|
131
|
-
container is already stopped); a terminal start failure releases it immediately.
|
|
132
|
-
A cleanup failure is logged separately and cannot replace the startup failure
|
|
133
|
-
shown to the visitor.
|
|
134
|
-
|
|
135
|
-
Cloudflare keeps a small prepared pool per application (7 instances on fresh
|
|
136
|
-
test hosts, measured in October 2026, whatever `max_instances` is) and places a
|
|
137
|
-
new instance only on demand: a start beyond the pool gets "no container instance
|
|
138
|
-
that can be provided" until Cloudflare has placed one (16 to 37 seconds
|
|
139
|
-
measured, after which boot was normal). `placement.mjs` keeps the same
|
|
140
|
-
reservation and launch options, requests placement again only for that specific
|
|
141
|
-
provider response, and renews the 45-second boot budget after each wait, up to
|
|
142
|
-
90 seconds from the start, extending the reservation to match. Boot, health
|
|
143
|
-
and claim errors are not retried. Placement exhaustion answers with the normal
|
|
144
|
-
failed-start retry state and releases the reservation; the capacity notice is
|
|
145
|
-
reserved for actual reservation exhaustion. Cleanup keeps only that terminal
|
|
146
|
-
answer and correlation for one minute, so a status poll after object recovery
|
|
147
|
-
still receives the right answer; its alarm then deletes the remaining metadata.
|
|
148
|
-
|
|
149
|
-
`DemoCapacity` alone admits visitors (`maxInstances` live demos). The Container
|
|
150
|
-
application's own `max_instances` is twice that, so instances Cloudflare still
|
|
151
|
-
counts after a demo ends or a start fails never refuse a place admission gave.
|
|
152
|
-
Its `rollout_active_grace_period` is an hour, a demo's longest life: a redeploy
|
|
153
|
-
that rolls the application out (a new image, instance type or limit) replaces
|
|
154
|
-
idle prepared instances at once and leaves every live demo running until it
|
|
155
|
-
ends. With no grace period a rollout replaced live demos' containers within two
|
|
156
|
-
minutes, and their visitors saw "This demo has ended".
|
|
1
|
+
# Native hosted demos
|
|
2
|
+
|
|
3
|
+
`rer deploy --demo` builds the seed's production Worker for a Worker Loader
|
|
4
|
+
host. See [adoption and deployment](../../export/DEMO.md#hosting-the-demo).
|
|
5
|
+
|
|
6
|
+
- `build.mjs`: ordinary Vite output, declared SQL and public configuration,
|
|
7
|
+
using the shared container migration-inventory and image-variable checks.
|
|
8
|
+
- `native/`: bundle preparation, per-visitor D1/seed-DO facets, prefixed R2,
|
|
9
|
+
durable queue delivery, seed-DO alarms and declared UTC crons.
|
|
10
|
+
A narrow bridge exposes only visitor resource operations to the seed; own-host
|
|
11
|
+
outbound requests return to the same visitor, including declared secondary hosts.
|
|
12
|
+
- `worker.mjs`, `pages.mjs`, `start-script.mjs`: Turnstile start, progress,
|
|
13
|
+
cancellation and visitor routing. Static assets are served at the edge.
|
|
14
|
+
- `capacity.mjs`: concurrent visitor admission and operator diagnostics. This
|
|
15
|
+
is used by the native host as well as retained during legacy upgrades.
|
|
16
|
+
- `upgrade.mjs`: verifies ownership, removes the old Container application and
|
|
17
|
+
derives the class migration from deployed namespaces and migration tag.
|
|
18
|
+
- `remove.mjs`: exact-id teardown, ownership collision checks and absence reads.
|
|
19
|
+
|
|
20
|
+
The historical resource-name hash remains stable so released container demos
|
|
21
|
+
can upgrade and be removed from a fresh checkout. Portable container hosting
|
|
22
|
+
remains separate from the native hosted-demo runtime.
|
|
23
|
+
`rer demo` remains the local Vite/Workers demo, with its own temporary state.
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { readFile, writeFile, realpath, cp, mkdir, rm } from 'node:fs/promises';
|
|
2
|
+
import { dirname, join, resolve } from 'node:path';
|
|
3
|
+
import { createRequire } from 'node:module';
|
|
4
|
+
import { pathToFileURL } from 'node:url';
|
|
5
|
+
import { containerMigrationInventory as demoMigrationInventory } from '../container/migration-inventory.mjs';
|
|
6
|
+
import { checkImageVariables as checkBuildVariables } from '../container/image-variables.mjs';
|
|
7
|
+
import { nativeBindings } from './native/config.mjs';
|
|
8
|
+
import { readMigrationRewrites, withRewrites } from '../deploy-kit/migrations.mjs';
|
|
9
|
+
|
|
10
|
+
// The seed's ordinary production Worker output, with the declared SQL inventory.
|
|
11
|
+
// No image, runtime emulator or dependency installation belongs in this bundle.
|
|
12
|
+
export async function buildDemo(root, output, declaration) {
|
|
13
|
+
const require = createRequire(join(root, 'package.json'));
|
|
14
|
+
const vite = await import(pathToFileURL(require.resolve('vite')));
|
|
15
|
+
const loaded = await vite.loadConfigFromFile({ command: 'build', mode: 'production' }, undefined, root);
|
|
16
|
+
if (!loaded) throw new Error('Vite configuration missing');
|
|
17
|
+
const emitted = new Set(), entryExports = new Map();
|
|
18
|
+
const config = { ...loaded.config, root, configFile: false, plugins: [...(loaded.config.plugins ?? []), {
|
|
19
|
+
name: 'rer-demo-output',
|
|
20
|
+
async writeBundle(output, bundle) {
|
|
21
|
+
const directory = resolve(root, output.dir ?? dirname(output.file));
|
|
22
|
+
for (const chunk of Object.values(bundle)) if (chunk.type === 'chunk') {
|
|
23
|
+
const path = await realpath(join(directory, chunk.fileName)); emitted.add(path);
|
|
24
|
+
if (chunk.isEntry) entryExports.set(path, new Set(chunk.exports));
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
}] };
|
|
28
|
+
await (await vite.createBuilder(config)).buildApp();
|
|
29
|
+
await (await import('../../cli/dev-identity.mjs')).refuseDevLayerOutput(root);
|
|
30
|
+
const workerConfig = JSON.parse(await readFile(join(root, 'dist/server/wrangler.json'), 'utf8'));
|
|
31
|
+
const entry = await realpath(resolve(root, 'dist/server', workerConfig.main));
|
|
32
|
+
const bindings = nativeBindings(workerConfig);
|
|
33
|
+
for (const binding of bindings.objects) if (!entryExports.get(entry)?.has(binding.class_name)) throw new Error(`Loader demo cannot bind ${binding.name}: missing Durable Object export ${binding.class_name}`);
|
|
34
|
+
if (!emitted.has(entry)) throw new Error('Demo Worker entry was not emitted by this build');
|
|
35
|
+
await checkBuildVariables(root, workerConfig.vars ?? {}, declaration.secrets ?? []);
|
|
36
|
+
const { unstable_splitSqlQuery: split } = require('wrangler');
|
|
37
|
+
const migrations = Object.fromEntries((workerConfig.d1_databases ?? []).map(db => [db.binding, []]));
|
|
38
|
+
const inventory = withRewrites(await demoMigrationInventory(root, { config: workerConfig, configFile: 'dist/server/wrangler.json' }), await readMigrationRewrites(root));
|
|
39
|
+
for (const { binding, ...migration } of inventory) migrations[binding].push({ ...migration, statements: split(migration.sql) });
|
|
40
|
+
await rm(output, { recursive: true, force: true });
|
|
41
|
+
await mkdir(join(output, 'app'), { recursive: true });
|
|
42
|
+
await cp(join(root, 'dist'), join(output, 'app/dist'), { recursive: true });
|
|
43
|
+
await writeFile(join(output, 'app/demo.json'), JSON.stringify({ config: workerConfig, migrations }) + '\n');
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (process.argv[1] === import.meta.filename) {
|
|
47
|
+
try { await buildDemo(resolve(process.argv[2]), resolve(process.argv[3]), JSON.parse(process.argv[4])); }
|
|
48
|
+
catch (error) { console.error(error.message); process.exitCode = 1; }
|
|
49
|
+
}
|