diff --git a/index.js b/index.js index fbab366..acc7d41 100644 --- a/index.js +++ b/index.js @@ -32,6 +32,88 @@ const OS_METHODS = [ 'userInfo' // => user ]; +const NODE_ENV = process.env.NODE_ENV || 'development'; +const HOSTNAME = process.env.HOSTNAME || os.hostname(); +const IP_ADDRESS = ip.address(); +const PID = process.pid; +const VERSION = process.version; + +// +// NOTE: this should be static/constant in-memory from when process is started +// (it should not change when cwd or files change, only when process reboot) +// +// worker_threads +let _worker_threads = {}; +if (hasWorkerThreads) { + _worker_threads = _.pick(worker_threads, [ + 'isMainThread', + semver.satisfies(process.version, '>=12.16.0') && 'resourceLimits', + 'threadId', + 'workerData' + ]); + + if (_.isObject(_worker_threads.resourceLimits)) { + _worker_threads.resourceLimits = _.pick(_worker_threads.resourceLimits, [ + 'maxYoungGenerationSizeMb', + 'maxOldGenerationSizeMb', + 'codeRangeSizeMb', + 'stackSizeMb' + ]); + } +} + +const _cluster = _.pick(cluster, [ + 'worker', + usesClusterPrimary ? 'isPrimary' : 'isMaster', + 'isWorker', + 'schedulingPolicy' +]); +if (_.isObject(_cluster.worker)) { + _cluster.worker = _.pick(_cluster.worker, [ + 'id', + 'process', + 'exitedAfterDisconnect', + 'isConnected', + 'isDead' + ]); + + if (_.isObject(_cluster.worker.process)) { + _cluster.worker.process = _.pick(_cluster.worker.process, [ + 'pid', + 'connected', + 'killed', + 'signalCode', + 'exitCode' + ]); + } +} + +let packageInfo = {}; +try { + packageInfo = readPkgUp.sync(); +} catch {} + +const info = {}; +if ( + typeof packageInfo === 'object' && + typeof packageInfo.packageJson === 'object' +) { + if (typeof packageInfo.packageJson.name === 'string') + info.name = packageInfo.packageJson.name; + if (typeof packageInfo.packageJson.version === 'string') + info.version = packageInfo.packageJson.version; +} + +const lastCommitLog = new LastCommitLog(); +let hash; +let gitTag; +try { + ({ hash, gitTag } = lastCommitLog.getLastCommitSync()); +} catch {} + +const lastCommit = { hash }; +if (gitTag) lastCommit.tag = gitTag; + // `os.version` added in 13.11.0 // https://nodejs.org/api/os.html#os_os_version if (semver.satisfies(process.version, '>=13.11.0')) { @@ -57,60 +139,8 @@ if (semver.satisfies(process.version, '>=13.11.0')) { // // Retrieves informations about the current running app. -// eslint-disable-next-line complexity function parseAppInfo() { - let packageInfo = {}; - try { - packageInfo = readPkgUp.sync(); - } catch {} - - const info = {}; - if ( - typeof packageInfo === 'object' && - typeof packageInfo.packageJson === 'object' - ) { - if (typeof packageInfo.packageJson.name === 'string') - info.name = packageInfo.packageJson.name; - if (typeof packageInfo.packageJson.version === 'string') - info.version = packageInfo.packageJson.version; - } - - const lastCommitLog = new LastCommitLog(); - let hash; - let gitTag; - try { - ({ hash, gitTag } = lastCommitLog.getLastCommitSync()); - } catch {} - - const lastCommit = { hash }; - if (gitTag) lastCommit.tag = gitTag; - const { NODE_ENV, HOSTNAME, IP_ADDRESS } = process.env; - - const _cluster = _.pick(cluster, [ - 'worker', - usesClusterPrimary ? 'isPrimary' : 'isMaster', - 'isWorker', - 'schedulingPolicy' - ]); if (_.isObject(_cluster.worker)) { - _cluster.worker = _.pick(_cluster.worker, [ - 'id', - 'process', - 'exitedAfterDisconnect', - 'isConnected', - 'isDead' - ]); - - if (_.isObject(_cluster.worker.process)) { - _cluster.worker.process = _.pick(_cluster.worker.process, [ - 'pid', - 'connected', - 'killed', - 'signalCode', - 'exitCode' - ]); - } - if (_.isFunction(_cluster.worker.isConnected)) _cluster.worker.isConnected = _cluster.worker.isConnected(); if (_.isFunction(_cluster.worker.isDead)) @@ -145,34 +175,14 @@ function parseAppInfo() { _os[key] = os[method](); } - // worker_threads - let _worker_threads = {}; - if (hasWorkerThreads) { - _worker_threads = _.pick(worker_threads, [ - 'isMainThread', - semver.satisfies(process.version, '>=12.16.0') && 'resourceLimits', - 'threadId', - 'workerData' - ]); - - if (_.isObject(_worker_threads.resourceLimits)) { - _worker_threads.resourceLimits = _.pick(_worker_threads.resourceLimits, [ - 'maxYoungGenerationSizeMb', - 'maxOldGenerationSizeMb', - 'codeRangeSizeMb', - 'stackSizeMb' - ]); - } - } - return { ...info, - node: process.version, + node: VERSION, ...lastCommit, - environment: NODE_ENV || 'development', - hostname: HOSTNAME || os.hostname(), - ip: IP_ADDRESS || ip.address(), - pid: process.pid, + environment: NODE_ENV, + hostname: HOSTNAME, + ip: IP_ADDRESS, + pid: PID, cluster: _cluster, os: _os, worker_threads: _worker_threads diff --git a/package.json b/package.json index 714dea5..525ff01 100644 --- a/package.json +++ b/package.json @@ -17,8 +17,8 @@ "Philipp Kursawe (https://pke.github.io)" ], "dependencies": { - "ip": "^1.1.8", - "last-commit-log": "^3.3.0", + "ip": "^2.0.1", + "last-commit-log": "^3.4.0", "lodash": "^4.17.21", "read-pkg-up": "7", "semver": "^7.5.0" @@ -38,7 +38,6 @@ "remark-cli": "^11.0.0", "remark-preset-github": "^4.0.4", "rimraf": "^5.0.0", - "testdouble": "3.17.2", "xo": "^0.54.2" }, "engines": { diff --git a/test/env.test.js b/test/env.test.js deleted file mode 100644 index 345ed27..0000000 --- a/test/env.test.js +++ /dev/null @@ -1,23 +0,0 @@ -const test = require('ava'); -const mockedEnv = require('mocked-env'); -const parseAppInfo = require('..'); - -test.afterEach((t) => { - t.context.restoreEnv(); -}); - -function run(t, input = [], expected = input) { - t.context.restoreEnv = mockedEnv({ - NODE_ENV: input[0], - HOSTNAME: input[1] - }); - const appInfo = parseAppInfo(); - t.is(appInfo.environment, expected[0]); - t.is(appInfo.hostname, expected[1]); -} - -test.serial('custom env vars', run, ['got', 'walder_frey']); -test('undefined env vars', run, undefined, [ - 'development', - require('node:os').hostname() -]); diff --git a/test/test.js b/test/test.js index c655cc2..7f590e6 100644 --- a/test/test.js +++ b/test/test.js @@ -1,44 +1,26 @@ const process = require('node:process'); const test = require('ava'); -const td = require('testdouble'); +const LastCommitLog = require('last-commit-log'); + +const lastCommitLog = new LastCommitLog(); +let hash; +let gitTag; +try { + ({ hash, gitTag } = lastCommitLog.getLastCommitSync()); +} catch {} test.beforeEach((t) => { - t.context.LastCommitLog = td.replace('last-commit-log'); t.context.parseAppInfo = require('..'); }); -test.afterEach(() => { - td.reset(); -}); - -test('does not include missing git info', (t) => { - td.when(t.context.LastCommitLog.prototype.getLastCommitSync()).thenReturn({}); - const appInfo = t.context.parseAppInfo(); - t.is(appInfo.hash, undefined); - t.is(appInfo.tag, undefined); -}); - -test('does not include missing git tag', (t) => { - td.when(t.context.LastCommitLog.prototype.getLastCommitSync()).thenReturn({ - hash: 0xd_ea_db_ea_dd_00_de - }); - const appInfo = t.context.parseAppInfo(); - t.is(appInfo.hash, 0xd_ea_db_ea_dd_00_de); - t.is(appInfo.tag, undefined); -}); - -test('returns this packages info', (t) => { - td.when(t.context.LastCommitLog.prototype.getLastCommitSync()).thenReturn({ - hash: 0xd_ea_db_ea_dd_00_de, - gitTag: '1.0.2' - }); +test('returns info', (t) => { const appInfo = t.context.parseAppInfo(); t.is(appInfo.node, process.version); t.is(appInfo.environment, 'test'); t.is(appInfo.hostname, require('node:os').hostname()); t.is(appInfo.pid, process.pid); t.is(appInfo.name, 'parse-app-info'); - t.is(appInfo.hash, 0xd_ea_db_ea_dd_00_de); - t.is(appInfo.tag, '1.0.2'); + t.is(appInfo.hash, hash); + t.is(appInfo.tag, gitTag); t.is(appInfo.ip, require('ip').address()); });