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