@runeditrun/rer 0.2.1 → 0.2.2

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 (62) hide show
  1. package/README.md +12 -2
  2. package/cli/auth-copy.mjs +25 -18
  3. package/cli/bounded.mjs +13 -4
  4. package/cli/contract.mjs +1 -1
  5. package/cli/deploy-inputs.mjs +33 -0
  6. package/cli/deploy-owner.mjs +61 -0
  7. package/cli/deploy.mjs +87 -15
  8. package/cli/door.mjs +1 -1
  9. package/cli/inspect.mjs +24 -32
  10. package/cli/lifecycle.mjs +14 -4
  11. package/cli/parse.mjs +3 -1
  12. package/cli/pnpm-probe.mjs +14 -0
  13. package/cli/pnpm-runtime.mjs +22 -22
  14. package/cli/portability-policy.mjs +55 -0
  15. package/cli/portability-worker.mjs +8 -4
  16. package/cli/portability.mjs +20 -4
  17. package/cli/project-pnpm.mjs +67 -0
  18. package/cli/release.mjs +35 -0
  19. package/cli/server-registry.mjs +2 -2
  20. package/cli/site-check.mjs +15 -6
  21. package/cli/site-data.mjs +6 -5
  22. package/cli/site-references.mjs +153 -3
  23. package/global/CHANGELOG.md +14 -0
  24. package/global/README.md +12 -2
  25. package/global/cli/install.mjs +26 -9
  26. package/global/cli/path-link.mjs +30 -0
  27. package/global/cli/status.mjs +1 -1
  28. package/global/cli/uninstall.mjs +5 -1
  29. package/global/launch/install-pinned.mjs +4 -0
  30. package/global/main.mjs +28 -3
  31. package/global/registry.mjs +2 -2
  32. package/modules/container/deploy-input.mjs +5 -9
  33. package/modules/dependencies/auth.mjs +2 -1
  34. package/modules/deploy-kit/README.md +25 -7
  35. package/modules/deploy-kit/d1-import.mjs +35 -0
  36. package/modules/deploy-kit/deploy.mjs +4 -3
  37. package/modules/deploy-kit/diagnostics.mjs +11 -1
  38. package/modules/deploy-kit/env-file.mjs +26 -0
  39. package/modules/deploy-kit/health-request.mjs +6 -1
  40. package/modules/deploy-kit/health.mjs +35 -15
  41. package/modules/deploy-kit/migration-ledger.mjs +48 -0
  42. package/modules/deploy-kit/migrations.mjs +11 -15
  43. package/modules/deploy-kit/owner-cli.mjs +20 -7
  44. package/modules/deploy-kit/owner-remove.mjs +105 -0
  45. package/modules/deploy-kit/owner-secret.mjs +49 -0
  46. package/modules/deploy-kit/owner.mjs +68 -10
  47. package/modules/deploy-kit/provider.mjs +15 -2
  48. package/modules/job-queue/config.mjs +4 -2
  49. package/modules/operator-auth/identity/oidc/README.md +18 -0
  50. package/modules/operator-auth/identity/oidc/config.mjs +4 -2
  51. package/modules/operator-auth/identity/oidc/index.d.mts +1 -1
  52. package/modules/operator-auth/identity/oidc/index.mjs +28 -9
  53. package/modules/operator-auth/identity/oidc/network.mjs +9 -4
  54. package/modules/operator-auth/identity/oidc/store.mjs +19 -3
  55. package/modules/operator-auth/index.d.mts +2 -0
  56. package/modules/operator-auth/routes.mjs +1 -1
  57. package/modules/portability/README.md +28 -0
  58. package/modules/portability/bridge.mjs +10 -3
  59. package/modules/portability/import.mjs +4 -2
  60. package/onboarding/commands.mjs +3 -1
  61. package/package.json +1 -1
  62. package/scripts/publish-check/publication-config.json +7 -1
@@ -0,0 +1,14 @@
1
+ import { missing } from './errors.mjs';
2
+
3
+ // A version check owns its descendants just like a lifecycle command. Cleanup
4
+ // starts at leader exit, even when quiet descendants still hold the pipes.
5
+ export async function probePnpm(root, store, signal, env = process.env) {
6
+ const probeSignal = AbortSignal.any([signal, AbortSignal.timeout(10000)].filter(Boolean));
7
+ const child = store.spawn('Checking pinned pnpm', 'pnpm', ['--version'], { cwd: root, env, signal: probeSignal });
8
+ let version = '';
9
+ child.stdout.on('data', chunk => { version = (version + chunk).slice(-1024); });
10
+ const code = await new Promise(resolve => { child.once('error', () => resolve(null)); child.once('exit', code => resolve(code)); });
11
+ await store.stop(child);
12
+ if (probeSignal.aborted || code !== 0) throw missing('The pinned pnpm version probe failed. Select the documented tool.');
13
+ return version.trim();
14
+ }
@@ -15,11 +15,11 @@ import { shellQuote } from './node-runtime-cache.mjs';
15
15
  // rer's own cache (~/.rer/pnpm/pnpm-VERSION). Only rer's own commands for the
