/* This file is part of the Notesnook project (https://notesnook.com/) Copyright (C) 2023 Streetwriters (Private) Limited This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ import { AsymmetricCipher, Cipher, DataFormat, SerializedKey, SerializedKeyPair } from "@notesnook/crypto"; import { KVStorage } from "./database/kv.js"; import { ConfigStorage } from "./database/config.js"; export type Output = TOutputFormat extends "uint8array" ? Uint8Array : string; export type FileEncryptionMetadata = { chunkSize: number; iv: string; size: number; salt: string; alg: string; }; export type FileEncryptionMetadataWithOutputType< TOutputFormat extends DataFormat > = FileEncryptionMetadata & { outputType: TOutputFormat }; export type FileEncryptionMetadataWithHash = FileEncryptionMetadata & { hash: string; hashType: string; }; export interface IStorage { write(key: string, data: T): Promise; writeMulti(entries: [string, T][]): Promise; readMulti(keys: string[]): Promise<[string, T][]>; read(key: string, isArray?: boolean): Promise; remove(key: string): Promise; removeMulti(keys: string[]): Promise; clear(): Promise; getAllKeys(): Promise; encrypt(key: SerializedKey, plainText: string): Promise>; encryptMulti( key: SerializedKey, items: string[] ): Promise[]>; decrypt(key: SerializedKey, cipherData: Cipher<"base64">): Promise; decryptMulti( key: SerializedKey, items: Cipher<"base64">[] ): Promise; decryptAsymmetric( keyPair: SerializedKeyPair, cipherData: AsymmetricCipher<"base64"> ): Promise; deriveCryptoKey(credentials: SerializedKey): Promise; hash( password: string, email: string, options?: { usesFallback?: boolean } ): Promise; getCryptoKey(): Promise; generateCryptoKey(password: string, salt?: string): Promise; generateCryptoKeyPair(): Promise; generateCryptoKeyFallback( password: string, salt?: string ): Promise; deriveCryptoKeyFallback(credentials: SerializedKey): Promise; // async generateRandomKey() { // const passwordBytes = randomBytes(124); // const password = passwordBytes.toString("base64"); // return await this.storage.generateCryptoKey(password); // } } export interface ICompressor { compress(data: string): Promise; decompress(data: string): Promise; } export type RequestOptions = { url: string; // metadata?: AttachmentMetadata; chunkSize: number; headers: { Authorization: string }; }; type Cancellable = { execute(): Promise; cancel(reason?: string): Promise; }; export interface IFileStorage { downloadFile( filename: string, requestOptions: RequestOptions ): Cancellable; uploadFile( filename: string, requestOptions: RequestOptions ): Cancellable; readEncrypted( filename: string, encryptionKey: SerializedKey, cipherData: FileEncryptionMetadataWithOutputType ): Promise | undefined>; writeEncryptedBase64( data: string, encryptionKey: SerializedKey, mimeType: string ): Promise; deleteFile( filename: string, requestOptions?: RequestOptions ): Promise; exists(filename: string): Promise; bulkExists(filenames: string[]): Promise; getUploadedFileSize(filename: string): Promise; clearFileStorage(): Promise; hashBase64(data: string): Promise<{ hash: string; type: string }>; } export type StorageAccessor = () => IStorage; export type KVStorageAccessor = () => KVStorage; export type ConfigStorageAccessor = () => ConfigStorage; export type CompressorAccessor = () => Promise;