manner.js 0.0.28 → 0.0.30

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/server.js +57 -4
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "manner.js",
3
- "version": "0.0.28",
3
+ "version": "0.0.30",
4
4
  "description": "Frontend mode project.",
5
5
  "repository": "git@github.com:leobrad/mode.git",
6
6
  "author": "Leo Ely",
package/server.js CHANGED
@@ -9,11 +9,25 @@ exports.parseOption = parseOption;
9
9
  exports.readCookie = readCookie;
10
10
  var _path = _interopRequireDefault(require("path"));
11
11
  var _fs = _interopRequireDefault(require("fs"));
12
+ var _zlib = _interopRequireDefault(require("zlib"));
13
+ var _stream = require("stream");
12
14
  var _handlebars = _interopRequireDefault(require("handlebars"));
13
15
  var _htmlMinifier = require("html-minifier");
14
- var _cacheOutput = _interopRequireDefault(require("./lib/cacheOutput"));
15
- var _compressOutput = _interopRequireDefault(require("./lib/compressOutput"));
16
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
+ }
17
31
  function isOption(string) {
18
32
  let ans = true;
19
33
  if (typeof string === 'string') {
@@ -146,8 +160,46 @@ class CommonHttp {
146
160
  this.compress = {};
147
161
  this.options = options;
148
162
  this.regexp = new RegExp(`\.(${getLists(options.fonts.concat(['html, ico', 'js']))})$`);
149
- this.cacheOutput = _cacheOutput.default.bind(this);
150
- this.compressOutput = _compressOutput.default.bind(this);
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
+ }
151
203
  }
152
204
  dealCompress(data, path, res) {
153
205
  if (this.compress[path] === undefined) {
@@ -172,6 +224,7 @@ class CommonHttp {
172
224
  } = this;
173
225
  res.end(time);
174
226
  } else if (this.regexp.test(url)) {
227
+ const restPath = url.substring(1, url.length);
175
228
  this.cacheOutput(req, res, restPath, _fs.default.readFileSync(_path.default.resolve('static', restPath)), parseInt(_fs.default.statSync(_path.default.resolve('static', restPath)).mtimeMs));
176
229
  } else if (url.substring(0, 4) === '/api') {
177
230
  const body = await new Promise((resolve, reject) => {