/** * src/adapters/local-blob-store.ts * ================================== * INTERIM local-disk blob store — NOT the real ST0RE bytes backend. * * KNOWN GAP: src/clients/st0re-client.ts only registers artifact METADATA * (a pointer: provider/bucket/key/region). It has no endpoint that accepts * and persists raw bytes — ST0RE does not yet expose a real object-storage * write path for 0pt to call. Until it does, document-capture-service.ts * and document-extraction-service.ts write attachment/extraction bytes here * so the capability is functionally real end-to-end (download → persist → * read back for extraction) instead of silently discarding the PDF bytes * and only faking a metadata pointer. * * This is single-node, unencrypted-at-rest beyond the host filesystem, and * has no retention/lifecycle management — do not treat it as production * object storage. Swap it for a real ST0RE (or direct MinIO/Azure Blob) * client the day one exists; every call site that constructs this class is * marked "INTERIM" so the seam is easy to find. */ import { promises as fs } from "fs"; import path from "path"; import { isProductionRuntime } from "../platform/dependency-mode"; import { SiaxError } from "../platform/errors"; export interface LocalBlobWriteResult { provider: "local_interim"; bucket: string; key: string; absolutePath: string; sizeBytes: number; } export class LocalBlobStore { constructor(private rootDir: string = process.env.OPT_LOCAL_BLOB_DIR || path.join(process.cwd(), ".data", "artifacts")) {} /** * A fallback that silently swaps durable object storage for a container's * local filesystem is a double truth: `document-capture-service` registers * an artifact pointer claiming the bytes are stored, while the bytes sit on * an ephemeral single-node disk that is discarded on the next redeploy. In * production the honest answer is DEPENDENCY_UNAVAILABLE (the capability is * blocked because no ST0RE bytes path exists yet), never a fabricated * success. Dev/test keep the adapter so the capture -> persist -> read-back * flow stays exercisable end to end. * * Asserted per-operation rather than in the constructor so that merely * WIRING the service (both services construct one eagerly as a default) is * not itself fatal — only actually persisting or reading bytes is. */ private assertAllowed(operation: string): void { if (isProductionRuntime()) { throw new SiaxError( "DEPENDENCY_UNAVAILABLE", "0pt.local_blob_store", `LocalBlobStore.${operation} is an interim dev/test adapter and may not persist document ` + `bytes in a production runtime; no canonical ST0RE bytes path is configured` ); } } async write(bucket: string, key: string, bytes: Buffer): Promise { this.assertAllowed("write"); const safeKey = key.replace(/\.\./g, "_"); const absolutePath = path.join(this.rootDir, bucket, safeKey); await fs.mkdir(path.dirname(absolutePath), { recursive: true }); await fs.writeFile(absolutePath, bytes); return { provider: "local_interim", bucket, key: safeKey, absolutePath, sizeBytes: bytes.length }; } async read(bucket: string, key: string): Promise { this.assertAllowed("read"); const absolutePath = path.join(this.rootDir, bucket, key); return fs.readFile(absolutePath); } }