oox 0.3.0-beta12 → 0.3.0-beta14
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 +29 -41
- package/bin/argv.js +4 -11
- package/bin/cli.js +19 -19
- package/bin/configurer.js +5 -9
- package/bin/loader.mjs +379 -459
- package/bin/proxyer.js +8 -10
- package/bin/register.js +6 -10
- package/bin/starter.js +44 -36
- package/index.js +44 -59
- package/index.mjs +4 -4
- package/logger.js +13 -20
- package/modules/http/index.js +35 -24
- package/modules/http/utils.js +11 -16
- package/modules/index.js +6 -9
- package/modules/module.js +2 -7
- package/modules/socketio/client.js +8 -11
- package/modules/socketio/index.js +17 -21
- package/modules/socketio/server.js +11 -16
- package/modules/socketio/socket.js +1 -4
- package/package.json +7 -7
- package/types/app.d.ts +2 -4
- 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/starter.d.ts +5 -2
- package/types/index.d.ts +7 -6
- package/types/modules/http/index.d.ts +6 -3
- package/types/modules/http/utils.d.ts +0 -3
- package/types/modules/index.d.ts +3 -3
- package/types/modules/socketio/client.d.ts +2 -2
- package/types/modules/socketio/index.d.ts +4 -4
- package/types/modules/socketio/server.d.ts +2 -3
- package/types/modules/socketio/socket.d.ts +2 -2
- package/utils.js +5 -11
package/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2020 lipingruan
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020 lipingruan
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,32 +1,32 @@
|
|
|
1
|
-

|
|
2
|
-
|
|
3
|
-

