@webpod/ps 0.0.0-beta.3 → 0.0.0-beta.5

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.3",
3
+ "version": "0.0.0-beta.5",
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",
@@ -53,47 +53,56 @@ __export(ts_exports, {
53
53
  default: () => ts_default,
54
54
  kill: () => kill,
55
55
  lookup: () => lookup,
56
- tree: () => tree
56
+ lookupSync: () => lookupSync,
57
+ tree: () => tree,
58
+ treeSync: () => treeSync
57
59
  });
58
60
  module.exports = __toCommonJS(ts_exports);
59
61
 
60
62
  // src/main/ts/ps.ts
61
63
  var import_node_process = __toESM(require("node:process"), 1);
62
64
  var import_node_fs = __toESM(require("node:fs"), 1);
65
+ var import_node_os = require("node:os");
63
66
  var import_ingrid = require("@webpod/ingrid");
64
67
  var import_spawn = require("zurk/spawn");
65
- var import_node_os = require("node:os");
66
68
  var EOL = /(\r\n)|(\n\r)|\n|\r/;
67
69
  var IS_WIN = import_node_process.default.platform === "win32";
68
70
  var isBin = (f) => {
69
- if (f === "")
70
- return false;
71
- if (!f.includes("/"))
72
- return true;
73
- if (!import_node_fs.default.existsSync(f))
74
- return false;
71
+ if (f === "") return false;
72
+ if (!f.includes("/")) return true;
73
+ if (!import_node_fs.default.existsSync(f)) return false;
75
74
  const stat = import_node_fs.default.lstatSync(f);
76
75
  return stat.isFile() || stat.isSymbolicLink();
77
76
  };
