@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
|
@@ -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
|
+
}
|
package/cli/managed-server.mjs
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import { launchSummary } from './run-transfers.mjs';
|
|
2
|
+
import { readySummary } from './ready-summary.mjs';
|
|
3
|
+
import { nativeOwnerEntry } from './native-owner-entry.mjs';
|
|
1
4
|
import { prepareLiveModels } from './ai-local.mjs';
|
|
2
5
|
import {fileURLToPath} from 'node:url';
|
|
3
6
|
import {join} from 'node:path';
|
|
@@ -19,23 +22,29 @@ import { readDevKey,requireDevKey,refuseManagedInstallation,resolvePersona,openU
|
|
|
19
22
|
import { announceDoorRoute,claimSavedName,moveCheckout,routeRunning,verifyDoorFor } from './door-mode.mjs';
|
|
20
23
|
|
|
21
24
|
// Dev opens through the local identity exchange. Preview serves production output.
|
|
22
|
-
export function ready(out,url,dev,json=false,siteUrl,demo){
|
|
23
|
-
if(json){out.event('ready',{url,...(dev?{signedInAs:dev.signedInAs??null,openUrl:dev.openUrl}:{}),...(siteUrl?{siteUrl}:{}),...(demo?{demo}:{})});return;}
|
|
25
|
+
export async function ready(out,url,dev,json=false,siteUrl,demo,root,ownerOpenUrl){
|
|
26
|
+
if(json){out.event('ready',{url,...(dev?{signedInAs:dev.signedInAs??null,openUrl:dev.openUrl}:{}),...(ownerOpenUrl?{ownerOpenUrl}:{}),...(siteUrl?{siteUrl}:{}),...(demo?{demo}:{})});return;}
|
|
27
|
+
if (root) {
|
|
28
|
+
for (const line of readySummary({ url, ...dev, siteUrl })) out.human(line);
|
|
29
|
+
for (const line of await launchSummary(root)) out.human(line);
|
|
30
|
+
out.human('Details for tools and troubleshooting:');
|
|
31
|
+
} else out.human(`Your app is ready, open it: ${dev?.openUrl ?? url}`);
|
|
24
32
|
out.human(`RER_READY ${url}`);
|
|
25
33
|
if(dev){
|
|
26
34
|
if(dev.signedInAs)out.human(`RER_SIGNED_IN ${dev.signedInAs}`);
|
|
27
35
|
else if(dev.ownerLeft)out.human('No active owner remains in this app. Use rer dev --as PERSONA with an active operator, or rer reset --yes and rer setup for a new owner.');
|
|
28
36
|
out.human(`RER_OPEN ${dev.openUrl}`);
|
|
29
|
-
out.human("Open that link on this computer. Its fragment holds this checkout's dev key, which the page keeps only as a cookie; never share the link.");
|
|
37
|
+
if (!root && !dev.native) out.human("Open that link on this computer. Its fragment holds this checkout's dev key, which the page keeps only as a cookie; never share the link.");
|
|
30
38
|
}
|
|
31
|
-
if(
|
|
39
|
+
if(ownerOpenUrl){out.human(`RER_OWNER_OPEN ${ownerOpenUrl}`);out.human('Native owner entry: open on this computer once within 15 minutes. The link is not saved.');}
|
|
40
|
+
if(siteUrl){if(!root)out.human(`Site: ${siteUrl}`);out.human(`RER_SITE ${siteUrl}`);}
|
|
32
41
|
if(demo){out.human(`RER_DEMO_ENDS_ON_STOP ${demo.state}`);out.human(`This demo runs on its own data in that folder, which is deleted when it stops: rer demo --stop, Ctrl-C, or at its end (${demo.ends.slice(11,16)}). Public reads and checks run for real. This checkout's own data is untouched.`);}
|
|
33
42
|
}
|
|
34
43
|
export async function managedServer(project,options,out,{clock={}}={}){
|
|
35
44
|
const registry=await serverRegistry(project.root),controller=new AbortController();
|
|
36
45
|
// A demo registers as its own mode: `rer dev --stop` never stops it by accident.
|
|
37
46
|
const mode=options.demo?'demo':options.verb;
|
|
38
|
-
let store,operation,proxy,activity,app,termination,reason,cleanupPromise,finished,selected,cleaned,site,demo,demoEnd;
|
|
47
|
+
let store,operation,proxy,activity,app,termination,reason,cleanupPromise,finished,selected,cleaned,site,demo,demoEnd,ownerEntry;
|
|
39
48
|
const scheduling=new AbortController();
|
|
40
49
|
let activityWrites=Promise.resolve(),pendingActivity,activityWriting=false,infrastructureFailure;
|
|
41
50
|
function recordActivity(fields){
|
|
@@ -44,7 +53,7 @@ export async function managedServer(project,options,out,{clock={}}={}){
|
|
|
44
53
|
}
|
|
45
54
|
const stopped=new Promise(resolve=>{finished=resolve;});
|
|
46
55
|
const cleanupDone=new Promise(resolve=>{cleaned=resolve;});
|
|
47
|
-
const cleanup=()=>cleanupPromise??=(async()=>{scheduling.abort();clearTimeout(demoEnd);activity?.close();await site?.close();await store?.closeCommands();if(demo)await rm(demo.state,{recursive:true,force:true});await proxy?.close();await operation?.release();})();
|
|
56
|
+
const cleanup=()=>cleanupPromise??=(async()=>{scheduling.abort();clearTimeout(demoEnd);activity?.close();await site?.close();await ownerEntry?.close();await store?.closeCommands();if(demo)await rm(demo.state,{recursive:true,force:true});await proxy?.close();await operation?.release();})();
|
|
48
57
|
const stop=async why=>{reason??=why;controller.abort();finished();await cleanupDone;return {};};
|
|
49
58
|
const handlers=new Map(['SIGINT','SIGTERM'].map(name=>[name,()=>{termination=name;void stop(name);} ]));
|
|
50
59
|
for(const [name,handler]of handlers)process.on(name,handler);
|
|
@@ -77,10 +86,11 @@ export async function managedServer(project,options,out,{clock={}}={}){
|
|
|
77
86
|
// A key is sent (or printed) for a door origin only once the door proves it is ours.
|
|
78
87
|
if(devIdentity)await verifyDoorFor(running.url);
|
|
79
88
|
const dev=devIdentity&&options.verb==='dev'&&await devReady(running.url,devKey,options,controller.signal);
|
|
80
|
-
await emitReady(out,options,project.root,running.url,dev,running.siteUrl,running.demo);return {reused:true};
|
|
89
|
+
await emitReady(out,options,project.root,running.url,dev,running.siteUrl,running.demo,(await registry.ownerEntry(running)).ownerOpenUrl);return {reused:true};
|
|
81
90
|
}
|
|
82
91
|
await registry.serve(async action=>{
|
|
83
92
|
if(action==='status')return {...activity?.snapshot(),url:selected?.url,probe:proxy?.probe,siteUrl:site?.url,demo};
|
|
93
|
+
if(action==='owner-entry')return ownerEntry?{ownerOpenUrl:ownerEntry.issue()}:{};
|
|
84
94
|
if(action==='stop')return stop('explicit stop');
|
|
85
95
|
throw Error('Unknown management operation');
|
|
86
96
|
});
|
|
@@ -97,7 +107,7 @@ export async function managedServer(project,options,out,{clock={}}={}){
|
|
|
97
107
|
if(!selected.door)await availablePort(selected.host,selected.port,{root:project.root,saved:!selected.explicitPort&&selected.savedOrigin!==undefined,verb:options.verb});
|
|
98
108
|
if(!selected.vars[keys.email])throw missing('Private owner settings are missing. Run rer setup first.');
|
|
99
109
|
if(devIdentity&&!devKey)devKey=await requireDevKey(project.root);
|
|
100
|
-
if(devIdentity&&options.verb
|
|
110
|
+
if(devIdentity&&['dev','preview'].includes(options.verb))refuseManagedInstallation(selected.vars,process.env);
|
|
101
111
|
if(options.demo)demo=await demoPreflight(project,selected.vars,options.idleTimeout);
|
|
102
112
|
const prerequisites=async()=>{
|
|
103
113
|
if(!demo&&project.adapter.localPrerequisites&&!await project.adapter.localPrerequisites({root:project.root,vars:selected.vars,email:selected.vars[keys.email],demo:false}))throw missing('Local migrations, owner or local authentication/mail settings are missing. Run rer setup first.');
|
|
@@ -115,10 +125,11 @@ export async function managedServer(project,options,out,{clock={}}={}){
|
|
|
115
125
|
activity=activityClock({...clock,seconds:options.idleTimeout,onIdle:()=>void stop('idle timeout'),onChange:recordActivity});
|
|
116
126
|
if(demo)demoEnd=setTimeout(()=>void stop('demo ended'),Math.min(Date.parse(demo.ends)-Date.now(),2**31-1));
|
|
117
127
|
const crons=project.adapter.scheduled!==false?await declaredCrons(project.root,project.adapter).catch(error=>{throw refuse(`The declared scheduled triggers cannot run locally: ${error.message}`);}):[];
|
|
118
|
-
|
|
128
|
+
if(devIdentity&&(options.verb==='preview'||demo))ownerEntry=nativeOwnerEntry({origin:selected.url,state:demo?.state??join(project.root,'.wrangler','state'),store,demo:!!demo,devKey});
|
|
129
|
+
proxy=await runtimeFrontDoor({host:selected.host,port:selected.port,activity,door:selected.door,devKey:devKey??undefined,origin:selected.url,ownerEntry:(req,res)=>ownerEntry?.handle(req,res)});const internal=proxy.runtimePort;
|
|
119
130
|
if(selected.door)await announceDoorRoute(selected,proxy,registry);
|
|
120
131
|
const modelsLive = options.verb==='dev' ? await prepareLiveModels(project.root,options,store,controller.signal) : undefined;
|
|
121
|
-
const adapterOptions={modelsLive,demo,selectedOrigin:selected.url,operationToken:operation.token,privateSettings:project.adapter.settings,runtimePort:internal,devKey:devKey??undefined,devLogin:false,scheduled:crons.length>0,beforeStart:options.verb==='dev'?prerequisites:undefined};
|
|
132
|
+
const adapterOptions={modelsLive,demo,selectedOrigin:selected.url,operationToken:operation.token,privateSettings:project.adapter.settings,runtimePort:internal,setRuntimePort:port=>proxy.setRuntimePort(port),devKey:devKey??undefined,devLogin:false,scheduled:crons.length>0,beforeStart:options.verb==='dev'?prerequisites:undefined};
|
|
122
133
|
const input={[keys.email]:selected.vars[keys.email]};
|
|
123
134
|
if(options.verb==='preview')await productionBuild(project,()=>store.run('Building fixed production output',process.execPath,[fileURLToPath(new URL('../bin/rer.mjs',import.meta.url)),'build','--cwd',project.root,'--json'],{cwd:project.root,phase:'build',signal:controller.signal,env:{...process.env,RER_LOCAL_OPERATION:operation.token},afterCommand:()=>recoverInterrupted(project.root,operation)}),text=>store.report(text));
|
|
124
135
|
await serverResources(project.root);
|
|
@@ -144,10 +155,10 @@ export async function managedServer(project,options,out,{clock={}}={}){
|
|
|
144
155
|
if(options.json)out.event('warning',{code:error.code,message});else out.progress(`Warning: ${message}\n`);
|
|
145
156
|
}
|
|
146
157
|
}
|
|
147
|
-
activity.ready();await emitReady(out,options,project.root,selected.url,dev,site?.url,demo);
|
|
158
|
+
activity.ready();await emitReady(out,options,project.root,selected.url,dev,site?.url,demo,ownerEntry?.issue());
|
|
148
159
|
if(crons.length){
|
|
149
160
|
out.progress(`Running ${crons.length===1?'the scheduled trigger':'the scheduled triggers'} locally: ${crons.join(' | ')}\n`);
|
|
150
|
-
void runScheduled({crons,url:`http://127.0.0.1:${
|
|
161
|
+
void runScheduled({crons,url:`http://127.0.0.1:${proxy.runtimePort}`,path:options.verb==='preview'?'/cdn-cgi/local/scheduled':undefined,signal:scheduling.signal,report:run=>reportScheduled(out,options,run)});
|
|
151
162
|
}
|
|
152
163
|
const result=await stopped;
|
|
153
164
|
if(termination)throw new RerError('cancelled',`Stopped by ${termination} after owned children were stopped.`,termination==='SIGINT'?130:143);
|
|
@@ -164,12 +175,12 @@ function reportScheduled(out,options,run){
|
|
|
164
175
|
// dev also keeps its RER_OPEN link in owner-only .rer/local/open-url (0600),
|
|
165
176
|
// which reset removes with the rest of .rer/local, for a person or agent that
|
|
166
177
|
// did not keep the terminal output.
|
|
167
|
-
async function emitReady(out,options,root,url,dev,siteUrl,demo){
|
|
178
|
+
async function emitReady(out,options,root,url,dev,siteUrl,demo,ownerOpenUrl){
|
|
168
179
|
if(options.verb==='dev'&&dev?.openUrl){
|
|
169
180
|
const {privateDirectory,writePrivateFile}=await shared('private-files.mjs');
|
|
170
181
|
await writePrivateFile(join(await privateDirectory(root,['.rer','local']),'open-url'),dev.openUrl+'\n');
|
|
171
182
|
}
|
|
172
|
-
ready(out,url,dev||undefined,options.json,siteUrl,demo);
|
|
183
|
+
await ready(out,url,dev||(ownerOpenUrl?{openUrl:ownerOpenUrl,native:true}:undefined),options.json,siteUrl,demo,options.verb === 'dev' && !options.demo ? root : undefined,ownerOpenUrl);
|
|
173
184
|
}
|
|
174
185
|
async function devReady(url,key,options,signal){
|
|
175
186
|
const as=options.as??'owner',persona=await resolvePersona(url,key,as,signal);
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { randomBytes, randomUUID, timingSafeEqual } from 'node:crypto';
|
|
2
|
+
import { readdir } from 'node:fs/promises';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
import { devGate, DEV_KEY_PATH } from '../modules/operator-auth/dev-gate.mjs';
|
|
5
|
+
import { OWNER_SIGNIN_SECONDS, OWNER_SIGNIN_PATH, ownerSigninDigest } from '../modules/operator-auth/owner-signin-grant.mjs';
|
|
6
|
+
import { loopback } from './parse.mjs';
|
|
7
|
+
import { openUrl } from './dev-identity.mjs';
|
|
8
|
+
import { shared } from './shared.mjs';
|
|
9
|
+
|
|
10
|
+
// The dev fragment exchange, with a run-owned, expiring, single-use key. Only
|
|
11
|
+
// an independent native grant digest enters the local ledger, on redemption.
|
|
12
|
+
// The public key cannot redeem that grant outside this proxy's lifetime. Neither
|
|
13
|
+
// credential is saved; the native grant supplies normal owner fencing.
|
|
14
|
+
export function nativeOwnerEntry({ origin, state, store, demo, devKey, clock = Date.now }) {
|
|
15
|
+
if (!loopback(new URL(origin).hostname)) return null;
|
|
16
|
+
let entry, grant;
|
|
17
|
+
const same = (a, b) => { const left = Buffer.from(a), right = Buffer.from(b); return left.length === right.length && timingSafeEqual(left, right); };
|
|
18
|
+
function issue() {
|
|
19
|
+
const key = randomBytes(32).toString('base64url');
|
|
20
|
+
store.addSecrets([key]);
|
|
21
|
+
entry = { key, expires: clock() + OWNER_SIGNIN_SECONDS * 1000, used: false, gate: devGate({ key, origin, local: true }) };
|
|
22
|
+
return openUrl(origin, key, { path: DEV_KEY_PATH, next: '/' });
|
|
23
|
+
}
|
|
24
|
+
async function database() {
|
|
25
|
+
const { DatabaseSync } = await import('node:sqlite');
|
|
26
|
+
const directory = join(state, 'v3/d1/miniflare-D1DatabaseObject'), found = [];
|
|
27
|
+
for (const file of await readdir(directory)) {
|
|
28
|
+
if (!file.endsWith('.sqlite')) continue;
|
|
29
|
+
const path = join(directory, file), db = new DatabaseSync(path, { readOnly: true });
|
|
30
|
+
try { if (db.prepare("SELECT 1 FROM sqlite_master WHERE name='oa_owner_signins' AND type='table'").get()) found.push(path); }
|
|
31
|
+
finally { db.close(); }
|
|
32
|
+
}
|
|
33
|
+
if (found.length !== 1) throw Error('Local native owner entry requires exactly one owner sign-in ledger.');
|
|
34
|
+
return new DatabaseSync(found[0]);
|
|
35
|
+
}
|
|
36
|
+
async function clear() {
|
|
37
|
+
if (!grant) return;
|
|
38
|
+
const db = await database();
|
|
39
|
+
try { db.prepare('DELETE FROM oa_owner_signins WHERE id=?').run(grant); grant = undefined; }
|
|
40
|
+
finally { db.close(); }
|
|
41
|
+
}
|
|
42
|
+
return {
|
|
43
|
+
issue,
|
|
44
|
+
async close() { entry = undefined; await clear(); },
|
|
45
|
+
async handle(req, res) {
|
|
46
|
+
if (req.url !== DEV_KEY_PATH) return false;
|
|
47
|
+
const reply = async response => {
|
|
48
|
+
res.writeHead(response.status, Object.fromEntries(response.headers));
|
|
49
|
+
res.end(Buffer.from(await response.arrayBuffer()));
|
|
50
|
+
return true;
|
|
51
|
+
};
|
|
52
|
+
const denied = () => new Response('This owner link is unavailable. Run the command again for a fresh link.', { status: 403, headers: { 'cache-control': 'no-store', 'content-type': 'text/plain; charset=utf-8', 'referrer-policy': 'no-referrer' } });
|
|
53
|
+
if (req.headers.host !== new URL(origin).host || req.headers['sec-fetch-site'] === 'cross-site' || req.headers.origin && req.headers.origin !== origin) return reply(denied());
|
|
54
|
+
if (req.method === 'GET' || req.method === 'HEAD') {
|
|
55
|
+
if (demo) return false;
|
|
56
|
+
return reply(await entry.gate.exchange(new Request(origin + DEV_KEY_PATH, { method: req.method })));
|
|
57
|
+
}
|
|
58
|
+
if (req.method !== 'POST') return demo ? false : reply(denied());
|
|
59
|
+
let body = '';
|
|
60
|
+
for await (const bytes of req) { body += bytes; if (body.length > 1024) return reply(denied()); }
|
|
61
|
+
let key; try { key = JSON.parse(body).key; } catch {}
|
|
62
|
+
// Preserve the demo persona through the same dev gate as its Worker.
|
|
63
|
+
if (demo && entry && typeof key === 'string' && !same(key, entry.key)) return reply(await devGate({ key: devKey, origin, local: true }).exchange(new Request(origin + DEV_KEY_PATH, { method: 'POST', headers: { 'content-type': 'application/json' }, body })));
|
|
64
|
+
if (!entry || typeof key !== 'string' || !same(key, entry.key) || entry.used || clock() >= entry.expires || req.headers.origin !== origin) return reply(denied());
|
|
65
|
+
entry.used = true;
|
|
66
|
+
const exchange = await entry.gate.exchange(new Request(origin + DEV_KEY_PATH, { method: 'POST', headers: { 'content-type': 'application/json' }, body }));
|
|
67
|
+
if (!exchange.ok) return reply(denied());
|
|
68
|
+
await clear();
|
|
69
|
+
const id = randomUUID(), nativeToken = randomBytes(32).toString('base64url');
|
|
70
|
+
store.addSecrets([nativeToken]);
|
|
71
|
+
const db = await database(), now = clock();
|
|
72
|
+
try {
|
|
73
|
+
db.prepare(`INSERT INTO oa_owner_signins(id,token_hash,operator_id,operator_version,created_at,expires_at)
|
|
74
|
+
SELECT ?,?,o.id,o.version,?,? FROM oa_operators o LEFT JOIN oa_bootstrap b ON b.claim_id=o.id
|
|
75
|
+
WHERE o.role='owner' AND o.status='active' ORDER BY b.claim_id IS NULL,o.created_at,o.id LIMIT 1`).run(id, await ownerSigninDigest(nativeToken), now, entry.expires);
|
|
76
|
+
grant = id;
|
|
77
|
+
} finally { db.close(); }
|
|
78
|
+
const { appFetch } = await shared('app-fetch.mjs');
|
|
79
|
+
const response = await appFetch(origin + OWNER_SIGNIN_PATH, { method: 'POST', redirect: 'manual', headers: { Origin: origin, 'content-type': 'application/json' }, body: JSON.stringify({ token: nativeToken }), signal: AbortSignal.timeout(10000) });
|
|
80
|
+
if (response.status !== 303) { await response.body?.cancel(); return reply(denied()); }
|
|
81
|
+
const cookies = response.headers.getSetCookie(); await response.body?.cancel();
|
|
82
|
+
res.writeHead(204, { 'cache-control': 'no-store', 'referrer-policy': 'no-referrer', 'set-cookie': cookies }); res.end();
|
|
83
|
+
return true;
|
|
84
|
+
},
|
|
85
|
+
};
|
|
86
|
+
}
|
package/cli/output.mjs
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { humanError } from './human-error.mjs';
|
|
1
2
|
import { handleBrokenPipes } from './stdio.mjs';
|
|
2
3
|
import { exportSummary } from './export-summary.mjs';
|
|
3
4
|
import { importSafetySummary } from './import-summary.mjs';
|
|
@@ -24,8 +25,8 @@ export function output(options) {
|
|
|
24
25
|
else if (!['describe', 'check', 'dev', 'preview', 'setup'].includes(options.verb) || !ok) this.human(`${options.verb}: ${ok ? 'complete' : 'failed'} (exit ${exitCode})`);
|
|
25
26
|
},
|
|
26
27
|
error(error) {
|
|
27
|
-
if (structuredResult) this.event('error', { code: error.code, message: error.message, exitCode: error.exitCode, ...(error.availableDeployments ? { environment: error.environment, availableDeployments: error.availableDeployments } : {}), ...(error.step ? { step: error.step } : {}) });
|
|
28
|
-
else write(2, error
|
|
28
|
+
if (structuredResult) this.event('error', { code: error.code, message: error.message, exitCode: error.exitCode, ...(!options.json && error.humanMessage ? { humanMessage: error.humanMessage } : {}), ...(error.availableDeployments ? { environment: error.environment, availableDeployments: error.availableDeployments } : {}), ...(error.siteDeploy ? {siteDeploy:error.siteDeploy} : {}), ...(error.step ? { step: error.step } : {}), ...(error.d1Import ? {d1Import:error.d1Import} : {}) });
|
|
29
|
+
else write(2, humanError(error) + '\n');
|
|
29
30
|
},
|
|
30
31
|
};
|
|
31
32
|
}
|
package/cli/parse.mjs
CHANGED
|
@@ -3,7 +3,7 @@ import { authoringVerbs, commonFlags, serverDefaults, signinVerb, verbs } from '
|
|
|
3
3
|
import { invalid, refuse } from './errors.mjs';
|
|
4
4
|
import { isLoopbackHost } from '../modules/operator-auth/loopback.mjs';
|
|
5
5
|
import { normalizeEmail } from '../modules/operator-auth/policy.mjs';
|
|
6
|
-
const booleans = new Set(['--help', '--json', '--non-interactive', '--yes', '--dry-run', '--stop', '--detach', '--foreground', '--no-queue', '--login', '--legacy-v1', '--acknowledge-unverified-metadata', '--door', '--direct', '--site', '--remove', '--demo', '--offline']);
|
|
6
|
+
const booleans = new Set(['--help', '--json', '--non-interactive', '--yes', '--dry-run', '--stop', '--detach', '--foreground', '--no-queue', '--login', '--legacy-v1', '--acknowledge-unverified-metadata', '--acknowledge-unverified-schema-objects', '--door', '--direct', '--site', '--remove', '--demo', '--offline']);
|
|
7
7
|
export const mailbox = value => typeof value === 'string' && value.length <= 254 && /^[a-zA-Z0-9.!+_%\-]+@[a-zA-Z0-9\-]+(?:\.[a-zA-Z0-9\-]+)+$/.test(value);
|
|
8
8
|
export const validOwnerName = value => typeof value === 'string' && Boolean(value.trim()) && value.length <= 200 && !/[\p{Cc}\p{Cf}]/u.test(value);
|
|
9
9
|
// A loopback host: the shared predicate (including `*.localhost` door names),
|
|
@@ -76,7 +76,7 @@ export function parse(argv, cwd = process.cwd(), { independent = false, deferTai
|
|
|
76
76
|
options.idleTimeout=Number(value);
|
|
77
77
|
}
|
|
78
78
|
if (options.port !== undefined) {
|
|
79
|
-
if (!/^\d+$/.test(options.port) || Number(options.port) < 1 || Number(options.port) > 65535) throw invalid('Port must be a decimal integer from 1 to 65535.');
|
|
79
|
+
if (!/^\d+$/.test(options.port) || Number(options.port) < 1 || Number(options.port) > 65535) throw Object.assign(invalid('Port must be a decimal integer from 1 to 65535.'), { humanMessage: `Port ${JSON.stringify(options.port)} is invalid. Use a port from 1 to 65535, for example: rer ${verb} --port 4800` });
|
|
80
80
|
options.port = Number(options.port);
|
|
81
81
|
}
|
|
82
82
|
for(const key of ['httpPort','httpsPort'])if(options[key]!==undefined){if(!/^[0-9]+$/.test(options[key])||Number(options[key])<1||Number(options[key])>65535)throw invalid('Ingress port must be an integer from 1 to 65535.');options[key]=Number(options[key]);}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { DatabaseSync } from 'node:sqlite';
|
|
2
|
+
import { createHash, createHmac } from 'node:crypto';
|
|
3
|
+
import { copyFile, lstat, mkdtemp, readFile, rm } from 'node:fs/promises';
|
|
4
|
+
import { join } from 'node:path';
|
|
5
|
+
import { tmpdir } from 'node:os';
|
|
6
|
+
import { parseJsonc } from './jsonc.mjs';
|
|
7
|
+
import { loadPolicy } from './portability-policy.mjs';
|
|
8
|
+
import { appliedMigrations, platformMigrationDeclaration } from './portability-migrations.mjs';
|
|
9
|
+
import { migrationReplay } from '../modules/portability/migration-history.mjs';
|
|
10
|
+
import { splitStatements } from '../modules/portability/sql-text.mjs';
|
|
11
|
+
import { requireEmptyTables } from '../modules/portability/restore-empty.mjs';
|
|
12
|
+
import { insist } from '../modules/portability/storage.mjs';
|
|
13
|
+
import { shared } from './shared.mjs';
|
|
14
|
+
|
|
15
|
+
const status = path => lstat(path).catch(error => { if (error.code === 'ENOENT') return null; throw error; });
|
|
16
|
+
// Miniflare's named D1 identity: the declared binding determines its file.
|
|
17
|
+
function databaseFile(id) {
|
|
18
|
+
const key = createHash('sha256').update('miniflare-D1DatabaseObject').digest();
|
|
19
|
+
const name = createHmac('sha256', key).update(id).digest().subarray(0, 16);
|
|
20
|
+
return Buffer.concat([name, createHmac('sha256', key).update(name).digest().subarray(0, 16)]).toString('hex') + '.sqlite';
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// Called under the runner's stopped-writer custody and operation lock. Even a
|
|
24
|
+
// read-only SQLite connection can checkpoint or alter shared memory: open only
|
|
25
|
+
// a private copy, including existing journal sidecars, before any native proxy.
|
|
26
|
+
export async function emptyNativePreflight({ root, binding, policy, policyModule, platformMigrations }) {
|
|
27
|
+
const { selectLocalState } = await shared('local/state.mjs');
|
|
28
|
+
const state = await selectLocalState(root);
|
|
29
|
+
const config = parseJsonc(await readFile(join(root, 'wrangler.jsonc'), 'utf8'));
|
|
30
|
+
const bindings = config.d1_databases?.filter(entry => entry.binding === binding) ?? [];
|
|
31
|
+
insist(bindings.length === 1, 'migration_ledger:binding', 409);
|
|
32
|
+
const id = bindings[0].preview_database_id ?? bindings[0].database_id;
|
|
33
|
+
insist(typeof id === 'string' && id.length > 0, 'migration_ledger:binding', 409);
|
|
34
|
+
let parent = state;
|
|
35
|
+
for (const part of ['v3', 'd1', 'miniflare-D1DatabaseObject']) {
|
|
36
|
+
parent = join(parent, part); const info = await status(parent);
|
|
37
|
+
if (!info) return;
|
|
38
|
+
insist(info.isDirectory() && !info.isSymbolicLink(), 'restore_state_unsafe', 409);
|
|
39
|
+
}
|
|
40
|
+
const path = join(parent, databaseFile(id)), paths = [path, path + '-wal', path + '-journal'];
|
|
41
|
+
const before = await Promise.all(paths.map(status));
|
|
42
|
+
if (!before[0]) return;
|
|
43
|
+
insist(before.every(info => !info || info.isFile() && !info.isSymbolicLink() && info.nlink === 1 && info.uid === process.getuid()), 'restore_state_unsafe', 409);
|
|
44
|
+
const directory = await mkdtemp(join(tmpdir(), 'rer-empty-preflight-')); let snapshot;
|
|
45
|
+
try {
|
|
46
|
+
for (let i = 0; i < paths.length; i++) if (before[i]) await copyFile(paths[i], join(directory, 'database.sqlite' + paths[i].slice(path.length)));
|
|
47
|
+
const after = await Promise.all(paths.map(status));
|
|
48
|
+
insist(before.every((info, i) => info ? after[i] && ['ino', 'size', 'mtimeMs', 'ctimeMs'].every(key => info[key] === after[i][key]) : !after[i]), 'restore_state_changed', 409);
|
|
49
|
+
snapshot = new DatabaseSync(join(directory, 'database.sqlite'), { readOnly: true });
|
|
50
|
+
// Wrangler can create a D1 file before any product migrations run. Only
|
|
51
|
+
// literal SQLite/Cloudflare engine internals may exist in that empty file;
|
|
52
|
+
// policy exclusions and migration ledgers do not make other schema empty.
|
|
53
|
+
// Every existing product/foreign object still goes through the full policy
|
|
54
|
+
// and occupied-table checks before migrations can change the destination.
|
|
55
|
+
if (!snapshot.prepare("SELECT 1 FROM sqlite_master WHERE substr(lower(name),1,7) <> 'sqlite_' AND substr(lower(name),1,4) <> '_cf_' LIMIT 1").get()) return;
|
|
56
|
+
const db = { prepare(sql) { const query = snapshot.prepare(sql); let values = []; return {
|
|
57
|
+
bind(...args) { values = args; return this; },
|
|
58
|
+
async all() { return { results: query.all(...values) }; },
|
|
59
|
+
async first() { return query.get(...values) ?? null; },
|
|
60
|
+
}; } };
|
|
61
|
+
const rules = await loadPolicy(root, policy, policyModule), declaration = platformMigrationDeclaration(platformMigrations);
|
|
62
|
+
const migrations = async () => migrationReplay(await appliedMigrations(root, db, binding, declaration, splitStatements), rules, declaration);
|
|
63
|
+
await requireEmptyTables({ db, policy: rules, migrations });
|
|
64
|
+
} finally { try { snapshot?.close(); } finally { await rm(directory, { recursive: true, force: true }); } }
|
|
65
|
+
}
|
|
@@ -18,7 +18,7 @@ export function platformMigrationDeclaration(input={}) {
|
|
|
18
18
|
|
|
19
19
|
// Reproduce the runner's stored SQL text, then recover SQLite's statement
|
|
20
20
|
// boundaries (migration-sql.mjs).
|
|
21
|
-
async function
|
|
21
|
+
export async function migrationInventory(root,binding,{prefix},split) {
|
|
22
22
|
const config=parseJsonc(await readFile(join(root,'wrangler.jsonc'),'utf8'));
|
|
23
23
|
const bindings=config.d1_databases?.filter(entry=>entry.binding===binding) ?? [];
|
|
24
24
|
insist(bindings.length===1,'migration_ledger:binding',409);
|
|
@@ -31,7 +31,7 @@ async function inventory(root,binding,{prefix},split) {
|
|
|
31
31
|
|
|
32
32
|
// Preserve the runner's applied order, resolving file identities by hash.
|
|
33
33
|
export async function appliedMigrations(root,db,binding,declaration,split) {
|
|
34
|
-
const ledger=await destinationLedger(db) ?? [],files=await
|
|
34
|
+
const ledger=await destinationLedger(db) ?? [],files=await migrationInventory(root,binding,declaration,split);
|
|
35
35
|
return matchMigrationLedger(ledger,files,declaration).map(name=>files.find(file=>file.name===name));
|
|
36
36
|
}
|
|
37
37
|
|
|
@@ -54,7 +54,7 @@ function constantRefusal(error) {
|
|
|
54
54
|
export async function mapMigrationLedger({root,binding='DB',platformMigrations,entries}) {
|
|
55
55
|
const declaration=platformMigrationDeclaration(platformMigrations);
|
|
56
56
|
insist(Array.isArray(entries) && entries.every(entry=>entry && typeof entry.name==='string' && typeof entry.sha256==='string' && /^[a-f0-9]{64}$/.test(entry.sha256)) && new Set(entries.map(entry=>entry.name)).size===entries.length,'migration_ledger:incompatible',409);
|
|
57
|
-
const files=await
|
|
57
|
+
const files=await migrationInventory(root,binding,declaration);
|
|
58
58
|
try {
|
|
59
59
|
const mapped=entries.map(({name,sha256})=>({name:matchMigrationLedger([{name,sha256}],files,declaration)[0]??name,sha256}));
|
|
60
60
|
insist(new Set(mapped.map(entry=>entry.name)).size===mapped.length,'migration_ledger:ambiguous',409);
|
|
@@ -62,25 +62,49 @@ export async function mapMigrationLedger({root,binding='DB',platformMigrations,e
|
|
|
62
62
|
} catch(error) { throw constantRefusal(error); }
|
|
63
63
|
}
|
|
64
64
|
|
|
65
|
+
// Older runners leave Wrangler's bookkeeping beside their authoritative hashed
|
|
66
|
+
// history. A tracked D1 ledger is another history and must agree with it.
|
|
67
|
+
async function legacyPairPreflight(db) {
|
|
68
|
+
const columns=await all(db,'PRAGMA table_info(_rer_deploy_migrations)');
|
|
69
|
+
insist(['name','sha256','state'].every(name=>columns.some(column=>column.name===name)),'migration_ledger:incompatible',409);
|
|
70
|
+
const history=await all(db,'SELECT name,sha256,state FROM _rer_deploy_migrations ORDER BY name');
|
|
71
|
+
insist(history.every(row=>row.state==='applied'),'migration_ledger:uncertain',409);
|
|
72
|
+
insist(history.every(row=>typeof row.name==='string' && typeof row.sha256==='string' && /^[a-f0-9]{64}$/.test(row.sha256)) && new Set(history.map(row=>row.name)).size===history.length,'migration_ledger:incompatible',409);
|
|
73
|
+
const nativeColumns=(await all(db,'PRAGMA table_info(d1_migrations)')).map(column=>column.name);
|
|
74
|
+
const tracked=nativeColumns.includes('state') || nativeColumns.includes('sha256');
|
|
75
|
+
insist(['id','name','applied_at'].every(name=>nativeColumns.includes(name)) && (!tracked || nativeColumns.includes('state') && nativeColumns.includes('sha256')),'migration_ledger:incompatible',409);
|
|
76
|
+
const native=await all(db,`SELECT name${tracked?',sha256,state':''} FROM d1_migrations ORDER BY id`);
|
|
77
|
+
insist(native.every(row=>typeof row.name==='string') && new Set(native.map(row=>row.name)).size===native.length,'migration_ledger:incompatible',409);
|
|
78
|
+
if(tracked) {
|
|
79
|
+
insist(native.every(row=>row.state==='applied'),'migration_ledger:uncertain',409);
|
|
80
|
+
insist(native.every(row=>typeof row.sha256==='string' && /^[a-f0-9]{64}$/.test(row.sha256)),'migration_ledger:incompatible',409);
|
|
81
|
+
insist(native.length===history.length && native.every((row,index)=>row.name===history[index].name && row.sha256===history[index].sha256),'migration_ledger:multiple',409);
|
|
82
|
+
} else insist(native.every(row=>history.some(prior=>prior.name===row.name)),'migration_ledger:multiple',409);
|
|
83
|
+
}
|
|
84
|
+
|
|
65
85
|
// Adapter entry point, before *any* native Wrangler apply. The caller supplies
|
|
66
86
|
// the real target binding and keeps its writers stopped through apply/transfer.
|
|
67
87
|
// This only reads: it never repairs history or creates a second ledger.
|
|
68
88
|
export async function nativeMigrationPreflight({root,db,binding='DB',platformMigrations}) {
|
|
69
89
|
const declaration=platformMigrationDeclaration(platformMigrations);
|
|
70
90
|
const tables=await all(db,`SELECT type,name FROM sqlite_master WHERE lower(name) IN (${ledgerNames.map(name=>`'${name}'`).join(',')})`);
|
|
71
|
-
|
|
91
|
+
const legacyPair=tables.length===2 && ['_rer_deploy_migrations','d1_migrations'].every(name=>tables.some(row=>row.name===name));
|
|
92
|
+
insist(tables.length<=1 || legacyPair,'migration_ledger:multiple',409);
|
|
72
93
|
insist(tables.every(row=>row.type==='table' && ledgerNames.includes(row.name)),'migration_ledger:incompatible',409);
|
|
73
|
-
const table=tables[0]?.name;
|
|
94
|
+
const table=legacyPair?'_rer_deploy_migrations':tables[0]?.name;
|
|
74
95
|
if(!table)return {nativeApply:true};
|
|
75
96
|
// This runner's history cannot authorize Wrangler to apply the same files.
|
|
76
|
-
insist(table!=='_rer_deploy_migrations','migration_ledger:runner',409);
|
|
77
|
-
const files=await
|
|
97
|
+
insist(table!=='_rer_deploy_migrations' || legacyPair,'migration_ledger:runner',409);
|
|
98
|
+
const files=await migrationInventory(root,binding,declaration);
|
|
99
|
+
let nativeApply=false;
|
|
78
100
|
try {
|
|
101
|
+
if(legacyPair)await legacyPairPreflight(db);
|
|
79
102
|
const ledger=await destinationLedger(db),applied=matchMigrationLedger(ledger,files,declaration);
|
|
80
103
|
// Wrangler skips only literal filenames, not transfer aliases or hashes.
|
|
81
104
|
if(table==='d1_migrations')insist(ledger.every(row=>files.some(file=>file.name===row.name)),'migration_ledger:incompatible',409);
|
|
105
|
+
nativeApply=table==='d1_migrations' && files.some(file=>!applied.includes(file.name));
|
|
82
106
|
// The checkout file is named: it is the seed's own, never a ledger value.
|
|
83
|
-
if(table
|
|
107
|
+
if(table!=='d1_migrations')for(const file of files)if(!applied.includes(file.name))throw Object.assign(new PortabilityError('migration_ledger:unapplied',409),{migration:file.name});
|
|
84
108
|
} catch(error) { throw constantRefusal(error); }
|
|
85
|
-
return {nativeApply
|
|
109
|
+
return {nativeApply};
|
|
86
110
|
}
|
|
@@ -44,11 +44,14 @@ export function serializablePolicy(input) {
|
|
|
44
44
|
return result;
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
-
export async function loadPolicy(root, policy, policyModule) {
|
|
47
|
+
export async function loadPolicy(root, policy, policyModule, bindings) {
|
|
48
48
|
if (policyModule === undefined) return serializablePolicy(policy);
|
|
49
49
|
if (policy !== undefined) throw new PortabilityError('ambiguous_policy', 409);
|
|
50
50
|
try {
|
|
51
|
-
const
|
|
51
|
+
const module = await import(await policyModuleURL(root, policyModule));
|
|
52
|
+
validatePolicy(module.default);
|
|
53
|
+
if (module.createPolicy !== undefined && typeof module.createPolicy !== 'function') throw new Error();
|
|
54
|
+
const input = bindings && module.createPolicy ? await module.createPolicy(bindings) : module.default;
|
|
52
55
|
validatePolicy(input);
|
|
53
56
|
return input;
|
|
54
57
|
} catch { throw new PortabilityError('invalid_policy_module', 409); }
|
|
@@ -1,8 +1,10 @@
|
|
|
1
|
+
import { installD1Preflight } from '../modules/container/adapters/d1-workerd-setup.mjs';
|
|
1
2
|
import { portabilityPlan, ensurePortabilitySchema } from '../modules/container/portability-schema.mjs';
|
|
2
3
|
import { exportArchive, importArchive, fileRecords } from '../modules/portability/files.mjs';
|
|
3
4
|
import { readLegacyArchive } from '../modules/portability/format.mjs';
|
|
4
5
|
import { recoverLegacyArchive } from '../modules/portability/legacy-files.mjs';
|
|
5
|
-
import {
|
|
6
|
+
import { requireEmptyTables } from '../modules/portability/restore-empty.mjs';
|
|
7
|
+
import { legacyTargetState, requireLegacyEmpty, requireProductEmpty } from '../modules/portability/legacy-safety.mjs';
|
|
6
8
|
import { migrationReplay } from '../modules/portability/migration-history.mjs';
|
|
7
9
|
import { join } from 'node:path';
|
|
8
10
|
import { mkdir, readFile, rm, stat, writeFile } from 'node:fs/promises';
|
|
@@ -25,7 +27,7 @@ async function dataConfiguration(root) {
|
|
|
25
27
|
return path;
|
|
26
28
|
}
|
|
27
29
|
|
|
28
|
-
async function run(direction, { root, path, journal, binding, buckets, policy, policyModule, transient, legacyRecovery, platformMigrations }) {
|
|
30
|
+
async function run(direction, { root, path, journal, binding, buckets, policy, policyModule, transient, legacyRecovery, acknowledgeUnverifiedSchemaObjects, platformMigrations }) {
|
|
29
31
|
// Lines for the person go to the CLI over IPC, which prints them as the command's progress.
|
|
30
32
|
const progress = transferProgress(line => process.send({ progress: line }));
|
|
31
33
|
if (!legacyRecovery && ['export', 'import'].includes(direction)) progress({ phase: 'start', direction, bytes: direction === 'export' ? await stateBytes(join(root, '.wrangler/state')) : (await stat(path)).size });
|
|
@@ -35,10 +37,13 @@ async function run(direction, { root, path, journal, binding, buckets, policy, p
|
|
|
35
37
|
for await (const record of fileRecords(path,legacyRecovery?readLegacyArchive:readArchive)) {}
|
|
36
38
|
if(journal)for await(const record of fileRecords(journal)) {}
|
|
37
39
|
}
|
|
38
|
-
|
|
40
|
+
const inputPolicy = policy;
|
|
41
|
+
policy = await loadPolicy(root, inputPolicy, policyModule);
|
|
39
42
|
const declaration=platformMigrationDeclaration(platformMigrations);
|
|
40
43
|
if (direction === 'policy-preflight') return { checkedPolicy: true };
|
|
41
|
-
const
|
|
44
|
+
const require = createRequire(join(root, 'package.json'));
|
|
45
|
+
const { getPlatformProxy, unstable_splitSqlQuery } = require('wrangler');
|
|
46
|
+
installD1Preflight(createRequire(require.resolve('wrangler/package.json'))('miniflare').D1_PLUGIN);
|
|
42
47
|
|
|
43
48
|
// Unlike Wrangler's --persist-to base, the API takes the versioned directory.
|
|
44
49
|
const configPath = await dataConfiguration(root);
|
|
@@ -47,19 +52,23 @@ async function run(direction, { root, path, journal, binding, buckets, policy, p
|
|
|
47
52
|
proxy = await getPlatformProxy({ configPath, persist: { path: join(root, '.wrangler/state/v3') }, remoteBindings: false });
|
|
48
53
|
if(direction==='migration-preflight')return await nativeMigrationPreflight({root,db:proxy.env[binding],binding,platformMigrations});
|
|
49
54
|
const migrations=async()=>migrationReplay(await appliedMigrations(root,proxy.env[binding],binding,declaration,unstable_splitSqlQuery),policy,declaration);
|
|
50
|
-
const
|
|
55
|
+
const boundBuckets = Object.fromEntries(Object.entries(buckets).map(([name, binding]) => [name, proxy.env[binding]]));
|
|
56
|
+
policy = await loadPolicy(root, inputPolicy, policyModule, { db: proxy.env[binding], buckets: boundBuckets });
|
|
57
|
+
const options={ db: proxy.env[binding], buckets: boundBuckets, policy, migrations };
|
|
58
|
+
if(direction==='schema-recovery-preflight') { await requireProductEmpty(options);return {checkedEmpty:true}; }
|
|
51
59
|
if(direction==='legacy-preflight') {
|
|
52
60
|
await requireLegacyEmpty(options,await legacyTargetState(options.db,options.migrations));
|
|
53
61
|
return {checkedEmpty:true};
|
|
54
62
|
}
|
|
55
63
|
await ensurePortabilitySchema(proxy.env[binding], await portabilityPlan(unstable_splitSqlQuery));
|
|
56
64
|
if(legacyRecovery)return await recoverLegacyArchive(options,path,legacyRecovery.output,transient,legacyRecovery.acknowledged);
|
|
65
|
+
if (direction === 'import') await requireEmptyTables(options);
|
|
57
66
|
const service = await createPortability(options);
|
|
58
67
|
// This checkout's operation lock means no other import can be running. Abort
|
|
59
68
|
// the recorded abandoned job, dropping only its exact staging copies and
|
|
60
69
|
// sidecars before the replacement import creates its own scratch tables.
|
|
61
70
|
if (direction === 'import' && await service.imports.abortAbandoned()) process.send({ progress: 'Cleaned up an unfinished earlier import: a restore that stopped part-way left its staged data here. Its staging is removed and this import starts from the beginning.' });
|
|
62
|
-
return await (direction === 'export' ? exportArchive(service,path,transient,progress) : importArchive(service,path,transient,journal,progress));
|
|
71
|
+
return await (direction === 'export' ? exportArchive(service,path,transient,progress) : importArchive(service,path,transient,journal,progress,{acknowledgeUnverifiedSchemaObjects}));
|
|
63
72
|
} finally { await proxy?.dispose(); await rm(configPath, { force: true }); }
|
|
64
73
|
}
|
|
65
74
|
|