feat(cloud): make relay public config opt-in
Co-authored-by: codex <[email protected]>
This commit is contained in:
1 parent
c2d004a20e
commit
4600dd719f
26 files changed
+411
-309
No files matched your search
@@ -4,7 +4,7 @@ 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";
|
||||
import { loadRepoEnv, resolvePublicConfig } from "./public-config.ts";
|
||||
|
||||
const temporaryDirectories: string[] = [];
|
||||
|
||||
@@ -15,17 +15,17 @@ afterEach(() => {
|
||||
});
|
||||
|
||||
describe("loadRepoEnv", () => {
|
||||
it("projects checked-in public defaults into Vite and Expo aliases", () => {
|
||||
it("does not project cloud configuration for an unconfigured clone", () => {
|
||||
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);
|
||||
expect(env.T3CODE_CLERK_PUBLISHABLE_KEY).toBeUndefined();
|
||||
expect(env.VITE_CLERK_PUBLISHABLE_KEY).toBeUndefined();
|
||||
expect(env.EXPO_PUBLIC_CLERK_PUBLISHABLE_KEY).toBeUndefined();
|
||||
expect(env.T3_RELAY_URL).toBeUndefined();
|
||||
expect(env.VITE_T3_RELAY_URL).toBeUndefined();
|
||||
});
|
||||
|
||||
it("applies process, root local, root, and checked-in precedence in that order", () => {
|
||||
it("applies process, root local, and root precedence in that order", () => {
|
||||
const repoRoot = makeTemporaryDirectory();
|
||||
writeFileSync(
|
||||
join(repoRoot, ".env"),
|
||||
@@ -65,6 +65,20 @@ describe("loadRepoEnv", () => {
|
||||
relayUrl: "https://legacy.example.test",
|
||||
});
|
||||
});
|
||||
|
||||
it("projects only the configured aliases", () => {
|
||||
expect(
|
||||
loadRepoEnv({
|
||||
baseEnv: {
|
||||
T3_RELAY_URL: "https://relay.example.test",
|
||||
},
|
||||
repoRoot: makeTemporaryDirectory(),
|
||||
}),
|
||||
).toEqual({
|
||||
T3_RELAY_URL: "https://relay.example.test",
|
||||
VITE_T3_RELAY_URL: "https://relay.example.test",
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
function makeTemporaryDirectory() {
|
||||
|
||||
@@ -4,11 +4,9 @@ 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;
|
||||
readonly clerkPublishableKey: string | undefined;
|
||||
readonly relayUrl: string | undefined;
|
||||
}
|
||||
|
||||
type Environment = Readonly<Record<string, string | undefined>>;
|
||||
@@ -17,13 +15,6 @@ 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,
|
||||
@@ -39,25 +30,31 @@ export function loadRepoEnv({
|
||||
...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,
|
||||
...(config.clerkPublishableKey
|
||||
? {
|
||||
T3CODE_CLERK_PUBLISHABLE_KEY: config.clerkPublishableKey,
|
||||
VITE_CLERK_PUBLISHABLE_KEY: config.clerkPublishableKey,
|
||||
EXPO_PUBLIC_CLERK_PUBLISHABLE_KEY: config.clerkPublishableKey,
|
||||
}
|
||||
: {}),
|
||||
...(config.relayUrl
|
||||
? {
|
||||
T3_RELAY_URL: config.relayUrl,
|
||||
VITE_T3_RELAY_URL: config.relayUrl,
|
||||
}
|
||||
: {}),
|
||||
};
|
||||
}
|
||||
|
||||
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,
|
||||
clerkPublishableKey: firstNonEmpty(
|
||||
sources,
|
||||
"T3CODE_CLERK_PUBLISHABLE_KEY",
|
||||
"VITE_CLERK_PUBLISHABLE_KEY",
|
||||
"EXPO_PUBLIC_CLERK_PUBLISHABLE_KEY",
|
||||
),
|
||||
relayUrl: firstNonEmpty(sources, "T3_RELAY_URL", "VITE_T3_RELAY_URL"),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user