@webpod/ps 0.0.0-beta.2 → 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 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
- * [ ] Brings sync API
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.2",
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.2",
43
- "zurk": "^0.0.32"
42
+ "@webpod/ingrid": "^0.0.0-beta.3",
43
+ "zurk": "^0.2.0"
44
44
  },
45
45
  "devDependencies": {
46
- "@types/node": "^20.11.30",
46
+ "@types/node": "^20.14.2",
47
47
  "c8": "^9.1.0",
48
48
  "concurrently": "^8.2.2",
49
- "esbuild": "^0.20.2",
50
- "esbuild-node-externals": "^1.13.0",
51
- "esbuild-plugin-entry-chunks": "^0.1.11",
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.3.0",
57
- "sinon": "^17.0.1",
56
+ "mocha": "^10.4.0",
57
+ "sinon": "^18.0.0",
58
58
  "ts-node": "^10.9.2",
59
- "typedoc": "^0.25.12",
60
- "typescript": "^5.4.3"
59
+ "typedoc": "^0.25.13",
60
+ "typescript": "^5.4.5"
61
61
  },
62
62
  "repository": {
63
63
  "type": "git",
@@ -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
- const { promise, resolve, reject } = makeDeferred();
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 = typeof psargs === "string" ? psargs.split(/\s+/) : psargs;
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
- const list = parseProcessList(extract(stdout), query);
90
- resolve(list);
91
- cb(null, list);
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,19 +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 tree = (_0, ..._1) => __async(void 0, [_0, ..._1], function* (opts, cb = noop) {
146
+ var _tree = ({
147
+ cb = noop,
148
+ opts,
149
+ sync = false
150
+ }) => {
138
151
  if (typeof opts === "string" || typeof opts === "number") {
139
- return tree({ pid: opts }, cb);
152
+ return _tree({ opts: { pid: opts }, cb, sync });
140
153
  }
141
- try {
154
+ const handle = (all) => {
155
+ if (opts === void 0)
156
+ return all;
142
157
  const { pid, recursive = false } = opts;
143
- const list = pickTree(yield lookup(), pid, recursive);
158
+ const list = pickTree(all, pid, recursive);
144
159
  cb(null, list);
145
160
  return list;
161
+ };
162
+ try {
163
+ const all = _lookup({ sync });
164
+ return sync ? handle(all) : all.then(handle);
146
165
  } catch (err) {
147
166
  cb(err);
148
167
  throw err;
149
168
  }
169
+ };
170
+ var tree = (opts, cb) => __async(void 0, null, function* () {
171
+ return _tree({ opts, cb });
150
172
  });
151
173
  var kill = (pid, opts, next) => {
152
174
  if (typeof opts == "function") {
@@ -230,6 +252,15 @@ var makeDeferred = () => {
230
252
  });
231
253
  return { resolve, reject, promise };
232
254
  };
255
+ var makePseudoDeferred = (r = {}) => {
256
+ return {
257
+ promise: r,
258
+ resolve: identity,
259
+ reject(e) {
260
+ throw e;
261
+ }
262
+ };
263
+ };
233
264
  var noop = () => {
234
265
  };
235
266
  var identity = (v) => v;
@@ -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, 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;
@@ -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 {Object[]} cb.processList
32
- * @return {Object}
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 = {
@@ -39,7 +52,8 @@ export type TPsTreeOpts = {
39
52
  recursive?: boolean;
40
53
  };
41
54
  export declare const pickTree: (list: TPsLookupEntry[], pid: string | number, recursive?: boolean) => TPsLookupEntry[];
42
- export declare const tree: (opts: string | number | TPsTreeOpts, cb?: TPsLookupCallback) => Promise<TPsLookupEntry[]>;
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;
@@ -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
- const { promise, resolve, reject } = makeDeferred();
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 = typeof psargs === "string" ? psargs.split(/\s+/) : psargs;
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
- const list = parseProcessList(extract(stdout), query);
31
- resolve(list);
32
- cb(null, list);
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,20 +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 tree = async (opts, cb = noop) => {
87
+ var _tree = ({
88
+ cb = noop,
89
+ opts,
90
+ sync = false
91
+ }) => {
79
92
  if (typeof opts === "string" || typeof opts === "number") {
80
- return tree({ pid: opts }, cb);
93
+ return _tree({ opts: { pid: opts }, cb, sync });
81
94
  }
82
- try {
95
+ const handle = (all) => {
96
+ if (opts === void 0)
97
+ return all;
83
98
  const { pid, recursive = false } = opts;
84
- const list = pickTree(await lookup(), pid, recursive);
99
+ const list = pickTree(all, pid, recursive);
85
100
  cb(null, list);
86
101
  return list;
102
+ };
103
+ try {
104
+ const all = _lookup({ sync });
105
+ return sync ? handle(all) : all.then(handle);
87
106
  } catch (err) {
88
107
  cb(err);
89
108
  throw err;
90
109
  }
91
110
  };
111
+ var tree = async (opts, cb) => _tree({ opts, cb });
92
112
  var kill = (pid, opts, next) => {
93
113
  if (typeof opts == "function") {
94
114
  return kill(pid, void 0, opts);
@@ -170,6 +190,15 @@ var makeDeferred = () => {
170
190
  });
171
191
  return { resolve, reject, promise };
172
192
  };
193
+ var makePseudoDeferred = (r = {}) => {
194
+ return {
195
+ promise: r,
196
+ resolve: identity,
197
+ reject(e) {
198
+ throw e;
199
+ }
200
+ };
201
+ };
173
202
  var noop = () => {
174
203
  };
175
204
  var identity = (v) => v;
@@ -1 +0,0 @@
1
- export declare const foo: () => undefined;