oox 0.0.7 → 0.2.0
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/.gitattributes +2 -2
- package/LICENSE +21 -21
- package/README.md +29 -29
- package/bin/argv.js +95 -95
- package/bin/cli.js +57 -57
- package/bin/configurer.js +77 -77
- package/bin/proxyer.js +94 -94
- package/bin/register.js +104 -104
- package/bin/starter.js +143 -139
- package/global.js +118 -13
- package/index.js +5 -8
- package/middleware.js +167 -0
- package/package.json +17 -17
- package/rpc/config.class.js +47 -47
- package/rpc/context.class.js +14 -14
- package/rpc/http.class.js +311 -326
- package/rpc/rpc.class.js +231 -130
- package/rpc/rpc.interface.class.js +119 -121
- package/rpc/socketio.class.js +222 -230
- package/service/service.class.js +73 -76
- package/service/socketio.class.js +144 -190
- package/setMap.class.js +67 -67
- package/socketio/client.class.js +189 -189
- package/socketio/server.class.js +221 -221
- package/socketio/socket.class.js +22 -22
- package/util.js +224 -352
- package/global.class.js +0 -124
package/global.js
CHANGED
|
@@ -1,13 +1,118 @@
|
|
|
1
|
-
|
|
2
|
-
if ( Error.stackTraceLimit < 20 ) Error.stackTraceLimit = 20
|
|
3
|
-
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
1
|
+
|
|
2
|
+
if ( Error.stackTraceLimit < 20 ) Error.stackTraceLimit = 20
|
|
3
|
+
|
|
4
|
+
const { AsyncLocalStorage } = require ( 'async_hooks' )
|
|
5
|
+
|
|
6
|
+
const Context = require ( './rpc/context.class' )
|
|
7
|
+
|
|
8
|
+
const RPC = require ( './rpc/rpc.interface.class' )
|
|
9
|
+
|
|
10
|
+
const Socket = require ( './socketio/socket.class' )
|
|
11
|
+
|
|
12
|
+
const SetMap = require ( './setMap.class' )
|
|
13
|
+
|
|
14
|
+
const Middleware = require ( './middleware' )
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
const Global = {
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
asyncStore: new AsyncLocalStorage ( ),
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
md: Middleware.handler,
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* @type {[RPC]}
|
|
32
|
+
*/
|
|
33
|
+
instances: [ ],
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* @type {Map<String,Socket>}
|
|
39
|
+
*/
|
|
40
|
+
sockets: new Map ( ),
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* @type {SocketIOServer[]}
|
|
46
|
+
*/
|
|
47
|
+
socketIOServers: [ ],
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* @type {Map<String,Set<Socket>>}
|
|
53
|
+
*/
|
|
54
|
+
socketIORegistry: new SetMap,
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* 生成随机不重复id
|
|
60
|
+
* @returns {String}
|
|
61
|
+
*/
|
|
62
|
+
genTraceId ( ) {
|
|
63
|
+
|
|
64
|
+
const uid = [
|
|
65
|
+
Math.floor ( Date.now ( ) / 1000 ).toString ( 16 ),
|
|
66
|
+
Math.floor ( Math.random ( ) * 0xffffffff ).toString ( 16 ).padStart ( 8, '0' )
|
|
67
|
+
]
|
|
68
|
+
|
|
69
|
+
return uid.join ( '' )
|
|
70
|
+
},
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* 获取链路跟踪上下文
|
|
76
|
+
* @param {Context} param0
|
|
77
|
+
* @returns {Context}
|
|
78
|
+
*/
|
|
79
|
+
genContext ( { caller, callerId, traceId, ip, sourceIP } = { } ) {
|
|
80
|
+
|
|
81
|
+
const context = new Context ( )
|
|
82
|
+
|
|
83
|
+
if ( caller ) {
|
|
84
|
+
|
|
85
|
+
context.caller = caller
|
|
86
|
+
} else {
|
|
87
|
+
|
|
88
|
+
const primaryService = this.instances [ 0 ]
|
|
89
|
+
|
|
90
|
+
if ( primaryService ) context.caller = primaryService.name
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
context.traceId = traceId || this.genTraceId ( )
|
|
94
|
+
context.ip = ip
|
|
95
|
+
context.sourceIP = sourceIP || ip
|
|
96
|
+
context.callerId = callerId
|
|
97
|
+
|
|
98
|
+
return context
|
|
99
|
+
},
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
*
|
|
105
|
+
* @returns {Context}
|
|
106
|
+
*/
|
|
107
|
+
getContext ( ) {
|
|
108
|
+
|
|
109
|
+
const context = this.asyncStore.getStore ( )
|
|
110
|
+
|
|
111
|
+
return context || this.genContext ( )
|
|
112
|
+
},
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* @type {Global}
|
|
117
|
+
*/
|
|
118
|
+
global.oox = module.exports = global.oox || Global
|
package/index.js
CHANGED
package/middleware.js
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Map<中间件名字,中间件函数>
|
|
6
|
+
* @type {Map<String,Function>}
|
|
7
|
+
*/
|
|
8
|
+
exports.middlewares = new Map ( )
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* WeakMap<殼函數,原函數>
|
|
14
|
+
* @type {Map<String,Function>}
|
|
15
|
+
*/
|
|
16
|
+
exports.wrappedActions = new Map ( )
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Map<中间件名字,Set<接口函数>>
|
|
22
|
+
* @type {Map<String,Set<Function>>}
|
|
23
|
+
*/
|
|
24
|
+
exports.middlewareActions = new Map ( )
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* WeakMap<接口函数,[中间件名字]>
|
|
30
|
+
* @type {WeakMap<Function,[String]>}
|
|
31
|
+
*/
|
|
32
|
+
exports.actionMiddlewares = new WeakMap ( )
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* @type {Map<String,Proxy>}
|
|
38
|
+
*/
|
|
39
|
+
exports.middlewareProxys = new Map ( )
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
*
|
|
45
|
+
* @param {String} key
|
|
46
|
+
* @param {Function} middleware
|
|
47
|
+
* @returns {(Function)=>{}}
|
|
48
|
+
*/
|
|
49
|
+
exports.define = ( key, middleware ) => {
|
|
50
|
+
|
|
51
|
+
if ( middleware ) {
|
|
52
|
+
|
|
53
|
+
exports.middlewares.set ( key, middleware )
|
|
54
|
+
|
|
55
|
+
exports.middlewareActions.set ( key, new Set ( ) )
|
|
56
|
+
|
|
57
|
+
const proxy = new Proxy ( middleware, {
|
|
58
|
+
|
|
59
|
+
has ( target, action ) {
|
|
60
|
+
|
|
61
|
+
const sourceMethod = exports.wrappedActions.get ( action )
|
|
62
|
+
|
|
63
|
+
return exports.middlewareActions.get ( key ).has ( sourceMethod )
|
|
64
|
+
}
|
|
65
|
+
} )
|
|
66
|
+
|
|
67
|
+
// save proxy
|
|
68
|
+
exports.middlewareProxys.set ( key, proxy )
|
|
69
|
+
} else {
|
|
70
|
+
|
|
71
|
+
return arg => exports.define ( key, arg )
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
*
|
|
79
|
+
* @param {String} key
|
|
80
|
+
* @param {Function} action
|
|
81
|
+
* @param {'start'|'end'} place
|
|
82
|
+
*/
|
|
83
|
+
exports.use = ( key, action, place='end' ) => {
|
|
84
|
+
|
|
85
|
+
if ( !exports.middlewareActions.has ( key ) ) exports.middlewareActions.set ( key, new Set ( ) )
|
|
86
|
+
|
|
87
|
+
if ( !exports.actionMiddlewares.has ( action ) ) exports.actionMiddlewares.set ( action, [ ] )
|
|
88
|
+
|
|
89
|
+
// 双向绑定
|
|
90
|
+
|
|
91
|
+
exports.middlewareActions.get ( key ).add ( action )
|
|
92
|
+
|
|
93
|
+
if ( place === 'end' ) {
|
|
94
|
+
|
|
95
|
+
exports.actionMiddlewares.get ( action ).push ( key )
|
|
96
|
+
} else {
|
|
97
|
+
|
|
98
|
+
exports.actionMiddlewares.get ( action ).unshift ( key )
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
exports.delete = ( key ) => {
|
|
105
|
+
|
|
106
|
+
exports.middlewares.delete ( key )
|
|
107
|
+
|
|
108
|
+
if ( exports.middlewareActions.has ( key ) ) {
|
|
109
|
+
|
|
110
|
+
const actions = exports.middlewareActions.get ( key ).values ( )
|
|
111
|
+
|
|
112
|
+
// remove action bindings
|
|
113
|
+
for ( const action of actions ) {
|
|
114
|
+
|
|
115
|
+
const middlewareNames = exports.actionMiddlewares.get ( action ).filter ( value => value !== key )
|
|
116
|
+
|
|
117
|
+
exports.actionMiddlewares.set ( action, middlewareNames )
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
exports.middlewareActions.get ( key ).clear ( )
|
|
122
|
+
|
|
123
|
+
exports.middlewareActions.delete ( key )
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
exports.handler = new Proxy ( o=>o, {
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
get ( target, key ) {
|
|
133
|
+
|
|
134
|
+
return exports.middlewareProxys.get ( key )
|
|
135
|
+
},
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
set ( target, key, action ) {
|
|
140
|
+
|
|
141
|
+
if ( exports.middlewares.has ( key ) ) {
|
|
142
|
+
|
|
143
|
+
exports.use ( key, action, 'start' )
|
|
144
|
+
} else {
|
|
145
|
+
|
|
146
|
+
exports.define ( key, action )
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
return true
|
|
150
|
+
},
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
deleteProperty ( target, key ) {
|
|
155
|
+
|
|
156
|
+
exports.delete ( key )
|
|
157
|
+
|
|
158
|
+
return true
|
|
159
|
+
},
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
apply ( target, thisArg, args ) {
|
|
164
|
+
|
|
165
|
+
return exports.define.apply ( exports, args )
|
|
166
|
+
}
|
|
167
|
+
} )
|
package/package.json
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "oox",
|
|
3
|
-
"version": "0.0
|
|
4
|
-
"description": "distributed api service",
|
|
5
|
-
"main": "index.js",
|
|
6
|
-
"bin": {
|
|
7
|
-
"oox": "bin/cli.js"
|
|
8
|
-
},
|
|
9
|
-
"author": "lipingruan",
|
|
10
|
-
"homepage": "https://github.com/lipingruan/oox",
|
|
11
|
-
"license": "MIT",
|
|
12
|
-
"dependencies": {
|
|
13
|
-
"chalk": "^4.1.0",
|
|
14
|
-
"socket.io": "^4.0
|
|
15
|
-
"socket.io-client": "^4.0
|
|
16
|
-
}
|
|
17
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "oox",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "distributed api service",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"bin": {
|
|
7
|
+
"oox": "bin/cli.js"
|
|
8
|
+
},
|
|
9
|
+
"author": "lipingruan",
|
|
10
|
+
"homepage": "https://github.com/lipingruan/oox",
|
|
11
|
+
"license": "MIT",
|
|
12
|
+
"dependencies": {
|
|
13
|
+
"chalk": "^4.1.0",
|
|
14
|
+
"socket.io": "^4.4.0",
|
|
15
|
+
"socket.io-client": "^4.4.0"
|
|
16
|
+
}
|
|
17
|
+
}
|
package/rpc/config.class.js
CHANGED
|
@@ -1,48 +1,48 @@
|
|
|
1
|
-
|
|
2
|
-
module.exports = class Config {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* 服务名称
|
|
8
|
-
*/
|
|
9
|
-
name = ''
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* 主机地址列表
|
|
15
|
-
*/
|
|
16
|
-
host = ''
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* 跨域处理
|
|
22
|
-
*/
|
|
23
|
-
origin = null
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* 网关地址
|
|
29
|
-
*/
|
|
30
|
-
gateway = {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* http 网关地址
|
|
36
|
-
* @type {{port:Number,path:String}}
|
|
37
|
-
*/
|
|
38
|
-
http: null,
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* socketio 网关地址
|
|
44
|
-
* @type {{port:Number,path:String}}
|
|
45
|
-
*/
|
|
46
|
-
socketio: null
|
|
47
|
-
}
|
|
1
|
+
|
|
2
|
+
module.exports = class Config {
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* 服务名称
|
|
8
|
+
*/
|
|
9
|
+
name = ''
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* 主机地址列表
|
|
15
|
+
*/
|
|
16
|
+
host = ''
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* 跨域处理
|
|
22
|
+
*/
|
|
23
|
+
origin = null
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* 网关地址
|
|
29
|
+
*/
|
|
30
|
+
gateway = {
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* http 网关地址
|
|
36
|
+
* @type {{port:Number,path:String}}
|
|
37
|
+
*/
|
|
38
|
+
http: null,
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* socketio 网关地址
|
|
44
|
+
* @type {{port:Number,path:String}}
|
|
45
|
+
*/
|
|
46
|
+
socketio: null
|
|
47
|
+
}
|
|
48
48
|
}
|
package/rpc/context.class.js
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
module.exports = class Context {
|
|
4
|
-
|
|
5
|
-
// 请求溯源IP
|
|
6
|
-
sourceIP = ''
|
|
7
|
-
// 请求者IP
|
|
8
|
-
ip = ''
|
|
9
|
-
// 请求溯源ID
|
|
10
|
-
traceId = ''
|
|
11
|
-
// 请求者名称
|
|
12
|
-
caller = ''
|
|
13
|
-
// 请求者ID (长连接专用)
|
|
14
|
-
callerId = ''
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
module.exports = class Context {
|
|
4
|
+
|
|
5
|
+
// 请求溯源IP
|
|
6
|
+
sourceIP = ''
|
|
7
|
+
// 请求者IP
|
|
8
|
+
ip = ''
|
|
9
|
+
// 请求溯源ID
|
|
10
|
+
traceId = ''
|
|
11
|
+
// 请求者名称
|
|
12
|
+
caller = ''
|
|
13
|
+
// 请求者ID (长连接专用)
|
|
14
|
+
callerId = ''
|
|
15
15
|
}
|