@runeditrun/rer 0.2.83 → 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 (269) hide show
  1. package/README.md +21 -24
  2. package/cli/README.md +17 -1
  3. package/cli/authoring.mjs +1 -0
  4. package/cli/bounded.mjs +1 -1
  5. package/cli/business/carry.mjs +149 -0
  6. package/cli/business/command.mjs +6 -1
  7. package/cli/business/connect.mjs +1 -1
  8. package/cli/business/local-commands.mjs +2 -0
  9. package/cli/business/opt-ins.mjs +8 -4
  10. package/cli/business/questions.mjs +4 -1
  11. package/cli/business/state.mjs +1 -1
  12. package/cli/business/supervisor.mjs +7 -1
  13. package/cli/container-transfer.mjs +2 -2
  14. package/cli/contract.mjs +7 -7
  15. package/cli/demo-host.mjs +5 -1
  16. package/cli/demo-native.mjs +2 -1
  17. package/cli/demo.mjs +6 -1
  18. package/cli/deploy-command.mjs +6 -4
  19. package/cli/deploy.mjs +9 -7
  20. package/cli/errors.mjs +1 -1
  21. package/cli/export.mjs +1 -1
  22. package/cli/human-error.mjs +20 -0
  23. package/cli/lifecycle.mjs +23 -5
  24. package/cli/main.mjs +1 -1
  25. package/cli/managed-data-schema.mjs +114 -0
  26. package/cli/managed-data-zip.mjs +116 -0
  27. package/cli/managed-data.mjs +110 -0
  28. package/cli/managed-server.mjs +25 -14
  29. package/cli/native-owner-entry.mjs +86 -0
  30. package/cli/output.mjs +3 -2
  31. package/cli/parse.mjs +3 -3
  32. package/cli/portability-empty.mjs +59 -0
  33. package/cli/portability-migrations.mjs +7 -5
  34. package/cli/portability-policy.mjs +5 -2
  35. package/cli/portability-worker.mjs +15 -6
  36. package/cli/portability.mjs +30 -8
  37. package/cli/ready-summary.mjs +9 -0
  38. package/cli/run-transfers.mjs +37 -0
  39. package/cli/runtime-front-door.mjs +4 -4
  40. package/cli/server-proxy.mjs +4 -3
  41. package/cli/server-registry.mjs +13 -8
  42. package/cli/site.mjs +6 -1
  43. package/cli/transfers.mjs +17 -4
  44. package/edition-authoring/base-copies.mjs +125 -30
  45. package/edition-authoring/copy-checkout.mjs +7 -120
  46. package/edition-authoring/edition-migrations.mjs +41 -0
  47. package/edition-authoring/files.mjs +67 -0
  48. package/edition-authoring/lock.mjs +2 -2
  49. package/edition-authoring/module-closure.mjs +232 -0
  50. package/edition-authoring/module-edges.mjs +100 -0
  51. package/edition-authoring/module-resolution.mjs +66 -0
  52. package/edition-authoring/module-syntax.mjs +57 -0
  53. package/edition-authoring/package-target.mjs +92 -0
  54. package/edition-authoring/runtime-dependencies.mjs +74 -97
  55. package/edition-authoring/vendor/babel-parser/LICENSE +22 -0
  56. package/edition-authoring/vendor/babel-parser/README.md +21 -0
  57. package/edition-authoring/vendor/babel-parser/babel-helper-string-parser/index.mjs +292 -0
  58. package/edition-authoring/vendor/babel-parser/babel-helper-validator-identifier/identifier.mjs +66 -0
  59. package/edition-authoring/vendor/babel-parser/babel-helper-validator-identifier/index.mjs +13 -0
  60. package/edition-authoring/vendor/babel-parser/babel-helper-validator-identifier/keyword.mjs +31 -0
  61. package/edition-authoring/vendor/babel-parser/babel-parser/index.mjs +99 -0
  62. package/edition-authoring/vendor/babel-parser/babel-parser/options.mjs +81 -0
  63. package/edition-authoring/vendor/babel-parser/babel-parser/parse-error/bind-operator-errors.mjs +8 -0
  64. package/edition-authoring/vendor/babel-parser/babel-parser/parse-error/module-errors.mjs +15 -0
  65. package/edition-authoring/vendor/babel-parser/babel-parser/parse-error/parse-expression-errors.mjs +10 -0
  66. package/edition-authoring/vendor/babel-parser/babel-parser/parse-error/pipeline-operator-errors.mjs +29 -0
  67. package/edition-authoring/vendor/babel-parser/babel-parser/parse-error/standard-errors.mjs +224 -0
  68. package/edition-authoring/vendor/babel-parser/babel-parser/parse-error/strict-mode-errors.mjs +17 -0
  69. package/edition-authoring/vendor/babel-parser/babel-parser/parse-error/to-node-description.mjs +31 -0
  70. package/edition-authoring/vendor/babel-parser/babel-parser/parse-error.mjs +114 -0
  71. package/edition-authoring/vendor/babel-parser/babel-parser/parser/base.mjs +34 -0
  72. package/edition-authoring/vendor/babel-parser/babel-parser/parser/comments.mjs +199 -0
  73. package/edition-authoring/vendor/babel-parser/babel-parser/parser/expression.mjs +1794 -0
  74. package/edition-authoring/vendor/babel-parser/babel-parser/parser/index.mjs +76 -0
  75. package/edition-authoring/vendor/babel-parser/babel-parser/parser/lval.mjs +439 -0
  76. package/edition-authoring/vendor/babel-parser/babel-parser/parser/node.mjs +111 -0
  77. package/edition-authoring/vendor/babel-parser/babel-parser/parser/statement.mjs +1962 -0
  78. package/edition-authoring/vendor/babel-parser/babel-parser/parser/util.mjs +244 -0
  79. package/edition-authoring/vendor/babel-parser/babel-parser/plugin-utils.mjs +116 -0
  80. package/edition-authoring/vendor/babel-parser/babel-parser/plugins/estree.mjs +417 -0
  81. package/edition-authoring/vendor/babel-parser/babel-parser/plugins/flow/index.mjs +2412 -0
  82. package/edition-authoring/vendor/babel-parser/babel-parser/plugins/flow/scope.mjs +37 -0
  83. package/edition-authoring/vendor/babel-parser/babel-parser/plugins/jsx/index.mjs +420 -0
  84. package/edition-authoring/vendor/babel-parser/babel-parser/plugins/jsx/xhtml.mjs +261 -0
  85. package/edition-authoring/vendor/babel-parser/babel-parser/plugins/placeholders.mjs +205 -0
  86. package/edition-authoring/vendor/babel-parser/babel-parser/plugins/typescript/index.mjs +2889 -0
  87. package/edition-authoring/vendor/babel-parser/babel-parser/plugins/typescript/scope.mjs +115 -0
  88. package/edition-authoring/vendor/babel-parser/babel-parser/plugins/v8intrinsic.mjs +26 -0
  89. package/edition-authoring/vendor/babel-parser/babel-parser/tokenizer/context.mjs +22 -0
  90. package/edition-authoring/vendor/babel-parser/babel-parser/tokenizer/index.mjs +1037 -0
  91. package/edition-authoring/vendor/babel-parser/babel-parser/tokenizer/state.mjs +178 -0
  92. package/edition-authoring/vendor/babel-parser/babel-parser/tokenizer/types.mjs +583 -0
  93. package/edition-authoring/vendor/babel-parser/babel-parser/types.mjs +1 -0
  94. package/edition-authoring/vendor/babel-parser/babel-parser/util/class-scope.mjs +82 -0
  95. package/edition-authoring/vendor/babel-parser/babel-parser/util/expression-scope.mjs +121 -0
  96. package/edition-authoring/vendor/babel-parser/babel-parser/util/identifier.mjs +23 -0
  97. package/edition-authoring/vendor/babel-parser/babel-parser/util/location.mjs +34 -0
  98. package/edition-authoring/vendor/babel-parser/babel-parser/util/production-parameter.mjs +40 -0
  99. package/edition-authoring/vendor/babel-parser/babel-parser/util/scope.mjs +173 -0
  100. package/edition-authoring/vendor/babel-parser/babel-parser/util/scopeflags.mjs +66 -0
  101. package/edition-authoring/vendor/babel-parser/babel-parser/util/whitespace.mjs +61 -0
  102. package/edition-authoring/vendor/yaml/LICENSE +13 -0
  103. package/edition-authoring/vendor/yaml/README.md +11 -0
  104. package/edition-authoring/vendor/yaml/compose/compose-collection.mjs +88 -0
  105. package/edition-authoring/vendor/yaml/compose/compose-doc.mjs +43 -0
  106. package/edition-authoring/vendor/yaml/compose/compose-node.mjs +110 -0
  107. package/edition-authoring/vendor/yaml/compose/compose-scalar.mjs +86 -0
  108. package/edition-authoring/vendor/yaml/compose/composer.mjs +218 -0
  109. package/edition-authoring/vendor/yaml/compose/resolve-block-map.mjs +115 -0
  110. package/edition-authoring/vendor/yaml/compose/resolve-block-scalar.mjs +198 -0
  111. package/edition-authoring/vendor/yaml/compose/resolve-block-seq.mjs +49 -0
  112. package/edition-authoring/vendor/yaml/compose/resolve-end.mjs +37 -0
  113. package/edition-authoring/vendor/yaml/compose/resolve-flow-collection.mjs +207 -0
  114. package/edition-authoring/vendor/yaml/compose/resolve-flow-scalar.mjs +228 -0
  115. package/edition-authoring/vendor/yaml/compose/resolve-props.mjs +148 -0
  116. package/edition-authoring/vendor/yaml/compose/util-contains-newline.mjs +34 -0
  117. package/edition-authoring/vendor/yaml/compose/util-empty-scalar-position.mjs +27 -0
  118. package/edition-authoring/vendor/yaml/compose/util-flow-indent-check.mjs +15 -0
  119. package/edition-authoring/vendor/yaml/compose/util-map-includes.mjs +13 -0
  120. package/edition-authoring/vendor/yaml/doc/Document.mjs +333 -0
  121. package/edition-authoring/vendor/yaml/doc/anchors.mjs +74 -0
  122. package/edition-authoring/vendor/yaml/doc/applyReviver.mjs +54 -0
  123. package/edition-authoring/vendor/yaml/doc/createNode.mjs +88 -0
  124. package/edition-authoring/vendor/yaml/doc/directives.mjs +176 -0
  125. package/edition-authoring/vendor/yaml/errors.mjs +57 -0
  126. package/edition-authoring/vendor/yaml/index.mjs +17 -0
  127. package/edition-authoring/vendor/yaml/log.mjs +11 -0
  128. package/edition-authoring/vendor/yaml/nodes/Alias.mjs +119 -0
  129. package/edition-authoring/vendor/yaml/nodes/Collection.mjs +147 -0
  130. package/edition-authoring/vendor/yaml/nodes/Node.mjs +38 -0
  131. package/edition-authoring/vendor/yaml/nodes/Pair.mjs +36 -0
  132. package/edition-authoring/vendor/yaml/nodes/Scalar.mjs +24 -0
  133. package/edition-authoring/vendor/yaml/nodes/YAMLMap.mjs +144 -0
  134. package/edition-authoring/vendor/yaml/nodes/YAMLSeq.mjs +113 -0
  135. package/edition-authoring/vendor/yaml/nodes/addPairToJSMap.mjs +62 -0
  136. package/edition-authoring/vendor/yaml/nodes/identity.mjs +36 -0
  137. package/edition-authoring/vendor/yaml/nodes/toJS.mjs +35 -0
  138. package/edition-authoring/vendor/yaml/parse/cst-scalar.mjs +214 -0
  139. package/edition-authoring/vendor/yaml/parse/cst-stringify.mjs +61 -0
  140. package/edition-authoring/vendor/yaml/parse/cst-visit.mjs +97 -0
  141. package/edition-authoring/vendor/yaml/parse/cst.mjs +98 -0
  142. package/edition-authoring/vendor/yaml/parse/lexer.mjs +722 -0
  143. package/edition-authoring/vendor/yaml/parse/line-counter.mjs +39 -0
  144. package/edition-authoring/vendor/yaml/parse/parser.mjs +975 -0
  145. package/edition-authoring/vendor/yaml/public-api.mjs +102 -0
  146. package/edition-authoring/vendor/yaml/schema/Schema.mjs +37 -0
  147. package/edition-authoring/vendor/yaml/schema/common/map.mjs +17 -0
  148. package/edition-authoring/vendor/yaml/schema/common/null.mjs +15 -0
  149. package/edition-authoring/vendor/yaml/schema/common/seq.mjs +17 -0
  150. package/edition-authoring/vendor/yaml/schema/common/string.mjs +14 -0
  151. package/edition-authoring/vendor/yaml/schema/core/bool.mjs +19 -0
  152. package/edition-authoring/vendor/yaml/schema/core/float.mjs +43 -0
  153. package/edition-authoring/vendor/yaml/schema/core/int.mjs +38 -0
  154. package/edition-authoring/vendor/yaml/schema/core/schema.mjs +23 -0
  155. package/edition-authoring/vendor/yaml/schema/json/schema.mjs +62 -0
  156. package/edition-authoring/vendor/yaml/schema/tags.mjs +96 -0
  157. package/edition-authoring/vendor/yaml/schema/yaml-1.1/binary.mjs +59 -0
  158. package/edition-authoring/vendor/yaml/schema/yaml-1.1/bool.mjs +26 -0
  159. package/edition-authoring/vendor/yaml/schema/yaml-1.1/float.mjs +46 -0
  160. package/edition-authoring/vendor/yaml/schema/yaml-1.1/int.mjs +71 -0
  161. package/edition-authoring/vendor/yaml/schema/yaml-1.1/merge.mjs +67 -0
  162. package/edition-authoring/vendor/yaml/schema/yaml-1.1/omap.mjs +74 -0
  163. package/edition-authoring/vendor/yaml/schema/yaml-1.1/pairs.mjs +78 -0
  164. package/edition-authoring/vendor/yaml/schema/yaml-1.1/schema.mjs +39 -0
  165. package/edition-authoring/vendor/yaml/schema/yaml-1.1/set.mjs +93 -0
  166. package/edition-authoring/vendor/yaml/schema/yaml-1.1/timestamp.mjs +101 -0
  167. package/edition-authoring/vendor/yaml/stringify/foldFlowLines.mjs +146 -0
  168. package/edition-authoring/vendor/yaml/stringify/stringify.mjs +130 -0
  169. package/edition-authoring/vendor/yaml/stringify/stringifyCollection.mjs +155 -0
  170. package/edition-authoring/vendor/yaml/stringify/stringifyComment.mjs +20 -0
  171. package/edition-authoring/vendor/yaml/stringify/stringifyDocument.mjs +85 -0
  172. package/edition-authoring/vendor/yaml/stringify/stringifyNumber.mjs +25 -0
  173. package/edition-authoring/vendor/yaml/stringify/stringifyPair.mjs +150 -0
  174. package/edition-authoring/vendor/yaml/stringify/stringifyString.mjs +338 -0
  175. package/edition-authoring/vendor/yaml/util.mjs +11 -0
  176. package/edition-authoring/vendor/yaml/visit.mjs +233 -0
  177. package/edition-authoring/workflow-copies.mjs +125 -0
  178. package/edition-authoring/workflow-scripts.mjs +112 -0
  179. package/foundation/runtime/contract-docs.mjs +754 -0
  180. package/foundation/runtime/run-data.d.mts +1 -0
  181. package/foundation/runtime/run-data.mjs +2 -0
  182. package/global/CHANGELOG.md +55 -3
  183. package/global/README.md +21 -24
  184. package/global/cli/command.mjs +8 -2
  185. package/global/cli/install.mjs +4 -5
  186. package/global/cli/path-link.mjs +3 -2
  187. package/global/cli/repo-command.mjs +3 -7
  188. package/global/launch/dev.mjs +12 -2
  189. package/global/launch/facts.mjs +2 -2
  190. package/global/launch/render.mjs +16 -9
  191. package/global/launch/state.mjs +4 -4
  192. package/global/main.mjs +12 -7
  193. package/install.sh +17 -6
  194. package/modules/container/README.md +6 -1
  195. package/modules/container/adapters/d1-limits.mjs +286 -0
  196. package/modules/container/adapters/d1-sqlite.mjs +95 -0
  197. package/modules/container/adapters/d1-workerd-preload.mjs +19 -0
  198. package/modules/container/adapters/d1-workerd-setup.mjs +26 -0
  199. package/modules/container/adapters/d1-workerd.mjs +5 -0
  200. package/modules/container/adapters/sqlite-worker.mjs +5 -3
  201. package/modules/container/build.mjs +1 -0
  202. package/modules/container/image-variables.mjs +8 -3
  203. package/modules/container/transfer.mjs +14 -4
  204. package/modules/container/worker-engine.mjs +3 -3
  205. package/modules/demo/declaration.mjs +1 -4
  206. package/modules/demo/vite.mjs +1 -1
  207. package/modules/demo-host/build.mjs +1 -0
  208. package/modules/demo-host/native/prepare.mjs +2 -1
  209. package/modules/demo-host/native/sql.mjs +7 -2
  210. package/modules/deploy-kit/README.md +23 -3
  211. package/modules/deploy-kit/d1-import-diagnostics.mjs +56 -0
  212. package/modules/deploy-kit/d1-import.mjs +17 -5
  213. package/modules/deploy-kit/diagnostics.mjs +29 -3
  214. package/modules/deploy-kit/owner-cli.mjs +20 -7
  215. package/modules/deploy-kit/provider-transport.mjs +19 -4
  216. package/modules/deploy-kit/provider.mjs +57 -22
  217. package/modules/deploy-kit/readiness-window.mjs +1 -1
  218. package/modules/deploy-kit/site.mjs +8 -3
  219. package/modules/deploy-kit/templates.mjs +3 -3
  220. package/modules/deploy-kit/worker.mjs +4 -4
  221. package/modules/import-kit/src/d1/README.md +23 -4
  222. package/modules/import-kit/src/d1/atomic-prefix.ts +2 -2
  223. package/modules/import-kit/src/d1/budget.ts +15 -7
  224. package/modules/import-kit/src/d1/column-layout.ts +5 -0
  225. package/modules/import-kit/src/d1/creation.ts +15 -8
  226. package/modules/import-kit/src/d1/database.ts +3 -3
  227. package/modules/import-kit/src/d1/erasure-pages.ts +2 -1
  228. package/modules/import-kit/src/d1/erasure.ts +2 -2
  229. package/modules/import-kit/src/d1/jobs.ts +10 -6
  230. package/modules/import-kit/src/d1/plans.ts +14 -8
  231. package/modules/import-kit/src/d1/retirement.ts +2 -2
  232. package/modules/import-kit/src/d1/sets.ts +1 -3
  233. package/modules/import-kit/src/d1/staged-creation.ts +3 -3
  234. package/modules/import-kit/src/door/limits.ts +1 -1
  235. package/modules/import-kit/src/door/refusals.ts +1 -1
  236. package/modules/portability/README.md +112 -4
  237. package/modules/portability/backfills.mjs +9 -8
  238. package/modules/portability/bridge.mjs +12 -5
  239. package/modules/portability/catalogue.mjs +12 -5
  240. package/modules/portability/files.mjs +4 -3
  241. package/modules/portability/http.mjs +3 -3
  242. package/modules/portability/import.mjs +18 -9
  243. package/modules/portability/index.d.mts +4 -3
  244. package/modules/portability/index.mjs +1 -1
  245. package/modules/portability/ledgers.mjs +3 -2
  246. package/modules/portability/legacy-safety.mjs +11 -2
  247. package/modules/portability/migration-history.mjs +2 -2
  248. package/modules/portability/progress.mjs +1 -0
  249. package/modules/portability/restore-empty.mjs +12 -0
  250. package/modules/release/github-verify.mjs +11 -2
  251. package/onboarding/adapter.mjs +35 -6
  252. package/onboarding/app-fetch.mjs +6 -4
  253. package/onboarding/development-runtime.mjs +19 -0
  254. package/onboarding/local/operation.mjs +45 -27
  255. package/onboarding/loopback-fetch.mjs +16 -0
  256. package/onboarding/operator-installer.mjs +4 -6
  257. package/onboarding/process-diagnostics.mjs +6 -2
  258. package/onboarding/production-runtime.mjs +4 -2
  259. package/onboarding/readiness.mjs +1 -0
  260. package/package.json +1 -1
  261. package/scripts/publish-check/README.md +6 -0
  262. package/scripts/publish-check/history_scan.py +5 -3
  263. package/scripts/publish-check/hygiene_config.py +2 -1
  264. package/scripts/publish-check/public_hygiene.py +1 -1
  265. package/scripts/publish-check/report.py +9 -3
  266. package/scripts/publish-check/third-party-notices.json +13 -0
  267. package/scripts/publish-check/third_party_notices.py +38 -0
  268. package/scripts/publish-check/tree_scan.py +6 -0
  269. package/scripts/publish-check.mjs +1 -1
