forked from denosaurs/plug
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.ts
135 lines (122 loc) Β· 2.97 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
import {
hex,
isAbsolute,
join,
normalize,
resolve,
toFileUrl,
} from "./deps.ts";
export const encoder = new TextEncoder();
export const decoder = new TextDecoder();
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);
}
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));
}
export async function hash(value: string): Promise<string> {
return decoder.decode(
hex(
new Uint8Array(
await crypto.subtle.digest("SHA-256", encoder.encode(value)),
),
),
);
}
export async function urlToFilename(url: URL): Promise<string> {
const cacheFilename = baseUrlToFilename(url);
const hashedFilename = await hash(url.pathname + url.search);
return join(cacheFilename, hashedFilename);
}
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.
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");
}
}
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");
}
}
}
}
export function denoCacheDir() {
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;
}