-
-
Notifications
You must be signed in to change notification settings - Fork 65
/
resolver.ts
68 lines (56 loc) · 1.74 KB
/
resolver.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
import { crypto, extname, resolve, toFileUrl } from "./deps.ts";
import { apiDirectory } from "./env.ts";
export const jsify = (file: string) => {
return file.replace(extname(file), ".js");
};
export const tsify = (file: string) => {
return file.replace(extname(file), ".ts");
};
export const jsxify = (file: string) => {
return file.replace(extname(file), ".jsx");
};
export const tsxify = (file: string) => {
return file.replace(extname(file), ".tsx");
};
export const isValidUrl = (url: string) => {
try {
return new URL(url);
} catch (_e) {
return false;
}
};
export const hashFile = (url: string) => {
// strip query params from hashing
url = url.split("?")[0];
const msgUint8 = new TextEncoder().encode(url);
const hashBuffer = crypto.subtle.digestSync("SHA-256", msgUint8);
const hashArray = Array.from(new Uint8Array(hashBuffer));
const hashHex = hashArray.map((b) => b.toString(16).padStart(2, "0")).join(
"",
);
return hashHex;
};
export const stripTrailingSlash = (url: string) => {
return url.endsWith("/") ? url.slice(0, -1) : url;
};
export const resolveFileUrl = (from: string, to: string) => {
return new URL(toFileUrl(resolve(from, to)).toString());
};
export const cacheBuster = (source: string, timestamp?: number) => {
return source.replace(
/\.(j|t)sx?/gi,
() => {
return `.js${timestamp ? `?ts=${timestamp}` : ""}`;
},
);
};
export const isRemoteSource = (value: string) => {
return value.startsWith("https://") ||
value.startsWith("http://");
};
export const isVendorSource = (value: string, vendorDirectory: string) => {
return value.indexOf(`.ultra/${vendorDirectory}`) >= 0;
};
export const isApiRoute = (value: string) => {
return value.indexOf(apiDirectory) >= 0;
};