@david/bench-registry@0.2.0Built and signed on GitHub ActionsBuilt and signed on GitHub Actions
Built and signed on GitHub Actions
latest
dsherret/bench-registrynpm and jsr proxy registry for benchmarking code that uses those registries
This package works with Deno
JSR Score
82%
Published
5 months ago (0.2.0)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960import { encodeHex } from "jsr:/@std/encoding@^1.0.1/hex"; import * as path from "jsr:@std/path@^1.0.0"; import type { CacheItem, CacheKey } from "./mod.ts"; export class FsCache { #dirPath: string; constructor(dirPath: string) { this.#dirPath = path.resolve(dirPath); Deno.mkdirSync(dirPath, { recursive: true }); } async createCacheKey(url: string) { return path.join(this.#dirPath, await hash(url)) as CacheKey; } async set(key: CacheKey, item: CacheItem) { const headersObj = Object.fromEntries(item.headers); await Deno.writeFile(key, item.body); await Deno.writeTextFile( key + ".headers", JSON.stringify(headersObj), ); } async get(key: CacheKey) { try { const body = await Deno.readFile(key); const headers = tryParseHeaders( await Deno.readTextFile(key + ".headers"), ); if (headers == null) { return undefined; } return { headers, body }; } catch (err) { if (err instanceof Deno.errors.NotFound) { return undefined; } else { throw err; } } } } function tryParseHeaders(data: string) { try { return JSON.parse(data); } catch { return undefined; } } async function hash(pathname: string) { const hashBuffer = await crypto.subtle.digest( "SHA-256", new TextEncoder().encode(pathname), ); return encodeHex(hashBuffer); }