@runeditrun/rer 0.2.23 → 0.2.25
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 +16 -7
- package/cli/browser-runtime.mjs +65 -0
- package/cli/demo-host.mjs +1 -1
- package/cli/deploy-claim-request.mjs +5 -4
- package/cli/deploy-command.mjs +7 -5
- package/cli/deploy-inputs.mjs +2 -1
- package/cli/deploy-observation.mjs +20 -2
- package/cli/deploy-owner.mjs +12 -7
- package/cli/deploy.mjs +19 -6
- package/cli/export.mjs +1 -1
- package/cli/hydration/browser.mjs +3 -9
- package/cli/inspect.mjs +3 -1
- package/cli/lifecycle.mjs +4 -1
- package/cli/site.mjs +2 -2
- package/global/CHANGELOG.md +37 -0
- package/global/README.md +16 -7
- package/global/browser-runtime.mjs +45 -0
- package/global/cli/layout.mjs +8 -4
- package/global/main.mjs +13 -3
- package/install.sh +56 -3
- package/modules/container/README.md +6 -0
- package/modules/container/adapters/d1.mjs +1 -1
- package/modules/container/build.mjs +2 -1
- package/modules/container/image-variables.mjs +27 -0
- package/modules/container/transfer.mjs +0 -1
- package/modules/container/worker-engine.mjs +1 -1
- package/modules/delivery-producer/declarations.mjs +6 -1
- package/modules/delivery-producer/mail.mjs +66 -0
- package/modules/demo/label.mjs +5 -2
- package/modules/demo-host/README.md +29 -7
- package/modules/demo-host/container.mjs +19 -7
- package/modules/demo-host/placement.mjs +19 -5
- package/modules/demo-host/plan.mjs +11 -6
- package/modules/demo-host/start-script.mjs +2 -1
- package/modules/demo-host/startup.mjs +3 -3
- package/modules/demo-host/worker.mjs +27 -8
- package/modules/deploy-kit/README.md +41 -6
- package/modules/deploy-kit/asset-health.mjs +28 -0
- package/modules/deploy-kit/d1-import.mjs +22 -4
- package/modules/deploy-kit/diagnostics.mjs +46 -1
- package/modules/deploy-kit/health.mjs +2 -23
- package/modules/deploy-kit/migration-ledger.mjs +6 -2
- package/modules/deploy-kit/migration-retry.mjs +91 -0
- package/modules/deploy-kit/migrations.mjs +9 -5
- package/modules/deploy-kit/owner-cli.mjs +9 -6
- package/modules/deploy-kit/owner-remove.mjs +104 -64
- package/modules/deploy-kit/pagination.mjs +99 -0
- package/modules/deploy-kit/provider-transport.mjs +28 -5
- package/modules/deploy-kit/provider.mjs +38 -75
- package/modules/deploy-kit/r2-objects.mjs +6 -17
- package/modules/deploy-kit/site-readback.mjs +67 -0
- package/modules/deploy-kit/site.mjs +17 -31
- package/modules/deploy-kit/uploaded-worker.mjs +9 -3
- package/modules/deploy-kit/worker.mjs +5 -2
- package/modules/erasure/queue.mjs +0 -2
- package/modules/job-queue/cli.mjs +1 -1
- package/modules/operator-auth/README.md +10 -2
- package/modules/operator-auth/config.mjs +1 -0
- package/modules/operator-auth/conformance/presentation.mjs +2 -2
- package/modules/operator-auth/identity/email-link/confirmation.mjs +2 -1
- package/modules/operator-auth/identity/email-link/expiry.mjs +8 -0
- package/modules/operator-auth/identity/email-link/index.mjs +2 -1
- package/modules/operator-auth/identity/email-link/message.mjs +3 -2
- package/modules/operator-auth/identity/oidc/jwt.mjs +1 -1
- package/modules/operator-auth/index.d.mts +2 -0
- package/modules/portability/README.md +7 -2
- package/modules/portability/bridge.mjs +3 -3
- package/modules/portability/catalogue.mjs +1 -1
- package/modules/portability/cli-options.mjs +1 -1
- package/modules/portability/import-staging.mjs +45 -0
- package/modules/portability/import.mjs +8 -8
- package/modules/portability/legacy-files.mjs +1 -1
- package/modules/portability/legacy-safety.mjs +3 -3
- package/modules/portability/payloads.mjs +1 -1
- package/modules/portability/storage.mjs +5 -2
- package/modules/release/source.mjs +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { workerSettings } from './owner.mjs';
|
|
2
|
+
import { readinessAPI } from './readiness-window.mjs';
|
|
3
|
+
import { assert } from './validation.mjs';
|
|
4
|
+
|
|
5
|
+
const ANNOTATION = /^rer-site:[0-9a-f]{64}$/;
|
|
6
|
+
function refuse(code, worker) { return Object.assign(new Error(code), { resource: `worker:${worker}` }); }
|
|
7
|
+
|
|
8
|
+
/** A provider snapshot; only a fresh upload may have incomplete visibility. */
|
|
9
|
+
export async function servedVersion(api, worker, { pending = false } = {}) {
|
|
10
|
+
if (!await workerSettings(api, worker)) return null;
|
|
11
|
+
const { deployments } = await api.request(`/workers/scripts/${worker}/deployments`);
|
|
12
|
+
assert(Array.isArray(deployments) && (pending || deployments.length > 0), 'invalid-deployments');
|
|
13
|
+
const [latest] = deployments;
|
|
14
|
+
const single = latest?.versions?.length === 1 && latest.versions[0].percentage === 100 ? latest.versions[0].version_id : null;
|
|
15
|
+
const newest = (await api.request(`/workers/scripts/${worker}/versions`))?.items?.[0]?.id;
|
|
16
|
+
let version;
|
|
17
|
+
try { version = single ? await api.request(`/workers/scripts/${worker}/versions/${single}`) : null; }
|
|
18
|
+
catch (error) { if (!pending || error.status !== 404) throw error; }
|
|
19
|
+
assert(!version || version.id === single, 'invalid-active-version');
|
|
20
|
+
const annotation = version?.annotations?.['workers/message'];
|
|
21
|
+
return { kit: typeof annotation === 'string' && ANNOTATION.test(annotation), annotation,
|
|
22
|
+
current: Boolean(single) && single === newest, versionId: single, newest, deploymentId: latest?.id, deployed: Boolean(latest), visible: Boolean(version) };
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Preflight remains strict: no propagation allowance from an earlier invocation. */
|
|
26
|
+
export async function ownedSiteVersion(api, worker) {
|
|
27
|
+
const served = await servedVersion(api, worker);
|
|
28
|
+
if (!served) return null;
|
|
29
|
+
if (!served.kit) throw refuse('resource-name-collision', worker);
|
|
30
|
+
if (!served.current) throw refuse('owned-resource-replaced', worker);
|
|
31
|
+
return served;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Only the upload response and the exact preflight version may appear while
|
|
35
|
+
* Cloudflare's deployment and version reads converge. Never replay the upload. */
|
|
36
|
+
export async function waitForSiteUpload({ api, worker, versionId, fingerprint, previous, window }) {
|
|
37
|
+
const bounded = readinessAPI(api, window, 'site-upload-timeout');
|
|
38
|
+
const annotation = `rer-site:${fingerprint}`;
|
|
39
|
+
let lastSeen = null, delay = 2000;
|
|
40
|
+
const timeout = cause => Object.assign(refuse('site-upload-timeout', worker), { lastSeen, cause });
|
|
41
|
+
while (window.remaining() > 0) {
|
|
42
|
+
try { lastSeen = await servedVersion(bounded, worker, { pending: true }); }
|
|
43
|
+
catch (error) { if (window.remaining() <= 0 || error.name === 'TimeoutError') throw timeout(error); throw error; }
|
|
44
|
+
if (lastSeen) {
|
|
45
|
+
const allowed = id => !id || id === versionId || id === previous?.versionId;
|
|
46
|
+
const expected = lastSeen.versionId === versionId ? annotation : previous?.annotation;
|
|
47
|
+
if (!allowed(lastSeen.versionId) || !allowed(lastSeen.newest) || lastSeen.deployed && !lastSeen.versionId
|
|
48
|
+
|| lastSeen.visible && lastSeen.annotation !== expected
|
|
49
|
+
|| lastSeen.versionId === previous?.versionId && lastSeen.deploymentId !== previous.deploymentId)
|
|
50
|
+
throw refuse('owned-resource-replaced', worker);
|
|
51
|
+
if (lastSeen.current && lastSeen.versionId === versionId && lastSeen.annotation === annotation) return lastSeen;
|
|
52
|
+
}
|
|
53
|
+
await window.sleep(Math.min(delay, window.remaining()));
|
|
54
|
+
delay = Math.min(delay * 2, 10000);
|
|
55
|
+
}
|
|
56
|
+
throw timeout();
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** Only provider UUIDs and kit fingerprints are safe to include in CLI output. */
|
|
60
|
+
export function siteReadbackSummary(error) {
|
|
61
|
+
if (error.message !== 'site-upload-timeout') return '';
|
|
62
|
+
const seen = error.lastSeen;
|
|
63
|
+
if (!seen) return ' Last readback: Worker not visible.';
|
|
64
|
+
const id = value => typeof value === 'string' && /^[a-f0-9-]{36}$/i.test(value) ? value : 'not visible';
|
|
65
|
+
const fingerprint = typeof seen.annotation === 'string' && ANNOTATION.test(seen.annotation) ? seen.annotation.slice('rer-site:'.length) : 'not visible';
|
|
66
|
+
return ` Last readback: serving ${id(seen.versionId)}, newest ${id(seen.newest)}, fingerprint ${fingerprint}.`;
|
|
67
|
+
}
|
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
import { mkdir, rename, writeFile } from 'node:fs/promises';
|
|
2
2
|
import { dirname } from 'node:path';
|
|
3
3
|
import { assert, hash, resourceName } from './validation.mjs';
|
|
4
|
-
import { ownerWorkerName
|
|
4
|
+
import { ownerWorkerName } from './owner.mjs';
|
|
5
5
|
import { uploadAssets, workerOrigin } from './worker.mjs';
|
|
6
|
+
import { readinessWindow, readinessAPI } from './readiness-window.mjs';
|
|
7
|
+
import { uploadVersionId } from './uploaded-worker.mjs';
|
|
8
|
+
import { ownedSiteVersion, waitForSiteUpload } from './site-readback.mjs';
|
|
9
|
+
export { servedVersion, siteReadbackSummary } from './site-readback.mjs';
|
|
6
10
|
|
|
7
11
|
/* A seed's run site (run-<seed>.com): the folder `rer build --site` writes,
|
|
8
12
|
* served on the owner's own account by a Base-generated module Worker (fixed
|
|
@@ -24,7 +28,6 @@ import { uploadAssets, workerOrigin } from './worker.mjs';
|
|
|
24
28
|
|
|
25
29
|
const RECEIPT_KIND = 'rer-deploy-site-receipt';
|
|
26
30
|
const APEX = /^(?!www\.)(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z][a-z0-9-]{0,62}$/;
|
|
27
|
-
const ANNOTATION = /^rer-site:[0-9a-f]{64}$/;
|
|
28
31
|
export const SITE_ASSETS_CONFIG = Object.freeze({ html_handling: 'auto-trailing-slash', not_found_handling: '404-page' });
|
|
29
32
|
export const SITE_MODULE = 'site-worker.mjs';
|
|
30
33
|
/** The site Worker. It names no domain: only the site's two Custom Domains route to it. */
|
|
@@ -65,28 +68,6 @@ export function sitePlan({ project, environment, compatibilityDate, declaration,
|
|
|
65
68
|
redirects: [{ from: `https://www.${domain}/*`, to: `https://${domain}/`, status: 301, keeps: ['path', 'query'] }] };
|
|
66
69
|
}
|
|
67
70
|
|
|
68
|
-
/** The version the Worker's latest deployment serves, as the kit reads ownership: null when there is no Worker. */
|
|
69
|
-
export async function servedVersion(api, worker) {
|
|
70
|
-
if (!await workerSettings(api, worker)) return null;
|
|
71
|
-
const { deployments } = await api.request(`/workers/scripts/${worker}/deployments`);
|
|
72
|
-
assert(Array.isArray(deployments) && deployments.length > 0, 'invalid-deployments');
|
|
73
|
-
const [latest] = deployments, single = latest.versions?.length === 1 && latest.versions[0].percentage === 100 ? latest.versions[0].version_id : null;
|
|
74
|
-
const newest = (await api.request(`/workers/scripts/${worker}/versions`))?.items?.[0]?.id;
|
|
75
|
-
const version = single ? await api.request(`/workers/scripts/${worker}/versions/${single}`) : null;
|
|
76
|
-
const annotation = version?.annotations?.['workers/message'];
|
|
77
|
-
return { kit: typeof annotation === 'string' && ANNOTATION.test(annotation), annotation, current: Boolean(single) && single === newest };
|
|
78
|
-
}
|
|
79
|
-
/** The Worker is absent (false) or the kit's (true); anything else is refused. */
|
|
80
|
-
async function ownWorker(api, worker) {
|
|
81
|
-
const served = await servedVersion(api, worker);
|
|
82
|
-
if (!served) return false;
|
|
83
|
-
// Someone else's Worker of this name, or one whose live version the kit did not upload.
|
|
84
|
-
if (!served.kit) throw refuse('resource-name-collision', 'worker', worker);
|
|
85
|
-
// A kit version that is no longer the newest one served alone: a dashboard rollback or a split deployment.
|
|
86
|
-
if (!served.current) throw refuse('owned-resource-replaced', 'worker', worker);
|
|
87
|
-
return true;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
71
|
async function writeReceipt(path, receipt) {
|
|
91
72
|
await mkdir(dirname(path), { recursive: true, mode: 0o700 });
|
|
92
73
|
const temporary = `${path}.${process.pid}.tmp`;
|
|
@@ -95,19 +76,19 @@ async function writeReceipt(path, receipt) {
|
|
|
95
76
|
}
|
|
96
77
|
|
|
97
78
|
/** Read-only preflight, then the site Worker upload and both Custom Domains, then readback. */
|
|
98
|
-
export async function siteDeploy({ api, plan, files, receiptPath }) {
|
|
79
|
+
export async function siteDeploy({ api, plan, files, receiptPath, readiness = {} }) {
|
|
99
80
|
assert(typeof receiptPath === 'string' && receiptPath.length, 'missing-receipt-path');
|
|
100
81
|
assert(plan?.module === SITE_MODULE && Array.isArray(files) && files.some(file => file.path === 'index.html'), 'invalid-site-assets');
|
|
101
82
|
const zones = await api.zoneRequest(`/zones?name=${plan.domain}&account.id=${api.accountId}`);
|
|
102
83
|
const zone = Array.isArray(zones) ? zones.find(row => row?.name === plan.domain && row.account?.id === api.accountId) : null;
|
|
103
84
|
if (!zone) throw refuse('site-zone-missing', 'zone', plan.domain);
|
|
104
85
|
// Preflight: the Worker and each hostname are the kit's own or absent before any write.
|
|
105
|
-
const
|
|
86
|
+
const previous = await ownedSiteVersion(api, plan.worker);
|
|
106
87
|
const attached = await api.request(`/workers/domains?zone_id=${zone.id}`);
|
|
107
88
|
assert(Array.isArray(attached), 'invalid-domain-list');
|
|
108
89
|
for (const hostname of plan.hostnames) {
|
|
109
90
|
const row = attached.find(item => item?.hostname === hostname);
|
|
110
|
-
if (row && (!
|
|
91
|
+
if (row && (!previous || row.service !== plan.worker)) throw refuse('resource-name-collision', 'domain', hostname);
|
|
111
92
|
}
|
|
112
93
|
|
|
113
94
|
const fingerprint = hash({ files: files.map(({ path, sha256 }) => ({ path, sha256 })), config: plan.assetsConfig, module: hash(SITE_WORKER), compatibilityDate: plan.compatibilityDate });
|
|
@@ -116,9 +97,14 @@ export async function siteDeploy({ api, plan, files, receiptPath }) {
|
|
|
116
97
|
body.set('metadata', new Blob([JSON.stringify({ main_module: SITE_MODULE, compatibility_date: plan.compatibilityDate, bindings: [{ type: 'assets', name: 'ASSETS' }],
|
|
117
98
|
assets: { jwt, config: { ...plan.assetsConfig, run_worker_first: true } }, annotations: { 'workers/message': `rer-site:${fingerprint}` } })], { type: 'application/json' }));
|
|
118
99
|
body.set(SITE_MODULE, new Blob([SITE_WORKER], { type: 'application/javascript+module' }), SITE_MODULE);
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
100
|
+
const window = readiness.window ?? readinessWindow(readiness);
|
|
101
|
+
window.start();
|
|
102
|
+
api = readinessAPI(api, window, 'site-upload-timeout');
|
|
103
|
+
const upload = await api.request(`/workers/scripts/${plan.worker}`, { method: 'PUT', body });
|
|
104
|
+
const versionId = uploadVersionId(upload);
|
|
105
|
+
assert(versionId, 'invalid-upload-version');
|
|
106
|
+
const readback = () => waitForSiteUpload({ api, worker: plan.worker, versionId, fingerprint, previous, window });
|
|
107
|
+
await readback();
|
|
122
108
|
await workerOrigin(api, plan.worker, { enable: true, workersDev: false });
|
|
123
109
|
for (const hostname of plan.hostnames) {
|
|
124
110
|
if (attached.some(row => row?.hostname === hostname)) continue;
|
|
@@ -126,7 +112,7 @@ export async function siteDeploy({ api, plan, files, receiptPath }) {
|
|
|
126
112
|
assert(row?.hostname === hostname && row.service === plan.worker && typeof row.id === 'string', 'invalid-domain-attach');
|
|
127
113
|
}
|
|
128
114
|
// Readback: the kit's Worker live and both domains on it.
|
|
129
|
-
|
|
115
|
+
await readback();
|
|
130
116
|
const domains = await api.request(`/workers/domains?zone_id=${zone.id}`);
|
|
131
117
|
for (const hostname of plan.hostnames) assert(domains.some(row => row.hostname === hostname && row.service === plan.worker), 'domain-readback-mismatch');
|
|
132
118
|
const receipt = { worker: plan.worker, domain: plan.domain, zoneId: zone.id, fingerprint, files: files.length, hostnames: plan.hostnames, redirect: plan.redirects[0] };
|
|
@@ -2,14 +2,20 @@ import { readinessAPI } from './readiness-window.mjs';
|
|
|
2
2
|
import { activeWorker, deployedFingerprint } from './worker.mjs';
|
|
3
3
|
import { assert, uuid } from './validation.mjs';
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
export async function uploadedWorker({ api, worker, upload, previous, fingerprint, window }) {
|
|
5
|
+
/** Normalize the version identity returned by the script upload endpoint. */
|
|
6
|
+
export function uploadVersionId(upload) {
|
|
8
7
|
const rawVersion = typeof upload?.deployment_id === 'string' ? upload.deployment_id.toLowerCase() : upload?.deployment_id;
|
|
9
8
|
// The legacy upload API also returns the UUID without separators (Wrangler
|
|
10
9
|
// normalizes this field before treating it as its uploaded version ID).
|
|
11
10
|
const versionId = typeof rawVersion === 'string' && /^[a-f0-9]{32}$/i.test(rawVersion) ? rawVersion.replace(/^(.{8})(.{4})(.{4})(.{4})(.{12})$/, '$1-$2-$3-$4-$5').toLowerCase() : rawVersion;
|
|
12
11
|
if (versionId !== undefined) assert(uuid(versionId), 'invalid-upload-version');
|
|
12
|
+
return versionId;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// Poll only the missing fresh Worker or the exact pre-upload version. A new
|
|
16
|
+
// unrelated version is drift, even during propagation; provider errors fail.
|
|
17
|
+
export async function uploadedWorker({ api, worker, upload, previous, fingerprint, window }) {
|
|
18
|
+
const versionId = uploadVersionId(upload);
|
|
13
19
|
const bounded = readinessAPI(api, window, 'uploaded-worker-timeout');
|
|
14
20
|
while (window.remaining() > 0) {
|
|
15
21
|
const current = await activeWorker(bounded, worker);
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { nameAssetFailure } from './diagnostics.mjs';
|
|
1
2
|
import { extname } from 'node:path';
|
|
2
3
|
import { assert, hash, hexId, uuid } from './validation.mjs';
|
|
3
4
|
import { moduleType, assetType } from './artifact.mjs';
|
|
@@ -61,7 +62,8 @@ export async function uploadAssets(api, worker, files) {
|
|
|
61
62
|
manifest[`/${file.path}`] = {hash:digest,size:Buffer.from(file.base64,'base64').length};
|
|
62
63
|
byHash.set(digest,file);
|
|
63
64
|
}
|
|
64
|
-
const session = await api.request(`/workers/scripts/${worker}/assets-upload-session`,{method:'POST',body:{manifest}})
|
|
65
|
+
const session = await api.request(`/workers/scripts/${worker}/assets-upload-session`,{method:'POST',body:{manifest}})
|
|
66
|
+
.catch(error => { throw nameAssetFailure(error,files.map(file => file.path)); });
|
|
65
67
|
assert(typeof session.jwt === 'string' && session.jwt && Array.isArray(session.buckets),'invalid-asset-session');
|
|
66
68
|
let completion = session.buckets.length ? null : session.jwt;
|
|
67
69
|
const seen = new Set();
|
|
@@ -72,7 +74,8 @@ export async function uploadAssets(api, worker, files) {
|
|
|
72
74
|
assert(byHash.has(key) && !seen.has(key),'invalid-asset-request'); seen.add(key);
|
|
73
75
|
const file = byHash.get(key); data.set(key,new Blob([file.base64],{type:assetType(file.path)}),file.path.split('/').at(-1));
|
|
74
76
|
}
|
|
75
|
-
const uploaded = await api.request('/workers/assets/upload?base64=true',{method:'POST',body:data,assetToken:session.jwt})
|
|
77
|
+
const uploaded = await api.request('/workers/assets/upload?base64=true',{method:'POST',body:data,assetToken:session.jwt})
|
|
78
|
+
.catch(error => { throw nameAssetFailure(error,bucket.map(key => byHash.get(key).path)); });
|
|
76
79
|
if (uploaded.jwt) completion = uploaded.jwt;
|
|
77
80
|
}
|
|
78
81
|
assert(typeof completion === 'string' && completion,'incomplete-asset-upload');
|
|
@@ -8,8 +8,6 @@ import { foreignKeyLinks } from './order.mjs';
|
|
|
8
8
|
* the whole erasure stays linear in its rows. The slice is verified inside the
|
|
9
9
|
* step's batch; anything the queue does not know about (rows written after it
|
|
10
10
|
* was built) is found by the full collection that runs once the queue is empty. */
|
|
11
|
-
const seq = n => String(n).padStart(10, '0');
|
|
12
|
-
|
|
13
11
|
export const clearQueue = (db, id) => statement(db, "DELETE FROM rer_erasure_scope WHERE operation=? AND kind='queue'", id);
|
|
14
12
|
|
|
15
13
|
/** Replaces the operation's queue with `entries`; chunks stay small enough for one bound value. */
|
|
@@ -45,7 +45,7 @@ async function main() {
|
|
|
45
45
|
let result;
|
|
46
46
|
if (verb === 'run') {
|
|
47
47
|
const statusCommand = [process.execPath, fileURLToPath(import.meta.url), 'status', '--json'].map(shellQuote).join(' ');
|
|
48
|
-
result = await queue.run(command, { ...options, onWaiting: (
|
|
48
|
+
result = await queue.run(command, { ...options, onWaiting: (_reason, details) => {
|
|
49
49
|
if (!options.json) console.error(`${waitingMessage(details)}\nInspect the queue with: ${statusCommand}`);
|
|
50
50
|
} });
|
|
51
51
|
if (result.timedOut) console.error(`Job queue max-wait expired: ${result.reason} (exit ${result.code})`);
|
|
@@ -151,7 +151,8 @@ Do not log confirmation URLs, input tokens, cookies or provider message bodies.
|
|
|
151
151
|
GET/HEAD confirmation uses a self-contained, responsive document. A magic link
|
|
152
152
|
says “Confirm sign-in to Example”; an invitation says “Accept your invitation to
|
|
153
153
|
Example”. Both explain that the link works once and show its remaining lifetime,
|
|
154
|
-
rounded up to
|
|
154
|
+
rounded up to days for lifetimes of a day or more, otherwise minutes (singular
|
|
155
|
+
for one day or minute). The sole primary form button is
|
|
155
156
|
“Confirm”. Only its hidden POST field contains the token. Invalid, expired,
|
|
156
157
|
consumed or unauthorized links return a styled 400 document with “Request a new
|
|
157
158
|
link” pointing to the configured sign-in page. HEAD has the same status and
|
|
@@ -219,7 +220,8 @@ Cookies are host-only, Path=/, HttpOnly, SameSite=Lax; production uses Secure an
|
|
|
219
220
|
127.0.0.1 or [::1] and uses `operator_session_local_<port>` (for example
|
|
220
221
|
`operator_session_local_8787`): browsers keep cookies per host, not per port, so
|
|
221
222
|
checkouts side by side on one loopback host each keep their own session. Session expiry defaults to
|
|
222
|
-
7 days (maximum 30), links to 15 minutes (maximum 60)
|
|
223
|
+
7 days (maximum 30), sign-in and step-up links to 15 minutes (maximum 60), and
|
|
224
|
+
invitations to 7 days; no silent sliding renewal.
|
|
223
225
|
`returnTo` must be a local absolute path; unsafe values become `/app`. It is stored
|
|
224
226
|
with the token, and cannot be replaced on confirmation. Auth methods never return
|
|
225
227
|
raw link tokens. `CompletedSession.cookie` is server-only and must only be emitted
|
|
@@ -309,6 +311,12 @@ The exported `isActiveOperator` is the shared admission predicate.
|
|
|
309
311
|
|
|
310
312
|
## Mail, OIDC, password and review seams
|
|
311
313
|
|
|
314
|
+
Invitation links default to seven days (604800 seconds). `invitationSeconds`
|
|
315
|
+
can shorten that lifetime, up to the seven-day maximum. `linkSeconds` remains
|
|
316
|
+
independent: sign-in and step-up links default to 15 minutes and allow at most
|
|
317
|
+
one hour. Invitation mail and confirmation pages describe the longer lifetime
|
|
318
|
+
in days; single use, cancellation and issuer-authority checks still apply.
|
|
319
|
+
|
|
312
320
|
Magic and invitation mail include both plain text and HTML. Each part has exactly
|
|
313
321
|
one confirmation URL; the HTML part presents it as a button with inline mail-safe
|
|
314
322
|
styling. The body sets Arial; its centered main content has a 480px maximum width
|
|
@@ -28,6 +28,7 @@ export function configure(input) {
|
|
|
28
28
|
allowList: input.allowList === undefined ? undefined : input.allowList.map(normalizeEmail),
|
|
29
29
|
sessionSeconds: seconds(input.sessionSeconds, 7 * 86400, 30 * 86400),
|
|
30
30
|
linkSeconds: seconds(input.linkSeconds, 900, 3600),
|
|
31
|
+
invitationSeconds: seconds(input.invitationSeconds, 7 * 86400, 7 * 86400),
|
|
31
32
|
reviewSeconds: seconds(input.reviewSeconds, 3600, 3600),
|
|
32
33
|
limitWindowSeconds: seconds(input.limitWindowSeconds, 900, 86400),
|
|
33
34
|
ipLimit: seconds(input.ipLimit, 30, 10000), emailLimit: seconds(input.emailLimit, 5, 1000),
|
|
@@ -35,7 +35,7 @@ export async function checkConfirmation(response, { kind, productName, minutes =
|
|
|
35
35
|
expect(html.includes(escape(productName)), 'escaped product name');
|
|
36
36
|
if (valid) {
|
|
37
37
|
expect(html.includes(`<h1>${kind === 'invite' ? 'Accept your invitation to' : 'Confirm sign-in to'} ${escape(productName)}</h1>`), 'plain confirmation heading');
|
|
38
|
-
expect(html.includes(`This link works once and expires in ${minutes} minutes.`), 'preview lifetime');
|
|
38
|
+
expect(html.includes(`This link works once and expires in ${kind === 'invite' ? '7 days' : `${minutes} minutes`}.`), 'preview lifetime');
|
|
39
39
|
expect(html.includes('<button type="submit">Confirm</button>'), 'explicit confirmation control');
|
|
40
40
|
expect(html.includes(`<input type="hidden" name="token" value="${token}">`) && html.split(token).length === 2, 'token appears only in hidden field');
|
|
41
41
|
} else {
|
|
@@ -47,7 +47,7 @@ export function checkMail(message, { kind, productName, token }) {
|
|
|
47
47
|
expect(message.subject === (kind === 'invite' ? `You're invited to ${productName}` : `Your sign-in link for ${productName}`), 'plain mail subject');
|
|
48
48
|
expect(!message.subject.includes(token), 'subject contains no credential');
|
|
49
49
|
for (const part of [message.text, message.html]) {
|
|
50
|
-
expect(typeof part === 'string' && part.includes(
|
|
50
|
+
expect(typeof part === 'string' && part.includes(`This link works once and expires in ${kind === 'invite' ? '7 days' : '15 minutes'}.`), 'multipart expiry');
|
|
51
51
|
expect(part.split(token).length === 2 && (part.match(/https?:\/\//g) ?? []).length === 1, 'one confirmation URL per part');
|
|
52
52
|
}
|
|
53
53
|
expect((message.html.match(/<a /g) ?? []).length === 1 && message.html.includes('>Confirm</a>'), 'one confirmation mail button');
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { escapeHtml } from '../../http.mjs';
|
|
2
2
|
import { pageResponse } from '../../page.mjs';
|
|
3
|
+
import { linkExpiry } from './expiry.mjs';
|
|
3
4
|
|
|
4
5
|
const HEADINGS = { invite: 'Accept your invitation to', magic: 'Confirm sign-in to', step_up: "Confirm it's you in", owner: 'Sign in as the owner of' };
|
|
5
6
|
|
|
@@ -7,7 +8,7 @@ export async function confirmationResponse({ branding, preview, token, method, a
|
|
|
7
8
|
const product = escapeHtml(branding.productName);
|
|
8
9
|
const heading = preview ? `${HEADINGS[preview.kind]} ${product}` : 'This link is invalid or expired';
|
|
9
10
|
const content = preview
|
|
10
|
-
? `<p>This link works once and expires in ${preview.expiresInMinutes}
|
|
11
|
+
? `<p>This link works once and expires in ${linkExpiry(preview.expiresInMinutes)}.</p><form method="post" action="${escapeHtml(action)}"><input type="hidden" name="token" value="${escapeHtml(token)}"><button type="submit">Confirm</button></form>`
|
|
11
12
|
: `<p>This link may have expired or already been used. Request a new link to continue.</p><a class="action" href="${escapeHtml(branding.signInPath)}">Request a new link</a>`;
|
|
12
13
|
// Native form POSTs need their Origin; never disclose the token path/query.
|
|
13
14
|
return pageResponse({ branding, heading, content, status: preview ? 200 : 400, method, referrer: preview ? 'strict-origin' : undefined });
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
// Mail and confirmation pages describe the same configured/remaining lifetime.
|
|
2
|
+
export function linkExpiry(minutes) {
|
|
3
|
+
if (minutes >= 1440) {
|
|
4
|
+
const days = Math.ceil(minutes / 1440);
|
|
5
|
+
return `${days} ${days === 1 ? 'day' : 'days'}`;
|
|
6
|
+
}
|
|
7
|
+
return `${minutes} ${minutes === 1 ? 'minute' : 'minutes'}`;
|
|
8
|
+
}
|
|
@@ -36,7 +36,8 @@ export function createEmailLinkLifecycle({ config: input, clock = Date.now, mail
|
|
|
36
36
|
}
|
|
37
37
|
async function newToken(kind, returnTo) {
|
|
38
38
|
const token = opaqueToken();
|
|
39
|
-
|
|
39
|
+
const lifetime = kind === 'invite' ? config.invitationSeconds : config.linkSeconds;
|
|
40
|
+
return { id: crypto.randomUUID(), token, hash: await hash('link', token), kind, returnTo, expiresAt: clock() + lifetime * 1000 };
|
|
40
41
|
}
|
|
41
42
|
// The email link: the app's own proof that a person holds an address.
|
|
42
43
|
async function sendLink(email, token, revoke = () => store.revokeLink(token.id, clock())) {
|
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import { accentInk } from '../../branding.mjs';
|
|
2
2
|
import { escapeHtml } from '../../http.mjs';
|
|
3
|
+
import { linkExpiry } from './expiry.mjs';
|
|
3
4
|
|
|
4
5
|
export function linkMessage(config, token, url) {
|
|
5
6
|
const { productName, accentColor } = config.branding;
|
|
6
7
|
const invitation = token.kind === 'invite', stepUp = token.kind === 'step_up';
|
|
7
8
|
const action = invitation ? 'accept your invitation to' : stepUp ? 'confirm a sensitive change in' : 'sign in to';
|
|
8
|
-
const minutes = config.linkSeconds / 60;
|
|
9
|
-
const expiry = `This link works once and expires in ${minutes}
|
|
9
|
+
const minutes = (invitation ? config.invitationSeconds : config.linkSeconds) / 60;
|
|
10
|
+
const expiry = `This link works once and expires in ${linkExpiry(minutes)}.`;
|
|
10
11
|
const heading = invitation ? `Accept your invitation to ${productName}` : stepUp ? `Confirm it's you in ${productName}` : `Confirm sign-in to ${productName}`;
|
|
11
12
|
return {
|
|
12
13
|
subject: invitation ? `You're invited to ${productName}` : stepUp ? `Confirm it's you in ${productName}` : `Your sign-in link for ${productName}`,
|
|
@@ -26,7 +26,7 @@ export function knowsKey(jwks, kid) {
|
|
|
26
26
|
|
|
27
27
|
export async function verifyIdToken(parsed, jwks, link, provider, settings, transaction, tokens, now) {
|
|
28
28
|
check(!Object.hasOwn(parsed.claims, 'events'), 'oidc_invalid_id_token');
|
|
29
|
-
const {
|
|
29
|
+
const { claims } = parsed;
|
|
30
30
|
await verifySignature(parsed, jwks);
|
|
31
31
|
// The token's issuer is the pinned issuer, and its audience is this client.
|
|
32
32
|
check(claims.iss === link.issuer && typeof claims.sub === 'string' && claims.sub.length > 0 && claims.sub.length <= 255, 'oidc_issuer_mismatch');
|
|
@@ -46,6 +46,8 @@ export interface AuthConfig {
|
|
|
46
46
|
origin: string; OWNER_EMAIL: string; SESSION_SECRET: string; SETUP_TOKEN?: string; MAIL_FROM?: string;
|
|
47
47
|
/** Local HTTP is allowed only for literal localhost, 127.0.0.1 or [::1] with matching Host. */
|
|
48
48
|
local?: boolean; allowList?: string[]; sessionSeconds?: number; linkSeconds?: number;
|
|
49
|
+
/** Invitation lifetime in seconds, up to seven days (the default); independent of sign-in/step-up links. */
|
|
50
|
+
invitationSeconds?: number;
|
|
49
51
|
review?: { enabled: boolean; operatorEmail: string }; reviewSeconds?: number;
|
|
50
52
|
limitWindowSeconds?: number; ipLimit?: number; emailLimit?: number;
|
|
51
53
|
/** Issuers the original admitVerifiedIdentity/createIdentityAdmission seams accept. */
|
|
@@ -301,8 +301,12 @@ transactions remain atomic.
|
|
|
301
301
|
download streams completed jobs. Abort removes snapshots and stored parts.
|
|
302
302
|
|
|
303
303
|
`imports.create(manifest,actor?)` creates staging under one durable import lock.
|
|
304
|
-
Upload parts strictly in order.
|
|
305
|
-
|
|
304
|
+
Upload parts strictly in order. Each upload stages rows and archived JSON in
|
|
305
|
+
batches bounded by D1 transport bytes, checkpointing each batch atomically.
|
|
306
|
+
A same-index/same-hash retry resumes an interrupted part or acknowledges a finished
|
|
307
|
+
part; different content returns 409. Reassembled bytes pass the part checksum
|
|
308
|
+
before completion advances. Staged data stays private until finalization.
|
|
309
|
+
Finalize requires the closing `archiveHash` and verifies all
|
|
306
310
|
parts, semantic contents, destination
|
|
307
311
|
emptiness, erasure and product invariants. R2 ownership is recorded after the
|
|
308
312
|
empty-namespace check. Verified object writes precede one atomic D1 batch, with
|
|
@@ -439,6 +443,7 @@ modules/portability/migration-statements.mjs
|
|
|
439
443
|
modules/portability/legacy-safety.mjs
|
|
440
444
|
modules/portability/export.mjs
|
|
441
445
|
modules/portability/import.mjs
|
|
446
|
+
modules/portability/import-staging.mjs
|
|
442
447
|
modules/portability/payloads.mjs
|
|
443
448
|
modules/portability/journal.mjs
|
|
444
449
|
modules/portability/http.mjs
|
|
@@ -25,7 +25,7 @@ export async function appliedMigrations(db) {
|
|
|
25
25
|
// earlier prefix. Restored rows never pass through a later migration, so one
|
|
26
26
|
// that writes or rewrites restorable data is refused, whether or not it changes
|
|
27
27
|
// the schema.
|
|
28
|
-
async function placement(
|
|
28
|
+
async function placement(manifest,cat,replay,applied) {
|
|
29
29
|
insist(canonicalJson(replay.names)===canonicalJson(applied) && await replay.schemaAfter(applied)===cat.identity,'schema_mismatch');
|
|
30
30
|
let earlier,unapplied;
|
|
31
31
|
if(manifest.migrations) {
|
|
@@ -62,7 +62,7 @@ export async function sameSchemaMigrations(db,manifest,cat,migrations) {
|
|
|
62
62
|
if(!applied)return;
|
|
63
63
|
insist(!manifest.migrations || manifest.migrations.every(name=>applied.includes(name)),'schema_mismatch:migrations');
|
|
64
64
|
if(!migrations){insist(!manifest.migrations || applied.every(name=>manifest.migrations.includes(name)),'schema_mismatch:migrations');return;}
|
|
65
|
-
await placement(
|
|
65
|
+
await placement(manifest,cat,await migrations(),applied);
|
|
66
66
|
}
|
|
67
67
|
// An archive made before later migrations restores only when those migrations
|
|
68
68
|
// are additive: new tables, new columns that take a default, or indexes (which
|
|
@@ -71,7 +71,7 @@ export async function additiveBridge(db,manifest,cat,migrations) {
|
|
|
71
71
|
insist(migrations,'schema_mismatch');
|
|
72
72
|
const replay=await migrations(),applied=await appliedMigrations(db)??[];
|
|
73
73
|
insist(!manifest.migrations || manifest.migrations.every(name=>applied.includes(name)),'schema_mismatch:migrations');
|
|
74
|
-
const {earlier}=await placement(
|
|
74
|
+
const {earlier}=await placement(manifest,cat,replay,applied);
|
|
75
75
|
const columns={};
|
|
76
76
|
for(const table of manifest.tables) {
|
|
77
77
|
const target=cat.tables.find(t=>t.name===table.name),label=':'+table.name;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { all, insist, quote
|
|
1
|
+
import { all, insist, quote } from './storage.mjs';
|
|
2
2
|
import { canonicalJson, sha256, base64, unbase64, utf8 } from './hash.mjs';
|
|
3
3
|
/** Tables no archive carries: SQLite/Cloudflare internals, migration ledgers,
|
|
4
4
|
* portability bookkeeping, development outboxes, operator identity and dependency
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { RPC_LIMIT, rpcSize, rpcSizeBytes } from '../d1-transport/index.mjs';
|
|
2
|
+
import { canonicalJson } from './hash.mjs';
|
|
3
|
+
import { statement, save, readPart, insist } from './storage.mjs';
|
|
4
|
+
import { checkPart } from './format.mjs';
|
|
5
|
+
|
|
6
|
+
const combine=(a,b)=>({utf8:a.utf8+b.utf8-2,units:a.units+b.units-2,hasWide:a.hasWide||b.hasWide});
|
|
7
|
+
const guardSql=`INSERT INTO rer_portability_guard(value) SELECT CASE WHEN (
|
|
8
|
+
EXISTS(SELECT 1 FROM rer_portability_jobs WHERE id=? AND phase='staging' AND completed=? AND metadata=?)
|
|
9
|
+
AND (SELECT import_job FROM rer_portability_state WHERE id=1)=?
|
|
10
|
+
) THEN 1 ELSE 0 END`;
|
|
11
|
+
|
|
12
|
+
// Rows and the archived JSON share the same durable cursor. Each checkpoint
|
|
13
|
+
// commits with its writes; retries cannot duplicate rows, even after a lost ACK.
|
|
14
|
+
// Product tables remain untouched until finalizeImport's atomic publication.
|
|
15
|
+
export async function stagePart(context,row,part,queries,stagedCounts) {
|
|
16
|
+
const {db,config}=context;
|
|
17
|
+
const pending=row.metadata.pendingPart;
|
|
18
|
+
insist(!pending || (pending.index===part.index && pending.sha256===part.sha256),'part_conflict',409);
|
|
19
|
+
let next=pending?.next??0;
|
|
20
|
+
while(next<queries.length) {
|
|
21
|
+
const before=canonicalJson(row.metadata),chunk=[];
|
|
22
|
+
// Reserve the largest cursor for this part before admitting any data.
|
|
23
|
+
row.metadata.pendingPart={index:part.index,sha256:part.sha256,next:queries.length};
|
|
24
|
+
const guard={sql:guardSql,params:[row.id,row.completed,before,row.id]};
|
|
25
|
+
const checkpoint={sql:'UPDATE rer_portability_jobs SET phase=?,completed=?,metadata=? WHERE id=?',params:[row.phase,row.completed,canonicalJson(row.metadata),row.id]};
|
|
26
|
+
let size=rpcSize([guard,checkpoint,{sql:'DELETE FROM rer_portability_guard'}]),end=next;
|
|
27
|
+
for(;end<queries.length;end++) {
|
|
28
|
+
const candidate=combine(size,rpcSize([queries[end]]));
|
|
29
|
+
if(rpcSizeBytes(candidate)>RPC_LIMIT)break;
|
|
30
|
+
size=candidate;chunk.push(queries[end]);
|
|
31
|
+
}
|
|
32
|
+
insist(end>next,'D1_RPC_TRANSPORT_LIMIT',413);
|
|
33
|
+
row.metadata.pendingPart.next=end;
|
|
34
|
+
await db.batch([statement(db,guard.sql,...guard.params),...chunk.map(q=>statement(db,q.sql,...q.params)),save(db,row),db.prepare('DELETE FROM rer_portability_guard')]);
|
|
35
|
+
next=end;
|
|
36
|
+
}
|
|
37
|
+
// Authenticate the reassembled stored bytes before acknowledging the part.
|
|
38
|
+
const stored=await readPart(db,row.id,part.index);
|
|
39
|
+
await checkPart(stored,part.index,config.partBytes);insist(stored.sha256===part.sha256,'part_checksum');
|
|
40
|
+
const before=canonicalJson(row.metadata),completed=row.completed;
|
|
41
|
+
delete row.metadata.pendingPart;row.metadata.stagedCounts=stagedCounts;row.completed++;
|
|
42
|
+
await db.batch([statement(db,guardSql,row.id,completed,before,row.id),
|
|
43
|
+
statement(db,'INSERT INTO rer_portability_parts VALUES(?,?,?)',row.id,part.index,part.sha256),
|
|
44
|
+
save(db,row),db.prepare('DELETE FROM rer_portability_guard')]);
|
|
45
|
+
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { all, statement, state, guard, job, save,
|
|
1
|
+
import { all, statement, state, guard, job, save, partQueries, parts, readPart, cleanCopies, quote, stream, insist, run } from './storage.mjs';
|
|
2
|
+
import { stagePart } from './import-staging.mjs';
|
|
2
3
|
import { refuseProviderData } from './provider-data.mjs';
|
|
3
4
|
import { refuseDevData } from './dev-data.mjs';
|
|
4
5
|
import { catalogue, validateRow, bindCell, writableColumns } from './catalogue.mjs';
|
|
@@ -69,20 +70,19 @@ export async function uploadPart(context,id,index,part) {
|
|
|
69
70
|
insist(entry && item.offset+unbase64(item.data).length<=entry.size,'binary_inventory');
|
|
70
71
|
}
|
|
71
72
|
}
|
|
72
|
-
|
|
73
|
+
const stagedCounts=Object.assign(Object.create(null),row.metadata.stagedCounts);
|
|
73
74
|
for(const [table,group] of groups) {
|
|
74
75
|
const width=group.rows[0].length,count=Math.min(30,Math.floor(100/width));
|
|
75
76
|
insist(count>0,'unsupported_restore_width:'+table);
|
|
76
|
-
const copy=row.metadata.copies[table],first=(
|
|
77
|
+
const copy=row.metadata.copies[table],first=(stagedCounts[table]??0)+1;
|
|
77
78
|
for(let start=0;start<group.rows.length;start+=count) {
|
|
78
79
|
const chunk=group.rows.slice(start,start+count);
|
|
79
|
-
inserts.push(
|
|
80
|
-
inserts.push(
|
|
80
|
+
inserts.push({sql:`INSERT INTO ${quote(copy)} (${group.columns}) VALUES${chunk.map(()=> '('+Array(width).fill('?').join(',')+')').join(',')}`,params:chunk.flat()});
|
|
81
|
+
inserts.push({sql:`INSERT INTO ${quote(sidecar(copy))} VALUES${chunk.map((_,i)=>`(?,${first+start+i},?,?)`).join(',')}`,params:group.metadata.slice(start,start+count).flat()});
|
|
81
82
|
}
|
|
82
|
-
|
|
83
|
+
stagedCounts[table]=first-1+group.rows.length;
|
|
83
84
|
}
|
|
84
|
-
|
|
85
|
-
try{await db.batch([guard(db,'(SELECT completed FROM rer_portability_jobs WHERE id=?)=?',id,before),...inserts,...partStatements(db,id,part),save(db,row),db.prepare('DELETE FROM rer_portability_guard')]);}
|
|
85
|
+
try{await stagePart(context,row,part,[...inserts,...partQueries(id,part).slice(1)],stagedCounts);}
|
|
86
86
|
catch(e){const duplicate=await statement(db,'SELECT sha256 FROM rer_portability_parts WHERE job=? AND idx=?',id,index).first();if(!duplicate || duplicate.sha256!==part.sha256)throw e;}
|
|
87
87
|
return importStatus(context,id);
|
|
88
88
|
}
|
|
@@ -12,7 +12,7 @@ export async function recoverLegacyArchive({db,buckets,policy:input,migrations},
|
|
|
12
12
|
insist(typeof output==='string' && output!==path,'legacy_output_required',409);
|
|
13
13
|
insist(!(await lstat(output).catch(error=>{if(error.code==='ENOENT')return null;throw error;})),'legacy_output_exists',409);
|
|
14
14
|
// Whole-file verification precedes even staging/audit schema writes.
|
|
15
|
-
for await(const
|
|
15
|
+
for await(const _record of fileRecords(path,readLegacyArchive)){}
|
|
16
16
|
const context={db,buckets,config:policy(input),migrations},target=await legacyTargetState(db,migrations);
|
|
17
17
|
await requireLegacyEmpty(context,target);
|
|
18
18
|
let id,manifest,closing,integrity,status,complete=false;
|
|
@@ -45,16 +45,16 @@ export async function legacyTargetState(db,migrations) {
|
|
|
45
45
|
}
|
|
46
46
|
return {emptyTables:names.filter(name=>!seeded.some(s=>s.name===name)),seededTables:seeded};
|
|
47
47
|
}
|
|
48
|
-
const seededCondition=
|
|
48
|
+
const seededCondition=table=>[`(SELECT count(*) FROM ${quote(table.name)})=? AND NOT EXISTS(SELECT 1 FROM (${imageSql(table.name,table.columns)}) WHERE image NOT IN (SELECT value FROM json_each(?)))`,table.images.length,JSON.stringify(table.images)];
|
|
49
49
|
export async function requireLegacyEmpty({db,buckets},{emptyTables,seededTables=[]}) {
|
|
50
50
|
for(const table of emptyTables)insist(!(await statement(db,`SELECT 1 FROM ${quote(table)} LIMIT 1`).first()),'legacy_target_not_empty:'+table,409);
|
|
51
|
-
for(const table of seededTables){const [condition,...values]=seededCondition(
|
|
51
|
+
for(const table of seededTables){const [condition,...values]=seededCondition(table);insist((await statement(db,`SELECT CASE WHEN (${condition}) THEN 1 ELSE 0 END AS ok`,...values).first()).ok===1,'legacy_target_not_empty:'+table.name,409);}
|
|
52
52
|
for(const [name,bucket] of Object.entries(buckets))insist(!(await bucket.list({limit:1})).objects.length,'legacy_bucket_not_empty:'+name,409);
|
|
53
53
|
}
|
|
54
54
|
export function legacyEmptyGuards(db,row) {
|
|
55
55
|
const recovery=row.metadata.legacyRecovery;
|
|
56
56
|
return [...(recovery?.emptyTables??[]).map(table=>guard(db,`NOT EXISTS(SELECT 1 FROM ${quote(table)})`)),
|
|
57
|
-
...(recovery?.seededTables??[]).map(table=>{const [condition,...values]=seededCondition(
|
|
57
|
+
...(recovery?.seededTables??[]).map(table=>{const [condition,...values]=seededCondition(table);return guard(db,condition,...values);})];
|
|
58
58
|
}
|
|
59
59
|
export async function ensureLegacyAudit(db) {
|
|
60
60
|
// Additive sidecar, installed only by recovery; existing product migrations
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { run, all, insist } from './storage.mjs';
|
|
2
2
|
import { base64, unbase64, StreamHash } from './hash.mjs';
|
|
3
3
|
export async function* byteChunks(source,limit) {
|
|
4
4
|
const reader=(source instanceof ReadableStream?source:new Response(source).body).getReader();
|
|
@@ -26,12 +26,15 @@ export function save(db,row) {
|
|
|
26
26
|
return statement(db,'UPDATE rer_portability_jobs SET phase=?,completed=?,metadata=? WHERE id=?',row.phase,row.completed,canonicalJson(row.metadata),row.id);
|
|
27
27
|
}
|
|
28
28
|
export function partStatements(db,id,part) {
|
|
29
|
-
|
|
29
|
+
return partQueries(id,part).map(({sql,params})=>statement(db,sql,...params));
|
|
30
|
+
}
|
|
31
|
+
export function partQueries(id,part) {
|
|
32
|
+
const json=canonicalJson(part), result=[{sql:'INSERT INTO rer_portability_parts VALUES(?,?,?)',params:[id,part.index,part.sha256]}];
|
|
30
33
|
for(let i=0,segment=0;i<json.length;segment++) {
|
|
31
34
|
let end=Math.min(i+32000,json.length);
|
|
32
35
|
const last=json.charCodeAt(end-1),next=json.charCodeAt(end);
|
|
33
36
|
if(last>=0xd800 && last<=0xdbff && next>=0xdc00 && next<=0xdfff) end--;
|
|
34
|
-
result.push(
|
|
37
|
+
result.push({sql:'INSERT INTO rer_portability_segments VALUES(?,?,?,?)',params:[id,part.index,segment,json.slice(i,end)]});i=end;
|
|
35
38
|
}
|
|
36
39
|
return result;
|
|
37
40
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { mkdtemp,rm } from 'node:fs/promises';
|
|
2
2
|
import { tmpdir } from 'node:os';
|
|
3
3
|
import { shared } from '../../cli/shared.mjs';
|
|
4
|
-
import { execFileSync
|
|
4
|
+
import { execFileSync } from 'node:child_process';
|
|
5
5
|
import { realpathSync, existsSync, lstatSync, mkdirSync } from 'node:fs';
|
|
6
6
|
import { resolve, dirname, relative, isAbsolute } from 'node:path';
|
|
7
7
|
import { stripVTControlCharacters } from 'node:util';
|