oox 0.2.0 → 0.3.0-beta3

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 (46) 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 +55 -105
  9. package/bin/starter.js +81 -144
  10. package/index.js +144 -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 +81 -0
  15. package/modules/module.js +16 -0
  16. package/package.json +34 -16
  17. package/types/app.d.ts +37 -0
  18. package/types/bin/argv.d.ts +8 -0
  19. package/types/bin/cli.d.ts +2 -0
  20. package/types/bin/configurer.d.ts +1 -0
  21. package/types/bin/proxyer.d.ts +1 -0
  22. package/types/bin/register.d.ts +1 -0
  23. package/types/bin/starter.d.ts +1 -0
  24. package/types/index.d.ts +70 -0
  25. package/types/modules/http/index.d.ts +47 -0
  26. package/types/modules/http/utils.d.ts +17 -0
  27. package/types/modules/index.d.ts +22 -0
  28. package/types/modules/module.d.ts +13 -0
  29. package/types/utils.d.ts +6 -0
  30. package/utils.js +63 -0
  31. package/.gitattributes +0 -2
  32. package/global.js +0 -118
  33. package/middleware.js +0 -167
  34. package/rpc/config.class.js +0 -48
  35. package/rpc/context.class.js +0 -15
  36. package/rpc/http.class.js +0 -312
  37. package/rpc/rpc.class.js +0 -231
  38. package/rpc/rpc.interface.class.js +0 -119
  39. package/rpc/socketio.class.js +0 -223
  40. package/service/service.class.js +0 -74
  41. package/service/socketio.class.js +0 -145
  42. package/setMap.class.js +0 -67
  43. package/socketio/client.class.js +0 -190
  44. package/socketio/server.class.js +0 -222
  45. package/socketio/socket.class.js +0 -23
  46. package/util.js +0 -224
