From 0eb2b727f6fbb8206ae9de9138b347cc6590ba14 Mon Sep 17 00:00:00 2001 From: Chengzhong Wu Date: Mon, 11 Mar 2024 11:04:27 +0800 Subject: [PATCH] src: return a number from process.constrainedMemory() constantly MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `0` is already a special value returned from `uv_get_constrained_memory` representing unknown or no constraint. Make `process.constrainedMemory()` constantly return a number instead to avoid polymorphic return type. PR-URL: https://github.com/nodejs/node/pull/52039 Reviewed-By: theanarkh Reviewed-By: Luigi Pinca Reviewed-By: Yagiz Nizipli Reviewed-By: Vinícius Lourenço Claro Cardoso --- doc/api/process.md | 8 ++++++-- src/node_process_methods.cc | 4 +--- test/parallel/test-process-constrained-memory.js | 10 ++-------- 3 files changed, 9 insertions(+), 13 deletions(-) diff --git a/doc/api/process.md b/doc/api/process.md index b0cbebcc148de8..ba0891a66a9652 100644 --- a/doc/api/process.md +++ b/doc/api/process.md @@ -1114,15 +1114,19 @@ over the IPC channel using `process.send()`. added: - v19.6.0 - v18.15.0 +changes: + - version: REPLACEME + pr-url: https://github.com/nodejs/node/pull/52039 + description: Aligned return value with `uv_get_constrained_memory`. --> > Stability: 1 - Experimental -* {number|undefined} +* {number} Gets the amount of memory available to the process (in bytes) based on limits imposed by the OS. If there is no such constraint, or the constraint -is unknown, `undefined` is returned. +is unknown, `0` is returned. See [`uv_get_constrained_memory`][uv_get_constrained_memory] for more information. diff --git a/src/node_process_methods.cc b/src/node_process_methods.cc index b7e68dc1d5d074..5266aae51f677b 100644 --- a/src/node_process_methods.cc +++ b/src/node_process_methods.cc @@ -211,9 +211,7 @@ static void MemoryUsage(const FunctionCallbackInfo& args) { static void GetConstrainedMemory(const FunctionCallbackInfo& args) { uint64_t value = uv_get_constrained_memory(); - if (value != 0) { - args.GetReturnValue().Set(static_cast(value)); - } + args.GetReturnValue().Set(static_cast(value)); } static void GetAvailableMemory(const FunctionCallbackInfo& args) { diff --git a/test/parallel/test-process-constrained-memory.js b/test/parallel/test-process-constrained-memory.js index 2bd94a6749e56b..03f99b166f72ca 100644 --- a/test/parallel/test-process-constrained-memory.js +++ b/test/parallel/test-process-constrained-memory.js @@ -1,12 +1,6 @@ 'use strict'; require('../common'); const assert = require('assert'); -const { Worker } = require('worker_threads'); + const constrainedMemory = process.constrainedMemory(); -if (constrainedMemory !== undefined) { - assert(constrainedMemory > 0); -} -if (!process.env.isWorker) { - process.env.isWorker = true; - new Worker(__filename); -} +assert.strictEqual(typeof constrainedMemory, 'number');