mlld
Version:
mlld: llm scripting language
142 lines (140 loc) • 3.54 kB
JavaScript
import { moduleNeedsToSerializable } from './chunk-KQOGKTK5.mjs';
import { __name, __publicField } from './chunk-NJQT543K.mjs';
import { createHash } from 'crypto';
var _InMemoryModuleCache = class _InMemoryModuleCache {
constructor() {
__publicField(this, "cache", /* @__PURE__ */ new Map());
__publicField(this, "index", /* @__PURE__ */ new Map());
}
/**
* Store module content in memory cache
*/
async store(content, source, importPath, options) {
const hash = createHash("sha256").update(content, "utf8").digest("hex");
const timestamp = Date.now();
const metadata = {
hash,
integrity: `sha256-${Buffer.from(hash, "hex").toString("base64")}`,
source,
cachedAt: new Date(timestamp).toISOString(),
size: Buffer.byteLength(content, "utf8"),
importPath
};
if (options?.dependencies) {
metadata.dependencies = {
...options.dependencies
};
}
if (options?.devDependencies) {
metadata.devDependencies = {
...options.devDependencies
};
}
if (options?.moduleNeeds) {
metadata.moduleNeeds = moduleNeedsToSerializable(options.moduleNeeds);
}
this.cache.set(hash, {
content,
metadata,
timestamp
});
if (importPath) {
this.index.set(importPath, hash);
}
return {
path: `memory://${hash}`,
hash,
timestamp
};
}
/**
* Retrieve module content from memory cache
*/
async retrieve(hash) {
const entry = this.cache.get(hash);
return entry?.content || null;
}
/**
* Get metadata for a cached module
*/
async getMetadata(hash) {
const entry = this.cache.get(hash);
return entry?.metadata || null;
}
/**
* Check if a module exists in memory cache
*/
async exists(hash) {
return this.cache.has(hash);
}
/**
* Find module by import path
*/
async findByImportPath(importPath) {
const hash = this.index.get(importPath);
if (!hash) return null;
const entry = this.cache.get(hash);
if (!entry) return null;
return {
path: `memory://${hash}`,
hash,
timestamp: entry.timestamp
};
}
/**
* List all cached modules (for debugging)
*/
async list() {
const results = [];
for (const [hash, entry] of this.cache) {
results.push({
importPath: entry.metadata.importPath,
hash,
size: entry.metadata.size
});
}
return results;
}
/**
* Clear the entire cache
*/
async clear() {
this.cache.clear();
this.index.clear();
}
/**
* Remove a specific module from cache
*/
async remove(hash) {
const entry = this.cache.get(hash);
if (!entry) return false;
if (entry.metadata.importPath) {
this.index.delete(entry.metadata.importPath);
}
this.cache.delete(hash);
return true;
}
/**
* Get cache statistics
*/
getStats() {
let totalSize = 0;
let oldestTimestamp = null;
for (const entry of this.cache.values()) {
totalSize += entry.metadata.size;
if (oldestTimestamp === null || entry.timestamp < oldestTimestamp) {
oldestTimestamp = entry.timestamp;
}
}
return {
moduleCount: this.cache.size,
totalSize,
oldestTimestamp
};
}
};
__name(_InMemoryModuleCache, "InMemoryModuleCache");
var InMemoryModuleCache = _InMemoryModuleCache;
export { InMemoryModuleCache };
//# sourceMappingURL=InMemoryModuleCache-2G5KADUM.mjs.map
//# sourceMappingURL=InMemoryModuleCache-2G5KADUM.mjs.map