|
| 1 | +define(['exports'], function (exports) { 'use strict'; |
| 2 | + |
| 3 | +/** |
| 4 | + * Cache API key/value store - github.com/optimalisatie/Cache-API-Key-Value-Store |
| 5 | + * Released under the terms of MIT license |
| 6 | + * |
| 7 | + * Copyright (C) 2018 github.com/optimalisatie |
| 8 | + */ |
| 9 | +var defaultStore = '--keyval'; |
| 10 | +var dateHeader = 'x-d'; |
| 11 | +var expireHeader = 'x-e'; |
| 12 | +var contentTypeHeader = 'Content-Type'; |
| 13 | +var slashKey = '/'; |
| 14 | +// open store |
| 15 | +function getStore(store, key, callback, value, expire) { |
| 16 | + store = store || defaultStore; |
| 17 | + return caches.open(store).then(function (cache) { |
| 18 | + callback(cache, new Request(['', defaultStore, key || ''].join(slashKey)), value, expire); |
| 19 | + }); |
| 20 | +} |
| 21 | +// get entry from cache |
| 22 | +function cacheGet(cache, cache_key) { |
| 23 | + return cache.match(cache_key).then(function (cachedata) { |
| 24 | + // expired check |
| 25 | + if (cacheExpired(cache, cache_key, cachedata)) { |
| 26 | + return; |
| 27 | + } |
| 28 | + return cachedata.json(); |
| 29 | + }); |
| 30 | +} |
| 31 | +// delete entry from cache |
| 32 | +function cacheDel(cache, cache_key) { |
| 33 | + cacheDelete(cache, cache_key); |
| 34 | +} |
| 35 | +// set entry in cache |
| 36 | +function cacheSet(cache, cache_key, value, expire) { |
| 37 | + var headers = {}; |
| 38 | + // JSON |
| 39 | + headers[contentTypeHeader] = 'application/json'; |
| 40 | + value = JSON.stringify(value); |
| 41 | + // expire time |
| 42 | + if (IS_INT(expire)) { |
| 43 | + headers[dateHeader] = timestamp(); |
| 44 | + headers[expireHeader] = expire; |
| 45 | + } |
| 46 | + var data = new Response(data, { |
| 47 | + headers: headers |
| 48 | + }); |
| 49 | + return cache.put(cache_key, data); |
| 50 | +} |
| 51 | +// delete expired |
| 52 | +function cacheExpired(cache, cache_key, cachedata) { |
| 53 | + var headers = cachedata.headers; |
| 54 | + // expired check |
| 55 | + var exp = INT(headers.get(expireHeader)); |
| 56 | + var date = INT(headers.get(dateHeader)); |
| 57 | + if (IS_INT(exp)) { |
| 58 | + // expired |
| 59 | + if ((date + exp) < timestamp()) { |
| 60 | + cacheDelete(cache, cache_key); |
| 61 | + return 1; |
| 62 | + } |
| 63 | + } |
| 64 | +} |
| 65 | +// return keys |
| 66 | +function cacheKeys(cache) { |
| 67 | + return cache.keys().then(function (rawKeys) { |
| 68 | + var keys = []; |
| 69 | + rawKeys.forEach(function (key) { |
| 70 | + key = new URL(key.url).pathname.split(slashKey); |
| 71 | + key.shift(); |
| 72 | + key.shift(); |
| 73 | + keys.push(key.join(slashKey)); |
| 74 | + }); |
| 75 | + return keys; |
| 76 | + }); |
| 77 | +} |
| 78 | +// clear store |
| 79 | +function cacheClear(cache) { |
| 80 | + return cache.keys().then(function (keys) { |
| 81 | + keys.forEach(function (key) { |
| 82 | + cacheDelete(cache, key); |
| 83 | + }); |
| 84 | + }); |
| 85 | +} |
| 86 | +// prune expired entries |
| 87 | +function cachePrune(cache) { |
| 88 | + return cache.keys().then(function (keys) { |
| 89 | + // read all entries which automatically verifies the expire date |
| 90 | + keys.forEach(function (cache_key) { |
| 91 | + cacheGet(cache, cache_key); |
| 92 | + }); |
| 93 | + }); |
| 94 | +} |
| 95 | +// delete from cache |
| 96 | +function cacheDelete(cache, cache_key) { |
| 97 | + cache["delete"](cache_key); |
| 98 | +} |
| 99 | +// return timestamp |
| 100 | +function timestamp() { |
| 101 | + return Math.round(Date.now() / 1000); |
| 102 | +} |
| 103 | +// return INTEGER |
| 104 | +function INT(num) { |
| 105 | + return parseInt(num); |
| 106 | +} |
| 107 | +// return INTEGER |
| 108 | +function IS_INT(num) { |
| 109 | + return isNaN(num); |
| 110 | +} |
| 111 | +function get(key, store) { |
| 112 | + return getStore(store, key, cacheGet); |
| 113 | +} |
| 114 | +function set(key, value, expire, store) { |
| 115 | + return getStore(store, key, cacheSet, value, expire); |
| 116 | +} |
| 117 | +function del(key, store) { |
| 118 | + return getStore(store, key, cacheDel); |
| 119 | +} |
| 120 | +function clear(store) { |
| 121 | + return cacheClear(store); |
| 122 | +} |
| 123 | +function prune(store) { |
| 124 | + return cachePrune(store); |
| 125 | +} |
| 126 | +function keys(store) { |
| 127 | + return cacheKeys(store); |
| 128 | +} |
| 129 | + |
| 130 | +exports.get = get; |
| 131 | +exports.set = set; |
| 132 | +exports.del = del; |
| 133 | +exports.clear = clear; |
| 134 | +exports.prune = prune; |
| 135 | +exports.keys = keys; |
| 136 | + |
| 137 | +Object.defineProperty(exports, '__esModule', { value: true }); |
| 138 | + |
| 139 | +}); |
0 commit comments