oox 0.2.0 → 0.3.0-beta1
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/LICENSE +21 -21
- package/README.md +32 -29
- package/app.js +142 -0
- package/bin/argv.js +70 -95
- package/bin/cli.js +43 -58
- package/bin/configurer.js +49 -78
- package/bin/proxyer.js +61 -95
- package/bin/register.js +54 -105
- package/bin/starter.js +77 -144
- package/index.js +149 -6
- package/index.mjs +4 -0
- package/modules/http/index.js +180 -0
- package/modules/http/utils.js +73 -0
- package/modules/index.js +88 -0
- package/modules/module.js +16 -0
- package/modules/socketio/client.js +101 -0
- package/modules/socketio/index.js +148 -0
- package/modules/socketio/server.js +130 -0
- package/modules/socketio/socket.js +4 -0
- package/package.json +34 -16
- package/types/app.d.ts +37 -0
- package/types/bin/argv.d.ts +8 -0
- package/types/bin/cli.d.ts +2 -0
- package/types/bin/configurer.d.ts +1 -0
- package/types/bin/proxyer.d.ts +1 -0
- package/types/bin/register.d.ts +1 -0
- package/types/bin/starter.d.ts +1 -0
- package/types/index.d.ts +70 -0
- package/types/modules/http/index.d.ts +47 -0
- package/types/modules/http/utils.d.ts +17 -0
- package/types/modules/index.d.ts +23 -0
- package/types/modules/module.d.ts +11 -0
- package/types/modules/socketio/client.d.ts +23 -0
- package/types/modules/socketio/index.d.ts +35 -0
- package/types/modules/socketio/server.d.ts +35 -0
- package/types/modules/socketio/socket.d.ts +11 -0
- package/types/utils.d.ts +6 -0
- package/utils.js +63 -0
- package/.gitattributes +0 -2
- package/global.js +0 -118
- package/middleware.js +0 -167
- package/rpc/config.class.js +0 -48
- package/rpc/context.class.js +0 -15
- package/rpc/http.class.js +0 -312
- package/rpc/rpc.class.js +0 -231
- package/rpc/rpc.interface.class.js +0 -119
- package/rpc/socketio.class.js +0 -223
- package/service/service.class.js +0 -74
- package/service/socketio.class.js +0 -145
- package/setMap.class.js +0 -67
- package/socketio/client.class.js +0 -190
- package/socketio/server.class.js +0 -222
- package/socketio/socket.class.js +0 -23
- package/util.js +0 -224
package/index.js
CHANGED
|
@@ -1,6 +1,149 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
"use strict";
|
|
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.getConfig = exports.config = exports.Config = exports.Context = exports.on = exports.execute = exports.call = exports.sourceKVMethods = exports.kvMethods = exports.getMethods = exports.setMethods = exports.asyncStore = exports.modules = exports.ModuleConfig = exports.Module = void 0;
|
|
4
|
+
const app = require("./app");
|
|
5
|
+
const utils_1 = require("./utils");
|
|
6
|
+
const module_1 = require("./modules/module");
|
|
7
|
+
exports.Module = module_1.default;
|
|
8
|
+
Object.defineProperty(exports, "ModuleConfig", { enumerable: true, get: function () { return module_1.ModuleConfig; } });
|
|
9
|
+
const modules_1 = require("./modules");
|
|
10
|
+
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
|
+
class Context extends app.Context {
|
|
13
|
+
// 请求溯源IP
|
|
14
|
+
sourceIP = '';
|
|
15
|
+
// 请求者IP
|
|
16
|
+
ip = '';
|
|
17
|
+
// 请求者名称
|
|
18
|
+
caller = 'anonymous';
|
|
19
|
+
// 请求者ID (长连接专用)
|
|
20
|
+
callerId = '';
|
|
21
|
+
// 请求者连接把柄
|
|
22
|
+
connection;
|
|
23
|
+
toJSON() {
|
|
24
|
+
const context = Object.assign({}, this);
|
|
25
|
+
delete context.connection;
|
|
26
|
+
return JSON.stringify(context);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
exports.Context = Context;
|
|
30
|
+
class Config {
|
|
31
|
+
// 服务名称
|
|
32
|
+
name = 'local';
|
|
33
|
+
// 主机地址
|
|
34
|
+
host = (0, utils_1.getIPAddress)(4)[0];
|
|
35
|
+
}
|
|
36
|
+
exports.Config = Config;
|
|
37
|
+
exports.config = new Config();
|
|
38
|
+
function getConfig() {
|
|
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
|
+
};
|
|
48
|
+
function setGenTraceIdFunction(fn) {
|
|
49
|
+
genTraceIdFunction = fn;
|
|
50
|
+
}
|
|
51
|
+
exports.setGenTraceIdFunction = setGenTraceIdFunction;
|
|
52
|
+
/**
|
|
53
|
+
* 生成随机不重复id
|
|
54
|
+
*/
|
|
55
|
+
function genTraceId() {
|
|
56
|
+
return genTraceIdFunction();
|
|
57
|
+
}
|
|
58
|
+
exports.genTraceId = genTraceId;
|
|
59
|
+
/**
|
|
60
|
+
* 获取链路跟踪上下文
|
|
61
|
+
*/
|
|
62
|
+
function genContext(context) {
|
|
63
|
+
const newContext = Object.assign(new Context(), context);
|
|
64
|
+
if (!newContext.traceId)
|
|
65
|
+
newContext.traceId = genTraceId();
|
|
66
|
+
if (!newContext.sourceIP)
|
|
67
|
+
newContext.sourceIP = newContext.ip;
|
|
68
|
+
return newContext;
|
|
69
|
+
}
|
|
70
|
+
exports.genContext = genContext;
|
|
71
|
+
function getContext() {
|
|
72
|
+
const context = exports.asyncStore.getStore();
|
|
73
|
+
return context || genContext();
|
|
74
|
+
}
|
|
75
|
+
exports.getContext = getContext;
|
|
76
|
+
async function serve() {
|
|
77
|
+
await exports.modules.serve();
|
|
78
|
+
}
|
|
79
|
+
exports.serve = serve;
|
|
80
|
+
async function stop() {
|
|
81
|
+
await exports.modules.stop();
|
|
82
|
+
}
|
|
83
|
+
exports.stop = stop;
|
|
84
|
+
class RPCKeepAliveConnection {
|
|
85
|
+
data;
|
|
86
|
+
nativeConnection = null;
|
|
87
|
+
adapter = null;
|
|
88
|
+
constructor(adapter, nativeConnection, data) {
|
|
89
|
+
this.adapter = adapter;
|
|
90
|
+
this.nativeConnection = nativeConnection;
|
|
91
|
+
if (data)
|
|
92
|
+
this.data = data;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
exports.RPCKeepAliveConnection = RPCKeepAliveConnection;
|
|
96
|
+
exports.keepAliveConnections = new Map();
|
|
97
|
+
function getKeepAliveConnections(name) {
|
|
98
|
+
return exports.keepAliveConnections.get(name) || new Map();
|
|
99
|
+
}
|
|
100
|
+
exports.getKeepAliveConnections = getKeepAliveConnections;
|
|
101
|
+
function getKeepAliveConnection(name, id) {
|
|
102
|
+
const connections = exports.keepAliveConnections.get(name);
|
|
103
|
+
if (!connections)
|
|
104
|
+
return null;
|
|
105
|
+
return connections.get(id);
|
|
106
|
+
}
|
|
107
|
+
exports.getKeepAliveConnection = getKeepAliveConnection;
|
|
108
|
+
function addKeepAliveConnection(connection) {
|
|
109
|
+
const { name, id } = connection.data;
|
|
110
|
+
if (exports.keepAliveConnections.has(name)) {
|
|
111
|
+
exports.keepAliveConnections.get(name).set(id, connection);
|
|
112
|
+
}
|
|
113
|
+
else {
|
|
114
|
+
exports.keepAliveConnections.set(name, new Map([[id, connection]]));
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
exports.addKeepAliveConnection = addKeepAliveConnection;
|
|
118
|
+
function removeKeepAliveConnection(name, id) {
|
|
119
|
+
if (name instanceof RPCKeepAliveConnection) {
|
|
120
|
+
id = name.data.id;
|
|
121
|
+
name = name.data.name;
|
|
122
|
+
}
|
|
123
|
+
if (exports.keepAliveConnections.has(name)) {
|
|
124
|
+
const group = exports.keepAliveConnections.get(name);
|
|
125
|
+
group.delete(id);
|
|
126
|
+
if (!group.size)
|
|
127
|
+
exports.keepAliveConnections.delete(name);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
exports.removeKeepAliveConnection = removeKeepAliveConnection;
|
|
131
|
+
async function rpc(arg1, action, params, context) {
|
|
132
|
+
if (!context || !context.traceId) {
|
|
133
|
+
context = getContext();
|
|
134
|
+
}
|
|
135
|
+
let connection;
|
|
136
|
+
if (arg1 instanceof RPCKeepAliveConnection) {
|
|
137
|
+
connection = arg1;
|
|
138
|
+
}
|
|
139
|
+
else if ('string' === typeof arg1) {
|
|
140
|
+
const connections = exports.keepAliveConnections.get(arg1);
|
|
141
|
+
if (!connections || !connections.size)
|
|
142
|
+
throw new Error(`Connection<${arg1}> not found`);
|
|
143
|
+
connection = connections.values().next().value;
|
|
144
|
+
}
|
|
145
|
+
else
|
|
146
|
+
throw new Error(`Unknown rpc arg1<${arg1}>`);
|
|
147
|
+
return connection.adapter.rpc(connection.nativeConnection, action, params, context);
|
|
148
|
+
}
|
|
149
|
+
exports.rpc = rpc;
|
package/index.mjs
ADDED
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.HTTPConfig = void 0;
|
|
4
|
+
const http = require("node:http");
|
|
5
|
+
const utils_1 = require("./utils");
|
|
6
|
+
const oox = require("../../index");
|
|
7
|
+
const module_1 = require("../module");
|
|
8
|
+
class HTTPConfig extends module_1.ModuleConfig {
|
|
9
|
+
// listen port
|
|
10
|
+
port = 0;
|
|
11
|
+
// service path
|
|
12
|
+
path = '/';
|
|
13
|
+
// browser cross origin
|
|
14
|
+
origin = '';
|
|
15
|
+
}
|
|
16
|
+
exports.HTTPConfig = HTTPConfig;
|
|
17
|
+
class HTTPModule extends module_1.default {
|
|
18
|
+
name = 'oox:http';
|
|
19
|
+
config = new HTTPConfig;
|
|
20
|
+
server = null;
|
|
21
|
+
setConfig(config) {
|
|
22
|
+
Object.assign(this.config, config);
|
|
23
|
+
}
|
|
24
|
+
getConfig() {
|
|
25
|
+
return this.config;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* start http service
|
|
29
|
+
*/
|
|
30
|
+
async serve() {
|
|
31
|
+
await this.stop();
|
|
32
|
+
const { port } = this.config;
|
|
33
|
+
this.server = http.createServer(this.call.bind(this));
|
|
34
|
+
this.server.listen(port);
|
|
35
|
+
const address = this.server.address();
|
|
36
|
+
if (!address || 'object' !== typeof address)
|
|
37
|
+
throw new Error('Cannot read http server port');
|
|
38
|
+
this.config.port = address.port;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* stop http service
|
|
42
|
+
*/
|
|
43
|
+
stop() {
|
|
44
|
+
if (this.server && this.server.listening)
|
|
45
|
+
return new Promise((resolve, reject) => {
|
|
46
|
+
this.server.close(function (error) {
|
|
47
|
+
if (error)
|
|
48
|
+
reject(error);
|
|
49
|
+
else
|
|
50
|
+
resolve();
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* browser cross origin
|
|
56
|
+
*/
|
|
57
|
+
cors(request, response) {
|
|
58
|
+
// origin checking
|
|
59
|
+
const origin = this.config.origin;
|
|
60
|
+
const requestOrigin = request.headers.origin;
|
|
61
|
+
if (origin && requestOrigin) {
|
|
62
|
+
if (origin === '*' || origin === requestOrigin || Array.isArray(origin) && origin.includes(requestOrigin)) {
|
|
63
|
+
response.setHeader('Access-Control-Allow-Origin', requestOrigin);
|
|
64
|
+
response.setHeader('Vary', 'Origin');
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
response.statusCode = 403;
|
|
68
|
+
response.end();
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
response.setHeader('Access-Control-Max-Age', 3600);
|
|
72
|
+
response.setHeader('Access-Control-Allow-Headers', 'x-caller,content-type');
|
|
73
|
+
response.setHeader('Access-Control-Allow-Methods', '*');
|
|
74
|
+
}
|
|
75
|
+
if (request.method === 'OPTIONS') {
|
|
76
|
+
response.statusCode = 204;
|
|
77
|
+
response.end();
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
return true;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* HTTP-RPC服务器请求监听方法
|
|
84
|
+
*/
|
|
85
|
+
async call(request, response) {
|
|
86
|
+
if (request.url !== this.config.path) {
|
|
87
|
+
const error = {
|
|
88
|
+
message: 'Invalid URL',
|
|
89
|
+
stack: ''
|
|
90
|
+
};
|
|
91
|
+
Error.captureStackTrace(error);
|
|
92
|
+
return this.respond(request, response, {
|
|
93
|
+
success: false,
|
|
94
|
+
error
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
if (!this.cors(request, response))
|
|
98
|
+
return;
|
|
99
|
+
let body = Object.create(null);
|
|
100
|
+
try {
|
|
101
|
+
body = await (0, utils_1.parseHTTPBody)(request);
|
|
102
|
+
if (!body || 'object' !== typeof body)
|
|
103
|
+
throw new Error('Content Invalid');
|
|
104
|
+
}
|
|
105
|
+
catch (error) {
|
|
106
|
+
return this.respond(request, response, {
|
|
107
|
+
success: false,
|
|
108
|
+
error: {
|
|
109
|
+
message: error.message,
|
|
110
|
+
stack: error.stack
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
// global unique id
|
|
115
|
+
const traceId = String(request.headers['x-trace-id'] || '');
|
|
116
|
+
// service name, required
|
|
117
|
+
const caller = String(request.headers['x-caller'] || 'anonymous');
|
|
118
|
+
// client ip or caller service ip
|
|
119
|
+
const ip = String(request.headers['x-ip'] || request.socket.remoteAddress || '');
|
|
120
|
+
// startup client ip
|
|
121
|
+
const sourceIP = String(request.headers['x-real-ip'] || '');
|
|
122
|
+
const { action, params = [] } = body;
|
|
123
|
+
const context = oox.genContext({ traceId, caller, sourceIP, ip, callerId: '' });
|
|
124
|
+
const format = await oox.call(action, params, context);
|
|
125
|
+
this.respond(request, response, format);
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* HTTP Response Catch
|
|
129
|
+
*/
|
|
130
|
+
respond(request, response, format) {
|
|
131
|
+
let formatString = '';
|
|
132
|
+
try {
|
|
133
|
+
formatString = JSON.stringify(format);
|
|
134
|
+
}
|
|
135
|
+
catch ({ message, stack }) {
|
|
136
|
+
delete format.body;
|
|
137
|
+
format.success = false;
|
|
138
|
+
format.error = {
|
|
139
|
+
message,
|
|
140
|
+
stack
|
|
141
|
+
};
|
|
142
|
+
formatString = JSON.stringify(format);
|
|
143
|
+
}
|
|
144
|
+
response.setHeader('Content-Type', 'application/json');
|
|
145
|
+
response.setHeader('Content-Length', Buffer.byteLength(formatString));
|
|
146
|
+
response.end(formatString);
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* HTTP RPC
|
|
150
|
+
*/
|
|
151
|
+
async rpc(url, action, params, context) {
|
|
152
|
+
if (!context || !context.traceId) {
|
|
153
|
+
context = oox.getContext();
|
|
154
|
+
}
|
|
155
|
+
const { traceId, caller, sourceIP } = context;
|
|
156
|
+
const headers = {
|
|
157
|
+
'Content-Type': 'application/json',
|
|
158
|
+
'x-trace-id': String(traceId),
|
|
159
|
+
};
|
|
160
|
+
if (caller)
|
|
161
|
+
headers['x-caller'] = String(caller);
|
|
162
|
+
if (sourceIP)
|
|
163
|
+
headers['x-real-ip'] = sourceIP;
|
|
164
|
+
// headers [ 'x-ip' ] = getIPAddress ( 4 ) [ 0 ]
|
|
165
|
+
const format = await (0, utils_1.httpRequest)(url, {
|
|
166
|
+
headers
|
|
167
|
+
}, JSON.stringify({ action, params }));
|
|
168
|
+
if ('string' === typeof format)
|
|
169
|
+
throw new Error(format);
|
|
170
|
+
const { error, body } = format;
|
|
171
|
+
if (error) {
|
|
172
|
+
const asyncError = new Error(error.message);
|
|
173
|
+
throw asyncError;
|
|
174
|
+
}
|
|
175
|
+
else {
|
|
176
|
+
return body;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
exports.default = HTTPModule;
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.httpRequest = exports.parseHTTPBody = exports.stream2buffer = void 0;
|
|
4
|
+
const http = require("node:http");
|
|
5
|
+
/**
|
|
6
|
+
* Stream => Buffer
|
|
7
|
+
*/
|
|
8
|
+
function stream2buffer(stream, totalLength = 0) {
|
|
9
|
+
return new Promise(function (resolve, reject) {
|
|
10
|
+
let buffers = [];
|
|
11
|
+
stream.on('error', reject);
|
|
12
|
+
if (totalLength) {
|
|
13
|
+
stream.on('data', function (data) { buffers.push(data); });
|
|
14
|
+
}
|
|
15
|
+
else {
|
|
16
|
+
stream.on('data', function (data) {
|
|
17
|
+
buffers.push(data);
|
|
18
|
+
totalLength += data.length;
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
stream.on('end', function () { resolve(Buffer.concat(buffers, totalLength)); });
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
exports.stream2buffer = stream2buffer;
|
|
25
|
+
/**
|
|
26
|
+
* Request => JSONObject
|
|
27
|
+
*/
|
|
28
|
+
async function parseHTTPBody(request) {
|
|
29
|
+
if (request.method === 'GET')
|
|
30
|
+
return null;
|
|
31
|
+
let contentType = request.headers['content-type'];
|
|
32
|
+
// application/json; charset=utf-8
|
|
33
|
+
if (contentType)
|
|
34
|
+
contentType = contentType.split(';')[0].trim();
|
|
35
|
+
const contentSize = request.headers['content-length'];
|
|
36
|
+
const buffer = await stream2buffer(request, +contentSize || 0);
|
|
37
|
+
if (contentSize && buffer.length !== +contentSize)
|
|
38
|
+
throw new Error('Content-Length Incorrect');
|
|
39
|
+
const bodyString = buffer.toString();
|
|
40
|
+
if ('application/json' === contentType) {
|
|
41
|
+
return JSON.parse(bodyString);
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
return bodyString;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
exports.parseHTTPBody = parseHTTPBody;
|
|
48
|
+
/**
|
|
49
|
+
* http request
|
|
50
|
+
*/
|
|
51
|
+
function httpRequest(url, options, body) {
|
|
52
|
+
return new Promise(function (resolve, reject) {
|
|
53
|
+
const request = http.request(url, options, async function (response) {
|
|
54
|
+
try {
|
|
55
|
+
const result = await parseHTTPBody(response);
|
|
56
|
+
resolve(result);
|
|
57
|
+
}
|
|
58
|
+
catch (error) {
|
|
59
|
+
const decoration = new Error(`${response.statusCode} - ${error.message}`);
|
|
60
|
+
reject(decoration);
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
request.on('error', reject);
|
|
64
|
+
if (body) {
|
|
65
|
+
request.method = 'POST';
|
|
66
|
+
request.setHeader('Content-Type', 'application/json');
|
|
67
|
+
request.setHeader('Content-Length', Buffer.byteLength(body));
|
|
68
|
+
request.write(body);
|
|
69
|
+
}
|
|
70
|
+
request.end();
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
exports.httpRequest = httpRequest;
|
package/modules/index.js
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const path = require("node:path");
|
|
4
|
+
const module_1 = require("./module");
|
|
5
|
+
const http_1 = require("./http");
|
|
6
|
+
const socketio_1 = require("./socketio");
|
|
7
|
+
class Modules extends module_1.default {
|
|
8
|
+
/**
|
|
9
|
+
* the module unique name
|
|
10
|
+
*/
|
|
11
|
+
name = 'oox:modules';
|
|
12
|
+
/**
|
|
13
|
+
* FIFO queue for modules starting
|
|
14
|
+
*/
|
|
15
|
+
#queue = [];
|
|
16
|
+
/**
|
|
17
|
+
* all modules map
|
|
18
|
+
*/
|
|
19
|
+
#map = new Map();
|
|
20
|
+
/**
|
|
21
|
+
* all builtin modules
|
|
22
|
+
*/
|
|
23
|
+
builtins = {
|
|
24
|
+
http: new http_1.default,
|
|
25
|
+
socketio: new socketio_1.default
|
|
26
|
+
};
|
|
27
|
+
constructor() {
|
|
28
|
+
super();
|
|
29
|
+
this.add(this.builtins.http)
|
|
30
|
+
.add(this.builtins.socketio);
|
|
31
|
+
}
|
|
32
|
+
add(module) {
|
|
33
|
+
if (!module.name || 'string' !== typeof module.name)
|
|
34
|
+
throw new Error(`Module<${module.name}> has noname`);
|
|
35
|
+
if (this.#map.has(module.name))
|
|
36
|
+
throw new Error(`Module<${module.name}> has exists`);
|
|
37
|
+
this.#map.set(module.name, module);
|
|
38
|
+
this.#queue.push(module);
|
|
39
|
+
return this;
|
|
40
|
+
}
|
|
41
|
+
get(name) {
|
|
42
|
+
return this.#map.get(name);
|
|
43
|
+
}
|
|
44
|
+
async remove(name) {
|
|
45
|
+
const module = this.get(name);
|
|
46
|
+
if (!module)
|
|
47
|
+
return;
|
|
48
|
+
await module.stop();
|
|
49
|
+
const index = this.#queue.indexOf(module);
|
|
50
|
+
this.#queue.splice(index, 1);
|
|
51
|
+
this.#map.delete(name);
|
|
52
|
+
}
|
|
53
|
+
async serve() {
|
|
54
|
+
try {
|
|
55
|
+
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
|
+
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;
|
|
@@ -0,0 +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;
|
|
@@ -0,0 +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;
|