File size: 6,876 Bytes
5ec2e9b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import { z, ZodError } from "zod";
import { parse } from "@std/toml";

export const ConfigSchema = z.object({
    server: z.object({
        port: z.number().default(Number(Deno.env.get("PORT")) || 8282),
        host: z.string().default(Deno.env.get("HOST") || "127.0.0.1"),
        use_unix_socket: z.boolean().default(
            Deno.env.get("SERVER_USE_UNIX_SOCKET") === "true" || false,
        ),
        unix_socket_path: z.string().default(
            Deno.env.get("SERVER_UNIX_SOCKET_PATH") ||
            "/tmp/invidious-companion.sock",
        ),
        base_path: z.string()
            .default(Deno.env.get("SERVER_BASE_PATH") || "/companion")
            .refine(
                (path) => path.startsWith("/"),
                {
                    message:
                        "SERVER_BASE_PATH must start with a forward slash (/). Example: '/companion'",
                },
            )
            .refine(
                (path) => !path.endsWith("/") || path === "/",
                {
                    message:
                        "SERVER_BASE_PATH must not end with a forward slash (/) unless it's the root path. Example: '/companion' not '/companion/'",
                },
            )
            .refine(
                (path) => !path.includes("//"),
                {
                    message:
                        "SERVER_BASE_PATH must not contain double slashes (//). Example: '/companion' not '//companion' or '/comp//anion'",
                },
            ),
        secret_key: z.preprocess(
            (val) =>
                val === undefined
                    ? Deno.env.get("SERVER_SECRET_KEY") || ""
                    : val,
            z.string().min(1),
        ).default(undefined),
        verify_requests: z.boolean().default(
            Deno.env.get("SERVER_VERIFY_REQUESTS") === "true" || false,
        ),
        encrypt_query_params: z.boolean().default(
            Deno.env.get("SERVER_ENCRYPT_QUERY_PARAMS") === "true" || false,
        ),
        enable_metrics: z.boolean().default(
            Deno.env.get("SERVER_ENABLE_METRICS") === "true" || false,
        ),
    }).strict().default({}),
    cache: z.object({
        enabled: z.boolean().default(
            Deno.env.get("CACHE_ENABLED") === "false" ? false : true,
        ),
        directory: z.string().default(
            Deno.env.get("CACHE_DIRECTORY") || "/var/tmp",
        ),
    }).strict().default({}),
    networking: z.object({
        proxy: z.string().nullable().default(Deno.env.get("PROXY") || null),
        auto_proxy: z.boolean().default(
            Deno.env.get("NETWORKING_AUTO_PROXY") === "true" || false,
        ),
        vpn_source: z.number().default(
            Number(Deno.env.get("NETWORKING_VPN_SOURCE")) || 1,
        ),
        ipv6_block: z.string().nullable().default(
            Deno.env.get("NETWORKING_IPV6_BLOCK") || null,
        ),
        fetch: z.object({
            timeout_ms: z.number().default(
                Number(Deno.env.get("NETWORKING_FETCH_TIMEOUT_MS")) || 30_000,
            ),
            retry: z.object({
                enabled: z.boolean().default(
                    Deno.env.get("NETWORKING_FETCH_RETRY_ENABLED") === "true" ||
                    false,
                ),
                times: z.number().optional().default(
                    Number(Deno.env.get("NETWORKING_FETCH_RETRY_TIMES")) || 1,
                ),
                initial_debounce: z.number().optional().default(
                    Number(
                        Deno.env.get("NETWORKING_FETCH_RETRY_INITIAL_DEBOUNCE"),
                    ) || 0,
                ),
                debounce_multiplier: z.number().optional().default(
                    Number(
                        Deno.env.get(
                            "NETWORKING_FETCH_RETRY_DEBOUNCE_MULTIPLIER",
                        ),
                    ) || 0,
                ),
            }).strict().default({}),
        }).strict().default({}),
        videoplayback: z.object({
            ump: z.boolean().default(
                Deno.env.get("NETWORKING_VIDEOPLAYBACK_UMP") === "true" ||
                false,
            ),
            video_fetch_chunk_size_mb: z.number().default(
                Number(
                    Deno.env.get(
                        "NETWORKING_VIDEOPLAYBACK_VIDEO_FETCH_CHUNK_SIZE_MB",
                    ),
                ) || 5,
            ),
        }).strict().default({}),
    }).strict().default({}),
    jobs: z.object({
        youtube_session: z.object({
            po_token_enabled: z.boolean().default(
                Deno.env.get("JOBS_YOUTUBE_SESSION_PO_TOKEN_ENABLED") ===
                    "false"
                    ? false
                    : true,
            ),
            frequency: z.string().default(
                Deno.env.get("JOBS_YOUTUBE_SESSION_FREQUENCY") || "*/5 * * * *",
            ),
        }).strict().default({}),
    }).strict().default({}),
    youtube_session: z.object({
        oauth_enabled: z.boolean().default(
            Deno.env.get("YOUTUBE_SESSION_OAUTH_ENABLED") === "true" || false,
        ),
        cookies: z.string().default(
            Deno.env.get("YOUTUBE_SESSION_COOKIES") || "",
        ),
    }).strict().default({}),
}).strict();

export type Config = z.infer<typeof ConfigSchema>;

export async function parseConfig() {
    const configFileName = Deno.env.get("CONFIG_FILE") || "config/config.toml";
    const configFileContents = await Deno.readTextFile(configFileName).catch(
        () => null,
    );
    if (configFileContents) {
        console.log("[INFO] Using custom settings local file");
    } else {
        console.log(
            "[INFO] No local config file found, using default config",
        );
    }

    try {
        const rawConfig = configFileContents ? parse(configFileContents) : {};
        const validatedConfig = ConfigSchema.parse(rawConfig);

        console.log("Loaded Configuration", validatedConfig);

        return validatedConfig;
    } catch (err) {
        let errorMessage =
            "There is an error in your configuration, check your environment variables";
        if (configFileContents) {
            errorMessage +=
                ` or in your configuration file located at ${configFileName}`;
        }
        console.log(errorMessage);
        if (err instanceof ZodError) {
            console.log(err.issues);
            // Include detailed error information in the thrown error for testing
            const detailedMessage = err.issues.map((issue) =>
                `${issue.path.join(".")}: ${issue.message}`
            ).join("; ");
            throw new Error(
                `Failed to parse configuration file: ${detailedMessage}`,
            );
        }
        // rethrow error if not Zod
        throw err;
    }
}