oox 0.2.0 → 0.3.0-beta1

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 (54) 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 +54 -105
  9. package/bin/starter.js +77 -144
  10. package/index.js +149 -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 +88 -0
  15. package/modules/module.js +16 -0
  16. package/modules/socketio/client.js +101 -0
  17. package/modules/socketio/index.js +148 -0
  18. package/modules/socketio/server.js +130 -0
  19. package/modules/socketio/socket.js +4 -0
  20. package/package.json +34 -16
  21. package/types/app.d.ts +37 -0
  22. package/types/bin/argv.d.ts +8 -0
  23. package/types/bin/cli.d.ts +2 -0
  24. package/types/bin/configurer.d.ts +1 -0
  25. package/types/bin/proxyer.d.ts +1 -0
  26. package/types/bin/register.d.ts +1 -0
  27. package/types/bin/starter.d.ts +1 -0
  28. package/types/index.d.ts +70 -0
  29. package/types/modules/http/index.d.ts +47 -0
  30. package/types/modules/http/utils.d.ts +17 -0
  31. package/types/modules/index.d.ts +23 -0
  32. package/types/modules/module.d.ts +11 -0
  33. package/types/modules/socketio/client.d.ts +23 -0
  34. package/types/modules/socketio/index.d.ts +35 -0
  35. package/types/modules/socketio/server.d.ts +35 -0
  36. package/types/modules/socketio/socket.d.ts +11 -0
  37. package/types/utils.d.ts +6 -0
  38. package/utils.js +63 -0
  39. package/.gitattributes +0 -2
  40. package/global.js +0 -118
  41. package/middleware.js +0 -167
  42. package/rpc/config.class.js +0 -48
  43. package/rpc/context.class.js +0 -15
  44. package/rpc/http.class.js +0 -312
  45. package/rpc/rpc.class.js +0 -231
  46. package/rpc/rpc.interface.class.js +0 -119
  47. package/rpc/socketio.class.js +0 -223
  48. package/service/service.class.js +0 -74
  49. package/service/socketio.class.js +0 -145
  50. package/setMap.class.js +0 -67
  51. package/socketio/client.class.js +0 -190
  52. package/socketio/server.class.js +0 -222
  53. package/socketio/socket.class.js +0 -23
  54. package/util.js +0 -224
