import { createHash } from "node:crypto"; import type { BinaryBody, BinaryStorage, BinaryStorageReadResult, BinaryStorageStatResult, BinaryStorageWriteResult, } from "../types"; import { BinaryBodyBufferReader } from "./BinaryBodyBufferReader"; import { BinaryBodyReadableStreamFactory } from "./BinaryBodyReadableStreamFactory"; export class InMemoryBinaryStorage implements BinaryStorage { readonly driverName = "memory"; private readonly values = new Map(); async write(args: { storageKey: string; body: BinaryBody }): Promise { const bytes = await new BinaryBodyBufferReader().read(args.body); this.values.set(args.storageKey, bytes); return { storageKey: args.storageKey, size: bytes.byteLength, sha256: createHash("sha256").update(bytes).digest("hex"), }; } async openReadStream(storageKey: string): Promise { const bytes = this.values.get(storageKey); if (!bytes) { return undefined; } return { body: new BinaryBodyReadableStreamFactory(bytes).create(), size: bytes.byteLength, }; } async stat(storageKey: string): Promise { const bytes = this.values.get(storageKey); if (!bytes) { return { exists: false }; } return { exists: true, size: bytes.byteLength }; } async delete(storageKey: string): Promise { this.values.delete(storageKey); } async deleteMany(storageKeys: ReadonlyArray): Promise { for (const key of storageKeys) { this.values.delete(key); } } async listByPrefix(prefix: string): Promise> { return Array.from(this.values.keys()).filter((key) => key.startsWith(prefix)); } } export { BinaryBodyBufferReader } from "./BinaryBodyBufferReader"; export { BinaryBodyReadableStreamFactory } from "./BinaryBodyReadableStreamFactory";