pubo-node 1.0.144 → 1.0.145

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