Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// ========================================================
// LocalForage - set up off-line cache for datasets/genes
// ========================================================
import localforage from 'localforage';
import 'localforage-getitems';
import 'localforage-setitems';
import 'localforage-removeitems';
// Set up configuration once, at start of app
localforage.config({
name: 'Loom',
storeName: 'datasets',
driver: [
localforage.INDEXEDDB,
localforage.WEBSQL,
localforage.LOCALSTORAGE,
],
});
const dataSchemaVersion = '0.0.1 # first semver';
// Check for schema version, wipe localForage cache if outdated
localforage.getItem('dataSchemaVersion')
.then((storedSchemaVersion) => {
console.log({
dataSchemaVersion,
storedSchemaVersion,
});
if (storedSchemaVersion) {
const schemas = [dataSchemaVersion, storedSchemaVersion]
.map((schemaString) => {
return schemaString
init () {
try {
// IOS fixed crash IndexedDB
// DOMException: Connection to Indexed Database server lost. Refresh the page to try again
// Disable IndexedDB for Safari
let driver = []
// eslint-disable-next-line no-useless-escape
if (!!navigator.userAgent.match(/Version\/[\d\.]+.*Safari/)) {
// console.log('Safari: No IndexedDB usage')
driver = [
localForage.WEBSQL,
localForage.LOCALSTORAGE
]
} else {
driver = [
localForage.INDEXEDDB,
localForage.WEBSQL,
localForage.LOCALSTORAGE
]
}
localForage.config({
driver
})
return localForage
} catch (error) {
console.error('localForage.config Error: ', error)
}
/**
* Internal dependencies
*/
import localforageBypass from './localforage-bypass';
/**
* Module variables
*/
const log = debug( 'calypso:localforage' );
const config = {
name: 'calypso',
storeName: 'calypso_store',
version: 1.0,
description: 'Calypso Browser Storage',
driver: [ localforage.INDEXEDDB, localforage.WEBSQL, localforage.LOCALSTORAGE ],
};
let _ready = false;
// Promise that resolves when our localforage configuration has been applied
const localForagePromise = localforage
.defineDriver( localforageBypass )
.then( () => {
localforage.config( config );
_ready = true;
return localforage;
} )
.catch( error => log( 'Configuring localforage: %s', error ) );
// Wraps a function to run after waiting until a promise has resolved.
// The promise should contain the original object for context.
const wrapOriginalFunc = ( promise, original ) => {
// default, the accessToken is used in Authorization header for all requests)
//
// The `dispatch` and `getState` parameters are simply passed from Redux as
// a convenience.
import fetch from 'node-fetch';
import localforage from 'localforage';
import config from '../../config/admin';
// Prefer localstorage before trying bulkier solutions.
localforage.config({
name: config.appName,
storeName: 'auth',
driver: [
localforage.INDEXEDDB,
localforage.WEBSQL,
localforage.LOCALSTORAGE
]
});
// Authentication route.
const tokensUrl = `${ config.authRoot }/tokens`;
const tokenExpirationWindow = 60 * 10; // 10min in seconds
const jsonHeaders = {
'Content-Type': 'application/json',
'Accept': 'application/json'
};
// Make the actual fetch() call. Reject if there's an error, otherwise resolve
// with the parsed JSON body.
platformService: PlatformService
) {
const dbConfig: LocalForageOptions = {
name: platformService.appName,
version: 3,
size: 4980736, // Size of database, in bytes. WebSQL-only for now.
storeName: `${platformService.appName}_db`,
description: `${platformService.appName}`,
};
this._db = localForage.createInstance(dbConfig);
this._db.setDriver([
localForage.INDEXEDDB,
localForage.WEBSQL,
localForage.LOCALSTORAGE,
]);
this._db.length(); // Init db now
}
async testLocalForage() {
await initLocalForage();
const DRIVERS = [
localforage.INDEXEDDB,
'memoryDriver',
localforage.LOCALSTORAGE,
localforage.WEBSQL,
localForageDrivers
];
for (const driverName of DRIVERS) {
console.log('Test ', driverName);
try {
await localforage.setDriver(driverName);
console.log('A');
await localforage.setItem('hello', 'world');
console.log('B');
const x = await localforage.getItem('hello');
console.log('got ', x);
await localforage.clear();
console.log('C');
} catch (error) {
console.log('Error', error);
}
static canCache(): boolean {
return localforage.supports(localforage.INDEXEDDB) || localforage.supports(localforage.WEBSQL);
}
return driverOrder.map(driver => {
switch (driver) {
case 'indexeddb':
return LocalForage.INDEXEDDB;
case 'websql':
return LocalForage.WEBSQL;
case 'localstorage':
return LocalForage.LOCALSTORAGE;
default:
return driver;
}
});
}
Persist.prototype.driver = function (val) {
if (val !== undefined) {
switch (val.toUpperCase()) {
case 'LOCALSTORAGE':
localforage.setDriver(localforage.LOCALSTORAGE);
break;
case 'WEBSQL':
localforage.setDriver(localforage.WEBSQL);
break;
case 'INDEXEDDB':
localforage.setDriver(localforage.INDEXEDDB);
break;
default:
throw('ForerunnerDB.Persist: The persistence driver you have specified is not found. Please use either IndexedDB, WebSQL or LocalStorage!');
}
return this;
}
return localforage.driver();
};