oox 0.3.0-beta11 → 0.3.0-beta13

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,66 +1,66 @@
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, action) {
25
- return new Proxy(function () { }, {
26
- get(target, key) {
27
- return dotCall(name, action ? action + '.' + key : key);
28
- },
29
- has(target, key) { return true; },
30
- apply(target, thisArg, args) {
31
- return oox.rpc(name, action, 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()) {
48
- const entries = fs.readdirSync(fullPath);
49
- for (const entry of entries) {
50
- if (/^index\.((\w?js)|(ts\w?))$/.test(entry))
51
- id = path.resolve(fullPath, entry);
52
- }
53
- name = filename;
54
- }
55
- else {
56
- const scriptMatch = filename.match(/(\w+)\.((\w?js)|(ts\w?))$/);
57
- if (!scriptMatch)
58
- continue;
59
- id = fullPath;
60
- name = scriptMatch[1];
61
- }
62
- if (!excludes.includes(name))
63
- rewriteModuleCache(id, dotCall(name, ''));
64
- }
65
- }
66
- exports.proxyGroup = proxyGroup;
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, action) {
25
+ return new Proxy(function () { }, {
26
+ get(target, key) {
27
+ return dotCall(name, action ? action + '.' + key : key);
28
+ },
29
+ has(target, key) { return true; },
30
+ apply(target, thisArg, args) {
31
+ return oox.rpc(name, action, 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()) {
48
+ const entries = fs.readdirSync(fullPath);
49
+ for (const entry of entries) {
50
+ if (/^index\.((\w?js)|(ts\w?))$/.test(entry))
51
+ id = path.resolve(fullPath, entry);
52
+ }
53
+ name = filename;
54
+ }
55
+ else {
56
+ const scriptMatch = filename.match(/(\w+)\.((\w?js)|(ts\w?))$/);
57
+ if (!scriptMatch)
58
+ continue;
59
+ id = fullPath;
60
+ name = scriptMatch[1];
61
+ }
62
+ if (!excludes.includes(name))
63
+ rewriteModuleCache(id, dotCall(name, ''));
64
+ }
65
+ }
66
+ exports.proxyGroup = proxyGroup;
package/bin/register.js CHANGED
@@ -1,55 +1,55 @@
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 socketio = oox.modules.get('socketio');
24
- const { host } = oox.config;
25
- const { port, path } = socketio.config;
26
- if (`ws://${host}:${port}${path}` === url)
27
- return;
28
- try {
29
- const socket = await socketio.connect(url);
30
- onConnection(socket, url);
31
- }
32
- catch (error) {
33
- if (!prevError)
34
- console.log((0, chalk_1.red) `[Registry]`, chalk_1.underline.red `${url}`, 'error.');
35
- await delay(5000);
36
- connect(url, error);
37
- }
38
- }
39
- async function onConnection(socket, url) {
40
- const { name } = socket.data;
41
- socket.on('disconnect', async () => {
42
- console.log((0, chalk_1.red) `[Registry]`, `Service<${name}>`, chalk_1.underline.red `${url}`, 'disconnected.');
43
- await delay(1000);
44
- connect(url);
45
- });
46
- console.log((0, chalk_1.green) `[Registry]`, `Service<${name}>`, chalk_1.underline.green `${url}`, 'connected.');
47
- }
48
- async function registry(urls) {
49
- if ('string' === typeof urls)
50
- urls = [urls];
51
- for (const url of urls) {
52
- connect(urlFormatter(url));
53
- }
54
- }
55
- exports.registry = registry;
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 socketio = oox.modules.get('socketio');
24
+ const { host } = oox.config;
25
+ const { port, path } = socketio.config;
26
+ if (`ws://${host}:${port}${path}` === url)
27
+ return;
28
+ try {
29
+ const socket = await socketio.connect(url);
30
+ onConnection(socket, url);
31
+ }
32
+ catch (error) {
33
+ if (!prevError)
34
+ console.log((0, chalk_1.red) `[Registry]`, chalk_1.underline.red `${url}`, 'error.');
35
+ await delay(5000);
36
+ connect(url, error);
37
+ }
38
+ }
39
+ async function onConnection(socket, url) {
40
+ const { name } = socket.data;
41
+ socket.on('disconnect', async () => {
42
+ console.log((0, chalk_1.red) `[Registry]`, `Service<${name}>`, chalk_1.underline.red `${url}`, 'disconnected.');
43
+ await delay(1000);
44
+ connect(url);
45
+ });
46
+ console.log((0, chalk_1.green) `[Registry]`, `Service<${name}>`, chalk_1.underline.green `${url}`, 'connected.');
47
+ }
48
+ async function registry(urls) {
49
+ if ('string' === typeof urls)
50
+ urls = [urls];
51
+ for (const url of urls) {
52
+ connect(urlFormatter(url));
53
+ }
54
+ }
55
+ exports.registry = registry;
package/bin/starter.js CHANGED
@@ -1,71 +1,71 @@
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, entryFilename) {
11
- const entryMatchRegExp = /(\w+)\.((\w?js)|(ts\w?))$/;
12
- if (!entryFilename) {
13
- const args = process.argv.slice(2);
14
- entryFilename = args.find(arg => !arg.includes('=') && entryMatchRegExp.test(arg));
15
- }
16
- if (!entryFilename)
17
- throw new Error('Cannot find entry file');
18
- const fullPath = path.resolve(entryFilename);
19
- const filename = path.basename(fullPath);
20
- const fullDirectory = path.dirname(fullPath);
21
- const directory = fullDirectory.split(path.sep).pop();
22
- const groupFullDirectory = env.group ? path.resolve(env.group) : '';
23
- const entryMatch = filename.match(entryMatchRegExp);
24
- const entryFilenameWithoutExtension = entryMatch[1];
25
- var name = entryFilenameWithoutExtension === 'index' && groupFullDirectory !== fullDirectory ? directory : entryFilenameWithoutExtension;
26
- return { name, path: fullPath, group: groupFullDirectory };
27
- }
28
- async function loadEntry(name, entryPath) {
29
- oox.config.name = name;
30
- // Typescript 4.7.3, not supported import() expression
31
- const methods = await eval(`import('file://${entryPath.replace(/\\/g, '/')}')`);
32
- oox.setMethods(methods.default || methods);
33
- }
34
- async function startup(env, entryFilename) {
35
- // 加载环境变量
36
- env = await (0, configurer_1.configure)(env);
37
- Object.assign(oox.config, env);
38
- // 获取服务入口地址
39
- const entryFile = getEntryFile(env, entryFilename);
40
- oox.config.entryFile = {
41
- path: entryFile.path.replace(/\\/g, '/'),
42
- group: entryFile.group.replace(/\\/g, '/'),
43
- };
44
- // 代理<服务间调用>
45
- if (env.group) {
46
- const excludes = [entryFile.name];
47
- if (Array.isArray(env.ignore))
48
- excludes.push(...env.ignore);
49
- (0, proxyer_1.proxyGroup)(entryFile.group, excludes);
50
- }
51
- // 加载服务
52
- await loadEntry(entryFile.name, entryFile.path);
53
- // 模块配置
54
- oox.modules.setConfig(oox.config);
55
- oox.emit('app:configured');
56
- const { http: { config: httpConfig }, socketio: { config: socketioConfig } } = oox.modules.builtins;
57
- // 服务启动
58
- await oox.serve();
59
- oox.emit('app:served');
60
- console.log();
61
- console.log('Service', (0, chalk_1.bold) `${oox.config.name}`, 'running.');
62
- if (!httpConfig.disabled)
63
- console.log(' at', chalk_1.underline.green `http://${oox.config.host}:${httpConfig.port}${httpConfig.path}`);
64
- if (!socketioConfig.disabled)
65
- console.log(' at', chalk_1.underline.green `ws://${oox.config.host}:${socketioConfig.port}${socketioConfig.path}`);
66
- console.log();
67
- // 服务注册
68
- if (env.registry)
69
- (0, register_1.registry)(env.registry);
70
- }
71
- exports.startup = startup;
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, entryFilename) {
11
+ const entryMatchRegExp = /(\w+)\.((\w?js)|(ts\w?))$/;
12
+ if (!entryFilename) {
13
+ const args = process.argv.slice(2);
14
+ entryFilename = args.find(arg => !arg.includes('=') && entryMatchRegExp.test(arg));
15
+ }
16
+ if (!entryFilename)
17
+ throw new Error('Cannot find entry file');
18
+ const fullPath = path.resolve(entryFilename);
19
+ const filename = path.basename(fullPath);
20
+ const fullDirectory = path.dirname(fullPath);
21
+ const directory = fullDirectory.split(path.sep).pop();
22
+ const groupFullDirectory = env.group ? path.resolve(env.group) : '';
23
+ const entryMatch = filename.match(entryMatchRegExp);
24
+ const entryFilenameWithoutExtension = entryMatch[1];
25
+ var name = entryFilenameWithoutExtension === 'index' && groupFullDirectory !== fullDirectory ? directory : entryFilenameWithoutExtension;
26
+ return { name, path: fullPath, group: groupFullDirectory };
27
+ }
28
+ async function loadEntry(name, entryPath) {
29
+ oox.config.name = name;
30
+ // Typescript 4.7.3, not supported import() expression
31
+ const methods = await eval(`import('file://${entryPath.replace(/\\/g, '/')}')`);
32
+ oox.setMethods(methods.default || methods);
33
+ }
34
+ async function startup(env, entryFilename) {
35
+ // 加载环境变量
36
+ env = await (0, configurer_1.configure)(env);
37
+ Object.assign(oox.config, env);
38
+ // 获取服务入口地址
39
+ const entryFile = getEntryFile(env, entryFilename);
40
+ oox.config.entryFile = {
41
+ path: entryFile.path.replace(/\\/g, '/'),
42
+ group: entryFile.group.replace(/\\/g, '/'),
43
+ };
44
+ // 模块配置
45
+ oox.modules.setConfig(oox.config);
46
+ oox.emit('app:configured');
47
+ const { http: { config: httpConfig }, socketio: { config: socketioConfig } } = oox.modules.builtins;
48
+ // 代理<服务间调用>
49
+ if (env.group) {
50
+ const excludes = [entryFile.name];
51
+ if (Array.isArray(env.ignore))
52
+ excludes.push(...env.ignore);
53
+ (0, proxyer_1.proxyGroup)(entryFile.group, excludes);
54
+ }
55
+ // 服务启动
56
+ await oox.serve();
57
+ // 加载服务
58
+ await loadEntry(entryFile.name, entryFile.path);
59
+ oox.emit('app:served');
60
+ console.log();
61
+ console.log('Service', (0, chalk_1.bold) `${oox.config.name}`, 'running.');
62
+ if (!httpConfig.disabled)
63
+ console.log(' at', chalk_1.underline.green `http://${oox.config.host}:${httpConfig.port}${httpConfig.path}`);
64
+ if (!socketioConfig.disabled)
65
+ console.log(' at', chalk_1.underline.green `ws://${oox.config.host}:${socketioConfig.port}${socketioConfig.path}`);
66
+ console.log();
67
+ // 服务注册
68
+ if (env.registry)
69
+ (0, register_1.registry)(env.registry);
70
+ }
71
+ exports.startup = startup;