oox 0.3.0-beta8 → 0.3.0-beta9
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/app.js +14 -2
- package/bin/starter.js +2 -0
- package/index.js +2 -2
- package/modules/http/index.js +8 -2
- package/package.json +1 -1
- package/types/app.d.ts +11 -0
- package/types/index.d.ts +1 -1
- package/types/modules/http/utils.d.ts +2 -2
package/app.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.execute = exports.call = exports.on = exports.getMethods = exports.setMethods = exports.sourceKVMethods = exports.kvMethods = exports.eventHub = exports.asyncStore = exports.Context = exports.logger = void 0;
|
|
3
|
+
exports.execute = exports.call = exports.emit = exports.off = exports.once = exports.on = exports.getMethods = exports.setMethods = exports.sourceKVMethods = exports.kvMethods = exports.eventHub = exports.asyncStore = exports.Context = exports.logger = void 0;
|
|
4
4
|
const node_events_1 = require("node:events");
|
|
5
5
|
const node_async_hooks_1 = require("node:async_hooks");
|
|
6
6
|
const utils_1 = require("./utils");
|
|
@@ -33,9 +33,21 @@ function getMethods() {
|
|
|
33
33
|
}
|
|
34
34
|
exports.getMethods = getMethods;
|
|
35
35
|
function on(event, listener) {
|
|
36
|
-
|
|
36
|
+
exports.eventHub.on(event, listener);
|
|
37
37
|
}
|
|
38
38
|
exports.on = on;
|
|
39
|
+
function once(event, listener) {
|
|
40
|
+
exports.eventHub.once(event, listener);
|
|
41
|
+
}
|
|
42
|
+
exports.once = once;
|
|
43
|
+
function off(event, listener) {
|
|
44
|
+
exports.eventHub.off(event, listener);
|
|
45
|
+
}
|
|
46
|
+
exports.off = off;
|
|
47
|
+
function emit(event, ...args) {
|
|
48
|
+
return exports.eventHub.emit(event, ...args);
|
|
49
|
+
}
|
|
50
|
+
exports.emit = emit;
|
|
39
51
|
/**
|
|
40
52
|
* Call an Function on RPC server
|
|
41
53
|
* @param action
|
package/bin/starter.js
CHANGED
|
@@ -47,9 +47,11 @@ async function startup() {
|
|
|
47
47
|
await loadEntry(entryFile.name, entryFile.path);
|
|
48
48
|
// 模块配置
|
|
49
49
|
oox.modules.setConfig(oox.config);
|
|
50
|
+
oox.emit('app:configured');
|
|
50
51
|
const { http: { config: httpConfig }, socketio: { config: socketioConfig } } = oox.modules.builtins;
|
|
51
52
|
// 服务启动
|
|
52
53
|
await oox.serve();
|
|
54
|
+
oox.emit('app:served');
|
|
53
55
|
console.log();
|
|
54
56
|
console.log('Service', (0, chalk_1.bold) `${oox.config.name}`, 'running.');
|
|
55
57
|
if (!httpConfig.disabled)
|
package/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.rpc = exports.setLoadBalancePolicy = exports.removeKeepAliveConnection = exports.addKeepAliveConnection = exports.getKeepAliveConnection = exports.getKeepAliveConnections = exports.keepAliveConnections = exports.RPCKeepAliveConnection = exports.stop = exports.serve = exports.getContext = exports.genContext = exports.genTraceId = exports.setGenTraceIdFunction = exports.config = exports.Config = exports.Context = exports.on = exports.logger = exports.execute = exports.call = exports.sourceKVMethods = exports.kvMethods = exports.getMethods = exports.setMethods = exports.asyncStore = exports.modules = exports.ModuleConfig = exports.Module = void 0;
|
|
3
|
+
exports.rpc = exports.setLoadBalancePolicy = exports.removeKeepAliveConnection = exports.addKeepAliveConnection = exports.getKeepAliveConnection = exports.getKeepAliveConnections = exports.keepAliveConnections = exports.RPCKeepAliveConnection = exports.stop = exports.serve = exports.getContext = exports.genContext = exports.genTraceId = exports.setGenTraceIdFunction = exports.config = exports.Config = exports.Context = exports.emit = exports.off = exports.once = exports.on = exports.logger = exports.execute = exports.call = exports.sourceKVMethods = exports.kvMethods = exports.getMethods = exports.setMethods = exports.asyncStore = exports.modules = exports.ModuleConfig = exports.Module = void 0;
|
|
4
4
|
const node_crypto_1 = require("node:crypto");
|
|
5
5
|
const app = require("./app");
|
|
6
6
|
const utils_1 = require("./utils");
|
|
@@ -9,7 +9,7 @@ exports.Module = module_1.default;
|
|
|
9
9
|
Object.defineProperty(exports, "ModuleConfig", { enumerable: true, get: function () { return module_1.ModuleConfig; } });
|
|
10
10
|
const modules_1 = require("./modules");
|
|
11
11
|
exports.modules = new modules_1.default;
|
|
12
|
-
exports.asyncStore = app.asyncStore, exports.setMethods = app.setMethods, exports.getMethods = app.getMethods, exports.kvMethods = app.kvMethods, exports.sourceKVMethods = app.sourceKVMethods, exports.call = app.call, exports.execute = app.execute, exports.logger = app.logger, exports.on = app.on;
|
|
12
|
+
exports.asyncStore = app.asyncStore, exports.setMethods = app.setMethods, exports.getMethods = app.getMethods, exports.kvMethods = app.kvMethods, exports.sourceKVMethods = app.sourceKVMethods, exports.call = app.call, exports.execute = app.execute, exports.logger = app.logger, exports.on = app.on, exports.once = app.once, exports.off = app.off, exports.emit = app.emit;
|
|
13
13
|
class Context extends app.Context {
|
|
14
14
|
// 请求溯源IP
|
|
15
15
|
sourceIP = '';
|
package/modules/http/index.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.HTTPConfig = void 0;
|
|
4
4
|
const http = require("node:http");
|
|
5
|
+
const node_querystring_1 = require("node:querystring");
|
|
5
6
|
const utils_1 = require("./utils");
|
|
6
7
|
const oox = require("../../index");
|
|
7
8
|
const module_1 = require("../module");
|
|
@@ -104,7 +105,12 @@ class HTTPModule extends module_1.default {
|
|
|
104
105
|
return;
|
|
105
106
|
let body = Object.create(null);
|
|
106
107
|
try {
|
|
107
|
-
|
|
108
|
+
if ('GET' === request.method) {
|
|
109
|
+
body = (0, node_querystring_1.parse)(request.url.split('?').pop());
|
|
110
|
+
}
|
|
111
|
+
else {
|
|
112
|
+
body = await (0, utils_1.parseHTTPBody)(request);
|
|
113
|
+
}
|
|
108
114
|
if (!body || 'object' !== typeof body)
|
|
109
115
|
throw new Error('Content Invalid');
|
|
110
116
|
}
|
|
@@ -125,7 +131,7 @@ class HTTPModule extends module_1.default {
|
|
|
125
131
|
const ip = String(request.headers['x-ip'] || request.socket.remoteAddress || '');
|
|
126
132
|
// startup client ip
|
|
127
133
|
const sourceIP = String(request.headers['x-real-ip'] || '');
|
|
128
|
-
const { action, params = [] } = body;
|
|
134
|
+
const { action = 'index', params = [] } = body;
|
|
129
135
|
const context = oox.genContext({ traceId, caller, sourceIP, ip, callerId: '' });
|
|
130
136
|
const format = await oox.call(action, params, context);
|
|
131
137
|
this.respond(request, response, format);
|
package/package.json
CHANGED
package/types/app.d.ts
CHANGED
|
@@ -25,10 +25,21 @@ export declare const kvMethods: Map<string, Function>;
|
|
|
25
25
|
export declare const sourceKVMethods: Map<string, Function>;
|
|
26
26
|
export declare function setMethods(methods: any): void;
|
|
27
27
|
export declare function getMethods(): any;
|
|
28
|
+
export declare function on(event: 'app:configured' | 'app:served' | 'app:stopped', listener: () => void): void;
|
|
28
29
|
export declare function on(event: 'request', listener: (action: string, params: any[], context: Context) => void): void;
|
|
29
30
|
export declare function on(event: 'success', listener: (action: string, params: any[], context: Context, result: ReturnsBody) => void): void;
|
|
30
31
|
export declare function on(event: 'fail', listener: (action: string, params: any[], context: Context, error: Error) => void): void;
|
|
31
32
|
export declare function on(event: 'log', listener: (context: Context, tag: string, msgs: any[]) => void): void;
|
|
33
|
+
export declare function on(event: string, listener: (...args: any[]) => void): void;
|
|
34
|
+
export declare function once(event: 'app:configured' | 'app:served' | 'app:stopped', listener: (...args: any[]) => void): void;
|
|
35
|
+
export declare function once(event: 'request', listener: (action: string, params: any[], context: Context) => void): void;
|
|
36
|
+
export declare function once(event: 'success', listener: (action: string, params: any[], context: Context, result: ReturnsBody) => void): void;
|
|
37
|
+
export declare function once(event: 'fail', listener: (action: string, params: any[], context: Context, error: Error) => void): void;
|
|
38
|
+
export declare function once(event: 'log', listener: (context: Context, tag: string, msgs: any[]) => void): void;
|
|
39
|
+
export declare function once(event: string, listener: (...args: any[]) => void): void;
|
|
40
|
+
export declare function off(event: 'app:configured' | 'app:served' | 'app:stopped' | 'request' | 'success' | 'fail' | 'log', listener: (...args: any[]) => void): void;
|
|
41
|
+
export declare function off(event: string, listener: (...args: any[]) => void): void;
|
|
42
|
+
export declare function emit(event: string, ...args: any[]): boolean;
|
|
32
43
|
/**
|
|
33
44
|
* Call an Function on RPC server
|
|
34
45
|
* @param action
|
package/types/index.d.ts
CHANGED
|
@@ -5,7 +5,7 @@ import Modules from './modules';
|
|
|
5
5
|
export { ReturnsBody } from './app';
|
|
6
6
|
export { Module, ModuleConfig };
|
|
7
7
|
export declare const modules: Modules;
|
|
8
|
-
export declare const asyncStore: import("async_hooks").AsyncLocalStorage<app.Context>, setMethods: typeof app.setMethods, getMethods: typeof app.getMethods, kvMethods: Map<string, Function>, sourceKVMethods: Map<string, Function>, call: typeof app.call, execute: typeof app.execute, logger: typeof app.logger, on: typeof app.on;
|
|
8
|
+
export declare const asyncStore: import("async_hooks").AsyncLocalStorage<app.Context>, setMethods: typeof app.setMethods, getMethods: typeof app.getMethods, kvMethods: Map<string, Function>, sourceKVMethods: Map<string, Function>, call: typeof app.call, execute: typeof app.execute, logger: typeof app.logger, on: typeof app.on, once: typeof app.once, off: typeof app.off, emit: typeof app.emit;
|
|
9
9
|
export declare class Context extends app.Context {
|
|
10
10
|
sourceIP: string;
|
|
11
11
|
ip: string;
|
|
@@ -2,11 +2,11 @@
|
|
|
2
2
|
/// <reference types="node" />
|
|
3
3
|
/// <reference types="node" />
|
|
4
4
|
import * as http from 'node:http';
|
|
5
|
-
import
|
|
5
|
+
import { Readable } from 'node:stream';
|
|
6
6
|
/**
|
|
7
7
|
* Stream => Buffer
|
|
8
8
|
*/
|
|
9
|
-
export declare function stream2buffer(stream:
|
|
9
|
+
export declare function stream2buffer(stream: Readable, totalLength?: number): Promise<Buffer>;
|
|
10
10
|
/**
|
|
11
11
|
* Request => JSONObject
|
|
12
12
|
*/
|