@runeditrun/rer 0.2.84 → 0.2.86
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.
- package/README.md +156 -50
- package/cli/README.md +78 -2
- package/cli/agent-door.mjs +149 -0
- package/cli/agent-fixtures.mjs +16 -0
- package/cli/agent-mail.mjs +54 -0
- package/cli/agent-network.mjs +64 -0
- package/cli/agent-reviewers.mjs +110 -0
- package/cli/agent-secrets.mjs +23 -0
- package/cli/agent-sponsors.mjs +43 -0
- package/cli/agent-surface.mjs +34 -0
- package/cli/agent-webhooks.mjs +19 -0
- package/cli/authoring.mjs +2 -1
- package/cli/bounded.mjs +1 -1
- package/cli/business/archive-task.mjs +32 -0
- package/cli/business/carry.mjs +151 -0
- package/cli/business/catchup-task.mjs +42 -0
- package/cli/business/choices.mjs +97 -0
- package/cli/business/command.mjs +29 -6
- package/cli/business/connect.mjs +20 -8
- package/cli/business/detector.mjs +4 -1
- package/cli/business/device.mjs +13 -0
- package/cli/business/export-inputs.mjs +69 -0
- package/cli/business/export-task.mjs +17 -0
- package/cli/business/invocation.mjs +8 -1
- package/cli/business/keychain.mjs +8 -1
- package/cli/business/lease.mjs +11 -1
- package/cli/business/local-commands.mjs +80 -30
- package/cli/business/outbound.mjs +9 -4
- package/cli/business/parsers-config.mjs +8 -7
- package/cli/business/parsers-syntax.mjs +7 -3
- package/cli/business/permissions.mjs +2 -0
- package/cli/business/questions.mjs +89 -6
- package/cli/business/setup-command.mjs +49 -0
- package/cli/business/setup-company.json +15 -0
- package/cli/business/setup-demo.mjs +95 -0
- package/cli/business/state.mjs +44 -10
- package/cli/business/supervisor.mjs +97 -32
- package/cli/business/vendor-door.mjs +230 -0
- package/cli/business/vendors/README.md +29 -3
- package/cli/business/vendors/common.mjs +8 -6
- package/cli/business/vendors/index.mjs +22 -11
- package/cli/business/vendors/sorry.mjs +106 -0
- package/cli/container-transfer.mjs +2 -2
- package/cli/contract.mjs +7 -7
- package/cli/demo-deploy-error.mjs +14 -0
- package/cli/demo-native.mjs +6 -2
- package/cli/deploy-command.mjs +6 -4
- package/cli/deploy-config.mjs +1 -1
- package/cli/deploy-owner-input.mjs +3 -1
- package/cli/deploy-variables.mjs +58 -0
- package/cli/deploy.mjs +9 -7
- package/cli/door-clauses.mjs +157 -0
- package/cli/errors.mjs +1 -1
- package/cli/export.mjs +16 -4
- package/cli/hosted-output.mjs +17 -7
- package/cli/human-error.mjs +20 -0
- package/cli/inspect.mjs +4 -0
- package/cli/jsonc.mjs +8 -4
- package/cli/lifecycle.mjs +26 -6
- package/cli/main.mjs +2 -1
- package/cli/managed-data-schema.mjs +127 -0
- package/cli/managed-data-zip.mjs +116 -0
- package/cli/managed-data.mjs +110 -0
- package/cli/managed-server.mjs +25 -14
- package/cli/native-owner-entry.mjs +86 -0
- package/cli/output.mjs +3 -2
- package/cli/parse.mjs +2 -2
- package/cli/portability-empty.mjs +65 -0
- package/cli/portability-migrations.mjs +33 -9
- package/cli/portability-policy.mjs +5 -2
- package/cli/portability-worker.mjs +15 -6
- package/cli/portability.mjs +30 -8
- package/cli/production-auth.mjs +1 -1
- package/cli/production-mail.mjs +8 -2
- package/cli/ready-summary.mjs +9 -0
- package/cli/run-transfers.mjs +37 -0
- package/cli/runtime-front-door.mjs +4 -4
- package/cli/server-proxy.mjs +4 -3
- package/cli/server-registry.mjs +13 -8
- package/cli/site.mjs +6 -1
- package/cli/transfers.mjs +17 -4
- package/edition-authoring/archive-source.mjs +59 -0
- package/edition-authoring/base-copies.mjs +147 -25
- package/edition-authoring/cli.mjs +1 -1
- package/edition-authoring/edition-migrations.mjs +7 -3
- package/edition-authoring/module-closure.mjs +2 -16
- package/edition-authoring/workflow-copies.mjs +1 -1
- package/edition-authoring/workflow-scripts.mjs +12 -3
- package/foundation/runtime/contract-docs.mjs +218 -34
- package/foundation/runtime/contract-prose-patterns.mjs +14 -0
- package/foundation/runtime/contract-prose.mjs +88 -0
- package/foundation/runtime/run-data.d.mts +1 -0
- package/foundation/runtime/run-data.mjs +2 -0
- package/global/CHANGELOG.md +126 -0
- package/global/README.md +156 -50
- package/global/cli/command.mjs +8 -2
- package/global/cli/install.mjs +4 -5
- package/global/cli/path-link.mjs +3 -2
- package/global/cli/repo-command.mjs +3 -7
- package/global/door/command.mjs +3 -1
- package/global/launch/dev.mjs +12 -2
- package/global/launch/facts.mjs +2 -2
- package/global/launch/render.mjs +16 -9
- package/global/launch/state.mjs +4 -4
- package/global/main.mjs +13 -8
- package/install.sh +17 -6
- package/modules/agent-door/CLAUSES.md +215 -0
- package/modules/agent-door/conformance/audit.mjs +66 -0
- package/modules/agent-door/conformance/authority.d.mts +2 -0
- package/modules/agent-door/conformance/authority.mjs +17 -0
- package/modules/agent-door/conformance/crypto.mjs +36 -0
- package/modules/agent-door/conformance/discovery.mjs +61 -0
- package/modules/agent-door/conformance/index.mjs +41 -0
- package/modules/agent-door/conformance/invocation.mjs +32 -0
- package/modules/agent-door/conformance/journey.mjs +112 -0
- package/modules/agent-door/conformance/narrowing.d.mts +1 -0
- package/modules/agent-door/conformance/narrowing.mjs +21 -0
- package/modules/agent-door/conformance/negative.mjs +47 -0
- package/modules/agent-door/conformance/network-modules.mjs +86 -0
- package/modules/agent-door/conformance/network-runtime.mjs +46 -0
- package/modules/agent-door/conformance/policy-phases.mjs +75 -0
- package/modules/agent-door/conformance/protocol.mjs +145 -0
- package/modules/agent-door/conformance/replay.mjs +38 -0
- package/modules/agent-door/conformance/schema.mjs +71 -0
- package/modules/agent-door/conformance/security.mjs +33 -0
- package/modules/agent-door/conformance/sponsors.mjs +68 -0
- package/modules/agent-door/conformance/surface.mjs +89 -0
- package/modules/agent-door/conformance/target.mjs +18 -0
- package/modules/agent-door/conformance/tool-name.d.mts +1 -0
- package/modules/agent-door/conformance/tool-name.mjs +7 -0
- package/modules/agent-door/conformance/webhooks.mjs +30 -0
- package/modules/container/README.md +6 -1
- package/modules/container/adapters/d1-limits.mjs +286 -0
- package/modules/container/adapters/d1-sqlite.mjs +95 -0
- package/modules/container/adapters/d1-workerd-preload.mjs +19 -0
- package/modules/container/adapters/d1-workerd-setup.mjs +26 -0
- package/modules/container/adapters/d1-workerd.mjs +5 -0
- package/modules/container/adapters/sqlite-worker.mjs +5 -3
- package/modules/container/build.mjs +1 -0
- package/modules/container/image-variables.mjs +14 -3
- package/modules/container/transfer.mjs +14 -4
- package/modules/container/worker-engine.mjs +3 -3
- package/modules/demo-host/README.md +6 -0
- package/modules/demo-host/build.mjs +1 -0
- package/modules/demo-host/native/prepare.mjs +2 -1
- package/modules/demo-host/native/sql.mjs +7 -2
- package/modules/demo-host/ownership.mjs +39 -0
- package/modules/demo-host/plan.mjs +2 -1
- package/modules/demo-host/remove.mjs +6 -1
- package/modules/deploy-kit/README.md +22 -2
- package/modules/deploy-kit/d1-import-diagnostics.mjs +56 -0
- package/modules/deploy-kit/d1-import.mjs +17 -5
- package/modules/deploy-kit/diagnostics.mjs +29 -3
- package/modules/deploy-kit/owner-cli.mjs +20 -7
- package/modules/deploy-kit/provider-transport.mjs +19 -4
- package/modules/deploy-kit/provider.mjs +57 -22
- package/modules/deploy-kit/readiness-window.mjs +1 -1
- package/modules/deploy-kit/site.mjs +8 -3
- package/modules/deploy-kit/worker.mjs +4 -4
- package/modules/import-kit/schema/importer-verification-receipt.schema.json +171 -0
- package/modules/import-kit/schema/importers.schema.json +107 -0
- package/modules/import-kit/src/d1/README.md +23 -4
- package/modules/import-kit/src/d1/atomic-prefix.ts +2 -2
- package/modules/import-kit/src/d1/budget.ts +15 -7
- package/modules/import-kit/src/d1/column-layout.ts +5 -0
- package/modules/import-kit/src/d1/creation.ts +15 -8
- package/modules/import-kit/src/d1/database.ts +3 -3
- package/modules/import-kit/src/d1/erasure-pages.ts +2 -1
- package/modules/import-kit/src/d1/erasure.ts +2 -2
- package/modules/import-kit/src/d1/jobs.ts +10 -6
- package/modules/import-kit/src/d1/plans.ts +14 -8
- package/modules/import-kit/src/d1/retirement.ts +2 -2
- package/modules/import-kit/src/d1/sets.ts +1 -3
- package/modules/import-kit/src/d1/staged-creation.ts +3 -3
- package/modules/import-kit/src/door/ADOPTING.md +20 -0
- package/modules/import-kit/src/door/README.md +2 -1
- package/modules/import-kit/src/door/admission.ts +5 -3
- package/modules/import-kit/src/door/archive.ts +5 -2
- package/modules/import-kit/src/door/authority.ts +1 -0
- package/modules/import-kit/src/door/grants.ts +1 -1
- package/modules/import-kit/src/door/handler.ts +14 -8
- package/modules/import-kit/src/door/index.ts +1 -1
- package/modules/import-kit/src/door/jobs.ts +40 -0
- package/modules/import-kit/src/door/limits.ts +1 -1
- package/modules/import-kit/src/door/operator.ts +3 -3
- package/modules/import-kit/src/door/planning.ts +10 -6
- package/modules/import-kit/src/door/protocol.ts +2 -2
- package/modules/import-kit/src/door/raw.ts +25 -0
- package/modules/import-kit/src/door/refusals.ts +2 -1
- package/modules/import-kit/src/door/types.ts +26 -6
- package/modules/import-kit/src/receipt/encoding.ts +37 -0
- package/modules/import-kit/src/receipt/index.ts +69 -0
- package/modules/import-kit/src/receipt/policy.ts +24 -0
- package/modules/import-kit/src/receipt/types.ts +64 -0
- package/modules/import-kit/src/receipt/uri.ts +45 -0
- package/modules/import-kit/src/receipt/validation.ts +87 -0
- package/modules/import-kit/src/status/encoding.ts +13 -0
- package/modules/import-kit/src/status/evidence.ts +218 -0
- package/modules/import-kit/src/status/index.ts +56 -0
- package/modules/import-kit/src/status/schema.ts +208 -0
- package/modules/import-kit/src/status/types.ts +86 -0
- package/modules/operator-auth/accounts/delegated.mjs +48 -0
- package/modules/operator-auth/index.d.mts +7 -0
- package/modules/operator-auth/service.mjs +2 -0
- package/modules/portability/README.md +138 -9
- package/modules/portability/backfills.mjs +9 -8
- package/modules/portability/bridge.mjs +32 -5
- package/modules/portability/catalogue.mjs +12 -5
- package/modules/portability/copies.mjs +8 -1
- package/modules/portability/derived.mjs +33 -0
- package/modules/portability/files.mjs +4 -3
- package/modules/portability/http-errors.mjs +1 -1
- package/modules/portability/http.mjs +3 -3
- package/modules/portability/import.mjs +29 -13
- package/modules/portability/index.d.mts +4 -3
- package/modules/portability/index.mjs +1 -1
- package/modules/portability/ledgers.mjs +3 -2
- package/modules/portability/legacy-safety.mjs +11 -2
- package/modules/portability/migration-history.mjs +2 -2
- package/modules/portability/progress.mjs +1 -0
- package/modules/portability/restore-empty.mjs +12 -0
- package/modules/release/github-verify.mjs +11 -2
- package/onboarding/adapter.mjs +35 -6
- package/onboarding/app-fetch.mjs +6 -4
- package/onboarding/development-runtime.mjs +19 -0
- package/onboarding/local/operation.mjs +45 -27
- package/onboarding/loopback-fetch.mjs +16 -0
- package/onboarding/operator-installer.mjs +2 -4
- package/onboarding/process-diagnostics.mjs +6 -2
- package/onboarding/production-runtime.mjs +4 -2
- package/onboarding/readiness.mjs +1 -0
- package/package.json +4 -2
- package/scripts/publish-check/copy_identities.py +3 -1
- package/scripts/publish-check/github_refs.py +32 -13
- package/scripts/publish-check/public_hygiene.py +1 -1
- package/cli/business/opt-ins.mjs +0 -33
package/cli/export.mjs
CHANGED
|
@@ -2,11 +2,15 @@ import { cp, mkdir, readFile, readdir, rm, writeFile, lstat } from 'node:fs/prom
|
|
|
2
2
|
import { createHash } from 'node:crypto';
|
|
3
3
|
import { resolve, dirname, relative } from 'node:path';
|
|
4
4
|
import { testSource } from '../composition/git-source.mjs';
|
|
5
|
-
import { cliVersion } from './version.mjs';
|
|
5
|
+
import { cliVersion, stampedVersion } from './version.mjs';
|
|
6
|
+
import { execFileSync } from 'node:child_process';
|
|
6
7
|
|
|
7
8
|
// Copy whole maintained source/tooling directories. Python publication scans
|
|
8
9
|
// 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'];
|
|
10
|
+
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/receipt', 'modules/import-kit/src/status', 'modules/import-kit/schema', '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'];
|
|
11
|
+
sourceRoots.push('modules/agent-door/conformance', 'modules/agent-door/CLAUSES.md');
|
|
12
|
+
sourceRoots.push('edition-authoring/archive-source.mjs');
|
|
13
|
+
sourceRoots.push('foundation/runtime/contract-prose.mjs', 'foundation/runtime/contract-prose-patterns.mjs');
|
|
10
14
|
export async function inventory(base, roots = sourceRoots) {
|
|
11
15
|
const files = [];
|
|
12
16
|
async function walk(path) {
|
|
@@ -40,8 +44,16 @@ export async function exportCLI(base, destination) {
|
|
|
40
44
|
record[path] = { source: path, sha256: createHash('sha256').update(bytes).digest('hex') };
|
|
41
45
|
}
|
|
42
46
|
const onboardingFiles=Object.fromEntries(await Promise.all((await inventory(base,['onboarding'])).map(async path=>[path.slice('onboarding/'.length),{source:path,sha256:createHash('sha256').update(await readFile(resolve(base,path))).digest('hex')}])));
|
|
43
|
-
//
|
|
44
|
-
|
|
47
|
+
// The maintained reference is committed beside its exporter. Stamp its last
|
|
48
|
+
// exported source change, so committing generated copies does not invalidate
|
|
49
|
+
// their version again. Exports to other checkouts still name the exact HEAD.
|
|
50
|
+
let version;
|
|
51
|
+
if (resolve(destination) === resolve(base, 'default-seed')) {
|
|
52
|
+
const revision = execFileSync('git', ['-C', base, 'log', '-1', '--format=%H', 'HEAD', '--',
|
|
53
|
+
'global/package.json', ...files, ...Object.values(onboardingFiles).map(file => file.source)], { encoding: 'utf8' }).trim();
|
|
54
|
+
const source = JSON.parse(await readFile(resolve(base, 'global/package.json'), 'utf8'));
|
|
55
|
+
version = stampedVersion(base, source.version, revision);
|
|
56
|
+
} else version = cliVersion(base);
|
|
45
57
|
await writeFile(resolve(target, 'inventory.json'), JSON.stringify({ version: 1, ...(version ? { cliVersion: version } : {}), onboarding: '../base-onboarding', onboardingFiles, files: record }, null, 2) + '\n');
|
|
46
58
|
await writeFile(resolve(destination, 'scripts/rer.mjs'), "#!/usr/bin/env node\nimport { main } from './base-cli/cli/main.mjs';\nprocess.exitCode = await main();\n");
|
|
47
59
|
}
|
package/cli/hosted-output.mjs
CHANGED
|
@@ -9,11 +9,12 @@ import { migrate } from '../modules/container/migrations.mjs';
|
|
|
9
9
|
import { readMigrationRewrites, withRewrites } from '../modules/deploy-kit/migrations.mjs';
|
|
10
10
|
import { RerError } from './errors.mjs';
|
|
11
11
|
import { hostedMailCredentials } from './hosted-mail.mjs';
|
|
12
|
+
import { shared } from './shared.mjs';
|
|
12
13
|
|
|
13
14
|
// Serve the production build the way rer deploy hosts it (assets behind the
|
|
14
15
|
// Worker, deploy identity bound, a public https origin) on loopback, with no
|
|
15
16
|
// network or account. The ingress hands the app the hosted URL and Host.
|
|
16
|
-
const
|
|
17
|
+
export const HOSTED_ORIGIN = 'https://rer-hosted-check.invalid';
|
|
17
18
|
const ingress = `export default {fetch(request,env){const url=new URL(request.headers.get('x-rer-original-url')),headers=new Headers(request.headers);headers.delete('x-rer-original-url');headers.set('host',url.host);return env.APP.fetch(new Request(url,{method:request.method,headers,redirect:'manual',...(!['GET','HEAD'].includes(request.method)?{body:request.body}:{})}))}}`;
|
|
18
19
|
const read = async path => JSON.parse(await readFile(path, 'utf8'));
|
|
19
20
|
const optional = path => read(path).catch(error => { if (error.code === 'ENOENT') return {}; throw error; });
|
|
@@ -43,7 +44,8 @@ const links = html => [...html.matchAll(/(?:src|href)=["'](\/[^"'#?\s]+)/g)].map
|
|
|
43
44
|
|
|
44
45
|
/** The build served as rer deploy hosts it, with declared vars, origin and
|
|
45
46
|
* stand-in deploy/Managed mail secrets. `fetch(path, init)` uses the deployed origin. */
|
|
46
|
-
export async function serveHosted(root, { declaration: declarationPath = 'deploy.json' } = {}) {
|
|
47
|
+
export async function serveHosted(root, { declaration: declarationPath = 'deploy.json', bindings = {}, outboundService } = {}) {
|
|
48
|
+
const { loopbackFetch } = await shared('loopback-fetch.mjs');
|
|
47
49
|
const build = join(root, 'dist/server'), client = join(root, 'dist/client');
|
|
48
50
|
let config;
|
|
49
51
|
try { config = await read(join(build, 'wrangler.json')); }
|
|
@@ -61,9 +63,9 @@ export async function serveHosted(root, { declaration: declarationPath = 'deploy
|
|
|
61
63
|
if (!Object.hasOwn(secrets, binding)) secrets[binding] = standIn({}, manifest.fields?.find(field => field.name === binding));
|
|
62
64
|
}
|
|
63
65
|
const mf = new Miniflare(convertV4MiniflareOptions({ host: '127.0.0.1', port: 0, cf: false, workers: [{ name: 'ingress', modules: true, script: ingress, serviceBindings: { APP: 'app' } }, {
|
|
64
|
-
name: 'app', modules, modulesRoot: build, compatibilityDate: config.compatibility_date, compatibilityFlags: config.compatibility_flags,
|
|
65
|
-
bindings: { ...config.vars, ...declaration.vars, ...secrets, ...(declaration.originVar ? { [declaration.originVar]:
|
|
66
|
-
RER_DEPLOY_SOURCE: identity.sourceSha, RER_DEPLOY_FINGERPRINT: identity.fingerprint, RER_VERSION: { id: identity.versionId, tag: '', timestamp: new Date().toISOString() } },
|
|
66
|
+
name: 'app', ...(outboundService ? { outboundService } : {}), modules, modulesRoot: build, compatibilityDate: config.compatibility_date, compatibilityFlags: config.compatibility_flags,
|
|
67
|
+
bindings: { ...config.vars, ...declaration.vars, ...secrets, ...(declaration.originVar ? { [declaration.originVar]: HOSTED_ORIGIN } : {}),
|
|
68
|
+
RER_DEPLOY_SOURCE: identity.sourceSha, RER_DEPLOY_FINGERPRINT: identity.fingerprint, RER_VERSION: { id: identity.versionId, tag: '', timestamp: new Date().toISOString() }, ...bindings },
|
|
67
69
|
d1Databases: Object.fromEntries((config.d1_databases ?? []).map(item => [item.binding, item.database_id ?? item.database_name])),
|
|
68
70
|
r2Buckets: Object.fromEntries((config.r2_buckets ?? []).map(item => [item.binding, item.bucket_name])),
|
|
69
71
|
kvNamespaces: Object.fromEntries((config.kv_namespaces ?? []).map(item => [item.binding, item.id ?? item.binding])),
|
|
@@ -84,8 +86,16 @@ export async function serveHosted(root, { declaration: declarationPath = 'deploy
|
|
|
84
86
|
await migrate(await mf.getD1Database(item.binding, 'app'), withRewrites(plan, await readMigrationRewrites(root), { partial: true }));
|
|
85
87
|
}
|
|
86
88
|
} catch (error) { await mf.dispose(); throw error; }
|
|
87
|
-
return { client, identity, origin:
|
|
88
|
-
|
|
89
|
+
return { client, identity, origin: HOSTED_ORIGIN,
|
|
90
|
+
database: async binding => {
|
|
91
|
+
if (!(config.d1_databases ?? []).some(item => item.binding === binding)) throw new RerError('hosted_database_invalid', 'Verification can read only a declared disposable database binding.', 1);
|
|
92
|
+
const db = await mf.getD1Database(binding, 'app');
|
|
93
|
+
return { prepare(sql) {
|
|
94
|
+
if (typeof sql !== 'string' || !/^\s*SELECT\b/i.test(sql) || sql.includes(';')) throw new RerError('hosted_database_invalid', 'Verification state access is read-only SELECT.', 1);
|
|
95
|
+
return db.prepare(sql);
|
|
96
|
+
} };
|
|
97
|
+
}, close: () => mf.dispose(),
|
|
98
|
+
fetch: (path, { headers, ...init } = {}) => loopbackFetch(loopback, { ...init, redirect: 'manual', headers: { ...headers, 'x-rer-original-url': `${HOSTED_ORIGIN}/${path}` } }) };
|
|
89
99
|
}
|
|
90
100
|
|
|
91
101
|
export async function checkHostedOutput(root, options) {
|
|
@@ -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/inspect.mjs
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { deployVariableClause } from './deploy-variables.mjs';
|
|
1
2
|
import { browserDoctor } from './browser-runtime.mjs';
|
|
2
3
|
import { dependencyDoctor } from './dependency-doctor.mjs';
|
|
3
4
|
import { inventory } from './export.mjs';
|
|
@@ -25,6 +26,7 @@ import { checkoutPlaceholderMarks } from './placeholder-mark.mjs';
|
|
|
25
26
|
import { contractStatusClauses } from './contract-status.mjs';
|
|
26
27
|
import { hostedExportCommand } from './hosted-export-command.mjs';
|
|
27
28
|
import { cloudflareCredentials } from './deploy-needs.mjs';
|
|
29
|
+
import { agentSurfaceClauses } from './agent-surface.mjs';
|
|
28
30
|
|
|
29
31
|
// cliVersion: the rer version of the code answering (absent for a vendored copy
|
|
30
32
|
// too old to record one). defaults: what a server start applies when the person
|
|
@@ -81,6 +83,8 @@ export async function check(project, explicitProfile) {
|
|
|
81
83
|
// A warning, never a failing clause: live run sites redeploy while seeds keep Base's mark.
|
|
82
84
|
let placeholderMarks = [];
|
|
83
85
|
clauses.push(...await deliveryDeclarationClauses(project.root));
|
|
86
|
+
clauses.push(await deployVariableClause(project.root, project.adapter));
|
|
87
|
+
clauses.push(...await agentSurfaceClauses(project.root));
|
|
84
88
|
for (const command of project.commands) add('SC-10.inputs.' + command.name, command.available, `Restore the declared inputs for ${command.name}.`);
|
|
85
89
|
for (const loop of project.descriptor.loops ?? []) add('SC-10.loop.' + loop.id, !loop.required || loop.enabled, `Enable the required ${loop.id} loop or correct its declaration.`);
|
|
86
90
|
if (project.profile === 'first-party' || explicitProfile === 'first-party') {
|
package/cli/jsonc.mjs
CHANGED
|
@@ -7,8 +7,8 @@ function location(text, position) {
|
|
|
7
7
|
// version's error wording. An explicit stack also handles deeply nested input.
|
|
8
8
|
// Only comments and commas accepted by the grammar are masked; offsets stay in
|
|
9
9
|
// the original source and comment removal can never join separate tokens.
|
|
10
|
-
export function parseJsonc(text) {
|
|
11
|
-
const result = text.split(''), stack = [{ kind: 'root', state: 'value' }];
|
|
10
|
+
export function parseJsonc(text, onProperty) {
|
|
11
|
+
const result = text.split(''), stack = [{ kind: 'root', state: 'value', path: [] }];
|
|
12
12
|
let index = 0;
|
|
13
13
|
const fail = (message, position = index) => {
|
|
14
14
|
throw new SyntaxError(`${position === text.length ? 'Unexpected end of JSON input' : message} at ${location(text, position)}`);
|
|
@@ -80,7 +80,10 @@ export function parseJsonc(text) {
|
|
|
80
80
|
index++; stack.pop();
|
|
81
81
|
} else if (frame.state === 'keyOrEnd') {
|
|
82
82
|
if (char !== '"') fail('Expected a property name');
|
|
83
|
-
|
|
83
|
+
const start = index; string();
|
|
84
|
+
frame.key = onProperty ? JSON.parse(text.slice(start, index)) : undefined;
|
|
85
|
+
onProperty?.([...frame.path, frame.key], start);
|
|
86
|
+
frame.state = 'colon';
|
|
84
87
|
} else if (frame.state === 'colon') {
|
|
85
88
|
if (char !== ':') fail("Expected ':' after property name");
|
|
86
89
|
index++; frame.state = 'value';
|
|
@@ -91,10 +94,11 @@ export function parseJsonc(text) {
|
|
|
91
94
|
if (text[index] === closing) result[comma] = ' ';
|
|
92
95
|
frame.state = frame.kind === 'object' ? 'keyOrEnd' : 'valueOrEnd';
|
|
93
96
|
} else {
|
|
97
|
+
const path = !onProperty ? undefined : frame.kind === 'root' ? [] : [...frame.path, frame.kind === 'array' ? frame.next++ : frame.key];
|
|
94
98
|
frame.state = frame.kind === 'root' ? 'end' : 'comma';
|
|
95
99
|
if (char === '{' || char === '[') {
|
|
96
100
|
index++;
|
|
97
|
-
stack.push({ kind: char === '{' ? 'object' : 'array', state: char === '{' ? 'keyOrEnd' : 'valueOrEnd' });
|
|
101
|
+
stack.push({ kind: char === '{' ? 'object' : 'array', state: char === '{' ? 'keyOrEnd' : 'valueOrEnd', path, next: 0 });
|
|
98
102
|
} else if (char === '"') string();
|
|
99
103
|
else if (char === '-' || digit()) number();
|
|
100
104
|
else {
|
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;
|
|
@@ -109,7 +112,9 @@ async function runLifecycle(project, options, out, deployRedaction) {
|
|
|
109
112
|
const declaration = adapter.deployDeclaration ?? adapter.deploy?.declaration;
|
|
110
113
|
const hosted = await (await import('./hosted-output.mjs')).checkHostedOutput(project.root, { declaration });
|
|
111
114
|
await store.step('Checking that page sections hydrate as separate units');
|
|
112
|
-
|
|
115
|
+
const hydration = await (await import('./hydration/check.mjs')).checkHydration(project.root, { pages: adapter.publicPages, declaration, report: lines => lines.forEach(line => store.report(line)) });
|
|
116
|
+
const agentDoor = await (await import('./agent-door.mjs')).checkAgentDoor(project.root, { declaration, setupManifest: project.setupManifest, owner: adapter.hooks?.owner, report: line => store.report(line) });
|
|
117
|
+
return { hosted, hydration, agentDoor };
|
|
113
118
|
}
|
|
114
119
|
if (['export', 'import'].includes(options.verb)) return await transfer(project, options, signal, { store, operationToken: operation.token });
|
|
115
120
|
if (options.verb === 'deploy') {
|
|
@@ -126,7 +131,7 @@ async function runLifecycle(project, options, out, deployRedaction) {
|
|
|
126
131
|
const privateSettings = [...adapter.settings, '.rer/local/models.json', ...(project.privateInputs??[])];
|
|
127
132
|
const run = () => adapter.run(project.root, input ?? { [emailKey]: options.ownerEmail ?? selected?.vars[emailKey] }, store,
|
|
128
133
|
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 });
|
|
134
|
+
{ 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
135
|
// A production build never sees the checkout's local settings, whatever the seed's build runs.
|
|
131
136
|
if (options.verb === 'build') await withoutLocalSettings(project.root, { settings: privateSettings, operationToken: operation?.token }, run);
|
|
132
137
|
else await run();
|
|
@@ -149,7 +154,7 @@ async function runLifecycle(project, options, out, deployRedaction) {
|
|
|
149
154
|
const deployNeeds = await hostedNeeds(project);
|
|
150
155
|
if (options.json) out.event('setup-ready', { url, setupUrl: url+'/setup', ...(deployNeeds.length ? { neededForDeploy: deployNeeds } : {}) });
|
|
151
156
|
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.`);
|
|
157
|
+
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
158
|
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
159
|
}
|
|
155
160
|
} else if (options.verb !== 'build') throw refuse('The requested first-party capability is unavailable.');
|
|
@@ -189,6 +194,21 @@ async function installStepError(error, store, root) {
|
|
|
189
194
|
if (approvedFailure(error)?.code === 'machine_busy') return error;
|
|
190
195
|
const { checkoutPath, stepDiagnostic, stepFailureText } = await shared('step-failure.mjs');
|
|
191
196
|
const step = approvedFailure(error)?.step ?? stepDiagnostic({ name: store.state.step, output: store.state.logs, log: store.redact(checkoutPath(root, store.logPath)) });
|
|
197
|
+
// pnpm's stable machine codes select public wording; its raw diagnostic is
|
|
198
|
+
// retained in the structured step and private capture, never in this sentence.
|
|
199
|
+
const text = step.output.join('\n');
|
|
200
|
+
const refusal = [
|
|
201
|
+
['ERR_PNPM_OUTDATED_LOCKFILE', 'pnpm-lock.yaml does not match package.json'],
|
|
202
|
+
['ERR_PNPM_NO_LOCKFILE', 'pnpm-lock.yaml is missing'],
|
|
203
|
+
['ERR_PNPM_LOCKFILE_BREAKING_CHANGE', 'pnpm-lock.yaml was written by an incompatible pnpm version'],
|
|
204
|
+
].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);
|
|
205
|
+
if (refusal) {
|
|
206
|
+
const { toolCommand } = await shared('tool-command.mjs');
|
|
207
|
+
const { shellQuote } = await import('./node-runtime-cache.mjs');
|
|
208
|
+
const [program, args] = toolCommand('pnpm', ['install', '--no-frozen-lockfile']);
|
|
209
|
+
const next = [program, ...args].map(value => /^[A-Za-z0-9_.-]+$/.test(value) ? value : shellQuote(value)).join(' ');
|
|
210
|
+
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 });
|
|
211
|
+
}
|
|
192
212
|
// Only a RerError's message or a symbolic error code is safe to show; a native message can hold private data.
|
|
193
213
|
const cause = error instanceof RerError ? `\n${error.message}` : /^[A-Z][A-Z0-9_]{1,63}$/.test(error?.code ?? '') ? `\nCause: ${error.code}` : '';
|
|
194
214
|
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
|
@@ -8,6 +8,7 @@ import { check, describe, doctorSummary } from './inspect.mjs';
|
|
|
8
8
|
import { selectNodeRuntime } from './node-runtime.mjs';
|
|
9
9
|
|
|
10
10
|
export async function main(argv = process.argv.slice(2)) {
|
|
11
|
+
if (argv[0] === 'door' && argv[1] === 'clauses') return (await import('./door-clauses.mjs')).doorClausesCommand(argv.slice(2));
|
|
11
12
|
if (argv[0] === 'business') return (await import('./business/command.mjs')).businessCommand(argv.slice(1));
|
|
12
13
|
if (argv[0] === 'companion') return (await import('./companion/command.mjs')).companionCommand(argv.slice(1));
|
|
13
14
|
// Every runtime a seed's hooks start inherits this: Wrangler and the Cloudflare
|
|
@@ -84,7 +85,7 @@ export async function main(argv = process.argv.slice(2)) {
|
|
|
84
85
|
out.result(true, 0, result); return 0;
|
|
85
86
|
} catch (error) {
|
|
86
87
|
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;
|
|
88
|
+
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
89
|
}
|
|
89
90
|
}
|
|
90
91
|
|
|
@@ -0,0 +1,127 @@
|
|
|
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
|
+
// Older native restores left engine-owned staging tables behind. They are
|
|
28
|
+
// transient copies, excluded from portability identity, not unknown product DDL.
|
|
29
|
+
const objects = source.filter(object => ['table', 'trigger', 'index', 'view'].includes(object.type)
|
|
30
|
+
&& !(object.type === 'table' && object.name.toLowerCase().startsWith('rer_portability_stage_')));
|
|
31
|
+
const retired = new Map();
|
|
32
|
+
let previous = new Map();
|
|
33
|
+
try {
|
|
34
|
+
for (const migration of plan) for (const sql of migration.statements) {
|
|
35
|
+
scratch.exec(sql);
|
|
36
|
+
const current = new Map(scratch.prepare(query).all().map(object => [key(object), object]));
|
|
37
|
+
for (const object of objects) {
|
|
38
|
+
const id = key(object), before = previous.get(id), after = current.get(id);
|
|
39
|
+
if (before && identity(before) === identity(object) && (!after || identity(after) !== identity(object))) retired.set(id, migration.name);
|
|
40
|
+
}
|
|
41
|
+
previous = current;
|
|
42
|
+
}
|
|
43
|
+
// A Managed dump omits derived virtual tables and their shadow storage.
|
|
44
|
+
// Install only exact policy names that the destination's own migrations
|
|
45
|
+
// establish as virtual tables. Source SQL never gains a virtual-table door.
|
|
46
|
+
const virtual = scratch.prepare('PRAGMA table_list').all().filter(table => table.type === 'virtual');
|
|
47
|
+
for (const table of virtual) if ((policy.derivedTables ?? []).includes(table.name) && !(policy.includeTables ?? []).includes(table.name)) {
|
|
48
|
+
insist(!objects.some(object => object.type === 'table' && object.name === table.name), 'managed_data_schema_incompatible', 409);
|
|
49
|
+
const target = previous.get(key({ type: 'table', name: table.name }));
|
|
50
|
+
try { await db.prepare(target.sql).run(); }
|
|
51
|
+
catch { throw new PortabilityError('managed_data_derived_refused', 409); }
|
|
52
|
+
}
|
|
53
|
+
// Native restore also installs runtime-owned bookkeeping. Derive it with
|
|
54
|
+
// the same destination helpers, never a source name or a policy wildcard.
|
|
55
|
+
const native = nativeDatabase(scratch);
|
|
56
|
+
await ensurePortabilitySchema(native, await portabilityPlan(splitStatements));
|
|
57
|
+
await ensureImportLeases(native);
|
|
58
|
+
previous = new Map((await all(native, query)).map(object => [key(object), object]));
|
|
59
|
+
const inventory = [], groups = new Map();
|
|
60
|
+
const account = (object, disposition, migration) => {
|
|
61
|
+
const group = JSON.stringify([object.type, disposition, migration]);
|
|
62
|
+
if (!groups.has(group)) groups.set(group, { type: object.type, disposition, ...(migration ? { migration } : {}), names: [] });
|
|
63
|
+
groups.get(group).names.push(object.name);
|
|
64
|
+
};
|
|
65
|
+
for (const type of ['table', 'trigger', 'index', 'view']) {
|
|
66
|
+
const entries = objects.filter(object => object.type === type);
|
|
67
|
+
let matched = 0;
|
|
68
|
+
for (const object of entries) {
|
|
69
|
+
const id = key(object), target = previous.get(id);
|
|
70
|
+
if (type === 'table') {
|
|
71
|
+
// Row exclusion never establishes schema compatibility. The validated
|
|
72
|
+
// runner ledger is translated separately, not restored as a product table.
|
|
73
|
+
if (object.name === ledger.table) { account(object, 'migration-ledger'); continue; }
|
|
74
|
+
if (target) {
|
|
75
|
+
if (excludedTable(object.name, policy)) account(object, 'excluded-by-policy');
|
|
76
|
+
else matched++;
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
const migration = retired.get(id);
|
|
80
|
+
insist(excludedTable(object.name, policy) && migration, 'managed_data_schema_table_missing', 409);
|
|
81
|
+
account(object, 'retired', migration);
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
if (target && identity(target) === identity(object)) { matched++; continue; }
|
|
85
|
+
const migration = retired.get(id);
|
|
86
|
+
insist(migration, target ? 'managed_data_schema_object_unaccounted_replacement' : 'managed_data_schema_object_unknown', 409);
|
|
87
|
+
account(object, target ? 'replaced' : 'retired', migration);
|
|
88
|
+
}
|
|
89
|
+
inventory.push({ type, source: entries.length, matched, accounted: entries.length - matched });
|
|
90
|
+
}
|
|
91
|
+
return { inventory, accounted: [...groups.values()].map(({ names, ...group }) => ({ ...group, count: names.length, namesSha256: namesHash(names) })) };
|
|
92
|
+
} finally { scratch.close(); }
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// Use the native import checker against a disposable destination, including its
|
|
96
|
+
// migration placement and table-definition checks. The real destination has
|
|
97
|
+
// not been opened yet. Source schema accounting above must succeed first.
|
|
98
|
+
export async function managedCompatibility(plan, inputPolicy, manifest, declaration, buckets) {
|
|
99
|
+
const scratch = new DatabaseSync(':memory:');
|
|
100
|
+
try {
|
|
101
|
+
for (const migration of plan) for (const sql of migration.statements) scratch.exec(sql);
|
|
102
|
+
const db = nativeDatabase(scratch);
|
|
103
|
+
await ensurePortabilitySchema(db, await portabilityPlan(splitStatements));
|
|
104
|
+
scratch.exec('CREATE TABLE d1_migrations(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT UNIQUE, applied_at TEXT DEFAULT CURRENT_TIMESTAMP)');
|
|
105
|
+
for (const migration of plan) scratch.prepare('INSERT INTO d1_migrations(name) VALUES(?)').run(migration.name);
|
|
106
|
+
const service = await createPortability({ db, buckets, policy: inputPolicy, migrations: async () => migrationReplay(plan, inputPolicy, declaration) });
|
|
107
|
+
await service.imports.create(manifest);
|
|
108
|
+
} catch (error) {
|
|
109
|
+
if (!(error instanceof PortabilityError)) throw error;
|
|
110
|
+
throw new PortabilityError('managed_data_schema_incompatible', 409);
|
|
111
|
+
} finally { scratch.close(); }
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function nativeDatabase(scratch) {
|
|
115
|
+
return {
|
|
116
|
+
prepare(sql) {
|
|
117
|
+
let values = [];
|
|
118
|
+
return {
|
|
119
|
+
bind(...args) { values = args; return this; },
|
|
120
|
+
async first() { return scratch.prepare(sql).get(...values) ?? null; },
|
|
121
|
+
async all() { return { results: scratch.prepare(sql).all(...values) }; },
|
|
122
|
+
async run() { return scratch.prepare(sql).run(...values); },
|
|
123
|
+
};
|
|
124
|
+
},
|
|
125
|
+
async batch(statements) { return Promise.all(statements.map(statement => statement.run())); },
|
|
126
|
+
};
|
|
127
|
+
}
|
|
@@ -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
|
+
}
|