import EventEmitter from "eventemitter3" ; import hash from "hash.js" ; import Credentials from "../credentials/Credentials" ; import { credentialsAllowsPurpose , getCredentials } from "../credentials/channel" ; import { detectFormat , getFormatForID } from "../io/formatRouter" ; import { fireInstantiationHandlers , registerDatasource } from "./register" ; import { AttachmentDetails , BufferLike , DatasourceConfiguration , DatasourceLoadedData , EncryptedContent , History , VaultID , VaultInsights } from "../types" ; memberof export default class TextDatasource extends EventEmitter { _content : EncryptedContent ; _credentials : Credentials ; type : string ; param credentials constructor ( credentials ) { super ( ) ; this . _credentials = credentials ; this . _credentials . restrictPurposes ( [ Credentials . PURPOSE_SECURE_EXPORT ] ) ; this . _content = "" ; try { const { data : credentialData } = getCredentials ( credentials . id ) ; const { datasource : datasourceConfig = { } } = credentialData || { } ; const { content = "" } = datasourceConfig as DatasourceConfiguration ; this . _content = content ; } catch ( err ) { } this . type = "text" ; fireInstantiationHandlers ( "text" , this ) ; } readonly get credentials ( ) : Credentials { return this . _credentials ; } memberof get hasContent ( ) : boolean { return this . _content && this . _content . length > 0 ; } param newCredentials param preflight returns async changePassword ( newCredentials , preflight ) { throw new Error ( "Changing password not supported" ) ; } param vaultID param attachmentID memberof getAttachment ( vaultID , attachmentID ) { return Promise . reject ( new Error ( "Attachments not supported" ) ) ; } returns memberof getAvailableStorage ( ) { return Promise . resolve ( null ) ; } memberof getConfiguration ( ) { return { type : "text" , content : this . _content } ; } returns memberof getTotalStorage ( ) { return Promise . resolve ( null ) ; } returns memberof getID ( ) { const content = this . type === "text" ? this . _content : this . toString ( ) ; if ( ! content ) { throw new Error ( "Failed getting ID: Datasource requires content for ID generation" ) ; } return hash . sha256 ( ) . update ( content ) . digest ( "hex" ) ; } param credentials returns throws memberof load ( credentials ) { if ( ! this . _content ) { return Promise . reject ( new Error ( "Failed to load vault: Content is empty" ) ) ; } if ( credentialsAllowsPurpose ( credentials . id , Credentials . PURPOSE_DECRYPT_VAULT ) !== true ) { return Promise . reject ( new Error ( "Provided credentials don't allow vault decryption" ) ) ; } const Format = detectFormat ( this . _content ) ; return Format . parseEncrypted ( this . _content , credentials ) . then ( ( history ) => ( { Format , history } ) ) ; } param vaultID param attachmentID param buffer param details memberof putAttachment ( vaultID , attachmentID , buffer , details ) { return Promise . reject ( new Error ( "Attachments not supported" ) ) ; } param vaultID param attachmentID memberof removeAttachment ( vaultID , attachmentID ) { return Promise . reject ( new Error ( "Attachments not supported" ) ) ; } param history param credentials returns memberof async save ( history , credentials ) { if ( credentialsAllowsPurpose ( credentials . id , Credentials . PURPOSE_ENCRYPT_VAULT ) !== true ) { throw new Error ( "Unable to save: Provided credentials don't allow vault encryption" ) ; } if ( ! history . format ) { throw new Error ( "Unable to save: Provided history does not contain a format" ) ; } return getFormatForID ( history . format ) . encodeRaw ( history , credentials ) ; } param content returns memberof setContent ( content ) { this . _content = content || "" ; return this ; } memberof supportsAttachments ( ) { return false ; } returns memberof supportsPasswordChange ( ) { return false ; } returns memberof supportsRemoteBypass ( ) { return false ; } param insights memberof updateInsights ( insights ) { return Promise . resolve ( ) ; } } registerDatasource ( "text" , TextDatasource ) ;