evaengine 0.11.0 → 0.11.2
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.
|
@@ -55,7 +55,6 @@ const defaultHeadersFilter = exports.defaultHeadersFilter = res => {
|
|
|
55
55
|
const requestToCacheKey = exports.requestToCacheKey = (req, hashStrategy) => {
|
|
56
56
|
const {
|
|
57
57
|
method,
|
|
58
|
-
baseUrl,
|
|
59
58
|
originalUrl,
|
|
60
59
|
query: originQuery,
|
|
61
60
|
route,
|
|
@@ -73,11 +72,12 @@ const requestToCacheKey = exports.requestToCacheKey = (req, hashStrategy) => {
|
|
|
73
72
|
throw new _exceptions.RuntimeException(`View cache middleware only support GET method of http request for ${originalUrl}`);
|
|
74
73
|
}
|
|
75
74
|
const { host = 'unknown' } = req.headers;
|
|
76
|
-
const
|
|
75
|
+
const [urlPath = ''] = originalUrl.split('?');
|
|
76
|
+
const key = [method, `/${host}`, urlPath].join('').replace(/:/g, '_').toLowerCase();
|
|
77
77
|
const strategy = hashStrategy || defaultHashStrategy;
|
|
78
78
|
const hash = _crypto2.default.createHash('md5').update(JSON.stringify(strategy({
|
|
79
79
|
method,
|
|
80
|
-
|
|
80
|
+
urlPath,
|
|
81
81
|
query,
|
|
82
82
|
uid
|
|
83
83
|
}))).digest('hex');
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/middlewares/view_cache.js"],"names":["defaultHashStrategy","obj","defaultHeadersFilter","res","headers","forEach","key","value","get","push","requestToCacheKey","req","hashStrategy","method","
|
|
1
|
+
{"version":3,"sources":["../../src/middlewares/view_cache.js"],"names":["defaultHashStrategy","obj","defaultHeadersFilter","res","headers","forEach","key","value","get","push","requestToCacheKey","req","hashStrategy","method","originalUrl","query","originQuery","route","uid","flush","util","isFunction","RuntimeException","toLowerCase","host","urlPath","split","join","replace","strategy","hash","crypto","createHash","update","JSON","stringify","digest","ViewCacheMiddleware","cache","logger","options","isObject","ttl","headersFilter","namespace","Object","assign","next","cacheKey","cachedHeaders","body","cachedBody","debug","length","setHeader","send","realSend","add","format","statusCode","set","catch","e","error","Cache","Logger"],"mappings":";;;;;;;;;AAAA;;AACA;;;;AACA;;;;AACA;;;;AACA;;;;AACA;;;;AACA;;;;AACA;;;;;;AAEO,MAAMA,oDAAsBC,OAAOA,GAAnC;;AAEA,MAAMC,sDAAwBC,GAAD,IAAS;AAC3C,QAAMC,UAAU,EAAhB;AACA,GAAC,cAAD,EAAiBC,OAAjB,CAA0BC,GAAD,IAAS;AAChC,UAAMC,QAAQJ,IAAIK,GAAJ,CAAQF,GAAR,CAAd;AACA,QAAIC,KAAJ,EAAW;AACTH,cAAQK,IAAR,CAAa,CAACH,GAAD,EAAMC,KAAN,CAAb;AACD;AACF,GALD;AAMA,SAAOH,OAAP;AACD,CATM;;AAWA,MAAMM,gDAAoB,CAACC,GAAD,EAAMC,YAAN,KAAuB;AACtD,QAAM;AACJC,UADI;AAEJC,eAFI;AAGJC,WAAOC,WAHH;AAIJC,SAJI;AAKJC,UAAM,IALF,CAKO;AALP,MAMFP,GANJ;AAOA,QAAMI,qBAAaC,WAAb,CAAN;AACA,SAAOD,MAAMI,KAAb;AACA,MAAIP,gBAAgB,CAACQ,eAAKC,UAAL,CAAgBT,YAAhB,CAArB,EAAoD;AAClD,UAAM,IAAIU,4BAAJ,CAAsB,mDAAkDR,WAAY,EAApF,CAAN;AACD;AACD,MAAI,CAACG,KAAL,EAAY;AACV,UAAM,IAAIK,4BAAJ,CAAsB,2CAA0CR,WAAY,EAA5E,CAAN;AACD;AACD,MAAID,OAAOU,WAAP,OAAyB,KAA7B,EAAoC;AAClC,UAAM,IAAID,4BAAJ,CAAsB,qEAAoER,WAAY,EAAtG,CAAN;AACD;AACD,QAAM,EAAEU,OAAO,SAAT,KAAuBb,IAAIP,OAAjC;AACA,QAAM,CAACqB,UAAU,EAAX,IAAiBX,YAAYY,KAAZ,CAAkB,GAAlB,CAAvB;AACA,QAAMpB,MAAM,CA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BAAOwB,KAAP,CAAa,8BAAb,EAA6Cf,QAA7C,EAAuDc,CAAvD;AACD,aAFD;AAGD;AACF,SAbD;AAcAf;AACD,OArCM;;AAAA;AAAA;AAAA;AAAA,SAAP;AAsCD,GAvDD;AAwDD;;AAED,8BAAaiB,eAAb,EAAoBC,gBAApB,EAA4B5B,mBAA5B,E,CAAiD;;kBAElCA,mB","file":"view_cache.js","sourcesContent":["import { Dependencies } from 'constitute';\nimport crypto from 'crypto';\nimport util from 'util';\nimport moment from 'moment-timezone';\nimport Logger from '../services/logger';\nimport Cache from '../services/cache';\nimport wrapper from '../utils/wrapper';\nimport { RuntimeException } from '../exceptions';\n\nexport const defaultHashStrategy = obj => obj;\n\nexport const defaultHeadersFilter = (res) => {\n const headers = [];\n ['Content-Type'].forEach((key) => {\n const value = res.get(key);\n if (value) {\n headers.push([key, value]);\n }\n });\n return headers;\n};\n\nexport const requestToCacheKey = (req, hashStrategy) => {\n const {\n method,\n originalUrl,\n query: originQuery,\n route,\n uid = null //Custom rule\n } = req;\n const query = { ...originQuery };\n delete query.flush;\n if (hashStrategy && !util.isFunction(hashStrategy)) {\n throw new RuntimeException(`View cache hash strategy must be a function for ${originalUrl}`);\n }\n if (!route) {\n throw new RuntimeException(`View cache middleware require route for ${originalUrl}`);\n }\n if (method.toLowerCase() !== 'get') {\n throw new RuntimeException(`View cache middleware only support GET method of http request for ${originalUrl}`);\n }\n const { host = 'unknown' } = req.headers;\n const [urlPath = ''] = originalUrl.split('?');\n const key = [method, `/${host}`, urlPath].join('').replace(/:/g, '_').toLowerCase();\n const strategy = hashStrategy || defaultHashStrategy;\n const hash = crypto\n .createHash('md5')\n .update(JSON\n .stringify(strategy({\n method,\n urlPath,\n query,\n uid\n })))\n .digest('hex');\n return [key, hash].join(':');\n};\n\n\n/**\n * @param {Cache} cache\n * @param {Logger} logger\n * @returns {function(*=)}\n * @constructor\n */\nfunction ViewCacheMiddleware(cache, logger) {\n return (options = {}) => {\n if (!util.isObject(options)) {\n options = { //eslint-disable-line no-param-reassign\n ttl: options\n };\n }\n const {\n ttl,\n headersFilter,\n namespace,\n hashStrategy\n } = Object.assign({\n ttl: 60,\n headersFilter: defaultHeadersFilter,\n hashStrategy: defaultHashStrategy,\n namespace: 'view'\n }, options);\n return wrapper(async (req, res, next) => {\n const cacheKey = requestToCacheKey(req, hashStrategy);\n const {\n headers: cachedHeaders = [],\n body: cachedBody\n } = await cache.namespace(namespace).get(cacheKey)\n || {\n headers: [],\n body: null\n };\n if (req.query.flush !== 'true' && cachedBody) {\n logger.debug('View cache hit by key %s', cacheKey);\n if (cachedHeaders.length > 0) {\n cachedHeaders.forEach(([key, value]) => {\n res.setHeader(key, value);\n });\n }\n res.setHeader('X-View-Cache-Hit', cacheKey);\n res.send(cachedBody);\n return;\n }\n res.realSend = res.send; //eslint-disable-line no-param-reassign\n res.send = (body) => { //eslint-disable-line no-param-reassign\n logger.debug('View cache missed by key %s, creating...', cacheKey);\n res.setHeader('X-View-Cache-Miss', cacheKey);\n res.setHeader('X-View-Cache-Expire-At', moment().add(ttl, 'minute').format('YYYY-MM-DD HH:mm:ss Z'));\n res.setHeader('X-View-Cache-Created-At', moment().format('YYYY-MM-DD HH:mm:ss Z'));\n res.realSend(body);\n const headers = headersFilter && util.isFunction(headersFilter) ?\n headersFilter(res) : defaultHeadersFilter(res);\n if (res.statusCode <= 500) {\n cache.namespace(namespace).set(cacheKey, { headers, body }, ttl).catch((e) => {\n logger.error('View cache set failed for %s', cacheKey, e);\n });\n }\n };\n next();\n });\n };\n}\n\nDependencies(Cache, Logger)(ViewCacheMiddleware);//eslint-disable-line new-cap\n\nexport default ViewCacheMiddleware;\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "evaengine",
|
|
3
|
-
"version": "0.11.
|
|
3
|
+
"version": "0.11.2",
|
|
4
4
|
"description": "EvaEngine for Node.js, a micro service development engine",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"package-lock": false,
|
|
@@ -88,7 +88,7 @@
|
|
|
88
88
|
"moment-timezone": "^0.5.13",
|
|
89
89
|
"morgan": "^1.8.2",
|
|
90
90
|
"mysql": "^2.13.0",
|
|
91
|
-
"node-mocks-http": "
|
|
91
|
+
"node-mocks-http": "~1.6.6",
|
|
92
92
|
"on-headers": "^1.0.1",
|
|
93
93
|
"pug": "^2.0.3",
|
|
94
94
|
"request": "^2.85.0",
|