import TextDatasource from "./TextDatasource" ; import { fireInstantiationHandlers , registerDatasource } from "./register" ; import Credentials from "../credentials/Credentials" ; import { getCredentials } from "../credentials/channel" ; import { AttachmentDetails , BufferLike , DatasourceConfigurationMemory , DatasourceLoadedData , EncryptedContent , History , MemoryStore , VaultID } from "../types" ; const TYPE = "memory" ; augments TextDatasource memberof export default class MemoryDatasource extends TextDatasource { _property : string ; _store : MemoryStore ; param credentials constructor ( credentials ) { super ( credentials ) ; const { data : credentialData } = getCredentials ( credentials . id ) ; const { datasource : datasourceConfig } = credentialData as { datasource : DatasourceConfigurationMemory ; } ; const { property } = datasourceConfig ; this . _property = property ; this . _store = global [ property ] = global [ property ] || { } ; this . type = TYPE ; fireInstantiationHandlers ( TYPE , this ) ; } memberof protected async _ensureAttachmentsPaths ( vaultID ) { this . _store . attachments = this . _store . attachments || { } ; this . _store . attachments [ vaultID ] = this . _store . attachments [ vaultID ] || { } ; } param vaultID param attachmentID memberof getAttachment ( vaultID , attachmentID ) { return this . _ensureAttachmentsPaths ( vaultID ) . then ( ( ) => { if ( ! this . _store . attachments [ vaultID ] [ attachmentID ] ) { throw new Error ( `No attachment found for ID: ${ attachmentID }` ) ; } return this . _store . attachments [ vaultID ] [ attachmentID ] ; } ) ; } memberof getConfiguration ( ) { return { type : "memory" , property : this . _property } ; } param credentials returns memberof async load ( credentials ) { if ( ! this . _store . vault ) { throw new Error ( "No vault in memory" ) ; } this . setContent ( this . _store . vault ) ; return super . load ( credentials ) ; } param vaultID param attachmentID param buffer param details memberof putAttachment ( vaultID , attachmentID , buffer , details ) { return this . _ensureAttachmentsPaths ( vaultID ) . then ( ( ) => { this . _store . attachments [ vaultID ] [ attachmentID ] = buffer ; } ) ; } param vaultID param attachmentID memberof removeAttachment ( vaultID , attachmentID ) { return this . _ensureAttachmentsPaths ( vaultID ) . then ( ( ) => { this . _store . attachments [ vaultID ] [ attachmentID ] = null ; delete this . _store . attachments [ vaultID ] [ attachmentID ] ; } ) ; } param history param credentials returns memberof save ( history , credentials ) { return super . save ( history , credentials ) . then ( encrypted => { this . _store . vault = encrypted ; return encrypted ; } ) ; } memberof supportsAttachments ( ) { return true ; } } registerDatasource ( TYPE , MemoryDatasource ) ;