Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
214 lines (189 loc) · 5.03 KB
/
Copy pathindex.js
File metadata and controls
214 lines (189 loc) · 5.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
const process = require('node:process');
const cluster = require('node:cluster');
const os = require('node:os');
const { execFileSync } = require('node:child_process');
const _ = require('lodash');
const readPkgUp = require('read-pkg-up');
const semver = require('semver');
const hasWorkerThreads = semver.satisfies(process.version, '>=12.11.0');
let worker_threads;
if (hasWorkerThreads) worker_threads = require('node:worker_threads');
const usesClusterPrimary = semver.satisfies(process.version, '>=16.0.0');
const OS_METHODS = [
'arch',
'cpus',
'endianness',
'freemem',
'getPriority', // => priority
'homedir',
'hostname',
'loadavg',
'networkInterfaces', // => network_interfaces
'platform',
'release',
'tmpdir',
'totalmem',
'type',
'uptime',
'userInfo' // => user
];
function getIpAddress() {
const interfaces = os.networkInterfaces();
for (const addresses of Object.values(interfaces)) {
for (const address of addresses || []) {
const family =
address.family === 4 || address.family === 'IPv4' ? 'IPv4' : 'IPv6';
if (family === 'IPv4' && !address.internal) return address.address;
}
}
return '127.0.0.1';
}
function getGitValue(arguments_) {
return execFileSync('git', arguments_, {
cwd: process.cwd(),
maxBuffer: 1024 * 1024 * 1024,
stdio: ['ignore', 'pipe', 'ignore']
})
.toString()
.trim();
}
const NODE_ENV = process.env.NODE_ENV || 'development';
const HOSTNAME = process.env.HOSTNAME || os.hostname();
const IP_ADDRESS = getIpAddress();
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;
}
let hash;
let gitTag;
try {
hash = getGitValue(['log', '-1', '--pretty=format:%H']);
gitTag = getGitValue(['tag', '--contains', 'HEAD']);
} 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')) {
OS_METHODS.push('version');
}
//
// Information about the app.
//
// The git infos can be missing if the project has no commit yet
// or is not connected to a git repo.
//
// * name - The name of the app
// * version - The version of the app
// * node - NodeJS version
// * hash - Last git commit hash
// * tag - Last git tag, if any
// * environment - Nodejs environment the app is run in
// * hostname - Name of the computer the app is run on
// * pid - Process ID of the app
// * cluster - Cluster info of the app
// * os - OS info of the app
//
// Retrieves informations about the current running app.
function parseAppInfo() {
if (_.isObject(_cluster.worker)) {
if (_.isFunction(_cluster.worker.isConnected))
_cluster.worker.isConnected = _cluster.worker.isConnected();
if (_.isFunction(_cluster.worker.isDead))
_cluster.worker.isDead = _cluster.worker.isDead();
}
const _os = {};
for (const method of OS_METHODS) {
let key = method;
switch (method) {
case 'getPriority': {
key = 'priority';
break;
}
case 'networkInterfaces': {
key = 'network_interfaces';
break;
}
case 'userInfo': {
key = 'user';
break;
}
default: {
key = method;
break;
}
}
_os[key] = os[method]();
}
return {
...info,
node: VERSION,
...lastCommit,
environment: NODE_ENV,
hostname: HOSTNAME,
ip: IP_ADDRESS,
pid: PID,
cluster: _cluster,
os: _os,
worker_threads: _worker_threads
};
}
module.exports = parseAppInfo;