trilium/src/services/port.ts
Panagiotis Papadopoulos 31c46753de refactor(server/utils): isDev move to utils and replace fn with boolean
this value cannot change during runtime,
=> there is no need to have these checks
as dynamic function, instead just
export the boolean value directly
2025-01-29 10:58:00 +01:00

27 lines
812 B
TypeScript

import config from "./config.js";
import { isDev, isElectron } from "./utils.js";
import dataDir from "./data_dir.js";
function parseAndValidate(portStr: string, source: string) {
const portNum = parseInt(portStr);
if (isNaN(portNum) || portNum < 0 || portNum >= 65536) {
console.log(`FATAL ERROR: Invalid port value "${portStr}" from ${source}, should be an integer between 0 and 65536.`);
process.exit(-1);
}
return portNum;
}
let port: number;
if (process.env.TRILIUM_PORT) {
port = parseAndValidate(process.env.TRILIUM_PORT, "environment variable TRILIUM_PORT");
} else if (isElectron) {
port = isDev ? 37740 : 37840;
} else {
port = parseAndValidate(config["Network"]["port"] || "3000", `Network.port in ${dataDir.CONFIG_INI_PATH}`);
}
export default port;