money-clip
Version:
For managing your client side cache. Tiny wrapper over IndexedDB supporting versioning and max age.
292 lines (275 loc) • 8.82 kB
JavaScript
/**
* https://bugs.webkit.org/show_bug.cgi?id=226547
* Safari has a horrible bug where IDB requests can hang while the browser is starting up.
* The only solution is to keep nudging it until it's awake.
* This probably creates garbage, but garbage is better than totally failing.
*/
function idbReady() {
const isSafari = !navigator.userAgentData &&
/Safari\//.test(navigator.userAgent) &&
!/Chrom(e|ium)\//.test(navigator.userAgent);
// No point putting other browsers or older versions of Safari through this mess.
if (!isSafari || !indexedDB.databases)
return Promise.resolve();
let intervalId;
return new Promise((resolve) => {
const tryIdb = () => indexedDB.databases().finally(resolve);
intervalId = setInterval(tryIdb, 100);
tryIdb();
}).finally(() => clearInterval(intervalId));
}
function promisifyRequest(request) {
return new Promise((resolve, reject) => {
// @ts-ignore - file size hacks
request.oncomplete = request.onsuccess = () => resolve(request.result);
// @ts-ignore - file size hacks
request.onabort = request.onerror = () => reject(request.error);
});
}
function createStore(dbName, storeName) {
const dbp = idbReady().then(() => {
const request = indexedDB.open(dbName);
request.onupgradeneeded = () => request.result.createObjectStore(storeName);
return promisifyRequest(request);
});
return (txMode, callback) => dbp.then((db) => callback(db.transaction(storeName, txMode).objectStore(storeName)));
}
let defaultGetStoreFunc;
function defaultGetStore() {
if (!defaultGetStoreFunc) {
defaultGetStoreFunc = createStore('keyval-store', 'keyval');
}
return defaultGetStoreFunc;
}
/**
* Get a value by its key.
*
* @param key
* @param customStore Method to get a custom store. Use with caution (see the docs).
*/
function get(key, customStore = defaultGetStore()) {
return customStore('readonly', (store) => promisifyRequest(store.get(key)));
}
/**
* Set a value with a key.
*
* @param key
* @param value
* @param customStore Method to get a custom store. Use with caution (see the docs).
*/
function set(key, value, customStore = defaultGetStore()) {
return customStore('readwrite', (store) => {
store.put(value, key);
return promisifyRequest(store.transaction);
});
}
/**
* Set multiple values at once. This is faster than calling set() multiple times.
* It's also atomic – if one of the pairs can't be added, none will be added.
*
* @param entries Array of entries, where each entry is an array of `[key, value]`.
* @param customStore Method to get a custom store. Use with caution (see the docs).
*/
function setMany(entries, customStore = defaultGetStore()) {
return customStore('readwrite', (store) => {
entries.forEach((entry) => store.put(entry[1], entry[0]));
return promisifyRequest(store.transaction);
});
}
/**
* Get multiple values by their keys
*
* @param keys
* @param customStore Method to get a custom store. Use with caution (see the docs).
*/
function getMany(keys, customStore = defaultGetStore()) {
return customStore('readonly', (store) => Promise.all(keys.map((key) => promisifyRequest(store.get(key)))));
}
/**
* Update a value. This lets you see the old value and update it as an atomic operation.
*
* @param key
* @param updater A callback that takes the old value and returns a new value.
* @param customStore Method to get a custom store. Use with caution (see the docs).
*/
function update(key, updater, customStore = defaultGetStore()) {
return customStore('readwrite', (store) =>
// Need to create the promise manually.
// If I try to chain promises, the transaction closes in browsers
// that use a promise polyfill (IE10/11).
new Promise((resolve, reject) => {
store.get(key).onsuccess = function () {
try {
store.put(updater(this.result), key);
resolve(promisifyRequest(store.transaction));
}
catch (err) {
reject(err);
}
};
}));
}
/**
* Delete a particular key from the store.
*
* @param key
* @param customStore Method to get a custom store. Use with caution (see the docs).
*/
function del(key, customStore = defaultGetStore()) {
return customStore('readwrite', (store) => {
store.delete(key);
return promisifyRequest(store.transaction);
});
}
/**
* Clear all values in the store.
*
* @param customStore Method to get a custom store. Use with caution (see the docs).
*/
function clear(customStore = defaultGetStore()) {
return customStore('readwrite', (store) => {
store.clear();
return promisifyRequest(store.transaction);
});
}
function eachCursor(customStore, callback) {
return customStore('readonly', (store) => {
// This would be store.getAllKeys(), but it isn't supported by Edge or Safari.
// And openKeyCursor isn't supported by Safari.
store.openCursor().onsuccess = function () {
if (!this.result)
return;
callback(this.result);
this.result.continue();
};
return promisifyRequest(store.transaction);
});
}
/**
* Get all keys in the store.
*
* @param customStore Method to get a custom store. Use with caution (see the docs).
*/
function keys(customStore = defaultGetStore()) {
const items = [];
return eachCursor(customStore, (cursor) => items.push(cursor.key)).then(() => items);
}
/**
* Get all values in the store.
*
* @param customStore Method to get a custom store. Use with caution (see the docs).
*/
function values(customStore = defaultGetStore()) {
const items = [];
return eachCursor(customStore, (cursor) => items.push(cursor.value)).then(() => items);
}
/**
* Get all entries in the store. Each entry is an array of `[key, value]`.
*
* @param customStore Method to get a custom store. Use with caution (see the docs).
*/
function entries(customStore = defaultGetStore()) {
const items = [];
return eachCursor(customStore, (cursor) => items.push([cursor.key, cursor.value])).then(() => items);
}
var idbKeyVal = {
__proto__: null,
clear: clear,
createStore: createStore,
del: del,
entries: entries,
get: get,
getMany: getMany,
keys: keys,
promisifyRequest: promisifyRequest,
set: set,
setMany: setMany,
update: update,
values: values
};
var defaultOpts = {
maxAge: Infinity,
version: 0,
lib: idbKeyVal
};
var getOpts = function getOpts(passedOptions) {
return Object.assign({}, defaultOpts, passedOptions);
};
var keyValLib = idbKeyVal;
var _get = function get(key, opts, store) {
var _getOpts = getOpts(opts),
maxAge = _getOpts.maxAge,
version = _getOpts.version,
lib = _getOpts.lib;
return lib.get(key, store).then(JSON.parse).then(function (parsed) {
var age = Date.now() - parsed.time;
if (age > maxAge || version !== parsed.version) {
lib.del(key, store);
return null;
}
return parsed.data;
})["catch"](function () {
return null;
});
};
var _set = function set(key, data, spec, store) {
var _getOpts2 = getOpts(spec),
lib = _getOpts2.lib,
version = _getOpts2.version;
return lib.set(key, JSON.stringify({
version: version,
time: Date.now(),
data: data
}), store)["catch"](function () {
return null;
});
};
var _getAll = function getAll(spec, store) {
var opts = getOpts(spec);
var keys;
return opts.lib.keys(store).then(function (retrievedKeys) {
keys = retrievedKeys;
return Promise.all(keys.map(function (key) {
return _get(key, opts, store);
}));
}).then(function (data) {
return data.reduce(function (acc, bundleData, index) {
if (bundleData) {
acc[keys[index]] = bundleData;
}
return acc;
}, {});
})["catch"](function () {});
};
var getConfiguredCache = function getConfiguredCache(spec) {
var opts = getOpts(spec);
var store;
if (opts.name) {
store = createStore(opts.name, opts.name);
}
return {
get: function get(key) {
return _get(key, opts, store);
},
set: function set(key, val) {
return _set(key, val, opts, store);
},
getAll: function getAll() {
return _getAll(opts, store);
},
del: function del(key) {
return opts.lib.del(key, store);
},
clear: function clear() {
return opts.lib.clear(store);
},
keys: function keys() {
return opts.lib.keys(store);
}
};
};
exports.get = _get;
exports.getAll = _getAll;
exports.getConfiguredCache = getConfiguredCache;
exports.keyValLib = keyValLib;
exports.set = _set;