pubo-node 1.0.155 → 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.
package/es/index.js CHANGED
@@ -1,8 +1,8 @@
1
1
  export { JsonStorage } from './storage/json';
2
2
  export { FtpClient, FtpClientPool } from './ftp-client';
3
3
  export { createRpcClient, GrpcList } from './grpc';
4
- export { PuboFileSystem } from './file-system';
5
- export { SIGKILL, isProcessDied, getProcessName, getProcessTree, getProcessByPpid, getProcessCpuUseByPid, getProcessCommandByPid, heartbeat } from './child-process';
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
- export { RosTopicManager, RosTopic } from './ros/topic';
7
+ export { RosTopicManager, RosTopic } from './ros/topic';
8
+ export { PuboFileSystem } from './file-system';
package/es/ros/topic.js CHANGED
@@ -2,122 +2,79 @@ import { exec, spawn } from 'child_process';
2
2
  import { Emitter, WatchDog, StringSplit, sleep } from 'pubo-utils';
3
3
  import * as YAML from 'yaml';
4
4
  import { SIGKILL } from '../child-process';
5
- function _empty() {}
6
- function _awaitIgnored(value, direct) {
7
- if (!direct) {
8
- return value && value.then ? value.then(_empty) : Promise.resolve();
9
- }
10
- }
11
- function _await(value, then, direct) {
12
- if (direct) {
13
- return then ? then(value) : value;
14
- }
15
- if (!value || !value.then) {
16
- value = Promise.resolve(value);
17
- }
18
- return then ? value.then(then) : value;
19
- }
20
- export var RosTopic = /*#__PURE__*/function () {
21
- function RosTopic(topic, messageType) {
22
- this.topic = void 0;
23
- this.messageType = void 0;
24
- this.emitter = new Emitter();
25
- this.subscribed = false;
26
- this.dog = new WatchDog({
27
- limit: 10,
28
- onTimeout: this.onTimeout.bind(this)
29
- });
30
- this.strSplit = new StringSplit('---');
31
- this.subscribeChildProcess = void 0;
32
- this.topic = topic;
33
- this.messageType = messageType;
34
- this.subscribed = false;
35
- this.emitter = new Emitter();
36
- }
37
- var _proto = RosTopic.prototype;
38
- _proto.onTimeout = function onTimeout() {
39
- try {
40
- var _this = this;
41
- if (!_this.subscribed) {
42
- return _await();
43
- }
44
- return _await(_this.unsubscribe(), function () {
45
- return _await(sleep(1000), function () {
46
- return _awaitIgnored(_this.subscribe());
47
- });
48
- });
49
- } catch (e) {
50
- return Promise.reject(e);
5
+ export class RosTopic {
6
+ topic;
7
+ messageType;
8
+ emitter = new Emitter();
9
+ subscribed = false;
10
+ dog = new WatchDog({ limit: 10, onTimeout: this.onTimeout.bind(this) });
11
+ strSplit = new StringSplit('---');
12
+ subscribeChildProcess;
13
+ constructor(topic, messageType) {
14
+ this.topic = topic;
15
+ this.messageType = messageType;
16
+ this.subscribed = false;
17
+ this.emitter = new Emitter();
51
18
  }
52
- };
53
- _proto.onData = function onData(data) {
54
- var tmp = this.strSplit.split(data.toString()).slice(-1)[0];
55
- if (!tmp) {
56
- return;
19
+ async onTimeout() {
20
+ if (!this.subscribed) {
21
+ return;
22
+ }
23
+ await this.unsubscribe();
24
+ await sleep(1000);
25
+ await this.subscribe();
57
26
  }
58
- this.dog.feed();
59
- var res = YAML.parse(tmp);
60
- this.emitter.emit('message', res);
61
- };
62
- _proto.subscribe = function subscribe() {
63
- try {
64
- var _this2 = this;
65
- if (_this2.subscribeChildProcess) {
66
- return _await();
67
- }
68
- _this2.subscribed = true;
69
- _this2.dog.init();
70
- _this2.subscribeChildProcess = spawn("rostopic", ['echo', _this2.topic]);
71
- _this2.subscribeChildProcess.stdout.on('data', _this2.onData.bind(_this2));
72
- _this2.subscribeChildProcess.stderr.on('data', function (buf) {
73
- return console.log(buf.toString());
74
- });
75
- return _await();
76
- } catch (e) {
77
- return Promise.reject(e);
27
+ onData(data) {
28
+ const tmp = this.strSplit.split(data.toString()).slice(-1)[0];
29
+ if (!tmp) {
30
+ return;
31
+ }
32
+ this.dog.feed();
33
+ const res = YAML.parse(tmp);
34
+ this.emitter.emit('message', res);
78
35
  }
79
- };
80
- _proto.unsubscribe = function unsubscribe() {
81
- try {
82
- var _this3 = this;
83
- if (!_this3.subscribeChildProcess) {
84
- return _await();
85
- }
86
- _this3.dog.stop();
87
- _this3.subscribed = false;
88
- return _await(SIGKILL(_this3.subscribeChildProcess.pid), function () {
89
- _this3.subscribeChildProcess = null;
90
- });
91
- } catch (e) {
92
- return Promise.reject(e);
36
+ async subscribe() {
37
+ if (this.subscribeChildProcess) {
38
+ return;
39
+ }
40
+ this.subscribed = true;
41
+ this.dog.init();
42
+ this.subscribeChildProcess = spawn(`rostopic`, ['echo', this.topic]);
43
+ this.subscribeChildProcess.stdout.on('data', this.onData.bind(this));
44
+ this.subscribeChildProcess.stderr.on('data', (buf) => console.log(buf.toString()));
93
45
  }
94
- };
95
- _proto.publish = function publish(payload) {
96
- var _this4 = this;
97
- var data = YAML.stringify(payload);
98
- return new Promise(function (resolve, reject) {
99
- exec("rostopic pub -1 " + _this4.topic + " " + _this4.messageType + " \"" + data + "\"", function (err, stdout) {
100
- if (err) {
101
- reject(err);
102
- } else {
103
- resolve(stdout);
46
+ async unsubscribe() {
47
+ if (!this.subscribeChildProcess) {
48
+ return;
104
49
  }
105
- });
106
- });
107
- };
108
- return RosTopic;
109
- }();
110
- export var RosTopicManager = {
111
- cache: [],
112
- getTopic: function getTopic(topic, messageType) {
113
- var tmp = this.cache.find(function (item) {
114
- return item.topic === topic;
115
- });
116
- if (tmp) {
117
- return tmp;
50
+ this.dog.stop();
51
+ this.subscribed = false;
52
+ await SIGKILL(this.subscribeChildProcess.pid);
53
+ this.subscribeChildProcess = null;
54
+ }
55
+ publish(payload) {
56
+ const data = YAML.stringify(payload);
57
+ return new Promise((resolve, reject) => {
58
+ exec(`rostopic pub -1 ${this.topic} ${this.messageType} "${data}"`, (err, stdout) => {
59
+ if (err) {
60
+ reject(err);
61
+ }
62
+ else {
63
+ resolve(stdout);
64
+ }
65
+ });
66
+ });
118
67
  }
119
- var instance = new RosTopic(topic, messageType);
120
- this.cache.push(instance);
121
- return instance;
122
- }
123
- };
68
+ }
69
+ export const RosTopicManager = {
70
+ cache: [],
71
+ getTopic: function (topic, messageType) {
72
+ const tmp = this.cache.find((item) => item.topic === topic);
73
+ if (tmp) {
74
+ return tmp;
75
+ }
76
+ const instance = new RosTopic(topic, messageType);
77
+ this.cache.push(instance);
78
+ return instance;
79
+ },
80
+ };
@@ -2,323 +2,180 @@ import { readFileSync, writeFile, mkdirSync, writeFileSync } from 'fs';
2
2
  import { SyncQueue } from 'pubo-utils';
3
3
  import { v4 as uuid } from 'uuid';
4
4
  // eslint-disable-next-line @typescript-eslint/no-var-requires
5
-
6
- function _empty() {}
7
- var cluster = require('cluster');
5
+ const cluster = require('cluster');
8
6
  // 主线程的实现
9
-
10
- function _awaitIgnored(value, direct) {
11
- if (!direct) {
12
- return value && value.then ? value.then(_empty) : Promise.resolve();
13
- }
14
- }
15
- var Manager = /*#__PURE__*/function () {
16
- function Manager(path, defaultState) {
17
- var _this = this;
18
- this.path = void 0;
19
- this._state = {};
20
- this.queue = new SyncQueue();
21
- this.key = void 0;
22
- this.defaultState = void 0;
23
- this.path = path;
24
- this.defaultState = defaultState;
25
- this.key = encodeURIComponent(path);
26
- cluster.on('online', function (worker) {
27
- worker.on('message', function (message) {
28
- _this.onMessage(message, worker);
29
- message = null;
30
- });
31
- });
32
- cluster.on('exit', function (worker) {
33
- worker.removeAllListeners('message');
34
- worker = null;
35
- });
36
- this.restore();
37
- if (global.GlobalEmitter) {
38
- global.GlobalEmitter.on('SIGINT', this.kill.bind(this));
39
- }
40
- }
41
- // 进程退出时,同步文件
42
- var _proto = Manager.prototype;
43
- _proto.kill = function kill() {
44
- try {
45
- var _this2 = this;
46
- return _await(_awaitIgnored(_this2.queue.push(function () {
47
- return _this2.syncFile();
48
- })));
49
- } catch (e) {
50
- return Promise.reject(e);
51
- }
52
- };
53
- _proto.onMessage = function onMessage(message, worker) {
54
- try {
55
- var _this3 = this;
56
- if (message.key !== _this3.key) {
57
- return _await();
58
- }
59
- var payload;
60
- return _await(_invoke(function () {
61
- if (message.type === 'get') {
62
- return _await(_this3.getState(), function (_this3$getState) {
63
- payload = _this3$getState;
64
- });
65
- } else return _invokeIgnored(function () {
66
- if (message.type === 'set') {
67
- return _await(_this3.setState(message.payload), function (_this3$setState) {
68
- payload = _this3$setState;
7
+ class Manager {
8
+ path;
9
+ _state = {};
10
+ queue = new SyncQueue();
11
+ key;
12
+ defaultState;
13
+ constructor(path, defaultState) {
14
+ this.path = path;
15
+ this.defaultState = defaultState;
16
+ this.key = encodeURIComponent(path);
17
+ cluster.on('online', (worker) => {
18
+ worker.on('message', (message) => {
19
+ this.onMessage(message, worker);
20
+ message = null;
69
21
  });
70
- }
71
22
  });
72
- }, function () {
73
- worker.send({
74
- uid: message.uid,
75
- key: _this3.key,
76
- payload: payload
23
+ cluster.on('exit', (worker) => {
24
+ worker.removeAllListeners('message');
25
+ worker = null;
77
26
  });
27
+ this.restore();
28
+ if (global.GlobalEmitter) {
29
+ global.GlobalEmitter.on('SIGINT', this.kill.bind(this));
30
+ }
31
+ else {
32
+ process.on('SIGINT', () => {
33
+ this.kill().then(() => process.exit(0));
34
+ });
35
+ }
36
+ }
37
+ // 进程退出时,同步文件
38
+ async kill() {
39
+ await this.queue.push(() => this.syncFile());
40
+ }
41
+ async onMessage(message, worker) {
42
+ if (message.key !== this.key) {
43
+ return;
44
+ }
45
+ let payload;
46
+ if (message.type === 'get') {
47
+ payload = await this.getState();
48
+ }
49
+ else if (message.type === 'set') {
50
+ payload = await this.setState(message.payload);
51
+ }
52
+ worker.send({ uid: message.uid, key: this.key, payload });
78
53
  message = null;
79
54
  worker = null;
80
- }));
81
- } catch (e) {
82
- return Promise.reject(e);
83
- }
84
- };
85
- _proto.sync = function sync() {
86
- if (this.queue.length > 0) {
87
- return;
88
55
  }
89
- this.queue.push(this._syncFile.bind(this));
90
- }
91
- // 同步文件备份
92
- ;
93
- _proto.syncFile = function syncFile() {
94
- writeFileSync(this.path, JSON.stringify(this._state));
95
- }
96
- // 异步文件备份
97
- ;
98
- _proto._syncFile = function _syncFile() {
99
- try {
100
- var _this4 = this;
101
- return _await(new Promise(function (resolve, reject) {
102
- writeFile(_this4.path, JSON.stringify(_this4._state), function (err) {
103
- if (err) {
104
- reject(err);
105
- } else {
106
- setTimeout(resolve, 100);
107
- }
56
+ sync() {
57
+ if (this.queue.length > 0) {
58
+ return;
59
+ }
60
+ this.queue.push(this._syncFile.bind(this));
61
+ }
62
+ // 同步文件备份
63
+ syncFile() {
64
+ writeFileSync(this.path, JSON.stringify(this._state));
65
+ }
66
+ // 异步文件备份
67
+ async _syncFile() {
68
+ return new Promise((resolve, reject) => {
69
+ writeFile(this.path, JSON.stringify(this._state), (err) => {
70
+ if (err) {
71
+ reject(err);
72
+ }
73
+ else {
74
+ setTimeout(resolve, 100);
75
+ }
76
+ });
108
77
  });
109
- }));
110
- } catch (e) {
111
- return Promise.reject(e);
112
78
  }
113
- };
114
- _proto.restore = function restore() {
115
- try {
116
- var buf = readFileSync(this.path);
117
- this._state = JSON.parse(buf.toString());
118
- } catch (err) {
119
- var _this$defaultState;
120
- var str = process.platform === 'win32' ? '\\' : '/';
121
- if (str) {
122
- mkdirSync(this.path.split(str).slice(0, -1).join(str), {
123
- recursive: true
124
- });
125
- }
126
- this.setState((_this$defaultState = this.defaultState) != null ? _this$defaultState : {});
79
+ restore() {
80
+ try {
81
+ const buf = readFileSync(this.path);
82
+ this._state = JSON.parse(buf.toString());
83
+ }
84
+ catch (err) {
85
+ const str = process.platform === 'win32' ? '\\' : '/';
86
+ if (str) {
87
+ mkdirSync(this.path.split(str).slice(0, -1).join(str), { recursive: true });
88
+ }
89
+ this.setState(this.defaultState ?? {});
90
+ }
127
91
  }
128
- };
129
- _proto.getState = function getState() {
130
- try {
131
- var _this5 = this;
132
- return _await(_this5._state);
133
- } catch (e) {
134
- return Promise.reject(e);
92
+ async getState() {
93
+ return this._state;
135
94
  }
136
- };
137
- _proto.setState = function setState(values) {
138
- try {
139
- var _this6 = this;
140
- _this6._state = values;
141
- _this6.sync();
142
- return _await();
143
- } catch (e) {
144
- return Promise.reject(e);
95
+ async setState(values) {
96
+ this._state = values;
97
+ this.sync();
145
98
  }
146
- };
147
- return Manager;
148
- }(); // work 线程的实现
149
- function _await(value, then, direct) {
150
- if (direct) {
151
- return then ? then(value) : value;
152
- }
153
- if (!value || !value.then) {
154
- value = Promise.resolve(value);
155
- }
156
- return then ? value.then(then) : value;
157
99
  }
158
- var Worker = /*#__PURE__*/function () {
159
- function Worker(path) {
160
- this.key = void 0;
161
- this.callback = {};
162
- this.key = encodeURIComponent(path);
163
- process.on('message', this.onMessage.bind(this));
164
- }
165
- var _proto2 = Worker.prototype;
166
- _proto2.onMessage = function onMessage(message) {
167
- if (message.key !== this.key) {
168
- return;
169
- }
170
- if (typeof this.callback[message.uid] === 'function') {
171
- this.callback[message.uid](message.payload);
172
- delete this.callback[message.uid];
100
+ // work 线程的实现
101
+ class Worker {
102
+ key;
103
+ callback = {};
104
+ constructor(path) {
105
+ this.key = encodeURIComponent(path);
106
+ process.on('message', this.onMessage.bind(this));
107
+ }
108
+ onMessage(message) {
109
+ if (message.key !== this.key) {
110
+ return;
111
+ }
112
+ if (typeof this.callback[message.uid] === 'function') {
113
+ this.callback[message.uid](message.payload);
114
+ delete this.callback[message.uid];
115
+ }
116
+ message = null;
173
117
  }
174
- message = null;
175
- };
176
- _proto2.call = function call(_ref) {
177
- var type = _ref.type,
178
- payload = _ref.payload;
179
- try {
180
- var _this7 = this;
181
- return _await(new Promise(function (resolve) {
182
- var uid = uuid();
183
- _this7.callback[uid] = function (data) {
184
- return resolve(data);
185
- };
186
- //@ts-ignore
187
- process.send({
188
- uid: uid,
189
- type: type,
190
- payload: payload,
191
- key: _this7.key
118
+ async call({ type, payload }) {
119
+ return new Promise((resolve) => {
120
+ const uid = uuid();
121
+ this.callback[uid] = (data) => resolve(data);
122
+ //@ts-ignore
123
+ process.send({ uid, type, payload, key: this.key });
124
+ payload = null;
125
+ type = null;
192
126
  });
193
- payload = null;
194
- type = null;
195
- }));
196
- } catch (e) {
197
- return Promise.reject(e);
198
127
  }
199
- };
200
- _proto2.getState = function getState() {
201
- try {
202
- var _this8 = this;
203
- return _await(_this8.call({
204
- type: 'get',
205
- payload: {}
206
- }));
207
- } catch (e) {
208
- return Promise.reject(e);
128
+ async getState() {
129
+ return this.call({ type: 'get', payload: {} });
209
130
  }
210
- };
211
- _proto2.setState = function setState(payload) {
212
- try {
213
- var _this9 = this;
214
- return _await(_this9.call({
215
- type: 'set',
216
- payload: payload
217
- }));
218
- } catch (e) {
219
- return Promise.reject(e);
131
+ async setState(payload) {
132
+ return this.call({ type: 'set', payload });
220
133
  }
221
- };
222
- return Worker;
223
- }();
224
- function _invokeIgnored(body) {
225
- var result = body();
226
- if (result && result.then) {
227
- return result.then(_empty);
228
- }
229
134
  }
230
- function _invoke(body, then) {
231
- var result = body();
232
- if (result && result.then) {
233
- return result.then(then);
234
- }
235
- return then(result);
236
- }
237
- export var JsonStorage = /*#__PURE__*/function () {
238
- function JsonStorage(path, options) {
239
- var _options;
240
- if (options === void 0) {
241
- options = {};
242
- }
243
- this.instance = void 0;
244
- this.queue = new SyncQueue();
245
- if (cluster.isPrimary) {
246
- this.instance = new Manager(path, options.defaultState);
247
- } else {
248
- this.instance = new Worker(path);
249
- }
250
- if ((_options = options) != null && _options.initialState) {
251
- this.merge(options.initialState);
135
+ export class JsonStorage {
136
+ instance;
137
+ queue = new SyncQueue();
138
+ constructor(path, options = {}) {
139
+ if (cluster.isPrimary) {
140
+ this.instance = new Manager(path, options.defaultState);
141
+ }
142
+ else {
143
+ this.instance = new Worker(path);
144
+ }
145
+ if (options?.initialState) {
146
+ this.merge(options.initialState);
147
+ }
252
148
  }
253
- }
254
- var _proto3 = JsonStorage.prototype;
255
- _proto3.getState = function getState() {
256
- try {
257
- var _this10 = this;
258
- return _await(_this10.queue.push(_this10.instance.getState.bind(_this10.instance)));
259
- } catch (e) {
260
- return Promise.reject(e);
149
+ async getState() {
150
+ return this.queue.push(this.instance.getState.bind(this.instance));
261
151
  }
262
- };
263
- _proto3.setState = function setState(state) {
264
- try {
265
- var _this11 = this;
266
- return _await(_this11.queue.push(function () {
267
- return _this11.instance.setState(state);
268
- }));
269
- } catch (e) {
270
- return Promise.reject(e);
152
+ async setState(state) {
153
+ return this.queue.push(() => this.instance.setState(state));
271
154
  }
272
- };
273
- _proto3.get = function get(key) {
274
- try {
275
- var _this12 = this;
276
- if (!key) {
277
- return _await(_this12.getState());
278
- } else {
279
- return _await(_this12.getState(), function (state) {
280
- return state[key];
281
- });
282
- }
283
- } catch (e) {
284
- return Promise.reject(e);
155
+ async get(key) {
156
+ if (!key) {
157
+ return this.getState();
158
+ }
159
+ else {
160
+ const state = await this.getState();
161
+ return state[key];
162
+ }
285
163
  }
286
- };
287
- _proto3.set = function set(key, values) {
288
- try {
289
- var _this13 = this;
290
- return _await(_this13.getState(), function (state) {
164
+ async set(key, values) {
165
+ const state = await this.getState();
291
166
  state[key] = values;
292
- return _awaitIgnored(_this13.setState(state));
293
- });
294
- } catch (e) {
295
- return Promise.reject(e);
167
+ await this.setState(state);
296
168
  }
297
- };
298
- _proto3.merge = function merge(values) {
299
- try {
300
- var _this14 = this;
301
- return _await(_this14.getState(), function (state) {
302
- for (var _i = 0, _Object$keys = Object.keys(values); _i < _Object$keys.length; _i++) {
303
- var key = _Object$keys[_i];
304
- state[key] = values[key];
169
+ async merge(values) {
170
+ const state = await this.getState();
171
+ for (const key of Object.keys(values)) {
172
+ state[key] = values[key];
305
173
  }
306
- return _awaitIgnored(_this14.setState(state));
307
- });
308
- } catch (e) {
309
- return Promise.reject(e);
174
+ await this.setState(state);
310
175
  }
311
- };
312
- _proto3.remove = function remove(key) {
313
- try {
314
- var _this15 = this;
315
- return _await(_this15.getState(), function (state) {
176
+ async remove(key) {
177
+ const state = await this.getState();
316
178
  delete state[key];
317
- return _awaitIgnored(_this15.setState(state));
318
- });
319
- } catch (e) {
320
- return Promise.reject(e);
179
+ await this.setState(state);
321
180
  }
322
- };
323
- return JsonStorage;
324
- }();
181
+ }