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.
Files changed (53) hide show
  1. package/README.md +4 -1
  2. package/app.js +142 -0
  3. package/bin/argv.js +59 -84
  4. package/bin/cli.js +39 -54
  5. package/bin/configurer.js +37 -66
  6. package/bin/proxyer.js +54 -88
  7. package/bin/register.js +44 -95
  8. package/bin/starter.js +66 -129
  9. package/index.js +149 -9
  10. package/index.mjs +4 -0
  11. package/modules/http/index.js +180 -0
  12. package/modules/http/utils.js +73 -0
  13. package/modules/index.js +88 -0
  14. package/modules/module.js +16 -0
  15. package/modules/socketio/client.js +101 -0
  16. package/modules/socketio/index.js +148 -0
  17. package/modules/socketio/server.js +130 -0
  18. package/modules/socketio/socket.js +4 -0
  19. package/package.json +35 -17
  20. package/types/app.d.ts +37 -0
  21. package/types/bin/argv.d.ts +8 -0
  22. package/types/bin/cli.d.ts +2 -0
  23. package/types/bin/configurer.d.ts +1 -0
  24. package/types/bin/proxyer.d.ts +1 -0
  25. package/types/bin/register.d.ts +1 -0
  26. package/types/bin/starter.d.ts +1 -0
  27. package/types/index.d.ts +70 -0
  28. package/types/modules/http/index.d.ts +47 -0
  29. package/types/modules/http/utils.d.ts +17 -0
  30. package/types/modules/index.d.ts +23 -0
  31. package/types/modules/module.d.ts +11 -0
  32. package/types/modules/socketio/client.d.ts +23 -0
  33. package/types/modules/socketio/index.d.ts +35 -0
  34. package/types/modules/socketio/server.d.ts +35 -0
  35. package/types/modules/socketio/socket.d.ts +11 -0
  36. package/types/utils.d.ts +6 -0
  37. package/utils.js +63 -0
  38. package/.gitattributes +0 -2
  39. package/global.class.js +0 -124
  40. package/global.js +0 -13
  41. package/rpc/config.class.js +0 -48
  42. package/rpc/context.class.js +0 -15
  43. package/rpc/http.class.js +0 -327
  44. package/rpc/rpc.class.js +0 -130
  45. package/rpc/rpc.interface.class.js +0 -121
  46. package/rpc/socketio.class.js +0 -231
  47. package/service/service.class.js +0 -86
  48. package/service/socketio.class.js +0 -188
  49. package/setMap.class.js +0 -67
  50. package/socketio/client.class.js +0 -190
  51. package/socketio/server.class.js +0 -222
  52. package/socketio/socket.class.js +0 -23
  53. package/util.js +0 -352
