Public: anyone can read this; the crew can change it.
Config is published at boot by the same rule as everywhere #17733
Merged 17h 42m ago, live in production
sold/edge-boot → main 0 commits
5 files
changed +104
−6
src/cloud/edge.ts +1 −1
| @@ -173,7 +173,7 @@ export class EdgePublisher { | ||
| 173 | 173 | * that is not `open` (a private app's) is taken down instead: never a file, |
| 174 | 174 | * never a KV key. |
| 175 | 175 | */ |
| 176 | async publish(snap: Snapshot, open = true): Promise<void> { | |
| 176 | async publish(snap: Snapshot, open: boolean): Promise<void> { | |
| 177 | 177 | if (!open) return this.remove(snap.scope); |
| 178 | 178 | const path = this.pathOf(snap.scope); |
| 179 | 179 | if (path === undefined) return; |
src/main.ts +2 −1
| @@ -9,6 +9,7 @@ import { DEPLOY_EVERY_MS, DEPLOY_MAX_WAIT_MS } from "./apps/deploy_hold.ts"; | ||
| 9 | 9 | import { join } from "node:path"; |
| 10 | 10 | import { sweepEntitlements } from "./billing/sync.ts"; |
| 11 | 11 | import { sweepDesk } from "./actions/desk.ts"; |
| 12 | import { edgeOpen } from "./cloud/edge_open.ts"; | |
| 12 | 13 | import { settleAll } from "./core/allocation.ts"; |
| 13 | 14 | import { resumePinnedWatches } from "./actions/pinned.ts"; |
| 14 | 15 | import { statusTick } from "./core/status_days.ts"; |
| @@ -484,7 +485,7 @@ await reconcile(app); | ||
| 484 | 485 | // held now so the edge file exists from the first request. |
| 485 | 486 | app.configs.start(); |
| 486 | 487 | for (const scope of app.configs.scopes()) |
| 487 | await app.edge().publish(app.configs.snapshot(scope)).catch((error) => | |
| 488 | await app.edge().publish(app.configs.snapshot(scope), edgeOpen(app.db, scope)).catch((error) => | |
| 488 | 489 | app.log.warn("edge publish failed", { scope, error: String(error) }), |
| 489 | 490 | ); |
| 490 | 491 | // Issues linked under a pillar's umbrella before pillars had labels. |
test/config-store.test.ts +4 −4
| @@ -148,7 +148,7 @@ describe("config store", () => { | ||
| 148 | 148 | const kv = kvFromEnv({ FORGE_CF_ACCOUNT: "acct1", FORGE_CF_KV_NAMESPACE: "ns1", FORGE_CF_API_TOKEN: "t" }); |
| 149 | 149 | expect(kvFromEnv({ FORGE_CF_ACCOUNT: "a/../b" })).toBeUndefined(); |
| 150 | 150 | const edge = new EdgePublisher(dir, { kv, fetch: fake }); |
| 151 | await edge.publish(app.configs.snapshot(FORGE_SCOPE)); | |
| 151 | await edge.publish(app.configs.snapshot(FORGE_SCOPE), true); | |
| 152 | 152 | expect(calls).toHaveLength(1); |
| 153 | 153 | expect(calls[0]!.url).toBe( |
| 154 | 154 | "https://api.cloudflare.com/client/v4/accounts/acct1/storage/kv/namespaces/ns1/values/forge", |
| @@ -171,8 +171,8 @@ describe("the edge publisher", () => { | ||
| 171 | 171 | const snap = (version: number) => ({ |
| 172 | 172 | scope: "forge", id: `s${version}`, version, values: { v: version }, updatedAt: "now", |
| 173 | 173 | }); |
| 174 | await edge.publish(snap(2)); | |
| 175 | await edge.publish(snap(1)); | |
| 174 | await edge.publish(snap(2), true); | |
| 175 | await edge.publish(snap(1), true); | |
| 176 | 176 | expect(calls).toHaveLength(1); |
| 177 | 177 | expect(calls[0]!.signal).toBeDefined(); |
| 178 | 178 | expect(edge.read("forge")!.snapshot).toBe("s2"); |
| @@ -185,7 +185,7 @@ describe("the edge publisher", () => { | ||
| 185 | 185 | const dir = await mkdtemp(join(tmpdir(), "forge-edge-")); |
| 186 | 186 | try { |
| 187 | 187 | const edge = new EdgePublisher(dir); |
| 188 | await edge.publish({ scope: "forge", id: "s1", version: 1, values: { v: 1 }, updatedAt: "now" }); | |
| 188 | await edge.publish({ scope: "forge", id: "s1", version: 1, values: { v: 1 }, updatedAt: "now" }, true); | |
| 189 | 189 | const path = edge.pathOf("forge")!; |
| 190 | 190 | writeFileSync(path, readFileSync(path, "utf8").replace('\\"v\\":1', '\\"v\\":9')); |
| 191 | 191 | expect(new EdgePublisher(dir).read("forge")).toBeUndefined(); |
test/edge-boot.test.ts added +80
| @@ -0,0 +1,80 @@ | ||
| 1 | /** | |
| 2 | * What a boot publishes of the config store: the real `src/main.ts` on a data directory that holds | |
| 3 | * config for the forge, a public app, a private app and an app that is gone. | |
| 4 | */ | |
| 5 | import { afterAll, expect, test } from "bun:test"; | |
| 6 | import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; | |
| 7 | import { tmpdir } from "node:os"; | |
| 8 | import { dirname, join } from "node:path"; | |
| 9 | import { newApp } from "../src/actions/apps.ts"; | |
| 10 | import { createApp } from "../src/app.ts"; | |
| 11 | import { edgeName } from "../src/cloud/edge.ts"; | |
| 12 | import { appScope, FORGE_SCOPE } from "../src/core/config.ts"; | |
| 13 | import { createUser } from "../src/core/users.ts"; | |
| 14 | import { freePort } from "../src/kernel/port.ts"; | |
| 15 | import { restoredEnv } from "../src/snapshot/boot.ts"; | |
| 16 | import { collectOutput } from "./helpers/collect-output.ts"; | |
| 17 | import { waitFor } from "./helpers/poll.ts"; | |
| 18 | ||
| 19 | const root = mkdtempSync(join(tmpdir(), "forge-edge-boot-")); | |
| 20 | afterAll(() => rmSync(root, { recursive: true, force: true })); | |
| 21 | const GONE = "app/gone1"; | |
| 22 | ||
| 23 | test("a boot publishes config by the same rule as a write", async () => { | |
| 24 | const dataDir = join(root, "data"); | |
| 25 | const app = createApp({ dataDir, port: 0 }); | |
| 26 | const scopes: Record<"public" | "private", string> = { public: "", private: "" }; | |
| 27 | const fileOf = (scope: string): string => join(dataDir, "edge", "config", `${edgeName(scope)}.json`); | |
| 28 | try { | |
| 29 | const owner = await createUser(app.db, { origin: "seed", trusted: true, username: "alice", email: "[email protected]", password: "a-good-passphrase", adminIfFirst: true }); | |
| 30 | if (!owner.ok) throw new Error(owner.error); | |
| 31 | for (const visibility of ["public", "private"] as const) { | |
| 32 | const made = await newApp(app, owner.value, { name: `${visibility}-app`, description: "d", visibility }); | |
| 33 | if (!made.ok) throw new Error(String(made.error)); | |
| 34 | scopes[visibility] = appScope(made.value.app.id); | |
| 35 | } | |
| 36 | for (const scope of [FORGE_SCOPE, scopes.public, scopes.private, GONE]) { | |
| 37 | const wrote = app.configs.set(scope, { k: "v" }, "alice", "w"); | |
| 38 | if (!wrote.ok) throw new Error(wrote.error); | |
| 39 | } | |
| 40 | } finally { | |
| 41 | app.close(); | |
| 42 | } | |
| 43 | // What is on disk before the boot: nothing of the open ones, an earlier file of the others. | |
| 44 | for (const scope of [FORGE_SCOPE, scopes.public]) rmSync(fileOf(scope), { force: true }); | |
| 45 | for (const scope of [scopes.private, GONE]) { | |
| 46 | mkdirSync(dirname(fileOf(scope)), { recursive: true }); | |
| 47 | writeFileSync(fileOf(scope), "earlier"); | |
| 48 | } | |
| 49 | ||
| 50 | const said = join(root, "kv.txt"); | |
| 51 | writeFileSync(said, ""); | |
| 52 | const calls = (): string[] => readFileSync(said, "utf8").split("\n").filter((l) => l !== ""); | |
| 53 | // A whole boot, but for what a boot proof keeps in: a read-only one publishes nothing. | |
| 54 | const { FORGE_READ_ONLY: _, ...env } = restoredEnv(dataDir, await freePort()); | |
| 55 | const child = Bun.spawn([process.execPath, "--preload", join(import.meta.dir, "helpers", "record-kv.ts"), join(import.meta.dir, "..", "src", "main.ts")], { | |
| 56 | cwd: root, | |
| 57 | env: { ...env, RECORD_KV: said, FORGE_CF_ACCOUNT: "a", FORGE_CF_KV_NAMESPACE: "n", FORGE_CF_API_TOKEN: "t" }, | |
| 58 | stdin: "ignore", | |
| 59 | stdout: "pipe", | |
| 60 | stderr: "pipe", | |
| 61 | }); | |
| 62 | const out = { text: "" }; | |
| 63 | const reading = collectOutput(out, child.stdout, child.stderr); | |
| 64 | try { | |
| 65 | // One call a scope. | |
| 66 | await waitFor(() => calls().length >= 4 || child.exitCode !== null, { every: 25, within: 20_000 }); | |
| 67 | } finally { | |
| 68 | child.kill("SIGKILL"); | |
| 69 | await child.exited; | |
| 70 | await reading; | |
| 71 | } | |
| 72 | expect(calls().sort(), out.text.slice(-6000)).toEqual( | |
| 73 | [`DELETE ${edgeName(scopes.private)}`, `DELETE ${edgeName(GONE)}`, `PUT ${edgeName(FORGE_SCOPE)}`, `PUT ${edgeName(scopes.public)}`].sort(), | |
| 74 | ); | |
| 75 | for (const scope of [FORGE_SCOPE, scopes.public]) expect([scope, existsSync(fileOf(scope))]).toEqual([scope, true]); | |
| 76 | for (const scope of [scopes.private, GONE]) { | |
| 77 | expect([scope, existsSync(fileOf(scope))]).toEqual([scope, false]); | |
| 78 | expect(readFileSync(join(dataDir, "edge", "withdrawn", `${edgeName(scope)}.json`), "utf8")).toBe("earlier"); | |
| 79 | } | |
| 80 | }, 90_000); |
test/helpers/record-kv.ts added +17
| @@ -0,0 +1,17 @@ | ||
| 1 | /** | |
| 2 | * A preload for a forge booted by a test: each call to Cloudflare's API is answered 200 here and | |
| 3 | * written as `METHOD key` to the file `RECORD_KV` names. Nothing leaves the process. | |
| 4 | */ | |
| 5 | import { appendFileSync } from "node:fs"; | |
| 6 | ||
| 7 | const real = globalThis.fetch; | |
| 8 | const to = process.env["RECORD_KV"]!; | |
| 9 | globalThis.fetch = Object.assign( | |
| 10 | async (input: Parameters<typeof fetch>[0], init?: RequestInit): Promise<Response> => { | |
| 11 | const url = input instanceof Request ? input.url : String(input); | |
| 12 | if (!url.startsWith("https://api.cloudflare.com/")) return real(input, init); | |
| 13 | appendFileSync(to, `${init?.method ?? "GET"} ${url.split("/").at(-1)}\n`); | |
| 14 | return new Response("{}", { status: 200 }); | |
| 15 | }, | |
| 16 | { preconnect: real.preconnect }, | |
| 17 | ); |
new above
offline