oox 0.0.9 → 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/README.md +4 -1
- package/app.js +142 -0
- package/bin/argv.js +59 -84
- package/bin/cli.js +39 -54
- package/bin/configurer.js +37 -66
- package/bin/proxyer.js +54 -88
- package/bin/register.js +44 -95
- package/bin/starter.js +66 -129
- package/index.js +149 -9
- 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 +35 -17
- 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.class.js +0 -124
- package/global.js +0 -13
- package/rpc/config.class.js +0 -48
- package/rpc/context.class.js +0 -15
- package/rpc/http.class.js +0 -327
- package/rpc/rpc.class.js +0 -130
- package/rpc/rpc.interface.class.js +0 -121
- package/rpc/socketio.class.js +0 -231
- package/service/service.class.js +0 -86
- package/service/socketio.class.js +0 -188
- 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 -352
package/bin/proxyer.js
CHANGED
|
@@ -1,95 +1,61 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.proxyGroup = void 0;
|
|
4
|
+
const fs = require("node:fs");
|
|
5
|
+
const path = require("node:path");
|
|
6
|
+
const oox = require("../index");
|
|
7
|
+
const node_module_1 = require("node:module");
|
|
10
8
|
/**
|
|
11
9
|
* 重写 require 缓存
|
|
12
|
-
* @param {String} id
|
|
13
|
-
* @param {*} exports
|
|
14
10
|
*/
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
const
|
|
18
|
-
|
|
19
|
-
const
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
m.
|
|
26
|
-
|
|
27
|
-
m.exports = exports
|
|
28
|
-
m.filename = m.id
|
|
29
|
-
m.loaded = true
|
|
30
|
-
m.children = [ ]
|
|
31
|
-
m.paths = paths
|
|
32
|
-
m.parent = null
|
|
33
|
-
|
|
34
|
-
require.cache [ id ] = m
|
|
11
|
+
function rewriteModuleCache(id, exports) {
|
|
12
|
+
const pathname = id.split(path.sep).slice(0, -1).join(path.sep);
|
|
13
|
+
const pathSplit = pathname.split(path.sep);
|
|
14
|
+
const paths = pathSplit.map((v, i, a) => a.slice(0, i + 1).concat('node_modules').join(path.sep)).reverse();
|
|
15
|
+
const m = new node_module_1.Module(id, null);
|
|
16
|
+
m.path = pathname;
|
|
17
|
+
m.exports = exports;
|
|
18
|
+
m.filename = m.id;
|
|
19
|
+
m.loaded = true;
|
|
20
|
+
m.children = [];
|
|
21
|
+
m.paths = paths;
|
|
22
|
+
require.cache[id] = m;
|
|
35
23
|
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
},
|
|
47
|
-
|
|
48
|
-
has ( target, key ) { return true },
|
|
49
|
-
|
|
50
|
-
apply ( target, thisObject, args ) {
|
|
51
|
-
|
|
52
|
-
return Service.call ( name, paths.join ( '.' ), args )
|
|
53
|
-
}
|
|
54
|
-
} )
|
|
24
|
+
function dotCall(name, paths = []) {
|
|
25
|
+
return new Proxy(function () { }, {
|
|
26
|
+
get(target, key) {
|
|
27
|
+
return dotCall(name, paths.concat(key));
|
|
28
|
+
},
|
|
29
|
+
has(target, key) { return true; },
|
|
30
|
+
apply(target, thisArg, args) {
|
|
31
|
+
return oox.rpc(name, paths.join('.'), args);
|
|
32
|
+
}
|
|
33
|
+
});
|
|
55
34
|
}
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
if (
|
|
62
|
-
|
|
63
|
-
const
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
id = fullPath + '/index.js'
|
|
82
|
-
name = filename
|
|
83
|
-
} else if ( filename.endsWith ( '.js' ) ) {
|
|
84
|
-
|
|
85
|
-
id = fullPath
|
|
86
|
-
name = filename.split ( '.js' ) [ 0 ]
|
|
87
|
-
} else continue
|
|
88
|
-
|
|
89
|
-
if ( !excludes.includes ( name ) ) rewriteModuleCache ( id, dotCall ( name ) )
|
|
35
|
+
function proxyGroup(groupDirectory, excludes = []) {
|
|
36
|
+
if (!groupDirectory)
|
|
37
|
+
return;
|
|
38
|
+
const directory = path.resolve(groupDirectory);
|
|
39
|
+
const stat = fs.statSync(directory);
|
|
40
|
+
if (!stat.isDirectory())
|
|
41
|
+
throw new Error('group must be directory');
|
|
42
|
+
const subs = fs.readdirSync(directory);
|
|
43
|
+
for (const filename of subs) {
|
|
44
|
+
const fullPath = path.resolve(directory + path.sep + filename);
|
|
45
|
+
const stat = fs.statSync(fullPath);
|
|
46
|
+
let id = '', name = '';
|
|
47
|
+
if (stat.isDirectory() && fs.existsSync(fullPath + '/index.js')) {
|
|
48
|
+
id = fullPath + '/index.js';
|
|
49
|
+
name = filename;
|
|
50
|
+
}
|
|
51
|
+
else if (filename.endsWith('.js')) {
|
|
52
|
+
id = fullPath;
|
|
53
|
+
name = filename.split('.js')[0];
|
|
54
|
+
}
|
|
55
|
+
else
|
|
56
|
+
continue;
|
|
57
|
+
if (!excludes.includes(name))
|
|
58
|
+
rewriteModuleCache(id, dotCall(name));
|
|
90
59
|
}
|
|
91
60
|
}
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
exports.setService = serviceClazz => Service = serviceClazz
|
|
61
|
+
exports.proxyGroup = proxyGroup;
|
package/bin/register.js
CHANGED
|
@@ -1,105 +1,54 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
function delay ( ms ) {
|
|
9
|
-
|
|
10
|
-
return new Promise ( function ( resolve, reject ) {
|
|
11
|
-
|
|
12
|
-
setTimeout ( resolve, ms )
|
|
13
|
-
} )
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
function urlFormatter ( service, url ) {
|
|
19
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.registry = void 0;
|
|
4
|
+
const chalk_1 = require("chalk");
|
|
5
|
+
const oox = require("../index");
|
|
6
|
+
const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));
|
|
7
|
+
function urlFormatter(url) {
|
|
20
8
|
// :6000
|
|
21
|
-
if (
|
|
22
|
-
|
|
9
|
+
if (url.startsWith(':'))
|
|
10
|
+
url = oox.config.host + url;
|
|
23
11
|
// http://127.0.0.1:6000
|
|
24
|
-
if (
|
|
25
|
-
|
|
12
|
+
if (url.startsWith('http://'))
|
|
13
|
+
url = url.slice(7);
|
|
26
14
|
// 127.0.0.1:6000
|
|
27
|
-
if (
|
|
28
|
-
|
|
29
|
-
const urlObject = new URL
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
return url
|
|
15
|
+
if (!url.startsWith('ws://'))
|
|
16
|
+
url = 'ws://' + url;
|
|
17
|
+
const urlObject = new URL(url);
|
|
18
|
+
if (urlObject.pathname === '/' && !url.endsWith('/'))
|
|
19
|
+
url = url + '/socket.io';
|
|
20
|
+
return url;
|
|
34
21
|
}
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
* @param {String} url
|
|
41
|
-
*/
|
|
42
|
-
async function connect ( service, url, prevError = null ) {
|
|
43
|
-
|
|
44
|
-
if ( service.socketio.config ) {
|
|
45
|
-
|
|
46
|
-
const { port, path } = service.socketio.config
|
|
47
|
-
|
|
48
|
-
if ( `ws://${service.config.host}:${port}${path}` === url ) return
|
|
49
|
-
}
|
|
50
|
-
|
|
22
|
+
async function connect(url, prevError = null) {
|
|
23
|
+
const { host } = oox.config;
|
|
24
|
+
const { port, path } = oox.modules.builtins.socketio.config;
|
|
25
|
+
if (`ws://${host}:${port}${path}` === url)
|
|
26
|
+
return;
|
|
51
27
|
try {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
await delay ( 5000 )
|
|
61
|
-
|
|
62
|
-
connect ( service, url, error )
|
|
28
|
+
const socket = await oox.modules.builtins.socketio.connect(url);
|
|
29
|
+
onConnection(socket, url);
|
|
30
|
+
}
|
|
31
|
+
catch (error) {
|
|
32
|
+
if (!prevError)
|
|
33
|
+
console.log((0, chalk_1.red) `[Registry]`, chalk_1.underline.red `${url}`, 'error.');
|
|
34
|
+
await delay(5000);
|
|
35
|
+
connect(url, error);
|
|
63
36
|
}
|
|
64
37
|
}
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
await delay ( 1000 )
|
|
75
|
-
|
|
76
|
-
connect ( service, url )
|
|
77
|
-
} )
|
|
78
|
-
|
|
79
|
-
console.log ( chalk.green`[Registry]`, chalk.underline.green`${url}`, 'connected.' )
|
|
38
|
+
async function onConnection(socket, url) {
|
|
39
|
+
const { name } = socket.data;
|
|
40
|
+
socket.on('disconnect', async () => {
|
|
41
|
+
console.log((0, chalk_1.red) `[Registry]`, `Service<${name}>`, chalk_1.underline.red `${url}`, 'disconnected.');
|
|
42
|
+
await delay(1000);
|
|
43
|
+
connect(url);
|
|
44
|
+
});
|
|
45
|
+
console.log((0, chalk_1.green) `[Registry]`, `Service<${name}>`, chalk_1.underline.green `${url}`, 'connected.');
|
|
80
46
|
}
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
* @param {Service} service
|
|
87
|
-
* @param {String[]} registry
|
|
88
|
-
*/
|
|
89
|
-
exports.connect = async ( service, registry ) => {
|
|
90
|
-
|
|
91
|
-
if ( 'string' === typeof registry ) {
|
|
92
|
-
|
|
93
|
-
connect ( service, urlFormatter ( service, registry ) )
|
|
94
|
-
} else {
|
|
95
|
-
|
|
96
|
-
for ( const url of registry ) {
|
|
97
|
-
|
|
98
|
-
connect ( service, urlFormatter ( service, url ) )
|
|
99
|
-
}
|
|
47
|
+
async function registry(urls) {
|
|
48
|
+
if ('string' === typeof urls)
|
|
49
|
+
urls = [urls];
|
|
50
|
+
for (const url of urls) {
|
|
51
|
+
connect(urlFormatter(url));
|
|
100
52
|
}
|
|
101
53
|
}
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
exports.setService = serviceClazz => Service = serviceClazz
|
|
54
|
+
exports.registry = registry;
|
package/bin/starter.js
CHANGED
|
@@ -1,140 +1,77 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const path = require
|
|
5
|
-
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
var [ entryFilename ] = args.filter ( arg => !arg.includes ( '=' ) && arg.endsWith ( '.js' ) )
|
|
23
|
-
|
|
24
|
-
if ( !entryFilename ) throw new Error ( 'Cannot find entry file' )
|
|
25
|
-
|
|
26
|
-
const fullPath = path.resolve ( entryFilename )
|
|
27
|
-
|
|
28
|
-
const filename = path.basename ( fullPath )
|
|
29
|
-
|
|
30
|
-
const directory = path.dirname ( fullPath ).split ( path.sep ).pop ( )
|
|
31
|
-
|
|
32
|
-
var name = filename === 'index.js' ? directory : filename.split ( '.js' ) [ 0 ]
|
|
33
|
-
|
|
34
|
-
return { name, path: fullPath }
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.startup = void 0;
|
|
4
|
+
const path = require("node:path");
|
|
5
|
+
const chalk_1 = require("chalk");
|
|
6
|
+
const oox = require("../index");
|
|
7
|
+
const proxyer_1 = require("./proxyer");
|
|
8
|
+
const configurer_1 = require("./configurer");
|
|
9
|
+
const register_1 = require("./register");
|
|
10
|
+
function getEntryFile(env) {
|
|
11
|
+
const args = process.argv.slice(2);
|
|
12
|
+
var [entryFilename] = args.filter(arg => !arg.includes('=') && arg.endsWith('.js'));
|
|
13
|
+
if (!entryFilename)
|
|
14
|
+
throw new Error('Cannot find entry file');
|
|
15
|
+
const fullPath = path.resolve(entryFilename);
|
|
16
|
+
const filename = path.basename(fullPath);
|
|
17
|
+
const fullDirectory = path.dirname(fullPath);
|
|
18
|
+
const directory = fullDirectory.split(path.sep).pop();
|
|
19
|
+
const groupFullDirectory = env.group ? path.resolve(env.group) : '';
|
|
20
|
+
var name = filename === 'index.js' && groupFullDirectory !== fullDirectory ? directory : filename.split('.js')[0];
|
|
21
|
+
return { name, path: fullPath, group: groupFullDirectory };
|
|
35
22
|
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* @type {Service}
|
|
43
|
-
*/
|
|
44
|
-
var entry = require ( entryPath )
|
|
45
|
-
|
|
46
|
-
if ( 'object' === typeof entry && 'function' === typeof entry.call && 'string' === typeof entry.name && 'object' === typeof entry.config ) {
|
|
47
|
-
|
|
48
|
-
// entry instanceof RPC
|
|
49
|
-
|
|
50
|
-
} else if ( 'function' === typeof entry && 'function' === typeof entry.call ) {
|
|
51
|
-
|
|
52
|
-
// entry extendsof RPC
|
|
53
|
-
|
|
54
|
-
entry = new entry ( name, null )
|
|
55
|
-
} else {
|
|
56
|
-
|
|
57
|
-
// custom service class
|
|
58
|
-
|
|
59
|
-
if ( !templatePath ) templatePath = path.dirname ( entryPath ) + '/service.class.js'
|
|
60
|
-
|
|
61
|
-
templatePath = path.resolve ( templatePath )
|
|
62
|
-
|
|
63
|
-
if ( fs.existsSync ( templatePath ) ) {
|
|
64
|
-
|
|
65
|
-
const CustomService = require ( templatePath )
|
|
66
|
-
|
|
67
|
-
entry = new CustomService ( name, entry )
|
|
68
|
-
} else {
|
|
69
|
-
|
|
70
|
-
entry = new Service ( name, entry )
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
return entry
|
|
23
|
+
function loadEntry(name, entryPath) {
|
|
24
|
+
oox.config.name = name;
|
|
25
|
+
const methods = require(entryPath);
|
|
26
|
+
oox.setMethods(methods);
|
|
75
27
|
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
exports.startup = async ( ) => {
|
|
80
|
-
|
|
28
|
+
async function startup() {
|
|
81
29
|
// 加载环境变量
|
|
82
|
-
const env =
|
|
83
|
-
|
|
84
|
-
Object.assign ( oox, env )
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
30
|
+
const env = (0, configurer_1.configure)();
|
|
31
|
+
Object.assign(oox.config, env);
|
|
88
32
|
// 获取服务入口地址
|
|
89
|
-
const entryFile = getEntryFile
|
|
90
|
-
|
|
33
|
+
const entryFile = getEntryFile(env);
|
|
91
34
|
// 代理<服务间调用>
|
|
92
|
-
if (
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
proxyer.proxyServices ( env.group, excludes )
|
|
35
|
+
if (env.group) {
|
|
36
|
+
const excludes = [entryFile.name];
|
|
37
|
+
if (Array.isArray(env.ignore))
|
|
38
|
+
excludes.push(...env.ignore);
|
|
39
|
+
(0, proxyer_1.proxyGroup)(entryFile.group, excludes);
|
|
99
40
|
}
|
|
100
|
-
|
|
101
41
|
// 加载服务
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
proxyer.setService ( entry.constructor )
|
|
105
|
-
register.setService ( entry.constructor )
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
42
|
+
loadEntry(entryFile.name, entryFile.path);
|
|
109
43
|
// 服务配置
|
|
110
|
-
if (
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
44
|
+
if (!env.port && !env.http && !env.socketio)
|
|
45
|
+
env.port = 0;
|
|
46
|
+
const httpConfig = oox.modules.builtins.http.config, socketioConfig = oox.modules.builtins.socketio.config;
|
|
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;
|
|
119
63
|
}
|
|
120
|
-
|
|
121
|
-
if ( 'http' in env ) entry.http.config = env.http
|
|
122
|
-
|
|
123
|
-
if ( 'socketio' in env ) entry.socketio.config = env.socketio
|
|
124
|
-
|
|
125
|
-
if ( env.origin ) entry.config.origin = env.origin
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
64
|
// 服务启动
|
|
130
|
-
await
|
|
131
|
-
|
|
132
|
-
console.log ( )
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
if (
|
|
136
|
-
|
|
137
|
-
|
|
65
|
+
await oox.serve();
|
|
66
|
+
console.log();
|
|
67
|
+
console.log('Service', (0, chalk_1.bold) `${oox.config.name}`, 'running.');
|
|
68
|
+
if (!httpConfig.disabled)
|
|
69
|
+
console.log(' at', chalk_1.underline.green `http://${oox.config.host}:${httpConfig.port}${httpConfig.path}`);
|
|
70
|
+
if (!socketioConfig.disabled)
|
|
71
|
+
console.log(' at', chalk_1.underline.green `ws://${oox.config.host}:${socketioConfig.port}${socketioConfig.path}`);
|
|
72
|
+
console.log();
|
|
138
73
|
// 服务注册
|
|
139
|
-
if (
|
|
140
|
-
|
|
74
|
+
if (env.registry)
|
|
75
|
+
(0, register_1.registry)(env.registry);
|
|
76
|
+
}
|
|
77
|
+
exports.startup = startup;
|
package/index.js
CHANGED
|
@@ -1,9 +1,149 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
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