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.
- package/LICENSE +21 -21
- package/README.md +29 -32
- package/app.js +131 -143
- package/bin/argv.js +63 -70
- package/bin/cli.js +57 -43
- package/bin/configurer.js +60 -62
- package/bin/loader.mjs +392 -279
- package/bin/proxy-import.js +12 -0
- package/bin/proxy-require.js +88 -0
- package/bin/register.js +46 -55
- package/bin/starter.js +63 -66
- package/index.js +155 -168
- package/index.mjs +4 -4
- package/logger.js +25 -40
- package/modules/http/index.js +286 -192
- package/modules/http/router.js +205 -0
- package/modules/http/utils.js +75 -73
- package/modules/index.js +86 -88
- package/modules/module.js +11 -16
- package/modules/socketio/client.js +97 -101
- package/modules/socketio/index.js +171 -168
- package/modules/socketio/server.js +188 -136
- package/modules/socketio/socket.js +1 -4
- package/package.json +14 -12
- package/types/app.d.ts +50 -51
- package/types/bin/argv.d.ts +8 -8
- package/types/bin/cli.d.ts +6 -2
- package/types/bin/configurer.d.ts +3 -1
- package/types/bin/proxy-import.d.ts +4 -0
- package/types/bin/proxy-require.d.ts +5 -0
- package/types/bin/register.d.ts +1 -1
- package/types/bin/starter.d.ts +5 -1
- package/types/index.d.ts +78 -76
- package/types/logger.d.ts +5 -4
- package/types/modules/http/index.d.ts +65 -47
- package/types/modules/http/router.d.ts +49 -0
- package/types/modules/http/utils.d.ts +14 -17
- package/types/modules/index.d.ts +24 -24
- package/types/modules/module.d.ts +11 -13
- package/types/modules/socketio/client.d.ts +23 -23
- package/types/modules/socketio/index.d.ts +37 -37
- package/types/modules/socketio/server.d.ts +44 -35
- package/types/modules/socketio/socket.d.ts +11 -11
- package/types/utils.d.ts +6 -6
- package/utils.js +57 -63
- package/bin/proxyer.js +0 -61
- package/types/bin/proxyer.d.ts +0 -1
package/index.js
CHANGED
|
@@ -1,168 +1,155 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
const
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
//
|
|
15
|
-
|
|
16
|
-
// 请求者
|
|
17
|
-
|
|
18
|
-
//
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
let genTraceIdFunction = () =>
|
|
51
|
-
function setGenTraceIdFunction(fn) {
|
|
52
|
-
genTraceIdFunction = fn;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
if (!newContext.
|
|
68
|
-
newContext.
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
function
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
if (arg1 instanceof RPCKeepAliveConnection) {
|
|
157
|
-
connection = arg1;
|
|
158
|
-
}
|
|
159
|
-
else if ('string' === typeof arg1) {
|
|
160
|
-
connection = loadBalancePolicy(arg1);
|
|
161
|
-
if (!connection)
|
|
162
|
-
throw new Error(`Connection<${arg1}> not found`);
|
|
163
|
-
}
|
|
164
|
-
else
|
|
165
|
-
throw new Error(`Unknown rpc arg1<${arg1}>`);
|
|
166
|
-
return connection.adapter.rpc(connection.nativeConnection, action, params, context);
|
|
167
|
-
}
|
|
168
|
-
exports.rpc = rpc;
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import * as app from './app.js';
|
|
3
|
+
import { getIPAddress } from './utils.js';
|
|
4
|
+
import Module, { ModuleConfig } from './modules/module.js';
|
|
5
|
+
import Modules from './modules/index.js';
|
|
6
|
+
export { Module, ModuleConfig };
|
|
7
|
+
export const modules = new Modules;
|
|
8
|
+
export const { asyncStore, setMethods, getMethods, kvMethods, sourceKVMethods, call, execute, logger, on, once, off, emit, } = app;
|
|
9
|
+
export class Context extends app.Context {
|
|
10
|
+
// 请求溯源IP
|
|
11
|
+
sourceIP = '';
|
|
12
|
+
// 请求者IP
|
|
13
|
+
ip = '';
|
|
14
|
+
// 请求者名称
|
|
15
|
+
caller = 'anonymous';
|
|
16
|
+
// 请求者ID (长连接专用)
|
|
17
|
+
callerId = '';
|
|
18
|
+
// 请求者连接把柄
|
|
19
|
+
connection;
|
|
20
|
+
toJSON() {
|
|
21
|
+
const context = Object.assign({}, this);
|
|
22
|
+
delete context.connection;
|
|
23
|
+
return context;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
export class Config {
|
|
27
|
+
// 服务名称
|
|
28
|
+
name = 'local';
|
|
29
|
+
// 服务列表路径
|
|
30
|
+
group = '';
|
|
31
|
+
// 非服务模块
|
|
32
|
+
ignore = [];
|
|
33
|
+
// 启动文件
|
|
34
|
+
entryInfo = {
|
|
35
|
+
// 启动文件路径
|
|
36
|
+
path: '',
|
|
37
|
+
// 服务列表路径
|
|
38
|
+
group: '',
|
|
39
|
+
};
|
|
40
|
+
// 主机地址
|
|
41
|
+
host = getIPAddress(4)[0];
|
|
42
|
+
// 默认监听端口
|
|
43
|
+
port = 0;
|
|
44
|
+
// 默认跨域设置
|
|
45
|
+
origin = '';
|
|
46
|
+
// 默认返回错误调用栈信息信息
|
|
47
|
+
errorStack = true;
|
|
48
|
+
}
|
|
49
|
+
export const config = new Config();
|
|
50
|
+
let genTraceIdFunction = () => randomUUID();
|
|
51
|
+
export function setGenTraceIdFunction(fn) {
|
|
52
|
+
genTraceIdFunction = fn;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* 生成随机不重复id
|
|
56
|
+
*/
|
|
57
|
+
export function genTraceId() {
|
|
58
|
+
return genTraceIdFunction();
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* 获取链路跟踪上下文
|
|
62
|
+
*/
|
|
63
|
+
export function genContext(context) {
|
|
64
|
+
const newContext = Object.assign(new Context(), context);
|
|
65
|
+
if (!newContext.traceId)
|
|
66
|
+
newContext.traceId = genTraceId();
|
|
67
|
+
if (!newContext.sourceIP)
|
|
68
|
+
newContext.sourceIP = newContext.ip;
|
|
69
|
+
return newContext;
|
|
70
|
+
}
|
|
71
|
+
export function getContext() {
|
|
72
|
+
const context = asyncStore.getStore();
|
|
73
|
+
return context || genContext();
|
|
74
|
+
}
|
|
75
|
+
export async function serve() {
|
|
76
|
+
await modules.serve();
|
|
77
|
+
}
|
|
78
|
+
export async function stop() {
|
|
79
|
+
await modules.stop();
|
|
80
|
+
}
|
|
81
|
+
export class RPCKeepAliveConnection {
|
|
82
|
+
data;
|
|
83
|
+
nativeConnection = null;
|
|
84
|
+
adapter = null;
|
|
85
|
+
constructor(adapter, nativeConnection, data) {
|
|
86
|
+
this.adapter = adapter;
|
|
87
|
+
this.nativeConnection = nativeConnection;
|
|
88
|
+
if (data)
|
|
89
|
+
this.data = data;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
export const keepAliveConnections = new Map();
|
|
93
|
+
export function getKeepAliveConnections(name) {
|
|
94
|
+
return keepAliveConnections.get(name) || new Map();
|
|
95
|
+
}
|
|
96
|
+
export function getKeepAliveConnection(name, id) {
|
|
97
|
+
const connections = keepAliveConnections.get(name);
|
|
98
|
+
if (!connections)
|
|
99
|
+
return null;
|
|
100
|
+
return connections.get(id);
|
|
101
|
+
}
|
|
102
|
+
export function addKeepAliveConnection(connection) {
|
|
103
|
+
const { name, id } = connection.data;
|
|
104
|
+
if (keepAliveConnections.has(name)) {
|
|
105
|
+
keepAliveConnections.get(name).set(id, connection);
|
|
106
|
+
}
|
|
107
|
+
else {
|
|
108
|
+
keepAliveConnections.set(name, new Map([[id, connection]]));
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
export function removeKeepAliveConnection(name, id) {
|
|
112
|
+
if (name instanceof RPCKeepAliveConnection) {
|
|
113
|
+
id = name.data.id;
|
|
114
|
+
name = name.data.name;
|
|
115
|
+
}
|
|
116
|
+
if (keepAliveConnections.has(name)) {
|
|
117
|
+
const group = keepAliveConnections.get(name);
|
|
118
|
+
group.delete(id);
|
|
119
|
+
if (!group.size)
|
|
120
|
+
keepAliveConnections.delete(name);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* random connection select for default load balance policy
|
|
125
|
+
* @param name service name
|
|
126
|
+
* @returns selected connection
|
|
127
|
+
*/
|
|
128
|
+
let loadBalancePolicy = (name) => {
|
|
129
|
+
const connections = keepAliveConnections.get(name);
|
|
130
|
+
if (!connections || !connections.size)
|
|
131
|
+
return null;
|
|
132
|
+
const arrayConnections = Array.from(connections.values());
|
|
133
|
+
const index = Math.floor(Math.random() * arrayConnections.length);
|
|
134
|
+
return arrayConnections[index];
|
|
135
|
+
};
|
|
136
|
+
export function setLoadBalancePolicy(policy) {
|
|
137
|
+
loadBalancePolicy = policy;
|
|
138
|
+
}
|
|
139
|
+
export async function rpc(arg1, action, params, context) {
|
|
140
|
+
if (!context || !context.traceId) {
|
|
141
|
+
context = getContext();
|
|
142
|
+
}
|
|
143
|
+
let connection;
|
|
144
|
+
if (arg1 instanceof RPCKeepAliveConnection) {
|
|
145
|
+
connection = arg1;
|
|
146
|
+
}
|
|
147
|
+
else if ('string' === typeof arg1) {
|
|
148
|
+
connection = loadBalancePolicy(arg1);
|
|
149
|
+
if (!connection)
|
|
150
|
+
throw new Error(`Connection<${arg1}> not found`);
|
|
151
|
+
}
|
|
152
|
+
else
|
|
153
|
+
throw new Error(`Unknown rpc arg1<${arg1}>`);
|
|
154
|
+
return connection.adapter.rpc(connection.nativeConnection, action, params, context);
|
|
155
|
+
}
|
package/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
import * as oox from './index.js'
|
|
3
|
-
|
|
4
|
-
export * from './index.js'
|
|
1
|
+
|
|
2
|
+
import * as oox from './index.js'
|
|
3
|
+
|
|
4
|
+
export * from './index.js'
|
|
5
5
|
export default oox
|
package/logger.js
CHANGED
|
@@ -1,40 +1,25 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
const
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
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;
|
|
1
|
+
import { emit, asyncStore } from './app.js';
|
|
2
|
+
export function info(...content) {
|
|
3
|
+
const context = asyncStore.getStore() || null;
|
|
4
|
+
emit('log', Date.now(), context, 'info', content);
|
|
5
|
+
}
|
|
6
|
+
export function warn(...content) {
|
|
7
|
+
const context = asyncStore.getStore() || null;
|
|
8
|
+
emit('log', Date.now(), context, 'warn', content);
|
|
9
|
+
}
|
|
10
|
+
export function error(...content) {
|
|
11
|
+
const context = asyncStore.getStore() || null;
|
|
12
|
+
emit('log', Date.now(), context, 'error', content);
|
|
13
|
+
}
|
|
14
|
+
export function tag(name) {
|
|
15
|
+
const context = asyncStore.getStore() || null;
|
|
16
|
+
emit('log', Date.now(), context, 'tag', name);
|
|
17
|
+
}
|
|
18
|
+
export function trace(name) {
|
|
19
|
+
const context = asyncStore.getStore() || null;
|
|
20
|
+
const trace = { stack: '' };
|
|
21
|
+
Error.captureStackTrace(trace);
|
|
22
|
+
const stack = trace.stack
|
|
23
|
+
.replace(/.*\n.*logger.js.*\n/, name || 'Untitle\n');
|
|
24
|
+
emit('log', Date.now(), context, 'trace', stack);
|
|
25
|
+
}
|