oox 0.3.0-beta1 → 0.3.0-beta10
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/app.js +15 -14
- package/bin/cli.js +42 -36
- package/bin/configurer.js +24 -9
- package/bin/loader.mjs +449 -0
- package/bin/proxyer.js +15 -10
- package/bin/register.js +3 -2
- package/bin/starter.js +26 -32
- package/index.js +35 -16
- package/index.mjs +1 -0
- package/logger.js +40 -0
- package/modules/http/index.js +15 -3
- package/modules/index.js +20 -20
- package/modules/socketio/index.js +20 -0
- package/modules/socketio/server.js +13 -7
- package/package.json +10 -5
- package/types/app.d.ts +14 -0
- package/types/bin/cli.d.ts +3 -1
- package/types/bin/configurer.d.ts +3 -1
- package/types/bin/starter.d.ts +3 -1
- package/types/index.d.ts +9 -3
- package/types/logger.d.ts +4 -0
- package/types/modules/http/utils.d.ts +2 -2
- package/types/modules/index.d.ts +1 -0
- package/types/modules/module.d.ts +2 -0
- package/types/modules/socketio/index.d.ts +3 -1
- package/types/modules/socketio/server.d.ts +2 -2
package/bin/register.js
CHANGED
|
@@ -20,12 +20,13 @@ function urlFormatter(url) {
|
|
|
20
20
|
return url;
|
|
21
21
|
}
|
|
22
22
|
async function connect(url, prevError = null) {
|
|
23
|
+
const socketio = oox.modules.get('socketio');
|
|
23
24
|
const { host } = oox.config;
|
|
24
|
-
const { port, path } =
|
|
25
|
+
const { port, path } = socketio.config;
|
|
25
26
|
if (`ws://${host}:${port}${path}` === url)
|
|
26
27
|
return;
|
|
27
28
|
try {
|
|
28
|
-
const socket = await
|
|
29
|
+
const socket = await socketio.connect(url);
|
|
29
30
|
onConnection(socket, url);
|
|
30
31
|
}
|
|
31
32
|
catch (error) {
|
package/bin/starter.js
CHANGED
|
@@ -7,9 +7,12 @@ const oox = require("../index");
|
|
|
7
7
|
const proxyer_1 = require("./proxyer");
|
|
8
8
|
const configurer_1 = require("./configurer");
|
|
9
9
|
const register_1 = require("./register");
|
|
10
|
-
function getEntryFile(env) {
|
|
11
|
-
const
|
|
12
|
-
|
|
10
|
+
function getEntryFile(env, entryFilename) {
|
|
11
|
+
const entryMatchRegExp = /(\w+)\.((\w?js)|(ts\w?))$/;
|
|
12
|
+
if (!entryFilename) {
|
|
13
|
+
const args = process.argv.slice(2);
|
|
14
|
+
entryFilename = args.find(arg => !arg.includes('=') && entryMatchRegExp.test(arg));
|
|
15
|
+
}
|
|
13
16
|
if (!entryFilename)
|
|
14
17
|
throw new Error('Cannot find entry file');
|
|
15
18
|
const fullPath = path.resolve(entryFilename);
|
|
@@ -17,20 +20,27 @@ function getEntryFile(env) {
|
|
|
17
20
|
const fullDirectory = path.dirname(fullPath);
|
|
18
21
|
const directory = fullDirectory.split(path.sep).pop();
|
|
19
22
|
const groupFullDirectory = env.group ? path.resolve(env.group) : '';
|
|
20
|
-
|
|
23
|
+
const entryMatch = filename.match(entryMatchRegExp);
|
|
24
|
+
const entryFilenameWithoutExtension = entryMatch[1];
|
|
25
|
+
var name = entryFilenameWithoutExtension === 'index' && groupFullDirectory !== fullDirectory ? directory : entryFilenameWithoutExtension;
|
|
21
26
|
return { name, path: fullPath, group: groupFullDirectory };
|
|
22
27
|
}
|
|
23
|
-
function loadEntry(name, entryPath) {
|
|
28
|
+
async function loadEntry(name, entryPath) {
|
|
24
29
|
oox.config.name = name;
|
|
25
|
-
|
|
26
|
-
|
|
30
|
+
// Typescript 4.7.3, not supported import() expression
|
|
31
|
+
const methods = await eval(`import('file://${entryPath.replace(/\\/g, '/')}')`);
|
|
32
|
+
oox.setMethods(methods.default || methods);
|
|
27
33
|
}
|
|
28
|
-
async function startup() {
|
|
34
|
+
async function startup(env, entryFilename) {
|
|
29
35
|
// 加载环境变量
|
|
30
|
-
|
|
36
|
+
env = await (0, configurer_1.configure)(env);
|
|
31
37
|
Object.assign(oox.config, env);
|
|
32
38
|
// 获取服务入口地址
|
|
33
|
-
const entryFile = getEntryFile(env);
|
|
39
|
+
const entryFile = getEntryFile(env, entryFilename);
|
|
40
|
+
oox.config.entryFile = {
|
|
41
|
+
path: entryFile.path.replace(/\\/g, '/'),
|
|
42
|
+
group: entryFile.group.replace(/\\/g, '/'),
|
|
43
|
+
};
|
|
34
44
|
// 代理<服务间调用>
|
|
35
45
|
if (env.group) {
|
|
36
46
|
const excludes = [entryFile.name];
|
|
@@ -39,30 +49,14 @@ async function startup() {
|
|
|
39
49
|
(0, proxyer_1.proxyGroup)(entryFile.group, excludes);
|
|
40
50
|
}
|
|
41
51
|
// 加载服务
|
|
42
|
-
loadEntry(entryFile.name, entryFile.path);
|
|
43
|
-
//
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
const
|
|
47
|
-
if ('number' === typeof env.port) {
|
|
48
|
-
httpConfig.port = socketioConfig.port = env.port;
|
|
49
|
-
}
|
|
50
|
-
if (env.origin)
|
|
51
|
-
httpConfig.origin = env.origin;
|
|
52
|
-
if ('http' in env) {
|
|
53
|
-
if (env.http)
|
|
54
|
-
Object.assign(httpConfig, env.http);
|
|
55
|
-
else
|
|
56
|
-
httpConfig.disabled = true;
|
|
57
|
-
}
|
|
58
|
-
if ('socketio' in env) {
|
|
59
|
-
if (env.socketio)
|
|
60
|
-
Object.assign(socketioConfig, env.socketio);
|
|
61
|
-
else
|
|
62
|
-
socketioConfig.disabled = true;
|
|
63
|
-
}
|
|
52
|
+
await loadEntry(entryFile.name, entryFile.path);
|
|
53
|
+
// 模块配置
|
|
54
|
+
oox.modules.setConfig(oox.config);
|
|
55
|
+
oox.emit('app:configured');
|
|
56
|
+
const { http: { config: httpConfig }, socketio: { config: socketioConfig } } = oox.modules.builtins;
|
|
64
57
|
// 服务启动
|
|
65
58
|
await oox.serve();
|
|
59
|
+
oox.emit('app:served');
|
|
66
60
|
console.log();
|
|
67
61
|
console.log('Service', (0, chalk_1.bold) `${oox.config.name}`, 'running.');
|
|
68
62
|
if (!httpConfig.disabled)
|
package/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.rpc = exports.removeKeepAliveConnection = exports.addKeepAliveConnection = exports.getKeepAliveConnection = exports.getKeepAliveConnections = exports.keepAliveConnections = exports.RPCKeepAliveConnection = exports.stop = exports.serve = exports.getContext = exports.genContext = exports.genTraceId = exports.setGenTraceIdFunction = exports.
|
|
3
|
+
exports.rpc = exports.setLoadBalancePolicy = exports.removeKeepAliveConnection = exports.addKeepAliveConnection = exports.getKeepAliveConnection = exports.getKeepAliveConnections = exports.keepAliveConnections = exports.RPCKeepAliveConnection = exports.stop = exports.serve = exports.getContext = exports.genContext = exports.genTraceId = exports.setGenTraceIdFunction = exports.config = exports.Config = exports.Context = exports.emit = exports.off = exports.once = exports.on = exports.logger = exports.execute = exports.call = exports.sourceKVMethods = exports.kvMethods = exports.getMethods = exports.setMethods = exports.asyncStore = exports.modules = exports.ModuleConfig = exports.Module = void 0;
|
|
4
|
+
const node_crypto_1 = require("node:crypto");
|
|
4
5
|
const app = require("./app");
|
|
5
6
|
const utils_1 = require("./utils");
|
|
6
7
|
const module_1 = require("./modules/module");
|
|
@@ -8,7 +9,7 @@ exports.Module = module_1.default;
|
|
|
8
9
|
Object.defineProperty(exports, "ModuleConfig", { enumerable: true, get: function () { return module_1.ModuleConfig; } });
|
|
9
10
|
const modules_1 = require("./modules");
|
|
10
11
|
exports.modules = new modules_1.default;
|
|
11
|
-
exports.asyncStore = app.asyncStore, exports.setMethods = app.setMethods, exports.getMethods = app.getMethods, exports.kvMethods = app.kvMethods, exports.sourceKVMethods = app.sourceKVMethods, exports.call = app.call, exports.execute = app.execute, exports.on = app.on;
|
|
12
|
+
exports.asyncStore = app.asyncStore, exports.setMethods = app.setMethods, exports.getMethods = app.getMethods, exports.kvMethods = app.kvMethods, exports.sourceKVMethods = app.sourceKVMethods, exports.call = app.call, exports.execute = app.execute, exports.logger = app.logger, exports.on = app.on, exports.once = app.once, exports.off = app.off, exports.emit = app.emit;
|
|
12
13
|
class Context extends app.Context {
|
|
13
14
|
// 请求溯源IP
|
|
14
15
|
sourceIP = '';
|
|
@@ -23,28 +24,30 @@ class Context extends app.Context {
|
|
|
23
24
|
toJSON() {
|
|
24
25
|
const context = Object.assign({}, this);
|
|
25
26
|
delete context.connection;
|
|
26
|
-
return
|
|
27
|
+
return context;
|
|
27
28
|
}
|
|
28
29
|
}
|
|
29
30
|
exports.Context = Context;
|
|
30
31
|
class Config {
|
|
31
32
|
// 服务名称
|
|
32
33
|
name = 'local';
|
|
34
|
+
// 启动文件
|
|
35
|
+
entryFile = {
|
|
36
|
+
// 启动文件路径
|
|
37
|
+
path: '',
|
|
38
|
+
// 服务列表路径
|
|
39
|
+
group: '',
|
|
40
|
+
};
|
|
33
41
|
// 主机地址
|
|
34
42
|
host = (0, utils_1.getIPAddress)(4)[0];
|
|
43
|
+
// 默认监听端口
|
|
44
|
+
port = 0;
|
|
45
|
+
// 默认跨域设置
|
|
46
|
+
origin = '';
|
|
35
47
|
}
|
|
36
48
|
exports.Config = Config;
|
|
37
49
|
exports.config = new Config();
|
|
38
|
-
|
|
39
|
-
}
|
|
40
|
-
exports.getConfig = getConfig;
|
|
41
|
-
let genTraceIdFunction = () => {
|
|
42
|
-
const uid = [
|
|
43
|
-
Math.floor(Date.now() / 1000).toString(16),
|
|
44
|
-
Math.floor(Math.random() * 0xffffffff).toString(16).padStart(8, '0')
|
|
45
|
-
];
|
|
46
|
-
return uid.join('');
|
|
47
|
-
};
|
|
50
|
+
let genTraceIdFunction = () => (0, node_crypto_1.randomUUID)();
|
|
48
51
|
function setGenTraceIdFunction(fn) {
|
|
49
52
|
genTraceIdFunction = fn;
|
|
50
53
|
}
|
|
@@ -128,6 +131,23 @@ function removeKeepAliveConnection(name, id) {
|
|
|
128
131
|
}
|
|
129
132
|
}
|
|
130
133
|
exports.removeKeepAliveConnection = removeKeepAliveConnection;
|
|
134
|
+
/**
|
|
135
|
+
* random connection select for default load balance policy
|
|
136
|
+
* @param name service name
|
|
137
|
+
* @returns selected connection
|
|
138
|
+
*/
|
|
139
|
+
let loadBalancePolicy = (name) => {
|
|
140
|
+
const connections = exports.keepAliveConnections.get(name);
|
|
141
|
+
if (!connections || !connections.size)
|
|
142
|
+
return null;
|
|
143
|
+
const arrayConnections = Array.from(connections.values());
|
|
144
|
+
const index = Math.floor(Math.random() * arrayConnections.length);
|
|
145
|
+
return arrayConnections[index];
|
|
146
|
+
};
|
|
147
|
+
function setLoadBalancePolicy(policy) {
|
|
148
|
+
loadBalancePolicy = policy;
|
|
149
|
+
}
|
|
150
|
+
exports.setLoadBalancePolicy = setLoadBalancePolicy;
|
|
131
151
|
async function rpc(arg1, action, params, context) {
|
|
132
152
|
if (!context || !context.traceId) {
|
|
133
153
|
context = getContext();
|
|
@@ -137,10 +157,9 @@ async function rpc(arg1, action, params, context) {
|
|
|
137
157
|
connection = arg1;
|
|
138
158
|
}
|
|
139
159
|
else if ('string' === typeof arg1) {
|
|
140
|
-
|
|
141
|
-
if (!
|
|
160
|
+
connection = loadBalancePolicy(arg1);
|
|
161
|
+
if (!connection)
|
|
142
162
|
throw new Error(`Connection<${arg1}> not found`);
|
|
143
|
-
connection = connections.values().next().value;
|
|
144
163
|
}
|
|
145
164
|
else
|
|
146
165
|
throw new Error(`Unknown rpc arg1<${arg1}>`);
|
package/index.mjs
CHANGED
package/logger.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.trace = exports.error = exports.warn = exports.info = void 0;
|
|
4
|
+
const app_1 = require("./app");
|
|
5
|
+
function info(...msgs) {
|
|
6
|
+
const context = app_1.asyncStore.getStore();
|
|
7
|
+
if (context)
|
|
8
|
+
app_1.eventHub.emit('log', context, 'info', msgs);
|
|
9
|
+
else
|
|
10
|
+
console.info('[INFO]', ...msgs);
|
|
11
|
+
}
|
|
12
|
+
exports.info = info;
|
|
13
|
+
function warn(...msgs) {
|
|
14
|
+
const context = app_1.asyncStore.getStore();
|
|
15
|
+
if (context)
|
|
16
|
+
app_1.eventHub.emit('log', context, 'warn', msgs);
|
|
17
|
+
else
|
|
18
|
+
console.warn('[WARN]', ...msgs);
|
|
19
|
+
}
|
|
20
|
+
exports.warn = warn;
|
|
21
|
+
function error(...msgs) {
|
|
22
|
+
const context = app_1.asyncStore.getStore();
|
|
23
|
+
if (context)
|
|
24
|
+
app_1.eventHub.emit('log', context, 'error', msgs);
|
|
25
|
+
else
|
|
26
|
+
console.error('[ERROR]', ...msgs);
|
|
27
|
+
}
|
|
28
|
+
exports.error = error;
|
|
29
|
+
function trace(name) {
|
|
30
|
+
const context = app_1.asyncStore.getStore();
|
|
31
|
+
const trace = { stack: '' };
|
|
32
|
+
Error.captureStackTrace(trace);
|
|
33
|
+
const stack = trace.stack
|
|
34
|
+
.replace(/.*\n.*logger.js.*\n/, name || 'Untitle\n');
|
|
35
|
+
if (context)
|
|
36
|
+
app_1.eventHub.emit('log', context, 'trace', stack);
|
|
37
|
+
else
|
|
38
|
+
console.log('[TRACE]', stack);
|
|
39
|
+
}
|
|
40
|
+
exports.trace = trace;
|
package/modules/http/index.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.HTTPConfig = void 0;
|
|
4
4
|
const http = require("node:http");
|
|
5
|
+
const node_querystring_1 = require("node:querystring");
|
|
5
6
|
const utils_1 = require("./utils");
|
|
6
7
|
const oox = require("../../index");
|
|
7
8
|
const module_1 = require("../module");
|
|
@@ -15,11 +16,17 @@ class HTTPConfig extends module_1.ModuleConfig {
|
|
|
15
16
|
}
|
|
16
17
|
exports.HTTPConfig = HTTPConfig;
|
|
17
18
|
class HTTPModule extends module_1.default {
|
|
18
|
-
name = '
|
|
19
|
+
name = 'http';
|
|
19
20
|
config = new HTTPConfig;
|
|
20
21
|
server = null;
|
|
21
22
|
setConfig(config) {
|
|
22
23
|
Object.assign(this.config, config);
|
|
24
|
+
if (!config.hasOwnProperty('port')) {
|
|
25
|
+
this.config.port = oox.config.port;
|
|
26
|
+
}
|
|
27
|
+
if (!config.hasOwnProperty('origin')) {
|
|
28
|
+
this.config.origin = oox.config.origin;
|
|
29
|
+
}
|
|
23
30
|
}
|
|
24
31
|
getConfig() {
|
|
25
32
|
return this.config;
|
|
@@ -98,7 +105,12 @@ class HTTPModule extends module_1.default {
|
|
|
98
105
|
return;
|
|
99
106
|
let body = Object.create(null);
|
|
100
107
|
try {
|
|
101
|
-
|
|
108
|
+
if ('GET' === request.method) {
|
|
109
|
+
body = (0, node_querystring_1.parse)(request.url.split('?').pop());
|
|
110
|
+
}
|
|
111
|
+
else {
|
|
112
|
+
body = await (0, utils_1.parseHTTPBody)(request);
|
|
113
|
+
}
|
|
102
114
|
if (!body || 'object' !== typeof body)
|
|
103
115
|
throw new Error('Content Invalid');
|
|
104
116
|
}
|
|
@@ -119,7 +131,7 @@ class HTTPModule extends module_1.default {
|
|
|
119
131
|
const ip = String(request.headers['x-ip'] || request.socket.remoteAddress || '');
|
|
120
132
|
// startup client ip
|
|
121
133
|
const sourceIP = String(request.headers['x-real-ip'] || '');
|
|
122
|
-
const { action, params = [] } = body;
|
|
134
|
+
const { action = 'index', params = [] } = body;
|
|
123
135
|
const context = oox.genContext({ traceId, caller, sourceIP, ip, callerId: '' });
|
|
124
136
|
const format = await oox.call(action, params, context);
|
|
125
137
|
this.respond(request, response, format);
|
package/modules/index.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const path = require("node:path");
|
|
4
3
|
const module_1 = require("./module");
|
|
5
4
|
const http_1 = require("./http");
|
|
6
5
|
const socketio_1 = require("./socketio");
|
|
@@ -22,12 +21,12 @@ class Modules extends module_1.default {
|
|
|
22
21
|
*/
|
|
23
22
|
builtins = {
|
|
24
23
|
http: new http_1.default,
|
|
25
|
-
socketio: new socketio_1.default
|
|
24
|
+
socketio: new socketio_1.default,
|
|
26
25
|
};
|
|
27
26
|
constructor() {
|
|
28
27
|
super();
|
|
29
|
-
this.add(this.builtins.http)
|
|
30
|
-
|
|
28
|
+
this.add(this.builtins.http);
|
|
29
|
+
this.add(this.builtins.socketio);
|
|
31
30
|
}
|
|
32
31
|
add(module) {
|
|
33
32
|
if (!module.name || 'string' !== typeof module.name)
|
|
@@ -50,25 +49,26 @@ class Modules extends module_1.default {
|
|
|
50
49
|
this.#queue.splice(index, 1);
|
|
51
50
|
this.#map.delete(name);
|
|
52
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
|
+
}
|
|
53
69
|
async serve() {
|
|
54
70
|
try {
|
|
55
71
|
for (const module of this.#queue) {
|
|
56
|
-
if (module.name === 'oox:socketio') {
|
|
57
|
-
// http & socketio shared port
|
|
58
|
-
const _socketio = module, _http = this.builtins.http;
|
|
59
|
-
const httpConfig = this.builtins.http.getConfig(), socketioConfig = _socketio.getConfig();
|
|
60
|
-
let isShareServer = false;
|
|
61
|
-
// 都没设置端口
|
|
62
|
-
isShareServer = !httpConfig.port && !socketioConfig.port;
|
|
63
|
-
// 都设置相同端口
|
|
64
|
-
isShareServer = isShareServer || httpConfig.port === socketioConfig.port;
|
|
65
|
-
// http 模块未被禁用
|
|
66
|
-
isShareServer = isShareServer && !httpConfig.disabled;
|
|
67
|
-
if (isShareServer) {
|
|
68
|
-
socketioConfig.path = path.posix.join(httpConfig.path, socketioConfig.path);
|
|
69
|
-
_socketio.server = _http.server;
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
72
|
if (!module.getConfig().disabled) {
|
|
73
73
|
await module.serve();
|
|
74
74
|
}
|
|
@@ -1,11 +1,31 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.sockets = void 0;
|
|
4
|
+
const path = require("node:path");
|
|
3
5
|
const client_1 = require("./client");
|
|
4
6
|
const oox = require("../../index");
|
|
5
7
|
const index_1 = require("../../index");
|
|
6
8
|
const socket_1 = require("./socket");
|
|
9
|
+
Object.defineProperty(exports, "sockets", { enumerable: true, get: function () { return socket_1.sockets; } });
|
|
7
10
|
class SocketIOModule extends client_1.default {
|
|
8
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
|
+
}
|
|
9
29
|
onSyncConnection(socket) {
|
|
10
30
|
const mSockets = Array.from(socket_1.sockets.values())
|
|
11
31
|
.filter(s => s !== socket &&
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.SocketIOConfig = void 0;
|
|
4
|
-
const http = require("http");
|
|
4
|
+
const http = require("node:http");
|
|
5
5
|
const socket_io_1 = require("socket.io");
|
|
6
|
-
const module_1 = require("../module");
|
|
7
|
-
const socket_1 = require("./socket");
|
|
8
6
|
const oox = require("../../index");
|
|
9
|
-
|
|
7
|
+
const index_1 = require("../../index");
|
|
8
|
+
const socket_1 = require("./socket");
|
|
9
|
+
class SocketIOConfig extends index_1.ModuleConfig {
|
|
10
10
|
// listen port
|
|
11
11
|
port = 0;
|
|
12
12
|
// service path
|
|
@@ -15,8 +15,8 @@ class SocketIOConfig extends module_1.ModuleConfig {
|
|
|
15
15
|
origin = '';
|
|
16
16
|
}
|
|
17
17
|
exports.SocketIOConfig = SocketIOConfig;
|
|
18
|
-
class SocketIOServer extends
|
|
19
|
-
name = '
|
|
18
|
+
class SocketIOServer extends index_1.Module {
|
|
19
|
+
name = 'socketio';
|
|
20
20
|
config = new SocketIOConfig;
|
|
21
21
|
/**
|
|
22
22
|
* means this.server created by myself<SocketIOServer>
|
|
@@ -26,6 +26,12 @@ class SocketIOServer extends module_1.default {
|
|
|
26
26
|
socketServer = null;
|
|
27
27
|
setConfig(config) {
|
|
28
28
|
Object.assign(this.config, config);
|
|
29
|
+
if (!config.hasOwnProperty('port')) {
|
|
30
|
+
this.config.port = oox.config.port;
|
|
31
|
+
}
|
|
32
|
+
if (!config.hasOwnProperty('origin')) {
|
|
33
|
+
this.config.origin = oox.config.origin;
|
|
34
|
+
}
|
|
29
35
|
}
|
|
30
36
|
getConfig() {
|
|
31
37
|
return this.config;
|
|
@@ -48,7 +54,7 @@ class SocketIOServer extends module_1.default {
|
|
|
48
54
|
if (this.socketServer)
|
|
49
55
|
await new Promise((resolve, reject) => this.socketServer.close(error => error ? reject(error) : resolve()));
|
|
50
56
|
if (this.#isSelfServer)
|
|
51
|
-
await new Promise((resolve, reject) => this.server.close(error => error ? reject(error) : resolve()));
|
|
57
|
+
await new Promise((resolve, reject) => this.server.listening && this.server.close(error => error ? reject(error) : resolve()));
|
|
52
58
|
}
|
|
53
59
|
genSocketIOServerOptions() {
|
|
54
60
|
const options = {
|
package/package.json
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "oox",
|
|
3
|
-
"version": "0.3.0-
|
|
3
|
+
"version": "0.3.0-beta10",
|
|
4
4
|
"description": "Graceful NodeJS distributed application solution",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"http",
|
|
7
|
+
"websocket",
|
|
7
8
|
"socket.io",
|
|
8
9
|
"rpc",
|
|
10
|
+
"framework",
|
|
9
11
|
"microservice",
|
|
10
12
|
"distributed",
|
|
11
13
|
"tracing"
|
|
@@ -14,9 +16,12 @@
|
|
|
14
16
|
"main": "index.js",
|
|
15
17
|
"types": "types/index.d.ts",
|
|
16
18
|
"exports": {
|
|
17
|
-
"
|
|
18
|
-
|
|
19
|
-
|
|
19
|
+
".": {
|
|
20
|
+
"import": "./index.mjs",
|
|
21
|
+
"require": "./index.js"
|
|
22
|
+
},
|
|
23
|
+
"./loader": "./bin/loader.mjs",
|
|
24
|
+
"./cli": "./bin/cli.js"
|
|
20
25
|
},
|
|
21
26
|
"bin": {
|
|
22
27
|
"oox": "bin/cli.js"
|
|
@@ -30,6 +35,6 @@
|
|
|
30
35
|
"socket.io-client": "^4.4.0"
|
|
31
36
|
},
|
|
32
37
|
"engines": {
|
|
33
|
-
"node": ">=
|
|
38
|
+
"node": ">=14.17.0"
|
|
34
39
|
}
|
|
35
40
|
}
|
package/types/app.d.ts
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
/// <reference types="node" />
|
|
3
3
|
import { EventEmitter } from 'node:events';
|
|
4
4
|
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
5
|
+
export * as logger from './logger';
|
|
5
6
|
export interface ReturnsBody {
|
|
6
7
|
traceId: string;
|
|
7
8
|
success: boolean;
|
|
@@ -12,6 +13,7 @@ export interface ReturnsBody {
|
|
|
12
13
|
};
|
|
13
14
|
}
|
|
14
15
|
export declare class Context {
|
|
16
|
+
[x: string]: any;
|
|
15
17
|
traceId?: string;
|
|
16
18
|
}
|
|
17
19
|
export declare const asyncStore: AsyncLocalStorage<Context>;
|
|
@@ -23,9 +25,21 @@ export declare const kvMethods: Map<string, Function>;
|
|
|
23
25
|
export declare const sourceKVMethods: Map<string, Function>;
|
|
24
26
|
export declare function setMethods(methods: any): void;
|
|
25
27
|
export declare function getMethods(): any;
|
|
28
|
+
export declare function on(event: 'app:configured' | 'app:served' | 'app:stopped', listener: () => void): void;
|
|
26
29
|
export declare function on(event: 'request', listener: (action: string, params: any[], context: Context) => void): void;
|
|
27
30
|
export declare function on(event: 'success', listener: (action: string, params: any[], context: Context, result: ReturnsBody) => void): void;
|
|
28
31
|
export declare function on(event: 'fail', listener: (action: string, params: any[], context: Context, error: Error) => void): void;
|
|
32
|
+
export declare function on(event: 'log', listener: (context: Context, tag: string, msgs: any[]) => void): void;
|
|
33
|
+
export declare function on(event: string, listener: (...args: any[]) => void): void;
|
|
34
|
+
export declare function once(event: 'app:configured' | 'app:served' | 'app:stopped', listener: (...args: any[]) => void): void;
|
|
35
|
+
export declare function once(event: 'request', listener: (action: string, params: any[], context: Context) => void): void;
|
|
36
|
+
export declare function once(event: 'success', listener: (action: string, params: any[], context: Context, result: ReturnsBody) => void): void;
|
|
37
|
+
export declare function once(event: 'fail', listener: (action: string, params: any[], context: Context, error: Error) => void): void;
|
|
38
|
+
export declare function once(event: 'log', listener: (context: Context, tag: string, msgs: any[]) => void): void;
|
|
39
|
+
export declare function once(event: string, listener: (...args: any[]) => void): void;
|
|
40
|
+
export declare function off(event: 'app:configured' | 'app:served' | 'app:stopped' | 'request' | 'success' | 'fail' | 'log', listener: (...args: any[]) => void): void;
|
|
41
|
+
export declare function off(event: string, listener: (...args: any[]) => void): void;
|
|
42
|
+
export declare function emit(event: string, ...args: any[]): boolean;
|
|
29
43
|
/**
|
|
30
44
|
* Call an Function on RPC server
|
|
31
45
|
* @param action
|
package/types/bin/cli.d.ts
CHANGED
package/types/bin/starter.d.ts
CHANGED
package/types/index.d.ts
CHANGED
|
@@ -5,22 +5,27 @@ import Modules from './modules';
|
|
|
5
5
|
export { ReturnsBody } from './app';
|
|
6
6
|
export { Module, ModuleConfig };
|
|
7
7
|
export declare const modules: Modules;
|
|
8
|
-
export declare const asyncStore: import("async_hooks").AsyncLocalStorage<app.Context>, setMethods: typeof app.setMethods, getMethods: typeof app.getMethods, kvMethods: Map<string, Function>, sourceKVMethods: Map<string, Function>, call: typeof app.call, execute: typeof app.execute, on: typeof app.on;
|
|
8
|
+
export declare const asyncStore: import("async_hooks").AsyncLocalStorage<app.Context>, setMethods: typeof app.setMethods, getMethods: typeof app.getMethods, kvMethods: Map<string, Function>, sourceKVMethods: Map<string, Function>, call: typeof app.call, execute: typeof app.execute, logger: typeof app.logger, on: typeof app.on, once: typeof app.once, off: typeof app.off, emit: typeof app.emit;
|
|
9
9
|
export declare class Context extends app.Context {
|
|
10
10
|
sourceIP: string;
|
|
11
11
|
ip: string;
|
|
12
12
|
caller: string;
|
|
13
13
|
callerId: string;
|
|
14
14
|
connection?: RPCKeepAliveConnection;
|
|
15
|
-
toJSON?():
|
|
15
|
+
toJSON?(): {} & this;
|
|
16
16
|
}
|
|
17
17
|
export declare class Config {
|
|
18
18
|
[x: string]: any;
|
|
19
19
|
name: string;
|
|
20
|
+
entryFile: {
|
|
21
|
+
path: string;
|
|
22
|
+
group: string;
|
|
23
|
+
};
|
|
20
24
|
host: string;
|
|
25
|
+
port: number;
|
|
26
|
+
origin: string;
|
|
21
27
|
}
|
|
22
28
|
export declare const config: Config;
|
|
23
|
-
export declare function getConfig(): void;
|
|
24
29
|
export declare function setGenTraceIdFunction(fn: () => string): void;
|
|
25
30
|
/**
|
|
26
31
|
* 生成随机不重复id
|
|
@@ -66,5 +71,6 @@ export declare function getKeepAliveConnection(name: string, id: string): RPCKee
|
|
|
66
71
|
export declare function addKeepAliveConnection(connection: RPCKeepAliveConnection): void;
|
|
67
72
|
export declare function removeKeepAliveConnection(connection: RPCKeepAliveConnection): void;
|
|
68
73
|
export declare function removeKeepAliveConnection(name: string, id: string): void;
|
|
74
|
+
export declare function setLoadBalancePolicy(policy: (name: string) => RPCKeepAliveConnection): void;
|
|
69
75
|
export declare function rpc(appName: string, action: string, params: any[], context?: Context): Promise<any>;
|
|
70
76
|
export declare function rpc(connection: RPCKeepAliveConnection, action: string, params: any[], context?: Context): Promise<any>;
|
|
@@ -2,11 +2,11 @@
|
|
|
2
2
|
/// <reference types="node" />
|
|
3
3
|
/// <reference types="node" />
|
|
4
4
|
import * as http from 'node:http';
|
|
5
|
-
import
|
|
5
|
+
import { Readable } from 'node:stream';
|
|
6
6
|
/**
|
|
7
7
|
* Stream => Buffer
|
|
8
8
|
*/
|
|
9
|
-
export declare function stream2buffer(stream:
|
|
9
|
+
export declare function stream2buffer(stream: Readable, totalLength?: number): Promise<Buffer>;
|
|
10
10
|
/**
|
|
11
11
|
* Request => JSONObject
|
|
12
12
|
*/
|
package/types/modules/index.d.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import SocketIOClient from './client';
|
|
2
2
|
import * as oox from '../../index';
|
|
3
3
|
import { RPCKeepAliveConnectionData, RPCConnectionAdapter } from '../../index';
|
|
4
|
-
import { Socket, ServerSocket, ClientSocket } from './socket';
|
|
4
|
+
import { Socket, sockets, ServerSocket, ClientSocket } from './socket';
|
|
5
|
+
export { Socket, sockets };
|
|
5
6
|
export default class SocketIOModule extends SocketIOClient implements RPCConnectionAdapter {
|
|
6
7
|
sockets: Map<string, Socket>;
|
|
8
|
+
serve(): Promise<void>;
|
|
7
9
|
onSyncConnection(socket: Socket): oox.RPCKeepAliveConnectionData[];
|
|
8
10
|
serverOnDisconnect(socket: ServerSocket, reason: string): void;
|
|
9
11
|
clientOnDisconnect(socket: ClientSocket, reason: any): void;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
-
import * as http from 'http';
|
|
2
|
+
import * as http from 'node:http';
|
|
3
3
|
import { Server, ServerOptions } from 'socket.io';
|
|
4
|
-
import Module,
|
|
4
|
+
import { Module, ModuleConfig } from '../../index';
|
|
5
5
|
import { ServerSocket as Socket } from './socket';
|
|
6
6
|
export declare class SocketIOConfig extends ModuleConfig {
|
|
7
7
|
port: number;
|