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/dist/pubo-node.js +1 -1
- package/es/child-process/index.js +129 -362
- package/es/file-system/index.js +25 -32
- package/es/ftp-client/index.js +120 -245
- package/es/grpc/index.js +92 -158
- package/es/index.d.ts +1 -1
- package/es/index.js +3 -3
- package/es/ros/topic.js +70 -113
- package/es/storage/json.js +148 -291
- package/es/utils/index.js +13 -13
- package/es/utils/network.js +30 -53
- package/lib/index.d.ts +1 -1
- package/lib/index.js +3 -3
- package/lib/storage/json.js +5 -0
- package/package.json +3 -3
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 {
|
|
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
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
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
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
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
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
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
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
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
|
-
|
|
96
|
-
|
|
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
|
-
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
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
|
-
|
|
120
|
-
|
|
121
|
-
|
|
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
|
+
};
|
package/es/storage/json.js
CHANGED
|
@@ -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
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
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
|
-
|
|
73
|
-
|
|
74
|
-
|
|
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
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
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
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
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
|
-
|
|
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
|
-
|
|
138
|
-
|
|
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
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
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
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
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
|
-
|
|
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
|
-
|
|
293
|
-
});
|
|
294
|
-
} catch (e) {
|
|
295
|
-
return Promise.reject(e);
|
|
167
|
+
await this.setState(state);
|
|
296
168
|
}
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
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
|
-
|
|
307
|
-
});
|
|
308
|
-
} catch (e) {
|
|
309
|
-
return Promise.reject(e);
|
|
174
|
+
await this.setState(state);
|
|
310
175
|
}
|
|
311
|
-
|
|
312
|
-
|
|
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
|
-
|
|
318
|
-
});
|
|
319
|
-
} catch (e) {
|
|
320
|
-
return Promise.reject(e);
|
|
179
|
+
await this.setState(state);
|
|
321
180
|
}
|
|
322
|
-
|
|
323
|
-
return JsonStorage;
|
|
324
|
-
}();
|
|
181
|
+
}
|