oox 0.0.9 → 0.1.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,231 @@
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
+ 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
231
  }
@@ -1,86 +1,82 @@
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
- }
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
+ let trace = { }
33
+
34
+ Error.captureStackTrace ( trace )
35
+
36
+ context = Global.genContextByStack ( trace.stack )
37
+ }
38
+
39
+ const socketIONodes = Global.socketIORegistry.get ( name )
40
+
41
+ if ( socketIONodes.length ) {
42
+
43
+ const node = this.selectSocketIONode ( socketIONodes )
44
+
45
+ return this.SocketIO.call ( node, action, params, context )
46
+ } else throw new Error ( 'No running service as ' + name )
47
+ }
48
+
49
+
50
+
51
+ static async emit ( name, action, params, context ) {
52
+
53
+ if ( !context || !context.traceId ) {
54
+
55
+ let trace = { }
56
+
57
+ Error.captureStackTrace ( trace )
58
+
59
+ context = Global.genContextByStack ( trace.stack )
60
+ }
61
+
62
+ const socketIONodes = Global.socketIORegistry.get ( name )
63
+
64
+ if ( socketIONodes.length ) {
65
+
66
+ const node = this.selectSocketIONode ( socketIONodes )
67
+
68
+ return this.SocketIO.emit ( node, action, params, context )
69
+ } else throw new Error ( 'No running service as ' + name )
70
+ }
71
+
72
+
73
+
74
+ /**
75
+ *
76
+ * @param {string[]} nodes
77
+ */
78
+ static selectSocketIONode ( nodes ) {
79
+
80
+ return nodes [ Math.floor ( Math.random ( ) * nodes.length ) ]
81
+ }
86
82
  }