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.
- package/.gitattributes +2 -2
- package/LICENSE +21 -21
- package/README.md +29 -29
- package/bin/argv.js +95 -95
- package/bin/cli.js +57 -57
- package/bin/configurer.js +77 -77
- package/bin/proxyer.js +94 -94
- package/bin/register.js +104 -104
- package/bin/starter.js +143 -139
- package/global.js +118 -13
- package/index.js +5 -8
- package/middleware.js +167 -0
- package/package.json +17 -17
- package/rpc/config.class.js +47 -47
- package/rpc/context.class.js +14 -14
- package/rpc/http.class.js +311 -326
- package/rpc/rpc.class.js +231 -130
- package/rpc/rpc.interface.class.js +119 -121
- package/rpc/socketio.class.js +222 -230
- package/service/service.class.js +73 -76
- package/service/socketio.class.js +144 -190
- package/setMap.class.js +67 -67
- package/socketio/client.class.js +189 -189
- package/socketio/server.class.js +221 -221
- package/socketio/socket.class.js +22 -22
- package/util.js +224 -352
- package/global.class.js +0 -124
package/bin/proxyer.js
CHANGED
|
@@ -1,95 +1,95 @@
|
|
|
1
|
-
|
|
2
|
-
const fs = require ( 'fs' )
|
|
3
|
-
|
|
4
|
-
const path = require ( 'path' )
|
|
5
|
-
|
|
6
|
-
var Service = require ( '../service/service.class' )
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* 重写 require 缓存
|
|
12
|
-
* @param {String} id
|
|
13
|
-
* @param {*} exports
|
|
14
|
-
*/
|
|
15
|
-
function rewriteModuleCache ( id, exports ) {
|
|
16
|
-
|
|
17
|
-
const pathname = id.split ( path.sep ).slice ( 0, -1 ).join ( path.sep )
|
|
18
|
-
|
|
19
|
-
const pathSplit = pathname.split ( path.sep )
|
|
20
|
-
|
|
21
|
-
const paths = pathSplit.map ( ( v, i, a ) => a.slice ( 0, i + 1 ).concat ( 'node_modules' ).join ( path.sep ) ).reverse ( )
|
|
22
|
-
|
|
23
|
-
const m = new module.constructor ( )
|
|
24
|
-
|
|
25
|
-
m.id = id
|
|
26
|
-
m.path = pathname
|
|
27
|
-
m.exports = exports
|
|
28
|
-
m.filename = m.id
|
|
29
|
-
m.loaded = true
|
|
30
|
-
m.children = [ ]
|
|
31
|
-
m.paths = paths
|
|
32
|
-
m.parent = null
|
|
33
|
-
|
|
34
|
-
require.cache [ id ] = m
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
function dotCall ( name, paths = [ ] ) {
|
|
40
|
-
|
|
41
|
-
return new Proxy ( function ( ) { }, {
|
|
42
|
-
|
|
43
|
-
get ( target, key ) {
|
|
44
|
-
|
|
45
|
-
return dotCall ( name, paths.concat ( key ) )
|
|
46
|
-
},
|
|
47
|
-
|
|
48
|
-
has ( target, key ) { return true },
|
|
49
|
-
|
|
50
|
-
apply ( target, thisObject, args ) {
|
|
51
|
-
|
|
52
|
-
return Service.call ( name, paths.join ( '.' ), args )
|
|
53
|
-
}
|
|
54
|
-
} )
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
exports.proxyServices = ( servicesDirectory, excludes = [ ] ) => {
|
|
60
|
-
|
|
61
|
-
if ( !servicesDirectory ) return
|
|
62
|
-
|
|
63
|
-
const directory = path.resolve ( servicesDirectory )
|
|
64
|
-
|
|
65
|
-
const stat = fs.statSync ( directory )
|
|
66
|
-
|
|
67
|
-
if ( !stat.isDirectory ( ) ) throw new Error ( 'services must be directory' )
|
|
68
|
-
|
|
69
|
-
const subs = fs.readdirSync ( directory )
|
|
70
|
-
|
|
71
|
-
for ( const filename of subs ) {
|
|
72
|
-
|
|
73
|
-
const fullPath = path.resolve ( directory + path.sep + filename )
|
|
74
|
-
|
|
75
|
-
const stat = fs.statSync ( fullPath )
|
|
76
|
-
|
|
77
|
-
let id = '', name = ''
|
|
78
|
-
|
|
79
|
-
if ( stat.isDirectory ( ) && fs.existsSync ( fullPath + '/index.js' ) ) {
|
|
80
|
-
|
|
81
|
-
id = fullPath + '/index.js'
|
|
82
|
-
name = filename
|
|
83
|
-
} else if ( filename.endsWith ( '.js' ) ) {
|
|
84
|
-
|
|
85
|
-
id = fullPath
|
|
86
|
-
name = filename.split ( '.js' ) [ 0 ]
|
|
87
|
-
} else continue
|
|
88
|
-
|
|
89
|
-
if ( !excludes.includes ( name ) ) rewriteModuleCache ( id, dotCall ( name ) )
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
1
|
+
|
|
2
|
+
const fs = require ( 'fs' )
|
|
3
|
+
|
|
4
|
+
const path = require ( 'path' )
|
|
5
|
+
|
|
6
|
+
var Service = require ( '../service/service.class' )
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* 重写 require 缓存
|
|
12
|
+
* @param {String} id
|
|
13
|
+
* @param {*} exports
|
|
14
|
+
*/
|
|
15
|
+
function rewriteModuleCache ( id, exports ) {
|
|
16
|
+
|
|
17
|
+
const pathname = id.split ( path.sep ).slice ( 0, -1 ).join ( path.sep )
|
|
18
|
+
|
|
19
|
+
const pathSplit = pathname.split ( path.sep )
|
|
20
|
+
|
|
21
|
+
const paths = pathSplit.map ( ( v, i, a ) => a.slice ( 0, i + 1 ).concat ( 'node_modules' ).join ( path.sep ) ).reverse ( )
|
|
22
|
+
|
|
23
|
+
const m = new module.constructor ( )
|
|
24
|
+
|
|
25
|
+
m.id = id
|
|
26
|
+
m.path = pathname
|
|
27
|
+
m.exports = exports
|
|
28
|
+
m.filename = m.id
|
|
29
|
+
m.loaded = true
|
|
30
|
+
m.children = [ ]
|
|
31
|
+
m.paths = paths
|
|
32
|
+
m.parent = null
|
|
33
|
+
|
|
34
|
+
require.cache [ id ] = m
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
function dotCall ( name, paths = [ ] ) {
|
|
40
|
+
|
|
41
|
+
return new Proxy ( function ( ) { }, {
|
|
42
|
+
|
|
43
|
+
get ( target, key ) {
|
|
44
|
+
|
|
45
|
+
return dotCall ( name, paths.concat ( key ) )
|
|
46
|
+
},
|
|
47
|
+
|
|
48
|
+
has ( target, key ) { return true },
|
|
49
|
+
|
|
50
|
+
apply ( target, thisObject, args ) {
|
|
51
|
+
|
|
52
|
+
return Service.call ( name, paths.join ( '.' ), args )
|
|
53
|
+
}
|
|
54
|
+
} )
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
exports.proxyServices = ( servicesDirectory, excludes = [ ] ) => {
|
|
60
|
+
|
|
61
|
+
if ( !servicesDirectory ) return
|
|
62
|
+
|
|
63
|
+
const directory = path.resolve ( servicesDirectory )
|
|
64
|
+
|
|
65
|
+
const stat = fs.statSync ( directory )
|
|
66
|
+
|
|
67
|
+
if ( !stat.isDirectory ( ) ) throw new Error ( 'services must be directory' )
|
|
68
|
+
|
|
69
|
+
const subs = fs.readdirSync ( directory )
|
|
70
|
+
|
|
71
|
+
for ( const filename of subs ) {
|
|
72
|
+
|
|
73
|
+
const fullPath = path.resolve ( directory + path.sep + filename )
|
|
74
|
+
|
|
75
|
+
const stat = fs.statSync ( fullPath )
|
|
76
|
+
|
|
77
|
+
let id = '', name = ''
|
|
78
|
+
|
|
79
|
+
if ( stat.isDirectory ( ) && fs.existsSync ( fullPath + '/index.js' ) ) {
|
|
80
|
+
|
|
81
|
+
id = fullPath + '/index.js'
|
|
82
|
+
name = filename
|
|
83
|
+
} else if ( filename.endsWith ( '.js' ) ) {
|
|
84
|
+
|
|
85
|
+
id = fullPath
|
|
86
|
+
name = filename.split ( '.js' ) [ 0 ]
|
|
87
|
+
} else continue
|
|
88
|
+
|
|
89
|
+
if ( !excludes.includes ( name ) ) rewriteModuleCache ( id, dotCall ( name ) )
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
|
|
95
95
|
exports.setService = serviceClazz => Service = serviceClazz
|
package/bin/register.js
CHANGED
|
@@ -1,105 +1,105 @@
|
|
|
1
|
-
|
|
2
|
-
const chalk = require ( 'chalk' )
|
|
3
|
-
|
|
4
|
-
var Service = require ( '../service/service.class' )
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
function delay ( ms ) {
|
|
9
|
-
|
|
10
|
-
return new Promise ( function ( resolve, reject ) {
|
|
11
|
-
|
|
12
|
-
setTimeout ( resolve, ms )
|
|
13
|
-
} )
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
function urlFormatter ( service, url ) {
|
|
19
|
-
|
|
20
|
-
// :6000
|
|
21
|
-
if ( url.startsWith ( ':' ) ) url = service.config.host + url
|
|
22
|
-
|
|
23
|
-
// http://127.0.0.1:6000
|
|
24
|
-
if ( url.startsWith ( 'http://' ) ) url = url.substr ( 7 )
|
|
25
|
-
|
|
26
|
-
// 127.0.0.1:6000
|
|
27
|
-
if ( !url.startsWith ( 'ws://' ) ) url = 'ws://' + url
|
|
28
|
-
|
|
29
|
-
const urlObject = new URL ( url )
|
|
30
|
-
|
|
31
|
-
if ( urlObject.pathname === '/' && !url.endsWith ( '/' ) ) url = url + '/socket.io'
|
|
32
|
-
|
|
33
|
-
return url
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
*
|
|
39
|
-
* @param {Service} service
|
|
40
|
-
* @param {String} url
|
|
41
|
-
*/
|
|
42
|
-
async function connect ( service, url, prevError = null ) {
|
|
43
|
-
|
|
44
|
-
if ( service.socketio.config ) {
|
|
45
|
-
|
|
46
|
-
const { port, path } = service.socketio.config
|
|
47
|
-
|
|
48
|
-
if ( `ws://${service.config.host}:${port}${path}` === url ) return
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
try {
|
|
52
|
-
|
|
53
|
-
const socket = await Service.SocketIO.connect ( url, service.name )
|
|
54
|
-
|
|
55
|
-
onConnection ( socket, service, url )
|
|
56
|
-
} catch ( error ) {
|
|
57
|
-
|
|
58
|
-
if ( !prevError ) console.log ( chalk.red`[Registry]`, chalk.underline.red`${url}`, 'error.' )
|
|
59
|
-
|
|
60
|
-
await delay ( 5000 )
|
|
61
|
-
|
|
62
|
-
connect ( service, url, error )
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
async function onConnection ( socket, service, url ) {
|
|
69
|
-
|
|
70
|
-
socket.on ( 'disconnect', async ( ) => {
|
|
71
|
-
|
|
72
|
-
console.log ( chalk.red`[Registry]`, chalk.underline.red`${url}`, 'disconnected.' )
|
|
73
|
-
|
|
74
|
-
await delay ( 1000 )
|
|
75
|
-
|
|
76
|
-
connect ( service, url )
|
|
77
|
-
} )
|
|
78
|
-
|
|
79
|
-
console.log ( chalk.green`[Registry]`, chalk.underline.green`${url}`, 'connected.' )
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
/**
|
|
85
|
-
*
|
|
86
|
-
* @param {Service} service
|
|
87
|
-
* @param {String[]} registry
|
|
88
|
-
*/
|
|
89
|
-
exports.connect = async ( service, registry ) => {
|
|
90
|
-
|
|
91
|
-
if ( 'string' === typeof registry ) {
|
|
92
|
-
|
|
93
|
-
connect ( service, urlFormatter ( service, registry ) )
|
|
94
|
-
} else {
|
|
95
|
-
|
|
96
|
-
for ( const url of registry ) {
|
|
97
|
-
|
|
98
|
-
connect ( service, urlFormatter ( service, url ) )
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
1
|
+
|
|
2
|
+
const chalk = require ( 'chalk' )
|
|
3
|
+
|
|
4
|
+
var Service = require ( '../service/service.class' )
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
function delay ( ms ) {
|
|
9
|
+
|
|
10
|
+
return new Promise ( function ( resolve, reject ) {
|
|
11
|
+
|
|
12
|
+
setTimeout ( resolve, ms )
|
|
13
|
+
} )
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
function urlFormatter ( service, url ) {
|
|
19
|
+
|
|
20
|
+
// :6000
|
|
21
|
+
if ( url.startsWith ( ':' ) ) url = service.config.host + url
|
|
22
|
+
|
|
23
|
+
// http://127.0.0.1:6000
|
|
24
|
+
if ( url.startsWith ( 'http://' ) ) url = url.substr ( 7 )
|
|
25
|
+
|
|
26
|
+
// 127.0.0.1:6000
|
|
27
|
+
if ( !url.startsWith ( 'ws://' ) ) url = 'ws://' + url
|
|
28
|
+
|
|
29
|
+
const urlObject = new URL ( url )
|
|
30
|
+
|
|
31
|
+
if ( urlObject.pathname === '/' && !url.endsWith ( '/' ) ) url = url + '/socket.io'
|
|
32
|
+
|
|
33
|
+
return url
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
*
|
|
39
|
+
* @param {Service} service
|
|
40
|
+
* @param {String} url
|
|
41
|
+
*/
|
|
42
|
+
async function connect ( service, url, prevError = null ) {
|
|
43
|
+
|
|
44
|
+
if ( service.socketio.config ) {
|
|
45
|
+
|
|
46
|
+
const { port, path } = service.socketio.config
|
|
47
|
+
|
|
48
|
+
if ( `ws://${service.config.host}:${port}${path}` === url ) return
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
try {
|
|
52
|
+
|
|
53
|
+
const socket = await Service.SocketIO.connect ( url, service.name )
|
|
54
|
+
|
|
55
|
+
onConnection ( socket, service, url )
|
|
56
|
+
} catch ( error ) {
|
|
57
|
+
|
|
58
|
+
if ( !prevError ) console.log ( chalk.red`[Registry]`, chalk.underline.red`${url}`, 'error.' )
|
|
59
|
+
|
|
60
|
+
await delay ( 5000 )
|
|
61
|
+
|
|
62
|
+
connect ( service, url, error )
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
async function onConnection ( socket, service, url ) {
|
|
69
|
+
|
|
70
|
+
socket.on ( 'disconnect', async ( ) => {
|
|
71
|
+
|
|
72
|
+
console.log ( chalk.red`[Registry]`, chalk.underline.red`${url}`, 'disconnected.' )
|
|
73
|
+
|
|
74
|
+
await delay ( 1000 )
|
|
75
|
+
|
|
76
|
+
connect ( service, url )
|
|
77
|
+
} )
|
|
78
|
+
|
|
79
|
+
console.log ( chalk.green`[Registry]`, chalk.underline.green`${url}`, 'connected.' )
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
*
|
|
86
|
+
* @param {Service} service
|
|
87
|
+
* @param {String[]} registry
|
|
88
|
+
*/
|
|
89
|
+
exports.connect = async ( service, registry ) => {
|
|
90
|
+
|
|
91
|
+
if ( 'string' === typeof registry ) {
|
|
92
|
+
|
|
93
|
+
connect ( service, urlFormatter ( service, registry ) )
|
|
94
|
+
} else {
|
|
95
|
+
|
|
96
|
+
for ( const url of registry ) {
|
|
97
|
+
|
|
98
|
+
connect ( service, urlFormatter ( service, url ) )
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
|
|
105
105
|
exports.setService = serviceClazz => Service = serviceClazz
|
package/bin/starter.js
CHANGED
|
@@ -1,140 +1,144 @@
|
|
|
1
|
-
|
|
2
|
-
const fs = require ( 'fs' )
|
|
3
|
-
|
|
4
|
-
const path = require ( 'path' )
|
|
5
|
-
|
|
6
|
-
const Service = require ( '../service/service.class' )
|
|
7
|
-
|
|
8
|
-
const chalk = require ( 'chalk' )
|
|
9
|
-
|
|
10
|
-
const proxyer = require ( './proxyer' )
|
|
11
|
-
|
|
12
|
-
const configurer = require ( './configurer' )
|
|
13
|
-
|
|
14
|
-
const register = require ( './register' )
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
function getEntryFile ( ) {
|
|
19
|
-
|
|
20
|
-
const args = process.argv.slice ( 2 )
|
|
21
|
-
|
|
22
|
-
var [ entryFilename ] = args.filter ( arg => !arg.includes ( '=' ) && arg.endsWith ( '.js' ) )
|
|
23
|
-
|
|
24
|
-
if ( !entryFilename ) throw new Error ( 'Cannot find entry file' )
|
|
25
|
-
|
|
26
|
-
const fullPath = path.resolve ( entryFilename )
|
|
27
|
-
|
|
28
|
-
const filename = path.basename ( fullPath )
|
|
29
|
-
|
|
30
|
-
const
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
// entry
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
if (
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
entry.
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
if ( env
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
console.log ( )
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
if (
|
|
1
|
+
|
|
2
|
+
const fs = require ( 'fs' )
|
|
3
|
+
|
|
4
|
+
const path = require ( 'path' )
|
|
5
|
+
|
|
6
|
+
const Service = require ( '../service/service.class' )
|
|
7
|
+
|
|
8
|
+
const chalk = require ( 'chalk' )
|
|
9
|
+
|
|
10
|
+
const proxyer = require ( './proxyer' )
|
|
11
|
+
|
|
12
|
+
const configurer = require ( './configurer' )
|
|
13
|
+
|
|
14
|
+
const register = require ( './register' )
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
function getEntryFile ( env ) {
|
|
19
|
+
|
|
20
|
+
const args = process.argv.slice ( 2 )
|
|
21
|
+
|
|
22
|
+
var [ entryFilename ] = args.filter ( arg => !arg.includes ( '=' ) && arg.endsWith ( '.js' ) )
|
|
23
|
+
|
|
24
|
+
if ( !entryFilename ) throw new Error ( 'Cannot find entry file' )
|
|
25
|
+
|
|
26
|
+
const fullPath = path.resolve ( entryFilename )
|
|
27
|
+
|
|
28
|
+
const filename = path.basename ( fullPath )
|
|
29
|
+
|
|
30
|
+
const fullDirectory = path.dirname ( fullPath )
|
|
31
|
+
|
|
32
|
+
const directory = fullDirectory.split ( path.sep ).pop ( )
|
|
33
|
+
|
|
34
|
+
const groupFullDirectory = env.group ? path.resolve ( env.group ) : ''
|
|
35
|
+
|
|
36
|
+
var name = filename === 'index.js' && groupFullDirectory !== fullDirectory ? directory : filename.split ( '.js' ) [ 0 ]
|
|
37
|
+
|
|
38
|
+
return { name, path: fullPath, group: groupFullDirectory }
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
function genEntry ( name, entryPath, templatePath ) {
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* @type {Service}
|
|
47
|
+
*/
|
|
48
|
+
var entry = require ( entryPath )
|
|
49
|
+
|
|
50
|
+
if ( 'object' === typeof entry && 'function' === typeof entry.call && 'string' === typeof entry.name && 'object' === typeof entry.config ) {
|
|
51
|
+
|
|
52
|
+
// entry instanceof RPC
|
|
53
|
+
|
|
54
|
+
} else if ( 'function' === typeof entry && 'function' === typeof entry.call ) {
|
|
55
|
+
|
|
56
|
+
// entry extendsof RPC
|
|
57
|
+
|
|
58
|
+
entry = new entry ( name, null )
|
|
59
|
+
} else {
|
|
60
|
+
|
|
61
|
+
// custom service class
|
|
62
|
+
|
|
63
|
+
if ( !templatePath ) templatePath = path.dirname ( entryPath ) + '/service.class.js'
|
|
64
|
+
|
|
65
|
+
templatePath = path.resolve ( templatePath )
|
|
66
|
+
|
|
67
|
+
if ( fs.existsSync ( templatePath ) ) {
|
|
68
|
+
|
|
69
|
+
const CustomService = require ( templatePath )
|
|
70
|
+
|
|
71
|
+
entry = new CustomService ( name, entry )
|
|
72
|
+
} else {
|
|
73
|
+
|
|
74
|
+
entry = new Service ( name, entry )
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return entry
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
exports.startup = async ( ) => {
|
|
84
|
+
|
|
85
|
+
// 加载环境变量
|
|
86
|
+
const env = configurer.configure ( )
|
|
87
|
+
|
|
88
|
+
Object.assign ( oox, env )
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
// 获取服务入口地址
|
|
93
|
+
const entryFile = getEntryFile ( env )
|
|
94
|
+
|
|
95
|
+
// 代理<服务间调用>
|
|
96
|
+
if ( env.group ) {
|
|
97
|
+
|
|
98
|
+
const excludes = [ entryFile.name ]
|
|
99
|
+
|
|
100
|
+
if ( Array.isArray ( env.ignore ) ) excludes.push ( ...env.ignore )
|
|
101
|
+
|
|
102
|
+
proxyer.proxyServices ( entryFile.group, excludes )
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// 加载服务
|
|
106
|
+
const entry = genEntry ( entryFile.name, entryFile.path, env.template )
|
|
107
|
+
|
|
108
|
+
proxyer.setService ( entry.constructor )
|
|
109
|
+
register.setService ( entry.constructor )
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
// 服务配置
|
|
114
|
+
if ( !env.port && !env.http && !env.socketio ) env.port = 0
|
|
115
|
+
|
|
116
|
+
if ( 'number' === typeof env.port ) {
|
|
117
|
+
|
|
118
|
+
if ( !entry.http.config ) entry.http.config = { }
|
|
119
|
+
if ( !entry.socketio.config ) entry.socketio.config = { }
|
|
120
|
+
|
|
121
|
+
entry.http.config.port =
|
|
122
|
+
entry.socketio.config.port = env.port
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
if ( 'http' in env ) entry.http.config = env.http
|
|
126
|
+
|
|
127
|
+
if ( 'socketio' in env ) entry.socketio.config = env.socketio
|
|
128
|
+
|
|
129
|
+
if ( env.origin ) entry.config.origin = env.origin
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
// 服务启动
|
|
134
|
+
await entry.serve ( )
|
|
135
|
+
|
|
136
|
+
console.log ( )
|
|
137
|
+
console.log ( 'Service', chalk.bold`${entry.name}`, 'running.' )
|
|
138
|
+
if ( entry.http.config ) console.log ( ' at', chalk.underline.green`http://${entry.config.host}:${entry.http.config.port}${entry.http.config.path}` )
|
|
139
|
+
if ( entry.socketio.config ) console.log ( ' at', chalk.underline.green`ws://${entry.config.host}:${entry.socketio.config.port}${entry.socketio.config.path}` )
|
|
140
|
+
console.log ( )
|
|
141
|
+
|
|
142
|
+
// 服务注册
|
|
143
|
+
if ( env.registry ) register.connect ( entry, env.registry )
|
|
140
144
|
}
|