@runeditrun/rer 0.2.84 → 0.2.85

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 (121) hide show
  1. package/README.md +21 -24
  2. package/cli/README.md +17 -1
  3. package/cli/bounded.mjs +1 -1
  4. package/cli/business/carry.mjs +149 -0
  5. package/cli/business/command.mjs +6 -1
  6. package/cli/business/connect.mjs +1 -1
  7. package/cli/business/local-commands.mjs +2 -0
  8. package/cli/business/opt-ins.mjs +8 -4
  9. package/cli/business/questions.mjs +4 -1
  10. package/cli/business/state.mjs +1 -1
  11. package/cli/business/supervisor.mjs +7 -1
  12. package/cli/container-transfer.mjs +2 -2
  13. package/cli/contract.mjs +6 -6
  14. package/cli/demo-native.mjs +2 -1
  15. package/cli/deploy-command.mjs +6 -4
  16. package/cli/deploy.mjs +9 -7
  17. package/cli/errors.mjs +1 -1
  18. package/cli/export.mjs +1 -1
  19. package/cli/human-error.mjs +20 -0
  20. package/cli/lifecycle.mjs +23 -5
  21. package/cli/main.mjs +1 -1
  22. package/cli/managed-data-schema.mjs +114 -0
  23. package/cli/managed-data-zip.mjs +116 -0
  24. package/cli/managed-data.mjs +110 -0
  25. package/cli/managed-server.mjs +25 -14
  26. package/cli/native-owner-entry.mjs +86 -0
  27. package/cli/output.mjs +3 -2
  28. package/cli/parse.mjs +2 -2
  29. package/cli/portability-empty.mjs +59 -0
  30. package/cli/portability-migrations.mjs +7 -5
  31. package/cli/portability-policy.mjs +5 -2
  32. package/cli/portability-worker.mjs +15 -6
  33. package/cli/portability.mjs +30 -8
  34. package/cli/ready-summary.mjs +9 -0
  35. package/cli/run-transfers.mjs +37 -0
  36. package/cli/runtime-front-door.mjs +4 -4
  37. package/cli/server-proxy.mjs +4 -3
  38. package/cli/server-registry.mjs +13 -8
  39. package/cli/site.mjs +6 -1
  40. package/cli/transfers.mjs +17 -4
  41. package/edition-authoring/base-copies.mjs +55 -7
  42. package/foundation/runtime/contract-docs.mjs +3 -2
  43. package/foundation/runtime/run-data.d.mts +1 -0
  44. package/foundation/runtime/run-data.mjs +2 -0
  45. package/global/CHANGELOG.md +33 -0
  46. package/global/README.md +21 -24
  47. package/global/cli/command.mjs +8 -2
  48. package/global/cli/install.mjs +4 -5
  49. package/global/cli/path-link.mjs +3 -2
  50. package/global/cli/repo-command.mjs +3 -7
  51. package/global/launch/dev.mjs +12 -2
  52. package/global/launch/facts.mjs +2 -2
  53. package/global/launch/render.mjs +16 -9
  54. package/global/launch/state.mjs +4 -4
  55. package/global/main.mjs +12 -7
  56. package/install.sh +17 -6
  57. package/modules/container/README.md +6 -1
  58. package/modules/container/adapters/d1-limits.mjs +286 -0
  59. package/modules/container/adapters/d1-sqlite.mjs +95 -0
  60. package/modules/container/adapters/d1-workerd-preload.mjs +19 -0
  61. package/modules/container/adapters/d1-workerd-setup.mjs +26 -0
  62. package/modules/container/adapters/d1-workerd.mjs +5 -0
  63. package/modules/container/adapters/sqlite-worker.mjs +5 -3
  64. package/modules/container/build.mjs +1 -0
  65. package/modules/container/image-variables.mjs +8 -3
  66. package/modules/container/transfer.mjs +14 -4
  67. package/modules/container/worker-engine.mjs +3 -3
  68. package/modules/demo-host/build.mjs +1 -0
  69. package/modules/demo-host/native/prepare.mjs +2 -1
  70. package/modules/demo-host/native/sql.mjs +7 -2
  71. package/modules/deploy-kit/README.md +22 -2
  72. package/modules/deploy-kit/d1-import-diagnostics.mjs +56 -0
  73. package/modules/deploy-kit/d1-import.mjs +17 -5
  74. package/modules/deploy-kit/diagnostics.mjs +29 -3
  75. package/modules/deploy-kit/owner-cli.mjs +20 -7
  76. package/modules/deploy-kit/provider-transport.mjs +19 -4
  77. package/modules/deploy-kit/provider.mjs +57 -22
  78. package/modules/deploy-kit/readiness-window.mjs +1 -1
  79. package/modules/deploy-kit/site.mjs +8 -3
  80. package/modules/deploy-kit/worker.mjs +4 -4
  81. package/modules/import-kit/src/d1/README.md +23 -4
  82. package/modules/import-kit/src/d1/atomic-prefix.ts +2 -2
  83. package/modules/import-kit/src/d1/budget.ts +15 -7
  84. package/modules/import-kit/src/d1/column-layout.ts +5 -0
  85. package/modules/import-kit/src/d1/creation.ts +15 -8
  86. package/modules/import-kit/src/d1/database.ts +3 -3
  87. package/modules/import-kit/src/d1/erasure-pages.ts +2 -1
  88. package/modules/import-kit/src/d1/erasure.ts +2 -2
  89. package/modules/import-kit/src/d1/jobs.ts +10 -6
  90. package/modules/import-kit/src/d1/plans.ts +14 -8
  91. package/modules/import-kit/src/d1/retirement.ts +2 -2
  92. package/modules/import-kit/src/d1/sets.ts +1 -3
  93. package/modules/import-kit/src/d1/staged-creation.ts +3 -3
  94. package/modules/import-kit/src/door/limits.ts +1 -1
  95. package/modules/import-kit/src/door/refusals.ts +1 -1
  96. package/modules/portability/README.md +112 -4
  97. package/modules/portability/backfills.mjs +9 -8
  98. package/modules/portability/bridge.mjs +12 -5
  99. package/modules/portability/catalogue.mjs +12 -5
  100. package/modules/portability/files.mjs +4 -3
  101. package/modules/portability/http.mjs +3 -3
  102. package/modules/portability/import.mjs +18 -9
  103. package/modules/portability/index.d.mts +4 -3
  104. package/modules/portability/index.mjs +1 -1
  105. package/modules/portability/ledgers.mjs +3 -2
  106. package/modules/portability/legacy-safety.mjs +11 -2
  107. package/modules/portability/migration-history.mjs +2 -2
  108. package/modules/portability/progress.mjs +1 -0
  109. package/modules/portability/restore-empty.mjs +12 -0
  110. package/modules/release/github-verify.mjs +11 -2
  111. package/onboarding/adapter.mjs +35 -6
  112. package/onboarding/app-fetch.mjs +6 -4
  113. package/onboarding/development-runtime.mjs +19 -0
  114. package/onboarding/local/operation.mjs +45 -27
  115. package/onboarding/loopback-fetch.mjs +16 -0
  116. package/onboarding/operator-installer.mjs +2 -4
  117. package/onboarding/process-diagnostics.mjs +6 -2
  118. package/onboarding/production-runtime.mjs +4 -2
  119. package/onboarding/readiness.mjs +1 -0
  120. package/package.json +1 -1
  121. package/scripts/publish-check/public_hygiene.py +1 -1
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/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/module-closure.mjs', 'edition-authoring/edition-migrations.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/module-resolution.mjs', 'edition-authoring/package-target.mjs', 'edition-authoring/module-syntax.mjs', 'edition-authoring/module-edges.mjs', 'edition-authoring/vendor/babel-parser', 'edition-authoring/workflow-copies.mjs', 'edition-authoring/workflow-scripts.mjs', 'edition-authoring/vendor/yaml', 'edition-authoring/sources.mjs', 'edition-authoring/updates.mjs', 'foundation/runtime/seed-contract.mjs', 'foundation/runtime/contract-docs.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/module-closure.mjs', 'edition-authoring/edition-migrations.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/module-resolution.mjs', 'edition-authoring/package-target.mjs', 'edition-authoring/module-syntax.mjs', 'edition-authoring/module-edges.mjs', 'edition-authoring/vendor/babel-parser', 'edition-authoring/workflow-copies.mjs', 'edition-authoring/workflow-scripts.mjs', 'edition-authoring/vendor/yaml', 'edition-authoring/sources.mjs', 'edition-authoring/updates.mjs', 'foundation/runtime/seed-contract.mjs', 'foundation/runtime/run-data.mjs', 'foundation/runtime/run-data.d.mts', 'foundation/runtime/contract-docs.mjs'];
10
10
  export async function inventory(base, roots = sourceRoots) {
11
11
  const files = [];
12
12
  async function walk(path) {
@@ -0,0 +1,20 @@
1
+ // Presentation only: the structured error and its captured step stay intact.
2
+ export function humanError(error, { retry } = {}) {
3
+ if (error.humanMessage) return error.humanMessage;
4
+ const text = String(error?.message ?? error);
5
+ const download = /https?:\/\/[^\s"'<>]+/.exec(text)?.[0];
6
+ if (download && /ECONNREFUSED/.test(text)) {
7
+ let url; try { url = new URL(download); } catch {}
8
+ const archive = url && /\/([^/]+)-(\d[^/]*)\.tgz/.exec(url.pathname);
9
+ if (url && archive) return `Could not download a dependency (${archive[1]} ${archive[2]}) because the package registry at ${url.host} refused the connection. Check your network or registry setting, then run: ${retry ?? 'rer install'}.`;
10
+ }
11
+ const technical = /(?:^|\n)\s*(?:at |[A-Za-z]*Error:)/.test(text);
12
+ if (technical || ['command_failed', 'launch_failed'].includes(error.code ?? error.event?.code)) {
13
+ const lines = text.split('\n').filter(line => !/^\s*at /.test(line))
14
+ .map(line => line.replace(/^(\s*)[A-Za-z]*Error:\s*/, '$1'));
15
+ const message = lines.shift()?.trim() || 'The command failed.';
16
+ const detail = lines.join('\n').trimEnd();
17
+ return `${message}\n${detail.trim() ? detail : `Run ${retry ?? 'the same command'} with --json for details.`}`;
18
+ }
19
+ return text;
20
+ }
package/cli/lifecycle.mjs CHANGED
@@ -34,7 +34,7 @@ async function runLifecycle(project, options, out, deployRedaction) {
34
34
  if(project.profile==='first-party'&&options.verb==='outbox')throw refuse('Captured mail has been retired. Run rer dev and open its RER_OPEN link.');
35
35
  if(project.profile==='first-party'&&['dev','preview'].includes(options.verb))return (await import('./managed-server.mjs')).managedServer(project,options,out);
36
36
  const cancellation = new AbortController(), signal = cancellation.signal;
37
- let termination, operation, store, temporary, proxy, failed = false;
37
+ let termination, operation, store, temporary, proxy, failed = false, installingFrozen = false;
38
38
  const handlers = new Map(['SIGINT', 'SIGTERM'].map(name => [name, () => { if (!termination) { termination = name; cancellation.abort(name); } }]));
39
39
  for (const [name, handler] of handlers) process.on(name, handler);
40
40
  try {
@@ -75,7 +75,7 @@ async function runLifecycle(project, options, out, deployRedaction) {
75
75
  const capture = ['.seed', 'installer', ...(process.env.RER_LOCAL_OPERATION || process.env.RER_ONBOARDING_PROCESS_OWNER ? [`commands-${process.pid}`] : [])];
76
76
  const directory = project.profile === 'first-party' && options.verb !== 'doctor' ? await privateDirectory(project.root, capture) : (temporary = await mkdtemp(join(tmpdir(), 'rer-command-')));
77
77
  const state = { logs: [] };
78
- store = await createCommands(directory, state, { terminal: text => out.progress(text), onWaiting: options.json ? details => out.event('waiting', details) : undefined, queue: ['install','setup','build','verify','deploy','export','import'].includes(options.verb)?{verb:options.verb,originalCI:process.env.CI==='true'}:undefined });
78
+ store = await createCommands(directory, state, { terminal: text => { if (!installingFrozen || options.json) out.progress(text); }, onWaiting: options.json ? details => out.event('waiting', details) : undefined, queue: ['install','setup','build','verify','deploy','export','import'].includes(options.verb)?{verb:options.verb,originalCI:process.env.CI==='true'}:undefined });
79
79
  store.state = state;
80
80
  if (deployRedaction) store.addSecrets?.(Object.values(process.env).filter(value => value && deployRedaction.redact(value) !== value));
81
81
  store.setupManifest = project.setupManifest;
@@ -93,12 +93,15 @@ async function runLifecycle(project, options, out, deployRedaction) {
93
93
  if (options.verb === 'install') {
94
94
  try {
95
95
  const { dependencyEnvironment } = await shared('dependency-environment.mjs');
96
+ if (!options.json) out.progress('Installing frozen dependencies\n');
97
+ installingFrozen = true;
96
98
  await store.run('Installing frozen dependencies', 'pnpm', ['install', '--frozen-lockfile'], { cwd: project.root, signal, env: { ...dependencyEnvironment(), CI: 'true' }, display: 'pnpm install --frozen-lockfile' });
99
+ installingFrozen = false;
97
100
  // The test browser downloads on first need (verify), not here. Package
98
101
  // scripts run with the pinned pnpm through rer run, which keeps pnpm off PATH.
99
102
  if (!options.json) store.report('Run this project\'s package scripts with rer run SCRIPT (it uses the pnpm this project pins).');
100
103
  return {};
101
- } catch (error) { if (signal.aborted) throw error; throw await installStepError(error, store, project.root); }
104
+ } catch (error) { if (signal.aborted) throw error; throw await installStepError(error, store, project.root); } finally { installingFrozen = false; }
102
105
  }
103
106
  if (options.verb === 'verify') {
104
107
  const target = adapter.verify;
@@ -126,7 +129,7 @@ async function runLifecycle(project, options, out, deployRedaction) {
126
129
  const privateSettings = [...adapter.settings, '.rer/local/models.json', ...(project.privateInputs??[])];
127
130
  const run = () => adapter.run(project.root, input ?? { [emailKey]: options.ownerEmail ?? selected?.vars[emailKey] }, store,
128
131
  child => { app = child; }, signal,
129
- { verb: options.verb, ownerKey: emailKey, selectedOrigin: selected?.url, runtimePort: proxy?.runtimePort, operationToken: operation?.token, dev: adapter.dev, privateSettings, devKey: devKey ?? undefined });
132
+ { verb: options.verb, ownerKey: emailKey, selectedOrigin: selected?.url, runtimePort: proxy?.runtimePort, setRuntimePort: port=>proxy.setRuntimePort(port), operationToken: operation?.token, dev: adapter.dev, privateSettings, devKey: devKey ?? undefined });
130
133
  // A production build never sees the checkout's local settings, whatever the seed's build runs.
131
134
  if (options.verb === 'build') await withoutLocalSettings(project.root, { settings: privateSettings, operationToken: operation?.token }, run);
132
135
  else await run();
@@ -149,7 +152,7 @@ async function runLifecycle(project, options, out, deployRedaction) {
149
152
  const deployNeeds = await hostedNeeds(project);
150
153
  if (options.json) out.event('setup-ready', { url, setupUrl: url+'/setup', ...(deployNeeds.length ? { neededForDeploy: deployNeeds } : {}) });
151
154
  else {
152
- out.human(`RER_SETUP_READY ${url}`); out.human(`Next: rer dev opens ${url} signed in as the owner; first-run setup finishes at ${url}/setup.`);
155
+ out.human(`RER_SETUP_READY ${url}`); out.human(`Next: rer dev opens ${url} signed in as the owner; first-run setup finishes at ${url}/setup. rer preview prints a one-time native owner link on loopback; rer demo also prints a separate native owner entry.`);
153
156
  if (deployNeeds.length) out.human(['To host it on your own Cloudflare account later, rer deploy needs:', ...deployNeeds.map(item => ` - ${item}`), 'Set them in your environment or a private (mode 0600) file passed with --env-file, then check with rer deploy --dry-run.'].join('\n'));
154
157
  }
155
158
  } else if (options.verb !== 'build') throw refuse('The requested first-party capability is unavailable.');
@@ -189,6 +192,21 @@ async function installStepError(error, store, root) {
189
192
  if (approvedFailure(error)?.code === 'machine_busy') return error;
190
193
  const { checkoutPath, stepDiagnostic, stepFailureText } = await shared('step-failure.mjs');
191
194
  const step = approvedFailure(error)?.step ?? stepDiagnostic({ name: store.state.step, output: store.state.logs, log: store.redact(checkoutPath(root, store.logPath)) });
195
+ // pnpm's stable machine codes select public wording; its raw diagnostic is
196
+ // retained in the structured step and private capture, never in this sentence.
197
+ const text = step.output.join('\n');
198
+ const refusal = [
199
+ ['ERR_PNPM_OUTDATED_LOCKFILE', 'pnpm-lock.yaml does not match package.json'],
200
+ ['ERR_PNPM_NO_LOCKFILE', 'pnpm-lock.yaml is missing'],
201
+ ['ERR_PNPM_LOCKFILE_BREAKING_CHANGE', 'pnpm-lock.yaml was written by an incompatible pnpm version'],
202
+ ].find(([code]) => text.includes(code)) ?? (text.includes('Headless installation requires a pnpm-lock.yaml file') ? ['ERR_PNPM_NO_LOCKFILE', 'pnpm-lock.yaml is missing'] : undefined);
203
+ if (refusal) {
204
+ const { toolCommand } = await shared('tool-command.mjs');
205
+ const { shellQuote } = await import('./node-runtime-cache.mjs');
206
+ const [program, args] = toolCommand('pnpm', ['install', '--no-frozen-lockfile']);
207
+ const next = [program, ...args].map(value => /^[A-Za-z0-9_.-]+$/.test(value) ? value : shellQuote(value)).join(' ');
208
+ return new RerError(refusal[0], `${refusal[1]}, so the install was stopped; run ${next} to update it, then retry.\n${stepFailureText(step)}`, 1, { step, childExitCode: step.exitCode, signal: step.signal });
209
+ }
192
210
  // Only a RerError's message or a symbolic error code is safe to show; a native message can hold private data.
193
211
  const cause = error instanceof RerError ? `\n${error.message}` : /^[A-Z][A-Z0-9_]{1,63}$/.test(error?.code ?? '') ? `\nCause: ${error.code}` : '';
194
212
  return new RerError(error instanceof RerError ? error.code : 'install_step_failed', `${stepFailureText(step)}${cause}`, error instanceof RerError ? error.exitCode : 1, { step, ...(Number.isInteger(step.exitCode) ? { childExitCode: step.exitCode, signal: step.signal } : {}) });
package/cli/main.mjs CHANGED
@@ -84,7 +84,7 @@ export async function main(argv = process.argv.slice(2)) {
84
84
  out.result(true, 0, result); return 0;
85
85
  } catch (error) {
86
86
  const failure = error instanceof RerError ? error : unexpectedFailure(error);
87
- out.error(failure); out.result(false, failure.exitCode, { ...failure.exportDetails, ...(failure.childExitCode === undefined ? {} : { childExitCode: failure.childExitCode, signal: failure.signal }) }); return failure.exitCode;
87
+ out.error(failure); out.result(false, failure.exitCode, { ...failure.exportDetails, ...(failure.siteDeploy ? {siteDeploy:failure.siteDeploy} : {}), ...(failure.d1Import ? {d1Import:failure.d1Import} : {}), ...(failure.childExitCode === undefined ? {} : { childExitCode: failure.childExitCode, signal: failure.signal }) }); return failure.exitCode;
88
88
  }
89
89
  }
90
90
 
@@ -0,0 +1,114 @@
1
+ import { DatabaseSync } from 'node:sqlite';
2
+ import { createHash } from 'node:crypto';
3
+ import { all, insist, PortabilityError } from '../modules/portability/storage.mjs';
4
+ import { excludedTable } from '../modules/portability/catalogue.mjs';
5
+ import { schemaSQL } from '../modules/portability/schema-identity.mjs';
6
+ import { ensurePortabilitySchema, portabilityPlan } from '../modules/container/portability-schema.mjs';
7
+ import { ensureImportLeases } from '../modules/portability/import-lease.mjs';
8
+ import { splitStatements } from '../modules/portability/sql-text.mjs';
9
+ import { createPortability } from '../modules/portability/index.mjs';
10
+ import { migrationReplay } from '../modules/portability/migration-history.mjs';
11
+
12
+ const query = "SELECT type,name,sql FROM sqlite_master WHERE sql IS NOT NULL AND substr(lower(name), 1, 7) <> 'sqlite_' AND substr(lower(name), 1, 4) <> '_cf_'";
13
+ const key = object => JSON.stringify([object.type, object.name]);
14
+ const namesHash = names => createHash('sha256').update(JSON.stringify(names.sort())).digest('hex');
15
+
16
+ // Compare the complete source inventory before policy projection can omit
17
+ // objects. Destination migrations and runtime establish native schema; only
18
+ // those migrations retire it. Source DDL is never installed. The caller has already
19
+ // validated and mapped the source migration ledger to native history.
20
+ export async function managedSchema(db, plan, policy, ledger) {
21
+ const source = await all(db, query), scratch = new DatabaseSync(':memory:');
22
+ const normalized = new Map();
23
+ const identity = object => {
24
+ if (!normalized.has(object.sql)) normalized.set(object.sql, schemaSQL(object.sql));
25
+ return normalized.get(object.sql);
26
+ };
27
+ const objects = source.filter(object => ['table', 'trigger', 'index', 'view'].includes(object.type));
28
+ const retired = new Map();
29
+ let previous = new Map();
30
+ try {
31
+ for (const migration of plan) for (const sql of migration.statements) {
32
+ scratch.exec(sql);
33
+ const current = new Map(scratch.prepare(query).all().map(object => [key(object), object]));
34
+ for (const object of objects) {
35
+ const id = key(object), before = previous.get(id), after = current.get(id);
36
+ if (before && identity(before) === identity(object) && (!after || identity(after) !== identity(object))) retired.set(id, migration.name);
37
+ }
38
+ previous = current;
39
+ }
40
+ // Native restore also installs runtime-owned bookkeeping. Derive it with
41
+ // the same destination helpers, never a source name or a policy wildcard.
42
+ const native = nativeDatabase(scratch);
43
+ await ensurePortabilitySchema(native, await portabilityPlan(splitStatements));
44
+ await ensureImportLeases(native);
45
+ previous = new Map((await all(native, query)).map(object => [key(object), object]));
46
+ const inventory = [], groups = new Map();
47
+ const account = (object, disposition, migration) => {
48
+ const group = JSON.stringify([object.type, disposition, migration]);
49
+ if (!groups.has(group)) groups.set(group, { type: object.type, disposition, ...(migration ? { migration } : {}), names: [] });
50
+ groups.get(group).names.push(object.name);
51
+ };
52
+ for (const type of ['table', 'trigger', 'index', 'view']) {
53
+ const entries = objects.filter(object => object.type === type);
54
+ let matched = 0;
55
+ for (const object of entries) {
56
+ const id = key(object), target = previous.get(id);
57
+ if (type === 'table') {
58
+ // Row exclusion never establishes schema compatibility. The validated
59
+ // runner ledger is translated separately, not restored as a product table.
60
+ if (object.name === ledger.table) { account(object, 'migration-ledger'); continue; }
61
+ if (target) {
62
+ if (excludedTable(object.name, policy)) account(object, 'excluded-by-policy');
63
+ else matched++;
64
+ continue;
65
+ }
66
+ const migration = retired.get(id);
67
+ insist(excludedTable(object.name, policy) && migration, 'managed_data_schema_table_missing', 409);
68
+ account(object, 'retired', migration);
69
+ continue;
70
+ }
71
+ if (target && identity(target) === identity(object)) { matched++; continue; }
72
+ const migration = retired.get(id);
73
+ insist(migration, target ? 'managed_data_schema_object_unaccounted_replacement' : 'managed_data_schema_object_unknown', 409);
74
+ account(object, target ? 'replaced' : 'retired', migration);
75
+ }
76
+ inventory.push({ type, source: entries.length, matched, accounted: entries.length - matched });
77
+ }
78
+ return { inventory, accounted: [...groups.values()].map(({ names, ...group }) => ({ ...group, count: names.length, namesSha256: namesHash(names) })) };
79
+ } finally { scratch.close(); }
80
+ }
81
+
82
+ // Use the native import checker against a disposable destination, including its
83
+ // migration placement and table-definition checks. The real destination has
84
+ // not been opened yet. Source schema accounting above must succeed first.
85
+ export async function managedCompatibility(plan, inputPolicy, manifest, declaration, buckets) {
86
+ const scratch = new DatabaseSync(':memory:');
87
+ try {
88
+ for (const migration of plan) for (const sql of migration.statements) scratch.exec(sql);
89
+ const db = nativeDatabase(scratch);
90
+ await ensurePortabilitySchema(db, await portabilityPlan(splitStatements));
91
+ scratch.exec('CREATE TABLE d1_migrations(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT UNIQUE, applied_at TEXT DEFAULT CURRENT_TIMESTAMP)');
92
+ for (const migration of plan) scratch.prepare('INSERT INTO d1_migrations(name) VALUES(?)').run(migration.name);
93
+ const service = await createPortability({ db, buckets, policy: inputPolicy, migrations: async () => migrationReplay(plan, inputPolicy, declaration) });
94
+ await service.imports.create(manifest);
95
+ } catch (error) {
96
+ if (!(error instanceof PortabilityError)) throw error;
97
+ throw new PortabilityError('managed_data_schema_incompatible', 409);
98
+ } finally { scratch.close(); }
99
+ }
100
+
101
+ function nativeDatabase(scratch) {
102
+ return {
103
+ prepare(sql) {
104
+ let values = [];
105
+ return {
106
+ bind(...args) { values = args; return this; },
107
+ async first() { return scratch.prepare(sql).get(...values) ?? null; },
108
+ async all() { return { results: scratch.prepare(sql).all(...values) }; },
109
+ async run() { return scratch.prepare(sql).run(...values); },
110
+ };
111
+ },
112
+ async batch(statements) { return Promise.all(statements.map(statement => statement.run())); },
113
+ };
114
+ }
@@ -0,0 +1,116 @@
1
+ import { open } from 'node:fs/promises';
2
+ import { constants } from 'node:fs';
3
+ import { createHash } from 'node:crypto';
4
+ import { PortabilityError } from '../modules/portability/storage.mjs';
5
+
6
+ // Platform's streaming producer uses stored entries, with a data descriptor.
7
+ // Compressed entries are deliberately unsupported: no expansion is performed.
8
+ export const managedDataLimits = Object.freeze({ archiveBytes: 128 * 1024 * 1024, entryBytes: 64 * 1024 * 1024, metadataBytes: 4 * 1024 * 1024, entries: 10000 });
9
+ const check = (condition, reason) => { if (!condition) throw new PortabilityError('managed_data_' + reason, 409); };
10
+ const digest = bytes => createHash('sha256').update(bytes).digest('hex');
11
+ const decoder = new TextDecoder('utf-8', { fatal: true });
12
+ const metadata = value => value && typeof value === 'object' && !Array.isArray(value) && Object.values(value).every(value => typeof value === 'string');
13
+ const safePath = path => typeof path === 'string' && path.length > 0 && path.length <= 1024 && !/[\\\x00-\x1f:]/.test(path) && !path.startsWith('/') && path.split('/').every(part => part && part !== '.' && part !== '..');
14
+ const crcTable = Uint32Array.from({ length: 256 }, (_, i) => { for (let j = 0; j < 8; j++) i = (i >>> 1) ^ (i & 1 ? 0xedb88320 : 0); return i >>> 0; });
15
+ function crc32(bytes) { let crc = 0xffffffff; for (const byte of bytes) crc = (crc >>> 8) ^ crcTable[(crc ^ byte) & 255]; return (crc ^ 0xffffffff) >>> 0; }
16
+
17
+ export async function isManagedDataZip(path) {
18
+ const file = await open(path, constants.O_RDONLY | constants.O_NOFOLLOW);
19
+ try { const bytes = Buffer.alloc(4); const { bytesRead } = await file.read(bytes, 0, 4, 0); return bytesRead >= 2 && bytes[0] === 80 && bytes[1] === 75; }
20
+ finally { await file.close(); }
21
+ }
22
+
23
+ // Nothing is extracted to a path supplied by the archive. Both directory and
24
+ // local headers must describe the same bounded, non-overlapping members.
25
+ export async function readManagedDataZip(path) {
26
+ const file = await open(path, constants.O_RDONLY | constants.O_NOFOLLOW);
27
+ let bytes;
28
+ try {
29
+ const info = await file.stat();
30
+ check(info.isFile() && info.size <= managedDataLimits.archiveBytes, 'oversized');
31
+ bytes = Buffer.alloc(info.size);
32
+ let offset = 0;
33
+ while (offset < bytes.length) { const read = await file.read(bytes, offset, bytes.length - offset, offset); check(read.bytesRead > 0, 'truncated'); offset += read.bytesRead; }
34
+ check((await file.stat()).size === info.size, 'truncated');
35
+ } finally { await file.close(); }
36
+ check(bytes.length >= 22, 'truncated');
37
+ let end = bytes.length - 22;
38
+ while (end >= Math.max(0, bytes.length - 65557) && bytes.readUInt32LE(end) !== 0x06054b50) end--;
39
+ check(end >= Math.max(0, bytes.length - 65557), 'truncated');
40
+ check(end + 22 + bytes.readUInt16LE(end + 20) === bytes.length, 'invalid_zip');
41
+ const count = bytes.readUInt16LE(end + 10), size = bytes.readUInt32LE(end + 12), start = bytes.readUInt32LE(end + 16);
42
+ check(!bytes.readUInt16LE(end + 4) && !bytes.readUInt16LE(end + 6) && count === bytes.readUInt16LE(end + 8), 'invalid_zip');
43
+ check(count > 0 && count <= managedDataLimits.entries && count !== 65535, 'oversized');
44
+ check(start + size === end, 'truncated');
45
+ const entries = new Map(), ranges = [];
46
+ let cursor = start, total = 0;
47
+ const text = data => { try { return decoder.decode(data); } catch { check(false, 'invalid_utf8'); } };
48
+ for (let i = 0; i < count; i++) {
49
+ check(cursor + 46 <= end && bytes.readUInt32LE(cursor) === 0x02014b50, 'truncated');
50
+ const flags = bytes.readUInt16LE(cursor + 8), method = bytes.readUInt16LE(cursor + 10), crc = bytes.readUInt32LE(cursor + 16);
51
+ const packed = bytes.readUInt32LE(cursor + 20), length = bytes.readUInt32LE(cursor + 24), nameLength = bytes.readUInt16LE(cursor + 28);
52
+ const next = cursor + 46 + nameLength + bytes.readUInt16LE(cursor + 30) + bytes.readUInt16LE(cursor + 32), offset = bytes.readUInt32LE(cursor + 42);
53
+ check(next <= end && !bytes.readUInt16LE(cursor + 34), 'truncated');
54
+ const name = text(bytes.subarray(cursor + 46, cursor + 46 + nameLength));
55
+ check(safePath(name), 'unsafe_path');
56
+ check(!entries.has(name), 'duplicate_member');
57
+ check(method === 0 && (flags & ~0x808) === 0, 'unsupported_compression');
58
+ check(length <= managedDataLimits.entryBytes && packed === length && (total += length) <= managedDataLimits.archiveBytes, 'oversized');
59
+ check(offset + 30 <= start && bytes.readUInt32LE(offset) === 0x04034b50, 'truncated');
60
+ check(bytes.readUInt16LE(offset + 6) === flags && bytes.readUInt16LE(offset + 8) === method, 'invalid_zip');
61
+ const body = offset + 30 + bytes.readUInt16LE(offset + 26) + bytes.readUInt16LE(offset + 28), tail = body + length;
62
+ check(body <= start && tail <= start && text(bytes.subarray(offset + 30, offset + 30 + bytes.readUInt16LE(offset + 26))) === name, 'invalid_zip');
63
+ let stop = tail;
64
+ if (flags & 8) {
65
+ check(stop + 12 <= start, 'truncated');
66
+ if (bytes.readUInt32LE(stop) === 0x08074b50) stop += 4;
67
+ check(stop + 12 <= start && bytes.readUInt32LE(stop) === crc && bytes.readUInt32LE(stop + 4) === packed && bytes.readUInt32LE(stop + 8) === length, 'invalid_zip');
68
+ stop += 12;
69
+ } else check(bytes.readUInt32LE(offset + 14) === crc && bytes.readUInt32LE(offset + 18) === packed && bytes.readUInt32LE(offset + 22) === length, 'invalid_zip');
70
+ const data = bytes.subarray(body, tail);
71
+ check(crc32(data) === crc, 'checksum');
72
+ entries.set(name, data); ranges.push([offset, stop]); cursor = next;
73
+ }
74
+ check(cursor === end, 'invalid_zip');
75
+ ranges.sort((a, b) => a[0] - b[0]);
76
+ check(ranges[0][0] === 0 && ranges.every((range, i) => range[1] === (ranges[i + 1]?.[0] ?? start)), 'invalid_zip');
77
+ const required = name => { check(safePath(name) && entries.has(name), 'missing_member'); return entries.get(name); };
78
+ const json = name => {
79
+ const data = required(name); check(data.length <= managedDataLimits.metadataBytes, 'oversized');
80
+ try { return JSON.parse(text(data)); } catch (error) { if (error instanceof PortabilityError) throw error; check(false, 'invalid_manifest'); }
81
+ };
82
+ const manifest = json('manifest.json');
83
+ check(manifest && typeof manifest === 'object' && manifest.kind === 'runeditrun-app-data', 'wrong_kind');
84
+ check(manifest.schema === 1, 'unsupported_version');
85
+ check(['installationId', 'appName', 'generatedAt'].every(key => typeof manifest[key] === 'string' && manifest[key].length > 0 && manifest[key].length <= 1024), 'invalid_manifest');
86
+ check(Array.isArray(manifest.databases) && manifest.databases.length > 0 && Array.isArray(manifest.buckets) && Array.isArray(manifest.notIncluded), 'invalid_manifest');
87
+ required('README.txt');
88
+ const used = new Set(['manifest.json', 'README.txt']), bindings = new Set();
89
+ const member = name => { check(!used.has(name), 'duplicate_member'); used.add(name); return required(name); };
90
+ const verified = (name, size, hash) => {
91
+ const data = member(name);
92
+ check(Number.isSafeInteger(size) && size >= 0 && size === data.length && typeof hash === 'string' && /^[a-f0-9]{64}$/.test(hash), 'invalid_manifest');
93
+ check(digest(data) === hash, 'checksum'); return data;
94
+ };
95
+ for (const database of manifest.databases) {
96
+ check(database && typeof database.binding === 'string' && /^[A-Za-z_][A-Za-z0-9_]*$/.test(database.binding) && !bindings.has(database.binding), 'invalid_manifest'); bindings.add(database.binding);
97
+ const prefix = 'data/d1/' + database.binding;
98
+ check(database.dumpPath === prefix + '.sql' && Array.isArray(database.migrationLedger), 'invalid_manifest');
99
+ verified(database.dumpPath, database.dumpBytes, database.dumpSha256);
100
+ member(prefix + '.json'); member(prefix + '-migrations.json');
101
+ check(JSON.stringify(json(prefix + '.json')) === JSON.stringify(database) && JSON.stringify(json(prefix + '-migrations.json')) === JSON.stringify(database.migrationLedger), 'invalid_manifest');
102
+ }
103
+ for (const bucket of manifest.buckets) {
104
+ check(bucket && typeof bucket.binding === 'string' && /^[A-Za-z_][A-Za-z0-9_]*$/.test(bucket.binding) && !bindings.has(bucket.binding) && Array.isArray(bucket.objects), 'invalid_manifest'); bindings.add(bucket.binding);
105
+ member('data/r2/' + bucket.binding + '.json');
106
+ check(JSON.stringify(json('data/r2/' + bucket.binding + '.json')) === JSON.stringify(bucket), 'invalid_manifest');
107
+ const keys = new Set();
108
+ for (const object of bucket.objects) {
109
+ check(object && typeof object.key === 'string' && !keys.has(object.key) && safePath(object.path) && object.path.startsWith('data/r2/' + bucket.binding + '/'), 'invalid_manifest'); keys.add(object.key);
110
+ check(metadata(object.httpMetadata) && metadata(object.customMetadata), 'invalid_manifest');
111
+ verified(object.path, object.size, object.sha256);
112
+ }
113
+ }
114
+ check(used.size === entries.size, 'unexpected_member');
115
+ return { manifest, entries };
116
+ }
@@ -0,0 +1,110 @@
1
+ import { mkdtemp, rm, writeFile } from 'node:fs/promises';
2
+ import { tmpdir } from 'node:os';
3
+ import { join, relative } from 'node:path';
4
+ import { createRequire } from 'node:module';
5
+ import { RerError } from './errors.mjs';
6
+ import { randomUUID } from 'node:crypto';
7
+ import { isManagedDataZip, readManagedDataZip } from './managed-data-zip.mjs';
8
+ import { loadPolicy } from './portability-policy.mjs';
9
+ import { managedSchema, managedCompatibility } from './managed-data-schema.mjs';
10
+ import { mapMigrationLedger, appliedMigrations, platformMigrationDeclaration, migrationInventory } from './portability-migrations.mjs';
11
+ import { createPortability, PortabilityError } from '../modules/portability/index.mjs';
12
+ import { readLedger } from '../modules/portability/ledgers.mjs';
13
+ import { exportArchive, fileRecords } from '../modules/portability/files.mjs';
14
+ import { portabilityPlan, ensurePortabilitySchema } from '../modules/container/portability-schema.mjs';
15
+ import { splitStatements } from '../modules/portability/sql-text.mjs';
16
+ import { sqliteTokens } from '../modules/portability/sqlite-tokens.mjs';
17
+
18
+ const check = (condition, reason) => { if (!condition) throw new PortabilityError('managed_data_' + reason, 409); };
19
+ const ledgerIdentity = entries => JSON.stringify(entries.map(({ name, sha256 }) => ({ name, sha256 })).sort((a, b) => a.name.localeCompare(b.name)));
20
+
21
+ // Optional adapter door. The callback still owns authentication, confirmation,
22
+ // custody and native restore. Only its input path changes, for this one call.
23
+ // Source SQL runs solely in a disposable workerd D1 sandbox, never in the target.
24
+ export async function withManagedDataImport(context, declaration, restore) {
25
+ try { return await intake(context, declaration, restore); }
26
+ catch (error) {
27
+ if (!(error instanceof PortabilityError)) throw error;
28
+ const reason = error.code.startsWith('managed_data_') ? error.code : error.code.split(':').slice(0, 2).join(':');
29
+ throw new RerError(reason, `Managed data restore refused (${reason}). The destination has not accepted this archive.`, 4);
30
+ }
31
+ }
32
+ async function intake(context, declaration, restore) {
33
+ if (!await isManagedDataZip(context.path)) return restore(context);
34
+ check(context.target === undefined || context.target === 'local', 'local_only');
35
+ check(context.platform !== 'container' && !context.legacyRecovery && !context.journal, 'unsupported_options');
36
+ const { root } = context, { binding = 'DB', buckets = {}, policy: inputPolicy, policyModule, platformMigrations, transient = [] } = declaration;
37
+ const { manifest, entries } = await readManagedDataZip(context.path);
38
+ check(manifest.databases.length === 1 && manifest.databases[0].binding === binding, 'binding_mismatch');
39
+ check(manifest.buckets.every(bucket => Object.values(buckets).includes(bucket.binding)), 'binding_mismatch');
40
+ const database = manifest.databases[0];
41
+ await mapMigrationLedger({ root, binding, platformMigrations, entries: database.migrationLedger });
42
+ check(database.migrationLedger.length > 0, 'missing_ledger');
43
+ let policy = await loadPolicy(root, inputPolicy, policyModule);
44
+ const { migrationReplay } = await import('../modules/portability/migration-history.mjs');
45
+ const { getPlatformProxy, unstable_splitSqlQuery } = createRequire(join(root, 'package.json'))('wrangler');
46
+ const directory = await mkdtemp(join(tmpdir(), 'rer-managed-data-'));
47
+ let proxy;
48
+ try {
49
+ const configPath = join(directory, 'wrangler.json');
50
+ await writeFile(configPath, JSON.stringify({ name: 'rer-managed-data', compatibility_date: '2026-09-01', d1_databases: [{ binding, database_name: 'rer-managed-data', database_id: randomUUID() }], r2_buckets: [...new Set(Object.values(buckets))].map(name => ({ binding: name, bucket_name: 'rer-managed-' + name.toLowerCase() })) }), { mode: 0o600 });
51
+ proxy = await getPlatformProxy({ configPath, persist: { path: join(directory, 'state') }, remoteBindings: false });
52
+ const db = proxy.env[binding];
53
+ try {
54
+ const sql = new TextDecoder('utf-8', { fatal: true }).decode(entries.get(database.dumpPath));
55
+ check(Buffer.byteLength(sql) <= 16 * 1024 * 1024, 'oversized');
56
+ const statements = splitStatements(sql);
57
+ check(statements.length <= 10000, 'oversized');
58
+ const prepared = [];
59
+ for (const statement of statements) {
60
+ context.signal?.throwIfAborted();
61
+ const tokens = sqliteTokens(statement), head = tokens.slice(0, 3).map(token => token.text.toLowerCase());
62
+ // No attach, extension, transaction or arbitrary query statements. Workerd
63
+ // supplies the independent SQL limits and has no filesystem SQL functions.
64
+ check(head[0] === 'create' && ['table', 'index', 'unique', 'view', 'trigger'].includes(head[1]) || head[0] === 'insert' && head[1] === 'into' || tokens.map(token => token.text.toLowerCase()).join('') === 'pragmadefer_foreign_keys=on', 'sql_refused');
65
+ const values = head[0] === 'insert' ? tokens.findIndex(token => token.type === 'keyword' && token.text.toLowerCase() === 'values') : -1;
66
+ check(!tokens.some(token => token.type === 'variable') && (head[0] !== 'insert' || values >= 0), 'sql_refused');
67
+ // Bind cells only in a top-level INSERT. DDL stays literal, including
68
+ // VALUES inside trigger bodies, where SQLite forbids bind parameters.
69
+ let sql = '', offset = 0;
70
+ const params = [];
71
+ for (const token of values < 0 ? [] : tokens.slice(values + 1)) if (token.type === 'string') {
72
+ const value = token.text.slice(1, -1).replaceAll("''", "'");
73
+ check(Buffer.byteLength(value) <= 2 * 1024 * 1024 && params.length < 100, 'oversized');
74
+ sql += statement.slice(offset, token.start) + '?'; offset = token.end; params.push(value);
75
+ }
76
+ sql += statement.slice(offset);
77
+ check(Buffer.byteLength(sql) <= 100000, 'oversized');
78
+ prepared.push(params.length ? db.prepare(sql).bind(...params) : db.prepare(sql));
79
+ }
80
+ await db.batch(prepared);
81
+ } catch (error) {
82
+ if (error instanceof PortabilityError || context.signal?.aborted) throw error;
83
+ check(false, 'sql_refused');
84
+ }
85
+ const ledger = await readLedger(db);
86
+ check(ledger?.table === 'rer_migrations' && ledgerIdentity(ledger.entries) === ledgerIdentity(database.migrationLedger), 'ledger_mismatch');
87
+ for (const bucket of manifest.buckets) for (const object of bucket.objects) {
88
+ context.signal?.throwIfAborted();
89
+ await proxy.env[bucket.binding].put(object.key, entries.get(object.path), { httpMetadata: object.httpMetadata ?? {}, customMetadata: object.customMetadata ?? {} });
90
+ }
91
+ const boundBuckets = Object.fromEntries(Object.entries(buckets).map(([name, binding]) => [name, proxy.env[binding]]));
92
+ policy = await loadPolicy(root, inputPolicy, policyModule, { db, buckets: boundBuckets });
93
+ const migrationDeclaration = platformMigrationDeclaration(platformMigrations);
94
+ const plan = await migrationInventory(root, binding, migrationDeclaration, unstable_splitSqlQuery);
95
+ const schema = await managedSchema(db, plan, policy, ledger);
96
+ await ensurePortabilitySchema(db, await portabilityPlan(unstable_splitSqlQuery));
97
+ const migrations = async () => migrationReplay(await appliedMigrations(root, db, binding, platformMigrationDeclaration(platformMigrations), unstable_splitSqlQuery), policy, platformMigrationDeclaration(platformMigrations));
98
+ const service = await createPortability({ db, buckets: boundBuckets, policy, migrations });
99
+ const path = join(directory, 'native.rer');
100
+ await exportArchive(service, path, transient);
101
+ for await (const record of fileRecords(path)) if (record.kind === 'manifest') {
102
+ await managedCompatibility(plan, policy, record, migrationDeclaration, boundBuckets);
103
+ break;
104
+ }
105
+ // Native import rechecks the complete archive and keeps all destination
106
+ // emptiness, migration, erasure, row-safety and atomic publication checks.
107
+ const result = await restore({ ...context, path });
108
+ return result && typeof result === 'object' ? { ...result, ...(Object.hasOwn(result, 'input') ? { input: relative(root, context.path) } : {}), managedSchema: schema } : result;
109
+ } finally { try { await proxy?.dispose(); } finally { await rm(directory, { recursive: true, force: true }); } }
110
+ }
@@ -1,3 +1,6 @@
1
+ import { launchSummary } from './run-transfers.mjs';
2
+ import { readySummary } from './ready-summary.mjs';
3
+ import { nativeOwnerEntry } from './native-owner-entry.mjs';
1
4
  import { prepareLiveModels } from './ai-local.mjs';
2
5
  import {fileURLToPath} from 'node:url';
3
6
  import {join} from 'node:path';
@@ -19,23 +22,29 @@ import { readDevKey,requireDevKey,refuseManagedInstallation,resolvePersona,openU
19
22
  import { announceDoorRoute,claimSavedName,moveCheckout,routeRunning,verifyDoorFor } from './door-mode.mjs';
20
23
 
21
24
  // Dev opens through the local identity exchange. Preview serves production output.
22
- export function ready(out,url,dev,json=false,siteUrl,demo){
23
- if(json){out.event('ready',{url,...(dev?{signedInAs:dev.signedInAs??null,openUrl:dev.openUrl}:{}),...(siteUrl?{siteUrl}:{}),...(demo?{demo}:{})});return;}
25
+ export async function ready(out,url,dev,json=false,siteUrl,demo,root,ownerOpenUrl){
26
+ if(json){out.event('ready',{url,...(dev?{signedInAs:dev.signedInAs??null,openUrl:dev.openUrl}:{}),...(ownerOpenUrl?{ownerOpenUrl}:{}),...(siteUrl?{siteUrl}:{}),...(demo?{demo}:{})});return;}
27
+ if (root) {
28
+ for (const line of readySummary({ url, ...dev, siteUrl })) out.human(line);
29
+ for (const line of await launchSummary(root)) out.human(line);
30
+ out.human('Details for tools and troubleshooting:');
31
+ } else out.human(`Your app is ready, open it: ${dev?.openUrl ?? url}`);
24
32
  out.human(`RER_READY ${url}`);
25
33
  if(dev){
26
34
  if(dev.signedInAs)out.human(`RER_SIGNED_IN ${dev.signedInAs}`);
27
35
  else if(dev.ownerLeft)out.human('No active owner remains in this app. Use rer dev --as PERSONA with an active operator, or rer reset --yes and rer setup for a new owner.');
28
36
  out.human(`RER_OPEN ${dev.openUrl}`);
29
- out.human("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.");
37
+ if (!root && !dev.native) out.human("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
38
  }
31
- if(siteUrl)out.human(`RER_SITE ${siteUrl}`);
39
+ if(ownerOpenUrl){out.human(`RER_OWNER_OPEN ${ownerOpenUrl}`);out.human('Native owner entry: open on this computer once within 15 minutes. The link is not saved.');}
40
+ if(siteUrl){if(!root)out.human(`Site: ${siteUrl}`);out.human(`RER_SITE ${siteUrl}`);}
32
41
  if(demo){out.human(`RER_DEMO_ENDS_ON_STOP ${demo.state}`);out.human(`This demo runs on its own data in that folder, which is deleted when it stops: rer demo --stop, Ctrl-C, or at its end (${demo.ends.slice(11,16)}). Public reads and checks run for real. This checkout's own data is untouched.`);}
33
42
  }
34
43
  export async function managedServer(project,options,out,{clock={}}={}){
35
44
  const registry=await serverRegistry(project.root),controller=new AbortController();
36
45
  // A demo registers as its own mode: `rer dev --stop` never stops it by accident.
37
46
  const mode=options.demo?'demo':options.verb;
38
- let store,operation,proxy,activity,app,termination,reason,cleanupPromise,finished,selected,cleaned,site,demo,demoEnd;
47
+ let store,operation,proxy,activity,app,termination,reason,cleanupPromise,finished,selected,cleaned,site,demo,demoEnd,ownerEntry;
39
48
  const scheduling=new AbortController();
40
49
  let activityWrites=Promise.resolve(),pendingActivity,activityWriting=false,infrastructureFailure;
41
50
  function recordActivity(fields){
@@ -44,7 +53,7 @@ export async function managedServer(project,options,out,{clock={}}={}){
44
53
  }
45
54
  const stopped=new Promise(resolve=>{finished=resolve;});
46
55
  const cleanupDone=new Promise(resolve=>{cleaned=resolve;});
47
- const cleanup=()=>cleanupPromise??=(async()=>{scheduling.abort();clearTimeout(demoEnd);activity?.close();await site?.close();await store?.closeCommands();if(demo)await rm(demo.state,{recursive:true,force:true});await proxy?.close();await operation?.release();})();
56
+ const cleanup=()=>cleanupPromise??=(async()=>{scheduling.abort();clearTimeout(demoEnd);activity?.close();await site?.close();await ownerEntry?.close();await store?.closeCommands();if(demo)await rm(demo.state,{recursive:true,force:true});await proxy?.close();await operation?.release();})();
48
57
  const stop=async why=>{reason??=why;controller.abort();finished();await cleanupDone;return {};};
49
58
  const handlers=new Map(['SIGINT','SIGTERM'].map(name=>[name,()=>{termination=name;void stop(name);} ]));
50
59
  for(const [name,handler]of handlers)process.on(name,handler);
@@ -77,10 +86,11 @@ export async function managedServer(project,options,out,{clock={}}={}){
77
86
  // A key is sent (or printed) for a door origin only once the door proves it is ours.
78
87
  if(devIdentity)await verifyDoorFor(running.url);
79
88
  const dev=devIdentity&&options.verb==='dev'&&await devReady(running.url,devKey,options,controller.signal);
80
- await emitReady(out,options,project.root,running.url,dev,running.siteUrl,running.demo);return {reused:true};
89
+ await emitReady(out,options,project.root,running.url,dev,running.siteUrl,running.demo,(await registry.ownerEntry(running)).ownerOpenUrl);return {reused:true};
81
90
  }
82
91
  await registry.serve(async action=>{
83
92
  if(action==='status')return {...activity?.snapshot(),url:selected?.url,probe:proxy?.probe,siteUrl:site?.url,demo};
93
+ if(action==='owner-entry')return ownerEntry?{ownerOpenUrl:ownerEntry.issue()}:{};
84
94
  if(action==='stop')return stop('explicit stop');
85
95
  throw Error('Unknown management operation');
86
96
  });
@@ -97,7 +107,7 @@ export async function managedServer(project,options,out,{clock={}}={}){
97
107
  if(!selected.door)await availablePort(selected.host,selected.port,{root:project.root,saved:!selected.explicitPort&&selected.savedOrigin!==undefined,verb:options.verb});
98
108
  if(!selected.vars[keys.email])throw missing('Private owner settings are missing. Run rer setup first.');
99
109
  if(devIdentity&&!devKey)devKey=await requireDevKey(project.root);
100
- if(devIdentity&&options.verb==='dev')refuseManagedInstallation(selected.vars,process.env);
110
+ if(devIdentity&&['dev','preview'].includes(options.verb))refuseManagedInstallation(selected.vars,process.env);
101
111
  if(options.demo)demo=await demoPreflight(project,selected.vars,options.idleTimeout);
102
112
  const prerequisites=async()=>{
103
113
  if(!demo&&project.adapter.localPrerequisites&&!await project.adapter.localPrerequisites({root:project.root,vars:selected.vars,email:selected.vars[keys.email],demo:false}))throw missing('Local migrations, owner or local authentication/mail settings are missing. Run rer setup first.');
@@ -115,10 +125,11 @@ export async function managedServer(project,options,out,{clock={}}={}){
115
125
  activity=activityClock({...clock,seconds:options.idleTimeout,onIdle:()=>void stop('idle timeout'),onChange:recordActivity});
116
126
  if(demo)demoEnd=setTimeout(()=>void stop('demo ended'),Math.min(Date.parse(demo.ends)-Date.now(),2**31-1));
117
127
  const crons=project.adapter.scheduled!==false?await declaredCrons(project.root,project.adapter).catch(error=>{throw refuse(`The declared scheduled triggers cannot run locally: ${error.message}`);}):[];
118
- proxy=await runtimeFrontDoor({host:selected.host,port:selected.port,activity,door:selected.door,devKey:devKey??undefined,origin:selected.url});const internal=proxy.runtimePort;
128
+ if(devIdentity&&(options.verb==='preview'||demo))ownerEntry=nativeOwnerEntry({origin:selected.url,state:demo?.state??join(project.root,'.wrangler','state'),store,demo:!!demo,devKey});
129
+ proxy=await runtimeFrontDoor({host:selected.host,port:selected.port,activity,door:selected.door,devKey:devKey??undefined,origin:selected.url,ownerEntry:(req,res)=>ownerEntry?.handle(req,res)});const internal=proxy.runtimePort;
119
130
  if(selected.door)await announceDoorRoute(selected,proxy,registry);
120
131
  const modelsLive = options.verb==='dev' ? await prepareLiveModels(project.root,options,store,controller.signal) : undefined;
121
- const adapterOptions={modelsLive,demo,selectedOrigin:selected.url,operationToken:operation.token,privateSettings:project.adapter.settings,runtimePort:internal,devKey:devKey??undefined,devLogin:false,scheduled:crons.length>0,beforeStart:options.verb==='dev'?prerequisites:undefined};
132
+ const adapterOptions={modelsLive,demo,selectedOrigin:selected.url,operationToken:operation.token,privateSettings:project.adapter.settings,runtimePort:internal,setRuntimePort:port=>proxy.setRuntimePort(port),devKey:devKey??undefined,devLogin:false,scheduled:crons.length>0,beforeStart:options.verb==='dev'?prerequisites:undefined};
122
133
  const input={[keys.email]:selected.vars[keys.email]};
123
134
  if(options.verb==='preview')await productionBuild(project,()=>store.run('Building fixed production output',process.execPath,[fileURLToPath(new URL('../bin/rer.mjs',import.meta.url)),'build','--cwd',project.root,'--json'],{cwd:project.root,phase:'build',signal:controller.signal,env:{...process.env,RER_LOCAL_OPERATION:operation.token},afterCommand:()=>recoverInterrupted(project.root,operation)}),text=>store.report(text));
124
135
  await serverResources(project.root);
@@ -144,10 +155,10 @@ export async function managedServer(project,options,out,{clock={}}={}){
144
155
  if(options.json)out.event('warning',{code:error.code,message});else out.progress(`Warning: ${message}\n`);
145
156
  }
146
157
  }
147
- activity.ready();await emitReady(out,options,project.root,selected.url,dev,site?.url,demo);
158
+ activity.ready();await emitReady(out,options,project.root,selected.url,dev,site?.url,demo,ownerEntry?.issue());
148
159
  if(crons.length){
149
160
  out.progress(`Running ${crons.length===1?'the scheduled trigger':'the scheduled triggers'} locally: ${crons.join(' | ')}\n`);
150
- void runScheduled({crons,url:`http://127.0.0.1:${internal}`,path:options.verb==='preview'?'/cdn-cgi/local/scheduled':undefined,signal:scheduling.signal,report:run=>reportScheduled(out,options,run)});
161
+ void runScheduled({crons,url:`http://127.0.0.1:${proxy.runtimePort}`,path:options.verb==='preview'?'/cdn-cgi/local/scheduled':undefined,signal:scheduling.signal,report:run=>reportScheduled(out,options,run)});
151
162
  }
152
163
  const result=await stopped;
153
164
  if(termination)throw new RerError('cancelled',`Stopped by ${termination} after owned children were stopped.`,termination==='SIGINT'?130:143);
@@ -164,12 +175,12 @@ function reportScheduled(out,options,run){
164
175
  // dev also keeps its RER_OPEN link in owner-only .rer/local/open-url (0600),
165
176
  // which reset removes with the rest of .rer/local, for a person or agent that
166
177
  // did not keep the terminal output.
167
- async function emitReady(out,options,root,url,dev,siteUrl,demo){
178
+ async function emitReady(out,options,root,url,dev,siteUrl,demo,ownerOpenUrl){
168
179
  if(options.verb==='dev'&&dev?.openUrl){
169
180
  const {privateDirectory,writePrivateFile}=await shared('private-files.mjs');
170
181
  await writePrivateFile(join(await privateDirectory(root,['.rer','local']),'open-url'),dev.openUrl+'\n');
171
182
  }
172
- ready(out,url,dev||undefined,options.json,siteUrl,demo);
183
+ await ready(out,url,dev||(ownerOpenUrl?{openUrl:ownerOpenUrl,native:true}:undefined),options.json,siteUrl,demo,options.verb === 'dev' && !options.demo ? root : undefined,ownerOpenUrl);
173
184
  }
174
185
  async function devReady(url,key,options,signal){
175
186
  const as=options.as??'owner',persona=await resolvePersona(url,key,as,signal);