@runeditrun/rer 0.2.21 → 0.2.22

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. package/cli/README.md +7 -0
  2. package/cli/demo-host.mjs +5 -5
  3. package/cli/deploy-command.mjs +2 -2
  4. package/cli/deploy.mjs +3 -1
  5. package/cli/export.mjs +1 -1
  6. package/cli/managed-server.mjs +5 -2
  7. package/cli/production-auth.mjs +19 -5
  8. package/modules/container/README.md +10 -0
  9. package/modules/container/build.mjs +7 -11
  10. package/modules/container/host.mjs +3 -1
  11. package/modules/container/ingress.mjs +15 -4
  12. package/modules/container/migration-inventory.mjs +33 -0
  13. package/modules/container/migrations.mjs +16 -2
  14. package/modules/delivery-producer/migrations.mjs +61 -0
  15. package/modules/demo/effects.mjs +4 -0
  16. package/modules/demo-host/README.md +27 -17
  17. package/modules/demo-host/capacity.mjs +1 -1
  18. package/modules/demo-host/container.mjs +65 -37
  19. package/modules/demo-host/pages.mjs +1 -1
  20. package/modules/demo-host/plan.mjs +19 -6
  21. package/modules/demo-host/remove.mjs +9 -7
  22. package/modules/demo-host/start-script.mjs +10 -1
  23. package/modules/demo-host/startup.mjs +4 -2
  24. package/modules/demo-host/worker.mjs +19 -2
  25. package/modules/dependencies/approval.mjs +3 -2
  26. package/modules/dependencies/index.mjs +2 -2
  27. package/modules/deploy-kit/README.md +4 -2
  28. package/modules/deploy-kit/diagnostics.mjs +9 -0
  29. package/modules/deploy-kit/provider-transport.mjs +56 -0
  30. package/modules/deploy-kit/provider.mjs +22 -20
  31. package/modules/operator-auth/README.md +4 -0
  32. package/modules/operator-auth/accounts/credentials.mjs +2 -2
  33. package/modules/operator-auth/accounts/email-change.mjs +2 -2
  34. package/modules/operator-auth/accounts/provider-control.mjs +3 -3
  35. package/modules/operator-auth/accounts/store.mjs +9 -8
  36. package/modules/operator-auth/dev/cli-session.mjs +5 -4
  37. package/modules/operator-auth/dev/index.mjs +5 -5
  38. package/modules/operator-auth/dev-gate.mjs +4 -2
  39. package/modules/operator-auth/identity/email-link/index.mjs +2 -1
  40. package/modules/operator-auth/signin/index.mjs +10 -2
  41. package/onboarding/adapter.mjs +11 -3
  42. package/onboarding/operator-installer.mjs +2 -2
  43. package/onboarding/operator-migrations.mjs +48 -0
  44. package/onboarding/process-table.mjs +45 -19
  45. package/package.json +1 -1
package/cli/README.md CHANGED
@@ -47,6 +47,13 @@ owner/sign-in hooks. The reference hook declaration is
47
47
  internal build/dev/verify targets and deployment policy. The CLI owns flags,
48
48
  pins, containment, cancellation, capture, output and sequencing.
49
49
 
50
+ `rer dev` runs the local migration hook before starting the app and checking its
51
+ owner prerequisites. The shared operator hook reads every SQL file in the seed's
52
+ canonical `migrations/core` inventory, including changes pulled since the last
53
+ build, and prints the applied names. A failed migration stops startup with its
54
+ captured error and `rer setup` guidance. `rer demo` uses the same inventory in
55
+ its isolated state directory.
56
+
50
57
  Successful terminal setup saves declared nonsecret installer answers in owned
51
58
  0600 storage under `.seed/installer`. A non-interactive repeat reuses those
52
59
  answers, with explicit flags or an answers file taking precedence. Runtime
package/cli/demo-host.mjs CHANGED
@@ -19,7 +19,7 @@ export async function demoHostCommand(project, options, store, signal, out, oper
19
19
  const app = await read(project.adapter.deploy?.declaration ?? 'deploy.json'), declaration = await read('deploy-demo.json');
20
20
  if (!(app.platforms ?? ['workers']).includes('container')) throw refuse('A hosted demo runs the seed\'s container image: declare "container" in deploy.json platforms first.');
21
21
  let plan;
22
- try { plan = demoPlan({ project: app.project, environment: options.environment ?? 'production', compatibilityDate: app.compatibilityDate, originVar: app.originVar, declaration, turnstileSiteKey: process.env.TURNSTILE_SITE_KEY?.trim() }); }
22
+ 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() }); }
23
23
  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", "instanceType" (${INSTANCE_TYPES.join(', ')}; default basic) and plain "settings".`); }
24
24
  if (options.remove) return removeDemo(plan, store, signal);
