Skip to content

Commit cf61f75

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 0f86d12 commit cf61f75

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
@@ -775,7 +775,10 @@ function updateConfig(config = {}) {
775775
nsolidConfig.assetsEnabled = normalized;
776776
}
777777
} else if (key === 'interval') {
778-
nsolidConfig.interval = +config.interval;
778+
const normalized = parsePositiveFiniteNumber(config.interval);
779+
if (normalized !== undefined) {
780+
nsolidConfig.interval = normalized;
781+
}
779782
} else if (key === 'traceSampleRate') {
780783
const normalized = parseTraceSampleRate(config.traceSampleRate);
781784
if (normalized !== undefined) {
@@ -915,9 +918,11 @@ function initializeConfig(nsolidConfig) {
915918
'prod';
916919

917920
// Metrics send interval
921+
const envInterval = parsePositiveFiniteNumber(process.env.NSOLID_INTERVAL);
922+
const pkgInterval = parsePositiveFiniteNumber(pkgConfig.nsolid.interval);
918923
nsolidConfig.interval =
919-
+process.env.NSOLID_INTERVAL ||
920-
pkgConfig.nsolid.interval ||
924+
envInterval ??
925+
pkgInterval ??
921926
DEFAULT_INTERVAL;
922927

923928
nsolidConfig.tags = getTags(process.env.NSOLID_TAGS || pkgConfig.nsolid.tags);
@@ -1172,6 +1177,32 @@ function parseTraceSampleRate(value) {
11721177
}
11731178

11741179

1180+
function parsePositiveFiniteNumber(value) {
1181+
if (value === undefined || value === null) {
1182+
return undefined;
1183+
}
1184+
1185+
let normalized;
1186+
if (typeof value === 'number') {
1187+
normalized = value;
1188+
} else if (typeof value === 'string') {
1189+
const trimmedValue = StringPrototypeTrim(value);
1190+
if (trimmedValue === '') {
1191+
return undefined;
1192+
}
1193+
normalized = +trimmedValue;
1194+
} else {
1195+
return undefined;
1196+
}
1197+
1198+
if (!NumberIsFinite(normalized) || normalized <= 0) {
1199+
return undefined;
1200+
}
1201+
1202+
return normalized;
1203+
}
1204+
1205+
11751206
function genPackageList() {
11761207
let main_path;
11771208
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)