@@ -1,121 +0,0 @@
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,231 +0,0 @@
1
-
2
- const SocketIOClient = require ( '../socketio/client.class' )
3
-
4
- const Context = require ( './context.class' )
5
-
6
- const RPC = require ( './rpc.interface.class' )
7
-
8
- const Socket = require ( '../socketio/socket.class' )
9
-
10
- const Global = require ( '../global' )
11
-
12
-
13
-
14
- module.exports = class SocketIOModule extends SocketIOClient {
15
-
16
-
17
-
18
- /**
19
- * @type {RPC}
20
- */
21
- rpc = null
22
-
23
-
24
-
25
- /**
26
- * @param config {{port:Number, path:String}}
27
- */
28
- set config ( config ) {
29
-
30
- if ( !config ) return this.rpc.config.gateway.socketio = null
31
-
32
- config = this.rpc.config.gateway.socketio = Object.assign ( this.config || { }, config )
33
-
34
- this.port = config.port || 0
35
-
36
- if ( config.path ) this.path = config.path
37
- else config.path = this.path
38
- }
39
-
40
-
41
-
42
- get config ( ) {
43
-
44
- return this.rpc.config.gateway.socketio
45
- }
46
-
47
-
48
-
49
- /**
50
- *
51
- * @param {RPC} rpc
52
- */
53
- constructor ( rpc ) {
54
-
55
- super ( rpc.name )
56
-
57
- this.rpc = rpc
58
-
59
- this.host = rpc.config.host
60
- }
61
-
62
-
63
-
64
- async serve ( ) {
65
-
66
- if ( !this.config ) return
67
-
68
- await super.serve ( )
69
-
70
- this.config.port = this.port
71
- this.config.path = this.path
72
- }
73
-
74
-
75
-
76
- async stop ( ) {
77
-
78
- await super.stop ( )
79
- }
80
-
81
-
82
-
83
- genOptions ( ) {
84
-
85
- const options = super.genOptions ( )
86
-
87
- if ( this.rpc.config.origin ) {
88
-
89
- const origin = this.rpc.config.origin
90
-
91
- options.cors = {
92
- origin
93
- }
94
- }
95
-
96
- return options
97
- }
98
-
99
-
100
-
101
- /**
102
- *
103
- * @param {Socket} socket
104
- */
105
- onConnection ( socket ) {
106
-
107
- super.onConnection ( socket )
108
-
109
- socket.setMaxListeners ( 0 )
110
-
111
- const connectionContext = {
112
- ip: socket.data.host,
113
- caller: socket.data.name,
114
- callerId: socket.data.id,
115
- }
116
-
117
- socket.on ( 'call', async ( action, params, context, callback ) => {
118
-
119
- if ( 'object' !== typeof context ) context = Global.genContext ( connectionContext )
120
- else context = Global.genContext ( Object.assign ( context, connectionContext ) )
121
-
122
- this.call ( action, params, context, callback )
123
- } )
124
- }
125
-
126
-
127
-
128
- async call ( action, params, context, callback ) {
129
-
130
- const format = await this.rpc.call ( action, params, context )
131
-
132
- 'function' === typeof callback && callback ( format )
133
- }
134
-
135
-
136
-
137
- /**
138
- *
139
- * @param {Socket} socket
140
- */
141
- static onConnection ( socket ) {
142
-
143
- super.onConnection ( socket )
144
-
145
- const [ callerServer ] = Global.socketIOServers.filter ( server => server.name === socket.data.owner )
146
-
147
- if ( callerServer ) socket.on ( 'call', callerServer.call.bind ( callerServer ) )
148
- }
149
-
150
-
151
-
152
- /**
153
- * RPC emit
154
- * @param {String} url
155
- * @param {String} action 函数名称
156
- * @param {[]} params 参数列表
157
- * @param {Context} context 上下文
158
- */
159
- static async emit ( url, action, params, context ) {
160
-
161
- if ( !context || !context.traceId ) {
162
-
163
- let trace = { }
164
-
165
- Error.captureStackTrace ( trace )
166
-
167
- context = Global.genContextByStack ( trace.stack )
168
- }
169
-
170
- /**
171
- * @type {Socket}
172
- */
173
- let socket = null
174
-
175
- try {
176
-
177
- socket = await this.connect ( url, context.caller )
178
- } catch ( error ) {
179
-
180
- // try again
181
- socket = await this.connect ( url, context.caller )
182
- }
183
-
184
- try {
185
-
186
- return await new Promise ( ( resolve, reject ) => {
187
-
188
- const onError = reason => reject ( new Error ( `SocketIO name[${socket.data.name}] id[${socket.data.id}] ` + ( 'string' === typeof reason ? reason : reason.message ) ) )
189
-
190
- // RPC 执行时中断连接
191
- socket.once ( 'disconnect', onError )
192
-
193
- socket.emit ( action, ...params, format => {
194
-
195
- socket.off ( 'disconnect', onError )
196
-
197
- resolve ( format )
198
- } )
199
- } )
200
- } catch ( error ) {
201
-
202
- throw new Error ( error.message )
203
- }
204
- }
205
-
206
-
207
-
208
- /**
209
- * HTTP RPC
210
- * @param {String} url
211
- * @param {String} action 函数名称
212
- * @param {[]} params 参数列表
213
- * @param {Context} context 上下文
214
- */
215
- static async call ( url, action, params, context ) {
216
-
217
- if ( !context || !context.traceId ) {
218
-
219
- let trace = { }
220
-
221
- Error.captureStackTrace ( trace )
222
-
223
- context = Global.genContextByStack ( trace.stack )
224
- }
225
-
226
- const { error, body } = await this.emit ( url, 'call', [ action, params, context ], context )
227
-
228
- if ( error ) throw new Error ( error.message )
229
- else return body
230
- }
231
- }
@@ -1,86 +0,0 @@
1
-
2
- const Global = require ( '../global' )
3
-
4
- const RPC = require ( '../rpc/rpc.class' )
5
-
6
- const SocketIO = require ( './socketio.class' )
7
-
8
- module.exports = class Service extends RPC {
9
-
10
-
11
-
12
- static SocketIO = SocketIO
13
-
14
-
15
-
16
- static async call ( name, action, params, context ) {
17
-
18
- if ( !context || !context.traceId ) {
19
-
20
- let trace = { }
21
-
22
- Error.captureStackTrace ( trace )
23
-
24
- context = Global.genContextByStack ( trace.stack )
25
- }
26
-
27
- const socketIONodes = Global.socketIORegistry.get ( name )
28
-
29
- if ( socketIONodes.length ) {
30
-
31
- const node = this.selectSocketIONode ( socketIONodes )
32
-
33
- return this.SocketIO.call ( node, action, params, context )
34
- } else throw new Error ( 'No running service as ' + name )
35
- }
36
-
37
-
38
-
39
- static async emit ( name, action, params, context ) {
40
-
41
- if ( !context || !context.traceId ) {
42
-
43
- let trace = { }
44
-
45
- Error.captureStackTrace ( trace )
46
-
47
- context = Global.genContextByStack ( trace.stack )
48
- }
49
-
50
- const socketIONodes = Global.socketIORegistry.get ( name )
51
-
52
- if ( socketIONodes.length ) {
53
-
54
- const node = this.selectSocketIONode ( socketIONodes )
55
-
56
- return this.SocketIO.emit ( node, action, params, context )
57
- } else throw new Error ( 'No running service as ' + name )
58
- }
59
-
60
-
61
-
62
- static async proxyCallById ( id, targetId, action, params, context ) {
63
-
64
- if ( !context || !context.traceId ) {
65
-
66
- let trace = { }
67
-
68
- Error.captureStackTrace ( trace )
69
-
70
- context = Global.genContextByStack ( trace.stack )
71
- }
72
-
73
- return this.SocketIO.emit ( id, 'proxyCall', [ targetId, action, params, context ], context )
74
- }
75
-
76
-
77
-
78
- /**
79
- *
80
- * @param {string[]} nodes
81
- */
82
- static selectSocketIONode ( nodes ) {
83
-
84
- return nodes [ Math.floor ( Math.random ( ) * nodes.length ) ]
85
- }
86
- }
@@ -1,188 +0,0 @@
1
-
2
- const RPCSocketIO = require ( '../rpc/socketio.class' )
3
-
4
- const Socket = require ( '../socketio/socket.class' )
5
-
6
- const Context = require ( '../rpc/context.class' )
7
-
8
- const Global = require ( '../global' )
9
-
10
-
11
-
12
- module.exports = class SocketIO extends RPCSocketIO {
13
-
14
-
15
-
16
- /**
17
- *
18
- * @param {Socket} socket
19
- * @returns
20
- */
21
- onConnection ( socket ) {
22
-
23
- super.onConnection ( socket )
24
-
25
- Global.socketIORegistry.add ( socket.data.name, socket.data.id )
26
-
27
- socket.on ( 'syncConnection', async fn => {
28
-
29
- if ( 'function' !== typeof fn ) return
30
-
31
- const data = await this.onSyncConnection ( socket )
32
-
33
- fn ( data )
34
- } )
35
-
36
- socket.on ( 'fetchActions', async ( search, fn ) => {
37
-
38
- if ( 'function' !== typeof fn ) return
39
-
40
- const data = await this.constructor.onFetchActions ( socket, search )
41
-
42
- fn ( data )
43
- } )
44
-
45
- socket.on ( 'proxyCall', async ( id, action, params, context, fn ) => {
46
-
47
- const result = await this.constructor.onProxyCall ( socket, id, action, params, context )
48
-
49
- if ( 'function' === typeof fn ) fn ( result )
50
- } )
51
- }
52
-
53
-
54
-
55
- /**
56
- *
57
- * @param {Socket} socket
58
- * @returns
59
- */
60
- onSyncConnection ( socket ) {
61
-
62
- const sockets = Array.from ( Global.sockets.values ( ) )
63
- .filter ( s =>
64
- s !== socket &&
65
- s.data.name !== socket.data.name &&
66
- s.data.id.startsWith ( 'ws://' ) )
67
-
68
- return sockets.map ( s => s.data )
69
- }
70
-
71
-
72
-
73
- /**
74
- *
75
- * @param {Socket} socket
76
- */
77
- onDisconnect ( socket, reason ) {
78
-
79
- super.onDisconnect ( socket, reason )
80
-
81
- Global.socketIORegistry.delete ( socket.data.name, socket.data.id )
82
- }
83
-
84
-
85
-
86
- /**
87
- *
88
- * @param {Socket} socket
89
- */
90
- static onConnection ( socket ) {
91
-
92
- super.onConnection ( socket )
93
-
94
- Global.socketIORegistry.add ( socket.data.name, socket.data.id )
95
-
96
- socket.emit ( 'syncConnection', socketDatas => this.onSyncConnection ( socket, socketDatas ) )
97
-
98
- socket.on ( 'fetchActions', async ( search, fn ) => {
99
-
100
- if ( 'function' !== typeof fn ) return
101
-
102
- const actions = await this.onFetchActions ( socket, search )
103
-
104
- fn ( actions )
105
- } )
106
-
107
- socket.on ( 'proxyCall', async ( id, action, params, context, fn ) => {
108
-
109
- const result = await this.onProxyCall ( socket, id, action, params, context )
110
-
111
- if ( 'function' === typeof fn ) fn ( result )
112
- } )
113
- }
114
-
115
-
116
-
117
- static onDisconnect ( socket, reason ) {
118
-
119
- super.onDisconnect ( socket, reason )
120
-
121
- Global.socketIORegistry.delete ( socket.data.name, socket.data.id )
122
- }
123
-
124
-
125
-
126
- /**
127
- *
128
- * @param {Socket} socket 是由哪个通道发送过来的
129
- * @param {Socket.Data[]} socketDatas
130
- */
131
- static onSyncConnection ( socket, socketDatas ) {
132
-
133
- for ( const socketData of socketDatas )
134
- if ( !Global.sockets.has ( socketData.id ) )
135
- this.connect ( socketData.id, socket.data.owner ).catch ( error => console.error ( error ) )
136
- }
137
-
138
-
139
-
140
- /**
141
- *
142
- * @param {Socket} socket
143
- * @param {String} search
144
- * @returns {String[]}
145
- */
146
- static onFetchActions ( socket, search ) {
147
-
148
- const data = [ ]
149
-
150
- const [ service ] = Global.instances.filter ( service => service.name === socket.data.owner )
151
-
152
- if ( !service ) return data
153
-
154
- for ( const key of service.kvMethods.keys ( ) )
155
- if ( key.includes ( search ) ) data.push ( key )
156
-
157
- return data
158
- }
159
-
160
-
161
-
162
- /**
163
- *
164
- * @param {Socket} socket
165
- * @param {String} id
166
- * @param {String} action
167
- * @param {Context} contextPrev
168
- * @param {[]} params
169
- */
170
- static async onProxyCall ( socket, id, action, params, contextPrev ) {
171
-
172
- const context = Global.genContext ( contextPrev )
173
-
174
- try {
175
-
176
- return await this.call ( id, action, params, context )
177
- } catch ( error ) {
178
-
179
- return {
180
- success: false,
181
- error: {
182
- message: error.message,
183
- stack: error.stack
184
- }
185
- }
186
- }
187
- }
188
- }
package/setMap.class.js DELETED
@@ -1,67 +0,0 @@
1
-
2
- module.exports = class SetMap {
3
-
4
-
5
-
6
- /**
7
- * @type {Map<String,Set>}
8
- */
9
- map = new Map ( )
10
-
11
-
12
-
13
- has ( name, node ) {
14
-
15
- if ( !this.map.has ( name ) ) return false
16
-
17
- if ( !node ) return true
18
-
19
- const set = this.map.get ( name )
20
-
21
- return set.has ( node )
22
- }
23
-
24
-
25
-
26
- add ( name, node ) {
27
-
28
- if ( !this.map.has ( name ) ) {
29
-
30
- this.map.set ( name, new Set ( [ node ] ) )
31
- } else {
32
-
33
- this.map.get ( name ).add ( node )
34
- }
35
- }
36
-
37
-
38
-
39
- delete ( name, node ) {
40
-
41
- if ( !this.map.has ( name ) ) return
42
-
43
- if ( !node ) return void this.map.get ( name ).clear ( )
44
-
45
- this.map.get ( name ).delete ( node )
46
- }
47
-
48
-
49
-
50
- get ( name ) {
51
-
52
- if ( !name ) {
53
-
54
- const result = Object.create ( null )
55
-
56
- for ( const key of this.map.keys ( ) ) {
57
-
58
- result [ key ] = this.get ( key )
59
- }
60
-
61
- return result
62
- } else if ( this.map.has ( name ) ) {
63
-
64
- return Array.from ( this.map.get ( name ).values ( ) )
65
- } else return [ ]
66
- }
67
- }