Skip to content

Commit da61279

Browse files
committed
lib,test: validate metrics interval updates
Validate interval updates in lib/nsolid.js before assigning them to nsolidConfig.interval. Ignore invalid values such as 0, negative numbers, NaN, Infinity, and non-numeric strings so the previous valid interval is preserved. Add a parallel test covering valid coercion, invalid updates, and partial updates that omit interval.
1 parent 66f4181 commit da61279

5 files changed

Lines changed: 173 additions & 3 deletions

File tree

lib/nsolid.js

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -771,7 +771,10 @@ function updateConfig(config = {}) {
771771
nsolidConfig.assetsEnabled = normalized;
772772
}
773773
} else if (key === 'interval') {
774-
nsolidConfig.interval = +config.interval;
774+
const normalized = parsePositiveFiniteNumber(config.interval);
775+
if (normalized !== undefined) {
776+
nsolidConfig.interval = normalized;
777+
}
775778
} else if (key === 'traceSampleRate') {
776779
const normalized = parseTraceSampleRate(config.traceSampleRate);
777780
if (normalized !== undefined) {
@@ -909,9 +912,11 @@ function initializeConfig(nsolidConfig) {
909912
'prod';
910913

911914
// Metrics send interval
915+
const envInterval = parsePositiveFiniteNumber(process.env.NSOLID_INTERVAL);
916+
const pkgInterval = parsePositiveFiniteNumber(pkgConfig.nsolid.interval);
912917
nsolidConfig.interval =
913-
+process.env.NSOLID_INTERVAL ||
914-
pkgConfig.nsolid.interval ||
918+
envInterval ??
919+
pkgInterval ??
915920
DEFAULT_INTERVAL;
916921

917922
nsolidConfig.tags = getTags(process.env.NSOLID_TAGS || pkgConfig.nsolid.tags);
@@ -1166,6 +1171,32 @@ function parseTraceSampleRate(value) {
11661171
}
11671172

11681173

1174+
function parsePositiveFiniteNumber(value) {
1175+
if (value === undefined || value === null) {
1176+
return undefined;
1177+
}
1178+
1179+
let normalized;
1180+
if (typeof value === 'number') {
1181+
normalized = value;
1182+
} else if (typeof value === 'string') {
1183+
const trimmedValue = StringPrototypeTrim(value);
1184+
if (trimmedValue === '') {
1185+
return undefined;
1186+
}
1187+
normalized = +trimmedValue;
1188+
} else {
1189+
return undefined;
1190+
}
1191+
1192+
if (!NumberIsFinite(normalized) || normalized <= 0) {
1193+
return undefined;
1194+
}
1195+
1196+
return normalized;
1197+
}
1198+
1199+
11691200
function genPackageList() {
11701201
let main_path;
11711202
let last_path;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "nsolid-interval-fixture",
3+
"version": "1.0.0",
4+
"nsolid": {
5+
"interval": 3000
6+
}
7+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict';
2+
3+
require('../common');
4+
const nsolid = require('nsolid');
5+
6+
nsolid.start();
7+
8+
console.log(JSON.stringify({
9+
interval: nsolid.config.interval,
10+
}));
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
'use strict';
2+
3+
require('../common');
4+
const assert = require('assert');
5+
const path = require('path');
6+
const { spawnSync } = require('child_process');
7+
8+
const script = path.join(__dirname,
9+
'../fixtures/test-nsolid-config-interval-env-script.js');
10+
const pkgJson = path.join(__dirname,
11+
'../fixtures/nsolid-interval-package.json');
12+
13+
function runWithEnv(envVars) {
14+
const filteredEnv = Object.fromEntries(
15+
Object.entries(process.env).filter(([key]) => !key.startsWith('NSOLID_')),
16+
);
17+
18+
const result = spawnSync(process.execPath, [script], {
19+
env: {
20+
...filteredEnv,
21+
...envVars,
22+
},
23+
encoding: 'utf8',
24+
});
25+
26+
if (result.status !== 0) {
27+
throw new Error(result.stderr || `Script failed with status ${result.status}`);
28+
}
29+
30+
return JSON.parse(result.stdout.trim());
31+
}
32+
33+
{
34+
const config = runWithEnv({});
35+
assert.strictEqual(config.interval, 5000);
36+
}
37+
38+
{
39+
const config = runWithEnv({
40+
NSOLID_INTERVAL: '2500',
41+
});
42+
assert.strictEqual(config.interval, 2500);
43+
}
44+
45+
{
46+
const config = runWithEnv({
47+
NSOLID_INTERVAL: 'invalid',
48+
});
49+
assert.strictEqual(config.interval, 5000);
50+
}
51+
52+
{
53+
const config = runWithEnv({
54+
NSOLID_INTERVAL: '0',
55+
});
56+
assert.strictEqual(config.interval, 5000);
57+
}
58+
59+
{
60+
const config = runWithEnv({
61+
NSOLID_INTERVAL: '-1',
62+
});
63+
assert.strictEqual(config.interval, 5000);
64+
}
65+
66+
{
67+
const config = runWithEnv({
68+
NSOLID_PACKAGE_JSON: pkgJson,
69+
});
70+
assert.strictEqual(config.interval, 3000);
71+
}
72+
73+
{
74+
const config = runWithEnv({
75+
NSOLID_PACKAGE_JSON: pkgJson,
76+
NSOLID_INTERVAL: '2500',
77+
});
78+
assert.strictEqual(config.interval, 2500);
79+
}
80+
81+
{
82+
const config = runWithEnv({
83+
NSOLID_PACKAGE_JSON: pkgJson,
84+
NSOLID_INTERVAL: '-1',
85+
});
86+
assert.strictEqual(config.interval, 3000);
87+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'use strict';
2+
3+
require('../common');
4+
const assert = require('assert');
5+
const nsolid = require('nsolid');
6+
7+
// Valid values should be normalized and persisted.
8+
nsolid.start({
9+
command: 9001,
10+
interval: '2500'
11+
});
12+
assert.strictEqual(nsolid.config.interval, 2500);
13+
14+
// Invalid updates must preserve the previous valid value.
15+
for (const interval of [
16+
0,
17+
-1,
18+
'0',
19+
'-1',
20+
'',
21+
' ',
22+
'still-invalid',
23+
Number.NaN,
24+
Number.POSITIVE_INFINITY,
25+
true,
26+
]) {
27+
nsolid.start({ interval });
28+
assert.strictEqual(nsolid.config.interval, 2500);
29+
}
30+
31+
// Partial updates that omit interval must not reset it.
32+
nsolid.start({
33+
tracingEnabled: true
34+
});
35+
assert.strictEqual(nsolid.config.interval, 2500);

0 commit comments

Comments
 (0)