dsh-wsl-workspace 0.3.0 → 0.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.de.md +3 -0
- package/README.es.md +3 -0
- package/README.fr.md +3 -0
- package/README.ja.md +3 -0
- package/README.ko.md +3 -0
- package/README.md +12 -0
- package/README.pt.md +3 -0
- package/README.ru.md +3 -0
- package/README.zh.md +3 -0
- package/TESTING.md +103 -0
- package/lib/fs.js +1 -1
- package/lib/index.js +351 -1
- package/lib/index.js.map +1 -1
- package/lib/shell.js +382 -382
- package/lib/wsl-C5_mxGPM.js +227 -227
- package/lib/wsl-credentials-BI4v5TNZ.js +108 -108
- package/package.json +2 -1
- package/src/host/wsl-skills.ts +430 -0
- package/src/index.ts +15 -0
- package/lib/paths-BDE1NVOv.js +0 -120
- package/lib/paths-BDE1NVOv.js.map +0 -1
- package/lib/wsl-DdNPGaPo.js +0 -1019
- package/lib/wsl-DdNPGaPo.js.map +0 -1
package/lib/wsl-C5_mxGPM.js
CHANGED
|
@@ -1,228 +1,228 @@
|
|
|
1
|
-
import { execFile, execFileSync } from "node:child_process";
|
|
2
|
-
import { promisify } from "node:util";
|
|
3
|
-
//#region src/shared/paths.ts
|
|
4
|
-
/** The two UNC hosts WSL exposes a distribution's filesystem under. */
|
|
5
|
-
const UNC_HOSTS = ["wsl.localhost", "wsl$"];
|
|
6
|
-
/**
|
|
7
|
-
* Parse a WSL UNC path into its distro and Linux path. Accepts the WSL2
|
|
8
|
-
* `\\wsl.localhost\<distro>\<linux>` form, the legacy `\\wsl$\<distro>\<linux>`
|
|
9
|
-
* interop form, and forward-slash spellings of either.
|
|
10
|
-
* @param raw - candidate absolute path.
|
|
11
|
-
* @returns the parsed target, or null when the path is not a WSL UNC.
|
|
12
|
-
*/
|
|
13
|
-
function parseWslUnc(raw) {
|
|
14
|
-
const normalized = raw.replace(/\\/g, "/").replace(/\/\/+/g, "//");
|
|
15
|
-
if (!normalized.startsWith("//")) return null;
|
|
16
|
-
const segments = normalized.slice(2).split("/");
|
|
17
|
-
const host = (segments[0] ?? "").toLowerCase();
|
|
18
|
-
if (!UNC_HOSTS.includes(host)) return null;
|
|
19
|
-
const distro = segments[1] ?? "";
|
|
20
|
-
if (distro === "") return null;
|
|
21
|
-
return {
|
|
22
|
-
distro,
|
|
23
|
-
linuxPath: `/${segments.slice(2).filter((segment) => segment.length > 0).join("/")}`
|
|
24
|
-
};
|
|
25
|
-
}
|
|
26
|
-
/**
|
|
27
|
-
* Normalize a Linux absolute path for the Host: collapse repeated slashes and
|
|
28
|
-
* strip a trailing slash (root becomes `/`).
|
|
29
|
-
* @param path - absolute Linux path.
|
|
30
|
-
* @returns the normalized path.
|
|
31
|
-
*/
|
|
32
|
-
function normalizeLinuxPath(path) {
|
|
33
|
-
const collapsed = path.replace(/\/+/g, "/");
|
|
34
|
-
return collapsed === "/" ? "/" : collapsed.replace(/\/$/, "");
|
|
35
|
-
}
|
|
36
|
-
/**
|
|
37
|
-
* Whether a path is an absolute, non-empty Linux path.
|
|
38
|
-
* @param path - candidate.
|
|
39
|
-
* @returns whether it starts with `/` and contains no NUL.
|
|
40
|
-
*/
|
|
41
|
-
function isAbsoluteLinuxPath(path) {
|
|
42
|
-
return path.startsWith("/") && !path.includes("\0");
|
|
43
|
-
}
|
|
44
|
-
/**
|
|
45
|
-
* Join a distro and a Linux absolute path into the WSL2 UNC form used as the
|
|
46
|
-
* workspace identity (`\\wsl.localhost\<distro>\<linux>`, backslash segments).
|
|
47
|
-
* @param distro - distro name.
|
|
48
|
-
* @param linuxPath - absolute Linux path (leading `/`).
|
|
49
|
-
* @returns the UNC path.
|
|
50
|
-
*/
|
|
51
|
-
function joinUnc(distro, linuxPath) {
|
|
52
|
-
if (!isAbsoluteLinuxPath(linuxPath)) throw new Error(`wsl-workspace: cannot map a non-absolute Linux path "${linuxPath}" to UNC`);
|
|
53
|
-
if (distro === "" || distro === "." || distro === ".." || /[\\/]/.test(distro)) throw new Error(`wsl-workspace: invalid distribution name "${distro}"`);
|
|
54
|
-
const normalized = linuxPath.replace(/\/+/g, "/").replace(/\/$/, "");
|
|
55
|
-
const windowsSegments = (normalized.startsWith("/") ? normalized.slice(1) : normalized).replace(/\//g, "\\");
|
|
56
|
-
return `\\\\wsl.localhost\\${distro}${windowsSegments === "" ? "" : `\\${windowsSegments}`}`;
|
|
57
|
-
}
|
|
58
|
-
/**
|
|
59
|
-
* Translate a Windows drive path to the drvfs mount path WSL distributions
|
|
60
|
-
* conventionally expose it at (`C:\foo` → `/mnt/c/foo`). Only single-letter
|
|
61
|
-
* drives under `/mnt` are mapped; custom mount points are out of scope.
|
|
62
|
-
* @param path - the candidate Windows path.
|
|
63
|
-
* @returns the `/mnt/<drive>/…` path, or `null` for non-drive paths.
|
|
64
|
-
*/
|
|
65
|
-
function windowsToMntPath(path) {
|
|
66
|
-
const match = /^([A-Za-z]):[\\/](.*)$/.exec(path);
|
|
67
|
-
if (match === null) return null;
|
|
68
|
-
const rest = (match[2] ?? "").replace(/\\/g, "/").replace(/\/+/g, "/").replace(/\/$/, "");
|
|
69
|
-
return `/mnt/${(match[1] ?? "").toLowerCase()}${rest === "" ? "" : `/${rest}`}`;
|
|
70
|
-
}
|
|
71
|
-
/**
|
|
72
|
-
* Translate a `/mnt/<drive>/…` path back to its Windows drive path.
|
|
73
|
-
* @param linuxPath - the candidate Linux path.
|
|
74
|
-
* @returns the `X:\…` drive path, or `null` when the path is not a drvfs mount.
|
|
75
|
-
*/
|
|
76
|
-
function mntToWindowsPath(linuxPath) {
|
|
77
|
-
const match = /^\/mnt\/([a-zA-Z])(?:\/(.*))?$/.exec(linuxPath);
|
|
78
|
-
if (match === null) return null;
|
|
79
|
-
const rest = (match[2] ?? "").replace(/\//g, "\\");
|
|
80
|
-
return `${(match[1] ?? "").toUpperCase()}:\\${rest}`;
|
|
81
|
-
}
|
|
82
|
-
/**
|
|
83
|
-
* Canonical Windows drive path for store keys and cross-realm identity:
|
|
84
|
-
* separators unified to `\`, trailing separator stripped, and the WHOLE path
|
|
85
|
-
* lowercased — Windows paths compare case-insensitively, and the workspace
|
|
86
|
-
* registry may realpath a different casing than the caller spelled (8.3 or
|
|
87
|
-
* on-disk casing), so the store key must collide across casings.
|
|
88
|
-
* @param path - candidate Windows drive path.
|
|
89
|
-
* @returns the canonical form, or `null` when not drive-shaped.
|
|
90
|
-
*/
|
|
91
|
-
function canonicalWindowsPath(path) {
|
|
92
|
-
const match = /^([A-Za-z]):[\\/](.*)$/.exec(path);
|
|
93
|
-
if (match === null) return null;
|
|
94
|
-
const rest = (match[2] ?? "").replace(/[\\/]+/g, "\\").replace(/\\$/, "").toLowerCase();
|
|
95
|
-
return `${(match[1] ?? "").toLowerCase()}:\\${rest}`;
|
|
96
|
-
}
|
|
97
|
-
/**
|
|
98
|
-
* True when a value is a Windows-shaped path (drive or UNC), which is how
|
|
99
|
-
* the shell executor decides the WSLENV `/p` translation flag: only Windows
|
|
100
|
-
* path values need translation when they cross into the Linux process.
|
|
101
|
-
* @param value - the environment value to classify.
|
|
102
|
-
* @returns whether the value looks like a Windows path.
|
|
103
|
-
*/
|
|
104
|
-
function isWindowsPathShaped(value) {
|
|
105
|
-
return /^[A-Za-z]:[\\/]/.test(value) || value.startsWith("\\\\");
|
|
106
|
-
}
|
|
107
|
-
/** Linux username shape for `wsl.exe -u`: starts with a letter or underscore, then letters/digits/`_`/`.`/`-` (max 64). */
|
|
108
|
-
const WSL_USERNAME_PATTERN = /^[A-Za-z_][A-Za-z0-9_.-]{0,63}$/;
|
|
109
|
-
/**
|
|
110
|
-
* Whether a value is a safe Linux username for `wsl.exe -u`. The check is
|
|
111
|
-
* strict on purpose: a value starting with `-` could be parsed as a wsl.exe
|
|
112
|
-
* option instead of a username.
|
|
113
|
-
* @param value - candidate username.
|
|
114
|
-
* @returns whether it matches the Linux username shape.
|
|
115
|
-
*/
|
|
116
|
-
function isValidWslUsername(value) {
|
|
117
|
-
return WSL_USERNAME_PATTERN.test(value);
|
|
118
|
-
}
|
|
119
|
-
//#endregion
|
|
120
|
-
//#region src/shared/wsl.ts
|
|
121
|
-
/**
|
|
122
|
-
* WSL discovery helpers (host side): enumerate installed distributions
|
|
123
|
-
* through `wsl.exe -l -q` and read the default distribution from the Lxss
|
|
124
|
-
* registry key. `wsl.exe` output is UTF-16LE on most builds, so decoding
|
|
125
|
-
* sniffs for NUL bytes before choosing an encoding.
|
|
126
|
-
* @module dsh-wsl-workspace/shared/wsl
|
|
127
|
-
*/
|
|
128
|
-
const execFileAsync = promisify(execFile);
|
|
129
|
-
/** Executable timeout for the short discovery calls. */
|
|
130
|
-
const DISCOVERY_TIMEOUT_MS = 1e4;
|
|
131
|
-
const LXSS_KEY = "HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Lxss";
|
|
132
|
-
/** Human text for an unknown rejection. */
|
|
133
|
-
function messageOf(value) {
|
|
134
|
-
return value instanceof Error ? value.message : String(value);
|
|
135
|
-
}
|
|
136
|
-
/**
|
|
137
|
-
* Decode `wsl.exe -l -q` output. Newer builds emit UTF-8; most emit UTF-16LE
|
|
138
|
-
* with NUL bytes interleaved — the NUL probe picks the right one.
|
|
139
|
-
* @param buffer - the raw captured output.
|
|
140
|
-
* @returns the decoded text.
|
|
141
|
-
*/
|
|
142
|
-
function decodeWslOutput(buffer) {
|
|
143
|
-
return buffer.includes(0) ? buffer.toString("utf16le") : buffer.toString("utf8");
|
|
144
|
-
}
|
|
145
|
-
/**
|
|
146
|
-
* List installed WSL distributions in `wsl.exe` order.
|
|
147
|
-
* @param wslPath - the `wsl.exe` executable (absolute or PATH name).
|
|
148
|
-
* @returns distribution names, blank lines dropped.
|
|
149
|
-
*/
|
|
150
|
-
async function listDistros(wslPath = "wsl.exe") {
|
|
151
|
-
let stdout;
|
|
152
|
-
try {
|
|
153
|
-
stdout = (await execFileAsync(wslPath, ["-l", "-q"], {
|
|
154
|
-
encoding: "buffer",
|
|
155
|
-
timeout: DISCOVERY_TIMEOUT_MS
|
|
156
|
-
})).stdout;
|
|
157
|
-
} catch (error) {
|
|
158
|
-
throw new Error(`wsl-workspace: cannot list WSL distributions (${messageOf(error)}); is WSL installed?`);
|
|
159
|
-
}
|
|
160
|
-
return decodeWslOutput(stdout).split(/\r?\n/).map((line) => line.trim()).filter((line) => line.length > 0);
|
|
161
|
-
}
|
|
162
|
-
/**
|
|
163
|
-
* Read the user's default distribution from the Lxss registry. Non-fatal:
|
|
164
|
-
* returns `undefined` when the value is absent or unreadable (the caller
|
|
165
|
-
* falls back to list order).
|
|
166
|
-
* @returns the default distribution name, or `undefined`.
|
|
167
|
-
*/
|
|
168
|
-
async function defaultDistro() {
|
|
169
|
-
try {
|
|
170
|
-
const value = await execFileAsync("reg.exe", [
|
|
171
|
-
"query",
|
|
172
|
-
LXSS_KEY,
|
|
173
|
-
"/v",
|
|
174
|
-
"DefaultDistribution"
|
|
175
|
-
], { timeout: DISCOVERY_TIMEOUT_MS });
|
|
176
|
-
const guid = /DefaultDistribution\s+REG_SZ\s+(\{[0-9a-fA-F-]+\})/i.exec(value.stdout)?.[1];
|
|
177
|
-
if (guid === void 0) return void 0;
|
|
178
|
-
const name = await execFileAsync("reg.exe", [
|
|
179
|
-
"query",
|
|
180
|
-
`${LXSS_KEY}\\${guid}`,
|
|
181
|
-
"/v",
|
|
182
|
-
"DistributionName"
|
|
183
|
-
], { timeout: DISCOVERY_TIMEOUT_MS });
|
|
184
|
-
const distro = /DistributionName\s+REG_SZ\s+(.+)/i.exec(name.stdout)?.[1]?.trim();
|
|
185
|
-
return distro === void 0 || distro === "" ? void 0 : distro;
|
|
186
|
-
} catch {
|
|
187
|
-
return;
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
/** Module-level cache for {@link defaultDistroSync} (one registry read per process). */
|
|
191
|
-
let syncDefaultResolved = false;
|
|
192
|
-
let syncDefault;
|
|
193
|
-
/**
|
|
194
|
-
* Synchronous variant of {@link defaultDistro} for executors that must
|
|
195
|
-
* resolve a distribution inside a synchronous plan step. Cached after the
|
|
196
|
-
* first read; non-fatal (returns `undefined` when the registry is
|
|
197
|
-
* unreadable, letting the caller fail loud with its own message).
|
|
198
|
-
* @returns the default distribution name, or `undefined`.
|
|
199
|
-
*/
|
|
200
|
-
function defaultDistroSync() {
|
|
201
|
-
if (syncDefaultResolved) return syncDefault;
|
|
202
|
-
syncDefaultResolved = true;
|
|
203
|
-
try {
|
|
204
|
-
const value = execFileSync("reg.exe", [
|
|
205
|
-
"query",
|
|
206
|
-
LXSS_KEY,
|
|
207
|
-
"/v",
|
|
208
|
-
"DefaultDistribution"
|
|
209
|
-
], { timeout: DISCOVERY_TIMEOUT_MS });
|
|
210
|
-
const guid = /DefaultDistribution\s+REG_SZ\s+(\{[0-9a-fA-F-]+\})/i.exec(String(value))?.[1];
|
|
211
|
-
if (guid === void 0) return void 0;
|
|
212
|
-
const name = execFileSync("reg.exe", [
|
|
213
|
-
"query",
|
|
214
|
-
`${LXSS_KEY}\\${guid}`,
|
|
215
|
-
"/v",
|
|
216
|
-
"DistributionName"
|
|
217
|
-
], { timeout: DISCOVERY_TIMEOUT_MS });
|
|
218
|
-
const distro = /DistributionName\s+REG_SZ\s+(.+)/i.exec(String(name))?.[1]?.trim();
|
|
219
|
-
syncDefault = distro === void 0 || distro === "" ? void 0 : distro;
|
|
220
|
-
} catch {
|
|
221
|
-
syncDefault = void 0;
|
|
222
|
-
}
|
|
223
|
-
return syncDefault;
|
|
224
|
-
}
|
|
225
|
-
//#endregion
|
|
226
|
-
export { isAbsoluteLinuxPath as a, joinUnc as c, parseWslUnc as d, windowsToMntPath as f, canonicalWindowsPath as i, mntToWindowsPath as l, defaultDistroSync as n, isValidWslUsername as o, listDistros as r, isWindowsPathShaped as s, defaultDistro as t, normalizeLinuxPath as u };
|
|
227
|
-
|
|
1
|
+
import { execFile, execFileSync } from "node:child_process";
|
|
2
|
+
import { promisify } from "node:util";
|
|
3
|
+
//#region src/shared/paths.ts
|
|
4
|
+
/** The two UNC hosts WSL exposes a distribution's filesystem under. */
|
|
5
|
+
const UNC_HOSTS = ["wsl.localhost", "wsl$"];
|
|
6
|
+
/**
|
|
7
|
+
* Parse a WSL UNC path into its distro and Linux path. Accepts the WSL2
|
|
8
|
+
* `\\wsl.localhost\<distro>\<linux>` form, the legacy `\\wsl$\<distro>\<linux>`
|
|
9
|
+
* interop form, and forward-slash spellings of either.
|
|
10
|
+
* @param raw - candidate absolute path.
|
|
11
|
+
* @returns the parsed target, or null when the path is not a WSL UNC.
|
|
12
|
+
*/
|
|
13
|
+
function parseWslUnc(raw) {
|
|
14
|
+
const normalized = raw.replace(/\\/g, "/").replace(/\/\/+/g, "//");
|
|
15
|
+
if (!normalized.startsWith("//")) return null;
|
|
16
|
+
const segments = normalized.slice(2).split("/");
|
|
17
|
+
const host = (segments[0] ?? "").toLowerCase();
|
|
18
|
+
if (!UNC_HOSTS.includes(host)) return null;
|
|
19
|
+
const distro = segments[1] ?? "";
|
|
20
|
+
if (distro === "") return null;
|
|
21
|
+
return {
|
|
22
|
+
distro,
|
|
23
|
+
linuxPath: `/${segments.slice(2).filter((segment) => segment.length > 0).join("/")}`
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Normalize a Linux absolute path for the Host: collapse repeated slashes and
|
|
28
|
+
* strip a trailing slash (root becomes `/`).
|
|
29
|
+
* @param path - absolute Linux path.
|
|
30
|
+
* @returns the normalized path.
|
|
31
|
+
*/
|
|
32
|
+
function normalizeLinuxPath(path) {
|
|
33
|
+
const collapsed = path.replace(/\/+/g, "/");
|
|
34
|
+
return collapsed === "/" ? "/" : collapsed.replace(/\/$/, "");
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Whether a path is an absolute, non-empty Linux path.
|
|
38
|
+
* @param path - candidate.
|
|
39
|
+
* @returns whether it starts with `/` and contains no NUL.
|
|
40
|
+
*/
|
|
41
|
+
function isAbsoluteLinuxPath(path) {
|
|
42
|
+
return path.startsWith("/") && !path.includes("\0");
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Join a distro and a Linux absolute path into the WSL2 UNC form used as the
|
|
46
|
+
* workspace identity (`\\wsl.localhost\<distro>\<linux>`, backslash segments).
|
|
47
|
+
* @param distro - distro name.
|
|
48
|
+
* @param linuxPath - absolute Linux path (leading `/`).
|
|
49
|
+
* @returns the UNC path.
|
|
50
|
+
*/
|
|
51
|
+
function joinUnc(distro, linuxPath) {
|
|
52
|
+
if (!isAbsoluteLinuxPath(linuxPath)) throw new Error(`wsl-workspace: cannot map a non-absolute Linux path "${linuxPath}" to UNC`);
|
|
53
|
+
if (distro === "" || distro === "." || distro === ".." || /[\\/]/.test(distro)) throw new Error(`wsl-workspace: invalid distribution name "${distro}"`);
|
|
54
|
+
const normalized = linuxPath.replace(/\/+/g, "/").replace(/\/$/, "");
|
|
55
|
+
const windowsSegments = (normalized.startsWith("/") ? normalized.slice(1) : normalized).replace(/\//g, "\\");
|
|
56
|
+
return `\\\\wsl.localhost\\${distro}${windowsSegments === "" ? "" : `\\${windowsSegments}`}`;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Translate a Windows drive path to the drvfs mount path WSL distributions
|
|
60
|
+
* conventionally expose it at (`C:\foo` → `/mnt/c/foo`). Only single-letter
|
|
61
|
+
* drives under `/mnt` are mapped; custom mount points are out of scope.
|
|
62
|
+
* @param path - the candidate Windows path.
|
|
63
|
+
* @returns the `/mnt/<drive>/…` path, or `null` for non-drive paths.
|
|
64
|
+
*/
|
|
65
|
+
function windowsToMntPath(path) {
|
|
66
|
+
const match = /^([A-Za-z]):[\\/](.*)$/.exec(path);
|
|
67
|
+
if (match === null) return null;
|
|
68
|
+
const rest = (match[2] ?? "").replace(/\\/g, "/").replace(/\/+/g, "/").replace(/\/$/, "");
|
|
69
|
+
return `/mnt/${(match[1] ?? "").toLowerCase()}${rest === "" ? "" : `/${rest}`}`;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Translate a `/mnt/<drive>/…` path back to its Windows drive path.
|
|
73
|
+
* @param linuxPath - the candidate Linux path.
|
|
74
|
+
* @returns the `X:\…` drive path, or `null` when the path is not a drvfs mount.
|
|
75
|
+
*/
|
|
76
|
+
function mntToWindowsPath(linuxPath) {
|
|
77
|
+
const match = /^\/mnt\/([a-zA-Z])(?:\/(.*))?$/.exec(linuxPath);
|
|
78
|
+
if (match === null) return null;
|
|
79
|
+
const rest = (match[2] ?? "").replace(/\//g, "\\");
|
|
80
|
+
return `${(match[1] ?? "").toUpperCase()}:\\${rest}`;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Canonical Windows drive path for store keys and cross-realm identity:
|
|
84
|
+
* separators unified to `\`, trailing separator stripped, and the WHOLE path
|
|
85
|
+
* lowercased — Windows paths compare case-insensitively, and the workspace
|
|
86
|
+
* registry may realpath a different casing than the caller spelled (8.3 or
|
|
87
|
+
* on-disk casing), so the store key must collide across casings.
|
|
88
|
+
* @param path - candidate Windows drive path.
|
|
89
|
+
* @returns the canonical form, or `null` when not drive-shaped.
|
|
90
|
+
*/
|
|
91
|
+
function canonicalWindowsPath(path) {
|
|
92
|
+
const match = /^([A-Za-z]):[\\/](.*)$/.exec(path);
|
|
93
|
+
if (match === null) return null;
|
|
94
|
+
const rest = (match[2] ?? "").replace(/[\\/]+/g, "\\").replace(/\\$/, "").toLowerCase();
|
|
95
|
+
return `${(match[1] ?? "").toLowerCase()}:\\${rest}`;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* True when a value is a Windows-shaped path (drive or UNC), which is how
|
|
99
|
+
* the shell executor decides the WSLENV `/p` translation flag: only Windows
|
|
100
|
+
* path values need translation when they cross into the Linux process.
|
|
101
|
+
* @param value - the environment value to classify.
|
|
102
|
+
* @returns whether the value looks like a Windows path.
|
|
103
|
+
*/
|
|
104
|
+
function isWindowsPathShaped(value) {
|
|
105
|
+
return /^[A-Za-z]:[\\/]/.test(value) || value.startsWith("\\\\");
|
|
106
|
+
}
|
|
107
|
+
/** Linux username shape for `wsl.exe -u`: starts with a letter or underscore, then letters/digits/`_`/`.`/`-` (max 64). */
|
|
108
|
+
const WSL_USERNAME_PATTERN = /^[A-Za-z_][A-Za-z0-9_.-]{0,63}$/;
|
|
109
|
+
/**
|
|
110
|
+
* Whether a value is a safe Linux username for `wsl.exe -u`. The check is
|
|
111
|
+
* strict on purpose: a value starting with `-` could be parsed as a wsl.exe
|
|
112
|
+
* option instead of a username.
|
|
113
|
+
* @param value - candidate username.
|
|
114
|
+
* @returns whether it matches the Linux username shape.
|
|
115
|
+
*/
|
|
116
|
+
function isValidWslUsername(value) {
|
|
117
|
+
return WSL_USERNAME_PATTERN.test(value);
|
|
118
|
+
}
|
|
119
|
+
//#endregion
|
|
120
|
+
//#region src/shared/wsl.ts
|
|
121
|
+
/**
|
|
122
|
+
* WSL discovery helpers (host side): enumerate installed distributions
|
|
123
|
+
* through `wsl.exe -l -q` and read the default distribution from the Lxss
|
|
124
|
+
* registry key. `wsl.exe` output is UTF-16LE on most builds, so decoding
|
|
125
|
+
* sniffs for NUL bytes before choosing an encoding.
|
|
126
|
+
* @module dsh-wsl-workspace/shared/wsl
|
|
127
|
+
*/
|
|
128
|
+
const execFileAsync = promisify(execFile);
|
|
129
|
+
/** Executable timeout for the short discovery calls. */
|
|
130
|
+
const DISCOVERY_TIMEOUT_MS = 1e4;
|
|
131
|
+
const LXSS_KEY = "HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Lxss";
|
|
132
|
+
/** Human text for an unknown rejection. */
|
|
133
|
+
function messageOf(value) {
|
|
134
|
+
return value instanceof Error ? value.message : String(value);
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Decode `wsl.exe -l -q` output. Newer builds emit UTF-8; most emit UTF-16LE
|
|
138
|
+
* with NUL bytes interleaved — the NUL probe picks the right one.
|
|
139
|
+
* @param buffer - the raw captured output.
|
|
140
|
+
* @returns the decoded text.
|
|
141
|
+
*/
|
|
142
|
+
function decodeWslOutput(buffer) {
|
|
143
|
+
return buffer.includes(0) ? buffer.toString("utf16le") : buffer.toString("utf8");
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* List installed WSL distributions in `wsl.exe` order.
|
|
147
|
+
* @param wslPath - the `wsl.exe` executable (absolute or PATH name).
|
|
148
|
+
* @returns distribution names, blank lines dropped.
|
|
149
|
+
*/
|
|
150
|
+
async function listDistros(wslPath = "wsl.exe") {
|
|
151
|
+
let stdout;
|
|
152
|
+
try {
|
|
153
|
+
stdout = (await execFileAsync(wslPath, ["-l", "-q"], {
|
|
154
|
+
encoding: "buffer",
|
|
155
|
+
timeout: DISCOVERY_TIMEOUT_MS
|
|
156
|
+
})).stdout;
|
|
157
|
+
} catch (error) {
|
|
158
|
+
throw new Error(`wsl-workspace: cannot list WSL distributions (${messageOf(error)}); is WSL installed?`);
|
|
159
|
+
}
|
|
160
|
+
return decodeWslOutput(stdout).split(/\r?\n/).map((line) => line.trim()).filter((line) => line.length > 0);
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Read the user's default distribution from the Lxss registry. Non-fatal:
|
|
164
|
+
* returns `undefined` when the value is absent or unreadable (the caller
|
|
165
|
+
* falls back to list order).
|
|
166
|
+
* @returns the default distribution name, or `undefined`.
|
|
167
|
+
*/
|
|
168
|
+
async function defaultDistro() {
|
|
169
|
+
try {
|
|
170
|
+
const value = await execFileAsync("reg.exe", [
|
|
171
|
+
"query",
|
|
172
|
+
LXSS_KEY,
|
|
173
|
+
"/v",
|
|
174
|
+
"DefaultDistribution"
|
|
175
|
+
], { timeout: DISCOVERY_TIMEOUT_MS });
|
|
176
|
+
const guid = /DefaultDistribution\s+REG_SZ\s+(\{[0-9a-fA-F-]+\})/i.exec(value.stdout)?.[1];
|
|
177
|
+
if (guid === void 0) return void 0;
|
|
178
|
+
const name = await execFileAsync("reg.exe", [
|
|
179
|
+
"query",
|
|
180
|
+
`${LXSS_KEY}\\${guid}`,
|
|
181
|
+
"/v",
|
|
182
|
+
"DistributionName"
|
|
183
|
+
], { timeout: DISCOVERY_TIMEOUT_MS });
|
|
184
|
+
const distro = /DistributionName\s+REG_SZ\s+(.+)/i.exec(name.stdout)?.[1]?.trim();
|
|
185
|
+
return distro === void 0 || distro === "" ? void 0 : distro;
|
|
186
|
+
} catch {
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
/** Module-level cache for {@link defaultDistroSync} (one registry read per process). */
|
|
191
|
+
let syncDefaultResolved = false;
|
|
192
|
+
let syncDefault;
|
|
193
|
+
/**
|
|
194
|
+
* Synchronous variant of {@link defaultDistro} for executors that must
|
|
195
|
+
* resolve a distribution inside a synchronous plan step. Cached after the
|
|
196
|
+
* first read; non-fatal (returns `undefined` when the registry is
|
|
197
|
+
* unreadable, letting the caller fail loud with its own message).
|
|
198
|
+
* @returns the default distribution name, or `undefined`.
|
|
199
|
+
*/
|
|
200
|
+
function defaultDistroSync() {
|
|
201
|
+
if (syncDefaultResolved) return syncDefault;
|
|
202
|
+
syncDefaultResolved = true;
|
|
203
|
+
try {
|
|
204
|
+
const value = execFileSync("reg.exe", [
|
|
205
|
+
"query",
|
|
206
|
+
LXSS_KEY,
|
|
207
|
+
"/v",
|
|
208
|
+
"DefaultDistribution"
|
|
209
|
+
], { timeout: DISCOVERY_TIMEOUT_MS });
|
|
210
|
+
const guid = /DefaultDistribution\s+REG_SZ\s+(\{[0-9a-fA-F-]+\})/i.exec(String(value))?.[1];
|
|
211
|
+
if (guid === void 0) return void 0;
|
|
212
|
+
const name = execFileSync("reg.exe", [
|
|
213
|
+
"query",
|
|
214
|
+
`${LXSS_KEY}\\${guid}`,
|
|
215
|
+
"/v",
|
|
216
|
+
"DistributionName"
|
|
217
|
+
], { timeout: DISCOVERY_TIMEOUT_MS });
|
|
218
|
+
const distro = /DistributionName\s+REG_SZ\s+(.+)/i.exec(String(name))?.[1]?.trim();
|
|
219
|
+
syncDefault = distro === void 0 || distro === "" ? void 0 : distro;
|
|
220
|
+
} catch {
|
|
221
|
+
syncDefault = void 0;
|
|
222
|
+
}
|
|
223
|
+
return syncDefault;
|
|
224
|
+
}
|
|
225
|
+
//#endregion
|
|
226
|
+
export { isAbsoluteLinuxPath as a, joinUnc as c, parseWslUnc as d, windowsToMntPath as f, canonicalWindowsPath as i, mntToWindowsPath as l, defaultDistroSync as n, isValidWslUsername as o, listDistros as r, isWindowsPathShaped as s, defaultDistro as t, normalizeLinuxPath as u };
|
|
227
|
+
|
|
228
228
|
//# sourceMappingURL=wsl-C5_mxGPM.js.map
|