78
- var lookup = (query = {}, cb = noop) => {
79
- const { promise, resolve, reject } = makeDeferred();
77
+ var lookup = (query = {}, cb = noop) => _lookup({ query, cb, sync: false });
78
+ var lookupSync = (query = {}, cb = noop) => _lookup({ query, cb, sync: true });
79
+ lookup.sync = lookupSync;
80
+ var _lookup = ({
81
+ query = {},
82
+ cb = noop,
83
+ sync = false
84
+ }) => {
85
+ const pFactory = sync ? makePseudoDeferred.bind(null, []) : makeDeferred;
86
+ const { promise, resolve, reject } = pFactory();
80
87
  const { psargs = ["-lx"] } = query;
81
- const args = typeof psargs === "string" ? psargs.split(/\s+/) : psargs;
88
+ const args = Array.isArray(psargs) ? psargs : psargs.split(/\s+/);
82
89
  const extract = IS_WIN ? extractWmic : identity;
90
+ let result = [];
83
91
  const callback = (err, { stdout }) => {
84
92
  if (err) {
85
93
  reject(err);
86
94
  cb(err);
87
95
  return;
88
96
  }
89
- const list = parseProcessList(extract(stdout), query);
90
- resolve(list);
91
- cb(null, list);
97
+ result = parseProcessList(extract(stdout), query);
98
+ resolve(result);
99
+ cb(null, result);
92
100
  };
93
101
  const ctx = IS_WIN ? {
94
102
  cmd: "cmd",
95
103
  input: "wmic process get ProcessId,ParentProcessId,CommandLine \n",
96
104
  callback,
105
+ sync,
97
106
  run(cb2) {
98
107
  cb2();
99
108
  }
@@ -103,10 +112,11 @@ var lookup = (query = {}, cb = noop) => {
103
112
  run(cb2) {
104
113
  cb2();
105
114
  },
115
+ sync,
106
116
  callback
107
117
  };
108
118
  (0, import_spawn.exec)(ctx);
109
- return promise;
119
+ return Object.assign(promise, result);
110
120
  };
111
121
  var parseProcessList = (output, query = {}) => {
112
122
  const processList = parseGrid(output.trim());
@@ -134,23 +144,34 @@ var pickTree = (list, pid, recursive = false) => {
134
144
  ...children.flatMap((p) => recursive ? pickTree(list, p.pid, true) : [])
135
145
  ];
136
146
  };
137
- var tree = (_0, ..._1) => __async(void 0, [_0, ..._1], function* (opts, cb = noop) {
147
+ var _tree = ({
148
+ cb = noop,
149
+ opts,
150
+ sync = false
151
+ }) => {
138
152
  if (typeof opts === "string" || typeof opts === "number") {
139
- return tree({ pid: opts }, cb);
153
+ return _tree({ opts: { pid: opts }, cb, sync });
140
154
  }
141
- try {
142
- const all = yield lookup();
143
- if (opts === void 0)
144
- return all;
155
+ const handle = (all) => {
156
+ if (opts === void 0) 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
  });
173
+ var treeSync = (opts, cb) => _tree({ opts, cb, sync: true });
174
+ tree.sync = treeSync;
154
175
  var kill = (pid, opts, next) => {
155
176
  if (typeof opts == "function") {
156
177
  return kill(pid, void 0, opts);
@@ -174,8 +195,7 @@ var kill = (pid, opts, next) => {
174
195
  let checkTimeoutTimer;
175
196
  let checkIsTimeout = false;
176
197
  const checkKilled = (finishCallback) => lookup({ pid }, (err, list = []) => {
177
- if (checkIsTimeout)
178
- return;
198
+ if (checkIsTimeout) return;
179
199
  if (err) {
180
200
  clearTimeout(checkTimeoutTimer);
181
201
  reject(err);
@@ -233,15 +253,26 @@ var makeDeferred = () => {
233
253
  });
234
254
  return { resolve, reject, promise };
235
255
  };
256
+ var makePseudoDeferred = (r = {}) => {
257
+ return {
258
+ promise: r,
259
+ resolve: identity,
260
+ reject(e) {
261
+ throw e;
262
+ }
263
+ };
264
+ };
236
265
  var noop = () => {
237
266
  };
238
267
  var identity = (v) => v;
239
268
 
240
269
  // src/main/ts/index.ts
241
- var ts_default = { lookup, kill, tree };
270
+ var ts_default = { kill, lookup, lookupSync, tree, treeSync };
242
271
  // Annotate the CommonJS export names for ESM import in node:
243
272
  0 && (module.exports = {
244
273
  kill,
245
274
  lookup,
246
- tree
275
+ lookupSync,
276
+ tree,
277
+ treeSync
247
278
  });
@@ -1,8 +1,16 @@
1
1
  export type * from './ps.js';
2
- export { kill, lookup, tree } from './ps.js';
2
+ export { kill, lookup, lookupSync, tree, treeSync } from './ps.js';
3
3
  declare const _default: {
4
- lookup: (query?: import("./ps.js").TPsLookupQuery, cb?: import("./ps.js").TPsLookupCallback) => Promise<import("./ps.js").TPsLookupEntry[]>;
5
4
  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[]>;
5
+ lookup: {
6
+ (query?: import("./ps.js").TPsLookupQuery, cb?: import("./ps.js").TPsLookupCallback): Promise<import("./ps.js").TPsLookupEntry[]>;
7
+ sync: (query?: import("./ps.js").TPsLookupQuery, cb?: import("./ps.js").TPsLookupCallback) => import("./ps.js").TPsLookupEntry[];
8
+ };
9
+ lookupSync: (query?: import("./ps.js").TPsLookupQuery, cb?: import("./ps.js").TPsLookupCallback) => import("./ps.js").TPsLookupEntry[];
10
+ tree: {
11
+ (opts?: string | number | import("./ps.js").TPsTreeOpts | undefined, cb?: import("./ps.js").TPsLookupCallback | undefined): Promise<import("./ps.js").TPsLookupEntry[]>;
12
+ sync: (opts?: string | number | import("./ps.js").TPsTreeOpts | undefined, cb?: import("./ps.js").TPsLookupCallback | undefined) => import("./ps.js").TPsLookupEntry[];
13
+ };
14
+ treeSync: (opts?: string | number | import("./ps.js").TPsTreeOpts | undefined, cb?: import("./ps.js").TPsLookupCallback | undefined) => import("./ps.js").TPsLookupEntry[];
7
15
  };
8
16
  export default _default;
@@ -26,12 +26,28 @@ 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
+ * @param {Object=null} cb.err
31
+ * @param {TPsLookupEntry[]} cb.processList
32
+ * @return {Promise<TPsLookupEntry[]>}
33
+ */
34
+ export declare const lookup: {
35
+ (query?: TPsLookupQuery, cb?: TPsLookupCallback): Promise<TPsLookupEntry[]>;
36
+ sync: (query?: TPsLookupQuery, cb?: TPsLookupCallback) => TPsLookupEntry[];
37
+ };
38
+ /**
39
+ * Looks up the process list synchronously
40
+ * @param query
41
+ * @param {String|String[]} query.pid
42
+ * @param {String} query.command RegExp String
43
+ * @param {String} query.arguments RegExp String
44
+ * @param {String|String[]} query.psargs
45
+ * @param {Function} [cb]
30
46
  * @param {Object=null} cb.err
31
47
  * @param {Object[]} cb.processList
32
- * @return {Object}
48
+ * @return {TPsLookupEntry[]}
33
49
  */
34
- export declare const lookup: (query?: TPsLookupQuery, cb?: TPsLookupCallback) => Promise<TPsLookupEntry[]>;
50
+ export declare const lookupSync: (query?: TPsLookupQuery, cb?: TPsLookupCallback) => TPsLookupEntry[];
35
51
  export declare const parseProcessList: (output: string, query?: TPsLookupQuery) => TPsLookupEntry[];
36
52
  export declare const extractWmic: (stdout: string) => string;
37
53
  export type TPsTreeOpts = {
@@ -39,7 +55,11 @@ export type TPsTreeOpts = {
39
55
  recursive?: boolean;
40
56
  };
41
57
  export declare const pickTree: (list: TPsLookupEntry[], pid: string | number, recursive?: boolean) => TPsLookupEntry[];
42
- export declare const tree: (opts?: string | number | TPsTreeOpts | undefined, cb?: TPsLookupCallback) => Promise<TPsLookupEntry[]>;
58
+ export declare const tree: {
59
+ (opts?: string | number | TPsTreeOpts | undefined, cb?: TPsLookupCallback): Promise<TPsLookupEntry[]>;
60
+ sync: (opts?: string | number | TPsTreeOpts | undefined, cb?: TPsLookupCallback) => TPsLookupEntry[];
61
+ };
62
+ export declare const treeSync: (opts?: string | number | TPsTreeOpts | undefined, cb?: TPsLookupCallback) => TPsLookupEntry[];
43
63
  /**
44
64
  * Kill process
45
65
  * @param pid
@@ -52,10 +72,3 @@ export declare const kill: (pid: string | number, opts?: TPsNext | TPsKillOption
52
72
  export declare const parseGrid: (output: string) => TPsLookupEntry[];
53
73
  export declare const formatOutput: (data: TIngridResponse) => TPsLookupEntry[];
54
74
  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,40 +1,47 @@
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) => {
10
- if (f === "")
11
- return false;
12
- if (!f.includes("/"))
13
- return true;
14
- if (!fs.existsSync(f))
15
- return false;
10
+ if (f === "") return false;
11
+ if (!f.includes("/")) return true;
12
+ if (!fs.existsSync(f)) return false;
16
13
  const stat = fs.lstatSync(f);
17
14
  return stat.isFile() || stat.isSymbolicLink();
18
15
  };
19
- var lookup = (query = {}, cb = noop) => {
20
- const { promise, resolve, reject } = makeDeferred();
16
+ var lookup = (query = {}, cb = noop) => _lookup({ query, cb, sync: false });
17
+ var lookupSync = (query = {}, cb = noop) => _lookup({ query, cb, sync: true });
18
+ lookup.sync = lookupSync;
19
+ var _lookup = ({
20
+ query = {},
21
+ cb = noop,
22
+ sync = false
23
+ }) => {
24
+ const pFactory = sync ? makePseudoDeferred.bind(null, []) : makeDeferred;
25
+ const { promise, resolve, reject } = pFactory();
21
26
  const { psargs = ["-lx"] } = query;
22
- const args = typeof psargs === "string" ? psargs.split(/\s+/) : psargs;
27
+ const args = Array.isArray(psargs) ? psargs : psargs.split(/\s+/);
23
28
  const extract = IS_WIN ? extractWmic : identity;
29
+ let result = [];
24
30
  const callback = (err, { stdout }) => {
25
31
  if (err) {
26
32
  reject(err);
27
33
  cb(err);
28
34
  return;
29
35
  }
30
- const list = parseProcessList(extract(stdout), query);
31
- resolve(list);
32
- cb(null, list);
36
+ result = parseProcessList(extract(stdout), query);
37
+ resolve(result);
38
+ cb(null, result);
33
39
  };
34
40
  const ctx = IS_WIN ? {
35
41
  cmd: "cmd",
36
42
  input: "wmic process get ProcessId,ParentProcessId,CommandLine \n",
37
43
  callback,
44
+ sync,
38
45
  run(cb2) {
39
46
  cb2();
40
47
  }
@@ -44,10 +51,11 @@ var lookup = (query = {}, cb = noop) => {
44
51
  run(cb2) {
45
52
  cb2();
46
53
  },
54
+ sync,
47
55
  callback
48
56
  };
49
57
  exec(ctx);
50
- return promise;
58
+ return Object.assign(promise, result);
51
59
  };
52
60
  var parseProcessList = (output, query = {}) => {
53
61
  const processList = parseGrid(output.trim());
@@ -75,23 +83,32 @@ var pickTree = (list, pid, recursive = false) => {
75
83
  ...children.flatMap((p) => recursive ? pickTree(list, p.pid, true) : [])
76
84
  ];
77
85
  };
78
- var tree = async (opts, cb = noop) => {
86
+ var _tree = ({
87
+ cb = noop,
88
+ opts,
89
+ sync = false
90
+ }) => {
79
91
  if (typeof opts === "string" || typeof opts === "number") {
80
- return tree({ pid: opts }, cb);
92
+ return _tree({ opts: { pid: opts }, cb, sync });
81
93
  }
82
- try {
83
- const all = await lookup();
84
- if (opts === void 0)
85
- return all;
94
+ const handle = (all) => {
95
+ if (opts === void 0) return all;
86
96
  const { pid, recursive = false } = opts;
87
97
  const list = pickTree(all, pid, recursive);
88
98
  cb(null, list);
89
99
  return list;
100
+ };
101
+ try {
102
+ const all = _lookup({ sync });
103
+ return sync ? handle(all) : all.then(handle);
90
104
  } catch (err) {
91
105
  cb(err);
92
106
  throw err;
93
107
  }
94
108
  };
109
+ var tree = async (opts, cb) => _tree({ opts, cb });
110
+ var treeSync = (opts, cb) => _tree({ opts, cb, sync: true });
111
+ tree.sync = treeSync;
95
112
  var kill = (pid, opts, next) => {
96
113
  if (typeof opts == "function") {
97
114
  return kill(pid, void 0, opts);
@@ -115,8 +132,7 @@ var kill = (pid, opts, next) => {
115
132
  let checkTimeoutTimer;
116
133
  let checkIsTimeout = false;
117
134
  const checkKilled = (finishCallback) => lookup({ pid }, (err, list = []) => {
118
- if (checkIsTimeout)
119
- return;
135
+ if (checkIsTimeout) return;
120
136
  if (err) {
121
137
  clearTimeout(checkTimeoutTimer);
122
138
  reject(err);
@@ -173,15 +189,26 @@ var makeDeferred = () => {
173
189
  });
174
190
  return { resolve, reject, promise };
175
191
  };
192
+ var makePseudoDeferred = (r = {}) => {
193
+ return {
194
+ promise: r,
195
+ resolve: identity,
196
+ reject(e) {
197
+ throw e;
198
+ }
199
+ };
200
+ };
176
201
  var noop = () => {
177
202
  };
178
203
  var identity = (v) => v;
179
204
 
180
205
  // src/main/ts/index.ts
181
- var ts_default = { lookup, kill, tree };
206
+ var ts_default = { kill, lookup, lookupSync, tree, treeSync };
182
207
  export {
183
208
  ts_default as default,
184
209
  kill,
185
210
  lookup,
186
- tree
211
+ lookupSync,
212
+ tree,
213
+ treeSync
187
214
  };
@@ -1 +0,0 @@
1
- export declare const foo: () => undefined;