@runeditrun/rer 0.2.80 → 0.2.82

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 (99) hide show
  1. package/README.md +12 -0
  2. package/cli/deploy-needs.mjs +4 -2
  3. package/cli/export.mjs +1 -1
  4. package/cli/ledger-refusal.mjs +18 -0
  5. package/cli/portability-migrations.mjs +30 -11
  6. package/cli/portability-worker.mjs +3 -1
  7. package/cli/portability.mjs +2 -0
  8. package/cli/release.mjs +4 -4
  9. package/cli/site-hosting.mjs +90 -0
  10. package/cli/site-render.mjs +3 -2
  11. package/cli/site.mjs +5 -2
  12. package/edition-authoring/base-copies.mjs +12 -1
  13. package/edition-authoring/copy-checkout.mjs +152 -1
  14. package/edition-authoring/files.mjs +1 -1
  15. package/edition-authoring/runtime-dependencies.mjs +108 -0
  16. package/global/CHANGELOG.md +25 -0
  17. package/global/README.md +12 -0
  18. package/modules/demo/README.md +8 -0
  19. package/modules/demo/navigation.mjs +131 -15
  20. package/modules/dependencies/auth.mjs +2 -2
  21. package/modules/deploy-kit/owner.mjs +8 -1
  22. package/modules/import-kit/src/d1/apply-effects.ts +33 -0
  23. package/modules/import-kit/src/d1/apply.ts +38 -10
  24. package/modules/import-kit/src/door/ADOPTING.md +452 -0
  25. package/modules/import-kit/src/door/README.md +553 -0
  26. package/modules/import-kit/src/door/admission.ts +45 -0
  27. package/modules/import-kit/src/door/archive.ts +129 -0
  28. package/modules/import-kit/src/door/authority.ts +107 -0
  29. package/modules/import-kit/src/door/failures.ts +70 -0
  30. package/modules/import-kit/src/door/grants.ts +162 -0
  31. package/modules/import-kit/src/door/handler.ts +90 -0
  32. package/modules/import-kit/src/door/index.ts +20 -0
  33. package/modules/import-kit/src/door/intake/csv.ts +74 -0
  34. package/modules/import-kit/src/door/intake/json.ts +182 -0
  35. package/modules/import-kit/src/door/intake/projection.ts +61 -0
  36. package/modules/import-kit/src/door/intake/types.ts +28 -0
  37. package/modules/import-kit/src/door/intake-policy.ts +38 -0
  38. package/modules/import-kit/src/door/intake-upload.ts +91 -0
  39. package/modules/import-kit/src/door/inventory.ts +68 -0
  40. package/modules/import-kit/src/door/jobs.ts +134 -0
  41. package/modules/import-kit/src/door/limits.ts +16 -0
  42. package/modules/import-kit/src/door/operator.ts +41 -0
  43. package/modules/import-kit/src/door/planning.ts +227 -0
  44. package/modules/import-kit/src/door/protocol.ts +38 -0
  45. package/modules/import-kit/src/door/refusals.ts +51 -0
  46. package/modules/import-kit/src/door/schema.ts +101 -0
  47. package/modules/import-kit/src/door/store.ts +152 -0
  48. package/modules/import-kit/src/door/types.ts +100 -0
  49. package/modules/import-kit/src/door/uploads.ts +98 -0
  50. package/modules/import-kit/src/provenance/numeric-credentials.ts +40 -0
  51. package/modules/import-kit/src/readers/README.md +139 -0
  52. package/modules/import-kit/src/readers/api-policy.ts +67 -0
  53. package/modules/import-kit/src/readers/api.ts +86 -0
  54. package/modules/import-kit/src/readers/bytes.ts +73 -0
  55. package/modules/import-kit/src/readers/errors.ts +33 -0
  56. package/modules/import-kit/src/readers/file.ts +74 -0
  57. package/modules/import-kit/src/readers/index.ts +3 -0
  58. package/modules/import-kit/src/readers/json.ts +29 -0
  59. package/modules/import-kit/src/readers/numeric-lexemes.ts +104 -0
  60. package/modules/import-kit/src/readers/original.ts +31 -0
  61. package/modules/import-kit/src/readers/records.ts +105 -0
  62. package/modules/import-kit/src/types.ts +3 -1
  63. package/modules/operator-auth/README.md +92 -3
  64. package/modules/operator-auth/accounts/admin.mjs +52 -20
  65. package/modules/operator-auth/accounts/audit.mjs +24 -0
  66. package/modules/operator-auth/accounts/business-team.mjs +91 -0
  67. package/modules/operator-auth/accounts/domain-audit.mjs +24 -0
  68. package/modules/operator-auth/accounts/native.mjs +3 -1
  69. package/modules/operator-auth/accounts/settings-page.mjs +15 -2
  70. package/modules/operator-auth/accounts/status.mjs +7 -2
  71. package/modules/operator-auth/accounts/store.mjs +32 -6
  72. package/modules/operator-auth/business-settings.mjs +40 -0
  73. package/modules/operator-auth/identity/email-link/index.mjs +1 -1
  74. package/modules/operator-auth/identity/oidc/admin-audit.mjs +31 -0
  75. package/modules/operator-auth/identity/oidc/index.mjs +12 -5
  76. package/modules/operator-auth/identity/oidc/store.mjs +2 -0
  77. package/modules/operator-auth/index.d.mts +40 -1
  78. package/modules/operator-auth/routes.mjs +11 -6
  79. package/modules/operator-auth/service.mjs +8 -3
  80. package/modules/operator-auth/signin/index.mjs +1 -1
  81. package/modules/portability/README.md +82 -1
  82. package/modules/portability/export.mjs +6 -4
  83. package/modules/portability/format.mjs +5 -0
  84. package/modules/portability/index.d.mts +10 -2
  85. package/modules/portability/index.mjs +3 -2
  86. package/modules/portability/ledgers.mjs +27 -4
  87. package/modules/release/README.md +74 -2
  88. package/modules/release/sign.mjs +4 -2
  89. package/modules/release/signature.mjs +14 -7
  90. package/modules/release/signing-cli.mjs +2 -1
  91. package/modules/release/statement.mjs +2 -2
  92. package/modules/release/vectors/generate.mjs +1 -0
  93. package/modules/release/vectors/publisher-projects.mjs +133 -0
  94. package/modules/release/vectors/release-signature-vectors.json +5195 -0
  95. package/modules/release/verify.mjs +5 -1
  96. package/onboarding/schema/SETUP.schema.json +43 -1
  97. package/onboarding/schema/business-field-inputs.mjs +0 -1
  98. package/onboarding/schema/setup-validation.mjs +3 -0
  99. package/package.json +3 -2
