mirror of
https://github.com/ether/etherpad-lite.git
synced 2025-04-20 23:46:14 -04:00
lint: src/node/utils/caching_middleware.js
This commit is contained in:
parent
02c1bf7d81
commit
f664f84da5
1 changed files with 18 additions and 21 deletions
|
@ -1,3 +1,5 @@
|
||||||
|
'use strict';
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* 2011 Peter 'Pita' Martischka (Primary Technology Ltd)
|
* 2011 Peter 'Pita' Martischka (Primary Technology Ltd)
|
||||||
*
|
*
|
||||||
|
@ -47,18 +49,16 @@ CACHE_DIR = existsSync(CACHE_DIR) ? CACHE_DIR : undefined;
|
||||||
|
|
||||||
const responseCache = {};
|
const responseCache = {};
|
||||||
|
|
||||||
function djb2Hash(data) {
|
const djb2Hash = (data) => {
|
||||||
const chars = data.split('').map((str) => str.charCodeAt(0));
|
const chars = data.split('').map((str) => str.charCodeAt(0));
|
||||||
return `${chars.reduce((prev, curr) => ((prev << 5) + prev) + curr, 5381)}`;
|
return `${chars.reduce((prev, curr) => ((prev << 5) + prev) + curr, 5381)}`;
|
||||||
}
|
};
|
||||||
|
|
||||||
function generateCacheKeyWithSha256(path) {
|
const generateCacheKeyWithSha256 =
|
||||||
return _crypto.createHash('sha256').update(path).digest('hex');
|
(path) => _crypto.createHash('sha256').update(path).digest('hex');
|
||||||
}
|
|
||||||
|
|
||||||
function generateCacheKeyWithDjb2(path) {
|
const generateCacheKeyWithDjb2 =
|
||||||
return Buffer.from(djb2Hash(path)).toString('hex');
|
(path) => Buffer.from(djb2Hash(path)).toString('hex');
|
||||||
}
|
|
||||||
|
|
||||||
let generateCacheKey;
|
let generateCacheKey;
|
||||||
|
|
||||||
|
@ -66,7 +66,7 @@ if (_crypto) {
|
||||||
generateCacheKey = generateCacheKeyWithSha256;
|
generateCacheKey = generateCacheKeyWithSha256;
|
||||||
} else {
|
} else {
|
||||||
generateCacheKey = generateCacheKeyWithDjb2;
|
generateCacheKey = generateCacheKeyWithDjb2;
|
||||||
console.warn('No crypto support in this nodejs runtime. A fallback to Djb2 (weaker) will be used.');
|
console.warn('No crypto support in this nodejs runtime. Djb2 (weaker) will be used.');
|
||||||
}
|
}
|
||||||
|
|
||||||
// MIMIC https://github.com/microsoft/TypeScript/commit/9677b0641cc5ba7d8b701b4f892ed7e54ceaee9a - END
|
// MIMIC https://github.com/microsoft/TypeScript/commit/9677b0641cc5ba7d8b701b4f892ed7e54ceaee9a - END
|
||||||
|
@ -80,8 +80,8 @@ if (_crypto) {
|
||||||
function CachingMiddleware() {
|
function CachingMiddleware() {
|
||||||
}
|
}
|
||||||
CachingMiddleware.prototype = new function () {
|
CachingMiddleware.prototype = new function () {
|
||||||
function handle(req, res, next) {
|
const handle = (req, res, next) => {
|
||||||
if (!(req.method == 'GET' || req.method == 'HEAD') || !CACHE_DIR) {
|
if (!(req.method === 'GET' || req.method === 'HEAD') || !CACHE_DIR) {
|
||||||
return next(undefined, req, res);
|
return next(undefined, req, res);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -89,7 +89,7 @@ CachingMiddleware.prototype = new function () {
|
||||||
const old_res = {};
|
const old_res = {};
|
||||||
|
|
||||||
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 path = require('url').parse(req.url).path;
|
||||||
const cacheKey = generateCacheKey(path);
|
const cacheKey = generateCacheKey(path);
|
||||||
|
@ -116,7 +116,7 @@ CachingMiddleware.prototype = new function () {
|
||||||
|
|
||||||
const _headers = {};
|
const _headers = {};
|
||||||
old_res.setHeader = res.setHeader;
|
old_res.setHeader = res.setHeader;
|
||||||
res.setHeader = function (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;
|
||||||
|
|
||||||
|
@ -126,11 +126,8 @@ CachingMiddleware.prototype = new function () {
|
||||||
|
|
||||||
old_res.writeHead = res.writeHead;
|
old_res.writeHead = res.writeHead;
|
||||||
res.writeHead = function (status, headers) {
|
res.writeHead = function (status, headers) {
|
||||||
const lastModified = (res.getHeader('last-modified') &&
|
|
||||||
new Date(res.getHeader('last-modified')));
|
|
||||||
|
|
||||||
res.writeHead = old_res.writeHead;
|
res.writeHead = old_res.writeHead;
|
||||||
if (status == 200) {
|
if (status === 200) {
|
||||||
// Update cache
|
// Update cache
|
||||||
let buffer = '';
|
let buffer = '';
|
||||||
|
|
||||||
|
@ -169,7 +166,7 @@ CachingMiddleware.prototype = new function () {
|
||||||
respond();
|
respond();
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
} 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;
|
old_res.write = res.write;
|
||||||
old_res.end = res.end;
|
old_res.end = res.end;
|
||||||
|
@ -204,10 +201,10 @@ CachingMiddleware.prototype = new function () {
|
||||||
const lastModified = (headers['last-modified'] &&
|
const lastModified = (headers['last-modified'] &&
|
||||||
new Date(headers['last-modified']));
|
new Date(headers['last-modified']));
|
||||||
|
|
||||||
if (statusCode == 200 && lastModified <= modifiedSince) {
|
if (statusCode === 200 && lastModified <= modifiedSince) {
|
||||||
res.writeHead(304, headers);
|
res.writeHead(304, headers);
|
||||||
res.end();
|
res.end();
|
||||||
} else if (req.method == 'GET') {
|
} else if (req.method === 'GET') {
|
||||||
const readStream = fs.createReadStream(pathStr);
|
const readStream = fs.createReadStream(pathStr);
|
||||||
res.writeHead(statusCode, headers);
|
res.writeHead(statusCode, headers);
|
||||||
readStream.pipe(res);
|
readStream.pipe(res);
|
||||||
|
@ -217,7 +214,7 @@ CachingMiddleware.prototype = new function () {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
};
|
||||||
|
|
||||||
this.handle = handle;
|
this.handle = handle;
|
||||||
}();
|
}();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue