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.
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,140 @@
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 directory = path.dirname ( fullPath ).split ( path.sep ).pop ( )
31
-
32
- var name = filename === 'index.js' ? directory : filename.split ( '.js' ) [ 0 ]
33
-
34
- return { name, path: fullPath }
35
- }
36
-
37
-
38
-
39
- function genEntry ( name, entryPath, templatePath ) {
40
-
41
- /**
42
- * @type {Service}
43
- */
44
- var entry = require ( entryPath )
45
-
46
- if ( 'object' === typeof entry && 'function' === typeof entry.call && 'string' === typeof entry.name && 'object' === typeof entry.config ) {
47
-
48
- // entry instanceof RPC
49
-
50
- } else if ( 'function' === typeof entry && 'function' === typeof entry.call ) {
51
-
52
- // entry extendsof RPC
53
-
54
- entry = new entry ( name, null )
55
- } else {
56
-
57
- // custom service class
58
-
59
- if ( !templatePath ) templatePath = path.dirname ( entryPath ) + '/service.class.js'
60
-
61
- templatePath = path.resolve ( templatePath )
62
-
63
- if ( fs.existsSync ( templatePath ) ) {
64
-
65
- const CustomService = require ( templatePath )
66
-
67
- entry = new CustomService ( name, entry )
68
- } else {
69
-
70
- entry = new Service ( name, entry )
71
- }
72
- }
73
-
74
- return entry
75
- }
76
-
77
-
78
-
79
- exports.startup = async ( ) => {
80
-
81
- // 加载环境变量
82
- const env = configurer.configure ( )
83
-
84
- Object.assign ( oox, env )
85
-
86
-
87
-
88
- // 获取服务入口地址
89
- const entryFile = getEntryFile ( )
90
-
91
- // 代理<服务间调用>
92
- if ( env.group ) {
93
-
94
- const excludes = [ entryFile.name ]
95
-
96
- if ( Array.isArray ( env.ignore ) ) excludes.push ( ...env.ignore )
97
-
98
- proxyer.proxyServices ( env.group, excludes )
99
- }
100
-
101
- // 加载服务
102
- const entry = genEntry ( entryFile.name, entryFile.path, env.template )
103
-
104
- proxyer.setService ( entry.constructor )
105
- register.setService ( entry.constructor )
106
-
107
-
108
-
109
- // 服务配置
110
- if ( !env.port && !env.http && !env.socketio ) env.port = 0
111
-
112
- if ( 'number' === typeof env.port ) {
113
-
114
- if ( !entry.http.config ) entry.http.config = { }
115
- if ( !entry.socketio.config ) entry.socketio.config = { }
116
-
117
- entry.http.config.port =
118
- entry.socketio.config.port = env.port
119
- }
120
-
121
- if ( 'http' in env ) entry.http.config = env.http
122
-
123
- if ( 'socketio' in env ) entry.socketio.config = env.socketio
124
-
125
- if ( env.origin ) entry.config.origin = env.origin
126
-
127
-
128
-
129
- // 服务启动
130
- await entry.serve ( )
131
-
132
- console.log ( )
133
- console.log ( 'Service', chalk.bold`${entry.name}`, 'running.' )
134
- if ( entry.http.config ) console.log ( ' at', chalk.underline.green`http://${entry.config.host}:${entry.http.config.port}${entry.http.config.path}` )
135
- if ( entry.socketio.config ) console.log ( ' at', chalk.underline.green`ws://${entry.config.host}:${entry.socketio.config.port}${entry.socketio.config.path}` )
136
- console.log ( )
137
-
138
- // 服务注册
139
- if ( env.registry ) register.connect ( entry, env.registry )
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 directory = path.dirname ( fullPath ).split ( path.sep ).pop ( )
31
+
32
+ var name = filename === 'index.js' ? directory : filename.split ( '.js' ) [ 0 ]
33
+
34
+ return { name, path: fullPath }
35
+ }
36
+
37
+
38
+
39
+ function genEntry ( name, entryPath, templatePath ) {
40
+
41
+ /**
42
+ * @type {Service}
43
+ */
44
+ var entry = require ( entryPath )
45
+
46
+ if ( 'object' === typeof entry && 'function' === typeof entry.call && 'string' === typeof entry.name && 'object' === typeof entry.config ) {
47
+
48
+ // entry instanceof RPC
49
+
50
+ } else if ( 'function' === typeof entry && 'function' === typeof entry.call ) {
51
+
52
+ // entry extendsof RPC
53
+
54
+ entry = new entry ( name, null )
55
+ } else {
56
+
57
+ // custom service class
58
+
59
+ if ( !templatePath ) templatePath = path.dirname ( entryPath ) + '/service.class.js'
60
+
61
+ templatePath = path.resolve ( templatePath )
62
+
63
+ if ( fs.existsSync ( templatePath ) ) {
64
+
65
+ const CustomService = require ( templatePath )
66
+
67
+ entry = new CustomService ( name, entry )
68
+ } else {
69
+
70
+ entry = new Service ( name, entry )
71
+ }
72
+ }
73
+
74
+ return entry
75
+ }
76
+
77
+
78
+
79
+ exports.startup = async ( ) => {
80
+
81
+ // 加载环境变量
82
+ const env = configurer.configure ( )
83
+
84
+ Object.assign ( oox, env )
85
+
86
+
87
+
88
+ // 获取服务入口地址
89
+ const entryFile = getEntryFile ( )
90
+
91
+ // 代理<服务间调用>
92
+ if ( env.group ) {
93
+
94
+ const excludes = [ entryFile.name ]
95
+
96
+ if ( Array.isArray ( env.ignore ) ) excludes.push ( ...env.ignore )
97
+
98
+ proxyer.proxyServices ( env.group, excludes )
99
+ }
100
+
101
+ // 加载服务
102
+ const entry = genEntry ( entryFile.name, entryFile.path, env.template )
103
+
104
+ proxyer.setService ( entry.constructor )
105
+ register.setService ( entry.constructor )
106
+
107
+
108
+
109
+ // 服务配置
110
+ if ( !env.port && !env.http && !env.socketio ) env.port = 0
111
+
112
+ if ( 'number' === typeof env.port ) {
113
+
114
+ if ( !entry.http.config ) entry.http.config = { }
115
+ if ( !entry.socketio.config ) entry.socketio.config = { }
116
+
117
+ entry.http.config.port =
118
+ entry.socketio.config.port = env.port
119
+ }
120
+
121
+ if ( 'http' in env ) entry.http.config = env.http
122
+
123
+ if ( 'socketio' in env ) entry.socketio.config = env.socketio
124
+
125
+ if ( env.origin ) entry.config.origin = env.origin
126
+
127
+
128
+
129
+ // 服务启动
130
+ await entry.serve ( )
131
+
132
+ console.log ( )
133
+ console.log ( 'Service', chalk.bold`${entry.name}`, 'running.' )
134
+ if ( entry.http.config ) console.log ( ' at', chalk.underline.green`http://${entry.config.host}:${entry.http.config.port}${entry.http.config.path}` )
135
+ if ( entry.socketio.config ) console.log ( ' at', chalk.underline.green`ws://${entry.config.host}:${entry.socketio.config.port}${entry.socketio.config.path}` )
136
+ console.log ( )
137
+
138
+ // 服务注册
139
+ if ( env.registry ) register.connect ( entry, env.registry )
140
140
  }