@merkur/plugin-http-cache 0.29.3 → 0.31.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.es5.js +1 -1
- package/lib/index.es9.cjs +10 -45
- package/lib/index.es9.mjs +11 -43
- package/lib/index.mjs +1 -1
- package/package.json +5 -5
package/lib/index.es5.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";function e(e,t){var n="undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(!n){if(Array.isArray(e)||(n=function(e,t){if(!e)return;if("string"==typeof e)return r(e,t);var n=Object.prototype.toString.call(e).slice(8,-1);"Object"===n&&e.constructor&&(n=e.constructor.name);if("Map"===n||"Set"===n)return Array.from(e);if("Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n))return r(e,t)}(e))||t&&e&&"number"==typeof e.length){n&&(e=n);var o=0,s=function(){};return{s:s,n:function(){return o>=e.length?{done:!0}:{done:!1,value:e[o++]}},e:function(e){throw e},f:s}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var i,u=!0,a=!1;return{s:function(){n=n.call(e)},n:function(){var e=n.next();return u=e.done,e},e:function(e){a=!0,i=e},f:function(){try{u||null==n.return||n.return()}finally{if(a)throw i}}}}function r(e,r){(null==r||r>e.length)&&(r=e.length);for(var t=0,n=new Array(r);t<r;t++)n[t]=e[t];return n}function t(e,r){var t=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);r&&(n=n.filter((function(r){return Object.getOwnPropertyDescriptor(e,r).enumerable}))),t.push.apply(t,n)}return t}function n(e){for(var r=1;r<arguments.length;r++){var n=null!=arguments[r]?arguments[r]:{};r%2?t(Object(n),!0).forEach((function(r){o(e,r,n[r])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):t(Object(n)).forEach((function(r){Object.defineProperty(e,r,Object.getOwnPropertyDescriptor(n,r))}))}return e}function o(e,r,t){return r in e?Object.defineProperty(e,r,{value:t,enumerable:!0,configurable:!0,writable:!0}):e[r]=t,e}function s(e,r,t,n,o,s,i){try{var u=e[s](i),a=u.value}catch(e){return void t(e)}u.done?r(a):Promise.resolve(a).then(n,o)}function i(e){return function(){var r=this,t=arguments;return new Promise((function(n,o){var i=e.apply(r,t);function u(e){s(i,n,o,u,a,"next",e)}function a(e){s(i,n,o,u,a,"throw",e)}u(void 0)}))}}function u(e,r){for(var t=0;t<r.length;t++){var n=r[t];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}require("regenerator-runtime/runtime.js"),require("core-js/modules/es.object.define-property.js"),require("core-js/modules/es.date.now.js"),require("core-js/modules/es.date.to-string.js"),require("core-js/modules/es.array.index-of.js"),require("core-js/modules/es.regexp.exec.js"),require("core-js/modules/es.string.replace.js"),require("core-js/modules/es.array.concat.js"),require("core-js/modules/es.array.iterator.js"),require("core-js/modules/es.map.js"),require("core-js/modules/es.object.to-string.js"),require("core-js/modules/es.string.iterator.js"),require("core-js/modules/web.dom-collections.iterator.js"),require("core-js/modules/es.object.keys.js"),require("core-js/modules/es.promise.js"),require("core-js/modules/es.symbol.js"),require("core-js/modules/es.array.filter.js"),require("core-js/modules/es.object.get-own-property-descriptor.js"),require("core-js/modules/es.array.for-each.js"),require("core-js/modules/web.dom-collections.for-each.js"),require("core-js/modules/es.object.get-own-property-descriptors.js"),require("core-js/modules/es.object.define-properties.js"),require("core-js/modules/es.array.slice.js"),require("core-js/modules/es.function.name.js"),require("core-js/modules/es.array.from.js"),require("core-js/modules/es.symbol.description.js"),require("core-js/modules/es.symbol.iterator.js"),require("core-js/modules/es.array.is-array.js"),Object.defineProperty(exports,"__esModule",{value:!0});var a=require("@merkur/core"),c=require("@merkur/plugin-http-client"),l=function(){function e(r,t){!function(e,r){if(!(e instanceof r))throw new TypeError("Cannot call a class as a function")}(this,e),this._value=r,this._ttl=t,this._created=Date.now()}var r,t,n;return r=e,(t=[{key:"value",get:function(){return this._value}},{key:"isExpired",value:function(){return Date.now()>this._created+this._ttl}},{key:"serialize",value:function(){return{value:this.value,ttl:this._ttl===1/0?"Infinity":this._ttl}}}])&&u(r.prototype,t),n&&u(r,n),Object.defineProperty(r,"prototype",{writable:!1}),e}(),p="development",f="undefined"!=typeof process&&process&&process.env?process.env.NODE_ENV:p;function h(e){var r=e.method,t=e.url,n=e.body,o=e.query,s=~["GET","HEAD"].indexOf(r)?o:n,i="";if(s)try{i=JSON.stringify(s).replace(/<\/script/gi,"<\\/script")}catch(e){console.warn("The provided data does not have valid JSON format",s)}return"".concat(r,":").concat(t,"?").concat(i)}function d(e,r){return m.apply(this,arguments)}function m(){return m=i(regeneratorRuntime.mark((function e(r,t){var o,s,i,u,a=arguments;return regeneratorRuntime.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:for(o=a.length,s=new Array(o>2?o-2:0),i=2;i<o;i++)s[i-2]=a[i];return e.next=3,t.apply(void 0,s);case 3:return u=e.sent,e.abrupt("return",n(n({},u),{},{$httpCache:r.httpCache.serialize()}));case 5:case"end":return e.stop()}}),e)}))),m.apply(this,arguments)}exports.cacheInTransformer=function(){return{transformResponse:function(e,r,t){return i(regeneratorRuntime.mark((function n(){var o,s;return regeneratorRuntime.wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return r.useCache&&!t.cached&&(o=e.$in.httpClient.cache,s=new l(c.copyResponse(t),r.ttl),o.set(h(r),s)),n.abrupt("return",[r,t]);case 2:case"end":return n.stop()}}),n)})))()}}},exports.cacheOutTransformer=function(){return{transformRequest:function(e,r,t){return i(regeneratorRuntime.mark((function o(){var s;return regeneratorRuntime.wrap((function(o){for(;;)switch(o.prev=o.next){case 0:if(!r.useCache){o.next=4;break}if(!(s=e.httpCache.get(h(r)))){o.next=4;break}return o.abrupt("return",[r,n(n({},s),{},{cached:!0})]);case 4:return o.abrupt("return",[r,t]);case 5:case"end":return o.stop()}}),o)})))()}}},exports.getCacheKey=h,exports.httpCachePlugin=function(){return{setup:function(r){return i(regeneratorRuntime.mark((function t(){return regeneratorRuntime.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(f!==p||r.$in.httpClient){t.next=2;break}throw new Error("You must setup plugin @merkur/plugin-http-client before @merkur/plugin-http-cache");case 2:return(r=n(n({},{httpCache:{get:function(e,r){var t=e.$in.httpClient.cache,n=t.get(r);return n&&!n.isExpired()?c.copyResponse(n.value):null},serialize:function(r){var t,n=r.$in.httpClient.cache,o={},s=e(n.keys());try{for(s.s();!(t=s.n()).done;){var i=t.value,u=n.get(i);u instanceof l&&(o[i]=u.serialize())}}catch(e){s.e(e)}finally{s.f()}return JSON.stringify(o).replace(/<\/script/gi,"<\\/script")},deserialize:function(e,r){var t=e.$in.httpClient.cache,n={};try{n=JSON.parse(r)}catch(e){console.warn("Failed to parse http cache data",e)}for(var o=0,s=Object.keys(n);o<s.length;o++){var i=s[o],u=n[i],a=u.value,c=u.ttl;"Infinity"===c&&(c=1/0),t.set(i,new l(a,c))}}}}),r)).$in.httpClient.cache=new Map,t.abrupt("return",r);case 5:case"end":return t.stop()}}),t)})))()},create:function(e){return i(regeneratorRuntime.mark((function r(){var t,n,o,s;return regeneratorRuntime.wrap((function(r){for(;;)switch(r.prev=r.next){case 0:return t=e.$in.httpClient.defaultConfig,n=t.useCache,o=t.transferServerCache,s=t.ttl,a.bindWidgetToFunctions(e,e.httpCache),c.setDefaultConfig(e,{useCache:null==n||n,transferServerCache:null!=o&&o,ttl:null!=s?s:6e4}),o&&(a.hookMethod(e,"info",d),e.$httpCache&&e.httpCache.deserialize(e.$httpCache)),r.abrupt("return",e);case 5:case"end":return r.stop()}}),r)})))()}}};
|
|
1
|
+
"use strict";function e(t){return e="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},e(t)}function t(e,t){var n="undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(!n){if(Array.isArray(e)||(n=function(e,t){if(!e)return;if("string"==typeof e)return r(e,t);var n=Object.prototype.toString.call(e).slice(8,-1);"Object"===n&&e.constructor&&(n=e.constructor.name);if("Map"===n||"Set"===n)return Array.from(e);if("Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n))return r(e,t)}(e))||t&&e&&"number"==typeof e.length){n&&(e=n);var o=0,i=function(){};return{s:i,n:function(){return o>=e.length?{done:!0}:{done:!1,value:e[o++]}},e:function(e){throw e},f:i}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var a,c=!0,u=!1;return{s:function(){n=n.call(e)},n:function(){var e=n.next();return c=e.done,e},e:function(e){u=!0,a=e},f:function(){try{c||null==n.return||n.return()}finally{if(u)throw a}}}}function r(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,n=new Array(t);r<t;r++)n[r]=e[r];return n}function n(){/*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */n=function(){return t};var t={},r=Object.prototype,o=r.hasOwnProperty,i=Object.defineProperty||function(e,t,r){e[t]=r.value},a="function"==typeof Symbol?Symbol:{},c=a.iterator||"@@iterator",u=a.asyncIterator||"@@asyncIterator",s=a.toStringTag||"@@toStringTag";function l(e,t,r){return Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}),e[t]}try{l({},"")}catch(e){l=function(e,t,r){return e[t]=r}}function f(e,t,r,n){var o=t&&t.prototype instanceof y?t:y,a=Object.create(o.prototype),c=new L(n||[]);return i(a,"_invoke",{value:O(e,r,c)}),a}function h(e,t,r){try{return{type:"normal",arg:e.call(t,r)}}catch(e){return{type:"throw",arg:e}}}t.wrap=f;var p={};function y(){}function d(){}function v(){}var m={};l(m,c,(function(){return this}));var g=Object.getPrototypeOf,j=g&&g(g(C([])));j&&j!==r&&o.call(j,c)&&(m=j);var b=v.prototype=y.prototype=Object.create(m);function w(e){["next","throw","return"].forEach((function(t){l(e,t,(function(e){return this._invoke(t,e)}))}))}function x(t,r){function n(i,a,c,u){var s=h(t[i],t,a);if("throw"!==s.type){var l=s.arg,f=l.value;return f&&"object"==e(f)&&o.call(f,"__await")?r.resolve(f.__await).then((function(e){n("next",e,c,u)}),(function(e){n("throw",e,c,u)})):r.resolve(f).then((function(e){l.value=e,c(l)}),(function(e){return n("throw",e,c,u)}))}u(s.arg)}var a;i(this,"_invoke",{value:function(e,t){function o(){return new r((function(r,o){n(e,t,r,o)}))}return a=a?a.then(o,o):o()}})}function O(e,t,r){var n="suspendedStart";return function(o,i){if("executing"===n)throw new Error("Generator is already running");if("completed"===n){if("throw"===o)throw i;return _()}for(r.method=o,r.arg=i;;){var a=r.delegate;if(a){var c=q(a,r);if(c){if(c===p)continue;return c}}if("next"===r.method)r.sent=r._sent=r.arg;else if("throw"===r.method){if("suspendedStart"===n)throw n="completed",r.arg;r.dispatchException(r.arg)}else"return"===r.method&&r.abrupt("return",r.arg);n="executing";var u=h(e,t,r);if("normal"===u.type){if(n=r.done?"completed":"suspendedYield",u.arg===p)continue;return{value:u.arg,done:r.done}}"throw"===u.type&&(n="completed",r.method="throw",r.arg=u.arg)}}}function q(e,t){var r=e.iterator[t.method];if(void 0===r){if(t.delegate=null,"throw"===t.method){if(e.iterator.return&&(t.method="return",t.arg=void 0,q(e,t),"throw"===t.method))return p;t.method="throw",t.arg=new TypeError("The iterator does not provide a 'throw' method")}return p}var n=h(r,e.iterator,t.arg);if("throw"===n.type)return t.method="throw",t.arg=n.arg,t.delegate=null,p;var o=n.arg;return o?o.done?(t[e.resultName]=o.value,t.next=e.nextLoc,"return"!==t.method&&(t.method="next",t.arg=void 0),t.delegate=null,p):o:(t.method="throw",t.arg=new TypeError("iterator result is not an object"),t.delegate=null,p)}function E(e){var t={tryLoc:e[0]};1 in e&&(t.catchLoc=e[1]),2 in e&&(t.finallyLoc=e[2],t.afterLoc=e[3]),this.tryEntries.push(t)}function k(e){var t=e.completion||{};t.type="normal",delete t.arg,e.completion=t}function L(e){this.tryEntries=[{tryLoc:"root"}],e.forEach(E,this),this.reset(!0)}function C(e){if(e){var t=e[c];if(t)return t.call(e);if("function"==typeof e.next)return e;if(!isNaN(e.length)){var r=-1,n=function t(){for(;++r<e.length;)if(o.call(e,r))return t.value=e[r],t.done=!1,t;return t.value=void 0,t.done=!0,t};return n.next=n}}return{next:_}}function _(){return{value:void 0,done:!0}}return d.prototype=v,i(b,"constructor",{value:v,configurable:!0}),i(v,"constructor",{value:d,configurable:!0}),d.displayName=l(v,s,"GeneratorFunction"),t.isGeneratorFunction=function(e){var t="function"==typeof e&&e.constructor;return!!t&&(t===d||"GeneratorFunction"===(t.displayName||t.name))},t.mark=function(e){return Object.setPrototypeOf?Object.setPrototypeOf(e,v):(e.__proto__=v,l(e,s,"GeneratorFunction")),e.prototype=Object.create(b),e},t.awrap=function(e){return{__await:e}},w(x.prototype),l(x.prototype,u,(function(){return this})),t.AsyncIterator=x,t.async=function(e,r,n,o,i){void 0===i&&(i=Promise);var a=new x(f(e,r,n,o),i);return t.isGeneratorFunction(r)?a:a.next().then((function(e){return e.done?e.value:a.next()}))},w(b),l(b,s,"Generator"),l(b,c,(function(){return this})),l(b,"toString",(function(){return"[object Generator]"})),t.keys=function(e){var t=Object(e),r=[];for(var n in t)r.push(n);return r.reverse(),function e(){for(;r.length;){var n=r.pop();if(n in t)return e.value=n,e.done=!1,e}return e.done=!0,e}},t.values=C,L.prototype={constructor:L,reset:function(e){if(this.prev=0,this.next=0,this.sent=this._sent=void 0,this.done=!1,this.delegate=null,this.method="next",this.arg=void 0,this.tryEntries.forEach(k),!e)for(var t in this)"t"===t.charAt(0)&&o.call(this,t)&&!isNaN(+t.slice(1))&&(this[t]=void 0)},stop:function(){this.done=!0;var e=this.tryEntries[0].completion;if("throw"===e.type)throw e.arg;return this.rval},dispatchException:function(e){if(this.done)throw e;var t=this;function r(r,n){return a.type="throw",a.arg=e,t.next=r,n&&(t.method="next",t.arg=void 0),!!n}for(var n=this.tryEntries.length-1;n>=0;--n){var i=this.tryEntries[n],a=i.completion;if("root"===i.tryLoc)return r("end");if(i.tryLoc<=this.prev){var c=o.call(i,"catchLoc"),u=o.call(i,"finallyLoc");if(c&&u){if(this.prev<i.catchLoc)return r(i.catchLoc,!0);if(this.prev<i.finallyLoc)return r(i.finallyLoc)}else if(c){if(this.prev<i.catchLoc)return r(i.catchLoc,!0)}else{if(!u)throw new Error("try statement without catch or finally");if(this.prev<i.finallyLoc)return r(i.finallyLoc)}}}},abrupt:function(e,t){for(var r=this.tryEntries.length-1;r>=0;--r){var n=this.tryEntries[r];if(n.tryLoc<=this.prev&&o.call(n,"finallyLoc")&&this.prev<n.finallyLoc){var i=n;break}}i&&("break"===e||"continue"===e)&&i.tryLoc<=t&&t<=i.finallyLoc&&(i=null);var a=i?i.completion:{};return a.type=e,a.arg=t,i?(this.method="next",this.next=i.finallyLoc,p):this.complete(a)},complete:function(e,t){if("throw"===e.type)throw e.arg;return"break"===e.type||"continue"===e.type?this.next=e.arg:"return"===e.type?(this.rval=this.arg=e.arg,this.method="return",this.next="end"):"normal"===e.type&&t&&(this.next=t),p},finish:function(e){for(var t=this.tryEntries.length-1;t>=0;--t){var r=this.tryEntries[t];if(r.finallyLoc===e)return this.complete(r.completion,r.afterLoc),k(r),p}},catch:function(e){for(var t=this.tryEntries.length-1;t>=0;--t){var r=this.tryEntries[t];if(r.tryLoc===e){var n=r.completion;if("throw"===n.type){var o=n.arg;k(r)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(e,t,r){return this.delegate={iterator:C(e),resultName:t,nextLoc:r},"next"===this.method&&(this.arg=void 0),p}},t}function o(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);t&&(n=n.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),r.push.apply(r,n)}return r}function i(e){for(var t=1;t<arguments.length;t++){var r=null!=arguments[t]?arguments[t]:{};t%2?o(Object(r),!0).forEach((function(t){a(e,t,r[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(r)):o(Object(r)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(r,t))}))}return e}function a(e,t,r){return t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function c(e,t,r,n,o,i,a){try{var c=e[i](a),u=c.value}catch(e){return void r(e)}c.done?t(u):Promise.resolve(u).then(n,o)}function u(e){return function(){var t=this,r=arguments;return new Promise((function(n,o){var i=e.apply(t,r);function a(e){c(i,n,o,a,u,"next",e)}function u(e){c(i,n,o,a,u,"throw",e)}a(void 0)}))}}function s(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}require("core-js/modules/es.object.define-property.js"),require("core-js/modules/es.date.now.js"),require("core-js/modules/es.date.to-string.js"),require("core-js/modules/es.array.index-of.js"),require("core-js/modules/es.regexp.exec.js"),require("core-js/modules/es.string.replace.js"),require("core-js/modules/es.array.concat.js"),require("core-js/modules/es.array.iterator.js"),require("core-js/modules/es.map.js"),require("core-js/modules/es.object.to-string.js"),require("core-js/modules/es.string.iterator.js"),require("core-js/modules/web.dom-collections.iterator.js"),require("core-js/modules/es.object.keys.js"),require("core-js/modules/es.promise.js"),require("core-js/modules/es.symbol.js"),require("core-js/modules/es.array.filter.js"),require("core-js/modules/es.object.get-own-property-descriptor.js"),require("core-js/modules/es.array.for-each.js"),require("core-js/modules/web.dom-collections.for-each.js"),require("core-js/modules/es.object.get-own-property-descriptors.js"),require("core-js/modules/es.object.define-properties.js"),require("core-js/modules/es.symbol.description.js"),require("core-js/modules/es.symbol.iterator.js"),require("core-js/modules/es.symbol.async-iterator.js"),require("core-js/modules/es.symbol.to-string-tag.js"),require("core-js/modules/es.json.to-string-tag.js"),require("core-js/modules/es.math.to-string-tag.js"),require("core-js/modules/es.object.create.js"),require("core-js/modules/es.object.get-prototype-of.js"),require("core-js/modules/es.function.name.js"),require("core-js/modules/es.object.set-prototype-of.js"),require("core-js/modules/es.array.reverse.js"),require("core-js/modules/es.array.slice.js"),require("core-js/modules/es.array.from.js"),require("core-js/modules/es.array.is-array.js"),Object.defineProperty(exports,"__esModule",{value:!0});var l=require("@merkur/core"),f=require("@merkur/plugin-http-client"),h=function(){function e(t,r){!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),this._value=t,this._ttl=r,this._created=Date.now()}var t,r,n;return t=e,(r=[{key:"value",get:function(){return this._value}},{key:"isExpired",value:function(){return Date.now()>this._created+this._ttl}},{key:"serialize",value:function(){return{value:this.value,ttl:this._ttl===1/0?"Infinity":this._ttl}}}])&&s(t.prototype,r),n&&s(t,n),Object.defineProperty(t,"prototype",{writable:!1}),e}(),p="development",y="undefined"!=typeof process&&process&&process.env?process.env.NODE_ENV:p;function d(e){var t=e.method,r=e.url,n=e.body,o=e.query,i=~["GET","HEAD"].indexOf(t)?o:n,a="";if(i)try{a=JSON.stringify(i).replace(/<\/script/gi,"<\\/script")}catch(e){console.warn("The provided data does not have valid JSON format",i)}return"".concat(t,":").concat(r,"?").concat(a)}function v(e,t){return m.apply(this,arguments)}function m(){return m=u(n().mark((function e(t,r){var o,a,c,u,s=arguments;return n().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:for(o=s.length,a=new Array(o>2?o-2:0),c=2;c<o;c++)a[c-2]=s[c];return e.next=3,r.apply(void 0,a);case 3:return u=e.sent,e.abrupt("return",i(i({},u),{},{$httpCache:t.httpCache.serialize()}));case 5:case"end":return e.stop()}}),e)}))),m.apply(this,arguments)}exports.cacheInTransformer=function(){return{transformResponse:function(e,t,r){return u(n().mark((function o(){var i,a;return n().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return t.useCache&&!r.cached&&(i=e.$in.httpClient.cache,a=new h(f.copyResponse(r),t.ttl),i.set(d(t),a)),n.abrupt("return",[t,r]);case 2:case"end":return n.stop()}}),o)})))()}}},exports.cacheOutTransformer=function(){return{transformRequest:function(e,t,r){return u(n().mark((function o(){var a;return n().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:if(!t.useCache){n.next=4;break}if(!(a=e.httpCache.get(d(t)))){n.next=4;break}return n.abrupt("return",[t,i(i({},a),{},{cached:!0})]);case 4:return n.abrupt("return",[t,r]);case 5:case"end":return n.stop()}}),o)})))()}}},exports.getCacheKey=d,exports.httpCachePlugin=function(){return{setup:function(e){return u(n().mark((function r(){return n().wrap((function(r){for(;;)switch(r.prev=r.next){case 0:if(y!==p||e.$in.httpClient){r.next=2;break}throw new Error("You must setup plugin @merkur/plugin-http-client before @merkur/plugin-http-cache");case 2:return(e=i(i({},{httpCache:{get:function(e,t){var r=e.$in.httpClient.cache,n=r.get(t);return n&&!n.isExpired()?f.copyResponse(n.value):null},serialize:function(e){var r,n=e.$in.httpClient.cache,o={},i=t(n.keys());try{for(i.s();!(r=i.n()).done;){var a=r.value,c=n.get(a);c instanceof h&&(o[a]=c.serialize())}}catch(e){i.e(e)}finally{i.f()}return JSON.stringify(o).replace(/<\/script/gi,"<\\/script")},deserialize:function(e,t){var r=e.$in.httpClient.cache,n={};try{n=JSON.parse(t)}catch(e){console.warn("Failed to parse http cache data",e)}for(var o=0,i=Object.keys(n);o<i.length;o++){var a=i[o],c=n[a],u=c.value,s=c.ttl;"Infinity"===s&&(s=1/0),r.set(a,new h(u,s))}}}}),e)).$in.httpClient.cache=new Map,r.abrupt("return",e);case 5:case"end":return r.stop()}}),r)})))()},create:function(e){return u(n().mark((function t(){var r,o,i,a;return n().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=e.$in.httpClient.defaultConfig,o=r.useCache,i=r.transferServerCache,a=r.ttl,l.bindWidgetToFunctions(e,e.httpCache),f.setDefaultConfig(e,{useCache:null==o||o,transferServerCache:null!=i&&i,ttl:null!=a?a:6e4}),i&&(l.hookMethod(e,"info",v),e.$httpCache&&e.httpCache.deserialize(e.$httpCache)),t.abrupt("return",e);case 5:case"end":return t.stop()}}),t)})))()}}};
|
package/lib/index.es9.cjs
CHANGED
|
@@ -3,16 +3,13 @@
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
|
|
7
6
|
var core = require('@merkur/core');
|
|
8
|
-
|
|
9
7
|
var pluginHttpClient = require('@merkur/plugin-http-client');
|
|
8
|
+
|
|
10
9
|
/**
|
|
11
10
|
* The cache entry is a typed container of cache data used to track the
|
|
12
11
|
* creation and expiration of cache entries.
|
|
13
12
|
*/
|
|
14
|
-
|
|
15
|
-
|
|
16
13
|
class CacheEntry {
|
|
17
14
|
/**
|
|
18
15
|
* Initializes the cache entry.
|
|
@@ -25,47 +22,41 @@ class CacheEntry {
|
|
|
25
22
|
this._ttl = ttl;
|
|
26
23
|
this._created = Date.now();
|
|
27
24
|
}
|
|
25
|
+
|
|
28
26
|
/**
|
|
29
27
|
* Returns the entry value.
|
|
30
28
|
*
|
|
31
29
|
* @return {*} The entry value.
|
|
32
30
|
*/
|
|
33
|
-
|
|
34
|
-
|
|
35
31
|
get value() {
|
|
36
32
|
return this._value;
|
|
37
33
|
}
|
|
34
|
+
|
|
38
35
|
/**
|
|
39
36
|
* Returns `true` if this entry has expired.
|
|
40
37
|
*
|
|
41
38
|
* @return {boolean} `true` if this entry has expired.
|
|
42
39
|
*/
|
|
43
|
-
|
|
44
|
-
|
|
45
40
|
isExpired() {
|
|
46
41
|
let now = Date.now();
|
|
47
42
|
return now > this._created + this._ttl;
|
|
48
43
|
}
|
|
44
|
+
|
|
49
45
|
/**
|
|
50
46
|
* Exports this cache entry into a JSON-serializable object.
|
|
51
47
|
*
|
|
52
48
|
* @return {{value: *, ttl: number}} This entry exported to a
|
|
53
49
|
* JSON-serializable object.
|
|
54
50
|
*/
|
|
55
|
-
|
|
56
|
-
|
|
57
51
|
serialize() {
|
|
58
52
|
return {
|
|
59
53
|
value: this.value,
|
|
60
54
|
ttl: this._ttl === Infinity ? 'Infinity' : this._ttl
|
|
61
55
|
};
|
|
62
56
|
}
|
|
63
|
-
|
|
64
57
|
}
|
|
65
|
-
|
|
66
58
|
const DEV = 'development';
|
|
67
59
|
const ENV = typeof process !== 'undefined' && process && process.env ? process.env.NODE_ENV : DEV;
|
|
68
|
-
|
|
69
60
|
function getCacheKey({
|
|
70
61
|
method,
|
|
71
62
|
url,
|
|
@@ -74,7 +65,6 @@ function getCacheKey({
|
|
|
74
65
|
}) {
|
|
75
66
|
const data = ~['GET', 'HEAD'].indexOf(method) ? query : body;
|
|
76
67
|
let dataQuery = '';
|
|
77
|
-
|
|
78
68
|
if (data) {
|
|
79
69
|
try {
|
|
80
70
|
dataQuery = JSON.stringify(data).replace(/<\/script/gi, '<\\/script');
|
|
@@ -82,24 +72,21 @@ function getCacheKey({
|
|
|
82
72
|
console.warn('The provided data does not have valid JSON format', data);
|
|
83
73
|
}
|
|
84
74
|
}
|
|
85
|
-
|
|
86
75
|
return `${method}:${url}?${dataQuery}`;
|
|
87
76
|
}
|
|
88
|
-
|
|
89
77
|
function httpCachePlugin() {
|
|
90
78
|
return {
|
|
91
79
|
async setup(widget) {
|
|
92
80
|
if (ENV === DEV && !widget.$in.httpClient) {
|
|
93
81
|
throw new Error('You must setup plugin @merkur/plugin-http-client before @merkur/plugin-http-cache');
|
|
94
82
|
}
|
|
95
|
-
|
|
96
|
-
|
|
83
|
+
widget = {
|
|
84
|
+
...httpCacheAPI(),
|
|
97
85
|
...widget
|
|
98
86
|
};
|
|
99
87
|
widget.$in.httpClient.cache = new Map();
|
|
100
88
|
return widget;
|
|
101
89
|
},
|
|
102
|
-
|
|
103
90
|
async create(widget) {
|
|
104
91
|
const {
|
|
105
92
|
useCache,
|
|
@@ -112,21 +99,16 @@ function httpCachePlugin() {
|
|
|
112
99
|
transferServerCache: transferServerCache !== null && transferServerCache !== void 0 ? transferServerCache : false,
|
|
113
100
|
ttl: ttl !== null && ttl !== void 0 ? ttl : 60000
|
|
114
101
|
});
|
|
115
|
-
|
|
116
102
|
if (transferServerCache) {
|
|
117
103
|
core.hookMethod(widget, 'info', infoHook);
|
|
118
|
-
|
|
119
104
|
if (widget.$httpCache) {
|
|
120
105
|
widget.httpCache.deserialize(widget.$httpCache);
|
|
121
106
|
}
|
|
122
107
|
}
|
|
123
|
-
|
|
124
108
|
return widget;
|
|
125
109
|
}
|
|
126
|
-
|
|
127
110
|
};
|
|
128
111
|
}
|
|
129
|
-
|
|
130
112
|
function cacheInTransformer() {
|
|
131
113
|
return {
|
|
132
114
|
async transformResponse(widget, request, response) {
|
|
@@ -137,32 +119,26 @@ function cacheInTransformer() {
|
|
|
137
119
|
const cacheEntry = new CacheEntry(pluginHttpClient.copyResponse(response), request.ttl);
|
|
138
120
|
cache.set(getCacheKey(request), cacheEntry);
|
|
139
121
|
}
|
|
140
|
-
|
|
141
122
|
return [request, response];
|
|
142
123
|
}
|
|
143
|
-
|
|
144
124
|
};
|
|
145
125
|
}
|
|
146
|
-
|
|
147
126
|
function cacheOutTransformer() {
|
|
148
127
|
return {
|
|
149
128
|
async transformRequest(widget, request, response) {
|
|
150
129
|
if (request.useCache) {
|
|
151
130
|
const cachedResponse = widget.httpCache.get(getCacheKey(request));
|
|
152
|
-
|
|
153
131
|
if (cachedResponse) {
|
|
154
|
-
return [request, {
|
|
132
|
+
return [request, {
|
|
133
|
+
...cachedResponse,
|
|
155
134
|
cached: true
|
|
156
135
|
}];
|
|
157
136
|
}
|
|
158
137
|
}
|
|
159
|
-
|
|
160
138
|
return [request, response];
|
|
161
139
|
}
|
|
162
|
-
|
|
163
140
|
};
|
|
164
141
|
}
|
|
165
|
-
|
|
166
142
|
function httpCacheAPI() {
|
|
167
143
|
return {
|
|
168
144
|
httpCache: {
|
|
@@ -173,61 +149,50 @@ function httpCacheAPI() {
|
|
|
173
149
|
const cacheEntry = cache.get(key);
|
|
174
150
|
return cacheEntry && !cacheEntry.isExpired() ? pluginHttpClient.copyResponse(cacheEntry.value) : null;
|
|
175
151
|
},
|
|
176
|
-
|
|
177
152
|
serialize(widget) {
|
|
178
153
|
const {
|
|
179
154
|
cache
|
|
180
155
|
} = widget.$in.httpClient;
|
|
181
156
|
const serializedData = {};
|
|
182
|
-
|
|
183
157
|
for (const key of cache.keys()) {
|
|
184
158
|
const entry = cache.get(key);
|
|
185
|
-
|
|
186
159
|
if (entry instanceof CacheEntry) {
|
|
187
160
|
serializedData[key] = entry.serialize();
|
|
188
161
|
}
|
|
189
162
|
}
|
|
190
|
-
|
|
191
163
|
return JSON.stringify(serializedData).replace(/<\/script/gi, '<\\/script');
|
|
192
164
|
},
|
|
193
|
-
|
|
194
165
|
deserialize(widget, serializedData) {
|
|
195
166
|
const {
|
|
196
167
|
cache
|
|
197
168
|
} = widget.$in.httpClient;
|
|
198
169
|
let parsedData = {};
|
|
199
|
-
|
|
200
170
|
try {
|
|
201
171
|
parsedData = JSON.parse(serializedData);
|
|
202
172
|
} catch (error) {
|
|
203
173
|
console.warn('Failed to parse http cache data', error);
|
|
204
174
|
}
|
|
205
|
-
|
|
206
175
|
for (const key of Object.keys(parsedData)) {
|
|
207
176
|
let {
|
|
208
177
|
value,
|
|
209
178
|
ttl
|
|
210
179
|
} = parsedData[key];
|
|
211
|
-
|
|
212
180
|
if (ttl === 'Infinity') {
|
|
213
181
|
ttl = Infinity;
|
|
214
182
|
}
|
|
215
|
-
|
|
216
183
|
cache.set(key, new CacheEntry(value, ttl));
|
|
217
184
|
}
|
|
218
185
|
}
|
|
219
|
-
|
|
220
186
|
}
|
|
221
187
|
};
|
|
222
188
|
}
|
|
223
|
-
|
|
224
189
|
async function infoHook(widget, originalInfoFn, ...rest) {
|
|
225
190
|
const originalInfo = await originalInfoFn(...rest);
|
|
226
|
-
return {
|
|
191
|
+
return {
|
|
192
|
+
...originalInfo,
|
|
227
193
|
$httpCache: widget.httpCache.serialize()
|
|
228
194
|
};
|
|
229
195
|
}
|
|
230
|
-
|
|
231
196
|
exports.cacheInTransformer = cacheInTransformer;
|
|
232
197
|
exports.cacheOutTransformer = cacheOutTransformer;
|
|
233
198
|
exports.getCacheKey = getCacheKey;
|
package/lib/index.es9.mjs
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { bindWidgetToFunctions, hookMethod } from '@merkur/core';
|
|
2
|
-
import {
|
|
2
|
+
import { setDefaultConfig, copyResponse } from '@merkur/plugin-http-client';
|
|
3
|
+
|
|
3
4
|
/**
|
|
4
5
|
* The cache entry is a typed container of cache data used to track the
|
|
5
6
|
* creation and expiration of cache entries.
|
|
6
7
|
*/
|
|
7
|
-
|
|
8
8
|
class CacheEntry {
|
|
9
9
|
/**
|
|
10
10
|
* Initializes the cache entry.
|
|
@@ -17,47 +17,41 @@ class CacheEntry {
|
|
|
17
17
|
this._ttl = ttl;
|
|
18
18
|
this._created = Date.now();
|
|
19
19
|
}
|
|
20
|
+
|
|
20
21
|
/**
|
|
21
22
|
* Returns the entry value.
|
|
22
23
|
*
|
|
23
24
|
* @return {*} The entry value.
|
|
24
25
|
*/
|
|
25
|
-
|
|
26
|
-
|
|
27
26
|
get value() {
|
|
28
27
|
return this._value;
|
|
29
28
|
}
|
|
29
|
+
|
|
30
30
|
/**
|
|
31
31
|
* Returns `true` if this entry has expired.
|
|
32
32
|
*
|
|
33
33
|
* @return {boolean} `true` if this entry has expired.
|
|
34
34
|
*/
|
|
35
|
-
|
|
36
|
-
|
|
37
35
|
isExpired() {
|
|
38
36
|
let now = Date.now();
|
|
39
37
|
return now > this._created + this._ttl;
|
|
40
38
|
}
|
|
39
|
+
|
|
41
40
|
/**
|
|
42
41
|
* Exports this cache entry into a JSON-serializable object.
|
|
43
42
|
*
|
|
44
43
|
* @return {{value: *, ttl: number}} This entry exported to a
|
|
45
44
|
* JSON-serializable object.
|
|
46
45
|
*/
|
|
47
|
-
|
|
48
|
-
|
|
49
46
|
serialize() {
|
|
50
47
|
return {
|
|
51
48
|
value: this.value,
|
|
52
49
|
ttl: this._ttl === Infinity ? 'Infinity' : this._ttl
|
|
53
50
|
};
|
|
54
51
|
}
|
|
55
|
-
|
|
56
52
|
}
|
|
57
|
-
|
|
58
53
|
const DEV = 'development';
|
|
59
54
|
const ENV = typeof process !== 'undefined' && process && process.env ? process.env.NODE_ENV : DEV;
|
|
60
|
-
|
|
61
55
|
function getCacheKey({
|
|
62
56
|
method,
|
|
63
57
|
url,
|
|
@@ -66,7 +60,6 @@ function getCacheKey({
|
|
|
66
60
|
}) {
|
|
67
61
|
const data = ~['GET', 'HEAD'].indexOf(method) ? query : body;
|
|
68
62
|
let dataQuery = '';
|
|
69
|
-
|
|
70
63
|
if (data) {
|
|
71
64
|
try {
|
|
72
65
|
dataQuery = JSON.stringify(data).replace(/<\/script/gi, '<\\/script');
|
|
@@ -74,24 +67,21 @@ function getCacheKey({
|
|
|
74
67
|
console.warn('The provided data does not have valid JSON format', data);
|
|
75
68
|
}
|
|
76
69
|
}
|
|
77
|
-
|
|
78
70
|
return `${method}:${url}?${dataQuery}`;
|
|
79
71
|
}
|
|
80
|
-
|
|
81
72
|
function httpCachePlugin() {
|
|
82
73
|
return {
|
|
83
74
|
async setup(widget) {
|
|
84
75
|
if (ENV === DEV && !widget.$in.httpClient) {
|
|
85
76
|
throw new Error('You must setup plugin @merkur/plugin-http-client before @merkur/plugin-http-cache');
|
|
86
77
|
}
|
|
87
|
-
|
|
88
|
-
|
|
78
|
+
widget = {
|
|
79
|
+
...httpCacheAPI(),
|
|
89
80
|
...widget
|
|
90
81
|
};
|
|
91
82
|
widget.$in.httpClient.cache = new Map();
|
|
92
83
|
return widget;
|
|
93
84
|
},
|
|
94
|
-
|
|
95
85
|
async create(widget) {
|
|
96
86
|
const {
|
|
97
87
|
useCache,
|
|
@@ -104,21 +94,16 @@ function httpCachePlugin() {
|
|
|
104
94
|
transferServerCache: transferServerCache !== null && transferServerCache !== void 0 ? transferServerCache : false,
|
|
105
95
|
ttl: ttl !== null && ttl !== void 0 ? ttl : 60000
|
|
106
96
|
});
|
|
107
|
-
|
|
108
97
|
if (transferServerCache) {
|
|
109
98
|
hookMethod(widget, 'info', infoHook);
|
|
110
|
-
|
|
111
99
|
if (widget.$httpCache) {
|
|
112
100
|
widget.httpCache.deserialize(widget.$httpCache);
|
|
113
101
|
}
|
|
114
102
|
}
|
|
115
|
-
|
|
116
103
|
return widget;
|
|
117
104
|
}
|
|
118
|
-
|
|
119
105
|
};
|
|
120
106
|
}
|
|
121
|
-
|
|
122
107
|
function cacheInTransformer() {
|
|
123
108
|
return {
|
|
124
109
|
async transformResponse(widget, request, response) {
|
|
@@ -129,32 +114,26 @@ function cacheInTransformer() {
|
|
|
129
114
|
const cacheEntry = new CacheEntry(copyResponse(response), request.ttl);
|
|
130
115
|
cache.set(getCacheKey(request), cacheEntry);
|
|
131
116
|
}
|
|
132
|
-
|
|
133
117
|
return [request, response];
|
|
134
118
|
}
|
|
135
|
-
|
|
136
119
|
};
|
|
137
120
|
}
|
|
138
|
-
|
|
139
121
|
function cacheOutTransformer() {
|
|
140
122
|
return {
|
|
141
123
|
async transformRequest(widget, request, response) {
|
|
142
124
|
if (request.useCache) {
|
|
143
125
|
const cachedResponse = widget.httpCache.get(getCacheKey(request));
|
|
144
|
-
|
|
145
126
|
if (cachedResponse) {
|
|
146
|
-
return [request, {
|
|
127
|
+
return [request, {
|
|
128
|
+
...cachedResponse,
|
|
147
129
|
cached: true
|
|
148
130
|
}];
|
|
149
131
|
}
|
|
150
132
|
}
|
|
151
|
-
|
|
152
133
|
return [request, response];
|
|
153
134
|
}
|
|
154
|
-
|
|
155
135
|
};
|
|
156
136
|
}
|
|
157
|
-
|
|
158
137
|
function httpCacheAPI() {
|
|
159
138
|
return {
|
|
160
139
|
httpCache: {
|
|
@@ -165,59 +144,48 @@ function httpCacheAPI() {
|
|
|
165
144
|
const cacheEntry = cache.get(key);
|
|
166
145
|
return cacheEntry && !cacheEntry.isExpired() ? copyResponse(cacheEntry.value) : null;
|
|
167
146
|
},
|
|
168
|
-
|
|
169
147
|
serialize(widget) {
|
|
170
148
|
const {
|
|
171
149
|
cache
|
|
172
150
|
} = widget.$in.httpClient;
|
|
173
151
|
const serializedData = {};
|
|
174
|
-
|
|
175
152
|
for (const key of cache.keys()) {
|
|
176
153
|
const entry = cache.get(key);
|
|
177
|
-
|
|
178
154
|
if (entry instanceof CacheEntry) {
|
|
179
155
|
serializedData[key] = entry.serialize();
|
|
180
156
|
}
|
|
181
157
|
}
|
|
182
|
-
|
|
183
158
|
return JSON.stringify(serializedData).replace(/<\/script/gi, '<\\/script');
|
|
184
159
|
},
|
|
185
|
-
|
|
186
160
|
deserialize(widget, serializedData) {
|
|
187
161
|
const {
|
|
188
162
|
cache
|
|
189
163
|
} = widget.$in.httpClient;
|
|
190
164
|
let parsedData = {};
|
|
191
|
-
|
|
192
165
|
try {
|
|
193
166
|
parsedData = JSON.parse(serializedData);
|
|
194
167
|
} catch (error) {
|
|
195
168
|
console.warn('Failed to parse http cache data', error);
|
|
196
169
|
}
|
|
197
|
-
|
|
198
170
|
for (const key of Object.keys(parsedData)) {
|
|
199
171
|
let {
|
|
200
172
|
value,
|
|
201
173
|
ttl
|
|
202
174
|
} = parsedData[key];
|
|
203
|
-
|
|
204
175
|
if (ttl === 'Infinity') {
|
|
205
176
|
ttl = Infinity;
|
|
206
177
|
}
|
|
207
|
-
|
|
208
178
|
cache.set(key, new CacheEntry(value, ttl));
|
|
209
179
|
}
|
|
210
180
|
}
|
|
211
|
-
|
|
212
181
|
}
|
|
213
182
|
};
|
|
214
183
|
}
|
|
215
|
-
|
|
216
184
|
async function infoHook(widget, originalInfoFn, ...rest) {
|
|
217
185
|
const originalInfo = await originalInfoFn(...rest);
|
|
218
|
-
return {
|
|
186
|
+
return {
|
|
187
|
+
...originalInfo,
|
|
219
188
|
$httpCache: widget.httpCache.serialize()
|
|
220
189
|
};
|
|
221
190
|
}
|
|
222
|
-
|
|
223
191
|
export { cacheInTransformer, cacheOutTransformer, getCacheKey, httpCachePlugin };
|
package/lib/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { bindWidgetToFunctions, hookMethod } from '@merkur/core';
|
|
2
|
-
import {
|
|
2
|
+
import { setDefaultConfig, copyResponse } from '@merkur/plugin-http-client';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* The cache entry is a typed container of cache data used to track the
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@merkur/plugin-http-cache",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.31.0",
|
|
4
4
|
"description": "Merkur plugin for maintaining http requests cache.",
|
|
5
5
|
"main": "lib/index",
|
|
6
6
|
"module": "lib/index",
|
|
@@ -48,14 +48,14 @@
|
|
|
48
48
|
},
|
|
49
49
|
"homepage": "https://merkur.js.org/",
|
|
50
50
|
"devDependencies": {
|
|
51
|
-
"@merkur/core": "^0.
|
|
52
|
-
"@merkur/plugin-component": "^0.
|
|
53
|
-
"@merkur/plugin-http-client": "^0.
|
|
51
|
+
"@merkur/core": "^0.31.0",
|
|
52
|
+
"@merkur/plugin-component": "^0.31.0",
|
|
53
|
+
"@merkur/plugin-http-client": "^0.31.0",
|
|
54
54
|
"rollup": "^2.70.2"
|
|
55
55
|
},
|
|
56
56
|
"peerDependencies": {
|
|
57
57
|
"@merkur/core": "^0.28.0",
|
|
58
58
|
"@merkur/plugin-http-client": "^0.28.1"
|
|
59
59
|
},
|
|
60
|
-
"gitHead": "
|
|
60
|
+
"gitHead": "ef1b38f23e43e2fd7ef6a2bec680fb40a2de3715"
|
|
61
61
|
}
|