@webpod/ps 0.0.0-beta.3 → 0.0.0-beta.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -1
- package/package.json +11 -11
- package/target/cjs/index.cjs +40 -12
- package/target/dts/index.d.ts +1 -1
- package/target/dts/ps.d.ts +17 -10
- package/target/esm/index.mjs +38 -12
- package/target/dts/foo.d.ts +0 -1
package/README.md
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
* [x] CJS and ESM package entry points
|
|
8
8
|
* [x] `table-parser` replaced with `@webpod/ingrid` to handle some issues: [neekey/ps#76](https://github.com/neekey/ps/issues/76), [neekey/ps#62](https://github.com/neekey/ps/issues/62), [neekey/table-parser#11](https://github.com/neekey/table-parser/issues/11), [neekey/table-parser#18](https://github.com/neekey/table-parser/issues/18)
|
|
9
9
|
* [x] Provides promisified responses
|
|
10
|
-
* [
|
|
10
|
+
* [x] Brings sync API
|
|
11
11
|
* [x] Builds a process tree
|
|
12
12
|
|
|
13
13
|
## Install
|
|
@@ -44,6 +44,9 @@ lookup({pid: 12345}, (err, list) => {
|
|
|
44
44
|
console.log('No such process found!')
|
|
45
45
|
}
|
|
46
46
|
})
|
|
47
|
+
|
|
48
|
+
// or syncronously
|
|
49
|
+
const _list = lookup.sync({pid: 12345})
|
|
47
50
|
```
|
|
48
51
|
|
|
49
52
|
Define a query opts to filter the results by `command` and/or `arguments` predicates:
|
|
@@ -109,6 +112,9 @@ const children = await tree({pid: 123, recursive: true})
|
|
|
109
112
|
{pid: 130, ppid: 125},
|
|
110
113
|
]
|
|
111
114
|
*/
|
|
115
|
+
|
|
116
|
+
// or syncronously
|
|
117
|
+
const list = tree.sync({pid: 123, recursive: true})
|
|
112
118
|
```
|
|
113
119
|
|
|
114
120
|
### kill()
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webpod/ps",
|
|
3
|
-
"version": "0.0.0-beta.
|
|
3
|
+
"version": "0.0.0-beta.4",
|
|
4
4
|
"description": "A process lookup utility",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -39,25 +39,25 @@
|
|
|
39
39
|
"pid"
|
|
40
40
|
],
|
|
41
41
|
"dependencies": {
|
|
42
|
-
"@webpod/ingrid": "^0.0.0-beta.
|
|
43
|
-
"zurk": "^0.0
|
|
42
|
+
"@webpod/ingrid": "^0.0.0-beta.3",
|
|
43
|
+
"zurk": "^0.2.0"
|
|
44
44
|
},
|
|
45
45
|
"devDependencies": {
|
|
46
|
-
"@types/node": "^20.
|
|
46
|
+
"@types/node": "^20.14.2",
|
|
47
47
|
"c8": "^9.1.0",
|
|
48
48
|
"concurrently": "^8.2.2",
|
|
49
|
-
"esbuild": "^0.
|
|
50
|
-
"esbuild-node-externals": "^1.13.
|
|
51
|
-
"esbuild-plugin-entry-chunks": "^0.1.
|
|
49
|
+
"esbuild": "^0.21.4",
|
|
50
|
+
"esbuild-node-externals": "^1.13.1",
|
|
51
|
+
"esbuild-plugin-entry-chunks": "^0.1.15",
|
|
52
52
|
"eslint": "^8.57.0",
|
|
53
53
|
"eslint-config-qiwi": "^2.1.3",
|
|
54
54
|
"fast-glob": "^3.3.2",
|
|
55
55
|
"minimist": "^1.2.8",
|
|
56
|
-
"mocha": "^10.
|
|
57
|
-
"sinon": "^
|
|
56
|
+
"mocha": "^10.4.0",
|
|
57
|
+
"sinon": "^18.0.0",
|
|
58
58
|
"ts-node": "^10.9.2",
|
|
59
|
-
"typedoc": "^0.25.
|
|
60
|
-
"typescript": "^5.4.
|
|
59
|
+
"typedoc": "^0.25.13",
|
|
60
|
+
"typescript": "^5.4.5"
|
|
61
61
|
},
|
|
62
62
|
"repository": {
|
|
63
63
|
"type": "git",
|
package/target/cjs/index.cjs
CHANGED
|
@@ -60,9 +60,9 @@ module.exports = __toCommonJS(ts_exports);
|
|
|
60
60
|
// src/main/ts/ps.ts
|
|
61
61
|
var import_node_process = __toESM(require("node:process"), 1);
|
|
62
62
|
var import_node_fs = __toESM(require("node:fs"), 1);
|
|
63
|
+
var import_node_os = require("node:os");
|
|
63
64
|
var import_ingrid = require("@webpod/ingrid");
|
|
64
65
|
var import_spawn = require("zurk/spawn");
|
|
65
|
-
var import_node_os = require("node:os");
|
|
66
66
|
var EOL = /(\r\n)|(\n\r)|\n|\r/;
|
|
67
67
|
var IS_WIN = import_node_process.default.platform === "win32";
|
|
68
68
|
var isBin = (f) => {
|
|
@@ -75,25 +75,33 @@ var isBin = (f) => {
|
|
|
75
75
|
const stat = import_node_fs.default.lstatSync(f);
|
|
76
76
|
return stat.isFile() || stat.isSymbolicLink();
|
|
77
77
|
};
|
|
78
|
-
var lookup = (query = {}, cb = noop) => {
|
|
79
|
-
|
|
78
|
+
var lookup = (query = {}, cb = noop) => _lookup({ query, cb, sync: false });
|
|
79
|
+
var _lookup = ({
|
|
80
|
+
query = {},
|
|
81
|
+
cb = noop,
|
|
82
|
+
sync = false
|
|
83
|
+
}) => {
|
|
84
|
+
const pFactory = sync ? makePseudoDeferred.bind(null, []) : makeDeferred;
|
|
85
|
+
const { promise, resolve, reject } = pFactory();
|
|
80
86
|
const { psargs = ["-lx"] } = query;
|
|
81
|
-
const args =
|
|
87
|
+
const args = Array.isArray(psargs) ? psargs : psargs.split(/\s+/);
|
|
82
88
|
const extract = IS_WIN ? extractWmic : identity;
|
|
89
|
+
let result = [];
|
|
83
90
|
const callback = (err, { stdout }) => {
|
|
84
91
|
if (err) {
|
|
85
92
|
reject(err);
|
|
86
93
|
cb(err);
|
|
87
94
|
return;
|
|
88
95
|
}
|
|
89
|
-
|
|
90
|
-
resolve(
|
|
91
|
-
cb(null,
|
|
96
|
+
result = parseProcessList(extract(stdout), query);
|
|
97
|
+
resolve(result);
|
|
98
|
+
cb(null, result);
|
|
92
99
|
};
|
|
93
100
|
const ctx = IS_WIN ? {
|
|
94
101
|
cmd: "cmd",
|
|
95
102
|
input: "wmic process get ProcessId,ParentProcessId,CommandLine \n",
|
|
96
103
|
callback,
|
|
104
|
+
sync,
|
|
97
105
|
run(cb2) {
|
|
98
106
|
cb2();
|
|
99
107
|
}
|
|
@@ -103,10 +111,11 @@ var lookup = (query = {}, cb = noop) => {
|
|
|
103
111
|
run(cb2) {
|
|
104
112
|
cb2();
|
|
105
113
|
},
|
|
114
|
+
sync,
|
|
106
115
|
callback
|
|
107
116
|
};
|
|
108
117
|
(0, import_spawn.exec)(ctx);
|
|
109
|
-
return promise;
|
|
118
|
+
return Object.assign(promise, result);
|
|
110
119
|
};
|
|
111
120
|
var parseProcessList = (output, query = {}) => {
|
|
112
121
|
const processList = parseGrid(output.trim());
|
|
@@ -134,22 +143,32 @@ var pickTree = (list, pid, recursive = false) => {
|
|
|
134
143
|
...children.flatMap((p) => recursive ? pickTree(list, p.pid, true) : [])
|
|
135
144
|
];
|
|
136
145
|
};
|
|
137
|
-
var
|
|
146
|
+
var _tree = ({
|
|
147
|
+
cb = noop,
|
|
148
|
+
opts,
|
|
149
|
+
sync = false
|
|
150
|
+
}) => {
|
|
138
151
|
if (typeof opts === "string" || typeof opts === "number") {
|
|
139
|
-
return
|
|
152
|
+
return _tree({ opts: { pid: opts }, cb, sync });
|
|
140
153
|
}
|
|
141
|
-
|
|
142
|
-
const all = yield lookup();
|
|
154
|
+
const handle = (all) => {
|
|
143
155
|
if (opts === void 0)
|
|
144
156
|
return all;
|
|
145
157
|
const { pid, recursive = false } = opts;
|
|
146
158
|
const list = pickTree(all, pid, recursive);
|
|
147
159
|
cb(null, list);
|
|
148
160
|
return list;
|
|
161
|
+
};
|
|
162
|
+
try {
|
|
163
|
+
const all = _lookup({ sync });
|
|
164
|
+
return sync ? handle(all) : all.then(handle);
|
|
149
165
|
} catch (err) {
|
|
150
166
|
cb(err);
|
|
151
167
|
throw err;
|
|
152
168
|
}
|
|
169
|
+
};
|
|
170
|
+
var tree = (opts, cb) => __async(void 0, null, function* () {
|
|
171
|
+
return _tree({ opts, cb });
|
|
153
172
|
});
|
|
154
173
|
var kill = (pid, opts, next) => {
|
|
155
174
|
if (typeof opts == "function") {
|
|
@@ -233,6 +252,15 @@ var makeDeferred = () => {
|
|
|
233
252
|
});
|
|
234
253
|
return { resolve, reject, promise };
|
|
235
254
|
};
|
|
255
|
+
var makePseudoDeferred = (r = {}) => {
|
|
256
|
+
return {
|
|
257
|
+
promise: r,
|
|
258
|
+
resolve: identity,
|
|
259
|
+
reject(e) {
|
|
260
|
+
throw e;
|
|
261
|
+
}
|
|
262
|
+
};
|
|
263
|
+
};
|
|
236
264
|
var noop = () => {
|
|
237
265
|
};
|
|
238
266
|
var identity = (v) => v;
|
package/target/dts/index.d.ts
CHANGED
|
@@ -3,6 +3,6 @@ export { kill, lookup, tree } from './ps.js';
|
|
|
3
3
|
declare const _default: {
|
|
4
4
|
lookup: (query?: import("./ps.js").TPsLookupQuery, cb?: import("./ps.js").TPsLookupCallback) => Promise<import("./ps.js").TPsLookupEntry[]>;
|
|
5
5
|
kill: (pid: string | number, opts?: string | number | import("./ps.js").TPsKillOptions | import("./ps.js").TPsNext | undefined, next?: import("./ps.js").TPsNext | undefined) => Promise<void>;
|
|
6
|
-
tree: (opts?: string | number | import("./ps.js").TPsTreeOpts | undefined, cb?: import("./ps.js").TPsLookupCallback) => Promise<import("./ps.js").TPsLookupEntry[]>;
|
|
6
|
+
tree: (opts?: string | number | import("./ps.js").TPsTreeOpts | undefined, cb?: import("./ps.js").TPsLookupCallback | undefined) => Promise<import("./ps.js").TPsLookupEntry[]>;
|
|
7
7
|
};
|
|
8
8
|
export default _default;
|
package/target/dts/ps.d.ts
CHANGED
|
@@ -26,12 +26,25 @@ export type TPsNext = (err?: any, data?: any) => void;
|
|
|
26
26
|
* @param {String} query.command RegExp String
|
|
27
27
|
* @param {String} query.arguments RegExp String
|
|
28
28
|
* @param {String|String[]} query.psargs
|
|
29
|
-
* @param {Function} cb
|
|
29
|
+
* @param {Function} [cb]
|
|
30
30
|
* @param {Object=null} cb.err
|
|
31
|
-
* @param {
|
|
32
|
-
* @return {
|
|
31
|
+
* @param {TPsLookupEntry[]} cb.processList
|
|
32
|
+
* @return {Promise<TPsLookupEntry[]>}
|
|
33
33
|
*/
|
|
34
34
|
export declare const lookup: (query?: TPsLookupQuery, cb?: TPsLookupCallback) => Promise<TPsLookupEntry[]>;
|
|
35
|
+
/**
|
|
36
|
+
* Looks up the process list synchronously
|
|
37
|
+
* @param query
|
|
38
|
+
* @param {String|String[]} query.pid
|
|
39
|
+
* @param {String} query.command RegExp String
|
|
40
|
+
* @param {String} query.arguments RegExp String
|
|
41
|
+
* @param {String|String[]} query.psargs
|
|
42
|
+
* @param {Function} [cb]
|
|
43
|
+
* @param {Object=null} cb.err
|
|
44
|
+
* @param {Object[]} cb.processList
|
|
45
|
+
* @return {TPsLookupEntry[]}
|
|
46
|
+
*/
|
|
47
|
+
export declare const lookupSync: (query?: TPsLookupQuery, cb?: TPsLookupCallback) => TPsLookupEntry[];
|
|
35
48
|
export declare const parseProcessList: (output: string, query?: TPsLookupQuery) => TPsLookupEntry[];
|
|
36
49
|
export declare const extractWmic: (stdout: string) => string;
|
|
37
50
|
export type TPsTreeOpts = {
|
|
@@ -40,6 +53,7 @@ export type TPsTreeOpts = {
|
|
|
40
53
|
};
|
|
41
54
|
export declare const pickTree: (list: TPsLookupEntry[], pid: string | number, recursive?: boolean) => TPsLookupEntry[];
|
|
42
55
|
export declare const tree: (opts?: string | number | TPsTreeOpts | undefined, cb?: TPsLookupCallback) => Promise<TPsLookupEntry[]>;
|
|
56
|
+
export declare const treeSync: (opts?: string | number | TPsTreeOpts | undefined, cb?: TPsLookupCallback) => TPsLookupEntry[];
|
|
43
57
|
/**
|
|
44
58
|
* Kill process
|
|
45
59
|
* @param pid
|
|
@@ -52,10 +66,3 @@ export declare const kill: (pid: string | number, opts?: TPsNext | TPsKillOption
|
|
|
52
66
|
export declare const parseGrid: (output: string) => TPsLookupEntry[];
|
|
53
67
|
export declare const formatOutput: (data: TIngridResponse) => TPsLookupEntry[];
|
|
54
68
|
export type PromiseResolve<T = any> = (value?: T | PromiseLike<T>) => void;
|
|
55
|
-
export declare const makeDeferred: <T = any, E = any>() => {
|
|
56
|
-
promise: Promise<T>;
|
|
57
|
-
resolve: PromiseResolve<T>;
|
|
58
|
-
reject: PromiseResolve<E>;
|
|
59
|
-
};
|
|
60
|
-
export declare const noop: () => void;
|
|
61
|
-
export declare const identity: <T>(v: T) => T;
|
package/target/esm/index.mjs
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
// src/main/ts/ps.ts
|
|
2
2
|
import process from "node:process";
|
|
3
3
|
import fs from "node:fs";
|
|
4
|
+
import { EOL as SystemEOL } from "node:os";
|
|
4
5
|
import { parse } from "@webpod/ingrid";
|
|
5
6
|
import { exec } from "zurk/spawn";
|
|
6
|
-
import { EOL as SystemEOL } from "node:os";
|
|
7
7
|
var EOL = /(\r\n)|(\n\r)|\n|\r/;
|
|
8
8
|
var IS_WIN = process.platform === "win32";
|
|
9
9
|
var isBin = (f) => {
|
|
@@ -16,25 +16,33 @@ var isBin = (f) => {
|
|
|
16
16
|
const stat = fs.lstatSync(f);
|
|
17
17
|
return stat.isFile() || stat.isSymbolicLink();
|
|
18
18
|
};
|
|
19
|
-
var lookup = (query = {}, cb = noop) => {
|
|
20
|
-
|
|
19
|
+
var lookup = (query = {}, cb = noop) => _lookup({ query, cb, sync: false });
|
|
20
|
+
var _lookup = ({
|
|
21
|
+
query = {},
|
|
22
|
+
cb = noop,
|
|
23
|
+
sync = false
|
|
24
|
+
}) => {
|
|
25
|
+
const pFactory = sync ? makePseudoDeferred.bind(null, []) : makeDeferred;
|
|
26
|
+
const { promise, resolve, reject } = pFactory();
|
|
21
27
|
const { psargs = ["-lx"] } = query;
|
|
22
|
-
const args =
|
|
28
|
+
const args = Array.isArray(psargs) ? psargs : psargs.split(/\s+/);
|
|
23
29
|
const extract = IS_WIN ? extractWmic : identity;
|
|
30
|
+
let result = [];
|
|
24
31
|
const callback = (err, { stdout }) => {
|
|
25
32
|
if (err) {
|
|
26
33
|
reject(err);
|
|
27
34
|
cb(err);
|
|
28
35
|
return;
|
|
29
36
|
}
|
|
30
|
-
|
|
31
|
-
resolve(
|
|
32
|
-
cb(null,
|
|
37
|
+
result = parseProcessList(extract(stdout), query);
|
|
38
|
+
resolve(result);
|
|
39
|
+
cb(null, result);
|
|
33
40
|
};
|
|
34
41
|
const ctx = IS_WIN ? {
|
|
35
42
|
cmd: "cmd",
|
|
36
43
|
input: "wmic process get ProcessId,ParentProcessId,CommandLine \n",
|
|
37
44
|
callback,
|
|
45
|
+
sync,
|
|
38
46
|
run(cb2) {
|
|
39
47
|
cb2();
|
|
40
48
|
}
|
|
@@ -44,10 +52,11 @@ var lookup = (query = {}, cb = noop) => {
|
|
|
44
52
|
run(cb2) {
|
|
45
53
|
cb2();
|
|
46
54
|
},
|
|
55
|
+
sync,
|
|
47
56
|
callback
|
|
48
57
|
};
|
|
49
58
|
exec(ctx);
|
|
50
|
-
return promise;
|
|
59
|
+
return Object.assign(promise, result);
|
|
51
60
|
};
|
|
52
61
|
var parseProcessList = (output, query = {}) => {
|
|
53
62
|
const processList = parseGrid(output.trim());
|
|
@@ -75,23 +84,31 @@ var pickTree = (list, pid, recursive = false) => {
|
|
|
75
84
|
...children.flatMap((p) => recursive ? pickTree(list, p.pid, true) : [])
|
|
76
85
|
];
|
|
77
86
|
};
|
|
78
|
-
var
|
|
87
|
+
var _tree = ({
|
|
88
|
+
cb = noop,
|
|
89
|
+
opts,
|
|
90
|
+
sync = false
|
|
91
|
+
}) => {
|
|
79
92
|
if (typeof opts === "string" || typeof opts === "number") {
|
|
80
|
-
return
|
|
93
|
+
return _tree({ opts: { pid: opts }, cb, sync });
|
|
81
94
|
}
|
|
82
|
-
|
|
83
|
-
const all = await lookup();
|
|
95
|
+
const handle = (all) => {
|
|
84
96
|
if (opts === void 0)
|
|
85
97
|
return all;
|
|
86
98
|
const { pid, recursive = false } = opts;
|
|
87
99
|
const list = pickTree(all, pid, recursive);
|
|
88
100
|
cb(null, list);
|
|
89
101
|
return list;
|
|
102
|
+
};
|
|
103
|
+
try {
|
|
104
|
+
const all = _lookup({ sync });
|
|
105
|
+
return sync ? handle(all) : all.then(handle);
|
|
90
106
|
} catch (err) {
|
|
91
107
|
cb(err);
|
|
92
108
|
throw err;
|
|
93
109
|
}
|
|
94
110
|
};
|
|
111
|
+
var tree = async (opts, cb) => _tree({ opts, cb });
|
|
95
112
|
var kill = (pid, opts, next) => {
|
|
96
113
|
if (typeof opts == "function") {
|
|
97
114
|
return kill(pid, void 0, opts);
|
|
@@ -173,6 +190,15 @@ var makeDeferred = () => {
|
|
|
173
190
|
});
|
|
174
191
|
return { resolve, reject, promise };
|
|
175
192
|
};
|
|
193
|
+
var makePseudoDeferred = (r = {}) => {
|
|
194
|
+
return {
|
|
195
|
+
promise: r,
|
|
196
|
+
resolve: identity,
|
|
197
|
+
reject(e) {
|
|
198
|
+
throw e;
|
|
199
|
+
}
|
|
200
|
+
};
|
|
201
|
+
};
|
|
176
202
|
var noop = () => {
|
|
177
203
|
};
|
|
178
204
|
var identity = (v) => v;
|
package/target/dts/foo.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const foo: () => undefined;
|