-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
util.ts
161 lines (147 loc) Β· 3.45 KB
/
util.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/**
* This file contains useful utility functions used by plug.
*
* @module
*/
import { isAbsolute, join, normalize, resolve, toFileUrl } from "@std/path";
import { encodeHex } from "@std/encoding/hex";
const encoder = new TextEncoder();
function baseUrlToFilename(url: URL): string {
const out = [];
const protocol = url.protocol.replace(":", "");
out.push(protocol);
switch (protocol) {
case "http":
case "https": {
const host = url.hostname;
const hostPort = url.port;
out.push(hostPort ? `${host}_PORT${hostPort}` : host);
break;
}
case "file":
case "data":
case "blob":
break;
default:
throw new TypeError(
`Don't know how to create cache name for protocol: ${protocol}`,
);
}
return join(...out);
}
/**
* Transforms a string into a URL.
*
* @private
*/
export function stringToURL(url: string): URL {
// deno-fmt-ignore
return url.startsWith("file://")
|| url.startsWith("http://")
|| url.startsWith("https://")
? new URL(url)
: toFileUrl(resolve(url));
}
/**
* SHA-256 hashes a string. Used internally to hash URLs for cache filenames.
*
* @private
*/
export async function hash(value: string): Promise<string> {
return encodeHex(
new Uint8Array(
await crypto.subtle.digest("SHA-256", encoder.encode(value)),
),
);
}
/**
* Transforms a URL into a filename for the cache.
*
* @private
*/
export async function urlToFilename(url: URL): Promise<string> {
const cacheFilename = baseUrlToFilename(url);
const hashedFilename = await hash(url.pathname + url.search);
return join(cacheFilename, hashedFilename);
}
/**
* Checks if a file exists.
*
* @private
*/
export async function isFile(filePath: string): Promise<boolean> {
try {
const stats = await Deno.lstat(filePath);
return stats.isFile;
} catch (err) {
if (err instanceof Deno.errors.NotFound) {
return false;
}
throw err;
}
}
// The rest of is based on code from denoland/deno_cache by the Deno authors
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
/**
* @returns The home directory of the user.
*/
export function homeDir(): string | undefined {
switch (Deno.build.os) {
case "windows":
return Deno.env.get("USERPROFILE");
case "linux":
case "darwin":
case "freebsd":
case "netbsd":
case "aix":
case "solaris":
case "illumos":
return Deno.env.get("HOME");
default:
throw Error("unreachable");
}
}
/**
* @returns The cache directory of the user.
*/
export function cacheDir(): string | undefined {
if (Deno.build.os === "darwin") {
const home = homeDir();
if (home) {
return join(home, "Library/Caches");
}
} else if (Deno.build.os === "windows") {
return Deno.env.get("LOCALAPPDATA");
} else {
const cacheHome = Deno.env.get("XDG_CACHE_HOME");
if (cacheHome) {
return cacheHome;
} else {
const home = homeDir();
if (home) {
return join(home, ".cache");
}
}
}
}
/**
* @returns The cache directory for Deno.
*/
export function denoCacheDir(): string | undefined {
const dd = Deno.env.get("DENO_DIR");
let root;
if (dd) {
root = normalize(isAbsolute(dd) ? dd : join(Deno.cwd(), dd));
} else {
const cd = cacheDir();
if (cd) {
root = join(cd, "deno");
} else {
const hd = homeDir();
if (hd) {
root = join(hd, ".deno");
}
}
}
return root;
}