-
Notifications
You must be signed in to change notification settings - Fork 16
/
mod.ts
111 lines (104 loc) · 3.68 KB
/
mod.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright 2018-2024 the Deno authors. MIT license.
/**
* A module which provides a TypeScript implementation of the Deno CLI's cache
* directory logic (`DENO_DIR`). This can be used in combination with other
* modules to provide user loadable APIs that are like the Deno CLI's
* functionality.
*
* This also can provide user read access in Deploy to a Deno CLI's cache when
* the cache is checked into the repository.
*
* ### Example
*
* ```ts
* import { createCache } from "@deno/cache-dir";
* import { createGraph } from "@deno/graph";
*
* // create a cache where the location will be determined environmentally
* const cache = createCache();
* // destructuring the two functions we need to pass to the graph
* const { cacheInfo, load } = cache;
* // create a graph that will use the cache above to load and cache dependencies
* const graph = await createGraph("https://deno.land/x/[email protected]/mod.ts", {
* cacheInfo,
* load,
* });
*
* // log out the console a similar output to `deno info` on the command line.
* console.log(graph.toString());
* ```
*
* @module
*/
import { FetchCacher } from "./cache.ts";
import type { CacheInfo, LoadResponse } from "@deno/graph";
import { DenoDir } from "./deno_dir.ts";
import { type CacheSetting, FileFetcher } from "./file_fetcher.ts";
export { FetchCacher } from "./cache.ts";
export { DenoDir } from "./deno_dir.ts";
export { HttpCache } from "./http_cache.ts";
export { DiskCache } from "./disk_cache.ts";
export { type CacheSetting, FileFetcher } from "./file_fetcher.ts";
export interface Loader {
/** A function that can be passed to a `deno_graph` building function to
* provide information about the cache to populate the output.
*/
cacheInfo?(specifier: string): CacheInfo;
/** A function that can be passed to a `deno_graph` that will load and cache
* dependencies in the graph in the disk cache.
*/
load(
specifier: string,
isDynamic?: boolean,
cacheSetting?: CacheSetting,
checksum?: string,
): Promise<LoadResponse | undefined>;
}
export type { LoadResponse } from "@deno/graph";
export type {
LoadResponseExternal,
LoadResponseModule,
} from "@deno/graph/types";
export interface CacheOptions {
/** Allow remote URLs to be fetched if missing from the cache. This defaults
* to `true`. Setting it to `false` is like passing the `--no-remote` in the
* Deno CLI, meaning that any modules not in cache error. */
allowRemote?: boolean;
/** Determines how the cache will be used. The default value is `"use"`
* meaning the cache will be used, and any remote module cache misses will
* be fetched and stored in the cache. */
cacheSetting?: CacheSetting;
/** This forces the cache into a `readOnly` mode, where fetched resources
* will not be stored on disk if `true`. The default is detected from the
* environment, checking to see if `Deno.writeFile` exists. */
readOnly?: boolean;
/** Specifies a path to the root of the cache. Setting this value overrides
* the detection of location from the environment. */
root?: string | URL;
/** Specifies a path to the local vendor directory if it exists. */
vendorRoot?: string | URL;
}
/**
* Creates a cache object that allows access to the internal `DENO_DIR` cache
* structure for remote dependencies and cached output of emitted modules.
*/
export function createCache({
root,
cacheSetting = "use",
allowRemote = true,
readOnly,
vendorRoot,
}: CacheOptions = {}): Loader {
const denoDir = new DenoDir(root);
const fileFetcher = new FileFetcher(
() => {
return denoDir.createHttpCache({
readOnly,
vendorRoot,
});
},
cacheSetting,
allowRemote,
);
return new FetchCacher(fileFetcher);
}