oox 0.3.0-beta9 → 0.3.1
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/LICENSE +21 -21
- package/README.md +29 -32
- package/app.js +131 -143
- package/bin/argv.js +63 -70
- package/bin/cli.js +57 -43
- package/bin/configurer.js +60 -62
- package/bin/loader.mjs +392 -279
- package/bin/proxy-import.js +12 -0
- package/bin/proxy-require.js +88 -0
- package/bin/register.js +46 -55
- package/bin/starter.js +63 -66
- package/index.js +155 -168
- package/index.mjs +4 -4
- package/logger.js +25 -40
- package/modules/http/index.js +234 -192
- package/modules/http/utils.js +74 -73
- package/modules/index.js +86 -88
- package/modules/module.js +11 -16
- package/modules/socketio/client.js +97 -101
- package/modules/socketio/index.js +171 -168
- package/modules/socketio/server.js +188 -136
- package/modules/socketio/socket.js +1 -4
- package/package.json +14 -12
- package/types/app.d.ts +50 -51
- package/types/bin/argv.d.ts +8 -8
- package/types/bin/cli.d.ts +6 -2
- package/types/bin/configurer.d.ts +3 -1
- package/types/bin/proxy-import.d.ts +4 -0
- package/types/bin/proxy-require.d.ts +5 -0
- package/types/bin/register.d.ts +1 -1
- package/types/bin/starter.d.ts +5 -1
- package/types/index.d.ts +78 -76
- package/types/logger.d.ts +5 -4
- package/types/modules/http/index.d.ts +58 -47
- package/types/modules/http/utils.d.ts +14 -17
- package/types/modules/index.d.ts +24 -24
- package/types/modules/module.d.ts +11 -13
- package/types/modules/socketio/client.d.ts +23 -23
- package/types/modules/socketio/index.d.ts +37 -37
- package/types/modules/socketio/server.d.ts +44 -35
- package/types/modules/socketio/socket.d.ts +11 -11
- package/types/utils.d.ts +6 -6
- package/utils.js +57 -63
- package/bin/proxyer.js +0 -61
- package/types/bin/proxyer.d.ts +0 -1
package/modules/index.js
CHANGED
|
@@ -1,88 +1,86 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
if (
|
|
33
|
-
throw new Error(`Module<${module.name}> has
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
this
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
}
|
|
88
|
-
exports.default = Modules;
|
|
1
|
+
import Module from './module.js';
|
|
2
|
+
import HTTP from './http/index.js';
|
|
3
|
+
import SocketIO from './socketio/index.js';
|
|
4
|
+
export default class Modules extends Module {
|
|
5
|
+
/**
|
|
6
|
+
* the module unique name
|
|
7
|
+
*/
|
|
8
|
+
name = 'oox:modules';
|
|
9
|
+
/**
|
|
10
|
+
* FIFO queue for modules starting
|
|
11
|
+
*/
|
|
12
|
+
#queue = [];
|
|
13
|
+
/**
|
|
14
|
+
* all modules map
|
|
15
|
+
*/
|
|
16
|
+
#map = new Map();
|
|
17
|
+
/**
|
|
18
|
+
* all builtin modules
|
|
19
|
+
*/
|
|
20
|
+
builtins = {
|
|
21
|
+
http: new HTTP,
|
|
22
|
+
socketio: new SocketIO,
|
|
23
|
+
};
|
|
24
|
+
constructor() {
|
|
25
|
+
super();
|
|
26
|
+
this.add(this.builtins.http);
|
|
27
|
+
this.add(this.builtins.socketio);
|
|
28
|
+
}
|
|
29
|
+
add(module) {
|
|
30
|
+
if (!module.name || 'string' !== typeof module.name)
|
|
31
|
+
throw new Error(`Module<${module.name}> has noname`);
|
|
32
|
+
if (this.#map.has(module.name))
|
|
33
|
+
throw new Error(`Module<${module.name}> has exists`);
|
|
34
|
+
this.#map.set(module.name, module);
|
|
35
|
+
this.#queue.push(module);
|
|
36
|
+
return this;
|
|
37
|
+
}
|
|
38
|
+
get(name) {
|
|
39
|
+
return this.#map.get(name);
|
|
40
|
+
}
|
|
41
|
+
async remove(name) {
|
|
42
|
+
const module = this.get(name);
|
|
43
|
+
if (!module)
|
|
44
|
+
return;
|
|
45
|
+
await module.stop();
|
|
46
|
+
const index = this.#queue.indexOf(module);
|
|
47
|
+
this.#queue.splice(index, 1);
|
|
48
|
+
this.#map.delete(name);
|
|
49
|
+
}
|
|
50
|
+
setConfig(config) {
|
|
51
|
+
for (const module of this.#queue) {
|
|
52
|
+
if (module.name in config) {
|
|
53
|
+
const moduleConfig = config[module.name];
|
|
54
|
+
if ('boolean' === typeof moduleConfig) {
|
|
55
|
+
// eg: http=yes/no
|
|
56
|
+
module.setConfig({ enabled: moduleConfig });
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
module.setConfig(moduleConfig);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
module.setConfig({ enabled: true });
|
|
64
|
+
}
|
|
65
|
+
config[module.name] = module.getConfig();
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
async serve() {
|
|
69
|
+
try {
|
|
70
|
+
for (const module of this.#queue) {
|
|
71
|
+
if (module.getConfig().enabled) {
|
|
72
|
+
await module.serve();
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
catch (error) {
|
|
77
|
+
await this.stop();
|
|
78
|
+
throw error;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
async stop() {
|
|
82
|
+
for (const module of this.#queue) {
|
|
83
|
+
await module.stop();
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
package/modules/module.js
CHANGED
|
@@ -1,16 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
class
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
getConfig() { return this.config; }
|
|
13
|
-
async serve() { }
|
|
14
|
-
async stop() { }
|
|
15
|
-
}
|
|
16
|
-
exports.default = Module;
|
|
1
|
+
export class ModuleConfig {
|
|
2
|
+
enabled = true;
|
|
3
|
+
}
|
|
4
|
+
export default class Module {
|
|
5
|
+
config;
|
|
6
|
+
name;
|
|
7
|
+
setConfig(config) { }
|
|
8
|
+
getConfig() { return this.config; }
|
|
9
|
+
async serve() { }
|
|
10
|
+
async stop() { }
|
|
11
|
+
}
|
|
@@ -1,101 +1,97 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
socket
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
socket.once('
|
|
84
|
-
socket.once('
|
|
85
|
-
socket.once('
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
this.clientOnSocketConnection(socket);
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
exports.default = SocketIOCore;
|
|
1
|
+
import * as SocketIOClient from 'socket.io-client';
|
|
2
|
+
import { sockets } from './socket.js';
|
|
3
|
+
import SocketIOServer from './server.js';
|
|
4
|
+
import * as oox from '../../index.js';
|
|
5
|
+
export default class SocketIOCore extends SocketIOServer {
|
|
6
|
+
/**
|
|
7
|
+
* connect to <SocketIO RPC> service
|
|
8
|
+
*/
|
|
9
|
+
async connect(url) {
|
|
10
|
+
let socket = sockets.get(url);
|
|
11
|
+
// 已经连接的直接返回
|
|
12
|
+
if (socket) {
|
|
13
|
+
try {
|
|
14
|
+
await this.clientWaitConnection(socket);
|
|
15
|
+
}
|
|
16
|
+
catch (error) {
|
|
17
|
+
this.clientOnSocketDisconnect(socket, error.message);
|
|
18
|
+
throw error;
|
|
19
|
+
}
|
|
20
|
+
return socket;
|
|
21
|
+
}
|
|
22
|
+
const headers = {
|
|
23
|
+
'x-caller': oox.config.name
|
|
24
|
+
};
|
|
25
|
+
const { host } = oox.config;
|
|
26
|
+
headers['x-ip'] = host;
|
|
27
|
+
headers['x-caller-id'] = this.getUrl();
|
|
28
|
+
// create socket handler
|
|
29
|
+
const mURL = new URL(url);
|
|
30
|
+
socket = SocketIOClient.io(mURL.origin, {
|
|
31
|
+
extraHeaders: headers,
|
|
32
|
+
path: mURL.pathname
|
|
33
|
+
});
|
|
34
|
+
socket.data = { name: 'anonymous', connected: false, id: url, host: mURL.host };
|
|
35
|
+
sockets.set(url, socket);
|
|
36
|
+
try {
|
|
37
|
+
await this.clientWaitConnection(socket);
|
|
38
|
+
}
|
|
39
|
+
catch (error) {
|
|
40
|
+
this.clientOnSocketDisconnect(socket, error);
|
|
41
|
+
throw error;
|
|
42
|
+
}
|
|
43
|
+
return socket;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* 客户端Socket连接事件
|
|
47
|
+
*/
|
|
48
|
+
clientOnSocketConnection(socket) {
|
|
49
|
+
socket.data.connected = true;
|
|
50
|
+
socket.once('disconnect', reason => this.clientOnSocketDisconnect(socket, reason));
|
|
51
|
+
this.clientOnConnection(socket);
|
|
52
|
+
}
|
|
53
|
+
clientOnDisconnect(socket, reason) { }
|
|
54
|
+
clientOnConnection(socket) { }
|
|
55
|
+
/**
|
|
56
|
+
* 客户端Socket断开事件
|
|
57
|
+
* @param {Socket} socket
|
|
58
|
+
*/
|
|
59
|
+
clientOnSocketDisconnect(socket, reason) {
|
|
60
|
+
socket.data.connected = false;
|
|
61
|
+
socket.disconnect();
|
|
62
|
+
sockets.delete(socket.data.id);
|
|
63
|
+
this.clientOnDisconnect(socket, reason);
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* 等待socket连接
|
|
67
|
+
*/
|
|
68
|
+
async clientWaitConnection(socket) {
|
|
69
|
+
if (socket.data.connected)
|
|
70
|
+
return;
|
|
71
|
+
if (socket.connect)
|
|
72
|
+
socket.connect();
|
|
73
|
+
try {
|
|
74
|
+
await new Promise((resolve, reject) => {
|
|
75
|
+
const onError = (reason) => {
|
|
76
|
+
socket.offAny(onError);
|
|
77
|
+
const message = 'string' === typeof reason ? reason : reason instanceof Error ? reason.message : 'connect error';
|
|
78
|
+
reject(new Error(message));
|
|
79
|
+
};
|
|
80
|
+
socket.once('disconnect', onError);
|
|
81
|
+
socket.once('connect_error', onError);
|
|
82
|
+
socket.once('connect_timeout', onError);
|
|
83
|
+
socket.once('reconnect_error', onError);
|
|
84
|
+
socket.once('reconnect_failed', onError);
|
|
85
|
+
socket.once('oox_connected', ({ name }) => {
|
|
86
|
+
socket.offAny(onError);
|
|
87
|
+
socket.data.name = name;
|
|
88
|
+
resolve();
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
catch (error) {
|
|
93
|
+
throw new Error(error.message);
|
|
94
|
+
}
|
|
95
|
+
this.clientOnSocketConnection(socket);
|
|
96
|
+
}
|
|
97
|
+
}
|