@runeditrun/rer 0.2.20 → 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 (51) hide show
  1. package/cli/README.md +7 -0
  2. package/cli/demo-host.mjs +9 -8
  3. package/cli/demo-removal-provider.mjs +24 -0
  4. package/cli/deploy-command.mjs +10 -7
  5. package/cli/deploy-hook.mjs +10 -14
  6. package/cli/deploy.mjs +3 -1
  7. package/cli/export.mjs +1 -1
  8. package/cli/managed-server.mjs +5 -2
  9. package/cli/production-auth.mjs +19 -5
  10. package/global/CHANGELOG.md +7 -1
  11. package/modules/container/README.md +10 -0
  12. package/modules/container/build.mjs +10 -12
  13. package/modules/container/host.mjs +3 -1
  14. package/modules/container/ingress.mjs +15 -4
  15. package/modules/container/migration-inventory.mjs +33 -0
  16. package/modules/container/migrations.mjs +16 -2
  17. package/modules/delivery-producer/migrations.mjs +61 -0
  18. package/modules/demo/effects.mjs +4 -0
  19. package/modules/demo-host/README.md +34 -15
  20. package/modules/demo-host/capacity.mjs +1 -1
  21. package/modules/demo-host/container.mjs +73 -39
  22. package/modules/demo-host/diagnostics.mjs +1 -0
  23. package/modules/demo-host/pages.mjs +1 -1
  24. package/modules/demo-host/placement.mjs +23 -0
  25. package/modules/demo-host/plan.mjs +20 -7
  26. package/modules/demo-host/remove.mjs +9 -7
  27. package/modules/demo-host/start-script.mjs +10 -1
  28. package/modules/demo-host/startup.mjs +4 -2
  29. package/modules/demo-host/worker.mjs +18 -1
  30. package/modules/dependencies/approval.mjs +3 -2
  31. package/modules/dependencies/index.mjs +2 -2
  32. package/modules/deploy-kit/README.md +16 -2
  33. package/modules/deploy-kit/diagnostics.mjs +9 -0
  34. package/modules/deploy-kit/health-request.mjs +7 -5
  35. package/modules/deploy-kit/provider-transport.mjs +56 -0
  36. package/modules/deploy-kit/provider.mjs +22 -20
  37. package/modules/operator-auth/README.md +4 -0
  38. package/modules/operator-auth/accounts/credentials.mjs +2 -2
  39. package/modules/operator-auth/accounts/email-change.mjs +2 -2
  40. package/modules/operator-auth/accounts/provider-control.mjs +3 -3
  41. package/modules/operator-auth/accounts/store.mjs +9 -8
  42. package/modules/operator-auth/dev/cli-session.mjs +5 -4
  43. package/modules/operator-auth/dev/index.mjs +5 -5
  44. package/modules/operator-auth/dev-gate.mjs +4 -2
  45. package/modules/operator-auth/identity/email-link/index.mjs +2 -1
  46. package/modules/operator-auth/signin/index.mjs +10 -2
  47. package/onboarding/adapter.mjs +11 -3
  48. package/onboarding/operator-installer.mjs +2 -2
  49. package/onboarding/operator-migrations.mjs +48 -0
  50. package/onboarding/process-table.mjs +45 -19
  51. 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,9 +19,9 @@ 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
- if (options.remove) return removeDemo(plan, store);
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.');
26
26
  const folder = join(project.root, '.rer', 'demo-host');
27
27
  await rm(folder, { recursive: true, force: true });
