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
@@ -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
+ }