-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlocation.ts
109 lines (93 loc) · 2.89 KB
/
location.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
import fs from "fs";
import { resolve } from "path";
import { createTempDir, unpack } from "./unpack";
export type DirectoryLocation = {
type: "directory";
path: string;
derivedName: string;
dispose: () => void;
};
export type XdcLocation = {
type: "xdc";
path: string;
filePath: string;
derivedName: string;
dispose: () => void;
};
export type UrlLocation = { type: "url"; url: string; dispose: () => void };
export type Location = DirectoryLocation | XdcLocation | UrlLocation;
export class LocationError extends Error {}
export function getLocation(location: string): Location {
if (location.startsWith("http://") || location.startsWith("https://")) {
return { type: "url", url: location, dispose: () => {} };
}
// Let's go easy on users and treat `localhost:8080` as
// `http://localhost:8080`
const localhostHostnames = ["localhost", "127.0.0.1", "[::1]"];
const startsWithSomeLocalhost = localhostHostnames.some((host) =>
location.startsWith(host),
);
if (startsWithSomeLocalhost) {
// Let's make sure that it's actually a localhost URL and it's not something
// like `localhost-activities/my-webxdc-app` or `localhost-enjoyers.com`.
let urlObj: URL | undefined;
try {
urlObj = new URL("http://" + location);
} catch (e) {}
const urlHostnameIsLocalhost = localhostHostnames.some(
(localhostString) => urlObj?.hostname === localhostString,
);
if (urlObj && urlHostnameIsLocalhost) {
return { type: "url", url: urlObj.href, dispose: () => {} };
}
}
const parts = location.split("/").filter((part) => part !== "");
const lastPart = parts[parts.length - 1];
if (!fs.existsSync(location)) {
throw new LocationError(`Not a file or directory: ${location}`);
}
const stats = fs.statSync(location);
if (location.endsWith(".xdc") && stats.isFile()) {
const path = createTempDir();
try {
unpack(location, path);
} catch (e) {
throw new LocationError(`Not a valid xdc zip file: ${location}`);
}
if (!hasIndexHtml(path)) {
throw new LocationError(
`Invalid xdc file (no index.html file inside): ${location}`,
);
}
return {
type: "xdc",
path,
filePath: location,
derivedName: lastPart.slice(0, lastPart.length - ".xdc".length),
dispose: () => {
fs.rmSync(path, { recursive: true });
},
};
}
if (!stats.isDirectory()) {
throw new LocationError(`Not an xdc file or a directory: ${location}`);
}
if (!hasIndexHtml(location)) {
throw new LocationError(
`Invalid xdc dir (no index.html file): ${location}`,
);
}
return {
type: "directory",
path: location,
derivedName: lastPart,
dispose: () => {},
};
}
function hasIndexHtml(location: string): boolean {
const p = resolve(location, "index.html");
if (!fs.existsSync(p)) {
return false;
}
return fs.statSync(p).isFile();
}