oox 0.3.0-beta2 → 0.3.0-beta5
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/index.js
CHANGED
|
@@ -1,144 +1,144 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.rpc = 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.getConfig = exports.config = exports.Config = exports.Context = exports.on = exports.execute = exports.call = exports.sourceKVMethods = exports.kvMethods = exports.getMethods = exports.setMethods = exports.asyncStore = exports.modules = exports.ModuleConfig = exports.Module = void 0;
|
|
4
|
-
const node_crypto_1 = require("node:crypto");
|
|
5
|
-
const app = require("./app");
|
|
6
|
-
const utils_1 = require("./utils");
|
|
7
|
-
const module_1 = require("./modules/module");
|
|
8
|
-
exports.Module = module_1.default;
|
|
9
|
-
Object.defineProperty(exports, "ModuleConfig", { enumerable: true, get: function () { return module_1.ModuleConfig; } });
|
|
10
|
-
const modules_1 = require("./modules");
|
|
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.on = app.on;
|
|
13
|
-
class Context extends app.Context {
|
|
14
|
-
// 请求溯源IP
|
|
15
|
-
sourceIP = '';
|
|
16
|
-
// 请求者IP
|
|
17
|
-
ip = '';
|
|
18
|
-
// 请求者名称
|
|
19
|
-
caller = 'anonymous';
|
|
20
|
-
// 请求者ID (长连接专用)
|
|
21
|
-
callerId = '';
|
|
22
|
-
// 请求者连接把柄
|
|
23
|
-
connection;
|
|
24
|
-
toJSON() {
|
|
25
|
-
const context = Object.assign({}, this);
|
|
26
|
-
delete context.connection;
|
|
27
|
-
return JSON.stringify(context);
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
exports.Context = Context;
|
|
31
|
-
class Config {
|
|
32
|
-
// 服务名称
|
|
33
|
-
name = 'local';
|
|
34
|
-
// 主机地址
|
|
35
|
-
host = (0, utils_1.getIPAddress)(4)[0];
|
|
36
|
-
}
|
|
37
|
-
exports.Config = Config;
|
|
38
|
-
exports.config = new Config();
|
|
39
|
-
function getConfig() {
|
|
40
|
-
}
|
|
41
|
-
exports.getConfig = getConfig;
|
|
42
|
-
let genTraceIdFunction = () => (0, node_crypto_1.randomUUID)();
|
|
43
|
-
function setGenTraceIdFunction(fn) {
|
|
44
|
-
genTraceIdFunction = fn;
|
|
45
|
-
}
|
|
46
|
-
exports.setGenTraceIdFunction = setGenTraceIdFunction;
|
|
47
|
-
/**
|
|
48
|
-
* 生成随机不重复id
|
|
49
|
-
*/
|
|
50
|
-
function genTraceId() {
|
|
51
|
-
return genTraceIdFunction();
|
|
52
|
-
}
|
|
53
|
-
exports.genTraceId = genTraceId;
|
|
54
|
-
/**
|
|
55
|
-
* 获取链路跟踪上下文
|
|
56
|
-
*/
|
|
57
|
-
function genContext(context) {
|
|
58
|
-
const newContext = Object.assign(new Context(), context);
|
|
59
|
-
if (!newContext.traceId)
|
|
60
|
-
newContext.traceId = genTraceId();
|
|
61
|
-
if (!newContext.sourceIP)
|
|
62
|
-
newContext.sourceIP = newContext.ip;
|
|
63
|
-
return newContext;
|
|
64
|
-
}
|
|
65
|
-
exports.genContext = genContext;
|
|
66
|
-
function getContext() {
|
|
67
|
-
const context = exports.asyncStore.getStore();
|
|
68
|
-
return context || genContext();
|
|
69
|
-
}
|
|
70
|
-
exports.getContext = getContext;
|
|
71
|
-
async function serve() {
|
|
72
|
-
await exports.modules.serve();
|
|
73
|
-
}
|
|
74
|
-
exports.serve = serve;
|
|
75
|
-
async function stop() {
|
|
76
|
-
await exports.modules.stop();
|
|
77
|
-
}
|
|
78
|
-
exports.stop = stop;
|
|
79
|
-
class RPCKeepAliveConnection {
|
|
80
|
-
data;
|
|
81
|
-
nativeConnection = null;
|
|
82
|
-
adapter = null;
|
|
83
|
-
constructor(adapter, nativeConnection, data) {
|
|
84
|
-
this.adapter = adapter;
|
|
85
|
-
this.nativeConnection = nativeConnection;
|
|
86
|
-
if (data)
|
|
87
|
-
this.data = data;
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
exports.RPCKeepAliveConnection = RPCKeepAliveConnection;
|
|
91
|
-
exports.keepAliveConnections = new Map();
|
|
92
|
-
function getKeepAliveConnections(name) {
|
|
93
|
-
return exports.keepAliveConnections.get(name) || new Map();
|
|
94
|
-
}
|
|
95
|
-
exports.getKeepAliveConnections = getKeepAliveConnections;
|
|
96
|
-
function getKeepAliveConnection(name, id) {
|
|
97
|
-
const connections = exports.keepAliveConnections.get(name);
|
|
98
|
-
if (!connections)
|
|
99
|
-
return null;
|
|
100
|
-
return connections.get(id);
|
|
101
|
-
}
|
|
102
|
-
exports.getKeepAliveConnection = getKeepAliveConnection;
|
|
103
|
-
function addKeepAliveConnection(connection) {
|
|
104
|
-
const { name, id } = connection.data;
|
|
105
|
-
if (exports.keepAliveConnections.has(name)) {
|
|
106
|
-
exports.keepAliveConnections.get(name).set(id, connection);
|
|
107
|
-
}
|
|
108
|
-
else {
|
|
109
|
-
exports.keepAliveConnections.set(name, new Map([[id, connection]]));
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
exports.addKeepAliveConnection = addKeepAliveConnection;
|
|
113
|
-
function removeKeepAliveConnection(name, id) {
|
|
114
|
-
if (name instanceof RPCKeepAliveConnection) {
|
|
115
|
-
id = name.data.id;
|
|
116
|
-
name = name.data.name;
|
|
117
|
-
}
|
|
118
|
-
if (exports.keepAliveConnections.has(name)) {
|
|
119
|
-
const group = exports.keepAliveConnections.get(name);
|
|
120
|
-
group.delete(id);
|
|
121
|
-
if (!group.size)
|
|
122
|
-
exports.keepAliveConnections.delete(name);
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
exports.removeKeepAliveConnection = removeKeepAliveConnection;
|
|
126
|
-
async function rpc(arg1, action, params, context) {
|
|
127
|
-
if (!context || !context.traceId) {
|
|
128
|
-
context = getContext();
|
|
129
|
-
}
|
|
130
|
-
let connection;
|
|
131
|
-
if (arg1 instanceof RPCKeepAliveConnection) {
|
|
132
|
-
connection = arg1;
|
|
133
|
-
}
|
|
134
|
-
else if ('string' === typeof arg1) {
|
|
135
|
-
const connections = exports.keepAliveConnections.get(arg1);
|
|
136
|
-
if (!connections || !connections.size)
|
|
137
|
-
throw new Error(`Connection<${arg1}> not found`);
|
|
138
|
-
connection = connections.values().next().value;
|
|
139
|
-
}
|
|
140
|
-
else
|
|
141
|
-
throw new Error(`Unknown rpc arg1<${arg1}>`);
|
|
142
|
-
return connection.adapter.rpc(connection.nativeConnection, action, params, context);
|
|
143
|
-
}
|
|
144
|
-
exports.rpc = rpc;
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.rpc = 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.getConfig = exports.config = exports.Config = exports.Context = exports.on = exports.execute = exports.call = exports.sourceKVMethods = exports.kvMethods = exports.getMethods = exports.setMethods = exports.asyncStore = exports.modules = exports.ModuleConfig = exports.Module = void 0;
|
|
4
|
+
const node_crypto_1 = require("node:crypto");
|
|
5
|
+
const app = require("./app");
|
|
6
|
+
const utils_1 = require("./utils");
|
|
7
|
+
const module_1 = require("./modules/module");
|
|
8
|
+
exports.Module = module_1.default;
|
|
9
|
+
Object.defineProperty(exports, "ModuleConfig", { enumerable: true, get: function () { return module_1.ModuleConfig; } });
|
|
10
|
+
const modules_1 = require("./modules");
|
|
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.on = app.on;
|
|
13
|
+
class Context extends app.Context {
|
|
14
|
+
// 请求溯源IP
|
|
15
|
+
sourceIP = '';
|
|
16
|
+
// 请求者IP
|
|
17
|
+
ip = '';
|
|
18
|
+
// 请求者名称
|
|
19
|
+
caller = 'anonymous';
|
|
20
|
+
// 请求者ID (长连接专用)
|
|
21
|
+
callerId = '';
|
|
22
|
+
// 请求者连接把柄
|
|
23
|
+
connection;
|
|
24
|
+
toJSON() {
|
|
25
|
+
const context = Object.assign({}, this);
|
|
26
|
+
delete context.connection;
|
|
27
|
+
return JSON.stringify(context);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
exports.Context = Context;
|
|
31
|
+
class Config {
|
|
32
|
+
// 服务名称
|
|
33
|
+
name = 'local';
|
|
34
|
+
// 主机地址
|
|
35
|
+
host = (0, utils_1.getIPAddress)(4)[0];
|
|
36
|
+
}
|
|
37
|
+
exports.Config = Config;
|
|
38
|
+
exports.config = new Config();
|
|
39
|
+
function getConfig() {
|
|
40
|
+
}
|
|
41
|
+
exports.getConfig = getConfig;
|
|
42
|
+
let genTraceIdFunction = () => (0, node_crypto_1.randomUUID)();
|
|
43
|
+
function setGenTraceIdFunction(fn) {
|
|
44
|
+
genTraceIdFunction = fn;
|
|
45
|
+
}
|
|
46
|
+
exports.setGenTraceIdFunction = setGenTraceIdFunction;
|
|
47
|
+
/**
|
|
48
|
+
* 生成随机不重复id
|
|
49
|
+
*/
|
|
50
|
+
function genTraceId() {
|
|
51
|
+
return genTraceIdFunction();
|
|
52
|
+
}
|
|
53
|
+
exports.genTraceId = genTraceId;
|
|
54
|
+
/**
|
|
55
|
+
* 获取链路跟踪上下文
|
|
56
|
+
*/
|
|
57
|
+
function genContext(context) {
|
|
58
|
+
const newContext = Object.assign(new Context(), context);
|
|
59
|
+
if (!newContext.traceId)
|
|
60
|
+
newContext.traceId = genTraceId();
|
|
61
|
+
if (!newContext.sourceIP)
|
|
62
|
+
newContext.sourceIP = newContext.ip;
|
|
63
|
+
return newContext;
|
|
64
|
+
}
|
|
65
|
+
exports.genContext = genContext;
|
|
66
|
+
function getContext() {
|
|
67
|
+
const context = exports.asyncStore.getStore();
|
|
68
|
+
return context || genContext();
|
|
69
|
+
}
|
|
70
|
+
exports.getContext = getContext;
|
|
71
|
+
async function serve() {
|
|
72
|
+
await exports.modules.serve();
|
|
73
|
+
}
|
|
74
|
+
exports.serve = serve;
|
|
75
|
+
async function stop() {
|
|
76
|
+
await exports.modules.stop();
|
|
77
|
+
}
|
|
78
|
+
exports.stop = stop;
|
|
79
|
+
class RPCKeepAliveConnection {
|
|
80
|
+
data;
|
|
81
|
+
nativeConnection = null;
|
|
82
|
+
adapter = null;
|
|
83
|
+
constructor(adapter, nativeConnection, data) {
|
|
84
|
+
this.adapter = adapter;
|
|
85
|
+
this.nativeConnection = nativeConnection;
|
|
86
|
+
if (data)
|
|
87
|
+
this.data = data;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
exports.RPCKeepAliveConnection = RPCKeepAliveConnection;
|
|
91
|
+
exports.keepAliveConnections = new Map();
|
|
92
|
+
function getKeepAliveConnections(name) {
|
|
93
|
+
return exports.keepAliveConnections.get(name) || new Map();
|
|
94
|
+
}
|
|
95
|
+
exports.getKeepAliveConnections = getKeepAliveConnections;
|
|
96
|
+
function getKeepAliveConnection(name, id) {
|
|
97
|
+
const connections = exports.keepAliveConnections.get(name);
|
|
98
|
+
if (!connections)
|
|
99
|
+
return null;
|
|
100
|
+
return connections.get(id);
|
|
101
|
+
}
|
|
102
|
+
exports.getKeepAliveConnection = getKeepAliveConnection;
|
|
103
|
+
function addKeepAliveConnection(connection) {
|
|
104
|
+
const { name, id } = connection.data;
|
|
105
|
+
if (exports.keepAliveConnections.has(name)) {
|
|
106
|
+
exports.keepAliveConnections.get(name).set(id, connection);
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
exports.keepAliveConnections.set(name, new Map([[id, connection]]));
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
exports.addKeepAliveConnection = addKeepAliveConnection;
|
|
113
|
+
function removeKeepAliveConnection(name, id) {
|
|
114
|
+
if (name instanceof RPCKeepAliveConnection) {
|
|
115
|
+
id = name.data.id;
|
|
116
|
+
name = name.data.name;
|
|
117
|
+
}
|
|
118
|
+
if (exports.keepAliveConnections.has(name)) {
|
|
119
|
+
const group = exports.keepAliveConnections.get(name);
|
|
120
|
+
group.delete(id);
|
|
121
|
+
if (!group.size)
|
|
122
|
+
exports.keepAliveConnections.delete(name);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
exports.removeKeepAliveConnection = removeKeepAliveConnection;
|
|
126
|
+
async function rpc(arg1, action, params, context) {
|
|
127
|
+
if (!context || !context.traceId) {
|
|
128
|
+
context = getContext();
|
|
129
|
+
}
|
|
130
|
+
let connection;
|
|
131
|
+
if (arg1 instanceof RPCKeepAliveConnection) {
|
|
132
|
+
connection = arg1;
|
|
133
|
+
}
|
|
134
|
+
else if ('string' === typeof arg1) {
|
|
135
|
+
const connections = exports.keepAliveConnections.get(arg1);
|
|
136
|
+
if (!connections || !connections.size)
|
|
137
|
+
throw new Error(`Connection<${arg1}> not found`);
|
|
138
|
+
connection = connections.values().next().value;
|
|
139
|
+
}
|
|
140
|
+
else
|
|
141
|
+
throw new Error(`Unknown rpc arg1<${arg1}>`);
|
|
142
|
+
return connection.adapter.rpc(connection.nativeConnection, action, params, context);
|
|
143
|
+
}
|
|
144
|
+
exports.rpc = rpc;
|
package/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
import * as oox from './index.js'
|
|
3
|
-
|
|
1
|
+
|
|
2
|
+
import * as oox from './index.js'
|
|
3
|
+
|
|
4
4
|
export default oox
|
package/modules/http/index.js
CHANGED
|
@@ -1,180 +1,180 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.HTTPConfig = void 0;
|
|
4
|
-
const http = require("node:http");
|
|
5
|
-
const utils_1 = require("./utils");
|
|
6
|
-
const oox = require("../../index");
|
|
7
|
-
const module_1 = require("../module");
|
|
8
|
-
class HTTPConfig extends module_1.ModuleConfig {
|
|
9
|
-
// listen port
|
|
10
|
-
port = 0;
|
|
11
|
-
// service path
|
|
12
|
-
path = '/';
|
|
13
|
-
// browser cross origin
|
|
14
|
-
origin = '';
|
|
15
|
-
}
|
|
16
|
-
exports.HTTPConfig = HTTPConfig;
|
|
17
|
-
class HTTPModule extends module_1.default {
|
|
18
|
-
name = '
|
|
19
|
-
config = new HTTPConfig;
|
|
20
|
-
server = null;
|
|
21
|
-
setConfig(config) {
|
|
22
|
-
Object.assign(this.config, config);
|
|
23
|
-
}
|
|
24
|
-
getConfig() {
|
|
25
|
-
return this.config;
|
|
26
|
-
}
|
|
27
|
-
/**
|
|
28
|
-
* start http service
|
|
29
|
-
*/
|
|
30
|
-
async serve() {
|
|
31
|
-
await this.stop();
|
|
32
|
-
const { port } = this.config;
|
|
33
|
-
this.server = http.createServer(this.call.bind(this));
|
|
34
|
-
this.server.listen(port);
|
|
35
|
-
const address = this.server.address();
|
|
36
|
-
if (!address || 'object' !== typeof address)
|
|
37
|
-
throw new Error('Cannot read http server port');
|
|
38
|
-
this.config.port = address.port;
|
|
39
|
-
}
|
|
40
|
-
/**
|
|
41
|
-
* stop http service
|
|
42
|
-
*/
|
|
43
|
-
stop() {
|
|
44
|
-
if (this.server && this.server.listening)
|
|
45
|
-
return new Promise((resolve, reject) => {
|
|
46
|
-
this.server.close(function (error) {
|
|
47
|
-
if (error)
|
|
48
|
-
reject(error);
|
|
49
|
-
else
|
|
50
|
-
resolve();
|
|
51
|
-
});
|
|
52
|
-
});
|
|
53
|
-
}
|
|
54
|
-
/**
|
|
55
|
-
* browser cross origin
|
|
56
|
-
*/
|
|
57
|
-
cors(request, response) {
|
|
58
|
-
// origin checking
|
|
59
|
-
const origin = this.config.origin;
|
|
60
|
-
const requestOrigin = request.headers.origin;
|
|
61
|
-
if (origin && requestOrigin) {
|
|
62
|
-
if (origin === '*' || origin === requestOrigin || Array.isArray(origin) && origin.includes(requestOrigin)) {
|
|
63
|
-
response.setHeader('Access-Control-Allow-Origin', requestOrigin);
|
|
64
|
-
response.setHeader('Vary', 'Origin');
|
|
65
|
-
}
|
|
66
|
-
else {
|
|
67
|
-
response.statusCode = 403;
|
|
68
|
-
response.end();
|
|
69
|
-
return false;
|
|
70
|
-
}
|
|
71
|
-
response.setHeader('Access-Control-Max-Age', 3600);
|
|
72
|
-
response.setHeader('Access-Control-Allow-Headers', 'x-caller,content-type');
|
|
73
|
-
response.setHeader('Access-Control-Allow-Methods', '*');
|
|
74
|
-
}
|
|
75
|
-
if (request.method === 'OPTIONS') {
|
|
76
|
-
response.statusCode = 204;
|
|
77
|
-
response.end();
|
|
78
|
-
return false;
|
|
79
|
-
}
|
|
80
|
-
return true;
|
|
81
|
-
}
|
|
82
|
-
/**
|
|
83
|
-
* HTTP-RPC服务器请求监听方法
|
|
84
|
-
*/
|
|
85
|
-
async call(request, response) {
|
|
86
|
-
if (request.url !== this.config.path) {
|
|
87
|
-
const error = {
|
|
88
|
-
message: 'Invalid URL',
|
|
89
|
-
stack: ''
|
|
90
|
-
};
|
|
91
|
-
Error.captureStackTrace(error);
|
|
92
|
-
return this.respond(request, response, {
|
|
93
|
-
success: false,
|
|
94
|
-
error
|
|
95
|
-
});
|
|
96
|
-
}
|
|
97
|
-
if (!this.cors(request, response))
|
|
98
|
-
return;
|
|
99
|
-
let body = Object.create(null);
|
|
100
|
-
try {
|
|
101
|
-
body = await (0, utils_1.parseHTTPBody)(request);
|
|
102
|
-
if (!body || 'object' !== typeof body)
|
|
103
|
-
throw new Error('Content Invalid');
|
|
104
|
-
}
|
|
105
|
-
catch (error) {
|
|
106
|
-
return this.respond(request, response, {
|
|
107
|
-
success: false,
|
|
108
|
-
error: {
|
|
109
|
-
message: error.message,
|
|
110
|
-
stack: error.stack
|
|
111
|
-
}
|
|
112
|
-
});
|
|
113
|
-
}
|
|
114
|
-
// global unique id
|
|
115
|
-
const traceId = String(request.headers['x-trace-id'] || '');
|
|
116
|
-
// service name, required
|
|
117
|
-
const caller = String(request.headers['x-caller'] || 'anonymous');
|
|
118
|
-
// client ip or caller service ip
|
|
119
|
-
const ip = String(request.headers['x-ip'] || request.socket.remoteAddress || '');
|
|
120
|
-
// startup client ip
|
|
121
|
-
const sourceIP = String(request.headers['x-real-ip'] || '');
|
|
122
|
-
const { action, params = [] } = body;
|
|
123
|
-
const context = oox.genContext({ traceId, caller, sourceIP, ip, callerId: '' });
|
|
124
|
-
const format = await oox.call(action, params, context);
|
|
125
|
-
this.respond(request, response, format);
|
|
126
|
-
}
|
|
127
|
-
/**
|
|
128
|
-
* HTTP Response Catch
|
|
129
|
-
*/
|
|
130
|
-
respond(request, response, format) {
|
|
131
|
-
let formatString = '';
|
|
132
|
-
try {
|
|
133
|
-
formatString = JSON.stringify(format);
|
|
134
|
-
}
|
|
135
|
-
catch ({ message, stack }) {
|
|
136
|
-
delete format.body;
|
|
137
|
-
format.success = false;
|
|
138
|
-
format.error = {
|
|
139
|
-
message,
|
|
140
|
-
stack
|
|
141
|
-
};
|
|
142
|
-
formatString = JSON.stringify(format);
|
|
143
|
-
}
|
|
144
|
-
response.setHeader('Content-Type', 'application/json');
|
|
145
|
-
response.setHeader('Content-Length', Buffer.byteLength(formatString));
|
|
146
|
-
response.end(formatString);
|
|
147
|
-
}
|
|
148
|
-
/**
|
|
149
|
-
* HTTP RPC
|
|
150
|
-
*/
|
|
151
|
-
async rpc(url, action, params, context) {
|
|
152
|
-
if (!context || !context.traceId) {
|
|
153
|
-
context = oox.getContext();
|
|
154
|
-
}
|
|
155
|
-
const { traceId, caller, sourceIP } = context;
|
|
156
|
-
const headers = {
|
|
157
|
-
'Content-Type': 'application/json',
|
|
158
|
-
'x-trace-id': String(traceId),
|
|
159
|
-
};
|
|
160
|
-
if (caller)
|
|
161
|
-
headers['x-caller'] = String(caller);
|
|
162
|
-
if (sourceIP)
|
|
163
|
-
headers['x-real-ip'] = sourceIP;
|
|
164
|
-
// headers [ 'x-ip' ] = getIPAddress ( 4 ) [ 0 ]
|
|
165
|
-
const format = await (0, utils_1.httpRequest)(url, {
|
|
166
|
-
headers
|
|
167
|
-
}, JSON.stringify({ action, params }));
|
|
168
|
-
if ('string' === typeof format)
|
|
169
|
-
throw new Error(format);
|
|
170
|
-
const { error, body } = format;
|
|
171
|
-
if (error) {
|
|
172
|
-
const asyncError = new Error(error.message);
|
|
173
|
-
throw asyncError;
|
|
174
|
-
}
|
|
175
|
-
else {
|
|
176
|
-
return body;
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
exports.default = HTTPModule;
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.HTTPConfig = void 0;
|
|
4
|
+
const http = require("node:http");
|
|
5
|
+
const utils_1 = require("./utils");
|
|
6
|
+
const oox = require("../../index");
|
|
7
|
+
const module_1 = require("../module");
|
|
8
|
+
class HTTPConfig extends module_1.ModuleConfig {
|
|
9
|
+
// listen port
|
|
10
|
+
port = 0;
|
|
11
|
+
// service path
|
|
12
|
+
path = '/';
|
|
13
|
+
// browser cross origin
|
|
14
|
+
origin = '';
|
|
15
|
+
}
|
|
16
|
+
exports.HTTPConfig = HTTPConfig;
|
|
17
|
+
class HTTPModule extends module_1.default {
|
|
18
|
+
name = 'http';
|
|
19
|
+
config = new HTTPConfig;
|
|
20
|
+
server = null;
|
|
21
|
+
setConfig(config) {
|
|
22
|
+
Object.assign(this.config, config);
|
|
23
|
+
}
|
|
24
|
+
getConfig() {
|
|
25
|
+
return this.config;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* start http service
|
|
29
|
+
*/
|
|
30
|
+
async serve() {
|
|
31
|
+
await this.stop();
|
|
32
|
+
const { port } = this.config;
|
|
33
|
+
this.server = http.createServer(this.call.bind(this));
|
|
34
|
+
this.server.listen(port);
|
|
35
|
+
const address = this.server.address();
|
|
36
|
+
if (!address || 'object' !== typeof address)
|
|
37
|
+
throw new Error('Cannot read http server port');
|
|
38
|
+
this.config.port = address.port;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* stop http service
|
|
42
|
+
*/
|
|
43
|
+
stop() {
|
|
44
|
+
if (this.server && this.server.listening)
|
|
45
|
+
return new Promise((resolve, reject) => {
|
|
46
|
+
this.server.close(function (error) {
|
|
47
|
+
if (error)
|
|
48
|
+
reject(error);
|
|
49
|
+
else
|
|
50
|
+
resolve();
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* browser cross origin
|
|
56
|
+
*/
|
|
57
|
+
cors(request, response) {
|
|
58
|
+
// origin checking
|
|
59
|
+
const origin = this.config.origin;
|
|
60
|
+
const requestOrigin = request.headers.origin;
|
|
61
|
+
if (origin && requestOrigin) {
|
|
62
|
+
if (origin === '*' || origin === requestOrigin || Array.isArray(origin) && origin.includes(requestOrigin)) {
|
|
63
|
+
response.setHeader('Access-Control-Allow-Origin', requestOrigin);
|
|
64
|
+
response.setHeader('Vary', 'Origin');
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
response.statusCode = 403;
|
|
68
|
+
response.end();
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
response.setHeader('Access-Control-Max-Age', 3600);
|
|
72
|
+
response.setHeader('Access-Control-Allow-Headers', 'x-caller,content-type');
|
|
73
|
+
response.setHeader('Access-Control-Allow-Methods', '*');
|
|
74
|
+
}
|
|
75
|
+
if (request.method === 'OPTIONS') {
|
|
76
|
+
response.statusCode = 204;
|
|
77
|
+
response.end();
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
return true;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* HTTP-RPC服务器请求监听方法
|
|
84
|
+
*/
|
|
85
|
+
async call(request, response) {
|
|
86
|
+
if (request.url !== this.config.path) {
|
|
87
|
+
const error = {
|
|
88
|
+
message: 'Invalid URL',
|
|
89
|
+
stack: ''
|
|
90
|
+
};
|
|
91
|
+
Error.captureStackTrace(error);
|
|
92
|
+
return this.respond(request, response, {
|
|
93
|
+
success: false,
|
|
94
|
+
error
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
if (!this.cors(request, response))
|
|
98
|
+
return;
|
|
99
|
+
let body = Object.create(null);
|
|
100
|
+
try {
|
|
101
|
+
body = await (0, utils_1.parseHTTPBody)(request);
|
|
102
|
+
if (!body || 'object' !== typeof body)
|
|
103
|
+
throw new Error('Content Invalid');
|
|
104
|
+
}
|
|
105
|
+
catch (error) {
|
|
106
|
+
return this.respond(request, response, {
|
|
107
|
+
success: false,
|
|
108
|
+
error: {
|
|
109
|
+
message: error.message,
|
|
110
|
+
stack: error.stack
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
// global unique id
|
|
115
|
+
const traceId = String(request.headers['x-trace-id'] || '');
|
|
116
|
+
// service name, required
|
|
117
|
+
const caller = String(request.headers['x-caller'] || 'anonymous');
|
|
118
|
+
// client ip or caller service ip
|
|
119
|
+
const ip = String(request.headers['x-ip'] || request.socket.remoteAddress || '');
|
|
120
|
+
// startup client ip
|
|
121
|
+
const sourceIP = String(request.headers['x-real-ip'] || '');
|
|
122
|
+
const { action, params = [] } = body;
|
|
123
|
+
const context = oox.genContext({ traceId, caller, sourceIP, ip, callerId: '' });
|
|
124
|
+
const format = await oox.call(action, params, context);
|
|
125
|
+
this.respond(request, response, format);
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* HTTP Response Catch
|
|
129
|
+
*/
|
|
130
|
+
respond(request, response, format) {
|
|
131
|
+
let formatString = '';
|
|
132
|
+
try {
|
|
133
|
+
formatString = JSON.stringify(format);
|
|
134
|
+
}
|
|
135
|
+
catch ({ message, stack }) {
|
|
136
|
+
delete format.body;
|
|
137
|
+
format.success = false;
|
|
138
|
+
format.error = {
|
|
139
|
+
message,
|
|
140
|
+
stack
|
|
141
|
+
};
|
|
142
|
+
formatString = JSON.stringify(format);
|
|
143
|
+
}
|
|
144
|
+
response.setHeader('Content-Type', 'application/json');
|
|
145
|
+
response.setHeader('Content-Length', Buffer.byteLength(formatString));
|
|
146
|
+
response.end(formatString);
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* HTTP RPC
|
|
150
|
+
*/
|
|
151
|
+
async rpc(url, action, params, context) {
|
|
152
|
+
if (!context || !context.traceId) {
|
|
153
|
+
context = oox.getContext();
|
|
154
|
+
}
|
|
155
|
+
const { traceId, caller, sourceIP } = context;
|
|
156
|
+
const headers = {
|
|
157
|
+
'Content-Type': 'application/json',
|
|
158
|
+
'x-trace-id': String(traceId),
|
|
159
|
+
};
|
|
160
|
+
if (caller)
|
|
161
|
+
headers['x-caller'] = String(caller);
|
|
162
|
+
if (sourceIP)
|
|
163
|
+
headers['x-real-ip'] = sourceIP;
|
|
164
|
+
// headers [ 'x-ip' ] = getIPAddress ( 4 ) [ 0 ]
|
|
165
|
+
const format = await (0, utils_1.httpRequest)(url, {
|
|
166
|
+
headers
|
|
167
|
+
}, JSON.stringify({ action, params }));
|
|
168
|
+
if ('string' === typeof format)
|
|
169
|
+
throw new Error(format);
|
|
170
|
+
const { error, body } = format;
|
|
171
|
+
if (error) {
|
|
172
|
+
const asyncError = new Error(error.message);
|
|
173
|
+
throw asyncError;
|
|
174
|
+
}
|
|
175
|
+
else {
|
|
176
|
+
return body;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
exports.default = HTTPModule;
|