pubo-node 1.0.140 → 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.
@@ -14,13 +14,15 @@ declare class GrpcClient {
14
14
  private client;
15
15
  private _timeout;
16
16
  connections: number;
17
+ private closing;
17
18
  constructor({ url, options, Grpc, cert }: any);
18
- request(service: any, method: any, data: any): Promise<any>;
19
+ request(service: any, method: any, data: any): Promise<Buffer>;
19
20
  _request({ service, method, data }: {
20
21
  service: any;
21
22
  method: any;
22
23
  data: any;
23
- }): Promise<unknown>;
24
+ }): Promise<Buffer>;
25
+ restart(): Promise<void>;
24
26
  close(): void;
25
27
  }
26
28
  export declare const GrpcList: GrpcClient[];
package/lib/grpc/index.js CHANGED
@@ -1,147 +1,79 @@
1
1
  "use strict";
2
2
 
3
- var __assign = this && this.__assign || function () {
4
- __assign = Object.assign || function (t) {
5
- for (var s, i = 1, n = arguments.length; i < n; i++) {
6
- s = arguments[i];
7
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
- var __awaiter = this && this.__awaiter || function (thisArg, _arguments, P, generator) {
14
- function adopt(value) {
15
- return value instanceof P ? value : new P(function (resolve) {
16
- resolve(value);
17
- });
3
+ function _empty() {}
4
+ function _awaitIgnored(value, direct) {
5
+ if (!direct) {
6
+ return value && value.then ? value.then(_empty) : Promise.resolve();
18
7
  }
19
- return new (P || (P = Promise))(function (resolve, reject) {
20
- function fulfilled(value) {
21
- try {
22
- step(generator.next(value));
23
- } catch (e) {
24
- reject(e);
25
- }
26
- }
27
- function rejected(value) {
28
- try {
29
- step(generator["throw"](value));
30
- } catch (e) {
31
- reject(e);
32
- }
33
- }
34
- function step(result) {
35
- result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
36
- }
37
- step((generator = generator.apply(thisArg, _arguments || [])).next());
38
- });
39
- };
40
- var __generator = this && this.__generator || function (thisArg, body) {
41
- var _ = {
42
- label: 0,
43
- sent: function sent() {
44
- if (t[0] & 1) throw t[1];
45
- return t[1];
46
- },
47
- trys: [],
48
- ops: []
49
- },
50
- f,
51
- y,
52
- t,
53
- g;
54
- return g = {
55
- next: verb(0),
56
- "throw": verb(1),
57
- "return": verb(2)
58
- }, typeof Symbol === "function" && (g[Symbol.iterator] = function () {
59
- return this;
60
- }), g;
61
- function verb(n) {
62
- return function (v) {
63
- return step([n, v]);
64
- };
8
+ }
9
+ function _await(value, then, direct) {
10
+ if (direct) {
11
+ return then ? then(value) : value;
65
12
  }
66
- function step(op) {
67
- if (f) throw new TypeError("Generator is already executing.");
68
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
69
- 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;
70
- if (y = 0, t) op = [op[0] & 2, t.value];
71
- switch (op[0]) {
72
- case 0:
73
- case 1:
74
- t = op;
75
- break;
76
- case 4:
77
- _.label++;
78
- return {
79
- value: op[1],
80
- done: false
81
- };
82
- case 5:
83
- _.label++;
84
- y = op[1];
85
- op = [0];
86
- continue;
87
- case 7:
88
- op = _.ops.pop();
89
- _.trys.pop();
90
- continue;
91
- default:
92
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
93
- _ = 0;
94
- continue;
95
- }
96
- if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
97
- _.label = op[1];
98
- break;
99
- }
100
- if (op[0] === 6 && _.label < t[1]) {
101
- _.label = t[1];
102
- t = op;
103
- break;
104
- }
105
- if (t && _.label < t[2]) {
106
- _.label = t[2];
107
- _.ops.push(op);
108
- break;
109
- }
110
- if (t[2]) _.ops.pop();
111
- _.trys.pop();
112
- continue;
113
- }
114
- op = body.call(thisArg, _);
13
+ if (!value || !value.then) {
14
+ value = Promise.resolve(value);
15
+ }
16
+ return then ? value.then(then) : value;
17
+ }
18
+ function _catch(body, recover) {
19
+ try {
20
+ var result = body();
21
+ } catch (e) {
22
+ return recover(e);
23
+ }
24
+ if (result && result.then) {
25
+ return result.then(void 0, recover);
26
+ }
27
+ return result;
28
+ }
29
+ function _continue(value, then) {
30
+ return value && value.then ? value.then(then) : then(value);
31
+ }
32
+ function _invoke(body, then) {
33
+ var result = body();
34
+ if (result && result.then) {
35
+ return result.then(then);
36
+ }
37
+ return then(result);
38
+ }
39
+ function _async(f) {
40
+ return function () {
41
+ for (var args = [], i = 0; i < arguments.length; i++) {
42
+ args[i] = arguments[i];
43
+ }
44
+ try {
45
+ return Promise.resolve(f.apply(this, args));
115
46
  } catch (e) {
116
- op = [6, e];
117
- y = 0;
118
- } finally {
119
- f = t = 0;
47
+ return Promise.reject(e);
120
48
  }
121
- if (op[0] & 5) throw op[1];
122
- return {
123
- value: op[0] ? op[1] : void 0,
124
- done: true
125
- };
126
- }
127
- };
49
+ };
50
+ }
51
+ function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
128
52
  Object.defineProperty(exports, "__esModule", {
129
53
  value: true
130
54
  });
131
55
  exports.createRpcClient = exports.GrpcList = void 0;
56
+ var pubo_utils_1 = require("pubo-utils");
132
57
  function passThrough(argument) {
133
58
  return argument;
134
59
  }
135
- var GrpcClient = /** @class */function () {
136
- function GrpcClient(_a) {
137
- var url = _a.url,
138
- _b = _a.options,
139
- options = _b === void 0 ? {} : _b,
140
- Grpc = _a.Grpc,
141
- cert = _a.cert;
142
- var _c;
60
+ var GrpcClient = /*#__PURE__*/function () {
61
+ function GrpcClient(_ref) {
62
+ var _this$options$timeout;
63
+ var url = _ref.url,
64
+ _ref$options = _ref.options,
65
+ options = _ref$options === void 0 ? {} : _ref$options,
66
+ Grpc = _ref.Grpc,
67
+ cert = _ref.cert;
68
+ this.url = void 0;
69
+ this.options = void 0;
70
+ this.Grpc = void 0;
71
+ this.credentials = void 0;
72
+ this.client = void 0;
73
+ this._timeout = void 0;
143
74
  this.connections = 0;
144
- var opt = __assign({
75
+ this.closing = false;
76
+ var opt = _extends({
145
77
  'grpc.max_send_message_length': -1,
146
78
  'grpc.max_receive_message_length': -1
147
79
  }, options);
@@ -150,70 +82,76 @@ var GrpcClient = /** @class */function () {
150
82
  this.Grpc = Grpc;
151
83
  this.credentials = credentials;
152
84
  this.options = opt;
153
- this.options.timeout = (_c = this.options.timeout) !== null && _c !== void 0 ? _c : 60000;
85
+ this.options.timeout = (_this$options$timeout = this.options.timeout) != null ? _this$options$timeout : 60000;
154
86
  }
155
- GrpcClient.prototype.request = function (service, method, data) {
156
- return __awaiter(this, void 0, void 0, function () {
157
- var error, result, err_1;
87
+ var _proto = GrpcClient.prototype;
88
+ _proto.request = function request(service, method, data) {
89
+ try {
158
90
  var _this = this;
159
- return __generator(this, function (_a) {
160
- switch (_a.label) {
161
- case 0:
162
- this.connections += 1;
163
- if (!this.client) {
164
- this.client = new this.Grpc.Client(this.url, this.credentials, this.options);
165
- }
166
- _a.label = 1;
167
- case 1:
168
- _a.trys.push([1, 3,, 4]);
169
- return [4 /*yield*/, this._request({
170
- service: service,
171
- method: method,
172
- data: data
173
- })];
174
- case 2:
175
- result = _a.sent();
176
- return [3 /*break*/, 4];
177
- case 3:
178
- err_1 = _a.sent();
179
- error = err_1;
180
- return [3 /*break*/, 4];
181
- case 4:
182
- this.connections -= 1;
183
- if (this.connections < 0) {
184
- this.connections = 0;
185
- }
186
- if (this.connections < 1) {
187
- if (this._timeout) {
188
- clearTimeout(this._timeout);
189
- this._timeout = null;
190
- }
191
- this._timeout = setTimeout(function () {
192
- return _this.close();
193
- }, 60000);
194
- }
195
- if (error) {
196
- this.close();
197
- throw new Error('grpc connection error.');
198
- }
199
- return [2 /*return*/, result];
91
+ return _await(_invoke(function () {
92
+ if (_this.closing) {
93
+ return _awaitIgnored((0, pubo_utils_1.waitFor)(_async(function () {
94
+ return !_this.closing;
95
+ }), {
96
+ checkTime: 100,
97
+ timeout: 20000
98
+ }));
200
99
  }
201
- });
202
- });
100
+ }, function () {
101
+ _this.connections += 1;
102
+ if (!_this.client) {
103
+ _this.client = new _this.Grpc.Client(_this.url, _this.credentials, _this.options);
104
+ }
105
+ var error;
106
+ var result = Buffer.alloc(0);
107
+ return _continue(_catch(function () {
108
+ return _await(_this._request({
109
+ service: service,
110
+ method: method,
111
+ data: data
112
+ }), function (_this$_request) {
113
+ result = _this$_request;
114
+ });
115
+ }, function (err) {
116
+ error = err;
117
+ }), function () {
118
+ _this.connections -= 1;
119
+ if (_this.connections < 0) {
120
+ _this.connections = 0;
121
+ }
122
+ if (_this.connections < 1) {
123
+ if (_this._timeout) {
124
+ clearTimeout(_this._timeout);
125
+ _this._timeout = null;
126
+ }
127
+ _this._timeout = setTimeout(function () {
128
+ return _this.close();
129
+ }, 60000);
130
+ }
131
+ if (error) {
132
+ _this.restart();
133
+ throw new Error('grpc connection error.');
134
+ }
135
+ return result;
136
+ });
137
+ }));
138
+ } catch (e) {
139
+ return Promise.reject(e);
140
+ }
203
141
  };
204
- GrpcClient.prototype._request = function (_a) {
205
- var _this = this;
206
- var service = _a.service,
207
- method = _a.method,
208
- data = _a.data;
142
+ _proto._request = function _request(_ref2) {
143
+ var _this2 = this;
144
+ var service = _ref2.service,
145
+ method = _ref2.method,
146
+ data = _ref2.data;
209
147
  return new Promise(function (resolve, reject) {
210
148
  var _ended = false;
211
149
  var _timeout = setTimeout(function () {
212
150
  _ended = true;
213
- _this.close();
151
+ _this2.restart();
214
152
  console.log('rpc request timeout');
215
153
  reject(new Error('timeout'));
216
- }, _this.options.timeout);
154
+ }, _this2.options.timeout);
217
155
  var _onResponse = function onResponse(err, res) {
218
156
  if (_ended) {
219
157
  return;
@@ -227,25 +165,41 @@ var GrpcClient = /** @class */function () {
227
165
  }
228
166
  _onResponse = null;
229
167
  };
230
- _this.client.makeUnaryRequest("/".concat(service, "/").concat(method), passThrough, passThrough, data, _onResponse);
168
+ _this2.client.makeUnaryRequest("/" + service + "/" + method, passThrough, passThrough, data, _onResponse);
231
169
  });
232
170
  };
233
- GrpcClient.prototype.close = function () {
234
- var _a;
235
- (_a = this.client) === null || _a === void 0 ? void 0 : _a.close();
171
+ _proto.restart = function restart() {
172
+ try {
173
+ var _this3 = this;
174
+ if (_this3.closing) {
175
+ return _await();
176
+ }
177
+ console.log('rpc client restarting.');
178
+ _this3.closing = true;
179
+ _this3.close();
180
+ return _await((0, pubo_utils_1.sleep)(2000), function () {
181
+ _this3.closing = false;
182
+ });
183
+ } catch (e) {
184
+ return Promise.reject(e);
185
+ }
186
+ };
187
+ _proto.close = function close() {
188
+ var _this$client;
189
+ (_this$client = this.client) == null ? void 0 : _this$client.close();
236
190
  this.client = null;
237
191
  delete this.client;
238
192
  };
239
193
  return GrpcClient;
240
194
  }();
241
195
  exports.GrpcList = [];
242
- function createRpcClient(_a) {
243
- var url = _a.url,
244
- _b = _a.options,
245
- options = _b === void 0 ? {} : _b,
246
- ServiceImp = _a.ServiceImp,
247
- Grpc = _a.Grpc,
248
- cert = _a.cert;
196
+ function createRpcClient(_ref3) {
197
+ var url = _ref3.url,
198
+ _ref3$options = _ref3.options,
199
+ options = _ref3$options === void 0 ? {} : _ref3$options,
200
+ ServiceImp = _ref3.ServiceImp,
201
+ Grpc = _ref3.Grpc,
202
+ cert = _ref3.cert;
249
203
  var client = new GrpcClient({
250
204
  url: url,
251
205
  options: options,
package/lib/ros/topic.js CHANGED
@@ -39,8 +39,10 @@ var child_process_1 = require("child_process");
39
39
  var pubo_utils_1 = require("pubo-utils");
40
40
  var YAML = __importStar(require("yaml"));
41
41
  var child_process_2 = require("../child-process");
42
- var RosTopic = /** @class */function () {
42
+ var RosTopic = /*#__PURE__*/function () {
43
43
  function RosTopic(topic, messageType) {
44
+ this.topic = void 0;
45
+ this.messageType = void 0;
44
46
  this.emitter = new pubo_utils_1.Emitter();
45
47
  this.subscribed = false;
46
48
  this.dog = new pubo_utils_1.WatchDog({
@@ -48,19 +50,21 @@ var RosTopic = /** @class */function () {
48
50
  onTimeout: this.onTimeout.bind(this)
49
51
  });
50
52
  this.strSplit = new pubo_utils_1.StringSplit('---');
53
+ this.subscribeChildProcess = void 0;
51
54
  this.topic = topic;
52
55
  this.messageType = messageType;
53
56
  this.subscribed = false;
54
57
  this.emitter = new pubo_utils_1.Emitter();
55
58
  }
56
- RosTopic.prototype.onTimeout = function () {
59
+ var _proto = RosTopic.prototype;
60
+ _proto.onTimeout = function onTimeout() {
57
61
  if (!this.subscribed) {
58
62
  return;
59
63
  }
60
64
  this.unsubscribe();
61
65
  this.subscribe();
62
66
  };
63
- RosTopic.prototype.onData = function (data) {
67
+ _proto.onData = function onData(data) {
64
68
  var tmp = this.strSplit.split(data.toString()).slice(-1)[0];
65
69
  if (!tmp) {
66
70
  return;
@@ -68,7 +72,7 @@ var RosTopic = /** @class */function () {
68
72
  var res = YAML.parse(tmp);
69
73
  this.emitter.emit('message', res);
70
74
  };
71
- RosTopic.prototype.subscribe = function () {
75
+ _proto.subscribe = function subscribe() {
72
76
  if (this.subscribeChildProcess) {
73
77
  return;
74
78
  }
@@ -77,7 +81,7 @@ var RosTopic = /** @class */function () {
77
81
  this.subscribeChildProcess.stdout.on('data', this.onData.bind(this));
78
82
  this.dog.init();
79
83
  };
80
- RosTopic.prototype.unsubscribe = function () {
84
+ _proto.unsubscribe = function unsubscribe() {
81
85
  if (this.subscribeChildProcess) {
82
86
  return;
83
87
  }
@@ -86,11 +90,11 @@ var RosTopic = /** @class */function () {
86
90
  (0, child_process_2.SIGKILL)(this.subscribeChildProcess.pid);
87
91
  this.subscribeChildProcess = null;
88
92
  };
89
- RosTopic.prototype.publish = function (payload) {
93
+ _proto.publish = function publish(payload) {
90
94
  var _this = this;
91
95
  var data = YAML.stringify(payload);
92
96
  return new Promise(function (resolve, reject) {
93
- (0, child_process_1.exec)("rostopic pub -1 ".concat(_this.topic, " ").concat(_this.messageType, " \"").concat(data, "\""), function (err, stdout) {
97
+ (0, child_process_1.exec)("rostopic pub -1 " + _this.topic + " " + _this.messageType + " \"" + data + "\"", function (err, stdout) {
94
98
  if (err) {
95
99
  reject(err);
96
100
  } else {