16
16
  // project put it on their PATH; nothing global changes, and it is used only
17
17
  // when the person's PATH has no pnpm of the pinned version.
18
- export const pnpmCache = () => join(homedir(), '.rer', 'pnpm');
19
- const directoryOf = version => join(pnpmCache(), `pnpm-${version}`);
20
- const shim = version => join(directoryOf(version), 'shim');
21
- const receipt = version => join(directoryOf(version), '.rer-verified.json');
22
- const entry = version => join(directoryOf(version), 'bin', 'pnpm.cjs');
18
+ export const pnpmCache = (env = process.env) => join(env.HOME || homedir(), '.rer', 'pnpm');
19
+ const directoryOf = (version, env) => join(pnpmCache(env), `pnpm-${version}`);
20
+ const shim = (version, env) => join(directoryOf(version, env), 'shim');
21
+ const receipt = (version, env) => join(directoryOf(version, env), '.rer-verified.json');
22
+ const entry = (version, env) => join(directoryOf(version, env), 'bin', 'pnpm.cjs');
23
23
 
24
24
  async function digest(path, algorithm, encoding) {
25
25
  const hash = createHash(algorithm);
@@ -30,25 +30,25 @@ async function owned(path, directory) {
30
30
  const stat = await lstat(path);
31
31
  if (stat.isSymbolicLink() || (directory ? !stat.isDirectory() : !stat.isFile()) || (process.getuid && stat.uid !== process.getuid()) || (stat.mode & 0o022)) throw Error('Unsafe pnpm cache placement.');
32
32
  }
33
- async function prepareCache() {
34
- for (const path of [join(homedir(), '.rer'), pnpmCache()]) {
33
+ async function prepareCache(env) {
34
+ for (const path of [join(env.HOME || homedir(), '.rer'), pnpmCache(env)]) {
35
35
  await mkdir(path, { mode: 0o700 }).catch(error => { if (error.code !== 'EEXIST') throw error; });
36
36
  try { await owned(path, true); } catch { throw missing(`The pnpm cache directory must be an owned, non-linked directory without group/other write access: ${path}. Correct its placement and permissions before retrying.`); }
37
37
  }
38
- return pnpmCache();
38
+ return pnpmCache(env);
39
39
  }
40
40
 
41
41
  /** The verified cached pnpm's PATH directory, or undefined when none is cached. */
42
- export async function cachedPnpm(version) {
43
- try { await lstat(directoryOf(version)); } catch (error) { if (error.code === 'ENOENT') return; throw error; }
42
+ export async function cachedPnpm(version, env = process.env) {
43
+ try { await lstat(directoryOf(version, env)); } catch (error) { if (error.code === 'ENOENT') return; throw error; }
44
44
  try {
45
- for (const path of [join(homedir(), '.rer'), pnpmCache(), directoryOf(version), shim(version)]) await owned(path, true);
46
- for (const path of [receipt(version), entry(version), join(shim(version), 'pnpm')]) await owned(path, false);
47
- const record = JSON.parse(await readFile(receipt(version), 'utf8'));
48
- if (record.version !== version || !/^sha512-/.test(record.integrity) || record.entrySha256 !== await digest(entry(version), 'sha256', 'hex')) throw Error('pnpm cache integrity mismatch.');
49
- return shim(version);
45
+ for (const path of [join(env.HOME || homedir(), '.rer'), pnpmCache(env), directoryOf(version, env), shim(version, env)]) await owned(path, true);
46
+ for (const path of [receipt(version, env), entry(version, env), join(shim(version, env), 'pnpm')]) await owned(path, false);
47
+ const record = JSON.parse(await readFile(receipt(version, env), 'utf8'));
48
+ if (record.version !== version || !/^sha512-/.test(record.integrity) || record.entrySha256 !== await digest(entry(version, env), 'sha256', 'hex')) throw Error('pnpm cache integrity mismatch.');
49
+ return shim(version, env);
50
50
  } catch {
51
- throw missing(`The cached pnpm is unsafe or incomplete. Preserve it for inspection with: mv ${shellQuote(directoryOf(version))} ${shellQuote(directoryOf(version) + '.quarantine-' + Date.now())}. Then rerun the original rer command to fetch a verified release.`);
51
+ throw missing(`The cached pnpm is unsafe or incomplete. Preserve it for inspection with: mv ${shellQuote(directoryOf(version, env))} ${shellQuote(directoryOf(version, env) + '.quarantine-' + Date.now())}. Then rerun the original rer command to fetch a verified release.`);
52
52
  }
53
53
  }
54
54
 
@@ -69,8 +69,8 @@ function extract(archive, directory, signal) {
69
69
  }
70
70
 
71
71
  /** Fetch, verify and cache pnpm VERSION; returns its PATH directory. `registry` is the npm registry origin. */
72
- export async function downloadPnpm(version, cancellation, { registry = 'https://registry.npmjs.org' } = {}) {
73
- const cache = await prepareCache(), temporary = await mkdtemp(join(cache, '.download-'));
72
+ export async function downloadPnpm(version, cancellation, { registry = 'https://registry.npmjs.org', env = process.env } = {}) {
73
+ const cache = await prepareCache(env), temporary = await mkdtemp(join(cache, '.download-'));
74
74
  const signal = AbortSignal.any([cancellation, AbortSignal.timeout(60000)].filter(Boolean));
75
75
  try {
76
76
  const metadata = join(temporary, 'release.json'), archive = join(temporary, 'pnpm.tgz');
@@ -86,11 +86,11 @@ export async function downloadPnpm(version, cancellation, { registry = 'https://
86
86
  if (JSON.parse(await readFile(join(extracted, 'package.json'), 'utf8')).version !== version) throw Error('Verified pnpm release did not name the project pin.');
87
87
  await mkdir(join(extracted, 'shim'), { mode: 0o700 });
88
88
  // `node` is rer's own: the command puts its Node first on PATH beside this.
89
- await writeFile(join(extracted, 'shim', 'pnpm'), `#!/bin/sh\nexec node ${shellQuote(entry(version))} "$@"\n`, { mode: 0o700 });
89
+ await writeFile(join(extracted, 'shim', 'pnpm'), `#!/bin/sh\nexec node ${shellQuote(entry(version, env))} "$@"\n`, { mode: 0o700 });
90
90
  await writeFile(join(extracted, '.rer-verified.json'), JSON.stringify({ version, integrity, entrySha256: await digest(join(extracted, 'bin', 'pnpm.cjs'), 'sha256', 'hex') }) + '\n', { flag: 'wx', mode: 0o600 });
91
91
  await chmod(extracted, 0o700);
92
- try { await rename(extracted, directoryOf(version)); }
93
- catch (error) { if (!['EEXIST', 'ENOTEMPTY'].includes(error.code) || !(await cachedPnpm(version))) throw error; }
94
- return shim(version);
92
+ try { await rename(extracted, directoryOf(version, env)); }
93
+ catch (error) { if (!['EEXIST', 'ENOTEMPTY'].includes(error.code) || !(await cachedPnpm(version, env))) throw error; }
94
+ return shim(version, env);
95
95
  } finally { await rm(temporary, { recursive: true, force: true }); }
96
96
  }
@@ -0,0 +1,55 @@
1
+ import { realpath, stat } from 'node:fs/promises';
2
+ import { isAbsolute, relative, resolve, sep } from 'node:path';
3
+ import { fileURLToPath, pathToFileURL } from 'node:url';
4
+ import { policy as validatePolicy } from '../modules/portability/catalogue.mjs';
5
+ import { PortabilityError } from '../modules/portability/index.mjs';
6
+
7
+ // Only seed configuration supplies this entry point. Never resolve executable
8
+ // policy from archive metadata, or stringify functions across the process seam.
9
+ export async function policyModuleURL(root, entry) {
10
+ try {
11
+ let path;
12
+ if (entry instanceof URL || (typeof entry === 'string' && entry.startsWith('file:'))) {
13
+ const url = new URL(entry);
14
+ if (url.protocol !== 'file:' || url.search || url.hash) throw new Error();
15
+ path = fileURLToPath(url);
16
+ } else {
17
+ if (typeof entry !== 'string' || !entry || isAbsolute(entry) || /^[a-z][a-z\d+.-]*:/i.test(entry)) throw new Error();
18
+ path = resolve(root, entry);
19
+ }
20
+ const base = await realpath(root), actual = await realpath(path), within = relative(base, actual);
21
+ if (!within || within === '..' || within.startsWith('..' + sep) || isAbsolute(within) || !(await stat(actual)).isFile()) throw new Error();
22
+ return pathToFileURL(actual).href;
23
+ } catch { throw new PortabilityError('invalid_policy_module', 409); }
24
+ }
25
+
26
+ export function serializablePolicy(input) {
27
+ const seen = new Set();
28
+ function copy(value) {
29
+ if (value === null || value === undefined || typeof value === 'string' || typeof value === 'boolean' || (typeof value === 'number' && Number.isFinite(value))) return value;
30
+ if (typeof value !== 'object' || seen.has(value) || (!Array.isArray(value) && ![Object.prototype, null].includes(Object.getPrototypeOf(value)))) throw new PortabilityError('policy_module_required', 409);
31
+ seen.add(value);
32
+ const result = Array.isArray(value) ? [] : Object.create(null);
33
+ for (const key of Reflect.ownKeys(value)) {
34
+ if (Array.isArray(value) && key === 'length') continue;
35
+ const descriptor = Object.getOwnPropertyDescriptor(value, key);
36
+ if (typeof key !== 'string' || !descriptor.enumerable || !('value' in descriptor)) throw new PortabilityError('policy_module_required', 409);
37
+ result[key] = copy(descriptor.value);
38
+ }
39
+ seen.delete(value);
40
+ return result;
41
+ }
42
+ const result = copy(input);
43
+ validatePolicy(result);
44
+ return result;
45
+ }
46
+
47
+ export async function loadPolicy(root, policy, policyModule) {
48
+ if (policyModule === undefined) return serializablePolicy(policy);
49
+ if (policy !== undefined) throw new PortabilityError('ambiguous_policy', 409);
50
+ try {
51
+ const { default: input } = await import(await policyModuleURL(root, policyModule));
52
+ validatePolicy(input);
53
+ return input;
54
+ } catch { throw new PortabilityError('invalid_policy_module', 409); }
55
+ }
@@ -4,27 +4,31 @@ import { readLegacyArchive } from '../modules/portability/format.mjs';
4
4
  import { recoverLegacyArchive } from '../modules/portability/legacy-files.mjs';
5
5
  import { legacyTargetState, requireLegacyEmpty } from '../modules/portability/legacy-safety.mjs';
6
6
  import { migrationReplay } from '../modules/portability/migration-history.mjs';
7
+ import { appliedMigrations as appliedMigrationNames } from '../modules/portability/bridge.mjs';
7
8
  import { join } from 'node:path';
8
9
  import { readFile } from 'node:fs/promises';
9
10
  import { createRequire } from 'node:module';
10
11
  import { createPortability, PortabilityError, readArchive } from '../modules/portability/index.mjs';
11
12
  import { parseJsonc } from './jsonc.mjs';
13
+ import { loadPolicy } from './portability-policy.mjs';
12
14
 
13
- // The migrations this installation applied, in Wrangler's applied order, so a
15
+ // The migrations this installation applied, in its ledger's order, so a
14
16
  // restore can place an older archive within them.
15
17
  async function appliedMigrations(root, db, binding, split) {
16
18
  const config = parseJsonc(await readFile(join(root, 'wrangler.jsonc'), 'utf8'));
17
19
  const directory = join(root, config.d1_databases.find(entry => entry.binding === binding).migrations_dir ?? 'migrations');
18
- const names = (await db.prepare('SELECT name FROM d1_migrations ORDER BY id').all()).results.map(row => row.name);
20
+ const names = await appliedMigrationNames(db) ?? [];
19
21
  return Promise.all(names.map(async name => ({ name, statements: split(await readFile(join(directory, name), 'utf8')) })));
20
22
  }
21
23
 
22
- async function run(direction, { root, path, journal, binding, buckets, policy, transient, legacyRecovery }) {
24
+ async function run(direction, { root, path, journal, binding, buckets, policy, policyModule, transient, legacyRecovery }) {
23
25
  // Validate all inputs before opening bindings or acquiring a restore lock.
24
26
  if (direction === 'import') {
25
27
  for await (const record of fileRecords(path,legacyRecovery?readLegacyArchive:readArchive)) {}
26
28
  if(journal)for await(const record of fileRecords(journal)) {}
27
29
  }
30
+ policy = await loadPolicy(root, policy, policyModule);
31
+ if (direction === 'policy-preflight') return { checkedPolicy: true };
28
32
  const { getPlatformProxy, unstable_splitSqlQuery } = createRequire(join(root, 'package.json'))('wrangler');
29
33
 
30
34
  // Unlike Wrangler's --persist-to base, the API takes the versioned directory.
@@ -46,7 +50,7 @@ async function run(direction, { root, path, journal, binding, buckets, policy, t
46
50
  try { console.log(JSON.stringify({ ok: true, ...await run(process.argv[2], JSON.parse(process.argv[3])) })); }
47
51
  catch (error) {
48
52
  // Erasure-fence refusals name the fence: an older archive is refused, not a failed transfer.
49
- const refusal = error instanceof PortabilityError && (error.status === 409 || ['wrong_product', 'schema_mismatch', 'table_inventory', 'configuration_inventory', 'erased_row', 'erasure_epoch', 'later_journal', 'invalid_journal'].includes(error.code.split(':')[0]));
53
+ const refusal = error instanceof PortabilityError && (error.status === 409 || ['wrong_product', 'schema_mismatch', 'table_inventory', 'configuration_inventory', 'erased_row', 'erasure_epoch', 'later_journal', 'invalid_journal', 'staged_validation'].includes(error.code.split(':')[0]));
50
54
  const exitCode = error.message === 'archive_json' ? 2 : refusal || error.code === 'EEXIST' ? 4 : 1;
51
55
  // Report only symbolic codes; native database errors can contain private data.
52
56
  const reason = error instanceof PortabilityError ? error.code.split(':')[0] : /^[A-Z_]+$/.test(error.code ?? '') ? error.code : error.message === 'archive_json' ? 'archive_json' : 'native_error';
@@ -5,10 +5,11 @@ import { readArchive, PortabilityError } from '../modules/portability/index.mjs'
5
5
  import { readLegacyArchive } from '../modules/portability/format.mjs';
6
6
  import { fileRecords } from '../modules/portability/files.mjs';
7
7
  import { RerError, refuse } from './errors.mjs';
8
+ import { policyModuleURL, serializablePolicy } from './portability-policy.mjs';
8
9
 
9
10
  // Seeds declare their real bindings and data policy. The shared module owns the
10
11
  // archive format and atomic restore; the shared runner owns its native children.
11
- export function localPortability({ database, binding = 'DB', buckets = {}, policy, transient = [] }) {
12
+ export function localPortability({ database, binding = 'DB', buckets = {}, policy, policyModule, transient = [] }) {
12
13
  async function run({ root, path, journal, store, signal, operationToken, platform, legacyRecovery }, direction) {
13
14
  if (direction === 'export') {
14
15
  if (await lstat(path).catch(error => { if (error.code === 'ENOENT') return null; throw error; })) throw refuse('Export requires a new archive path.');
@@ -22,6 +23,7 @@ export function localPortability({ database, binding = 'DB', buckets = {}, polic
22
23
  if(journal)for await(const record of fileRecords(journal)) {}
23
24
  }
24
25
  catch (error) {
26
+ if (error.message === 'archive_json') throw new RerError('invalid_archive', 'Restore refused (archive_json): archive JSON is invalid.', 2);
25
27
  if (!(error instanceof PortabilityError)) throw error;
26
28
  const message = error.code === 'legacy_archive_unverifiable'
27
29
  ? 'Restore refused (legacy_archive_unverifiable): version 1 metadata cannot be authenticated. Upgrade the original installation and re-export, or use rer import --legacy-v1 --input FILE --yes --acknowledge-unverified-metadata --output exports/recovered-v2.rer in an empty installation. See modules/portability/README.md#backup-only-v1-recovery.'
@@ -31,8 +33,22 @@ export function localPortability({ database, binding = 'DB', buckets = {}, polic
31
33
  }
32
34
  }
33
35
  if(legacyRecovery)store.report('Legacy v1 recovery: manifest metadata is unverified. Restored data will be audited and exported to a new v2 backup.');
34
- if(platform==='container') return (await import('./container-transfer.mjs')).containerTransfer({root,path,journal,store,signal,direction,binding,buckets,policy,transient});
35
- const input={ root, path, journal, binding, buckets, policy, transient, legacyRecovery };
36
+ let declaration;
37
+ try {
38
+ if (policyModule !== undefined) {
39
+ if (policy !== undefined) throw new PortabilityError('ambiguous_policy', 409);
40
+ if (platform === 'container') throw new PortabilityError('container_policy_module_unsupported', 409);
41
+ declaration = { policyModule: await policyModuleURL(root, policyModule) };
42
+ } else declaration = { policy: serializablePolicy(policy) };
43
+ } catch (error) {
44
+ if (!(error instanceof PortabilityError)) throw error;
45
+ throw refuse(`Local portability policy refused (${error.code}). Executable policies require a seed-owned policyModule with a valid default policy export; policyModule requires native local transport.`);
46
+ }
47
+ if(platform==='container') return (await import('./container-transfer.mjs')).containerTransfer({root,path,journal,store,signal,direction,binding,buckets,policy:declaration.policy,transient});
48
+ const input={ root, path, journal, binding, buckets, ...declaration, transient, legacyRecovery };
49
+ // Check the executable policy before migrations; the transfer worker imports
50
+ // it again in its own process so module closures are retained during restore.
51
+ if (policyModule !== undefined) await nativeWorker(store,'policy-preflight',input,signal);
36
52
  // Do not apply pending product migrations to a populated recovery target.
37
53
  // Check real existing bindings first; finalize repeats this under the lock.
38
54
  if(legacyRecovery)await nativeWorker(store,'legacy-preflight',input,signal);
@@ -48,7 +64,7 @@ export function localPortability({ database, binding = 'DB', buckets = {}, polic
48
64
 
49
65
  async function nativeWorker(store,direction,input,signal) {
50
66
  const worker = fileURLToPath(new URL('./portability-worker.mjs', import.meta.url));
51
- const child = store.spawn(`${direction === 'legacy-preflight' ? 'Checking empty recovery destination' : direction === 'export' ? 'Exporting' : 'Restoring'} local installation data`, process.execPath,
67
+ const child = store.spawn(`${direction === 'policy-preflight' ? 'Checking archive policy' : direction === 'legacy-preflight' ? 'Checking empty recovery destination' : direction === 'export' ? 'Exporting' : 'Restoring'} local installation data`, process.execPath,
52
68
  [worker, direction, JSON.stringify(input)], { cwd: input.root, signal, env: { ...process.env, WRANGLER_SEND_METRICS: 'false' } });
53
69
  let output = '';
54
70
  child.stdout.on('data', chunk => { output += chunk; });
@@ -0,0 +1,67 @@
1
+ // Resolve the checkout's package manager before its own rer can be loaded.
2
+ // The verified cache is private to rer; the resulting PATH travels to children.
3
+ import { mkdtemp, readFile, rm } from 'node:fs/promises';
4
+ import { delimiter, dirname, join } from 'node:path';
5
+ import { homedir, tmpdir } from 'node:os';
6
+ import { cachedPnpm, downloadPnpm } from './pnpm-runtime.mjs';
7
+ import { missing, RerError } from './errors.mjs';
8
+ import { shellQuote } from './node-runtime-cache.mjs';
9
+ import { probePnpm } from './pnpm-probe.mjs';
10
+ import { shared } from './shared.mjs';
11
+
12
+ export async function projectPnpmPin(root) {
13
+ let pkg;
14
+ try { pkg = JSON.parse(await readFile(join(root, 'package.json'), 'utf8')); } catch {}
15
+ const pin = /^pnpm@(\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?)(?:\+sha\d+\.[0-9a-f]+)?$/.exec(pkg?.packageManager ?? '')?.[1];
16
+ if (!pin) throw missing('This first-party checkout must declare an exact pnpm version in package.json packageManager. Correct that declaration, then rerun rer.');
17
+ return pin;
18
+ }
19
+ async function probe(root, env, signal, store) {
20
+ try { return await probePnpm(root, store, signal, env); }
21
+ catch (error) { if (signal?.aborted) throw error; return undefined; }
22
+ }
23
+ export async function ensureProjectPnpm(root, options = {}) {
24
+ if (options.store) return ensure(root, options);
25
+ // Global bootstrap has no adapter or lifecycle store yet. Give it the same
26
+ // command custody, with a temporary log and cancellation lasting through the
27
+ // download, both probes, and cleanup.
28
+ const cancellation = new AbortController();
29
+ const signal = AbortSignal.any([options.signal, cancellation.signal].filter(Boolean));
30
+ let termination, directory, store;
31
+ const handlers = new Map(['SIGINT', 'SIGTERM'].map(name => [name, () => { termination ??= name; cancellation.abort(name); }]));
32
+ for (const [name, handler] of handlers) process.on(name, handler);
33
+ try {
34
+ // Invalid manifests refuse before acquiring command custody or tools.
35
+ const pin = await projectPnpmPin(root);
36
+ directory = await mkdtemp(join(tmpdir(), 'rer-prerequisites-'));
37
+ const { createCommands } = await shared('commands.mjs');
38
+ store = await createCommands(directory, { logs: [] }, { terminal: () => {} });
39
+ store.setCommandSignal(signal);
40
+ return await ensure(root, { ...options, signal, store }, pin);
41
+ } finally {
42
+ try { await store?.closeCommands(); if (directory) await rm(directory, { recursive: true, force: true }); }
43
+ finally { for (const [name, handler] of handlers) process.removeListener(name, handler); }
44
+ if (termination) throw new RerError('cancelled', `Stopped by ${termination} after owned children were stopped.`, termination === 'SIGINT' ? 130 : 143);
45
+ }
46
+ }
47
+ async function ensure(root, { env = process.env, signal, report = () => {}, store }, pin) {
48
+ pin ??= await projectPnpmPin(root);
49
+ if (await probe(root, env, signal, store) === pin) return env;
50
+ let directory = await cachedPnpm(pin, env);
51
+ if (!directory) {
52
+ report(`Fetching pnpm ${pin} for this project; rer keeps it in its own cache.`);
53
+ try { directory = await downloadPnpm(pin, signal, { env }); }
54
+ catch (error) {
55
+ if (signal?.aborted || error instanceof RerError) throw error;
56
+ const prefix = join(env.HOME || homedir(), '.rer', 'pnpm', `manual-${pin}`);
57
+ throw missing(`pnpm ${pin} could not be fetched (${error.message}). Retry with a working connection, or run in this shell: npm install --prefix ${shellQuote(prefix)} --no-save pnpm@${pin} && export PATH=${shellQuote(join(prefix, 'node_modules/.bin'))}:"$PATH"`);
58
+ }
59
+ }
60
+ // Select the verified pnpm before any tool beside Node. Its shim still finds
61
+ // this process's Node before the caller's PATH, but cannot be shadowed by a
62
+ // different pnpm installed in that Node's bin directory.
63
+ const supplied = { ...env, PATH: [directory, dirname(process.execPath), env.PATH].filter(Boolean).join(delimiter) };
64
+ if (await probe(root, supplied, signal, store) !== pin) throw missing(`The verified pnpm ${pin} did not report the project version. Run ${shellQuote(join(directory, 'pnpm'))} --version to inspect it, then rerun rer.`);
65
+ Object.assign(env, supplied);
66
+ return env;
67
+ }
package/cli/release.mjs CHANGED
@@ -1,5 +1,40 @@
1
1
  // Preserve release's accepted parser, producer, receipt and validation exits.
2
+ const releaseHelp = `Usage: rer release --repo DIR --project PUBLISHER/NAME --repository GITHUB_URL --output NEW_DIR [FLAGS]
3
+
4
+ Produce local release artifacts after frozen installation, verification and publication checks.
5
+ Without --tag, write a preview. This command never pushes, uploads or deploys.
6
+
7
+ --repo DIR Source Git repository.
8
+ --project PUBLISHER/NAME
9
+ Publishing identity.
10
+ --repository GITHUB_URL
11
+ Credential-free https://github.com/<owner>/<repository> destination.
12
+ --output NEW_DIR New output directory outside the source repository.
13
+ --ref REF Selected committed source (default: HEAD).
14
+ --tag [vVERSION] Prepare source artifacts and create a local annotated tag;
15
+ an explicit tag must match the committed release version.
16
+ --dry-run Run the real gates and suppress local tag creation.
17
+ --clean-history Prepare a parentless public export.
18
+ --onto GITHUB_URL#BRANCH
19
+ Prepare a public child of the destination's current branch;
20
+ cannot be combined with --clean-history.
21
+ --env-file FILE Credential file for publication inspection.
22
+ --no-queue Bypass the job queue only in the original CI environment.
23
+ --help Print this help without performing release work.
24
+
25
+ Signing and receipt commands:
26
+ rer release sign --artifact DIR (--key-file FILE | --key-stdin) [--scope test|rer]
27
+ rer release receipt --artifact DIR --anchors FILE [--environment test] [--after VERSION:SHA256]
28
+ rer release keygen --scope test|rer --publisher NAMESPACE --github-owner LOGIN --github-owner-id ID --anchor-out FILE
29
+ [--not-before YYYY-MM-DDTHH:MM:SSZ] [--not-after YYYY-MM-DDTHH:MM:SSZ]
30
+ [--unsigned-through PROJECT:VERSION ...]
31
+
32
+ Signing scope defaults to rer; receipt environment defaults to production.
33
+ Key generation requires non-terminal stdout; --key-stdin requires non-terminal stdin.
34
+ `;
35
+
2
36
  export async function dispatchRelease(args) {
37
+ if (args.length === 1 && args[0] === '--help') { console.log(releaseHelp); return 0; }
3
38
  if (['sign', 'receipt', 'keygen'].includes(args[0])) return (await import('../modules/release/signing-cli.mjs')).dispatchSigning(args[0], args.slice(1));
4
39
  try {
5
40
  const options = {};
@@ -1,6 +1,6 @@
1
1
  import { createConnection, createServer } from 'node:net';
2
2
  import { randomUUID, createHash } from 'node:crypto';
3
- import { userInfo, tmpdir } from 'node:os';
3
+ import { homedir, tmpdir } from 'node:os';
4
4
  import { chmod, realpath, unlink, lstat, mkdir } from 'node:fs/promises';
5
5
  import { join, resolve } from 'node:path';
6
6
  import { setTimeout as delay } from 'node:timers/promises';
@@ -80,7 +80,7 @@ function limitMessage(counted,cap,explicit,mode) {
80
80
  // the old default of 2 in records written before this rule) never lowers the cap
81
81
  // below the current default.
82
82
  export const defaultServerLimit=serverDefaults.serverLimit;
83
- export const serversDirectory=(env=process.env)=>resolve(env.RER_SERVERS_DIR??join(userInfo().homedir,'.rer/servers'));
83
+ export const serversDirectory=(env=process.env)=>resolve(env.RER_SERVERS_DIR??join((env.HOME || homedir()),'.rer/servers'));
84
84
  export async function serverRegistry(root,{env=process.env,memory=availableMemory}={}) {
85
85
  const floor=memoryFloor(env),explicit=env.RER_SERVER_LIMIT!==undefined,limit=positive(env.RER_SERVER_LIMIT,'RER_SERVER_LIMIT',defaultServerLimit),journey=declaresJourney(env);
86
86
  const checkout=await realpath(root), lifetime=await identity();
@@ -3,7 +3,7 @@ import { join, posix } from 'node:path';
3
3
  import { createRequire } from 'node:module';
4
4
  import { pathToFileURL } from 'node:url';
5
5
  import { privateDataScanner, decodedText } from './site-data.mjs';
6
- import { javascriptContent } from './site-references.mjs';
6
+ import { javascriptContent, javascriptContext } from './site-references.mjs';
7
7
  import { javascriptStrings } from './site-syntax.mjs';
8
8
  import { markupReferences, markupStrings, htmlElements, cssStrings } from './site-markup.mjs';
9
9
  import { refuse, RerError } from './errors.mjs';
@@ -77,15 +77,24 @@ export async function checkSite(folder, { privateValues = [], privateIdentities
77
77
  const require = createRequire(join(root, 'package.json'));
78
78
  ({parseAst:parse} = await import(pathToFileURL(require.resolve('vite')).href));
79
79
  }
80
+ const documents = [], treesByFile = new Map();
81
+ for (const [site, bytes] of all) {
82
+ const text = bytes.toString('utf8'), trees = /\.[cm]?js$/.test(site) && parse ? [parse(text)] : [];
83
+ if (/\.(?:html|svg)$/.test(site)) {
84
+ documents.push(text);
85
+ for (const {tag, attributes, content} of htmlElements(text)) {
86
+ if (tag === 'script' && !attributes.has('src') && parse && /^(?:|module|(?:text|application)\/(?:java|ecma)script)$/i.test(attributes.get('type') ?? '')) trees.push(parse(content));
87
+ }
88
+ }
89
+ treesByFile.set(site, trees);
90
+ }
91
+ const context = javascriptContext(documents, [...treesByFile.values()].flat());
80
92
  const problems = [], unresolved = [];
81
93
  for (const [site, bytes] of all) {
82
94
  if (markers.some(marker => bytes.includes(marker))) throw refuse(`The run site file ${site} contains the local-only dev layer.`);
83
95
  const html = /\.(?:html|svg)$/.test(site), css = site.endsWith('.css'), js = /\.[cm]?js$/.test(site);
84
- const text = bytes.toString('utf8'), trees = js && parse ? [parse(text)] : [], literals = css ? cssStrings(text) : html ? markupStrings(text) : [];
85
- if (html) for (const {tag, attributes, content} of htmlElements(text)) {
86
- if (tag === 'script' && !attributes.has('src') && parse && /^(?:|module|(?:text|application)\/(?:java|ecma)script)$/i.test(attributes.get('type') ?? '')) trees.push(parse(content));
87
- }
88
- const scripts = trees.map(javascriptContent), strings = trees.flatMap(javascriptStrings);
96
+ const text = bytes.toString('utf8'), trees = treesByFile.get(site), literals = css ? cssStrings(text) : html ? markupStrings(text) : [];
97
+ const scripts = trees.map(tree => javascriptContent(tree, context)), strings = trees.flatMap(javascriptStrings);
89
98
  if (containsPrivateData(bytes, [...literals, ...strings, ...strings.flatMap(markupStrings), ...scripts.flatMap(script => script.literals)])) throw new RerError('site_private_data', `The run site file ${site} contains this installation's private data (owner identity or a secret). The run site renders only edition identity.`, 4);
90
99
  if (!html && !css && !js) continue;
91
100
  const refs = [...(js ? [] : markupReferences(text, html)), ...scripts.flatMap(script => script.references)];
package/cli/site-data.mjs CHANGED
@@ -22,8 +22,9 @@ export function decodedText(text) {
22
22
  export function privateDataScanner(values, identities) {
23
23
  const secrets = values.filter(value => typeof value === 'string' && value.length);
24
24
  const owners = identities.filter(value => typeof value === 'string' && value.length).map(value => ({value, ...identityPattern(value)}));
25
- const encodedValues = [...secrets, ...owners.map(item => item.value)].flatMap(value => [Buffer.from(value).toString('base64'), Buffer.from(value).toString('base64').replace(/=+$/, ''), Buffer.from(value).toString('base64url')]);
26
- const base64 = encodedValues.map(value => new RegExp(`(?<![A-Za-z0-9+/_-])${value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}(?![A-Za-z0-9+/_=-])`));
25
+ const encodings = value => [Buffer.from(value).toString('base64'), Buffer.from(value).toString('base64').replace(/=+$/, ''), Buffer.from(value).toString('base64url')];
26
+ const encodedSecrets = secrets.flatMap(encodings);
27
+ const encodedOwners = owners.flatMap(({value}) => encodings(value)).map(value => new RegExp(`(?<![A-Za-z0-9+/_-])${value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}(?![A-Za-z0-9+/_=-])`));
27
28
  return (bytes, literalValues = []) => {
28
29
  if (secrets.some(value => bytes.includes(Buffer.from(value)))) return true;
29
30
  const raw = bytes.toString('utf8');
@@ -42,10 +43,10 @@ export function privateDataScanner(values, identities) {
42
43
  forms.add(plain); forms.add(decodedText(plain));
43
44
  }
44
45
  if (secrets.some(value => form.includes(value)) || owners.some(({pattern}) => pattern.test(form))) return true;
45
- if (base64.some(pattern => pattern.test(form))) return true;
46
+ if (encodedSecrets.some(value => form.includes(value)) || encodedOwners.some(pattern => pattern.test(form))) return true;
46
47
  // A base64 payload may include surrounding text, changing its alignment.
47
- for (const match of form.matchAll(/[A-Za-z0-9+/_-]{8,}={0,2}/g)) {
48
- const plain = Buffer.from(match[0], 'base64').toString('utf8');
48
+ for (const match of form.matchAll(/[A-Za-z0-9+/_-]{8,}={0,2}/g)) for (let offset = 0; offset < 4; offset++) {
49
+ const plain = Buffer.from(match[0].slice(offset), 'base64').toString('utf8');
49
50
  if (secrets.some(value => plain.includes(value)) || owners.some(({pattern}) => pattern.test(plain))) return true;
50
51
  }
51
52
  }