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.
@@ -1,231 +1,223 @@
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
- }
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
+ }
231
223
  }
@@ -1,77 +1,74 @@
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
- return this.SocketIO.emit ( id, 'proxyCall', [ targetId, action, params, context ], context )
65
- }
66
-
67
-
68
-
69
- /**
70
- *
71
- * @param {string[]} nodes
72
- */
73
- static selectSocketIONode ( nodes ) {
74
-
75
- return nodes [ Math.floor ( Math.random ( ) * nodes.length ) ]
76
- }
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
+ }
77
74
  }