@runeditrun/rer 0.2.18 → 0.2.20

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 (113) hide show
  1. package/README.md +7 -7
  2. package/cli/README.md +22 -21
  3. package/cli/adapter.mjs +5 -0
  4. package/cli/container.mjs +7 -2
  5. package/cli/contract.mjs +6 -4
  6. package/cli/delivery-declarations.mjs +36 -0
  7. package/cli/demo-host.mjs +2 -2
  8. package/cli/demo.mjs +16 -5
  9. package/cli/deploy-command.mjs +4 -1
  10. package/cli/deploy.mjs +4 -1
  11. package/cli/detached-server.mjs +2 -2
  12. package/cli/dev-identity.mjs +1 -1
  13. package/cli/export.mjs +1 -1
  14. package/cli/inspect.mjs +17 -15
  15. package/cli/lifecycle.mjs +16 -49
  16. package/cli/listen.mjs +31 -0
  17. package/cli/local.mjs +12 -18
  18. package/cli/main.mjs +7 -0
  19. package/cli/managed-server.mjs +26 -44
  20. package/cli/output.mjs +1 -1
  21. package/cli/parse.mjs +8 -7
  22. package/cli/port.mjs +20 -20
  23. package/cli/production-auth.mjs +38 -0
  24. package/cli/server-proxy.mjs +2 -6
  25. package/cli/server-registry.mjs +7 -2
  26. package/cli/setup-input.mjs +3 -0
  27. package/cli/signin.mjs +41 -0
  28. package/global/CHANGELOG.md +18 -0
  29. package/global/README.md +7 -7
  30. package/global/cli/install.mjs +4 -28
  31. package/global/cli/path-link.mjs +25 -21
  32. package/global/launch/dev.mjs +1 -1
  33. package/global/launch/facts.mjs +11 -14
  34. package/global/launch/failure.mjs +20 -0
  35. package/global/launch/render.mjs +2 -6
  36. package/global/launch/worker.mjs +3 -3
  37. package/global/main.mjs +3 -3
  38. package/modules/container/build.mjs +2 -2
  39. package/modules/container/demo-egress.mjs +35 -0
  40. package/modules/container/worker-engine.mjs +3 -3
  41. package/modules/d1-transport/index.d.mts +9 -0
  42. package/modules/d1-transport/index.mjs +124 -0
  43. package/modules/delivery-producer/bindings.mjs +140 -0
  44. package/modules/delivery-producer/declarations.mjs +49 -0
  45. package/modules/demo/README.md +34 -0
  46. package/modules/demo/effects.mjs +73 -0
  47. package/modules/demo/index.d.mts +15 -0
  48. package/modules/demo/index.mjs +63 -0
  49. package/modules/demo/label.d.mts +2 -0
  50. package/modules/demo/label.mjs +24 -0
  51. package/modules/demo/responses.mjs +26 -0
  52. package/modules/demo/sockets.mjs +13 -0
  53. package/modules/demo/vite.d.mts +2 -0
  54. package/modules/demo/vite.mjs +19 -0
  55. package/modules/demo-host/README.md +5 -1
  56. package/modules/demo-host/container.mjs +4 -4
  57. package/modules/demo-host/pages.mjs +1 -1
  58. package/modules/demo-host/worker.mjs +1 -1
  59. package/modules/deploy-kit/README.md +2 -0
  60. package/modules/deploy-kit/owner-cli.mjs +5 -2
  61. package/modules/deploy-kit/owner-signin.mjs +34 -0
  62. package/modules/deploy-kit/owner.mjs +2 -2
  63. package/modules/deploy-kit/r2-objects.mjs +8 -5
  64. package/modules/job-queue/storage.mjs +4 -1
  65. package/modules/mail/README.md +62 -87
  66. package/modules/mail/src/adapters/rer/contract.mjs +22 -0
  67. package/modules/mail/src/adapters/rer/index.mjs +30 -0
  68. package/modules/mail/src/core/transport.mjs +1 -1
  69. package/modules/mail/src/core/validation.mjs +6 -3
  70. package/modules/mail/src/development/index.d.mts +1 -6
  71. package/modules/mail/src/development/index.mjs +3 -16
  72. package/modules/mail/src/sender.mjs +2 -1
  73. package/modules/operator-auth/README.md +53 -9
  74. package/modules/operator-auth/accounts/email-change.mjs +34 -0
  75. package/modules/operator-auth/accounts/owner-signin.mjs +50 -0
  76. package/modules/operator-auth/accounts/schema/owner-signin.sql +10 -0
  77. package/modules/operator-auth/csp.mjs +1 -2
  78. package/modules/operator-auth/dev/cli-session.mjs +32 -0
  79. package/modules/operator-auth/dev/index.mjs +18 -11
  80. package/modules/operator-auth/identity/email-link/confirmation.mjs +3 -3
  81. package/modules/operator-auth/identity/email-link/index.mjs +4 -1
  82. package/modules/operator-auth/index.d.mts +8 -2
  83. package/modules/operator-auth/owner-signin-grant.mjs +10 -0
  84. package/modules/operator-auth/page.mjs +1 -1
  85. package/modules/operator-auth/routes.mjs +11 -1
  86. package/modules/operator-auth/service.mjs +4 -0
  87. package/modules/operator-auth/signin/index.d.mts +1 -1
  88. package/modules/portability/README.md +6 -0
  89. package/modules/portability/catalogue.mjs +1 -1
  90. package/modules/portability/copies.mjs +17 -2
  91. package/modules/portability/index.mjs +2 -0
  92. package/modules/portability/provider-data.mjs +1 -1
  93. package/modules/portability/storage.mjs +5 -1
  94. package/onboarding/README.md +8 -11
  95. package/onboarding/adapter-auth.mjs +6 -38
  96. package/onboarding/adapter-labels.mjs +1 -1
  97. package/onboarding/adapter.mjs +8 -11
  98. package/onboarding/app-fetch.mjs +1 -1
  99. package/onboarding/dependency-approval.mjs +5 -15
  100. package/onboarding/dev-identity.mjs +1 -1
  101. package/onboarding/operator-install.mjs +6 -7
  102. package/onboarding/operator-installer.mjs +5 -5
  103. package/onboarding/operator-prerequisites.mjs +4 -5
  104. package/onboarding/port-probe.mjs +38 -0
  105. package/onboarding/process-diagnostics.mjs +36 -20
  106. package/onboarding/server.mjs +1 -1
  107. package/onboarding/ui/app.js +1 -1
  108. package/operations/README.md +3 -0
  109. package/package.json +4 -2
  110. package/scripts/publish-check/public_hygiene.py +31 -10
  111. package/modules/mail/src/development/viewer-html.mjs +0 -42
  112. package/modules/mail/src/development/viewer.mjs +0 -72
  113. package/modules/operator-auth/dev/badge.mjs +0 -44
