dsh-wsl-workspace 0.3.2 → 0.4.0
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.md +12 -0
- package/TESTING.md +11 -5
- package/lib/client.js +1053 -1052
- package/lib/client.js.map +1 -1
- package/lib/fs.js +1 -1
- package/lib/index.js +116 -13
- package/lib/index.js.map +1 -1
- package/lib/shell.js +382 -382
- package/lib/{wsl-credentials-BI4v5TNZ.js → wsl-credentials-BwNmfX_y.js} +109 -109
- package/lib/{wsl-credentials-BI4v5TNZ.js.map → wsl-credentials-BwNmfX_y.js.map} +1 -1
- package/lib/{wsl-C5_mxGPM.js → wsl-qwxCKU5O.js} +229 -228
- package/lib/wsl-qwxCKU5O.js.map +1 -0
- package/package.json +13 -2
- package/src/host/wsl-skills.ts +134 -12
- package/src/shared/paths.ts +6 -3
- package/lib/wsl-C5_mxGPM.js.map +0 -1
|
@@ -1,228 +1,229 @@
|
|
|
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
|
|
20
|
-
|
|
21
|
-
return
|
|
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
|
-
* @param
|
|
49
|
-
* @
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
if (
|
|
54
|
-
|
|
55
|
-
const
|
|
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
|
-
const
|
|
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
|
-
}
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
let
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
*
|
|
196
|
-
*
|
|
197
|
-
*
|
|
198
|
-
*
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
syncDefaultResolved
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
"
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
"
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
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 parts = segments.filter((segment) => segment.length > 0);
|
|
20
|
+
const distro = parts[1] ?? "";
|
|
21
|
+
if (distro === "") return null;
|
|
22
|
+
return {
|
|
23
|
+
distro,
|
|
24
|
+
linuxPath: `/${parts.slice(2).join("/")}`
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Normalize a Linux absolute path for the Host: collapse repeated slashes and
|
|
29
|
+
* strip a trailing slash (root becomes `/`).
|
|
30
|
+
* @param path - absolute Linux path.
|
|
31
|
+
* @returns the normalized path.
|
|
32
|
+
*/
|
|
33
|
+
function normalizeLinuxPath(path) {
|
|
34
|
+
const collapsed = path.replace(/\/+/g, "/");
|
|
35
|
+
return collapsed === "/" ? "/" : collapsed.replace(/\/$/, "");
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Whether a path is an absolute, non-empty Linux path.
|
|
39
|
+
* @param path - candidate.
|
|
40
|
+
* @returns whether it starts with `/` and contains no NUL.
|
|
41
|
+
*/
|
|
42
|
+
function isAbsoluteLinuxPath(path) {
|
|
43
|
+
return path.startsWith("/") && !path.includes("\0");
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Join a distro and a Linux absolute path into the WSL2 UNC form used as the
|
|
47
|
+
* workspace identity (`\\wsl.localhost\<distro>\<linux>`, backslash segments).
|
|
48
|
+
* @param distro - distro name.
|
|
49
|
+
* @param linuxPath - absolute Linux path (leading `/`).
|
|
50
|
+
* @returns the UNC path.
|
|
51
|
+
*/
|
|
52
|
+
function joinUnc(distro, linuxPath) {
|
|
53
|
+
if (!isAbsoluteLinuxPath(linuxPath)) throw new Error(`wsl-workspace: cannot map a non-absolute Linux path "${linuxPath}" to UNC`);
|
|
54
|
+
if (distro === "" || distro === "." || distro === ".." || /[\\/]/.test(distro)) throw new Error(`wsl-workspace: invalid distribution name "${distro}"`);
|
|
55
|
+
const normalized = linuxPath.replace(/\/+/g, "/").replace(/\/$/, "");
|
|
56
|
+
const windowsSegments = (normalized.startsWith("/") ? normalized.slice(1) : normalized).replace(/\//g, "\\");
|
|
57
|
+
return `\\\\wsl.localhost\\${distro}${windowsSegments === "" ? "" : `\\${windowsSegments}`}`;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Translate a Windows drive path to the drvfs mount path WSL distributions
|
|
61
|
+
* conventionally expose it at (`C:\foo` → `/mnt/c/foo`). Only single-letter
|
|
62
|
+
* drives under `/mnt` are mapped; custom mount points are out of scope.
|
|
63
|
+
* @param path - the candidate Windows path.
|
|
64
|
+
* @returns the `/mnt/<drive>/…` path, or `null` for non-drive paths.
|
|
65
|
+
*/
|
|
66
|
+
function windowsToMntPath(path) {
|
|
67
|
+
const match = /^([A-Za-z]):[\\/](.*)$/.exec(path);
|
|
68
|
+
if (match === null) return null;
|
|
69
|
+
const rest = (match[2] ?? "").replace(/\\/g, "/").replace(/\/+/g, "/").replace(/\/$/, "");
|
|
70
|
+
return `/mnt/${(match[1] ?? "").toLowerCase()}${rest === "" ? "" : `/${rest}`}`;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Translate a `/mnt/<drive>/…` path back to its Windows drive path.
|
|
74
|
+
* @param linuxPath - the candidate Linux path.
|
|
75
|
+
* @returns the `X:\…` drive path, or `null` when the path is not a drvfs mount.
|
|
76
|
+
*/
|
|
77
|
+
function mntToWindowsPath(linuxPath) {
|
|
78
|
+
const match = /^\/mnt\/([a-zA-Z])(?:\/(.*))?$/.exec(linuxPath);
|
|
79
|
+
if (match === null) return null;
|
|
80
|
+
const rest = (match[2] ?? "").replace(/\//g, "\\");
|
|
81
|
+
return `${(match[1] ?? "").toUpperCase()}:\\${rest}`;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Canonical Windows drive path for store keys and cross-realm identity:
|
|
85
|
+
* separators unified to `\`, trailing separator stripped, and the WHOLE path
|
|
86
|
+
* lowercased — Windows paths compare case-insensitively, and the workspace
|
|
87
|
+
* registry may realpath a different casing than the caller spelled (8.3 or
|
|
88
|
+
* on-disk casing), so the store key must collide across casings.
|
|
89
|
+
* @param path - candidate Windows drive path.
|
|
90
|
+
* @returns the canonical form, or `null` when not drive-shaped.
|
|
91
|
+
*/
|
|
92
|
+
function canonicalWindowsPath(path) {
|
|
93
|
+
const match = /^([A-Za-z]):[\\/](.*)$/.exec(path);
|
|
94
|
+
if (match === null) return null;
|
|
95
|
+
const rest = (match[2] ?? "").replace(/[\\/]+/g, "\\").replace(/\\$/, "").toLowerCase();
|
|
96
|
+
return `${(match[1] ?? "").toLowerCase()}:\\${rest}`;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* True when a value is a Windows-shaped path (drive or UNC), which is how
|
|
100
|
+
* the shell executor decides the WSLENV `/p` translation flag: only Windows
|
|
101
|
+
* path values need translation when they cross into the Linux process.
|
|
102
|
+
* @param value - the environment value to classify.
|
|
103
|
+
* @returns whether the value looks like a Windows path.
|
|
104
|
+
*/
|
|
105
|
+
function isWindowsPathShaped(value) {
|
|
106
|
+
return /^[A-Za-z]:[\\/]/.test(value) || value.startsWith("\\\\");
|
|
107
|
+
}
|
|
108
|
+
/** Linux username shape for `wsl.exe -u`: starts with a letter or underscore, then letters/digits/`_`/`.`/`-` (max 64). */
|
|
109
|
+
const WSL_USERNAME_PATTERN = /^[A-Za-z_][A-Za-z0-9_.-]{0,63}$/;
|
|
110
|
+
/**
|
|
111
|
+
* Whether a value is a safe Linux username for `wsl.exe -u`. The check is
|
|
112
|
+
* strict on purpose: a value starting with `-` could be parsed as a wsl.exe
|
|
113
|
+
* option instead of a username.
|
|
114
|
+
* @param value - candidate username.
|
|
115
|
+
* @returns whether it matches the Linux username shape.
|
|
116
|
+
*/
|
|
117
|
+
function isValidWslUsername(value) {
|
|
118
|
+
return WSL_USERNAME_PATTERN.test(value);
|
|
119
|
+
}
|
|
120
|
+
//#endregion
|
|
121
|
+
//#region src/shared/wsl.ts
|
|
122
|
+
/**
|
|
123
|
+
* WSL discovery helpers (host side): enumerate installed distributions
|
|
124
|
+
* through `wsl.exe -l -q` and read the default distribution from the Lxss
|
|
125
|
+
* registry key. `wsl.exe` output is UTF-16LE on most builds, so decoding
|
|
126
|
+
* sniffs for NUL bytes before choosing an encoding.
|
|
127
|
+
* @module dsh-wsl-workspace/shared/wsl
|
|
128
|
+
*/
|
|
129
|
+
const execFileAsync = promisify(execFile);
|
|
130
|
+
/** Executable timeout for the short discovery calls. */
|
|
131
|
+
const DISCOVERY_TIMEOUT_MS = 1e4;
|
|
132
|
+
const LXSS_KEY = "HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Lxss";
|
|
133
|
+
/** Human text for an unknown rejection. */
|
|
134
|
+
function messageOf(value) {
|
|
135
|
+
return value instanceof Error ? value.message : String(value);
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Decode `wsl.exe -l -q` output. Newer builds emit UTF-8; most emit UTF-16LE
|
|
139
|
+
* with NUL bytes interleaved — the NUL probe picks the right one.
|
|
140
|
+
* @param buffer - the raw captured output.
|
|
141
|
+
* @returns the decoded text.
|
|
142
|
+
*/
|
|
143
|
+
function decodeWslOutput(buffer) {
|
|
144
|
+
return buffer.includes(0) ? buffer.toString("utf16le") : buffer.toString("utf8");
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* List installed WSL distributions in `wsl.exe` order.
|
|
148
|
+
* @param wslPath - the `wsl.exe` executable (absolute or PATH name).
|
|
149
|
+
* @returns distribution names, blank lines dropped.
|
|
150
|
+
*/
|
|
151
|
+
async function listDistros(wslPath = "wsl.exe") {
|
|
152
|
+
let stdout;
|
|
153
|
+
try {
|
|
154
|
+
stdout = (await execFileAsync(wslPath, ["-l", "-q"], {
|
|
155
|
+
encoding: "buffer",
|
|
156
|
+
timeout: DISCOVERY_TIMEOUT_MS
|
|
157
|
+
})).stdout;
|
|
158
|
+
} catch (error) {
|
|
159
|
+
throw new Error(`wsl-workspace: cannot list WSL distributions (${messageOf(error)}); is WSL installed?`);
|
|
160
|
+
}
|
|
161
|
+
return decodeWslOutput(stdout).split(/\r?\n/).map((line) => line.trim()).filter((line) => line.length > 0);
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Read the user's default distribution from the Lxss registry. Non-fatal:
|
|
165
|
+
* returns `undefined` when the value is absent or unreadable (the caller
|
|
166
|
+
* falls back to list order).
|
|
167
|
+
* @returns the default distribution name, or `undefined`.
|
|
168
|
+
*/
|
|
169
|
+
async function defaultDistro() {
|
|
170
|
+
try {
|
|
171
|
+
const value = await execFileAsync("reg.exe", [
|
|
172
|
+
"query",
|
|
173
|
+
LXSS_KEY,
|
|
174
|
+
"/v",
|
|
175
|
+
"DefaultDistribution"
|
|
176
|
+
], { timeout: DISCOVERY_TIMEOUT_MS });
|
|
177
|
+
const guid = /DefaultDistribution\s+REG_SZ\s+(\{[0-9a-fA-F-]+\})/i.exec(value.stdout)?.[1];
|
|
178
|
+
if (guid === void 0) return void 0;
|
|
179
|
+
const name = await execFileAsync("reg.exe", [
|
|
180
|
+
"query",
|
|
181
|
+
`${LXSS_KEY}\\${guid}`,
|
|
182
|
+
"/v",
|
|
183
|
+
"DistributionName"
|
|
184
|
+
], { timeout: DISCOVERY_TIMEOUT_MS });
|
|
185
|
+
const distro = /DistributionName\s+REG_SZ\s+(.+)/i.exec(name.stdout)?.[1]?.trim();
|
|
186
|
+
return distro === void 0 || distro === "" ? void 0 : distro;
|
|
187
|
+
} catch {
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
/** Module-level cache for {@link defaultDistroSync} (one registry read per process). */
|
|
192
|
+
let syncDefaultResolved = false;
|
|
193
|
+
let syncDefault;
|
|
194
|
+
/**
|
|
195
|
+
* Synchronous variant of {@link defaultDistro} for executors that must
|
|
196
|
+
* resolve a distribution inside a synchronous plan step. Cached after the
|
|
197
|
+
* first read; non-fatal (returns `undefined` when the registry is
|
|
198
|
+
* unreadable, letting the caller fail loud with its own message).
|
|
199
|
+
* @returns the default distribution name, or `undefined`.
|
|
200
|
+
*/
|
|
201
|
+
function defaultDistroSync() {
|
|
202
|
+
if (syncDefaultResolved) return syncDefault;
|
|
203
|
+
syncDefaultResolved = true;
|
|
204
|
+
try {
|
|
205
|
+
const value = execFileSync("reg.exe", [
|
|
206
|
+
"query",
|
|
207
|
+
LXSS_KEY,
|
|
208
|
+
"/v",
|
|
209
|
+
"DefaultDistribution"
|
|
210
|
+
], { timeout: DISCOVERY_TIMEOUT_MS });
|
|
211
|
+
const guid = /DefaultDistribution\s+REG_SZ\s+(\{[0-9a-fA-F-]+\})/i.exec(String(value))?.[1];
|
|
212
|
+
if (guid === void 0) return void 0;
|
|
213
|
+
const name = execFileSync("reg.exe", [
|
|
214
|
+
"query",
|
|
215
|
+
`${LXSS_KEY}\\${guid}`,
|
|
216
|
+
"/v",
|
|
217
|
+
"DistributionName"
|
|
218
|
+
], { timeout: DISCOVERY_TIMEOUT_MS });
|
|
219
|
+
const distro = /DistributionName\s+REG_SZ\s+(.+)/i.exec(String(name))?.[1]?.trim();
|
|
220
|
+
syncDefault = distro === void 0 || distro === "" ? void 0 : distro;
|
|
221
|
+
} catch {
|
|
222
|
+
syncDefault = void 0;
|
|
223
|
+
}
|
|
224
|
+
return syncDefault;
|
|
225
|
+
}
|
|
226
|
+
//#endregion
|
|
227
|
+
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 };
|
|
228
|
+
|
|
229
|
+
//# sourceMappingURL=wsl-qwxCKU5O.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"wsl-qwxCKU5O.js","names":[],"sources":["../src/shared/paths.ts","../src/shared/wsl.ts"],"sourcesContent":["/**\r\n * WSL path helpers shared by the client and host halves. Pure and\r\n * dependency-free so both planes can import them without a runtime edge.\r\n */\r\n\r\n/** WSL2 default loopback bridge host: `\\\\wsl.localhost\\<distro>\\...`. */\r\nconst WSL_LOCALHOST_HOST = 'wsl.localhost'\r\n/** Legacy WSL interop host: `\\\\wsl$\\<distro>\\...`. */\r\nconst WSL_LEGACY_HOST = 'wsl$'\r\n\r\n/** The two UNC hosts WSL exposes a distribution's filesystem under. */\r\nconst UNC_HOSTS = [WSL_LOCALHOST_HOST, WSL_LEGACY_HOST]\r\n\r\n/** One WSL workspace coordinate parsed out of a UNC path. */\r\nexport interface WslUncTarget {\r\n /** Distro name (e.g. `Ubuntu`) as `wsl -l -q` reports it. */\r\n readonly distro: string\r\n /** Normalized absolute Linux path (leading `/`; no trailing slash except root). */\r\n readonly linuxPath: string\r\n}\r\n\r\n/**\r\n * Parse a WSL UNC path into its distro and Linux path. Accepts the WSL2\r\n * `\\\\wsl.localhost\\<distro>\\<linux>` form, the legacy `\\\\wsl$\\<distro>\\<linux>`\r\n * interop form, and forward-slash spellings of either.\r\n * @param raw - candidate absolute path.\r\n * @returns the parsed target, or null when the path is not a WSL UNC.\r\n */\r\nexport function parseWslUnc(raw: string): WslUncTarget | null {\r\n const normalized = raw.replace(/\\\\/g, '/').replace(/\\/\\/+/g, '//')\r\n if (!normalized.startsWith('//')) return null\r\n const segments = normalized.slice(2).split('/')\r\n const host = (segments[0] ?? '').toLowerCase()\r\n if (!UNC_HOSTS.includes(host)) return null\r\n // Empty segments (double separators anywhere after the host) are\r\n // collapsed: `\\\\wsl.localhost\\\\Ubuntu\\home` and trailing slashes must\r\n // parse the same as their single-separator spelling.\r\n const parts = segments.filter(segment => segment.length > 0)\r\n const distro = parts[1] ?? ''\r\n if (distro === '') return null\r\n return { distro, linuxPath: `/${parts.slice(2).join('/')}` }\r\n}\r\n\r\n/**\r\n * Whether a path resolves into a WSL distro through either UNC form.\r\n * @param raw - candidate absolute path.\r\n * @returns whether the path parses as a WSL UNC.\r\n */\r\nexport function isWslUnc(raw: string): boolean {\r\n return parseWslUnc(raw) !== null\r\n}\r\n\r\n/**\r\n * Translate a WSL UNC path to the absolute Linux path a process inside the\r\n * distribution can open. Throws on non-WSL input: callers rely on this\r\n * conversion to hand paths to the Linux world, so a silent pass-through\r\n * would hand a Windows path to bash.\r\n * @param uncPath - a path {@link parseWslUnc} accepts.\r\n * @returns the absolute Linux path.\r\n */\r\nexport function uncToLinux(uncPath: string): string {\r\n const parts = parseWslUnc(uncPath)\r\n if (parts === null) {\r\n throw new Error(`wsl-workspace: \"${uncPath}\" is not a WSL UNC path`)\r\n }\r\n return parts.linuxPath\r\n}\r\n\r\n/**\r\n * Normalize a Linux absolute path for the Host: collapse repeated slashes and\r\n * strip a trailing slash (root becomes `/`).\r\n * @param path - absolute Linux path.\r\n * @returns the normalized path.\r\n */\r\nexport function normalizeLinuxPath(path: string): string {\r\n const collapsed = path.replace(/\\/+/g, '/')\r\n return collapsed === '/' ? '/' : collapsed.replace(/\\/$/, '')\r\n}\r\n\r\n/**\r\n * Whether a path is an absolute, non-empty Linux path.\r\n * @param path - candidate.\r\n * @returns whether it starts with `/` and contains no NUL.\r\n */\r\nexport function isAbsoluteLinuxPath(path: string): boolean {\r\n return path.startsWith('/') && !path.includes('\\0')\r\n}\r\n\r\n/**\r\n * Join a distro and a Linux absolute path into the WSL2 UNC form used as the\r\n * workspace identity (`\\\\wsl.localhost\\<distro>\\<linux>`, backslash segments).\r\n * @param distro - distro name.\r\n * @param linuxPath - absolute Linux path (leading `/`).\r\n * @returns the UNC path.\r\n */\r\nexport function joinUnc(distro: string, linuxPath: string): string {\r\n if (!isAbsoluteLinuxPath(linuxPath)) {\r\n throw new Error(`wsl-workspace: cannot map a non-absolute Linux path \"${linuxPath}\" to UNC`)\r\n }\r\n // Defense in depth: a distribution name with separators or dot-dirs would\r\n // escape the `\\\\wsl.localhost\\` share structure (the host route validates\r\n // wire-supplied names too; every other caller passes through here).\r\n if (distro === '' || distro === '.' || distro === '..' || /[\\\\/]/.test(distro)) {\r\n throw new Error(`wsl-workspace: invalid distribution name \"${distro}\"`)\r\n }\r\n const normalized = linuxPath.replace(/\\/+/g, '/').replace(/\\/$/, '')\r\n const withoutLeading = normalized.startsWith('/') ? normalized.slice(1) : normalized\r\n const windowsSegments = withoutLeading.replace(/\\//g, '\\\\')\r\n const suffix = windowsSegments === '' ? '' : `\\\\${windowsSegments}`\r\n return `\\\\\\\\wsl.localhost\\\\${distro}${suffix}`\r\n}\r\n\r\n/**\r\n * Translate a Windows drive path to the drvfs mount path WSL distributions\r\n * conventionally expose it at (`C:\\foo` → `/mnt/c/foo`). Only single-letter\r\n * drives under `/mnt` are mapped; custom mount points are out of scope.\r\n * @param path - the candidate Windows path.\r\n * @returns the `/mnt/<drive>/…` path, or `null` for non-drive paths.\r\n */\r\nexport function windowsToMntPath(path: string): string | null {\r\n const match = /^([A-Za-z]):[\\\\/](.*)$/.exec(path)\r\n if (match === null) return null\r\n const rest = (match[2] ?? '').replace(/\\\\/g, '/').replace(/\\/+/g, '/').replace(/\\/$/, '')\r\n return `/mnt/${(match[1] ?? '').toLowerCase()}${rest === '' ? '' : `/${rest}`}`\r\n}\r\n\r\n/**\r\n * Translate a `/mnt/<drive>/…` path back to its Windows drive path.\r\n * @param linuxPath - the candidate Linux path.\r\n * @returns the `X:\\…` drive path, or `null` when the path is not a drvfs mount.\r\n */\r\nexport function mntToWindowsPath(linuxPath: string): string | null {\r\n const match = /^\\/mnt\\/([a-zA-Z])(?:\\/(.*))?$/.exec(linuxPath)\r\n if (match === null) return null\r\n const rest = (match[2] ?? '').replace(/\\//g, '\\\\')\r\n return `${(match[1] ?? '').toUpperCase()}:\\\\${rest}`\r\n}\r\n\r\n/**\r\n * Canonical Windows drive path for store keys and cross-realm identity:\r\n * separators unified to `\\`, trailing separator stripped, and the WHOLE path\r\n * lowercased — Windows paths compare case-insensitively, and the workspace\r\n * registry may realpath a different casing than the caller spelled (8.3 or\r\n * on-disk casing), so the store key must collide across casings.\r\n * @param path - candidate Windows drive path.\r\n * @returns the canonical form, or `null` when not drive-shaped.\r\n */\r\nexport function canonicalWindowsPath(path: string): string | null {\r\n const match = /^([A-Za-z]):[\\\\/](.*)$/.exec(path)\r\n if (match === null) return null\r\n const rest = (match[2] ?? '').replace(/[\\\\/]+/g, '\\\\').replace(/\\\\$/, '').toLowerCase()\r\n return `${(match[1] ?? '').toLowerCase()}:\\\\${rest}`\r\n}\r\n\r\n/**\r\n * True when a value is a Windows-shaped path (drive or UNC), which is how\r\n * the shell executor decides the WSLENV `/p` translation flag: only Windows\r\n * path values need translation when they cross into the Linux process.\r\n * @param value - the environment value to classify.\r\n * @returns whether the value looks like a Windows path.\r\n */\r\nexport function isWindowsPathShaped(value: string): boolean {\r\n return /^[A-Za-z]:[\\\\/]/.test(value) || value.startsWith('\\\\\\\\')\r\n}\r\n\r\n/** Linux username shape for `wsl.exe -u`: starts with a letter or underscore, then letters/digits/`_`/`.`/`-` (max 64). */\r\nconst WSL_USERNAME_PATTERN = /^[A-Za-z_][A-Za-z0-9_.-]{0,63}$/\r\n\r\n/**\r\n * Whether a value is a safe Linux username for `wsl.exe -u`. The check is\r\n * strict on purpose: a value starting with `-` could be parsed as a wsl.exe\r\n * option instead of a username.\r\n * @param value - candidate username.\r\n * @returns whether it matches the Linux username shape.\r\n */\r\nexport function isValidWslUsername(value: string): boolean {\r\n return WSL_USERNAME_PATTERN.test(value)\r\n}\r\n","/**\n * WSL discovery helpers (host side): enumerate installed distributions\n * through `wsl.exe -l -q` and read the default distribution from the Lxss\n * registry key. `wsl.exe` output is UTF-16LE on most builds, so decoding\n * sniffs for NUL bytes before choosing an encoding.\n * @module dsh-wsl-workspace/shared/wsl\n */\n\nimport { execFile, execFileSync } from 'node:child_process'\nimport { promisify } from 'node:util'\n\nconst execFileAsync = promisify(execFile)\n\n/** Executable timeout for the short discovery calls. */\nconst DISCOVERY_TIMEOUT_MS = 10_000\n\nconst LXSS_KEY = 'HKCU\\\\Software\\\\Microsoft\\\\Windows\\\\CurrentVersion\\\\Lxss'\n\n/** Human text for an unknown rejection. */\nfunction messageOf(value: unknown): string {\n return value instanceof Error ? value.message : String(value)\n}\n\n/**\n * Decode `wsl.exe -l -q` output. Newer builds emit UTF-8; most emit UTF-16LE\n * with NUL bytes interleaved — the NUL probe picks the right one.\n * @param buffer - the raw captured output.\n * @returns the decoded text.\n */\nexport function decodeWslOutput(buffer: Buffer): string {\n return buffer.includes(0) ? buffer.toString('utf16le') : buffer.toString('utf8')\n}\n\n/**\n * List installed WSL distributions in `wsl.exe` order.\n * @param wslPath - the `wsl.exe` executable (absolute or PATH name).\n * @returns distribution names, blank lines dropped.\n */\nexport async function listDistros(wslPath = 'wsl.exe'): Promise<string[]> {\n let stdout: Buffer\n try {\n const result = await execFileAsync(wslPath, ['-l', '-q'], { encoding: 'buffer', timeout: DISCOVERY_TIMEOUT_MS })\n stdout = result.stdout as Buffer\n } catch (error) {\n throw new Error(`wsl-workspace: cannot list WSL distributions (${messageOf(error)}); is WSL installed?`)\n }\n return decodeWslOutput(stdout)\n .split(/\\r?\\n/)\n .map(line => line.trim())\n .filter(line => line.length > 0)\n}\n\n/**\n * Read the user's default distribution from the Lxss registry. Non-fatal:\n * returns `undefined` when the value is absent or unreadable (the caller\n * falls back to list order).\n * @returns the default distribution name, or `undefined`.\n */\nexport async function defaultDistro(): Promise<string | undefined> {\n try {\n const value = await execFileAsync('reg.exe', ['query', LXSS_KEY, '/v', 'DefaultDistribution'], {\n timeout: DISCOVERY_TIMEOUT_MS,\n })\n const guid = /DefaultDistribution\\s+REG_SZ\\s+(\\{[0-9a-fA-F-]+\\})/i.exec(value.stdout)?.[1]\n if (guid === undefined) return undefined\n const name = await execFileAsync('reg.exe', ['query', `${LXSS_KEY}\\\\${guid}`, '/v', 'DistributionName'], {\n timeout: DISCOVERY_TIMEOUT_MS,\n })\n const distro = /DistributionName\\s+REG_SZ\\s+(.+)/i.exec(name.stdout)?.[1]?.trim()\n return distro === undefined || distro === '' ? undefined : distro\n } catch {\n return undefined\n }\n}\n\n/** Module-level cache for {@link defaultDistroSync} (one registry read per process). */\nlet syncDefaultResolved = false\nlet syncDefault: string | undefined\n\n/**\n * Synchronous variant of {@link defaultDistro} for executors that must\n * resolve a distribution inside a synchronous plan step. Cached after the\n * first read; non-fatal (returns `undefined` when the registry is\n * unreadable, letting the caller fail loud with its own message).\n * @returns the default distribution name, or `undefined`.\n */\nexport function defaultDistroSync(): string | undefined {\n if (syncDefaultResolved) return syncDefault\n syncDefaultResolved = true\n try {\n const value = execFileSync('reg.exe', ['query', LXSS_KEY, '/v', 'DefaultDistribution'], {\n timeout: DISCOVERY_TIMEOUT_MS,\n })\n const guid = /DefaultDistribution\\s+REG_SZ\\s+(\\{[0-9a-fA-F-]+\\})/i.exec(String(value))?.[1]\n if (guid === undefined) return undefined\n const name = execFileSync('reg.exe', ['query', `${LXSS_KEY}\\\\${guid}`, '/v', 'DistributionName'], {\n timeout: DISCOVERY_TIMEOUT_MS,\n })\n const distro = /DistributionName\\s+REG_SZ\\s+(.+)/i.exec(String(name))?.[1]?.trim()\n syncDefault = distro === undefined || distro === '' ? undefined : distro\n } catch {\n syncDefault = undefined\n }\n return syncDefault\n}\n"],"mappings":";;;;AAWA,MAAM,YAAY,CAAC,iBAAoB,MAAe;;;;;;;;AAiBtD,SAAgB,YAAY,KAAkC;CAC5D,MAAM,aAAa,IAAI,QAAQ,OAAO,GAAG,CAAC,CAAC,QAAQ,UAAU,IAAI;CACjE,IAAI,CAAC,WAAW,WAAW,IAAI,GAAG,OAAO;CACzC,MAAM,WAAW,WAAW,MAAM,CAAC,CAAC,CAAC,MAAM,GAAG;CAC9C,MAAM,QAAQ,SAAS,MAAM,GAAA,CAAI,YAAY;CAC7C,IAAI,CAAC,UAAU,SAAS,IAAI,GAAG,OAAO;CAItC,MAAM,QAAQ,SAAS,QAAO,YAAW,QAAQ,SAAS,CAAC;CAC3D,MAAM,SAAS,MAAM,MAAM;CAC3B,IAAI,WAAW,IAAI,OAAO;CAC1B,OAAO;EAAE;EAAQ,WAAW,IAAI,MAAM,MAAM,CAAC,CAAC,CAAC,KAAK,GAAG;CAAI;AAC7D;;;;;;;AAiCA,SAAgB,mBAAmB,MAAsB;CACvD,MAAM,YAAY,KAAK,QAAQ,QAAQ,GAAG;CAC1C,OAAO,cAAc,MAAM,MAAM,UAAU,QAAQ,OAAO,EAAE;AAC9D;;;;;;AAOA,SAAgB,oBAAoB,MAAuB;CACzD,OAAO,KAAK,WAAW,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OAAO,KAAA;EAC/B,MAAM,OAAO,MAAM,cAAc,WAAW;GAAC;GAAS,GAAG,SAAS,IAAI;GAAQ;GAAM;EAAkB,GAAG,EACvG,SAAS,qBACX,CAAC;EACD,MAAM,SAAS,oCAAoC,KAAK,KAAK,MAAM,CAAC,GAAG,EAAE,EAAE,KAAK;EAChF,OAAO,WAAW,KAAA,KAAa,WAAW,KAAK,KAAA,IAAY;CAC7D,QAAQ;EACN;CACF;AACF;;AAGA,IAAI,sBAAsB;AAC1B,IAAI;;;;;;;;AASJ,SAAgB,oBAAwC;CACtD,IAAI,qBAAqB,OAAO;CAChC,sBAAsB;CACtB,IAAI;EACF,MAAM,QAAQ,aAAa,WAAW;GAAC;GAAS;GAAU;GAAM;EAAqB,GAAG,EACtF,SAAS,qBACX,CAAC;EACD,MAAM,OAAO,sDAAsD,KAAK,OAAO,KAAK,CAAC,CAAC,GAAG;EACzF,IAAI,SAAS,KAAA,GAAW,OAAO,KAAA;EAC/B,MAAM,OAAO,aAAa,WAAW;GAAC;GAAS,GAAG,SAAS,IAAI;GAAQ;GAAM;EAAkB,GAAG,EAChG,SAAS,qBACX,CAAC;EACD,MAAM,SAAS,oCAAoC,KAAK,OAAO,IAAI,CAAC,CAAC,GAAG,EAAE,EAAE,KAAK;EACjF,cAAc,WAAW,KAAA,KAAa,WAAW,KAAK,KAAA,IAAY;CACpE,QAAQ;EACN,cAAc,KAAA;CAChB;CACA,OAAO;AACT"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-wsl-workspace",
|
|
3
3
|
"description": "WSL workspace support for DeepSeek Harness: add a WSL workspace from the web GUI and run the whole agent session (bash + file tools) inside the WSL distribution, VS Code Remote-WSL style. No toolchain install inside WSL required.",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.4.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
},
|
|
10
10
|
"main": "lib/index.js",
|
|
11
11
|
"scripts": {
|
|
12
|
-
"build": "tsdown && node scripts/verify-lib.mjs",
|
|
12
|
+
"build": "tsdown && node scripts/prune-lib.mjs && node scripts/verify-lib.mjs",
|
|
13
13
|
"verify": "node scripts/verify-lib.mjs",
|
|
14
14
|
"typecheck": "tsc -p tsconfig.json --noEmit"
|
|
15
15
|
},
|
|
@@ -31,9 +31,20 @@
|
|
|
31
31
|
"@deepseek-ai/dsh-client-locale",
|
|
32
32
|
"@deepseek-ai/dsh-client-ui-sidebar"
|
|
33
33
|
]
|
|
34
|
+
},
|
|
35
|
+
"compatibility": {
|
|
36
|
+
"dshReleases": {
|
|
37
|
+
"0.1.0-rc.7": "compatible",
|
|
38
|
+
"0.1.0-rc.8": "compatible",
|
|
39
|
+
"0.1.1-rc.1": "compatible",
|
|
40
|
+
"0.1.1-rc.2": "compatible"
|
|
41
|
+
}
|
|
34
42
|
}
|
|
35
43
|
},
|
|
36
44
|
"license": "MIT",
|
|
45
|
+
"engines": {
|
|
46
|
+
"node": ">=24"
|
|
47
|
+
},
|
|
37
48
|
"keywords": [
|
|
38
49
|
"deepseek-harness",
|
|
39
50
|
"dsh",
|