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