-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils.ts
189 lines (152 loc) · 5.05 KB
/
utils.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import * as os from "os";
import * as fs from "fs/promises";
export async function findManifestJSON(packageType: string): Promise<string> {
const manifestJSONPath = `${packageType}.json`;
try {
await fs.access(manifestJSONPath, fs.constants.F_OK);
return manifestJSONPath;
} catch (_e) {
// Ignore error
}
const staticFile = `static/${packageType}.json`;
try {
await fs.access(staticFile, fs.constants.F_OK);
return staticFile;
} catch (_e) {
// Ignore error
}
throw new Error(
`Could not find manifest JSON at ${JSON.stringify(manifestJSONPath)} or ${JSON.stringify(staticFile)}`,
);
}
declare global {
namespace NodeJS {
interface ProcessEnv {
FOUNDRY_DATA_PATH?: string;
FOUNDRY_HOST_NAME?: string;
FOUNDRY_PORT?: string;
LOCALAPPDATA?: string;
XDG_DATA_HOME?: string;
}
}
}
type HostData = {
isWSLToWindows: boolean;
isHostConfigured: boolean;
host: string;
};
export async function findFoundryHost(): Promise<HostData> {
const foundryHostNameEnv = process.env.FOUNDRY_HOST_NAME;
const hasHostEnv = foundryHostNameEnv != null;
const foundryHost = foundryHostNameEnv ?? "localhost";
let foundryPort: number;
const envPortString = process.env.FOUNDRY_PORT;
const isHostConfigured = hasHostEnv || envPortString != null;
if (envPortString != null) {
if (!/[0-9]+/.test(envPortString)) {
throw new Error(
`Expected FOUNDRY_PORT to be a number, got ${JSON.stringify(envPortString)}.`,
);
}
foundryPort = Number.parseInt(envPortString, 10);
} else {
foundryPort = 30000;
}
const pingResult = await ping(foundryHost, foundryPort);
if (pingResult.error == null) {
return { isWSLToWindows: false, isHostConfigured, host: pingResult.host };
}
const foundryNotRuning =
"If Foundry is not running please start it. Otherwise if Foundry is running on a custom address set FOUNDRY_HOST_NAME and FOUNDRY_PORT.";
// If the environment variable in WSL isn't set also try reaching the host through Windows.
if (!hasHostEnv && (await isWSL())) {
// The default host of localhost won't work on WSL if the server is running on Windows.
// Reaching Windows is possible through `${hostname}.local`.
const hostname = os.hostname();
const wslToWindowsPingResult = await ping(`${hostname}.local`, foundryPort);
if (wslToWindowsPingResult.error == null) {
return {
isWSLToWindows: true,
isHostConfigured,
host: wslToWindowsPingResult.host,
};
}
throw new Error(
`Could not ping localhost:30000 (WSL) or ${hostname}.local (Windows)
${foundryNotRuning}
WSL Error - ${formatError(pingResult.error)}
Windows Error - ${formatError(wslToWindowsPingResult.error)}`,
);
}
throw new Error(
`Could not ping localhost:30000
${foundryNotRuning}
Error: ${pingResult.error.message}`,
);
}
type PingResult =
| { host: string; error?: never }
| { host?: never; error: Error };
async function ping(hostName: string, port: number): Promise<PingResult> {
if (!Number.isInteger(port)) {
return {
error: new Error(`Port must be a valid integer got ${port}`),
};
}
if (port < 0 || port > 65535) {
return {
error: new Error(`Port must be between 0 and 65535, got ${port}`),
};
}
const host = `${hostName}:${port}`;
try {
const response = await fetch(`http://${host}`, { method: "OPTION" });
await response.body?.cancel("Body not needed");
} catch (error) {
return { error: toError(error) };
}
return { host: `${hostName}:${port}` };
}
// Since anything can be thrown, e.g `throw 1`, this function ensures it's an instance of `Error`.
function toError(error: unknown): Error {
if (error instanceof Error) {
return error;
}
return new Error(formatError(error));
}
function formatError(error: unknown): string {
if (!(error instanceof Error)) {
// @ts-expect-error if `toString` fails there's nothing to do anyways
// eslint-disable-next-line
return error.toString();
}
if (error.cause != null) {
// If `toString` fails there's nothing to do anyways
// eslint-disable-next-line
return `${error.toString()}, cause = ${error.cause.toString()}`;
}
return error.toString();
}
// A cached value of whether the current environment is WSL
let _isWSL: boolean | undefined = undefined;
async function isWSL(): Promise<boolean> {
if (_isWSL != null) {
return _isWSL;
}
// Checking for the WSLInterop file seems to be the way that the ecosystem has settled on to check if the system is running WSL.
// See for example: https://github.com/canonical/snapd/blob/37eb0a311917af15622237db10011d9c62e8cb12/release/release.go#L151
try {
await fs.access("/proc/sys/fs/binfmt_misc/WSLInterop", fs.constants.F_OK);
_isWSL = true;
return _isWSL;
} catch (_e) {
// Ignore this error. It just means that the file doesn't exist but there's a fallback to check next.
}
try {
await fs.access("/run/WSL", fs.constants.F_OK);
_isWSL = true;
} catch (_e) {
_isWSL = false;
}
return _isWSL;
}