manner.js 0.0.43 → 0.0.45

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/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "manner.js",
3
- "version": "0.0.43",
3
+ "version": "0.0.45",
4
4
  "description": "Frontend mode project.",
5
5
  "repository": "git@github.com:leobrad/mode.git",
6
6
  "author": "Leo Ely",
7
7
  "license": "MIT",
8
8
  "scripts": {
9
- "build": "gulp build && node dist/scripts/index.js",
9
+ "build": "gulp build && node dist/script/index.js",
10
10
  "test": "jest"
11
11
  },
12
12
  "devDependencies": {
package/server/index.js CHANGED
@@ -3,16 +3,16 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- Object.defineProperty(exports, "CommonHttp", {
6
+ Object.defineProperty(exports, "formatHttpDate", {
7
7
  enumerable: true,
8
8
  get: function () {
9
- return _CommonHttp.default;
9
+ return _formatHttpDate.default;
10
10
  }
11
11
  });
12
- Object.defineProperty(exports, "parseOption", {
12
+ Object.defineProperty(exports, "formatHttpKey", {
13
13
  enumerable: true,
14
14
  get: function () {
15
- return _parseOption.default;
15
+ return _formatHttpKey.default;
16
16
  }
17
17
  });
18
18
  Object.defineProperty(exports, "readCookie", {
@@ -21,7 +21,7 @@ Object.defineProperty(exports, "readCookie", {
21
21
  return _readCookie.default;
22
22
  }
23
23
  });
24
- var _CommonHttp = _interopRequireDefault(require("./class/CommonHttp"));
25
- var _parseOption = _interopRequireDefault(require("./lib/parseOption"));
26
24
  var _readCookie = _interopRequireDefault(require("./lib/readCookie"));
25
+ var _formatHttpDate = _interopRequireDefault(require("./lib/formatHttpDate"));
26
+ var _formatHttpKey = _interopRequireDefault(require("./lib/formatHttpKey"));
27
27
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
@@ -4,7 +4,8 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = formateHttpDate;
7
- function formateHttpDate(date) {
7
+ function formateHttpDate(ms) {
8
+ const date = new Date(ms);
8
9
  let [week, month, day, year, time, zone] = date.toString().split(' ');
9
10
  zone = zone.split('+')[0];
10
11
  return month + ', ' + day + ' ' + month + ' ' + year + ' ' + time + ' ' + zone;
@@ -1,143 +0,0 @@
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
- const {
21
- fonts
22
- } = options;
23
- if (options.fonts === undefined) {
24
- options.fonts = [];
25
- }
26
- this.time = new Date().getTime();
27
- this.modify = {};
28
- this.file = {};
29
- this.raw = {};
30
- this.compress = {};
31
- this.options = options;
32
- this.regexp = new RegExp(`\.(${getLists(options.fonts.concat(['ico', 'js', 'png', 'jpg']))})$`);
33
- }
34
- compressOutput(req, res, buffer, path) {
35
- res.setHeader('Vary', 'Accept-Encoding');
36
- let acceptEncoding = req.headers['accept-encoding'];
37
- if (/gzip/.test(acceptEncoding)) {
38
- res.writeHead(200, {
39
- 'Content-Encoding': 'gzip'
40
- });
41
- this.dealCompress(_zlib.default.gzipSync(buffer), path, res);
42
- } else {
43
- res.writeHead(200, {});
44
- this.dealDirect(buffer, path, res);
45
- }
46
- }
47
- cacheOutput(req, res, path, file, ms) {
48
- let ifModifiedSince = req.headers['If-Modified-Since'];
49
- if (ifModifiedSince === undefined) {
50
- if (this.modify[path] === undefined) {
51
- this.modify[path] = new Date(ms).toString();
52
- }
53
- if (this.file[path] === undefined) {
54
- this.file[path] = file;
55
- }
56
- res.setHeader('Last-Modified', (0, _formateHttpDate.default)(this.modify[path]));
57
- this.compressOutput(req, res, this.file[path], path);
58
- } else {
59
- if (this.modify[path] === undefined) {
60
- this.modify[path] = new Date(ms).toString();
61
- }
62
- if ((0, _parseDateString.default)(new Date().toString()) < (0, _parseDateString.default)(this.modify[path])) {
63
- if (this.file[path] === undefined) {
64
- this.file[path] = file;
65
- }
66
- res.setHeader('Last-Modified', (0, _formateHttpDate.default)(this.modify[path]));
67
- this.compressOutput(req, res, this.file[path], path);
68
- } else {
69
- res.writeHead(304);
70
- res.end();
71
- }
72
- }
73
- }
74
- dealCompress(data, path, res) {
75
- if (this.compress[path] === undefined) {
76
- this.compress[path] = data;
77
- }
78
- res.end(this.compress[path]);
79
- }
80
- directDeal(data, path, res) {
81
- if (this.raw[path] === undefined) {
82
- this.compress[path] = data;
83
- }
84
- res.end(this.raw[path]);
85
- }
86
- async process(req, res) {
87
- try {
88
- const {
89
- url
90
- } = req;
91
- if (url === '/update/time') {
92
- const {
93
- time
94
- } = this;
95
- res.end(String(time));
96
- } else if (this.regexp.test(url)) {
97
- const restPath = url.substring(1, url.length);
98
- this.cacheOutput(req, res, restPath, _fs.default.readFileSync(_path.default.resolve('static', restPath)), parseInt(_fs.default.statSync(_path.default.resolve('static', restPath)).mtimeMs));
99
- } else if (url.substring(0, 4) === '/api') {
100
- const body = await new Promise((resolve, reject) => {
101
- req.on('data', data => {
102
- resolve(data.toString());
103
- });
104
- });
105
- const {
106
- location
107
- } = this.options;
108
- if (location !== undefined) {
109
- const response = await fetch(location + url, {
110
- method: 'POST',
111
- body
112
- });
113
- for (const k of response.headers.keys()) {
114
- res.setHeader((0, _formateHttpKey.default)(k), response.headers.get(k));
115
- }
116
- const data = await response.text();
117
- res.end(JSON.stringify(data));
118
- }
119
- } else {
120
- if (this.html === undefined) {
121
- const {
122
- title,
123
- content,
124
- icon
125
- } = this.options;
126
- this.html = (0, _getHtml.default)(title, content, icon);
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;
@@ -1,36 +0,0 @@
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
- }
@@ -1,28 +0,0 @@
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
- }
@@ -1,11 +0,0 @@
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
- }
@@ -1,38 +0,0 @@
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, icon) {
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
- {{#if icon}}
20
- <link rel="icon" type="image/png" size="16x16" href="{{icon}}" />
21
- {{else}}
22
- <link rel="shortcut icon" href="http://www.example.com/my_empty_resource" />
23
- {{/if}}
24
- </head>
25
- <body>
26
- <main id="root"></main>
27
- <script src="main.bundle.js"></script>
28
- </body>
29
- </html>
30
- `);
31
- return (0, _htmlMinifier.minify)(template({
32
- title,
33
- content,
34
- icon
35
- }), {
36
- collapseWhitespace: true
37
- });
38
- }
@@ -1,9 +0,0 @@
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
- }
@@ -1,70 +0,0 @@
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
- }