diff --git a/bower.json b/bower.json index 542a699..c7cfde9 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "superapi-cache", - "version": "0.10.4", + "version": "0.10.5", "homepage": "https://github.com/stephanebachelier/superapi-cache", "authors": [ "Stéphane Bachelier " diff --git a/dist/index.js b/dist/index.js index 92f69cd..2199bb7 100644 --- a/dist/index.js +++ b/dist/index.js @@ -71,6 +71,11 @@ function cache() { return res; } + // exclude binary response from cache + if (['arraybuffer', 'blob'].indexOf(res.responseType) >= -1) { + return res; + } + return config.store.setItem(uuid, { expires: config.maxAge === 0 ? 0 : Date.now() + config.maxAge, data: config.serialize(req, res) diff --git a/dist/umd/superapi-cache.js b/dist/umd/superapi-cache.js index f5818e8..895aaf8 100644 --- a/dist/umd/superapi-cache.js +++ b/dist/umd/superapi-cache.js @@ -127,6 +127,11 @@ return /******/ (function(modules) { // webpackBootstrap return res; } + // exclude binary response from cache + if (['arraybuffer', 'blob'].indexOf(res.responseType) >= -1) { + return res; + } + return config.store.setItem(uuid, { expires: config.maxAge === 0 ? 0 : Date.now() + config.maxAge, data: config.serialize(req, res) diff --git a/dist/umd/superapi-cache.js.map b/dist/umd/superapi-cache.js.map index 7d45aaa..6b027d3 100644 --- a/dist/umd/superapi-cache.js.map +++ b/dist/umd/superapi-cache.js.map @@ -1 +1 @@ -{"version":3,"sources":["webpack:///webpack/universalModuleDefinition","webpack:///webpack/bootstrap 51082eb5422064ad3d1c","webpack:///./lib/index.js","webpack:///./lib/read-cache.js","webpack:///./lib/hydrate.js","webpack:///./lib/serialize.js","webpack:///./lib/memory.js","webpack:///./lib/exclude.js"],"names":[],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,CAAC;AACD,O;ACVA;AACA;;AAEA;AACA;;AAEA;AACA;AACA;;AAEA;AACA;AACA,uBAAe;AACf;AACA;AACA;;AAEA;AACA;;AAEA;AACA;;AAEA;AACA;AACA;;;AAGA;AACA;;AAEA;AACA;;AA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tB,SAAI,IAAI,GAAJ,CAAQ,KAAR,CAAc,MAAd,CAAJ,EAA2B;AACzB,eAAQ,IAAR,CADyB;AAEzB,cAAO,KAAP,CAFyB;MAA3B;IADY,CAAd,CAnB8D;;AA0B9D,OAAI,KAAJ,EAAW;AACT,YAAO,IAAP,CADS;IAAX;;;AA1B8D,UA+BvD,KAAP,CA/B8D;EAAjD","file":"superapi-cache.js","sourcesContent":["(function webpackUniversalModuleDefinition(root, factory) {\n\tif(typeof exports === 'object' && typeof module === 'object')\n\t\tmodule.exports = factory();\n\telse if(typeof define === 'function' && define.amd)\n\t\tdefine(\"superapi-cache\", [], factory);\n\telse if(typeof exports === 'object')\n\t\texports[\"superapi-cache\"] = factory();\n\telse\n\t\troot[\"superapi-cache\"] = factory();\n})(this, function() {\nreturn \n\n\n/** WEBPACK FOOTER **\n ** webpack/universalModuleDefinition\n **/"," \t// The module cache\n \tvar installedModules = {};\n\n \t// The require function\n \tfunction __webpack_require__(moduleId) {\n\n \t\t// Check if module is in cache\n \t\tif(installedModules[moduleId])\n \t\t\treturn installedModules[moduleId].exports;\n\n \t\t// Create a new module (and put it into the cache)\n \t\tvar module = installedModules[moduleId] = {\n \t\t\texports: {},\n \t\t\tid: moduleId,\n \t\t\tloaded: false\n \t\t};\n\n \t\t// Execute the module function\n \t\tmodules[moduleId].call(module.exports, module, module.exports, __webpack_require__);\n\n \t\t// Flag the module as loaded\n \t\tmodule.loaded = true;\n\n \t\t// Return the exports of the module\n \t\treturn module.exports;\n \t}\n\n\n \t// expose the modules object (__webpack_modules__)\n \t__webpack_require__.m = modules;\n\n \t// expose the module cache\n \t__webpack_require__.c = installedModules;\n\n \t// __webpack_public_path__\n \t__webpack_require__.p = \"\";\n\n \t// Load entry module and return exports\n \treturn __webpack_require__(0);\n\n\n\n/** WEBPACK FOOTER **\n ** webpack/bootstrap 51082eb5422064ad3d1c\n **/","'use strict'\n\nimport readCache from './read-cache'\nimport serialize from './serialize'\nimport memoryStore from './memory'\nimport exclude from './exclude'\n\nfunction cache (config = {}) {\n config.store = config.store || memoryStore\n const key = config.key || cache.key\n\n config.maxAge = config.maxAge || 0\n config.readCache = config.readCache || readCache\n config.serialize = config.serialize || serialize\n config.clearOnStale = config.clearOnStale !== undefined ? config.clearOnStale : true\n\n config.exclude = config.exclude || {}\n config.exclude.query = config.exclude.query || true\n config.exclude.paths = config.exclude.paths || []\n config.exclude.filter = null\n\n if (config.log !== false) {\n config.log = typeof config.log === 'function' ? config.log : console.log.bind(console)\n }\n\n return (req, next, service) => {\n if (exclude(req, service, config.exclude)) {\n return null\n }\n\n const uuid = key(req)\n\n // clear cache if method different from GET.\n // We should exclude HEAD\n const method = req.method.toLowerCase()\n\n if (method === 'head') {\n return null\n }\n \n if (method !== 'get') {\n config.store.removeItem(uuid)\n return null\n }\n\n const f = () => {\n return next()\n .then(res => {\n const type = res.status / 100 | 0\n\n // only cache 2xx response\n if (type !== 2) {\n return res\n }\n\n return config.store.setItem(uuid, {\n expires: config.maxAge === 0 ? 0 : Date.now() + config.maxAge,\n data: config.serialize(req, res)\n })\n })\n }\n\n return config.store.getItem(uuid).then(value => {\n return config.readCache(req, config.log)(value)\n .catch(err => {\n // clean up cache if stale\n config.clearOnStale && err.reason === 'cache-stale' ? config.store.removeItem(uuid).then(f) : f()\n })\n })\n }\n}\n\ncache.readCache = readCache\ncache.serialize = serialize\n\ncache.key = function (req) {\n return req.url\n}\n\nexport default cache\n\n\n\n/** WEBPACK FOOTER **\n ** ./lib/index.js\n **/","import hydrate from './hydrate'\n\nexport default function (req, log) {\n return function (value) {\n return new Promise((resolve, reject) => {\n if (!value || !value.data) {\n log('cache-miss', req.url)\n const error = new Error()\n\n error.reason = 'cache-miss'\n error.message = 'Value not found from cache'\n return reject(error)\n }\n\n const { expires, data } = value\n\n if (expires !== 0 && (expires < Date.now())) {\n log('cache-stale', req.url)\n const error = new Error()\n\n error.reason = 'cache-stale'\n error.message = 'Value is stale'\n return reject(error)\n }\n\n // hydrate pseudo xhr from cached value\n req.xhr = hydrate(data)\n\n // override request end callback\n req.callback = (err, res) => {\n log('cache-hit', req.url)\n\n if (err) {\n return reject(err, res)\n }\n\n resolve(res)\n }\n\n req.emit('end')\n })\n }\n}\n\n\n\n/** WEBPACK FOOTER **\n ** ./lib/read-cache.js\n **/","/**\nvalue should have the following format\n{\n body: {\n responseType: '',\n responseText: value,\n status: '',\n statusText: ''\n },\n headers: ''\n}\n*/\n\n// borrow from superagent\nfunction trim (s = '') {\n return ''.trim ? s.trim(s) : s.replace(/(^\\s*|\\s*$)/g, '')\n}\n\n// borrow from superagent\nfunction parseHeaders (str = '') {\n const lines = str.split(/\\r?\\n/)\n let fields = {}\n let index\n let line\n let field\n let val\n\n lines.pop() // trailing CRLF\n\n for (let i = 0, len = lines.length; i < len; i += 1) {\n line = lines[i]\n index = line.indexOf(':')\n field = line.slice(0, index).toLowerCase()\n val = trim(line.slice(index + 1))\n fields[field] = val\n }\n\n return fields\n}\n\nexport default function hydrate (value) {\n const xhr = value.body || {}\n const headers = parseHeaders(value.headers)\n\n xhr.getAllResponseHeaders = function () {\n return value.headers\n }\n\n xhr.getResponseHeader = function (header) {\n return headers[header]\n }\n\n return xhr\n}\n\n\n\n/** WEBPACK FOOTER **\n ** ./lib/hydrate.js\n **/","export default function serialize (req, res) {\n return {\n headers: res.xhr.getAllResponseHeaders(),\n body: {\n responseType: res.xhr.responseType,\n responseText: res.xhr.responseText,\n status: res.status,\n statusText: res.statusText\n }\n }\n}\n\n\n\n/** WEBPACK FOOTER **\n ** ./lib/serialize.js\n **/","class MemoryStore {\n constructor () {\n this._store = {}\n }\n\n getItem (key) {\n return Promise.resolve(this._store[key] || null)\n }\n\n setItem (key, value) {\n this._store[key] = value\n return Promise.resolve(value)\n }\n\n clear () {\n this._store = {}\n return Promise.resolve()\n }\n}\n\nexport default MemoryStore\n\n\n\n/** WEBPACK FOOTER **\n ** ./lib/memory.js\n **/","\nexport default function exclude (req, service, exclusions = {}) {\n if (service) {\n if (service.use && service.use.cache === false) {\n return true\n }\n }\n\n if ((typeof exclusions.filter === 'function') && !exclusions.filter(req)) {\n return true\n }\n\n // do not cache request with query\n if (exclusions.query && req.url.match(/\\?.*$/)) {\n return true\n }\n\n let found = false\n const paths = exclusions.paths || []\n\n paths.forEach(regexp => {\n if (req.url.match(regexp)) {\n found = true\n return found\n }\n })\n\n if (found) {\n return true\n }\n\n // All rules explained. fo not rewrite regexp.\n return false\n}\n\n\n\n/** WEBPACK FOOTER **\n ** ./lib/exclude.js\n **/"],"sourceRoot":""} \ No newline at end of file +{"version":3,"sources":["webpack:///webpack/universalModuleDefinition","webpack:///webpack/bootstrap 240dde1d22a2fd55f2ef","webpack:///./lib/index.js","webpack:///./lib/read-cache.js","webpack:///./lib/hydrate.js","webpack:///./lib/serialize.js","webpack:///./lib/memory.js","webpack:///./lib/exclude.js"],"names":[],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,CAAC;AACD,O;ACVA;AACA;;AAEA;AACA;;AAEA;AACA;AACA;;AAEA;AACA;AACA,uBAAe;AACf;AACA;AACA;;AAEA;AACA;;AAEA;AACA;;AAEA;AACA;AACA;;;AAGA;AACA;;AAEA;AACA;;AAEA;AACA;;AAEA;AACA;;;;;;;ACtCA;;;;;;;;;;;;;;;;;;;;;;;;AAOA,UAAS,KAAT,GAA6B;OAAb,+DAAS,kBAAI;;AAC3B,UAAO,KAAP,GAAe,OAAO,KAAP,oBAAf,CAD2B;AAE3B,OAAM,MAAM,OAAO,GAAP,IAAc,MAAM,GAAN,CAFC;;AAI3B,UAAO,MAAP,GAAgB,OAAO,MAAP,IAAiB,CAAjB,CAJW;AAK3B,UAAO,SAAP,GAAmB,OAAO,SAAP,uBAAnB,CAL2B;AAM3B,UAAO,SAAP,GAAmB,OAAO,SAAP,uBAAnB,CAN2B;AAO3B,UAAO,YAAP,GAAsB,OAAO,YAAP,KAAwB,SAAxB,GAAoC,OAAO,YAAP,GAAsB,IAA1D,CAPK;;AA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,CAAY,GAAZ,KAAoB,IAApB,CAAvB,CADY;;;;6BAIL,KAAK,OAAO;AACnB,YAAK,MAAL,CAAY,GAAZ,IAAmB,KAAnB,CADmB;AAEnB,cAAO,QAAQ,OAAR,CAAgB,KAAhB,CAAP,CAFmB;;;;6BAKZ;AACP,YAAK,MAAL,GAAc,EAAd,CADO;AAEP,cAAO,QAAQ,OAAR,EAAP,CAFO;;;;UAdL;;;mBAoBS;;;;;;;;;;;;mBCnBS;AAAT,UAAS,OAAT,CAAkB,GAAlB,EAAuB,OAAvB,EAAiD;OAAjB,mEAAa,kBAAI;;AAC9D,OAAI,OAAJ,EAAa;AACX,SAAI,QAAQ,GAAR,IAAe,QAAQ,GAAR,CAAY,KAAZ,KAAsB,KAAtB,EAA6B;AAC9C,cAAO,IAAP,CAD8C;MAAhD;IADF;;AAMA,OAAI,OAAQ,WAAW,MAAX,KAAsB,UAA7B,IAA4C,CAAC,WAAW,MAAX,CAAkB,GAAlB,CAAD,EAAyB;AACxE,YAAO,IAAP,CADwE;IAA1E;;;AAP8D,OAY1D,WAAW,KAAX,IAAoB,IAAI,GAAJ,CAAQ,KAAR,CAAc,OAAd,CAApB,EAA4C;AAC9C,YAAO,IAAP,CAD8C;IAAhD;;AAIA,OAAI,QAAQ,KAAR,CAhB0D;AAiB9D,OAAM,QAAQ,WAAW,KAAX,IAAoB,EAApB,CAjBgD;;AAmB9D,SAAM,OAAN,CAAc,kBAAU;AACtB,SAAI,IAAI,GAAJ,CAAQ,KAAR,CAAc,MAAd,CAAJ,EAA2B;AACzB,eAAQ,IAAR,CADyB;AAEzB,cAAO,KAAP,CAFyB;MAA3B;IADY,CAAd,CAnB8D;;AA0B9D,OAAI,KAAJ,EAAW;AACT,YAAO,IAAP,CADS;IAAX;;;AA1B8D,UA+BvD,KAAP,CA/B8D;EAAjD","file":"superapi-cache.js","sourcesContent":["(function webpackUniversalModuleDefinition(root, factory) {\n\tif(typeof exports === 'object' && typeof module === 'object')\n\t\tmodule.exports = factory();\n\telse if(typeof define === 'function' && define.amd)\n\t\tdefine(\"superapi-cache\", [], factory);\n\telse if(typeof exports === 'object')\n\t\texports[\"superapi-cache\"] = factory();\n\telse\n\t\troot[\"superapi-cache\"] = factory();\n})(this, function() {\nreturn \n\n\n/** WEBPACK FOOTER **\n ** webpack/universalModuleDefinition\n **/"," \t// The module cache\n \tvar installedModules = {};\n\n \t// The require function\n \tfunction __webpack_require__(moduleId) {\n\n \t\t// Check if module is in cache\n \t\tif(installedModules[moduleId])\n \t\t\treturn installedModules[moduleId].exports;\n\n \t\t// Create a new module (and put it into the cache)\n \t\tvar module = installedModules[moduleId] = {\n \t\t\texports: {},\n \t\t\tid: moduleId,\n \t\t\tloaded: false\n \t\t};\n\n \t\t// Execute the module function\n \t\tmodules[moduleId].call(module.exports, module, module.exports, __webpack_require__);\n\n \t\t// Flag the module as loaded\n \t\tmodule.loaded = true;\n\n \t\t// Return the exports of the module\n \t\treturn module.exports;\n \t}\n\n\n \t// expose the modules object (__webpack_modules__)\n \t__webpack_require__.m = modules;\n\n \t// expose the module cache\n \t__webpack_require__.c = installedModules;\n\n \t// __webpack_public_path__\n \t__webpack_require__.p = \"\";\n\n \t// Load entry module and return exports\n \treturn __webpack_require__(0);\n\n\n\n/** WEBPACK FOOTER **\n ** webpack/bootstrap 240dde1d22a2fd55f2ef\n **/","'use strict'\n\nimport readCache from './read-cache'\nimport serialize from './serialize'\nimport memoryStore from './memory'\nimport exclude from './exclude'\n\nfunction cache (config = {}) {\n config.store = config.store || memoryStore\n const key = config.key || cache.key\n\n config.maxAge = config.maxAge || 0\n config.readCache = config.readCache || readCache\n config.serialize = config.serialize || serialize\n config.clearOnStale = config.clearOnStale !== undefined ? config.clearOnStale : true\n\n config.exclude = config.exclude || {}\n config.exclude.query = config.exclude.query || true\n config.exclude.paths = config.exclude.paths || []\n config.exclude.filter = null\n\n if (config.log !== false) {\n config.log = typeof config.log === 'function' ? config.log : console.log.bind(console)\n }\n\n return (req, next, service) => {\n if (exclude(req, service, config.exclude)) {\n return null\n }\n\n const uuid = key(req)\n\n // clear cache if method different from GET.\n // We should exclude HEAD\n const method = req.method.toLowerCase()\n\n if (method === 'head') {\n return null\n }\n \n if (method !== 'get') {\n config.store.removeItem(uuid)\n return null\n }\n\n const f = () => {\n return next()\n .then(res => {\n const type = res.status / 100 | 0\n\n // only cache 2xx response\n if (type !== 2) {\n return res\n }\n\n // exclude binary response from cache\n if (['arraybuffer', 'blob'].indexOf(res.responseType) >= -1) {\n return res;\n }\n\n return config.store.setItem(uuid, {\n expires: config.maxAge === 0 ? 0 : Date.now() + config.maxAge,\n data: config.serialize(req, res)\n })\n })\n }\n\n return config.store.getItem(uuid).then(value => {\n return config.readCache(req, config.log)(value)\n .catch(err => {\n // clean up cache if stale\n config.clearOnStale && err.reason === 'cache-stale' ? config.store.removeItem(uuid).then(f) : f()\n })\n })\n }\n}\n\ncache.readCache = readCache\ncache.serialize = serialize\n\ncache.key = function (req) {\n return req.url\n}\n\nexport default cache\n\n\n\n/** WEBPACK FOOTER **\n ** ./lib/index.js\n **/","import hydrate from './hydrate'\n\nexport default function (req, log) {\n return function (value) {\n return new Promise((resolve, reject) => {\n if (!value || !value.data) {\n log('cache-miss', req.url)\n const error = new Error()\n\n error.reason = 'cache-miss'\n error.message = 'Value not found from cache'\n return reject(error)\n }\n\n const { expires, data } = value\n\n if (expires !== 0 && (expires < Date.now())) {\n log('cache-stale', req.url)\n const error = new Error()\n\n error.reason = 'cache-stale'\n error.message = 'Value is stale'\n return reject(error)\n }\n\n // hydrate pseudo xhr from cached value\n req.xhr = hydrate(data)\n\n // override request end callback\n req.callback = (err, res) => {\n log('cache-hit', req.url)\n\n if (err) {\n return reject(err, res)\n }\n\n resolve(res)\n }\n\n req.emit('end')\n })\n }\n}\n\n\n\n/** WEBPACK FOOTER **\n ** ./lib/read-cache.js\n **/","/**\nvalue should have the following format\n{\n body: {\n responseType: '',\n responseText: value,\n status: '',\n statusText: ''\n },\n headers: ''\n}\n*/\n\n// borrow from superagent\nfunction trim (s = '') {\n return ''.trim ? s.trim(s) : s.replace(/(^\\s*|\\s*$)/g, '')\n}\n\n// borrow from superagent\nfunction parseHeaders (str = '') {\n const lines = str.split(/\\r?\\n/)\n let fields = {}\n let index\n let line\n let field\n let val\n\n lines.pop() // trailing CRLF\n\n for (let i = 0, len = lines.length; i < len; i += 1) {\n line = lines[i]\n index = line.indexOf(':')\n field = line.slice(0, index).toLowerCase()\n val = trim(line.slice(index + 1))\n fields[field] = val\n }\n\n return fields\n}\n\nexport default function hydrate (value) {\n const xhr = value.body || {}\n const headers = parseHeaders(value.headers)\n\n xhr.getAllResponseHeaders = function () {\n return value.headers\n }\n\n xhr.getResponseHeader = function (header) {\n return headers[header]\n }\n\n return xhr\n}\n\n\n\n/** WEBPACK FOOTER **\n ** ./lib/hydrate.js\n **/","export default function serialize (req, res) {\n return {\n headers: res.xhr.getAllResponseHeaders(),\n body: {\n responseType: res.xhr.responseType,\n responseText: res.xhr.responseText,\n status: res.status,\n statusText: res.statusText\n }\n }\n}\n\n\n\n/** WEBPACK FOOTER **\n ** ./lib/serialize.js\n **/","class MemoryStore {\n constructor () {\n this._store = {}\n }\n\n getItem (key) {\n return Promise.resolve(this._store[key] || null)\n }\n\n setItem (key, value) {\n this._store[key] = value\n return Promise.resolve(value)\n }\n\n clear () {\n this._store = {}\n return Promise.resolve()\n }\n}\n\nexport default MemoryStore\n\n\n\n/** WEBPACK FOOTER **\n ** ./lib/memory.js\n **/","\nexport default function exclude (req, service, exclusions = {}) {\n if (service) {\n if (service.use && service.use.cache === false) {\n return true\n }\n }\n\n if ((typeof exclusions.filter === 'function') && !exclusions.filter(req)) {\n return true\n }\n\n // do not cache request with query\n if (exclusions.query && req.url.match(/\\?.*$/)) {\n return true\n }\n\n let found = false\n const paths = exclusions.paths || []\n\n paths.forEach(regexp => {\n if (req.url.match(regexp)) {\n found = true\n return found\n }\n })\n\n if (found) {\n return true\n }\n\n // All rules explained. fo not rewrite regexp.\n return false\n}\n\n\n\n/** WEBPACK FOOTER **\n ** ./lib/exclude.js\n **/"],"sourceRoot":""} \ No newline at end of file diff --git a/dist/umd/superapi-cache.min.js b/dist/umd/superapi-cache.min.js index fbe64b9..8d32956 100644 --- a/dist/umd/superapi-cache.min.js +++ b/dist/umd/superapi-cache.min.js @@ -1,2 +1,2 @@ -!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define("superapi-cache",[],t):"object"==typeof exports?exports["superapi-cache"]=t():e["superapi-cache"]=t()}(this,function(){return function(e){function t(n){if(r[n])return r[n].exports;var u=r[n]={exports:{},id:n,loaded:!1};return e[n].call(u.exports,u,u.exports,t),u.loaded=!0,u.exports}var r={};return t.m=e,t.c=r,t.p="",t(0)}([function(e,t,r){"use strict";function n(e){return e&&e.__esModule?e:{"default":e}}function u(){var e=arguments.length<=0||void 0===arguments[0]?{}:arguments[0];e.store=e.store||c["default"];var t=e.key||u.key;return e.maxAge=e.maxAge||0,e.readCache=e.readCache||o["default"],e.serialize=e.serialize||l["default"],e.clearOnStale=void 0!==e.clearOnStale?e.clearOnStale:!0,e.exclude=e.exclude||{},e.exclude.query=e.exclude.query||!0,e.exclude.paths=e.exclude.paths||[],e.exclude.filter=null,e.log!==!1&&(e.log="function"==typeof e.log?e.log:console.log.bind(console)),function(r,n,u){if((0,d["default"])(r,u,e.exclude))return null;var a=t(r),o=r.method.toLowerCase();if("head"===o)return null;if("get"!==o)return e.store.removeItem(a),null;var s=function(){return n().then(function(t){var n=t.status/100|0;return 2!==n?t:e.store.setItem(a,{expires:0===e.maxAge?0:Date.now()+e.maxAge,data:e.serialize(r,t)})})};return e.store.getItem(a).then(function(t){return e.readCache(r,e.log)(t)["catch"](function(t){e.clearOnStale&&"cache-stale"===t.reason?e.store.removeItem(a).then(s):s()})})}}Object.defineProperty(t,"__esModule",{value:!0});var a=r(1),o=n(a),s=r(3),l=n(s),i=r(4),c=n(i),f=r(5),d=n(f);u.readCache=o["default"],u.serialize=l["default"],u.key=function(e){return e.url},t["default"]=u,e.exports=t["default"]},function(e,t,r){"use strict";function n(e){return e&&e.__esModule?e:{"default":e}}Object.defineProperty(t,"__esModule",{value:!0}),t["default"]=function(e,t){return function(r){return new Promise(function(n,u){if(!r||!r.data){t("cache-miss",e.url);var o=new Error;return o.reason="cache-miss",o.message="Value not found from cache",u(o)}var s=r.expires,l=r.data;if(0!==s&&sl;l+=1)a=t[l],u=a.indexOf(":"),o=a.slice(0,u).toLowerCase(),s=r(a.slice(u+1)),n[o]=s;return n}function u(e){var t=e.body||{},r=n(e.headers);return t.getAllResponseHeaders=function(){return e.headers},t.getResponseHeader=function(e){return r[e]},t}Object.defineProperty(t,"__esModule",{value:!0}),t["default"]=u,e.exports=t["default"]},function(e,t){"use strict";function r(e,t){return{headers:t.xhr.getAllResponseHeaders(),body:{responseType:t.xhr.responseType,responseText:t.xhr.responseText,status:t.status,statusText:t.statusText}}}Object.defineProperty(t,"__esModule",{value:!0}),t["default"]=r,e.exports=t["default"]},function(e,t){"use strict";function r(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}var n=function(){function e(e,t){for(var r=0;r=-1?t:e.store.setItem(a,{expires:0===e.maxAge?0:Date.now()+e.maxAge,data:e.serialize(r,t)})})};return e.store.getItem(a).then(function(t){return e.readCache(r,e.log)(t)["catch"](function(t){e.clearOnStale&&"cache-stale"===t.reason?e.store.removeItem(a).then(s):s()})})}}Object.defineProperty(t,"__esModule",{value:!0});var a=r(1),o=n(a),s=r(3),l=n(s),i=r(4),c=n(i),f=r(5),d=n(f);u.readCache=o["default"],u.serialize=l["default"],u.key=function(e){return e.url},t["default"]=u,e.exports=t["default"]},function(e,t,r){"use strict";function n(e){return e&&e.__esModule?e:{"default":e}}Object.defineProperty(t,"__esModule",{value:!0}),t["default"]=function(e,t){return function(r){return new Promise(function(n,u){if(!r||!r.data){t("cache-miss",e.url);var o=new Error;return o.reason="cache-miss",o.message="Value not found from cache",u(o)}var s=r.expires,l=r.data;if(0!==s&&sl;l+=1)a=t[l],u=a.indexOf(":"),o=a.slice(0,u).toLowerCase(),s=r(a.slice(u+1)),n[o]=s;return n}function u(e){var t=e.body||{},r=n(e.headers);return t.getAllResponseHeaders=function(){return e.headers},t.getResponseHeader=function(e){return r[e]},t}Object.defineProperty(t,"__esModule",{value:!0}),t["default"]=u,e.exports=t["default"]},function(e,t){"use strict";function r(e,t){return{headers:t.xhr.getAllResponseHeaders(),body:{responseType:t.xhr.responseType,responseText:t.xhr.responseText,status:t.status,statusText:t.statusText}}}Object.defineProperty(t,"__esModule",{value:!0}),t["default"]=r,e.exports=t["default"]},function(e,t){"use strict";function r(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}var n=function(){function e(e,t){for(var r=0;r {\n if (exclude(req, service, config.exclude)) {\n return null\n }\n\n const uuid = key(req)\n\n // clear cache if method different from GET.\n // We should exclude HEAD\n const method = req.method.toLowerCase()\n\n if (method === 'head') {\n return null\n }\n \n if (method !== 'get') {\n config.store.removeItem(uuid)\n return null\n }\n\n const f = () => {\n return next()\n .then(res => {\n const type = res.status / 100 | 0\n\n // only cache 2xx response\n if (type !== 2) {\n return res\n }\n\n return config.store.setItem(uuid, {\n expires: config.maxAge === 0 ? 0 : Date.now() + config.maxAge,\n data: config.serialize(req, res)\n })\n })\n }\n\n return config.store.getItem(uuid).then(value => {\n return config.readCache(req, config.log)(value)\n .catch(err => {\n // clean up cache if stale\n config.clearOnStale && err.reason === 'cache-stale' ? config.store.removeItem(uuid).then(f) : f()\n })\n })\n }\n}\n\ncache.readCache = readCache\ncache.serialize = serialize\n\ncache.key = function (req) {\n return req.url\n}\n\nexport default cache\n\n\n\n/** WEBPACK FOOTER **\n ** ./lib/index.js\n **/","import hydrate from './hydrate'\n\nexport default function (req, log) {\n return function (value) {\n return new Promise((resolve, reject) => {\n if (!value || !value.data) {\n log('cache-miss', req.url)\n const error = new Error()\n\n error.reason = 'cache-miss'\n error.message = 'Value not found from cache'\n return reject(error)\n }\n\n const { expires, data } = value\n\n if (expires !== 0 && (expires < Date.now())) {\n log('cache-stale', req.url)\n const error = new Error()\n\n error.reason = 'cache-stale'\n error.message = 'Value is stale'\n return reject(error)\n }\n\n // hydrate pseudo xhr from cached value\n req.xhr = hydrate(data)\n\n // override request end callback\n req.callback = (err, res) => {\n log('cache-hit', req.url)\n\n if (err) {\n return reject(err, res)\n }\n\n resolve(res)\n }\n\n req.emit('end')\n })\n }\n}\n\n\n\n/** WEBPACK FOOTER **\n ** ./lib/read-cache.js\n **/","/**\nvalue should have the following format\n{\n body: {\n responseType: '',\n responseText: value,\n status: '',\n statusText: ''\n },\n headers: ''\n}\n*/\n\n// borrow from superagent\nfunction trim (s = '') {\n return ''.trim ? s.trim(s) : s.replace(/(^\\s*|\\s*$)/g, '')\n}\n\n// borrow from superagent\nfunction parseHeaders (str = '') {\n const lines = str.split(/\\r?\\n/)\n let fields = {}\n let index\n let line\n let field\n let val\n\n lines.pop() // trailing CRLF\n\n for (let i = 0, len = lines.length; i < len; i += 1) {\n line = lines[i]\n index = line.indexOf(':')\n field = line.slice(0, index).toLowerCase()\n val = trim(line.slice(index + 1))\n fields[field] = val\n }\n\n return fields\n}\n\nexport default function hydrate (value) {\n const xhr = value.body || {}\n const headers = parseHeaders(value.headers)\n\n xhr.getAllResponseHeaders = function () {\n return value.headers\n }\n\n xhr.getResponseHeader = function (header) {\n return headers[header]\n }\n\n return xhr\n}\n\n\n\n/** WEBPACK FOOTER **\n ** ./lib/hydrate.js\n **/","export default function serialize (req, res) {\n return {\n headers: res.xhr.getAllResponseHeaders(),\n body: {\n responseType: res.xhr.responseType,\n responseText: res.xhr.responseText,\n status: res.status,\n statusText: res.statusText\n }\n }\n}\n\n\n\n/** WEBPACK FOOTER **\n ** ./lib/serialize.js\n **/","class MemoryStore {\n constructor () {\n this._store = {}\n }\n\n getItem (key) {\n return Promise.resolve(this._store[key] || null)\n }\n\n setItem (key, value) {\n this._store[key] = value\n return Promise.resolve(value)\n }\n\n clear () {\n this._store = {}\n return Promise.resolve()\n }\n}\n\nexport default MemoryStore\n\n\n\n/** WEBPACK FOOTER **\n ** ./lib/memory.js\n **/","\nexport default function exclude (req, service, exclusions = {}) {\n if (service) {\n if (service.use && service.use.cache === false) {\n return true\n }\n }\n\n if ((typeof exclusions.filter === 'function') && !exclusions.filter(req)) {\n return true\n }\n\n // do not cache request with query\n if (exclusions.query && req.url.match(/\\?.*$/)) {\n return true\n }\n\n let found = false\n const paths = exclusions.paths || []\n\n paths.forEach(regexp => {\n if (req.url.match(regexp)) {\n found = true\n return found\n }\n })\n\n if (found) {\n return true\n }\n\n // All rules explained. fo not rewrite regexp.\n return false\n}\n\n\n\n/** WEBPACK FOOTER **\n ** ./lib/exclude.js\n **/"],"sourceRoot":""} \ No newline at end of file +{"version":3,"sources":["webpack:///webpack/universalModuleDefinition","webpack:///superapi-cache.min.js","webpack:///webpack/bootstrap 94aea5894f5971abe040","webpack:///./lib/index.js","webpack:///./lib/read-cache.js","webpack:///./lib/hydrate.js","webpack:///./lib/serialize.js","webpack:///./lib/memory.js","webpack:///./lib/exclude.js"],"names":["root","factory","exports","module","define","amd","this","modules","__webpack_require__","moduleId","installedModules","id","loaded","call","m","c","p","_interopRequireDefault","obj","__esModule","default","cache","config","arguments","length","undefined","store","_memory2","key","maxAge","readCache","_readCache2","serialize","_serialize2","clearOnStale","exclude","query","paths","filter","log","console","bind","req","next","service","_exclude2","uuid","method","toLowerCase","removeItem","f","then","res","type","status","indexOf","responseType","setItem","expires","Date","now","data","getItem","value","err","reason","Object","defineProperty","_readCache","_serialize","_memory","_exclude","url","Promise","resolve","reject","error","Error","message","xhr","_hydrate2","callback","emit","_hydrate","trim","s","replace","parseHeaders","str","lines","split","fields","index","line","field","val","pop","i","len","slice","hydrate","body","headers","getAllResponseHeaders","getResponseHeader","header","responseText","statusText","_classCallCheck","instance","Constructor","TypeError","_createClass","defineProperties","target","props","descriptor","enumerable","configurable","writable","protoProps","staticProps","prototype","MemoryStore","_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":"superapi-cache.min.js","sourcesContent":["(function webpackUniversalModuleDefinition(root, factory) {\n\tif(typeof exports === 'object' && typeof module === 'object')\n\t\tmodule.exports = factory();\n\telse if(typeof define === 'function' && define.amd)\n\t\tdefine(\"superapi-cache\", [], factory);\n\telse if(typeof exports === 'object')\n\t\texports[\"superapi-cache\"] = factory();\n\telse\n\t\troot[\"superapi-cache\"] = factory();\n})(this, function() {\nreturn \n\n\n/** WEBPACK FOOTER **\n ** webpack/universalModuleDefinition\n **/","(function webpackUniversalModuleDefinition(root, factory) {\n\tif(typeof exports === 'object' && typeof module === 'object')\n\t\tmodule.exports = factory();\n\telse if(typeof define === 'function' && define.amd)\n\t\tdefine(\"superapi-cache\", [], factory);\n\telse if(typeof exports === 'object')\n\t\texports[\"superapi-cache\"] = factory();\n\telse\n\t\troot[\"superapi-cache\"] = factory();\n})(this, function() {\nreturn /******/ (function(modules) { // webpackBootstrap\n/******/ \t// The module cache\n/******/ \tvar installedModules = {};\n/******/\n/******/ \t// The require function\n/******/ \tfunction __webpack_require__(moduleId) {\n/******/\n/******/ \t\t// Check if module is in cache\n/******/ \t\tif(installedModules[moduleId])\n/******/ \t\t\treturn installedModules[moduleId].exports;\n/******/\n/******/ \t\t// Create a new module (and put it into the cache)\n/******/ \t\tvar module = installedModules[moduleId] = {\n/******/ \t\t\texports: {},\n/******/ \t\t\tid: moduleId,\n/******/ \t\t\tloaded: false\n/******/ \t\t};\n/******/\n/******/ \t\t// Execute the module function\n/******/ \t\tmodules[moduleId].call(module.exports, module, module.exports, __webpack_require__);\n/******/\n/******/ \t\t// Flag the module as loaded\n/******/ \t\tmodule.loaded = true;\n/******/\n/******/ \t\t// Return the exports of the module\n/******/ \t\treturn module.exports;\n/******/ \t}\n/******/\n/******/\n/******/ \t// expose the modules object (__webpack_modules__)\n/******/ \t__webpack_require__.m = modules;\n/******/\n/******/ \t// expose the module cache\n/******/ \t__webpack_require__.c = installedModules;\n/******/\n/******/ \t// __webpack_public_path__\n/******/ \t__webpack_require__.p = \"\";\n/******/\n/******/ \t// Load entry module and return exports\n/******/ \treturn __webpack_require__(0);\n/******/ })\n/************************************************************************/\n/******/ ([\n/* 0 */\n/***/ function(module, exports, __webpack_require__) {\n\n\t'use strict';\n\t\n\tObject.defineProperty(exports, \"__esModule\", {\n\t value: true\n\t});\n\t\n\tvar _readCache = __webpack_require__(1);\n\t\n\tvar _readCache2 = _interopRequireDefault(_readCache);\n\t\n\tvar _serialize = __webpack_require__(3);\n\t\n\tvar _serialize2 = _interopRequireDefault(_serialize);\n\t\n\tvar _memory = __webpack_require__(4);\n\t\n\tvar _memory2 = _interopRequireDefault(_memory);\n\t\n\tvar _exclude = __webpack_require__(5);\n\t\n\tvar _exclude2 = _interopRequireDefault(_exclude);\n\t\n\tfunction _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }\n\t\n\tfunction cache() {\n\t var config = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];\n\t\n\t config.store = config.store || _memory2.default;\n\t var key = config.key || cache.key;\n\t\n\t config.maxAge = config.maxAge || 0;\n\t config.readCache = config.readCache || _readCache2.default;\n\t config.serialize = config.serialize || _serialize2.default;\n\t config.clearOnStale = config.clearOnStale !== undefined ? config.clearOnStale : true;\n\t\n\t config.exclude = config.exclude || {};\n\t config.exclude.query = config.exclude.query || true;\n\t config.exclude.paths = config.exclude.paths || [];\n\t config.exclude.filter = null;\n\t\n\t if (config.log !== false) {\n\t config.log = typeof config.log === 'function' ? config.log : console.log.bind(console);\n\t }\n\t\n\t return function (req, next, service) {\n\t if ((0, _exclude2.default)(req, service, config.exclude)) {\n\t return null;\n\t }\n\t\n\t var uuid = key(req);\n\t\n\t // clear cache if method different from GET.\n\t // We should exclude HEAD\n\t var method = req.method.toLowerCase();\n\t\n\t if (method === 'head') {\n\t return null;\n\t }\n\t\n\t if (method !== 'get') {\n\t config.store.removeItem(uuid);\n\t return null;\n\t }\n\t\n\t var f = function f() {\n\t return next().then(function (res) {\n\t var type = res.status / 100 | 0;\n\t\n\t // only cache 2xx response\n\t if (type !== 2) {\n\t return res;\n\t }\n\t\n\t // exclude binary response from cache\n\t if (['arraybuffer', 'blob'].indexOf(res.responseType) >= -1) {\n\t return res;\n\t }\n\t\n\t return config.store.setItem(uuid, {\n\t expires: config.maxAge === 0 ? 0 : Date.now() + config.maxAge,\n\t data: config.serialize(req, res)\n\t });\n\t });\n\t };\n\t\n\t return config.store.getItem(uuid).then(function (value) {\n\t return config.readCache(req, config.log)(value).catch(function (err) {\n\t // clean up cache if stale\n\t config.clearOnStale && err.reason === 'cache-stale' ? config.store.removeItem(uuid).then(f) : f();\n\t });\n\t });\n\t };\n\t}\n\t\n\tcache.readCache = _readCache2.default;\n\tcache.serialize = _serialize2.default;\n\t\n\tcache.key = function (req) {\n\t return req.url;\n\t};\n\t\n\texports.default = cache;\n\tmodule.exports = exports['default'];\n\n/***/ },\n/* 1 */\n/***/ function(module, exports, __webpack_require__) {\n\n\t'use strict';\n\t\n\tObject.defineProperty(exports, \"__esModule\", {\n\t value: true\n\t});\n\t\n\texports.default = function (req, log) {\n\t return function (value) {\n\t return new Promise(function (resolve, reject) {\n\t if (!value || !value.data) {\n\t log('cache-miss', req.url);\n\t var error = new Error();\n\t\n\t error.reason = 'cache-miss';\n\t error.message = 'Value not found from cache';\n\t return reject(error);\n\t }\n\t\n\t var expires = value.expires;\n\t var data = value.data;\n\t\n\t if (expires !== 0 && expires < Date.now()) {\n\t log('cache-stale', req.url);\n\t var error = new Error();\n\t\n\t error.reason = 'cache-stale';\n\t error.message = 'Value is stale';\n\t return reject(error);\n\t }\n\t\n\t // hydrate pseudo xhr from cached value\n\t req.xhr = (0, _hydrate2.default)(data);\n\t\n\t // override request end callback\n\t req.callback = function (err, res) {\n\t log('cache-hit', req.url);\n\t\n\t if (err) {\n\t return reject(err, res);\n\t }\n\t\n\t resolve(res);\n\t };\n\t\n\t req.emit('end');\n\t });\n\t };\n\t};\n\t\n\tvar _hydrate = __webpack_require__(2);\n\n\tvar _hydrate2 = _interopRequireDefault(_hydrate);\n\n\tfunction _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }\n\n\tmodule.exports = exports['default'];\n\n/***/ },\n/* 2 */\n/***/ function(module, exports) {\n\n\t'use strict';\n\t\n\tObject.defineProperty(exports, \"__esModule\", {\n\t value: true\n\t});\n\texports.default = hydrate;\n\t/**\n\tvalue should have the following format\n\t{\n\t body: {\n\t responseType: '',\n\t responseText: value,\n\t status: '',\n\t statusText: ''\n\t },\n\t headers: ''\n\t}\n\t*/\n\t\n\t// borrow from superagent\n\tfunction trim() {\n\t var s = arguments.length <= 0 || arguments[0] === undefined ? '' : arguments[0];\n\t\n\t return ''.trim ? s.trim(s) : s.replace(/(^\\s*|\\s*$)/g, '');\n\t}\n\t\n\t// borrow from superagent\n\tfunction parseHeaders() {\n\t var str = arguments.length <= 0 || arguments[0] === undefined ? '' : arguments[0];\n\t\n\t var lines = str.split(/\\r?\\n/);\n\t var fields = {};\n\t var index = undefined;\n\t var line = undefined;\n\t var field = undefined;\n\t var val = undefined;\n\t\n\t lines.pop(); // trailing CRLF\n\t\n\t for (var i = 0, len = lines.length; i < len; i += 1) {\n\t line = lines[i];\n\t index = line.indexOf(':');\n\t field = line.slice(0, index).toLowerCase();\n\t val = trim(line.slice(index + 1));\n\t fields[field] = val;\n\t }\n\t\n\t return fields;\n\t}\n\t\n\tfunction hydrate(value) {\n\t var xhr = value.body || {};\n\t var headers = parseHeaders(value.headers);\n\t\n\t xhr.getAllResponseHeaders = function () {\n\t return value.headers;\n\t };\n\t\n\t xhr.getResponseHeader = function (header) {\n\t return headers[header];\n\t };\n\t\n\t return xhr;\n\t}\n\tmodule.exports = exports['default'];\n\n/***/ },\n/* 3 */\n/***/ function(module, exports) {\n\n\t\"use strict\";\n\t\n\tObject.defineProperty(exports, \"__esModule\", {\n\t value: true\n\t});\n\texports.default = serialize;\n\tfunction serialize(req, res) {\n\t return {\n\t headers: res.xhr.getAllResponseHeaders(),\n\t body: {\n\t responseType: res.xhr.responseType,\n\t responseText: res.xhr.responseText,\n\t status: res.status,\n\t statusText: res.statusText\n\t }\n\t };\n\t}\n\tmodule.exports = exports['default'];\n\n/***/ },\n/* 4 */\n/***/ function(module, exports) {\n\n\t\"use strict\";\n\t\n\tvar _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if (\"value\" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();\n\t\n\tObject.defineProperty(exports, \"__esModule\", {\n\t value: true\n\t});\n\t\n\tfunction _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError(\"Cannot call a class as a function\"); } }\n\t\n\tvar MemoryStore = function () {\n\t function MemoryStore() {\n\t _classCallCheck(this, MemoryStore);\n\t\n\t this._store = {};\n\t }\n\t\n\t _createClass(MemoryStore, [{\n\t key: \"getItem\",\n\t value: function getItem(key) {\n\t return Promise.resolve(this._store[key] || null);\n\t }\n\t }, {\n\t key: \"setItem\",\n\t value: function setItem(key, value) {\n\t this._store[key] = value;\n\t return Promise.resolve(value);\n\t }\n\t }, {\n\t key: \"clear\",\n\t value: function clear() {\n\t this._store = {};\n\t return Promise.resolve();\n\t }\n\t }]);\n\t\n\t return MemoryStore;\n\t}();\n\t\n\texports.default = MemoryStore;\n\tmodule.exports = exports['default'];\n\n/***/ },\n/* 5 */\n/***/ function(module, exports) {\n\n\t'use strict';\n\t\n\tObject.defineProperty(exports, \"__esModule\", {\n\t value: true\n\t});\n\texports.default = exclude;\n\tfunction exclude(req, service) {\n\t var exclusions = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2];\n\t\n\t if (service) {\n\t if (service.use && service.use.cache === false) {\n\t return true;\n\t }\n\t }\n\t\n\t if (typeof exclusions.filter === 'function' && !exclusions.filter(req)) {\n\t return true;\n\t }\n\t\n\t // do not cache request with query\n\t if (exclusions.query && req.url.match(/\\?.*$/)) {\n\t return true;\n\t }\n\t\n\t var found = false;\n\t var paths = exclusions.paths || [];\n\t\n\t paths.forEach(function (regexp) {\n\t if (req.url.match(regexp)) {\n\t found = true;\n\t return found;\n\t }\n\t });\n\t\n\t if (found) {\n\t return true;\n\t }\n\t\n\t // All rules explained. fo not rewrite regexp.\n\t return false;\n\t}\n\tmodule.exports = exports['default'];\n\n/***/ }\n/******/ ])\n});\n;\n\n\n/** WEBPACK FOOTER **\n ** superapi-cache.min.js\n **/"," \t// The module cache\n \tvar installedModules = {};\n\n \t// The require function\n \tfunction __webpack_require__(moduleId) {\n\n \t\t// Check if module is in cache\n \t\tif(installedModules[moduleId])\n \t\t\treturn installedModules[moduleId].exports;\n\n \t\t// Create a new module (and put it into the cache)\n \t\tvar module = installedModules[moduleId] = {\n \t\t\texports: {},\n \t\t\tid: moduleId,\n \t\t\tloaded: false\n \t\t};\n\n \t\t// Execute the module function\n \t\tmodules[moduleId].call(module.exports, module, module.exports, __webpack_require__);\n\n \t\t// Flag the module as loaded\n \t\tmodule.loaded = true;\n\n \t\t// Return the exports of the module\n \t\treturn module.exports;\n \t}\n\n\n \t// expose the modules object (__webpack_modules__)\n \t__webpack_require__.m = modules;\n\n \t// expose the module cache\n \t__webpack_require__.c = installedModules;\n\n \t// __webpack_public_path__\n \t__webpack_require__.p = \"\";\n\n \t// Load entry module and return exports\n \treturn __webpack_require__(0);\n\n\n\n/** WEBPACK FOOTER **\n ** webpack/bootstrap 94aea5894f5971abe040\n **/","'use strict'\n\nimport readCache from './read-cache'\nimport serialize from './serialize'\nimport memoryStore from './memory'\nimport exclude from './exclude'\n\nfunction cache (config = {}) {\n config.store = config.store || memoryStore\n const key = config.key || cache.key\n\n config.maxAge = config.maxAge || 0\n config.readCache = config.readCache || readCache\n config.serialize = config.serialize || serialize\n config.clearOnStale = config.clearOnStale !== undefined ? config.clearOnStale : true\n\n config.exclude = config.exclude || {}\n config.exclude.query = config.exclude.query || true\n config.exclude.paths = config.exclude.paths || []\n config.exclude.filter = null\n\n if (config.log !== false) {\n config.log = typeof config.log === 'function' ? config.log : console.log.bind(console)\n }\n\n return (req, next, service) => {\n if (exclude(req, service, config.exclude)) {\n return null\n }\n\n const uuid = key(req)\n\n // clear cache if method different from GET.\n // We should exclude HEAD\n const method = req.method.toLowerCase()\n\n if (method === 'head') {\n return null\n }\n \n if (method !== 'get') {\n config.store.removeItem(uuid)\n return null\n }\n\n const f = () => {\n return next()\n .then(res => {\n const type = res.status / 100 | 0\n\n // only cache 2xx response\n if (type !== 2) {\n return res\n }\n\n // exclude binary response from cache\n if (['arraybuffer', 'blob'].indexOf(res.responseType) >= -1) {\n return res;\n }\n\n return config.store.setItem(uuid, {\n expires: config.maxAge === 0 ? 0 : Date.now() + config.maxAge,\n data: config.serialize(req, res)\n })\n })\n }\n\n return config.store.getItem(uuid).then(value => {\n return config.readCache(req, config.log)(value)\n .catch(err => {\n // clean up cache if stale\n config.clearOnStale && err.reason === 'cache-stale' ? config.store.removeItem(uuid).then(f) : f()\n })\n })\n }\n}\n\ncache.readCache = readCache\ncache.serialize = serialize\n\ncache.key = function (req) {\n return req.url\n}\n\nexport default cache\n\n\n\n/** WEBPACK FOOTER **\n ** ./lib/index.js\n **/","import hydrate from './hydrate'\n\nexport default function (req, log) {\n return function (value) {\n return new Promise((resolve, reject) => {\n if (!value || !value.data) {\n log('cache-miss', req.url)\n const error = new Error()\n\n error.reason = 'cache-miss'\n error.message = 'Value not found from cache'\n return reject(error)\n }\n\n const { expires, data } = value\n\n if (expires !== 0 && (expires < Date.now())) {\n log('cache-stale', req.url)\n const error = new Error()\n\n error.reason = 'cache-stale'\n error.message = 'Value is stale'\n return reject(error)\n }\n\n // hydrate pseudo xhr from cached value\n req.xhr = hydrate(data)\n\n // override request end callback\n req.callback = (err, res) => {\n log('cache-hit', req.url)\n\n if (err) {\n return reject(err, res)\n }\n\n resolve(res)\n }\n\n req.emit('end')\n })\n }\n}\n\n\n\n/** WEBPACK FOOTER **\n ** ./lib/read-cache.js\n **/","/**\nvalue should have the following format\n{\n body: {\n responseType: '',\n responseText: value,\n status: '',\n statusText: ''\n },\n headers: ''\n}\n*/\n\n// borrow from superagent\nfunction trim (s = '') {\n return ''.trim ? s.trim(s) : s.replace(/(^\\s*|\\s*$)/g, '')\n}\n\n// borrow from superagent\nfunction parseHeaders (str = '') {\n const lines = str.split(/\\r?\\n/)\n let fields = {}\n let index\n let line\n let field\n let val\n\n lines.pop() // trailing CRLF\n\n for (let i = 0, len = lines.length; i < len; i += 1) {\n line = lines[i]\n index = line.indexOf(':')\n field = line.slice(0, index).toLowerCase()\n val = trim(line.slice(index + 1))\n fields[field] = val\n }\n\n return fields\n}\n\nexport default function hydrate (value) {\n const xhr = value.body || {}\n const headers = parseHeaders(value.headers)\n\n xhr.getAllResponseHeaders = function () {\n return value.headers\n }\n\n xhr.getResponseHeader = function (header) {\n return headers[header]\n }\n\n return xhr\n}\n\n\n\n/** WEBPACK FOOTER **\n ** ./lib/hydrate.js\n **/","export default function serialize (req, res) {\n return {\n headers: res.xhr.getAllResponseHeaders(),\n body: {\n responseType: res.xhr.responseType,\n responseText: res.xhr.responseText,\n status: res.status,\n statusText: res.statusText\n }\n }\n}\n\n\n\n/** WEBPACK FOOTER **\n ** ./lib/serialize.js\n **/","class MemoryStore {\n constructor () {\n this._store = {}\n }\n\n getItem (key) {\n return Promise.resolve(this._store[key] || null)\n }\n\n setItem (key, value) {\n this._store[key] = value\n return Promise.resolve(value)\n }\n\n clear () {\n this._store = {}\n return Promise.resolve()\n }\n}\n\nexport default MemoryStore\n\n\n\n/** WEBPACK FOOTER **\n ** ./lib/memory.js\n **/","\nexport default function exclude (req, service, exclusions = {}) {\n if (service) {\n if (service.use && service.use.cache === false) {\n return true\n }\n }\n\n if ((typeof exclusions.filter === 'function') && !exclusions.filter(req)) {\n return true\n }\n\n // do not cache request with query\n if (exclusions.query && req.url.match(/\\?.*$/)) {\n return true\n }\n\n let found = false\n const paths = exclusions.paths || []\n\n paths.forEach(regexp => {\n if (req.url.match(regexp)) {\n found = true\n return found\n }\n })\n\n if (found) {\n return true\n }\n\n // All rules explained. fo not rewrite regexp.\n return false\n}\n\n\n\n/** WEBPACK FOOTER **\n ** ./lib/exclude.js\n **/"],"sourceRoot":""} \ No newline at end of file diff --git a/package.json b/package.json index bc7be40..273a0ed 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "superapi-cache", - "version": "0.10.4", + "version": "0.10.5", "description": "Caching module for superapi", "homepage": "https://github.com/stephanebachelier/superapi-cache", "author": {