From ca25682495b0ae43193cac62415fc62ed3949bdd Mon Sep 17 00:00:00 2001 From: "CanbiZ (MickLesk)" <47820557+MickLesk@users.noreply.github.com> Date: Thu, 23 Apr 2026 13:51:35 +0200 Subject: [PATCH] feat(nodejs): auto-size NODE_OPTIONS heap and apply in Termix updates - setup_nodejs now sets NODE_OPTIONS only when not already set - Heap size is auto-derived from NODE_MAX_OLD_SPACE_SIZE, var_ram, or MemTotal - Auto heap is clamped to 1024..4096 MB to avoid too-small or too-large defaults - Termix update path now calls setup_nodejs before frontend/backend builds so Node heap defaults are applied consistently during updates --- ct/termix.sh | 2 ++ misc/tools.func | 32 +++++++++++++++++++++++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/ct/termix.sh b/ct/termix.sh index c89d09e31..d58c940f8 100644 --- a/ct/termix.sh +++ b/ct/termix.sh @@ -155,6 +155,8 @@ EOF /opt/termix/nginx/client_body msg_ok "Recreated Directories" + NODE_VERSION="24" setup_nodejs + msg_info "Building Frontend" cd /opt/termix export COREPACK_ENABLE_DOWNLOAD_PROMPT=0 diff --git a/misc/tools.func b/misc/tools.func index aa9bf4fed..87b848326 100644 --- a/misc/tools.func +++ b/misc/tools.func @@ -6420,7 +6420,37 @@ function setup_nodejs() { msg_ok "Setup Node.js $NODE_VERSION" fi - export NODE_OPTIONS="--max-old-space-size=4096" + # Set a safe default heap limit for Node.js builds if not explicitly provided. + # Priority: + # 1) NODE_OPTIONS (caller/user override) + # 2) NODE_MAX_OLD_SPACE_SIZE (explicit MB override) + # 3) var_ram (LXC memory setting, MB) + # 4) /proc/meminfo (runtime memory detection) + # Auto value is clamped to 1024..4096 MB. + if [[ -z "${NODE_OPTIONS:-}" ]]; then + local node_heap_mb="" + + if [[ -n "${NODE_MAX_OLD_SPACE_SIZE:-}" ]] && [[ "${NODE_MAX_OLD_SPACE_SIZE}" =~ ^[0-9]+$ ]]; then + node_heap_mb="${NODE_MAX_OLD_SPACE_SIZE}" + elif [[ -n "${var_ram:-}" ]] && [[ "${var_ram}" =~ ^[0-9]+$ ]]; then + node_heap_mb=$((var_ram * 75 / 100)) + else + local total_mem_kb="" + total_mem_kb=$(awk '/^MemTotal:/ {print $2; exit}' /proc/meminfo 2>/dev/null || echo "") + if [[ "$total_mem_kb" =~ ^[0-9]+$ ]]; then + local total_mem_mb=$((total_mem_kb / 1024)) + node_heap_mb=$((total_mem_mb * 75 / 100)) + fi + fi + + if [[ -z "$node_heap_mb" ]] || ((node_heap_mb < 1024)); then + node_heap_mb=1024 + elif ((node_heap_mb > 4096)); then + node_heap_mb=4096 + fi + + export NODE_OPTIONS="--max-old-space-size=${node_heap_mb}" + fi # Ensure valid working directory for npm (avoids uv_cwd error) if [[ ! -d /opt ]]; then