|
|
4
|
-
|
|
5
|
-
graceful microservice framework.
|
|
6
|
-
|
|
7
|
-
### Features
|
|
8
|
-
- [x] HTTP & socket.io support (native SSL coming soon)
|
|
9
|
-
- [x] Zero configure startup
|
|
10
|
-
- [x] Non-invasive coding style
|
|
11
|
-
- [x] Intuitive route style
|
|
12
|
-
- [x] Intuitive RPC style
|
|
13
|
-
- [x] Anywhere interceptor
|
|
14
|
-
- [x] Distributed tracing
|
|
15
|
-
- [x] Service nodes expand infinitely
|
|
16
|
-
- [x] Automatic service discovery (P2P)
|
|
17
|
-
- [x] Automatic load balancing (support customization)
|
|
18
|
-
- [x] `Distributed / Standalone` seamless handoff
|
|
19
|
-
- [x] Highly customizable
|
|
20
|
-
|
|
21
|
-
### Installation
|
|
22
|
-
```bash
|
|
23
|
-
npm install -g oox
|
|
24
|
-
```
|
|
25
|
-
|
|
26
|
-
### Usage
|
|
27
|
-
```bash
|
|
28
|
-
oox --help
|
|
29
|
-
```
|
|
30
|
-
|
|
31
|
-
### Documentation
|
|
32
|
-
[oox.js.org](https://oox.js.org/)
|
|
1
|
+

|
|
2
|
+
|
|
3
|
+

|
|
4
|
+
|
|
5
|
+
graceful microservice framework.
|
|
6
|
+
|
|
7
|
+
### Features
|
|
8
|
+
- [x] HTTP & socket.io support (native SSL coming soon)
|
|
9
|
+
- [x] Zero configure startup
|
|
10
|
+
- [x] Non-invasive coding style
|
|
11
|
+
- [x] Intuitive route style
|
|
12
|
+
- [x] Intuitive RPC style
|
|
13
|
+
- [x] Anywhere interceptor
|
|
14
|
+
- [x] Distributed tracing
|
|
15
|
+
- [x] Service nodes expand infinitely
|
|
16
|
+
- [x] Automatic service discovery (P2P)
|
|
17
|
+
- [x] Automatic load balancing (support customization)
|
|
18
|
+
- [x] `Distributed / Standalone` seamless handoff
|
|
19
|
+
- [x] Highly customizable
|
|
20
|
+
|
|
21
|
+
### Installation
|
|
22
|
+
```bash
|
|
23
|
+
npm install -g oox
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
### Usage
|
|
27
|
+
```bash
|
|
28
|
+
oox --help
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
### Documentation
|
|
32
|
+
[oox.js.org](https://oox.js.org/)
|
package/app.js
CHANGED
|
@@ -1,17 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
const utils_1 = require("./utils");
|
|
7
|
-
exports.logger = require("./logger");
|
|
8
|
-
class Context {
|
|
1
|
+
import { EventEmitter } from 'node:events';
|
|
2
|
+
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
3
|
+
import { genKVMethods } from './utils.js';
|
|
4
|
+
export * as logger from './logger.js';
|
|
5
|
+
export class Context {
|
|
9
6
|
// 请求溯源ID
|
|
10
7
|
traceId = '';
|
|
11
8
|
}
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
exports.eventHub = new node_events_1.EventEmitter();
|
|
9
|
+
export const asyncStore = new AsyncLocalStorage();
|
|
10
|
+
export const eventHub = new EventEmitter();
|
|
15
11
|
/**
|
|
16
12
|
* sourceMethods => methods
|
|
17
13
|
*/
|
|
@@ -19,35 +15,29 @@ let sourceMethods = {};
|
|
|
19
15
|
/**
|
|
20
16
|
* the kvMethods is all actions refs [has bind this]
|
|
21
17
|
*/
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
function setMethods(methods) {
|
|
18
|
+
export const kvMethods = new Map();
|
|
19
|
+
export const sourceKVMethods = new Map();
|
|
20
|
+
export function setMethods(methods) {
|
|
25
21
|
sourceMethods = methods;
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
22
|
+
kvMethods.clear();
|
|
23
|
+
sourceKVMethods.clear();
|
|
24
|
+
genKVMethods(methods, kvMethods, sourceKVMethods);
|
|
29
25
|
}
|
|
30
|
-
|
|
31
|
-
function getMethods() {
|
|
26
|
+
export function getMethods() {
|
|
32
27
|
return sourceMethods;
|
|
33
28
|
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
exports.eventHub.on(event, listener);
|
|
29
|
+
export function on(event, listener) {
|
|
30
|
+
eventHub.on(event, listener);
|
|
37
31
|
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
exports.eventHub.once(event, listener);
|
|
32
|
+
export function once(event, listener) {
|
|
33
|
+
eventHub.once(event, listener);
|
|
41
34
|
}
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
exports.eventHub.off(event, listener);
|
|
35
|
+
export function off(event, listener) {
|
|
36
|
+
eventHub.off(event, listener);
|
|
45
37
|
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
return exports.eventHub.emit(event, ...args);
|
|
38
|
+
export function emit(event, ...args) {
|
|
39
|
+
return eventHub.emit(event, ...args);
|
|
49
40
|
}
|
|
50
|
-
exports.emit = emit;
|
|
51
41
|
/**
|
|
52
42
|
* Call an Function on RPC server
|
|
53
43
|
* @param action
|
|
@@ -55,11 +45,11 @@ exports.emit = emit;
|
|
|
55
45
|
* @param context
|
|
56
46
|
* @returns
|
|
57
47
|
*/
|
|
58
|
-
async function call(action, params = [], context) {
|
|
48
|
+
export async function call(action, params = [], context) {
|
|
59
49
|
if (!Array.isArray(params))
|
|
60
50
|
params = [params];
|
|
61
51
|
const { traceId } = context;
|
|
62
|
-
|
|
52
|
+
eventHub.emit('request', action, params, context);
|
|
63
53
|
const returns = {
|
|
64
54
|
traceId,
|
|
65
55
|
success: false
|
|
@@ -68,25 +58,24 @@ async function call(action, params = [], context) {
|
|
|
68
58
|
const result = await execute(action, [...params], context);
|
|
69
59
|
returns.body = result;
|
|
70
60
|
returns.success = true;
|
|
71
|
-
|
|
61
|
+
eventHub.emit('success', action, params, context, result);
|
|
72
62
|
}
|
|
73
63
|
catch (error) {
|
|
74
64
|
returns.error = {
|
|
75
65
|
message: error.message,
|
|
76
66
|
stack: error.stack
|
|
77
67
|
};
|
|
78
|
-
|
|
68
|
+
eventHub.emit('fail', action, params, context, error);
|
|
79
69
|
}
|
|
80
70
|
finally {
|
|
81
71
|
return returns;
|
|
82
72
|
}
|
|
83
73
|
}
|
|
84
|
-
|
|
85
|
-
async function execute(action, params, context) {
|
|
74
|
+
export async function execute(action, params, context) {
|
|
86
75
|
const __proxy = '__proxy', _proxy = '_proxy';
|
|
87
76
|
if (__proxy === action || _proxy === action || action.endsWith(_proxy))
|
|
88
77
|
throw new Error('Invalid Action[' + action + ']');
|
|
89
|
-
const methods =
|
|
78
|
+
const methods = kvMethods;
|
|
90
79
|
// 目标函数
|
|
91
80
|
const target = methods.get(action);
|
|
92
81
|
// 目标代理函数
|
|
@@ -94,7 +83,7 @@ async function execute(action, params, context) {
|
|
|
94
83
|
// 即不存在目标也不存在目标代理时, 报错函数不存在
|
|
95
84
|
if (!target && !targetProxy)
|
|
96
85
|
throw new Error('Unknown Action [' + action + ']');
|
|
97
|
-
|
|
86
|
+
asyncStore.enterWith(context);
|
|
98
87
|
// ============================ PROXY BEGIN ============================
|
|
99
88
|
// 最顶层代理
|
|
100
89
|
const topProxy = methods.get(__proxy);
|
|
@@ -140,4 +129,3 @@ async function execute(action, params, context) {
|
|
|
140
129
|
return await target(...params);
|
|
141
130
|
}
|
|
142
131
|
}
|
|
143
|
-
exports.execute = execute;
|
package/bin/argv.js
CHANGED
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.parseEnvArg = exports.getEnvArg = exports.getEnvArgs = exports.getAllEnvArgs = void 0;
|
|
4
|
-
function getAllEnvArgs() {
|
|
1
|
+
export function getAllEnvArgs() {
|
|
5
2
|
const args = process.argv.slice(2);
|
|
6
3
|
const env = {};
|
|
7
4
|
for (const arg of args) {
|
|
@@ -23,17 +20,15 @@ function getAllEnvArgs() {
|
|
|
23
20
|
}
|
|
24
21
|
return env;
|
|
25
22
|
}
|
|
26
|
-
|
|
27
|
-
function getEnvArgs(names) {
|
|
23
|
+
export function getEnvArgs(names) {
|
|
28
24
|
const env = {};
|
|
29
25
|
for (let name of names) {
|
|
30
26
|
env[name] = getEnvArg(name);
|
|
31
27
|
}
|
|
32
28
|
return env;
|
|
33
29
|
}
|
|
34
|
-
exports.getEnvArgs = getEnvArgs;
|
|
35
30
|
// 从命令行参数列表中获取参数值
|
|
36
|
-
function getEnvArg(name) {
|
|
31
|
+
export function getEnvArg(name) {
|
|
37
32
|
if (process.argv.includes(name) ||
|
|
38
33
|
process.argv.includes('-' + name) ||
|
|
39
34
|
process.argv.includes('--' + name)) {
|
|
@@ -48,8 +43,7 @@ function getEnvArg(name) {
|
|
|
48
43
|
return parseEnvArg(arg);
|
|
49
44
|
}
|
|
50
45
|
}
|
|
51
|
-
|
|
52
|
-
function parseEnvArg(value) {
|
|
46
|
+
export function parseEnvArg(value) {
|
|
53
47
|
switch (value) {
|
|
54
48
|
case 'no': return false;
|
|
55
49
|
case 'yes': return true;
|
|
@@ -67,4 +61,3 @@ function parseEnvArg(value) {
|
|
|
67
61
|
}
|
|
68
62
|
}
|
|
69
63
|
}
|
|
70
|
-
exports.parseEnvArg = parseEnvArg;
|
package/bin/cli.js
CHANGED
|
@@ -1,12 +1,9 @@
|
|
|
1
1
|
#! /usr/bin/env node
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const
|
|
6
|
-
|
|
7
|
-
Object.defineProperty(exports, "startup", { enumerable: true, get: function () { return starter_1.startup; } });
|
|
8
|
-
const pkg = require("../package.json");
|
|
9
|
-
exports.version = pkg.version;
|
|
2
|
+
import chalk from 'chalk';
|
|
3
|
+
import { registLoader, configure, startup } from './starter.js';
|
|
4
|
+
import pkg from '../package.json' with { type: 'json' };
|
|
5
|
+
export const version = pkg.version;
|
|
6
|
+
export { registLoader, configure, startup };
|
|
10
7
|
const jsExecutionFilename = process.argv[1];
|
|
11
8
|
if (/(oox.*cli\.js)$/.test(jsExecutionFilename)) {
|
|
12
9
|
const args = process.argv.slice(2);
|
|
@@ -15,35 +12,38 @@ if (/(oox.*cli\.js)$/.test(jsExecutionFilename)) {
|
|
|
15
12
|
if (!command || ['help', '-h', '-help', '--help', 'version', '-v', '-version', '--version'].includes(command)) {
|
|
16
13
|
isStartup = false;
|
|
17
14
|
console.log();
|
|
18
|
-
console.log('OOX Service');
|
|
19
|
-
console.log(
|
|
20
|
-
console.log(
|
|
15
|
+
console.log('OOX Service Engine');
|
|
16
|
+
console.log(chalk.bold `version`, chalk.bold.green `${pkg.version}`);
|
|
17
|
+
console.log(chalk.underline `${pkg.homepage}`);
|
|
21
18
|
console.log();
|
|
22
19
|
}
|
|
23
20
|
if (['help', '-h', '-help', '--help'].includes(command)) {
|
|
24
|
-
console.log(
|
|
21
|
+
console.log(chalk.bold `Usage:`);
|
|
25
22
|
console.log(' oox entry.js');
|
|
26
23
|
console.log();
|
|
27
24
|
console.log(' oox entry.js port=8080');
|
|
28
25
|
console.log();
|
|
29
26
|
console.log(' oox app/entry/index.js group=app/ env=envs/entry.js ignore=core');
|
|
30
27
|
console.log();
|
|
31
|
-
console.log(
|
|
28
|
+
console.log(chalk.bold `Params:`);
|
|
32
29
|
const params = [
|
|
33
30
|
[' default-env [ file ] ', '.js or .json file, merge to oox.config'],
|
|
34
|
-
[' env [ file ] ', '.js or .json file, merge to oox.config',
|
|
35
|
-
[' port [ int ] ', 'set',
|
|
31
|
+
[' env [ file ] ', '.js or .json file, merge to oox.config', chalk.bold `(after default-env)`],
|
|
32
|
+
[' port [ int ] ', 'set', chalk.bold `0`, 'for random port, or any integer > 0'],
|
|
36
33
|
[' group [ dir ] ', 'service group directory, all LocalCall transform to RPC'],
|
|
37
34
|
[' ignore [ name ] ', 'set a name for LocalCall do not transform to RPC, support string | array<string>'],
|
|
38
35
|
[' http [ json ] ', 'HTTP server options, support flat name, ex: http.path=/api'],
|
|
39
36
|
[' socketio [ json ] ', 'SocketIO server options, support flat name'],
|
|
40
37
|
[' registry [ urls ] ', 'registry service url, support string | array<string>'],
|
|
41
|
-
[' origin [ urls ] ', 'set',
|
|
42
|
-
[' ... ', 'set params as',
|
|
38
|
+
[' origin [ urls ] ', 'set', chalk.bold `*`, 'allow any connections <Access-Control-Allow-Origin>'],
|
|
39
|
+
[' ... ', 'set params as', chalk.bold `foo=bar` + ',', 'usage as', chalk.bold `oox.config.foo`]
|
|
43
40
|
];
|
|
44
41
|
params.forEach(row => console.log(...row, '\n'));
|
|
45
42
|
console.log();
|
|
46
43
|
}
|
|
47
|
-
if (isStartup)
|
|
48
|
-
|
|
44
|
+
if (isStartup) {
|
|
45
|
+
await configure();
|
|
46
|
+
await registLoader();
|
|
47
|
+
await startup();
|
|
48
|
+
}
|
|
49
49
|
}
|
package/bin/configurer.js
CHANGED
|
@@ -1,9 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const fs = require("node:fs");
|
|
5
|
-
const path = require("node:path");
|
|
6
|
-
const argv = require("./argv");
|
|
1
|
+
import * as fs from 'node:fs';
|
|
2
|
+
import * as path from 'node:path';
|
|
3
|
+
import * as argv from './argv.js';
|
|
7
4
|
/**
|
|
8
5
|
* x.y=1 => { x: { y: 1 } }
|
|
9
6
|
*/
|
|
@@ -12,7 +9,7 @@ function mergeFlatEnv(env) {
|
|
|
12
9
|
// x.y.z
|
|
13
10
|
if (key.includes('.') && !key.startsWith('.') && !key.endsWith('.')) {
|
|
14
11
|
const subKeys = key.split('.');
|
|
15
|
-
const valueKey = subKeys.pop();
|
|
12
|
+
const valueKey = subKeys.pop() || '';
|
|
16
13
|
let tmpEnv = env;
|
|
17
14
|
for (const subKey of subKeys) {
|
|
18
15
|
if (subKey in tmpEnv) { }
|
|
@@ -42,7 +39,7 @@ async function readEnvFile(filePath) {
|
|
|
42
39
|
}
|
|
43
40
|
return env.default || env;
|
|
44
41
|
}
|
|
45
|
-
async function
|
|
42
|
+
export async function buildConfig(mergeEnv) {
|
|
46
43
|
const env = Object.create(null);
|
|
47
44
|
const defaultEnvPath = argv.getEnvArg('default-env');
|
|
48
45
|
const targetEnvPath = argv.getEnvArg('env');
|
|
@@ -61,4 +58,3 @@ async function configure(mergeEnv) {
|
|
|
61
58
|
Object.assign(env, mergeEnv);
|
|
62
59
|
return env;
|
|
63
60
|
}
|
|
64
|
-
exports.configure = configure;
|