@runeditrun/rer 0.2.16 → 0.2.17

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.
@@ -0,0 +1,32 @@
1
+ import { observedReadiness } from '../modules/deploy-kit/readiness-window.mjs';
2
+ // Retry only the bootstrap HTTP exchange, never the hook's other effects. The
3
+ // current invocation must prove it uploaded this token to a newly created Worker.
4
+ export async function ownerClaimRequest(fetch, input, init, { deployed, tokenKey = 'SETUP_TOKEN', signal } = {}) {
5
+ const url = new URL(input instanceof Request ? input.url : input);
6
+ if (deployed?.origin && url.origin !== deployed.origin) throw new Error('owner-claim-origin-mismatch');
7
+ const known = deployed?.propagation ? deployed : observedReadiness(url.origin);
8
+ const propagation = known?.propagation;
9
+ let token;
10
+ try { token = JSON.parse(typeof init.body === 'string' ? init.body : input instanceof Request ? await input.clone().text() : 'null')?.token; } catch {}
11
+ const fresh = propagation?.freshUpload && propagation.versionId === (deployed?.versionId ?? known.versionId) && propagation.matchesToken(tokenKey, token);
12
+ const window = fresh ? propagation.window : undefined;
13
+ for (;;) {
14
+ const remaining = window?.remaining() ?? 30000;
15
+ if (remaining <= 0) throw new Error('owner-claim-readiness-timeout');
16
+ const timeout = AbortSignal.timeout(Math.max(1, Math.ceil(Math.min(30000, remaining))));
17
+ const signals = [signal, init.signal, input instanceof Request ? input.signal : undefined, timeout].filter(Boolean);
18
+ const response = await fetch(input instanceof Request ? input.clone() : input, { ...init, redirect: 'manual', signal: AbortSignal.any(signals) });
19
+ if (response.redirected) throw new Error('owner-claim-redirected');
20
+ if (response.status === 303) {
21
+ const destination = response.headers.get('location');
22
+ if (destination && new URL(destination, url).origin !== url.origin) throw new Error('owner-claim-origin-mismatch');
23
+ }
24
+ let invalid = false;
25
+ if (fresh && response.status === 403) {
26
+ try { invalid = (await response.clone().json())?.error === 'invalid_setup_token'; } catch {}
27
+ }
28
+ if (!invalid || window.remaining() <= 0) return response;
29
+ await response.body?.cancel();
30
+ await window.pause();
31
+ }
32
+ }
@@ -1,3 +1,4 @@
1
+ import { observedReadiness } from '../modules/deploy-kit/readiness-window.mjs';
1
2
  import { readFile, readdir } from 'node:fs/promises';
2
3
  import { contained } from './project.mjs';
3
4
  import { invalid, refuse, RerError } from './errors.mjs';