25
25
  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,8 +31,8 @@ export async function demoHostCommand(project, options, store, signal, out, oper
31
31
  await prepareDemoImage(join(folder, 'image'));
32
32
  for (const file of DEMO_FILES) await cp(new URL(`../modules/demo-host/${file}`, import.meta.url), join(folder, file));
33
33
  await writeFile(join(folder, 'wrangler.json'), JSON.stringify(plan.wrangler, null, 2) + '\n');
34
- const summary = { worker: plan.worker, origin: plan.origin, hostname: plan.hostname, folder: '.rer/demo-host', maxInstances: plan.maxInstances, settings: plan.settings, instanceType: plan.instanceType, startsPerIpPerMinute: 3, minutes: 60, idleMinutes: 15, enableInternet: true };
35
- const human = [`Demo plan: Worker ${summary.worker} on ${summary.hostname} (custom domain; workers.dev off), written to ${summary.folder}`,
34
+ 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 };
35
+ const human = [`Demo plan: Worker ${summary.worker} on ${summary.hostnames.join(', ')} (custom domains; workers.dev off), written to ${summary.folder}`,
36
36
  ` 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`,
37
37
  ` each demo ends after 60 minutes or 15 idle minutes; its container is destroyed and its state deleted`];
38
38
  if (options.dryRun) {
@@ -61,7 +61,7 @@ async function removeDemo(plan, store, signal) {
61
61
  const { providerRefusal } = await import('../modules/deploy-kit/diagnostics.mjs');
62
62
  const { demoRemove } = await import('../modules/demo-host/remove.mjs');
63
63
  const { demoRemovalFetch } = await import('./demo-removal-provider.mjs');
64
- await store.step(`Removing the demo Worker ${plan.worker} on ${plan.hostname}`);
64
+ await store.step(`Removing the demo Worker ${plan.worker} on ${plan.hostnames.join(', ')}`);
65
65
  let result;
66
66
  try { result = await demoRemove({ api: cloudflare({ accountId: process.env.CLOUDFLARE_ACCOUNT_ID, token: process.env.CLOUDFLARE_API_TOKEN, fetch: demoRemovalFetch(signal) }), plan }); }
67
67
  catch (error) {
@@ -71,5 +71,5 @@ async function removeDemo(plan, store, signal) {
71
71
  throw new RerError('deploy_refused', `Demo remove stopped: ${code}${detail ? ` ${detail}` : ''}. Nothing that is not the demo's was touched; run it again once the cause is fixed.`, 1);
72
72
  }
73
73
  store.report(result.alreadyRemoved ? `deploy --demo --remove: no demo Worker ${plan.worker} exists; nothing to remove.` : `Removed ${result.removed.join(', ')} and read back that each is gone.`);
74
- return { removed: true, demo: { worker: plan.worker, hostname: plan.hostname }, deleted: result.removed, alreadyRemoved: Boolean(result.alreadyRemoved) };
74
+ return { removed: true, demo: { worker: plan.worker, hostname: plan.hostname, hostnames: plan.hostnames }, deleted: result.removed, alreadyRemoved: Boolean(result.alreadyRemoved) };
75
75
  }
@@ -15,7 +15,7 @@ import { rememberHookState, hookState } from './deploy-hook-record.mjs';
15
15
  import { main as owner } from '../modules/deploy-kit/owner-cli.mjs';
16
16
  import { localOnlyBinding, ownerWorkerName } from '../modules/deploy-kit/owner.mjs';
17
17
  import { refuseLocalModels } from '../modules/deploy-kit/local-only.mjs';
18
- import { providerRefusal } from '../modules/deploy-kit/diagnostics.mjs';
18
+ import { providerRefusal, providerTransport } from '../modules/deploy-kit/diagnostics.mjs';
19
19
  import { shared } from './shared.mjs';
20
20
 
21
21
  export function validateDeployOptions(options) {
@@ -136,7 +136,7 @@ export async function deployCommand(context) {
136
136
  if (setup) throw commandFailure(redact(setup));
137
137
  const readiness = mutationReadinessFailure(error, { again, redact });
138
138
  if (readiness) throw commandFailure(readiness);
139
- const refused = providerRefusal(error);
139
+ const refused = providerRefusal(error) ?? providerTransport(error);
140
140
  throw commandFailure(redact(`${state} Failed at ${step}: ${error.message}${refused ? ` (${refused})` : ''}${error.cause ? ` (${error.cause})` : ''}. Fix the cause and run ${again} again using the same environment credentials. ${outcome && recorded ? `To retire the token without rebuilding: ${again} --unset-secret ${setupManifest?.steps?.find(item => item.type === 'owner-claim')?.tokenField ?? 'SETUP_TOKEN'}.` : ''}`));
141
141
  }
142
142
  }
package/cli/deploy.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  import { requireProductionAuth } from './production-auth.mjs';
2
2
  import { readFile } from 'node:fs/promises';
3
- import { deployDiagnostic } from '../modules/deploy-kit/diagnostics.mjs';
3
+ import { deployDiagnostic, providerTransport } from '../modules/deploy-kit/diagnostics.mjs';
4
4
  import { shared } from './shared.mjs';
5
5
  import { contained } from './project.mjs';
6
6
  import { missing, invalid, refuse, RerError } from './errors.mjs';
@@ -124,6 +124,8 @@ function failureText({ error, step, created, again, retry, secretChange, deploye
124
124
  const resource = diagnostic?.resource ?? (error.resource ? redact(String(error.resource)) : undefined);
125
125
  if (code === 'resource-not-listed') return `Deploy stopped at ${at}: ${resource} was created, but Cloudflare's account listing still does not show it after two minutes. Nothing was uploaded.\nResources created this attempt: ${created.join(', ') || 'none'}.\nRun ${again} again once the listing catches up: it reuses ${resource}. To delete what this environment created instead: ${again} --remove --yes.`;
126
126
  const lines = [`Deploy failed at ${at}: ${code}${resource ? ` (${resource})` : ''}${diagnostic?.providerCodes?.length ? ` (provider ${diagnostic.providerCodes.join(', ')})` : ''}.`];
127
+ const transport = providerTransport(error, redact);
128
+ if (transport) lines.push(`${transport}.`);
127
129
  if (diagnostic?.output && diagnostic.output !== '(no provider output)') lines.push(`First redacted output lines:\n${diagnostic.output}`);
128
130
  lines.push(`Resources created this attempt: ${created.join(', ') || 'none'}.`);
129
131
  lines.push(recovery({ code, at, resource, again, retry, secretChange }));
package/cli/export.mjs CHANGED
@@ -6,7 +6,7 @@ import { cliVersion } from './version.mjs';
6
6
 
7
7
  // Copy whole maintained source/tooling directories. Python publication scans
8
8
  // load sibling modules and data dynamically; they belong in the inventory too.
9
- export const sourceRoots = ['bin/rer.mjs', 'cli', 'modules/delivery-producer/declarations.mjs', 'modules/delivery-producer/bindings.mjs', 'modules/job-queue', 'modules/release', 'modules/deploy-kit', 'modules/container', 'modules/demo-host', 'modules/demo', 'modules/integrations/core', 'modules/mail', 'modules/ai', 'modules/portability', 'modules/d1-transport', 'modules/erasure', 'modules/dependencies', 'modules/operator-auth', 'modules/owner-claim', 'operations', 'scripts/publish-check', 'scripts/publish-check.mjs', 'composition/git-source.mjs', 'edition-authoring/manifest.mjs', 'edition-authoring/files.mjs'];
9
+ export const sourceRoots = ['bin/rer.mjs', 'cli', 'modules/delivery-producer/declarations.mjs', 'modules/delivery-producer/bindings.mjs', 'modules/delivery-producer/migrations.mjs', 'modules/job-queue', 'modules/release', 'modules/deploy-kit', 'modules/container', 'modules/demo-host', 'modules/demo', 'modules/integrations/core', 'modules/mail', 'modules/ai', 'modules/portability', 'modules/d1-transport', 'modules/erasure', 'modules/dependencies', 'modules/operator-auth', 'modules/owner-claim', 'operations', 'scripts/publish-check', 'scripts/publish-check.mjs', 'composition/git-source.mjs', 'edition-authoring/manifest.mjs', 'edition-authoring/files.mjs'];
10
10
  export async function inventory(base, roots = sourceRoots) {
11
11
  const files = [];
12
12
  async function walk(path) {
@@ -98,7 +98,10 @@ export async function managedServer(project,options,out,{clock={}}={}){
98
98
  if(devIdentity&&!devKey)devKey=await requireDevKey(project.root);
99
99
  if(devIdentity&&options.verb==='dev')refuseManagedInstallation(selected.vars,process.env);
100
100
  if(options.demo)demo=await demoPreflight(project,selected.vars,options.idleTimeout);
101
- else if(project.adapter.localPrerequisites&&!await project.adapter.localPrerequisites({root:project.root,vars:selected.vars,email:selected.vars[keys.email],demo:false}))throw missing('Local migrations, owner or local authentication/mail settings are missing. Run rer setup first.');
101
+ const prerequisites=async()=>{
102
+ if(!demo&&project.adapter.localPrerequisites&&!await project.adapter.localPrerequisites({root:project.root,vars:selected.vars,email:selected.vars[keys.email],demo:false}))throw missing('Local migrations, owner or local authentication/mail settings are missing. Run rer setup first.');
103
+ };
104
+ if(options.verb!=='dev')await prerequisites();
102
105
  await registry.update({url:selected.url,host:selected.host,port:selected.port,models:options.models??'fake'});
103
106
  const {privateDirectory}=await shared('private-files.mjs');
104
107
  if(!demo)await privateDirectory(project.root,['.wrangler','state']);
@@ -114,7 +117,7 @@ export async function managedServer(project,options,out,{clock={}}={}){
114
117
  proxy=await runtimeFrontDoor({host:selected.host,port:selected.port,activity,door:selected.door,devKey:devKey??undefined,origin:selected.url});const internal=proxy.runtimePort;
115
118
  if(selected.door)await announceDoorRoute(selected,proxy,registry);
116
119
  const modelsLive = options.verb==='dev' ? await prepareLiveModels(project.root,options,store,controller.signal) : undefined;
117
- const adapterOptions={modelsLive,demo,selectedOrigin:selected.url,operationToken:operation.token,privateSettings:project.adapter.settings,runtimePort:internal,devKey:devKey??undefined,devLogin:false,scheduled:crons.length>0};
120
+ const adapterOptions={modelsLive,demo,selectedOrigin:selected.url,operationToken:operation.token,privateSettings:project.adapter.settings,runtimePort:internal,devKey:devKey??undefined,devLogin:false,scheduled:crons.length>0,beforeStart:options.verb==='dev'?prerequisites:undefined};
118
121
  const input={[keys.email]:selected.vars[keys.email]};
119
122
  if(options.verb==='preview')await productionBuild(project,()=>store.run('Building fixed production output',process.execPath,[fileURLToPath(new URL('../bin/rer.mjs',import.meta.url)),'build','--cwd',project.root,'--json'],{cwd:project.root,phase:'build',signal:controller.signal,env:{...process.env,RER_LOCAL_OPERATION:operation.token},afterCommand:()=>recoverInterrupted(project.root,operation)}),text=>store.report(text));
120
123
  await serverResources(project.root);
@@ -14,16 +14,23 @@ export async function requireProductionAuth({ root, declaration, env, setupManif
14
14
  if (!setupManifest?.steps?.some(step => step.type === 'owner-claim') && !dependencies.length && !('MAIL_PROVIDER' in bindings) && !declaration.secrets?.some(item => item.binding === 'MAIL_API_KEY')) return;
15
15
  const provider = bindings.MAIL_PROVIDER;
16
16
  let mailSelected = ['resend', 'sendgrid', 'cloudflare', 'rer'].includes(provider) && mailbox(bindings.MAIL_FROM);
17
+ let relaySelected = true;
17
18
  if (provider === 'rer') {
18
- try { validateRerConfig({ relayUrl: bindings.MAIL_RELAY_URL }); } catch { mailSelected = false; }
19
+ try { validateRerConfig({ relayUrl: bindings.MAIL_RELAY_URL }); } catch { relaySelected = false; mailSelected = false; }
19
20
  }
20
- const required = ['MAIL_API_KEY', ...(provider === 'cloudflare' ? ['MAIL_ACCOUNT_ID'] : [])];
21
- if (mailSelected && required.every(key => bindings[key]?.trim())) return;
21
+ // Secrets arrive under the seed's declared runtime binding, which need not
22
+ // be the shared facade's MAIL_API_KEY (for example native SendGrid auth).
23
+ const credentialKeys = ['MAIL_API_KEY', ...(provider === 'sendgrid' ? ['SENDGRID_API_KEY'] : [])];
24
+ const declaredCredentials = credentialKeys.filter(key => declaration.secrets?.some(item => item.binding === key));
25
+ const required = provider === 'cloudflare' ? ['MAIL_ACCOUNT_ID'] : [];
26
+ const hasMailSecrets = live => credentialKeys.some(key => bindings[key]?.trim() || live.includes(key))
27
+ && required.every(key => bindings[key]?.trim() || live.includes(key));
28
+ if (mailSelected && hasMailSecrets([])) return;
22
29
  if (mailSelected && !options.dryRun && declarationPath) {
23
30
  try {
24
31
  const state = await hookState(root, options.environment ?? 'production');
25
32
  const existing = await owner(['inspect', '--declaration', declarationPath, '--environment', options.environment ?? 'production', ...(state ? ['--state', state] : [])], env, { cwd: root, localOnlyKeys: adapter?.localOnlyKeys });
26
- if (required.every(key => bindings[key]?.trim() || existing.liveSecrets.includes(key))) return;
33
+ if (hasMailSecrets(existing.liveSecrets)) return;
27
34
  } catch (error) {
28
35
  if (!['missing-owner-receipt', 'missing-input'].includes(error.message)) throw refuse(`Production sign-in could not verify the existing deployment and its mail secrets${/^[a-z][a-z0-9-]{0,80}$/.test(error.message)?` (${error.message})`:''}. Restore provider access and the owned deployment record before retrying.`);
29
36
  }
@@ -34,5 +41,12 @@ export async function requireProductionAuth({ root, declaration, env, setupManif
34
41
  if (dependencies.some(item => {
35
42
  try { const value = answers?.[item.address] ?? bindings[item.address], issuer = new URL(value); return issuer.protocol === 'https:' && issuer.origin === value && !issuer.username && !issuer.password; } catch { return false; }
36
43
  })) return;
37
- throw refuse('Production sign-in is not configured. Configure outbound email (MAIL_PROVIDER, MAIL_FROM and MAIL_API_KEY; Cloudflare also needs MAIL_ACCOUNT_ID, RER needs MAIL_RELAY_URL), or select and approve an Auth SSO install through the declared oidc-provider@1 setup dependency. Local dev and demo use automatic RER_OPEN sign-in. Nothing was built or uploaded.');
44
+ const missing = [
45
+ ...(!['resend', 'sendgrid', 'cloudflare', 'rer'].includes(provider) ? ['MAIL_PROVIDER (resend, sendgrid, cloudflare or rer)'] : []),
46
+ ...(!mailbox(bindings.MAIL_FROM) ? ['MAIL_FROM'] : []),
47
+ ...(!credentialKeys.some(key => bindings[key]?.trim()) ? [declaredCredentials.join(' or ') || credentialKeys.join(' or ')] : []),
48
+ ...required.filter(key => !bindings[key]?.trim()),
49
+ ...(!relaySelected ? ['MAIL_RELAY_URL (assigned HTTPS relay URL)'] : []),
50
+ ];
51
+ throw refuse(`Production sign-in is not configured. Configure outbound email; missing or invalid: ${missing.join(', ')}. Or select and approve an Auth SSO install through the declared oidc-provider@1 setup dependency. Local dev and demo use automatic RER_OPEN sign-in. Nothing was built or uploaded.`);
38
52
  }
@@ -49,6 +49,9 @@ PID/heartbeat diagnostics and is never unlinked. The host applies hashed
49
49
  migrations using a separately committed claim, followed by atomic SQL plus
50
50
  completion. Changed or uncertain history stops startup. Cron uses Base's five
51
51
  numeric-field grammar and dispatches matching handlers at minute boundaries.
52
+ Image packaging uses the deployment producer's complete migration inventory,
53
+ including packaging policy prefixes and `extraMigrationDirectories`, in declared
54
+ directory order. Conflicting packaging policies stop the build.
52
55
  Node queues retain pending/claimed/dead messages across restarts; expired claims
53
56
  are delivered again. Consumers must already be idempotent. Dead jobs make health
54
57
  unhealthy. `/health` reports the container platform and the image ID supplied by
@@ -71,6 +74,13 @@ headers from other peers are discarded. `RER_SSH_IDENTITY` and
71
74
  are logged. Local loopback origins support a disposable SSH Docker host for
72
75
  qualification. Owner hosting/account setup remains an owner step.
73
76
 
77
+ An isolated front can set `RER_INGRESS_ORIGIN` to its primary public origin and
78
+ `RER_INGRESS_ORIGINS` to a JSON array of allowed HTTPS origins. It must overwrite
79
+ `x-rer-ingress-origin` on each forwarded request with its validated public
80
+ origin. The container accepts that header only with a fixed primary origin and
81
+ an exact allowlist match, then strips it before the app. Requests without the
82
+ header, including health probes and owner claims, use the primary origin.
83
+
74
84
  A seed can package its own scoped Node runner with a declaration such as:
75
85
 
76
86
  ```json
@@ -1,14 +1,13 @@
1
- import { readFile, writeFile, readdir, realpath, cp, mkdir, rm } from 'node:fs/promises';
1
+ import { readFile, writeFile, realpath, cp, mkdir, rm } from 'node:fs/promises';
2
2
  import { dirname, join, resolve, sep } from 'node:path';
3
3
  import { createRequire } from 'node:module';
4
4
  import { pathToFileURL } from 'node:url';
5
- import { createHash } from 'node:crypto';
6
5
  import { parseJsonc } from '../../cli/jsonc.mjs';
7
6
  import { portabilityPlan } from './portability-schema.mjs';
8
7
  import { checkBoundary } from './boundary.mjs';
9
8
  import { localDurableObjects } from './durable-objects.mjs';
10
9
  import { readMigrationRewrites, withRewrites } from '../deploy-kit/migrations.mjs';
11
- const hash = source => createHash('sha256').update(source).digest('hex');
10
+ import { containerMigrationInventory } from './migration-inventory.mjs';
12
11
  export async function buildContainer(root, options={}) {
13
12
  const previous=process.cwd();process.chdir(root);
14
13
  try{return await buildImage(root,options);}finally{process.chdir(previous);}
@@ -67,14 +66,11 @@ async function buildImage(root, { output = join(root,'.rer/container/image'), en
67
66
  if (!freshServerFiles.has(serverEntry)) throw new Error('Container server entry was not emitted by this build');
68
67
  await checkBoundary(root,{engine,cacheContract,entries:[serverEntry],emittedFiles:freshServerFiles,outputRoot:serverOutput});
69
68
  const { unstable_splitSqlQuery:split } = require('wrangler');
70
- const migrations = {}, rewrites = await readMigrationRewrites(root);
71
- for (const db of workerConfig.d1_databases ?? []) {
72
- let directory = resolve(root,db.migrations_dir ?? 'migrations');
73
- try { await readdir(directory); } catch { directory = resolve(root,'dist/server',db.migrations_dir); }
74
- migrations[db.binding] = [];
75
- for (const name of (await readdir(directory)).filter(name => name.endsWith('.sql')).sort()) { const sql = await readFile(join(directory,name),'utf8'); migrations[db.binding].push({ name,sql,sha256:hash(sql),statements:split(sql) }); }
76
- migrations[db.binding] = withRewrites(migrations[db.binding], rewrites, { partial:true });
77
- }
69
+ const configFile = engine === 'workerd' ? 'dist/server/wrangler.json' : 'wrangler.jsonc';
70
+ const rewrites = await readMigrationRewrites(root);
71
+ const inventory = withRewrites(await containerMigrationInventory(root,{config:workerConfig,configFile}),rewrites);
72
+ const migrations = Object.fromEntries((workerConfig.d1_databases ?? []).map(db => [db.binding,[]]));
73
+ for (const {binding,...migration} of inventory) migrations[binding].push({...migration,statements:split(migration.sql)});
78
74
  const entry = engine === 'workerd' ? 'dist/server/'+workerConfig.main : 'dist/server/index.mjs';
79
75
  await readFile(join(root,entry));
80
76
  if (engine === 'node' && workerConfig.kv_namespaces?.length) throw new Error('Node container does not support KV');
@@ -11,6 +11,8 @@ export async function startHost({ root = '/app', data = '/data', settings = proc
11
11
  const release = await recordVolumeOwner(data);
12
12
  let engine, server, timer, idleTimer, runner, closing, lastMinute = -1, lastRequest=Date.now();
13
13
  try {
14
+ const origins = settings.RER_INGRESS_ORIGINS === undefined ? undefined : JSON.parse(settings.RER_INGRESS_ORIGINS);
15
+ 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');
14
16
  const create = manifest.engine === 'node' ? (await import('./node-engine.mjs')).nodeEngine : (await import('./worker-engine.mjs')).workerEngine;
15
17
  engine = await create({ root,data,manifest,settings,edge });
16
18
  const schedules = (manifest.config.triggers?.crons ?? []).map(cron => [cron,parseCron(cron)]);
@@ -18,7 +20,7 @@ export async function startHost({ root = '/app', data = '/data', settings = proc
18
20
  server = createServer(async (req, res) => {
19
21
  try {
20
22
  lastRequest=Date.now();
21
- const request = await incoming(req, { trustedProxy:settings.TRUSTED_PROXY, origin:settings.RER_INGRESS_ORIGIN, maxRequestBytes:manifest.maxRequestBytes });
23
+ const request = await incoming(req, { trustedProxy:settings.TRUSTED_PROXY, origin:settings.RER_INGRESS_ORIGIN, origins, maxRequestBytes:manifest.maxRequestBytes });
22
24
  let response = await engine.fetch(request);
23
25
  if (new URL(request.url).pathname === '/health' && response.ok) {
24
26
  const body = await response.json(); const deadJobs = engine.dead();
@@ -2,15 +2,26 @@ import { Readable } from 'node:stream';
2
2
  import { lookup } from 'node:dns/promises';
3
3
  import { isIP } from 'node:net';
4
4
  // `origin` (RER_INGRESS_ORIGIN) fixes the public origin for a container whose only
5
- // peer is its own front, such as a hosted demo's Durable Object.
6
- export async function incoming(req, { trustedProxy, origin, maxRequestBytes = 25 * 1024 * 1024 } = {}) {
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 } = {}) {
7
8
  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);
8
9
  const peer = req.socket.remoteAddress?.replace('::ffff:', '') ?? '';
9
10
  const trusted = trustedProxy && (isIP(trustedProxy) ? peer===trustedProxy : (await lookup(trustedProxy,{all:true})).some(item=>item.address.replace('::ffff:','')===peer));
10
11
  let host = req.headers.host, proto = 'http', address = peer;
11
12
  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; }
12
- if (origin) { const fixed = new URL(origin); host = fixed.host; proto = fixed.protocol.slice(0, -1); }
13
- for (const name of ['cf-connecting-ip','x-forwarded-host','x-forwarded-proto','x-forwarded-for','forwarded','x-rer-original-url']) headers.delete(name);
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
+ 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);
14
25
  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');
15
26
  headers.set('cf-connecting-ip', address); headers.set('host', host);
16
27
  const url = `${proto}://${host}${req.url}`;
@@ -0,0 +1,33 @@
1
+ import {readFile} from 'node:fs/promises';
2
+ import {dirname, resolve} from 'node:path';
3
+ import {isDeepStrictEqual} from 'node:util';
4
+ import {collectMigrations, sourcePath} from '../delivery-producer/migrations.mjs';
5
+
6
+ // These are the producer's source policy and exported policy locations. Never
7
+ // infer extra directories from a seed name or scan undeclared SQL trees.
8
+ const packagingFiles = ['packaging.json','rer-packaging.json','scripts/delivery/base/packaging.json'];
9
+ async function migrationPolicy(root) {
10
+ const policies = [];
11
+ for (const candidate of packagingFiles) {
12
+ const file = await sourcePath(root,candidate,{optional:true});
13
+ if (!file) continue;
14
+ const packaging = JSON.parse(await readFile(file,'utf8'));
15
+ const inputs = packaging.artifact?.specInputs;
16
+ if (!inputs || typeof inputs !== 'object' || Array.isArray(inputs)) throw new Error('Invalid container migration packaging policy');
17
+ policies.push({migrationPrefix:inputs.migrationPrefix === undefined ? '' : inputs.migrationPrefix,extraMigrationDirectories:inputs.extraMigrationDirectories === undefined ? [] : inputs.extraMigrationDirectories,...(inputs.wranglerFile === undefined ? {} : {wranglerFile:inputs.wranglerFile})});
18
+ }
19
+ if (policies.some(policy => !isDeepStrictEqual(policy,policies[0]))) throw new Error('Conflicting container migration packaging policies');
20
+ return policies[0] ?? {migrationPrefix:'',extraMigrationDirectories:[]};
21
+ }
22
+
23
+ export async function containerMigrationInventory(root,{config,configFile}) {
24
+ const policy = await migrationPolicy(root);
25
+ const file = await sourcePath(root,policy.wranglerFile ?? configFile);
26
+ const migrationConfig = policy.wranglerFile === undefined ? config : JSON.parse(await readFile(file,'utf8'));
27
+ const resources = (config.d1_databases ?? []).map(db => ({kind:'d1',binding:db.binding}));
28
+ const directories = (migrationConfig.d1_databases ?? []).map(db => {
29
+ if (typeof db.migrations_dir !== 'string' || !db.migrations_dir.trim()) throw new Error('Invalid D1 migrations_dir');
30
+ return {binding:db.binding,path:resolve(dirname(file),db.migrations_dir),prefix:policy.migrationPrefix};
31
+ });
32
+ return collectMigrations(root,[...directories,...policy.extraMigrationDirectories],resources);
33
+ }
@@ -1,10 +1,24 @@
1
- import { validateMigrations } from '../deploy-kit/migrations.mjs';
1
+ import { createHash } from 'node:crypto';
2
+ import { assertMigrationSqlSafe } from '../deploy-kit/sql-safety.mjs';
3
+ import { sqlTokens } from '../deploy-kit/sql-tokens.mjs';
2
4
  import { recorded } from '../deploy-kit/migration-ledger.mjs';
5
+ function validateMigrations(migrations) {
6
+ const names = new Set();
7
+ for (const migration of migrations) {
8
+ if (!/^[\w.-]+\.sql$/.test(migration.name) || names.has(migration.name)) throw new Error('migration-order-or-name');
9
+ names.add(migration.name);
10
+ if (typeof migration.sql !== 'string' || !migration.sql.trim() || Buffer.byteLength(migration.sql) > 10*1024*1024 || createHash('sha256').update(migration.sql).digest('hex') !== migration.sha256 || !sqlTokens(migration.sql).length) throw new Error('invalid-migration-bytes');
11
+ assertMigrationSqlSafe(migration.sql);
12
+ if (migration.rewrites !== undefined && (!Array.isArray(migration.rewrites) || !migration.rewrites.every(rewrite => /^[a-f0-9]{64}$/.test(rewrite?.from ?? '') && rewrite.from !== migration.sha256 && typeof rewrite.reason === 'string' && rewrite.reason.trim()))) throw new Error('invalid-migration-rewrite');
13
+ }
14
+ }
3
15
  export async function migrate(db, migrations, { ledger = '_rer_deploy_migrations' } = {}) {
4
16
  if (!['_rer_deploy_migrations', '_rer_portability_migrations'].includes(ledger)) throw new Error('Invalid migration ledger');
5
17
  validateMigrations(migrations);
6
18
  await db.prepare(`CREATE TABLE IF NOT EXISTS ${ledger}(name TEXT PRIMARY KEY,sha256 TEXT NOT NULL,state TEXT NOT NULL CHECK(state IN ('applying','applied')))`).run();
7
- const history = (await db.prepare(`SELECT name,sha256,state FROM ${ledger} ORDER BY name`).all()).results;
19
+ // The producer preserves declared directory order, including prefixes. Rowid
20
+ // preserves application order without changing the existing ledger schema.
21
+ const history = (await db.prepare(`SELECT name,sha256,state FROM ${ledger} ORDER BY rowid`).all()).results;
8
22
  if (history.some((row, i) => !recorded(row, migrations[i]) || row.state !== 'applied')) throw new Error('changed-or-uncertain-migration-history');
9
23
  const legacy = await db.prepare("SELECT name FROM sqlite_master WHERE type='table' AND name='d1_migrations'").first();
10
24
  if (ledger === '_rer_deploy_migrations' && legacy && (await db.prepare('SELECT name FROM d1_migrations ORDER BY id').all()).results.some(row => !history.some(prior => prior.name === row.name))) throw new Error('legacy-migration-history-needs-reviewed-hash-adoption');
@@ -0,0 +1,61 @@
1
+ import {lstat, readFile, readdir, realpath} from 'node:fs/promises';
2
+ import {resolve, relative, isAbsolute, sep, join} from 'node:path';
3
+ import {createHash} from 'node:crypto';
4
+ import {array, keys, string, bindingPattern} from './bindings.mjs';
5
+ export const codeUnitOrder = (a,b) => a < b ? -1 : a > b ? 1 : 0;
6
+ const privatePart = /^(?:\.git|\.env(?:\..*)?|\.dev\.vars(?:\..*)?|\.npmrc|\.yarnrc(?:\.yml)?|\.netrc|\.pypirc|release-provenance\.json)$/i;
7
+ /** Check each actual path component, including in-root symlinks. Never read through one. */
8
+ export async function sourcePath(root, input, {base = root, kind = 'file', optional = false} = {}) {
9
+ if (typeof input !== 'string' || !input || /[\\\x00-\x1f\x7f]/.test(input)) throw Error('Invalid candidate source path');
10
+ const rootPath = await realpath(root);
11
+ let destination = resolve(base, input);
12
+ // macOS /var and /tmp aliases (and an explicitly supplied root alias) refer
13
+ // to the same candidate root. Normalize only that known root, not child links.
14
+ const lexicalWithin = relative(resolve(root), destination);
15
+ if (lexicalWithin && lexicalWithin !== '..' && !lexicalWithin.startsWith(`..${sep}`) && !isAbsolute(lexicalWithin)) destination = resolve(rootPath, lexicalWithin);
16
+ const within = relative(rootPath, destination);
17
+ if (!within || within === '..' || within.startsWith(`..${sep}`) || isAbsolute(within)) throw Error('Input escapes candidate source or names its root');
18
+ const parts = within.split(sep);
19
+ if (parts.some(part => privatePart.test(part))) throw Error('Private candidate source path is forbidden');
20
+ let path = rootPath;
21
+ for (let index = 0; index < parts.length; index++) {
22
+ path = join(path, parts[index]);
23
+ let stat;
24
+ try { stat = await lstat(path); }
25
+ catch (error) { if (error.code === 'ENOENT' && optional) return null; throw error; }
26
+ if (stat.isSymbolicLink()) throw Error('Candidate source symlink is forbidden');
27
+ if (index < parts.length - 1 ? !stat.isDirectory() : kind === 'directory' ? !stat.isDirectory() : !stat.isFile()) throw Error(`Candidate source must be a regular ${kind}`);
28
+ }
29
+ return path;
30
+ }
31
+ export function decodeUtf8(bytes, label) {
32
+ // Preserve a UTF-8 BOM: it is part of both SQL text and its byte digest.
33
+ try { return new TextDecoder('utf-8', {fatal:true, ignoreBOM:true}).decode(bytes); }
34
+ catch { throw Error(`Invalid UTF-8 ${label}`); }
35
+ }
36
+ export async function collectMigrations(root, directories, resources) {
37
+ const result = [], identities = new Set();
38
+ for (const directory of array(directories, 'migration directories')) {
39
+ keys(directory, ['binding','path','prefix','optional'], 'migration directory');
40
+ string(directory.binding, 'migration binding', bindingPattern);
41
+ if (!resources.some(row => row.kind === 'd1' && row.binding === directory.binding)) throw Error(`Migration directory requires D1 binding: ${directory.binding}`);
42
+ if (directory.optional !== undefined && typeof directory.optional !== 'boolean') throw Error('Invalid migration optional flag');
43
+ const prefix = directory.prefix === undefined ? '' : directory.prefix;
44
+ if (typeof prefix !== 'string' || !/^[\w.-]*$/.test(prefix)) throw Error('Invalid migration prefix');
45
+ const path = await sourcePath(root, directory.path, {kind:'directory',optional:directory.optional === true});
46
+ if (path === null) continue;
47
+ for (const file of (await readdir(path, {withFileTypes:true})).sort((a,b) => codeUnitOrder(a.name,b.name))) {
48
+ if (!file.name.endsWith('.sql')) continue;
49
+ if (!file.isFile() || !/^[\w.-]+\.sql$/.test(file.name)) throw Error('Migration must be a regular SQL file');
50
+ const name = prefix + file.name, identity = `${directory.binding}/${name}`;
51
+ if (identities.has(identity)) throw Error(`Duplicate migration identity: ${identity}`);
52
+ identities.add(identity);
53
+ const filePath = await sourcePath(root, join(path, file.name));
54
+ const bytes = await readFile(filePath);
55
+ if (bytes.length > 10 * 1024 * 1024) throw Error('Migration exceeds supported limit');
56
+ const sql = decodeUtf8(bytes, 'migration');
57
+ result.push({binding:directory.binding,name,sha256:createHash('sha256').update(bytes).digest('hex'),sql});
58
+ }
59
+ }
60
+ return result;
61
+ }
@@ -4,6 +4,10 @@ import { effectSuccess } from './responses.mjs';
4
4
  const readRequests = new WeakSet();
5
5
  let installed = false;
6
6
 
7
+ // Shared runtime consumers may honour suppressed results only while the demo
8
+ // guard is installed. This keeps Node-side authoring imports free of Workers APIs.
9
+ export function demoEffectsEnabled() { return installed; }
10
+
7
11
  // Read intent stays in memory, never in a header the remote server could use.
8
12
  export function fetchDemoRead(input, init) {
9
13
  const request = new Request(input, init);
@@ -5,7 +5,14 @@ that adopted demo mode (`withDemo`, export/DEMO.md). `plan.mjs` turns `deploy-de
5
5
  (optionally `instanceType`, one of Cloudflare's predefined Container instance
6
6
  types, `basic` unless set) into the Worker's Wrangler configuration; the CLI (`cli/demo-host.mjs`) writes
7
7
  it with these files and the seed's container image folder (workerd engine) to
8
- `.rer/demo-host/` and runs `wrangler deploy` there.
8
+ `.rer/demo-host/` and runs `wrangler deploy` there. Declared
9
+ `container.hostVariables` resolve from demo settings to the primary hostname or
10
+ its descendants (for example `page.demo.<domain>`); each gets a custom-domain
11
+ route to the same front. The visitor cookie covers these hosts, while the
12
+ primary host's `__Host` owner session stays host-only. The front supplies a
13
+ validated public origin to the container ingress, so secondary page routing
14
+ sees the requested host. Anonymous secondary requests return to the primary
15
+ start page.
9
16
 
10
17
  - `worker.mjs`: `GET /` is the start page; `POST /__demo/start` checks the
11
18
  exact Origin, the `STARTS` rate limit (3 per IP per 60 s), Turnstile's
@@ -29,12 +36,11 @@ it with these files and the seed's container image folder (workerd engine) to
29
36
  guard simulates effects on people and third-party accounts, while the
30
37
  container egress independently refuses direct mail/SMS provider transports
31
38
  and SMTP ports. Public-address filtering and TLS remain enforced.
32
- The platform stops a container about two minutes after its Durable Object's
33
- last event, when it evicts the object (and the object's exit watcher with
34
- it). So the alarm stays in flight until the demo's next check, at most three
35
- minutes, and arms the next at once, as the Containers class keeps its own
36
- object alive. A request or alarm that finds the container gone ends the demo
37
- and answers the ended page (410), never a 5xx.
39
+ The platform inactivity timeout is set after start and reapplied when an
40
+ object recovers an already-running container. Persisted alarms enforce the
41
+ exact idle/absolute deadline without keeping a timer in flight. Requests
42
+ check both deadlines before refreshing activity. A container found stopped
43
+ ends the demo and answers the ended page (410).
38
44
  - `capacity.mjs`: `DemoCapacity` counts unexpired reservation rows inside a
39
45
  storage transaction, excluding diagnostics and expired rows. A lost start
40
46
  handoff expires after one minute; a successful claim extends the reservation
@@ -54,7 +60,8 @@ it with these files and the seed's container image folder (workerd engine) to
54
60
  with the refusal's status)
55
61
  and shows each step: the check, the private copy starting, the sign-in. A
56
62
  refusal, a network failure or a start past 25 seconds is shown with Try
57
- again, which runs a new check; progress is announced through a
63
+ again, which first destroys the abandoned copy and releases its place through
64
+ `POST /__demo/cancel`, then runs a new check; progress is announced through a
58
65
  `role="status"` region and failures through `role="alert"`. Without
59
66
  JavaScript the form posts as before and gets the 303 or the page.
60
67
  - `remove.mjs`: `rer deploy --demo --remove --yes`. The deploy passes the
@@ -77,9 +84,10 @@ is in DEMO.md.
77
84
  Startup has a 45-second budget, with a separate five-second HTTP probe limit.
78
85
  The Durable Object owns that work and cleanup while the page polls; a browser
79
86
  that leaves cannot abandon it. Retrying a pending copy resumes the same
80
- reservation. Failed starts retain the bounded operator diagnostic and destroy
81
- the container; cleanup retries keep a reservation only while destruction is
82
- unconfirmed. Every attempt logs its timestamp, elapsed time, duration and
87
+ 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
88
+ the container. Destruction retries for failed starts never extend or reclaim the
89
+ released place. Each release logs the id's SHA-256 fingerprint with the start's
90
+ correlation. Every attempt logs its timestamp, elapsed time, duration and
83
91
  HTTP status or fixed error category, including successful probes. Exit records
84
92
  include the numeric platform exit code when supplied. A separate random
85
93
  `x-rer-demo-correlation` response header joins these events without disclosing
@@ -99,16 +107,18 @@ no access. The token never belongs in a URL. Ordinary visitors keep the generic
99
107
  failure page. No raw error text, health body, cookies or environment values are
100
108
  included in this diagnostic.
101
109
 
102
- Failed destruction retains its alarm and capacity reservation for cleanup;
103
- only confirmed destruction (or an already-stopped container) releases the
104
- place. A cleanup failure is logged separately and cannot replace the startup
105
- failure shown to the visitor.
110
+ Failed destruction retains its alarm for cleanup. A live visitor's demo also
111
+ retains its capacity reservation until destruction is confirmed (or the
112
+ container is already stopped); a terminal start failure releases it immediately.
113
+ A cleanup failure is logged separately and cannot replace the startup failure
114
+ shown to the visitor.
106
115
 
107
116
  Cloudflare may briefly report that it has no prepared container instance even
108
117
  when the application is below `max_instances`. `placement.mjs` keeps the same
109
118
  reservation and launch options, requests placement again only for that specific
110
119
  provider response, and stays within the original startup deadline. Boot, health
111
- and claim errors are not retried. Placement exhaustion answers 503 with the
112
- capacity notice and releases the reservation. Cleanup keeps only that terminal
120
+ and claim errors are not retried. Placement exhaustion answers with the normal
121
+ failed-start retry state and releases the reservation; the capacity notice is
122
+ reserved for actual reservation exhaustion. Cleanup keeps only that terminal
113
123
  answer and correlation for one minute, so a status poll after object recovery
114
124
  still receives the right answer; its alarm then deletes the remaining metadata.
@@ -45,7 +45,7 @@ export class DemoCapacity extends DurableObject {
45
45
  }
46
46
 
47
47
  async release(id) {
48
- await this.ctx.storage.delete(id);
48
+ return this.ctx.storage.delete(id);
49
49
  }
50
50
  }
51
51