Skip to content

Commit a838790

Browse files
alexgleasonzbjornson
authored andcommitted
Fix browser compatibility
1 parent 8005763 commit a838790

File tree

5 files changed

+73
-8
lines changed

5 files changed

+73
-8
lines changed

lib/defaultMetrics.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@ const metrics = {
2424
processOpenFileDescriptors,
2525
processMaxFileDescriptors,
2626
eventLoopLag,
27+
28+
...(typeof process !== 'undefined' &&
2729
// eslint-disable-next-line n/no-unsupported-features/node-builtins
28-
...(typeof process.getActiveResourcesInfo === 'function'
30+
typeof process.getActiveResourcesInfo === 'function'
2931
? { processResources }
3032
: {}),
3133
processHandles,

lib/metrics/osMemoryHeap.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,15 @@ function notLinuxVariant(registry, config = {}) {
2727
});
2828
}
2929

30+
function isLinux() {
31+
return typeof process !== 'undefined' && process.platform === 'linux';
32+
}
33+
3034
module.exports = (registry, config) =>
31-
process.platform === 'linux'
35+
isLinux()
3236
? linuxVariant(registry, config)
3337
: notLinuxVariant(registry, config);
3438

35-
module.exports.metricNames =
36-
process.platform === 'linux'
37-
? linuxVariant.metricNames
38-
: [PROCESS_RESIDENT_MEMORY];
39+
module.exports.metricNames = isLinux()
40+
? linuxVariant.metricNames
41+
: [PROCESS_RESIDENT_MEMORY];

lib/metrics/processStartTime.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
'use strict';
22

33
const Gauge = require('../gauge');
4-
const startInSeconds = Math.round(Date.now() / 1000 - process.uptime());
4+
5+
const uptime =
6+
typeof process !== 'undefined' && typeof process.uptime === 'function'
7+
? process.uptime()
8+
: 0;
9+
10+
const startInSeconds = Math.round(Date.now() / 1000 - uptime);
511

612
const PROCESS_START_TIME = 'process_start_time_seconds';
713

lib/metrics/version.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
'use strict';
22

33
const Gauge = require('../gauge');
4-
const version = process.version;
4+
5+
const version =
6+
typeof process !== 'undefined' && typeof process.version === 'string'
7+
? process.version
8+
: 'v0.0.0'; // Fallback for environments where process.version is not available
9+
510
const versionSegments = version.slice(1).split('.').map(Number);
611

712
const NODE_VERSION_INFO = 'nodejs_version_info';

test/browserCompatibilityTest.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
'use strict';
2+
3+
describe('Browser compatibility', () => {
4+
const globalRegistry = require('../index').register;
5+
let originalProcess;
6+
7+
beforeEach(() => {
8+
originalProcess = global.process;
9+
});
10+
11+
afterEach(() => {
12+
global.process = originalProcess;
13+
globalRegistry.clear();
14+
});
15+
16+
it('should be able to import Counter, Gauge, and Histogram without process global', () => {
17+
// Simulate browser environment by removing process global
18+
delete global.process;
19+
20+
expect(() => {
21+
const { Counter, Gauge, Histogram } = require('../index');
22+
23+
// Verify that the classes are available
24+
expect(Counter).toBeDefined();
25+
expect(Gauge).toBeDefined();
26+
expect(Histogram).toBeDefined();
27+
28+
// Verify that we can create instances
29+
const counter = new Counter({
30+
name: 'test_counter',
31+
help: 'Test counter',
32+
});
33+
34+
const gauge = new Gauge({
35+
name: 'test_gauge',
36+
help: 'Test gauge',
37+
});
38+
39+
const histogram = new Histogram({
40+
name: 'test_histogram',
41+
help: 'Test histogram',
42+
});
43+
44+
expect(counter).toBeInstanceOf(Counter);
45+
expect(gauge).toBeInstanceOf(Gauge);
46+
expect(histogram).toBeInstanceOf(Histogram);
47+
}).not.toThrow();
48+
});
49+
});

0 commit comments

Comments
 (0)