/** * The development server, read from config/dev-server.json: the one place * that names it (the Worker's dev deploy, every app's dev build and these * scripts read it). `host` is the dev Worker's host; `fakeGoogleHost` the * stand-in Google the dev Worker's calendar sign-in reaches. */import { readFileSync } from"node:fs";import { dirname, join } from"node:path";import { fileURLToPath } from"node:url";exportconst DEV_SERVER_FILE = join(dirname(fileURLToPath(import.meta.url)), "..", "..", "..", "config", "dev-server.json");typeDevServer = { host: string; fakeGoogleHost?: string; personalSlug?: string };function read(file = DEV_SERVER_FILE): DevServer {let parsed: Partial<DevServer>;try { parsed = JSON.parse(readFileSync(file, "utf8")) asPartial<DevServer>; } catch (e) {thrownewError(`${file} could not be read (${(e as Error).message}): it names the dev Worker ({"host": "<your dev Worker's host>"})`); }if (typeof parsed.host !== "string" || !/^[A-Za-z0-9.-]+$/.test(parsed.host)) thrownewError(`${file}: "host" is no host name`);return parsed asDevServer;}/** The dev Worker's host, e.g. `cottondb-dev.<account>.workers.dev`. */exportfunction devHost(file?: string): string {return read(file).host;}/** The dev Worker's address, `https://<host>`. */exportfunction devBase(file?: string): string {return`https://${devHost(file)}`;}/** The slug of the personal object the import and the dev scripts address (`/b/<slug>`). */exportfunction personalSlug(file?: string): string {const slug = read(file).personalSlug;if (typeof slug !== "string" || !/^[a-z0-9-]+$/.test(slug)) thrownewError(`${file ?? DEV_SERVER_FILE}: "personalSlug" is no slug`);return slug;}/** The vars a dev deploy passes to wrangler (`--var NAME:value`), which wrangler.jsonc's env.dev leaves out. */exportfunction devDeployVars(file?: string): Record<string, string> {const server = read(file);return { OPEN_HOST: server.host, ...(server.fakeGoogleHost ? { GOOGLE_BASE: `https://${server.fakeGoogleHost}` } : {}), };}