This repository has no description
1import { env } from "$env/dynamic/private";
2
3const cleanUrl = (value: string | undefined, fallback: string) => {
4 const raw = value?.trim() || fallback;
5 return raw.replace(/\/+$/, "");
6};
7
8export type WebConfig = {
9 bobbinUrl: string;
10 knotMirrorUrl: string;
11 apiUrl: string;
12 /** the domain user sites are served under, e.g. "tngl.io" */
13 sitesDomain: string;
14 camoUrl: string;
15 avatarUrl: string;
16 /** the secrets camo and avatar sign with, so neither leaves the server */
17 camoSecret: string;
18 avatarSecret: string;
19};
20
21export type PublicWebConfig = Pick<
22 WebConfig,
23 "bobbinUrl" | "knotMirrorUrl" | "apiUrl" | "sitesDomain"
24> & {
25 /** camo has a secret, so markup can route images through it */
26 camoEnabled: boolean;
27};
28
29type WebConfigEnv = {
30 BOBBIN_URL?: string;
31 KNOTMIRROR_URL?: string;
32 TANGLED_API_URL?: string;
33 API_URL?: string;
34 KNOT_RESOLVER_URL?: string;
35 SITES_DOMAIN?: string;
36 CAMO_URL?: string;
37 CAMO_SHARED_SECRET?: string;
38 AVATAR_URL?: string;
39 AVATAR_SHARED_SECRET?: string;
40};
41
42export const resolveConfig = (values: WebConfigEnv): WebConfig => ({
43 bobbinUrl: cleanUrl(values.BOBBIN_URL, "http://127.0.0.1:8090"),
44 knotMirrorUrl: cleanUrl(values.KNOTMIRROR_URL, ""),
45 apiUrl: cleanUrl(values.TANGLED_API_URL ?? values.API_URL, "http://127.0.0.1:8080"),
46 sitesDomain: values.SITES_DOMAIN?.trim() || "tngl.io",
47 camoUrl: cleanUrl(values.CAMO_URL, "https://camo.tangled.sh"),
48 avatarUrl: cleanUrl(values.AVATAR_URL, "https://avatar.tangled.sh"),
49 camoSecret: values.CAMO_SHARED_SECRET?.trim() ?? "",
50 avatarSecret: values.AVATAR_SHARED_SECRET?.trim() ?? ""
51});
52
53export const getConfig = (): WebConfig => resolveConfig(env as WebConfigEnv);
54
55// listed one by one, so anything new in WebConfig stays private until it is
56// named here
57export const getPublicConfig = (): PublicWebConfig => {
58 const config = getConfig();
59 return {
60 bobbinUrl: config.bobbinUrl,
61 knotMirrorUrl: config.knotMirrorUrl,
62 apiUrl: config.apiUrl,
63 sitesDomain: config.sitesDomain,
64 camoEnabled: config.camoSecret !== ""
65 };
66};