@@ -92,9 +92,14 @@ export async function serverRegistry(root,{env=process.env,memory=availableMemor
92
92
  if(info.isSymbolicLink()||!info.isDirectory()||info.uid!==process.getuid())throw refuse('Server registry directory is foreign or linked. Preserve it and choose an owned RER_SERVERS_DIR.');
93
93
  await chmod(path,0o700);
94
94
  }
95
- try{const info=await lstat(join(directory,'state.json'));if(!info.isFile()||info.isSymbolicLink()||info.nlink!==1||info.uid!==process.getuid())throw refuse('Server registry state is foreign or linked. Preserve it for inspection.');await chmod(join(directory,'state.json'),0o600);}catch(error){if(error.code!=='ENOENT')throw error;}
96
95
  }
97
- const change=async action=>{await (secured??=secure());return transaction(directory,lifetime.info,action);};
96
+ async function validateState(path){
97
+ // Hold the transaction lock before inspecting the state inode. A concurrent
98
+ // atomic replacement can otherwise make lstat report the retired inode with
99
+ // zero links, even though both files are regular and owned by this user.
100
+ try{const info=await lstat(path);if(!info.isFile()||info.isSymbolicLink()||info.nlink!==1||info.uid!==process.getuid())throw refuse('Server registry state is foreign or linked. Preserve it for inspection.');await chmod(path,0o600);}catch(error){if(error.code!=='ENOENT')throw error;}
101
+ }
102
+ const change=async action=>{await (secured??=secure());return transaction(directory,lifetime.info,action,{validateState});};
98
103
  let owned, control, closing;