@@ -43,7 +44,18 @@ export async function deployCommand(context) {
43
44
  validateDeployOptions(options);
44
45
  try { await refuseLocalModels(root); } catch { throw refuse('Deploy refuses the local-only key file .rer/local/models.json. Remove live-dev credentials before deployment.'); }
45
46
  const { path, value: declaration } = await declarationFor(root, adapter);
46
- ownerWorkerName(declaration.project, options.environment ?? 'production');
47
+ try { ownerWorkerName(declaration.project, options.environment ?? 'production'); }
48
+ catch (error) {
49
+ const message = {
50
+ 'invalid-owner-environment': 'Invalid deployment environment: use at most 32 characters, starting with a lowercase letter, with lowercase letters or digits separated by single hyphens.',
51
+ 'invalid-owner-project': 'Invalid deployment project in the deploy declaration: use a lowercase project name, optionally preceded by its owner and a slash.',
52
+ 'owner-name-too-long': 'The project and environment produce a Worker name longer than 63 characters.',
53
+ }[error.message];
54
+ if (!message) throw error;
55
+ const failure = invalid(`${message} Nothing was uploaded.`);
56
+ store.report(failure.message);
57
+ throw failure;
58
+ }
47
59
  const local = localOnlyBinding(declaration, localOnlyKeys);
48
60
  if (local) throw refuse(`${path} names the local-only key ${local} as a deploy binding or secret. Remove it; local dev keys never reach a deployment.`);
49
61
  // Custom hooks historically ignored these flags. Only the ownership controller
@@ -60,19 +72,19 @@ export async function deployCommand(context) {
60
72
  // A hook keeping the kit's record in its own state file named it; every kit
61
73
  // step after the hook (inspect, secret, record-owner) reads that record.
62
74
  let hookRecord = await hookState(root, environment);
63
- const command = (verb, extra = [], values = env) => owner([verb, '--declaration', path, '--environment', environment, ...(hookRecord ? ['--state', hookRecord] : []), ...extra], values, { cwd: root, localOnlyKeys });
75
+ const command = (verb, extra = [], values = env, more = {}) => owner([verb, '--declaration', path, '--environment', environment, ...(hookRecord ? ['--state', hookRecord] : []), ...extra], values, { cwd: root, localOnlyKeys, ...more });
64
76
  // A hook that deployed through the kit's standard record lets rer issue and
65
77
  // retire the setup token; a hook keeping an unnamed record of its own keeps its token.
66
78
  let recorded;
67
- const inspect = async () => {
68
- try { return recorded = await command('inspect'); }
79
+ const inspect = async (origin = deployed?.origin) => {
80
+ try { return recorded = await command('inspect', [], env, {propagation:observedReadiness(origin)?.propagation}); }
69
81
  catch (error) { if (error.message !== 'missing-owner-receipt') throw error; }
70
82
  };
71
83
  let observedClaims = [];
72
84
  let deployed, claimed, linked = false, linkResult, step = 'custom deploy';
73
- const link = async (token, origin) => { if (!linked) { const state = await inspect(); linkResult = await linkDependencies?.(state?.origin ?? origin, token); linked = true; } return linkResult; };
85
+ const link = async (token, origin) => { if (!linked) { const state = await inspect(origin); linkResult = await linkDependencies?.(state?.origin ?? origin, token); linked = true; } return linkResult; };
74
86
  try {
75
- return await observeOwner({ paths: ['/auth/bootstrap', adapter.hooks?.owner?.path, setupManifest?.steps?.find(item => item.type === 'owner-claim')?.path].filter(Boolean), inspect, beforeClaim: link, addSecret }, async claims => {
87
+ return await observeOwner({ paths: ['/auth/bootstrap', adapter.hooks?.owner?.path, setupManifest?.steps?.find(item => item.type === 'owner-claim')?.path].filter(Boolean), inspect, beforeClaim: link, addSecret, deployed: () => deployed, tokenKey: setupManifest?.steps?.find(item => item.type === 'owner-claim')?.tokenField }, async claims => {
76
88
  observedClaims = claims;
77
89
  const saved = { ...process.env };
78
90
  try {
@@ -86,8 +98,9 @@ export async function deployCommand(context) {
86
98
  await rememberHookState(root, environment, deployed?.state);
87
99
  hookRecord = await hookState(root, environment);
88
100
  const origin = hookOrigin(deployed, deployAgain(options));
101
+ const uploaded = observedReadiness(origin);
89
102
  step = 'wait for the deployed source';
90
- await waitForSource({ origin, sourceSha, again: deployAgain(options) });
103
+ await waitForSource({ origin, sourceSha, uploaded, again: deployAgain(options) });
91
104
  step = 'verify recorded deployment';
92
105
  const state = await inspect();
93
106
  if ((state?.origin ?? origin) !== origin || claims.some(item => item.origin !== origin)) throw new Error('owner-claim-origin-mismatch');
@@ -95,8 +108,9 @@ export async function deployCommand(context) {
95
108
  claimed = claims.at(-1)?.outcome;
96
109
  step = 'claim owner';
97
110
  if (typeof deployed.setupToken === 'string' && deployed.setupToken) addSecret(deployed.setupToken);
111
+ const sentSecrets = (uploaded?.sentSecrets ?? []).filter(binding => uploaded.propagation.matchesToken(binding, env[declaration.secrets.find(item => item.binding === binding)?.environment]));
98
112
  const kit = state ? { secret: (extra, values) => command('secret', extra, values), recordClaim: outcome => command('record-owner', ['--outcome', outcome]) } : { token: deployed.setupToken || undefined };
99
- claimed = await claimHostedOwner({ hook: adapter.hooks?.owner, setupManifest, declaration, deployed: { ...state, origin, owner: claimed, observedOwner: () => claims.at(-1)?.outcome }, env, supplied, ...kit, addSecret, beforeClaim: token => link(token, origin), store, signal: context.signal });
113
+ claimed = await claimHostedOwner({ hook: adapter.hooks?.owner, setupManifest, declaration, deployed: { ...state, sentSecrets, propagation:deployed.propagation ?? uploaded?.propagation, origin, owner: claimed, observedOwner: () => claims.at(-1)?.outcome }, env, supplied, ...kit, addSecret, beforeClaim: token => link(token, origin), store, signal: context.signal });
100
114
  if (!linked) await linkDependencies?.(origin);
101
115
  step = 'verify hosted setup';
102
116
  await checkHostedSetup({ origin, claimed: claimed ?? state?.ownerClaim?.outcome ?? claims.at(-1)?.outcome, signal: context.signal });
@@ -12,8 +12,15 @@ 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, again }) {
16
- try { await waitHealth({ origin, expected: { sourceSha }, request: (url, init) => globalThis.fetch(url, init) }); }
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 } : {}) }); }
17
24
  catch (error) {
18
25
  const last = error.attempts?.at(-1);
19
26
  let named;
@@ -22,7 +29,8 @@ export async function waitForSource({ origin, sourceSha, again }) {
22
29
  : last.status !== 200 ? `its last answer was HTTP ${last.status}`
23
30
  : typeof named === 'string' && /^[0-9a-f]{7,64}$/.test(named) ? `it still answers for commit ${named.slice(0, 12)}`
24
31
  : 'it answers without a deploy identity';
25
- throw hookFailure(`The deploy hook reported a deployment at ${origin}, but its /health never named this checkout's commit ${sourceSha.slice(0, 12)} (${seen}), so rer claimed no owner. Check that the hook uploaded this build and that /health answers deploymentHealth(env), then run ${again} again.`);
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.`);
26
34
  }
27
35
  }
28
36
 
@@ -1,8 +1,10 @@
1
+ import { observeReadiness, observedReadiness } from '../modules/deploy-kit/readiness-window.mjs';
2
+ import { ownerClaimRequest } from './deploy-claim-request.mjs';
1
3
  // Observe the actual bootstrap exchange of a custom hook, including hooks that
2
4
  // call the shared owner helper themselves. Never issue a second claim after it.
3
5
  // The kit's deployment record, when the hook keeps one there, is checked before
4
6
  // linking with its live setup token; rer checks the origin the hook returns too.
5
- export async function observeOwner({ paths, inspect, beforeClaim, addSecret }, run) {
7
+ export async function observeOwner({ paths, inspect, beforeClaim, addSecret, deployed: knownDeployment, tokenKey }, run) {
6
8
  const fetch = globalThis.fetch, claims = [];
7
9
  globalThis.fetch = async (input, init = {}) => {
8
10
  const url = new URL(input instanceof Request ? input.url : input);
@@ -12,10 +14,11 @@ export async function observeOwner({ paths, inspect, beforeClaim, addSecret }, r
12
14
  }
13
15
  if (typeof body?.token !== 'string' || !body.token) return fetch(input, init);
14
16
  addSecret(body.token);
15
- const deployed = await inspect();
17
+ const deployed = await inspect(url.origin);
16
18
  if (deployed && url.origin !== deployed.origin) throw new Error('owner-claim-origin-mismatch');
17
19
  await beforeClaim?.(body.token, url.origin);
18
- const response = await fetch(input, init);
20
+ const known = knownDeployment?.() ?? observedReadiness(url.origin);
21
+ const response = await ownerClaimRequest(fetch, input, init, {deployed:known ?? deployed, tokenKey});
19
22
  let code;
20
23
  if (response.status !== 303) {
21
24
  try { code = (await response.clone().json()).error; } catch {}
@@ -24,6 +27,6 @@ export async function observeOwner({ paths, inspect, beforeClaim, addSecret }, r
24
27
  claims.push({ origin: url.origin, outcome, status: response.status });
25
28
  return response;
26
29
  };
27
- try { return await run(claims); }
30
+ try { return await observeReadiness(() => run(claims)); }
28
31
  finally { globalThis.fetch = fetch; }
29
32
  }
@@ -1,3 +1,4 @@
1
+ import { ownerClaimRequest } from './deploy-claim-request.mjs';
1
2
  import { observeOwner } from './deploy-observation.mjs';
2
3
  import { randomBytes } from 'node:crypto';
3
4
 
@@ -55,7 +56,7 @@ export async function claimHostedOwner({ hook, setupManifest, declaration, deplo
55
56
  let observed;
56
57
  try {
57
58
  if (deployed.observedOwner) await hook(context);
58
- else await observeOwner({ paths: ['/auth/bootstrap', claim?.path].filter(Boolean), inspect, addSecret }, async claims => { await hook(context); observed = claims.at(-1)?.outcome; });
59
+ else await observeOwner({ paths: ['/auth/bootstrap', claim?.path].filter(Boolean), inspect, addSecret, deployed: () => deployed, tokenKey }, async claims => { await hook(context); observed = claims.at(-1)?.outcome; });
59
60
  } catch (cause) { if (cause.message === 'owner-claim-failed') throw cause; throw ownerFailure(cause.message); }
60
61
  // Function hooks can keep existing owners without returning a value. Observe
61
62
  // their actual response, instead of assuming that a resolved promise claimed.
@@ -66,9 +67,9 @@ export async function claimHostedOwner({ hook, setupManifest, declaration, deplo
66
67
  const displayName = (typeof hook.displayName === 'function' ? hook.displayName({}) : undefined) || 'Owner';
67
68
  let response, code = 'no code';
68
69
  try {
69
- response = await fetch(new URL(hook.path ?? '/auth/bootstrap', deployed.origin), { method: 'POST', redirect: 'manual', signal: AbortSignal.timeout(30000),
70
- headers: { Origin: deployed.origin, 'Content-Type': 'application/json' }, body: JSON.stringify({ token, displayName, returnTo: hook.returnTo ?? '/' }) });
71
- } catch { throw ownerFailure('the hosted application did not answer'); }
70
+ response = await ownerClaimRequest(fetch, new URL(hook.path ?? '/auth/bootstrap', deployed.origin), { method: 'POST',
71
+ headers: { Origin: deployed.origin, 'Content-Type': 'application/json' }, body: JSON.stringify({ token, displayName, returnTo: hook.returnTo ?? '/' }) }, {deployed, tokenKey, signal});
72
+ } catch (error) { throw ownerFailure(['owner-claim-origin-mismatch','owner-claim-redirected','owner-claim-readiness-timeout'].includes(error.message) ? error.message : 'the hosted application did not answer'); }
72
73
  if (response.status !== 303) { try { const body = await response.json(); if (typeof body?.error === 'string' && /^[a-z_]{1,64}$/.test(body.error)) code = body.error; } catch {} }
73
74
  else await response.body?.cancel();
74
75
  const outcome = response.status === 303 ? 'claimed' : response.status === 409 && code === 'already_initialized' ? 'kept' : undefined;
package/cli/deploy.mjs CHANGED
@@ -79,12 +79,12 @@ export function ownerDeploy(declarationPath, lifecycle) {
79
79
  const sent = supplied.filter(name => deployed.sentSecrets.includes(name));
80
80
  if (sent.length) store.report(`rer supplied ${sent.join(', ')} from the local setup owner and fresh generated values.`);
81
81
  step = 'health';
82
- await owner('health');
82
+ await owner('health', [], env, {propagation:deployed.propagation});
83
83
  step = 'claim owner';
84
- const { liveSecrets, retiredSecrets, ownerClaim } = await owner('inspect');
84
+ const { liveSecrets, retiredSecrets, ownerClaim } = await owner('inspect', [], env, {propagation:deployed.propagation});
85
85
  const linked = token => linkDependencies?.(deployed.origin, token);
86
- const claimed = await claimHostedOwner({ hook: lifecycle.hooks?.owner, setupManifest, declaration, deployed: { ...deployed, liveSecrets, retiredSecrets, ownerClaim }, env, supplied, addSecret: value => { redaction.addSecrets([value]); store.addSecrets?.([value]); },
87
- secret: (extra, values) => owner('secret', extra, values), beforeClaim: linked, recordClaim: outcome => owner('record-owner', ['--outcome', outcome]), inspect: () => owner('inspect'), store, signal });
86
+ const claimed = await claimHostedOwner({ hook: lifecycle.hooks?.owner, setupManifest, declaration, deployed: { ...deployed, propagation:deployed.propagation, liveSecrets, retiredSecrets, ownerClaim }, env, supplied, addSecret: value => { redaction.addSecrets([value]); store.addSecrets?.([value]); },
87
+ secret: (extra, values) => owner('secret', extra, values), beforeClaim: linked, recordClaim: outcome => owner('record-owner', ['--outcome', outcome]), inspect: () => owner('inspect', [], env, {propagation:deployed.propagation}), store, signal });
88
88
  // A seed that claims its owner its own way links with the token it was given.
89
89
  if (claimed === undefined) await linked(env[declared(claim?.tokenField ?? 'SETUP_TOKEN')?.environment]);
90
90
  step = 'verify hosted setup';
package/cli/door.mjs CHANGED
@@ -241,11 +241,12 @@ export async function verifyDoor(env, port) {
241
241
 
242
242
  async function verifyOnce(env, port) {
243
243
  const directory = doorDirectory(env), record = await runningDoor(env);
244
- const { listeners, processName } = await shared('process-diagnostics.mjs');
245
- const holders = (await listeners()).filter(row => row.names.some(name => portOf(name) === port));
244
+ const { listeners, processName, inspectionContext } = await shared('process-diagnostics.mjs');
245
+ const context = inspectionContext();
246
+ const holders = (await listeners(undefined,context,port)).filter(row => row.names.some(name => portOf(name) === port));
246
247
  const foreign = holders.find(row => row.pid !== record?.pid);
247
248
  const withheld = 'No sign-in link was printed.';
248
- if (foreign) throw refuse(`Port ${port} is held by PID ${foreign.pid} (${await processName(foreign.pid)}), which is not this account's front door${record ? ` (PID ${record.pid})` : ''}. ${withheld} Stop PID ${foreign.pid}, then run rer door restart.`);
249
+ if (foreign) throw refuse(`Port ${port} is held by PID ${foreign.pid} (${await processName(foreign.pid,context)}), which is not this account's front door${record ? ` (PID ${record.pid})` : ''}. ${withheld} Stop PID ${foreign.pid}, then run rer door restart.`);
249
250
  if (!record || record.port !== port) throw refuse(`The front door is not running on port ${port}. ${withheld} Start it with rer door start, then run this again.`);
250
251
  const token = (await readPrivate(join(directory, 'token')))?.trim();
251
252
  if (!token) throw refuse(`The front door's token is missing from ${directory}. ${withheld} Run rer door restart.`);
package/cli/local.mjs CHANGED
@@ -40,16 +40,18 @@ export async function selectedOrigin(project, options, { setup = false } = {}) {
40
40
  }
41
41
 
42
42
  export async function availablePort(host, port, { root, saved = false } = {}) {
43
+ const { portListener, inspectionContext } = await shared('process-diagnostics.mjs');
44
+ const context = inspectionContext();
43
45
  const socket = createServer();
44
46
  try { await new Promise((resolve, reject) => { socket.once('error', reject); socket.listen(port, host, resolve); }); }
45
47
  catch (error) {
46
48
  if (error.code !== 'EADDRINUSE') throw refuse(`Port ${port} cannot be used on ${host}: ${error.code ?? error.message}.`);
47
- throw refuse(await busyPortMessage({ port, host, root, saved }));
49
+ throw refuse(await busyPortMessage({ port, host, root, saved, context }));
48
50
  }
49
51
  finally { if (socket.listening) await new Promise(resolve => socket.close(resolve)); }
50
52
  // A server on every address (*:port) lets a bind to 127.0.0.1 succeed.
51
- const { portListener } = await shared('process-diagnostics.mjs');
52
- if (await portListener(port, host)) throw refuse(await busyPortMessage({ port, host, root, saved }));
53
+ const listener = await portListener(port, host, context);
54
+ if (listener) throw refuse(await busyPortMessage({ port, host, root, saved, context, listener }));
53
55
  }
54
56
 
55
57
  export {setupInput} from './setup-input.mjs';
@@ -8,17 +8,26 @@ import { shared } from './shared.mjs';
8
8
  export async function probePnpm(root, store, signal, env = process.env, timeoutMs = 10000) {
9
9
  const { dependencyEnvironment } = await shared('dependency-environment.mjs');
10
10
  const timeout = AbortSignal.timeout(timeoutMs), probeSignal = AbortSignal.any([signal, timeout].filter(Boolean));
11
- const child = store.spawn('Checking pinned pnpm', 'pnpm', ['--version'], { cwd: root, env: dependencyEnvironment(env), signal: probeSignal });
12
- let version = '', errors = '';
13
- child.stdout.on('data', chunk => { version = (version + chunk).slice(-1024); });
14
- child.stderr.on('data', chunk => { errors = (errors + chunk).slice(0, 4096); });
15
- const { code, exitSignal, error } = await new Promise(resolve => { child.once('error', error => resolve({ code: null, error })); child.once('exit', (code, exitSignal) => resolve({ code, exitSignal })); });
16
- await store.stop(child);
17
- if (code === 0 && !probeSignal.aborted) return version.trim();
18
- const line = (store.redact?.(errors) ?? errors).split('\n').map(text => text.trim()).find(Boolean)?.slice(0, 200);
11
+ let child, result, cleanup, version = '', errors = '';
12
+ // Redact before bounding the text: a limit must not cut through a secret.
13
+ const filter = store.redactionStream(text => { errors = (errors + text).slice(-4096); });
14
+ try {
15
+ child = store.spawn('Checking pinned pnpm', 'pnpm', ['--version'], { cwd: root, env: dependencyEnvironment(env), signal: probeSignal });
16
+ child.stdout.on('data', chunk => { version = (version + chunk).slice(-1024); });
17
+ child.stderr.on('data', chunk => filter.write(chunk));
18
+ result = await new Promise(resolve => { child.once('error', error => resolve({ code: null, error })); child.once('exit', (code, exitSignal) => resolve({ code, exitSignal })); });
19
+ } catch (error) { result = { code: null, error }; }
20
+ if (child) try { await store.stop(child); } catch (error) { cleanup = store.redact(String(error.message)); }
21
+ filter.end();
22
+ const { code, exitSignal, error } = result;
23
+ if (code === 0 && !probeSignal.aborted && !cleanup) return version.trim();
24
+ const line = errors.split('\n').map(text => text.trim()).find(Boolean)?.slice(0, 200);
19
25
  const reason = timeout.aborted ? `pnpm --version gave no answer within ${timeoutMs / 1000} s`
20
- : error ? `pnpm could not start (${error.code ?? error.message})`
26
+ : error ? `pnpm could not start (${store.redact(error.code ?? error.message)})`
27
+ : cleanup ? `pnpm --version exited ${code}; process cleanup failed: ${cleanup}`
21
28
  : code === null ? `pnpm --version was stopped by ${exitSignal}`
22
29
  : `pnpm --version exited ${code}${line ? `: ${line}` : ''}`;
23
- throw Object.assign(missing(`The pinned pnpm version probe failed (${reason}). Select the documented tool.`), { reason });
30
+ const diagnostic = `Command: pnpm --version\nExit code: ${code ?? 'none'}; signal: ${exitSignal ?? 'none'}\nStderr (redacted):\n${errors.trim() || '(no stderr)'}`;
31
+ const message = `The pinned pnpm version probe failed (${reason}).\n${diagnostic}\nSelect the documented tool.`;
32
+ throw Object.assign(missing(message), { reason, diagnostic });
24
33
  }
package/cli/port.mjs CHANGED
@@ -17,18 +17,22 @@ async function bindable(host, port) {
17
17
  // The first port from `from` that nothing listens on, on any address, and that
18
18
  // this host can bind. Another checkout's server may listen on every address,
19
19
  // which a bind to 127.0.0.1 alone does not notice.
20
- export async function firstFreePort(host, from = defaultPort) {
21
- const { listeningPorts } = await shared('process-diagnostics.mjs');
22
- const busy = await listeningPorts();
23
- for (let port = from; port <= 65535; port++) if (!busy.has(port) && await bindable(host, port)) return port;
20
+ export async function firstFreePort(host, from = defaultPort, context) {
21
+ const { listeningPorts, inspectionContext } = await shared('process-diagnostics.mjs');
22
+ context ??= inspectionContext();
23
+ const busy = await listeningPorts(context);
24
+ for (let port = from; port <= 65535; port++) {
25
+ if (performance.now() >= context.deadline) throw Error('Free-port search stopped at the shared inspection deadline.');
26
+ if (!busy.has(port) && await bindable(host, port)) return port;
27
+ }
24
28
  throw Error(`No free port from ${from} upward.`);
25
29
  }
26
30
 
27
31
  // Who holds a port: a server another checkout registered (its path and the
28
32
  // command that stops it), else the process and its working directory.
29
- export async function portHolder(port, host, root, env = process.env) {
33
+ export async function portHolder(port, host, root, env = process.env, context, observed) {
30
34
  const { portListener } = await shared('process-diagnostics.mjs');
31
- const listener = await portListener(port, host);
35
+ const listener = observed ?? await portListener(port, host, context);
32
36
  if (!listener) return undefined;
33
37
  const state = await readJson(join(serversDirectory(env), 'state.json'), { running: [] }).catch(() => ({ running: [] }));
34
38
  const record = (state.running ?? []).find(item => item.owner?.pid === listener.pid && item.port === port);
@@ -37,12 +41,15 @@ export async function portHolder(port, host, root, env = process.env) {
37
41
 
38
42
  // The refusal for a busy port. For a checkout that wants to move, it names the
39
43
  // holder and gives the exact command that moves this checkout to a free port.
40
- export async function busyPortMessage({ port, host, root, saved, env = process.env }) {
41
- const holder = await portHolder(port, host, root, env);
42
- const next = await firstFreePort(host, port + 1).catch(() => undefined);
44
+ export async function busyPortMessage({ port, host, root, saved, env = process.env, context, listener }) {
45
+ const { inspectionContext } = await shared('process-diagnostics.mjs');
46
+ context ??= inspectionContext();
47
+ const holder = await portHolder(port, host, root, env, context, listener);
48
+
43
49
  const kind = saved ? `Port ${port} is saved for this checkout but is` : `Port ${port} is`;
44
- if (!holder) return `${kind} in use by another process that this account cannot inspect. Stop it, or pick a free port${next ? ` such as ${next}` : ''}.`;
50
+ if (!holder) return `${kind} unavailable for binding; completed inspection found no listener on ${host}. Pick a free port or retry after checking local socket availability.`;
45
51
  if (holder.own) return `${kind} held by this checkout's own ${holder.server.mode} server (PID ${holder.pid}). Stop it with: ${stopInstruction(holder.server)}`;
52
+ const next = await firstFreePort(host, port + 1, context).catch(() => undefined);
46
53
  const who = holder.server
47
54
  ? `the ${holder.server.mode} server for ${holder.checkout} (PID ${holder.pid})`
48
55
  : `PID ${holder.pid} (${holder.command})${holder.cwd ? ` running in ${holder.cwd}` : ''}`;
@@ -19,7 +19,7 @@ export async function projectPnpmPin(root) {
19
19
  }
20
20
  async function probe(root, env, signal, store, timeoutMs) {
21
21
  try { return { version: await probePnpm(root, store, signal, env, timeoutMs) }; }
22
- catch (error) { if (signal?.aborted) throw error; return { reason: error.reason ?? error.message }; }
22
+ catch (error) { if (signal?.aborted) throw error; return { reason: error.reason ?? error.message, diagnostic: error.diagnostic }; }
23
23
  }
24
24
  export async function ensureProjectPnpm(root, options = {}) {
25
25
  if (options.store) return ensure(root, options);
@@ -71,5 +71,7 @@ async function ensure(root, { env = process.env, signal, report = () => {}, stor
71
71
  if (result.version === pin) { Object.assign(env, supplied); return env; }
72
72
  }
73
73
  const failed = result.version === undefined ? result.reason : `it reported ${JSON.stringify(result.version.slice(0, 64))}`;
74
- throw missing(`The verified pnpm ${pin} did not report the project version after 3 tries (${failed}). Run ${shellQuote(join(directory, 'pnpm'))} --version to inspect it, then rerun rer.`);
74
+ const message = `The verified pnpm ${pin} did not report the project version after 3 tries (${failed}).${result.diagnostic ? `\n${result.diagnostic}\n` : ' '}Run ${shellQuote(join(directory, 'pnpm'))} --version to inspect it, then rerun rer.`;
75
+ store.report(message);
76
+ throw missing(message);
75
77
  }
@@ -0,0 +1,35 @@
1
+ import { homedir } from 'node:os';
2
+ import { StringDecoder } from 'node:string_decoder';
3
+
4
+ // Vite and config plugins can write directly, before a build rejects. Redact
5
+ // at this child process's output boundary, including paths split across writes.
6
+ export function redactSiteText(text) {
7
+ return text.replaceAll(homedir(), '~')
8
+ .replace(/\/(?:Users|home)\/[^/\s:)'"<>]+/g, '~')
9
+ .replace(/[A-Za-z]:[\\/]+(?:Users|Documents and Settings)[\\/]+[^\\/\r\n:)'"<>]+/gi, '~');
10
+ }
11
+
12
+ export function redactSiteOutput() {
13
+ for (const stream of [process.stdout, process.stderr]) {
14
+ const write = stream.write.bind(stream), decoder = new StringDecoder('utf8');
15
+ let pending = '';
16
+ stream.write = (chunk, encoding, callback) => {
17
+ if (typeof encoding === 'function') { callback = encoding; encoding = undefined; }
18
+ pending += typeof chunk === 'string' ? chunk : decoder.write(chunk);
19
+ const end = pending.lastIndexOf('\n') + 1;
20
+ const ready = pending.slice(0, end); pending = pending.slice(end);
21
+ if (ready) return write(redactSiteText(ready), encoding, callback);
22
+ callback?.(); return true;
23
+ };
24
+ process.on('exit', () => { pending += decoder.end(); if (pending) write(redactSiteText(pending)); });
25
+ }
26
+ }
27
+
28
+ export function siteBuildDiagnostic(error) {
29
+ const message = String(error?.message ?? error);
30
+ const plugin = error?.plugin && !message.includes(error.plugin) ? `[plugin ${error.plugin}]\n` : '';
31
+ const file = error?.loc?.file ?? error?.id;
32
+ const location = file ? `${file}${error.loc?.line ? `:${error.loc.line}${error.loc.column !== undefined ? `:${error.loc.column}` : ''}` : ''}` : '';
33
+ const frame = error?.frame && !message.includes(error.frame) ? error.frame : '';
34
+ return [plugin + message, location && !message.includes(location) ? location : '', frame].filter(Boolean).join('\n');
35
+ }
@@ -14,6 +14,9 @@ import { createRequire } from 'node:module';
14
14
  import { dirname, join, resolve, sep } from 'node:path';
15
15
  import { pathToFileURL } from 'node:url';
16
16
  import { buildClients, writeSiteAsset } from './site-client.mjs';
17
+ import { redactSiteOutput, siteBuildDiagnostic } from './site-diagnostics.mjs';
18
+
19
+ redactSiteOutput();
17
20
 
18
21
  const [root, entry, output, config, clientJSON] = process.argv.slice(2);
19
22
  const fail = message => { process.stderr.write(`${message}\n`); process.exit(2); };
@@ -38,7 +41,7 @@ try {
38
41
  ssr: resolve(root, entry), outDir: server, emptyOutDir: true, copyPublicDir: false, ssrEmitAssets: true,
39
42
  cssCodeSplit: false, assetsDir: ASSETS, sourcemap: false, minify: false, reportCompressedSize: false, watch: null,
40
43
  },
41
- }).catch(error => fail(`The run site entry ${entry} did not build: ${error.message.split('\n')[0]}${config ? '' : ' If its pages need Vite plugins (a docs virtual module, a CSS toolchain), name a site Vite config: site: { config } in the command adapter.'}`));
44
+ }).catch(error => fail(`The run site entry ${entry} did not build: ${siteBuildDiagnostic(error)}${config ? '' : '\nIf its pages need Vite plugins (a docs virtual module, a CSS toolchain), name a site Vite config: site: { config } in the command adapter.'}`));
42
45
  const outputs = (Array.isArray(built) ? built : [built]).flatMap(result => result.output ?? []);
43
46
  const chunk = outputs.find(item => item.type === 'chunk' && item.isEntry);
44
47
  if (!chunk) fail(`The run site entry ${entry} produced no module.`);
package/cli/site.mjs CHANGED
@@ -8,6 +8,7 @@ import { readDevKey } from './dev-identity.mjs';
8
8
  import { shared } from './shared.mjs';
9
9
  import { unpublishedIdentities } from './site-private.mjs';
10
10
  import { privateSitePaths } from './site-inputs.mjs';
11
+ import { redactSiteText } from './site-diagnostics.mjs';
11
12
 
12
13
  // The run site: a seed's static marketing pages, /run and docs, built from
13
14
  // edition identity with no installation data. A seed opts in with
@@ -62,6 +63,10 @@ export async function buildSite(project, store, signal) {
62
63
  const code = await readFile(`${staging}.refusal`, 'utf8').catch(() => '');
63
64
  if (code === 'site_private_input') throw new RerError(code, 'Run site client imports a private installation file or a file outside the seed and its declared dependencies.', 4);
64
65
  if (code === 'site_client_server_import') throw new RerError(code, 'Run site client imports a server-only module. Use browser dependencies in every reachable island module.', 4);
66
+ // Command capture appends its absolute log path after the child output.
67
+ const { approvedFailure, commandFailure } = await shared('failures.mjs');
68
+ const diagnostic = approvedFailure(error);
69
+ if (diagnostic) throw commandFailure(redactSiteText(diagnostic.message), diagnostic);
65
70
  throw error;
66
71
  }
67
72
  const checked = await checkSite(staging, {root:project.root, ...await privateValues(project)});
@@ -1,5 +1,17 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.2.17
4
+
5
+ - Container builds check the app's reachable runtime imports from Wrangler main and fresh server output. Dormant source files and ordinary calls containing cloudflare-looking strings no longer fail the boundary; reachable Node-engine restrictions remain.
6
+ - Site builds preserve complete plugin diagnostics and source locations while redacting absolute home paths.
7
+ - Fresh Worker deploys wait for the uploaded version and secrets before the owner claim. A matching fresh setup token that has not propagated and a fresh workers.dev 1042/404 readback retry inside the existing upload deadline; wrong tokens and unrelated 404s still fail.
8
+ - Demo removal accepts empty successful DELETE responses, so removing a recorded demo with its Custom Domain completes in one command.
9
+ - Local dev, setup and demo inspect the started process tree with nonblocking macOS socket queries. Slow or failed diagnostics retry within one 15-second bound; unresolved ownership refuses with the command and bound instead of falsely naming another owner.
10
+
11
+ ### Upgrade notes
12
+
13
+ - Update the exact rer package pin, or refresh vendored operating commands, to receive these changes. Rebuild container images for the import-graph boundary. Existing local data and seed-owned application source are unchanged.
14
+
3
15
  ## 0.2.16
4
16
 
5
17
  - A hosted demo's container stays up until the visitor's 15 idle minutes or the demo's hour, however long the visitor goes without a request. Before, the platform stopped it about three minutes after the last request, the next request got a Cloudflare 500, and its place stayed held. A container that is gone anyway (stopped by the platform, or failing once stopped) now ends its demo, frees its place at once and answers the ended page (410, with "Start a new demo").
@@ -1,41 +1,77 @@
1
- import { readdir, readFile } from 'node:fs/promises';
2
- import { join, relative, sep } from 'node:path';
3
- import { createRequire } from 'node:module';
4
- import { pathToFileURL } from 'node:url';
5
- export async function checkBoundary(root, { engine='node', cacheContract }={}) {
6
- const ts=await import(pathToFileURL(createRequire(join(root,'package.json')).resolve('typescript'))), failures=[];
7
- async function walk(directory){
8
- for(const item of await readdir(directory,{withFileTypes:true})){
9
- const path=join(directory,item.name);if(item.isDirectory()){await walk(path);continue;}if(!/\.[cm]?[jt]sx?$/.test(item.name))continue;
10
- const name=relative(root,path),tree=ts.createSourceFile(path,await readFile(path,'utf8'),ts.ScriptTarget.Latest,true);
11
- const cache=cacheContract==='lamp-retained-v1'&&/(?:^|\/)platform\/blobs\/snapshot-cache\.ts$/.test(name);
12
- const bindings=/(?:^|\/)platform\/bindings\.server\.ts$/.test(name);
1
+ import { relative, sep } from 'node:path';
2
+ import { configuredEntry, walkImportGraph } from './import-graph.mjs';
3
+
4
+ export async function checkBoundary(root, { engine='node', cacheContract, entries, emittedFiles, outputRoot }={}) {
5
+ const failures = [];
6
+ if (Boolean(emittedFiles) !== Boolean(outputRoot)) throw new Error('Emitted files and output root must be supplied together');
7
+ const roots = entries ?? [await configuredEntry(root)];
8
+ if (!Array.isArray(roots) || !roots.length) throw new Error('At least one app entry is required');
9
+ const graph = await walkImportGraph(root, {
10
+ entries: roots,
11
+ emittedFiles,
12
+ outputRoot,
13
+ onFile({ ts, tree, path }) {
14
+ const name = relative(root, path).split(sep).join('/');
15
+ const cache = cacheContract === 'lamp-retained-v1' && /(?:^|\/)platform\/blobs\/snapshot-cache\.ts$/.test(name);
16
+ const bindings = /(?:^|\/)platform\/bindings\.server\.ts$/.test(name);
13
17
  // The demo guard an exported seed carries reads RER_DEMO from env: that one
14
18
  // file, and only its unrenamed `env` import.
15
- const demo=name.split(sep).join('/')==='src/modules/demo/index.mjs';
16
- const fail=api=>failures.push({path:name,api}),localNames=new Set();
17
- function names(node){if((ts.isVariableDeclaration(node)||ts.isFunctionDeclaration(node)||ts.isImportSpecifier(node))&&node.name&&ts.isIdentifier(node.name))localNames.add(node.name.text);ts.forEachChild(node,names);}names(tree);
18
- function visit(node){
19
- if((ts.isImportDeclaration(node)||ts.isExportDeclaration(node))&&node.moduleSpecifier&&ts.isStringLiteral(node.moduleSpecifier)){
20
- const module=node.moduleSpecifier.text;
21
- if(module.startsWith('cloudflare:')){
22
- const imports=node.importClause?.namedBindings;
23
- const allowed=module==='cloudflare:workers'&&imports&&ts.isNamedImports(imports)&&imports.elements.every(element=>(demo&&!node.importClause.name&&!element.propertyName&&element.name.text==='env')||(bindings&&(element.propertyName??element.name).text==='env')||(cache&&(element.propertyName??element.name).text==='DurableObject'));
24
- if(!allowed&&!(engine==='workerd'&&module==='cloudflare:workers'))fail('Cloudflare import outside binding/cache contract');
19
+ const demo = name === 'src/modules/demo/index.mjs';
20
+ const fail = api => failures.push({ path:name, api }), localNames = new Set();
21
+ function collectNames(node) {
22
+ if ((ts.isVariableDeclaration(node) || ts.isFunctionDeclaration(node) || ts.isImportSpecifier(node)) && node.name && ts.isIdentifier(node.name)) localNames.add(node.name.text);
23
+ ts.forEachChild(node, collectNames);
24
+ }
25
+ collectNames(tree);
26
+ function visit(node) {
27
+ const runtimeDeclaration = declaration => {
28
+ if (ts.isImportDeclaration(declaration)) {
29
+ const clause = declaration.importClause;
30
+ if (!clause) return true;
31
+ if (clause.isTypeOnly) return false;
32
+ if (clause.name) return true;
33
+ const bindings = clause.namedBindings;
34
+ return !bindings || ts.isNamespaceImport(bindings) || bindings.elements.length === 0 || bindings.elements.some(element => !element.isTypeOnly);
35
+ }
36
+ if (ts.isExportDeclaration(declaration)) {
37
+ if (declaration.isTypeOnly) return false;
38
+ const clause = declaration.exportClause;
39
+ return !clause || !ts.isNamedExports(clause) || clause.elements.length === 0 || clause.elements.some(element => !element.isTypeOnly);
40
+ }
41
+ return false;
42
+ };
43
+ if ((ts.isImportDeclaration(node) || ts.isExportDeclaration(node)) && runtimeDeclaration(node) && node.moduleSpecifier && ts.isStringLiteralLike(node.moduleSpecifier)) {
44
+ const module = node.moduleSpecifier.text;
45
+ if (module.startsWith('cloudflare:')) {
46
+ const imports = node.importClause?.namedBindings;
47
+ const runtimeImports = imports && ts.isNamedImports(imports) ? imports.elements.filter(element => !element.isTypeOnly) : [];
48
+ const allowed = module === 'cloudflare:workers' && runtimeImports.length > 0 && imports && ts.isNamedImports(imports) && runtimeImports.every(element =>
49
+ (demo && !node.importClause.name && !element.propertyName && element.name.text === 'env') ||
50
+ (bindings && (element.propertyName ?? element.name).text === 'env') ||
51
+ (cache && (element.propertyName ?? element.name).text === 'DurableObject'));
52
+ if (!allowed && !(engine === 'workerd' && module === 'cloudflare:workers')) fail('Cloudflare import outside binding/cache contract');
25
53
  }
26
54
  }
27
- if(ts.isCallExpression(node)&&node.arguments.some((arg,index)=>ts.isStringLiteral(arg)&&arg.text.startsWith('cloudflare:')&&!(engine==='workerd'&&node.expression.kind===ts.SyntaxKind.ImportKeyword&&index===0&&arg.text==='cloudflare:workers')))fail('Dynamic Cloudflare import');
28
- if(ts.isIdentifier(node)&&engine!=='workerd'&&['WebSocketPair','passThroughOnException','timingSafeEqual','HTMLRewriter'].includes(node.text))fail(node.text);
29
- if(ts.isCallExpression(node)&&ts.isIdentifier(node.expression)&&node.expression.text==='email'&&!localNames.has('email'))fail('email()');
30
- if(ts.isPropertyAccessExpression(node)&&((node.expression.getText(tree)==='caches'&&node.name.text==='default')||(node.expression.getText(tree)==='request'&&node.name.text==='cf')))fail(node.getText(tree));
31
- if(ts.isElementAccessExpression(node)&&ts.isStringLiteral(node.argumentExpression)&&((node.expression.getText(tree)==='caches'&&node.argumentExpression.text==='default')||(node.expression.getText(tree)==='request'&&node.argumentExpression.text==='cf')))fail(node.getText(tree));
32
- if(engine!=='workerd'&&ts.isHeritageClause(node)&&node.token===ts.SyntaxKind.ExtendsKeyword&&node.types.some(type=>/DurableObject/.test(type.expression.getText(tree)))&&!cache)fail('New Durable Object');
33
- ts.forEachChild(node,visit);
55
+ if (ts.isImportEqualsDeclaration(node) && !node.isTypeOnly && ts.isExternalModuleReference(node.moduleReference) && ts.isStringLiteralLike(node.moduleReference.expression) && node.moduleReference.expression.text.startsWith('cloudflare:')) fail('Dynamic Cloudflare import');
56
+ if (ts.isCallExpression(node) && node.arguments[0] && ts.isStringLiteralLike(node.arguments[0])) {
57
+ const module = node.arguments[0].text;
58
+ const isDynamicImport = node.expression.kind === ts.SyntaxKind.ImportKeyword;
59
+ const isRequire = ts.isIdentifier(node.expression) && node.expression.text === 'require';
60
+ if ((isDynamicImport || isRequire) && module.startsWith('cloudflare:') && !(engine === 'workerd' && isDynamicImport && module === 'cloudflare:workers')) fail('Dynamic Cloudflare import');
61
+ }
62
+ if (ts.isIdentifier(node) && engine !== 'workerd' && ['WebSocketPair','passThroughOnException','timingSafeEqual','HTMLRewriter'].includes(node.text)) fail(node.text);
63
+ if (ts.isCallExpression(node) && ts.isIdentifier(node.expression) && node.expression.text === 'email' && !localNames.has('email')) fail('email()');
64
+ if (ts.isPropertyAccessExpression(node) && ((node.expression.getText(tree) === 'caches' && node.name.text === 'default') || (node.expression.getText(tree) === 'request' && node.name.text === 'cf'))) fail(node.getText(tree));
65
+ if (ts.isElementAccessExpression(node) && ts.isStringLiteral(node.argumentExpression) && ((node.expression.getText(tree) === 'caches' && node.argumentExpression.text === 'default') || (node.expression.getText(tree) === 'request' && node.argumentExpression.text === 'cf'))) fail(node.getText(tree));
66
+ if (engine !== 'workerd' && ts.isHeritageClause(node) && node.token === ts.SyntaxKind.ExtendsKeyword && node.types.some(type => /DurableObject/.test(type.expression.getText(tree))) && !cache) fail('New Durable Object');
67
+ ts.forEachChild(node, visit);
34
68
  }
35
69
  visit(tree);
36
- }
37
- }
38
- await walk(join(root,'src'));
39
- if(failures.length){const cause=new Error('Container import boundary failed');cause.failures=failures;throw cause;}
40
- return {engine,ok:true};
70
+ },
71
+ });
72
+ failures.push(...graph.failures);
73
+ if (failures.length) { const cause = new Error('Container import boundary failed'); cause.failures = failures; throw cause; }
74
+ return { engine, ok:true };
41
75
  }
76
+
77
+ export { configuredEntry };