feat(cloud): manage cloudflared installation and relay defaults
Co-authored-by: codex <[email protected]>
This commit is contained in:
1 parent
2b5213d920
commit
eadcf71a49
38 files changed
+1557
-74
No files matched your search
@@ -17,6 +17,10 @@ import * as Schema from "effect/Schema";
|
||||
import { Argument, Command, Flag } from "effect/unstable/cli";
|
||||
import { ChildProcess } from "effect/unstable/process";
|
||||
|
||||
import { loadRepoEnv } from "./lib/public-config.ts";
|
||||
|
||||
Object.assign(process.env, loadRepoEnv());
|
||||
|
||||
const BASE_SERVER_PORT = 13773;
|
||||
const BASE_WEB_PORT = 5733;
|
||||
const MAX_HASH_OFFSET = 3000;
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
// @effect-diagnostics nodeBuiltinImport:off - Tests exercise root env file precedence directly.
|
||||
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { afterEach, describe, expect, it } from "vitest";
|
||||
|
||||
import { DEFAULT_PUBLIC_CONFIG, loadRepoEnv, resolvePublicConfig } from "./public-config.ts";
|
||||
|
||||
const temporaryDirectories: string[] = [];
|
||||
|
||||
afterEach(() => {
|
||||
for (const directory of temporaryDirectories.splice(0)) {
|
||||
rmSync(directory, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
describe("loadRepoEnv", () => {
|
||||
it("projects checked-in public defaults into Vite and Expo aliases", () => {
|
||||
const env = loadRepoEnv({ baseEnv: {}, repoRoot: makeTemporaryDirectory() });
|
||||
|
||||
expect(env.T3CODE_CLERK_PUBLISHABLE_KEY).toBe(DEFAULT_PUBLIC_CONFIG.clerkPublishableKey);
|
||||
expect(env.VITE_CLERK_PUBLISHABLE_KEY).toBe(DEFAULT_PUBLIC_CONFIG.clerkPublishableKey);
|
||||
expect(env.EXPO_PUBLIC_CLERK_PUBLISHABLE_KEY).toBe(DEFAULT_PUBLIC_CONFIG.clerkPublishableKey);
|
||||
expect(env.T3_RELAY_URL).toBe(DEFAULT_PUBLIC_CONFIG.relayUrl);
|
||||
expect(env.VITE_T3_RELAY_URL).toBe(DEFAULT_PUBLIC_CONFIG.relayUrl);
|
||||
});
|
||||
|
||||
it("applies process, root local, root, and checked-in precedence in that order", () => {
|
||||
const repoRoot = makeTemporaryDirectory();
|
||||
writeFileSync(
|
||||
join(repoRoot, ".env"),
|
||||
"T3CODE_CLERK_PUBLISHABLE_KEY=pk_root\nT3_RELAY_URL=https://root.example.test\n",
|
||||
);
|
||||
writeFileSync(
|
||||
join(repoRoot, ".env.local"),
|
||||
"T3CODE_CLERK_PUBLISHABLE_KEY=pk_local\nT3_RELAY_URL=https://local.example.test\n",
|
||||
);
|
||||
|
||||
expect(loadRepoEnv({ baseEnv: {}, repoRoot }).T3_RELAY_URL).toBe("https://local.example.test");
|
||||
expect(
|
||||
loadRepoEnv({
|
||||
baseEnv: {
|
||||
T3CODE_CLERK_PUBLISHABLE_KEY: "pk_ci",
|
||||
T3_RELAY_URL: "https://ci.example.test",
|
||||
},
|
||||
repoRoot,
|
||||
}),
|
||||
).toMatchObject({
|
||||
T3CODE_CLERK_PUBLISHABLE_KEY: "pk_ci",
|
||||
VITE_CLERK_PUBLISHABLE_KEY: "pk_ci",
|
||||
EXPO_PUBLIC_CLERK_PUBLISHABLE_KEY: "pk_ci",
|
||||
T3_RELAY_URL: "https://ci.example.test",
|
||||
VITE_T3_RELAY_URL: "https://ci.example.test",
|
||||
});
|
||||
});
|
||||
|
||||
it("accepts legacy framework aliases as root overrides", () => {
|
||||
expect(
|
||||
resolvePublicConfig({
|
||||
VITE_CLERK_PUBLISHABLE_KEY: "pk_legacy",
|
||||
VITE_T3_RELAY_URL: "https://legacy.example.test",
|
||||
}),
|
||||
).toEqual({
|
||||
clerkPublishableKey: "pk_legacy",
|
||||
relayUrl: "https://legacy.example.test",
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
function makeTemporaryDirectory() {
|
||||
const directory = mkdtempSync(join(tmpdir(), "t3code-public-config-"));
|
||||
temporaryDirectories.push(directory);
|
||||
return directory;
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
// @effect-diagnostics nodeBuiltinImport:off - Build bootstrap reads optional root env files before an Effect runtime exists.
|
||||
import * as NodeFS from "node:fs";
|
||||
import * as NodePath from "node:path";
|
||||
import * as NodeURL from "node:url";
|
||||
import * as NodeUtil from "node:util";
|
||||
|
||||
import { DEFAULT_T3_CLERK_PUBLISHABLE_KEY, DEFAULT_T3_RELAY_URL } from "@t3tools/shared/relayAuth";
|
||||
|
||||
export interface T3CodePublicConfig {
|
||||
readonly clerkPublishableKey: string;
|
||||
readonly relayUrl: string;
|
||||
}
|
||||
|
||||
type Environment = Readonly<Record<string, string | undefined>>;
|
||||
|
||||
const REPO_ROOT = NodePath.dirname(
|
||||
NodePath.dirname(NodePath.dirname(NodeURL.fileURLToPath(import.meta.url))),
|
||||
);
|
||||
|
||||
// These values are intentionally public. Client builds embed them so a fresh clone can use
|
||||
// T3 Cloud without local setup. Use root .env files or CI environment variables to override them.
|
||||
export const DEFAULT_PUBLIC_CONFIG: T3CodePublicConfig = {
|
||||
clerkPublishableKey: DEFAULT_T3_CLERK_PUBLISHABLE_KEY,
|
||||
relayUrl: DEFAULT_T3_RELAY_URL,
|
||||
};
|
||||
|
||||
export function loadRepoEnv({
|
||||
baseEnv = process.env,
|
||||
repoRoot = REPO_ROOT,
|
||||
}: {
|
||||
readonly baseEnv?: Environment;
|
||||
readonly repoRoot?: string;
|
||||
} = {}): Record<string, string | undefined> {
|
||||
const rootEnv = readEnvFile(NodePath.join(repoRoot, ".env"));
|
||||
const localEnv = readEnvFile(NodePath.join(repoRoot, ".env.local"));
|
||||
const config = resolvePublicConfig(baseEnv, localEnv, rootEnv);
|
||||
|
||||
return {
|
||||
...rootEnv,
|
||||
...localEnv,
|
||||
...baseEnv,
|
||||
T3CODE_CLERK_PUBLISHABLE_KEY: config.clerkPublishableKey,
|
||||
T3_RELAY_URL: config.relayUrl,
|
||||
VITE_CLERK_PUBLISHABLE_KEY: config.clerkPublishableKey,
|
||||
VITE_T3_RELAY_URL: config.relayUrl,
|
||||
EXPO_PUBLIC_CLERK_PUBLISHABLE_KEY: config.clerkPublishableKey,
|
||||
};
|
||||
}
|
||||
|
||||
export function resolvePublicConfig(...sources: readonly Environment[]): T3CodePublicConfig {
|
||||
return {
|
||||
clerkPublishableKey:
|
||||
firstNonEmpty(
|
||||
sources,
|
||||
"T3CODE_CLERK_PUBLISHABLE_KEY",
|
||||
"VITE_CLERK_PUBLISHABLE_KEY",
|
||||
"EXPO_PUBLIC_CLERK_PUBLISHABLE_KEY",
|
||||
) ?? DEFAULT_PUBLIC_CONFIG.clerkPublishableKey,
|
||||
relayUrl:
|
||||
firstNonEmpty(sources, "T3_RELAY_URL", "VITE_T3_RELAY_URL") ?? DEFAULT_PUBLIC_CONFIG.relayUrl,
|
||||
};
|
||||
}
|
||||
|
||||
function firstNonEmpty(sources: readonly Environment[], ...names: readonly string[]) {
|
||||
for (const source of sources) {
|
||||
for (const name of names) {
|
||||
const value = source[name]?.trim();
|
||||
if (value) {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function readEnvFile(path: string): Record<string, string | undefined> {
|
||||
return NodeFS.existsSync(path) ? NodeUtil.parseEnv(NodeFS.readFileSync(path, "utf8")) : {};
|
||||
}
|
||||
Reference in new issue
Block a user