pubo-node 1.0.152 → 1.0.154
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 +3 -2
- package/es/file-system/index.d.ts +2 -0
- package/es/file-system/index.js +11 -9
- package/es/grpc/index.d.ts +0 -2
- package/es/grpc/index.js +56 -94
- package/es/index.d.ts +1 -2
- package/es/index.js +1 -2
- package/es/ros/topic.d.ts +2 -2
- package/es/ros/topic.js +59 -20
- package/es/storage/json.d.ts +1 -0
- package/es/storage/json.js +69 -41
- package/lib/child-process/index.js +3 -2
- package/lib/file-system/index.d.ts +2 -0
- package/lib/file-system/index.js +11 -9
- package/lib/grpc/index.d.ts +0 -2
- package/lib/grpc/index.js +22 -23
- package/lib/index.d.ts +1 -2
- package/lib/index.js +2 -3
- package/lib/ros/topic.d.ts +2 -2
- package/lib/ros/topic.js +12 -9
- package/lib/storage/json.d.ts +1 -0
- package/lib/storage/json.js +26 -7
- package/package.json +2 -2
- package/es/pitch.d.ts +0 -4
- package/es/pitch.js +0 -23
- package/lib/pitch.d.ts +0 -4
- package/lib/pitch.js +0 -15
|
@@ -313,8 +313,8 @@ export var getProcessTree = _async(function (pid, tree) {
|
|
|
313
313
|
children: []
|
|
314
314
|
};
|
|
315
315
|
}
|
|
316
|
-
return _await(getProcessByPpid(pid), function (
|
|
317
|
-
return _continue(_forOf(
|
|
316
|
+
return _await(getProcessByPpid(pid), function (pidList) {
|
|
317
|
+
return _continue(_forOf(pidList, function (id) {
|
|
318
318
|
var item = {
|
|
319
319
|
pid: id,
|
|
320
320
|
children: []
|
|
@@ -327,6 +327,7 @@ export var getProcessTree = _async(function (pid, tree) {
|
|
|
327
327
|
return tree;
|
|
328
328
|
} else {
|
|
329
329
|
tree = null;
|
|
330
|
+
return null;
|
|
330
331
|
}
|
|
331
332
|
});
|
|
332
333
|
});
|
|
@@ -18,6 +18,8 @@ interface PuboFileSystemInterface {
|
|
|
18
18
|
} | null) => Promise<string[]>;
|
|
19
19
|
open: (path: fs.PathLike, flags?: fs.OpenMode, mode?: fs.Mode | null) => Promise<number>;
|
|
20
20
|
close: (fd: number) => Promise<void>;
|
|
21
|
+
mkdir: (path: fs.PathLike, options?: fs.MakeDirectoryOptions) => Promise<void>;
|
|
22
|
+
rm: (path: fs.PathLike) => Promise<void>;
|
|
21
23
|
write: <TBuffer extends NodeJS.ArrayBufferView>(fd: number, buffer: TBuffer, offset?: number | null, length?: number | null, position?: number | null) => Promise<void>;
|
|
22
24
|
}
|
|
23
25
|
export declare const PuboFileSystem: PuboFileSystemInterface;
|
package/es/file-system/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as fs from 'fs';
|
|
2
|
-
var
|
|
2
|
+
var callback2promise = function callback2promise(fn) {
|
|
3
3
|
return function () {
|
|
4
4
|
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
5
5
|
args[_key] = arguments[_key];
|
|
@@ -22,12 +22,14 @@ var callbackToPromise = function callbackToPromise(fn) {
|
|
|
22
22
|
};
|
|
23
23
|
};
|
|
24
24
|
export var PuboFileSystem = {
|
|
25
|
-
read:
|
|
26
|
-
readFile:
|
|
27
|
-
writeFile:
|
|
28
|
-
readdir:
|
|
29
|
-
open:
|
|
30
|
-
close:
|
|
31
|
-
write:
|
|
32
|
-
stat:
|
|
25
|
+
read: callback2promise(fs.read),
|
|
26
|
+
readFile: callback2promise(fs.readFile),
|
|
27
|
+
writeFile: callback2promise(fs.writeFile),
|
|
28
|
+
readdir: callback2promise(fs.readdir),
|
|
29
|
+
open: callback2promise(fs.open),
|
|
30
|
+
close: callback2promise(fs.close),
|
|
31
|
+
write: callback2promise(fs.write),
|
|
32
|
+
stat: callback2promise(fs.stat),
|
|
33
|
+
mkdir: callback2promise(fs.mkdir),
|
|
34
|
+
rm: callback2promise(fs.rm)
|
|
33
35
|
};
|
package/es/grpc/index.d.ts
CHANGED
|
@@ -14,7 +14,6 @@ declare class GrpcClient {
|
|
|
14
14
|
private client;
|
|
15
15
|
private _timeout;
|
|
16
16
|
connections: number;
|
|
17
|
-
private closing;
|
|
18
17
|
constructor({ url, options, Grpc, cert }: any);
|
|
19
18
|
request(service: any, method: any, data: any): Promise<Buffer>;
|
|
20
19
|
_request({ service, method, data }: {
|
|
@@ -22,7 +21,6 @@ declare class GrpcClient {
|
|
|
22
21
|
method: any;
|
|
23
22
|
data: any;
|
|
24
23
|
}): Promise<Buffer>;
|
|
25
|
-
restart(): Promise<void>;
|
|
26
24
|
close(): void;
|
|
27
25
|
}
|
|
28
26
|
export declare const GrpcList: GrpcClient[];
|
package/es/grpc/index.js
CHANGED
|
@@ -1,9 +1,3 @@
|
|
|
1
|
-
function _empty() {}
|
|
2
|
-
function _awaitIgnored(value, direct) {
|
|
3
|
-
if (!direct) {
|
|
4
|
-
return value && value.then ? value.then(_empty) : Promise.resolve();
|
|
5
|
-
}
|
|
6
|
-
}
|
|
7
1
|
function _await(value, then, direct) {
|
|
8
2
|
if (direct) {
|
|
9
3
|
return then ? then(value) : value;
|
|
@@ -27,30 +21,10 @@ function _catch(body, recover) {
|
|
|
27
21
|
function _continue(value, then) {
|
|
28
22
|
return value && value.then ? value.then(then) : then(value);
|
|
29
23
|
}
|
|
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));
|
|
44
|
-
} catch (e) {
|
|
45
|
-
return Promise.reject(e);
|
|
46
|
-
}
|
|
47
|
-
};
|
|
48
|
-
}
|
|
49
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); }
|
|
50
|
-
|
|
51
|
-
function passThrough(argument) {
|
|
25
|
+
var passThrough = function passThrough(argument) {
|
|
52
26
|
return argument;
|
|
53
|
-
}
|
|
27
|
+
};
|
|
54
28
|
var GrpcClient = /*#__PURE__*/function () {
|
|
55
29
|
function GrpcClient(_ref) {
|
|
56
30
|
var _this$options$timeout;
|
|
@@ -66,7 +40,6 @@ var GrpcClient = /*#__PURE__*/function () {
|
|
|
66
40
|
this.client = void 0;
|
|
67
41
|
this._timeout = void 0;
|
|
68
42
|
this.connections = 0;
|
|
69
|
-
this.closing = false;
|
|
70
43
|
var opt = _extends({
|
|
71
44
|
'grpc.max_send_message_length': -1,
|
|
72
45
|
'grpc.max_receive_message_length': -1
|
|
@@ -76,58 +49,58 @@ var GrpcClient = /*#__PURE__*/function () {
|
|
|
76
49
|
this.Grpc = Grpc;
|
|
77
50
|
this.credentials = credentials;
|
|
78
51
|
this.options = opt;
|
|
79
|
-
this.options.timeout = (_this$options$timeout = this.options.timeout) != null ? _this$options$timeout :
|
|
52
|
+
this.options.timeout = (_this$options$timeout = this.options.timeout) != null ? _this$options$timeout : 10000;
|
|
53
|
+
Grpc = null;
|
|
54
|
+
options = null;
|
|
55
|
+
cert = null;
|
|
80
56
|
}
|
|
81
57
|
var _proto = GrpcClient.prototype;
|
|
82
58
|
_proto.request = function request(service, method, data) {
|
|
83
59
|
try {
|
|
84
60
|
var _this = this;
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
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 () {
|
|
82
|
+
service = null;
|
|
83
|
+
method = null;
|
|
84
|
+
data = null;
|
|
85
|
+
_this.connections -= 1;
|
|
86
|
+
if (_this.connections < 0) {
|
|
87
|
+
_this.connections = 0;
|
|
98
88
|
}
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
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.');
|
|
89
|
+
if (_this.connections < 1) {
|
|
90
|
+
if (_this._timeout) {
|
|
91
|
+
clearTimeout(_this._timeout);
|
|
92
|
+
_this._timeout = null;
|
|
128
93
|
}
|
|
129
|
-
|
|
130
|
-
|
|
94
|
+
_this._timeout = setTimeout(function () {
|
|
95
|
+
return _this.close();
|
|
96
|
+
}, 60000);
|
|
97
|
+
}
|
|
98
|
+
if (error) {
|
|
99
|
+
console.log(error);
|
|
100
|
+
_this.close();
|
|
101
|
+
throw new Error('grpc connection error.');
|
|
102
|
+
}
|
|
103
|
+
return result;
|
|
131
104
|
}));
|
|
132
105
|
} catch (e) {
|
|
133
106
|
return Promise.reject(e);
|
|
@@ -142,11 +115,11 @@ var GrpcClient = /*#__PURE__*/function () {
|
|
|
142
115
|
var _ended = false;
|
|
143
116
|
var _timeout = setTimeout(function () {
|
|
144
117
|
_ended = true;
|
|
145
|
-
_this2.
|
|
118
|
+
_this2.close();
|
|
146
119
|
console.log('rpc request timeout');
|
|
147
120
|
reject(new Error('timeout'));
|
|
148
121
|
}, _this2.options.timeout);
|
|
149
|
-
var
|
|
122
|
+
var onResponse = function onResponse(err, res) {
|
|
150
123
|
if (_ended) {
|
|
151
124
|
return;
|
|
152
125
|
} else {
|
|
@@ -157,29 +130,16 @@ var GrpcClient = /*#__PURE__*/function () {
|
|
|
157
130
|
} else {
|
|
158
131
|
resolve(res);
|
|
159
132
|
}
|
|
160
|
-
_onResponse = null;
|
|
161
133
|
};
|
|
162
|
-
_this2.client.makeUnaryRequest("/" + service + "/" + method, passThrough, passThrough, data,
|
|
134
|
+
_this2.client.makeUnaryRequest("/" + service + "/" + method, passThrough, passThrough, data ? Buffer.from(data) : Buffer.alloc(0), onResponse);
|
|
163
135
|
});
|
|
164
136
|
};
|
|
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
137
|
_proto.close = function close() {
|
|
182
138
|
var _this$client;
|
|
139
|
+
if (this._timeout) {
|
|
140
|
+
clearTimeout(this._timeout);
|
|
141
|
+
delete this._timeout;
|
|
142
|
+
}
|
|
183
143
|
(_this$client = this.client) == null ? void 0 : _this$client.close();
|
|
184
144
|
this.client = null;
|
|
185
145
|
delete this.client;
|
|
@@ -201,6 +161,8 @@ export function createRpcClient(_ref3) {
|
|
|
201
161
|
cert: cert
|
|
202
162
|
});
|
|
203
163
|
GrpcList.push(client);
|
|
164
|
+
Grpc = null;
|
|
165
|
+
options = null;
|
|
204
166
|
return new ServiceImp({
|
|
205
167
|
request: client.request.bind(client)
|
|
206
168
|
});
|
package/es/index.d.ts
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
export { JsonStorage } from './storage/json';
|
|
2
2
|
export { FtpClient, FtpClientPool, FtpConnectOptions, FtpFile } from './ftp-client';
|
|
3
|
-
export { createRpcClient } from './grpc';
|
|
3
|
+
export { createRpcClient, GrpcList } from './grpc';
|
|
4
4
|
export { PuboFileSystem } from './file-system';
|
|
5
5
|
export { SIGKILL, isProcessDied, getProcessName, getProcessTree, getProcessByPpid, getProcessCpuUseByPid, getProcessCommandByPid, heartbeat, } from './child-process';
|
|
6
6
|
export { isPortAvailable } from './utils';
|
|
7
|
-
export { pitch } from './pitch';
|
|
8
7
|
export { getWifiName, getNetworks } from './utils/network';
|
|
9
8
|
export { RosTopicManager, RosTopic } from './ros/topic';
|
package/es/index.js
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
export { JsonStorage } from './storage/json';
|
|
2
2
|
export { FtpClient, FtpClientPool } from './ftp-client';
|
|
3
|
-
export { createRpcClient } from './grpc';
|
|
3
|
+
export { createRpcClient, GrpcList } from './grpc';
|
|
4
4
|
export { PuboFileSystem } from './file-system';
|
|
5
5
|
export { SIGKILL, isProcessDied, getProcessName, getProcessTree, getProcessByPpid, getProcessCpuUseByPid, getProcessCommandByPid, heartbeat } from './child-process';
|
|
6
6
|
export { isPortAvailable } from './utils';
|
|
7
|
-
export { pitch } from './pitch';
|
|
8
7
|
export { getWifiName, getNetworks } from './utils/network';
|
|
9
8
|
export { RosTopicManager, RosTopic } from './ros/topic';
|
package/es/ros/topic.d.ts
CHANGED
|
@@ -10,8 +10,8 @@ export declare class RosTopic {
|
|
|
10
10
|
constructor(topic: any, messageType: any);
|
|
11
11
|
private onTimeout;
|
|
12
12
|
private onData;
|
|
13
|
-
subscribe(): void
|
|
14
|
-
unsubscribe(): void
|
|
13
|
+
subscribe(): Promise<void>;
|
|
14
|
+
unsubscribe(): Promise<void>;
|
|
15
15
|
publish(payload: any): Promise<unknown>;
|
|
16
16
|
}
|
|
17
17
|
export declare const RosTopicManager: {
|
package/es/ros/topic.js
CHANGED
|
@@ -1,7 +1,22 @@
|
|
|
1
1
|
import { exec, spawn } from 'child_process';
|
|
2
|
-
import { Emitter, WatchDog, StringSplit } from 'pubo-utils';
|
|
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
|
+
}
|
|
5
20
|
export var RosTopic = /*#__PURE__*/function () {
|
|
6
21
|
function RosTopic(topic, messageType) {
|
|
7
22
|
this.topic = void 0;
|
|
@@ -9,7 +24,7 @@ export var RosTopic = /*#__PURE__*/function () {
|
|
|
9
24
|
this.emitter = new Emitter();
|
|
10
25
|
this.subscribed = false;
|
|
11
26
|
this.dog = new WatchDog({
|
|
12
|
-
limit:
|
|
27
|
+
limit: 10,
|
|
13
28
|
onTimeout: this.onTimeout.bind(this)
|
|
14
29
|
});
|
|
15
30
|
this.strSplit = new StringSplit('---');
|
|
@@ -21,43 +36,67 @@ export var RosTopic = /*#__PURE__*/function () {
|
|
|
21
36
|
}
|
|
22
37
|
var _proto = RosTopic.prototype;
|
|
23
38
|
_proto.onTimeout = function onTimeout() {
|
|
24
|
-
|
|
25
|
-
|
|
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);
|
|
26
51
|
}
|
|
27
|
-
this.unsubscribe();
|
|
28
|
-
this.subscribe();
|
|
29
52
|
};
|
|
30
53
|
_proto.onData = function onData(data) {
|
|
31
54
|
var tmp = this.strSplit.split(data.toString()).slice(-1)[0];
|
|
32
55
|
if (!tmp) {
|
|
33
56
|
return;
|
|
34
57
|
}
|
|
58
|
+
this.dog.feed();
|
|
35
59
|
var res = YAML.parse(tmp);
|
|
36
60
|
this.emitter.emit('message', res);
|
|
37
61
|
};
|
|
38
62
|
_proto.subscribe = function subscribe() {
|
|
39
|
-
|
|
40
|
-
|
|
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);
|
|
41
78
|
}
|
|
42
|
-
this.subscribed = true;
|
|
43
|
-
this.subscribeChildProcess = spawn("rostopic", ['echo', this.topic]);
|
|
44
|
-
this.subscribeChildProcess.stdout.on('data', this.onData.bind(this));
|
|
45
|
-
this.dog.init();
|
|
46
79
|
};
|
|
47
80
|
_proto.unsubscribe = function unsubscribe() {
|
|
48
|
-
|
|
49
|
-
|
|
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);
|
|
50
93
|
}
|
|
51
|
-
this.dog.stop();
|
|
52
|
-
this.subscribed = false;
|
|
53
|
-
SIGKILL(this.subscribeChildProcess.pid);
|
|
54
|
-
this.subscribeChildProcess = null;
|
|
55
94
|
};
|
|
56
95
|
_proto.publish = function publish(payload) {
|
|
57
|
-
var
|
|
96
|
+
var _this4 = this;
|
|
58
97
|
var data = YAML.stringify(payload);
|
|
59
98
|
return new Promise(function (resolve, reject) {
|
|
60
|
-
exec("rostopic pub -1 " +
|
|
99
|
+
exec("rostopic pub -1 " + _this4.topic + " " + _this4.messageType + " \"" + data + "\"", function (err, stdout) {
|
|
61
100
|
if (err) {
|
|
62
101
|
reject(err);
|
|
63
102
|
} else {
|
package/es/storage/json.d.ts
CHANGED
package/es/storage/json.js
CHANGED
|
@@ -1,19 +1,17 @@
|
|
|
1
|
-
import { readFileSync, writeFile, mkdirSync } from 'fs';
|
|
1
|
+
import { readFileSync, writeFile, mkdirSync, writeFileSync } from 'fs';
|
|
2
2
|
import { SyncQueue } from 'pubo-utils';
|
|
3
3
|
import { v4 as uuid } from 'uuid';
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
}
|
|
8
|
-
if (!value || !value.then) {
|
|
9
|
-
value = Promise.resolve(value);
|
|
10
|
-
}
|
|
11
|
-
return then ? value.then(then) : value;
|
|
12
|
-
}
|
|
4
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
5
|
+
|
|
6
|
+
function _empty() {}
|
|
13
7
|
var cluster = require('cluster');
|
|
14
8
|
// 主线程的实现
|
|
15
9
|
|
|
16
|
-
function
|
|
10
|
+
function _awaitIgnored(value, direct) {
|
|
11
|
+
if (!direct) {
|
|
12
|
+
return value && value.then ? value.then(_empty) : Promise.resolve();
|
|
13
|
+
}
|
|
14
|
+
}
|
|
17
15
|
var Manager = /*#__PURE__*/function () {
|
|
18
16
|
function Manager(path, defaultState) {
|
|
19
17
|
var _this = this;
|
|
@@ -36,31 +34,51 @@ var Manager = /*#__PURE__*/function () {
|
|
|
36
34
|
worker = null;
|
|
37
35
|
});
|
|
38
36
|
this.restore();
|
|
37
|
+
if (global.GlobalEmitter) {
|
|
38
|
+
global.GlobalEmitter.on('SIGINT', this.kill.bind(this));
|
|
39
|
+
} else {
|
|
40
|
+
process.on('SIGINT', function () {
|
|
41
|
+
_this.kill().then(function () {
|
|
42
|
+
return process.exit(0);
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
}
|
|
39
46
|
}
|
|
47
|
+
// 进程退出时,同步文件
|
|
40
48
|
var _proto = Manager.prototype;
|
|
41
|
-
_proto.
|
|
49
|
+
_proto.kill = function kill() {
|
|
42
50
|
try {
|
|
43
51
|
var _this2 = this;
|
|
44
|
-
|
|
52
|
+
return _await(_awaitIgnored(_this2.queue.push(function () {
|
|
53
|
+
return _this2.syncFile();
|
|
54
|
+
})));
|
|
55
|
+
} catch (e) {
|
|
56
|
+
return Promise.reject(e);
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
_proto.onMessage = function onMessage(message, worker) {
|
|
60
|
+
try {
|
|
61
|
+
var _this3 = this;
|
|
62
|
+
if (message.key !== _this3.key) {
|
|
45
63
|
return _await();
|
|
46
64
|
}
|
|
47
65
|
var payload;
|
|
48
66
|
return _await(_invoke(function () {
|
|
49
67
|
if (message.type === 'get') {
|
|
50
|
-
return _await(
|
|
51
|
-
payload =
|
|
68
|
+
return _await(_this3.getState(), function (_this3$getState) {
|
|
69
|
+
payload = _this3$getState;
|
|
52
70
|
});
|
|
53
71
|
} else return _invokeIgnored(function () {
|
|
54
72
|
if (message.type === 'set') {
|
|
55
|
-
return _await(
|
|
56
|
-
payload =
|
|
73
|
+
return _await(_this3.setState(message.payload), function (_this3$setState) {
|
|
74
|
+
payload = _this3$setState;
|
|
57
75
|
});
|
|
58
76
|
}
|
|
59
77
|
});
|
|
60
78
|
}, function () {
|
|
61
79
|
worker.send({
|
|
62
80
|
uid: message.uid,
|
|
63
|
-
key:
|
|
81
|
+
key: _this3.key,
|
|
64
82
|
payload: payload
|
|
65
83
|
});
|
|
66
84
|
message = null;
|
|
@@ -71,16 +89,18 @@ var Manager = /*#__PURE__*/function () {
|
|
|
71
89
|
}
|
|
72
90
|
};
|
|
73
91
|
_proto.sync = function sync() {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
if (_this3.queue.length > 0) {
|
|
77
|
-
return _await();
|
|
78
|
-
}
|
|
79
|
-
return _await(_awaitIgnored(_this3.queue.push(_this3._syncFile.bind(_this3))));
|
|
80
|
-
} catch (e) {
|
|
81
|
-
return Promise.reject(e);
|
|
92
|
+
if (this.queue.length > 0) {
|
|
93
|
+
return;
|
|
82
94
|
}
|
|
83
|
-
|
|
95
|
+
this.queue.push(this._syncFile.bind(this));
|
|
96
|
+
}
|
|
97
|
+
// 同步文件备份
|
|
98
|
+
;
|
|
99
|
+
_proto.syncFile = function syncFile() {
|
|
100
|
+
writeFileSync(this.path, JSON.stringify(this._state));
|
|
101
|
+
}
|
|
102
|
+
// 异步文件备份
|
|
103
|
+
;
|
|
84
104
|
_proto._syncFile = function _syncFile() {
|
|
85
105
|
try {
|
|
86
106
|
var _this4 = this;
|
|
@@ -89,7 +109,7 @@ var Manager = /*#__PURE__*/function () {
|
|
|
89
109
|
if (err) {
|
|
90
110
|
reject(err);
|
|
91
111
|
} else {
|
|
92
|
-
resolve
|
|
112
|
+
setTimeout(resolve, 100);
|
|
93
113
|
}
|
|
94
114
|
});
|
|
95
115
|
}));
|
|
@@ -124,18 +144,22 @@ var Manager = /*#__PURE__*/function () {
|
|
|
124
144
|
try {
|
|
125
145
|
var _this6 = this;
|
|
126
146
|
_this6._state = values;
|
|
127
|
-
|
|
147
|
+
_this6.sync();
|
|
148
|
+
return _await();
|
|
128
149
|
} catch (e) {
|
|
129
150
|
return Promise.reject(e);
|
|
130
151
|
}
|
|
131
152
|
};
|
|
132
153
|
return Manager;
|
|
133
154
|
}(); // work 线程的实现
|
|
134
|
-
function
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
return result.then(_empty);
|
|
155
|
+
function _await(value, then, direct) {
|
|
156
|
+
if (direct) {
|
|
157
|
+
return then ? then(value) : value;
|
|
138
158
|
}
|
|
159
|
+
if (!value || !value.then) {
|
|
160
|
+
value = Promise.resolve(value);
|
|
161
|
+
}
|
|
162
|
+
return then ? value.then(then) : value;
|
|
139
163
|
}
|
|
140
164
|
var Worker = /*#__PURE__*/function () {
|
|
141
165
|
function Worker(path) {
|
|
@@ -165,7 +189,7 @@ var Worker = /*#__PURE__*/function () {
|
|
|
165
189
|
_this7.callback[uid] = function (data) {
|
|
166
190
|
return resolve(data);
|
|
167
191
|
};
|
|
168
|
-
|
|
192
|
+
//@ts-ignore
|
|
169
193
|
process.send({
|
|
170
194
|
uid: uid,
|
|
171
195
|
type: type,
|
|
@@ -203,6 +227,12 @@ var Worker = /*#__PURE__*/function () {
|
|
|
203
227
|
};
|
|
204
228
|
return Worker;
|
|
205
229
|
}();
|
|
230
|
+
function _invokeIgnored(body) {
|
|
231
|
+
var result = body();
|
|
232
|
+
if (result && result.then) {
|
|
233
|
+
return result.then(_empty);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
206
236
|
function _invoke(body, then) {
|
|
207
237
|
var result = body();
|
|
208
238
|
if (result && result.then) {
|
|
@@ -210,11 +240,6 @@ function _invoke(body, then) {
|
|
|
210
240
|
}
|
|
211
241
|
return then(result);
|
|
212
242
|
}
|
|
213
|
-
function _awaitIgnored(value, direct) {
|
|
214
|
-
if (!direct) {
|
|
215
|
-
return value && value.then ? value.then(_empty) : Promise.resolve();
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
243
|
export var JsonStorage = /*#__PURE__*/function () {
|
|
219
244
|
function JsonStorage(path, options) {
|
|
220
245
|
var _options;
|
|
@@ -222,6 +247,7 @@ export var JsonStorage = /*#__PURE__*/function () {
|
|
|
222
247
|
options = {};
|
|
223
248
|
}
|
|
224
249
|
this.instance = void 0;
|
|
250
|
+
this.queue = new SyncQueue();
|
|
225
251
|
if (cluster.isPrimary) {
|
|
226
252
|
this.instance = new Manager(path, options.defaultState);
|
|
227
253
|
} else {
|
|
@@ -235,7 +261,7 @@ export var JsonStorage = /*#__PURE__*/function () {
|
|
|
235
261
|
_proto3.getState = function getState() {
|
|
236
262
|
try {
|
|
237
263
|
var _this10 = this;
|
|
238
|
-
return _await(_this10.instance.getState());
|
|
264
|
+
return _await(_this10.queue.push(_this10.instance.getState.bind(_this10.instance)));
|
|
239
265
|
} catch (e) {
|
|
240
266
|
return Promise.reject(e);
|
|
241
267
|
}
|
|
@@ -243,7 +269,9 @@ export var JsonStorage = /*#__PURE__*/function () {
|
|
|
243
269
|
_proto3.setState = function setState(state) {
|
|
244
270
|
try {
|
|
245
271
|
var _this11 = this;
|
|
246
|
-
return _await(_this11.
|
|
272
|
+
return _await(_this11.queue.push(function () {
|
|
273
|
+
return _this11.instance.setState(state);
|
|
274
|
+
}));
|
|
247
275
|
} catch (e) {
|
|
248
276
|
return Promise.reject(e);
|
|
249
277
|
}
|
|
@@ -75,8 +75,8 @@ const getProcessTree = async (pid, tree) => {
|
|
|
75
75
|
isRoot = true;
|
|
76
76
|
tree = { pid, children: [] };
|
|
77
77
|
}
|
|
78
|
-
const
|
|
79
|
-
for (const id of
|
|
78
|
+
const pidList = await getProcessByPpid(pid);
|
|
79
|
+
for (const id of pidList) {
|
|
80
80
|
const item = { pid: id, children: [] };
|
|
81
81
|
await (0, exports.getProcessTree)(id, item);
|
|
82
82
|
tree.children.push(item);
|
|
@@ -86,6 +86,7 @@ const getProcessTree = async (pid, tree) => {
|
|
|
86
86
|
}
|
|
87
87
|
else {
|
|
88
88
|
tree = null;
|
|
89
|
+
return null;
|
|
89
90
|
}
|
|
90
91
|
};
|
|
91
92
|
exports.getProcessTree = getProcessTree;
|