oox 0.3.0-beta9 → 0.3.2

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 (47) 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 +286 -192
  16. package/modules/http/router.js +205 -0
  17. package/modules/http/utils.js +75 -73
  18. package/modules/index.js +86 -88
  19. package/modules/module.js +11 -16
  20. package/modules/socketio/client.js +97 -101
  21. package/modules/socketio/index.js +171 -168
  22. package/modules/socketio/server.js +188 -136
  23. package/modules/socketio/socket.js +1 -4
  24. package/package.json +14 -12
  25. package/types/app.d.ts +50 -51
  26. package/types/bin/argv.d.ts +8 -8
  27. package/types/bin/cli.d.ts +6 -2
  28. package/types/bin/configurer.d.ts +3 -1
  29. package/types/bin/proxy-import.d.ts +4 -0
  30. package/types/bin/proxy-require.d.ts +5 -0
  31. package/types/bin/register.d.ts +1 -1
  32. package/types/bin/starter.d.ts +5 -1
  33. package/types/index.d.ts +78 -76
  34. package/types/logger.d.ts +5 -4
  35. package/types/modules/http/index.d.ts +65 -47
  36. package/types/modules/http/router.d.ts +49 -0
  37. package/types/modules/http/utils.d.ts +14 -17
  38. package/types/modules/index.d.ts +24 -24
  39. package/types/modules/module.d.ts +11 -13
  40. package/types/modules/socketio/client.d.ts +23 -23
  41. package/types/modules/socketio/index.d.ts +37 -37
  42. package/types/modules/socketio/server.d.ts +44 -35
  43. package/types/modules/socketio/socket.d.ts +11 -11
  44. package/types/utils.d.ts +6 -6
  45. package/utils.js +57 -63
  46. package/bin/proxyer.js +0 -61
  47. package/types/bin/proxyer.d.ts +0 -1