package/cli/deploy.mjs CHANGED
@@ -54,23 +54,23 @@ export function ownerDeploy(declarationPath, lifecycle) {
54
54
  const { env, supplied, generated, reused, rotated } = await deployEnvironment({ root, options, declaration, setupManifest, settings: lifecycle.settings, localOnlyKeys });
55
55
  if (options.setSecret || options.unsetSecret) secretChangeInputs(declaration, options, env, again);
56
56
  const kept = keptSecrets(declaration, setupManifest);
57
- if (cloudflareCredentials.some(name => !env[name]?.trim())) throw missing(missingCredentialsMessage(declaration, env, {dryRun:options.dryRun,signIn}));
57
+ if (!options.dryRun && cloudflareCredentials.some(name => !env[name]?.trim())) throw missing(missingCredentialsMessage(declaration, env, {dryRun:options.dryRun,signIn}));
58
58
  const { main } = await import('../modules/deploy-kit/owner-cli.mjs');
59
59
  const { createRedaction } = await shared('redaction.mjs');
60
60
  const redaction = createRedaction();
61
61
  redaction.addSecrets([...(await settingKinds({ declaration, setupManifest })).privateValues(env), env.CLOUDFLARE_ACCOUNT_ID, ...(declaration.secrets ?? []).map(item => env[item.environment]).filter(Boolean)]);
62
62
  const claim = setupManifest?.steps?.find(item => item.type === 'owner-claim');
63
63
  const declared = binding => (Array.isArray(declaration.secrets) ? declaration.secrets : []).find(item => item?.binding === binding);
64
- const owner = async (command, extra = [], values = env, more = {}) => main([command, '--declaration', declarationPath, '--environment', environment, ...(['dry-run', 'deploy', 'health'].includes(command) ? await deployProductArguments(root, productDirectory) : []), ...extra], values, { cwd: root, localOnlyKeys, ...more });
64
+ const owner = async (command, extra = [], values = env, more = {}) => main([command, '--declaration', declarationPath, '--environment', environment, ...(['dry-run', 'local-plan', 'deploy', 'health'].includes(command) ? await deployProductArguments(root, productDirectory) : []), ...extra], values, { cwd: root, localOnlyKeys, ...more });
65
65
  // Keep confirmed creation events across deploy and the independent health call.
66
66
  const created = [];
67
67
  let step = 'deploy', deployed;
68
68
  try {
69
69
  if (options.dryRun) {
70
- const {optionalUnset} = await owner('dry-run');
71
- if (optionalUnset.length) store.report(`deploy: optional secrets not supplied: ${optionalUnset.join(', ')}. A Worker that does not hold one deploys without it.`);
72
- store.report('deploy: dry run complete; target secret bindings checked; no deployment occurred.');
73
- return { dryRun: true, deployed: false };
70
+ const {plan} = await owner('local-plan');
71
+ store.report(`Would deploy: ${JSON.stringify(plan)}`);
72
+ store.report('deploy: local dry run complete; no credentials were needed, no Cloudflare requests were made and nothing was changed remotely. Target secrets and remote readiness were not checked.');
73
+ return { dryRun: true, deployed: false, plan };
74
74
  }
75
75
  // A custom hook's own state file, when its deploy named one.
76
76
  const state = change.length ? recordedState : await hookState(root, environment);
@@ -137,7 +137,9 @@ export function ownerDeploy(declarationPath, lifecycle) {
137
137
  } catch (error) {
138
138
  if (error instanceof RerError) throw error;
139
139
  const { commandFailure } = await shared('failures.mjs');
140
- throw commandFailure(failureText({ error, step, created, again, retry, secretChange: Boolean(change.length && !options.remove), deployed, redact: redaction.redact }));
140
+ const d1Import = deployDiagnostic(error,redaction.redact)?.d1Import;
141
+ throw commandFailure(failureText({ error, step, created, again, retry, secretChange: Boolean(change.length && !options.remove), deployed, redact: redaction.redact }),
142
+ d1Import ? {d1Import} : {});
141
143
  }
142
144
  }, { declaration: declarationPath, managesHostedOwner: true });
143
145
  }
package/cli/errors.mjs CHANGED
@@ -14,5 +14,5 @@ export async function lifecycleError(error, store) {
14
14
  const {shared}=await import('./shared.mjs');
15
15
  const {approvedFailure}=await shared('failures.mjs');
16
16
  const approved=approvedFailure(error);
17
- return new RerError(['owner_conflict','machine_busy'].includes(approved?.code)?'safety_refusal':'command_failed', approved?.message ?? `${store?.state?.step ?? 'Local lifecycle'} failed. Inspect the private redacted capture log.`, approved?.exitCode ?? 1, {childExitCode:approved?.childExitCode,signal:approved?.signal});
17
+ return new RerError(['owner_conflict','machine_busy'].includes(approved?.code)?'safety_refusal':'command_failed', approved?.message ?? `${store?.state?.step ?? 'Local lifecycle'} failed. Inspect the private redacted capture log.`, approved?.exitCode ?? 1, {childExitCode:approved?.childExitCode,signal:approved?.signal,...(approved?.d1Import ? {d1Import:approved.d1Import} : {})});
18
18
  }
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/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'];
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
+ }