diff --git a/index.js b/index.js index 0cc81c2..cc4b5f6 100644 --- a/index.js +++ b/index.js @@ -60,14 +60,19 @@ export default class { const _write = res.write.bind(res); - this.client.hgetall(cacheKey, (err, result) => { - if ( result && result.body && result.body.length ) { + this.client.get(cacheKey, (err, result) => { + if ( result && result.length ) { if (this.options.httpHeader) { res.setHeader(`${this.options.httpHeader}`, 'HIT') } - res.setHeader('Content-Type', 'application/json'); - _write(result.body); - res.end(); + if(binary) { //Convert back to binary buffer + _write(new Buffer(result, 'base64')); + res.end(); + } else { + res.setHeader('Content-Type', 'application/json'); + _write(result); + res.end(); + } } else { if (this.options.httpHeader) { res.setHeader(`${this.options.httpHeader}`, 'MISS') @@ -75,27 +80,20 @@ export default class { return next() } }); - + res.write = (body) => { + /** convert binary to base64 string **/ if(binary && typeof body !== 'string'){ body = new Buffer(body).toString('base64'); } - + if ( typeof body !== 'string' ) { _write(body); res.end(); } - let entry = { - body: body, - stale: this.options.stale, - created: +new Date(), - } - - this.client.hmset(cacheKey, entry); - this.client.expire(cacheKey, this.options.ttl); - entry = null; - + this.client.set(cacheKey, body, 'EX', this.options.ttl); + _write(body); res.end(); } diff --git a/lib/library.js b/lib/library.js index f2c1fbb..853933d 100644 --- a/lib/library.js +++ b/lib/library.js @@ -1,2 +1,2 @@ -!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define("library",[],t):"object"==typeof exports?exports.library=t():e.library=t()}(this,function(){return function(e){function t(n){if(r[n])return r[n].exports;var o=r[n]={i:n,l:!1,exports:{}};return e[n].call(o.exports,o,o.exports,t),o.l=!0,o.exports}var r={};return t.m=e,t.c=r,t.d=function(e,r,n){t.o(e,r)||Object.defineProperty(e,r,{configurable:!1,enumerable:!0,get:n})},t.n=function(e){var r=e&&e.__esModule?function(){return e.default}:function(){return e};return t.d(r,"a",r),r},t.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},t.p="",t(t.s=0)}([function(e,t,r){"use strict";function n(e){return e&&e.__esModule?e:{default:e}}function o(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(t,"__esModule",{value:!0});var i="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},a=function(){function e(e,t){for(var r=0;r0&&void 0!==arguments[0]?arguments[0]:{cache:!0,key:"asrc",ttl:900,stale:60};o(this,e),this.options=t,this.client=new s.default({enableOfflineQueue:!1})}return a(e,[{key:"middleware",value:function(){var e=this,t=arguments;return function(r,n,o){if(!e.options.cache)return o();if(!n.use_redis_cache)return o();var a=t,u=!1;"object"===i(a[0])&&"boolean"==typeof a[0].binary&&(u=a[0].binary);var s=r.query.query&&r.query.query?(0,c.default)(r.query.query):null,l=void 0;if(Array.isArray(r.body)){var p=r.body.filter(function(e){return e.operationName}).map(function(e){return e.operationName});l=p&&p.length?p.join(","):null}else l=r.body.operationName?r.body.operationName:null;var f=r.body&&r.body?(0,c.default)(r.body):"",d=new y({key:e.options.key,requestGetQuery:s,queryOperationName:l,queryHash:f}).cacheKey;l=null;var b=n.write.bind(n);e.client.hgetall(d,function(t,r){if(!(r&&r.body&&r.body.length))return e.options.httpHeader&&n.setHeader(""+e.options.httpHeader,"MISS"),o();e.options.httpHeader&&n.setHeader(""+e.options.httpHeader,"HIT"),n.setHeader("Content-Type","application/json"),b(r.body),n.end()}),n.write=function(t){u&&"string"!=typeof t&&(t=new Buffer(t).toString("base64")),"string"!=typeof t&&(b(t),n.end());var r={body:t,stale:e.options.stale,created:+new Date};e.client.hmset(d,r),e.client.expire(d,e.options.ttl),r=null,b(t),n.end()}}}}]),e}();t.default=d},function(e,t){e.exports=require("ioredis")},function(e,t){e.exports=require("hash-sum")},function(e,t){e.exports={name:"apollo-server-redis-cache",version:"0.1.4",structureVersion:"0.1.4",description:"Apollo GraphQL server redis cache middleware",main:"lib/library.js",scripts:{build:"webpack -p --env build"},author:{name:"B. Deren",email:"deren.bogdan@gmail.com"},license:"MIT",repository:{type:"git",url:"https://github.com/sgtram/apollo-server-redis-cache"},keywords:["GraphQL","Apollo","Server","Express","Redis","Cache"],dependencies:{"hash-sum":"^1.0.2",ioredis:"^3.2.1"},devDependencies:{"babel-cli":"^6.26.0","babel-eslint":"^8.0.2","babel-loader":"^7.1.2","babel-preset-env":"^1.6.1",eslint:"^4.11.0","eslint-loader":"^1.9.0",webpack:"^3.8.1","webpack-node-externals":"^1.6.0"}}}])}); +!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define("library",[],t):"object"==typeof exports?exports.library=t():e.library=t()}(this,function(){return function(e){function t(n){if(r[n])return r[n].exports;var o=r[n]={i:n,l:!1,exports:{}};return e[n].call(o.exports,o,o.exports,t),o.l=!0,o.exports}var r={};return t.m=e,t.c=r,t.d=function(e,r,n){t.o(e,r)||Object.defineProperty(e,r,{configurable:!1,enumerable:!0,get:n})},t.n=function(e){var r=e&&e.__esModule?function(){return e.default}:function(){return e};return t.d(r,"a",r),r},t.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},t.p="",t(t.s=0)}([function(e,t,r){"use strict";function n(e){return e&&e.__esModule?e:{default:e}}function o(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(t,"__esModule",{value:!0});var i="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},u=function(){function e(e,t){for(var r=0;r0&&void 0!==arguments[0]?arguments[0]:{cache:!0,key:"asrc",ttl:900,stale:60};o(this,e),this.options=t,this.client=new s.default({enableOfflineQueue:!1})}return u(e,[{key:"middleware",value:function(){var e=this,t=arguments;return function(r,n,o){if(!e.options.cache)return o();if(!n.use_redis_cache)return o();var u=t,a=!1;"object"===i(u[0])&&"boolean"==typeof u[0].binary&&(a=u[0].binary);var s=r.query.query&&r.query.query?(0,c.default)(r.query.query):null,l=void 0;if(Array.isArray(r.body)){var f=r.body.filter(function(e){return e.operationName}).map(function(e){return e.operationName});l=f&&f.length?f.join(","):null}else l=r.body.operationName?r.body.operationName:null;var p=r.body&&r.body?(0,c.default)(r.body):"",d=new y({key:e.options.key,requestGetQuery:s,queryOperationName:l,queryHash:p}).cacheKey;l=null;var b=n.write.bind(n);e.client.get(d,function(t,r){if(!r||!r.length)return e.options.httpHeader&&n.setHeader(""+e.options.httpHeader,"MISS"),o();e.options.httpHeader&&n.setHeader(""+e.options.httpHeader,"HIT"),a?(b(new Buffer(r,"base64")),n.end()):(n.setHeader("Content-Type","application/json"),b(r),n.end())}),n.write=function(t){a&&"string"!=typeof t&&(t=new Buffer(t).toString("base64")),"string"!=typeof t&&(b(t),n.end()),e.client.set(d,t,"EX",e.options.ttl),b(t),n.end()}}}}]),e}();t.default=d},function(e,t){e.exports=require("ioredis")},function(e,t){e.exports=require("hash-sum")},function(e,t){e.exports={name:"apollo-server-redis-cache",version:"0.1.5",structureVersion:"0.1.5",description:"Apollo GraphQL server redis cache middleware",main:"lib/library.js",scripts:{build:"webpack -p --env build"},author:{name:"B. Deren",email:"deren.bogdan@gmail.com"},license:"MIT",repository:{type:"git",url:"https://github.com/sgtram/apollo-server-redis-cache"},keywords:["GraphQL","Apollo","Server","Express","Redis","Cache"],dependencies:{"hash-sum":"^1.0.2",ioredis:"^3.2.1"},devDependencies:{"babel-cli":"^6.26.0","babel-eslint":"^8.0.2","babel-loader":"^7.1.2","babel-preset-env":"^1.6.1",eslint:"^4.11.0","eslint-loader":"^1.9.0",webpack:"^3.8.1","webpack-node-externals":"^1.6.0"}}}])}); //# sourceMappingURL=library.js.map \ No newline at end of file diff --git a/lib/library.js.map b/lib/library.js.map index a1710ef..34527c0 100644 --- a/lib/library.js.map +++ b/lib/library.js.map @@ -1 +1 @@ -{"version":3,"sources":["webpack:///webpack/universalModuleDefinition","webpack:///library.js","webpack:///webpack/bootstrap fa0313033eea8ae2cb5b","webpack:///./index.js","webpack:///external \"ioredis\"","webpack:///external \"hash-sum\"","webpack:///./package.json"],"names":["root","factory","exports","module","define","amd","this","modules","__webpack_require__","moduleId","installedModules","i","l","call","m","c","d","name","getter","o","Object","defineProperty","configurable","enumerable","get","n","__esModule","object","property","prototype","hasOwnProperty","p","s","_interopRequireDefault","obj","default","_classCallCheck","instance","Constructor","TypeError","value","_typeof","Symbol","iterator","constructor","_createClass","defineProperties","target","props","length","descriptor","writable","key","protoProps","staticProps","_ioredis","_ioredis2","_hashSum2","_hashSum3","_package2","_package","newObj","CacheKey","structureVersion","queryOperationName","requestGetQuery","queryHash","_class","options","arguments","undefined","cache","ttl","stale","client","enableOfflineQueue","_this","_arguments","req","res","next","use_redis_cache","binary","query","Array","isArray","body","names","filter","q","operationName","map","join","cacheKey","_write","write","bind","hgetall","err","result","httpHeader","setHeader","end","Buffer","toString","entry","created","Date","hmset","expire","require","version","description","main","scripts","build","author","email","license","repository","type","url","keywords","dependencies","hash-sum","ioredis","devDependencies","babel-cli","babel-eslint","babel-loader","babel-preset-env","eslint","eslint-loader","webpack","webpack-node-externals"],"mappings":"CAAA,SAAAA,EAAAC,GACA,gBAAAC,UAAA,gBAAAC,QACAA,OAAAD,QAAAD,IACA,kBAAAG,gBAAAC,IACAD,OAAA,aAAAH,GACA,gBAAAC,SACAA,QAAA,QAAAD,IAEAD,EAAA,QAAAC,KACCK,KAAA,WACD,MCAgB,UAAUC,GCN1B,QAAAC,GAAAC,GAGA,GAAAC,EAAAD,GACA,MAAAC,GAAAD,GAAAP,OAGA,IAAAC,GAAAO,EAAAD,IACAE,EAAAF,EACAG,GAAA,EACAV,WAUA,OANAK,GAAAE,GAAAI,KAAAV,EAAAD,QAAAC,IAAAD,QAAAM,GAGAL,EAAAS,GAAA,EAGAT,EAAAD,QAvBA,GAAAQ,KA4DA,OAhCAF,GAAAM,EAAAP,EAGAC,EAAAO,EAAAL,EAGAF,EAAAQ,EAAA,S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,EACNb,MAAOG,EAAKR,QAAQK,MACpBiC,SAAU,GAAIC,MAGhB/B,GAAKF,OAAOkC,MAAMf,EAAUY,GAC5B7B,EAAKF,OAAOmC,OAAOhB,EAAUjB,EAAKR,QAAQI,KAC1CiC,EAAQ,KAERX,EAAOR,GACPP,EAAIuB,YF+HHnC,IAGTjE,GAAQiC,QAAUgC,GAIZ,SAAUhE,EAAQD,GGzOxBC,EAAAD,QAAA4G,QAAA,YH+OM,SAAU3G,EAAQD,GI/OxBC,EAAAD,QAAA4G,QAAA,aJqPM,SAAU3G,EAAQD,GKrPxBC,EAAAD,SAAkBe,KAAA,4BAAA8F,QAAA,QAAAhD,iBAAA,QAAAiD,YAAA,+CAAAC,KAAA,iBAAAC,SAAgLC,MAAA,0BAAiCC,QAAWnG,KAAA,WAAAoG,MAAA,0BAAmDC,QAAA,MAAAC,YAA+BC,KAAA,MAAAC,IAAA,uDAAyEC,UAAA,uDAAAC,cAAoFC,WAAA,SAAAC,QAAA,UAAuCC,iBAAoBC,YAAA,UAAAC,eAAA,SAAAC,eAAA,SAAAC,mBAAA,SAAAC,OAAA,UAAAC,gBAAA,SAAAC,QAAA,SAAAC,yBAAA","file":"library.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(\"library\", [], factory);\n\telse if(typeof exports === 'object')\n\t\texports[\"library\"] = factory();\n\telse\n\t\troot[\"library\"] = factory();\n})(this, function() {\nreturn \n\n\n// WEBPACK FOOTER //\n// webpack/universalModuleDefinition","(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(\"library\", [], factory);\n\telse if(typeof exports === 'object')\n\t\texports[\"library\"] = factory();\n\telse\n\t\troot[\"library\"] = 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/******/ \t\t}\n/******/ \t\t// Create a new module (and put it into the cache)\n/******/ \t\tvar module = installedModules[moduleId] = {\n/******/ \t\t\ti: moduleId,\n/******/ \t\t\tl: false,\n/******/ \t\t\texports: {}\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.l = 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// define getter function for harmony exports\n/******/ \t__webpack_require__.d = function(exports, name, getter) {\n/******/ \t\tif(!__webpack_require__.o(exports, name)) {\n/******/ \t\t\tObject.defineProperty(exports, name, {\n/******/ \t\t\t\tconfigurable: false,\n/******/ \t\t\t\tenumerable: true,\n/******/ \t\t\t\tget: getter\n/******/ \t\t\t});\n/******/ \t\t}\n/******/ \t};\n/******/\n/******/ \t// getDefaultExport function for compatibility with non-harmony modules\n/******/ \t__webpack_require__.n = function(module) {\n/******/ \t\tvar getter = module && module.__esModule ?\n/******/ \t\t\tfunction getDefault() { return module['default']; } :\n/******/ \t\t\tfunction getModuleExports() { return module; };\n/******/ \t\t__webpack_require__.d(getter, 'a', getter);\n/******/ \t\treturn getter;\n/******/ \t};\n/******/\n/******/ \t// Object.prototype.hasOwnProperty.call\n/******/ \t__webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };\n/******/\n/******/ \t// __webpack_public_path__\n/******/ \t__webpack_require__.p = \"\";\n/******/\n/******/ \t// Load entry module and return exports\n/******/ \treturn __webpack_require__(__webpack_require__.s = 0);\n/******/ })\n/************************************************************************/\n/******/ ([\n/* 0 */\n/***/ (function(module, exports, __webpack_require__) {\n\n\"use strict\";\n\n\nObject.defineProperty(exports, \"__esModule\", {\n value: true\n});\n\nvar _typeof = typeof Symbol === \"function\" && typeof Symbol.iterator === \"symbol\" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === \"function\" && obj.constructor === Symbol && obj !== Symbol.prototype ? \"symbol\" : typeof obj; };\n\nvar _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\nvar _ioredis = __webpack_require__(1);\n\nvar _ioredis2 = _interopRequireDefault(_ioredis);\n\nvar _hashSum2 = __webpack_require__(2);\n\nvar _hashSum3 = _interopRequireDefault(_hashSum2);\n\nvar _package2 = __webpack_require__(3);\n\nvar _package = _interopRequireWildcard(_package2);\n\nfunction _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; } }\n\nfunction _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }\n\nfunction _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError(\"Cannot call a class as a function\"); } }\n\nvar CacheKey = function () {\n function CacheKey(obj) {\n _classCallCheck(this, CacheKey);\n\n this.obj = obj ? obj : {};\n }\n\n _createClass(CacheKey, [{\n key: 'cacheKey',\n get: function get() {\n var key = this.obj.key + ':' + _package.structureVersion + ':' + (this.obj.queryOperationName ? this.obj.queryOperationName + ':' : '') + (this.obj.requestGetQuery ? this.obj.requestGetQuery + ':' : '') + this.obj.queryHash;\n return key;\n }\n }]);\n\n return CacheKey;\n}();\n\nvar _class = function () {\n function _class() {\n var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : { cache: true, key: 'asrc', ttl: 900, stale: 60 };\n\n _classCallCheck(this, _class);\n\n this.options = options;\n this.client = new _ioredis2.default({ enableOfflineQueue: false });\n }\n\n _createClass(_class, [{\n key: 'middleware',\n value: function middleware() {\n var _this = this,\n _arguments = arguments;\n\n return function (req, res, next) {\n if (!_this.options.cache) {\n return next();\n }\n\n if (!res.use_redis_cache) {\n return next();\n }\n\n var options = _arguments;\n\n var binary = false;\n if (_typeof(options[0]) === 'object' && typeof options[0].binary === 'boolean') {\n binary = options[0].binary;\n }\n\n var requestGetQuery = req.query.query && req.query.query ? (0, _hashSum3.default)(req.query.query) : null;\n var queryOperationName = void 0;\n if (Array.isArray(req.body)) {\n var names = req.body.filter(function (q) {\n return q.operationName;\n }).map(function (q) {\n return q.operationName;\n });\n queryOperationName = names && names.length ? names.join(',') : null;\n } else {\n queryOperationName = req.body.operationName ? req.body.operationName : null;\n }\n var queryHash = req.body && req.body ? (0, _hashSum3.default)(req.body) : '';\n\n var cacheKey = new CacheKey({\n key: _this.options.key,\n requestGetQuery: requestGetQuery,\n queryOperationName: queryOperationName,\n queryHash: queryHash\n }).cacheKey;\n\n queryOperationName = null;\n\n var _write = res.write.bind(res);\n\n _this.client.hgetall(cacheKey, function (err, result) {\n if (result && result.body && result.body.length) {\n if (_this.options.httpHeader) {\n res.setHeader('' + _this.options.httpHeader, 'HIT');\n }\n res.setHeader('Content-Type', 'application/json');\n _write(result.body);\n res.end();\n } else {\n if (_this.options.httpHeader) {\n res.setHeader('' + _this.options.httpHeader, 'MISS');\n }\n return next();\n }\n });\n\n res.write = function (body) {\n if (binary && typeof body !== 'string') {\n body = new Buffer(body).toString('base64');\n }\n\n if (typeof body !== 'string') {\n _write(body);\n res.end();\n }\n\n var entry = {\n body: body,\n stale: _this.options.stale,\n created: +new Date()\n };\n\n _this.client.hmset(cacheKey, entry);\n _this.client.expire(cacheKey, _this.options.ttl);\n entry = null;\n\n _write(body);\n res.end();\n };\n };\n }\n }]);\n\n return _class;\n}();\n\nexports.default = _class;\n\n/***/ }),\n/* 1 */\n/***/ (function(module, exports) {\n\nmodule.exports = require(\"ioredis\");\n\n/***/ }),\n/* 2 */\n/***/ (function(module, exports) {\n\nmodule.exports = require(\"hash-sum\");\n\n/***/ }),\n/* 3 */\n/***/ (function(module, exports) {\n\nmodule.exports = {\"name\":\"apollo-server-redis-cache\",\"version\":\"0.1.4\",\"structureVersion\":\"0.1.4\",\"description\":\"Apollo GraphQL server redis cache middleware\",\"main\":\"lib/library.js\",\"scripts\":{\"build\":\"webpack -p --env build\"},\"author\":{\"name\":\"B. Deren\",\"email\":\"deren.bogdan@gmail.com\"},\"license\":\"MIT\",\"repository\":{\"type\":\"git\",\"url\":\"https://github.com/sgtram/apollo-server-redis-cache\"},\"keywords\":[\"GraphQL\",\"Apollo\",\"Server\",\"Express\",\"Redis\",\"Cache\"],\"dependencies\":{\"hash-sum\":\"^1.0.2\",\"ioredis\":\"^3.2.1\"},\"devDependencies\":{\"babel-cli\":\"^6.26.0\",\"babel-eslint\":\"^8.0.2\",\"babel-loader\":\"^7.1.2\",\"babel-preset-env\":\"^1.6.1\",\"eslint\":\"^4.11.0\",\"eslint-loader\":\"^1.9.0\",\"webpack\":\"^3.8.1\",\"webpack-node-externals\":\"^1.6.0\"}}\n\n/***/ })\n/******/ ]);\n});\n\n\n// WEBPACK FOOTER //\n// library.js"," \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 \t\t}\n \t\t// Create a new module (and put it into the cache)\n \t\tvar module = installedModules[moduleId] = {\n \t\t\ti: moduleId,\n \t\t\tl: false,\n \t\t\texports: {}\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.l = 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// define getter function for harmony exports\n \t__webpack_require__.d = function(exports, name, getter) {\n \t\tif(!__webpack_require__.o(exports, name)) {\n \t\t\tObject.defineProperty(exports, name, {\n \t\t\t\tconfigurable: false,\n \t\t\t\tenumerable: true,\n \t\t\t\tget: getter\n \t\t\t});\n \t\t}\n \t};\n\n \t// getDefaultExport function for compatibility with non-harmony modules\n \t__webpack_require__.n = function(module) {\n \t\tvar getter = module && module.__esModule ?\n \t\t\tfunction getDefault() { return module['default']; } :\n \t\t\tfunction getModuleExports() { return module; };\n \t\t__webpack_require__.d(getter, 'a', getter);\n \t\treturn getter;\n \t};\n\n \t// Object.prototype.hasOwnProperty.call\n \t__webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };\n\n \t// __webpack_public_path__\n \t__webpack_require__.p = \"\";\n\n \t// Load entry module and return exports\n \treturn __webpack_require__(__webpack_require__.s = 0);\n\n\n\n// WEBPACK FOOTER //\n// webpack/bootstrap fa0313033eea8ae2cb5b","import _Cache from 'ioredis'\nimport _hashSum from 'hash-sum'\nimport * as _package from './package.json'\n\nclass CacheKey {\n constructor(obj) {\n this.obj = obj ? obj : {}\n }\n get cacheKey() {\n const key = this.obj.key + ':' + \n _package.structureVersion + ':' + \n (this.obj.queryOperationName ? this.obj.queryOperationName + ':' : '') + \n (this.obj.requestGetQuery ? this.obj.requestGetQuery + ':' : '') + \n this.obj.queryHash\n return key\n }\n}\n\nexport default class {\n constructor(options = { cache: true, key: 'asrc', ttl: 900, stale: 60 }) {\n this.options = options\n this.client = new _Cache({ enableOfflineQueue: false })\n }\n\n middleware() {\n return (req, res, next) => {\n if (!this.options.cache) {\n return next()\n }\n \n if (!res.use_redis_cache) {\n return next()\n }\n\n const options = arguments;\n \n let binary = false;\n if ( typeof options[0] === 'object' && typeof options[0].binary === 'boolean' ) {\n binary = options[0].binary;\n }\n\n const requestGetQuery = req.query.query && req.query.query ? _hashSum(req.query.query) : null\n let queryOperationName;\n if (Array.isArray(req.body)) {\n const names = req.body.filter(q => q.operationName).map(q => q.operationName)\n queryOperationName = names && names.length ? names.join(',') : null\n } else {\n queryOperationName = req.body.operationName ? req.body.operationName : null\n }\n const queryHash = req.body && req.body ? _hashSum(req.body) : ''\n\n const cacheKey = new CacheKey({\n key: this.options.key,\n requestGetQuery,\n queryOperationName,\n queryHash,\n }).cacheKey\n\n queryOperationName = null;\n\n const _write = res.write.bind(res);\n\n this.client.hgetall(cacheKey, (err, result) => {\n if ( result && result.body && result.body.length ) {\n if (this.options.httpHeader) {\n res.setHeader(`${this.options.httpHeader}`, 'HIT')\n }\n res.setHeader('Content-Type', 'application/json');\n _write(result.body);\n res.end();\n } else {\n if (this.options.httpHeader) {\n res.setHeader(`${this.options.httpHeader}`, 'MISS')\n }\n return next()\n }\n });\n\n res.write = (body) => {\n if(binary && typeof body !== 'string'){\n body = new Buffer(body).toString('base64');\n }\n\n if ( typeof body !== 'string' ) {\n _write(body);\n res.end();\n }\n\n let entry = {\n body: body,\n stale: this.options.stale,\n created: +new Date(),\n }\n \n this.client.hmset(cacheKey, entry);\n this.client.expire(cacheKey, this.options.ttl);\n entry = null;\n \n _write(body);\n res.end();\n }\n }\n }\n}\n\n\n// WEBPACK FOOTER //\n// ./index.js","module.exports = require(\"ioredis\");\n\n\n//////////////////\n// WEBPACK FOOTER\n// external \"ioredis\"\n// module id = 1\n// module chunks = 0","module.exports = require(\"hash-sum\");\n\n\n//////////////////\n// WEBPACK FOOTER\n// external \"hash-sum\"\n// module id = 2\n// module chunks = 0","module.exports = {\"name\":\"apollo-server-redis-cache\",\"version\":\"0.1.4\",\"structureVersion\":\"0.1.4\",\"description\":\"Apollo GraphQL server redis cache middleware\",\"main\":\"lib/library.js\",\"scripts\":{\"build\":\"webpack -p --env build\"},\"author\":{\"name\":\"B. Deren\",\"email\":\"deren.bogdan@gmail.com\"},\"license\":\"MIT\",\"repository\":{\"type\":\"git\",\"url\":\"https://github.com/sgtram/apollo-server-redis-cache\"},\"keywords\":[\"GraphQL\",\"Apollo\",\"Server\",\"Express\",\"Redis\",\"Cache\"],\"dependencies\":{\"hash-sum\":\"^1.0.2\",\"ioredis\":\"^3.2.1\"},\"devDependencies\":{\"babel-cli\":\"^6.26.0\",\"babel-eslint\":\"^8.0.2\",\"babel-loader\":\"^7.1.2\",\"babel-preset-env\":\"^1.6.1\",\"eslint\":\"^4.11.0\",\"eslint-loader\":\"^1.9.0\",\"webpack\":\"^3.8.1\",\"webpack-node-externals\":\"^1.6.0\"}}\n\n\n//////////////////\n// WEBPACK FOOTER\n// ./package.json\n// module id = 3\n// module chunks = 0"],"sourceRoot":""} \ No newline at end of file +{"version":3,"sources":["webpack:///webpack/universalModuleDefinition","webpack:///library.js","webpack:///webpack/bootstrap c485942c6e022a17969f","webpack:///./index.js","webpack:///external \"ioredis\"","webpack:///external \"hash-sum\"","webpack:///./package.json"],"names":["root","factory","exports","module","define","amd","this","modules","__webpack_require__","moduleId","installedModules","i","l","call","m","c","d","name","getter","o","Object","defineProperty","configurable","enumerable","get","n","__esModule","object","property","prototype","hasOwnProperty","p","s","_interopRequireDefault","obj","default","_classCallCheck","instance","Constructor","TypeError","value","_typeof","Symbol","iterator","constructor","_createClass","defineProperties","target","props","length","descriptor","writable","key","protoProps","staticProps","_ioredis","_ioredis2","_hashSum2","_hashSum3","_package2","_package","newObj","CacheKey","structureVersion","queryOperationName","requestGetQuery","queryHash","_class","options","arguments","undefined","cache","ttl","stale","client","enableOfflineQueue","_this","_arguments","req","res","next","use_redis_cache","binary","query","Array","isArray","body","names","filter","q","operationName","map","join","cacheKey","_write","write","bind","err","result","httpHeader","setHeader","Buffer","end","toString","set","require","version","description","main","scripts","build","author","email","license","repository","type","url","keywords","dependencies","hash-sum","ioredis","devDependencies","babel-cli","babel-eslint","babel-loader","babel-preset-env","eslint","eslint-loader","webpack","webpack-node-externals"],"mappings":"CAAA,SAAAA,EAAAC,GACA,gBAAAC,UAAA,gBAAAC,QACAA,OAAAD,QAAAD,IACA,kBAAAG,gBAAAC,IACAD,OAAA,aAAAH,GACA,gBAAAC,SACAA,QAAA,QAAAD,IAEAD,EAAA,QAAAC,KACCK,KAAA,WACD,MCAgB,UAAUC,GCN1B,QAAAC,GAAAC,GAGA,GAAAC,EAAAD,GACA,MAAAC,GAAAD,GAAAP,OAGA,IAAAC,GAAAO,EAAAD,IACAE,EAAAF,EACAG,GAAA,EACAV,WAUA,OANAK,GAAAE,GAAAI,KAAAV,EAAAD,QAAAC,IAAAD,QAAAM,GAGAL,EAAAS,GAAA,EAGAT,EAAAD,QAvBA,GAAAQ,KA4DA,OAhCAF,GAAAM,EAAAP,EAGAC,EAAAO,EAAAL,EAGAF,EAAAQ,EAAA,SAAAd,EAAAe,EAAAC,GACAV,EAAAW,EAAAjB,EAAAe,IACAG,OAAAC,eAAAnB,EAAAe,GACAK,cAAA,EACAC,YAAA,EACAC,IAAAN,KAMAV,EAAAiB,EAAA,SAAAtB,GACA,GAAAe,GAAAf,KAAAuB,WACA,WAA2B,MAAAvB,GAAA,SAC3B,WAAiC,MAAAA,GAEjC,OADAK,GAAAQ,EAAAE,EAAA,IAAAA,GACAA,GAIAV,EAAAW,EAAA,SAAAQ,EAAAC,GAAsD,MAAAR,QAAAS,UAAAC,eAAAjB,KAAAc,EAAAC,IAGtDpB,EAAAuB,EAAA,GAGAvB,IAAAwB,EAAA,KDgBM,SAAU7B,EAAQD,EAASM,GAEjC,YAyBA,SAASyB,GAAuBC,GAAO,MAAOA,IAAO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AAyEC,UAAA,uDAAAC,cAAoFC,WAAA,SAAAC,QAAA,UAAuCC,iBAAoBC,YAAA,UAAAC,eAAA,SAAAC,eAAA,SAAAC,mBAAA,SAAAC,OAAA,UAAAC,gBAAA,SAAAC,QAAA,SAAAC,yBAAA","file":"library.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(\"library\", [], factory);\n\telse if(typeof exports === 'object')\n\t\texports[\"library\"] = factory();\n\telse\n\t\troot[\"library\"] = factory();\n})(this, function() {\nreturn \n\n\n// WEBPACK FOOTER //\n// webpack/universalModuleDefinition","(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(\"library\", [], factory);\n\telse if(typeof exports === 'object')\n\t\texports[\"library\"] = factory();\n\telse\n\t\troot[\"library\"] = 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/******/ \t\t}\n/******/ \t\t// Create a new module (and put it into the cache)\n/******/ \t\tvar module = installedModules[moduleId] = {\n/******/ \t\t\ti: moduleId,\n/******/ \t\t\tl: false,\n/******/ \t\t\texports: {}\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.l = 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// define getter function for harmony exports\n/******/ \t__webpack_require__.d = function(exports, name, getter) {\n/******/ \t\tif(!__webpack_require__.o(exports, name)) {\n/******/ \t\t\tObject.defineProperty(exports, name, {\n/******/ \t\t\t\tconfigurable: false,\n/******/ \t\t\t\tenumerable: true,\n/******/ \t\t\t\tget: getter\n/******/ \t\t\t});\n/******/ \t\t}\n/******/ \t};\n/******/\n/******/ \t// getDefaultExport function for compatibility with non-harmony modules\n/******/ \t__webpack_require__.n = function(module) {\n/******/ \t\tvar getter = module && module.__esModule ?\n/******/ \t\t\tfunction getDefault() { return module['default']; } :\n/******/ \t\t\tfunction getModuleExports() { return module; };\n/******/ \t\t__webpack_require__.d(getter, 'a', getter);\n/******/ \t\treturn getter;\n/******/ \t};\n/******/\n/******/ \t// Object.prototype.hasOwnProperty.call\n/******/ \t__webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };\n/******/\n/******/ \t// __webpack_public_path__\n/******/ \t__webpack_require__.p = \"\";\n/******/\n/******/ \t// Load entry module and return exports\n/******/ \treturn __webpack_require__(__webpack_require__.s = 0);\n/******/ })\n/************************************************************************/\n/******/ ([\n/* 0 */\n/***/ (function(module, exports, __webpack_require__) {\n\n\"use strict\";\n\n\nObject.defineProperty(exports, \"__esModule\", {\n value: true\n});\n\nvar _typeof = typeof Symbol === \"function\" && typeof Symbol.iterator === \"symbol\" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === \"function\" && obj.constructor === Symbol && obj !== Symbol.prototype ? \"symbol\" : typeof obj; };\n\nvar _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\nvar _ioredis = __webpack_require__(1);\n\nvar _ioredis2 = _interopRequireDefault(_ioredis);\n\nvar _hashSum2 = __webpack_require__(2);\n\nvar _hashSum3 = _interopRequireDefault(_hashSum2);\n\nvar _package2 = __webpack_require__(3);\n\nvar _package = _interopRequireWildcard(_package2);\n\nfunction _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; } }\n\nfunction _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }\n\nfunction _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError(\"Cannot call a class as a function\"); } }\n\nvar CacheKey = function () {\n function CacheKey(obj) {\n _classCallCheck(this, CacheKey);\n\n this.obj = obj ? obj : {};\n }\n\n _createClass(CacheKey, [{\n key: 'cacheKey',\n get: function get() {\n var key = this.obj.key + ':' + _package.structureVersion + ':' + (this.obj.queryOperationName ? this.obj.queryOperationName + ':' : '') + (this.obj.requestGetQuery ? this.obj.requestGetQuery + ':' : '') + this.obj.queryHash;\n return key;\n }\n }]);\n\n return CacheKey;\n}();\n\nvar _class = function () {\n function _class() {\n var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : { cache: true, key: 'asrc', ttl: 900, stale: 60 };\n\n _classCallCheck(this, _class);\n\n this.options = options;\n this.client = new _ioredis2.default({ enableOfflineQueue: false });\n }\n\n _createClass(_class, [{\n key: 'middleware',\n value: function middleware() {\n var _this = this,\n _arguments = arguments;\n\n return function (req, res, next) {\n if (!_this.options.cache) {\n return next();\n }\n\n if (!res.use_redis_cache) {\n return next();\n }\n\n var options = _arguments;\n\n var binary = false;\n if (_typeof(options[0]) === 'object' && typeof options[0].binary === 'boolean') {\n binary = options[0].binary;\n }\n\n var requestGetQuery = req.query.query && req.query.query ? (0, _hashSum3.default)(req.query.query) : null;\n var queryOperationName = void 0;\n if (Array.isArray(req.body)) {\n var names = req.body.filter(function (q) {\n return q.operationName;\n }).map(function (q) {\n return q.operationName;\n });\n queryOperationName = names && names.length ? names.join(',') : null;\n } else {\n queryOperationName = req.body.operationName ? req.body.operationName : null;\n }\n var queryHash = req.body && req.body ? (0, _hashSum3.default)(req.body) : '';\n\n var cacheKey = new CacheKey({\n key: _this.options.key,\n requestGetQuery: requestGetQuery,\n queryOperationName: queryOperationName,\n queryHash: queryHash\n }).cacheKey;\n\n queryOperationName = null;\n\n var _write = res.write.bind(res);\n\n _this.client.get(cacheKey, function (err, result) {\n if (result && result.length) {\n if (_this.options.httpHeader) {\n res.setHeader('' + _this.options.httpHeader, 'HIT');\n }\n if (binary) {\n //Convert back to binary buffer\n _write(new Buffer(result, 'base64'));\n res.end();\n } else {\n res.setHeader('Content-Type', 'application/json');\n _write(result);\n res.end();\n }\n } else {\n if (_this.options.httpHeader) {\n res.setHeader('' + _this.options.httpHeader, 'MISS');\n }\n return next();\n }\n });\n\n res.write = function (body) {\n /** convert binary to base64 string **/\n if (binary && typeof body !== 'string') {\n body = new Buffer(body).toString('base64');\n }\n\n if (typeof body !== 'string') {\n _write(body);\n res.end();\n }\n\n _this.client.set(cacheKey, body, 'EX', _this.options.ttl);\n\n _write(body);\n res.end();\n };\n };\n }\n }]);\n\n return _class;\n}();\n\nexports.default = _class;\n\n/***/ }),\n/* 1 */\n/***/ (function(module, exports) {\n\nmodule.exports = require(\"ioredis\");\n\n/***/ }),\n/* 2 */\n/***/ (function(module, exports) {\n\nmodule.exports = require(\"hash-sum\");\n\n/***/ }),\n/* 3 */\n/***/ (function(module, exports) {\n\nmodule.exports = {\"name\":\"apollo-server-redis-cache\",\"version\":\"0.1.5\",\"structureVersion\":\"0.1.5\",\"description\":\"Apollo GraphQL server redis cache middleware\",\"main\":\"lib/library.js\",\"scripts\":{\"build\":\"webpack -p --env build\"},\"author\":{\"name\":\"B. Deren\",\"email\":\"deren.bogdan@gmail.com\"},\"license\":\"MIT\",\"repository\":{\"type\":\"git\",\"url\":\"https://github.com/sgtram/apollo-server-redis-cache\"},\"keywords\":[\"GraphQL\",\"Apollo\",\"Server\",\"Express\",\"Redis\",\"Cache\"],\"dependencies\":{\"hash-sum\":\"^1.0.2\",\"ioredis\":\"^3.2.1\"},\"devDependencies\":{\"babel-cli\":\"^6.26.0\",\"babel-eslint\":\"^8.0.2\",\"babel-loader\":\"^7.1.2\",\"babel-preset-env\":\"^1.6.1\",\"eslint\":\"^4.11.0\",\"eslint-loader\":\"^1.9.0\",\"webpack\":\"^3.8.1\",\"webpack-node-externals\":\"^1.6.0\"}}\n\n/***/ })\n/******/ ]);\n});\n\n\n// WEBPACK FOOTER //\n// library.js"," \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 \t\t}\n \t\t// Create a new module (and put it into the cache)\n \t\tvar module = installedModules[moduleId] = {\n \t\t\ti: moduleId,\n \t\t\tl: false,\n \t\t\texports: {}\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.l = 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// define getter function for harmony exports\n \t__webpack_require__.d = function(exports, name, getter) {\n \t\tif(!__webpack_require__.o(exports, name)) {\n \t\t\tObject.defineProperty(exports, name, {\n \t\t\t\tconfigurable: false,\n \t\t\t\tenumerable: true,\n \t\t\t\tget: getter\n \t\t\t});\n \t\t}\n \t};\n\n \t// getDefaultExport function for compatibility with non-harmony modules\n \t__webpack_require__.n = function(module) {\n \t\tvar getter = module && module.__esModule ?\n \t\t\tfunction getDefault() { return module['default']; } :\n \t\t\tfunction getModuleExports() { return module; };\n \t\t__webpack_require__.d(getter, 'a', getter);\n \t\treturn getter;\n \t};\n\n \t// Object.prototype.hasOwnProperty.call\n \t__webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };\n\n \t// __webpack_public_path__\n \t__webpack_require__.p = \"\";\n\n \t// Load entry module and return exports\n \treturn __webpack_require__(__webpack_require__.s = 0);\n\n\n\n// WEBPACK FOOTER //\n// webpack/bootstrap c485942c6e022a17969f","import _Cache from 'ioredis'\nimport _hashSum from 'hash-sum'\nimport * as _package from './package.json'\n\nclass CacheKey {\n constructor(obj) {\n this.obj = obj ? obj : {}\n }\n get cacheKey() {\n const key = this.obj.key + ':' + \n _package.structureVersion + ':' + \n (this.obj.queryOperationName ? this.obj.queryOperationName + ':' : '') + \n (this.obj.requestGetQuery ? this.obj.requestGetQuery + ':' : '') + \n this.obj.queryHash\n return key\n }\n}\n\nexport default class {\n constructor(options = { cache: true, key: 'asrc', ttl: 900, stale: 60 }) {\n this.options = options\n this.client = new _Cache({ enableOfflineQueue: false })\n }\n\n middleware() {\n return (req, res, next) => {\n if (!this.options.cache) {\n return next()\n }\n \n if (!res.use_redis_cache) {\n return next()\n }\n\n const options = arguments;\n \n let binary = false;\n if ( typeof options[0] === 'object' && typeof options[0].binary === 'boolean' ) {\n binary = options[0].binary;\n }\n\n const requestGetQuery = req.query.query && req.query.query ? _hashSum(req.query.query) : null\n let queryOperationName;\n if (Array.isArray(req.body)) {\n const names = req.body.filter(q => q.operationName).map(q => q.operationName)\n queryOperationName = names && names.length ? names.join(',') : null\n } else {\n queryOperationName = req.body.operationName ? req.body.operationName : null\n }\n const queryHash = req.body && req.body ? _hashSum(req.body) : ''\n\n const cacheKey = new CacheKey({\n key: this.options.key,\n requestGetQuery,\n queryOperationName,\n queryHash,\n }).cacheKey\n\n queryOperationName = null;\n\n const _write = res.write.bind(res);\n\n this.client.get(cacheKey, (err, result) => {\n if ( result && result.length ) {\n if (this.options.httpHeader) {\n res.setHeader(`${this.options.httpHeader}`, 'HIT')\n }\n if(binary) { //Convert back to binary buffer\n _write(new Buffer(result, 'base64'));\n res.end();\n } else {\n res.setHeader('Content-Type', 'application/json');\n _write(result);\n res.end();\n }\n } else {\n if (this.options.httpHeader) {\n res.setHeader(`${this.options.httpHeader}`, 'MISS')\n }\n return next()\n }\n });\n \n res.write = (body) => {\n /** convert binary to base64 string **/\n if(binary && typeof body !== 'string'){\n body = new Buffer(body).toString('base64');\n }\n \n if ( typeof body !== 'string' ) {\n _write(body);\n res.end();\n }\n\n this.client.set(cacheKey, body, 'EX', this.options.ttl);\n\n _write(body);\n res.end();\n }\n }\n }\n}\n\n\n// WEBPACK FOOTER //\n// ./index.js","module.exports = require(\"ioredis\");\n\n\n//////////////////\n// WEBPACK FOOTER\n// external \"ioredis\"\n// module id = 1\n// module chunks = 0","module.exports = require(\"hash-sum\");\n\n\n//////////////////\n// WEBPACK FOOTER\n// external \"hash-sum\"\n// module id = 2\n// module chunks = 0","module.exports = {\"name\":\"apollo-server-redis-cache\",\"version\":\"0.1.5\",\"structureVersion\":\"0.1.5\",\"description\":\"Apollo GraphQL server redis cache middleware\",\"main\":\"lib/library.js\",\"scripts\":{\"build\":\"webpack -p --env build\"},\"author\":{\"name\":\"B. Deren\",\"email\":\"deren.bogdan@gmail.com\"},\"license\":\"MIT\",\"repository\":{\"type\":\"git\",\"url\":\"https://github.com/sgtram/apollo-server-redis-cache\"},\"keywords\":[\"GraphQL\",\"Apollo\",\"Server\",\"Express\",\"Redis\",\"Cache\"],\"dependencies\":{\"hash-sum\":\"^1.0.2\",\"ioredis\":\"^3.2.1\"},\"devDependencies\":{\"babel-cli\":\"^6.26.0\",\"babel-eslint\":\"^8.0.2\",\"babel-loader\":\"^7.1.2\",\"babel-preset-env\":\"^1.6.1\",\"eslint\":\"^4.11.0\",\"eslint-loader\":\"^1.9.0\",\"webpack\":\"^3.8.1\",\"webpack-node-externals\":\"^1.6.0\"}}\n\n\n//////////////////\n// WEBPACK FOOTER\n// ./package.json\n// module id = 3\n// module chunks = 0"],"sourceRoot":""} \ No newline at end of file diff --git a/package.json b/package.json index 9a88a34..244668a 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "apollo-server-redis-cache", - "version": "0.1.4", - "structureVersion": "0.1.4", + "version": "0.1.5", + "structureVersion": "0.1.5", "description": "Apollo GraphQL server redis cache middleware", "main": "lib/library.js", "scripts": {