pubo-node 1.0.144 → 1.0.145

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.
@@ -219,6 +219,7 @@ export var SIGKILL = _async(function (pid, signal) {
219
219
  var tmp = [tree.pid];
220
220
  flatProcessTree(tree, tmp);
221
221
  tmp.reverse();
222
+ tree = null;
222
223
  return _continue(_forOf(tmp, function (item) {
223
224
  return _awaitIgnored(_SIGKILL(item, signal));
224
225
  }), function () {
@@ -312,7 +313,9 @@ export function getProcessByPpid(pid) {
312
313
  }
313
314
  // 获取进程树
314
315
  export var getProcessTree = _async(function (pid, tree) {
316
+ var isRoot = false;
315
317
  if (!tree) {
318
+ isRoot = true;
316
319
  tree = {
317
320
  pid: pid,
318
321
  children: []
@@ -328,17 +331,24 @@ export var getProcessTree = _async(function (pid, tree) {
328
331
  tree.children.push(item);
329
332
  });
330
333
  }), function () {
331
- return tree;
334
+ if (isRoot) {
335
+ return tree;
336
+ } else {
337
+ tree = null;
338
+ }
332
339
  });
333
340
  });
334
341
  });
335
342
  var flatProcessTree = function flatProcessTree(tree, tmp) {
336
343
  if (tree.children) {
337
- tmp.push.apply(tmp, tree.children.map(function (item) {
344
+ var _tmp;
345
+ (_tmp = tmp).push.apply(_tmp, tree.children.map(function (item) {
338
346
  return item.pid;
339
347
  }));
340
348
  tree.children.forEach(function (item) {
341
349
  return flatProcessTree(item, tmp);
342
350
  });
343
351
  }
352
+ tree = null;
353
+ tmp = null;
344
354
  };
@@ -1,356 +1,142 @@
1
1
  "use strict";
2
-
3
- function _await(value, then, direct) {
4
- if (direct) {
5
- return then ? then(value) : value;
6
- }
7
- if (!value || !value.then) {
8
- value = Promise.resolve(value);
9
- }
10
- return then ? value.then(then) : value;
11
- }
12
- var SIGKILL = _async(function (pid, signal) {
13
- if (signal === void 0) {
14
- signal = 2;
15
- }
16
- return process.platform === 'win32' ? new Promise(function (resolve, reject) {
17
- (0, child_process_1.exec)("taskkill /pid " + pid + " /T /F", function (err) {
18
- if (err) {
19
- reject(err);
20
- } else {
21
- resolve('');
22
- }
23
- });
24
- }) : _await((0, exports.getProcessTree)(pid), function (tree) {
25
- // 获取所有进程PID,从叶到根
26
- var tmp = [tree.pid];
27
- flatProcessTree(tree, tmp);
28
- tmp.reverse();
29
- return _continue(_forOf(tmp, function (item) {
30
- return _awaitIgnored(_SIGKILL(item, signal));
31
- }), function () {
32
- return 'success';
33
- });
34
- });
35
- });
36
- var _iteratorSymbol = /*#__PURE__*/typeof Symbol !== "undefined" ? Symbol.iterator || (Symbol.iterator = Symbol("Symbol.iterator")) : "@@iterator";
37
- function _settle(pact, state, value) {
38
- if (!pact.s) {
39
- if (value instanceof _Pact) {
40
- if (value.s) {
41
- if (state & 1) {
42
- state = value.s;
43
- }
44
- value = value.v;
45
- } else {
46
- value.o = _settle.bind(null, pact, state);
47
- return;
48
- }
49
- }
50
- if (value && value.then) {
51
- value.then(_settle.bind(null, pact, state), _settle.bind(null, pact, 2));
52
- return;
53
- }
54
- pact.s = state;
55
- pact.v = value;
56
- var observer = pact.o;
57
- if (observer) {
58
- observer(pact);
59
- }
60
- }
61
- }
62
- var _Pact = /*#__PURE__*/function () {
63
- function _Pact() {}
64
- _Pact.prototype.then = function (onFulfilled, onRejected) {
65
- var result = new _Pact();
66
- var state = this.s;
67
- if (state) {
68
- var callback = state & 1 ? onFulfilled : onRejected;
69
- if (callback) {
70
- try {
71
- _settle(result, 1, callback(this.v));
72
- } catch (e) {
73
- _settle(result, 2, e);
74
- }
75
- return result;
76
- } else {
77
- return this;
78
- }
79
- }
80
- this.o = function (_this) {
81
- try {
82
- var value = _this.v;
83
- if (_this.s & 1) {
84
- _settle(result, 1, onFulfilled ? onFulfilled(value) : value);
85
- } else if (onRejected) {
86
- _settle(result, 1, onRejected(value));
87
- } else {
88
- _settle(result, 2, value);
89
- }
90
- } catch (e) {
91
- _settle(result, 2, e);
92
- }
93
- };
94
- return result;
95
- };
96
- return _Pact;
97
- }();
98
- function _isSettledPact(thenable) {
99
- return thenable instanceof _Pact && thenable.s & 1;
100
- }
101
- function _forTo(array, body, check) {
102
- var i = -1,
103
- pact,
104
- reject;
105
- function _cycle(result) {
106
- try {
107
- while (++i < array.length && (!check || !check())) {
108
- result = body(i);
109
- if (result && result.then) {
110
- if (_isSettledPact(result)) {
111
- result = result.v;
112
- } else {
113
- result.then(_cycle, reject || (reject = _settle.bind(null, pact = new _Pact(), 2)));
114
- return;
115
- }
116
- }
117
- }
118
- if (pact) {
119
- _settle(pact, 1, result);
120
- } else {
121
- pact = result;
122
- }
123
- } catch (e) {
124
- _settle(pact || (pact = new _Pact()), 2, e);
125
- }
126
- }
127
- _cycle();
128
- return pact;
129
- }
130
- // 杀死进程
131
- var _SIGKILL = _async(function (pid, signal, times) {
132
- if (signal === void 0) {
133
- signal = 2;
134
- }
135
- if (times === void 0) {
136
- times = 1;
137
- }
138
- if (times > 5) {
139
- throw new Error('SIGKILL 失败. times > 5');
140
- }
141
- if (process.platform === 'win32') {
142
- process.kill(pid, signal);
143
- return;
144
- }
145
- (0, child_process_1.exec)("kill -" + signal + " " + pid);
146
- return _continueIgnored(_catch(function () {
147
- return _awaitIgnored((0, pubo_utils_1.waitFor)(function () {
148
- return isProcessDied(pid);
149
- }, {
150
- checkTime: 1000,
151
- timeout: 10000
152
- }));
153
- }, function () {
154
- return _awaitIgnored(_SIGKILL(pid, 9, times + 1));
155
- }));
156
- }); // 广度优先遍历进程树,将pid放入tmp
157
- function _forOf(target, body, check) {
158
- if (typeof target[_iteratorSymbol] === "function") {
159
- var _cycle = function _cycle(result) {
160
- try {
161
- while (!(step = iterator.next()).done && (!check || !check())) {
162
- result = body(step.value);
163
- if (result && result.then) {
164
- if (_isSettledPact(result)) {
165
- result = result.v;
166
- } else {
167
- result.then(_cycle, reject || (reject = _settle.bind(null, pact = new _Pact(), 2)));
168
- return;
169
- }
170
- }
171
- }
172
- if (pact) {
173
- _settle(pact, 1, result);
174
- } else {
175
- pact = result;
176
- }
177
- } catch (e) {
178
- _settle(pact || (pact = new _Pact()), 2, e);
179
- }
180
- };
181
- var iterator = target[_iteratorSymbol](),
182
- step,
183
- pact,
184
- reject;
185
- _cycle();
186
- if (iterator["return"]) {
187
- var _fixup = function _fixup(value) {
188
- try {
189
- if (!step.done) {
190
- iterator["return"]();
191
- }
192
- } catch (e) {}
193
- return value;
194
- };
195
- if (pact && pact.then) {
196
- return pact.then(_fixup, function (e) {
197
- throw _fixup(e);
198
- });
199
- }
200
- _fixup();
201
- }
202
- return pact;
203
- }
204
- // No support for Symbol.iterator
205
- if (!("length" in target)) {
206
- throw new TypeError("Object is not iterable");
207
- }
208
- // Handle live collections properly
209
- var values = [];
210
- for (var i = 0; i < target.length; i++) {
211
- values.push(target[i]);
212
- }
213
- return _forTo(values, function (i) {
214
- return body(values[i]);
215
- }, check);
216
- }
217
- // 判断进程是否死亡
218
- var isProcessDied = _async(function (pid) {
219
- return _await(getProcessCpuUseByPid(pid), function (used) {
220
- return used < 0;
221
- });
222
- });
223
- function _continue(value, then) {
224
- return value && value.then ? value.then(then) : then(value);
225
- }
226
- function _async(f) {
227
- return function () {
228
- for (var args = [], i = 0; i < arguments.length; i++) {
229
- args[i] = arguments[i];
230
- }
231
- try {
232
- return Promise.resolve(f.apply(this, args));
233
- } catch (e) {
234
- return Promise.reject(e);
235
- }
236
- };
237
- }
238
- function _empty() {}
239
- function _awaitIgnored(value, direct) {
240
- if (!direct) {
241
- return value && value.then ? value.then(_empty) : Promise.resolve();
242
- }
243
- }
244
- function _catch(body, recover) {
245
- try {
246
- var result = body();
247
- } catch (e) {
248
- return recover(e);
249
- }
250
- if (result && result.then) {
251
- return result.then(void 0, recover);
252
- }
253
- return result;
254
- }
255
- function _continueIgnored(value) {
256
- if (value && value.then) {
257
- return value.then(_empty);
258
- }
259
- }
260
- Object.defineProperty(exports, "__esModule", {
261
- value: true
262
- });
2
+ Object.defineProperty(exports, "__esModule", { value: true });
263
3
  exports.SIGKILL = exports.getProcessTree = exports.getProcessByPpid = exports.isProcessDied = exports.getProcessCommandByPid = exports.getProcessCpuUseByPid = exports.getProcessName = void 0;
264
- var child_process_1 = require("child_process");
265
- var pubo_utils_1 = require("pubo-utils");
4
+ const child_process_1 = require("child_process");
5
+ const pubo_utils_1 = require("pubo-utils");
266
6
  // 获取进程名称
267
7
  function getProcessName(pid) {
268
- return new Promise(function (resolve, reject) {
269
- (0, child_process_1.exec)("grep \"Name:\" /proc/" + pid + "/status", function (err, data) {
270
- if (err) {
271
- reject(err);
272
- } else {
273
- resolve(data);
274
- }
8
+ return new Promise((resolve, reject) => {
9
+ (0, child_process_1.exec)(`grep "Name:" /proc/${pid}/status`, (err, data) => {
10
+ if (err) {
11
+ reject(err);
12
+ }
13
+ else {
14
+ resolve(data);
15
+ }
16
+ });
275
17
  });
276
- });
277
18
  }
278
19
  exports.getProcessName = getProcessName;
279
20
  // 获取进程 cpu 使用率
280
21
  function getProcessCpuUseByPid(pid) {
281
- return new Promise(function (resolve) {
282
- (0, child_process_1.exec)("ps -p " + pid + " -o %cpu=", function (err, stdout) {
283
- if (err) {
284
- resolve(-1);
285
- } else {
286
- resolve(parseFloat(stdout.toString()));
287
- }
22
+ return new Promise((resolve) => {
23
+ (0, child_process_1.exec)(`ps -p ${pid} -o %cpu=`, (err, stdout) => {
24
+ if (err) {
25
+ resolve(-1);
26
+ }
27
+ else {
28
+ resolve(parseFloat(stdout.toString()));
29
+ }
30
+ });
288
31
  });
289
- });
290
32
  }
291
33
  exports.getProcessCpuUseByPid = getProcessCpuUseByPid;
292
34
  // 获取进程 command 使用率
293
35
  function getProcessCommandByPid(pid) {
294
- return new Promise(function (resolve) {
295
- (0, child_process_1.exec)("ps -p " + pid + " -o command=", function (err, stdout) {
296
- if (err) {
297
- resolve('');
298
- } else {
299
- resolve(stdout.toString().split('\n')[0]);
300
- }
36
+ return new Promise((resolve) => {
37
+ (0, child_process_1.exec)(`ps -p ${pid} -o command=`, (err, stdout) => {
38
+ if (err) {
39
+ resolve('');
40
+ }
41
+ else {
42
+ resolve(stdout.toString().split('\n')[0]);
43
+ }
44
+ });
301
45
  });
302
- });
303
46
  }
304
47
  exports.getProcessCommandByPid = getProcessCommandByPid;
48
+ // 判断进程是否死亡
49
+ async function isProcessDied(pid) {
50
+ const used = await getProcessCpuUseByPid(pid);
51
+ return used < 0;
52
+ }
305
53
  exports.isProcessDied = isProcessDied;
306
54
  // 获取子进程
307
55
  function getProcessByPpid(pid) {
308
- return new Promise(function (resolve) {
309
- (0, child_process_1.exec)("ps -o pid --no-headers --ppid " + pid, function (err, stdout) {
310
- if (err) {
311
- resolve([]);
312
- } else {
313
- resolve(stdout.split('\n').filter(function (item) {
314
- return !!item;
315
- }).map(function (item) {
316
- return parseFloat(item.trim());
317
- }));
318
- }
56
+ return new Promise((resolve) => {
57
+ (0, child_process_1.exec)(`ps -o pid --no-headers --ppid ${pid}`, (err, stdout) => {
58
+ if (err) {
59
+ resolve([]);
60
+ }
61
+ else {
62
+ resolve(stdout
63
+ .split('\n')
64
+ .filter((item) => !!item)
65
+ .map((item) => parseFloat(item.trim())));
66
+ }
67
+ });
319
68
  });
320
- });
321
69
  }
322
70
  exports.getProcessByPpid = getProcessByPpid;
323
71
  // 获取进程树
324
- var getProcessTree = _async(function (pid, tree) {
325
- if (!tree) {
326
- tree = {
327
- pid: pid,
328
- children: []
329
- };
330
- }
331
- return _await(getProcessByPpid(pid), function (pids) {
332
- return _continue(_forOf(pids, function (id) {
333
- var item = {
334
- pid: id,
335
- children: []
336
- };
337
- return _await((0, exports.getProcessTree)(id, item), function () {
72
+ const getProcessTree = async (pid, tree) => {
73
+ let isRoot = false;
74
+ if (!tree) {
75
+ isRoot = true;
76
+ tree = { pid, children: [] };
77
+ }
78
+ const pids = await getProcessByPpid(pid);
79
+ for (const id of pids) {
80
+ const item = { pid: id, children: [] };
81
+ await (0, exports.getProcessTree)(id, item);
338
82
  tree.children.push(item);
339
- });
340
- }), function () {
341
- return tree;
342
- });
343
- });
344
- });
83
+ }
84
+ if (isRoot) {
85
+ return tree;
86
+ }
87
+ else {
88
+ tree = null;
89
+ }
90
+ };
345
91
  exports.getProcessTree = getProcessTree;
346
- var flatProcessTree = function flatProcessTree(tree, tmp) {
347
- if (tree.children) {
348
- tmp.push.apply(tmp, tree.children.map(function (item) {
349
- return item.pid;
350
- }));
351
- tree.children.forEach(function (item) {
352
- return flatProcessTree(item, tmp);
353
- });
354
- }
92
+ // 杀死进程
93
+ async function _SIGKILL(pid, signal = 2, times = 1) {
94
+ if (times > 5) {
95
+ throw new Error('SIGKILL 失败. times > 5');
96
+ }
97
+ if (process.platform === 'win32') {
98
+ process.kill(pid, signal);
99
+ return;
100
+ }
101
+ (0, child_process_1.exec)(`kill -${signal} ${pid}`);
102
+ try {
103
+ await (0, pubo_utils_1.waitFor)(async () => isProcessDied(pid), { checkTime: 1000, timeout: 10000 });
104
+ }
105
+ catch (err) {
106
+ await _SIGKILL(pid, 9, times + 1);
107
+ }
108
+ }
109
+ // 广度优先遍历进程树,将pid放入tmp
110
+ const flatProcessTree = (tree, tmp) => {
111
+ if (tree.children) {
112
+ tmp.push(...tree.children.map((item) => item.pid));
113
+ tree.children.forEach((item) => flatProcessTree(item, tmp));
114
+ }
115
+ tree = null;
116
+ tmp = null;
355
117
  };
356
- exports.SIGKILL = SIGKILL;
118
+ async function SIGKILL(pid, signal = 2) {
119
+ if (process.platform === 'win32') {
120
+ return new Promise((resolve, reject) => {
121
+ (0, child_process_1.exec)(`taskkill /pid ${pid} /T /F`, (err) => {
122
+ if (err) {
123
+ reject(err);
124
+ }
125
+ else {
126
+ resolve('');
127
+ }
128
+ });
129
+ });
130
+ }
131
+ let tree = await (0, exports.getProcessTree)(pid);
132
+ // 获取所有进程PID,从叶到根
133
+ const tmp = [tree.pid];
134
+ flatProcessTree(tree, tmp);
135
+ tmp.reverse();
136
+ tree = null;
137
+ for (const item of tmp) {
138
+ await _SIGKILL(item, signal);
139
+ }
140
+ return 'success';
141
+ }
142
+ exports.SIGKILL = SIGKILL;
@@ -1,70 +1,52 @@
1
1
  "use strict";
2
-
3
- var __createBinding = this && this.__createBinding || (Object.create ? function (o, m, k, k2) {
4
- if (k2 === undefined) k2 = k;
5
- var desc = Object.getOwnPropertyDescriptor(m, k);
6
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
7
- desc = {
8
- enumerable: true,
9
- get: function get() {
10
- return m[k];
11
- }
12
- };
13
- }
14
- Object.defineProperty(o, k2, desc);
15
- } : function (o, m, k, k2) {
16
- if (k2 === undefined) k2 = k;
17
- o[k2] = m[k];
18
- });
19
- var __setModuleDefault = this && this.__setModuleDefault || (Object.create ? function (o, v) {
20
- Object.defineProperty(o, "default", {
21
- enumerable: true,
22
- value: v
23
- });
24
- } : function (o, v) {
25
- o["default"] = v;
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
26
17
  });
27
- var __importStar = this && this.__importStar || function (mod) {
28
- if (mod && mod.__esModule) return mod;
29
- var result = {};
30
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
31
- __setModuleDefault(result, mod);
32
- return result;
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
33
24
  };
34
- Object.defineProperty(exports, "__esModule", {
35
- value: true
36
- });
25
+ Object.defineProperty(exports, "__esModule", { value: true });
37
26
  exports.PuboFileSystem = void 0;
38
- var fs = __importStar(require("fs"));
39
- var callbackToPromise = function callbackToPromise(fn) {
40
- return function () {
41
- for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
42
- args[_key] = arguments[_key];
43
- }
44
- return new Promise(function (resolve, reject) {
45
- fn.apply(void 0, args.concat([function (err) {
46
- if (err) {
47
- reject(err);
48
- }
49
- for (var _len2 = arguments.length, rest = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
50
- rest[_key2 - 1] = arguments[_key2];
51
- }
52
- if (rest.length < 2) {
53
- resolve(rest[0]);
54
- } else {
55
- resolve([].concat(rest));
56
- }
57
- }]));
27
+ const fs = __importStar(require("fs"));
28
+ const callbackToPromise = (fn) => {
29
+ return (...args) => new Promise((resolve, reject) => {
30
+ fn(...args, (err, ...rest) => {
31
+ if (err) {
32
+ reject(err);
33
+ }
34
+ if (rest.length < 2) {
35
+ resolve(rest[0]);
36
+ }
37
+ else {
38
+ resolve([...rest]);
39
+ }
40
+ });
58
41
  });
59
- };
60
42
  };
61
43
  exports.PuboFileSystem = {
62
- read: callbackToPromise(fs.read),
63
- readFile: callbackToPromise(fs.readFile),
64
- writeFile: callbackToPromise(fs.writeFile),
65
- readdir: callbackToPromise(fs.readdir),
66
- open: callbackToPromise(fs.open),
67
- close: callbackToPromise(fs.close),
68
- write: callbackToPromise(fs.write),
69
- stat: callbackToPromise(fs.stat)
70
- };
44
+ read: callbackToPromise(fs.read),
45
+ readFile: callbackToPromise(fs.readFile),
46
+ writeFile: callbackToPromise(fs.writeFile),
47
+ readdir: callbackToPromise(fs.readdir),
48
+ open: callbackToPromise(fs.open),
49
+ close: callbackToPromise(fs.close),
50
+ write: callbackToPromise(fs.write),
51
+ stat: callbackToPromise(fs.stat),
52
+ };