Skip to content

Commit

Permalink
feat: rewrite and optimize for real-world usage (no longer supports e…
Browse files Browse the repository at this point in the history
…nv vars, uses static constant variables)
  • Loading branch information
titanism committed Nov 26, 2024
1 parent 4681a5d commit e68a565
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 132 deletions.
164 changes: 87 additions & 77 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')) {
Expand All @@ -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))
Expand Down Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
"Philipp Kursawe <[email protected]> (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"
Expand All @@ -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": {
Expand Down
23 changes: 0 additions & 23 deletions test/env.test.js

This file was deleted.

40 changes: 11 additions & 29 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -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());
});

0 comments on commit e68a565

Please sign in to comment.