update
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
const spawn = require('child_process').spawn;
|
||||
|
||||
module.exports = { tree, pidsForTree, getStat };
|
||||
|
||||
function getStat() {
|
||||
return new Promise((resolve) => {
|
||||
const command = `ls /proc | grep -E '^[0-9]+$' | xargs -I{} cat /proc/{}/stat`;
|
||||
const spawned = spawn('sh', ['-c', command], {
|
||||
stdio: ['pipe', 'pipe', 'pipe'],
|
||||
});
|
||||
|
||||
var res = '';
|
||||
spawned.stdout.on('data', (data) => (res += data));
|
||||
spawned.on('close', () => resolve(res));
|
||||
});
|
||||
}
|
||||
|
||||
function template(s) {
|
||||
var stat = null;
|
||||
// 'pid', 'comm', 'state', 'ppid', 'pgrp'
|
||||
// %d (%s) %c %d %d
|
||||
s.replace(
|
||||
/(\d+) \((.*?)\)\s(.+?)\s(\d+)\s/g,
|
||||
(all, PID, COMMAND, STAT, PPID) => {
|
||||
stat = { PID, COMMAND, PPID, STAT };
|
||||
}
|
||||
);
|
||||
|
||||
return stat;
|
||||
}
|
||||
|
||||
function tree(stats) {
|
||||
const processes = stats.split('\n').map(template).filter(Boolean);
|
||||
|
||||
return processes;
|
||||
}
|
||||
|
||||
function pidsForTree(tree, pid) {
|
||||
if (typeof pid === 'number') {
|
||||
pid = pid.toString();
|
||||
}
|
||||
const parents = [pid];
|
||||
const pids = [];
|
||||
|
||||
tree.forEach((proc) => {
|
||||
if (parents.indexOf(proc.PPID) !== -1) {
|
||||
parents.push(proc.PID);
|
||||
pids.push(proc);
|
||||
}
|
||||
});
|
||||
|
||||
return pids;
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2018 Nikita Skovoroda <chalkerx@gmail.com>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
@@ -0,0 +1,58 @@
|
||||
'use strict';
|
||||
|
||||
/** @type {(t: import('tape').Test) => false | void} */
|
||||
// eslint-disable-next-line consistent-return
|
||||
module.exports = function runSymbolTests(t) {
|
||||
t.equal(typeof Symbol, 'function', 'global Symbol is a function');
|
||||
|
||||
if (typeof Symbol !== 'function') { return false; }
|
||||
|
||||
t.notEqual(Symbol(), Symbol(), 'two symbols are not equal');
|
||||
|
||||
/*
|
||||
t.equal(
|
||||
Symbol.prototype.toString.call(Symbol('foo')),
|
||||
Symbol.prototype.toString.call(Symbol('foo')),
|
||||
'two symbols with the same description stringify the same'
|
||||
);
|
||||
*/
|
||||
|
||||
/*
|
||||
var foo = Symbol('foo');
|
||||
|
||||
t.notEqual(
|
||||
String(foo),
|
||||
String(Symbol('bar')),
|
||||
'two symbols with different descriptions do not stringify the same'
|
||||
);
|
||||
*/
|
||||
|
||||
t.equal(typeof Symbol.prototype.toString, 'function', 'Symbol#toString is a function');
|
||||
// t.equal(String(foo), Symbol.prototype.toString.call(foo), 'Symbol#toString equals String of the same symbol');
|
||||
|
||||
t.equal(typeof Object.getOwnPropertySymbols, 'function', 'Object.getOwnPropertySymbols is a function');
|
||||
|
||||
/** @type {{ [k in symbol]?: unknown }} */
|
||||
var obj = {};
|
||||
var sym = Symbol('test');
|
||||
var symObj = Object(sym);
|
||||
t.notEqual(typeof sym, 'string', 'Symbol is not a string');
|
||||
t.equal(Object.prototype.toString.call(sym), '[object Symbol]', 'symbol primitive Object#toStrings properly');
|
||||
t.equal(Object.prototype.toString.call(symObj), '[object Symbol]', 'symbol primitive Object#toStrings properly');
|
||||
|
||||
var symVal = 42;
|
||||
obj[sym] = symVal;
|
||||
// eslint-disable-next-line no-restricted-syntax, no-unused-vars
|
||||
for (var _ in obj) { t.fail('symbol property key was found in for..in of object'); }
|
||||
|
||||
t.deepEqual(Object.keys(obj), [], 'no enumerable own keys on symbol-valued object');
|
||||
t.deepEqual(Object.getOwnPropertyNames(obj), [], 'no own names on symbol-valued object');
|
||||
t.deepEqual(Object.getOwnPropertySymbols(obj), [sym], 'one own symbol on symbol-valued object');
|
||||
t.equal(Object.prototype.propertyIsEnumerable.call(obj, sym), true, 'symbol is enumerable');
|
||||
t.deepEqual(Object.getOwnPropertyDescriptor(obj, sym), {
|
||||
configurable: true,
|
||||
enumerable: true,
|
||||
value: 42,
|
||||
writable: true
|
||||
}, 'property descriptor is correct');
|
||||
};
|
||||
Reference in New Issue
Block a user