import StorageAdapter from './adapters/FileSystemStorageAdapter' ; import { IStorageAdapter , IStorageAdapterConfig } from './adapters/IStorageAdapter' ; import { PrivateKeyExistsError } from './errors' ; export interface IKeyStorageConfig { /**
     * Path to folder where to store the data (Node.js only).
     */ dir ? : string ; /**
     * Name of the IndexedDB database where to store the data (Browsers only).
     */ name ? : string ; /**
     * The  IStorageAdapter implementation to be used as a storage backend.
      adapter ? : IStorageAdapter ; } const DEFAULTS = { dir : '.virgil_keys' , name : 'VirgilKeys' } ; /**
 * Class representing a storage container for private key data.
 * Use this class if you need to load the keys stored with
 * version 4.x of this library. For new code, use the
 *  PrivateKeyStorage instead.
 *
 *  deprecated export class KeyStorage { private adapter : IStorageAdapter ; constructor ( config = { } ) { console . log ( 'Warning! `KeyStorage` is deprecated. Use `PrivateKeyStorage` instead.' ) ; this . adapter = resolveAdapter ( config ) ; } param { string } name returns { Promise < boolean > } exists ( name ) { validateName ( name ) ; return this . adapter . exists ( name ) ; } param { string } name returns { Promise < string | null > } load ( name ) { validateName ( name ) ; return this . adapter . load ( name ) ; } param { string } name returns { Promise < boolean > } remove ( name ) { validateName ( name ) ; return this . adapter . remove ( name ) ; } param { string } name param { string } data returns { Promise < void > } save ( name , data ) { validateName ( name ) ; return this . adapter . store ( name , data ) . catch ( error => { if ( error && error . code === 'EEXIST' ) { return Promise . reject ( new PrivateKeyExistsError ( ) ) ; } return Promise . reject ( error ) ; } ) ; } } function resolveAdapter ( config ) { if ( typeof config === 'string' ) { return new StorageAdapter ( { dir : config , name : config } ) ; } const { adapter , ... rest } = config ; if ( adapter != null ) { return adapter ; } return new StorageAdapter ( { ... DEFAULTS , ... rest } ) ; } function validateName ( name ) { if ( ! name ) throw new TypeError ( 'Argument `name` is required.' ) ; } function validateData ( data ) { if ( ! data ) throw new TypeError ( 'Argument `data` is required.' ) ; }