99
104
  async function inspect(state) {
100
105
  const kept=[];
@@ -47,6 +47,9 @@ export async function setupInput(project, options, vars, signal, { origin } = {}
47
47
  const savedEmail = vars[names.email];
48
48
  if (!answers[names.email] && savedEmail) answers[names.email] = savedEmail;
49
49
  if (hasOwnerName) answers[names.name] ??= seed.model.fields[names.name].default ?? 'Owner';
50
+ // Saved secrets satisfy readiness and must also reach the seed's owner hook
51
+ // when setup resumes. Keep them in private input, never the saved safe answers.
52
+ for(const field of seed.manifest.fields)if(field.kind==='secret'&&!answers[field.name]&&typeof vars[field.name]==='string')answers[field.name]=vars[field.name];
50
53
  if(process.env[localDefaultsVariable]==='1')applyLocalDefaults(seed,answers,vars,origin);
51
54
  const entered=seed.manifest.fields.filter(field=>field.kind==='secret'&&field.input==='entered');
52
55
  for(const field of entered)if(!answers[field.name]&&!vars[field.name]&&process.env[field.name])answers[field.name]=process.env[field.name];
package/cli/signin.mjs ADDED
@@ -0,0 +1,41 @@
1
+ import { declarationFor } from './deploy-command.mjs';
2
+ import { deployEnvironment, deployAgain } from './deploy-inputs.mjs';
3
+ import { hookState } from './deploy-hook-record.mjs';
4
+ import { missing, refuse, RerError } from './errors.mjs';
5
+ import { shared } from './shared.mjs';
6
+ import { main as owner } from '../modules/deploy-kit/owner-cli.mjs';
7
+ import { deployDiagnostic, providerRefusal } from '../modules/deploy-kit/diagnostics.mjs';
8
+
9
+ // rer signin: the owner's way into a hosted install without mail. With the same
10
+ // Cloudflare credentials that deployed it, rer writes a one-time grant (only its
11
+ // SHA-256) into the deployment's auth D1 and prints the link once to this terminal.
12
+ // It never reaches a log, receipt or the deploy record; the Worker admits it only
13
+ // for the recorded owner, once, within 15 minutes, and logs its use.
14
+ export async function signinCommand(project, options, out, { fetch } = {}) {
15
+ if (project.profile !== 'first-party' || typeof project.adapter?.deploy !== 'function') throw refuse('rer signin needs a seed with an owner-account deploy declaration.');
16
+ const root = project.root, environment = options.environment ?? 'production', again = deployAgain(options).replace(/^rer deploy/, 'rer signin');
17
+ const { path, value: declaration } = await declarationFor(root, project.adapter);
18
+ const { env } = await deployEnvironment({ root, options, declaration, localOnlyKeys: project.adapter.localOnlyKeys ?? [] });
19
+ if (['CLOUDFLARE_ACCOUNT_ID', 'CLOUDFLARE_API_TOKEN'].some(name => !env[name]?.trim())) throw missing('rer signin uses the Cloudflare credentials that deployed this environment: set CLOUDFLARE_ACCOUNT_ID and CLOUDFLARE_API_TOKEN in the environment or an --env-file, then run it again.');
20
+ const { createRedaction, credentialValues } = await shared('redaction.mjs');
21
+ const redaction = createRedaction();
22
+ redaction.addSecrets([...credentialValues(env), env.CLOUDFLARE_ACCOUNT_ID]);
23
+ const state = await hookState(root, environment);
24
+ let result;
25
+ try { result = await owner(['signin', '--declaration', path, '--environment', environment, ...(state ? ['--state', state] : [])], env, { cwd: root, localOnlyKeys: project.adapter.localOnlyKeys ?? [], ...(fetch ? { fetch } : {}) }); }
26
+ catch (error) { throw failure(error, { environment, again, redact: redaction.redact }); }
27
+ const minutes = Math.round((result.expiresAt - Date.now()) / 60000);
28
+ if (!options.json) out.human(`One-time sign-in link for the owner ${result.email}. It works once, only for that owner, and expires in ${minutes} minutes. rer does not save it; open it now:\n${result.link}`);
29
+ return { owner: result.email, expiresAt: result.expiresAt, ...(options.json ? { link: result.link } : {}) };
30
+ }
31
+
32
+ function failure(error, { environment, again, redact }) {
33
+ const code = deployDiagnostic(error, redact)?.code ?? error.message;
34
+ if (code === 'missing-owner-receipt') return refuse(`This checkout has no completed rer deployment of the ${environment} environment. Run rer signin from the checkout that deployed it (rer deploy --environment ${environment}).`);
35
+ if (code === 'owned-resource-replaced') return refuse(`The ${environment} Worker is no longer the one this checkout deployed, so rer will not sign anyone in to it. Run rer deploy --environment ${environment} first.`);
36
+ if (code === 'owner-signin-unsupported') return refuse(`The ${environment} deployment's database has no owner sign-in table. Update the seed to a rer that includes operator-auth's accounts/schema/owner-signin.sql migration, run rer deploy --environment ${environment}, then ${again}.`);
37
+ if (code === 'owner-signin-ambiguous') return refuse(`More than one D1 database of the ${environment} deployment holds an owner sign-in table, so rer cannot tell which one signs in the owner. Nothing was written.`);
38
+ if (code === 'owner-not-claimed') return refuse(`No owner has claimed the ${environment} deployment yet. Run rer deploy --environment ${environment}: it claims the owner. Nothing was written.`);
39
+ const refusal = providerRefusal(error, redact);
40
+ return new RerError('command_failed', `rer signin failed: ${/^[a-zA-Z0-9_-]{1,120}$/.test(code) ? redact(code) : 'signin-failed'}${refusal ? ` (${refusal})` : ''}. No sign-in link was issued; run ${again} again.`, 1);
41
+ }
@@ -1,5 +1,23 @@
1
1
  # Changelog
2
2
 
3
+ ## Unreleased
4
+
5
+ - Concurrent checkout launches validate server registry state under its transaction lock, avoiding a false foreign-file refusal during atomic updates.
6
+ - A checkout behind the front door serves its run-site preview above the app's assigned port instead of trying privileged ports.
7
+ - Hosted demo image builds use the adapter's declared deploy file, including names other than `deploy.json`.
8
+ - `rer deploy --remove --yes` empties small R2 buckets whose object listing omits pagination metadata. Ambiguous full pages and inconsistent cursors still stop removal.
9
+ - Setup uses a mapped owner-name field and its declared default without requiring a second `displayName`. `--owner-name` supplies that mapped field.
10
+ - Import-kit and portability encode mixed-script D1 bindings and count their UTF-8 and RPC sizes without splitting atomic transactions. Retained import plans are read in bounded pages. Source adopters must refresh both modules and include their shared `d1-transport` dependency before removing an edition's transport wrapper.
11
+
12
+ ## 0.2.19
13
+
14
+ - `rer signin [--environment NAME]` uses the credentials that deployed a seed to print a one-time owner sign-in link, valid for 15 minutes. It requires no email delivery, refuses non-owners and used or expired links, and records successful use in the auth audit log.
15
+ - A freshly signed-in owner can correct their sign-in email in Operator access. The global command installs the checkout's pinned rer package before running `signin`.
16
+
17
+ ### Upgrade notes
18
+
19
+ - Update the exact rer package pin or refresh vendored commands and operator-auth. Apply `modules/operator-auth/accounts/schema/owner-signin.sql` as a new numbered migration, then redeploy. The reference seed includes the migration and README instructions. Run `rer signin` from the checkout that deployed the environment with its Cloudflare credentials.
20
+
3
21
  ## 0.2.17
4
22
 
5
23
  - 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.
package/global/README.md CHANGED
@@ -6,7 +6,7 @@ checkout always gets the behaviour it was built and tested with.
6
6
 
7
7
  ## Install
8
8
 
9
- Any one of these leaves `rer` on your PATH:
9
+ Install `rer` with one of these commands:
10
10
 
11
11
  ```sh
12
12
  npx -y @runeditrun/rer cli install # user install in ~/.local/share/rer, launcher ~/.local/bin/rer
@@ -23,9 +23,10 @@ the package's own `rer cli install`. A failed or refused download leaves the
23
23
  version you had selected.
24
24
 
25
25
  No installer edits shell profiles. The canonical launcher is `~/.local/bin/rer`.
26
- When that directory is absent from PATH, installation also links it into an
27
- existing writable, user-owned PATH directory if available, preserving foreign
28
- commands. Otherwise it prints one activation line to run in the current shell:
26
+ Installation creates that directory if needed and never writes into another
27
+ PATH directory. It names any existing `rer` elsewhere on PATH and leaves it
28
+ unchanged. If `~/.local/bin` is absent from PATH or another command shadows it,
29
+ it prints one activation line to run in the current shell:
29
30
  `export PATH="$HOME/.local/bin:$PATH"` (with fish syntax for fish). The launcher runs `node` from
30
31
  PATH, so a Node upgrade does not break it. A re-run with the same build changes
31
32
  nothing; a different build is installed beside the current one, the previous one
@@ -99,7 +100,6 @@ RER_SIGNED_IN owner@cradle.test
99
100
  RER_OPEN http://127.0.0.1:8787/__dev/key#...
100
101
  ```
101
102
 
102
- or `RER_SIGN_IN <outbox>` for a seed that signs in through its development outbox.
103
103
  A seed that declares a run site prints one more line after those,
104
104
  `RER_SITE http://127.0.0.1:8788`: a local preview of its static run site (its
105
105
  marketing homepage, `/run` and docs) on the first free port above the app's. It
@@ -262,7 +262,7 @@ line that still names it. A later refresh is a version bump:
262
262
  | `@runeditrun/rer/modules/portability`, `/catalogue`, `/cli`, `/cli-options`, `/files`, `/format`, `/http`, `/import`, `/journal`, `/legacy-safety`, `/migration-history`, `/storage` | portability module | `scripts/base-cli/modules/portability/*.mjs` |
263
263
  | `@runeditrun/rer/modules/release/export`, `/publication-scan`, `/source`, `/signature` | release export, publication scan and shared signature verifier | `scripts/base-cli/modules/release/*.mjs` |
264
264
  | `@runeditrun/rer/modules/deploy-kit/owner`, `/owner-cli`, `/prepare`, `/provider` | deploy kit | `scripts/base-cli/modules/deploy-kit/*.mjs` |
265
- | `@runeditrun/rer/modules/operator-auth`, `/dev`, `/dev/badge`, `/dev-admission`, `/accounts/native`, `/identity/email-link` | operator auth | `scripts/base-cli/modules/operator-auth/index.mjs`, `dev/index.mjs`, `dev/badge.mjs`, `dev-admission.mjs`, `accounts/native.mjs`, `identity/email-link/index.mjs` |
265
+ | `@runeditrun/rer/modules/operator-auth`, `/dev`, `/dev-admission`, `/accounts/native`, `/identity/email-link` | operator auth | `scripts/base-cli/modules/operator-auth/index.mjs`, `dev/index.mjs`, `dev-admission.mjs`, `accounts/native.mjs`, `identity/email-link/index.mjs` |
266
266
  | `@runeditrun/rer/modules/erasure/fence` | erasure fence statements | `scripts/base-cli/modules/erasure/fence.mjs` |
267
267
  | `@runeditrun/rer/modules/owner-claim`, `/modules/dependencies` | owner claim, dependency links | `scripts/base-cli/modules/owner-claim/index.mjs`, `dependencies/index.mjs` |
268
268
  | `@runeditrun/rer/internal/PATH` | any other package file, for tests | `scripts/base-cli/PATH`, and `scripts/base-onboarding/PATH` with PATH under `onboarding/` |
@@ -273,7 +273,7 @@ In a checkout, every verb runs that checkout's own pinned `rer`; `rer VERB --hel
273
273
  there lists its flags:
274
274
 
275
275
  `install`, `setup`, `dev`, `preview`, `build`, `verify`, `reset --yes`, `deploy`,
276
- `export`, `import`, `outbox`, `doctor`, `describe`, `check`, `release`,
276
+ `export`, `import`, `doctor`, `describe`, `check`, `release`,
277
277
  `release sign`, `release receipt` and `release keygen`.
278
278
 
279
279
  Anywhere: `rer list [DIR]`, `rer cli status|install|uninstall`, `rer update`,
@@ -2,12 +2,11 @@
2
2
  // version directory, switch `current` atomically, keep the previous version,
3
3
  // prune older ones and write the launcher. Never edits shell profiles.
4
4
  import { randomUUID } from 'node:crypto';
5
- import { access, constants, chmod, cp, mkdir, readdir, readlink, realpath, rename, rm, symlink, writeFile } from 'node:fs/promises';
5
+ import { chmod, cp, mkdir, readdir, readlink, realpath, rename, rm, symlink, writeFile } from 'node:fs/promises';
6
6
  import { basename, join, resolve } from 'node:path';
7
7
  import { ownManifest, ownRoot, packageDigest, packageFileList } from '../package-info.mjs';
8
8
  import { installRecord, launcherOwner, launcherScript, layout } from './layout.mjs';
9
- import { exposeLauncher } from './path-link.mjs';
10
- import { shellQuote } from '../../cli/node-runtime-cache.mjs';
9
+ import { pathAdvice } from './path-link.mjs';
11
10
  import { runningDoor } from '../../cli/door.mjs';
12
11
 
13
12
  async function readLink(path) { try { return await readlink(path); } catch (error) { if (['ENOENT', 'EINVAL'].includes(error.code)) return undefined; throw error; } }
@@ -40,22 +39,6 @@ async function writeLauncher(paths) {
40
39
  await rename(temporary, paths.launcher);
41
40
  }
42
41
 
43
- export async function pathAdvice(paths, env = process.env) {
44
- const entries = (env.PATH ?? '').split(':').filter(Boolean).map(entry => resolve(entry));
45
- for (const directory of entries) {
46
- if (directory === resolve(paths.bin)) return undefined;
47
- const candidate = join(directory, 'rer');
48
- try {
49
- await access(candidate, constants.X_OK);
50
- const found = await launcherOwner(await realpath(candidate));
51
- if (found.state === 'ours' && found.root === paths.data) return undefined;
52
- break; // This command shadows the canonical launcher, even if it is later on PATH.
53
- } catch (error) { if (!['ENOENT', 'EACCES', 'ENOTDIR'].includes(error.code)) throw error; }
54
- }
55
- if (basename(env.SHELL ?? '') === 'fish') return `Activate rer in this shell:\n set -gx PATH ${shellQuote(paths.bin)} $PATH`;
56
- return `Activate rer in this shell:\n export PATH=${shellQuote(paths.bin)}:"$PATH"`;
57
- }
58
-
59
42
  export async function install() {
60
43
  const paths = layout(), manifest = ownManifest(), digest = await packageDigest(ownRoot);
61
44
  const name = `${manifest.version}-${digest.slice(0, 8)}`, channel = process.env.RER_INSTALL_CHANNEL === 'script' ? 'script' : 'npx';
@@ -69,7 +52,7 @@ export async function install() {
69
52
  let present = false;
70
53
  try { present = await packageDigest(join(paths.versions, name)) === digest; } catch (error) { if (error.code !== 'ENOENT') throw error; }
71
54
  if (present && current === join('versions', name) && launcher.text === script && record?.channel === channel) {
72
- const advice = await activation(paths, record);
55
+ const advice = await pathAdvice(paths);
73
56
  process.stdout.write(`rer ${manifest.version} is already current (${join(paths.versions, name)}). Nothing changed.\n${advice ? advice + '\n' : ''}`);
74
57
  return 0;
75
58
  }
@@ -83,14 +66,7 @@ export async function install() {
83
66
  for (const entry of await readdir(paths.versions)) if (!keep.has(entry) && !entry.startsWith('.staging-')) await rm(join(paths.versions, entry), { recursive: true, force: true });
84
67
  await writeFile(paths.record, JSON.stringify({ channel, links: record?.links ?? [], previous: previousName && keep.has(previousName) ? previousName : null }, null, 2) + '\n', { mode: 0o644 });
85
68
  if (launcher.text !== script) await writeLauncher(paths);
86
- const advice = await activation(paths, await installRecord(paths));
69
+ const advice = await pathAdvice(paths);
87
70
  process.stdout.write(`Installed rer ${manifest.version} in ${join(paths.versions, name)}.\n${paths.launcher} runs it.${previousName ? ` The previous version (${previousName}) is kept.` : ''}\n${advice ? advice + '\n' : ''}`);
88
71
  return 0;
89
72
  }
90
-
91
- async function activation(paths, record) {
92
- const link = await exposeLauncher(paths);
93
- const links = [...new Set([...(record?.links ?? []), ...(link ? [link] : [])])];
94
- if (JSON.stringify(record?.links ?? []) !== JSON.stringify(links)) await writeFile(paths.record, JSON.stringify({ ...record, links }, null, 2) + '\n', { mode: 0o644 });
95
- return link ? undefined : await pathAdvice(paths);
96
- }
@@ -1,30 +1,34 @@
1
- // Offer the canonical launcher through an existing user-owned PATH directory.
2
- // Foreign commands and links are never replaced, including during uninstall.
3
- import { access, constants, lstat, readlink, symlink } from 'node:fs/promises';
4
- import { delimiter, isAbsolute, join, resolve } from 'node:path';
1
+ // Installation writes only the canonical launcher in ~/.local/bin.
2
+ // Other PATH commands are reported, never replaced or linked alongside.
3
+ import { access, constants, lstat, readlink, stat } from 'node:fs/promises';
4
+ import { basename, delimiter, join, resolve } from 'node:path';
5
+ import { shellQuote } from '../../cli/node-runtime-cache.mjs';
5
6
 
7
+ // Older installs recorded extra PATH links; uninstall can still remove those.
6
8
  export async function ownedPathLink(path, launcher) {
7
9
  try { return (await lstat(path)).isSymbolicLink() && (await readlink(path)) === resolve(launcher); }
8
10
  catch (error) { if (error.code === 'ENOENT' || error.code === 'EINVAL') return false; throw error; }
9
11
  }
10
- export async function exposeLauncher(paths, env = process.env) {
11
- for (const entry of (env.PATH ?? '').split(delimiter).filter(Boolean)) {
12
- const directory = resolve(entry), candidate = join(directory, 'rer');
13
- if (directory === resolve(paths.bin)) return undefined;
12
+
13
+ export async function pathAdvice(paths, env = process.env) {
14
+ const entries = [...new Set((env.PATH ?? '').split(delimiter).map(entry => resolve(entry)))];
15
+ const bin = resolve(paths.bin), lines = [];
16
+ let firstCommand;
17
+ for (const directory of entries) {
18
+ if (directory === bin) { firstCommand ??= paths.launcher; continue; }
19
+ const candidate = join(directory, 'rer');
14
20
  try {
15
- try {
16
- await lstat(candidate);
17
- if (await ownedPathLink(candidate, paths.launcher)) return candidate;
18
- await access(candidate, constants.X_OK);
19
- return undefined;
20
- } catch (error) { if (error.code !== 'ENOENT') continue; }
21
- // npm/npx add transient command directories; never install into them.
22
- if (!isAbsolute(entry) || directory.split('/').includes('node_modules') || directory.split('/').includes('_npx')) continue;
23
- const info = await lstat(directory);
24
- if (!info.isDirectory() || !process.getuid || info.uid !== process.getuid() || (!(info.mode & 0o200) || (info.mode & 0o022))) continue;
25
- await access(directory, constants.W_OK | constants.X_OK);
26
- try { await symlink(paths.launcher, candidate); return candidate; }
27
- catch (error) { if (error.code !== 'EEXIST' && error.code !== 'EACCES' && error.code !== 'EPERM') throw error; }
21
+ await access(candidate, constants.X_OK);
22
+ if (!(await stat(candidate)).isFile()) continue;
23
+ firstCommand ??= candidate;
24
+ lines.push(`Left existing rer at ${candidate} unchanged.`);
28
25
  } catch (error) { if (!['ENOENT', 'EACCES', 'ENOTDIR'].includes(error.code)) throw error; }
29
26
  }
27
+ if (!entries.includes(bin) || firstCommand !== paths.launcher) {
28
+ const command = basename(env.SHELL ?? '') === 'fish'
29
+ ? `set -gx PATH ${shellQuote(paths.bin)} $PATH`
30
+ : `export PATH=${shellQuote(paths.bin)}:"$PATH"`;
31
+ lines.push(`Add ~/.local/bin to PATH in this shell: ${command}`);
32
+ }
33
+ return lines.join('\n');
30
34
  }
@@ -42,7 +42,7 @@ export async function launchDev(target) {
42
42
  catch (failure) { if (!(failure instanceof LaunchFailure)) throw failure; process.stderr.write(failure.message + '\n'); return failure.exitCode; }
43
43
  }
44
44
  const here = await realpath(process.cwd()).catch(() => undefined);
45
- const out = printer({ json: devArgs.includes('--json'), login: devArgs.includes('--login'), stopHint: `rer dev --stop${here === checkout ? '' : ` --cwd ${JSON.stringify(checkout)}`}`, keyless: doorStart });
45
+ const out = printer({ json: devArgs.includes('--json'), stopHint: `rer dev --stop${here === checkout ? '' : ` --cwd ${JSON.stringify(checkout)}`}`, keyless: doorStart });
46
46
  const detach = args.includes('--detach') || (!args.includes('--foreground') && !process.stdout.isTTY);
47
47
  return detach ? detached(target, devArgs, directory, out, doorStart) : foreground(target, devArgs, directory, out, doorStart);
48
48
  }
@@ -1,10 +1,10 @@
1
1
  // What a launch needs to know about a checkout before it changes anything.
2
2
  import { execFile } from 'node:child_process';
3
3
  import { stat } from 'node:fs/promises';
4
- import { createServer } from 'node:net';
5
4
  import { join } from 'node:path';
6
5
  import { promisify } from 'node:util';
7
- import { listeningPorts } from '../../onboarding/process-diagnostics.mjs';
6
+ import { inspectionContext } from '../../onboarding/process-diagnostics.mjs';
7
+ import { probePort } from '../../onboarding/port-probe.mjs';
8
8
  import { LaunchFailure } from './failure.mjs';
9
9
 
10
10
  const run = promisify(execFile);
@@ -64,16 +64,9 @@ export async function setupNeeded(root, described, last) {
64
64
  return await finishedSince(root, described, last.at) ? undefined : 'resume';
65
65
  }
66
66
 
67
- async function bindable(host, port) {
68
- const socket = createServer();
69
- try { await new Promise((resolve, reject) => { socket.once('error', reject); socket.listen({ port, host, exclusive: true }, resolve); }); return true; }
70
- catch (error) { return host === '::1' && ['EADDRNOTAVAIL', 'EAFNOSUPPORT'].includes(error.code); }
71
- finally { if (socket.listening) await new Promise(resolve => socket.close(resolve)); }
72
- }
73
-
74
- // Whether something now listens on a port or keeps it from binding.
67
+ // Only a positive listener observation justifies saying another process took it.
75
68
  export async function portTaken(port) {
76
- return (await listeningPorts()).has(port) || !await bindable('127.0.0.1', port) || !await bindable('::1', port);
69
+ return Boolean((await probePort('127.0.0.1', port, { hosts: ['127.0.0.1', '::1'], anyAddress: true })).listener);
77
70
  }
78
71
 
79
72
  // The first port from 8787 (RER_DEV_PORT_FROM moves the start) that is not
@@ -84,10 +77,14 @@ export async function portTaken(port) {
84
77
  export async function freePort(ports, claim, env = process.env) {
85
78
  const from = Number(env.RER_DEV_PORT_FROM ?? 8787);
86
79
  if (!Number.isInteger(from) || from < 1 || from > 65535) throw new LaunchFailure(2, 'RER_DEV_PORT_FROM must be a port number.');
87
- const held = new Set(ports.filter(Boolean)), busy = await listeningPorts();
80
+ const held = new Set(ports.filter(Boolean)), context = inspectionContext();
88
81
  for (let port = from; port <= 65535; port++) {
89
- if (held.has(port) || busy.has(port)) continue;
90
- if (!await bindable('127.0.0.1', port) || !await bindable('::1', port)) continue;
82
+ if (held.has(port)) continue;
83
+ const result = await probePort('127.0.0.1', port, { context, hosts: ['127.0.0.1', '::1'], anyAddress: true });
84
+ if (!result.available) {
85
+ if (performance.now() >= context.deadline) throw new LaunchFailure(4, `Could not find a free port from ${from} in time (system busy). Run rer dev again or pass rer dev --port PORT with a free port.`);
86
+ continue;
87
+ }
91
88
  const claimed = await claim(port);
92
89
  if (claimed) return { port, ...claimed };
93
90
  }
@@ -1,5 +1,25 @@
1
+ import { createRedaction, credentialValues } from '../../onboarding/redaction.mjs';
2
+
1
3
  // A launch that stops with the dispatcher's own message (or the launcher's)
2
4
  // and exit code. `event` keeps the dispatcher's JSON error event when it sent one.
3
5
  export class LaunchFailure extends Error {
4
6
  constructor(exitCode, message, event) { super(message); this.exitCode = exitCode; this.event = event; }
5
7
  }
8
+
9
+ // Native errors have no dispatcher exit code, but their message is still the
10
+ // cause of this launch's failure. Redact before transporting it to followers;
11
+ // a JSON caller must receive the same cause and recovery as a terminal caller.
12
+ export function workerFailure(error, { root, env = process.env }) {
13
+ const redaction = createRedaction();
14
+ redaction.addSecrets([...credentialValues(env), ...credentialValues(error?.event)]);
15
+ const redact = text => redaction.redact(String(text).replace(/([#&]rer-dev=)[^&\s"'<>]+/g, '$1[REDACTED]'));
16
+ const clean = value => typeof value === 'string' ? redact(value)
17
+ : Array.isArray(value) ? value.map(clean)
18
+ : value && typeof value === 'object' ? Object.fromEntries(Object.entries(value).map(([key, item]) => [key, clean(item)])) : value;
19
+ const exitCode = Number.isInteger(error?.exitCode) && error.exitCode > 0 && error.exitCode <= 255 ? error.exitCode : 1;
20
+ const cause = redact(error?.message || error || 'The launch stopped before the app was ready.');
21
+ const retry = `rer dev --cwd '${root.replaceAll("'", "'\\''")}'`;
22
+ const message = `${cause}\nCorrect the cause, then run ${retry} again.`;
23
+ const event = { version: 1, verb: 'dev', event: 'error', code: 'launch_failed', ...clean(error?.event), message, exitCode };
24
+ return new LaunchFailure(exitCode, message, event);
25
+ }
@@ -5,7 +5,7 @@ const quiet = 20000;
5
5
 
6
6
  // `keyless` (a start by the front door, whose log this is): the ready lines
7
7
  // leave out the dev-key link, so the door's files never hold a key.
8
- export function printer({ json, login, stopHint, keyless = false }) {
8
+ export function printer({ json, stopHint, keyless = false }) {
9
9
  const started = Date.now();
10
10
  let current = 'preparing', last = Date.now();
11
11
  const event = (name, fields) => process.stdout.write(JSON.stringify({ version: 1, verb: 'dev', event: name, ...fields }) + '\n');
@@ -24,13 +24,9 @@ export function printer({ json, login, stopHint, keyless = false }) {
24
24
  if (keyless) { const { openUrl, ...rest } = ready; ready = rest; }
25
25
  if (json) { event('server', { pid, log }); process.stdout.write(JSON.stringify(ready) + '\n'); return; }
26
26
  const out = [`RER_READY ${ready.url}`];
27
- if (ready.openUrl && !login) {
27
+ if (ready.openUrl) {
28
28
  if (ready.signedInAs) out.push(`RER_SIGNED_IN ${ready.signedInAs}`);
29
29
  out.push(`RER_OPEN ${ready.openUrl}`, 'Open that link on this computer. Its fragment holds this checkout\'s dev key, which the page keeps only as a cookie; never share the link.');
30
- } else if (ready.signInUrl) {
31
- out.push(`RER_SIGN_IN ${ready.signInUrl}`);
32
- if (ready.openUrl) out.push(`RER_OPEN ${ready.openUrl}`);
33
- out.push(`Open ${ready.openUrl ? 'the last link on this computer (it carries the dev key; never share it)' : 'the outbox'}, choose the newest sign-in message and follow its link. No email was sent.`);
34
30
  }
35
31
  // A seed with a run site: its static homepage, /run and docs, served locally.
36
32
  if (ready.siteUrl) out.push(`RER_SITE ${ready.siteUrl}`);
@@ -11,7 +11,7 @@ import { realpath } from 'node:fs/promises';
11
11
  import { join } from 'node:path';
12
12
  import { processInfo } from '../../modules/job-queue/processes.mjs';
13
13
  import { liveServers } from '../registry.mjs';
14
- import { LaunchFailure } from './failure.mjs';
14
+ import { LaunchFailure, workerFailure } from './failure.mjs';
15
15
  import { prepare } from './prepare.mjs';
16
16
  import { claimLaunch, eventLog, launchDirectory, liveWorker, releaseLaunch, serverRecord, stopRequest } from './state.mjs';
17
17
  import { startServer } from './steps.mjs';
@@ -53,8 +53,8 @@ if (!await claimLaunch(directory, token)) {
53
53
  await events.write({ event: 'ready', ready: started.ready, pid, ...(ours ? { log: serverLog } : {}) });
54
54
  } catch (error) {
55
55
  log(String(error?.stack ?? error));
56
- const failure = stopping.signal.aborted ? new LaunchFailure(1, 'rer dev --stop stopped this launch before the app was ready.') : error;
57
- await events.write({ event: 'failed', exitCode: failure.exitCode ?? 1, message: failure.exitCode ? failure.message : `The launch failed unexpectedly; see ${join(directory, 'launch.log')}.`, ...(failure.event ? { error: failure.event } : {}) });
56
+ const failure = workerFailure(stopping.signal.aborted ? new LaunchFailure(1, 'rer dev --stop stopped this launch before the app was ready.') : error, { root });
57
+ await events.write({ event: 'failed', exitCode: failure.exitCode, message: failure.message, error: failure.event });
58
58
  } finally {
59
59
  await releaseLaunch(directory, token);
60
60
  logFile.end();
package/global/main.mjs CHANGED
@@ -30,13 +30,13 @@ pins (installed first by any verb when node_modules holds another), else its ven
30
30
  Run COMMAND once this computer's job queue admits it (heavy package scripts
31
31
  of a checkout that pins the rer package use it).
32
32
 
33
- Checkout verbs: install, setup, dev, demo, preview, build, verify, reset, deploy, export, import,
34
- outbox, doctor, describe, check, release. Run rer VERB --help in a checkout for its flags.
33
+ Checkout verbs: install, setup, dev, demo, preview, build, verify, reset, deploy, signin, export,
34
+ import, doctor, describe, check, release. Run rer VERB --help in a checkout for its flags.
35
35
  `;
36
36
 
37
37
  // The checkout verbs that install the pinned package before they run when
38
38
  // node_modules holds another version or none (dev installs it in its launch).
39
- const installsFirst = new Set([...Object.keys(verbs), 'release'].filter(verb => verb !== 'dev'));
39
+ const installsFirst = new Set([...Object.keys(verbs), 'signin', 'release'].filter(verb => verb !== 'dev'));
40
40
 
41
41
  export async function main(argv) {
42
42
  handleBrokenPipes();
@@ -86,10 +86,10 @@ async function buildImage(root, { output = join(root,'.rer/container/image'), en
86
86
  for (const path of runner.files) await cp(within(path),join(output,'app/runner',path),{recursive:true,filter:path => !path.includes('/tests/') && !path.endsWith('/synthetic.mjs')});
87
87
  }
88
88
  const base = resolve(import.meta.dirname,'../..');
89
- for (const path of ['modules/container','modules/portability','modules/deploy-kit','cli/scheduled.mjs','cli/jsonc.mjs','modules/integrations/core/public-address.ts']) { await mkdir(join(output,'runtime',path,'..'),{recursive:true}); await cp(join(base,path),join(output,'runtime',path),{recursive:true,filter:path => !path.includes('/tests/')}); }
89
+ for (const path of ['modules/container','modules/portability','modules/d1-transport','modules/deploy-kit','cli/scheduled.mjs','cli/jsonc.mjs','modules/integrations/core/public-address.ts']) { await mkdir(join(output,'runtime',path,'..'),{recursive:true}); await cp(join(base,path),join(output,'runtime',path),{recursive:true,filter:path => !path.includes('/tests/')}); }
90
90
  const dependencies = engine === 'workerd' ? { miniflare:'5.20260907.0-alpha' } : {};
91
91
  await writeFile(join(output,'runtime/package.json'),JSON.stringify({private:true,type:'module',dependencies}));
92
92
  await writeFile(join(output,'Dockerfile'),`FROM node:26.8.1-bookworm-slim\nRUN apt-get update && apt-get install -y --no-install-recommends util-linux ca-certificates && apt-get clean\nRUN mkdir -p /private /archive\nWORKDIR /runtime\nCOPY runtime/package.json ./package.json\nRUN npm install --omit=dev --no-audit --no-fund\nCOPY runtime/ /runtime/\nCOPY app/ /app/\nENV NODE_ENV=production PORT=8080 WRANGLER_SEND_METRICS=false\nEXPOSE 8080\nVOLUME /data\nENTRYPOINT ["sh", "/runtime/modules/container/entrypoint.sh"]\n`);
93
93
  return { output,manifest };
94
94
  }
95
- if (process.argv[1] === import.meta.filename) { try { const {output,engine}=JSON.parse(process.argv[3]??'{}'),result=await buildContainer(resolve(process.argv[2]),{...(output?{output:resolve(output)}:{}),...(engine?{engine}:{})}); console.log(JSON.stringify({output:result.output,engine:result.manifest.engine})); } catch(cause) { console.error(cause.message); if(cause.failures)console.error(JSON.stringify(cause.failures));process.exitCode=1; } }
95
+ if (process.argv[1] === import.meta.filename) { try { const {output,engine,declaration}=JSON.parse(process.argv[3]??'{}'),result=await buildContainer(resolve(process.argv[2]),{...(output?{output:resolve(output)}:{}),...(engine?{engine}:{}),...(declaration?{declaration}:{})}); console.log(JSON.stringify({output:result.output,engine:result.manifest.engine})); } catch(cause) { console.error(cause.message); if(cause.failures)console.error(JSON.stringify(cause.failures));process.exitCode=1; } }
@@ -0,0 +1,35 @@
1
+ // Public-address filtering and TLS belong to NETWORK. Demo's application guard
2
+ // simulates effects on people/accounts; this is defense for direct transports.
3
+ const transports = new Set([
4
+ 'api.sendgrid.com', 'api.resend.com', 'api.twilio.com',
5
+ 'api.mailgun.net', 'api.eu.mailgun.net', 'api.postmarkapp.com',
6
+ ]);
7
+ const mailPorts = new Set(['25', '465', '587', '2525']);
8
+
9
+ function check(url) {
10
+ const host = url.hostname.toLowerCase().replace(/\.$/, '');
11
+ if (transports.has(host) || mailPorts.has(url.port)) throw new TypeError('Demo direct messaging transport refused');
12
+ }
13
+
14
+ export default {
15
+ async fetch(request, env) {
16
+ if (env.RER_DEMO !== '1') return env.NETWORK.fetch(request);
17
+ const redirect = request.redirect;
18
+ for (let hops = 0; ; hops++) {
19
+ const url = new URL(request.url);
20
+ check(url);
21
+ // Check every redirect before contacting its destination, including when
22
+ // an otherwise public URL redirects into a messaging provider.
23
+ const response = await env.NETWORK.fetch(new Request(request.clone(), { redirect: 'manual' }));
24
+ const location = response.headers.get('location');
25
+ if (![301, 302, 303, 307, 308].includes(response.status) || location === null || redirect === 'manual') return response;
26
+ await response.body?.cancel();
27
+ if (redirect === 'error' || hops === 20) throw new TypeError('Demo egress redirect refused');
28
+ const next = new URL(location, url), headers = new Headers(request.headers);
29
+ const get = response.status === 303 && !['GET', 'HEAD'].includes(request.method) || [301, 302].includes(response.status) && request.method === 'POST';
30
+ if (get) for (const name of ['content-type', 'content-length', 'content-encoding', 'content-language', 'content-location']) headers.delete(name);
31
+ if (next.origin !== url.origin) for (const name of ['authorization', 'cookie', 'proxy-authorization']) headers.delete(name);
32
+ request = new Request(next, { method: get ? 'GET' : request.method, headers, body: get ? null : request.body, redirect, signal: request.signal, duplex: 'half' });
33
+ }
34
+ },
35
+ };
@@ -19,9 +19,9 @@ export async function workerEngine({ root, data, manifest, settings, edge }) {
19
19
  };
20
20
  const mf = new Miniflare(convertV4MiniflareOptions({ host:'127.0.0.1',port:0,cf:false,resourcePersistencePath:data,unsafeLocalExplorer:false,
21
21
  workers:[{ name:'ingress',modules:true,script:`export default {fetch(request,env){const observed=request.headers.get('x-rer-original-url');if(!observed)return new Response(null,{status:400});const url=new URL(observed),headers=new Headers(request.headers);headers.delete('x-rer-original-url');headers.set('host',url.host);return env.APP.fetch(new Request(url,{method:request.method,headers,redirect:'manual',...(!['GET','HEAD'].includes(request.method)?{body:request.body}:{})}))}}`,serviceBindings:{ APP:'app' } }, app,
22
- // A demo (RER_DEMO=1) sends nothing: its egress worker refuses every request.
23
- settings.RER_DEMO === '1' ? { name:'egress',modules:true,script:"export default {fetch(request){throw new TypeError('fetch failed: demo egress is denied ('+new URL(request.url).host+')')}}" }
24
- : { name:'egress',modules:true,script:'export default {fetch(request,env){return env.NETWORK.fetch(request)}}',serviceBindings:{ NETWORK:{ network:{ allow:['public'],tlsOptions:{ trustBrowserCas:true,trustedCertificates:[...rootCertificates] } } } } }],
22
+ { name:'egress',modules:[{ type:'ESModule',path:join(import.meta.dirname,'demo-egress.mjs') }],modulesRoot:import.meta.dirname,
23
+ bindings:{ RER_DEMO:settings.RER_DEMO ?? '' },
24
+ serviceBindings:{ NETWORK:{ network:{ allow:['public'],tlsOptions:{ trustBrowserCas:true,trustedCertificates:[...rootCertificates] } } } } }],
25
25
  }));
26
26
  await mf.ready; const databases = {}, objects = {};
27
27
  for (const [binding, plan] of Object.entries(manifest.migrations)) { const db = await mf.getD1Database(binding, 'app'); databases[binding] = db; await migrate(db, plan); await ensurePortabilitySchema(db, manifest.portabilityMigrations); }
@@ -0,0 +1,9 @@
1
+ export interface Sql { sql: string; params?: any[] }
2
+ export const RPC_LIMIT: number;
3
+ export function wireSql(statement: Sql): { sql: string; params: any[] };
4
+ export function wireBatch(statements: Sql[]): Sql[];
5
+ export function rpcBytes(statements: Sql[]): number;
6
+ export interface RpcSize { utf8: number; units: number; hasWide: boolean }
7
+ export function rpcSize(statements: Sql[]): RpcSize;
8
+ export function rpcSizeBytes(size: RpcSize): number;
9
+ export function wireDatabase<T extends { prepare(sql: string): any; batch(statements: any[]): Promise<any> }>(database: T): T;