oox 0.1.0 → 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/bin/starter.js CHANGED
@@ -15,7 +15,7 @@ const register = require ( './register' )
15
15
 
16
16
 
17
17
 
18
- function getEntryFile ( ) {
18
+ function getEntryFile ( env ) {
19
19
 
20
20
  const args = process.argv.slice ( 2 )
21
21
 
@@ -27,11 +27,15 @@ function getEntryFile ( ) {
27
27
 
28
28
  const filename = path.basename ( fullPath )
29
29
 
30
- const directory = path.dirname ( fullPath ).split ( path.sep ).pop ( )
30
+ const fullDirectory = path.dirname ( fullPath )
31
31
 
32
- var name = filename === 'index.js' ? directory : filename.split ( '.js' ) [ 0 ]
32
+ const directory = fullDirectory.split ( path.sep ).pop ( )
33
33
 
34
- return { name, path: fullPath }
34
+ const groupFullDirectory = env.group ? path.resolve ( env.group ) : ''
35
+
36
+ var name = filename === 'index.js' && groupFullDirectory !== fullDirectory ? directory : filename.split ( '.js' ) [ 0 ]
37
+
38
+ return { name, path: fullPath, group: groupFullDirectory }
35
39
  }
36
40
 
37
41
 
@@ -86,7 +90,7 @@ exports.startup = async ( ) => {
86
90
 
87
91
 
88
92
  // 获取服务入口地址
89
- const entryFile = getEntryFile ( )
93
+ const entryFile = getEntryFile ( env )
90
94
 
91
95
  // 代理<服务间调用>
92
96
  if ( env.group ) {
@@ -95,7 +99,7 @@ exports.startup = async ( ) => {
95
99
 
96
100
  if ( Array.isArray ( env.ignore ) ) excludes.push ( ...env.ignore )
97
101
 
98
- proxyer.proxyServices ( env.group, excludes )
102
+ proxyer.proxyServices ( entryFile.group, excludes )
99
103
  }
100
104
 
101
105
  // 加载服务
package/global.js CHANGED
@@ -1,7 +1,7 @@
1
1
 
2
2
  if ( Error.stackTraceLimit < 20 ) Error.stackTraceLimit = 20
3
3
 
4
- const { getTraceIdByStack } = require ( './util' )
4
+ const { AsyncLocalStorage } = require ( 'async_hooks' )
5
5
 
6
6
  const Context = require ( './rpc/context.class' )
7
7
 
@@ -19,22 +19,18 @@ const Global = {
19
19
 
20
20
 
21
21
 
22
- md: Middleware.handler,
22
+ asyncStore: new AsyncLocalStorage ( ),
23
23
 
24
24
 
25
25
 
26
- /**
27
- * @type {[RPC]}
28
- */
29
- instances: [ ],
26
+ md: Middleware.handler,
30
27
 
31
28
 
32
29
 
33
30
  /**
34
- * all contexts map
35
- * @type {Map<String,Context>}
31
+ * @type {[RPC]}
36
32
  */
37
- contexts: new Map ( ),
33
+ instances: [ ],
38
34
 
39
35
 
40
36
 
@@ -106,34 +102,13 @@ const Global = {
106
102
 
107
103
  /**
108
104
  *
109
- * @param {String} stack
110
105
  * @returns {Context}
111
106
  */
112
- genContextByStack ( stack ) {
113
-
114
- if ( !stack ) {
115
-
116
- let trace = { }
117
-
118
- Error.captureStackTrace ( trace )
119
-
120
- stack = trace.stack
121
- }
122
-
123
- const traceId = getTraceIdByStack ( stack )
124
-
125
- if ( traceId ) {
126
-
127
- const sourceContext = this.contexts.get ( traceId )
128
-
129
- if ( sourceContext ) {
107
+ getContext ( ) {
130
108
 
131
- return this.genContext ( { traceId, sourceIP: sourceContext.sourceIP } )
132
- } else {
109
+ const context = this.asyncStore.getStore ( )
133
110
 
134
- return this.genContext ( { traceId } )
135
- }
136
- } else return this.genContext ( )
111
+ return context || this.genContext ( )
137
112
  },
138
113
  }
139
114
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "oox",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "distributed api service",
5
5
  "main": "index.js",
6
6
  "bin": {
@@ -11,7 +11,7 @@
11
11
  "license": "MIT",
12
12
  "dependencies": {
13
13
  "chalk": "^4.1.0",
14
- "socket.io": "^4.0.2",
15
- "socket.io-client": "^4.0.2"
14
+ "socket.io": "^4.4.0",
15
+ "socket.io-client": "^4.4.0"
16
16
  }
17
17
  }
package/rpc/http.class.js CHANGED
@@ -1,7 +1,7 @@
1
1
 
2
2
  const http = require ( 'http' )
3
3
 
4
- const { parseHTTPBody, getTraceIdByStack, getIPAddress, httpRequest } = require ( '../util' )
4
+ const { parseHTTPBody, getIPAddress, httpRequest } = require ( '../util' )
5
5
 
6
6
  const Context = require ( './context.class' )
7
7
 
@@ -238,17 +238,6 @@ module.exports = class HTTPModule {
238
238
 
239
239
  let formatString = ''
240
240
 
241
- if ( format instanceof Error ) {
242
-
243
- format = {
244
- success: false,
245
- error: {
246
- message: format.message,
247
- stack: format.stack
248
- }
249
- }
250
- }
251
-
252
241
  try {
253
242
 
254
243
  formatString = JSON.stringify ( format )
@@ -286,11 +275,7 @@ module.exports = class HTTPModule {
286
275
 
287
276
  if ( !context || !context.traceId ) {
288
277
 
289
- let trace = { }
290
-
291
- Error.captureStackTrace ( trace )
292
-
293
- context = Global.genContextByStack ( trace.stack )
278
+ context = Global.getContext ( )
294
279
  }
295
280
 
296
281
  const { traceId, caller, sourceIP } = context
package/rpc/rpc.class.js CHANGED
@@ -11,6 +11,8 @@ const Global = require ( '../global' )
11
11
 
12
12
  const RPCInterface = require ( './rpc.interface.class' )
13
13
 
14
+ const Middleware = require ( '../middleware' )
15
+
14
16
 
15
17
 
16
18
  module.exports = class RPC extends RPCInterface {
@@ -88,8 +90,6 @@ module.exports = class RPC extends RPCInterface {
88
90
 
89
91
  const { traceId } = context
90
92
 
91
- Global.contexts.set ( traceId, context )
92
-
93
93
  this.emit ( 'request', action, params, context )
94
94
 
95
95
  const format = {
@@ -97,11 +97,9 @@ module.exports = class RPC extends RPCInterface {
97
97
  success: false
98
98
  }
99
99
 
100
- const OOXTrace = genOOXTrace ( traceId, this.kvMethods )
101
-
102
100
  try {
103
101
 
104
- const result = await OOXTrace [ traceId ] ( action, [ ...params ], context )
102
+ const result = await this.execute ( this.kvMethods, action, [ ...params ], context )
105
103
 
106
104
  format.body = result
107
105
 
@@ -111,18 +109,123 @@ module.exports = class RPC extends RPCInterface {
111
109
  } catch ( error ) {
112
110
 
113
111
  format.error = {
114
- message: error.message,
115
- stack: error.stack
112
+ message: error.message,
113
+ stack: error.stack
116
114
  }
117
115
 
118
116
  this.emit ( 'fail', action, params, context, error )
119
117
  } finally {
120
118
 
121
- delete OOXTrace [ traceId ]
119
+ return format
120
+ }
121
+ }
122
122
 
123
- Global.contexts.delete ( traceId )
124
123
 
125
- return format
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 )
126
228
  }
127
229
  }
230
+
128
231
  }
@@ -160,11 +160,7 @@ module.exports = class SocketIOModule extends SocketIOClient {
160
160
 
161
161
  if ( !context || !context.traceId ) {
162
162
 
163
- let trace = { }
164
-
165
- Error.captureStackTrace ( trace )
166
-
167
- context = Global.genContextByStack ( trace.stack )
163
+ context = Global.getContext ( )
168
164
  }
169
165
 
170
166
  /**
@@ -216,11 +212,7 @@ module.exports = class SocketIOModule extends SocketIOClient {
216
212
 
217
213
  if ( !context || !context.traceId ) {
218
214
 
219
- let trace = { }
220
-
221
- Error.captureStackTrace ( trace )
222
-
223
- context = Global.genContextByStack ( trace.stack )
215
+ context = Global.getContext ( )
224
216
  }
225
217
 
226
218
  const { error, body } = await this.emit ( url, 'call', [ action, params, context ], context )
@@ -29,11 +29,7 @@ module.exports = class Service extends RPC {
29
29
 
30
30
  if ( !context || !context.traceId ) {
31
31
 
32
- let trace = { }
33
-
34
- Error.captureStackTrace ( trace )
35
-
36
- context = Global.genContextByStack ( trace.stack )
32
+ context = Global.getContext ( )
37
33
  }
38
34
 
39
35
  const socketIONodes = Global.socketIORegistry.get ( name )
@@ -52,11 +48,7 @@ module.exports = class Service extends RPC {
52
48
 
53
49
  if ( !context || !context.traceId ) {
54
50
 
55
- let trace = { }
56
-
57
- Error.captureStackTrace ( trace )
58
-
59
- context = Global.genContextByStack ( trace.stack )
51
+ context = Global.getContext ( )
60
52
  }
61
53
 
62
54
  const socketIONodes = Global.socketIORegistry.get ( name )
package/util.js CHANGED
@@ -5,8 +5,6 @@ const http = require ( 'http' )
5
5
 
6
6
  const https = require ( 'https' )
7
7
 
8
- const querystring = require('querystring')
9
-
10
8
  const Context = require ( './rpc/context.class' )
11
9
 
12
10
  const Middleware = require ( './middleware' )
@@ -223,151 +221,4 @@ exports.genKVMethods = function ( methods, kvMethods=new Map(), nameStack=[] ) {
223
221
  }
224
222
 
225
223
  return kvMethods
226
- }
227
-
228
-
229
-
230
- /**
231
- * parse traceId from execution stack
232
- * @param {String} stack
233
- * @returns {String}
234
- */
235
- exports.getTraceIdByStack = function ( stack ) {
236
-
237
- if ( !stack ) {
238
-
239
- let trace = { }
240
-
241
- Error.captureStackTrace ( trace )
242
-
243
- stack = trace.stack
244
- }
245
-
246
- const prefix = 'OOXTrace.<computed>'
247
-
248
- const index = stack.indexOf ( prefix )
249
-
250
- if ( index === -1 ) return null
251
-
252
- return stack.slice ( index ).match ( /(?<=as\s).*?(?=\s|\])/ ) [ 0 ]
253
- }
254
-
255
-
256
-
257
- /**
258
- *
259
- * @param {String} traceId 全局唯一请求ID
260
- * @param {Map<String,Function>} methods 服务函数扁平化列表
261
- */
262
- exports.genOOXTrace = function ( traceId, methods ) {
263
-
264
- const OOXTrace = { /* OOXTrace.<computed> [as traceId] */ }
265
-
266
-
267
-
268
- /**
269
- * trace magic function
270
- * @param {String} action
271
- * @param {Array} params
272
- * @param {Context} context
273
- */
274
- OOXTrace [ traceId ] = async function ( action, params, context ) {
275
-
276
- const __proxy = '__proxy', _proxy = '_proxy'
277
-
278
-
279
-
280
- // 目标函数
281
- const target = methods.get ( action )
282
-
283
- // 目标代理函数
284
- const targetProxy = methods.get ( action + _proxy )
285
-
286
- // 即不存在目标也不存在目标代理时, 报错函数不存在
287
- if ( !target && !targetProxy ) throw new Error ( 'Invalid Action [' + action + ']' )
288
-
289
-
290
-
291
- // 最顶层代理
292
- const topProxy = methods.get ( __proxy )
293
-
294
- if ( topProxy ) {
295
-
296
- const proxyReturns = await topProxy ( action, params, context )
297
-
298
- if ( proxyReturns !== undefined ) return proxyReturns
299
- }
300
-
301
-
302
-
303
- // 'x.y.z' => [ 'x', 'y', 'z' ]
304
- const nameStack = action.split ( '.' ), size = nameStack.length - 1
305
-
306
- let index = -1, proxyPrefix = ''
307
-
308
-
309
-
310
- // 根代理遍历
311
- while ( ++index < size ) {
312
-
313
- // x.
314
- // x.y.
315
- proxyPrefix += nameStack [ index ] + '.'
316
-
317
- // x.__proxy
318
- // x.y.__proxy
319
- const rootProxy = methods.get ( proxyPrefix + __proxy )
320
-
321
- // x.__proxy ( 'y.z', ... )
322
- // x.y.__proxy ( 'z', ... )
323
- if ( rootProxy ) {
324
-
325
- const proxyReturns = await rootProxy ( nameStack.slice ( index ).join ( '.' ), params, context )
326
-
327
- if ( proxyReturns !== undefined ) return proxyReturns
328
- }
329
- }
330
-
331
-
332
-
333
- // 同级代理
334
- const layerProxy = methods.get ( proxyPrefix + _proxy )
335
-
336
- if ( layerProxy ) {
337
-
338
- const proxyReturns = await layerProxy ( nameStack [ index ], params, context )
339
-
340
- if ( proxyReturns !== undefined ) return proxyReturns
341
- }
342
-
343
-
344
-
345
- if ( targetProxy ) {
346
-
347
- const proxyReturns = await targetProxy ( params, context )
348
-
349
- if ( proxyReturns !== undefined ) return proxyReturns
350
- }
351
-
352
-
353
-
354
- // make sure target action execute after all proxies
355
- if ( target ) {
356
-
357
- const sourceMethod = Middleware.wrappedActions.get ( action )
358
-
359
- const middlewareNames = Middleware.actionMiddlewares.get ( sourceMethod )
360
-
361
- if ( middlewareNames && middlewareNames.length ) for ( const name of middlewareNames ) {
362
-
363
- const middleware = Middleware.middlewares.get ( name )
364
-
365
- await middleware ( action, params, context )
366
- }
367
-
368
- return await target ( ...params )
369
- }
370
- }
371
-
372
- return OOXTrace
373
- }
224
+ }