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/rpc/rpc.class.js CHANGED
@@ -1,130 +1,231 @@
1
-
2
- const { genOOXTrace } = require ( '../util' )
3
-
4
- const Context = require ( './context.class' )
5
-
6
- if ( Error.stackTraceLimit < 20 ) Error.stackTraceLimit = 20
7
-
8
- const HTTP = require ( './http.class' )
9
-
10
- const SocketIO = require ( './socketio.class' )
11
-
12
- const Global = require ( '../global' )
13
-
14
- const RPCInterface = require ( './rpc.interface.class' )
15
-
16
-
17
-
18
- module.exports = class RPC extends RPCInterface {
19
-
20
- static Context = Context
21
-
22
- static HTTP = HTTP
23
- static SocketIO = SocketIO
24
-
25
-
26
-
27
- /**
28
- * @type {HTTP}
29
- */
30
- http = new this.constructor.HTTP ( this )
31
-
32
-
33
-
34
- /**
35
- * @type {SocketIO}
36
- */
37
- socketio = new this.constructor.SocketIO ( this )
38
-
39
-
40
-
41
- /**
42
- * new RPC service
43
- * @param {*} methods
44
- */
45
- constructor ( name, methods ) {
46
- super ( name, methods )
47
-
48
- Global.instances.push ( this )
49
- }
50
-
51
-
52
-
53
- async serve ( ) {
54
-
55
- let isShareServer = false
56
-
57
- if ( this.http.config && this.socketio.config ) {
58
-
59
- isShareServer = !this.http.config.port && !this.socketio.config.port
60
-
61
- isShareServer |= this.http.config.port === this.socketio.config.port
62
- }
63
-
64
- await this.http.serve ( )
65
-
66
- if ( isShareServer ) this.socketio.server = this.http.server
67
-
68
- await this.socketio.serve ( )
69
- }
70
-
71
-
72
-
73
- async stop ( ) {
74
-
75
- await this.http.stop ( )
76
- await this.socketio.stop ( )
77
- }
78
-
79
-
80
-
81
- /**
82
- * 通用RPC调用方法
83
- * @param {String} action
84
- * @param {[]} params
85
- * @param {Context} context
86
- */
87
- async call ( action, params=[], context ) {
88
-
89
- if ( !Array.isArray ( params ) ) params = [ params ]
90
-
91
- const { traceId } = context
92
-
93
- Global.contexts.set ( traceId, context )
94
-
95
- this.emit ( 'request', action, params, context )
96
-
97
- const format = {
98
- traceId,
99
- success: false
100
- }
101
-
102
- const OOXTrace = genOOXTrace ( traceId, this.kvMethods )
103
-
104
- try {
105
-
106
- const result = await OOXTrace [ traceId ] ( action, [ ...params ], context )
107
-
108
- format.body = result
109
-
110
- format.success = true
111
-
112
- this.emit ( 'success', action, params, context, result )
113
- } catch ( error ) {
114
-
115
- format.error = {
116
- message: error.message,
117
- stack: error.stack
118
- }
119
-
120
- this.emit ( 'fail', action, params, context, error )
121
- } finally {
122
-
123
- delete OOXTrace [ traceId ]
124
-
125
- Global.contexts.delete ( traceId )
126
-
127
- return format
128
- }
129
- }
130
- }
1
+
2
+ const { genOOXTrace } = require ( '../util' )
3
+
4
+ const Context = require ( './context.class' )
5
+
6
+ const HTTP = require ( './http.class' )
7
+
8
+ const SocketIO = require ( './socketio.class' )
9
+
10
+ const Global = require ( '../global' )
11
+
12
+ const RPCInterface = require ( './rpc.interface.class' )
13
+
14
+ const Middleware = require ( '../middleware' )
15
+
16
+
17
+
18
+ module.exports = class RPC extends RPCInterface {
19
+
20
+ static Context = Context
21
+
22
+ static HTTP = HTTP
23
+ static SocketIO = SocketIO
24
+
25
+
26
+
27
+ /**
28
+ * @type {HTTP}
29
+ */
30
+ http = new this.constructor.HTTP ( this )
31
+
32
+
33
+
34
+ /**
35
+ * @type {SocketIO}
36
+ */
37
+ socketio = new this.constructor.SocketIO ( this )
38
+
39
+
40
+
41
+ /**
42
+ * new RPC service
43
+ * @param {*} methods
44
+ */
45
+ constructor ( name, methods ) {
46
+ super ( name, methods )
47
+
48
+ Global.instances.push ( this )
49
+ }
50
+
51
+
52
+
53
+ async serve ( ) {
54
+
55
+ let isShareServer = false
56
+
57
+ if ( this.http.config && this.socketio.config ) {
58
+
59
+ isShareServer = !this.http.config.port && !this.socketio.config.port
60
+
61
+ isShareServer |= this.http.config.port === this.socketio.config.port
62
+ }
63
+
64
+ await this.http.serve ( )
65
+
66
+ if ( isShareServer ) this.socketio.server = this.http.server
67
+
68
+ await this.socketio.serve ( )
69
+ }
70
+
71
+
72
+
73
+ async stop ( ) {
74
+
75
+ await this.http.stop ( )
76
+ await this.socketio.stop ( )
77
+ }
78
+
79
+
80
+
81
+ /**
82
+ * 通用RPC调用方法
83
+ * @param {String} action
84
+ * @param {[]} params
85
+ * @param {Context} context
86
+ */
87
+ async call ( action, params=[], context ) {
88
+
89
+ if ( !Array.isArray ( params ) ) params = [ params ]
90
+
91
+ const { traceId } = context
92
+
93
+ this.emit ( 'request', action, params, context )
94
+
95
+ const format = {
96
+ traceId,
97
+ success: false
98
+ }
99
+
100
+ try {
101
+
102
+ const result = await this.execute ( this.kvMethods, action, [ ...params ], context )
103
+
104
+ format.body = result
105
+
106
+ format.success = true
107
+
108
+ this.emit ( 'success', action, params, context, result )
109
+ } catch ( error ) {
110
+
111
+ format.error = {
112
+ message: error.message,
113
+ stack: error.stack
114
+ }
115
+
116
+ this.emit ( 'fail', action, params, context, error )
117
+ } finally {
118
+
119
+ return format
120
+ }
121
+ }
122
+
123
+
124
+ /**
125
+ *
126
+ * @param {Map<String,Function>} methods 服务函数扁平化列表
127
+ * @param {String} action
128
+ * @param {Array} params
129
+ * @param {Context} context
130
+ */
131
+ async execute ( methods, action, params, context ) {
132
+
133
+ const __proxy = '__proxy', _proxy = '_proxy'
134
+
135
+
136
+
137
+ // 目标函数
138
+ const target = methods.get ( action )
139
+
140
+ // 目标代理函数
141
+ const targetProxy = methods.get ( action + _proxy )
142
+
143
+ // 即不存在目标也不存在目标代理时, 报错函数不存在
144
+ if ( !target && !targetProxy ) throw new Error ( 'Invalid Action [' + action + ']' )
145
+
146
+ Global.asyncStore.enterWith ( context )
147
+
148
+
149
+
150
+ // 最顶层代理
151
+ const topProxy = methods.get ( __proxy )
152
+
153
+ if ( topProxy ) {
154
+
155
+ const proxyReturns = await topProxy ( action, params, context )
156
+
157
+ if ( proxyReturns !== undefined ) return proxyReturns
158
+ }
159
+
160
+
161
+
162
+ // 'x.y.z' => [ 'x', 'y', 'z' ]
163
+ const nameStack = action.split ( '.' ), size = nameStack.length - 1
164
+
165
+ let index = -1, proxyPrefix = ''
166
+
167
+
168
+
169
+ // 根代理遍历
170
+ while ( ++index < size ) {
171
+
172
+ // x.
173
+ // x.y.
174
+ proxyPrefix += nameStack [ index ] + '.'
175
+
176
+ // x.__proxy
177
+ // x.y.__proxy
178
+ const rootProxy = methods.get ( proxyPrefix + __proxy )
179
+
180
+ // x.__proxy ( 'y.z', ... )
181
+ // x.y.__proxy ( 'z', ... )
182
+ if ( rootProxy ) {
183
+
184
+ const proxyReturns = await rootProxy ( nameStack.slice ( index ).join ( '.' ), params, context )
185
+
186
+ if ( proxyReturns !== undefined ) return proxyReturns
187
+ }
188
+ }
189
+
190
+
191
+
192
+ // 同级代理
193
+ const layerProxy = methods.get ( proxyPrefix + _proxy )
194
+
195
+ if ( layerProxy ) {
196
+
197
+ const proxyReturns = await layerProxy ( nameStack [ index ], params, context )
198
+
199
+ if ( proxyReturns !== undefined ) return proxyReturns
200
+ }
201
+
202
+
203
+
204
+ if ( targetProxy ) {
205
+
206
+ const proxyReturns = await targetProxy ( params, context )
207
+
208
+ if ( proxyReturns !== undefined ) return proxyReturns
209
+ }
210
+
211
+
212
+
213
+ // make sure target action execute after all proxies
214
+ if ( target ) {
215
+
216
+ const sourceMethod = Middleware.wrappedActions.get ( action )
217
+
218
+ const middlewareNames = Middleware.actionMiddlewares.get ( sourceMethod )
219
+
220
+ if ( middlewareNames && middlewareNames.length ) for ( const name of middlewareNames ) {
221
+
222
+ const middleware = Middleware.middlewares.get ( name )
223
+
224
+ await middleware ( action, params, context )
225
+ }
226
+
227
+ return await target ( ...params )
228
+ }
229
+ }
230
+
231
+ }
@@ -1,121 +1,119 @@
1
-
2
- const { EventEmitter } = require ( 'events' )
3
-
4
- const { genKVMethods, getIPAddress } = require ( '../util' )
5
-
6
- const Context = require ( './context.class' )
7
-
8
- const Config = require ( './config.class' )
9
-
10
- if ( Error.stackTraceLimit < 20 ) Error.stackTraceLimit = 20
11
-
12
-
13
-
14
- module.exports = class RPC extends EventEmitter {
15
-
16
-
17
-
18
- /**
19
- * refMethods => methods
20
- */
21
- refMethods = { }
22
-
23
-
24
-
25
- /**
26
- * the kvMethods is all actions refs
27
- * @type {Map<String,Function>}
28
- */
29
- kvMethods = new Map ( )
30
-
31
-
32
-
33
- set methods ( methods ) {
34
-
35
- this.refMethods = methods
36
-
37
- this.kvMethods.clear ( )
38
-
39
- genKVMethods ( methods, this.kvMethods )
40
- }
41
-
42
-
43
-
44
- appendMethods ( methods ) {
45
-
46
- genKVMethods ( methods, this.kvMethods )
47
- }
48
-
49
-
50
-
51
- get methods ( ) {
52
-
53
- return this.refMethods
54
- }
55
-
56
-
57
-
58
- /**
59
- * RPC Node Name
60
- */
61
- name = ''
62
-
63
-
64
-
65
- config = new Config
66
-
67
-
68
-
69
- /**
70
- * new RPC service
71
- * @param {*} methods
72
- */
73
- constructor ( name, methods ) {
74
- super ( )
75
-
76
- this.name = name
77
-
78
- if ( methods )
79
- this.methods = methods
80
-
81
- const [ host ] = getIPAddress ( 4 )
82
-
83
- this.config.host = host
84
-
85
- this.config.name = name
86
- }
87
-
88
-
89
-
90
- async serve ( ) { }
91
-
92
-
93
-
94
- async stop ( ) { }
95
-
96
-
97
-
98
- /**
99
- * RPC事件监听
100
- * @param {'request'|'success'|'fail'} event
101
- * @param {(action: String, params: [], context: Context, result: {
102
- * traceId: String,
103
- * success: Boolean,
104
- * body: any,
105
- * error: Error
106
- * } | Error) => void} listener
107
- */
108
- on ( event, listener ) {
109
- return super.on ( event, listener )
110
- }
111
-
112
-
113
-
114
- /**
115
- * 通用RPC调用方法
116
- * @param {String} action
117
- * @param {[]} params
118
- * @param {Context} context
119
- */
120
- async call ( action, params=[], context ) { }
121
- }
1
+
2
+ const { EventEmitter } = require ( 'events' )
3
+
4
+ const { genKVMethods, getIPAddress } = require ( '../util' )
5
+
6
+ const Context = require ( './context.class' )
7
+
8
+ const Config = require ( './config.class' )
9
+
10
+
11
+
12
+ module.exports = class RPC extends EventEmitter {
13
+
14
+
15
+
16
+ /**
17
+ * refMethods => methods
18
+ */
19
+ refMethods = { }
20
+
21
+
22
+
23
+ /**
24
+ * the kvMethods is all actions refs
25
+ * @type {Map<String,Function>}
26
+ */
27
+ kvMethods = new Map ( )
28
+
29
+
30
+
31
+ set methods ( methods ) {
32
+
33
+ this.refMethods = methods
34
+
35
+ this.kvMethods.clear ( )
36
+
37
+ genKVMethods ( methods, this.kvMethods )
38
+ }
39
+
40
+
41
+
42
+ appendMethods ( methods ) {
43
+
44
+ genKVMethods ( methods, this.kvMethods )
45
+ }
46
+
47
+
48
+
49
+ get methods ( ) {
50
+
51
+ return this.refMethods
52
+ }
53
+
54
+
55
+
56
+ /**
57
+ * RPC Node Name
58
+ */
59
+ name = ''
60
+
61
+
62
+
63
+ config = new Config
64
+
65
+
66
+
67
+ /**
68
+ * new RPC service
69
+ * @param {*} methods
70
+ */
71
+ constructor ( name, methods ) {
72
+ super ( )
73
+
74
+ this.name = name
75
+
76
+ if ( methods )
77
+ this.methods = methods
78
+
79
+ const [ host ] = getIPAddress ( 4 )
80
+
81
+ this.config.host = host
82
+
83
+ this.config.name = name
84
+ }
85
+
86
+
87
+
88
+ async serve ( ) { }
89
+
90
+
91
+
92
+ async stop ( ) { }
93
+
94
+
95
+
96
+ /**
97
+ * RPC事件监听
98
+ * @param {'request'|'success'|'fail'} event
99
+ * @param {(action: String, params: [], context: Context, result: {
100
+ * traceId: String,
101
+ * success: Boolean,
102
+ * body: any,
103
+ * error: Error
104
+ * } | Error) => void} listener
105
+ */
106
+ on ( event, listener ) {
107
+ return super.on ( event, listener )
108
+ }
109
+
110
+
111
+
112
+ /**
113
+ * 通用RPC调用方法
114
+ * @param {String} action
115
+ * @param {[]} params
116
+ * @param {Context} context
117
+ */
118
+ async call ( action, params=[], context ) { }
119
+ }