package/README.md CHANGED
@@ -337,6 +337,7 @@ line that still names it. A later refresh is a version bump:
337
337
  | `@runeditrun/rer/modules/deploy-kit/owner`, `/owner-cli`, `/prepare`, `/provider` | deploy kit | `scripts/base-cli/modules/deploy-kit/*.mjs` |
338
338
  | `@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` |
339
339
  | `@runeditrun/rer/modules/import-kit/d1` | asynchronous D1 import, reviewed reversal-policy conversion and preserved-undo preview (TypeScript Worker entry) | `scripts/base-cli/modules/import-kit/src/d1/index.ts` |
340
+ | `@runeditrun/rer/modules/import-kit/door` | reviewed operator and grant intake, plan, apply, reconciliation and Undo (TypeScript Worker entry) | `scripts/base-cli/modules/import-kit/src/door/index.ts` |
340
341
  | `@runeditrun/rer/modules/erasure/fence` | erasure fence statements | `scripts/base-cli/modules/erasure/fence.mjs` |
341
342
  | `@runeditrun/rer/modules/owner-claim`, `/modules/dependencies` | owner claim, dependency links | `scripts/base-cli/modules/owner-claim/index.mjs`, `dependencies/index.mjs` |
342
343
  | `@runeditrun/rer/internal/PATH` | any other package file, for tests | `scripts/base-cli/PATH`, and `scripts/base-onboarding/PATH` with PATH under `onboarding/` |
@@ -611,3 +612,14 @@ setup process is still running, `confirm-no-turn-started JOB_ID
611
612
  checkout changed or the provider recorded a session for it. It reads the
612
613
  checkout with Git inside the same boundary, because setup could have changed
613
614
  the checkout's Git configuration.
615
+
616
+
617
+ ## Import door for adopting Workers
618
+
619
+ Bundle `@runeditrun/rer/modules/import-kit/door` to use the maintained
620
+ `createImportOperatorHandler` or `createImportDoorHandler`, schema migrations,
621
+ settings hooks and public option types. This TypeScript entry includes its real
622
+ runtime and type dependencies. The existing
623
+ `@runeditrun/rer/modules/import-kit/d1` entry remains available. See the
624
+ [adoption guide](https://github.com/runeditrun/base/blob/main/modules/import-kit/src/door/ADOPTING.md)
625
+ for native adapters, consent, declared intake projection and reviewed apply/Undo.
@@ -21,12 +21,14 @@ export function deployGuide(declaration) {
21
21
  return value;
22
22
  }
23
23
 
24
+ /** The account permissions an API token needs to deploy this declaration (the run site's /docs/hosting lists the same). */
25
+ export const deployPermissions = declaration => permissions.filter(([, used]) => used(declaration)).map(([name]) => name);
26
+
24
27
  // Where a person gets the Cloudflare values: the project's guide, else the token page and the permissions.
25
28
  function credentialHelp(declaration) {
26
29
  const guide = deployGuide(declaration);
27
30
  if (guide) return [`Follow this project's deploy guide: ${guide}`];
28
- const needed = permissions.filter(([, used]) => used(declaration)).map(([name]) => name);
29
- return [`Create an account API token at ${tokenPage} with these account permissions: ${needed.join(', ')}. Copy your account ID from the Cloudflare dashboard.`];
31
+ return [`Create an account API token at ${tokenPage} with these account permissions: ${deployPermissions(declaration).join(', ')}. Copy your account ID from the Cloudflare dashboard.`];
30
32
  }
31
33
 
