moodle/lib/amd/build/storagewrapper.min.js.map
2023-03-09 09:53:19 +08:00

1 line
6.8 KiB
Plaintext

{"version":3,"file":"storagewrapper.min.js","sources":["../src/storagewrapper.js"],"sourcesContent":["// This file is part of Moodle - http://moodle.org/\n//\n// Moodle is free software: you can redistribute it and/or modify\n// it under the terms of the GNU General Public License as published by\n// the Free Software Foundation, either version 3 of the License, or\n// (at your option) any later version.\n//\n// Moodle is distributed in the hope that it will be useful,\n// but WITHOUT ANY WARRANTY; without even the implied warranty of\n// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n// GNU General Public License for more details.\n//\n// You should have received a copy of the GNU General Public License\n// along with Moodle. If not, see <http://www.gnu.org/licenses/>.\n\n/**\n * Wrap an instance of the browser's local or session storage to handle\n * cache expiry, key namespacing and other helpful things.\n *\n * @module core/storagewrapper\n * @copyright 2017 Ryan Wyllie <ryan@moodle.com>\n * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\ndefine(['core/config'], function(config) {\n\n /**\n * Constructor.\n *\n * @param {object} storage window.localStorage or window.sessionStorage\n */\n var Wrapper = function(storage) {\n this.storage = storage;\n this.supported = this.detectSupport();\n this.hashSource = config.wwwroot + '/' + config.jsrev;\n this.hash = this.hashString(this.hashSource);\n this.prefix = this.hash + '/';\n this.jsrevPrefix = this.hashString(config.wwwroot) + '/jsrev';\n this.validateCache();\n };\n\n /**\n * Check if the browser supports the type of storage.\n *\n * @method detectSupport\n * @return {boolean} True if the browser supports storage.\n */\n Wrapper.prototype.detectSupport = function() {\n if (config.jsrev == -1) {\n // Disable cache if debugging.\n return false;\n }\n if (typeof (this.storage) === \"undefined\") {\n return false;\n }\n var testKey = 'test';\n try {\n if (this.storage === null) {\n return false;\n }\n // MDL-51461 - Some browsers misreport availability of the storage\n // so check it is actually usable.\n this.storage.setItem(testKey, '1');\n this.storage.removeItem(testKey);\n return true;\n } catch (ex) {\n return false;\n }\n };\n\n /**\n * Add a unique prefix to all keys so multiple moodle sites do not share caches.\n *\n * @method prefixKey\n * @param {string} key The cache key to prefix.\n * @return {string} The new key\n */\n Wrapper.prototype.prefixKey = function(key) {\n return this.prefix + key;\n };\n\n /**\n * Check the current jsrev version and clear the cache if it has been bumped.\n *\n * @method validateCache\n */\n Wrapper.prototype.validateCache = function() {\n if (!this.supported) {\n return;\n }\n var cacheVersion = this.storage.getItem(this.jsrevPrefix);\n if (cacheVersion === null) {\n this.storage.setItem(this.jsrevPrefix, config.jsrev);\n return;\n }\n\n var moodleVersion = config.jsrev;\n if (moodleVersion != cacheVersion) {\n this.storage.clear();\n this.storage.setItem(this.jsrevPrefix, config.jsrev);\n }\n };\n\n /**\n * Hash a string, used to make shorter key prefixes.\n *\n * @method hashString\n * @param {String} source The string to hash\n * @return {Number}\n */\n Wrapper.prototype.hashString = function(source) {\n // From http://stackoverflow.com/questions/7616461/generate-a-hash-from-string-in-javascript-jquery.\n /* jshint bitwise: false */\n /* eslint no-bitwise: \"off\" */\n var hash = 0;\n var i, chr, len;\n if (source.length === 0) {\n return hash;\n }\n for (i = 0, len = source.length; i < len; i++) {\n chr = source.charCodeAt(i);\n hash = ((hash << 5) - hash) + chr;\n hash |= 0; // Convert to 32bit integer\n }\n return hash;\n };\n\n /**\n * Get a value from local storage. Remember - all values must be strings.\n *\n * @method get\n * @param {string} key The cache key to check.\n * @return {boolean|string} False if the value is not in the cache, or some other error - a string otherwise.\n */\n Wrapper.prototype.get = function(key) {\n if (!this.supported) {\n return false;\n }\n key = this.prefixKey(key);\n\n return this.storage.getItem(key);\n };\n\n /**\n * Set a value to local storage. Remember - all values must be strings.\n *\n * @method set\n * @param {string} key The cache key to set.\n * @param {string} value The value to set.\n * @return {boolean} False if the value can't be saved in the cache, or some other error - true otherwise.\n */\n Wrapper.prototype.set = function(key, value) {\n if (!this.supported) {\n return false;\n }\n key = this.prefixKey(key);\n // This can throw exceptions when the storage limit is reached.\n try {\n this.storage.setItem(key, value);\n } catch (e) {\n return false;\n }\n return true;\n };\n\n return Wrapper;\n});\n"],"names":["define","config","Wrapper","storage","supported","this","detectSupport","hashSource","wwwroot","jsrev","hash","hashString","prefix","jsrevPrefix","validateCache","prototype","setItem","removeItem","ex","prefixKey","key","cacheVersion","getItem","clear","source","i","len","length","charCodeAt","get","set","value","e"],"mappings":";;;;;;;;AAuBAA,6BAAO,CAAC,gBAAgB,SAASC,YAOzBC,QAAU,SAASC,cACdA,QAAUA,aACVC,UAAYC,KAAKC,qBACjBC,WAAaN,OAAOO,QAAU,IAAMP,OAAOQ,WAC3CC,KAAOL,KAAKM,WAAWN,KAAKE,iBAC5BK,OAASP,KAAKK,KAAO,SACrBG,YAAcR,KAAKM,WAAWV,OAAOO,SAAW,cAChDM,wBASTZ,QAAQa,UAAUT,cAAgB,eACT,GAAjBL,OAAOQ,aAEA,UAEmB,IAAlBJ,KAAKF,eACN,aAIc,OAAjBE,KAAKF,eAKJA,QAAQa,QAPH,OAOoB,UACzBb,QAAQc,WARH,SASH,GACT,MAAOC,WACE,IAWfhB,QAAQa,UAAUI,UAAY,SAASC,YAC5Bf,KAAKO,OAASQ,KAQzBlB,QAAQa,UAAUD,cAAgB,cACzBT,KAAKD,eAGNiB,aAAehB,KAAKF,QAAQmB,QAAQjB,KAAKQ,gBACxB,OAAjBQ,aAKgBpB,OAAOQ,OACNY,oBACZlB,QAAQoB,aACRpB,QAAQa,QAAQX,KAAKQ,YAAaZ,OAAOQ,kBAPzCN,QAAQa,QAAQX,KAAKQ,YAAaZ,OAAOQ,SAkBtDP,QAAQa,UAAUJ,WAAa,SAASa,YAKhCC,EAAQC,IADRhB,KAAO,KAEW,IAAlBc,OAAOG,cACAjB,SAENe,EAAI,EAAGC,IAAMF,OAAOG,OAAQF,EAAIC,IAAKD,IAEtCf,MAASA,MAAQ,GAAKA,KADhBc,OAAOI,WAAWH,GAExBf,MAAQ,SAELA,MAUXR,QAAQa,UAAUc,IAAM,SAAST,aACxBf,KAAKD,YAGVgB,IAAMf,KAAKc,UAAUC,KAEdf,KAAKF,QAAQmB,QAAQF,OAWhClB,QAAQa,UAAUe,IAAM,SAASV,IAAKW,WAC7B1B,KAAKD,iBACC,EAEXgB,IAAMf,KAAKc,UAAUC,cAGZjB,QAAQa,QAAQI,IAAKW,OAC5B,MAAOC,UACE,SAEJ,GAGJ9B"}