config.ts
Raw
1import path from "node:path";
2
3const env = process.env;
4
5/** parseInt with a default that distinguishes "unset" from "explicit 0". */
6function intEnv(value: string | undefined, defaultValue: number): number {
7 if (value === undefined || value === "") return defaultValue;
8 const parsed = parseInt(value, 10);
9 return Number.isFinite(parsed) ? parsed : defaultValue;
10}
11
12const config = {
13 OWNER_DISPLAY_NAME: env.OWNER_DISPLAY_NAME ?? "Admin",
14 INLINE_MAX_BYTES: parseInt(env.INLINE_MAX_BYTES ?? "", 10) || 524288,
15 MAX_UPLOAD_BYTES:
16 parseInt(env.MAX_UPLOAD_BYTES ?? "", 10) || 10 * 1024 * 1024,
17 MAX_USER_UPLOAD_BYTES:
18 parseInt(env.MAX_USER_UPLOAD_BYTES ?? "", 10) || 2 * 1024 * 1024,
19 TRUSTED_PROXY: !!env.TRUSTED_PROXY,
20 RATE_LIMIT_DISABLED: !!env.RATE_LIMIT_DISABLED,
21 SSH_DISABLED: !!env.SSH_DISABLED,
22 PORT: parseInt(env.PORT ?? "", 10) || 3000,
23 SSH_PORT: parseInt(env.SSH_PORT ?? "", 10) || 2222,
24 REGISTRATION_TYPE: (env.REGISTRATION_TYPE ?? "enabled") as
25 | "enabled"
26 | "disabled"
27 | "queue",
28 REGISTER_QUESTION: env.REGISTER_QUESTION ?? "",
29 BASE_URL:
30 env.BASE_URL ??
31 `http://localhost:${parseInt(env.PORT ?? "", 10) || 3000}`,
32 // Derived from BASE_URL after the literal closes (see below). Declared
33 // here so consumers get a typed config object.
34 PUBLIC_HTTPS: false,
35 PUBLIC_ORIGIN: "",
36 DATA_DIR: path.resolve(env.DATA_DIR ?? "./data"),
37 HIGHLIGHT_WORKERS: parseInt(env.HIGHLIGHT_WORKERS ?? "", 10) || 4,
38 COMMITTER_NAME: env.COMMITTER_NAME ?? env.OWNER_DISPLAY_NAME ?? "Admin",
39 COMMITTER_EMAIL: "",
40 EXTRA_ALLOWED_SIGNERS_PATH: env.EXTRA_ALLOWED_SIGNERS_PATH ?? null,
41 MAX_TITLE_BYTES: parseInt(env.MAX_TITLE_BYTES ?? "", 10) || 500,
42 MAX_TEXT_BODY_BYTES: parseInt(env.MAX_TEXT_BODY_BYTES ?? "", 10) || 100_000,
43 MAX_USERNAME_BYTES: parseInt(env.MAX_USERNAME_BYTES ?? "", 10) || 64,
44 MAX_PASSWORD_BYTES: parseInt(env.MAX_PASSWORD_BYTES ?? "", 10) || 1024,
45 CI_DOCKER_SOCKET: env.CI_DOCKER_SOCKET ?? "",
46 CI_MAX_HISTORY: parseInt(env.CI_MAX_HISTORY ?? "", 10) || 50,
47 CI_MAX_CONCURRENT: parseInt(env.CI_MAX_CONCURRENT ?? "", 10) || 2,
48 CI_DEFAULT_TIMEOUT: parseInt(env.CI_DEFAULT_TIMEOUT ?? "", 10) || 3600,
49 // Cap on simultaneous source archive generation jobs (one release with
50 // include_source_code spawns three git-archive + compressor pipelines
51 // back-to-back). Without this cap, an admin firing several releases in
52 // quick succession can saturate CPU. Excess jobs are queued in memory.
53 MAX_CONCURRENT_ARCHIVE_JOBS:
54 parseInt(env.MAX_CONCURRENT_ARCHIVE_JOBS ?? "", 10) || 2,
55 // Cap on any server-side render that holds the whole content in
56 // RAM and runs synchronous CPU work on it (blob view, commit/patch
57 // diff, markdown). Above this, the UI shows a "too large to
58 // preview" stub and links to the raw endpoint. Setting to 0
59 // disables inline rendering entirely.
60 MAX_RENDER_BYTES: intEnv(env.MAX_RENDER_BYTES, 10 * 1024 * 1024),
61 // Cap on the streamed /raw download. 0 means no limit (current
62 // behaviour) — useful on a trusted LAN where you actually want to
63 // pull large blobs out of the browser. Public deployments should
64 // pin this to something sane.
65 MAX_RAW_DOWNLOAD_BYTES: intEnv(env.MAX_RAW_DOWNLOAD_BYTES, 0),
66};
67
68// Derived values that depend on other config fields
69const baseUrl = new URL(config.BASE_URL);
70config.PUBLIC_HTTPS = baseUrl.protocol === "https:";
71config.PUBLIC_ORIGIN = baseUrl.origin;
72config.COMMITTER_EMAIL =
73 env.COMMITTER_EMAIL ?? `${config.OWNER_DISPLAY_NAME}@${baseUrl.hostname}`;
74
75export default config;
76