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.
Files changed (45) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +29 -32
  3. package/app.js +131 -143
  4. package/bin/argv.js +63 -70
  5. package/bin/cli.js +57 -43
  6. package/bin/configurer.js +60 -62
  7. package/bin/loader.mjs +392 -279
  8. package/bin/proxy-import.js +12 -0
  9. package/bin/proxy-require.js +88 -0
  10. package/bin/register.js +46 -55
  11. package/bin/starter.js +63 -66
  12. package/index.js +155 -168
  13. package/index.mjs +4 -4
  14. package/logger.js +25 -40
  15. package/modules/http/index.js +234 -192
  16. package/modules/http/utils.js +74 -73
  17. package/modules/index.js +86 -88
  18. package/modules/module.js +11 -16
  19. package/modules/socketio/client.js +97 -101
  20. package/modules/socketio/index.js +171 -168
  21. package/modules/socketio/server.js +188 -136
  22. package/modules/socketio/socket.js +1 -4
  23. package/package.json +14 -12
  24. package/types/app.d.ts +50 -51
  25. package/types/bin/argv.d.ts +8 -8
  26. package/types/bin/cli.d.ts +6 -2
  27. package/types/bin/configurer.d.ts +3 -1
  28. package/types/bin/proxy-import.d.ts +4 -0
  29. package/types/bin/proxy-require.d.ts +5 -0
  30. package/types/bin/register.d.ts +1 -1
  31. package/types/bin/starter.d.ts +5 -1
  32. package/types/index.d.ts +78 -76
  33. package/types/logger.d.ts +5 -4
  34. package/types/modules/http/index.d.ts +58 -47
  35. package/types/modules/http/utils.d.ts +14 -17
  36. package/types/modules/index.d.ts +24 -24
  37. package/types/modules/module.d.ts +11 -13
  38. package/types/modules/socketio/client.d.ts +23 -23
  39. package/types/modules/socketio/index.d.ts +37 -37
  40. package/types/modules/socketio/server.d.ts +44 -35
  41. package/types/modules/socketio/socket.d.ts +11 -11
  42. package/types/utils.d.ts +6 -6
  43. package/utils.js +57 -63
  44. package/bin/proxyer.js +0 -61
  45. package/types/bin/proxyer.d.ts +0 -1
package/modules/index.js CHANGED
@@ -1,88 +1,86 @@
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
+ 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
- "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
+ 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
- "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
+ 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
+ }