@runeditrun/rer 0.2.4 → 0.2.6
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 +3 -0
- package/cli/README.md +18 -7
- package/cli/contract.mjs +3 -0
- package/cli/deploy-command.mjs +30 -12
- package/cli/deploy-hook-record.mjs +32 -0
- package/cli/deploy-hook.mjs +31 -0
- package/cli/deploy-observation.mjs +4 -3
- package/cli/deploy-owner.mjs +11 -5
- package/cli/deploy-source.mjs +24 -0
- package/cli/deploy.mjs +7 -1
- package/cli/inspect.mjs +2 -2
- package/cli/lifecycle.mjs +15 -0
- package/cli/pnpm-probe.mjs +14 -7
- package/cli/project-pnpm.mjs +15 -7
- package/cli/setup-input.mjs +6 -4
- package/global/CHANGELOG.md +31 -0
- package/global/README.md +3 -0
- package/global/launch/facts.mjs +1 -6
- package/global/launch/prepare.mjs +4 -3
- package/global/launch/state.mjs +18 -18
- package/modules/container/README.md +3 -1
- package/modules/container/boundary.mjs +5 -2
- package/modules/deploy-kit/README.md +11 -8
- package/modules/deploy-kit/deploy.mjs +14 -5
- package/modules/deploy-kit/diagnostics.mjs +8 -0
- package/modules/deploy-kit/health-request.mjs +7 -5
- package/modules/deploy-kit/owner-cli.mjs +3 -1
- package/modules/deploy-kit/owner-remove.mjs +1 -0
- package/modules/deploy-kit/owner-secret-names.mjs +31 -0
- package/modules/deploy-kit/owner-secret.mjs +6 -3
- package/modules/deploy-kit/owner.mjs +22 -16
- package/modules/deploy-kit/provider.mjs +7 -0
- package/modules/job-queue/identity.mjs +3 -1
- package/modules/job-queue/policy.mjs +2 -1
- package/onboarding/adapter-settings.mjs +14 -5
- package/onboarding/local/owner.mjs +35 -10
- package/package.json +1 -1
- package/scripts/publish-check/publication-config.json +11 -1
package/global/launch/state.mjs
CHANGED
|
@@ -39,6 +39,19 @@ export async function liveWorker(directory) {
|
|
|
39
39
|
return await live(holder) ? holder : undefined;
|
|
40
40
|
}
|
|
41
41
|
|
|
42
|
+
// A claim file is written to a private file first and hard-linked into place,
|
|
43
|
+
// so it appears complete or not at all: a process finding it never reads a
|
|
44
|
+
// half-written claim as a dead one and removes it. False when one is there.
|
|
45
|
+
async function linkClaim(path, record) {
|
|
46
|
+
const written = `${path}.${process.pid}`;
|
|
47
|
+
await writeFile(written, JSON.stringify(record), { mode: 0o600 });
|
|
48
|
+
try {
|
|
49
|
+
await link(written, path);
|
|
50
|
+
return true;
|
|
51
|
+
} catch (error) { if (error.code !== 'EEXIST') throw error; return false; }
|
|
52
|
+
finally { await rm(written, { force: true }); }
|
|
53
|
+
}
|
|
54
|
+
|
|
42
55
|
// A port chosen for a setup stays claimed while the process that chose it
|
|
43
56
|
// lives, so two launches preparing at once never pick the same port.
|
|
44
57
|
export async function claimPort(port, env = process.env) {
|
|
@@ -46,10 +59,7 @@ export async function claimPort(port, env = process.env) {
|
|
|
46
59
|
await mkdir(directory, { recursive: true, mode: 0o700 });
|
|
47
60
|
const { start } = await processInfo(process.pid);
|
|
48
61
|
for (let attempt = 0; attempt < 2; attempt++) {
|
|
49
|
-
|
|
50
|
-
await writeFile(path, JSON.stringify({ pid: process.pid, start }), { flag: 'wx', mode: 0o600 });
|
|
51
|
-
return { release: async () => { if ((await readJson(path))?.pid === process.pid) await rm(path, { force: true }); } };
|
|
52
|
-
} catch (error) { if (error.code !== 'EEXIST') throw error; }
|
|
62
|
+
if (await linkClaim(path, { pid: process.pid, start })) return { release: async () => { if ((await readJson(path))?.pid === process.pid) await rm(path, { force: true }); } };
|
|
53
63
|
if (await live(await readJson(path))) return undefined;
|
|
54
64
|
await rm(path, { force: true });
|
|
55
65
|
}
|
|
@@ -68,14 +78,7 @@ export async function claimPinInstall(checkout, { env = process.env, signal, onW
|
|
|
68
78
|
const path = pinInstallPath(checkout, env), { start } = await processInfo(process.pid);
|
|
69
79
|
for (let waited = false; ; ) {
|
|
70
80
|
signal?.throwIfAborted();
|
|
71
|
-
|
|
72
|
-
const written = `${path}.${process.pid}`;
|
|
73
|
-
await writeFile(written, JSON.stringify({ pid: process.pid, start }), { mode: 0o600 });
|
|
74
|
-
try {
|
|
75
|
-
await link(written, path);
|
|
76
|
-
return async () => { if ((await readJson(path))?.pid === process.pid) await rm(path, { force: true }); };
|
|
77
|
-
} catch (error) { if (error.code !== 'EEXIST') throw error; }
|
|
78
|
-
finally { await rm(written, { force: true }); }
|
|
81
|
+
if (await linkClaim(path, { pid: process.pid, start })) return async () => { if ((await readJson(path))?.pid === process.pid) await rm(path, { force: true }); };
|
|
79
82
|
const holder = await readJson(path);
|
|
80
83
|
if (!await live(holder)) { await rm(path, { force: true }); continue; }
|
|
81
84
|
if (!waited) { waited = true; onWait?.(holder.pid); }
|
|
@@ -99,15 +102,12 @@ export async function claimLaunch(directory, token) {
|
|
|
99
102
|
const path = join(directory, 'worker.json');
|
|
100
103
|
for (let attempt = 0; attempt < 3; attempt++) {
|
|
101
104
|
const { start } = await processInfo(process.pid);
|
|
102
|
-
|
|
103
|
-
await writeFile(path, JSON.stringify({ token, pid: process.pid, start, startedAt: Date.now(), events: `events-${token}.jsonl` }), { flag: 'wx', mode: 0o600 });
|
|
105
|
+
if (await linkClaim(path, { token, pid: process.pid, start, startedAt: Date.now(), events: `events-${token}.jsonl` })) {
|
|
104
106
|
for (const entry of await readdir(directory)) if (/^events-.*\.jsonl$/.test(entry) && entry !== `events-${token}.jsonl`) await rm(join(directory, entry), { force: true });
|
|
105
107
|
return true;
|
|
106
|
-
} catch (error) {
|
|
107
|
-
if (error.code !== 'EEXIST') throw error;
|
|
108
|
-
if (await liveWorker(directory)) return false;
|
|
109
|
-
await rm(path, { force: true });
|
|
110
108
|
}
|
|
109
|
+
if (await liveWorker(directory)) return false;
|
|
110
|
+
await rm(path, { force: true });
|
|
111
111
|
}
|
|
112
112
|
return false;
|
|
113
113
|
}
|
|
@@ -13,7 +13,9 @@ selected through `cacheContract` when the seed implements that contract. This
|
|
|
13
13
|
is not a general Durable Object adapter. A seed requiring KV or other
|
|
14
14
|
Cloudflare-only APIs cannot use Node.
|
|
15
15
|
The import boundary rejects those APIs and new Cloudflare imports outside the
|
|
16
|
-
platform binding/cache boundary.
|
|
16
|
+
platform binding/cache boundary. The one other file it admits is an exported
|
|
17
|
+
seed's demo guard, `src/modules/demo/index.mjs`, and there only
|
|
18
|
+
`import { env } from 'cloudflare:workers'` (it reads `RER_DEMO`).
|
|
17
19
|
|
|
18
20
|
The entrypoint holds a Linux kernel `flock` for the lifetime of the host. Each
|
|
19
21
|
installation has one local data volume and one writer. The lock inode retains
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { readdir, readFile } from 'node:fs/promises';
|
|
2
|
-
import { join, relative } from 'node:path';
|
|
2
|
+
import { join, relative, sep } from 'node:path';
|
|
3
3
|
import { createRequire } from 'node:module';
|
|
4
4
|
import { pathToFileURL } from 'node:url';
|
|
5
5
|
export async function checkBoundary(root, { engine='node', cacheContract }={}) {
|
|
@@ -10,6 +10,9 @@ export async function checkBoundary(root, { engine='node', cacheContract }={}) {
|
|
|
10
10
|
const name=relative(root,path),tree=ts.createSourceFile(path,await readFile(path,'utf8'),ts.ScriptTarget.Latest,true);
|
|
11
11
|
const cache=cacheContract==='lamp-retained-v1'&&/(?:^|\/)platform\/blobs\/snapshot-cache\.ts$/.test(name);
|
|
12
12
|
const bindings=/(?:^|\/)platform\/bindings\.server\.ts$/.test(name);
|
|
13
|
+
// The demo guard an exported seed carries reads RER_DEMO from env: that one
|
|
14
|
+
// file, and only its unrenamed `env` import.
|
|
15
|
+
const demo=name.split(sep).join('/')==='src/modules/demo/index.mjs';
|
|
13
16
|
const fail=api=>failures.push({path:name,api}),localNames=new Set();
|
|
14
17
|
function names(node){if((ts.isVariableDeclaration(node)||ts.isFunctionDeclaration(node)||ts.isImportSpecifier(node))&&node.name&&ts.isIdentifier(node.name))localNames.add(node.name.text);ts.forEachChild(node,names);}names(tree);
|
|
15
18
|
function visit(node){
|
|
@@ -17,7 +20,7 @@ export async function checkBoundary(root, { engine='node', cacheContract }={}) {
|
|
|
17
20
|
const module=node.moduleSpecifier.text;
|
|
18
21
|
if(module.startsWith('cloudflare:')){
|
|
19
22
|
const imports=node.importClause?.namedBindings;
|
|
20
|
-
const allowed=module==='cloudflare:workers'&&imports&&ts.isNamedImports(imports)&&imports.elements.every(element=>(bindings&&(element.propertyName??element.name).text==='env')||(cache&&(element.propertyName??element.name).text==='DurableObject'));
|
|
23
|
+
const allowed=module==='cloudflare:workers'&&imports&&ts.isNamedImports(imports)&&imports.elements.every(element=>(demo&&!node.importClause.name&&!element.propertyName&&element.name.text==='env')||(bindings&&(element.propertyName??element.name).text==='env')||(cache&&(element.propertyName??element.name).text==='DurableObject'));
|
|
21
24
|
if(!allowed)fail('Cloudflare import outside binding/cache contract');
|
|
22
25
|
}
|
|
23
26
|
}
|
|
@@ -107,14 +107,15 @@ Ownership lives in a state file, by default `.rer/deploy/<environment>.json` und
|
|
|
107
107
|
- **One run at a time.** A deploy takes an exclusive lock file, `<state file>.lock`, for the whole run and removes it at the end, even after a failure. A second run on the same state file is refused as `owner-state-locked` before any provider request. If a run was killed (power loss, `kill -9`) and no deploy is running, delete the lock file by hand.
|
|
108
108
|
|
|
109
109
|
The CLI prints JSON: refusal codes and resource names only, never inputs, tokens or provider bodies.
|
|
110
|
+
A refused provider call also carries `providerRefusal` on the thrown error: the call (`PUT /workers/scripts/<name>`) and Cloudflare's own codes and messages, with the account id and token removed and each message cut to 300 characters. `rer deploy` names it when a custom deploy hook fails on one.
|
|
110
111
|
|
|
111
112
|
### rer deploy: inputs, the hosted owner, secret changes and removal
|
|
112
113
|
|
|
113
114
|
`rer deploy` runs these steps for a seed whose adapter uses `ownerDeploy` from `cli/deploy.mjs`.
|
|
114
115
|
|
|
115
116
|
- **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 a fresh random value for each secret field the manifest generates (`generate`), such as the session secret and the setup token; a value the Worker already holds is kept. A still-missing secret stops the run before any write with a message that names it.
|
|
116
|
-
- **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. 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.
|
|
117
|
-
- **Secret changes.** `rer deploy --set-secret NAME` (value from the environment or `--env-file`, under the declaration's environment name) and `--unset-secret NAME` change one declared secret of the recorded Worker without a build.
|
|
117
|
+
- **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.
|
|
118
|
+
- **Secret changes.** `rer deploy --set-secret NAME` (value from the environment or `--env-file`, under the declaration's environment name) and `--unset-secret NAME` change one declared secret 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.
|
|
118
119
|
- **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 resource whose create answer was lost: under this environment's derived name it is deleted once it is confirmed empty (a D1 with no tables, a KV namespace with no keys, an R2 bucket, which the provider refuses to delete while it holds objects); one holding data is refused as `uncertain-resource-not-empty`, and a queue, whose backlog rer cannot read, as `uncertain-resource-unverifiable` (delete it by hand if it is the empty one from the interrupted run). It then reads back that none is still listed and deletes the state file. An R2 bucket must be empty; a bucket with objects stays, the state file keeps it, and a later `--remove --yes` finishes. Resources rer did not create are never touched.
|
|
119
120
|
|
|
120
121
|
### If the state file is lost
|
|
@@ -286,12 +287,14 @@ retains the existing window, retrying only 404, 503 and network failures for
|
|
|
286
287
|
30 seconds by default.
|
|
287
288
|
|
|
288
289
|
The returned receipt carries `health: {ok, versionId, attempts}`. Each attempt is
|
|
289
|
-
`{offsetMs, status, cfRay, contentType,
|
|
290
|
-
a transport failure
|
|
291
|
-
|
|
292
|
-
message names the class and the last status
|
|
293
|
-
`health-http-failed-last-status-503`) and which carries `attempts
|
|
294
|
-
|
|
290
|
+
`{offsetMs, status, cfRay, contentType, errorClass}`: `status` is null for
|
|
291
|
+
a transport failure and `errorClass` is null for the passing attempt. The receipt
|
|
292
|
+
and owner state keep no response text, since an unhealthy app may echo a secret.
|
|
293
|
+
A failed wait throws an error whose message names the class and the last status
|
|
294
|
+
(for example `health-http-failed-last-status-503`) and which carries `attempts`
|
|
295
|
+
(each with `body`, the first 256 response bytes decoded as UTF-8, for the
|
|
296
|
+
caller's redacted diagnostic), `status` and the identity `receipt` (with
|
|
297
|
+
`health.ok` false). Provider diagnostics are never
|
|
295
298
|
retained; the endpoint must not return credentials. `readiness: {totalMs, clock,
|
|
296
299
|
sleep}` selects a smaller bound and deterministic time; `healthOptions.now` and
|
|
297
300
|
`healthOptions.sleep` act as `clock` and `sleep` for the same purpose.
|
|
@@ -11,8 +11,10 @@ import { verifyHealth } from './health.mjs';
|
|
|
11
11
|
/** Controller-owned plan, inert validated artifact bytes; no project imports or subprocesses.
|
|
12
12
|
* `createResources` defaults to creating only without a prior receipt. Owner-account
|
|
13
13
|
* mode creates owned resources itself first, then passes false. `uploadWithoutReceipt`
|
|
14
|
-
* re-uploads an identical build when no receipt survived, instead of refusing the no-op.
|
|
15
|
-
|
|
14
|
+
* re-uploads an identical build when no receipt survived, instead of refusing the no-op.
|
|
15
|
+
* `declaredSecrets` (default `plan.secrets`) are the secrets the fingerprint names: owner mode
|
|
16
|
+
* passes the declaration's, so retiring the spent setup token leaves the build unchanged. */
|
|
17
|
+
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}) {
|
|
16
18
|
validatePlan(plan); validateArtifact(artifact);
|
|
17
19
|
await verifyServiceGrants(api, plan);
|
|
18
20
|
for (const item of plan.d1) validateMigrations(item.migrations);
|
|
@@ -24,7 +26,7 @@ export async function deploy({api,plan,artifact,artifactSha256,previousReceipt,r
|
|
|
24
26
|
const secrets = await secretBindings(api,plan.worker,plan,env,Boolean(existing));
|
|
25
27
|
const resources = await reconcileResources(api,plan,{create:createResources});
|
|
26
28
|
const baseConfig = renderConfig(plan,resources,api.accountId,artifact.source.sha,'',artifact.main,artifact.assets.length > 0);
|
|
27
|
-
const fingerprint = hash({plan,config:baseConfig,source:artifact.source,modules:artifact.modules.map(({path,sha256}) => ({path,sha256})),assets:artifact.assets.map(({path,sha256}) => ({path,sha256}))});
|
|
29
|
+
const fingerprint = hash({plan:{...plan,secrets:declaredSecrets},config:baseConfig,source:artifact.source,modules:artifact.modules.map(({path,sha256}) => ({path,sha256})),assets:artifact.assets.map(({path,sha256}) => ({path,sha256}))});
|
|
28
30
|
const config = renderConfig(plan,resources,api.accountId,artifact.source.sha,fingerprint,artifact.main,artifact.assets.length > 0);
|
|
29
31
|
const bindings = bindingsFor(config,resources,plan);
|
|
30
32
|
const namespaces = await durableNamespaces(api,plan);
|
|
@@ -64,16 +66,23 @@ export async function deploy({api,plan,artifact,artifactSha256,previousReceipt,r
|
|
|
64
66
|
return receipt;
|
|
65
67
|
}
|
|
66
68
|
|
|
69
|
+
// Response prefixes belong only in the caller's redacted diagnostic, never in
|
|
70
|
+
// the ownership record: an unhealthy app may echo a secret, as the secret command also keeps.
|
|
71
|
+
const recorded = attempts => attempts.map(({body:_,...attempt}) => attempt);
|
|
72
|
+
|
|
67
73
|
/** After an upload the route may take a while to appear: wait for /health with a bound and
|
|
68
74
|
* record every attempt in `receipt.health`. `onHealth(receipt)` runs after each attempt and at the end. */
|
|
69
75
|
async function firstHealth({api,plan,receipt,fetch,healthOptions,readiness,onHealth,asset}) {
|
|
70
76
|
receipt.health = {ok:false,attempts:[]};
|
|
71
77
|
const {now,sleep,...options} = healthOptions;
|
|
72
78
|
const wait = {...(now ? {clock:now} : {}),...(sleep ? {sleep} : {}),...readiness,onAttempt:async (_attempt,attempts) => {
|
|
73
|
-
receipt.health.attempts =
|
|
79
|
+
receipt.health.attempts = recorded(attempts);
|
|
74
80
|
if (onHealth) await onHealth(receipt);
|
|
75
81
|
}};
|
|
76
|
-
try {
|
|
82
|
+
try {
|
|
83
|
+
const health = await verifyHealth({...options,api,plan,receipt,fetch,readiness:wait,asset});
|
|
84
|
+
receipt.health = health.attempts ? {...health,attempts:recorded(health.attempts)} : health;
|
|
85
|
+
}
|
|
77
86
|
catch (error) {
|
|
78
87
|
receipt.health.ok = false; receipt.health.errorClass = error.message;
|
|
79
88
|
if (onHealth) await onHealth(receipt);
|
|
@@ -16,6 +16,14 @@ export function deployDiagnostic(error, redact) {
|
|
|
16
16
|
...(detail.providerCodes?.length ? {providerCodes:detail.providerCodes} : {}),
|
|
17
17
|
output:(detail.output ? (redact ? clean(detail.output) : '(provider output withheld; rer deploy provides redacted details)') : '(no provider output)').split(/\r?\n/).slice(0,8).map(line=>line.slice(0,500)).join('\n')};
|
|
18
18
|
}
|
|
19
|
+
/** One line naming the provider call that was refused and what Cloudflare said, from
|
|
20
|
+
* the error's own `providerRefusal` (set by any copy of the kit); undefined otherwise. */
|
|
21
|
+
export function providerRefusal(error, redact = String) {
|
|
22
|
+
const refusal = error?.providerRefusal;
|
|
23
|
+
if (typeof refusal?.operation !== 'string' || !Array.isArray(refusal.errors)) return undefined;
|
|
24
|
+
const said = refusal.errors.map(item => [item?.code, typeof item?.message === 'string' ? redact(item.message).slice(0,300) : undefined].filter(part => part !== undefined && part !== '').join(' ')).filter(Boolean).slice(0,3);
|
|
25
|
+
return `Cloudflare refused ${redact(refusal.operation).slice(0,200)}${said.length ? `: ${said.join('; ')}` : ''}`;
|
|
26
|
+
}
|
|
19
27
|
function operation(path, options = {}) {
|
|
20
28
|
const write = options.method && options.method !== 'GET';
|
|
21
29
|
if (/\/d1\/database\/[^/]+\/query/.test(path)) return 'migrate D1';
|
|
@@ -20,7 +20,8 @@ async function bodyPrefix(response) {
|
|
|
20
20
|
}
|
|
21
21
|
|
|
22
22
|
/** One credential-free, bounded request. Retain only bounded response evidence. */
|
|
23
|
-
|
|
23
|
+
/** `expected` holds the deploy identity fields the caller knows; each must match. */
|
|
24
|
+
export async function healthRequest({origin, expected, request, timeoutMs, offsetMs}) {
|
|
24
25
|
const attempt = {offsetMs,status:null,cfRay:null,contentType:null,body:'',errorClass:null};
|
|
25
26
|
let response;
|
|
26
27
|
try {
|
|
@@ -58,11 +59,12 @@ export async function healthRequest({origin, receipt, versionId, request, timeou
|
|
|
58
59
|
let body;
|
|
59
60
|
try { body = JSON.parse(bytes.toString('utf8')); }
|
|
60
61
|
catch { attempt.errorClass = 'malformed-response'; return {attempt,transient:false}; }
|
|
61
|
-
if (body?.ok !== true ||
|
|
62
|
+
if (body?.ok !== true || Object.entries(expected).some(([key, value]) => body.deployment?.[key] !== value)) {
|
|
62
63
|
attempt.errorClass = 'health-identity-mismatch';
|
|
63
64
|
// Right after an upload the edge can still answer from another version: that
|
|
64
65
|
// settles within the bound. The expected version answering wrongly does not.
|
|
65
|
-
const
|
|
66
|
+
const key = expected.versionId ? 'versionId' : 'sourceSha';
|
|
67
|
+
const other = body?.ok === true && typeof body.deployment?.[key] === 'string' && body.deployment[key] !== expected[key];
|
|
66
68
|
return {attempt,transient:other};
|
|
67
69
|
}
|
|
68
70
|
return {attempt,transient:false};
|
|
@@ -74,13 +76,13 @@ const MIN_DELAY_MS = 2000, MAX_DELAY_MS = 10000;
|
|
|
74
76
|
|
|
75
77
|
/** The total bound includes requests, body reads and backoff; no request starts at the deadline.
|
|
76
78
|
* Only a transient outcome is retried. Every attempt is recorded, in order, whatever the end. */
|
|
77
|
-
export async function waitHealth({origin,receipt,versionId,request,timeoutMs=30000,totalMs=90000,clock=()=>performance.now(),sleep=ms=>new Promise(resolve=>setTimeout(resolve,ms)),onAttempt=()=>{},single=false}) {
|
|
79
|
+
export async function waitHealth({origin,receipt,versionId,expected={sourceSha:receipt.source.sha,fingerprint:receipt.fingerprint,versionId},request,timeoutMs=30000,totalMs=90000,clock=()=>performance.now(),sleep=ms=>new Promise(resolve=>setTimeout(resolve,ms)),onAttempt=()=>{},single=false}) {
|
|
78
80
|
assert(Number.isFinite(totalMs) && totalMs > 0 && totalMs <= 90000 && Number.isFinite(timeoutMs) && timeoutMs > 0,'invalid-health-bound');
|
|
79
81
|
const start = clock(); const attempts = []; let delay = 1000;
|
|
80
82
|
while (true) {
|
|
81
83
|
const remaining = totalMs - (clock()-start);
|
|
82
84
|
if (remaining <= 0) break;
|
|
83
|
-
const result = await healthRequest({origin,
|
|
85
|
+
const result = await healthRequest({origin,expected,request,timeoutMs:Math.max(1,Math.ceil(Math.min(timeoutMs,remaining))),offsetMs:Math.round(clock()-start)});
|
|
84
86
|
attempts.push(result.attempt); await onAttempt(result.attempt,attempts);
|
|
85
87
|
if (!result.attempt.errorClass) return {ok:true,versionId,attempts};
|
|
86
88
|
// `single` asks once, except that an edge still answering from the previous version settles within the bound.
|
|
@@ -82,7 +82,9 @@ export async function main(argv = process.argv.slice(2), env = process.env, {fet
|
|
|
82
82
|
}
|
|
83
83
|
// Dispatch is the provider's: on some accounts a new Cron starts only at the account scheduler's next refresh.
|
|
84
84
|
if (plan.crons.length) notice(cronNotice);
|
|
85
|
-
|
|
85
|
+
// The fingerprint names every declared secret, retired ones too: an untouched repeat after the claim is a no-op.
|
|
86
|
+
const declaredSecrets = current === declaration ? plan.secrets : ownerPlan(target,declaration,artifact,{localOnlyKeys}).secrets;
|
|
87
|
+
const {receipt,created,sentSecrets} = await ownerDeploy({api,target,plan,artifact,statePath,env,fetch,healthOptions,settle,declaredSecrets}).catch(error => {
|
|
86
88
|
api.annotateFailure(error,error.message.startsWith('missing-secret-') ? 'secrets' : undefined);
|
|
87
89
|
throw error;
|
|
88
90
|
});
|
|
@@ -56,6 +56,7 @@ export async function ownerRemove({api: provider, target, statePath, clock, slee
|
|
|
56
56
|
if (!sameDeployment(live, expected)) throw refuse('owned-resource-replaced', 'worker', target.worker);
|
|
57
57
|
worker = target.worker;
|
|
58
58
|
}
|
|
59
|
+
assert(!state.pendingSecret, 'secret-change-unsettled');
|
|
59
60
|
const listed = {}, doomed = [];
|
|
60
61
|
for (const kind of KINDS) {
|
|
61
62
|
if (!owned.some(item => item.kind === kind)) continue;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { identifier } from './validation.mjs';
|
|
2
|
+
import { deployedFingerprint } from './worker.mjs';
|
|
3
|
+
|
|
4
|
+
// Materialized binding families emitted by bindingsFor and secretBindings. Upload
|
|
5
|
+
// directives such as inherit, and unrecognized types, cannot classify history.
|
|
6
|
+
const historicalBindingTypes = new Set(['assets','plain_text','version_metadata','d1','r2_bucket','kv_namespace','queue','durable_object_namespace','service','secret_text']);
|
|
7
|
+
|
|
8
|
+
export const validSecretNames = names => Array.isArray(names) && names.every(identifier) && new Set(names).size === names.length;
|
|
9
|
+
|
|
10
|
+
/** Provider secret listings contain names and types, never values to persist. */
|
|
11
|
+
export function secretNames(rows) {
|
|
12
|
+
if (!Array.isArray(rows) || !rows.every(row => row?.type === 'secret_text')) return null;
|
|
13
|
+
const names = rows.map(row => row.name);
|
|
14
|
+
return validSecretNames(names) ? names : null;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** Older intents lack a name snapshot. Only the exact historical starting version
|
|
18
|
+
* can supply it: neither the plan nor today's successor proves yesterday's names.
|
|
19
|
+
* Version resources.bindings is optional in the provider API. Absent, malformed or
|
|
20
|
+
* mismatched historical evidence cannot authorize adoption. */
|
|
21
|
+
export async function pendingSecretNames(api, worker, {pendingSecret, receipt}) {
|
|
22
|
+
if (pendingSecret.names !== undefined) return pendingSecret.names;
|
|
23
|
+
let anchor;
|
|
24
|
+
try { anchor = await api.request(`/workers/scripts/${worker}/versions/${pendingSecret.versionId}`); }
|
|
25
|
+
catch (error) { if (error.status === 404) return null; throw error; }
|
|
26
|
+
const bindings = anchor?.resources?.bindings;
|
|
27
|
+
if (anchor?.id !== pendingSecret.versionId || anchor?.resources?.script?.etag !== receipt.provider.etag
|
|
28
|
+
|| !Array.isArray(bindings) || !bindings.every(row => row && typeof row === 'object' && !Array.isArray(row) && historicalBindingTypes.has(row.type))
|
|
29
|
+
|| !validSecretNames(bindings.map(row => row.name)) || deployedFingerprint({bindings}) !== receipt.fingerprint) return null;
|
|
30
|
+
return bindings.filter(row => row.type === 'secret_text' && row.name !== pendingSecret.binding).map(row => row.name);
|
|
31
|
+
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { assert, identifier } from './validation.mjs';
|
|
2
2
|
import { activeWorker } from './worker.mjs';
|
|
3
|
+
import { secretNames } from './owner-secret-names.mjs';
|
|
3
4
|
import { waitHealth } from './health-request.mjs';
|
|
4
5
|
import { definiteProviderRefusal } from './provider.mjs';
|
|
5
6
|
import { readOwnerState, writeOwnerState, withStateLock, withScopeHints, sameDeployment, refuse, recordSecretChange, settlePendingSecret } from './owner.mjs';
|
|
@@ -26,10 +27,12 @@ export async function ownerSecret({api: provider, target, statePath, binding, va
|
|
|
26
27
|
const live = await activeWorker(api, target.worker);
|
|
27
28
|
if (!live) throw refuse('owned-resource-missing', 'worker', target.worker);
|
|
28
29
|
if (!sameDeployment(live, state.receipt.provider)) throw refuse('owned-resource-replaced', 'worker', target.worker);
|
|
30
|
+
assert(!state.pendingSecret, 'secret-change-unsettled');
|
|
29
31
|
const path = `/workers/scripts/${target.worker}/secrets`, present = value !== undefined;
|
|
30
|
-
const names =
|
|
31
|
-
|
|
32
|
-
|
|
32
|
+
const names = secretNames(await api.request(path));
|
|
33
|
+
assert(names, 'invalid-secret-list');
|
|
34
|
+
const existed = names.includes(binding), changed = present || existed;
|
|
35
|
+
state.pendingSecret = {binding, present, versionId:state.receipt.provider.versionId, names:names.filter(name => name !== binding).sort()}; await writeOwnerState(statePath, state);
|
|
33
36
|
let failure, unchanged = false, confirming = false;
|
|
34
37
|
const put = () => api.request(path, {method:'PUT', body:{name:binding, text:value, type:'secret_text'}});
|
|
35
38
|
try {
|
|
@@ -9,6 +9,7 @@ import { secretBindings, activeWorker, deployedFingerprint } from './worker.mjs'
|
|
|
9
9
|
import { validateMigrations } from './migrations.mjs';
|
|
10
10
|
import { deploy } from './deploy.mjs';
|
|
11
11
|
import { verifyHealth } from './health.mjs';
|
|
12
|
+
import { validSecretNames, secretNames, pendingSecretNames } from './owner-secret-names.mjs';
|
|
12
13
|
|
|
13
14
|
/* Owner-account mode: the owner's own Cloudflare account, token and account id.
|
|
14
15
|
* Names are derived from the project id and the owner's environment. A local
|
|
@@ -170,7 +171,8 @@ export async function readOwnerState(path, target) {
|
|
|
170
171
|
// Declared secrets rer removed on purpose (the spent setup token): later plans omit them.
|
|
171
172
|
assert(state.pendingSecret === undefined || identifier(state.pendingSecret?.binding) && typeof state.pendingSecret?.present === 'boolean'
|
|
172
173
|
&& (state.pendingSecret.versionId === undefined || uuid(state.pendingSecret.versionId))
|
|
173
|
-
&& (state.pendingSecret.repeatVersionId === undefined || uuid(state.pendingSecret.repeatVersionId))
|
|
174
|
+
&& (state.pendingSecret.repeatVersionId === undefined || uuid(state.pendingSecret.repeatVersionId))
|
|
175
|
+
&& (state.pendingSecret.names === undefined || validSecretNames(state.pendingSecret.names) && !state.pendingSecret.names.includes(state.pendingSecret.binding)), 'invalid-owner-state');
|
|
174
176
|
assert(state.ownerClaim === undefined || ['claimed','kept'].includes(state.ownerClaim?.outcome), 'invalid-owner-state');
|
|
175
177
|
assert(state.retiredSecrets === undefined || Array.isArray(state.retiredSecrets) && state.retiredSecrets.every(identifier), 'invalid-owner-state');
|
|
176
178
|
assert(state.accountId === target.accountId && state.project === target.project && state.environment === target.environment && state.worker === target.worker, 'owner-state-target-mismatch');
|
|
@@ -245,22 +247,20 @@ export function recordSecretChange(state, current, {binding, present}) {
|
|
|
245
247
|
/** A secret change rer recorded as pending but never confirmed (a lost response, or a run that
|
|
246
248
|
* ended before the change settled). It is that change only when the Worker's versions list shows
|
|
247
249
|
* exactly one version after the recorded starting version and it is the live one, still running the receipt's
|
|
248
|
-
* exact script and build fingerprint: adopt its version. A confirming PUT also records its
|
|
250
|
+
* exact script and build fingerprint, with the other secret names unchanged: adopt its version. A confirming PUT also records its
|
|
249
251
|
* own starting version before it writes; its immediate successor can be recovered by the
|
|
250
252
|
* same checks. Anything later is left to the identity check, which refuses it. */
|
|
251
253
|
export async function settlePendingSecret(api, worker, state, {preflight = false, observe, confirm} = {}) {
|
|
252
254
|
if (!state.pendingSecret || !state.receipt) return false;
|
|
253
255
|
const live = await activeWorker(api, worker);
|
|
254
256
|
if (sameDeployment(live, state.receipt.provider)) {
|
|
255
|
-
//
|
|
256
|
-
//
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
return false;
|
|
263
|
-
}
|
|
257
|
+
// Both deployment and newest version must corroborate an unapplied write.
|
|
258
|
+
// A stale deployment alone must not erase an accepted but unsettled intent.
|
|
259
|
+
const versions = (await api.request(`/workers/scripts/${worker}/versions`))?.items;
|
|
260
|
+
const unchanged = Array.isArray(versions) && versions.every(item => uuid(item?.id))
|
|
261
|
+
&& new Set(versions.map(item => item.id)).size === versions.length && versions[0]?.id === live.versionId;
|
|
262
|
+
observe?.(unchanged ? live : null);
|
|
263
|
+
if (!preflight || !unchanged) return false;
|
|
264
264
|
delete state.pendingSecret;
|
|
265
265
|
return true;
|
|
266
266
|
}
|
|
@@ -269,6 +269,7 @@ export async function settlePendingSecret(api, worker, state, {preflight = false
|
|
|
269
269
|
// Newest first, as the provider lists them.
|
|
270
270
|
const versions = (await api.request(`/workers/scripts/${worker}/versions`))?.items;
|
|
271
271
|
const ids = Array.isArray(versions) ? versions.map(item => item?.id) : [];
|
|
272
|
+
if (!ids.every(uuid) || new Set(ids).size !== ids.length) return false;
|
|
272
273
|
const {versionId, repeatVersionId} = state.pendingSecret;
|
|
273
274
|
const immediate = ids.indexOf(versionId) === 1;
|
|
274
275
|
const repeated = repeatVersionId && ids[1] === repeatVersionId && ids.indexOf(versionId) === 2;
|
|
@@ -277,7 +278,11 @@ export async function settlePendingSecret(api, worker, state, {preflight = false
|
|
|
277
278
|
// either the original immediate successor or the second write's successor.
|
|
278
279
|
if (ids[0] !== live.versionId || !(repeatVersionId ? repeated || preflight && immediate && live.versionId === repeatVersionId : immediate)) return false;
|
|
279
280
|
const rows = await api.request(`/workers/scripts/${worker}/secrets`);
|
|
280
|
-
|
|
281
|
+
const names = secretNames(rows);
|
|
282
|
+
if (!names || names.includes(state.pendingSecret.binding) !== state.pendingSecret.present) return false;
|
|
283
|
+
const priorNames = await pendingSecretNames(api, worker, state);
|
|
284
|
+
const otherNames = names.filter(name => name !== state.pendingSecret.binding);
|
|
285
|
+
if (!priorNames || priorNames.length !== otherNames.length || !priorNames.every(name => otherNames.includes(name))) return false;
|
|
281
286
|
if (confirm && !repeatVersionId) { await confirm(live); return false; }
|
|
282
287
|
recordSecretChange(state, live, state.pendingSecret);
|
|
283
288
|
return true;
|
|
@@ -333,20 +338,21 @@ export async function withStateLock(path, run) {
|
|
|
333
338
|
|
|
334
339
|
/** Find or create every declared resource under the owner's account, migrate, upload and check health.
|
|
335
340
|
* Ownership preflight precedes provider writes. A re-run of the same build writes nothing. */
|
|
336
|
-
export async function ownerDeploy({api: provider, target, plan, artifact, statePath, env = {}, fetch = globalThis.fetch, healthOptions = {}, readiness = {}, settle = {}}) {
|
|
341
|
+
export async function ownerDeploy({api: provider, target, plan, artifact, statePath, env = {}, fetch = globalThis.fetch, healthOptions = {}, readiness = {}, settle = {}, declaredSecrets = plan.secrets}) {
|
|
337
342
|
assert(typeof statePath === 'string' && statePath.length, 'missing-owner-state-path');
|
|
338
343
|
const api = withSettledLists(withScopeHints(provider),settle);
|
|
339
344
|
assert(target?.accountId === api.accountId, 'owner-account-mismatch');
|
|
340
345
|
validatePlan(plan); assertOwnerNames(target, plan);
|
|
341
346
|
for (const item of plan.d1) validateMigrations(item.migrations);
|
|
342
|
-
return withStateLock(statePath, () => deployLocked({api, target, plan, artifact, statePath, env, fetch, healthOptions, readiness, settle}));
|
|
347
|
+
return withStateLock(statePath, () => deployLocked({api, target, plan, artifact, statePath, env, fetch, healthOptions, readiness, settle, declaredSecrets}));
|
|
343
348
|
}
|
|
344
|
-
async function deployLocked({api, target, plan, artifact, statePath, env, fetch, healthOptions, readiness, settle}) {
|
|
349
|
+
async function deployLocked({api, target, plan, artifact, statePath, env, fetch, healthOptions, readiness, settle, declaredSecrets}) {
|
|
345
350
|
const state = await readOwnerState(statePath, target);
|
|
346
351
|
const save = () => writeOwnerState(statePath, state);
|
|
347
352
|
if (await settlePendingSecret(api, target.worker, state, {preflight:true})) await save();
|
|
348
353
|
await checkServiceWorkers(api, plan);
|
|
349
354
|
const priorWorker = await checkOwnedWorker(api, plan, artifact, state);
|
|
355
|
+
assert(!state.pendingSecret, 'secret-change-unsettled');
|
|
350
356
|
const exists = priorWorker !== null;
|
|
351
357
|
const create = await ownedResourcesToCreate(api, plan, state);
|
|
352
358
|
if (exists) await checkCronOwnership(api, plan);
|
|
@@ -389,7 +395,7 @@ async function deployLocked({api, target, plan, artifact, statePath, env, fetch,
|
|
|
389
395
|
return result;
|
|
390
396
|
}};
|
|
391
397
|
const receipt = await deploy({api:observed, plan, artifact, artifactSha256:hash(artifact), previousReceipt:state.receipt ?? undefined,
|
|
392
|
-
createResources:false, uploadWithoutReceipt:true, env, fetch, healthOptions, readiness,
|
|
398
|
+
createResources:false, uploadWithoutReceipt:true, env, fetch, healthOptions, readiness, declaredSecrets,
|
|
393
399
|
onReceipt: async candidate => { if (candidate.changed || !state.receipt) { state.receipt = candidate; delete state.pendingUpload; delete state.pendingSecret; await save(); } },
|
|
394
400
|
// Keep each first-health attempt beside the identity it belongs to.
|
|
395
401
|
onHealth: async candidate => { if (candidate.changed) { state.receipt = candidate; await save(); } }});
|
|
@@ -56,6 +56,13 @@ export function cloudflare({ accountId, token, fetch: request = globalThis.fetch
|
|
|
56
56
|
if (!response.ok || payload?.success === false) {
|
|
57
57
|
const error = new Error(response.ok ? 'provider-unsuccessful-or-malformed' : `provider-http-${response.status}`);
|
|
58
58
|
error.status = response.status;
|
|
59
|
+
// What Cloudflare refused, on the error itself so any caller (a custom hook
|
|
60
|
+
// with its own copy of the kit too) can say it: the call, and Cloudflare's own
|
|
61
|
+
// codes and messages, without credentials and bounded.
|
|
62
|
+
const bounded = text => redactAuth(text).replace(/[\x00-\x1f\x7f-\x9f]+/g,' ').trim().slice(0,300);
|
|
63
|
+
const said = (Array.isArray(payload?.errors) ? payload.errors : []).filter(x => Number.isSafeInteger(x?.code) || typeof x?.message === 'string').slice(0,3)
|
|
64
|
+
.map(x => ({...(Number.isSafeInteger(x.code) ? {code:x.code} : {}),...(typeof x.message === 'string' ? {message:bounded(x.message)} : {})}));
|
|
65
|
+
error.providerRefusal = {operation:`${method} ${path.split('?')[0]}`,errors:said.length || payload !== undefined || !output.trim() ? said : [{message:bounded(output.trim().split('\n')[0])}]};
|
|
59
66
|
throw recordDeployFailure(error,{providerCodes:(Array.isArray(payload?.errors) ? payload.errors : []).filter(x => Number.isSafeInteger(x?.code)).map(x=>x.code).slice(0,8),
|
|
60
67
|
output:redactAuth(Array.isArray(payload?.errors) ? payload.errors.filter(x=>typeof x?.message==='string').map(x=>x.message).join('\n') : output)});
|
|
61
68
|
}
|
|
@@ -11,7 +11,9 @@ export async function identity() {
|
|
|
11
11
|
const token = randomUUID(), socket = join(tmpdir(), `rer-job-${token}.sock`);
|
|
12
12
|
// Darwin's TMPDIR can be long; use the short system temporary directory then.
|
|
13
13
|
const path = Buffer.byteLength(socket) < 100 ? socket : `/tmp/rer-job-${token}.sock`;
|
|
14
|
-
|
|
14
|
+
// A prober that timed out or exited may close before the answer is written;
|
|
15
|
+
// the prober reports that itself, so the answer's EPIPE or ECONNRESET is dropped.
|
|
16
|
+
const server = createServer(client => { client.on('error', () => {}); client.end(token); });
|
|
15
17
|
await new Promise((resolve, reject) => { server.once('error', reject); server.listen(path, resolve); });
|
|
16
18
|
await chmod(path, 0o600);
|
|
17
19
|
const { title, ...processIdentity } = await processInfo(process.pid);
|
|
@@ -23,7 +23,8 @@ export async function callerPolicy(env=process.env) {
|
|
|
23
23
|
const lifetime=await identity(),token=randomUUID(),path=`/tmp/rer-policy-${token}.sock`;
|
|
24
24
|
// An invalid inherited claim fails closed even when the child changes CI.
|
|
25
25
|
const ci=!value&&env.CI==='true';
|
|
26
|
-
|
|
26
|
+
// As with identity: a prober that closed first reports that itself.
|
|
27
|
+
const server=createServer(socket=>{socket.on('error',()=>{});socket.end(JSON.stringify({token,ci}));});
|
|
27
28
|
await new Promise((resolve,reject)=>{server.once('error',reject);server.listen(path,resolve);});await chmod(path,0o600);
|
|
28
29
|
return {ci,environment:{RER_JOB_POLICY:JSON.stringify({path,token,owner:lifetime.info})},async close(){await new Promise(resolve=>server.close(resolve));await unlink(path).catch(error=>{if(error.code!=='ENOENT')throw error;});await lifetime.close();}};
|
|
29
30
|
}
|
|
@@ -124,11 +124,20 @@ export async function replaceSavedOrigin(root, options, origin) {
|
|
|
124
124
|
return rewriteOrigin(root, options, (await readSettings(root, options)).values, origin);
|
|
125
125
|
}
|
|
126
126
|
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
127
|
+
// Rewrite only one saved setting (setup replacing the zero-setup owner). The
|
|
128
|
+
// caller holds the local operation lock.
|
|
129
|
+
export async function replaceSavedSetting(root, options, key, value) {
|
|
130
|
+
return rewriteSetting(root, options, (await readSettings(root, options)).values, key, value);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function rewriteOrigin(root, options, values, origin) {
|
|
134
|
+
return rewriteSetting(root, options, values, options.originKey ?? 'AUTH_ORIGIN', origin);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
async function rewriteSetting(root, options, values, key, value) {
|
|
138
|
+
const current = await readSettings(root, options), format = options.format ?? 'dotenv', merged = { ...values, [key]: value };
|
|
139
|
+
const text = format === 'json' ? JSON.stringify(merged, null, 1) + '\n' : current.text.replace(new RegExp(`^${key}=.*$`, 'm'), `${key}=${dotenvValue(value)}`);
|
|
140
|
+
if (format !== 'json' && localVariables(text)[key] !== value) throw new Error(`Could not change ${key} in ${options.file ?? '.dev.vars'}; put ${key}=${value} on its own line.`);
|
|
132
141
|
await writePrivateFile(current.path, text);
|
|
133
142
|
return merged;
|
|
134
143
|
}
|
|
@@ -1,20 +1,27 @@
|
|
|
1
|
-
import {readdir,lstat} from 'node:fs/promises';
|
|
2
|
-
import {join} from 'node:path';
|
|
1
|
+
import {readdir,lstat,realpath} from 'node:fs/promises';
|
|
2
|
+
import {basename,join} from 'node:path';
|
|
3
3
|
import {DatabaseSync} from 'node:sqlite';
|
|
4
4
|
|
|
5
|
-
//
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
5
|
+
// The owner the zero-setup `rer dev` gives a checkout before its real owner is known.
|
|
6
|
+
export function zeroSetupOwner(checkout){
|
|
7
|
+
const label=basename(checkout).toLowerCase().replace(/[^a-z0-9-]+/g,'-').replace(/^-+|-+$/g,'').slice(0,63)||'checkout';
|
|
8
|
+
return `owner@${label}.test`;
|
|
9
|
+
}
|
|
10
|
+
async function databases(root){
|
|
9
11
|
let directory=root;
|
|
10
12
|
for(const part of ['.wrangler','state','v3','d1','miniflare-D1DatabaseObject']){
|
|
11
13
|
directory=join(directory,part);let status;try{status=await lstat(directory);}catch(error){if(error.code==='ENOENT')return []; throw error;}
|
|
12
14
|
if(status.isSymbolicLink()||!status.isDirectory())throw Error('Canonical local state is unsafe. Preserve it and recover its placement before setup.');
|
|
13
15
|
}
|
|
14
|
-
|
|
15
|
-
|
|
16
|
+
return (await readdir(directory,{withFileTypes:true})).filter(file=>file.isFile()&&file.name.endsWith('.sqlite')).map(file=>join(directory,file.name));
|
|
17
|
+
}
|
|
18
|
+
// Read only the canonical local D1 files, before settings/build/migrations can
|
|
19
|
+
// mutate anything. An unrelated database does not establish an owner claim.
|
|
20
|
+
export async function localOwners(root){
|
|
21
|
+
const found=[];
|
|
22
|
+
for(const path of await databases(root)){
|
|
16
23
|
let db,owners=[];
|
|
17
|
-
try{db=new DatabaseSync(
|
|
24
|
+
try{db=new DatabaseSync(path,{readOnly:true});
|
|
18
25
|
const tables=new Set(db.prepare("SELECT name FROM sqlite_master WHERE type='table'").all().map(row=>row.name));
|
|
19
26
|
if(tables.has('oa_operators'))owners=db.prepare("SELECT email FROM oa_operators WHERE role='owner' AND status='active'").all();
|
|
20
27
|
}finally{db?.close();}
|
|
@@ -22,13 +29,31 @@ export async function localOwners(root){
|
|
|
22
29
|
}
|
|
23
30
|
return [...new Set(found)];
|
|
24
31
|
}
|
|
32
|
+
// Returns the existing owners, and `placeholder` when the canonical database's
|
|
33
|
+
// only owner is still this checkout's zero-setup owner: setup replaces it.
|
|
25
34
|
export async function assertLocalOwner(root,email,failure=message=>Error(message),inspect,setting='private owner email setting'){
|
|
26
35
|
const owners=inspect?await inspect(root):await localOwners(root);
|
|
27
36
|
if(!Array.isArray(owners)||owners.some(owner=>typeof owner!=='string'))throw failure('The localOwner hook must return existing owner email addresses without changing state.');
|
|
37
|
+
const placeholder=!inspect&&owners.length===1&&owners[0].toLowerCase()===zeroSetupOwner(await realpath(root))?owners[0]:undefined;
|
|
38
|
+
if(placeholder)return {owners,placeholder};
|
|
28
39
|
if(owners.length&&!owners.some(owner=>owner.toLowerCase()===email.toLowerCase())){
|
|
29
40
|
const address=safeOwner(owners[0]);
|
|
30
41
|
throw failure(`Local database already belongs to ${address}. Restore ${setting} to ${address}, then run rer setup --owner-email ${address}; or back up data, run rer dev --stop, then rer reset --yes followed by rer setup --owner-email ${safeOwner(email)}. Reset deletes local data. Existing data and private settings were kept.`);
|
|
31
42
|
}
|
|
32
|
-
return owners;
|
|
43
|
+
return {owners};
|
|
44
|
+
}
|
|
45
|
+
// The placeholder owner's own row takes the real owner's address, so every
|
|
46
|
+
// record that names the owner by id stays theirs. Its version moves on, which
|
|
47
|
+
// retires sign-in links issued to the placeholder.
|
|
48
|
+
export async function replaceLocalOwner(root,from,to,failure=message=>Error(message)){
|
|
49
|
+
for(const path of await databases(root)){
|
|
50
|
+
const db=new DatabaseSync(path);
|
|
51
|
+
try{
|
|
52
|
+
const tables=new Set(db.prepare("SELECT name FROM sqlite_master WHERE type='table'").all().map(row=>row.name));
|
|
53
|
+
if(!tables.has('oa_operators'))continue;
|
|
54
|
+
if(db.prepare('SELECT 1 FROM oa_operators WHERE lower(email)=lower(?) AND lower(email)<>lower(?)').get(to,from))throw failure(`${safeOwner(to)} already has an account in the local database, so the zero-setup owner ${safeOwner(from)} was kept. Run rer setup --owner-email ${safeOwner(from)}, or back up data and run rer reset --yes.`);
|
|
55
|
+
db.prepare("UPDATE oa_operators SET email=?,version=version+1 WHERE lower(email)=lower(?) AND role='owner' AND status='active'").run(to,from);
|
|
56
|
+
}finally{db.close();}
|
|
57
|
+
}
|
|
33
58
|
}
|
|
34
59
|
export const safeOwner=email=>email.length<=254&&/^[a-zA-Z0-9.!+_%\-]+@[a-zA-Z0-9\-]+(?:\.[a-zA-Z0-9\-]+)+$/.test(email)?email:'<existing-owner-email>';
|