@@ -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) {
@@ -54,15 +54,16 @@ export async function demoHostCommand(project, options, store, signal, out, oper
54
54
  return { dryRun: false, deployed: true, origin: plan.origin, demo: summary };
55
55
  }
56
56
 
57
- async function removeDemo(plan, store) {
57
+ async function removeDemo(plan, store, signal) {
58
58
  const names = ['CLOUDFLARE_ACCOUNT_ID', 'CLOUDFLARE_API_TOKEN'];
59
59
  if (names.some(name => !process.env[name]?.trim())) throw missing(`${names.join(' and ')} are required to remove the demo; see DEMO.md "Removing a hosted demo".`);
60
60
  const { cloudflare } = await import('../modules/deploy-kit/provider.mjs');
61
61
  const { providerRefusal } = await import('../modules/deploy-kit/diagnostics.mjs');
62
62
  const { demoRemove } = await import('../modules/demo-host/remove.mjs');
63
- await store.step(`Removing the demo Worker ${plan.worker} on ${plan.hostname}`);
63
+ const { demoRemovalFetch } = await import('./demo-removal-provider.mjs');
64
+ await store.step(`Removing the demo Worker ${plan.worker} on ${plan.hostnames.join(', ')}`);
64
65
  let result;
65
- try { result = await demoRemove({ api: cloudflare({ accountId: process.env.CLOUDFLARE_ACCOUNT_ID, token: process.env.CLOUDFLARE_API_TOKEN }), plan }); }
66
+ try { result = await demoRemove({ api: cloudflare({ accountId: process.env.CLOUDFLARE_ACCOUNT_ID, token: process.env.CLOUDFLARE_API_TOKEN, fetch: demoRemovalFetch(signal) }), plan }); }
66
67
  catch (error) {
67
68
  if (error instanceof RerError) throw error;
68
69
  const code = /^[a-z0-9-]{1,80}$/.test(error.message) ? error.message : 'demo-remove-failed';
@@ -70,5 +71,5 @@ async function removeDemo(plan, store) {
70
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);
71
72
  }
72
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.`);
73
- 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) };
74
75
  }
@@ -0,0 +1,24 @@
1
+ import { setTimeout as pause } from 'node:timers/promises';
2
+
3
+ // Only demo removal uses these retries. The provider's original request signal
4
+ // bounds every attempt and backoff together; no retry gets a fresh deadline.
5
+ export function demoRemovalFetch(signal, request = globalThis.fetch) {
6
+ return async (input, init = {}) => {
7
+ const signals = [init.signal, signal].filter(Boolean);
8
+ const bounded = signals.length ? AbortSignal.any(signals) : undefined;
9
+ const method = init.method ?? 'GET';
10
+ for (let attempt = 0; ; attempt++) {
11
+ bounded?.throwIfAborted();
12
+ const response = await request(input, { ...init, signal: bounded });
13
+ // A DELETE can have succeeded before the provider lost its answer. Its
14
+ // retry then sees 404; the remover still performs its full absence readback.
15
+ if (method === 'DELETE' && response.status === 404) {
16
+ void response.body?.cancel().catch(() => {});
17
+ return new Response(null, { status: 204 });
18
+ }
19
+ if (!['GET', 'DELETE'].includes(method) || response.status < 500 || response.status > 599 || attempt === 2) return response;
20
+ void response.body?.cancel().catch(() => {});
21
+ await pause(250 * 2 ** attempt, undefined, { signal: bounded });
22
+ }
23
+ };
24
+ }
@@ -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) {
@@ -78,9 +78,9 @@ export async function deployCommand(context) {
78
78
  const command = (verb, extra = [], values = env, more = {}) => owner([verb, '--declaration', path, '--environment', environment, ...(hookRecord ? ['--state', hookRecord] : []), ...extra], values, { cwd: root, localOnlyKeys, ...more });
79
79
  // A hook that deployed through the kit's standard record lets rer issue and
80
80
  // retire the setup token; a hook keeping an unnamed record of its own keeps its token.
81
- let recorded;
81
+ let recorded, hookReturned = false;
82
82
  const inspect = async (origin = deployed?.origin) => {
83
- try { return recorded = await command('inspect', [], env, {propagation:observedReadiness(origin)?.propagation}); }
83
+ try { return recorded = await command('inspect', [], env, hookReturned ? {} : {propagation:observedReadiness(origin)?.propagation}); }
84
84
  catch (error) { if (error.message !== 'missing-owner-receipt') throw error; }
85
85
  };
86
86
  let observedClaims = [];
@@ -97,18 +97,21 @@ export async function deployCommand(context) {
97
97
  for (const key of Object.keys(process.env)) if (!Object.hasOwn(saved, key)) delete process.env[key];
98
98
  Object.assign(process.env, saved);
99
99
  }
100
+ hookReturned = true;
100
101
  if (options.dryRun) return deployed;
101
102
  await rememberHookState(root, environment, deployed?.state);
102
103
  hookRecord = await hookState(root, environment);
103
104
  const origin = hookOrigin(deployed, deployAgain(options));
104
105
  const uploaded = observedReadiness(origin);
105
- step = 'wait for the deployed source';
106
- await waitForSource({ origin, sourceSha, uploaded, again: deployAgain(options) });
107
106
  step = 'verify recorded deployment';
108
- const state = await inspect();
107
+ // A hook may already have claimed the owner and retired its token. Read the
108
+ // current version independently of the original upload's spent deadline.
109
+ const state = await inspect(origin);
109
110
  if ((state?.origin ?? origin) !== origin || claims.some(item => item.origin !== origin)) throw new Error('owner-claim-origin-mismatch');
110
111
  if (claims.some(item => !item.outcome)) throw new Error('owner-claim-failed');
111
112
  claimed = claims.at(-1)?.outcome;
113
+ step = 'wait for the deployed source';
114
+ await waitForSource({ origin, sourceSha, versionId: state?.versionId ?? (!claims.length && !deployed.owner ? uploaded?.versionId : undefined), again: deployAgain(options) });
112
115
  step = 'claim owner';
113
116
  if (typeof deployed.setupToken === 'string' && deployed.setupToken) addSecret(deployed.setupToken);
114
117
  const sentSecrets = (uploaded?.sentSecrets ?? []).filter(binding => uploaded.propagation.matchesToken(binding, env[declaration.secrets.find(item => item.binding === binding)?.environment]));
@@ -133,7 +136,7 @@ export async function deployCommand(context) {
133
136
  if (setup) throw commandFailure(redact(setup));
134
137
  const readiness = mutationReadinessFailure(error, { again, redact });
135
138
  if (readiness) throw commandFailure(readiness);
136
- const refused = providerRefusal(error);
139
+ const refused = providerRefusal(error) ?? providerTransport(error);
137
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'}.` : ''}`));
138
141
  }
139
142
  }
@@ -12,25 +12,21 @@ export function hookOrigin(deployed, again) {
12
12
  throw hookFailure(`The deploy hook finished without reporting a live deployment, so rer claimed no owner. A deploy hook returns { deployed: true, origin } once its upload is live. Check the hook's output above, fix the hook, then run ${again} again.`);
13
13
  }
14
14
 
15
- export async function waitForSource({ origin, sourceSha, uploaded, again }) {
16
- // Only a kit upload observed in this hook invocation supplies a deadline and
17
- // version. An external hook without that proof keeps its independent wait.
18
- const propagation = uploaded?.propagation;
19
- const fresh = propagation?.freshUpload && propagation.versionId === uploaded.versionId;
20
- const window = fresh ? propagation.window : undefined;
21
- try { await waitHealth({ origin, expected: { sourceSha, ...(uploaded?.versionId ? { versionId: uploaded.versionId } : {}) },
22
- request: (url, init) => globalThis.fetch(url, init),
23
- ...(window ? { window, clock: window.clock, sleep: window.sleep, freshUpload: true } : {}) }); }
15
+ export async function waitForSource({ origin, sourceSha, versionId, again }) {
16
+ // Finalization gets its own 90 seconds after the hook returns: upload, owner
17
+ // claim and token retirement may have consumed the upload readiness window.
18
+ // Retirement also creates a new version; use the current recorded identity.
19
+ try { await waitHealth({ origin, expected: { sourceSha, ...(versionId ? { versionId } : {}) }, retryIdentityMismatch: true,
20
+ request: (url, init) => globalThis.fetch(url, init) }); }
24
21
  catch (error) {
25
22
  const last = error.attempts?.at(-1);
26
- let named;
27
- try { named = JSON.parse(last.body).deployment?.sourceSha; } catch {}
23
+ let named, version;
24
+ try { const identity = JSON.parse(last.body).deployment; named = identity?.sourceSha; version = identity?.versionId; } catch {}
28
25
  const seen = !last || last.status === null ? 'it did not answer'
29
26
  : last.status !== 200 ? `its last answer was HTTP ${last.status}`
30
- : typeof named === 'string' && /^[0-9a-f]{7,64}$/.test(named) ? `it still answers for commit ${named.slice(0, 12)}`
27
+ : typeof named === 'string' && /^[0-9a-f]{7,64}$/.test(named) ? `it still answers for commit ${named.slice(0, 12)}${typeof version === 'string' && /^[a-zA-Z0-9_-]{1,64}$/.test(version) ? `, Worker version ${version}` : ''}`
31
28
  : 'it answers without a deploy identity';
32
- const deadline = window?.remaining() <= 0 ? ' before the upload readiness deadline' : '';
33
- throw hookFailure(`The deploy hook reported a deployment at ${origin}, but its /health never named this checkout's commit ${sourceSha.slice(0, 12)}${uploaded?.versionId ? ' and the uploaded Worker version' : ''}${deadline} (${seen}), so rer stopped before owner finalization. Check that the hook uploaded this build and that /health answers deploymentHealth(env), then run ${again} again.`);
29
+ throw hookFailure(`The deploy hook reported a deployment at ${origin}, but its /health did not confirm this checkout's commit ${sourceSha.slice(0, 12)}${versionId ? ' and the current recorded Worker version' : ''} within the 90-second finalization wait (${seen}; ${last?.errorClass ?? 'health-timeout'}), so rer stopped before owner finalization. Check that the hook uploaded this build and that /health answers deploymentHealth(env), then run ${again} again.`);
34
30
  }
35
31
  }
36
32
 
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
  }
@@ -1,6 +1,12 @@
1
1
  # Changelog
2
2
 
3
- ## Unreleased
3
+ ## 0.2.21
4
+
5
+ - Demo removal retries transient Cloudflare provider failures while preserving the resource ownership checks.
6
+ - Deployment finalization lets health and source identity settle before refusing a successful rollout.
7
+ - Hosted demos keep the same visitor reservation while Cloudflare prepares an available container, within the existing startup budget. Placement exhaustion returns a clean capacity refusal.
8
+ - Status polling retains a failed start's terminal answer briefly after cleanup, including after Durable Object recovery.
9
+ - Demo image builds load Vite configuration preparation before checking the root Wrangler Worker entry.
4
10
 
5
11
  - Concurrent checkout launches validate server registry state under its transaction lock, avoiding a false foreign-file refusal during atomic updates.
6
12
  - A checkout behind the front door serves its run-site preview above the app's assigned port instead of trying privileged ports.
@@ -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);}
@@ -18,9 +17,11 @@ async function buildImage(root, { output = join(root,'.rer/container/image'), en
18
17
  declaration ??= JSON.parse(await readFile(join(root,'deploy.json'),'utf8'));
19
18
  engine ??= declaration.container?.engine ?? 'workerd'; cacheContract ??= declaration.container?.cacheContract; runner ??= declaration.container?.runner;
20
19
  if (!['workerd','node'].includes(engine)) throw new Error('Unsupported container engine');
21
- await checkBoundary(root,{engine,cacheContract});
22
20
  const vite = await import(pathToFileURL(require.resolve('vite'))), loaded = await vite.loadConfigFromFile({ command:'build',mode:'production' },undefined,root);
23
21
  if (!loaded) throw new Error('Vite configuration missing');
22
+ // Config loading may prepare the seed's root Wrangler configuration. Inspect
23
+ // its declared entry after that preparation, still before compiling the app.
24
+ await checkBoundary(root,{engine,cacheContract});
24
25
  const config = loaded.config;
25
26
  const outputExports = new Map();
26
27
  const freshServerFiles = new Set(), serverOutput = resolve(root,'dist/server');
@@ -65,14 +66,11 @@ async function buildImage(root, { output = join(root,'.rer/container/image'), en
65
66
  if (!freshServerFiles.has(serverEntry)) throw new Error('Container server entry was not emitted by this build');
66
67
  await checkBoundary(root,{engine,cacheContract,entries:[serverEntry],emittedFiles:freshServerFiles,outputRoot:serverOutput});
67
68
  const { unstable_splitSqlQuery:split } = require('wrangler');
68
- const migrations = {}, rewrites = await readMigrationRewrites(root);
69
- for (const db of workerConfig.d1_databases ?? []) {
70
- let directory = resolve(root,db.migrations_dir ?? 'migrations');
71
- try { await readdir(directory); } catch { directory = resolve(root,'dist/server',db.migrations_dir); }
72
- migrations[db.binding] = [];
73
- 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) }); }
74
- migrations[db.binding] = withRewrites(migrations[db.binding], rewrites, { partial:true });
75
- }
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)});
76
74
  const entry = engine === 'workerd' ? 'dist/server/'+workerConfig.main : 'dist/server/index.mjs';
77
75
  await readFile(join(root,entry));
78
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);