manner.js 0.0.29 → 0.0.31

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.
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ Object.defineProperty(exports, "clearCookie", {
7
+ enumerable: true,
8
+ get: function () {
9
+ return _setCookie.default;
10
+ }
11
+ });
12
+ Object.defineProperty(exports, "filterNamespace", {
13
+ enumerable: true,
14
+ get: function () {
15
+ return _filename.default;
16
+ }
17
+ });
18
+ Object.defineProperty(exports, "readCookie", {
19
+ enumerable: true,
20
+ get: function () {
21
+ return _setCookie.default;
22
+ }
23
+ });
24
+ Object.defineProperty(exports, "setCookie", {
25
+ enumerable: true,
26
+ get: function () {
27
+ return _setCookie.default;
28
+ }
29
+ });
30
+ var _filename = _interopRequireDefault(require("./lib/filename"));
31
+ var _setCookie = _interopRequireDefault(require("./lib/setCookie"));
32
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = clearCookie;
7
+ function clearCookie(namespaces) {
8
+ Object.keys(namespaces).forEach(n => {
9
+ if (n.expires !== undefined) {
10
+ Object.keys(n).forEach(k => {
11
+ if (n === 'user') {
12
+ document.cookie[n + '_' + k] = '';
13
+ }
14
+ window.localStorage.removeItem(n + '_' + k);
15
+ });
16
+ delete namespaces[n];
17
+ }
18
+ });
19
+ return namespaces;
20
+ }
@@ -0,0 +1,15 @@
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
+ Object.keys(namespace).forEach(k => {
10
+ if (k !== 'expires') {
11
+ ans[k] = namespace[k];
12
+ }
13
+ });
14
+ return ans;
15
+ }
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = readCookie;
7
+ var _clearCookie = _interopRequireDefault(require("./clearCookie"));
8
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
9
+ function readCookie() {
10
+ const cookies = {};
11
+ document.cookie.split(';').forEach(i => {
12
+ const [key, value] = i.split('=');
13
+ cookies[key.trim()] = value;
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
+ for (let i = 0; i < window.localStorage.length; i += 1) {
27
+ const k = window.localStorage.key(i);
28
+ const result = k.split('_');
29
+ if (result.length === 2) {
30
+ const [namespace, key] = result;
31
+ if (namespaces[namespace] === undefined) {
32
+ namespaces[namespace] = {};
33
+ }
34
+ namespaces[namespace][key] = window.localStorage.getItem(k);
35
+ }
36
+ }
37
+ return (0, _clearCookie.default)(namespaces);
38
+ }
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = setCookie;
7
+ function setCookie(response) {
8
+ const cookie = response.headers.get('cookie');
9
+ if (cookie !== null) {
10
+ if (cookie === '') {
11
+ document.cookie.split(';').forEach(c => {
12
+ const [k, v] = c.split('=');
13
+ const [namespace] = k.split('_');
14
+ if (namespace === 'user') {
15
+ document.cookie = k + '=' + v;
16
+ }
17
+ window.localStorage.set(k, v);
18
+ });
19
+ } else {
20
+ cookie.split(';').forEach(c => {
21
+ document.cookie = c;
22
+ });
23
+ }
24
+ }
25
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "manner.js",
3
- "version": "0.0.29",
3
+ "version": "0.0.31",
4
4
  "description": "Frontend mode project.",
5
5
  "repository": "git@github.com:leobrad/mode.git",
6
6
  "author": "Leo Ely",
@@ -0,0 +1,143 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ var _path = _interopRequireDefault(require("path"));
8
+ var _fs = _interopRequireDefault(require("fs"));
9
+ var _zlib = _interopRequireDefault(require("zlib"));
10
+ var _getHtml = _interopRequireDefault(require("../lib/getHtml"));
11
+ var _parseDateString = _interopRequireDefault(require("../lib/parseDateString"));
12
+ var _formateHttpDate = _interopRequireDefault(require("../lib/formateHttpDate"));
13
+ var _formateHttpKey = _interopRequireDefault(require("../lib/formateHttpKey"));
14
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
+ function getLists(list) {
16
+ return list.join('|');
17
+ }
18
+ class CommonHttp {
19
+ constructor(options) {
20
+ this.time = new Date().getTime();
21
+ const {
22
+ fonts
23
+ } = options;
24
+ if (options.fonts === undefined) {
25
+ options.fonts = [];
26
+ }
27
+ this.time = new Date().getTime();
28
+ this.modify = {};
29
+ this.file = {};
30
+ this.raw = {};
31
+ this.compress = {};
32
+ this.options = options;
33
+ this.regexp = new RegExp(`\.(${getLists(options.fonts.concat(['html, ico', 'js']))})$`);
34
+ }
35
+ compressOutput(req, res, buffer, path) {
36
+ res.setHeader('Vary', 'Accept-Encoding');
37
+ let acceptEncoding = req.headers['accept-encoding'];
38
+ if (/gzip/.test(acceptEncoding)) {
39
+ res.writeHead(200, {
40
+ 'Content-Encoding': 'gzip'
41
+ });
42
+ this.dealCompress(_zlib.default.gzipSync(buffer), path, res);
43
+ } else {
44
+ res.writeHead(200, {});
45
+ this.dealDirect(buffer, path, res);
46
+ }
47
+ }
48
+ cacheOutput(req, res, path, file, ms) {
49
+ let ifModifiedSince = req.headers['If-Modified-Since'];
50
+ if (ifModifiedSince === undefined) {
51
+ if (this.modify[path] === undefined) {
52
+ this.modify[path] = new Date(ms).toString();
53
+ }
54
+ if (this.file[path] === undefined) {
55
+ this.file[path] = file;
56
+ }
57
+ res.setHeader('Last-Modified', (0, _formateHttpDate.default)(this.modify[path]));
58
+ this.compressOutput(req, res, this.file[path], path);
59
+ } else {
60
+ if (this.modify[path] === undefined) {
61
+ this.modify[path] = new Date(ms).toString();
62
+ }
63
+ if ((0, _parseDateString.default)(new Date().toString()) < (0, _parseDateString.default)(this.modify[path])) {
64
+ if (this.file[path] === undefined) {
65
+ this.file[path] = file;
66
+ }
67
+ res.setHeader('Last-Modified', (0, _formateHttpDate.default)(this.modify[path]));
68
+ this.compressOutput(req, res, this.file[path], path);
69
+ } else {
70
+ res.writeHead(304);
71
+ res.end();
72
+ }
73
+ }
74
+ }
75
+ dealCompress(data, path, res) {
76
+ if (this.compress[path] === undefined) {
77
+ this.compress[path] = data;
78
+ }
79
+ res.end(this.compress[path]);
80
+ }
81
+ directDeal(data, path, res) {
82
+ if (this.raw[path] === undefined) {
83
+ this.compress[path] = data;
84
+ }
85
+ res.end(this.raw[path]);
86
+ }
87
+ async process(req, res) {
88
+ try {
89
+ const {
90
+ url
91
+ } = req;
92
+ if (url === '/update/time') {
93
+ const {
94
+ time
95
+ } = this;
96
+ res.end(time);
97
+ } else if (this.regexp.test(url)) {
98
+ const restPath = url.substring(1, url.length);
99
+ this.cacheOutput(req, res, restPath, _fs.default.readFileSync(_path.default.resolve('static', restPath)), parseInt(_fs.default.statSync(_path.default.resolve('static', restPath)).mtimeMs));
100
+ } else if (url.substring(0, 4) === '/api') {
101
+ const body = await new Promise((resolve, reject) => {
102
+ req.on('data', data => {
103
+ resolve(data.toString());
104
+ });
105
+ });
106
+ const {
107
+ location
108
+ } = this.options;
109
+ if (location !== undefined) {
110
+ const response = await fetch(location + url, {
111
+ method: 'POST',
112
+ body
113
+ });
114
+ for (const k of response.headers.keys()) {
115
+ res.setHeader((0, _formateHttpKey.default)(k), response.headers.get(k));
116
+ }
117
+ const data = await response.text();
118
+ res.end(JSON.stringify(data));
119
+ }
120
+ } else {
121
+ if (this.html === undefined) {
122
+ const {
123
+ title,
124
+ content
125
+ } = this.options;
126
+ this.html = (0, _getHtml.default)(title, content);
127
+ }
128
+ this.cacheOutput(req, res, '*html', this.html, this.time);
129
+ }
130
+ } catch (e) {
131
+ const {
132
+ develope
133
+ } = this.options;
134
+ if (develope === true) {
135
+ throw e;
136
+ } else {
137
+ res.writeHead(500);
138
+ res.end();
139
+ }
140
+ }
141
+ }
142
+ }
143
+ exports.default = CommonHttp;
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ Object.defineProperty(exports, "CommonHttp", {
7
+ enumerable: true,
8
+ get: function () {
9
+ return _CommonHttp.default;
10
+ }
11
+ });
12
+ Object.defineProperty(exports, "parseOption", {
13
+ enumerable: true,
14
+ get: function () {
15
+ return _parseOption.default;
16
+ }
17
+ });
18
+ Object.defineProperty(exports, "readCookie", {
19
+ enumerable: true,
20
+ get: function () {
21
+ return _readCookie.default;
22
+ }
23
+ });
24
+ var _CommonHttp = _interopRequireDefault(require("./class/CommonHttp"));
25
+ var _parseOption = _interopRequireDefault(require("./lib/parseOption"));
26
+ var _readCookie = _interopRequireDefault(require("./lib/readCookie"));
27
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = cacheOutput;
7
+ var _formateHttpDate = _interopRequireDefault(require("./formateHttpDate"));
8
+ var _parseHttpDate = _interopRequireDefault(require("./parseHttpDate"));
9
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
10
+ function cacheOutput(req, res, path, file, ms) {
11
+ let ifModifiedSince = req.headers['If-Modified-Since'];
12
+ if (ifModifiedSince === undefined) {
13
+ if (this.modify[path] === undefined) {
14
+ this.modify[path] = new Date(ms).toString();
15
+ }
16
+ if (this.file[path] === undefined) {
17
+ this.file[path] = file;
18
+ }
19
+ res.setHeader('Last-Modified', (0, _formateHttpDate.default)(this.modify[path]));
20
+ this.compressOutput(req, res, this.file[path], path);
21
+ } else {
22
+ if (this.modify[path] === undefined) {
23
+ this.modify[path] = new Date(ms).toString();
24
+ }
25
+ if ((0, _parseHttpDate.default)(new Date().toString()) < (0, _parseHttpDate.default)(this.modify[path])) {
26
+ if (this.file[path] === undefined) {
27
+ this.file[path] = file;
28
+ }
29
+ res.setHeader('Last-Modified', (0, _formateHttpDate.default)(this.modify[path]));
30
+ this.compressOutput(req, res, this.file[path], path);
31
+ } else {
32
+ res.writeHead(304);
33
+ res.end();
34
+ }
35
+ }
36
+ }
@@ -0,0 +1,28 @@
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
+ function onError(err) {
11
+ if (err) {
12
+ console.error('An error occurred:', err);
13
+ process.exitCode = 1;
14
+ }
15
+ }
16
+ function compressOutput(req, res, buffer, path) {
17
+ res.setHeader('Vary', 'Accept-Encoding');
18
+ let acceptEncoding = req.headers['accept-encoding'];
19
+ if (/gzip/.test(acceptEncoding)) {
20
+ res.writeHead(200, {
21
+ 'Content-Encoding': 'gzip'
22
+ });
23
+ this.dealCompress(_zlib.default.gzipSync(buffer), path, res);
24
+ } else {
25
+ res.writeHead(200, {});
26
+ this.dealDirect(buffer, path, res);
27
+ }
28
+ }
@@ -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,32 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = getHtml;
7
+ var _handlebars = _interopRequireDefault(require("handlebars"));
8
+ var _htmlMinifier = require("html-minifier");
9
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
10
+ function getHtml(title, content) {
11
+ const template = _handlebars.default.compile(`
12
+ <!doctype html>
13
+ <html lang="en">
14
+ <head>
15
+ <meta charset="utf-8" />
16
+ <title>{{title}}</title>
17
+ <meta name="description" content="{{content}}" />
18
+ <meta name="viewport" content="width=device-width, initial-scale=1" />
19
+ </head>
20
+ <body>
21
+ <main id="root"></main>
22
+ <script src="main.bundle.js"></script>
23
+ </body>
24
+ </html>
25
+ `);
26
+ return (0, _htmlMinifier.minify)(template({
27
+ title,
28
+ content
29
+ }), {
30
+ collapseWhitespace: true
31
+ });
32
+ }
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = parseDateString;
7
+ function parseDateString(dateString) {
8
+ return new Date(httpDate).getTime();
9
+ }
@@ -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,70 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.parseOption = 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.readCookie = 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
+ }
package/client.js DELETED
@@ -1,81 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.clearCookie = clearCookie;
7
- exports.filterNamespace = filterNamespace;
8
- exports.readCookie = readCookie;
9
- exports.setCookie = setCookie;
10
- function clearCookie(namespaces) {
11
- Object.keys(namespaces).forEach(n => {
12
- if (n.expires !== undefined) {
13
- Object.keys(n).forEach(k => {
14
- if (n === 'user') {
15
- document.cookie[n + '_' + k] = '';
16
- }
17
- window.localStorage.removeItem(n + '_' + k);
18
- });
19
- delete namespaces[n];
20
- }
21
- });
22
- return namespaces;
23
- }
24
- function filterNamespace(namespace) {
25
- const ans = {};
26
- Object.keys(namespace).forEach(k => {
27
- if (k !== 'expires') {
28
- ans[k] = namespace[k];
29
- }
30
- });
31
- return ans;
32
- }
33
- function readCookie() {
34
- const cookies = {};
35
- document.cookie.split(';').forEach(i => {
36
- const [key, value] = i.split('=');
37
- cookies[key.trim()] = value;
38
- });
39
- const namespaces = {};
40
- Object.keys(cookies).forEach(k => {
41
- const result = k.split('_');
42
- if (result.length === 2) {
43
- const [namespace, key] = result;
44
- if (namespaces[namespace] === undefined) {
45
- namespaces[namespace] = {};
46
- }
47
- namespaces[namespace][key] = cookies[k];
48
- }
49
- });
50
- for (let i = 0; i < window.localStorage.length; i += 1) {
51
- const k = window.localStorage.key(i);
52
- const result = k.split('_');
53
- if (result.length === 2) {
54
- const [namespace, key] = result;
55
- if (namespaces[namespace] === undefined) {
56
- namespaces[namespace] = {};
57
- }
58
- namespaces[namespace][key] = window.localStorage.getItem(k);
59
- }
60
- }
61
- return clearCookie(namespaces);
62
- }
63
- function setCookie(response) {
64
- const cookie = response.headers.get('cookie');
65
- if (cookie !== null) {
66
- if (cookie === '') {
67
- document.cookie.split(';').forEach(c => {
68
- const [k, v] = c.split('=');
69
- const [namespace] = k.split('_');
70
- if (namespace === 'user') {
71
- document.cookie = k + '=' + v;
72
- }
73
- window.localStorage.set(k, v);
74
- });
75
- } else {
76
- cookie.split(';').forEach(c => {
77
- document.cookie = c;
78
- });
79
- }
80
- }
81
- }
package/server.js DELETED
@@ -1,271 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.CommonHttp = void 0;
7
- exports.default = getHtml;
8
- exports.parseOption = parseOption;
9
- exports.readCookie = readCookie;
10
- var _path = _interopRequireDefault(require("path"));
11
- var _fs = _interopRequireDefault(require("fs"));
12
- var _zlib = _interopRequireDefault(require("zlib"));
13
- var _stream = require("stream");
14
- var _handlebars = _interopRequireDefault(require("handlebars"));
15
- var _htmlMinifier = require("html-minifier");
16
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
17
- function onError(err) {
18
- if (err) {
19
- console.error('An error occurred:', err);
20
- process.exitCode = 1;
21
- }
22
- }
23
- function parseDateString(dateString) {
24
- return new Date(httpDate).getTime();
25
- }
26
- function formateHttpDate(date) {
27
- let [week, month, day, year, time, zone] = date.toString().split(' ');
28
- zone = zone.split('+')[0];
29
- return month + ', ' + day + ' ' + month + ' ' + year + ' ' + time + ' ' + zone;
30
- }
31
- function isOption(string) {
32
- let ans = true;
33
- if (typeof string === 'string') {
34
- let i = 0;
35
- for (i = 0; i < 1; i += 1) {
36
- if (string.charAt(i) === '-') {
37
- break;
38
- }
39
- }
40
- if (i === 0) {
41
- ans = false;
42
- }
43
- } else {
44
- ans = false;
45
- }
46
- return ans;
47
- }
48
- function transformOption(value) {
49
- let ans;
50
- const s = value.split('-');
51
- if (s.length >= 1) {
52
- ans = s.map((e, i) => {
53
- if (i === 0) {
54
- return e;
55
- } else {
56
- return e.charAt(0).toUpperCase() + e.substring(1, e.length);
57
- }
58
- });
59
- } else {
60
- ans = value;
61
- }
62
- return ans.join('');
63
- }
64
- function parseOption(...params) {
65
- const ans = {};
66
- for (let i = 0; i < params.length; i += 1) {
67
- const param = params[i];
68
- if (param.charAt(0) === '-') {
69
- const regexp = /^\-([a-z])$/;
70
- if (regexp.test(param)) {
71
- const [_, k] = param.match(regexp);
72
- if (isOption(params[i + 1])) {
73
- ans[k] = params[i + 1];
74
- i += 1;
75
- } else {
76
- ans[k] = true;
77
- }
78
- }
79
- if (param.charAt(1) === '-') {
80
- const regexp = /^\-\-([a-z\-]+)$/;
81
- if (regexp.test(param)) {
82
- const [_, k] = param.match(regexp);
83
- if (isOption(params[i + 1])) {
84
- ans[transformOption(k)] = params[i + 1];
85
- i += 1;
86
- } else {
87
- ans[k] = true;
88
- }
89
- }
90
- }
91
- }
92
- }
93
- return ans;
94
- }
95
- function readCookie(cookie) {
96
- const cookies = {};
97
- if (typeof cookie === 'string') {
98
- cookie.split(';').forEach(i => {
99
- const [key, value] = i.split('=');
100
- cookies[key.trim()] = value;
101
- });
102
- }
103
- const namespaces = {};
104
- Object.keys(cookies).forEach(k => {
105
- const result = k.split('_');
106
- if (result.length === 2) {
107
- const [namespace, key] = result;
108
- if (namespaces[namespace] === undefined) {
109
- namespaces[namespace] = {};
110
- }
111
- namespaces[namespace][key] = cookies[k];
112
- }
113
- });
114
- return namespaces;
115
- }
116
- function formateHttpKey(key) {
117
- return key.split('-').map(v => {
118
- return v.substring(0, 1).toUpperCase() + v.substring(1, v.length);
119
- }).join('-');
120
- }
121
- function getHtml(title, content) {
122
- const template = _handlebars.default.compile(`
123
- <!doctype html>
124
- <html lang="en">
125
- <head>
126
- <meta charset="utf-8" />
127
- <title>{{title}}</title>
128
- <meta name="description" content="{{content}}" />
129
- <meta name="viewport" content="width=device-width, initial-scale=1" />
130
- </head>
131
- <body>
132
- <main id="root"></main>
133
- <script src="main.bundle.js"></script>
134
- </body>
135
- </html>
136
- `);
137
- return (0, _htmlMinifier.minify)(template({
138
- title,
139
- content
140
- }), {
141
- collapseWhitespace: true
142
- });
143
- }
144
- function getLists(list) {
145
- return list.join('|');
146
- }
147
- const time = new Date().getTime();
148
- class CommonHttp {
149
- constructor(options) {
150
- this.time = new Date().getTime();
151
- const {
152
- fonts
153
- } = options;
154
- if (options.fonts === undefined) {
155
- options.fonts = [];
156
- }
157
- this.modify = {};
158
- this.file = {};
159
- this.raw = {};
160
- this.compress = {};
161
- this.options = options;
162
- this.regexp = new RegExp(`\.(${getLists(options.fonts.concat(['html, ico', 'js']))})$`);
163
- }
164
- compressOutput(req, res, buffer, path) {
165
- res.setHeader('Vary', 'Accept-Encoding');
166
- let acceptEncoding = req.headers['accept-encoding'];
167
- if (/gzip/.test(acceptEncoding)) {
168
- res.writeHead(200, {
169
- 'Content-Encoding': 'gzip'
170
- });
171
- this.dealCompress(_zlib.default.gzipSync(buffer), path, res);
172
- } else {
173
- res.writeHead(200, {});
174
- this.dealDirect(buffer, path, res);
175
- }
176
- }
177
- cacheOutput(req, res, path, file, ms) {
178
- let ifModifiedSince = req.headers['If-Modified-Since'];
179
- if (ifModifiedSince === undefined) {
180
- if (this.modify[path] === undefined) {
181
- this.modify[path] = new Date(ms).toString();
182
- }
183
- if (this.file[path] === undefined) {
184
- this.file[path] = file;
185
- }
186
- res.setHeader('Last-Modified', formateHttpDate(this.modify[path]));
187
- this.compressOutput(req, res, this.file[path], path);
188
- } else {
189
- if (this.modify[path] === undefined) {
190
- this.modify[path] = new Date(ms).toString();
191
- }
192
- if (parseDateString(new Date().toString()) < parseDateString(this.modify[path])) {
193
- if (this.file[path] === undefined) {
194
- this.file[path] = file;
195
- }
196
- res.setHeader('Last-Modified', formateHttpDate(this.modify[path]));
197
- this.compressOutput(req, res, this.file[path], path);
198
- } else {
199
- res.writeHead(304);
200
- res.end();
201
- }
202
- }
203
- }
204
- dealCompress(data, path, res) {
205
- if (this.compress[path] === undefined) {
206
- this.compress[path] = data;
207
- }
208
- res.end(this.compress[path]);
209
- }
210
- directDeal(data, path, res) {
211
- if (this.raw[path] === undefined) {
212
- this.compress[path] = data;
213
- }
214
- res.end(this.raw[path]);
215
- }
216
- async process(req, res) {
217
- try {
218
- const {
219
- url
220
- } = req;
221
- if (url === '/update/time') {
222
- const {
223
- time
224
- } = this;
225
- res.end(time);
226
- } else if (this.regexp.test(url)) {
227
- this.cacheOutput(req, res, restPath, _fs.default.readFileSync(_path.default.resolve('static', restPath)), parseInt(_fs.default.statSync(_path.default.resolve('static', restPath)).mtimeMs));
228
- } else if (url.substring(0, 4) === '/api') {
229
- const body = await new Promise((resolve, reject) => {
230
- req.on('data', data => {
231
- resolve(data.toString());
232
- });
233
- });
234
- const {
235
- location
236
- } = this.options;
237
- if (location !== undefined) {
238
- const response = await fetch(location + url, {
239
- method: 'POST',
240
- body
241
- });
242
- for (const k of response.headers.keys()) {
243
- res.setHeader(formateHttpKey(k), response.headers.get(k));
244
- }
245
- const data = await response.text();
246
- res.end(JSON.stringify(data));
247
- }
248
- } else {
249
- if (this.html === undefined) {
250
- const {
251
- title,
252
- content
253
- } = this.options;
254
- this.html = getHtml(title, content);
255
- }
256
- this.cacheOutput(req, res, '*html', this.html, time);
257
- }
258
- } catch (e) {
259
- const {
260
- develope
261
- } = this.options;
262
- if (develope === true) {
263
- throw e;
264
- } else {
265
- res.writeHead(500);
266
- res.end();
267
- }
268
- }
269
- }
270
- }
271
- exports.CommonHttp = CommonHttp;