1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.beforeRes = exports.afterRes = exports.beforeReq = undefined;
|
7 |
|
8 | var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; };
|
9 |
|
10 |
|
11 | var _log = require('./log');
|
12 |
|
13 | var _log2 = _interopRequireDefault(_log);
|
14 |
|
15 | var _rule = require('./config/rule');
|
16 |
|
17 | var _config = require('./config/config');
|
18 |
|
19 | var config = _interopRequireWildcard(_config);
|
20 |
|
21 | var _mime = require('mime');
|
22 |
|
23 | var _mime2 = _interopRequireDefault(_mime);
|
24 |
|
25 | var _iconvLite = require('iconv-lite');
|
26 |
|
27 | var _iconvLite2 = _interopRequireDefault(_iconvLite);
|
28 |
|
29 | var _zlib = require('zlib');
|
30 |
|
31 | var _zlib2 = _interopRequireDefault(_zlib);
|
32 |
|
33 | var _buffer = require('buffer');
|
34 |
|
35 | var _promise = require('promise');
|
36 |
|
37 | var _promise2 = _interopRequireDefault(_promise);
|
38 |
|
39 | var _path = require('path');
|
40 |
|
41 | var _path2 = _interopRequireDefault(_path);
|
42 |
|
43 | function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
|
44 |
|
45 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
46 |
|
47 |
|
48 |
|
49 | var checkMetaCharset = /<meta(?:\s)+.*charset(?:\s)*=(?:[\s'"])*([^"']+)/i;
|
50 |
|
51 | var autoDecodeRegs = /text\/.+|(?:application\/(?:json.*|.*javascript))/i;
|
52 |
|
53 | var excludeDecodeExt = ['ttf', 'eot', 'svg', 'woff'];
|
54 |
|
55 | var decodeCompress = function decodeCompress(bodyData, encode) {
|
56 | return new _promise2.default(function (resolve, reject) {
|
57 |
|
58 | if (bodyData) {
|
59 | var isZip = /gzip/i.test(encode);
|
60 | var isDeflate = /deflate/i.test(encode);
|
61 | if (isZip) {
|
62 | _zlib2.default.gunzip(bodyData, function (err, buff) {
|
63 | if (err) {
|
64 | reject(err.message);
|
65 | _log2.default.error('decompress err: ', err.message);
|
66 | } else {
|
67 | resolve(buff);
|
68 | }
|
69 | });
|
70 | } else if (isDeflate) {
|
71 | _zlib2.default.inflateRaw(bodyData, function (err, buff) {
|
72 | if (err) {
|
73 | reject(err.message);
|
74 | _log2.default.error('decompress err: ', err.message);
|
75 | } else {
|
76 | resolve(buff);
|
77 | }
|
78 | });
|
79 | } else {
|
80 | resolve(bodyData);
|
81 | }
|
82 | } else {
|
83 | resolve(new _buffer.Buffer(""));
|
84 | }
|
85 | });
|
86 | };
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 | var beforeReq = function beforeReq(reqInfo) {
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 | return (0, _rule.parseRule)(reqInfo).then(function (result) {
|
149 | return result || reqInfo;
|
150 | }).then(function (reqInfo) {
|
151 | return reqInfo;
|
152 | });
|
153 | };
|
154 |
|
155 | var decodeContent = function decodeContent(bodyData, contentType, contentEncoding, isDecode) {
|
156 |
|
157 | return decodeCompress(bodyData, contentEncoding)
|
158 |
|
159 | .then(function (bodyData) {
|
160 |
|
161 | var charset = 'UTF-8',
|
162 | tmp = void 0;
|
163 | var ext = _mime2.default.extension(contentType);
|
164 | if (!bodyData || !isDecode) {
|
165 | return { bodyData: bodyData, charset: charset };
|
166 | }
|
167 | if (contentType) {
|
168 |
|
169 | tmp = contentType.match(/charset=([^;]+)/);
|
170 | if (tmp && tmp.length > 0) {
|
171 | charset = tmp[1].toUpperCase();
|
172 | }
|
173 | }
|
174 | if (_buffer.Buffer.isBuffer(bodyData)) {
|
175 |
|
176 | if (charset !== 'UTF-8' && _iconvLite2.default.encodingExists(charset)) {
|
177 | bodyData = _iconvLite2.default.decode(bodyData, charset);
|
178 | } else if (contentType && (ext === 'html' || ext === 'htm')) {
|
179 |
|
180 | var strBodyData = bodyData.toString();
|
181 |
|
182 | var _tmp = strBodyData.match(checkMetaCharset);
|
183 | if (_tmp && _tmp[1]) {
|
184 | _tmp[1] = _tmp[1].toUpperCase();
|
185 | if (_tmp[1] !== "UTF-8" && _iconvLite2.default.encodingExists(_tmp[1])) {
|
186 | charset = _tmp[1];
|
187 | bodyData = _iconvLite2.default.decode(bodyData, _tmp[1]);
|
188 | } else {
|
189 | bodyData = strBodyData;
|
190 | }
|
191 | } else {
|
192 | bodyData = strBodyData;
|
193 | }
|
194 | } else {
|
195 | bodyData = bodyData.toString();
|
196 | }
|
197 | }
|
198 |
|
199 | return {
|
200 | bodyData: bodyData,
|
201 | charset: charset
|
202 | };
|
203 | });
|
204 | };
|
205 |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 |
|
212 |
|
213 |
|
214 |
|
215 |
|
216 |
|
217 |
|
218 |
|
219 |
|
220 |
|
221 |
|
222 |
|
223 |
|
224 | var beforeRes = function beforeRes(resInfo) {
|
225 | return _promise2.default.resolve(resInfo).then(function (resInfo) {
|
226 |
|
227 | var disCache = config.get('disCache');
|
228 | if (disCache) {
|
229 | resInfo.headers['cache-control'] = "no-store";
|
230 | resInfo.headers.expires = "0";
|
231 | delete resInfo.headers.etag;
|
232 | delete resInfo.headers['last-modifed'];
|
233 | }
|
234 | return resInfo;
|
235 | }).then(function (resInfo) {
|
236 |
|
237 | var disCache = config.get('disCache');
|
238 |
|
239 | var useListener = false;
|
240 | if (disCache || useListener) {
|
241 | var _ret = function () {
|
242 | var contentType = resInfo.headers['content-type'] || "";
|
243 | var contentEncoding = resInfo.headers['content-encoding'];
|
244 |
|
245 | var ext = _path2.default.extname(resInfo.path.split('?')[0].split('#')[0]) || "";
|
246 | ext = (ext.split('.')[1] || "").toLowerCase();
|
247 | if (excludeDecodeExt.some(function (cur) {
|
248 | return cur === ext;
|
249 | })) {
|
250 | ext = "";
|
251 | }
|
252 | return {
|
253 | v: decodeContent(resInfo.bodyData, contentType, contentEncoding, autoDecodeRegs.test(contentType) && !!ext).then(function (_ref) {
|
254 | var charset = _ref.charset;
|
255 | var bodyData = _ref.bodyData;
|
256 |
|
257 | var extension = _mime2.default.extension(contentType);
|
258 | delete resInfo.headers['content-encoding'];
|
259 |
|
260 | if (disCache && contentType && (extension === 'html' || extension === 'htm') && typeof bodyData === 'string') {
|
261 | bodyData = bodyData.replace("<head>", '<head>\n\t\t\t\t\t\t<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />\n\t\t\t\t\t\t<meta http-equiv="Pragma" content="no-cache" />\n\t\t\t\t\t\t<meta http-equiv="Expires" content="0" />');
|
262 | }
|
263 | resInfo.charset = charset;
|
264 | resInfo.bodyData = bodyData;
|
265 | return resInfo;
|
266 | })
|
267 | };
|
268 | }();
|
269 |
|
270 | if ((typeof _ret === 'undefined' ? 'undefined' : _typeof(_ret)) === "object") return _ret.v;
|
271 | }
|
272 | return _promise2.default.resolve(resInfo);
|
273 | }).then(function (resInfo) {
|
274 |
|
275 | if (typeof resInfo.bodyData === 'string') {
|
276 | resInfo.bodyData = _iconvLite2.default.encode(resInfo.bodyData, resInfo.charset || "UTF-8");
|
277 | }
|
278 | return resInfo;
|
279 | });
|
280 |
|
281 |
|
282 |
|
283 | };
|
284 |
|
285 |
|
286 |
|
287 |
|
288 |
|
289 |
|
290 |
|
291 |
|
292 |
|
293 |
|
294 |
|
295 |
|
296 |
|
297 |
|
298 |
|
299 |
|
300 |
|
301 |
|
302 |
|
303 |
|
304 |
|
305 |
|
306 |
|
307 |
|
308 |
|
309 | var afterRes = function afterRes(result) {
|
310 | return result;
|
311 | };
|
312 |
|
313 | exports.beforeReq = beforeReq;
|
314 | exports.afterRes = afterRes;
|
315 | exports.beforeRes = beforeRes; |
\ | No newline at end of file |