@runeditrun/rer 0.2.84 → 0.2.85
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +21 -24
- package/cli/README.md +17 -1
- package/cli/bounded.mjs +1 -1
- package/cli/business/carry.mjs +149 -0
- package/cli/business/command.mjs +6 -1
- package/cli/business/connect.mjs +1 -1
- package/cli/business/local-commands.mjs +2 -0
- package/cli/business/opt-ins.mjs +8 -4
- package/cli/business/questions.mjs +4 -1
- package/cli/business/state.mjs +1 -1
- package/cli/business/supervisor.mjs +7 -1
- package/cli/container-transfer.mjs +2 -2
- package/cli/contract.mjs +6 -6
- package/cli/demo-native.mjs +2 -1
- package/cli/deploy-command.mjs +6 -4
- package/cli/deploy.mjs +9 -7
- package/cli/errors.mjs +1 -1
- package/cli/export.mjs +1 -1
- package/cli/human-error.mjs +20 -0
- package/cli/lifecycle.mjs +23 -5
- package/cli/main.mjs +1 -1
- package/cli/managed-data-schema.mjs +114 -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 +59 -0
- package/cli/portability-migrations.mjs +7 -5
- package/cli/portability-policy.mjs +5 -2
- package/cli/portability-worker.mjs +15 -6
- package/cli/portability.mjs +30 -8
- 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/base-copies.mjs +55 -7
- package/foundation/runtime/contract-docs.mjs +3 -2
- package/foundation/runtime/run-data.d.mts +1 -0
- package/foundation/runtime/run-data.mjs +2 -0
- package/global/CHANGELOG.md +33 -0
- package/global/README.md +21 -24
- 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/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 +12 -7
- package/install.sh +17 -6
- 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 +8 -3
- package/modules/container/transfer.mjs +14 -4
- package/modules/container/worker-engine.mjs +3 -3
- 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/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/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/limits.ts +1 -1
- package/modules/import-kit/src/door/refusals.ts +1 -1
- package/modules/portability/README.md +112 -4
- package/modules/portability/backfills.mjs +9 -8
- package/modules/portability/bridge.mjs +12 -5
- package/modules/portability/catalogue.mjs +12 -5
- package/modules/portability/files.mjs +4 -3
- package/modules/portability/http.mjs +3 -3
- package/modules/portability/import.mjs +18 -9
- 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 +1 -1
- package/scripts/publish-check/public_hygiene.py +1 -1
|
@@ -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,59 @@
|
|
|
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
|
+
const db = { prepare(sql) { const query = snapshot.prepare(sql); let values = []; return {
|
|
51
|
+
bind(...args) { values = args; return this; },
|
|
52
|
+
async all() { return { results: query.all(...values) }; },
|
|
53
|
+
async first() { return query.get(...values) ?? null; },
|
|
54
|
+
}; } };
|
|
55
|
+
const rules = await loadPolicy(root, policy, policyModule), declaration = platformMigrationDeclaration(platformMigrations);
|
|
56
|
+
const migrations = async () => migrationReplay(await appliedMigrations(root, db, binding, declaration, splitStatements), rules, declaration);
|
|
57
|
+
await requireEmptyTables({ db, policy: rules, migrations });
|
|
58
|
+
} finally { try { snapshot?.close(); } finally { await rm(directory, { recursive: true, force: true }); } }
|
|
59
|
+
}
|
|
@@ -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);
|
|
@@ -74,13 +74,15 @@ export async function nativeMigrationPreflight({root,db,binding='DB',platformMig
|
|
|
74
74
|
if(!table)return {nativeApply:true};
|
|
75
75
|
// This runner's history cannot authorize Wrangler to apply the same files.
|
|
76
76
|
insist(table!=='_rer_deploy_migrations','migration_ledger:runner',409);
|
|
77
|
-
const files=await
|
|
77
|
+
const files=await migrationInventory(root,binding,declaration);
|
|
78
|
+
let nativeApply=false;
|
|
78
79
|
try {
|
|
79
80
|
const ledger=await destinationLedger(db),applied=matchMigrationLedger(ledger,files,declaration);
|
|
80
81
|
// Wrangler skips only literal filenames, not transfer aliases or hashes.
|
|
81
82
|
if(table==='d1_migrations')insist(ledger.every(row=>files.some(file=>file.name===row.name)),'migration_ledger:incompatible',409);
|
|
83
|
+
nativeApply=table==='d1_migrations' && files.some(file=>!applied.includes(file.name));
|
|
82
84
|
// The checkout file is named: it is the seed's own, never a ledger value.
|
|
83
85
|
if(table==='rer_migrations')for(const file of files)if(!applied.includes(file.name))throw Object.assign(new PortabilityError('migration_ledger:unapplied',409),{migration:file.name});
|
|
84
86
|
} catch(error) { throw constantRefusal(error); }
|
|
85
|
-
return {nativeApply
|
|
87
|
+
return {nativeApply};
|
|
86
88
|
}
|
|
@@ -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
|
|
package/cli/portability.mjs
CHANGED
|
@@ -7,23 +7,30 @@ import { fileRecords } from '../modules/portability/files.mjs';
|
|
|
7
7
|
import { RerError, refuse } from './errors.mjs';
|
|
8
8
|
import { policyModuleURL, serializablePolicy } from './portability-policy.mjs';
|
|
9
9
|
import { shared } from './shared.mjs';
|
|
10
|
+
import { emptyNativePreflight } from './portability-empty.mjs';
|
|
11
|
+
import { withManagedDataImport } from './managed-data.mjs';
|
|
10
12
|
import { ledgerRefusal } from './ledger-refusal.mjs';
|
|
11
13
|
|
|
14
|
+
export { withManagedDataImport };
|
|
15
|
+
|
|
12
16
|
const reExport = 'Re-export from the original installation with rer export --output exports/recovered.rer (add --target remote for a hosted installation), then retry rer import --input exports/recovered.rer --yes. Keep the original archive until recovery succeeds.';
|
|
13
17
|
|
|
14
18
|
// Seeds declare their real bindings and data policy. The shared module owns the
|
|
15
19
|
// archive format and atomic restore; the shared runner owns its native children.
|
|
16
|
-
export function localPortability({ database, binding = 'DB', buckets = {}, policy, policyModule, transient = [], platformMigrations }) {
|
|
17
|
-
async function run({ root, path, journal, store, signal, operationToken, platform, legacyRecovery }, direction) {
|
|
20
|
+
export function localPortability({ database, binding = 'DB', buckets = {}, policy, policyModule, transient = [], platformMigrations, managedData = false }) {
|
|
21
|
+
async function run({ root, path, journal, store, signal, operationToken, platform, legacyRecovery, acknowledgeUnverifiedSchemaObjects }, direction) {
|
|
18
22
|
if (direction === 'export') {
|
|
19
23
|
if (await lstat(path).catch(error => { if (error.code === 'ENOENT') return null; throw error; })) throw refuse('Export requires a new archive path.');
|
|
20
24
|
}
|
|
21
25
|
if(legacyRecovery && (!legacyRecovery.acknowledged || await lstat(legacyRecovery.output).catch(error=>{if(error.code==='ENOENT')return null;throw error;})))throw refuse('Legacy recovery requires acknowledgement and a new output archive path.');
|
|
26
|
+
let unverifiedSchemaRecovery=false;
|
|
22
27
|
if (direction === 'import') {
|
|
23
28
|
// Refuse damaged or unverifiable archives before migrations open the
|
|
24
29
|
// destination database. Worker and finalization verify again before publish.
|
|
25
30
|
try {
|
|
26
|
-
for await (const record of fileRecords(path,legacyRecovery?readLegacyArchive:readArchive)) {
|
|
31
|
+
for await (const record of fileRecords(path,legacyRecovery?readLegacyArchive:readArchive)) {
|
|
32
|
+
if(record.kind==='manifest')unverifiedSchemaRecovery=!legacyRecovery && acknowledgeUnverifiedSchemaObjects===true && record.schemaObjects===undefined;
|
|
33
|
+
}
|
|
27
34
|
if(journal)for await(const record of fileRecords(journal)) {}
|
|
28
35
|
}
|
|
29
36
|
catch (error) {
|
|
@@ -48,14 +55,23 @@ export function localPortability({ database, binding = 'DB', buckets = {}, polic
|
|
|
48
55
|
if (!(error instanceof PortabilityError)) throw error;
|
|
49
56
|
throw refuse(`Local portability policy refused (${error.code}). Executable policies require a seed-owned policyModule with a valid default policy export; policyModule requires native local transport.`);
|
|
50
57
|
}
|
|
51
|
-
if(platform==='container') return (await import('./container-transfer.mjs')).containerTransfer({root,path,journal,store,signal,direction,binding,buckets,policy:declaration.policy,transient});
|
|
52
|
-
const input={ root, path, journal, binding, buckets, ...declaration, transient, legacyRecovery, platformMigrations };
|
|
58
|
+
if(platform==='container') return (await import('./container-transfer.mjs')).containerTransfer({root,path,journal,store,signal,direction,binding,buckets,policy:declaration.policy,transient,acknowledgeUnverifiedSchemaObjects});
|
|
59
|
+
const input={ root, path, journal, binding, buckets, ...declaration, transient, legacyRecovery, acknowledgeUnverifiedSchemaObjects, platformMigrations };
|
|
53
60
|
// Check the executable policy before migrations; the transfer worker imports
|
|
54
61
|
// it again in its own process so module closures are retained during restore.
|
|
55
62
|
if (policyModule !== undefined) await nativeWorker(store,'policy-preflight',input,signal);
|
|
56
63
|
// Do not apply pending product migrations to a populated recovery target.
|
|
57
64
|
// Check real existing bindings first; finalize repeats this under the lock.
|
|
58
65
|
if(legacyRecovery)await nativeWorker(store,'legacy-preflight',input,signal);
|
|
66
|
+
if(direction==='import' && !legacyRecovery) {
|
|
67
|
+
try { await emptyNativePreflight(input); }
|
|
68
|
+
catch(error) {
|
|
69
|
+
if(error instanceof PortabilityError && error.code.startsWith('target_not_empty:'))throw new RerError('restore_refused',occupiedRefusal(input,'target_not_empty',error.code.slice(error.code.indexOf(':')+1)),4);
|
|
70
|
+
if(error instanceof PortabilityError)throw refuse(`Local restore refused (${error.code}).`);
|
|
71
|
+
throw error;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
if(unverifiedSchemaRecovery)await nativeWorker(store,'schema-recovery-preflight',input,signal);
|
|
59
75
|
const {nativeApply}=await nativeWorker(store,'migration-preflight',input,signal);
|
|
60
76
|
// Local migrations need no provider account: wrangler runs without credentials.
|
|
61
77
|
// Its info lines (the local location and "Use --remote if you want to access
|
|
@@ -68,13 +84,15 @@ export function localPortability({ database, binding = 'DB', buckets = {}, polic
|
|
|
68
84
|
if(legacyRecovery)receipt.output=relative(root,legacyRecovery.output);
|
|
69
85
|
return { target: 'local', [direction === 'export' ? 'output' : 'input']: relative(root, path), ...receipt };
|
|
70
86
|
}
|
|
71
|
-
|
|
87
|
+
const restore = context => run(context, 'import');
|
|
88
|
+
const intake = context => withManagedDataImport(context, { binding, buckets, policy, policyModule, platformMigrations, transient }, restore);
|
|
89
|
+
return { legacyRecovery:true, export: context => run(context, 'export'), import: managedData ? intake : restore, ...(managedData ? { managedDataImport: intake } : {}) };
|
|
72
90
|
}
|
|
73
91
|
|
|
74
92
|
async function nativeWorker(store,direction,input,signal) {
|
|
75
93
|
// The notice filter loads before the worker links its static node:sqlite import.
|
|
76
94
|
const worker = fileURLToPath(new URL('./portability-worker.mjs', import.meta.url)), notice = new URL('./sqlite-notice.mjs', import.meta.url).href;
|
|
77
|
-
const child = store.spawn(`${direction === 'migration-preflight' ? 'Checking migration history for' : direction === 'policy-preflight' ? 'Checking archive policy' :
|
|
95
|
+
const child = store.spawn(`${direction === 'migration-preflight' ? 'Checking migration history for' : direction === 'policy-preflight' ? 'Checking archive policy' : ['legacy-preflight','schema-recovery-preflight'].includes(direction) ? 'Checking empty recovery destination' : direction === 'export' ? 'Exporting' : 'Restoring'} local installation data`, process.execPath,
|
|
78
96
|
['--import', notice, worker, direction, JSON.stringify(input)], { cwd: input.root, signal, env: { ...process.env, WRANGLER_SEND_METRICS: 'false' }, stdio: ['ignore', 'pipe', 'pipe', 'ipc'] });
|
|
79
97
|
let receipt;
|
|
80
98
|
// Progress lines come first, as {progress}; the last message is the receipt.
|
|
@@ -91,8 +109,12 @@ async function nativeWorker(store,direction,input,signal) {
|
|
|
91
109
|
: receipt.safety ? `Restore refused (${receipt.reason}): safety normalization for ${receipt.safety} violates the declared import policy.`
|
|
92
110
|
: receipt.reason?.startsWith('migration_ledger:') ? ledgerRefusal(receipt)
|
|
93
111
|
: receipt.migration ? `Restore refused (schema_mismatch:migration_data): migration ${receipt.migration} changed data after this archive was made, so its rows would skip that change. Restore an archive made after it.`
|
|
94
|
-
: receipt.occupied ?
|
|
112
|
+
: receipt.occupied ? occupiedRefusal(input,receipt.reason,receipt.occupied)
|
|
95
113
|
: `The portability module refused or failed the archive operation (${receipt.reason ?? 'incomplete'}${receipt.native ? `: ${receipt.native.class}${receipt.native.code && receipt.native.code !== receipt.reason ? `, code ${receipt.native.code}` : ''}` : ''}).${receipt.native && receipt.exitCode === 1 ? ' Run the same command again; it starts over.' : ''}`, receipt.exitCode ?? 1);
|
|
96
114
|
delete receipt.ok;
|
|
97
115
|
return receipt;
|
|
98
116
|
}
|
|
117
|
+
|
|
118
|
+
function occupiedRefusal(input,reason,occupied) {
|
|
119
|
+
return `Restore refused (${reason}): ${reason === 'bucket_not_empty' ? `storage bucket ${occupied} in this installation already holds files` : `table ${occupied} in this installation already holds rows its migrations did not insert`}, and a restore only goes into an empty installation. To replace this installation's data with the archive, keep a copy with rer export --output exports/before-restore.rer, then run rer reset --yes and rer import --input ${relative(input.root, input.path)} --yes again.`;
|
|
120
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// Keep the credential link only where the caller already permits openUrl.
|
|
2
|
+
export function readySummary({ url, openUrl, signedInAs, siteUrl }) {
|
|
3
|
+
const lines = [openUrl
|
|
4
|
+
? `Open your app${signedInAs ? ', already signed in' : ''} on this computer: ${openUrl}`
|
|
5
|
+
: `Open your app on this computer: ${url}`];
|
|
6
|
+
if (siteUrl) lines.push(`Help and public site preview (secondary): ${siteUrl}`);
|
|
7
|
+
if (openUrl) lines.push(`App address (without sign-in): ${url}`, 'Keep the sign-in link private.');
|
|
8
|
+
return lines;
|
|
9
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
// Archive payloads measured by install.sh; Git and package managers do not
|
|
2
|
+
// expose wire-byte totals here. Disk blocks are measured separately, never
|
|
3
|
+
// substituted for download sizes. No checkout content or credentials are read.
|
|
4
|
+
import { execFile } from 'node:child_process';
|
|
5
|
+
import { join } from 'node:path';
|
|
6
|
+
import { fileURLToPath } from 'node:url';
|
|
7
|
+
import { promisify } from 'node:util';
|
|
8
|
+
import { coldRunBudget } from '../foundation/runtime/run-data.mjs';
|
|
9
|
+
const ownRoot = fileURLToPath(new URL('../', import.meta.url));
|
|
10
|
+
|
|
11
|
+
const execute = promisify(execFile);
|
|
12
|
+
export function showBudget(args) {
|
|
13
|
+
if (!args.includes('--json') && !process.env.RER_BUDGET_SHOWN) process.stderr.write(coldRunBudget + '\n');
|
|
14
|
+
process.env.RER_BUDGET_SHOWN = '1';
|
|
15
|
+
}
|
|
16
|
+
const payload = value => /^\d+$/.test(value ?? '') ? `${value} bytes` : 'not measured';
|
|
17
|
+
async function disk(path) {
|
|
18
|
+
try {
|
|
19
|
+
const { stdout } = await execute('du', ['-sk', path], { timeout: 10000 });
|
|
20
|
+
const blocks = Number(stdout.match(/^\d+/)?.[0]);
|
|
21
|
+
if (Number.isSafeInteger(blocks)) return `${blocks * 1024} bytes`;
|
|
22
|
+
} catch { /* An unavailable measurement is stated, never estimated. */ }
|
|
23
|
+
return 'not measured';
|
|
24
|
+
}
|
|
25
|
+
export async function launchSummary(root) {
|
|
26
|
+
const components = [['rer', ownRoot], ['clone (Git objects)', root && join(root, '.git')], ['dependencies', root && join(root, 'node_modules')]];
|
|
27
|
+
const sizes = await Promise.all(components.map(async ([name, path]) => `${name}: ${path ? await disk(path) : 'not measured'}`));
|
|
28
|
+
const lines = [
|
|
29
|
+
`Downloads this run (archive payloads): Node installer: ${payload(process.env.RER_TRANSFER_NODE_BYTES)}; rer installer: ${payload(process.env.RER_TRANSFER_RER_BYTES)}.`,
|
|
30
|
+
'Other downloads: project Node, clone and dependencies: not measured. Package-manager caches and metadata are not included in archive counts.',
|
|
31
|
+
`Disk use now (allocated space, not download sizes): ${sizes.join('; ')}.`,
|
|
32
|
+
];
|
|
33
|
+
if (process.env.RER_TRANSFER_NODE_DISK) lines.push(`Installer Node disk use: ${payload(process.env.RER_TRANSFER_NODE_DISK)}.`);
|
|
34
|
+
if (process.env.RER_LAUNCH_PATH_STEP) lines.push(process.env.RER_LAUNCH_PATH_STEP);
|
|
35
|
+
|
|
36
|
+
return lines;
|
|
37
|
+
}
|
|
@@ -9,11 +9,11 @@ import { trafficProxy } from './server-proxy.mjs';
|
|
|
9
9
|
// for application URLs, cookies and sign-in links.
|
|
10
10
|
// In door mode (`door`) the listener is 127.0.0.1 on a port the OS assigns,
|
|
11
11
|
// and only the machine's front door reaches it.
|
|
12
|
-
export async function runtimeFrontDoor({ host, port, activity, door, devKey, origin }) {
|
|
13
|
-
|
|
12
|
+
export async function runtimeFrontDoor({ host, port, activity, door, devKey, origin, ownerEntry }) {
|
|
13
|
+
let runtimePort = await runtimeLoopbackPort(port);
|
|
14
14
|
const observer = activity ?? { begin: () => () => {}, touch: () => {} };
|
|
15
|
-
const proxy = await trafficProxy({ host, port, target: runtimePort, activity: observer, door, devKey, origin });
|
|
16
|
-
return { ...proxy, runtimePort };
|
|
15
|
+
const proxy = await trafficProxy({ host, port, target: runtimePort, activity: observer, door, devKey, origin, ownerEntry });
|
|
16
|
+
return { ...proxy, get runtimePort(){return runtimePort;}, setRuntimePort(port){runtimePort=port;proxy.setTarget(port);} };
|
|
17
17
|
}
|
|
18
18
|
|
|
19
19
|
// The runtime binds this port strictly, seconds after it is chosen. A port the
|
package/cli/server-proxy.mjs
CHANGED
|
@@ -23,12 +23,13 @@ import { UpstreamAgent } from './upstream-agent.mjs';
|
|
|
23
23
|
// A Host that is neither loopback, a `*.localhost` name nor the configured
|
|
24
24
|
// origin's (origin) gets a plain 421 here: the application would refuse it
|
|
25
25
|
// anyway, and an auth check that throws on it must not answer with a crash page.
|
|
26
|
-
export async function trafficProxy({host,port,target,activity,door,devKey,origin}) {
|
|
26
|
+
export async function trafficProxy({host,port,target,activity,door,devKey,origin,ownerEntry}) {
|
|
27
27
|
const probe=randomUUID(),sockets=new Set(),keyed=keyHolder(devKey),configured=origin?new URL(origin).host:null;
|
|
28
28
|
const agent=new UpstreamAgent();let closing=false;
|
|
29
|
-
const server=http.createServer((req,res)=>{
|
|
29
|
+
const server=http.createServer(async (req,res)=>{
|
|
30
30
|
if(door&&!doorAdmits(req,door)){req.resume();res.writeHead(403,{'content-type':'text/plain; charset=utf-8'});res.end('This checkout answers only through its front door.');return;}
|
|
31
31
|
if(!hostAdmits(req,configured)){req.resume();res.writeHead(421,{'content-type':'text/plain; charset=utf-8'});res.end('This local server answers only its own host.');return;}
|
|
32
|
+
try { if(await ownerEntry?.(req,res))return; } catch {req.resume();notFound(res);return;}
|
|
32
33
|
if(!publicPath(req.url)||fsPath(req.url)&&!keyed(req)){req.resume();notFound(res);return;}
|
|
33
34
|
const internal=req.headers['x-rer-probe']===probe;delete req.headers['x-rer-probe'];
|
|
34
35
|
const endActivity=internal?()=>{}:activity.begin();let finished=false;
|
|
@@ -102,7 +103,7 @@ export async function trafficProxy({host,port,target,activity,door,devKey,origin
|
|
|
102
103
|
upstream.on('error',()=>socket.destroy());socket.on('error',()=>upstream.destroy());socket.on('close',()=>upstream.destroy());upstream.on('close',()=>socket.destroy());
|
|
103
104
|
});
|
|
104
105
|
try{await listen(server,host,port);}catch(error){agent.destroy();throw error;}
|
|
105
|
-
return {probe,port:server.address().port,async close(){closing=true;agent.destroy();for(const socket of sockets)socket.destroy();await new Promise(resolve=>server.close(resolve));}};
|
|
106
|
+
return {probe,port:server.address().port,setTarget(port){target=port;},async close(){closing=true;agent.destroy();for(const socket of sockets)socket.destroy();await new Promise(resolve=>server.close(resolve));}};
|
|
106
107
|
}
|
|
107
108
|
function doorAdmits(req,door){
|
|
108
109
|
const host=String(req.headers.host??'').toLowerCase(),given=Buffer.from(String(req.headers[door.header]??'')),secret=Buffer.from(door.secret);
|
package/cli/server-registry.mjs
CHANGED
|
@@ -28,10 +28,11 @@ export const declaresJourney = env => {
|
|
|
28
28
|
if(env.RER_SERVER_JOURNEY==='1')return true;
|
|
29
29
|
throw invalid('RER_SERVER_JOURNEY must be 1 to declare a test or CI journey.');
|
|
30
30
|
};
|
|
31
|
-
async function request(record, action) {
|
|
31
|
+
async function request(record, action, {timeoutMs=35000,signal}={}) {
|
|
32
32
|
return new Promise((resolveRequest,reject)=>{
|
|
33
|
-
const socket=createConnection(record.control);let text='';
|
|
34
|
-
|
|
33
|
+
const socket=createConnection({path:record.control,signal});let text='';
|
|
34
|
+
const timer=setTimeout(()=>socket.destroy(Error('Managed server did not complete cleanup.')),timeoutMs);
|
|
35
|
+
socket.once('close',()=>clearTimeout(timer));
|
|
35
36
|
socket.on('connect',()=>socket.end(JSON.stringify({token:record.token,checkout:record.checkout,generation:record.generation,action})));
|
|
36
37
|
socket.on('data',chunk=>{text+=chunk;if(text.length>16384)socket.destroy(Error('Invalid managed server reply.'));});
|
|
37
38
|
socket.on('end',()=>{try{const value=JSON.parse(text);if(value.token!==record.token||value.checkout!==record.checkout||value.generation!==record.generation)throw Error('Unverified checkout server');resolveRequest(value);}catch(error){reject(error);}});
|
|
@@ -198,15 +199,19 @@ export async function serverRegistry(root,{env=process.env,memory=availableMemor
|
|
|
198
199
|
}
|
|
199
200
|
async function update(fields){Object.assign(owned,fields);await change(state=>{const item=state.running.find(item=>item.generation===owned.generation);if(!item)throw Error('Server reservation disappeared');Object.assign(item,fields);});}
|
|
200
201
|
async function updateActivity(fields){await change(state=>{const item=state.running.find(item=>item.generation===owned.generation);if(item)Object.assign(item,fields);});}
|
|
201
|
-
async function waitReady(record,signal){
|
|
202
|
-
|
|
202
|
+
async function waitReady(record,signal,{timeoutMs=300000,now=()=>performance.now()}={}){
|
|
203
|
+
const deadline=now()+timeoutMs;
|
|
204
|
+
while(now()<deadline){
|
|
203
205
|
signal?.throwIfAborted();if(!await alive(record.owner))throw refuse('Concurrent server startup failed. Retry after recovery.');
|
|
204
|
-
|
|
205
|
-
await
|
|
206
|
+
const remaining=deadline-now();if(remaining<=0)break;
|
|
207
|
+
try{const status=await request(record,'status',{timeoutMs:Math.min(35000,remaining),signal});if(now()<deadline&&status.ready)return {...record,...status};}
|
|
208
|
+
catch(error){if(signal?.aborted)throw error;}
|
|
209
|
+
if(now()<deadline)await delay(Math.min(250,deadline-now()),undefined,{signal});
|
|
206
210
|
}
|
|
207
211
|
throw refuse('The existing server is still starting. Wait for its ready output.');
|
|
208
212
|
}
|
|
209
|
-
|
|
213
|
+
|
|
214
|
+
return {checkout,existing,stop,reserve,serve,update,updateActivity,waitReady,ownerEntry:record=>request(record,'owner-entry'),custodyToken:()=>owned?.token,
|
|
210
215
|
// The sockets close even when the record cannot be removed (its registry
|
|
211
216
|
// deleted under it, say): an open listener would keep this process alive.
|
|
212
217
|
close(){return closing??=(async()=>{
|
package/cli/site.mjs
CHANGED
|
@@ -10,6 +10,7 @@ import { unpublishedIdentities } from './site-private.mjs';
|
|
|
10
10
|
import { privateSitePaths } from './site-inputs.mjs';
|
|
11
11
|
import { redactSiteText } from './site-diagnostics.mjs';
|
|
12
12
|
import { siteRuntime, buildSiteRuntime, runtimeSummary } from './site-runtime.mjs';
|
|
13
|
+
import { deployDiagnostic, providerFailure } from '../modules/deploy-kit/diagnostics.mjs';
|
|
13
14
|
import { siteHosting } from './site-hosting.mjs';
|
|
14
15
|
|
|
15
16
|
// The run site: a seed's static marketing pages, /run and docs, built from
|
|
@@ -154,7 +155,11 @@ export async function siteCommand(project, options, store, signal, out) {
|
|
|
154
155
|
return { dryRun: false, deployed: true, origin: result.origin, site: summary };
|
|
155
156
|
} catch (error) {
|
|
156
157
|
if (error instanceof RerError) throw error;
|
|
157
|
-
|
|
158
|
+
const { createRedaction } = await shared('redaction.mjs');
|
|
159
|
+
const redaction = createRedaction();
|
|
160
|
+
redaction.addSecrets(names.map(name => process.env[name]));
|
|
161
|
+
const diagnostic = deployDiagnostic(error,redaction.redact), provider = providerFailure(error,redaction.redact);
|
|
162
|
+
throw new RerError('deploy_refused', redaction.redact(`Run site deploy stopped: ${/^[a-z0-9-]{1,80}$/.test(error.message) ? error.message : 'site-deploy-failed'}${error.resource ? ` (${error.resource})` : ''}.${provider ? ` ${provider}.` : ''}${siteReadbackSummary(error)}`), 1, {step:diagnostic?.step, ...(diagnostic ? {siteDeploy:diagnostic} : {})});
|
|
158
163
|
}
|
|
159
164
|
}
|
|
160
165
|
|
package/cli/transfers.mjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
import { isManagedDataZip } from './managed-data-zip.mjs';
|
|
1
2
|
import { stat } from 'node:fs/promises';
|
|
2
|
-
import { relative } from 'node:path';
|
|
3
|
+
import { basename, relative } from 'node:path';
|
|
3
4
|
import {localDependencyLinks} from './dependency-doctor.mjs';
|
|
4
5
|
import { contained } from './project.mjs';
|
|
5
6
|
import { missing, refuse } from './errors.mjs';
|
|
@@ -18,19 +19,31 @@ export async function transfer(project, options, signal, custody) {
|
|
|
18
19
|
if(target!=='local'||options.platform==='container'||api?.legacyRecovery!==true)throw refuse('Legacy v1 recovery is supported only by the local Workers portability adapter in an empty isolated installation.');
|
|
19
20
|
}else if(direction==='import' && (options.acknowledgeUnverifiedMetadata||options.output))throw refuse('--acknowledge-unverified-metadata and import --output require --legacy-v1.');
|
|
20
21
|
if (!api || typeof api[direction] !== 'function' || (target === 'remote' && api.remote !== true)) throw refuse(`Complete ${target} ${direction} is unsupported: the reviewed portability API and binding policy must be adopted first.`);
|
|
21
|
-
const
|
|
22
|
+
const pathArgument = async (flag, action) => {
|
|
23
|
+
try { return await action(); } catch (error) {
|
|
24
|
+
if (error.code === 'safety_refusal' && error.message === 'Path must be a contained checkout path without traversal.') {
|
|
25
|
+
const quote = value => "'" + value.replaceAll("'", "'\\''") + "'";
|
|
26
|
+
const staged = value => `.rer/recovery/${basename(value.replaceAll('\\', '/'))}`;
|
|
27
|
+
const args = ['rer import', '--input', quote(flag === '--input' ? staged(options.input) : options.input), ...(options.journal ? ['--journal', quote(staged(options.journal))] : []), '--yes', '--cwd', quote(project.root)];
|
|
28
|
+
error.humanMessage = `${flag} ${JSON.stringify(options[flag.slice(2)])} must stay inside this checkout, without .. or an absolute path, for example .rer/recovery/<name>.rer. Stage the archives there as owned private files (0600), then run: ${args.join(' ')}`;
|
|
29
|
+
}
|
|
30
|
+
throw error;
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
const located = direction === 'export' ? await locateExport(project.root, file) : await pathArgument('--input', () => locateImport(project.root, file)), path = located.path;
|
|
22
34
|
const legacyOutput = options.legacyV1 ? await locateExport(project.root, options.output) : undefined;
|
|
23
35
|
const legacyRecovery=legacyOutput?{output:legacyOutput.path,acknowledged:options.acknowledgeUnverifiedMetadata}:undefined;
|
|
24
36
|
const sessionFile = options.sessionFile ? await contained(project.root, options.sessionFile, { privateFile: true }) : undefined;
|
|
25
37
|
if(options.journal && legacyRecovery)throw refuse('--journal requires normal v2 import; legacy v1 metadata cannot authenticate a later-journal recovery.');
|
|
26
38
|
if (options.journal && (direction !== 'import' || target !== 'local')) throw refuse('--journal applies to a local import only.');
|
|
27
39
|
// A later archive of the same installation carries its latest erasure journal.
|
|
28
|
-
const journal = options.journal ? await contained(project.root, options.journal, { privateFile: true }) : undefined;
|
|
40
|
+
const journal = options.journal ? await pathArgument('--journal', () => contained(project.root, options.journal, { privateFile: true })) : undefined;
|
|
29
41
|
await project.adapter.prepareData?.(project.root, custody.store, signal);
|
|
30
42
|
await custody.store.step(`${target === 'remote' ? 'Hosted' : 'Local'} ${direction}`);
|
|
31
43
|
// Only now can the seed's Vite configuration load in a checkout that never built.
|
|
32
44
|
await located.unserved(); await legacyOutput?.unserved();
|
|
33
|
-
const
|
|
45
|
+
const operation = direction === 'import' && typeof api.managedDataImport === 'function' && await isManagedDataZip(path) ? api.managedDataImport : api[direction];
|
|
46
|
+
const result=await operation.call(api, { root: project.root, target, path, journal, origin: options.origin, sessionFile, signal, platform:options.platform, legacyRecovery, acknowledgeUnverifiedSchemaObjects:options.acknowledgeUnverifiedSchemaObjects, ...custody });
|
|
34
47
|
// The archive's size, for the line a finished export ends with.
|
|
35
48
|
if(direction==='export')return {...result,target,...(target==='remote'&&options.origin?{origin:new URL(options.origin).origin}:{}),output:result.output??relative(project.root,path),bytes:(await stat(path)).size};
|
|
36
49
|
if(direction==='import' && target==='local' && (await localDependencyLinks(project.root)).some(link=>link.needs_relink)) {
|