@just-every/mcp-read-website-fast
Version:
Markdown Content Preprocessor - Fetch web pages, extract content, convert to clean Markdown
55 lines (54 loc) • 1.42 kB
JavaScript
import { createHash } from 'crypto';
import { mkdir, readFile, writeFile, access } from 'fs/promises';
import { join } from 'path';
export class DiskCache {
cacheDir;
constructor(cacheDir = '.cache') {
this.cacheDir = cacheDir;
}
async init() {
await mkdir(this.cacheDir, { recursive: true });
}
getCacheKey(url) {
return createHash('sha256').update(url).digest('hex');
}
getCachePath(url) {
const key = this.getCacheKey(url);
return join(this.cacheDir, `${key}.json`);
}
async has(url) {
try {
await access(this.getCachePath(url));
return true;
}
catch {
return false;
}
}
async get(url) {
try {
const path = this.getCachePath(url);
const data = await readFile(path, 'utf-8');
return JSON.parse(data);
}
catch {
return null;
}
}
async put(url, markdown, title) {
const entry = {
url,
markdown,
timestamp: Date.now(),
title,
};
const path = this.getCachePath(url);
await writeFile(path, JSON.stringify(entry, null, 2));
}
async getAge(url) {
const entry = await this.get(url);
if (!entry)
return null;
return Date.now() - entry.timestamp;
}
}