oox 0.3.0-beta2 → 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.
- package/LICENSE +21 -21
- package/README.md +32 -32
- package/app.js +142 -142
- package/bin/argv.js +70 -70
- package/bin/cli.js +43 -43
- package/bin/configurer.js +49 -49
- package/bin/proxyer.js +61 -61
- package/bin/register.js +55 -55
- package/bin/starter.js +81 -81
- package/index.js +144 -144
- package/index.mjs +3 -3
- package/modules/http/index.js +180 -180
- package/modules/http/utils.js +73 -73
- package/modules/index.js +81 -68
- package/modules/module.js +16 -16
- package/package.json +2 -2
- package/types/app.d.ts +37 -37
- package/types/bin/argv.d.ts +8 -8
- package/types/bin/cli.d.ts +2 -2
- package/types/bin/configurer.d.ts +1 -1
- package/types/bin/proxyer.d.ts +1 -1
- package/types/bin/register.d.ts +1 -1
- package/types/bin/starter.d.ts +1 -1
- package/types/index.d.ts +70 -70
- package/types/modules/http/index.d.ts +47 -47
- package/types/modules/http/utils.d.ts +17 -17
- package/types/modules/index.d.ts +22 -21
- package/types/modules/module.d.ts +13 -13
- package/types/utils.d.ts +6 -6
- package/utils.js +63 -63
package/bin/configurer.js
CHANGED
|
@@ -1,49 +1,49 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.configure = void 0;
|
|
4
|
-
const fs = require("node:fs");
|
|
5
|
-
const path = require("node:path");
|
|
6
|
-
const argv = require("./argv");
|
|
7
|
-
/**
|
|
8
|
-
* x.y=1 => { x: { y: 1 } }
|
|
9
|
-
*/
|
|
10
|
-
function mergeFlatEnv(env) {
|
|
11
|
-
for (const key of Object.keys(env)) {
|
|
12
|
-
// x.y.z
|
|
13
|
-
if (key.includes('.') && !key.startsWith('.') && !key.endsWith('.')) {
|
|
14
|
-
const subKeys = key.split('.');
|
|
15
|
-
const valueKey = subKeys.pop();
|
|
16
|
-
let tmpEnv = env;
|
|
17
|
-
for (const subKey of subKeys) {
|
|
18
|
-
if (subKey in tmpEnv) { }
|
|
19
|
-
else {
|
|
20
|
-
tmpEnv[subKey] = {};
|
|
21
|
-
}
|
|
22
|
-
tmpEnv = tmpEnv[subKey];
|
|
23
|
-
}
|
|
24
|
-
tmpEnv[valueKey] = env[key];
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
function configure() {
|
|
29
|
-
let env = Object.create(null);
|
|
30
|
-
const defaultEnvPath = argv.getEnvArg('default-env');
|
|
31
|
-
if (defaultEnvPath && fs.existsSync(defaultEnvPath)) {
|
|
32
|
-
Object.assign(env, require(path.resolve(defaultEnvPath)));
|
|
33
|
-
}
|
|
34
|
-
const envPath = argv.getEnvArg('env');
|
|
35
|
-
if (envPath && fs.existsSync(envPath)) {
|
|
36
|
-
Object.assign(env, require(path.resolve(envPath)));
|
|
37
|
-
}
|
|
38
|
-
Object.assign(env, argv.getAllEnvArgs());
|
|
39
|
-
mergeFlatEnv(env);
|
|
40
|
-
if ('string' === typeof env.ignore)
|
|
41
|
-
env.ignore = env.ignore.split(',');
|
|
42
|
-
if ('string' === typeof env.registry)
|
|
43
|
-
env.registry = env.registry.split(',');
|
|
44
|
-
if ('string' === typeof env.origin && env.origin.includes(',')) {
|
|
45
|
-
env.origin = env.origin.split(',');
|
|
46
|
-
}
|
|
47
|
-
return env;
|
|
48
|
-
}
|
|
49
|
-
exports.configure = configure;
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.configure = void 0;
|
|
4
|
+
const fs = require("node:fs");
|
|
5
|
+
const path = require("node:path");
|
|
6
|
+
const argv = require("./argv");
|
|
7
|
+
/**
|
|
8
|
+
* x.y=1 => { x: { y: 1 } }
|
|
9
|
+
*/
|
|
10
|
+
function mergeFlatEnv(env) {
|
|
11
|
+
for (const key of Object.keys(env)) {
|
|
12
|
+
// x.y.z
|
|
13
|
+
if (key.includes('.') && !key.startsWith('.') && !key.endsWith('.')) {
|
|
14
|
+
const subKeys = key.split('.');
|
|
15
|
+
const valueKey = subKeys.pop();
|
|
16
|
+
let tmpEnv = env;
|
|
17
|
+
for (const subKey of subKeys) {
|
|
18
|
+
if (subKey in tmpEnv) { }
|
|
19
|
+
else {
|
|
20
|
+
tmpEnv[subKey] = {};
|
|
21
|
+
}
|
|
22
|
+
tmpEnv = tmpEnv[subKey];
|
|
23
|
+
}
|
|
24
|
+
tmpEnv[valueKey] = env[key];
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
function configure() {
|
|
29
|
+
let env = Object.create(null);
|
|
30
|
+
const defaultEnvPath = argv.getEnvArg('default-env');
|
|
31
|
+
if (defaultEnvPath && fs.existsSync(defaultEnvPath)) {
|
|
32
|
+
Object.assign(env, require(path.resolve(defaultEnvPath)));
|
|
33
|
+
}
|
|
34
|
+
const envPath = argv.getEnvArg('env');
|
|
35
|
+
if (envPath && fs.existsSync(envPath)) {
|
|
36
|
+
Object.assign(env, require(path.resolve(envPath)));
|
|
37
|
+
}
|
|
38
|
+
Object.assign(env, argv.getAllEnvArgs());
|
|
39
|
+
mergeFlatEnv(env);
|
|
40
|
+
if ('string' === typeof env.ignore)
|
|
41
|
+
env.ignore = env.ignore.split(',');
|
|
42
|
+
if ('string' === typeof env.registry)
|
|
43
|
+
env.registry = env.registry.split(',');
|
|
44
|
+
if ('string' === typeof env.origin && env.origin.includes(',')) {
|
|
45
|
+
env.origin = env.origin.split(',');
|
|
46
|
+
}
|
|
47
|
+
return env;
|
|
48
|
+
}
|
|
49
|
+
exports.configure = configure;
|
package/bin/proxyer.js
CHANGED
|
@@ -1,61 +1,61 @@
|
|
|
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() && 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;
|
|
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() && 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,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 socketio = oox.modules.get('socketio');
|
|
7
|
-
const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));
|
|
8
|
-
function urlFormatter(url) {
|
|
9
|
-
// :6000
|
|
10
|
-
if (url.startsWith(':'))
|
|
11
|
-
url = oox.config.host + url;
|
|
12
|
-
// http://127.0.0.1:6000
|
|
13
|
-
if (url.startsWith('http://'))
|
|
14
|
-
url = url.slice(7);
|
|
15
|
-
// 127.0.0.1:6000
|
|
16
|
-
if (!url.startsWith('ws://'))
|
|
17
|
-
url = 'ws://' + url;
|
|
18
|
-
const urlObject = new URL(url);
|
|
19
|
-
if (urlObject.pathname === '/' && !url.endsWith('/'))
|
|
20
|
-
url = url + '/socket.io';
|
|
21
|
-
return url;
|
|
22
|
-
}
|
|
23
|
-
async function connect(url, prevError = null) {
|
|
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 socketio = oox.modules.get('socketio');
|
|
7
|
+
const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));
|
|
8
|
+
function urlFormatter(url) {
|
|
9
|
+
// :6000
|
|
10
|
+
if (url.startsWith(':'))
|
|
11
|
+
url = oox.config.host + url;
|
|
12
|
+
// http://127.0.0.1:6000
|
|
13
|
+
if (url.startsWith('http://'))
|
|
14
|
+
url = url.slice(7);
|
|
15
|
+
// 127.0.0.1:6000
|
|
16
|
+
if (!url.startsWith('ws://'))
|
|
17
|
+
url = 'ws://' + url;
|
|
18
|
+
const urlObject = new URL(url);
|
|
19
|
+
if (urlObject.pathname === '/' && !url.endsWith('/'))
|
|
20
|
+
url = url + '/socket.io';
|
|
21
|
+
return url;
|
|
22
|
+
}
|
|
23
|
+
async function connect(url, prevError = null) {
|
|
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,81 +1,81 @@
|
|
|
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
|
-
const module_socketio_1 = require("@oox/module-socketio");
|
|
11
|
-
// preload modules
|
|
12
|
-
const socketio = new module_socketio_1.default();
|
|
13
|
-
oox.modules.add(socketio);
|
|
14
|
-
function getEntryFile(env) {
|
|
15
|
-
const args = process.argv.slice(2);
|
|
16
|
-
var [entryFilename] = args.filter(arg => !arg.includes('=') && arg.endsWith('.js'));
|
|
17
|
-
if (!entryFilename)
|
|
18
|
-
throw new Error('Cannot find entry file');
|
|
19
|
-
const fullPath = path.resolve(entryFilename);
|
|
20
|
-
const filename = path.basename(fullPath);
|
|
21
|
-
const fullDirectory = path.dirname(fullPath);
|
|
22
|
-
const directory = fullDirectory.split(path.sep).pop();
|
|
23
|
-
const groupFullDirectory = env.group ? path.resolve(env.group) : '';
|
|
24
|
-
var name = filename === 'index.js' && groupFullDirectory !== fullDirectory ? directory : filename.split('.js')[0];
|
|
25
|
-
return { name, path: fullPath, group: groupFullDirectory };
|
|
26
|
-
}
|
|
27
|
-
function loadEntry(name, entryPath) {
|
|
28
|
-
oox.config.name = name;
|
|
29
|
-
const methods = require(entryPath);
|
|
30
|
-
oox.setMethods(methods);
|
|
31
|
-
}
|
|
32
|
-
async function startup() {
|
|
33
|
-
// 加载环境变量
|
|
34
|
-
const env = (0, configurer_1.configure)();
|
|
35
|
-
Object.assign(oox.config, env);
|
|
36
|
-
// 获取服务入口地址
|
|
37
|
-
const entryFile = getEntryFile(env);
|
|
38
|
-
// 代理<服务间调用>
|
|
39
|
-
if (env.group) {
|
|
40
|
-
const excludes = [entryFile.name];
|
|
41
|
-
if (Array.isArray(env.ignore))
|
|
42
|
-
excludes.push(...env.ignore);
|
|
43
|
-
(0, proxyer_1.proxyGroup)(entryFile.group, excludes);
|
|
44
|
-
}
|
|
45
|
-
// 加载服务
|
|
46
|
-
loadEntry(entryFile.name, entryFile.path);
|
|
47
|
-
// 服务配置
|
|
48
|
-
if (!env.port && !env.http && !env.socketio)
|
|
49
|
-
env.port = 0;
|
|
50
|
-
const httpConfig = oox.modules.builtins.http.config, socketioConfig = socketio.config;
|
|
51
|
-
if ('number' === typeof env.port) {
|
|
52
|
-
httpConfig.port = socketioConfig.port = env.port;
|
|
53
|
-
}
|
|
54
|
-
if (env.origin)
|
|
55
|
-
httpConfig.origin = env.origin;
|
|
56
|
-
if ('http' in env) {
|
|
57
|
-
if (env.http)
|
|
58
|
-
Object.assign(httpConfig, env.http);
|
|
59
|
-
else
|
|
60
|
-
httpConfig.disabled = true;
|
|
61
|
-
}
|
|
62
|
-
if ('socketio' in env) {
|
|
63
|
-
if (env.socketio)
|
|
64
|
-
Object.assign(socketioConfig, env.socketio);
|
|
65
|
-
else
|
|
66
|
-
socketioConfig.disabled = true;
|
|
67
|
-
}
|
|
68
|
-
// 服务启动
|
|
69
|
-
await oox.serve();
|
|
70
|
-
console.log();
|
|
71
|
-
console.log('Service', (0, chalk_1.bold) `${oox.config.name}`, 'running.');
|
|
72
|
-
if (!httpConfig.disabled)
|
|
73
|
-
console.log(' at', chalk_1.underline.green `http://${oox.config.host}:${httpConfig.port}${httpConfig.path}`);
|
|
74
|
-
if (!socketioConfig.disabled)
|
|
75
|
-
console.log(' at', chalk_1.underline.green `ws://${oox.config.host}:${socketioConfig.port}${socketioConfig.path}`);
|
|
76
|
-
console.log();
|
|
77
|
-
// 服务注册
|
|
78
|
-
if (env.registry)
|
|
79
|
-
(0, register_1.registry)(env.registry);
|
|
80
|
-
}
|
|
81
|
-
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
|
+
const module_socketio_1 = require("@oox/module-socketio");
|
|
11
|
+
// preload modules
|
|
12
|
+
const socketio = new module_socketio_1.default();
|
|
13
|
+
oox.modules.add(socketio);
|
|
14
|
+
function getEntryFile(env) {
|
|
15
|
+
const args = process.argv.slice(2);
|
|
16
|
+
var [entryFilename] = args.filter(arg => !arg.includes('=') && arg.endsWith('.js'));
|
|
17
|
+
if (!entryFilename)
|
|
18
|
+
throw new Error('Cannot find entry file');
|
|
19
|
+
const fullPath = path.resolve(entryFilename);
|
|
20
|
+
const filename = path.basename(fullPath);
|
|
21
|
+
const fullDirectory = path.dirname(fullPath);
|
|
22
|
+
const directory = fullDirectory.split(path.sep).pop();
|
|
23
|
+
const groupFullDirectory = env.group ? path.resolve(env.group) : '';
|
|
24
|
+
var name = filename === 'index.js' && groupFullDirectory !== fullDirectory ? directory : filename.split('.js')[0];
|
|
25
|
+
return { name, path: fullPath, group: groupFullDirectory };
|
|
26
|
+
}
|
|
27
|
+
function loadEntry(name, entryPath) {
|
|
28
|
+
oox.config.name = name;
|
|
29
|
+
const methods = require(entryPath);
|
|
30
|
+
oox.setMethods(methods);
|
|
31
|
+
}
|
|
32
|
+
async function startup() {
|
|
33
|
+
// 加载环境变量
|
|
34
|
+
const env = (0, configurer_1.configure)();
|
|
35
|
+
Object.assign(oox.config, env);
|
|
36
|
+
// 获取服务入口地址
|
|
37
|
+
const entryFile = getEntryFile(env);
|
|
38
|
+
// 代理<服务间调用>
|
|
39
|
+
if (env.group) {
|
|
40
|
+
const excludes = [entryFile.name];
|
|
41
|
+
if (Array.isArray(env.ignore))
|
|
42
|
+
excludes.push(...env.ignore);
|
|
43
|
+
(0, proxyer_1.proxyGroup)(entryFile.group, excludes);
|
|
44
|
+
}
|
|
45
|
+
// 加载服务
|
|
46
|
+
loadEntry(entryFile.name, entryFile.path);
|
|
47
|
+
// 服务配置
|
|
48
|
+
if (!env.port && !env.http && !env.socketio)
|
|
49
|
+
env.port = 0;
|
|
50
|
+
const httpConfig = oox.modules.builtins.http.config, socketioConfig = socketio.config;
|
|
51
|
+
if ('number' === typeof env.port) {
|
|
52
|
+
httpConfig.port = socketioConfig.port = env.port;
|
|
53
|
+
}
|
|
54
|
+
if (env.origin)
|
|
55
|
+
httpConfig.origin = env.origin;
|
|
56
|
+
if ('http' in env) {
|
|
57
|
+
if (env.http)
|
|
58
|
+
Object.assign(httpConfig, env.http);
|
|
59
|
+
else
|
|
60
|
+
httpConfig.disabled = true;
|
|
61
|
+
}
|
|
62
|
+
if ('socketio' in env) {
|
|
63
|
+
if (env.socketio)
|
|
64
|
+
Object.assign(socketioConfig, env.socketio);
|
|
65
|
+
else
|
|
66
|
+
socketioConfig.disabled = true;
|
|
67
|
+
}
|
|
68
|
+
// 服务启动
|
|
69
|
+
await oox.serve();
|
|
70
|
+
console.log();
|
|
71
|
+
console.log('Service', (0, chalk_1.bold) `${oox.config.name}`, 'running.');
|
|
72
|
+
if (!httpConfig.disabled)
|
|
73
|
+
console.log(' at', chalk_1.underline.green `http://${oox.config.host}:${httpConfig.port}${httpConfig.path}`);
|
|
74
|
+
if (!socketioConfig.disabled)
|
|
75
|
+
console.log(' at', chalk_1.underline.green `ws://${oox.config.host}:${socketioConfig.port}${socketioConfig.path}`);
|
|
76
|
+
console.log();
|
|
77
|
+
// 服务注册
|
|
78
|
+
if (env.registry)
|
|
79
|
+
(0, register_1.registry)(env.registry);
|
|
80
|
+
}
|
|
81
|
+
exports.startup = startup;
|