@@ -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
+ }
@@ -1,168 +1,171 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.sockets = void 0;
4
- const path = require("node:path");
5
- const client_1 = require("./client");
6
- const oox = require("../../index");
7
- const index_1 = require("../../index");
8
- const socket_1 = require("./socket");
9
- Object.defineProperty(exports, "sockets", { enumerable: true, get: function () { return socket_1.sockets; } });
10
- class SocketIOModule extends client_1.default {
11
- sockets = socket_1.sockets;
12
- async serve() {
13
- await this.stop();
14
- const _http = oox.modules.builtins.http;
15
- const httpConfig = _http.getConfig(), config = this.getConfig();
16
- let isShareServer = false;
17
- // 都没设置端口
18
- isShareServer = !httpConfig.port && !config.port;
19
- // 都设置相同端口
20
- isShareServer = isShareServer || httpConfig.port === config.port;
21
- // http 模块未被禁用
22
- isShareServer = isShareServer && !httpConfig.disabled;
23
- if (isShareServer) {
24
- config.path = path.posix.join(httpConfig.path, config.path);
25
- this.server = _http.server;
26
- }
27
- await super.serve();
28
- }
29
- onSyncConnection(socket) {
30
- const mSockets = Array.from(socket_1.sockets.values())
31
- .filter(s => s !== socket &&
32
- s.data.name !== socket.data.name &&
33
- s.data.id.startsWith('ws://'));
34
- return mSockets.map(s => s.data);
35
- }
36
- serverOnDisconnect(socket, reason) {
37
- super.serverOnDisconnect(socket, reason);
38
- (0, index_1.removeKeepAliveConnection)(socket.data.name, socket.data.id);
39
- }
40
- clientOnDisconnect(socket, reason) {
41
- super.clientOnDisconnect(socket, reason);
42
- (0, index_1.removeKeepAliveConnection)(socket.data.name, socket.data.id);
43
- }
44
- /**
45
- *
46
- * @param socket 是由哪个通道发送过来的
47
- * @param connectionDatas
48
- */
49
- clientOnSyncConnection(socket, connectionDatas) {
50
- for (const data of connectionDatas)
51
- if (!socket_1.sockets.has(data.id))
52
- this.connect(data.id).catch((error) => console.error(error));
53
- }
54
- onFetchActions(socket, search) {
55
- const data = [];
56
- for (const key of oox.kvMethods.keys())
57
- if (!key.endsWith('_proxy') && key.includes(search))
58
- data.push(key);
59
- return data;
60
- }
61
- fetchActions(id, search = '') {
62
- let socket = socket_1.sockets.get(id);
63
- if (!socket) {
64
- const connections = (0, index_1.getKeepAliveConnections)(id);
65
- if (!connections || !connections.size)
66
- throw new Error(`Unknown service identify<${id}>`);
67
- id = connections.keys().next().value;
68
- socket = socket_1.sockets.get(id);
69
- }
70
- if (!socket)
71
- throw new Error(`Unknown service identify<${id}>`);
72
- return this.emit(socket.data.id, 'fetchActions', [search]);
73
- }
74
- onConnection(socket) {
75
- const { id, name, host } = socket.data;
76
- const connection = new index_1.RPCKeepAliveConnection(this, id, socket.data);
77
- (0, index_1.addKeepAliveConnection)(connection);
78
- const connectionContext = {
79
- sourceIP: '',
80
- ip: host,
81
- caller: name,
82
- callerId: id,
83
- connection
84
- };
85
- socket.on('fetchActions', async (search, fn) => {
86
- if ('function' !== typeof fn)
87
- return;
88
- const data = await this.onFetchActions(socket, search);
89
- fn(data);
90
- });
91
- socket.on('call', async (action, params, context, callback) => {
92
- if ('object' !== typeof context)
93
- context = oox.genContext(connectionContext);
94
- else
95
- context = oox.genContext(Object.assign(context, connectionContext));
96
- this.call(action, params, context, callback);
97
- });
98
- }
99
- /**
100
- *
101
- * @param {Socket} socket
102
- */
103
- serverOnConnection(socket) {
104
- super.serverOnConnection(socket);
105
- socket.setMaxListeners(0);
106
- socket.on('syncConnection', async (fn) => {
107
- if ('function' !== typeof fn)
108
- return;
109
- const data = this.onSyncConnection(socket);
110
- fn(data);
111
- });
112
- this.onConnection(socket);
113
- }
114
- async call(action, params, context, callback) {
115
- const returns = await oox.call(action, params, context);
116
- 'function' === typeof callback && callback(returns);
117
- return returns;
118
- }
119
- clientOnConnection(socket) {
120
- super.clientOnConnection(socket);
121
- socket.emit('syncConnection', (socketDatas) => this.clientOnSyncConnection(socket, socketDatas));
122
- this.onConnection(socket);
123
- }
124
- /**
125
- * socketio emit
126
- */
127
- async emit(url, action, params) {
128
- let socket = null;
129
- try {
130
- socket = await this.connect(url);
131
- }
132
- catch (error) {
133
- // try again
134
- socket = await this.connect(url);
135
- }
136
- try {
137
- return await new Promise((resolve, reject) => {
138
- const onError = (reason) => {
139
- const message = 'string' === typeof reason ? reason : reason instanceof Error ? reason.message : 'connect error';
140
- reject(new Error(message));
141
- };
142
- // RPC 执行时中断连接
143
- socket.once('disconnect', onError);
144
- socket.emit(action, ...params, (returns) => {
145
- socket.off('disconnect', onError);
146
- resolve(returns);
147
- });
148
- });
149
- }
150
- catch (error) {
151
- throw new Error(error.message);
152
- }
153
- }
154
- /**
155
- * RPC
156
- */
157
- async rpc(url, action, params, context) {
158
- if (!context || !context.traceId) {
159
- context = oox.getContext();
160
- }
161
- const { error, body } = await this.emit(url, 'call', [action, params, context]);
162
- if (error)
163
- throw new Error(error.message);
164
- else
165
- return body;
166
- }
167
- }
168
- exports.default = SocketIOModule;
1
+ import * as path from 'node:path';
2
+ import SocketIOClient from './client.js';
3
+ import * as oox from '../../index.js';
4
+ import { RPCKeepAliveConnection, removeKeepAliveConnection, addKeepAliveConnection, getKeepAliveConnections } from '../../index.js';
5
+ import { sockets } from './socket.js';
6
+ export { sockets };
7
+ export default class SocketIOModule extends SocketIOClient {
8
+ sockets = sockets;
9
+ async serve() {
10
+ await this.stop();
11
+ const _http = oox.modules.builtins.http;
12
+ const httpConfig = _http.getConfig(), config = this.getConfig();
13
+ let isShareServer = false;
14
+ // 都没设置端口
15
+ isShareServer = !httpConfig.port && !config.port;
16
+ // 都设置相同端口
17
+ isShareServer ||= httpConfig.port === config.port;
18
+ // http 模块未被禁用
19
+ isShareServer &&= httpConfig.enabled;
20
+ if (isShareServer) {
21
+ config.path = path.posix.join(httpConfig.path, config.path);
22
+ this.server = _http.server;
23
+ this.config.ssl = _http.config.ssl;
24
+ }
25
+ await super.serve();
26
+ }
27
+ onSyncConnection(socket) {
28
+ const mSockets = Array.from(sockets.values())
29
+ .filter(s => s !== socket &&
30
+ s.data.name !== socket.data.name &&
31
+ /^wss?:\/\/.+$/.test(s.data.id));
32
+ return mSockets.map(s => s.data);
33
+ }
34
+ serverOnDisconnect(socket, reason) {
35
+ super.serverOnDisconnect(socket, reason);
36
+ removeKeepAliveConnection(socket.data.name, socket.data.id);
37
+ }
38
+ clientOnDisconnect(socket, reason) {
39
+ super.clientOnDisconnect(socket, reason);
40
+ removeKeepAliveConnection(socket.data.name, socket.data.id);
41
+ }
42
+ /**
43
+ *
44
+ * @param socket 是由哪个通道发送过来的
45
+ * @param connectionDatas
46
+ */
47
+ clientOnSyncConnection(socket, connectionDatas) {
48
+ for (const data of connectionDatas)
49
+ if (!sockets.has(data.id))
50
+ this.connect(data.id).catch((error) => console.error(error));
51
+ }
52
+ onFetchActions(socket, search) {
53
+ const data = [];
54
+ for (const key of oox.kvMethods.keys())
55
+ if (!key.endsWith('_proxy') && key.includes(search))
56
+ data.push(key);
57
+ return data;
58
+ }
59
+ fetchActions(id, search = '') {
60
+ let socket = sockets.get(id);
61
+ if (!socket) {
62
+ const connections = getKeepAliveConnections(id);
63
+ if (!connections || !connections.size)
64
+ throw new Error(`Unknown service identify<${id}>`);
65
+ id = connections.keys().next().value;
66
+ socket = sockets.get(id);
67
+ }
68
+ if (!socket)
69
+ throw new Error(`Unknown service identify<${id}>`);
70
+ return this.emit(socket.data.id, 'fetchActions', [search]);
71
+ }
72
+ onConnection(socket) {
73
+ const { id, name, host } = socket.data;
74
+ const connection = new RPCKeepAliveConnection(this, id, socket.data);
75
+ addKeepAliveConnection(connection);
76
+ const connectionContext = {
77
+ sourceIP: '',
78
+ ip: host,
79
+ caller: name,
80
+ callerId: id,
81
+ connection
82
+ };
83
+ socket.on('fetchActions', async (search, fn) => {
84
+ if ('function' !== typeof fn)
85
+ return;
86
+ const data = await this.onFetchActions(socket, search);
87
+ fn(data);
88
+ });
89
+ socket.on('call', async (action, params, context, callback) => {
90
+ if ('object' !== typeof context)
91
+ context = oox.genContext(connectionContext);
92
+ else
93
+ context = oox.genContext(Object.assign(context, connectionContext));
94
+ this.call(action, params, context, callback);
95
+ });
96
+ }
97
+ /**
98
+ *
99
+ * @param {Socket} socket
100
+ */
101
+ serverOnConnection(socket) {
102
+ super.serverOnConnection(socket);
103
+ socket.setMaxListeners(0);
104
+ socket.on('syncConnection', async (fn) => {
105
+ if ('function' !== typeof fn)
106
+ return;
107
+ const data = this.onSyncConnection(socket);
108
+ fn(data);
109
+ });
110
+ this.onConnection(socket);
111
+ }
112
+ async call(action, params, context, callback) {
113
+ const returns = await oox.call(action, params, context);
114
+ if (!oox.config.errorStack && returns.error) {
115
+ // 不返回错误调用栈信息
116
+ delete returns.error.stack;
117
+ }
118
+ 'function' === typeof callback && callback(returns);
119
+ return returns;
120
+ }
121
+ clientOnConnection(socket) {
122
+ super.clientOnConnection(socket);
123
+ socket.emit('syncConnection', (socketDatas) => this.clientOnSyncConnection(socket, socketDatas));
124
+ this.onConnection(socket);
125
+ }
126
+ /**
127
+ * socketio emit
128
+ */
129
+ async emit(url, event, params) {
130
+ let socket = null;
131
+ try {
132
+ socket = await this.connect(url);
133
+ }
134
+ catch (error) {
135
+ // try again
136
+ socket = await this.connect(url);
137
+ }
138
+ try {
139
+ return await new Promise((resolve, reject) => {
140
+ const onError = (reason) => {
141
+ const message = 'string' === typeof reason ? reason : reason instanceof Error ? reason.message : 'connect error';
142
+ reject(new Error(message));
143
+ };
144
+ // RPC 执行时中断连接
145
+ socket.once('disconnect', onError);
146
+ socket.emit(event, ...params, (returns) => {
147
+ socket.off('disconnect', onError);
148
+ resolve(returns);
149
+ });
150
+ });
151
+ }
152
+ catch (error) {
153
+ throw new Error(error.message);
154
+ }
155
+ }
156
+ /**
157
+ * RPC
158
+ */
159
+ async rpc(url, action, params, context) {
160
+ if (!context || !context.traceId) {
161
+ context = oox.getContext();
162
+ }
163
+ const { success, error, body } = await this.emit(url, 'call', [action, params, context]);
164
+ if (success)
165
+ return body;
166
+ else if (error)
167
+ throw new Error(error.message);
168
+ else
169
+ throw new Error('[RPC] Unknown Error');
170
+ }
171
+ }