| 27 | import { randomBytes } from "crypto"; |
| 28 | |
| 29 | export class NodeStorageInterface implements IStorage { |
| 30 | storage = {}; |
| 31 | crypto = new NNCrypto(); |
| 32 | |
| 33 | async removeMulti(keys: string[]): Promise<void> { |
| 34 | for (const key of keys) { |
| 35 | this.remove(key); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | async write<T>(key: string, data: T): Promise<void> { |
| 40 | this.storage[key] = data; |
| 41 | } |
| 42 | |
| 43 | async writeMulti<T>(entries: [key: string, data: T][]) { |
| 44 | for (const [key, value] of entries) { |
| 45 | this.storage[key] = value; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | async readMulti<T>(keys: string[]): Promise<[string, T][]> { |
| 50 | const result: [string, T][] = []; |
| 51 | keys.forEach((key) => { |
| 52 | result.push([key, this.storage[key]]); |
| 53 | }); |
| 54 | return result; |
| 55 | } |
| 56 | |
| 57 | async read<T>( |
| 58 | key: string, |
| 59 | isArray?: boolean | undefined |
| 60 | ): Promise<T | undefined> { |
| 61 | return this.storage[key]; |
| 62 | } |
| 63 | |
| 64 | async remove(key: string): Promise<void> { |
| 65 | delete this.storage[key]; |
| 66 | } |
| 67 | |
| 68 | async clear(): Promise<void> { |
| 69 | this.storage = {}; |
| 70 | } |
| 71 | |
| 72 | async getAllKeys(): Promise<string[]> { |
| 73 | return Object.keys(this.storage); |
| 74 | } |
| 75 | |
| 76 | async encrypt(key: SerializedKey, plainText: string) { |
| 77 | return await this.crypto.encrypt(key, plainText, "text", "base64"); |
| 78 | } |
| 79 | |
| 80 | async encryptMulti(key: SerializedKey, items: string[]) { |
| 81 | return await this.crypto.encryptMulti(key, items, "text", "base64"); |
| 82 | } |
| 83 | |
| 84 | decrypt(key: SerializedKey, cipherData: Cipher<"base64">): Promise<string> { |
| 85 | cipherData.format = "base64"; |
| 86 | return this.crypto.decrypt(key, cipherData, "text"); |
nothing calls this directly
no outgoing calls
no test coverage detected