-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathcache.ts
33 lines (28 loc) · 913 Bytes
/
cache.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// Copyright 2018-2024 the Deno authors. MIT license.
import type { LoadResponse } from "@deno/graph";
import type { CacheSetting, FileFetcher } from "./file_fetcher.ts";
/** Provides an interface to Deno's CLI cache.
*
* It is better to use the {@linkcode createCache} function directly. */
export class FetchCacher {
#fileFetcher: FileFetcher;
constructor(fileFetcher: FileFetcher) {
this.#fileFetcher = fileFetcher;
}
// this should have the same interface as deno_graph's loader
load = (
specifier: string,
_isDynamic?: boolean,
cacheSetting?: CacheSetting,
checksum?: string,
): Promise<LoadResponse | undefined> => {
const url = new URL(specifier);
return this.#fileFetcher.fetchOnce(url, { cacheSetting, checksum })
.catch((e) => {
if (e instanceof Deno.errors.NotFound) {
return undefined;
}
throw e;
});
};
}