package/bin/proxyer.js CHANGED
@@ -1,95 +1,61 @@
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
- exports.setService = serviceClazz => Service = serviceClazz
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.proxyGroup = void 0;
4
+ const fs = require("node:fs");
5
+ const path = require("node:path");
6
+ const oox = require("../index");
7
+ const node_module_1 = require("node:module");
8
+ /**
9
+ * 重写 require 缓存
10
+ */
11
+ function rewriteModuleCache(id, exports) {
12
+ const pathname = id.split(path.sep).slice(0, -1).join(path.sep);
13
+ const pathSplit = pathname.split(path.sep);
14
+ const paths = pathSplit.map((v, i, a) => a.slice(0, i + 1).concat('node_modules').join(path.sep)).reverse();
15
+ const m = new node_module_1.Module(id, null);
16
+ m.path = pathname;
17
+ m.exports = exports;
18
+ m.filename = m.id;
19
+ m.loaded = true;
20
+ m.children = [];
21
+ m.paths = paths;
22
+ require.cache[id] = m;
23
+ }
24
+ function dotCall(name, paths = []) {
25
+ return new Proxy(function () { }, {
26
+ get(target, key) {
27
+ return dotCall(name, paths.concat(key));
28
+ },
29
+ has(target, key) { return true; },
30
+ apply(target, thisArg, args) {
31
+ return oox.rpc(name, paths.join('.'), args);
32
+ }
33
+ });
34
+ }
35
+ function proxyGroup(groupDirectory, excludes = []) {
36
+ if (!groupDirectory)
37
+ return;
38
+ const directory = path.resolve(groupDirectory);
39
+ const stat = fs.statSync(directory);
40
+ if (!stat.isDirectory())
41
+ throw new Error('group must be directory');
42
+ const subs = fs.readdirSync(directory);
43
+ for (const filename of subs) {
44
+ const fullPath = path.resolve(directory + path.sep + filename);
45
+ const stat = fs.statSync(fullPath);
46
+ let id = '', name = '';
47
+ if (stat.isDirectory() && fs.existsSync(fullPath + '/index.js')) {
48
+ id = fullPath + '/index.js';
49
+ name = filename;
50
+ }
51
+ else if (filename.endsWith('.js')) {
52
+ id = fullPath;
53
+ name = filename.split('.js')[0];
54
+ }
55
+ else
56
+ continue;
57
+ if (!excludes.includes(name))
58
+ rewriteModuleCache(id, dotCall(name));
59
+ }
60
+ }
61
+ exports.proxyGroup = proxyGroup;
package/bin/register.js CHANGED
@@ -1,105 +1,54 @@
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
- exports.setService = serviceClazz => Service = serviceClazz
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.registry = void 0;
4
+ const chalk_1 = require("chalk");
5
+ const oox = require("../index");
6
+ const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));
7
+ function urlFormatter(url) {
8
+ // :6000
9
+ if (url.startsWith(':'))
10
+ url = oox.config.host + url;
11
+ // http://127.0.0.1:6000
12
+ if (url.startsWith('http://'))
13
+ url = url.slice(7);
14
+ // 127.0.0.1:6000
15
+ if (!url.startsWith('ws://'))
16
+ url = 'ws://' + url;
17
+ const urlObject = new URL(url);
18
+ if (urlObject.pathname === '/' && !url.endsWith('/'))
19
+ url = url + '/socket.io';
20
+ return url;
21
+ }
22
+ async function connect(url, prevError = null) {
23
+ const { host } = oox.config;
24
+ const { port, path } = oox.modules.builtins.socketio.config;
25
+ if (`ws://${host}:${port}${path}` === url)
26
+ return;
27
+ try {
28
+ const socket = await oox.modules.builtins.socketio.connect(url);
29
+ onConnection(socket, url);
30
+ }
31
+ catch (error) {
32
+ if (!prevError)
33
+ console.log((0, chalk_1.red) `[Registry]`, chalk_1.underline.red `${url}`, 'error.');
34
+ await delay(5000);
35
+ connect(url, error);
36
+ }
37
+ }
38
+ async function onConnection(socket, url) {
39
+ const { name } = socket.data;
40
+ socket.on('disconnect', async () => {
41
+ console.log((0, chalk_1.red) `[Registry]`, `Service<${name}>`, chalk_1.underline.red `${url}`, 'disconnected.');
42
+ await delay(1000);
43
+ connect(url);
44
+ });
45
+ console.log((0, chalk_1.green) `[Registry]`, `Service<${name}>`, chalk_1.underline.green `${url}`, 'connected.');
46
+ }
47
+ async function registry(urls) {
48
+ if ('string' === typeof urls)
49
+ urls = [urls];
50
+ for (const url of urls) {
51
+ connect(urlFormatter(url));
52
+ }
53
+ }
54
+ exports.registry = registry;
package/bin/starter.js CHANGED
@@ -1,144 +1,77 @@
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 )
144
- }
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.startup = void 0;
4
+ const path = require("node:path");
5
+ const chalk_1 = require("chalk");
6
+ const oox = require("../index");
7
+ const proxyer_1 = require("./proxyer");
8
+ const configurer_1 = require("./configurer");
9
+ const register_1 = require("./register");
10
+ function getEntryFile(env) {
11
+ const args = process.argv.slice(2);
12
+ var [entryFilename] = args.filter(arg => !arg.includes('=') && arg.endsWith('.js'));
13
+ if (!entryFilename)
14
+ throw new Error('Cannot find entry file');
15
+ const fullPath = path.resolve(entryFilename);
16
+ const filename = path.basename(fullPath);
17
+ const fullDirectory = path.dirname(fullPath);
18
+ const directory = fullDirectory.split(path.sep).pop();
19
+ const groupFullDirectory = env.group ? path.resolve(env.group) : '';
20
+ var name = filename === 'index.js' && groupFullDirectory !== fullDirectory ? directory : filename.split('.js')[0];
21
+ return { name, path: fullPath, group: groupFullDirectory };
22
+ }
23
+ function loadEntry(name, entryPath) {
24
+ oox.config.name = name;
25
+ const methods = require(entryPath);
26
+ oox.setMethods(methods);
27
+ }
28
+ async function startup() {
29
+ // 加载环境变量
30
+ const env = (0, configurer_1.configure)();
31
+ Object.assign(oox.config, env);
32
+ // 获取服务入口地址
33
+ const entryFile = getEntryFile(env);
34
+ // 代理<服务间调用>
35
+ if (env.group) {
36
+ const excludes = [entryFile.name];
37
+ if (Array.isArray(env.ignore))
38
+ excludes.push(...env.ignore);
39
+ (0, proxyer_1.proxyGroup)(entryFile.group, excludes);
40
+ }
41
+ // 加载服务
42
+ loadEntry(entryFile.name, entryFile.path);
43
+ // 服务配置
44
+ if (!env.port && !env.http && !env.socketio)
45
+ env.port = 0;
46
+ const httpConfig = oox.modules.builtins.http.config, socketioConfig = oox.modules.builtins.socketio.config;
47
+ if ('number' === typeof env.port) {
48
+ httpConfig.port = socketioConfig.port = env.port;
49
+ }
50
+ if (env.origin)
51
+ httpConfig.origin = env.origin;
52
+ if ('http' in env) {
53
+ if (env.http)
54
+ Object.assign(httpConfig, env.http);
55
+ else
56
+ httpConfig.disabled = true;
57
+ }
58
+ if ('socketio' in env) {
59
+ if (env.socketio)
60
+ Object.assign(socketioConfig, env.socketio);
61
+ else
62
+ socketioConfig.disabled = true;
63
+ }
64
+ // 服务启动
65
+ await oox.serve();
66
+ console.log();
67
+ console.log('Service', (0, chalk_1.bold) `${oox.config.name}`, 'running.');
68
+ if (!httpConfig.disabled)
69
+ console.log(' at', chalk_1.underline.green `http://${oox.config.host}:${httpConfig.port}${httpConfig.path}`);
70
+ if (!socketioConfig.disabled)
71
+ console.log(' at', chalk_1.underline.green `ws://${oox.config.host}:${socketioConfig.port}${socketioConfig.path}`);
72
+ console.log();
73
+ // 服务注册
74
+ if (env.registry)
75
+ (0, register_1.registry)(env.registry);
76
+ }
77
+ exports.startup = startup;