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,191 +1,145 @@
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 = new Context ( )
173
-
174
- context.traceId = contextPrev.traceId
175
- context.sourceIP = socket.data.host
176
-
177
- try {
178
-
179
- return await this.emit ( id, 'call', [ action, params, context ], context )
180
- } catch ( error ) {
181
-
182
- return {
183
- success: false,
184
- error: {
185
- message: error.message,
186
- stack: error.stack
187
- }
188
- }
189
- }
190
- }
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
+ }
191
145
  }
package/setMap.class.js CHANGED
@@ -1,67 +1,67 @@
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
+
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
+ }