pubo-node 1.0.129 → 1.0.136

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,3 +1,7 @@
1
1
  export declare function getProcessName(pid: any): Promise<string>;
2
+ export declare function getProcessCpuUseByPid(pid: number): Promise<number>;
3
+ export declare function getProcessCommandByPid(pid: number): Promise<string>;
2
4
  export declare function isProcessDied(pid: any): Promise<boolean>;
5
+ export declare function getProcessByPpid(pid: number): Promise<number[]>;
6
+ export declare const getProcessTree: (pid: number, tree?: any) => Promise<any>;
3
7
  export declare function SIGKILL(pid: any, type?: number): Promise<void>;
@@ -113,47 +113,150 @@ var __generator = this && this.__generator || function (thisArg, body) {
113
113
  };
114
114
  }
115
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
127
+ };
128
+ }
129
+ };
130
+ throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
131
+ };
116
132
  import { exec } from 'child_process';
117
133
  import { waitFor } from 'pubo-utils';
134
+ // 获取进程名称
118
135
  export function getProcessName(pid) {
119
- return __awaiter(this, void 0, void 0, function () {
120
- return __generator(this, function (_a) {
121
- return [2 /*return*/, new Promise(function (resolve) {
122
- var child = exec("grep \"Name:\" /proc/".concat(pid, "/status"));
123
- var resolved = false;
124
- var cb = function cb(data) {
125
- if (resolved) {
126
- return;
127
- }
128
- resolved = true;
129
- resolve(data);
130
- child = null;
131
- };
132
- child.stdout.on('data', function (data) {
133
- return cb(data.toString());
134
- });
135
- child.stderr.on('data', function (data) {
136
- return cb(data.toString());
137
- });
138
- })];
136
+ return new Promise(function (resolve, reject) {
137
+ exec("grep \"Name:\" /proc/".concat(pid, "/status"), function (err, data) {
138
+ if (err) {
139
+ reject(err);
140
+ } else {
141
+ resolve(data);
142
+ }
143
+ });
144
+ });
145
+ }
146
+ // 获取进程 cpu 使用率
147
+ export function getProcessCpuUseByPid(pid) {
148
+ return new Promise(function (resolve) {
149
+ exec("ps -p ".concat(pid, " -o %cpu="), function (err, stdout) {
150
+ if (err) {
151
+ resolve(-1);
152
+ } else {
153
+ resolve(parseFloat(stdout.toString()));
154
+ }
139
155
  });
140
156
  });
141
157
  }
158
+ // 获取进程 command 使用率
159
+ export function getProcessCommandByPid(pid) {
160
+ return new Promise(function (resolve) {
161
+ exec("ps -p ".concat(pid, " -o command="), function (err, stdout) {
162
+ if (err) {
163
+ resolve('');
164
+ } else {
165
+ resolve(stdout.toString().split('\n')[0]);
166
+ }
167
+ });
168
+ });
169
+ }
170
+ // 判断进程是否死亡
142
171
  export function isProcessDied(pid) {
143
- var _a;
144
172
  return __awaiter(this, void 0, void 0, function () {
145
- var name;
173
+ var used;
174
+ return __generator(this, function (_a) {
175
+ switch (_a.label) {
176
+ case 0:
177
+ return [4 /*yield*/, getProcessCpuUseByPid(pid)];
178
+ case 1:
179
+ used = _a.sent();
180
+ return [2 /*return*/, used < 0];
181
+ }
182
+ });
183
+ });
184
+ }
185
+ // 获取子进程
186
+ export function getProcessByPpid(pid) {
187
+ return new Promise(function (resolve) {
188
+ exec("ps -o pid --no-headers --ppid ".concat(pid), function (err, stdout) {
189
+ if (err) {
190
+ resolve([]);
191
+ } else {
192
+ resolve(stdout.split('\n').filter(function (item) {
193
+ return !!item;
194
+ }).map(function (item) {
195
+ return parseFloat(item.trim());
196
+ }));
197
+ }
198
+ });
199
+ });
200
+ }
201
+ // 获取进程树
202
+ export var getProcessTree = function getProcessTree(pid, tree) {
203
+ return __awaiter(void 0, void 0, void 0, function () {
204
+ var pids, pids_1, pids_1_1, id, item, e_1_1;
205
+ var e_1, _a;
146
206
  return __generator(this, function (_b) {
147
207
  switch (_b.label) {
148
208
  case 0:
149
- return [4 /*yield*/, getProcessName(pid)];
209
+ if (!tree) {
210
+ tree = {
211
+ pid: pid,
212
+ children: []
213
+ };
214
+ }
215
+ return [4 /*yield*/, getProcessByPpid(pid)];
150
216
  case 1:
151
- name = _b.sent();
152
- return [2 /*return*/, ((_a = name.split(':')[2]) === null || _a === void 0 ? void 0 : _a.trim()) === 'No such file or directory'];
217
+ pids = _b.sent();
218
+ _b.label = 2;
219
+ case 2:
220
+ _b.trys.push([2, 7, 8, 9]);
221
+ pids_1 = __values(pids), pids_1_1 = pids_1.next();
222
+ _b.label = 3;
223
+ case 3:
224
+ if (!!pids_1_1.done) return [3 /*break*/, 6];
225
+ id = pids_1_1.value;
226
+ item = {
227
+ pid: id,
228
+ children: []
229
+ };
230
+ return [4 /*yield*/, getProcessTree(id, item)];
231
+ case 4:
232
+ _b.sent();
233
+ tree.children.push(item);
234
+ _b.label = 5;
235
+ case 5:
236
+ pids_1_1 = pids_1.next();
237
+ return [3 /*break*/, 3];
238
+ case 6:
239
+ return [3 /*break*/, 9];
240
+ case 7:
241
+ e_1_1 = _b.sent();
242
+ e_1 = {
243
+ error: e_1_1
244
+ };
245
+ return [3 /*break*/, 9];
246
+ case 8:
247
+ try {
248
+ if (pids_1_1 && !pids_1_1.done && (_a = pids_1["return"])) _a.call(pids_1);
249
+ } finally {
250
+ if (e_1) throw e_1.error;
251
+ }
252
+ return [7 /*endfinally*/];
253
+ case 9:
254
+ return [2 /*return*/, tree];
153
255
  }
154
256
  });
155
257
  });
156
- }
258
+ };
259
+ // 杀死进程
157
260
  export function SIGKILL(pid, type) {
158
261
  if (type === void 0) {
159
262
  type = 2;
@@ -1,10 +1,28 @@
1
1
  /// <reference types="node" />
2
- interface CreateClientProps {
2
+ export interface CreateClientProps {
3
3
  url: string;
4
4
  options?: any;
5
5
  ServiceImp: any;
6
6
  Grpc: any;
7
7
  cert?: Buffer;
8
8
  }
9
+ declare class GrpcClient {
10
+ private readonly url;
11
+ private readonly options;
12
+ private readonly Grpc;
13
+ private readonly credentials;
14
+ private client;
15
+ private _timeout;
16
+ connections: number;
17
+ constructor({ url, options, Grpc, cert }: any);
18
+ request(service: any, method: any, data: any): Promise<any>;
19
+ _request({ service, method, data }: {
20
+ service: any;
21
+ method: any;
22
+ data: any;
23
+ }): Promise<unknown>;
24
+ close(): void;
25
+ }
26
+ export declare const GrpcList: GrpcClient[];
9
27
  export declare function createRpcClient<T>({ url, options, ServiceImp, Grpc, cert }: CreateClientProps): T;
10
28
  export {};
package/es/grpc/index.js CHANGED
@@ -8,6 +8,124 @@ var __assign = this && this.__assign || function () {
8
8
  };
9
9
  return __assign.apply(this, arguments);
10
10
  };
11
+ var __awaiter = this && this.__awaiter || function (thisArg, _arguments, P, generator) {
12
+ function adopt(value) {
13
+ return value instanceof P ? value : new P(function (resolve) {
14
+ resolve(value);
15
+ });
16
+ }
17
+ return new (P || (P = Promise))(function (resolve, reject) {
18
+ function fulfilled(value) {
19
+ try {
20
+ step(generator.next(value));
21
+ } catch (e) {
22
+ reject(e);
23
+ }
24
+ }
25
+ function rejected(value) {
26
+ try {
27
+ step(generator["throw"](value));
28
+ } catch (e) {
29
+ reject(e);
30
+ }
31
+ }
32
+ function step(result) {
33
+ result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
34
+ }
35
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
36
+ });
37
+ };
38
+ var __generator = this && this.__generator || function (thisArg, body) {
39
+ var _ = {
40
+ label: 0,
41
+ sent: function sent() {
42
+ if (t[0] & 1) throw t[1];
43
+ return t[1];
44
+ },
45
+ trys: [],
46
+ ops: []
47
+ },
48
+ f,
49
+ y,
50
+ t,
51
+ g;
52
+ return g = {
53
+ next: verb(0),
54
+ "throw": verb(1),
55
+ "return": verb(2)
56
+ }, typeof Symbol === "function" && (g[Symbol.iterator] = function () {
57
+ return this;
58
+ }), g;
59
+ function verb(n) {
60
+ return function (v) {
61
+ return step([n, v]);
62
+ };
63
+ }
64
+ function step(op) {
65
+ if (f) throw new TypeError("Generator is already executing.");
66
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
67
+ 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;
68
+ if (y = 0, t) op = [op[0] & 2, t.value];
69
+ switch (op[0]) {
70
+ case 0:
71
+ case 1:
72
+ t = op;
73
+ break;
74
+ case 4:
75
+ _.label++;
76
+ return {
77
+ value: op[1],
78
+ done: false
79
+ };
80
+ case 5:
81
+ _.label++;
82
+ y = op[1];
83
+ op = [0];
84
+ continue;
85
+ case 7:
86
+ op = _.ops.pop();
87
+ _.trys.pop();
88
+ continue;
89
+ default:
90
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
91
+ _ = 0;
92
+ continue;
93
+ }
94
+ if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
95
+ _.label = op[1];
96
+ break;
97
+ }
98
+ if (op[0] === 6 && _.label < t[1]) {
99
+ _.label = t[1];
100
+ t = op;
101
+ break;
102
+ }
103
+ if (t && _.label < t[2]) {
104
+ _.label = t[2];
105
+ _.ops.push(op);
106
+ break;
107
+ }
108
+ if (t[2]) _.ops.pop();
109
+ _.trys.pop();
110
+ continue;
111
+ }
112
+ op = body.call(thisArg, _);
113
+ } catch (e) {
114
+ op = [6, e];
115
+ y = 0;
116
+ } finally {
117
+ f = t = 0;
118
+ }
119
+ if (op[0] & 5) throw op[1];
120
+ return {
121
+ value: op[0] ? op[1] : void 0,
122
+ done: true
123
+ };
124
+ }
125
+ };
126
+ function passThrough(argument) {
127
+ return argument;
128
+ }
11
129
  var GrpcClient = /** @class */function () {
12
130
  function GrpcClient(_a) {
13
131
  var url = _a.url,
@@ -16,6 +134,7 @@ var GrpcClient = /** @class */function () {
16
134
  Grpc = _a.Grpc,
17
135
  cert = _a.cert;
18
136
  var _c;
137
+ this.connections = 0;
19
138
  var opt = __assign({
20
139
  'grpc.max_send_message_length': -1,
21
140
  'grpc.max_receive_message_length': -1
@@ -25,37 +144,95 @@ var GrpcClient = /** @class */function () {
25
144
  this.Grpc = Grpc;
26
145
  this.credentials = credentials;
27
146
  this.options = opt;
28
- this.options.timeout = (_c = this.options.timeout) !== null && _c !== void 0 ? _c : 5000;
147
+ this.options.timeout = (_c = this.options.timeout) !== null && _c !== void 0 ? _c : 60000;
29
148
  }
30
- GrpcClient.prototype.passThrough = function (argument) {
31
- return argument;
32
- };
33
- GrpcClient.prototype.reset = function () {
34
- if (!this.client) {
35
- return;
36
- }
37
- this.client.close();
38
- this.client = null;
39
- };
40
149
  GrpcClient.prototype.request = function (service, method, data) {
150
+ return __awaiter(this, void 0, void 0, function () {
151
+ var error, result, err_1;
152
+ var _this = this;
153
+ return __generator(this, function (_a) {
154
+ switch (_a.label) {
155
+ case 0:
156
+ this.connections += 1;
157
+ if (!this.client) {
158
+ this.client = new this.Grpc.Client(this.url, this.credentials, this.options);
159
+ }
160
+ _a.label = 1;
161
+ case 1:
162
+ _a.trys.push([1, 3,, 4]);
163
+ return [4 /*yield*/, this._request({
164
+ service: service,
165
+ method: method,
166
+ data: data
167
+ })];
168
+ case 2:
169
+ result = _a.sent();
170
+ return [3 /*break*/, 4];
171
+ case 3:
172
+ err_1 = _a.sent();
173
+ error = err_1;
174
+ return [3 /*break*/, 4];
175
+ case 4:
176
+ this.connections -= 1;
177
+ if (this.connections < 0) {
178
+ this.connections = 0;
179
+ }
180
+ if (this.connections < 1) {
181
+ if (this._timeout) {
182
+ clearTimeout(this._timeout);
183
+ this._timeout = null;
184
+ }
185
+ this._timeout = setTimeout(function () {
186
+ return _this.close();
187
+ }, 60000);
188
+ }
189
+ if (error) {
190
+ this.close();
191
+ throw new Error('grpc connection error.');
192
+ }
193
+ return [2 /*return*/, result];
194
+ }
195
+ });
196
+ });
197
+ };
198
+ GrpcClient.prototype._request = function (_a) {
41
199
  var _this = this;
42
- var path = "/".concat(service, "/").concat(method);
43
- if (!this.client) {
44
- this.client = new this.Grpc.Client(this.url, this.credentials, this.options);
45
- }
200
+ var service = _a.service,
201
+ method = _a.method,
202
+ data = _a.data;
46
203
  return new Promise(function (resolve, reject) {
47
- _this.client.makeUnaryRequest(path, _this.passThrough, _this.passThrough, data, function (err, res) {
204
+ var _ended = false;
205
+ var _timeout = setTimeout(function () {
206
+ _ended = true;
207
+ _this.close();
208
+ console.log('rpc request timeout');
209
+ reject(new Error('timeout'));
210
+ }, _this.options.timeout);
211
+ var _onResponse = function onResponse(err, res) {
212
+ if (_ended) {
213
+ return;
214
+ } else {
215
+ clearTimeout(_timeout);
216
+ }
48
217
  if (err) {
49
- _this.reset();
50
218
  reject(err);
51
219
  } else {
52
220
  resolve(res);
53
221
  }
54
- });
222
+ _onResponse = null;
223
+ };
224
+ _this.client.makeUnaryRequest("/".concat(service, "/").concat(method), passThrough, passThrough, data, _onResponse);
55
225
  });
56
226
  };
227
+ GrpcClient.prototype.close = function () {
228
+ var _a;
229
+ (_a = this.client) === null || _a === void 0 ? void 0 : _a.close();
230
+ this.client = null;
231
+ delete this.client;
232
+ };
57
233
  return GrpcClient;
58
234
  }();
235
+ export var GrpcList = [];
59
236
  export function createRpcClient(_a) {
60
237
  var url = _a.url,
61
238
  _b = _a.options,
@@ -69,6 +246,7 @@ export function createRpcClient(_a) {
69
246
  Grpc: Grpc,
70
247
  cert: cert
71
248
  });
249
+ GrpcList.push(client);
72
250
  return new ServiceImp({
73
251
  request: client.request.bind(client)
74
252
  });
package/es/index.d.ts CHANGED
@@ -2,8 +2,8 @@ export { JsonStorage } from './storage/json';
2
2
  export { FtpClient, FtpClientPool, FtpConnectOptions, FtpFile } from './ftp-client';
3
3
  export { createRpcClient } from './grpc';
4
4
  export { PuboFileSystem } from './file-system';
5
- export { SIGKILL, isProcessDied, getProcessName } from './child-process';
5
+ export { SIGKILL, isProcessDied, getProcessName, getProcessTree, getProcessByPpid, getProcessCpuUseByPid, getProcessCommandByPid, } from './child-process';
6
6
  export { isPortAvailable } from './utils';
7
7
  export { pitch } from './pitch';
8
8
  export { getWifiName, getNetworks } from './utils/network';
9
- export { RosTopic, RosTopicManager } from './ros/topic';
9
+ export { RosTopicManager, RosTopic } from './ros/topic';
package/es/index.js CHANGED
@@ -2,8 +2,8 @@ export { JsonStorage } from './storage/json';
2
2
  export { FtpClient, FtpClientPool } from './ftp-client';
3
3
  export { createRpcClient } from './grpc';
4
4
  export { PuboFileSystem } from './file-system';
5
- export { SIGKILL, isProcessDied, getProcessName } from './child-process';
5
+ export { SIGKILL, isProcessDied, getProcessName, getProcessTree, getProcessByPpid, getProcessCpuUseByPid, getProcessCommandByPid } from './child-process';
6
6
  export { isPortAvailable } from './utils';
7
7
  export { pitch } from './pitch';
8
8
  export { getWifiName, getNetworks } from './utils/network';
9
- export { RosTopic, RosTopicManager } from './ros/topic';
9
+ export { RosTopicManager, RosTopic } from './ros/topic';
package/es/ros/topic.js CHANGED
@@ -52,18 +52,13 @@ var RosTopic = /** @class */function () {
52
52
  RosTopic.prototype.publish = function (payload) {
53
53
  var _this = this;
54
54
  var data = YAML.stringify(payload);
55
- return new Promise(function (resolve) {
56
- var _a, _b;
57
- var child = exec("rostopic pub -1 ".concat(_this.topic, " ").concat(_this.messageType, " \"").concat(data, "\""));
58
- (_a = child.stdout) === null || _a === void 0 ? void 0 : _a.once('data', function (buf) {
59
- console.log(buf.toString());
60
- (child === null || child === void 0 ? void 0 : child.pid) && SIGKILL(child === null || child === void 0 ? void 0 : child.pid);
61
- resolve('');
62
- });
63
- (_b = child.stderr) === null || _b === void 0 ? void 0 : _b.once('data', function (err) {
64
- console.log(err.toString());
65
- (child === null || child === void 0 ? void 0 : child.pid) && SIGKILL(child === null || child === void 0 ? void 0 : child.pid);
66
- resolve('');
55
+ return new Promise(function (resolve, reject) {
56
+ exec("rostopic pub -1 ".concat(_this.topic, " ").concat(_this.messageType, " \"").concat(data, "\""), function (err, stdout) {
57
+ if (err) {
58
+ reject(err);
59
+ } else {
60
+ resolve(stdout);
61
+ }
67
62
  });
68
63
  });
69
64
  };
@@ -1,8 +1,10 @@
1
+ export interface JsonStorageOptions {
2
+ initialState?: any;
3
+ defaultState?: any;
4
+ }
1
5
  export declare class JsonStorage {
2
6
  private readonly instance;
3
- constructor(path: string, options?: {
4
- initialState: any;
5
- });
7
+ constructor(path: string, options: JsonStorageOptions);
6
8
  private getState;
7
9
  private setState;
8
10
  get(key?: string): Promise<any>;
@@ -133,8 +133,9 @@ import { readFileSync, writeFile, mkdirSync } from 'fs';
133
133
  import { SyncQueue } from 'pubo-utils';
134
134
  import { v4 as uuid } from 'uuid';
135
135
  var cluster = require('cluster');
136
+ // 主线程的实现
136
137
  var Manager = /** @class */function () {
137
- function Manager(path) {
138
+ function Manager(path, defaultState) {
138
139
  var _this = this;
139
140
  this._state = {};
140
141
  this.queue = new SyncQueue();
@@ -163,6 +164,7 @@ var Manager = /** @class */function () {
163
164
  payload = _a.sent();
164
165
  return [3 /*break*/, 4];
165
166
  case 2:
167
+ if (!(message.type === 'set')) return [3 /*break*/, 4];
166
168
  return [4 /*yield*/, this.setState(message.payload)];
167
169
  case 3:
168
170
  payload = _a.sent();
@@ -214,6 +216,7 @@ var Manager = /** @class */function () {
214
216
  });
215
217
  };
216
218
  Manager.prototype.restore = function () {
219
+ var _a;
217
220
  try {
218
221
  var buf = readFileSync(this.path);
219
222
  this._state = JSON.parse(buf.toString());
@@ -224,7 +227,7 @@ var Manager = /** @class */function () {
224
227
  recursive: true
225
228
  });
226
229
  }
227
- this._state = {};
230
+ this.setState((_a = this.defaultState) !== null && _a !== void 0 ? _a : {});
228
231
  }
229
232
  };
230
233
  Manager.prototype.getState = function () {
@@ -251,6 +254,7 @@ var Manager = /** @class */function () {
251
254
 
252
255
  return Manager;
253
256
  }();
257
+ // work 线程的实现
254
258
  var Worker = /** @class */function () {
255
259
  function Worker(path) {
256
260
  this.callback = {};
@@ -312,13 +316,8 @@ var Worker = /** @class */function () {
312
316
  }();
313
317
  var JsonStorage = /** @class */function () {
314
318
  function JsonStorage(path, options) {
315
- if (options === void 0) {
316
- options = {
317
- initialState: null
318
- };
319
- }
320
319
  if (cluster.isPrimary) {
321
- this.instance = new Manager(path);
320
+ this.instance = new Manager(path, options.defaultState);
322
321
  } else {
323
322
  this.instance = new Worker(path);
324
323
  }
@@ -1,3 +1,7 @@
1
1
  export declare function getProcessName(pid: any): Promise<string>;
2
+ export declare function getProcessCpuUseByPid(pid: number): Promise<number>;
3
+ export declare function getProcessCommandByPid(pid: number): Promise<string>;
2
4
  export declare function isProcessDied(pid: any): Promise<boolean>;
5
+ export declare function getProcessByPpid(pid: number): Promise<number[]>;
6
+ export declare const getProcessTree: (pid: number, tree?: any) => Promise<any>;
3
7
  export declare function SIGKILL(pid: any, type?: number): Promise<void>;