manner.js 0.0.1 → 0.0.3
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/README.md +1 -1
- package/package.json +1 -1
- package/packages/client/dist/32.index.js +1 -0
- package/packages/client/dist/index.js +2 -0
- package/packages/client/dist/index.js.LICENSE.txt +9 -0
- package/packages/client/postcss.config.js +6 -0
- package/packages/client/src/class/Dimmer.js +19 -0
- package/packages/client/src/class/Emitter.js +36 -0
- package/packages/client/src/component/Router/index.js +88 -0
- package/packages/client/src/component/Router/index.module.css +2 -0
- package/packages/client/src/component/UpdateConfirm/index.js +61 -0
- package/packages/client/src/component/UpdateConfirm/index.module.css +40 -0
- package/packages/client/src/component/WebApp/index.js +42 -0
- package/packages/client/src/index.js +6 -0
- package/packages/client/src/lib/checkUpdate.js +9 -0
- package/packages/client/src/lib/clearCookie.js +16 -0
- package/packages/client/src/lib/filterNamespace.js +9 -0
- package/packages/client/src/lib/formateLocation.js +8 -0
- package/packages/client/src/lib/readCookie.js +32 -0
- package/packages/client/src/lib/setCookie.js +19 -0
- package/packages/client/src/lib/template/locationTemplate/index.js +14 -0
- package/packages/client/src/lib/template/locationTemplate/index.module.css +4 -0
- package/packages/client/src/obj/location.js +3 -0
- package/packages/client/src/page/NotFound/index.js +21 -0
- package/packages/client/src/page/NotFound/index.module.css +28 -0
- package/packages/client/webpack.config.dev.js +62 -0
- package/packages/client/webpack.config.pro.js +47 -0
- package/packages/server/dist/class/CommonHttp.js +70 -0
- package/packages/server/dist/index.js +12 -0
- package/packages/server/dist/lib/cacheOutput.js +41 -0
- package/packages/server/dist/lib/compressOutput.js +44 -0
- package/packages/server/dist/lib/filterNamespace.js +17 -0
- package/packages/server/dist/lib/formateHttpDate.js +11 -0
- package/packages/server/dist/lib/formateHttpKey.js +11 -0
- package/packages/server/dist/lib/parseHttpDate.js +9 -0
- package/packages/server/dist/lib/parseOption.js +70 -0
- package/packages/server/dist/lib/readCookie.js +27 -0
- package/packages/server/src/class/CommonHttp.js +64 -0
- package/packages/server/src/index.js +2 -0
- package/packages/server/src/lib/cacheOutput.js +36 -0
- package/packages/server/src/lib/compressOutput.js +40 -0
- package/packages/server/src/lib/filterNamespace.js +11 -0
- package/packages/server/src/lib/formateHttpDate.js +5 -0
- package/packages/server/src/lib/formateHttpKey.js +5 -0
- package/packages/server/src/lib/parseHttpDate.js +3 -0
- package/packages/server/src/lib/parseOption.js +66 -0
- package/packages/server/src/lib/readCookie.js +21 -0
- package/packages/server/test/CommonHttp.test.js +18 -0
- package/packages/server/test/parseOption.test.js +9 -0
- /package/packages/{mode-client → client}/package.json +0 -0
- /package/packages/{mode-server → server}/package.json +0 -0
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
const path = require('path');
|
|
2
|
+
const HtmlWebpackPlugin = require('html-webpack-plugin');
|
|
3
|
+
|
|
4
|
+
module.exports = {
|
|
5
|
+
mode: 'development',
|
|
6
|
+
entry: {
|
|
7
|
+
main: './src/index.js',
|
|
8
|
+
},
|
|
9
|
+
output: {
|
|
10
|
+
filename: '[name].bundle.js',
|
|
11
|
+
path: path.resolve(__dirname, 'static'),
|
|
12
|
+
},
|
|
13
|
+
plugins: [
|
|
14
|
+
new HtmlWebpackPlugin({
|
|
15
|
+
minify: true,
|
|
16
|
+
template: './src/html/index.html',
|
|
17
|
+
}),
|
|
18
|
+
],
|
|
19
|
+
devServer: {
|
|
20
|
+
compress: true,
|
|
21
|
+
port: 9000,
|
|
22
|
+
proxy: {
|
|
23
|
+
'/api': 'http://localhost:3005'
|
|
24
|
+
},
|
|
25
|
+
},
|
|
26
|
+
module: {
|
|
27
|
+
rules: [
|
|
28
|
+
{
|
|
29
|
+
test: /\.js$/,
|
|
30
|
+
exclude: [
|
|
31
|
+
/node_modules/,
|
|
32
|
+
],
|
|
33
|
+
use: {
|
|
34
|
+
loader: 'babel-loader',
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
test: /\.css$/i,
|
|
39
|
+
use: [
|
|
40
|
+
'style-loader',
|
|
41
|
+
{
|
|
42
|
+
loader: 'css-loader',
|
|
43
|
+
options: {
|
|
44
|
+
modules: {
|
|
45
|
+
auto: /\.module\.css$/
|
|
46
|
+
},
|
|
47
|
+
},
|
|
48
|
+
},
|
|
49
|
+
'postcss-loader',
|
|
50
|
+
]
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
test: /\.(png|jpe?g|gif|svg)$/,
|
|
54
|
+
use: [
|
|
55
|
+
{
|
|
56
|
+
loader: 'file-loader',
|
|
57
|
+
}
|
|
58
|
+
]
|
|
59
|
+
},
|
|
60
|
+
],
|
|
61
|
+
},
|
|
62
|
+
};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
const path = require('path');
|
|
2
|
+
const HtmlWebpackPlugin = require('html-webpack-plugin');
|
|
3
|
+
|
|
4
|
+
module.exports = {
|
|
5
|
+
mode: 'production',
|
|
6
|
+
entry: {
|
|
7
|
+
main: './src/index.js',
|
|
8
|
+
},
|
|
9
|
+
output: {
|
|
10
|
+
filename: 'index.js',
|
|
11
|
+
path: path.resolve(__dirname, 'dist'),
|
|
12
|
+
},
|
|
13
|
+
module: {
|
|
14
|
+
rules: [
|
|
15
|
+
{
|
|
16
|
+
test: /\.js$/,
|
|
17
|
+
exclude: /node_modules/,
|
|
18
|
+
use: {
|
|
19
|
+
loader: 'babel-loader',
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
test: /\.css$/i,
|
|
24
|
+
use: [
|
|
25
|
+
'style-loader',
|
|
26
|
+
{
|
|
27
|
+
loader: 'css-loader',
|
|
28
|
+
options: {
|
|
29
|
+
modules: {
|
|
30
|
+
auto: /\.module\.css$/
|
|
31
|
+
},
|
|
32
|
+
},
|
|
33
|
+
},
|
|
34
|
+
'postcss-loader',
|
|
35
|
+
]
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
test: /\.(png|jpe?g|gif|svg)$/,
|
|
39
|
+
use: [
|
|
40
|
+
{
|
|
41
|
+
loader: 'file-loader',
|
|
42
|
+
}
|
|
43
|
+
]
|
|
44
|
+
},
|
|
45
|
+
],
|
|
46
|
+
},
|
|
47
|
+
};
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
var _cacheOutput = _interopRequireDefault(require("../lib/cacheOutput"));
|
|
8
|
+
var _formateHttpKey = _interopRequireDefault(require("../lib/formateHttpKey"));
|
|
9
|
+
var _filterNamespace = _interopRequireDefault(require("../lib/filterNamespace"));
|
|
10
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
11
|
+
function getLists(list) {
|
|
12
|
+
return list.join('|');
|
|
13
|
+
}
|
|
14
|
+
class CommonHttp {
|
|
15
|
+
constructor(options) {
|
|
16
|
+
this.time = new Date().getTime();
|
|
17
|
+
const {
|
|
18
|
+
fonts
|
|
19
|
+
} = options;
|
|
20
|
+
if (options.fonts === undefined) {
|
|
21
|
+
options.fonts = [];
|
|
22
|
+
}
|
|
23
|
+
this.options = options;
|
|
24
|
+
this.regexp = new RegExp(`\.(${getLists(options.fonts.concat(['html, ico', 'js']))})$`);
|
|
25
|
+
}
|
|
26
|
+
async process(req, res) {
|
|
27
|
+
try {
|
|
28
|
+
const {
|
|
29
|
+
url
|
|
30
|
+
} = req;
|
|
31
|
+
if (url === '/update/time') {
|
|
32
|
+
const {
|
|
33
|
+
time
|
|
34
|
+
} = this;
|
|
35
|
+
res.end(time);
|
|
36
|
+
} else if (this.regexp.test(url)) {
|
|
37
|
+
(0, _cacheOutput.default)(req, res, restPath, fs.readFileSync(path.resolve('static', restPath)), parseInt(fs.statSync(path.resolve('static', restPath)).mtimeMs));
|
|
38
|
+
} else if (url.substring(0, 4) === '/api') {
|
|
39
|
+
const body = await new Promise((resolve, reject) => {
|
|
40
|
+
req.on('data', data => {
|
|
41
|
+
resolve(data.toString());
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
const {
|
|
45
|
+
location
|
|
46
|
+
} = this.options;
|
|
47
|
+
const response = await fetch(location + url, {
|
|
48
|
+
method: 'POST',
|
|
49
|
+
body
|
|
50
|
+
});
|
|
51
|
+
for (const k of response.headers.keys()) {
|
|
52
|
+
res.setHeader((0, _formateHttpKey.default)(k), response.headers.get(k));
|
|
53
|
+
}
|
|
54
|
+
const data = await response.text();
|
|
55
|
+
res.end(JSON.stringify(data));
|
|
56
|
+
}
|
|
57
|
+
} catch (e) {
|
|
58
|
+
const {
|
|
59
|
+
develope
|
|
60
|
+
} = this.options;
|
|
61
|
+
if (develope === true) {
|
|
62
|
+
throw e;
|
|
63
|
+
} else {
|
|
64
|
+
res.writeHead(500);
|
|
65
|
+
res.end();
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
var _default = exports.default = CommonHttp;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.parseOption = exports.CommonHttp = void 0;
|
|
7
|
+
var _CommonHttp = _interopRequireWildcard(require("./class/CommonHttp"));
|
|
8
|
+
exports.CommonHttp = _CommonHttp;
|
|
9
|
+
var _parseOption = _interopRequireWildcard(require("./lib/parseOption"));
|
|
10
|
+
exports.parseOption = _parseOption;
|
|
11
|
+
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
|
|
12
|
+
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = cacheOutput;
|
|
7
|
+
var _compressOutput = _interopRequireDefault(require("./compressOutput"));
|
|
8
|
+
var _formateHttpDate = _interopRequireDefault(require("./formateHttpDate"));
|
|
9
|
+
var _parseHttpDate = _interopRequireDefault(require("./parseHttpDate"));
|
|
10
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
11
|
+
const output = {
|
|
12
|
+
modify: {},
|
|
13
|
+
file: {}
|
|
14
|
+
};
|
|
15
|
+
function cacheOutput(req, res, path, file, ms) {
|
|
16
|
+
let ifModifiedSince = req.headers['If-Modified-Since'];
|
|
17
|
+
if (ifModifiedSince === undefined) {
|
|
18
|
+
if (output.modify[path] === undefined) {
|
|
19
|
+
output.modify[path] = new Date(ms).toString();
|
|
20
|
+
}
|
|
21
|
+
if (output.file[path] === undefined) {
|
|
22
|
+
output.file[path] = file;
|
|
23
|
+
}
|
|
24
|
+
res.setHeader('Last-Modified', (0, _formateHttpDate.default)(output.modify[path]));
|
|
25
|
+
(0, _compressOutput.default)(req, res, output.file[path], path);
|
|
26
|
+
} else {
|
|
27
|
+
if (output.modify[path] === undefined) {
|
|
28
|
+
output.modify[path] = new Date(ms).toString();
|
|
29
|
+
}
|
|
30
|
+
if (parseDateString(new Date().toString()) < parseDateString(output.modify[path])) {
|
|
31
|
+
if (output.file[path] === undefined) {
|
|
32
|
+
output.file[path] = file;
|
|
33
|
+
}
|
|
34
|
+
res.setHeader('Last-Modified', (0, _formateHttpDate.default)(output.modify[path]));
|
|
35
|
+
(0, _compressOutput.default)(req, res, output.file[path], path);
|
|
36
|
+
} else {
|
|
37
|
+
res.writeHead(304);
|
|
38
|
+
res.end();
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = compressOutput;
|
|
7
|
+
var _zlib = _interopRequireDefault(require("zlib"));
|
|
8
|
+
var _stream = require("stream");
|
|
9
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
10
|
+
const output = {
|
|
11
|
+
compress: {},
|
|
12
|
+
raw: {}
|
|
13
|
+
};
|
|
14
|
+
function onError(err) {
|
|
15
|
+
if (err) {
|
|
16
|
+
console.error('An error occurred:', err);
|
|
17
|
+
process.exitCode = 1;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
function dealCompress(data, path, res) {
|
|
21
|
+
if (cache.compress[path] === undefined) {
|
|
22
|
+
output.compress[path] = data;
|
|
23
|
+
}
|
|
24
|
+
res.end(output.compress[path]);
|
|
25
|
+
}
|
|
26
|
+
function directDeal(data, path, res) {
|
|
27
|
+
if (output.raw[path] === undefined) {
|
|
28
|
+
output.compress[path] = data;
|
|
29
|
+
}
|
|
30
|
+
res.end(output.raw[path]);
|
|
31
|
+
}
|
|
32
|
+
function compressOutput(req, res, buffer, path) {
|
|
33
|
+
res.setHeader('Vary', 'Accept-Encoding');
|
|
34
|
+
let acceptEncoding = req.headers['accept-encoding'];
|
|
35
|
+
if (/gzip/.test(acceptEncoding)) {
|
|
36
|
+
res.writeHead(200, {
|
|
37
|
+
'Content-Encoding': 'gzip'
|
|
38
|
+
});
|
|
39
|
+
dealCompress(_zlib.default.gzipSync(buffer), path, res);
|
|
40
|
+
} else {
|
|
41
|
+
res.writeHead(200, {});
|
|
42
|
+
dealDirect(buffer, path, res);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = filterNamespace;
|
|
7
|
+
function filterNamespace(namespace) {
|
|
8
|
+
const ans = {};
|
|
9
|
+
if (typeof namespace === 'object') {
|
|
10
|
+
Object.keys(namespace).forEach(k => {
|
|
11
|
+
if (k !== 'expires') {
|
|
12
|
+
ans[k] = namespace[k];
|
|
13
|
+
}
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
return ans;
|
|
17
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = formateHttpDate;
|
|
7
|
+
function formateHttpDate(date) {
|
|
8
|
+
let [week, month, day, year, time, zone] = date.toString().split(' ');
|
|
9
|
+
zone = zone.split('+')[0];
|
|
10
|
+
return month + ', ' + day + ' ' + month + ' ' + year + ' ' + time + ' ' + zone;
|
|
11
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = formateHttpKey;
|
|
7
|
+
function formateHttpKey(key) {
|
|
8
|
+
return key.split('-').map(v => {
|
|
9
|
+
return v.substring(0, 1).toUpperCase() + v.substring(1, v.length);
|
|
10
|
+
}).join('-');
|
|
11
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = parseOption;
|
|
7
|
+
function isOption(string) {
|
|
8
|
+
let ans = true;
|
|
9
|
+
if (typeof string === 'string') {
|
|
10
|
+
let i = 0;
|
|
11
|
+
for (i = 0; i < 1; i += 1) {
|
|
12
|
+
if (string.charAt(i) === '-') {
|
|
13
|
+
break;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
if (i === 0) {
|
|
17
|
+
ans = false;
|
|
18
|
+
}
|
|
19
|
+
} else {
|
|
20
|
+
ans = false;
|
|
21
|
+
}
|
|
22
|
+
return ans;
|
|
23
|
+
}
|
|
24
|
+
function transformOption(value) {
|
|
25
|
+
let ans;
|
|
26
|
+
const s = value.split('-');
|
|
27
|
+
if (s.length >= 1) {
|
|
28
|
+
ans = s.map((e, i) => {
|
|
29
|
+
if (i === 0) {
|
|
30
|
+
return e;
|
|
31
|
+
} else {
|
|
32
|
+
return e.charAt(0).toUpperCase() + e.substring(1, e.length);
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
} else {
|
|
36
|
+
ans = value;
|
|
37
|
+
}
|
|
38
|
+
return ans.join('');
|
|
39
|
+
}
|
|
40
|
+
function parseOption(...params) {
|
|
41
|
+
const ans = {};
|
|
42
|
+
for (let i = 0; i < params.length; i += 1) {
|
|
43
|
+
const param = params[i];
|
|
44
|
+
if (param.charAt(0) === '-') {
|
|
45
|
+
const regexp = /^\-([a-z])$/;
|
|
46
|
+
if (regexp.test(param)) {
|
|
47
|
+
const [_, k] = param.match(regexp);
|
|
48
|
+
if (isOption(params[i + 1])) {
|
|
49
|
+
ans[k] = params[i + 1];
|
|
50
|
+
i += 1;
|
|
51
|
+
} else {
|
|
52
|
+
ans[k] = true;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
if (param.charAt(1) === '-') {
|
|
56
|
+
const regexp = /^\-\-([a-z\-]+)$/;
|
|
57
|
+
if (regexp.test(param)) {
|
|
58
|
+
const [_, k] = param.match(regexp);
|
|
59
|
+
if (isOption(params[i + 1])) {
|
|
60
|
+
ans[transformOption(k)] = params[i + 1];
|
|
61
|
+
i += 1;
|
|
62
|
+
} else {
|
|
63
|
+
ans[k] = true;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return ans;
|
|
70
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = readCookie;
|
|
7
|
+
function readCookie(cookie) {
|
|
8
|
+
const cookies = {};
|
|
9
|
+
if (typeof cookie === 'string') {
|
|
10
|
+
cookie.split(';').forEach(i => {
|
|
11
|
+
const [key, value] = i.split('=');
|
|
12
|
+
cookies[key.trim()] = value;
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
const namespaces = {};
|
|
16
|
+
Object.keys(cookies).forEach(k => {
|
|
17
|
+
const result = k.split('_');
|
|
18
|
+
if (result.length === 2) {
|
|
19
|
+
const [namespace, key] = result;
|
|
20
|
+
if (namespaces[namespace] === undefined) {
|
|
21
|
+
namespaces[namespace] = {};
|
|
22
|
+
}
|
|
23
|
+
namespaces[namespace][key] = cookies[k];
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
return namespaces;
|
|
27
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import cacheOutput from '~/lib/cacheOutput';
|
|
2
|
+
import formateHttpKey from '~/lib/formateHttpKey';
|
|
3
|
+
import filterNamespace from '~/lib/filterNamespace';
|
|
4
|
+
|
|
5
|
+
function getLists(list) {
|
|
6
|
+
return list.join('|');
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
class CommonHttp {
|
|
10
|
+
constructor(options) {
|
|
11
|
+
this.time = new Date().getTime();
|
|
12
|
+
const { fonts, } = options;
|
|
13
|
+
if (options.fonts === undefined) {
|
|
14
|
+
options.fonts = [];
|
|
15
|
+
}
|
|
16
|
+
this.options = options;
|
|
17
|
+
this.regexp = new RegExp(`\.(${getLists(options.fonts.concat(['html, ico', 'js']))})$`);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
async process(req, res) {
|
|
21
|
+
try {
|
|
22
|
+
const { url, } = req;
|
|
23
|
+
if (url === '/update/time') {
|
|
24
|
+
const { time, } = this;
|
|
25
|
+
res.end(time);
|
|
26
|
+
} else if (this.regexp.test(url)) {
|
|
27
|
+
cacheOutput(req, res, restPath,
|
|
28
|
+
fs.readFileSync(path.resolve('static', restPath)),
|
|
29
|
+
parseInt(fs.statSync(path.resolve('static', restPath)).mtimeMs),
|
|
30
|
+
);
|
|
31
|
+
} else if (url.substring(0, 4) === '/api') {
|
|
32
|
+
const body = await new Promise((resolve, reject) => {
|
|
33
|
+
req.on('data', (data) => {
|
|
34
|
+
resolve(data.toString());
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
const {
|
|
38
|
+
location,
|
|
39
|
+
} = this.options;
|
|
40
|
+
const response = await fetch(location + url, {
|
|
41
|
+
method: 'POST',
|
|
42
|
+
body,
|
|
43
|
+
});
|
|
44
|
+
for (const k of response.headers.keys()) {
|
|
45
|
+
res.setHeader(formateHttpKey(k), response.headers.get(k));
|
|
46
|
+
}
|
|
47
|
+
const data = await response.text();
|
|
48
|
+
res.end(JSON.stringify(data));
|
|
49
|
+
}
|
|
50
|
+
} catch (e) {
|
|
51
|
+
const {
|
|
52
|
+
develope,
|
|
53
|
+
} = this.options;
|
|
54
|
+
if (develope === true) {
|
|
55
|
+
throw e;
|
|
56
|
+
} else {
|
|
57
|
+
res.writeHead(500);
|
|
58
|
+
res.end();
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export default CommonHttp;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import compressOutput from '~/lib/compressOutput';
|
|
2
|
+
import formateHttpDate from '~/lib/formateHttpDate';
|
|
3
|
+
import parseHttpDate from '~/lib/parseHttpDate';
|
|
4
|
+
|
|
5
|
+
const output = {
|
|
6
|
+
modify: {},
|
|
7
|
+
file: {},
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export default function cacheOutput(req, res, path, file, ms) {
|
|
11
|
+
let ifModifiedSince = req.headers['If-Modified-Since'];
|
|
12
|
+
if (ifModifiedSince === undefined) {
|
|
13
|
+
if (output.modify[path] === undefined) {
|
|
14
|
+
output.modify[path] = new Date(ms).toString();
|
|
15
|
+
}
|
|
16
|
+
if (output.file[path] === undefined) {
|
|
17
|
+
output.file[path] = file;
|
|
18
|
+
}
|
|
19
|
+
res.setHeader('Last-Modified', formateHttpDate(output.modify[path]));
|
|
20
|
+
compressOutput(req, res, output.file[path], path);
|
|
21
|
+
} else {
|
|
22
|
+
if (output.modify[path] === undefined) {
|
|
23
|
+
output.modify[path] = new Date(ms).toString();
|
|
24
|
+
}
|
|
25
|
+
if (parseDateString(new Date().toString()) < parseDateString(output.modify[path])) {
|
|
26
|
+
if (output.file[path] === undefined) {
|
|
27
|
+
output.file[path] = file;
|
|
28
|
+
}
|
|
29
|
+
res.setHeader('Last-Modified', formateHttpDate(output.modify[path]));
|
|
30
|
+
compressOutput(req, res, output.file[path], path);
|
|
31
|
+
} else {
|
|
32
|
+
res.writeHead(304);
|
|
33
|
+
res.end();
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import zlib from 'zlib';
|
|
2
|
+
import { pipeline, } from 'stream';
|
|
3
|
+
|
|
4
|
+
const output = {
|
|
5
|
+
compress: {},
|
|
6
|
+
raw: {},
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
function onError(err) {
|
|
10
|
+
if (err) {
|
|
11
|
+
console.error('An error occurred:', err);
|
|
12
|
+
process.exitCode = 1;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function dealCompress(data, path, res) {
|
|
17
|
+
if (cache.compress[path] === undefined) {
|
|
18
|
+
output.compress[path] = data;
|
|
19
|
+
}
|
|
20
|
+
res.end(output.compress[path]);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function directDeal(data, path, res) {
|
|
24
|
+
if (output.raw[path] === undefined) {
|
|
25
|
+
output.compress[path] = data;
|
|
26
|
+
}
|
|
27
|
+
res.end(output.raw[path]);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export default function compressOutput(req, res, buffer, path) {
|
|
31
|
+
res.setHeader('Vary', 'Accept-Encoding');
|
|
32
|
+
let acceptEncoding = req.headers['accept-encoding'];
|
|
33
|
+
if (/gzip/.test(acceptEncoding)) {
|
|
34
|
+
res.writeHead(200, { 'Content-Encoding': 'gzip' });
|
|
35
|
+
dealCompress(zlib.gzipSync(buffer), path, res);
|
|
36
|
+
} else {
|
|
37
|
+
res.writeHead(200, {});
|
|
38
|
+
dealDirect(buffer, path, res);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
function isOption(string) {
|
|
2
|
+
let ans = true;
|
|
3
|
+
if (typeof string === 'string') {
|
|
4
|
+
let i = 0;
|
|
5
|
+
for (i = 0; i < 1; i += 1) {
|
|
6
|
+
if (string.charAt(i) === '-') {
|
|
7
|
+
break;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
if (i === 0) {
|
|
11
|
+
ans = false;
|
|
12
|
+
}
|
|
13
|
+
} else {
|
|
14
|
+
ans = false;
|
|
15
|
+
}
|
|
16
|
+
return ans;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function transformOption(value) {
|
|
20
|
+
let ans;
|
|
21
|
+
const s = value.split('-');
|
|
22
|
+
if (s.length >= 1) {
|
|
23
|
+
ans = s.map((e, i) => {
|
|
24
|
+
if (i === 0) {
|
|
25
|
+
return e;
|
|
26
|
+
} else {
|
|
27
|
+
return e.charAt(0).toUpperCase() + e.substring(1, e.length);
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
} else {
|
|
31
|
+
ans = value;
|
|
32
|
+
}
|
|
33
|
+
return ans.join('');
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export default function parseOption(...params) {
|
|
37
|
+
const ans = {};
|
|
38
|
+
for (let i = 0; i < params.length; i += 1) {
|
|
39
|
+
const param = params[i];
|
|
40
|
+
if (param.charAt(0) === '-') {
|
|
41
|
+
const regexp = /^\-([a-z])$/;
|
|
42
|
+
if (regexp.test(param)) {
|
|
43
|
+
const [_, k] = param.match(regexp);
|
|
44
|
+
if (isOption(params[i+1])) {
|
|
45
|
+
ans[k] = params[i+1];
|
|
46
|
+
i += 1;
|
|
47
|
+
} else {
|
|
48
|
+
ans[k] = true;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
if (param.charAt(1) === '-') {
|
|
52
|
+
const regexp = /^\-\-([a-z\-]+)$/;
|
|
53
|
+
if (regexp.test(param)) {
|
|
54
|
+
const [_, k] = param.match(regexp);
|
|
55
|
+
if (isOption(params[i+1])) {
|
|
56
|
+
ans[transformOption(k)] = params[i+1];
|
|
57
|
+
i += 1;
|
|
58
|
+
} else {
|
|
59
|
+
ans[k] = true;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return ans;
|
|
66
|
+
}
|