pubo-node 1.0.156 → 1.0.157

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,260 +1,135 @@
1
- var _excluded = ["driver"],
2
- _excluded2 = ["maxConnection"];
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
- function _invoke(body, then) {
13
- var result = body();
14
- if (result && result.then) {
15
- return result.then(then);
16
- }
17
- return then(result);
18
- }
19
- function _empty() {}
20
- function _awaitIgnored(value, direct) {
21
- if (!direct) {
22
- return value && value.then ? value.then(_empty) : Promise.resolve();
23
- }
24
- }
25
- function _async(f) {
26
- return function () {
27
- for (var args = [], i = 0; i < arguments.length; i++) {
28
- args[i] = arguments[i];
1
+ import { random, waitFor } from 'pubo-utils';
2
+ export class FtpClient {
3
+ driver;
4
+ options;
5
+ state = { running: false, connected: false, destroyed: false, connecting: false };
6
+ client;
7
+ _len = 0;
8
+ id = random();
9
+ constructor({ driver, ...options }) {
10
+ this.driver = driver;
11
+ this.options = options;
29
12
  }
30
- try {
31
- return Promise.resolve(f.apply(this, args));
32
- } catch (e) {
33
- return Promise.reject(e);
13
+ get len() {
14
+ return this._len;
34
15
  }
35
- };
36
- }
37
- 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); }
38
- function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
39
- function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
40
- function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
41
- function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
42
- function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
43
- import { random, waitFor } from 'pubo-utils';
44
- export var FtpClient = /*#__PURE__*/function () {
45
- function FtpClient(_ref) {
46
- var driver = _ref.driver,
47
- options = _objectWithoutPropertiesLoose(_ref, _excluded);
48
- this.driver = void 0;
49
- this.options = void 0;
50
- this.state = {
51
- running: false,
52
- connected: false,
53
- destroyed: false,
54
- connecting: false
55
- };
56
- this.client = void 0;
57
- this._len = 0;
58
- this.id = random();
59
- this.put = this.bind('put');
60
- this["delete"] = this.bind('delete');
61
- this.list = this.bind('list');
62
- this.rename = this.bind('rename');
63
- this.driver = driver;
64
- this.options = options;
65
- }
66
- var _proto = FtpClient.prototype;
67
- _proto.connect = function connect() {
68
- try {
69
- var _exit = false;
70
- var _this = this;
71
- if (!_this.client) {
72
- _this.client = new _this.driver();
73
- }
74
- _this.state.destroyed = false;
75
- return _await(_invoke(function () {
76
- if (_this.state.connecting) {
77
- return _await(waitFor(function () {
78
- return _this.state.connected;
79
- }, {
80
- checkTime: 1000,
81
- timeout: 10000
82
- }), function () {
83
- _exit = true;
16
+ set len(n) {
17
+ this._len = n;
18
+ if (this._len < 1) {
19
+ this.close();
20
+ }
21
+ }
22
+ put = this.bind('put');
23
+ delete = this.bind('delete');
24
+ list = this.bind('list');
25
+ rename = this.bind('rename');
26
+ async connect() {
27
+ if (!this.client) {
28
+ this.client = new this.driver();
29
+ }
30
+ this.state.destroyed = false;
31
+ if (this.state.connecting) {
32
+ await waitFor(() => this.state.connected, { checkTime: 1000, timeout: 10000 });
84
33
  return 'connected';
85
- });
86
34
  }
87
- }, function (_result) {
88
- if (_exit) return _result;
89
- _this.state.connecting = true;
90
- return new Promise(function (resolve, reject) {
91
- _this.client.once('ready', function () {
92
- _this.state.connected = true;
93
- resolve('connected');
94
- _this.state.connecting = false;
95
- });
96
- _this.client.once('error', function (err) {
97
- reject(err);
98
- _this.close();
99
- });
100
- _this.client.connect(_extends({}, _this.options));
35
+ this.state.connecting = true;
36
+ return new Promise((resolve, reject) => {
37
+ this.client.once('ready', () => {
38
+ this.state.connected = true;
39
+ resolve('connected');
40
+ this.state.connecting = false;
41
+ });
42
+ this.client.once('error', (err) => {
43
+ reject(err);
44
+ this.close();
45
+ });
46
+ this.client.connect({ ...this.options });
101
47
  });
102
- }));
103
- } catch (e) {
104
- return Promise.reject(e);
105
48
  }
106
- };
107
- _proto.close = function close() {
108
- this.client.end();
109
- this.state.connected = false;
110
- this.state.destroyed = true;
111
- this.state.connecting = false;
112
- this.client = null;
113
- };
114
- _proto.run = function run(_ref2) {
115
- var fn = _ref2.fn,
116
- args = _ref2.args;
117
- try {
118
- var _this2 = this;
119
- _this2.len += 1;
120
- return _await(_invoke(function () {
121
- if (!_this2.state.connected) {
122
- return _awaitIgnored(_this2.connect());
49
+ close() {
50
+ this.client.end();
51
+ this.state.connected = false;
52
+ this.state.destroyed = true;
53
+ this.state.connecting = false;
54
+ this.client = null;
55
+ }
56
+ async run({ fn, args }) {
57
+ this.len += 1;
58
+ if (!this.state.connected) {
59
+ await this.connect();
123
60
  }
124
- }, function () {
125
- return _invoke(function () {
126
- if (_this2.state.running) {
127
- return _awaitIgnored(waitFor(function () {
128
- return !_this2.state.running;
129
- }, {
130
- checkTime: 1000,
131
- timeout: 6000000
132
- }));
133
- }
134
- }, function () {
135
- _this2.state.running = true;
136
- return new Promise(function (resolve, reject) {
137
- var _this2$client;
138
- (_this2$client = _this2.client)[fn].apply(_this2$client, args.concat([function (err, res) {
139
- if (err) {
140
- reject(err);
141
- } else {
142
- resolve(res);
143
- }
144
- }]));
145
- });
61
+ if (this.state.running) {
62
+ await waitFor(() => !this.state.running, { checkTime: 1000, timeout: 6000000 });
63
+ }
64
+ this.state.running = true;
65
+ return new Promise((resolve, reject) => {
66
+ this.client[fn](...args, (err, res) => {
67
+ if (err) {
68
+ reject(err);
69
+ }
70
+ else {
71
+ resolve(res);
72
+ }
73
+ });
146
74
  });
147
- }));
148
- } catch (e) {
149
- return Promise.reject(e);
150
75
  }
151
- };
152
- _proto.bind = function bind(fn) {
153
- var _this3 = this;
154
- return _async(function () {
155
- for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
156
- args[_key] = arguments[_key];
157
- }
158
- return _await(_this3.run({
159
- fn: fn,
160
- args: args
161
- }), function (res) {
162
- _this3.state.running = false;
163
- _this3.len -= 1;
164
- return res;
165
- });
166
- });
167
- };
168
- _proto.get = function get() {
169
- try {
170
- var _this4 = this;
171
- var res = Buffer.alloc(0);
172
- for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
173
- args[_key2] = arguments[_key2];
174
- }
175
- return _await(_this4.run({
176
- fn: 'get',
177
- args: args
178
- }), function (stream) {
179
- return new Promise(function (resolve) {
180
- stream.on('data', function (chunk) {
181
- res = Buffer.concat([res, chunk], res.byteLength + chunk.byteLength);
182
- });
183
- stream.on('end', function () {
184
- resolve(res);
185
- _this4.state.running = false;
186
- _this4.len -= 1;
187
- });
76
+ bind(fn) {
77
+ return async (...args) => {
78
+ const res = await this.run({ fn, args });
79
+ this.state.running = false;
80
+ this.len -= 1;
81
+ return res;
82
+ };
83
+ }
84
+ async get(...args) {
85
+ let res = Buffer.alloc(0);
86
+ const stream = await this.run({ fn: 'get', args });
87
+ return new Promise((resolve) => {
88
+ stream.on('data', (chunk) => {
89
+ res = Buffer.concat([res, chunk], res.byteLength + chunk.byteLength);
90
+ });
91
+ stream.on('end', () => {
92
+ resolve(res);
93
+ this.state.running = false;
94
+ this.len -= 1;
95
+ });
188
96
  });
189
- });
190
- } catch (e) {
191
- return Promise.reject(e);
192
97
  }
193
- };
194
- _createClass(FtpClient, [{
195
- key: "len",
196
- get: function get() {
197
- return this._len;
198
- },
199
- set: function set(n) {
200
- this._len = n;
201
- if (this._len < 1) {
202
- this.close();
203
- }
98
+ }
99
+ export class FtpClientPool {
100
+ options;
101
+ maxConnection;
102
+ pool = [];
103
+ constructor({ maxConnection = 5, ...options }) {
104
+ this.options = options;
105
+ this.maxConnection = maxConnection;
106
+ }
107
+ get = this.bind('get');
108
+ put = this.bind('put');
109
+ delete = this.bind('delete');
110
+ list = this.bind('list');
111
+ rename = this.bind('rename');
112
+ get len() {
113
+ return this.pool.length;
204
114
  }
205
- }]);
206
- return FtpClient;
207
- }();
208
- export var FtpClientPool = /*#__PURE__*/function () {
209
- function FtpClientPool(_ref3) {
210
- var _ref3$maxConnection = _ref3.maxConnection,
211
- maxConnection = _ref3$maxConnection === void 0 ? 5 : _ref3$maxConnection,
212
- options = _objectWithoutPropertiesLoose(_ref3, _excluded2);
213
- this.options = void 0;
214
- this.maxConnection = void 0;
215
- this.pool = [];
216
- this.get = this.bind('get');
217
- this.put = this.bind('put');
218
- this["delete"] = this.bind('delete');
219
- this.list = this.bind('list');
220
- this.rename = this.bind('rename');
221
- this.options = options;
222
- this.maxConnection = maxConnection;
223
- }
224
- var _proto2 = FtpClientPool.prototype;
225
- _proto2.bind = function bind(fn) {
226
- var _this5 = this;
227
- return _async(function () {
228
- var client = _this5.client;
229
- return _await(client[fn].apply(client, arguments), function (res) {
230
- if (client.len < 1) {
231
- var index = _this5.pool.findIndex(function (item) {
232
- return item.id === client.id;
233
- });
234
- _this5.pool.splice(index, 1);
115
+ get client() {
116
+ if (this.pool.length < this.maxConnection) {
117
+ const client = new FtpClient(this.options);
118
+ this.pool.push(client);
119
+ return client;
235
120
  }
236
- return res;
237
- });
238
- });
239
- };
240
- _createClass(FtpClientPool, [{
241
- key: "len",
242
- get: function get() {
243
- return this.pool.length;
121
+ this.pool.sort((a, b) => a.len - b.len);
122
+ return this.pool[0];
244
123
  }
245
- }, {
246
- key: "client",
247
- get: function get() {
248
- if (this.pool.length < this.maxConnection) {
249
- var client = new FtpClient(this.options);
250
- this.pool.push(client);
251
- return client;
252
- }
253
- this.pool.sort(function (a, b) {
254
- return a.len - b.len;
255
- });
256
- return this.pool[0];
124
+ bind(fn) {
125
+ return async (...args) => {
126
+ const client = this.client;
127
+ const res = await client[fn](...args);
128
+ if (client.len < 1) {
129
+ const index = this.pool.findIndex((item) => item.id === client.id);
130
+ this.pool.splice(index, 1);
131
+ }
132
+ return res;
133
+ };
257
134
  }
258
- }]);
259
- return FtpClientPool;
260
- }();
135
+ }
package/es/grpc/index.js CHANGED
@@ -1,169 +1,103 @@
1
- function _await(value, then, direct) {
2
- if (direct) {
3
- return then ? then(value) : value;
4
- }
5
- if (!value || !value.then) {
6
- value = Promise.resolve(value);
7
- }
8
- return then ? value.then(then) : value;
9
- }
10
- function _catch(body, recover) {
11
- try {
12
- var result = body();
13
- } catch (e) {
14
- return recover(e);
15
- }
16
- if (result && result.then) {
17
- return result.then(void 0, recover);
18
- }
19
- return result;
20
- }
21
- function _continue(value, then) {
22
- return value && value.then ? value.then(then) : then(value);
23
- }
24
- 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); }
25
- var passThrough = function passThrough(argument) {
26
- return argument;
27
- };
28
- var GrpcClient = /*#__PURE__*/function () {
29
- function GrpcClient(_ref) {
30
- var _this$options$timeout;
31
- var url = _ref.url,
32
- _ref$options = _ref.options,
33
- options = _ref$options === void 0 ? {} : _ref$options,
34
- Grpc = _ref.Grpc,
35
- cert = _ref.cert;
36
- this.url = void 0;
37
- this.options = void 0;
38
- this.Grpc = void 0;
39
- this.credentials = void 0;
40
- this.client = void 0;
41
- this._timeout = void 0;
42
- this.connections = 0;
43
- var opt = _extends({
44
- 'grpc.max_send_message_length': -1,
45
- 'grpc.max_receive_message_length': -1
46
- }, options);
47
- var credentials = cert ? Grpc.credentials.createSsl(cert) : Grpc.credentials.createInsecure();
48
- this.url = url;
49
- this.Grpc = Grpc;
50
- this.credentials = credentials;
51
- this.options = opt;
52
- this.options.timeout = (_this$options$timeout = this.options.timeout) != null ? _this$options$timeout : 10000;
53
- Grpc = null;
54
- options = null;
55
- cert = null;
56
- }
57
- var _proto = GrpcClient.prototype;
58
- _proto.request = function request(service, method, data) {
59
- try {
60
- var _this = this;
61
- if (_this._timeout) {
62
- clearTimeout(_this._timeout);
63
- _this._timeout = null;
64
- }
65
- _this.connections += 1;
66
- if (!_this.client) {
67
- _this.client = new _this.Grpc.Client(_this.url, _this.credentials, _this.options);
68
- }
69
- var error;
70
- var result = Buffer.alloc(0);
71
- return _await(_continue(_catch(function () {
72
- return _await(_this._request({
73
- service: service,
74
- method: method,
75
- data: data
76
- }), function (_this$_request) {
77
- result = _this$_request;
78
- });
79
- }, function (err) {
80
- error = err;
81
- }), function () {
1
+ const passThrough = (argument) => argument;
2
+ class GrpcClient {
3
+ url;
4
+ options;
5
+ Grpc;
6
+ credentials;
7
+ client;
8
+ _timeout;
9
+ connections = 0;
10
+ constructor({ url, options = {}, Grpc, cert }) {
11
+ const opt = { 'grpc.max_send_message_length': -1, 'grpc.max_receive_message_length': -1, ...options };
12
+ const credentials = cert ? Grpc.credentials.createSsl(cert) : Grpc.credentials.createInsecure();
13
+ this.url = url;
14
+ this.Grpc = Grpc;
15
+ this.credentials = credentials;
16
+ this.options = opt;
17
+ this.options.timeout = this.options.timeout ?? 10000;
18
+ Grpc = null;
19
+ options = null;
20
+ cert = null;
21
+ }
22
+ async request(service, method, data) {
23
+ if (this._timeout) {
24
+ clearTimeout(this._timeout);
25
+ this._timeout = null;
26
+ }
27
+ this.connections += 1;
28
+ if (!this.client) {
29
+ this.client = new this.Grpc.Client(this.url, this.credentials, this.options);
30
+ }
31
+ let error;
32
+ let result = Buffer.alloc(0);
33
+ try {
34
+ result = await this._request({ service, method, data });
35
+ }
36
+ catch (err) {
37
+ error = err;
38
+ }
82
39
  service = null;
83
40
  method = null;
84
41
  data = null;
85
- _this.connections -= 1;
86
- if (_this.connections < 0) {
87
- _this.connections = 0;
42
+ this.connections -= 1;
43
+ if (this.connections < 0) {
44
+ this.connections = 0;
88
45
  }
89
- if (_this.connections < 1) {
90
- if (_this._timeout) {
91
- clearTimeout(_this._timeout);
92
- _this._timeout = null;
93
- }
94
- _this._timeout = setTimeout(function () {
95
- return _this.close();
96
- }, 60000);
46
+ if (this.connections < 1) {
47
+ if (this._timeout) {
48
+ clearTimeout(this._timeout);
49
+ this._timeout = null;
50
+ }
51
+ this._timeout = setTimeout(() => this.close(), 60000);
97
52
  }
98
53
  if (error) {
99
- console.log(error);
100
- _this.close();
101
- throw new Error('grpc connection error.');
54
+ console.log(error);
55
+ this.close();
56
+ throw new Error('grpc connection error.');
102
57
  }
103
58
  return result;
104
- }));
105
- } catch (e) {
106
- return Promise.reject(e);
107
59
  }
108
- };
109
- _proto._request = function _request(_ref2) {
110
- var _this2 = this;
111
- var service = _ref2.service,
112
- method = _ref2.method,
113
- data = _ref2.data;
114
- return new Promise(function (resolve, reject) {
115
- var _ended = false;
116
- var _timeout = setTimeout(function () {
117
- _ended = true;
118
- _this2.close();
119
- console.log('rpc request timeout');
120
- reject(new Error('timeout'));
121
- }, _this2.options.timeout);
122
- var onResponse = function onResponse(err, res) {
123
- if (_ended) {
124
- return;
125
- } else {
126
- clearTimeout(_timeout);
127
- }
128
- if (err) {
129
- reject(err);
130
- } else {
131
- resolve(res);
60
+ _request({ service, method, data }) {
61
+ return new Promise((resolve, reject) => {
62
+ let _ended = false;
63
+ const _timeout = setTimeout(() => {
64
+ _ended = true;
65
+ this.close();
66
+ console.log('rpc request timeout');
67
+ reject(new Error('timeout'));
68
+ }, this.options.timeout);
69
+ const onResponse = (err, res) => {
70
+ if (_ended) {
71
+ return;
72
+ }
73
+ else {
74
+ clearTimeout(_timeout);
75
+ }
76
+ if (err) {
77
+ reject(err);
78
+ }
79
+ else {
80
+ resolve(res);
81
+ }
82
+ };
83
+ this.client.makeUnaryRequest(`/${service}/${method}`, passThrough, passThrough, data ? Buffer.from(data) : Buffer.alloc(0), onResponse);
84
+ });
85
+ }
86
+ close() {
87
+ if (this._timeout) {
88
+ clearTimeout(this._timeout);
89
+ delete this._timeout;
132
90
  }
133
- };
134
- _this2.client.makeUnaryRequest("/" + service + "/" + method, passThrough, passThrough, data ? Buffer.from(data) : Buffer.alloc(0), onResponse);
135
- });
136
- };
137
- _proto.close = function close() {
138
- var _this$client;
139
- if (this._timeout) {
140
- clearTimeout(this._timeout);
141
- delete this._timeout;
91
+ this.client?.close();
92
+ this.client = null;
93
+ delete this.client;
142
94
  }
143
- (_this$client = this.client) == null ? void 0 : _this$client.close();
144
- this.client = null;
145
- delete this.client;
146
- };
147
- return GrpcClient;
148
- }();
149
- export var GrpcList = [];
150
- export function createRpcClient(_ref3) {
151
- var url = _ref3.url,
152
- _ref3$options = _ref3.options,
153
- options = _ref3$options === void 0 ? {} : _ref3$options,
154
- ServiceImp = _ref3.ServiceImp,
155
- Grpc = _ref3.Grpc,
156
- cert = _ref3.cert;
157
- var client = new GrpcClient({
158
- url: url,
159
- options: options,
160
- Grpc: Grpc,
161
- cert: cert
162
- });
163
- GrpcList.push(client);
164
- Grpc = null;
165
- options = null;
166
- return new ServiceImp({
167
- request: client.request.bind(client)
168
- });
169
- }
95
+ }
96
+ export const GrpcList = [];
97
+ export function createRpcClient({ url, options = {}, ServiceImp, Grpc, cert }) {
98
+ const client = new GrpcClient({ url, options, Grpc, cert });
99
+ GrpcList.push(client);
100
+ Grpc = null;
101
+ options = null;
102
+ return new ServiceImp({ request: client.request.bind(client) });
103
+ }
package/es/index.d.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  export { JsonStorage } from './storage/json';
2
2
  export { FtpClient, FtpClientPool, FtpConnectOptions, FtpFile } from './ftp-client';
3
3
  export { createRpcClient, GrpcList } from './grpc';
4
- export { PuboFileSystem } from './file-system';
5
4
  export { SIGKILL, isProcessDied, getProcessName, getProcessTree, getProcessByPpid, getProcessCpuUseByPid, getProcessCommandByPid, heartbeat, } from './child-process';
6
5
  export { isPortAvailable } from './utils';
7
6
  export { getWifiName, getNetworks } from './utils/network';
8
7
  export { RosTopicManager, RosTopic } from './ros/topic';
8
+ export { PuboFileSystem } from './file-system';