oox 0.3.0-beta11 → 0.3.0-beta13

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