@runeditrun/rer 0.2.43 → 0.2.45
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 +7 -1
- package/cli/README.md +11 -1
- package/cli/business/command.mjs +1 -1
- package/cli/business/keychain.mjs +1 -1
- package/cli/business/lease.mjs +1 -1
- package/cli/business/questions.mjs +2 -2
- package/cli/contract.mjs +1 -1
- package/cli/deploy-readiness.mjs +2 -2
- package/cli/deploy.mjs +18 -5
- package/cli/local.mjs +6 -2
- package/cli/parse.mjs +9 -3
- package/cli/port.mjs +10 -11
- package/cli/server-limit-file.mjs +21 -0
- package/cli/server-registry.mjs +13 -6
- package/cli/server-resources.mjs +13 -2
- package/global/CHANGELOG.md +17 -0
- package/global/README.md +7 -1
- package/global/launch/capacity.mjs +9 -4
- package/global/launch/facts.mjs +11 -19
- package/modules/demo-host/README.md +3 -0
- package/modules/demo-host/build.mjs +3 -1
- package/modules/demo-host/native/config.mjs +2 -0
- package/modules/demo-host/native/prepare.mjs +3 -1
- package/modules/demo-host/native/warm.mjs +49 -0
- package/modules/demo-host/native/worker.mjs +4 -2
- package/modules/demo-host/pages.mjs +6 -1
- package/modules/dependencies/README.md +5 -2
- package/modules/dependencies/index.mjs +4 -3
- package/modules/dependencies/protocol.mjs +7 -5
- package/modules/deploy-kit/README.md +16 -1
- package/modules/deploy-kit/deploy.mjs +5 -3
- package/modules/deploy-kit/diagnostics.mjs +8 -0
- package/modules/deploy-kit/migration-ledger.mjs +28 -8
- package/modules/deploy-kit/migration-retry.mjs +32 -16
- package/modules/deploy-kit/migrations.mjs +10 -4
- package/modules/deploy-kit/owner-cli.mjs +9 -6
- package/modules/deploy-kit/owner-secret-copy.mjs +38 -0
- package/modules/deploy-kit/owner-secret-names.mjs +1 -1
- package/modules/deploy-kit/owner-secret.mjs +39 -18
- package/modules/deploy-kit/owner.mjs +25 -15
- package/modules/deploy-kit/provider.mjs +20 -3
- package/modules/operator-auth/identity/oidc/config.mjs +10 -8
- package/modules/operator-auth/signin/index.mjs +3 -3
- package/modules/release/catalogue.mjs +1 -1
- package/onboarding/port-probe.mjs +23 -1
- package/onboarding/process-diagnostics.mjs +3 -2
- package/onboarding/schema/setup-validation.mjs +1 -1
- package/onboarding/theme.mjs +1 -1
- package/onboarding/ui/app.js +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { readdir } from 'node:fs/promises';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { pathToFileURL } from 'node:url';
|
|
4
|
+
import { gzipSync } from 'node:zlib';
|
|
5
|
+
import { HASHED_ASSET } from './config.mjs';
|
|
6
|
+
|
|
7
|
+
// The start page prefetches the console's hashed JS and CSS while the visitor
|
|
8
|
+
// waits for the human check and sign-in, so each view's first visit finds its
|
|
9
|
+
// chunks in the browser's cache, and the colo holds them for the next visitor.
|
|
10
|
+
// At most WARM_BYTES of gzip-compressed transfer, the console first: the
|
|
11
|
+
// root and console layout, the console's own views (what its nav links to),
|
|
12
|
+
// its deeper views, the other routes, then the remaining chunks.
|
|
13
|
+
export const WARM_BYTES = 1024 * 1024;
|
|
14
|
+
|
|
15
|
+
const segments = path => path.split('/').filter(Boolean);
|
|
16
|
+
const dynamic = path => segments(path).filter(part => part.startsWith('$')).length;
|
|
17
|
+
const within = (path, root) => path === root || path.startsWith(root.endsWith('/') ? root : root + '/');
|
|
18
|
+
|
|
19
|
+
// TanStack Start's route manifest from the server build: each route's CSS and
|
|
20
|
+
// the chunks it preloads.
|
|
21
|
+
async function startRoutes(server) {
|
|
22
|
+
const directory = join(server, 'assets');
|
|
23
|
+
const file = (await readdir(directory).catch(() => [])).find(name => /^_tanstack-start-manifest_v-[\w-]+\.js$/.test(name));
|
|
24
|
+
if (!file) return {};
|
|
25
|
+
const { tsrStartManifest } = await import(pathToFileURL(join(directory, file)));
|
|
26
|
+
return tsrStartManifest().routes ?? {};
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function routeOrder(routes, consolePath) {
|
|
30
|
+
const ids = Object.keys(routes).filter(id => id !== '__root__');
|
|
31
|
+
const views = id => within(id, consolePath) && id !== consolePath;
|
|
32
|
+
const nav = id => views(id) && !dynamic(id) && segments(id).length === segments(consolePath).length + 1 && !id.endsWith('_');
|
|
33
|
+
const rank = id => [consolePath, consolePath + '/'].includes(id) ? 0 : nav(id) ? 1 : views(id) ? 2 : 3;
|
|
34
|
+
const sorted = ids.sort((a, b) => rank(a) - rank(b) || dynamic(a) - dynamic(b) || segments(a).length - segments(b).length || a.localeCompare(b));
|
|
35
|
+
return ['__root__', ...sorted].filter(id => routes[id]);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** The ordered, size-capped paths the start page prefetches, from dist/client's files and the server build. */
|
|
39
|
+
export async function warmList(files, server, consolePath = '/app') {
|
|
40
|
+
const sizes = new Map(files.map(file => ['/' + file.path, file]).filter(([path]) => HASHED_ASSET.test(path) && /\.(js|css)$/.test(path))
|
|
41
|
+
.map(([path, file]) => [path, gzipSync(Buffer.from(file.base64, 'base64')).length]));
|
|
42
|
+
const routes = await startRoutes(server);
|
|
43
|
+
const ordered = new Set();
|
|
44
|
+
for (const id of routeOrder(routes, consolePath)) for (const path of [...(routes[id].css ?? []), ...(routes[id].preloads ?? [])]) if (sizes.has(path)) ordered.add(path);
|
|
45
|
+
for (const path of [...sizes.keys()].sort((a, b) => sizes.get(a) - sizes.get(b))) ordered.add(path);
|
|
46
|
+
const warm = []; let bytes = 0;
|
|
47
|
+
for (const path of ordered) if (bytes + sizes.get(path) <= WARM_BYTES) { warm.push(path); bytes += sizes.get(path); }
|
|
48
|
+
return { paths: warm, bytes, of: sizes.size };
|
|
49
|
+
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import front from '../worker.mjs';
|
|
2
2
|
import { bundle } from './loader.mjs';
|
|
3
|
+
import { HASHED_ASSET } from './config.mjs';
|
|
3
4
|
export { HostBridge } from './bridge.mjs';
|
|
4
5
|
export { DemoAssets, DemoEgress } from './loader.mjs';
|
|
5
6
|
export { NativeDemo } from './visitor.mjs';
|
|
@@ -13,10 +14,11 @@ export default {
|
|
|
13
14
|
if (['GET', 'HEAD'].includes(request.method) && assets.has(url.pathname)) {
|
|
14
15
|
const response = await env.ASSETS.fetch(request);
|
|
15
16
|
const headers = new Headers(response.headers);
|
|
16
|
-
if (
|
|
17
|
+
if (HASHED_ASSET.test(url.pathname)) headers.set('cache-control', 'public, max-age=31536000, immutable');
|
|
17
18
|
headers.set('x-rer-demo-assets', 'edge');
|
|
18
19
|
return new Response(response.body, { status: response.status, headers });
|
|
19
20
|
}
|
|
20
|
-
|
|
21
|
+
// The start page prefetches the console's chunks while the visitor waits.
|
|
22
|
+
return front.fetch(request, { ...env, DEMO_WARM: bundle.warm }, ctx);
|
|
21
23
|
},
|
|
22
24
|
};
|
|
@@ -24,10 +24,15 @@ ol.steps{display:none;list-style:none;margin:22px auto 0;padding:0;width:max-con
|
|
|
24
24
|
@media(min-width:600px) and (max-width:1023px){main{width:calc(100% - 64px);max-width:666px;margin:100px auto 48px}.card{padding:36px}h1{font-size:38px;line-height:46px}.intro{font-size:18px;line-height:27px}}
|
|
25
25
|
@media(max-width:599px){main,.ended main{width:calc(100% - 32px);margin:32px auto}.ended .card{padding:26px 22px}.ended h1{font-size:29px;line-height:36px}.ended .intro{font-size:17px;line-height:25px;margin-bottom:28px}.card{padding:26px 22px;border-radius:16px}.brand{font-size:16px}h1{font-size:34px;line-height:41px;letter-spacing:-.8px}.intro{font-size:17px;line-height:25px}ul{font-size:16px;line-height:25px;margin:22px 0 26px}li{padding-left:22px;margin-bottom:9px}li:before{left:2px;top:9px}.button{font-size:18px;margin-top:22px}.note{font-size:13px;line-height:20px}.steps{font-size:15px}.back{margin-top:24px;font-size:14px}}`;
|
|
26
26
|
|
|
27
|
+
// The console's chunks, fetched at idle priority while the visitor waits for
|
|
28
|
+
// the check and sign-in (native/warm.mjs). The browser ignores a failed one,
|
|
29
|
+
// and one with prefetching turned off fetches none.
|
|
30
|
+
const warm = env => (env.DEMO_WARM ?? []).map(path => `<link rel="prefetch" href="${escape(path)}">`).join('');
|
|
31
|
+
|
|
27
32
|
function html(env, { page, title, body, status = 200, headers = {}, turnstile = false, refresh = false }) {
|
|
28
33
|
const nonce = crypto.randomUUID().replaceAll('-', ''), site = new URL(env.DEMO_SITE);
|
|
29
34
|
const policy = `default-src 'none'; style-src 'nonce-${nonce}'; img-src 'self'; form-action 'self'; base-uri 'none'; frame-ancestors 'none'${turnstile ? `; script-src 'nonce-${nonce}' https://challenges.cloudflare.com; frame-src https://challenges.cloudflare.com; connect-src 'self' https://challenges.cloudflare.com` : ''}`;
|
|
30
|
-
return new Response(`<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><meta name="robots" content="noindex"><title>${escape(title)}</title>${refresh ? '<noscript><meta http-equiv="refresh" content="2;url=/__demo/status"></noscript>' : ''}<style nonce="${nonce}">${STYLE}</style>${turnstile ? `<noscript><style nonce="${nonce}">#note{display:none}</style></noscript><script nonce="${nonce}">${START_SCRIPT}</script><script nonce="${nonce}" src="https://challenges.cloudflare.com/turnstile/v0/api.js" async defer></script
|
|
35
|
+
return new Response(`<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><meta name="robots" content="noindex"><title>${escape(title)}</title>${refresh ? '<noscript><meta http-equiv="refresh" content="2;url=/__demo/status"></noscript>' : ''}<style nonce="${nonce}">${STYLE}</style>${turnstile ? `<noscript><style nonce="${nonce}">#note{display:none}</style></noscript><script nonce="${nonce}">${START_SCRIPT}</script><script nonce="${nonce}" src="https://challenges.cloudflare.com/turnstile/v0/api.js" async defer></script>${warm(env)}` : ''}</head>
|
|
31
36
|
<body class="${page}"><main><section class="card" aria-labelledby="title"><div class="mark" aria-hidden="true"></div><p class="brand">${escape(env.DEMO_NAME)}</p>${body}</section><nav class="back" aria-label="Return"><a href="${escape(site.origin)}/">Back to ${escape(site.host)}</a></nav></main></body></html>`,
|
|
32
37
|
{ status, headers: { 'content-type': 'text/html; charset=utf-8', 'cache-control': 'no-store', 'content-security-policy': policy, 'referrer-policy': 'strict-origin', 'x-content-type-options': 'nosniff', ...headers } });
|
|
33
38
|
}
|
|
@@ -22,8 +22,11 @@ The unqualified JWKS URL selects the identity interface. The application retains
|
|
|
22
22
|
its private key in the server database. Sector/install identity survives re-link,
|
|
23
23
|
provider moves and export/import. Keys are isolated per interface.
|
|
24
24
|
|
|
25
|
-
Provider origins and metadata endpoints require HTTPS.
|
|
26
|
-
|
|
25
|
+
Provider origins and metadata endpoints require HTTPS. Under `rer dev` (the
|
|
26
|
+
`local` option, which needs a loopback app origin) a provider may instead be
|
|
27
|
+
another locally running seed at `http://127.0.0.1`, `http://[::1]` or
|
|
28
|
+
`http://localhost` on any port, so two local seeds can link; every other `http`
|
|
29
|
+
address and every hosted deploy still requires HTTPS.
|
|
27
30
|
Every provider serves `/.well-known/rer-dependencies` with the same graph fields,
|
|
28
31
|
including its `provides` list and selected dependency provider origins. The CLI
|
|
29
32
|
traverses these documents before setup effects, including selected optional
|
|
@@ -2,13 +2,14 @@ import { nativeDependencyAuthority } from './native.mjs';
|
|
|
2
2
|
import { approveSetup, approveOwner, approvalFence, callbacksFor } from './approval.mjs';
|
|
3
3
|
import { dependencyStore } from './store.mjs';
|
|
4
4
|
import { publicKey, assertion, digest } from './crypto.mjs';
|
|
5
|
+
import { isLoopbackHost } from '../operator-auth/loopback.mjs';
|
|
5
6
|
import { CALLBACK, INTERFACE, PATHS, NETWORK, insist, originOf, providerMetadata } from './protocol.mjs';
|
|
6
7
|
import { readJson } from '../operator-auth/identity/oidc/network.mjs';
|
|
7
8
|
import { readInput } from '../operator-auth/input.mjs';
|
|
8
9
|
import { ownerFence, pinStatement } from './authority.mjs';
|
|
9
10
|
export { INTERFACE, CALLBACK, PATHS } from './protocol.mjs';
|
|
10
11
|
export function createDependencies({db,origin,name,provider,interfaces=[{interface:INTERFACE,provider,paths:PATHS}],provides=[],local=false,auth,claimOwner,clock=Date.now,fetch:fetcher=globalThis.fetch}) {
|
|
11
|
-
originOf(origin,local);insist(Array.isArray(provides) && new Set(provides).size===provides.length && provides.every(value=>/^[a-z][a-z0-9-]*@[1-9][0-9]*$/.test(value)));
|
|
12
|
+
originOf(origin,local);insist(!local || isLoopbackHost(new URL(origin).hostname));insist(Array.isArray(provides) && new Set(provides).size===provides.length && provides.every(value=>/^[a-z][a-z0-9-]*@[1-9][0-9]*$/.test(value)));
|
|
12
13
|
insist(Array.isArray(interfaces) && new Set(interfaces.map(d=>d.interface)).size===interfaces.length && interfaces.every(d=>/^[a-z][a-z0-9-]*@[1-9][0-9]*$/.test(d.interface) && Array.isArray(d.paths) && new Set(d.paths).size===d.paths.length && d.paths.every(p=>typeof p==='string' && /^\/[A-Za-z0-9_~/-]+$/.test(p) && !p.startsWith('//'))));
|
|
13
14
|
const store=dependencyStore(db);
|
|
14
15
|
const native=auth?.native?nativeDependencyAuthority(auth.accounts.dependencies):null;
|
|
@@ -34,7 +35,7 @@ export function createDependencies({db,origin,name,provider,interfaces=[{interfa
|
|
|
34
35
|
const pendingPin=native?null:await store.first('SELECT provider_id AS id,issuer,client_id AS clientId,linked_at AS linkedAt FROM oa_provider_session_pins WHERE session_id=?',pending.approved_session);
|
|
35
36
|
const initialApproval=await approvalFence(pending,contract,expected,callbacks,clock(),pendingPin,native);
|
|
36
37
|
insist(await store.first(`SELECT 1 WHERE ${initialApproval.sql}`,...initialApproval.args),'local_owner_proof_required',403);
|
|
37
|
-
const metadata=await providerMetadata(expected,fetcher,contract),installation=await store.installation(contract);
|
|
38
|
+
const metadata=await providerMetadata(expected,fetcher,contract,local),installation=await store.installation(contract);
|
|
38
39
|
const codeDigest=await digest(expected+'\0'+data.code);
|
|
39
40
|
const consumed=await store.first('INSERT INTO rer_dependency_codes(digest,created_at) VALUES(?,?) ON CONFLICT DO NOTHING RETURNING digest',codeDigest,clock());
|
|
40
41
|
insist(consumed?.digest===codeDigest,'link_code_used',409);
|
|
@@ -101,7 +102,7 @@ export function createDependencies({db,origin,name,provider,interfaces=[{interfa
|
|
|
101
102
|
insist(request.headers.get('origin')===origin,'invalid_link_origin',403);
|
|
102
103
|
if(path==='/auth/dependencies/unlink')return await unlink(request);
|
|
103
104
|
if(path==='/auth/dependencies/relink' || path==='/auth/dependencies/setup') {
|
|
104
|
-
const input=await readInput(request),contract=input.interface??INTERFACE;originOf(input.issuer);const declared=interfaces.find(d=>d.interface===contract);insist(declared);
|
|
105
|
+
const input=await readInput(request),contract=input.interface??INTERFACE;originOf(input.issuer,local);const declared=interfaces.find(d=>d.interface===contract);insist(declared);
|
|
105
106
|
const callbacks=callbacksFor(origin,declared);
|
|
106
107
|
if(path==='/auth/dependencies/setup') await approveSetup({db,auth,request,input,contract,callbacks,clock});
|
|
107
108
|
else await approveOwner({store,actor:await owner(request,'providers:relink'),input,contract,callbacks,clock,native});
|
|
@@ -6,19 +6,21 @@ export const CALLBACK = '/.well-known/rer-link/callback';
|
|
|
6
6
|
export const PATHS = ['/auth/oidc/auth/callback','/auth/oidc/auth/logout'];
|
|
7
7
|
export const NETWORK = { timeoutMs:5000 };
|
|
8
8
|
export function insist(value, code = 'invalid_link', status = 400) { if (!value) throw Object.assign(new Error(code),{code,status}); }
|
|
9
|
+
// https always; plain http only to a loopback host while the app runs under `rer dev`.
|
|
10
|
+
const schemeAllowed=(u,local)=>u.protocol==='https:' || local && u.protocol==='http:' && isLoopbackHost(u.hostname);
|
|
9
11
|
export function originOf(value, local = false) {
|
|
10
12
|
let u;try { u=new URL(value); } catch { insist(false); }
|
|
11
|
-
insist(u.origin===value && !u.username && !u.password && (u
|
|
13
|
+
insist(u.origin===value && !u.username && !u.password && schemeAllowed(u,local));
|
|
12
14
|
return u.origin;
|
|
13
15
|
}
|
|
14
|
-
export async function providerMetadata(issuer, fetcher, contract = INTERFACE) {
|
|
15
|
-
originOf(issuer);
|
|
16
|
-
const p={...PRESETS.auth,id:'auth',issuer,endpointOrigins:[issuer],tokenAuthMethod:'private_key_jwt',keyAlgorithm:'ES256',minimumVersion:MINIMUM_PROVIDER_VERSION};
|
|
16
|
+
export async function providerMetadata(issuer, fetcher, contract = INTERFACE, local = false) {
|
|
17
|
+
originOf(issuer,local);
|
|
18
|
+
const p={...PRESETS.auth,id:'auth',issuer,endpointOrigins:[issuer],local,tokenAuthMethod:'private_key_jwt',keyAlgorithm:'ES256',minimumVersion:MINIMUM_PROVIDER_VERSION};
|
|
17
19
|
const oidc=contract===INTERFACE?await discover(fetcher,p,NETWORK):{};
|
|
18
20
|
const link=await readJson(fetcher,`${issuer}/.well-known/rer-provider`,NETWORK);
|
|
19
21
|
insist(link.issuer===issuer && (link.interface===contract || Array.isArray(link.provides) && new Set(link.provides).size===link.provides.length && link.provides.every(i=>typeof i==='string' && /^[a-z][a-z0-9-]*@[1-9][0-9]*$/.test(i)) && link.provides.includes(contract)) && link.version===1,'unsupported_link_interface',502);
|
|
20
22
|
for (const name of ['initiate','redeem','deregister']) {
|
|
21
|
-
const u=new URL(link[name]);insist(u.origin===issuer && u
|
|
23
|
+
const u=new URL(link[name]);insist(u.origin===issuer && schemeAllowed(u,local) && !u.search && !u.hash && !u.username && !u.password,'untrusted_link_endpoint',502);
|
|
22
24
|
}
|
|
23
25
|
return {...oidc,link};
|
|
24
26
|
}
|
|
@@ -117,6 +117,7 @@ A transport failure carries `providerTransport`: the operation, sanitized endpoi
|
|
|
117
117
|
|
|
118
118
|
- **Inputs.** Declared secrets come from the environment, then `--env-file FILE` (an owned 0600 file of `KEY=VALUE` lines), which wins. For a secret the Worker does not hold yet, rer supplies the setup manifest's owner email (`emailField`) from the local setup's private settings, and for each secret field the manifest generates (`generate`) the value rer keeps for this environment in `.rer/deploy/<environment>.secrets.env` (0600, ignored by Git), else a fresh random value, which rer keeps once the Worker holds it (the one-time setup token is always fresh and never kept); a value the Worker already holds is kept. The build runs with the checkout's local settings files moved aside, and a `dist/` holding a `.dev.vars*`, `.env*` or `*.env` file is refused by name before any provider request. A still-missing secret stops the run before any write with a message that names it, unless its declaration says `"optional": true`: then the deploy binds nothing for it and reports it as not set, and `--set-secret` adds it later.
|
|
119
119
|
- **Hosted owner.** After deploy and health pass, rer claims the setup owner on the hosted app: it POSTs the setup token to the seed's standard owner step (`hooks.owner`, by default `/auth/bootstrap`). 303 means claimed; `409 already_initialized` means an owner was already claimed and is kept. Then rer deletes the token secret from the Worker, records the new Worker version in the state file, and lists the token in `retiredSecrets`, so later plans no longer declare it. The build fingerprint still names it, and the recorded claim stays as recorded, so an untouched repeat deploy writes nothing. A Worker that still holds a token rer did not upload in this run (an older deploy) first gets a fresh token through the same recorded secret change. Dependency approvals that need the token run before the claim. A seed with an owner step function, an identity-provider claim, or no declared token secret claims its owner its own way.
|
|
120
|
+
- **Several secrets at once.** `rer deploy --set-secret MAIL_API_KEY --set-secret MAIL_FROM` or `rer deploy --set-secret MAIL_API_KEY,MAIL_FROM` (likewise `--unset-secret A,B`) changes all of them in one new Worker version or none: a group of secrets is never half set. Every named secret must be declared and, for `--set-secret`, have its value in the environment or `--env-file`; otherwise rer names each missing one and changes nothing. One secret uses Cloudflare's secret call. Several are one copy of the live version through the Versions API (its own modules, assets and compatibility settings, every other binding inherited), then deployed at 100%: Cloudflare's per-secret calls each make a version, and its settings call can add secrets but not remove them (measured 17 October 2026). The copy carries a marker rer records first, so a copy whose answer was lost is recognised, and one uploaded but not deployed is deployed by the same command run again.
|
|
120
121
|
- **Secret changes.** `rer deploy --set-secret NAME` (value from the environment or `--env-file`, under the declaration's environment name), `--unset-secret NAME` and `--rotate-secret NAME[,NAME]` (a fresh value for a secret rer generates, kept in `.rer/deploy/<environment>.secrets.env` once set) change declared secrets of the recorded Worker without a build. Before writing, rer records the starting version and the exact live names of all other secrets, never their values. Accepted or ambiguous writes retain that intent until readback confirms the change; explicit refusals clear it. An ambiguous failure, or a later command's preflight, can clear an unapplied intent only when the live deployment still matches the receipt and the newest listed version matches it too. A surviving intent blocks further mutations until consistency returns; retry the same command then. Recovery requires the recorded immediate successor (or the recorded confirming rotation's successor), the receipt's script etag and fingerprint, the intended binding's presence, and an unchanged set of other secret names. Ambiguous rotations require a confirming PUT before reporting success. Older intents without names may recover only from the exact starting version's historical bindings, with its id, etag and fingerprint verified; unavailable or malformed historical bindings refuse adoption. Each historical binding must have an exact provider type supported by this kit; unrecognized types cannot prove that a name was not a secret. Added, removed or renamed other secrets refuse deploy and removal before writes. Values remain opaque: these checks cannot distinguish an otherwise identical outside change to the pending binding.
|
|
121
122
|
- **Removal.** `rer deploy --remove --yes` deletes what the state file says rer created for this environment: the Worker (schedules first; its Durable Objects go with it) and each D1, R2, KV and queue that is still the one rer created, after checking all of them before the first delete. A replaced or uncertain resource refuses the whole removal, except a D1 or KV whose create answer was lost: under this environment's derived name it is deleted once confirmed empty (a D1 with no tables, a KV namespace with no keys); one holding data is refused as `uncertain-resource-not-empty`, and an uncertain queue, whose backlog rer cannot read, as `uncertain-resource-unverifiable` (delete it by hand if it is the empty one from the interrupted run).
|
|
122
123
|
|
|
@@ -202,7 +203,7 @@ No Single Redirect (dynamic redirect ruleset) permission is needed. Base's refer
|
|
|
202
203
|
|
|
203
204
|
SQL files have ordered unique names and SHA-256 identities. `d1_migrations` holds controller history with ordered IDs, names, timestamps, SHA-256 hashes and states. Native archives already exclude this installation table. Standard Wrangler name-only inserts remain possible, but their missing hashes/states block subsequent Base deployment pending reviewed adoption. Applied entries must be an unchanged prefix of the desired set. A new migration gets an `applying` claim before its SQL and completion marker are submitted together through the [Cloudflare D1 SQL import API](https://developers.cloudflare.com/api/resources/d1/subresources/database/methods/import/), using the same whole-file protocol as Wrangler to preserve compound trigger bodies and atomic SQL/completion. Uploads use bounded HTTPS R2 destinations, without account credentials; polling is bounded and signed URLs are never diagnostic output. A failed import keeps Cloudflare's own codes and messages on the error (`providerRefusal`, which `rer deploy` prints, with credentials and URLs removed and each message cut to 300 characters) and names the migration it was running.
|
|
204
205
|
|
|
205
|
-
An import that did not complete leaves its `applying` claim. The next deploy of the same migration bytes runs it again only when it proves none of it applied (`migration-retry.mjs`): the import is atomic, so the migration's effects that would make a second run fail (a CREATE without IF NOT EXISTS, ADD COLUMN, RENAME) and that last to the end of the file must all be absent. Then no attempt committed, and one that commits late makes the re-run fail whole instead of applying twice. A migration with no such effect, or with any of them present, stays refused as `uncertain-migration-claim`; the message names the migration, what is present, and the exact step: undo what is present and deploy again, or confirm by hand and delete or complete its claim with `wrangler d1 execute`. The kit never re-runs a migration whose effects may be present, rolls back user data, or marks an unknown outcome successful. Any other unconfirmed or changed history is still `changed-or-uncertain-migration-history`. Readback checks every final identity.
|
|
206
|
+
An import that did not complete leaves its `applying` claim. The next deploy of the same migration bytes runs it again only when it proves none of it applied (`migration-retry.mjs`): the import is atomic, so the migration's effects that would make a second run fail (a CREATE without IF NOT EXISTS, ADD COLUMN, RENAME) and that last to the end of the file must all be absent. Then no attempt committed, and one that commits late makes the re-run fail whole instead of applying twice. A name the file drops or renames away existed before it ran, so a table it rebuilds under the same name, and that table's indexes, are not checked. A migration with no such effect, or with any of them present, stays refused as `uncertain-migration-claim`; the message names the migration, what is present, and the exact step: undo what is present and deploy again, or confirm by hand and delete or complete its claim with `wrangler d1 execute`. The kit never re-runs a migration whose effects may be present, rolls back user data, or marks an unknown outcome successful. Any other unconfirmed or changed history is still `changed-or-uncertain-migration-history`. Readback checks every final identity.
|
|
206
207
|
|
|
207
208
|
A seed that had to rewrite a migration it already released declares the rewrite
|
|
208
209
|
in `migrations/rewrites.json`: `{ "version": 1, "rewrites": [{ "migration":
|
|
@@ -215,6 +216,20 @@ migration in the set, and `to` is that migration's current bytes; every other
|
|
|
215
216
|
change to applied history is still refused. Keep the record for as long as any
|
|
216
217
|
installation may hold the released digest.
|
|
217
218
|
|
|
219
|
+
An `applying` claim on `from`, left when D1 rolled back an import of the released
|
|
220
|
+
bytes, is retried with the current bytes by `rer deploy`, which runs migrations
|
|
221
|
+
inside its `<state file>.lock` (One run at a time), so no other deploy of that
|
|
222
|
+
install is running. The same proof as above must hold for the current bytes, and the
|
|
223
|
+
completion marker moves the claim to the current digest only as that import
|
|
224
|
+
commits. The deploy prints `Migrations: retried <name>: the earlier attempt was
|
|
225
|
+
rolled back (D1 <database>).` A caller without that lock keeps the refusal. An
|
|
226
|
+
`applying` claim on any other bytes stays `changed-or-uncertain-migration-history`,
|
|
227
|
+
and the message gives the `wrangler d1 execute ... DELETE FROM d1_migrations WHERE
|
|
228
|
+
name=... AND sha256=... AND state='applying'` command with the database and
|
|
229
|
+
migration filled in. It is safe once that attempt has ended and no other deploy of
|
|
230
|
+
the environment is running: the claim and the migration commit together, so a claim
|
|
231
|
+
still `applying` means none of it applied.
|
|
232
|
+
|
|
218
233
|
Completion preserves the original SQL bytes and always starts on a new line.
|
|
219
234
|
Comment-only migrations are rejected before any provider request or ledger claim.
|
|
220
235
|
The controller adds a separator only when the final non-comment token is not a
|
|
@@ -15,8 +15,10 @@ import { verifyHealth } from './health.mjs';
|
|
|
15
15
|
* mode creates owned resources itself first, then passes false. `uploadWithoutReceipt`
|
|
16
16
|
* re-uploads an identical build when no receipt survived, instead of refusing the no-op.
|
|
17
17
|
* `declaredSecrets` (default `plan.secrets`) are the secrets the fingerprint names: owner mode
|
|
18
|
-
* passes the declaration's, so retiring the spent setup token leaves the build unchanged.
|
|
19
|
-
|
|
18
|
+
* passes the declaration's, so retiring the spent setup token leaves the build unchanged.
|
|
19
|
+
* `locked`: the caller holds the install's deploy lock (owner mode), so migrate may retry a
|
|
20
|
+
* claim on bytes a reviewed rewrite replaced. */
|
|
21
|
+
export async function deploy({api,plan,artifact,artifactSha256,previousReceipt,repeatOnly=false,createResources=!previousReceipt,uploadWithoutReceipt=false,onReceipt,onHealth,readiness={},env={},fetch=globalThis.fetch,healthOptions={},declaredSecrets=plan.secrets,locked=false}) {
|
|
20
22
|
validatePlan(plan); validateArtifact(artifact);
|
|
21
23
|
await verifyServiceGrants(api, plan);
|
|
22
24
|
for (const item of plan.d1) validateMigrations(item.migrations);
|
|
@@ -43,7 +45,7 @@ export async function deploy({api,plan,artifact,artifactSha256,previousReceipt,r
|
|
|
43
45
|
}
|
|
44
46
|
const migrations = [];
|
|
45
47
|
// A migration failure names its database, so the owner knows which one to inspect.
|
|
46
|
-
for (const item of plan.d1) migrations.push({binding:item.binding,...await migrate(api,resources.d1.find(x => x.binding === item.binding).id,item.migrations,{apply:!unchanged,upgrade:true}).catch(error => { error.resource ??= `d1:${item.name}`; throw error; })});
|
|
48
|
+
for (const item of plan.d1) migrations.push({binding:item.binding,...await migrate(api,resources.d1.find(x => x.binding === item.binding).id,item.migrations,{apply:!unchanged,upgrade:true,locked}).catch(error => { error.resource ??= `d1:${item.name}`; throw error; })});
|
|
47
49
|
const window = readiness.window ?? readinessWindow({...(healthOptions.now ? {clock:healthOptions.now} : {}), ...(healthOptions.sleep ? {sleep:healthOptions.sleep} : {}), ...readiness});
|
|
48
50
|
let upload;
|
|
49
51
|
if (!unchanged) { window.start(); upload = await uploadWorker(api,{plan,artifact,bindings,secrets,migration,fingerprint}); }
|
|
@@ -40,6 +40,14 @@ export function migrationFailure(error, again = 'rer deploy') {
|
|
|
40
40
|
? `Some of its changes are present (${present.join(', ')}). Undo them so the database is as it was before ${name}, then run ${again} again: rer checks again and runs it.`
|
|
41
41
|
: `It creates no table, index, view, trigger or column, so rer cannot check whether it applied. If none of its changes are present, delete its claim with ${run('DELETE FROM d1_migrations')}, then run ${again} again.`} If it did apply in full, record that instead with ${run("UPDATE d1_migrations SET state='applied'")}.`;
|
|
42
42
|
}
|
|
43
|
+
const unconfirmed = error?.unconfirmedClaim;
|
|
44
|
+
if (typeof unconfirmed?.name === 'string' && /^[a-f0-9]{64}$/.test(unconfirmed.sha256 ?? '')) {
|
|
45
|
+
const name = safe(unconfirmed.name).replaceAll(' ','?');
|
|
46
|
+
const run = `npx wrangler d1 execute ${database ?? '<database>'} --remote --command "DELETE FROM d1_migrations WHERE name='${name}' AND sha256='${unconfirmed.sha256}' AND state='applying'"`;
|
|
47
|
+
return `Migration ${name} in ${database ? `D1 ${database}` : 'its D1 database'} was started on bytes this build does not have (SHA-256 ${unconfirmed.sha256.slice(0,12)}) and never confirmed. ${unconfirmed.rewritten
|
|
48
|
+
? `A reviewed rewrite in migrations/rewrites.json replaces those bytes; rer deploy retries it under its deploy lock, and this run does not hold one.`
|
|
49
|
+
: `No reviewed rewrite in migrations/rewrites.json replaces those bytes, so rer will not run it again.`} D1 imports a migration and its completion marker as one atomic file, so a claim still applying after that attempt ended means none of it applied. Deleting the claim is safe once that attempt has ended (it failed, or its run was stopped) and no other deploy of this environment is running: ${run}, then run ${again} again. While a deploy may still be running, leave it.`;
|
|
50
|
+
}
|
|
43
51
|
if (typeof error?.migration === 'string') return `Migration ${safe(error.migration)} did not apply; the next ${again} runs it again once rer proves none of it applied.`;
|
|
44
52
|
return undefined;
|
|
45
53
|
}
|
|
@@ -44,10 +44,26 @@ export const recorded = (row, migration) => row.name === migration?.name && (row
|
|
|
44
44
|
const validPrefix = (rows, migrations) => rows.every((row, index) => recorded(row, migrations[index]) && row.state === 'applied');
|
|
45
45
|
const same = (a, b) => a.length === b.length && a.every((row, index) => row.name === b[index].name && row.sha256 === b[index].sha256 && row.state === b[index].state);
|
|
46
46
|
|
|
47
|
-
|
|
47
|
+
/** History this build does not match. When it ends in an unconfirmed claim after a
|
|
48
|
+
* valid prefix, the error names that claim (`unconfirmedClaim`) so the refusal can
|
|
49
|
+
* give the exact command that clears it; `rewritten` says a reviewed rewrite
|
|
50
|
+
* replaced the claimed bytes and only a deploy holding the install's lock retries it. */
|
|
51
|
+
function unmatched(rows, migrations) {
|
|
52
|
+
const error = new Error('changed-or-uncertain-migration-history'), last = rows.at(-1);
|
|
53
|
+
if (last?.state === 'applying' && /^[a-f0-9]{64}$/.test(last.sha256 ?? '') && validPrefix(rows.slice(0, -1), migrations)) {
|
|
54
|
+
const next = migrations[rows.length - 1];
|
|
55
|
+
error.unconfirmedClaim = { name:last.name, sha256:last.sha256, rewritten:last.name === next?.name && (next.rewrites ?? []).some(rewrite => rewrite.from === last.sha256) };
|
|
56
|
+
}
|
|
57
|
+
return error;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** `rewritten`: the caller holds the install's deploy lock (owner.mjs withStateLock),
|
|
61
|
+
* so no other deploy of it is running; a claim on bytes a reviewed rewrite replaced
|
|
62
|
+
* is then this build's to retry. */
|
|
63
|
+
export async function readLedger(api, id, migrations, query, { rewritten = false } = {}) {
|
|
48
64
|
const read = async sql => (await query(api, id, sql))[0].results;
|
|
49
65
|
const tables = (await read(`SELECT name FROM sqlite_master WHERE type='table' AND name IN ('${legacy}','${ledger}')`)).map(row => row.name);
|
|
50
|
-
let old = [], applied = [], transition = '', claimed = false;
|
|
66
|
+
let old = [], applied = [], transition = '', claimed = false, claimedFrom;
|
|
51
67
|
if (tables.includes(legacy)) {
|
|
52
68
|
const columns = await read(`SELECT name FROM pragma_table_info('${legacy}')`);
|
|
53
69
|
assert(columns.length === 3 && columns.every(row => ['name','sha256','state'].includes(row.name)), 'invalid-migration-ledger');
|
|
@@ -59,11 +75,15 @@ export async function readLedger(api, id, migrations, query) {
|
|
|
59
75
|
assert(['id','name','applied_at'].every(name => columns.includes(name)), 'invalid-migration-ledger');
|
|
60
76
|
if (columns.includes('sha256') && columns.includes('state')) {
|
|
61
77
|
applied = await read(`SELECT name, sha256, state FROM ${ledger} ORDER BY id`);
|
|
62
|
-
// The last row may claim this build's next migration
|
|
63
|
-
//
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
78
|
+
// The last row may claim this build's next migration whose import did not
|
|
79
|
+
// complete: its exact bytes, or (`rewritten`) bytes a reviewed rewrite replaced.
|
|
80
|
+
// migrate re-runs it only on proof (migration-retry.mjs).
|
|
81
|
+
const last = applied.at(-1), next = migrations[applied.length - 1];
|
|
82
|
+
if (!tables.includes(legacy) && last?.state === 'applying' && last.name === next?.name) {
|
|
83
|
+
if (last.sha256 === next.sha256) claimed = Boolean(applied.pop());
|
|
84
|
+
else if (rewritten && (next.rewrites ?? []).some(rewrite => rewrite.from === last.sha256)) { claimedFrom = last.sha256; claimed = Boolean(applied.pop()); }
|
|
85
|
+
}
|
|
86
|
+
if (!validPrefix(applied, migrations)) throw unmatched(applied, migrations);
|
|
67
87
|
if (tables.includes(legacy)) assert(same(applied, old), 'changed-or-uncertain-migration-history');
|
|
68
88
|
} else {
|
|
69
89
|
assert(!columns.includes('sha256') && !columns.includes('state'), 'invalid-migration-ledger');
|
|
@@ -82,7 +102,7 @@ export async function readLedger(api, id, migrations, query) {
|
|
|
82
102
|
applied = old;
|
|
83
103
|
}
|
|
84
104
|
if (tables.includes(legacy)) transition += `DROP TABLE ${legacy};`;
|
|
85
|
-
return { applied, transition, claimed, exists:tables.includes(ledger) || tables.includes(legacy) };
|
|
105
|
+
return { applied, transition, claimed, claimedFrom, exists:tables.includes(ledger) || tables.includes(legacy) };
|
|
86
106
|
}
|
|
87
107
|
function insert(row) {
|
|
88
108
|
return `INSERT INTO ${ledger}(name,sha256,state) VALUES (${literal(row.name)},${literal(row.sha256)},'applied');\n`;
|
|
@@ -8,6 +8,8 @@ import { literal } from './migration-ledger.mjs';
|
|
|
8
8
|
// end of the file proves it: its effect absent now means no attempt committed, and
|
|
9
9
|
// an earlier attempt that commits late makes the re-run fail whole instead of
|
|
10
10
|
// applying twice. Every such effect must be absent; any present one is refused.
|
|
11
|
+
// A name the file drops or renames away existed before it ran, so an object it
|
|
12
|
+
// makes again under that name (a rebuilt table, its indexes) proves nothing.
|
|
11
13
|
|
|
12
14
|
const word = token => token?.kind === 'word' ? token.value.toUpperCase() : '';
|
|
13
15
|
const lower = token => token?.kind === 'word' || token?.kind === 'identifier' ? token.value.toLowerCase() : undefined;
|
|
@@ -43,11 +45,13 @@ function create(statement) {
|
|
|
43
45
|
/** The effects of a migration whose absence proves it never applied. */
|
|
44
46
|
export function retryGuards(sql) {
|
|
45
47
|
let guards = [];
|
|
48
|
+
const before = new Set();
|
|
49
|
+
const guard = item => { if (!before.has(item.object) && !before.has(item.table) && !before.has(`${item.table}.${item.column}`)) guards.push(item); };
|
|
46
50
|
for (const statement of statements(sql)) {
|
|
47
51
|
const first = word(statement[0]);
|
|
48
52
|
if (first === 'CREATE') {
|
|
49
|
-
const
|
|
50
|
-
if (
|
|
53
|
+
const created = create(statement);
|
|
54
|
+
if (created) guard(created);
|
|
51
55
|
} else if (first === 'ALTER' && word(statement[1]) === 'TABLE') {
|
|
52
56
|
const { name:table, next } = named(statement, 2);
|
|
53
57
|
let index = next;
|
|
@@ -55,21 +59,26 @@ export function retryGuards(sql) {
|
|
|
55
59
|
if (word(statement[index]) === 'COLUMN') index++;
|
|
56
60
|
const column = lower(statement[index]);
|
|
57
61
|
if (!table) guards = [];
|
|
58
|
-
else if (action === 'ADD' && column)
|
|
62
|
+
else if (action === 'ADD' && column) guard({ table, column });
|
|
59
63
|
else if (action === 'RENAME' && word(statement[next + 1]) === 'TO') {
|
|
60
64
|
const to = named(statement, next + 2).name;
|
|
61
|
-
guards = guards.filter(
|
|
62
|
-
|
|
65
|
+
guards = guards.filter(item => item.table !== table);
|
|
66
|
+
before.add(table);
|
|
67
|
+
if (to) guard({ type:'table', object:to, table:to });
|
|
63
68
|
} else if (action === 'RENAME' && column && word(statement[index + 1]) === 'TO' && lower(statement[index + 2])) {
|
|
64
|
-
guards = guards.filter(
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
else
|
|
69
|
+
guards = guards.filter(item => !(item.table === table && item.column === column));
|
|
70
|
+
before.add(`${table}.${column}`);
|
|
71
|
+
guard({ table, column:lower(statement[index + 2]) });
|
|
72
|
+
} else if (action === 'DROP' && column) {
|
|
73
|
+
guards = guards.filter(item => !(item.table === table && item.column === column));
|
|
74
|
+
before.add(`${table}.${column}`);
|
|
75
|
+
} else guards = [];
|
|
68
76
|
} else if (first === 'DROP') {
|
|
69
77
|
const type = word(statement[1]);
|
|
70
78
|
const { name } = named(statement, word(statement[2]) === 'IF' ? 4 : 2);
|
|
71
79
|
// A later drop removes the effect it would prove; an unreadable one proves nothing.
|
|
72
|
-
guards = name ? guards.filter(
|
|
80
|
+
guards = name ? guards.filter(item => item.object !== name && !(['TABLE', 'VIEW'].includes(type) && item.table === name)) : [];
|
|
81
|
+
if (name) before.add(name);
|
|
73
82
|
}
|
|
74
83
|
}
|
|
75
84
|
return guards;
|
|
@@ -78,14 +87,21 @@ export function retryGuards(sql) {
|
|
|
78
87
|
const label = guard => guard.column === undefined ? `${guard.type} ${guard.object}` : `column ${guard.table}.${guard.column}`;
|
|
79
88
|
|
|
80
89
|
/** Refuse unless the claimed migration's effects are provably absent: it then
|
|
81
|
-
* may run again. `read(sql)` returns one SELECT's rows.
|
|
90
|
+
* may run again. `read(sql)` returns one SELECT's rows. D1 accepts at most five
|
|
91
|
+
* terms in a compound SELECT, so objects and columns are each one plain query. */
|
|
82
92
|
export async function assertRetryable(read, migration) {
|
|
83
93
|
const guards = retryGuards(migration.sql), present = [];
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
94
|
+
const names = list => [...new Set(list)].map(literal).join(',');
|
|
95
|
+
const objects = guards.filter(guard => guard.column === undefined), columns = guards.filter(guard => guard.column !== undefined);
|
|
96
|
+
for (let start = 0; start < objects.length; start += 100) {
|
|
97
|
+
const part = objects.slice(start, start + 100);
|
|
98
|
+
const found = new Set((await read(`SELECT type, lower(name) AS name FROM sqlite_master WHERE lower(name) IN (${names(part.map(guard => guard.object))})`)).map(row => `${row.type} ${row.name}`));
|
|
99
|
+
for (const guard of part) if (found.has(label(guard))) present.push(label(guard));
|
|
100
|
+
}
|
|
101
|
+
for (let start = 0; start < columns.length; start += 100) {
|
|
102
|
+
const part = columns.slice(start, start + 100);
|
|
103
|
+
const found = new Set((await read(`SELECT lower(m.name) AS tbl, lower(p.name) AS col FROM sqlite_master AS m, pragma_table_info(m.name) AS p WHERE m.type='table' AND lower(m.name) IN (${names(part.map(guard => guard.table))})`)).map(row => `column ${row.tbl}.${row.col}`));
|
|
104
|
+
for (const guard of part) if (found.has(label(guard))) present.push(label(guard));
|
|
89
105
|
}
|
|
90
106
|
if (!guards.length || present.length) throw Object.assign(new Error('uncertain-migration-claim'), { migrationClaim:{ name:migration.name, present:[...new Set(present)] } });
|
|
91
107
|
}
|
|
@@ -53,9 +53,10 @@ export async function query(api, id, sql) {
|
|
|
53
53
|
assert(Array.isArray(rows) && rows.length > 0 && rows.every(x => x.success === true && Array.isArray(x.results)), 'd1-query-failed');
|
|
54
54
|
return rows;
|
|
55
55
|
}
|
|
56
|
-
|
|
56
|
+
/** `locked`: the caller holds the install's deploy lock (owner.mjs withStateLock). */
|
|
57
|
+
export async function migrate(api, id, migrations, { apply = true, upgrade = apply, locked = false } = {}) {
|
|
57
58
|
validateMigrations(migrations);
|
|
58
|
-
const { applied, transition, exists, claimed } = await readLedger(api, id, migrations, query);
|
|
59
|
+
const { applied, transition, exists, claimed, claimedFrom } = await readLedger(api, id, migrations, query, { rewritten:locked });
|
|
59
60
|
const pending = migrations.slice(applied.length);
|
|
60
61
|
assert(apply || pending.length === 0,'pending-migrations');
|
|
61
62
|
if (transition && upgrade) {
|
|
@@ -73,12 +74,17 @@ export async function migrate(api, id, migrations, { apply = true, upgrade = app
|
|
|
73
74
|
// A second terminator after trailing comments creates an empty D1 statement.
|
|
74
75
|
// Keep the newline even when terminated: a final line comment must end.
|
|
75
76
|
const separator = last?.kind === 'symbol' && last.value === ';' ? '' : '; ';
|
|
76
|
-
|
|
77
|
+
// A claim on bytes a reviewed rewrite replaced moves to these bytes only when this
|
|
78
|
+
// import commits; an earlier attempt that commits first leaves it applied on those.
|
|
79
|
+
const done = claimedFrom && index === 0
|
|
80
|
+
? `UPDATE ${ledger} SET sha256=${literal(migration.sha256)}, state='applied' WHERE name=${literal(migration.name)} AND sha256=${literal(claimedFrom)} AND state='applying'`
|
|
81
|
+
: `UPDATE ${ledger} SET state='applied' WHERE name=${literal(migration.name)} AND sha256=${literal(migration.sha256)}`;
|
|
82
|
+
await api.importSql(id,`${migration.sql}\n${separator}${done};`)
|
|
77
83
|
.catch(error => { error.migration ??= migration.name; throw error; });
|
|
78
84
|
}
|
|
79
85
|
if (pending.length) {
|
|
80
86
|
const final = (await query(api,id,`SELECT name, sha256, state FROM ${ledger} ORDER BY id`))[0].results;
|
|
81
87
|
assert(final.length === migrations.length && final.every((x,i) => x.state === 'applied' && recorded(x, migrations[i])), 'migration-readback-mismatch');
|
|
82
88
|
}
|
|
83
|
-
return { applied:pending.map(x => x.name), history:migrations.map(({name,sha256}) => ({name,sha256})) };
|
|
89
|
+
return { applied:pending.map(x => x.name), retried:claimed ? [pending[0].name] : [], history:migrations.map(({name,sha256}) => ({name,sha256})) };
|
|
84
90
|
}
|
|
@@ -17,7 +17,7 @@ import { refuseLocalModels } from './local-only.mjs';
|
|
|
17
17
|
import { publicOrigin } from './public-origin.mjs';
|
|
18
18
|
|
|
19
19
|
export const cronNotice = 'This Worker has a Cron Trigger. Cloudflare may take up to about two hours to start its first scheduled run on some accounts.';
|
|
20
|
-
const usage = 'usage: owner-cli.mjs deploy|health|remove|inspect|record-owner|signin --declaration <file> --environment <name> [--product <dir>] [--state <file>] [--outcome claimed|kept] | secret ... --set NAME|--unset NAME';
|
|
20
|
+
const usage = 'usage: owner-cli.mjs deploy|health|remove|inspect|record-owner|signin --declaration <file> --environment <name> [--product <dir>] [--state <file>] [--outcome claimed|kept] | secret ... --set NAME [--set NAME]|--unset NAME [--unset NAME]';
|
|
21
21
|
|
|
22
22
|
/** The owner's committed source and existing local build. Run the seed's build first. */
|
|
23
23
|
async function ownerArtifact(root, product) {
|
|
@@ -30,7 +30,7 @@ async function ownerArtifact(root, product) {
|
|
|
30
30
|
/** Inputs: CLOUDFLARE_ACCOUNT_ID and CLOUDFLARE_API_TOKEN, plus the secrets the declaration names on first deploy. */
|
|
31
31
|
export async function main(argv = process.argv.slice(2), env = process.env, {fetch = globalThis.fetch, cwd = process.cwd(), healthOptions, propagation, settle, localOnlyKeys = [], notice = line => console.error(line), onCreated} = {}) {
|
|
32
32
|
let parsed;
|
|
33
|
-
try { parsed = parseArgs({args:argv,allowPositionals:true,strict:true,options:{declaration:{type:'string'},environment:{type:'string'},product:{type:'string',default:''},state:{type:'string'},set:{type:'string'},unset:{type:'string'},outcome:{type:'string'}}}); }
|
|
33
|
+
try { parsed = parseArgs({args:argv,allowPositionals:true,strict:true,options:{declaration:{type:'string'},environment:{type:'string'},product:{type:'string',default:''},state:{type:'string'},set:{type:'string',multiple:true},unset:{type:'string',multiple:true},outcome:{type:'string'}}}); }
|
|
34
34
|
catch { throw new Error('usage'); }
|
|
35
35
|
const {positionals:[command,...rest],values} = parsed;
|
|
36
36
|
if (!['deploy','health','secret','remove','inspect','record-owner','signin'].includes(command) || rest.length || !values.declaration || !values.environment) throw new Error('usage');
|
|
@@ -66,10 +66,12 @@ export async function main(argv = process.argv.slice(2), env = process.env, {fet
|
|
|
66
66
|
const statePath = values.state ? resolve(root,values.state) : join(root,'.rer','deploy',`${target.environment}.json`);
|
|
67
67
|
if (command === 'remove') return {ok:true,...await ownerRemove({api,target,statePath}).catch(error => { throw api.annotateFailure(error,'remove'); })};
|
|
68
68
|
if (command === 'secret') {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
const
|
|
72
|
-
|
|
69
|
+
// Every named secret is changed in one Worker version, or none is.
|
|
70
|
+
const named = [...new Set(values.set ?? values.unset)], declared = Array.isArray(declaration.secrets) ? declaration.secrets : [];
|
|
71
|
+
const undeclared = named.filter(binding => !declared.some(entry => entry?.binding === binding));
|
|
72
|
+
if (undeclared.length) { const error = new Error('undeclared-secret'); error.resource = undeclared.map(binding => `secret:${binding}`).join(', '); throw error; }
|
|
73
|
+
const changes = named.map(binding => ({binding, value:values.set === undefined ? undefined : env[declared.find(entry => entry?.binding === binding).environment]}));
|
|
74
|
+
return {ok:true,...await ownerSecret({api,target,statePath,changes,fetch,healthOptions}).catch(error => { throw api.annotateFailure(error,'secrets'); })};
|
|
73
75
|
}
|
|
74
76
|
// A secret rer retired (the spent setup token) is no longer part of the plan.
|
|
75
77
|
const state = await settleOwnerState({api,target,statePath});
|
|
@@ -114,6 +116,7 @@ export async function main(argv = process.argv.slice(2), env = process.env, {fet
|
|
|
114
116
|
api.annotateFailure(error,error.message.startsWith('missing-secret-') ? 'secrets' : undefined);
|
|
115
117
|
throw error;
|
|
116
118
|
});
|
|
119
|
+
for (const {binding,retried = []} of receipt.migrations) for (const name of retried) notice(`Migrations: retried ${name}: the earlier attempt was rolled back (D1 ${plan.d1.find(item => item.binding === binding).name}).`);
|
|
117
120
|
// Optional secrets the Worker does not hold: never supplied, or removed with --unset.
|
|
118
121
|
const optionalUnset = declaredSecrets.filter(item => item?.optional && (unsetSecrets.includes(item.binding) || retiredSecrets.includes(item.binding))).map(item => item.binding);
|
|
119
122
|
const result = {ok:true,changed:receipt.changed,worker:target.worker,origin:receipt.origin,versionId:receipt.provider.versionId,created,sentSecrets,...(optionalUnset.length ? {optionalUnset} : {}),retiredSecrets,...(state.ownerClaim ? {ownerClaim:state.ownerClaim} : {}),state:statePath};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { assert, uuid } from './validation.mjs';
|
|
2
|
+
|
|
3
|
+
// Several secrets change as ONE Worker version, so a half-changed group never runs.
|
|
4
|
+
// Cloudflare's per-secret calls each deploy a version of their own, and its settings
|
|
5
|
+
// call adds secrets but never removes one (both measured on 17 October 2026). A copy
|
|
6
|
+
// of the live version does both: its own modules, assets and compatibility settings,
|
|
7
|
+
// every binding inherited except the named secrets, which it sets or leaves out. The
|
|
8
|
+
// copy carries a marker rer saved before uploading it, so a copy whose answer was lost,
|
|
9
|
+
// or that was uploaded but not yet deployed, is recognised as this change and no other.
|
|
10
|
+
export const copyMessage = marker => `rer-secrets:${marker}`;
|
|
11
|
+
|
|
12
|
+
/** Upload the copy (not yet deployed) and return its version id. */
|
|
13
|
+
export async function uploadSecretCopy(api, worker, live, {changes, marker}) {
|
|
14
|
+
const {parts, entry} = await api.workerContent(worker, live.versionId);
|
|
15
|
+
const runtime = live.version?.resources?.script_runtime, bindings = live.version?.resources?.bindings;
|
|
16
|
+
assert(typeof runtime?.compatibility_date === 'string' && Array.isArray(bindings), 'invalid-active-version');
|
|
17
|
+
const named = new Set(changes.map(change => change.binding));
|
|
18
|
+
const metadata = {main_module:entry, compatibility_date:runtime.compatibility_date, compatibility_flags:runtime.compatibility_flags ?? [],
|
|
19
|
+
bindings:[...bindings.filter(row => !named.has(row.name)).map(row => ({type:'inherit',name:row.name})),
|
|
20
|
+
...changes.filter(change => change.value !== undefined).map(change => ({type:'secret_text',name:change.binding,text:change.value}))],
|
|
21
|
+
keep_assets:true, annotations:{'workers/message':copyMessage(marker)}};
|
|
22
|
+
const data = new FormData();
|
|
23
|
+
data.set('metadata', new Blob([JSON.stringify(metadata)], {type:'application/json'}));
|
|
24
|
+
for (const [name, file] of parts) data.set(name, file, name);
|
|
25
|
+
const version = await api.request(`/workers/scripts/${worker}/versions?bindings_inherit=strict`, {method:'POST', body:data});
|
|
26
|
+
assert(uuid(version?.id), 'invalid-secret-copy');
|
|
27
|
+
return version.id;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Send all traffic to the copy. */
|
|
31
|
+
export const deploySecretCopy = (api, worker, versionId) => api.request(`/workers/scripts/${worker}/deployments`, {method:'POST', body:{strategy:'percentage', versions:[{version_id:versionId, percentage:100}]}});
|
|
32
|
+
|
|
33
|
+
/** The newest version is this change's copy, uploaded right after the receipt's version and not deployed. */
|
|
34
|
+
export async function undeployedCopy(api, worker, {pendingSecret, receipt}, versions) {
|
|
35
|
+
if (!pendingSecret.copy || versions[1]?.id !== receipt.provider.versionId || !uuid(versions[0]?.id)) return null;
|
|
36
|
+
const copy = await api.request(`/workers/scripts/${worker}/versions/${versions[0].id}`);
|
|
37
|
+
return copy?.id === versions[0].id && copy.annotations?.['workers/message'] === copyMessage(pendingSecret.copy) ? copy.id : null;
|
|
38
|
+
}
|
|
@@ -27,5 +27,5 @@ export async function pendingSecretNames(api, worker, {pendingSecret, receipt})
|
|
|
27
27
|
if (anchor?.id !== pendingSecret.versionId || anchor?.resources?.script?.etag !== receipt.provider.etag
|
|
28
28
|
|| !Array.isArray(bindings) || !bindings.every(row => row && typeof row === 'object' && !Array.isArray(row) && historicalBindingTypes.has(row.type))
|
|
29
29
|
|| !validSecretNames(bindings.map(row => row.name)) || deployedFingerprint({bindings}) !== receipt.fingerprint) return null;
|
|
30
|
-
return bindings.filter(row => row.type === 'secret_text' && row.name
|
|
30
|
+
return bindings.filter(row => row.type === 'secret_text' && !pendingSecret.bindings.includes(row.name)).map(row => row.name);
|
|
31
31
|
}
|