2025-07-26 20:42:58 +02:00
|
|
|
import { dirname } from 'node:path';
|
|
|
|
|
import { fileURLToPath } from 'node:url';
|
|
|
|
|
import { readFile } from 'fs/promises';
|
|
|
|
|
import { createHash } from 'crypto';
|
|
|
|
|
import { DEFAULT_CONFIG } from './defaultConfig.js';
|
2025-09-20 19:37:27 +02:00
|
|
|
import fs, { readFileSync } from 'fs';
|
2025-09-13 18:57:56 +02:00
|
|
|
import logger from './services/logger.js';
|
2025-09-20 19:37:27 +02:00
|
|
|
import { packageUp } from 'package-up';
|
2025-09-09 18:41:14 +02:00
|
|
|
|
|
|
|
|
const RE_GT = />/g;
|
|
|
|
|
const RE_WEBP = /\/format\/webp/gi;
|
|
|
|
|
const RE_EXT = /\.(jpe?g|png|gif)(\?.*)?$/i;
|
|
|
|
|
const HTTPS_PREFIX = 'https://';
|
2023-03-13 13:42:43 +01:00
|
|
|
|
2025-09-18 15:38:23 +02:00
|
|
|
/**
|
|
|
|
|
* Safely stringify a value to JSON for storage.
|
|
|
|
|
* - Returns null when the input is null or undefined.
|
|
|
|
|
* - Uses JSON.stringify directly otherwise.
|
|
|
|
|
*
|
|
|
|
|
* @template T
|
|
|
|
|
* @param {T} v - Any JSON-serializable value.
|
|
|
|
|
* @returns {string|null} JSON string or null.
|
|
|
|
|
*/
|
|
|
|
|
export const toJson = (v) => (v == null ? null : JSON.stringify(v));
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Safely parse JSON text coming from storage.
|
|
|
|
|
* - Returns the provided fallback when input is null/undefined.
|
|
|
|
|
* - Returns the fallback when parsing fails.
|
|
|
|
|
*
|
|
|
|
|
* @template T
|
|
|
|
|
* @param {string|null|undefined} txt - JSON text from DB/storage.
|
|
|
|
|
* @param {T} fallback - Value to return when txt is null/invalid.
|
|
|
|
|
* @returns {T} Parsed value or fallback.
|
|
|
|
|
*/
|
|
|
|
|
export const fromJson = (txt, fallback) => {
|
|
|
|
|
if (txt == null) return fallback;
|
|
|
|
|
try {
|
|
|
|
|
return JSON.parse(txt);
|
|
|
|
|
} catch {
|
|
|
|
|
return fallback;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Determine if the current process runs in development mode.
|
|
|
|
|
* Returns true when NODE_ENV is not 'production'.
|
|
|
|
|
* @returns {boolean}
|
|
|
|
|
*/
|
2025-07-26 20:42:58 +02:00
|
|
|
function inDevMode() {
|
|
|
|
|
return process.env.NODE_ENV == null || process.env.NODE_ENV !== 'production';
|
2024-11-22 09:11:10 +01:00
|
|
|
}
|
|
|
|
|
|
2025-09-18 15:38:23 +02:00
|
|
|
/**
|
|
|
|
|
* Check if a word contains any of the strings in the given array (case-insensitive, substring match).
|
|
|
|
|
* @param {string} word
|
|
|
|
|
* @param {string[]} arr
|
|
|
|
|
* @returns {boolean}
|
|
|
|
|
*/
|
2020-02-26 09:05:20 +01:00
|
|
|
function isOneOf(word, arr) {
|
2025-07-26 20:42:58 +02:00
|
|
|
if (!arr || arr.length === 0 || word == null) return false;
|
|
|
|
|
const lowerWord = word.toLowerCase();
|
|
|
|
|
return arr.some((item) => lowerWord.indexOf(item.toLowerCase()) !== -1);
|
2018-01-20 20:23:27 +01:00
|
|
|
}
|
2024-09-05 13:34:14 +02:00
|
|
|
|
2025-09-18 15:38:23 +02:00
|
|
|
/**
|
|
|
|
|
* Check if a value is null or an empty string/array.
|
|
|
|
|
* @param {any} val
|
|
|
|
|
* @returns {boolean}
|
|
|
|
|
*/
|
2021-05-30 09:37:45 +02:00
|
|
|
function nullOrEmpty(val) {
|
2025-07-26 20:42:58 +02:00
|
|
|
return val == null || val.length === 0;
|
2021-05-30 09:37:45 +02:00
|
|
|
}
|
2024-09-05 13:34:14 +02:00
|
|
|
|
2025-09-18 15:38:23 +02:00
|
|
|
/**
|
|
|
|
|
* Convert a day time string (HH:mm) to epoch milliseconds for the given reference date.
|
|
|
|
|
* @param {string} timeString - Format HH:mm
|
|
|
|
|
* @param {number} now - Epoch ms used as the date basis
|
|
|
|
|
* @returns {number}
|
|
|
|
|
*/
|
2021-05-30 09:37:45 +02:00
|
|
|
function timeStringToMs(timeString, now) {
|
2025-07-26 20:42:58 +02:00
|
|
|
const d = new Date(now);
|
|
|
|
|
const parts = timeString.split(':');
|
|
|
|
|
d.setHours(parts[0]);
|
|
|
|
|
d.setMinutes(parts[1]);
|
|
|
|
|
d.setSeconds(0);
|
|
|
|
|
return d.getTime();
|
2021-05-30 09:37:45 +02:00
|
|
|
}
|
2024-09-05 13:34:14 +02:00
|
|
|
|
2025-09-18 15:38:23 +02:00
|
|
|
/**
|
|
|
|
|
* Check whether current time is within configured working hours, or no hours are set.
|
|
|
|
|
* If working hours are missing or incomplete, returns true.
|
|
|
|
|
* @param {{workingHours?: {from?: string, to?: string}}} config
|
|
|
|
|
* @param {number} now - Epoch ms
|
|
|
|
|
* @returns {boolean}
|
|
|
|
|
*/
|
2021-05-30 09:37:45 +02:00
|
|
|
function duringWorkingHoursOrNotSet(config, now) {
|
2025-07-26 20:42:58 +02:00
|
|
|
const { workingHours } = config;
|
|
|
|
|
if (workingHours == null || nullOrEmpty(workingHours.from) || nullOrEmpty(workingHours.to)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
const toDate = timeStringToMs(workingHours.to, now);
|
|
|
|
|
const fromDate = timeStringToMs(workingHours.from, now);
|
|
|
|
|
return fromDate <= now && toDate >= now;
|
2021-05-30 09:37:45 +02:00
|
|
|
}
|
|
|
|
|
|
2025-09-18 15:38:23 +02:00
|
|
|
/**
|
|
|
|
|
* Return the directory name of the current module (ESM equivalent of __dirname).
|
|
|
|
|
* @returns {string}
|
|
|
|
|
*/
|
2023-03-13 13:42:43 +01:00
|
|
|
function getDirName() {
|
2025-07-26 20:42:58 +02:00
|
|
|
return dirname(fileURLToPath(import.meta.url));
|
2024-09-05 13:34:14 +02:00
|
|
|
}
|
|
|
|
|
|
2025-09-18 15:38:23 +02:00
|
|
|
/**
|
|
|
|
|
* Build a sha256 hash string from the provided inputs (ignores null/empty strings).
|
|
|
|
|
* Returns null if there are no valid inputs.
|
|
|
|
|
* @param {...(string|null|undefined)} inputs
|
|
|
|
|
* @returns {string|null}
|
|
|
|
|
*/
|
2024-09-05 13:34:14 +02:00
|
|
|
function buildHash(...inputs) {
|
2025-07-26 20:42:58 +02:00
|
|
|
if (inputs == null) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
const cleaned = inputs.filter((i) => i != null && i.length > 0);
|
|
|
|
|
if (cleaned.length === 0) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return createHash('sha256').update(cleaned.join(',')).digest('hex');
|
2023-03-13 13:42:43 +01:00
|
|
|
}
|
|
|
|
|
|
2025-09-18 15:38:23 +02:00
|
|
|
/**
|
|
|
|
|
* The in-memory configuration object. Call refreshConfig() to populate/update.
|
|
|
|
|
* @type {any}
|
|
|
|
|
*/
|
2024-11-20 22:22:16 +01:00
|
|
|
let config = {};
|
2025-09-09 18:41:14 +02:00
|
|
|
|
2025-09-18 15:38:23 +02:00
|
|
|
/**
|
|
|
|
|
* Read config JSON from disk (conf/config.json) and parse it.
|
|
|
|
|
* @returns {Promise<any>} Parsed configuration object.
|
|
|
|
|
*/
|
2025-07-26 20:42:58 +02:00
|
|
|
export async function readConfigFromStorage() {
|
|
|
|
|
return JSON.parse(await readFile(new URL('../conf/config.json', import.meta.url)));
|
2024-11-20 22:22:16 +01:00
|
|
|
}
|
|
|
|
|
|
2025-09-18 15:38:23 +02:00
|
|
|
/**
|
|
|
|
|
* Refresh the in-memory config, ensuring the file exists and setting backward-compatible defaults.
|
|
|
|
|
* Populates defaults for analyticsEnabled, demoMode, sqlitepath when missing.
|
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
|
*/
|
2025-07-26 20:42:58 +02:00
|
|
|
export async function refreshConfig() {
|
2025-09-09 18:41:14 +02:00
|
|
|
checkIfConfigExistsAndWriteIfNot();
|
|
|
|
|
|
2025-07-26 20:42:58 +02:00
|
|
|
try {
|
|
|
|
|
config = await readConfigFromStorage();
|
2025-09-18 15:38:23 +02:00
|
|
|
//backwards compatibility...
|
2025-07-26 20:42:58 +02:00
|
|
|
config.analyticsEnabled ??= null;
|
|
|
|
|
config.demoMode ??= false;
|
2025-09-18 15:38:23 +02:00
|
|
|
// default sqlitepath when missing in older configs
|
|
|
|
|
config.sqlitepath ??= '/db';
|
2025-07-26 20:42:58 +02:00
|
|
|
} catch (error) {
|
|
|
|
|
config = { ...DEFAULT_CONFIG };
|
2025-09-13 18:57:56 +02:00
|
|
|
logger.info('Error reading config file.', error);
|
2025-07-26 20:42:58 +02:00
|
|
|
}
|
2024-11-20 22:22:16 +01:00
|
|
|
}
|
2025-08-30 21:21:34 +02:00
|
|
|
|
2025-09-09 18:41:14 +02:00
|
|
|
/**
|
2025-09-18 15:38:23 +02:00
|
|
|
* If the config file does not exist, create it with DEFAULT_CONFIG.
|
|
|
|
|
* @returns {void}
|
2025-09-09 18:41:14 +02:00
|
|
|
*/
|
|
|
|
|
const checkIfConfigExistsAndWriteIfNot = () => {
|
|
|
|
|
if (!fs.existsSync(`${getDirName()}/../conf/config.json`)) {
|
2025-09-13 18:57:56 +02:00
|
|
|
logger.info('Could not find config file. Will create one with default values now');
|
2025-09-09 18:41:14 +02:00
|
|
|
fs.writeFileSync(`${getDirName()}/../conf/config.json`, JSON.stringify({ ...DEFAULT_CONFIG }));
|
|
|
|
|
}
|
|
|
|
|
};
|
2025-08-30 21:21:34 +02:00
|
|
|
|
2025-09-18 15:38:23 +02:00
|
|
|
/**
|
|
|
|
|
* Normalize image URLs:
|
|
|
|
|
* - Trim, remove stray '>' characters.
|
|
|
|
|
* - Convert '/format/webp' segments to '/format/jpg'.
|
|
|
|
|
* - Enforce HTTPS and ensure a valid image extension (jpg/png/gif). If URL contains '.jpg' without query, cut trailing parts.
|
|
|
|
|
* - Return null for invalid inputs.
|
|
|
|
|
* @param {string} url
|
|
|
|
|
* @returns {string|null}
|
|
|
|
|
*/
|
2025-08-30 21:21:34 +02:00
|
|
|
const normalizeImageUrl = (url) => {
|
|
|
|
|
if (typeof url !== 'string' || url.length === 0) return null;
|
|
|
|
|
|
|
|
|
|
let u = url.trim().replace(RE_GT, '');
|
|
|
|
|
if (RE_WEBP.test(u)) u = u.replace(RE_WEBP, '/format/jpg');
|
|
|
|
|
if (!u.startsWith(HTTPS_PREFIX)) return null;
|
|
|
|
|
if (!RE_EXT.test(u)) {
|
|
|
|
|
const jpgIdx = u.toLowerCase().lastIndexOf('.jpg');
|
|
|
|
|
if (jpgIdx > -1) u = u.slice(0, jpgIdx + 4);
|
|
|
|
|
}
|
|
|
|
|
return u;
|
|
|
|
|
};
|
|
|
|
|
|
2025-09-20 19:37:27 +02:00
|
|
|
/**
|
|
|
|
|
* returns Fredy's version
|
|
|
|
|
* @returns {Promise<*|string>}
|
|
|
|
|
*/
|
|
|
|
|
async function getPackageVersion() {
|
|
|
|
|
try {
|
|
|
|
|
const packagePath = await packageUp();
|
|
|
|
|
const packageJson = readFileSync(packagePath, 'utf8');
|
|
|
|
|
const json = JSON.parse(packageJson);
|
|
|
|
|
return json.version;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
logger.error('Error reading version from package.json', error);
|
|
|
|
|
}
|
|
|
|
|
return 'N/A';
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-20 22:22:16 +01:00
|
|
|
await refreshConfig();
|
2023-03-13 13:42:43 +01:00
|
|
|
|
2025-07-26 20:42:58 +02:00
|
|
|
export { isOneOf };
|
2025-08-30 21:21:34 +02:00
|
|
|
export { normalizeImageUrl };
|
2025-07-26 20:42:58 +02:00
|
|
|
export { inDevMode };
|
|
|
|
|
export { nullOrEmpty };
|
|
|
|
|
export { duringWorkingHoursOrNotSet };
|
|
|
|
|
export { getDirName };
|
|
|
|
|
export { config };
|
|
|
|
|
export { buildHash };
|
2025-09-20 19:37:27 +02:00
|
|
|
export { getPackageVersion };
|
2023-03-13 13:42:43 +01:00
|
|
|
export default {
|
2025-07-26 20:42:58 +02:00
|
|
|
isOneOf,
|
|
|
|
|
nullOrEmpty,
|
|
|
|
|
duringWorkingHoursOrNotSet,
|
|
|
|
|
getDirName,
|
|
|
|
|
config,
|
2025-09-18 15:38:23 +02:00
|
|
|
toJson,
|
|
|
|
|
fromJson,
|
2023-03-13 13:42:43 +01:00
|
|
|
};
|