mirror of
https://github.com/ether/etherpad-lite.git
synced 2025-04-21 07:56:16 -04:00
lint: src/node/utils/caching_middleware.js
This commit is contained in:
parent
392d9dcfde
commit
7ab3ee2121
1 changed files with 23 additions and 23 deletions
|
@ -85,14 +85,14 @@ CachingMiddleware.prototype = new function () {
|
||||||
return next(undefined, req, res);
|
return next(undefined, req, res);
|
||||||
}
|
}
|
||||||
|
|
||||||
const old_req = {};
|
const oldReq = {};
|
||||||
const old_res = {};
|
const oldRes = {};
|
||||||
|
|
||||||
const supportsGzip =
|
const supportsGzip =
|
||||||
(req.get('Accept-Encoding') || '').indexOf('gzip') !== -1;
|
(req.get('Accept-Encoding') || '').indexOf('gzip') !== -1;
|
||||||
|
|
||||||
const path = require('url').parse(req.url).path;
|
const url = new URL(req.url, 'http://localhost');
|
||||||
const cacheKey = generateCacheKey(path);
|
const cacheKey = generateCacheKey(url.pathname + url.search);
|
||||||
|
|
||||||
fs.stat(`${CACHE_DIR}minified_${cacheKey}`, (error, stats) => {
|
fs.stat(`${CACHE_DIR}minified_${cacheKey}`, (error, stats) => {
|
||||||
const modifiedSince = (req.headers['if-modified-since'] &&
|
const modifiedSince = (req.headers['if-modified-since'] &&
|
||||||
|
@ -105,7 +105,7 @@ CachingMiddleware.prototype = new function () {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Always issue get to downstream.
|
// Always issue get to downstream.
|
||||||
old_req.method = req.method;
|
oldReq.method = req.method;
|
||||||
req.method = 'GET';
|
req.method = 'GET';
|
||||||
|
|
||||||
const expirationDate = new Date(((responseCache[cacheKey] || {}).headers || {}).expires);
|
const expirationDate = new Date(((responseCache[cacheKey] || {}).headers || {}).expires);
|
||||||
|
@ -115,18 +115,18 @@ CachingMiddleware.prototype = new function () {
|
||||||
}
|
}
|
||||||
|
|
||||||
const _headers = {};
|
const _headers = {};
|
||||||
old_res.setHeader = res.setHeader;
|
oldRes.setHeader = res.setHeader;
|
||||||
res.setHeader = (key, value) => {
|
res.setHeader = (key, value) => {
|
||||||
// Don't set cookies, see issue #707
|
// Don't set cookies, see issue #707
|
||||||
if (key.toLowerCase() === 'set-cookie') return;
|
if (key.toLowerCase() === 'set-cookie') return;
|
||||||
|
|
||||||
_headers[key.toLowerCase()] = value;
|
_headers[key.toLowerCase()] = value;
|
||||||
old_res.setHeader.call(res, key, value);
|
oldRes.setHeader.call(res, key, value);
|
||||||
};
|
};
|
||||||
|
|
||||||
old_res.writeHead = res.writeHead;
|
oldRes.writeHead = res.writeHead;
|
||||||
res.writeHead = function (status, headers) {
|
res.writeHead = (status, headers) => {
|
||||||
res.writeHead = old_res.writeHead;
|
res.writeHead = oldRes.writeHead;
|
||||||
if (status === 200) {
|
if (status === 200) {
|
||||||
// Update cache
|
// Update cache
|
||||||
let buffer = '';
|
let buffer = '';
|
||||||
|
@ -136,20 +136,20 @@ CachingMiddleware.prototype = new function () {
|
||||||
});
|
});
|
||||||
headers = _headers;
|
headers = _headers;
|
||||||
|
|
||||||
old_res.write = res.write;
|
oldRes.write = res.write;
|
||||||
old_res.end = res.end;
|
oldRes.end = res.end;
|
||||||
res.write = function (data, encoding) {
|
res.write = (data, encoding) => {
|
||||||
buffer += data.toString(encoding);
|
buffer += data.toString(encoding);
|
||||||
};
|
};
|
||||||
res.end = function (data, encoding) {
|
res.end = (data, encoding) => {
|
||||||
async.parallel([
|
async.parallel([
|
||||||
function (callback) {
|
(callback) => {
|
||||||
const path = `${CACHE_DIR}minified_${cacheKey}`;
|
const path = `${CACHE_DIR}minified_${cacheKey}`;
|
||||||
fs.writeFile(path, buffer, (error, stats) => {
|
fs.writeFile(path, buffer, (error, stats) => {
|
||||||
callback();
|
callback();
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
function (callback) {
|
(callback) => {
|
||||||
const path = `${CACHE_DIR}minified_${cacheKey}.gz`;
|
const path = `${CACHE_DIR}minified_${cacheKey}.gz`;
|
||||||
zlib.gzip(buffer, (error, content) => {
|
zlib.gzip(buffer, (error, content) => {
|
||||||
if (error) {
|
if (error) {
|
||||||
|
@ -168,10 +168,10 @@ CachingMiddleware.prototype = new function () {
|
||||||
};
|
};
|
||||||
} else if (status === 304) {
|
} else if (status === 304) {
|
||||||
// Nothing new changed from the cached version.
|
// Nothing new changed from the cached version.
|
||||||
old_res.write = res.write;
|
oldRes.write = res.write;
|
||||||
old_res.end = res.end;
|
oldRes.end = res.end;
|
||||||
res.write = function (data, encoding) {};
|
res.write = (data, encoding) => {};
|
||||||
res.end = function (data, encoding) { respond(); };
|
res.end = (data, encoding) => { respond(); };
|
||||||
} else {
|
} else {
|
||||||
res.writeHead(status, headers);
|
res.writeHead(status, headers);
|
||||||
}
|
}
|
||||||
|
@ -184,9 +184,9 @@ CachingMiddleware.prototype = new function () {
|
||||||
// TODO: Implement locking on write or ditch caching of gzip and use
|
// TODO: Implement locking on write or ditch caching of gzip and use
|
||||||
// existing middlewares.
|
// existing middlewares.
|
||||||
function respond() {
|
function respond() {
|
||||||
req.method = old_req.method || req.method;
|
req.method = oldReq.method || req.method;
|
||||||
res.write = old_res.write || res.write;
|
res.write = oldRes.write || res.write;
|
||||||
res.end = old_res.end || res.end;
|
res.end = oldRes.end || res.end;
|
||||||
|
|
||||||
const headers = {};
|
const headers = {};
|
||||||
Object.assign(headers, (responseCache[cacheKey].headers || {}));
|
Object.assign(headers, (responseCache[cacheKey].headers || {}));
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue