@oazmi/build-tools
Version:
general deno build tool scripts which I practically use in all of my typescript repos
56 lines (55 loc) • 1.91 kB
JavaScript
// Copyright 2018-2025 the Deno authors. MIT license.
import * as dntShim from "../../../../../_dnt.shims.js";
import { isAbsolute } from "../../../@std/path/1.1.3/mod.js";
import { assert } from "./util.js";
import { instantiate, } from "./lib/deno_cache_dir.generated.js";
export class HttpCache {
#cache;
#readOnly;
constructor(cache, readOnly) {
this.#cache = cache;
this.#readOnly = readOnly;
}
static async create(options) {
assert(isAbsolute(options.root), "Root must be an absolute path.");
if (options.vendorRoot != null) {
assert(isAbsolute(options.vendorRoot), "Vendor root must be an absolute path.");
}
const { GlobalHttpCache, LocalHttpCache } = await instantiate();
let cache;
if (options.vendorRoot != null) {
cache = LocalHttpCache.new(options.vendorRoot, options.root,
/* allow global to local copy */ !options.readOnly);
}
else {
cache = GlobalHttpCache.new(options.root);
}
return new HttpCache(cache, options.readOnly);
}
[Symbol.dispose]() {
this.free();
}
free() {
this.#cache?.free();
}
getHeaders(url) {
const map = this.#cache.getHeaders(url.toString());
return map == null ? undefined : Object.fromEntries(map);
}
get(url, options) {
const data = this.#cache.get(url.toString(), options?.checksum);
return data == null ? undefined : data;
}
set(url, headers, content) {
if (this.#readOnly === undefined) {
this.#readOnly =
(dntShim.Deno.permissions.querySync({ name: "write" })).state === "denied"
? true
: false;
}
if (this.#readOnly) {
return;
}
this.#cache.set(url.toString(), headers, content);
}
}