Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ module.exports = function childrenOfPid(pid, includeRoot, callback) {
var processLister;
if (process.platform === 'win32') {
// See also: https://github.com/nodejs/node-v0.x-archive/issues/2318
processLister = spawn('wmic.exe', ['PROCESS', 'GET', 'Name,ProcessId,ParentProcessId,Status,WorkingSetSize']);
processLister = spawn('wmic.exe', ['PROCESS', 'GET', 'Name,ProcessId,ParentProcessId,WorkingSetSize']);
} else {
processLister = spawn('ps', ['-A', '-o', 'ppid,pid,stat,comm,rss']);
}
Expand All @@ -72,8 +72,11 @@ module.exports = function childrenOfPid(pid, includeRoot, callback) {
}

// Convert RSS to number of bytes
columns[4] = parseInt(columns[4], 10);
if (process.platform !== 'win32') {
if (process.platform == 'win32') {
columns[3] = parseInt(columns[3], 10);
}
else {
columns[4] = parseInt(columns[4], 10);
columns[4] *= 1024;
}

Expand All @@ -84,6 +87,13 @@ module.exports = function childrenOfPid(pid, includeRoot, callback) {
row[h.shift()] = h.length ? columns.shift() : columns.join(' ');
}

// For Windows, WMIC.exe never returns any value for "Status" which used to get value corresponding to "STAT"
// See: https://docs.microsoft.com/en-us/windows/win32/cimwin32prov/win32-process?redirectedfrom=MSDN
// So just set "null" for the backward compatibility.
if (process.platform == 'win32') {
row['STAT'] = null;
}

return cb(null, row);
}),
es.writeArray(function (err, ps) {
Expand Down Expand Up @@ -122,19 +132,14 @@ function normalizeHeader(str) {
switch (str) {
case 'Name':
return 'COMMAND';
break;
case 'ParentProcessId':
return 'PPID';
break;
case 'ProcessId':
return 'PID';
break;
case 'Status':
return 'STAT';
break;
case 'WorkingSetSize':
return 'RSS';
break;
default:
throw new Error('Unknown process listing header: ' + str);
}
Expand Down
9 changes: 7 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ var red = chalk.red,
green = chalk.green,
cyan = chalk.cyan;

var isWin = process.platform === 'win32';
var isNumGreaterThanZero = (n) => !isNaN(parseInt(n, 10)) && n > 0;

var scripts = {
parent: path.join(__dirname, 'exec', 'parent.js'),
child: path.join(__dirname, 'exec', 'child.js')
Expand Down Expand Up @@ -65,9 +68,11 @@ test(cyan('Includes itself it includeRoot is true'), function (t) {
};

t.equal(current.PID, '' + process.pid, green('✓ Current PID is valid'));
t.equal(current.COMMAND, 'node', green('✓ Current COMM is node'));
t.equal(current.COMMAND, isWin ? 'node.exe' : 'node', green('✓ Current COMM is node'));
t.equal(isNumGreaterThanZero(current.RSS), true, green('✓ Current RSS is valid'));
t.notEqual(other.PID, '' + process.pid, green('✓ Current PID is valid'));
t.equal(other.COMMAND, 'ps', green('✓ Current COMM is ps'));
t.equal(other.COMMAND, isWin ? 'WMIC.exe' : 'ps', green('✓ Current COMM is ps'));
t.equal(isNumGreaterThanZero(other.RSS), true, green('✓ Other RSS is valid'));
t.end();
});
});
Expand Down