32
34
  export function missingCredentialsMessage(declaration, env, { dryRun = false, signIn } = {}) {
package/cli/export.mjs CHANGED
@@ -6,7 +6,7 @@ import { cliVersion } from './version.mjs';
6
6
 
7
7
  // Copy whole maintained source/tooling directories. Python publication scans
8
8
  // load sibling modules and data dynamically; they belong in the inventory too.
9
- export const sourceRoots = ['default-seed/public/rer-theme.css', 'default-seed/public/brand/mark.svg', 'default-seed/public/fonts/outfit', 'bin/rer.mjs', 'cli', 'modules/delivery-producer/declarations.mjs', 'modules/delivery-producer/bindings.mjs', 'modules/delivery-producer/migrations.mjs', 'modules/delivery-producer/mail.mjs', 'modules/job-queue', 'modules/release', 'modules/deploy-kit', 'modules/site', 'modules/container', 'modules/demo-host', 'modules/demo', 'modules/integrations/core', 'modules/mail', 'modules/ai', 'modules/portability', 'modules/d1-transport', 'modules/import-kit/src/d1', 'modules/import-kit/src/types.ts', 'modules/import-kit/src/plan', 'modules/import-kit/src/provenance/canonical-hash.ts', 'modules/import-kit/src/provenance/canonical-stream.ts', 'modules/import-kit/src/provenance/credential-names.ts', 'modules/import-kit/src/provenance/credentials.ts', 'modules/import-kit/src/provenance/identity.ts', 'modules/import-kit/src/provenance/json-snapshot.ts', 'modules/import-kit/src/provenance/resumable-sha256.ts', 'modules/import-kit/src/provenance/validate-json.ts', 'modules/import-kit/src/journal/authority.ts', 'modules/import-kit/src/storage/originals.ts', 'modules/import-kit/src/storage/provenance.ts', 'modules/import-kit/src/storage/schema.ts', 'modules/erasure', 'modules/dependencies', 'modules/operator-auth', 'modules/owner-claim', 'operations', 'scripts/publish-check', 'scripts/publish-check.mjs', 'composition/git-source.mjs', 'edition-authoring/adopt.mjs', 'edition-authoring/base-acknowledgements.mjs', 'edition-authoring/base-copies.mjs', 'edition-authoring/cli.mjs', 'edition-authoring/copy-checkout.mjs', 'edition-authoring/files.mjs', 'edition-authoring/lock.mjs', 'edition-authoring/manifest.mjs', 'edition-authoring/offline-git.mjs', 'edition-authoring/parent-comparison.mjs', 'edition-authoring/projection-lock.mjs', 'edition-authoring/release.mjs', 'edition-authoring/sources.mjs', 'edition-authoring/updates.mjs', 'foundation/runtime/seed-contract.mjs'];
9
+ export const sourceRoots = ['default-seed/public/rer-theme.css', 'default-seed/public/brand/mark.svg', 'default-seed/public/fonts/outfit', 'bin/rer.mjs', 'cli', 'modules/delivery-producer/declarations.mjs', 'modules/delivery-producer/bindings.mjs', 'modules/delivery-producer/migrations.mjs', 'modules/delivery-producer/mail.mjs', 'modules/job-queue', 'modules/release', 'modules/deploy-kit', 'modules/site', 'modules/container', 'modules/demo-host', 'modules/demo', 'modules/integrations/core', 'modules/mail', 'modules/ai', 'modules/portability', 'modules/d1-transport', 'modules/import-kit/src/d1', 'modules/import-kit/src/door', 'modules/import-kit/src/readers', 'modules/import-kit/src/provenance/numeric-credentials.ts', 'modules/import-kit/src/types.ts', 'modules/import-kit/src/plan', 'modules/import-kit/src/provenance/canonical-hash.ts', 'modules/import-kit/src/provenance/canonical-stream.ts', 'modules/import-kit/src/provenance/credential-names.ts', 'modules/import-kit/src/provenance/credentials.ts', 'modules/import-kit/src/provenance/identity.ts', 'modules/import-kit/src/provenance/json-snapshot.ts', 'modules/import-kit/src/provenance/resumable-sha256.ts', 'modules/import-kit/src/provenance/validate-json.ts', 'modules/import-kit/src/journal/authority.ts', 'modules/import-kit/src/storage/originals.ts', 'modules/import-kit/src/storage/provenance.ts', 'modules/import-kit/src/storage/schema.ts', 'modules/erasure', 'modules/dependencies', 'modules/operator-auth', 'modules/owner-claim', 'operations', 'scripts/publish-check', 'scripts/publish-check.mjs', 'composition/git-source.mjs', 'edition-authoring/adopt.mjs', 'edition-authoring/base-acknowledgements.mjs', 'edition-authoring/base-copies.mjs', 'edition-authoring/cli.mjs', 'edition-authoring/copy-checkout.mjs', 'edition-authoring/files.mjs', 'edition-authoring/lock.mjs', 'edition-authoring/manifest.mjs', 'edition-authoring/offline-git.mjs', 'edition-authoring/parent-comparison.mjs', 'edition-authoring/projection-lock.mjs', 'edition-authoring/release.mjs', 'edition-authoring/runtime-dependencies.mjs', 'edition-authoring/sources.mjs', 'edition-authoring/updates.mjs', 'foundation/runtime/seed-contract.mjs'];
10
10
  export async function inventory(base, roots = sourceRoots) {
11
11
  const files = [];
12
12
  async function walk(path) {
@@ -0,0 +1,18 @@
1
+ // What a person does about each migration-history refusal (portability-migrations.mjs, modules/portability/ledgers.mjs).
2
+ // Codes are constant categories; a migration named by an archive or the checkout follows the second colon.
3
+ const fixes = {
4
+ unknown: 'the archive records a migration whose file name or content this checkout does not have. Restore with the checkout version that exported it (or a later one), with its migration files unchanged.',
5
+ conflict: 'a migration in the archive has the name of one checkout file and the content of another. Restore with the checkout version that exported it, with its migration files unchanged.',
6
+ ambiguous: 'one migration identity matches several checkout files or ledger entries. Keep a single file per migration and declare the seed\'s migration prefix.',
7
+ unapplied: 'the installation\'s history lacks a migration this checkout ships. Apply it with the runner that owns the history (RER Platform for a Managed installation), then retry.',
8
+ multiple: 'the database holds more than one migration ledger. Reconcile its history with the runner that owns it.',
9
+ uncertain: 'a migration was left partly applied. Finish or roll back that operation with its runner, then retry.',
10
+ runner: 'the history belongs to the older Base deploy runner, which can adopt it. Use that runner first.',
11
+ incompatible: 'the migration ledger or the checkout declaration has an unsupported shape. Use the matching checkout and its declaration.',
12
+ binding: 'the checkout declares no single D1 binding with that name. Use the matching checkout.',
13
+ };
14
+ export function ledgerRefusal(receipt) {
15
+ const [, kind, ...name] = receipt.reason.split(':');
16
+ const named = receipt.unapplied ?? (name.length ? name.join(':') : undefined);
17
+ return `Migration history refused (${receipt.reason}): ${named ? `migration ${named}: ` : ''}${fixes[kind] ?? 'its history does not match this checkout.'}`;
18
+ }
@@ -35,6 +35,33 @@ export async function appliedMigrations(root,db,binding,declaration,split) {
35
35
  return matchMigrationLedger(ledger,files,declaration).map(name=>files.find(file=>file.name===name));
36
36
  }
37
37
 
38
+ // Ledger contents can contain private values. Report constant categories,
39
+ // retaining the transfer's canonical compatibility and hash-matching policy.
40
+ function constantRefusal(error) {
41
+ if(!(error instanceof PortabilityError))return error;
42
+ const code=error.code.startsWith('migration_ledger:')?error.code.split(':').slice(0,2).join(':')
43
+ :error.code==='schema_mismatch:uncertain_migrations'?'migration_ledger:uncertain':'migration_ledger:incompatible';
44
+ return Object.assign(new PortabilityError(code,409),error.migration===undefined?{}:{migration:error.migration});
45
+ }
46
+
47
+ // For adapters with their own archive format: the entries of a migration ledger
48
+ // ([{name,sha256}], as RER Platform's rer_migrations records them or as an
49
+ // archive carries them) named by the checkout's migration files, matched by
50
+ // SHA-256 and refused when a name or hash differs from the checkout's. The
51
+ // result is the native-equivalent ledger: write it to an export, or compare it
52
+ // with the archive on import, instead of comparing ledger controllers.
53
+ // Declared data-only entries keep their own names. Reads files only.
54
+ export async function mapMigrationLedger({root,binding='DB',platformMigrations,entries}) {
55
+ const declaration=platformMigrationDeclaration(platformMigrations);
56
+ insist(Array.isArray(entries) && entries.every(entry=>entry && typeof entry.name==='string' && typeof entry.sha256==='string' && /^[a-f0-9]{64}$/.test(entry.sha256)) && new Set(entries.map(entry=>entry.name)).size===entries.length,'migration_ledger:incompatible',409);
57
+ const files=await inventory(root,binding,declaration);
58
+ try {
59
+ const mapped=entries.map(({name,sha256})=>({name:matchMigrationLedger([{name,sha256}],files,declaration)[0]??name,sha256}));
60
+ insist(new Set(mapped.map(entry=>entry.name)).size===mapped.length,'migration_ledger:ambiguous',409);
61
+ return mapped;
62
+ } catch(error) { throw constantRefusal(error); }
63
+ }
64
+
38
65
  // Adapter entry point, before *any* native Wrangler apply. The caller supplies
39
66
  // the real target binding and keeps its writers stopped through apply/transfer.
40
67
  // This only reads: it never repairs history or creates a second ledger.
@@ -52,16 +79,8 @@ export async function nativeMigrationPreflight({root,db,binding='DB',platformMig
52
79
  const ledger=await destinationLedger(db),applied=matchMigrationLedger(ledger,files,declaration);
53
80
  // Wrangler skips only literal filenames, not transfer aliases or hashes.
54
81
  if(table==='d1_migrations')insist(ledger.every(row=>files.some(file=>file.name===row.name)),'migration_ledger:incompatible',409);
55
- if(table==='rer_migrations')for(const file of files)insist(applied.includes(file.name),'migration_ledger:unapplied',409);
56
- } catch(error) {
57
- // Ledger contents can contain private values. Report constant categories,
58
- // retaining the transfer's canonical compatibility and hash-matching policy.
59
- if(error instanceof PortabilityError) {
60
- const code=error.code.startsWith('migration_ledger:')?error.code.split(':').slice(0,2).join(':')
61
- :error.code==='schema_mismatch:uncertain_migrations'?'migration_ledger:uncertain':'migration_ledger:incompatible';
62
- throw new PortabilityError(code,409);
63
- }
64
- throw error;
65
- }
82
+ // The checkout file is named: it is the seed's own, never a ledger value.
83
+ if(table==='rer_migrations')for(const file of files)if(!applied.includes(file.name))throw Object.assign(new PortabilityError('migration_ledger:unapplied',409),{migration:file.name});
84
+ } catch(error) { throw constantRefusal(error); }
66
85
  return {nativeApply:table==='d1_migrations'};
67
86
  }
@@ -74,6 +74,8 @@ catch (error) {
74
74
  const reason = error instanceof PortabilityError ? (error.code.startsWith('migration_ledger:') ? error.code : error.code.split(':')[0]) : /^[A-Z_]+$/.test(error.code ?? '') ? error.code : error.message === 'archive_json' ? 'archive_json' : 'native_error';
75
75
  // A later migration that changes restorable data is named: it is a file of this installation.
76
76
  const migration = error instanceof PortabilityError && error.code.startsWith('schema_mismatch:migration_data:') ? error.code.slice('schema_mismatch:migration_data:'.length) : undefined;
77
+ // An unapplied migration is a file of this checkout: it is named so it can be applied.
78
+ const unapplied = error instanceof PortabilityError && error.code === 'migration_ledger:unapplied' ? error.migration : undefined;
77
79
  const reExport = error instanceof PortabilityError && ['schema_mismatch:legacy_schema','schema_mismatch:unsupported_sql'].includes(error.code);
78
80
  // A populated destination names its table or bucket: schema names, never row data.
79
81
  const occupied = error instanceof PortabilityError && /^(target|bucket)_not_empty:/.test(error.code) ? error.code.slice(error.code.indexOf(':') + 1) : undefined;
@@ -82,5 +84,5 @@ catch (error) {
82
84
  // Anything that is not a portability refusal names its class and code, never its message: that can hold row data.
83
85
  const native = error instanceof PortabilityError || error.message === 'archive_json' ? undefined : nativeFailure(error);
84
86
  process.exitCode = exitCode;
85
- await reply({ ok: false, code: exitCode === 2 ? 'invalid_archive' : exitCode === 4 ? 'restore_refused' : 'transfer_failed', reason, ...(reExport ? { reExport: true } : {}), ...(migration ? { migration } : {}), ...(occupied ? { occupied } : {}), ...(safety ? { safety } : {}), ...(native ? { native } : {}), exitCode });
87
+ await reply({ ok: false, code: exitCode === 2 ? 'invalid_archive' : exitCode === 4 ? 'restore_refused' : 'transfer_failed', reason, ...(reExport ? { reExport: true } : {}), ...(migration ? { migration } : {}), ...(unapplied ? { unapplied } : {}), ...(occupied ? { occupied } : {}), ...(safety ? { safety } : {}), ...(native ? { native } : {}), exitCode });
86
88
  }
@@ -7,6 +7,7 @@ import { fileRecords } from '../modules/portability/files.mjs';
7
7
  import { RerError, refuse } from './errors.mjs';
8
8
  import { policyModuleURL, serializablePolicy } from './portability-policy.mjs';
9
9
  import { shared } from './shared.mjs';
10
+ import { ledgerRefusal } from './ledger-refusal.mjs';
10
11
 
11
12
  const reExport = 'Re-export from the original installation with rer export --output exports/recovered.rer (add --target remote for a hosted installation), then retry rer import --input exports/recovered.rer --yes. Keep the original archive until recovery succeeds.';
12
13
 
@@ -88,6 +89,7 @@ async function nativeWorker(store,direction,input,signal) {
88
89
  : receipt.reason === 'archive_json' ? `Restore refused: the archive is damaged or incomplete. ${reExport}`
89
90
  : receipt.reExport ? `Restore refused (schema_mismatch): the schema definitions cannot be safely verified by this version. ${reExport}`
90
91
  : receipt.safety ? `Restore refused (${receipt.reason}): safety normalization for ${receipt.safety} violates the declared import policy.`
92
+ : receipt.reason?.startsWith('migration_ledger:') ? ledgerRefusal(receipt)
91
93
  : receipt.migration ? `Restore refused (schema_mismatch:migration_data): migration ${receipt.migration} changed data after this archive was made, so its rows would skip that change. Restore an archive made after it.`
92
94
  : receipt.occupied ? `Restore refused (${receipt.reason}): ${receipt.reason === 'bucket_not_empty' ? `storage bucket ${receipt.occupied} in this installation already holds files` : `table ${receipt.occupied} in this installation already holds rows its migrations did not insert`}, and a restore only goes into an empty installation. To replace this installation's data with the archive, keep a copy with rer export --output exports/before-restore.rer, then run rer reset --yes and rer import --input ${relative(input.root, input.path)} --yes again.`
93
95
  : `The portability module refused or failed the archive operation (${receipt.reason ?? 'incomplete'}${receipt.native ? `: ${receipt.native.class}${receipt.native.code && receipt.native.code !== receipt.reason ? `, code ${receipt.native.code}` : ''}` : ''}).${receipt.native && receipt.exitCode === 1 ? ' Run the same command again; it starts over.' : ''}`, receipt.exitCode ?? 1);
package/cli/release.mjs CHANGED
@@ -2,15 +2,14 @@ import { releaseVerb } from './contract.mjs';
2
2
  import { parseTimeoutSeconds, timeoutBounds } from '../scripts/publish-check/timeout.mjs';
3
3
 
4
4
  // Preserve release's accepted parser, producer, receipt and validation exits.
5
- const releaseHelp = `Usage: rer release --repo DIR --project PUBLISHER/NAME --repository GITHUB_URL --output NEW_DIR [FLAGS]
5
+ const releaseHelp = `Usage: rer release --repo DIR --project OWNER/NAME --repository GITHUB_URL --output NEW_DIR [FLAGS]
6
6
 
7
7
  Produce local artifacts using the exact source commit's green GitHub Verify,
8
8
  frozen installation, local build and publication checks.
9
9
  Without --tag, write a preview. This command never pushes, uploads or deploys.
10
10
 
11
11
  --repo DIR Source Git repository.
12
- --project PUBLISHER/NAME
13
- Publishing identity.
12
+ --project OWNER/NAME Project identity recorded by the edition.
14
13
  --repository GITHUB_URL
15
14
  Credential-free https://github.com/<owner>/<repository> destination.
16
15
  --output NEW_DIR New output directory outside the source repository.
@@ -40,13 +39,14 @@ Without --tag, write a preview. This command never pushes, uploads or deploys.
40
39
  --help Print this help without performing release work.
41
40
 
42
41
  Signing and receipt commands:
43
- rer release sign --artifact DIR (--key-file FILE | --key-stdin) [--scope test|rer]
42
+ rer release sign --artifact DIR (--key-file FILE | --key-stdin) [--scope test|rer] [--publisher NAMESPACE]
44
43
  rer release receipt --artifact DIR --anchors FILE [--environment test] [--after VERSION:SHA256]
45
44
  rer release keygen --scope test|rer --publisher NAMESPACE --github-owner LOGIN --github-owner-id ID --anchor-out FILE
46
45
  [--not-before YYYY-MM-DDTHH:MM:SSZ] [--not-after YYYY-MM-DDTHH:MM:SSZ]
47
46
  [--unsigned-through PROJECT:VERSION ...]
48
47
 
49
48
  Signing scope defaults to rer; receipt environment defaults to production.
49
+ Signing publisher defaults to the project prefix; use --publisher for a different publisher.
50
50
  Key generation requires non-terminal stdout; --key-stdin requires non-terminal stdin.
51
51
  `;
52
52
 
@@ -0,0 +1,90 @@
1
+ import { readFile } from 'node:fs/promises';
2
+ import { contained } from './project.mjs';
3
+ import { RerError } from './errors.mjs';
4
+ import { declarationFor } from './deploy-command.mjs';
5
+ import { deployGuide, deployPermissions } from './deploy-needs.mjs';
6
+ import { deliveryDeclarationSources } from './delivery-declarations.mjs';
7
+ import { ownerPlatforms, ownerWorkerName } from '../modules/deploy-kit/owner.mjs';
8
+
9
+ // What the run site's /docs/hosting tells a person about putting this seed on
10
+ // their own Cloudflare account, read from the declarations rer deploy reads: the
11
+ // deploy declaration, the setup manifest and the mail delivery declarations. It
12
+ // holds environment names, permissions and counts, never a value. `hosting` is
13
+ // null, with the reason, when rer deploy could not put this seed on Workers from
14
+ // them; the page then offers Run Edit Run's help alone. Never throws for what a
15
+ // seed declares, so a declaration gap never stops rer dev's run site.
16
+ const OWN = ['resend', 'sendgrid', 'cloudflare'];
17
+ const NATIVE = { resend: 'RESEND_API_KEY', sendgrid: 'SENDGRID_API_KEY' };
18
+
19
+ class Gap extends Error {}
20
+ async function json(root, path) {
21
+ let text;
22
+ try { text = await readFile(await contained(root, path, { optional: true }), 'utf8'); }
23
+ catch (error) { if (error.code === 'ENOENT') return undefined; throw new Gap(`${path} cannot be read`); }
24
+ try { return JSON.parse(text); } catch { throw new Gap(`${path} is not valid JSON`); }
25
+ }
26
+
27
+ /** { hosting } for /docs/hosting, or { hosting: null, reason } when the seed does not deploy to Workers from its declarations. */
28
+ export async function siteHosting({ root, adapter }) {
29
+ if (typeof adapter?.deploy !== 'function') return { hosting: null, reason: 'the command adapter has no deploy' };
30
+ try { return { hosting: await read(root, adapter) }; }
31
+ catch (error) {
32
+ // rer deploy's own refusals of the declaration (missing, ambiguous, invalid) are gaps too.
33
+ if (error instanceof Gap || error instanceof RerError || /^[a-z]+(?:-[a-z0-9]+)+$/.test(error?.message ?? '')) return { hosting: null, reason: error.message };
34
+ throw error;
35
+ }
36
+ }
37
+
38
+ async function read(root, adapter) {
39
+ const { path, value: declaration } = await declarationFor(root, adapter);
40
+ if (!ownerPlatforms(declaration).includes('workers')) throw new Gap(`${path} does not deploy to Workers`);
41
+ const guide = deployGuide(declaration);
42
+ const manifest = await json(root, adapter.setup?.manifestFile ?? 'rer-setup.json') ?? {};
43
+ const fields = new Map((Array.isArray(manifest.fields) ? manifest.fields : []).map(field => [field?.name, field]));
44
+ const steps = Array.isArray(manifest.steps) ? manifest.steps : [], claim = steps.find(step => step?.type === 'owner-claim');
45
+ const vars = declaration.vars ?? {}, secrets = (Array.isArray(declaration.secrets) ? declaration.secrets : []).filter(item => item?.binding && item.environment);
46
+ // What the deploy file can set, by binding: a declared secret under its environment name,
47
+ // and a mail declaration's inputs under their own (productionMail reads the same).
48
+ const input = new Map(secrets.map(item => [item.binding, item.environment])), offered = new Set();
49
+ for (const [source, select] of deliveryDeclarationSources) {
50
+ const spec = select(await json(root, source));
51
+ if (!spec) continue;
52
+ for (const name of [...(spec.publicVariableNames ?? []), ...(spec.secretNames ?? []), ...(spec.optionalSecretNames ?? [])]) if (!input.has(name)) input.set(name, name);
53
+ for (const provider of spec.mail?.providers ?? []) offered.add(provider);
54
+ }
55
+ for (const option of fields.get('MAIL_PROVIDER')?.options ?? []) offered.add(option?.value);
56
+ const sso = Array.isArray(manifest.dependencies) && manifest.dependencies.some(item => item?.interface === 'oidc-provider@1');
57
+ let mail;
58
+ if (claim || sso || 'MAIL_PROVIDER' in vars || input.has('MAIL_PROVIDER') || input.has('MAIL_API_KEY')) {
59
+ const fixedProvider = input.has('MAIL_PROVIDER') ? undefined : vars.MAIL_PROVIDER;
60
+ if (!input.has('MAIL_PROVIDER') && !OWN.includes(fixedProvider)) throw new Gap(`${path} fixes MAIL_PROVIDER to ${JSON.stringify(fixedProvider ?? null)} and the deploy file cannot choose one`);
61
+ // The provider key the declaration requires, else one it accepts.
62
+ const keys = ['MAIL_API_KEY', 'MAIL_PROVIDER_API_KEY', NATIVE[fixedProvider]].filter(Boolean), needed = name => secrets.some(item => item.binding === name && !item.optional);
63
+ const key = input.get(keys.find(needed)) ?? keys.map(name => input.get(name)).find(Boolean);
64
+ if (!key) throw new Gap(`${path} declares no input for the mail provider's API key`);
65
+ mail = { providers: OWN.filter(provider => offered.has(provider)), ...(input.has('MAIL_PROVIDER') ? { provider: input.get('MAIL_PROVIDER') } : { fixedProvider }),
66
+ ...(input.has('MAIL_FROM') ? { from: input.get('MAIL_FROM') } : {}), key, ...(input.has('MAIL_ACCOUNT_ID') ? { accountId: input.get('MAIL_ACCOUNT_ID') } : {}) };
67
+ }
68
+ // rer deploy generates the setup manifest's generated secrets and keeps the installation keys; every other
69
+ // required secret the owner supplies once, under its environment name (neededForRealDeploy lists the same).
70
+ const generated = new Set([...fields.values()].filter(field => field?.kind === 'secret' && field.generate).map(field => field.name));
71
+ const ownerEmail = claim?.emailField && input.get(claim.emailField);
72
+ const shown = new Set([ownerEmail, mail?.provider, mail?.from, mail?.key, mail?.accountId].filter(Boolean)), required = [];
73
+ for (const item of secrets) {
74
+ if (item.optional || generated.has(item.binding) || ['INSTALLATION_KEY_PREVIOUS', 'MAIL_RELAY_URL'].includes(item.binding) || shown.has(item.environment) || required.some(entry => entry.name === item.environment)) continue;
75
+ const field = fields.get(item.binding);
76
+ required.push({ name: item.environment, ...(typeof field?.label === 'string' ? { label: field.label } : {}), ...(typeof field?.help === 'string' ? { help: field.help } : {}) });
77
+ }
78
+ const count = key => Array.isArray(declaration[key]) ? declaration[key].length : 0;
79
+ return {
80
+ declaration: path,
81
+ // A custom deploy hook may name and place its Worker itself: only rer's own deploy's address is known.
82
+ ...(adapter.deploy.managesHostedOwner === true ? { worker: ownerWorkerName(declaration.project, 'production') } : {}),
83
+ permissions: deployPermissions(declaration),
84
+ resources: { d1: count('d1'), r2: count('r2'), kv: count('kv'), queues: count('queues'), durableObjects: count('durableObjects'), crons: count('crons') },
85
+ ...(ownerEmail ? { ownerEmail } : {}),
86
+ ...(mail ? { mail } : {}),
87
+ required,
88
+ ...(guide ? { guide } : {}),
89
+ };
90
+ }
@@ -9,7 +9,8 @@
9
9
  // toolchain); the app's own Vite config is never loaded, because its Worker
10
10
  // plugins do not run in Node. It reads no installation data. A seed with a
11
11
  // CONTRACT.md gets it parsed as sitePages({ contract }), for its /contract
12
- // page; one that does not parse stops the build, naming its lines.
12
+ // page; one that does not parse stops the build, naming its lines. `hosting`
13
+ // (cli/site-hosting.mjs) is what its /docs/hosting page says.
13
14
  // Usage: node site-render.mjs ROOT ENTRY OUTPUT [CONFIG]
14
15
  import { shared } from './shared.mjs';
15
16
  const { seedIcons, readIconFiles } = await shared('icon-files.mjs');
@@ -68,7 +69,7 @@ try {
68
69
  const options = clientJSON ? JSON.parse(clientJSON) : {};
69
70
  const clientBuild = await buildClients(vite, root, output, options), clients = clientBuild.clients;
70
71
  styles = [...new Set([...styles,...clientBuild.styles])];
71
- const build = await module.sitePages({ styles, clients, ...(contract ? { contract } : {}) });
72
+ const build = await module.sitePages({ styles, clients, ...(contract ? { contract } : {}), hosting: options.hosting ?? null });
72
73
  if (!Array.isArray(build?.pages) || !Array.isArray(build.assets)) fail('sitePages() returns { pages, assets }.');
73
74
 
74
75
  const written = new Set();
package/cli/site.mjs CHANGED
@@ -10,13 +10,15 @@ import { unpublishedIdentities } from './site-private.mjs';
10
10
  import { privateSitePaths } from './site-inputs.mjs';
11
11
  import { redactSiteText } from './site-diagnostics.mjs';
12
12
  import { siteRuntime, buildSiteRuntime, runtimeSummary } from './site-runtime.mjs';
13
+ import { siteHosting } from './site-hosting.mjs';
13
14
 
14
15
  // The run site: a seed's static marketing pages, /run and docs, built from
15
16
  // edition identity with no installation data. A seed opts in with
16
17
  // commandAdapter's `site: { entry, output, declaration, config?, runtime? }`: the module
17
18
  // whose sitePages({ styles }) returns the pages, the ignored folder the build
18
19
  // writes, the deploy declaration naming the run site's domain, and optionally
19
- // the Vite config holding the plugins the site pages need.
20
+ // the Vite config holding the plugins the site pages need. sitePages also gets
21
+ // `hosting`, what /docs/hosting says, read from the seed's deploy declarations.
20
22
  const relativePath = value => typeof value === 'string' && /^[A-Za-z0-9._/-]+$/.test(value) && !value.split('/').some(part => !part || part === '.' || part === '..');
21
23
 
22
24
  export function declaredSite(project) {
@@ -62,7 +64,8 @@ export async function buildSite(project, store, signal, { prepare = false, deplo
62
64
  await rm(staging, { recursive: true, force: true });
63
65
  try {
64
66
  await mkdir(staging);
65
- try { await store.run('Building the run site', process.execPath, [fileURLToPath(new URL('./site-render.mjs', import.meta.url)), project.root, site.entry, staging, site.config ?? '', JSON.stringify({entries:site.clients ?? {}, config:site.clientConfig, privatePaths:privateSitePaths(project.adapter), ...(site.runtime ? {runtime:site.runtime, corpus:`${staging}.corpus.json`} : {})})], { cwd: project.root, signal, heavy: false }); }
67
+ const { hosting } = await siteHosting(project);
68
+ try { await store.run('Building the run site', process.execPath, [fileURLToPath(new URL('./site-render.mjs', import.meta.url)), project.root, site.entry, staging, site.config ?? '', JSON.stringify({entries:site.clients ?? {}, config:site.clientConfig, privatePaths:privateSitePaths(project.adapter), hosting, ...(site.runtime ? {runtime:site.runtime, corpus:`${staging}.corpus.json`} : {})})], { cwd: project.root, signal, heavy: false }); }
66
69
  catch (error) {
67
70
  const code = await readFile(`${staging}.refusal`, 'utf8').catch(() => '');
68
71
  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);
@@ -12,7 +12,8 @@ import crypto from 'node:crypto';
12
12
  import {execFileSync} from 'node:child_process';
13
13
  import {hash, json, git, raw, gitReader, safe, noLinks, testSource} from './files.mjs';
14
14
  import {snapshot, acknowledgementDrift} from './base-acknowledgements.mjs';
15
- import {requireMaterializedCopies} from './copy-checkout.mjs';
15
+ import {completeAuthoringImports, requireMaterializedCopies, preflightRefresh} from './copy-checkout.mjs';
16
+ import {completeRuntimeCopies} from './runtime-dependencies.mjs';
16
17
 
17
18
  export const RECORD = 'base-copies.json';
18
19
  const commit = /^[a-f0-9]{40}$/, sha256 = /^[a-f0-9]{64}$/;
@@ -382,12 +383,22 @@ export function refreshCopies(root, {positional: [revision], record: recordPath,
382
383
  if (!sources.length && !Object.keys(copy.adapted || {}).length) throw Error(`No Base source at ${copy.originPath} in ${revision}`);
383
384
  return {copy, sources, retired, removed: Object.keys(copy.files).filter(name => !Object.hasOwn(copy.adapted || {}, name) && !sources.some(([source]) => mapped(source, copy.originPath, copy.path) === name))};
384
385
  });
386
+ completeAuthoringImports(root, plans, {tree: next, read, revision, base: origin.repository});
387
+ completeRuntimeCopies(plans, {tree: next, read, local, upstream, revision, checkTarget(name) {
388
+ safe(name); noLinks(root, name);
389
+ const target = path.join(root, name);
390
+ if (fs.existsSync(target) && !fs.lstatSync(target).isFile()) throw Error(`Runtime dependency is not a regular file: ${name}`);
391
+ }});
392
+ // Closure may add individually recorded siblings when the seed originally
393
+ // selected runtime files one at a time instead of copying the directory.
394
+ record.copies = plans.map(plan => plan.copy).sort((a, b) => a.path.localeCompare(b.path));
385
395
  // A migration a database may already have applied is never rewritten in place:
386
396
  // the schema change it brings belongs in a new migration of the seed's own.
387
397
  const applied = plans.flatMap(({copy, sources}) => sources.map(([source, {object}]) => [mapped(source, copy.originPath, copy.path), object])
388
398
  .filter(([name, object]) => name.split('/').slice(0, -1).includes('migrations') && local(name) && local(name) !== upstream.get(object)).map(([name]) => name));
389
399
  if (applied.length) throw Error(`Refusing to rewrite migrations that may already be applied: ${applied.join(', ')}. Base changed these files after they were copied; keep them as they are, drop them from ${recordPath}, and add a new migration for the change instead.`);
390
400
  const bytes = read.blobs(plans.flatMap(plan => plan.sources.map(([, {object}]) => object)));
401
+ preflightRefresh(root, plans, recordPath, files);
391
402
  const changes = [];
392
403
  for (const {copy, sources, removed} of plans) {
393
404
  for (const name of removed) { noLinks(root, name); fs.rmSync(path.join(root, name), {force: true}); }
@@ -1,6 +1,7 @@
1
1
  import fs from 'node:fs';
2
2
  import path from 'node:path';
3
- import {git, raw, safe, noLinks} from './files.mjs';
3
+ import {git, raw, safe, noLinks, testSource} from './files.mjs';
4
+ import {specifiers} from './runtime-dependencies.mjs';
4
5
  const quote = value => "'" + value.replaceAll("'", "'\\''") + "'";
5
6
 
6
7
  // -v marks assume-unchanged; -f marks fsmonitor-valid. Read them separately
@@ -67,3 +68,153 @@ export function requireMaterializedCopies(root, record, files) {
67
68
  }
68
69
  }
69
70
  }
71
+
72
+ // Every recorded authoring module is a root, including exported mirrors and
73
+ // local adaptations. Edition-owned tracked siblings end traversal at that edge.
74
+ export function completeAuthoringImports(root, plans, {tree, read, revision, base}) {
75
+ const mapped = (name, copy) => copy.path + name.slice(copy.originPath.length);
76
+ const authoring = name => path.posix.basename(path.posix.dirname(name)) === 'edition-authoring' && name.endsWith('.mjs') && !testSource(name);
77
+ const tracked = index(root), planned = new Map(), recorded = new Map(), queue = [];
78
+ for (const plan of plans) {
79
+ for (const name of Object.keys(plan.copy.files)) recorded.set(name, plan);
80
+ for (const [source, entry] of plan.sources) {
81
+ const target = mapped(source, plan.copy);
82
+ planned.set(target, plan);
83
+ if (authoring(source)) queue.push({source, target, entry});
84
+ }
85
+ for (const target of Object.keys(plan.copy.adapted || {})) {
86
+ const source = plan.copy.originPath + target.slice(plan.copy.path.length);
87
+ if (authoring(source)) queue.push({source, target, adapted: true});
88
+ }
89
+ }
90
+ const refuse = (target, reason, command) => {
91
+ throw Error(`Cannot refresh authoring import ${target}: ${reason}. Run ${command}, then repeat refresh-base; no files were refreshed.`);
92
+ };
93
+ const move = target => `mv -i -- ${quote(target)} ${quote(target + '.refresh-backup')}`;
94
+ // Unlike the write preflight, closure must also validate files it only relies
95
+ // on. Read actual directory entries to reject aliases, never infer volume case.
96
+ function inspect(target) {
97
+ const parts = safe(target).split('/');
98
+ let parent = root;
99
+ for (const [i, part] of parts.entries()) {
100
+ const absolute = path.join(parent, part), name = parts.slice(0, i + 1).join('/');
101
+ let stat;
102
+ try { stat = fs.lstatSync(absolute); }
103
+ catch (error) {
104
+ if (error.code === 'ENOENT') return null;
105
+ refuse(name, error.message, move(name));
106
+ }
107
+ if (!fs.readdirSync(parent).includes(part)) refuse(name, 'the volume resolves a differently spelled entry', move(name));
108
+ if (stat.isSymbolicLink()) refuse(name, 'symlink in import path', move(name));
109
+ if (i < parts.length - 1 && !stat.isDirectory()) refuse(name, 'import parent is not a directory', move(name));
110
+ if (i === parts.length - 1) {
111
+ if (!stat.isFile()) refuse(name, 'import is not a regular file', move(name));
112
+ return stat;
113
+ }
114
+ parent = absolute;
115
+ }
116
+ }
117
+ const blobs = new Map(), seen = new Set();
118
+ for (let i = 0; i < queue.length; i++) {
119
+ const {source, target, entry, adapted} = queue[i];
120
+ if (seen.has(target)) continue;
121
+ seen.add(target);
122
+ inspect(target);
123
+ let bytes;
124
+ if (adapted) bytes = fs.readFileSync(path.join(root, target), 'utf8');
125
+ else {
126
+ if (!blobs.has(entry.object)) blobs.set(entry.object, read.blobs([entry.object]).get(entry.object).toString('utf8'));
127
+ bytes = blobs.get(entry.object);
128
+ }
129
+ for (const specifier of specifiers(bytes)) {
130
+ const directory = path.posix.dirname(source);
131
+ const dependency = path.posix.normalize(path.posix.join(directory, specifier));
132
+ const destination = path.posix.normalize(path.posix.join(path.posix.dirname(target), specifier));
133
+ if (path.posix.dirname(dependency) !== directory) {
134
+ refuse(target, `import ${specifier} leaves its directory`, `\${EDITOR:-vi} ${quote(target)}`);
135
+ }
136
+ const exists = inspect(destination), owner = recorded.get(destination);
137
+ if (owner?.removed.includes(destination)) {
138
+ const original = owner.copy.originPath + destination.slice(owner.copy.path.length);
139
+ const restore = tracked.has(destination) ? `git restore -- ${quote(destination)}` :
140
+ `git -C ${quote(base)} show ${quote(owner.copy.revision + ':' + original)} > ${quote(destination)}`;
141
+ refuse(destination, 'recorded file was removed but is still imported', restore);
142
+ }
143
+ if (planned.has(destination) || owner) continue;
144
+ if (tracked.has(destination)) {
145
+ if (!exists) refuse(destination, 'tracked edition source is missing', `git restore -- ${quote(destination)}`);
146
+ continue;
147
+ }
148
+ if (exists) refuse(destination, 'existing file has no Base copy record or Git index entry', `git add -f -- ${quote(destination)}`);
149
+ const value = tree.get(dependency);
150
+ if (!value) refuse(destination, `no source at ${dependency} in ${revision}`, `\${EDITOR:-vi} ${quote(target)}`);
151
+ // A directory copy can already own this destination's namespace. Extend
152
+ // that record; otherwise the dependency is a new singleton copy.
153
+ let plan = plans.find(plan => destination.startsWith(plan.copy.path + '/'));
154
+ if (plan && mapped(dependency, plan.copy) !== destination) {
155
+ refuse(destination, 'source cannot fit its enclosing copy record', `\${EDITOR:-vi} base-copies.json`);
156
+ }
157
+ if (!plan) {
158
+ if (plans.some(plan => plan.copy.path.startsWith(destination + '/'))) refuse(destination, 'overlaps a copy directory', `\${EDITOR:-vi} base-copies.json`);
159
+ plan = {copy: {path: destination, originPath: dependency, revision, files: {}, modes: {}}, sources: [], removed: []};
160
+ plans.push(plan);
161
+ }
162
+ plan.sources.push([dependency, value]);
163
+ planned.set(destination, plan);
164
+ queue.push({source: dependency, target: destination, entry: value});
165
+ }
166
+ }
167
+ }
168
+
169
+ // Inspect real filesystem facts in apply order without opening or changing any
170
+ // file. This cannot see later races, disk-space or I/O failures, umask on newly
171
+ // created directories, or two new names differing only in case on an insensitive
172
+ // volume. Only earlier removals are remembered; no future directory is modelled.
173
+ export function preflightRefresh(root, plans, recordPath, seedFiles) {
174
+ const uid = process.geteuid?.(), freed = new Set();
175
+ const identity = (name, stat) => stat.nlink > 1 ? name : `${stat.dev}:${stat.ino}`;
176
+ const writable = (target, stat, directory = false) => {
177
+ if (uid === 0 && !(stat.mode & 0o222)) throw Error('no write permission bits');
178
+ fs.accessSync(target, fs.constants.W_OK | (directory ? fs.constants.X_OK : 0));
179
+ };
180
+ function inspect(name) {
181
+ let parent = root, parentStat = fs.lstatSync(root);
182
+ if (!parentStat.isDirectory()) throw Error('checkout root is not a directory');
183
+ const parts = safe(name).split('/');
184
+ for (const [i, part] of parts.entries()) {
185
+ const target = path.join(parent, part), relative = parts.slice(0, i + 1).join('/');
186
+ let stat;
187
+ try { stat = fs.lstatSync(target); }
188
+ catch (error) { if (error.code !== 'ENOENT') throw error; }
189
+ if (!stat || (stat.isFile() && freed.has(identity(relative, stat)))) return {parent, parentStat};
190
+ if (i === parts.length - 1) {
191
+ if (!stat.isFile()) throw Error('destination is not a regular file');
192
+ return {parent, parentStat, target, stat};
193
+ }
194
+ if (!stat.isDirectory()) throw Error(`directory required at ${relative}`);
195
+ parent = target; parentStat = stat;
196
+ }
197
+ }
198
+ function check(name, operation) {
199
+ try {
200
+ const found = inspect(name), {parent, parentStat, target, stat} = found;
201
+ if (operation === 'remove') {
202
+ if (!stat) return;
203
+ writable(parent, parentStat, true);
204
+ if (uid !== undefined && uid !== 0 && (parentStat.mode & 0o1000) && uid !== parentStat.uid && uid !== stat.uid) throw Error('sticky parent and file are owned by another user');
205
+ freed.add(identity(name, stat));
206
+ } else if (stat) {
207
+ if (operation !== 'record' && !seedFiles.has(name)) throw Error('existing destination is ignored or resolves a differently spelled entry');
208
+ writable(target, stat);
209
+ if (operation !== 'record' && uid !== undefined && uid !== 0 && uid !== stat.uid) throw Error('file is owned by another user; cannot set its mode');
210
+ } else writable(parent, parentStat, true);
211
+ } catch (error) {
212
+ throw Error(`Cannot refresh ${name}: ${error.message}. Fix the named path and its parent permissions before refreshing; no files were refreshed.`);
213
+ }
214
+ }
215
+ for (const {copy, sources, removed} of plans) {
216
+ for (const name of removed) check(name, 'remove');
217
+ for (const [source] of [...sources].sort(([a], [b]) => a.localeCompare(b))) check(copy.path + source.slice(copy.originPath.length), 'write');
218
+ }
219
+ check(recordPath, 'record');
220
+ }
@@ -45,7 +45,7 @@ export function noLinks(root, relative) {
45
45
  let stat;
46
46
  try { stat = fs.lstatSync(current); }
47
47
  catch (error) { if (error.code !== 'ENOENT') throw error; }
48
- if (stat?.isSymbolicLink()) throw Error('Symlink in destination path');
48
+ if (stat?.isSymbolicLink()) throw Error(`Symlink in destination path: ${relative} (${path.relative(root, current)})`);
49
49
  }
50
50
  }
51
51
  export function tracked(root, allowMissing = false) {