|
|
import config from '@automattic/calypso-config'; |
|
|
import debugFactory from 'debug'; |
|
|
import { kebabCase } from 'lodash'; |
|
|
import { bumpStat } from 'calypso/lib/analytics/mc'; |
|
|
import { once } from 'calypso/lib/memoize-last'; |
|
|
import { |
|
|
getStoredItem as bypassGet, |
|
|
getAllStoredItems as bypassGetAll, |
|
|
setStoredItem as bypassSet, |
|
|
clearStorage as bypassClear, |
|
|
activate as activateBypass, |
|
|
} from './bypass'; |
|
|
import { StoredItems } from './types'; |
|
|
|
|
|
const debug = debugFactory( 'calypso:browser-storage' ); |
|
|
|
|
|
let shouldBypass = false; |
|
|
let shouldDisableIDB = false; |
|
|
|
|
|
const DB_NAME = 'calypso'; |
|
|
const DB_VERSION = 2; |
|
|
const STORE_NAME = 'calypso_store'; |
|
|
|
|
|
const SANITY_TEST_KEY = 'browser-storage-sanity-test'; |
|
|
|
|
|
|
|
|
const isAffectedSafari = |
|
|
config.isEnabled( 'safari-idb-mitigation' ) && |
|
|
typeof window !== 'undefined' && |
|
|
!! window.IDBKeyRange?.lowerBound( 0 ).includes && |
|
|
!! ( window as any ).webkitAudioContext && |
|
|
!! window.PointerEvent; |
|
|
|
|
|
debug( 'Safari IDB mitigation active: %s', isAffectedSafari ); |
|
|
|
|
|
export const supportsIDB = once( async () => { |
|
|
if ( typeof window === 'undefined' || ! window.indexedDB ) { |
|
|
debug( 'IDB not found in host' ); |
|
|
return false; |
|
|
} |
|
|
|
|
|
if ( shouldDisableIDB ) { |
|
|
debug( 'IDB disabled' ); |
|
|
return false; |
|
|
} |
|
|
|
|
|
try { |
|
|
const testValue = Date.now().toString(); |
|
|
await idbSet( SANITY_TEST_KEY, testValue ); |
|
|
await idbGet( SANITY_TEST_KEY ); |
|
|
return true; |
|
|
} catch ( error ) { |
|
|
|
|
|
return false; |
|
|
} |
|
|
} ); |
|
|
|
|
|
const getDB = once( () => { |
|
|
const request = window.indexedDB.open( DB_NAME, DB_VERSION ); |
|
|
return new Promise< IDBDatabase >( ( resolve, reject ) => { |
|
|
try { |
|
|
if ( request ) { |
|
|
request.onerror = ( event ) => { |
|
|
|
|
|
|
|
|
if ( request.error && request.error.name === 'InvalidStateError' ) { |
|
|
event.preventDefault(); |
|
|
} |
|
|
reject( request.error ); |
|
|
}; |
|
|
request.onsuccess = () => { |
|
|
const db = request.result; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
db.onerror = function ( errorEvent: any ) { |
|
|
debug( 'IDB Error', errorEvent ); |
|
|
if ( errorEvent.target?.error?.name ) { |
|
|
bumpStat( 'calypso-browser-storage', kebabCase( errorEvent.target.error.name ) ); |
|
|
|
|
|
if ( errorEvent.target.error.name === 'QuotaExceededError' ) { |
|
|
|
|
|
shouldDisableIDB = true; |
|
|
supportsIDB.clear(); |
|
|
debug( 'disabling IDB because we saw a QuotaExceededError' ); |
|
|
} |
|
|
} |
|
|
}; |
|
|
db.onversionchange = () => { |
|
|
|
|
|
|
|
|
db.close(); |
|
|
}; |
|
|
resolve( db ); |
|
|
}; |
|
|
request.onupgradeneeded = () => request.result.createObjectStore( STORE_NAME ); |
|
|
} |
|
|
} catch ( error ) { |
|
|
reject( error ); |
|
|
} |
|
|
} ); |
|
|
} ); |
|
|
|
|
|
function idbGet< T >( key: string ): Promise< T | undefined > { |
|
|
return new Promise( ( resolve, reject ) => { |
|
|
getDB() |
|
|
.then( ( db ) => { |
|
|
const transaction = db.transaction( STORE_NAME, 'readonly' ); |
|
|
const get = transaction.objectStore( STORE_NAME ).get( key ); |
|
|
|
|
|
const success = () => resolve( get.result ); |
|
|
const error = () => reject( transaction.error ); |
|
|
|
|
|
transaction.oncomplete = success; |
|
|
transaction.onabort = error; |
|
|
transaction.onerror = error; |
|
|
} ) |
|
|
.catch( ( err ) => reject( err ) ); |
|
|
} ); |
|
|
} |
|
|
|
|
|
type EventTargetWithCursorResult = EventTarget & { result: IDBCursorWithValue | null }; |
|
|
|
|
|
function idbGetAll( pattern?: RegExp ): Promise< StoredItems > { |
|
|
return getDB().then( |
|
|
( db ) => |
|
|
new Promise( ( resolve, reject ) => { |
|
|
const results: StoredItems = {}; |
|
|
const transaction = db.transaction( STORE_NAME, 'readonly' ); |
|
|
const getAll = transaction.objectStore( STORE_NAME ).openCursor(); |
|
|
|
|
|
const success = ( event: Event ) => { |
|
|
const cursor = ( event.target as EventTargetWithCursorResult ).result; |
|
|
if ( cursor ) { |
|
|
const { primaryKey: key, value } = cursor; |
|
|
if ( |
|
|
key && |
|
|
typeof key === 'string' && |
|
|
key !== SANITY_TEST_KEY && |
|
|
( ! pattern || pattern.test( key ) ) |
|
|
) { |
|
|
results[ key ] = value; |
|
|
} |
|
|
cursor.continue(); |
|
|
} else { |
|
|
|
|
|
resolve( results ); |
|
|
} |
|
|
}; |
|
|
const error = () => reject( transaction.error ); |
|
|
|
|
|
getAll.onsuccess = success; |
|
|
transaction.onabort = error; |
|
|
transaction.onerror = error; |
|
|
} ) |
|
|
); |
|
|
} |
|
|
|
|
|
let idbWriteCount = 0; |
|
|
let idbWriteBlock: Promise< void > | null = null; |
|
|
async function idbSet< T >( key: string, value: T ): Promise< void > { |
|
|
|
|
|
if ( idbWriteBlock ) { |
|
|
await idbWriteBlock; |
|
|
} |
|
|
|
|
|
|
|
|
if ( isAffectedSafari && ++idbWriteCount % 20 === 0 ) { |
|
|
await idbSafariReset(); |
|
|
} |
|
|
|
|
|
return new Promise( ( resolve, reject ) => { |
|
|
getDB() |
|
|
.then( async ( db ) => { |
|
|
const transaction = db.transaction( STORE_NAME, 'readwrite' ); |
|
|
transaction.objectStore( STORE_NAME ).put( value, key ); |
|
|
|
|
|
const success = () => resolve(); |
|
|
const error = () => reject( transaction.error ); |
|
|
|
|
|
transaction.oncomplete = success; |
|
|
transaction.onabort = error; |
|
|
transaction.onerror = error; |
|
|
} ) |
|
|
.catch( ( err ) => reject( err ) ); |
|
|
} ); |
|
|
} |
|
|
|
|
|
function idbClear(): Promise< void > { |
|
|
return new Promise( ( resolve, reject ) => { |
|
|
getDB() |
|
|
.then( ( db ) => { |
|
|
const transaction = db.transaction( STORE_NAME, 'readwrite' ); |
|
|
transaction.objectStore( STORE_NAME ).clear(); |
|
|
|
|
|
const success = () => resolve(); |
|
|
const error = () => reject( transaction.error ); |
|
|
|
|
|
transaction.oncomplete = success; |
|
|
transaction.onabort = error; |
|
|
transaction.onerror = error; |
|
|
} ) |
|
|
.catch( ( err ) => reject( err ) ); |
|
|
} ); |
|
|
} |
|
|
|
|
|
function idbRemove(): Promise< void > { |
|
|
return new Promise( ( resolve, reject ) => { |
|
|
const deleteRequest = window.indexedDB.deleteDatabase( DB_NAME ); |
|
|
deleteRequest.onsuccess = () => { |
|
|
getDB.clear(); |
|
|
resolve(); |
|
|
}; |
|
|
deleteRequest.onerror = ( event ) => reject( event ); |
|
|
} ); |
|
|
} |
|
|
|
|
|
async function idbSafariReset() { |
|
|
if ( idbWriteBlock ) { |
|
|
return idbWriteBlock; |
|
|
} |
|
|
debug( 'performing safari idb mitigation' ); |
|
|
idbWriteBlock = _idbSafariReset(); |
|
|
idbWriteBlock.finally( () => { |
|
|
idbWriteBlock = null; |
|
|
debug( 'idb mitigation complete' ); |
|
|
} ); |
|
|
return idbWriteBlock; |
|
|
} |
|
|
|
|
|
async function _idbSafariReset(): Promise< void > { |
|
|
const items = await idbGetAll(); |
|
|
|
|
|
await idbRemove(); |
|
|
|
|
|
return new Promise( ( resolve, reject ) => { |
|
|
getDB().then( |
|
|
( db ) => { |
|
|
const transaction = db.transaction( STORE_NAME, 'readwrite' ); |
|
|
const oStore = transaction.objectStore( STORE_NAME ); |
|
|
|
|
|
for ( let [ key, value ] of Object.entries( items ) ) { |
|
|
oStore.put( value, key ); |
|
|
} |
|
|
const success = () => resolve(); |
|
|
const error = () => reject( transaction.error ); |
|
|
|
|
|
transaction.oncomplete = success; |
|
|
transaction.onabort = error; |
|
|
transaction.onerror = error; |
|
|
}, |
|
|
( err ) => reject( err ) |
|
|
); |
|
|
} ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function bypassPersistentStorage( shouldBypassPersistentStorage: boolean ) { |
|
|
shouldBypass = shouldBypassPersistentStorage; |
|
|
|
|
|
if ( shouldBypass ) { |
|
|
activateBypass(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export async function getStoredItem< T >( key: string ): Promise< T | undefined > { |
|
|
if ( shouldBypass ) { |
|
|
return await bypassGet( key ); |
|
|
} |
|
|
|
|
|
const idbSupported = await supportsIDB(); |
|
|
if ( ! idbSupported ) { |
|
|
try { |
|
|
const valueString = window.localStorage.getItem( key ); |
|
|
if ( valueString === undefined || valueString === null ) { |
|
|
return undefined; |
|
|
} |
|
|
|
|
|
return JSON.parse( valueString ); |
|
|
} catch { |
|
|
return undefined; |
|
|
} |
|
|
} |
|
|
|
|
|
return await idbGet( key ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export async function getAllStoredItems( pattern?: RegExp ): Promise< StoredItems > { |
|
|
if ( shouldBypass ) { |
|
|
return await bypassGetAll( pattern ); |
|
|
} |
|
|
|
|
|
const idbSupported = await supportsIDB(); |
|
|
if ( ! idbSupported ) { |
|
|
try { |
|
|
const entries = Object.entries( window.localStorage ).map( ( [ key, value ] ) => [ |
|
|
key, |
|
|
value !== undefined ? JSON.parse( value ) : undefined, |
|
|
] ); |
|
|
|
|
|
if ( ! pattern ) { |
|
|
return Object.fromEntries( entries ); |
|
|
} |
|
|
|
|
|
return Object.fromEntries( entries.filter( ( [ key ] ) => pattern.test( key ) ) ); |
|
|
} catch { |
|
|
return {}; |
|
|
} |
|
|
} |
|
|
|
|
|
return await idbGetAll( pattern ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export async function setStoredItem< T >( key: string, value: T ): Promise< void > { |
|
|
if ( shouldBypass ) { |
|
|
return await bypassSet( key, value ); |
|
|
} |
|
|
|
|
|
const idbSupported = await supportsIDB(); |
|
|
if ( ! idbSupported ) { |
|
|
try { |
|
|
window.localStorage.setItem( key, JSON.stringify( value ) ); |
|
|
} catch { |
|
|
|
|
|
} |
|
|
return; |
|
|
} |
|
|
|
|
|
return await idbSet( key, value ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export async function clearStorage(): Promise< void > { |
|
|
if ( shouldBypass ) { |
|
|
return await bypassClear(); |
|
|
} |
|
|
|
|
|
const idbSupported = await supportsIDB(); |
|
|
if ( ! idbSupported ) { |
|
|
try { |
|
|
window.localStorage.clear(); |
|
|
} catch { |
|
|
|
|
|
} |
|
|
return; |
|
|
} |
|
|
|
|
|
return await idbClear(); |
|
|
} |
|
|
|