Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/sandbox/daemon/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -528,8 +528,8 @@ const configUpdateH = makeConfigUpdateHandler({
},
});

function hydrate(): void {
const diskOutcome = readConfig(bootConfig.repoDir);
async function hydrate(): Promise<void> {
const diskOutcome = await readConfig(bootConfig.repoDir);
if (diskOutcome.kind === "invalid") {
console.warn(
`[daemon] ignoring .decocms/daemon.json: ${diskOutcome.reason}`,
Expand All @@ -541,7 +541,7 @@ function hydrate(): void {
orchestrator.handle({ kind: "bootstrap", config: initial });
}

hydrate();
await hydrate();

if (!store.read()) {
console.log(
Expand Down
32 changes: 16 additions & 16 deletions packages/sandbox/daemon/persistence.test.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,48 @@
import { afterAll, describe, expect, it } from "bun:test";
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { readConfig } from "./persistence";

function repoWithDaemonJson(contents: string): string {
const dir = mkdtempSync(join(tmpdir(), "persistence-test-"));
mkdirSync(join(dir, ".decocms"), { recursive: true });
writeFileSync(join(dir, ".decocms", "daemon.json"), contents);
async function repoWithDaemonJson(contents: string): Promise<string> {
const dir = await mkdtemp(join(tmpdir(), "persistence-test-"));
await mkdir(join(dir, ".decocms"), { recursive: true });
await writeFile(join(dir, ".decocms", "daemon.json"), contents);
return dir;
}

describe("readConfig", () => {
const tmpDirs: string[] = [];
afterAll(() => {
for (const d of tmpDirs) rmSync(d, { recursive: true, force: true });
afterAll(async () => {
for (const d of tmpDirs) await rm(d, { recursive: true, force: true });
});

it("returns absent when no file exists", () => {
const dir = mkdtempSync(join(tmpdir(), "persistence-test-"));
it("returns absent when no file exists", async () => {
const dir = await mkdtemp(join(tmpdir(), "persistence-test-"));
tmpDirs.push(dir);
expect(readConfig(dir)).toEqual({ kind: "absent" });
expect(await readConfig(dir)).toEqual({ kind: "absent" });
});

it("accepts a valid config", () => {
const dir = repoWithDaemonJson(
it("accepts a valid config", async () => {
const dir = await repoWithDaemonJson(
JSON.stringify({ application: { port: 3000 } }),
);
tmpDirs.push(dir);
expect(readConfig(dir)).toEqual({
expect(await readConfig(dir)).toEqual({
kind: "valid",
config: { application: { port: 3000 } },
});
});

it("rejects a config that fails schema validation instead of trusting it blindly", () => {
it("rejects a config that fails schema validation instead of trusting it blindly", async () => {
// Regression: a tenant-committed .decocms/daemon.json bypasses the
// daemon-token auth that guards PUT /config, so it must not skip the
// same field validation that route enforces (here: port out of range).
const dir = repoWithDaemonJson(
const dir = await repoWithDaemonJson(
JSON.stringify({ application: { port: 99999 } }),
);
tmpDirs.push(dir);
const outcome = readConfig(dir);
const outcome = await readConfig(dir);
expect(outcome.kind).toBe("invalid");
});
});
6 changes: 3 additions & 3 deletions packages/sandbox/daemon/persistence.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { readFileSync } from "node:fs";
import { readFile } from "node:fs/promises";
import { join } from "node:path";
import type { TenantConfig } from "./types";
import { validateTenantConfig } from "./validate";
Expand All @@ -22,10 +22,10 @@ export type ReadOutcome =
* never writes this file; it exists only if a tenant committed one to the
* repo themselves.
*/
export function readConfig(repoDir: string): ReadOutcome {
export async function readConfig(repoDir: string): Promise<ReadOutcome> {
let raw: string;
try {
raw = readFileSync(configPath(repoDir), "utf-8");
raw = await readFile(configPath(repoDir), "utf-8");
} catch (e) {
const err = e as NodeJS.ErrnoException;
if (err.code === "ENOENT") return { kind: "absent" };
Expand Down
2 changes: 1 addition & 1 deletion packages/sandbox/daemon/setup/orchestrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ export class SetupOrchestrator {
* read-then-write.
*/
private async fillApplicationDefaults(repoDir: string): Promise<void> {
const outcome = readConfig(repoDir);
const outcome = await readConfig(repoDir);
const diskApp =
outcome.kind === "valid" ? outcome.config.application : undefined;

Expand Down
Loading