pubo-node 1.0.141 → 1.0.142

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.
@@ -1,171 +1,268 @@
1
- var __awaiter = this && this.__awaiter || function (thisArg, _arguments, P, generator) {
2
- function adopt(value) {
3
- return value instanceof P ? value : new P(function (resolve) {
4
- resolve(value);
5
- });
1
+ import { exec } from 'child_process';
2
+ import { waitFor } from 'pubo-utils';
3
+ // 获取进程名称
4
+
5
+ function _await(value, then, direct) {
6
+ if (direct) {
7
+ return then ? then(value) : value;
6
8
  }
7
- return new (P || (P = Promise))(function (resolve, reject) {
8
- function fulfilled(value) {
9
- try {
10
- step(generator.next(value));
11
- } catch (e) {
12
- reject(e);
9
+ if (!value || !value.then) {
10
+ value = Promise.resolve(value);
11
+ }
12
+ return then ? value.then(then) : value;
13
+ }
14
+ function _async(f) {
15
+ return function () {
16
+ for (var args = [], i = 0; i < arguments.length; i++) {
17
+ args[i] = arguments[i];
18
+ }
19
+ try {
20
+ return Promise.resolve(f.apply(this, args));
21
+ } catch (e) {
22
+ return Promise.reject(e);
23
+ }
24
+ };
25
+ }
26
+ var _iteratorSymbol = /*#__PURE__*/typeof Symbol !== "undefined" ? Symbol.iterator || (Symbol.iterator = Symbol("Symbol.iterator")) : "@@iterator";
27
+ function _settle(pact, state, value) {
28
+ if (!pact.s) {
29
+ if (value instanceof _Pact) {
30
+ if (value.s) {
31
+ if (state & 1) {
32
+ state = value.s;
33
+ }
34
+ value = value.v;
35
+ } else {
36
+ value.o = _settle.bind(null, pact, state);
37
+ return;
38
+ }
39
+ }
40
+ if (value && value.then) {
41
+ value.then(_settle.bind(null, pact, state), _settle.bind(null, pact, 2));
42
+ return;
43
+ }
44
+ pact.s = state;
45
+ pact.v = value;
46
+ var observer = pact.o;
47
+ if (observer) {
48
+ observer(pact);
49
+ }
50
+ }
51
+ }
52
+ var _Pact = /*#__PURE__*/function () {
53
+ function _Pact() {}
54
+ _Pact.prototype.then = function (onFulfilled, onRejected) {
55
+ var result = new _Pact();
56
+ var state = this.s;
57
+ if (state) {
58
+ var callback = state & 1 ? onFulfilled : onRejected;
59
+ if (callback) {
60
+ try {
61
+ _settle(result, 1, callback(this.v));
62
+ } catch (e) {
63
+ _settle(result, 2, e);
64
+ }
65
+ return result;
66
+ } else {
67
+ return this;
13
68
  }
14
69
  }
15
- function rejected(value) {
70
+ this.o = function (_this) {
16
71
  try {
17
- step(generator["throw"](value));
72
+ var value = _this.v;
73
+ if (_this.s & 1) {
74
+ _settle(result, 1, onFulfilled ? onFulfilled(value) : value);
75
+ } else if (onRejected) {
76
+ _settle(result, 1, onRejected(value));
77
+ } else {
78
+ _settle(result, 2, value);
79
+ }
18
80
  } catch (e) {
19
- reject(e);
81
+ _settle(result, 2, e);
20
82
  }
21
- }
22
- function step(result) {
23
- result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
24
- }
25
- step((generator = generator.apply(thisArg, _arguments || [])).next());
26
- });
27
- };
28
- var __generator = this && this.__generator || function (thisArg, body) {
29
- var _ = {
30
- label: 0,
31
- sent: function sent() {
32
- if (t[0] & 1) throw t[1];
33
- return t[1];
34
- },
35
- trys: [],
36
- ops: []
37
- },
38
- f,
39
- y,
40
- t,
41
- g;
42
- return g = {
43
- next: verb(0),
44
- "throw": verb(1),
45
- "return": verb(2)
46
- }, typeof Symbol === "function" && (g[Symbol.iterator] = function () {
47
- return this;
48
- }), g;
49
- function verb(n) {
50
- return function (v) {
51
- return step([n, v]);
52
83
  };
53
- }
54
- function step(op) {
55
- if (f) throw new TypeError("Generator is already executing.");
56
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
57
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
58
- if (y = 0, t) op = [op[0] & 2, t.value];
59
- switch (op[0]) {
60
- case 0:
61
- case 1:
62
- t = op;
63
- break;
64
- case 4:
65
- _.label++;
66
- return {
67
- value: op[1],
68
- done: false
69
- };
70
- case 5:
71
- _.label++;
72
- y = op[1];
73
- op = [0];
74
- continue;
75
- case 7:
76
- op = _.ops.pop();
77
- _.trys.pop();
78
- continue;
79
- default:
80
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
81
- _ = 0;
82
- continue;
83
- }
84
- if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
85
- _.label = op[1];
86
- break;
87
- }
88
- if (op[0] === 6 && _.label < t[1]) {
89
- _.label = t[1];
90
- t = op;
91
- break;
92
- }
93
- if (t && _.label < t[2]) {
94
- _.label = t[2];
95
- _.ops.push(op);
96
- break;
84
+ return result;
85
+ };
86
+ return _Pact;
87
+ }();
88
+ function _isSettledPact(thenable) {
89
+ return thenable instanceof _Pact && thenable.s & 1;
90
+ }
91
+ function _forTo(array, body, check) {
92
+ var i = -1,
93
+ pact,
94
+ reject;
95
+ function _cycle(result) {
96
+ try {
97
+ while (++i < array.length && (!check || !check())) {
98
+ result = body(i);
99
+ if (result && result.then) {
100
+ if (_isSettledPact(result)) {
101
+ result = result.v;
102
+ } else {
103
+ result.then(_cycle, reject || (reject = _settle.bind(null, pact = new _Pact(), 2)));
104
+ return;
97
105
  }
98
- if (t[2]) _.ops.pop();
99
- _.trys.pop();
100
- continue;
106
+ }
107
+ }
108
+ if (pact) {
109
+ _settle(pact, 1, result);
110
+ } else {
111
+ pact = result;
101
112
  }
102
- op = body.call(thisArg, _);
103
113
  } catch (e) {
104
- op = [6, e];
105
- y = 0;
106
- } finally {
107
- f = t = 0;
114
+ _settle(pact || (pact = new _Pact()), 2, e);
108
115
  }
109
- if (op[0] & 5) throw op[1];
110
- return {
111
- value: op[0] ? op[1] : void 0,
112
- done: true
113
- };
114
116
  }
115
- };
116
- var __values = this && this.__values || function (o) {
117
- var s = typeof Symbol === "function" && Symbol.iterator,
118
- m = s && o[s],
119
- i = 0;
120
- if (m) return m.call(o);
121
- if (o && typeof o.length === "number") return {
122
- next: function next() {
123
- if (o && i >= o.length) o = void 0;
124
- return {
125
- value: o && o[i++],
126
- done: !o
117
+ _cycle();
118
+ return pact;
119
+ }
120
+ function _forOf(target, body, check) {
121
+ if (typeof target[_iteratorSymbol] === "function") {
122
+ var _cycle = function _cycle(result) {
123
+ try {
124
+ while (!(step = iterator.next()).done && (!check || !check())) {
125
+ result = body(step.value);
126
+ if (result && result.then) {
127
+ if (_isSettledPact(result)) {
128
+ result = result.v;
129
+ } else {
130
+ result.then(_cycle, reject || (reject = _settle.bind(null, pact = new _Pact(), 2)));
131
+ return;
132
+ }
133
+ }
134
+ }
135
+ if (pact) {
136
+ _settle(pact, 1, result);
137
+ } else {
138
+ pact = result;
139
+ }
140
+ } catch (e) {
141
+ _settle(pact || (pact = new _Pact()), 2, e);
142
+ }
143
+ };
144
+ var iterator = target[_iteratorSymbol](),
145
+ step,
146
+ pact,
147
+ reject;
148
+ _cycle();
149
+ if (iterator["return"]) {
150
+ var _fixup = function _fixup(value) {
151
+ try {
152
+ if (!step.done) {
153
+ iterator["return"]();
154
+ }
155
+ } catch (e) {}
156
+ return value;
127
157
  };
158
+ if (pact && pact.then) {
159
+ return pact.then(_fixup, function (e) {
160
+ throw _fixup(e);
161
+ });
162
+ }
163
+ _fixup();
128
164
  }
129
- };
130
- throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
131
- };
132
- var __read = this && this.__read || function (o, n) {
133
- var m = typeof Symbol === "function" && o[Symbol.iterator];
134
- if (!m) return o;
135
- var i = m.call(o),
136
- r,
137
- ar = [],
138
- e;
165
+ return pact;
166
+ }
167
+ // No support for Symbol.iterator
168
+ if (!("length" in target)) {
169
+ throw new TypeError("Object is not iterable");
170
+ }
171
+ // Handle live collections properly
172
+ var values = [];
173
+ for (var i = 0; i < target.length; i++) {
174
+ values.push(target[i]);
175
+ }
176
+ return _forTo(values, function (i) {
177
+ return body(values[i]);
178
+ }, check);
179
+ }
180
+ function _continue(value, then) {
181
+ return value && value.then ? value.then(then) : then(value);
182
+ }
183
+ function _empty() {}
184
+ function _awaitIgnored(value, direct) {
185
+ if (!direct) {
186
+ return value && value.then ? value.then(_empty) : Promise.resolve();
187
+ }
188
+ }
189
+ function _catch(body, recover) {
139
190
  try {
140
- while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
141
- } catch (error) {
142
- e = {
143
- error: error
144
- };
145
- } finally {
146
- try {
147
- if (r && !r.done && (m = i["return"])) m.call(i);
148
- } finally {
149
- if (e) throw e.error;
150
- }
191
+ var result = body();
192
+ } catch (e) {
193
+ return recover(e);
151
194
  }
152
- return ar;
153
- };
154
- var __spreadArray = this && this.__spreadArray || function (to, from, pack) {
155
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
156
- if (ar || !(i in from)) {
157
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
158
- ar[i] = from[i];
159
- }
195
+ if (result && result.then) {
196
+ return result.then(void 0, recover);
160
197
  }
161
- return to.concat(ar || Array.prototype.slice.call(from));
162
- };
163
- import { exec } from 'child_process';
164
- import { waitFor } from 'pubo-utils';
165
- // 获取进程名称
198
+ return result;
199
+ }
200
+ function _continueIgnored(value) {
201
+ if (value && value.then) {
202
+ return value.then(_empty);
203
+ }
204
+ }
205
+ export var SIGKILL = _async(function (pid, signal) {
206
+ if (signal === void 0) {
207
+ signal = 2;
208
+ }
209
+ return process.platform === 'win32' ? new Promise(function (resolve, reject) {
210
+ exec("taskkill /pid " + pid + " /T /F", function (err) {
211
+ if (err) {
212
+ reject(err);
213
+ } else {
214
+ resolve('');
215
+ }
216
+ });
217
+ }) : _await(getProcessTree(pid), function (tree) {
218
+ // 获取所有进程PID,从叶到根
219
+ var tmp = [tree.pid];
220
+ flatProcessTree(tree, tmp);
221
+ tmp.reverse();
222
+ return _continue(_forOf(tmp, function (item) {
223
+ return _awaitIgnored(_SIGKILL(item, signal));
224
+ }), function () {
225
+ return 'success';
226
+ });
227
+ });
228
+ });
229
+ // 杀死进程
230
+ var _SIGKILL = _async(function (pid, signal, times) {
231
+ if (signal === void 0) {
232
+ signal = 2;
233
+ }
234
+ if (times === void 0) {
235
+ times = 1;
236
+ }
237
+ if (times > 5) {
238
+ throw new Error('SIGKILL 失败. times > 5');
239
+ }
240
+ if (process.platform === 'win32') {
241
+ process.kill(pid, signal);
242
+ return;
243
+ }
244
+ exec("kill -" + signal + " " + pid);
245
+ return _continueIgnored(_catch(function () {
246
+ return _awaitIgnored(waitFor(function () {
247
+ return isProcessDied(pid);
248
+ }, {
249
+ checkTime: 1000,
250
+ timeout: 10000
251
+ }));
252
+ }, function () {
253
+ return _awaitIgnored(_SIGKILL(pid, 9, times + 1));
254
+ }));
255
+ }); // 广度优先遍历进程树,将pid放入tmp
256
+ // 判断进程是否死亡
257
+ export var isProcessDied = _async(function (pid) {
258
+ return _await(getProcessCpuUseByPid(pid), function (used) {
259
+ return used < 0;
260
+ });
261
+ });
262
+ // 获取子进程
166
263
  export function getProcessName(pid) {
167
264
  return new Promise(function (resolve, reject) {
168
- exec("grep \"Name:\" /proc/".concat(pid, "/status"), function (err, data) {
265
+ exec("grep \"Name:\" /proc/" + pid + "/status", function (err, data) {
169
266
  if (err) {
170
267
  reject(err);
171
268
  } else {
@@ -177,7 +274,7 @@ export function getProcessName(pid) {
177
274
  // 获取进程 cpu 使用率
178
275
  export function getProcessCpuUseByPid(pid) {
179
276
  return new Promise(function (resolve) {
180
- exec("ps -p ".concat(pid, " -o %cpu="), function (err, stdout) {
277
+ exec("ps -p " + pid + " -o %cpu=", function (err, stdout) {
181
278
  if (err) {
182
279
  resolve(-1);
183
280
  } else {
@@ -189,7 +286,7 @@ export function getProcessCpuUseByPid(pid) {
189
286
  // 获取进程 command 使用率
190
287
  export function getProcessCommandByPid(pid) {
191
288
  return new Promise(function (resolve) {
192
- exec("ps -p ".concat(pid, " -o command="), function (err, stdout) {
289
+ exec("ps -p " + pid + " -o command=", function (err, stdout) {
193
290
  if (err) {
194
291
  resolve('');
195
292
  } else {
@@ -198,25 +295,9 @@ export function getProcessCommandByPid(pid) {
198
295
  });
199
296
  });
200
297
  }
201
- // 判断进程是否死亡
202
- export function isProcessDied(pid) {
203
- return __awaiter(this, void 0, void 0, function () {
204
- var used;
205
- return __generator(this, function (_a) {
206
- switch (_a.label) {
207
- case 0:
208
- return [4 /*yield*/, getProcessCpuUseByPid(pid)];
209
- case 1:
210
- used = _a.sent();
211
- return [2 /*return*/, used < 0];
212
- }
213
- });
214
- });
215
- }
216
- // 获取子进程
217
298
  export function getProcessByPpid(pid) {
218
299
  return new Promise(function (resolve) {
219
- exec("ps -o pid --no-headers --ppid ".concat(pid), function (err, stdout) {
300
+ exec("ps -o pid --no-headers --ppid " + pid, function (err, stdout) {
220
301
  if (err) {
221
302
  resolve([]);
222
303
  } else {
@@ -230,185 +311,34 @@ export function getProcessByPpid(pid) {
230
311
  });
231
312
  }
232
313
  // 获取进程树
233
- export var getProcessTree = function getProcessTree(pid, tree) {
234
- return __awaiter(void 0, void 0, void 0, function () {
235
- var pids, pids_1, pids_1_1, id, item, e_1_1;
236
- var e_1, _a;
237
- return __generator(this, function (_b) {
238
- switch (_b.label) {
239
- case 0:
240
- if (!tree) {
241
- tree = {
242
- pid: pid,
243
- children: []
244
- };
245
- }
246
- return [4 /*yield*/, getProcessByPpid(pid)];
247
- case 1:
248
- pids = _b.sent();
249
- _b.label = 2;
250
- case 2:
251
- _b.trys.push([2, 7, 8, 9]);
252
- pids_1 = __values(pids), pids_1_1 = pids_1.next();
253
- _b.label = 3;
254
- case 3:
255
- if (!!pids_1_1.done) return [3 /*break*/, 6];
256
- id = pids_1_1.value;
257
- item = {
258
- pid: id,
259
- children: []
260
- };
261
- return [4 /*yield*/, getProcessTree(id, item)];
262
- case 4:
263
- _b.sent();
264
- tree.children.push(item);
265
- _b.label = 5;
266
- case 5:
267
- pids_1_1 = pids_1.next();
268
- return [3 /*break*/, 3];
269
- case 6:
270
- return [3 /*break*/, 9];
271
- case 7:
272
- e_1_1 = _b.sent();
273
- e_1 = {
274
- error: e_1_1
275
- };
276
- return [3 /*break*/, 9];
277
- case 8:
278
- try {
279
- if (pids_1_1 && !pids_1_1.done && (_a = pids_1["return"])) _a.call(pids_1);
280
- } finally {
281
- if (e_1) throw e_1.error;
282
- }
283
- return [7 /*endfinally*/];
284
- case 9:
285
- return [2 /*return*/, tree];
286
- }
287
- });
288
- });
289
- };
290
- // 杀死进程
291
- function _SIGKILL(pid, signal, times) {
292
- if (signal === void 0) {
293
- signal = 2;
294
- }
295
- if (times === void 0) {
296
- times = 1;
314
+ export var getProcessTree = _async(function (pid, tree) {
315
+ if (!tree) {
316
+ tree = {
317
+ pid: pid,
318
+ children: []
319
+ };
297
320
  }
298
- return __awaiter(this, void 0, void 0, function () {
299
- var err_1;
300
- var _this = this;
301
- return __generator(this, function (_a) {
302
- switch (_a.label) {
303
- case 0:
304
- if (times > 5) {
305
- throw new Error('SIGKILL 失败. times > 5');
306
- }
307
- if (process.platform === 'win32') {
308
- process.kill(pid, signal);
309
- return [2 /*return*/];
310
- }
311
-
312
- exec("kill -".concat(signal, " ").concat(pid));
313
- _a.label = 1;
314
- case 1:
315
- _a.trys.push([1, 3,, 5]);
316
- return [4 /*yield*/, waitFor(function () {
317
- return __awaiter(_this, void 0, void 0, function () {
318
- return __generator(this, function (_a) {
319
- return [2 /*return*/, isProcessDied(pid)];
320
- });
321
- });
322
- }, {
323
- checkTime: 1000,
324
- timeout: 10000
325
- })];
326
- case 2:
327
- _a.sent();
328
- return [3 /*break*/, 5];
329
- case 3:
330
- err_1 = _a.sent();
331
- return [4 /*yield*/, _SIGKILL(pid, 9, times + 1)];
332
- case 4:
333
- _a.sent();
334
- return [3 /*break*/, 5];
335
- case 5:
336
- return [2 /*return*/];
337
- }
321
+ return _await(getProcessByPpid(pid), function (pids) {
322
+ return _continue(_forOf(pids, function (id) {
323
+ var item = {
324
+ pid: id,
325
+ children: []
326
+ };
327
+ return _await(getProcessTree(id, item), function () {
328
+ tree.children.push(item);
329
+ });
330
+ }), function () {
331
+ return tree;
338
332
  });
339
333
  });
340
- }
341
- // 广度优先遍历进程树,将pid放入tmp
334
+ });
342
335
  var flatProcessTree = function flatProcessTree(tree, tmp) {
343
336
  if (tree.children) {
344
- tmp.push.apply(tmp, __spreadArray([], __read(tree.children.map(function (item) {
337
+ tmp.push.apply(tmp, tree.children.map(function (item) {
345
338
  return item.pid;
346
- })), false));
339
+ }));
347
340
  tree.children.forEach(function (item) {
348
341
  return flatProcessTree(item, tmp);
349
342
  });
350
343
  }
351
- };
352
- export function SIGKILL(pid, signal) {
353
- if (signal === void 0) {
354
- signal = 2;
355
- }
356
- return __awaiter(this, void 0, void 0, function () {
357
- var tree, tmp, tmp_1, tmp_1_1, item, e_2_1;
358
- var e_2, _a;
359
- return __generator(this, function (_b) {
360
- switch (_b.label) {
361
- case 0:
362
- if (process.platform === 'win32') {
363
- return [2 /*return*/, new Promise(function (resolve, reject) {
364
- exec("taskkill /pid ".concat(pid, " /T /F"), function (err) {
365
- if (err) {
366
- reject(err);
367
- } else {
368
- resolve('');
369
- }
370
- });
371
- })];
372
- }
373
- return [4 /*yield*/, getProcessTree(pid)];
374
- case 1:
375
- tree = _b.sent();
376
- tmp = [tree.pid];
377
- flatProcessTree(tree, tmp);
378
- tmp.reverse();
379
- _b.label = 2;
380
- case 2:
381
- _b.trys.push([2, 7, 8, 9]);
382
- tmp_1 = __values(tmp), tmp_1_1 = tmp_1.next();
383
- _b.label = 3;
384
- case 3:
385
- if (!!tmp_1_1.done) return [3 /*break*/, 6];
386
- item = tmp_1_1.value;
387
- return [4 /*yield*/, _SIGKILL(item, signal)];
388
- case 4:
389
- _b.sent();
390
- _b.label = 5;
391
- case 5:
392
- tmp_1_1 = tmp_1.next();
393
- return [3 /*break*/, 3];
394
- case 6:
395
- return [3 /*break*/, 9];
396
- case 7:
397
- e_2_1 = _b.sent();
398
- e_2 = {
399
- error: e_2_1
400
- };
401
- return [3 /*break*/, 9];
402
- case 8:
403
- try {
404
- if (tmp_1_1 && !tmp_1_1.done && (_a = tmp_1["return"])) _a.call(tmp_1);
405
- } finally {
406
- if (e_2) throw e_2.error;
407
- }
408
- return [7 /*endfinally*/];
409
- case 9:
410
- return [2 /*return*/, 'success'];
411
- }
412
- });
413
- });
414
- }
344
+ };