oox 0.3.0-beta1 → 0.3.0-beta2
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 -54
- package/bin/starter.js +81 -77
- package/index.js +144 -149
- package/index.mjs +3 -3
- package/modules/http/index.js +180 -180
- package/modules/http/utils.js +73 -73
- package/modules/index.js +68 -88
- package/modules/module.js +16 -16
- package/package.json +4 -4
- 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 +21 -23
- package/types/modules/module.d.ts +13 -11
- package/types/utils.d.ts +6 -6
- package/utils.js +63 -63
- package/modules/socketio/client.js +0 -101
- package/modules/socketio/index.js +0 -148
- package/modules/socketio/server.js +0 -130
- package/modules/socketio/socket.js +0 -4
- package/types/modules/socketio/client.d.ts +0 -23
- package/types/modules/socketio/index.d.ts +0 -35
- package/types/modules/socketio/server.d.ts +0 -35
- package/types/modules/socketio/socket.d.ts +0 -11
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 = 'oox: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;
|
|
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 = 'oox: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;
|
package/modules/http/utils.js
CHANGED
|
@@ -1,73 +1,73 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.httpRequest = exports.parseHTTPBody = exports.stream2buffer = void 0;
|
|
4
|
-
const http = require("node:http");
|
|
5
|
-
/**
|
|
6
|
-
* Stream => Buffer
|
|
7
|
-
*/
|
|
8
|
-
function stream2buffer(stream, totalLength = 0) {
|
|
9
|
-
return new Promise(function (resolve, reject) {
|
|
10
|
-
let buffers = [];
|
|
11
|
-
stream.on('error', reject);
|
|
12
|
-
if (totalLength) {
|
|
13
|
-
stream.on('data', function (data) { buffers.push(data); });
|
|
14
|
-
}
|
|
15
|
-
else {
|
|
16
|
-
stream.on('data', function (data) {
|
|
17
|
-
buffers.push(data);
|
|
18
|
-
totalLength += data.length;
|
|
19
|
-
});
|
|
20
|
-
}
|
|
21
|
-
stream.on('end', function () { resolve(Buffer.concat(buffers, totalLength)); });
|
|
22
|
-
});
|
|
23
|
-
}
|
|
24
|
-
exports.stream2buffer = stream2buffer;
|
|
25
|
-
/**
|
|
26
|
-
* Request => JSONObject
|
|
27
|
-
*/
|
|
28
|
-
async function parseHTTPBody(request) {
|
|
29
|
-
if (request.method === 'GET')
|
|
30
|
-
return null;
|
|
31
|
-
let contentType = request.headers['content-type'];
|
|
32
|
-
// application/json; charset=utf-8
|
|
33
|
-
if (contentType)
|
|
34
|
-
contentType = contentType.split(';')[0].trim();
|
|
35
|
-
const contentSize = request.headers['content-length'];
|
|
36
|
-
const buffer = await stream2buffer(request, +contentSize || 0);
|
|
37
|
-
if (contentSize && buffer.length !== +contentSize)
|
|
38
|
-
throw new Error('Content-Length Incorrect');
|
|
39
|
-
const bodyString = buffer.toString();
|
|
40
|
-
if ('application/json' === contentType) {
|
|
41
|
-
return JSON.parse(bodyString);
|
|
42
|
-
}
|
|
43
|
-
else {
|
|
44
|
-
return bodyString;
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
exports.parseHTTPBody = parseHTTPBody;
|
|
48
|
-
/**
|
|
49
|
-
* http request
|
|
50
|
-
*/
|
|
51
|
-
function httpRequest(url, options, body) {
|
|
52
|
-
return new Promise(function (resolve, reject) {
|
|
53
|
-
const request = http.request(url, options, async function (response) {
|
|
54
|
-
try {
|
|
55
|
-
const result = await parseHTTPBody(response);
|
|
56
|
-
resolve(result);
|
|
57
|
-
}
|
|
58
|
-
catch (error) {
|
|
59
|
-
const decoration = new Error(`${response.statusCode} - ${error.message}`);
|
|
60
|
-
reject(decoration);
|
|
61
|
-
}
|
|
62
|
-
});
|
|
63
|
-
request.on('error', reject);
|
|
64
|
-
if (body) {
|
|
65
|
-
request.method = 'POST';
|
|
66
|
-
request.setHeader('Content-Type', 'application/json');
|
|
67
|
-
request.setHeader('Content-Length', Buffer.byteLength(body));
|
|
68
|
-
request.write(body);
|
|
69
|
-
}
|
|
70
|
-
request.end();
|
|
71
|
-
});
|
|
72
|
-
}
|
|
73
|
-
exports.httpRequest = httpRequest;
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.httpRequest = exports.parseHTTPBody = exports.stream2buffer = void 0;
|
|
4
|
+
const http = require("node:http");
|
|
5
|
+
/**
|
|
6
|
+
* Stream => Buffer
|
|
7
|
+
*/
|
|
8
|
+
function stream2buffer(stream, totalLength = 0) {
|
|
9
|
+
return new Promise(function (resolve, reject) {
|
|
10
|
+
let buffers = [];
|
|
11
|
+
stream.on('error', reject);
|
|
12
|
+
if (totalLength) {
|
|
13
|
+
stream.on('data', function (data) { buffers.push(data); });
|
|
14
|
+
}
|
|
15
|
+
else {
|
|
16
|
+
stream.on('data', function (data) {
|
|
17
|
+
buffers.push(data);
|
|
18
|
+
totalLength += data.length;
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
stream.on('end', function () { resolve(Buffer.concat(buffers, totalLength)); });
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
exports.stream2buffer = stream2buffer;
|
|
25
|
+
/**
|
|
26
|
+
* Request => JSONObject
|
|
27
|
+
*/
|
|
28
|
+
async function parseHTTPBody(request) {
|
|
29
|
+
if (request.method === 'GET')
|
|
30
|
+
return null;
|
|
31
|
+
let contentType = request.headers['content-type'];
|
|
32
|
+
// application/json; charset=utf-8
|
|
33
|
+
if (contentType)
|
|
34
|
+
contentType = contentType.split(';')[0].trim();
|
|
35
|
+
const contentSize = request.headers['content-length'];
|
|
36
|
+
const buffer = await stream2buffer(request, +contentSize || 0);
|
|
37
|
+
if (contentSize && buffer.length !== +contentSize)
|
|
38
|
+
throw new Error('Content-Length Incorrect');
|
|
39
|
+
const bodyString = buffer.toString();
|
|
40
|
+
if ('application/json' === contentType) {
|
|
41
|
+
return JSON.parse(bodyString);
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
return bodyString;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
exports.parseHTTPBody = parseHTTPBody;
|
|
48
|
+
/**
|
|
49
|
+
* http request
|
|
50
|
+
*/
|
|
51
|
+
function httpRequest(url, options, body) {
|
|
52
|
+
return new Promise(function (resolve, reject) {
|
|
53
|
+
const request = http.request(url, options, async function (response) {
|
|
54
|
+
try {
|
|
55
|
+
const result = await parseHTTPBody(response);
|
|
56
|
+
resolve(result);
|
|
57
|
+
}
|
|
58
|
+
catch (error) {
|
|
59
|
+
const decoration = new Error(`${response.statusCode} - ${error.message}`);
|
|
60
|
+
reject(decoration);
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
request.on('error', reject);
|
|
64
|
+
if (body) {
|
|
65
|
+
request.method = 'POST';
|
|
66
|
+
request.setHeader('Content-Type', 'application/json');
|
|
67
|
+
request.setHeader('Content-Length', Buffer.byteLength(body));
|
|
68
|
+
request.write(body);
|
|
69
|
+
}
|
|
70
|
+
request.end();
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
exports.httpRequest = httpRequest;
|
package/modules/index.js
CHANGED
|
@@ -1,88 +1,68 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
this.#
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
const
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
_socketio.server = _http.server;
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
if (!module.getConfig().disabled) {
|
|
73
|
-
await module.serve();
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
catch (error) {
|
|
78
|
-
await this.stop();
|
|
79
|
-
throw error;
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
async stop() {
|
|
83
|
-
for (const module of this.#queue) {
|
|
84
|
-
await module.stop();
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
exports.default = Modules;
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const module_1 = require("./module");
|
|
4
|
+
const http_1 = require("./http");
|
|
5
|
+
class Modules extends module_1.default {
|
|
6
|
+
/**
|
|
7
|
+
* the module unique name
|
|
8
|
+
*/
|
|
9
|
+
name = 'oox:modules';
|
|
10
|
+
/**
|
|
11
|
+
* FIFO queue for modules starting
|
|
12
|
+
*/
|
|
13
|
+
#queue = [];
|
|
14
|
+
/**
|
|
15
|
+
* all modules map
|
|
16
|
+
*/
|
|
17
|
+
#map = new Map();
|
|
18
|
+
/**
|
|
19
|
+
* all builtin modules
|
|
20
|
+
*/
|
|
21
|
+
builtins = {
|
|
22
|
+
http: new http_1.default,
|
|
23
|
+
};
|
|
24
|
+
constructor() {
|
|
25
|
+
super();
|
|
26
|
+
this.add(this.builtins.http);
|
|
27
|
+
}
|
|
28
|
+
add(module) {
|
|
29
|
+
if (!module.name || 'string' !== typeof module.name)
|
|
30
|
+
throw new Error(`Module<${module.name}> has noname`);
|
|
31
|
+
if (this.#map.has(module.name))
|
|
32
|
+
throw new Error(`Module<${module.name}> has exists`);
|
|
33
|
+
this.#map.set(module.name, module);
|
|
34
|
+
this.#queue.push(module);
|
|
35
|
+
return this;
|
|
36
|
+
}
|
|
37
|
+
get(name) {
|
|
38
|
+
return this.#map.get(name);
|
|
39
|
+
}
|
|
40
|
+
async remove(name) {
|
|
41
|
+
const module = this.get(name);
|
|
42
|
+
if (!module)
|
|
43
|
+
return;
|
|
44
|
+
await module.stop();
|
|
45
|
+
const index = this.#queue.indexOf(module);
|
|
46
|
+
this.#queue.splice(index, 1);
|
|
47
|
+
this.#map.delete(name);
|
|
48
|
+
}
|
|
49
|
+
async serve() {
|
|
50
|
+
try {
|
|
51
|
+
for (const module of this.#queue) {
|
|
52
|
+
if (!module.getConfig().disabled) {
|
|
53
|
+
await module.serve();
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
catch (error) {
|
|
58
|
+
await this.stop();
|
|
59
|
+
throw error;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
async stop() {
|
|
63
|
+
for (const module of this.#queue) {
|
|
64
|
+
await module.stop();
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
exports.default = Modules;
|