@@ -1,222 +0,0 @@
1
-
2
- const { getIPAddress } = require ( '../util' )
3
-
4
- const http = require ( 'http' )
5
-
6
- const SocketIO = require ( 'socket.io' )
7
-
8
- const Socket = require ( './socket.class' )
9
-
10
- const Global = require ( '../global' )
11
-
12
-
13
-
14
- module.exports = class SocketIOServer {
15
-
16
-
17
-
18
- /**
19
- * service name
20
- */
21
- name = ''
22
-
23
-
24
-
25
- /**
26
- * service host ip
27
- */
28
- host = getIPAddress ( 4 ) [ 0 ]
29
-
30
-
31
-
32
- /**
33
- * listen port
34
- */
35
- port = 0
36
-
37
-
38
-
39
- /**
40
- * socket.io service path
41
- */
42
- path = '/socket.io'
43
-
44
-
45
-
46
- /**
47
- * means this.server created by myself<SocketIOServer>
48
- */
49
- #isSelfServer = false
50
-
51
-
52
-
53
- /**
54
- * @type {http.Server}
55
- */
56
- server = null
57
-
58
-
59
-
60
- /**
61
- * @type {SocketIO.Server}
62
- */
63
- socketServer = null
64
-
65
-
66
-
67
- constructor ( name ) {
68
-
69
- this.name = name
70
-
71
- Global.socketIOServers.push ( this )
72
- }
73
-
74
-
75
-
76
- async serve ( ) {
77
-
78
- await this.stop ( )
79
-
80
- const port = this.port
81
-
82
- const isRndPort = 'number' !== typeof port || port === 0
83
-
84
- const isSelfServer = this.#isSelfServer = this.server ? true : false
85
-
86
- const server = this.server = isSelfServer ? this.server :
87
- http.createServer ( ( request, response ) => response.end ( 'No HTTP Gateway' ) )
88
-
89
- if ( !server.listening ) server.listen ( isRndPort ? 0 : port )
90
-
91
- this.port = server.address ( ).port
92
-
93
- this.createSocketIOServer ( )
94
- }
95
-
96
-
97
-
98
- async stop ( ) {
99
-
100
- if ( this.socketServer )
101
- await new Promise ( ( resolve, reject ) =>
102
- this.socketServer.close ( error => error ? reject ( error ) : resolve ( ) ) )
103
-
104
- if ( this.#isSelfServer )
105
- await new Promise ( ( resolve, reject ) => {
106
-
107
- this.server.close ( function ( error ) {
108
-
109
- if ( error ) reject ( error )
110
- else resolve ( )
111
- } )
112
- } )
113
- }
114
-
115
-
116
-
117
- genOptions ( ) {
118
-
119
- return {
120
- /**
121
- * name of the path to capture
122
- * @default "/socket.io"
123
- */
124
- path: this.path,
125
- /**
126
- * how many ms before a client without namespace is closed
127
- * @default 45000
128
- */
129
- connectTimeout: 5000,
130
- /**
131
- * how many ms without a pong packet to consider the connection closed
132
- * @default 5000
133
- */
134
- pingTimeout: 2000,
135
- /**
136
- * how many ms before sending a new ping packet
137
- * @default 25000
138
- */
139
- pingInterval: 10000,
140
- /**
141
- * how many bytes or characters a message can be, before closing the session (to avoid DoS).
142
- * @default 1e5 (100 KB)
143
- */
144
- maxHttpBufferSize: 1e5
145
- }
146
- }
147
-
148
-
149
-
150
- createSocketIOServer ( ) {
151
-
152
- const socketServer = this.socketServer = new SocketIO.Server ( this.server, this.genOptions ( ) )
153
-
154
- socketServer.on ( 'connection', async socket => {
155
-
156
- try {
157
-
158
- await this.onSocketConnection ( socket )
159
- } catch ( error ) {
160
-
161
- socket.send ( error.message ).disconnect ( true )
162
- }
163
- } )
164
- }
165
-
166
-
167
-
168
- /**
169
- * 服务端Socket连接事件
170
- * @param {Socket} socket
171
- */
172
- onSocketConnection ( socket ) {
173
-
174
- const headers = socket.handshake.headers
175
-
176
- const callerId = headers [ 'x-caller-id' ] || socket.id
177
-
178
- // 已经存在相同的连接
179
- if ( Global.sockets.has ( callerId ) ) throw new Error ( 'Connection Exists' )
180
-
181
- // client ip or caller service ip
182
- const ip = headers [ 'x-real-ip' ] || headers [ 'x-ip' ] || socket.handshake.address
183
-
184
- // service name
185
- const caller = headers [ 'x-caller' ] || 'anonymous'
186
-
187
- socket.data = { connected: true, host: ip, name: caller, id: callerId, owner: this.name }
188
-
189
- // 保存 callerId 与 socket 对应关系
190
- Global.sockets.set ( callerId, socket )
191
-
192
- socket.on ( 'disconnect', reason => this.onSocketDisconnect ( socket, new Error ( `SocketIO name[${caller}] id[${callerId}] ${reason}` ) ) )
193
-
194
- socket.emit ( 'oox_connected', { name: this.name } )
195
-
196
- this.onConnection ( socket )
197
- }
198
-
199
-
200
-
201
- onConnection ( socket ) { }
202
-
203
-
204
-
205
- /**
206
- * 服务端Socket断开事件
207
- * @param {Socket} socket
208
- * @param {Error} reason
209
- */
210
- onSocketDisconnect ( socket, reason ) {
211
-
212
- socket.data.connected = false
213
-
214
- Global.sockets.delete ( socket.data.id )
215
-
216
- this.onDisconnect ( socket, reason )
217
- }
218
-
219
-
220
-
221
- onDisconnect ( socket, reason ) { }
222
- }
@@ -1,23 +0,0 @@
1
-
2
- const SocketIO = require ( 'socket.io' )
3
-
4
- module.exports = class Socket extends SocketIO.Socket {
5
-
6
-
7
-
8
- static Data = {
9
- connected: false,
10
- host: '',
11
- name: '',
12
- id: '',
13
- owner: ''
14
- }
15
-
16
-
17
-
18
- /**
19
- * Socket 扩展连接信息
20
- * @type {Socket.Data}
21
- */
22
- data = Object.assign ( { }, Socket.Data )
23
- }
package/util.js DELETED
@@ -1,224 +0,0 @@
1
-
2
- const os = require ( 'os' )
3
-
4
- const http = require ( 'http' )
5
-
6
- const https = require ( 'https' )
7
-
8
- const Context = require ( './rpc/context.class' )
9
-
10
- const Middleware = require ( './middleware' )
11
-
12
-
13
-
14
- exports.getIPAddress = function ( version = 4 ) {
15
-
16
- const interfaces = os.networkInterfaces ( )
17
-
18
- const ip = [ ]
19
-
20
- for ( const name of Object.keys ( interfaces ) )
21
- for ( const intf of interfaces [ name ] )
22
- if ( intf.mac !== '00:00:00:00:00:00' )
23
- if ( ( version !== 4 && version !== 6 ) || 'IPv' + version === intf.family )
24
- ip.push ( intf.address )
25
-
26
- if ( !ip.length ) {
27
- if ( version !== 6 ) ip.push ( '127.0.0.1' )
28
- if ( version !== 4 ) ip.push ( '::1' )
29
- }
30
-
31
- return ip
32
- }
33
-
34
-
35
-
36
- /**
37
- * String => Buffer
38
- * @param {*} stream
39
- * @param {*} totalLength
40
- * @returns {Promise<Buffer>}
41
- */
42
- exports.stream2buffer = function ( stream, totalLength=0 ) {
43
-
44
- return new Promise ( function ( resolve, reject ) {
45
-
46
- let buffers = [ ]
47
-
48
- stream.on ( 'error', reject )
49
-
50
- if ( totalLength ) {
51
-
52
- stream.on ( 'data', function ( data ) { buffers.push ( data ) } )
53
- } else {
54
-
55
- stream.on ( 'data', function ( data ) {
56
- buffers.push ( data )
57
- totalLength += data.length
58
- } )
59
- }
60
-
61
- stream.on ( 'end', function ( ) { resolve ( Buffer.concat ( buffers, totalLength ) ) } )
62
- } )
63
- }
64
-
65
-
66
-
67
- /**
68
- * Request => JSONObject
69
- * @param {http.IncomingMessage} request
70
- */
71
- exports.parseHTTPBody = async function ( request ) {
72
-
73
- if ( request.method === 'GET' ) return null
74
-
75
- let contentType = request.headers [ 'content-type' ]
76
-
77
- // application/json; charset=utf-8
78
- if ( contentType ) contentType = contentType.split ( ';' ) [ 0 ].trim ( )
79
-
80
- const contentSize = request.headers [ 'content-length' ]
81
-
82
- const buffer = await exports.stream2buffer ( request, +contentSize || 0 )
83
-
84
- if ( contentSize && buffer.length !== +contentSize ) throw new Error ( 'Content-Length Incorrect' )
85
-
86
- const bodyString = buffer.toString ( )
87
-
88
- /*
89
- if ( 'application/x-www-form-urlencoded' === contentType )
90
- return querystring.parse ( bodyString )
91
- */
92
-
93
- try {
94
-
95
- return JSON.parse ( bodyString )
96
- } catch ( error ) {
97
-
98
- return bodyString
99
- }
100
- }
101
-
102
-
103
-
104
- /**
105
- * http request
106
- * @param {URL|String|http.RequestOptions} url
107
- * @param {http.RequestOptions|String} options
108
- * @param {String} body
109
- */
110
- exports.httpRequest = function ( url, options, body ) {
111
-
112
- if ( 'string' === typeof options ) {
113
-
114
- body = options
115
- options = { }
116
- }
117
-
118
- return new Promise ( function ( resolve, reject ) {
119
-
120
- const request = http.request ( url, options, async function ( response ) {
121
-
122
- try {
123
-
124
- const result = await exports.parseHTTPBody ( response )
125
-
126
- resolve ( result )
127
- } catch ( error ) {
128
-
129
- const decoration = new Error ( `${response.statusCode} - ${error.message}` )
130
-
131
- reject ( decoration )
132
- }
133
- } )
134
-
135
- request.on ( 'error', reject )
136
-
137
- if ( body ) {
138
-
139
- request.method = 'POST'
140
-
141
- request.setHeader ( 'Content-Type', 'application/json' )
142
-
143
- request.setHeader ( 'Content-Length', Buffer.byteLength ( body ) )
144
-
145
- request.write ( body )
146
- }
147
-
148
- request.end ( )
149
- } )
150
- }
151
-
152
-
153
-
154
- exports.getAllCallablePropertyNames = function ( obj ) {
155
-
156
- if ( !obj ) return [ ]
157
-
158
- let props = [ ], tmpProps = [ ], index = 0, size = 0, tmpProp = ''
159
-
160
- const bans = [ "constructor", "__defineGetter__", "__defineSetter__"
161
- , "hasOwnProperty", "__lookupGetter__", "__lookupSetter__"
162
- , "isPrototypeOf", "propertyIsEnumerable", "toString"
163
- , "valueOf", "__proto__", "toLocaleString" ]
164
-
165
- do {
166
-
167
- tmpProps = Object.getOwnPropertyNames ( obj )
168
-
169
- index = -1, size = tmpProps.length
170
-
171
- while ( ++index < size ) {
172
-
173
- tmpProp = tmpProps [ index ]
174
-
175
- if ( !props.includes ( tmpProp ) && !bans.includes ( tmpProp ) ) {
176
-
177
- props.push ( tmpProp )
178
- }
179
- }
180
-
181
- } while ( obj = Object.getPrototypeOf ( obj ) )
182
-
183
- return props
184
- }
185
-
186
-
187
-
188
- /**
189
- *
190
- * @param {Object} methods
191
- * @param {Map<String,Function>} kvMethods
192
- * @param {Array<String>} nameStack
193
- */
194
- exports.genKVMethods = function ( methods, kvMethods=new Map(), nameStack=[] ) {
195
-
196
- let keys = exports.getAllCallablePropertyNames ( methods )
197
-
198
- let index = -1, size = keys.length
199
-
200
- while ( ++index < size ) {
201
-
202
- let key = keys [ index ]
203
-
204
- /**
205
- * @type {Function}
206
- */
207
- let val = methods [ key ]
208
-
209
- if ( 'function' === typeof val ) {
210
-
211
- const action = nameStack.concat ( key ).join ( '.' )
212
-
213
- // 中間件函數脫殼綁定
214
- Middleware.wrappedActions.set ( action, val )
215
-
216
- kvMethods.set ( action, val.bind ( methods ) )
217
- } else {
218
-
219
- exports.genKVMethods ( val, kvMethods, nameStack.concat ( key ) )
220
- }
221
- }
222
-
223
- return kvMethods
224
- }