Skip to content

Commit 0eb2b72

Browse files
committed
src: return a number from process.constrainedMemory() constantly
`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: #52039 Reviewed-By: theanarkh <theratliter@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz.nizipli@sentry.io> Reviewed-By: Vinícius Lourenço Claro Cardoso <contact@viniciusl.com.br>
1 parent 20525f1 commit 0eb2b72

File tree

3 files changed

+9
-13
lines changed

3 files changed

+9
-13
lines changed

doc/api/process.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -1114,15 +1114,19 @@ over the IPC channel using `process.send()`.
11141114
added:
11151115
- v19.6.0
11161116
- v18.15.0
1117+
changes:
1118+
- version: REPLACEME
1119+
pr-url: https://github.com/nodejs/node/pull/52039
1120+
description: Aligned return value with `uv_get_constrained_memory`.
11171121
-->
11181122

11191123
> Stability: 1 - Experimental
11201124
1121-
* {number|undefined}
1125+
* {number}
11221126

11231127
Gets the amount of memory available to the process (in bytes) based on
11241128
limits imposed by the OS. If there is no such constraint, or the constraint
1125-
is unknown, `undefined` is returned.
1129+
is unknown, `0` is returned.
11261130

11271131
See [`uv_get_constrained_memory`][uv_get_constrained_memory] for more
11281132
information.

src/node_process_methods.cc

+1-3
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,7 @@ static void MemoryUsage(const FunctionCallbackInfo<Value>& args) {
211211

212212
static void GetConstrainedMemory(const FunctionCallbackInfo<Value>& args) {
213213
uint64_t value = uv_get_constrained_memory();
214-
if (value != 0) {
215-
args.GetReturnValue().Set(static_cast<double>(value));
216-
}
214+
args.GetReturnValue().Set(static_cast<double>(value));
217215
}
218216

219217
static void GetAvailableMemory(const FunctionCallbackInfo<Value>& args) {
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
'use strict';
22
require('../common');
33
const assert = require('assert');
4-
const { Worker } = require('worker_threads');
4+
55
const constrainedMemory = process.constrainedMemory();
6-
if (constrainedMemory !== undefined) {
7-
assert(constrainedMemory > 0);
8-
}
9-
if (!process.env.isWorker) {
10-
process.env.isWorker = true;
11-
new Worker(__filename);
12-
}
6+
assert.strictEqual(typeof constrainedMemory, 'number');

0 commit comments

Comments
 (0)