UNPKG

mlld

Version:

mlld: a modular prompt scripting language

175 lines (173 loc) 4.85 kB
import { __name, __publicField } from './chunk-OMKLS24H.mjs'; import * as fs from 'fs/promises'; import * as path from 'path'; import { createHash } from 'crypto'; var _ImmutableCache = class _ImmutableCache { constructor(projectPath, options) { __publicField(this, "cacheDir"); __publicField(this, "inMemory"); __publicField(this, "memoryCache"); this.inMemory = options?.inMemory || false; this.memoryCache = /* @__PURE__ */ new Map(); if (this.inMemory) { return; } const isServerless = projectPath.startsWith("/var/task") || process.env.LAMBDA_TASK_ROOT || process.env.VERCEL || process.env.AWS_LAMBDA_FUNCTION_NAME; if (isServerless) { this.cacheDir = path.join("/tmp", ".mlld", "cache", "imports"); } else { this.cacheDir = path.join(projectPath, ".mlld", "cache", "imports"); } } /** * Get cached content by URL and hash */ async get(url, expectedHash) { if (process.env.MLLD_TEST === "1") { return null; } const urlHash = this.hashUrl(url); if (this.inMemory) { const entry = this.memoryCache.get(urlHash); if (!entry) return null; if (expectedHash && entry.contentHash !== expectedHash) { return null; } return entry.content; } const cachePath = path.join(this.cacheDir, urlHash); try { const metaPath = `${cachePath}.meta.json`; const metaContent = await fs.readFile(metaPath, "utf8"); const meta = JSON.parse(metaContent); if (expectedHash && meta.contentHash !== expectedHash) { return null; } const content = await fs.readFile(cachePath, "utf8"); const actualHash = createHash("sha256").update(content, "utf8").digest("hex"); if (actualHash !== meta.contentHash) { await this.remove(url); return null; } return content; } catch (error) { return null; } } /** * Store content in cache */ async set(url, content) { if (process.env.MLLD_TEST === "1") { return createHash("sha256").update(content, "utf8").digest("hex"); } const urlHash = this.hashUrl(url); const contentHash = createHash("sha256").update(content, "utf8").digest("hex"); if (this.inMemory) { const entry = { url, content, contentHash, cachedAt: (/* @__PURE__ */ new Date()).toISOString(), size: content.length }; this.memoryCache.set(urlHash, entry); return contentHash; } await fs.mkdir(this.cacheDir, { recursive: true }); const cachePath = path.join(this.cacheDir, urlHash); await fs.writeFile(cachePath, content, "utf8"); const meta = { url, contentHash, cachedAt: (/* @__PURE__ */ new Date()).toISOString(), size: content.length }; await fs.writeFile(`${cachePath}.meta.json`, JSON.stringify(meta, null, 2)); return contentHash; } /** * Remove cached entry */ async remove(url) { const urlHash = this.hashUrl(url); if (this.inMemory) { this.memoryCache.delete(urlHash); return; } const cachePath = path.join(this.cacheDir, urlHash); try { await fs.unlink(cachePath); await fs.unlink(`${cachePath}.meta.json`); } catch { } } /** * Clear entire cache */ async clear() { if (this.inMemory) { this.memoryCache.clear(); return; } try { await fs.rm(this.cacheDir, { recursive: true, force: true }); } catch { } } /** * Get cache statistics */ async getStats() { if (this.inMemory) { let totalSize = 0; const urls = []; for (const entry of this.memoryCache.values()) { totalSize += entry.size; urls.push(entry.url); } return { entries: this.memoryCache.size, totalSize, urls }; } try { const files = await fs.readdir(this.cacheDir); const metaFiles = files.filter((f) => f.endsWith(".meta.json")); let totalSize = 0; const urls = []; for (const metaFile of metaFiles) { const metaPath = path.join(this.cacheDir, metaFile); const metaContent = await fs.readFile(metaPath, "utf8"); const meta = JSON.parse(metaContent); totalSize += meta.size || 0; urls.push(meta.url); } return { entries: metaFiles.length, totalSize, urls }; } catch { return { entries: 0, totalSize: 0, urls: [] }; } } hashUrl(url) { return createHash("sha256").update(url, "utf8").digest("hex"); } }; __name(_ImmutableCache, "ImmutableCache"); var ImmutableCache = _ImmutableCache; export { ImmutableCache }; //# sourceMappingURL=chunk-RO3YC7AJ.mjs.map //# sourceMappingURL=chunk-RO3YC7AJ.mjs.map