dsh-wsl-workspace 0.2.2 → 0.3.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.de.md +49 -0
- package/README.es.md +49 -0
- package/README.fr.md +49 -0
- package/README.ja.md +49 -0
- package/README.ko.md +49 -0
- package/README.md +47 -47
- package/README.pt.md +49 -0
- package/README.ru.md +49 -0
- package/README.zh.md +46 -46
- package/lib/client.js +76 -5
- package/lib/client.js.map +1 -1
- package/lib/fs.js +188 -174
- package/lib/fs.js.map +1 -1
- package/lib/index.js +573 -541
- package/lib/index.js.map +1 -1
- package/lib/{paths-DBaSmi7x.js → paths-BDE1NVOv.js} +17 -2
- package/lib/paths-BDE1NVOv.js.map +1 -0
- package/lib/shell.js +382 -381
- package/lib/shell.js.map +1 -1
- package/lib/wsl-C5_mxGPM.js +228 -0
- package/lib/wsl-C5_mxGPM.js.map +1 -0
- package/lib/wsl-DdNPGaPo.js +1019 -0
- package/lib/wsl-DdNPGaPo.js.map +1 -0
- package/lib/wsl-credentials-BI4v5TNZ.js +109 -0
- package/lib/wsl-credentials-BI4v5TNZ.js.map +1 -0
- package/package.json +71 -61
- package/src/client/AddWslWorkspace.tsx +349 -346
- package/src/client/api.ts +124 -104
- package/src/client/index.ts +233 -194
- package/src/fs.ts +247 -228
- package/src/host/variants.ts +164 -199
- package/src/index.ts +93 -27
- package/src/shared/paths.ts +175 -159
- package/src/shared/wsl-credentials.ts +135 -85
- package/src/shell.ts +451 -441
- package/image-1.png +0 -0
- package/image.png +0 -0
- package/lib/paths-DBaSmi7x.js.map +0 -1
- package/lib/wsl-GjkUifnx.js +0 -179
- package/lib/wsl-GjkUifnx.js.map +0 -1
package/image-1.png
DELETED
|
Binary file
|
package/image.png
DELETED
|
Binary file
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"paths-DBaSmi7x.js","names":[],"sources":["../src/shared/paths.ts"],"sourcesContent":["/**\n * WSL path helpers shared by the client and host halves. Pure and\n * dependency-free so both planes can import them without a runtime edge.\n */\n\n/** WSL2 default loopback bridge host: `\\\\wsl.localhost\\<distro>\\...`. */\nconst WSL_LOCALHOST_HOST = 'wsl.localhost'\n/** Legacy WSL interop host: `\\\\wsl$\\<distro>\\...`. */\nconst WSL_LEGACY_HOST = 'wsl$'\n\n/** The two UNC hosts WSL exposes a distribution's filesystem under. */\nconst UNC_HOSTS = [WSL_LOCALHOST_HOST, WSL_LEGACY_HOST]\n\n/** One WSL workspace coordinate parsed out of a UNC path. */\nexport interface WslUncTarget {\n /** Distro name (e.g. `Ubuntu`) as `wsl -l -q` reports it. */\n readonly distro: string\n /** Normalized absolute Linux path (leading `/`; no trailing slash except root). */\n readonly linuxPath: string\n}\n\n/**\n * Parse a WSL UNC path into its distro and Linux path. Accepts the WSL2\n * `\\\\wsl.localhost\\<distro>\\<linux>` form, the legacy `\\\\wsl$\\<distro>\\<linux>`\n * interop form, and forward-slash spellings of either.\n * @param raw - candidate absolute path.\n * @returns the parsed target, or null when the path is not a WSL UNC.\n */\nexport function parseWslUnc(raw: string): WslUncTarget | null {\n const normalized = raw.replace(/\\\\/g, '/').replace(/\\/\\/+/g, '//')\n if (!normalized.startsWith('//')) return null\n const segments = normalized.slice(2).split('/')\n const host = (segments[0] ?? '').toLowerCase()\n if (!UNC_HOSTS.includes(host)) return null\n const distro = segments[1] ?? ''\n if (distro === '') return null\n const rest = segments.slice(2).filter(segment => segment.length > 0)\n return { distro, linuxPath: `/${rest.join('/')}` }\n}\n\n/**\n * Whether a path resolves into a WSL distro through either UNC form.\n * @param raw - candidate absolute path.\n * @returns whether the path parses as a WSL UNC.\n */\nexport function isWslUnc(raw: string): boolean {\n return parseWslUnc(raw) !== null\n}\n\n/**\n * Translate a WSL UNC path to the absolute Linux path a process inside the\n * distribution can open. Throws on non-WSL input: callers rely on this\n * conversion to hand paths to the Linux world, so a silent pass-through\n * would hand a Windows path to bash.\n * @param uncPath - a path {@link parseWslUnc} accepts.\n * @returns the absolute Linux path.\n */\nexport function uncToLinux(uncPath: string): string {\n const parts = parseWslUnc(uncPath)\n if (parts === null) {\n throw new Error(`wsl-workspace: \"${uncPath}\" is not a WSL UNC path`)\n }\n return parts.linuxPath\n}\n\n/**\n * Normalize a Linux absolute path for the Host: collapse repeated slashes and\n * strip a trailing slash (root becomes `/`).\n * @param path - absolute Linux path.\n * @returns the normalized path.\n */\nexport function normalizeLinuxPath(path: string): string {\n const collapsed = path.replace(/\\/+/g, '/')\n return collapsed === '/' ? '/' : collapsed.replace(/\\/$/, '')\n}\n\n/**\n * Whether a path is an absolute, non-empty Linux path.\n * @param path - candidate.\n * @returns whether it starts with `/` and contains no NUL.\n */\nexport function isAbsoluteLinuxPath(path: string): boolean {\n return path.startsWith('/') && !path.includes('\\0')\n}\n\n/**\n * Join a distro and a Linux absolute path into the WSL2 UNC form used as the\n * workspace identity (`\\\\wsl.localhost\\<distro>\\<linux>`, backslash segments).\n * @param distro - distro name.\n * @param linuxPath - absolute Linux path (leading `/`).\n * @returns the UNC path.\n */\nexport function joinUnc(distro: string, linuxPath: string): string {\n if (!isAbsoluteLinuxPath(linuxPath)) {\n throw new Error(`wsl-workspace: cannot map a non-absolute Linux path \"${linuxPath}\" to UNC`)\n }\n // Defense in depth: a distribution name with separators or dot-dirs would\n // escape the `\\\\wsl.localhost\\` share structure (the host route validates\n // wire-supplied names too; every other caller passes through here).\n if (distro === '' || distro === '.' || distro === '..' || /[\\\\/]/.test(distro)) {\n throw new Error(`wsl-workspace: invalid distribution name \"${distro}\"`)\n }\n const normalized = linuxPath.replace(/\\/+/g, '/').replace(/\\/$/, '')\n const withoutLeading = normalized.startsWith('/') ? normalized.slice(1) : normalized\n const windowsSegments = withoutLeading.replace(/\\//g, '\\\\')\n const suffix = windowsSegments === '' ? '' : `\\\\${windowsSegments}`\n return `\\\\\\\\wsl.localhost\\\\${distro}${suffix}`\n}\n\n/**\n * Translate a Windows drive path to the drvfs mount path WSL distributions\n * conventionally expose it at (`C:\\foo` → `/mnt/c/foo`). Only single-letter\n * drives under `/mnt` are mapped; custom mount points are out of scope.\n * @param path - the candidate Windows path.\n * @returns the `/mnt/<drive>/…` path, or `null` for non-drive paths.\n */\nexport function windowsToMntPath(path: string): string | null {\n const match = /^([A-Za-z]):[\\\\/](.*)$/.exec(path)\n if (match === null) return null\n const rest = (match[2] ?? '').replace(/\\\\/g, '/').replace(/\\/+/g, '/').replace(/\\/$/, '')\n return `/mnt/${(match[1] ?? '').toLowerCase()}${rest === '' ? '' : `/${rest}`}`\n}\n\n/**\n * Translate a `/mnt/<drive>/…` path back to its Windows drive path.\n * @param linuxPath - the candidate Linux path.\n * @returns the `X:\\…` drive path, or `null` when the path is not a drvfs mount.\n */\nexport function mntToWindowsPath(linuxPath: string): string | null {\n const match = /^\\/mnt\\/([a-zA-Z])(?:\\/(.*))?$/.exec(linuxPath)\n if (match === null) return null\n const rest = (match[2] ?? '').replace(/\\//g, '\\\\')\n return `${(match[1] ?? '').toUpperCase()}:\\\\${rest}`\n}\n\n/**\n * True when a value is a Windows-shaped path (drive or UNC), which is how\n * the shell executor decides the WSLENV `/p` translation flag: only Windows\n * path values need translation when they cross into the Linux process.\n * @param value - the environment value to classify.\n * @returns whether the value looks like a Windows path.\n */\nexport function isWindowsPathShaped(value: string): boolean {\n return /^[A-Za-z]:[\\\\/]/.test(value) || value.startsWith('\\\\\\\\')\n}\n\n/** Linux username shape for `wsl.exe -u`: starts with a letter or underscore, then letters/digits/`_`/`.`/`-` (max 64). */\nconst WSL_USERNAME_PATTERN = /^[A-Za-z_][A-Za-z0-9_.-]{0,63}$/\n\n/**\n * Whether a value is a safe Linux username for `wsl.exe -u`. The check is\n * strict on purpose: a value starting with `-` could be parsed as a wsl.exe\n * option instead of a username.\n * @param value - candidate username.\n * @returns whether it matches the Linux username shape.\n */\nexport function isValidWslUsername(value: string): boolean {\n return WSL_USERNAME_PATTERN.test(value)\n}\n"],"mappings":";;AAWA,MAAM,YAAY,CAAC,iBAAoB,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K;AAEpD;;;;;;;;AASA,SAAgB,iBAAiB,MAA6B;CAC5D,MAAM,QAAQ,yBAAyB,KAAK,IAAI;CAChD,IAAI,UAAU,MAAM,OAAO;CAC3B,MAAM,QAAQ,MAAM,MAAM,GAAA,CAAI,QAAQ,OAAO,GAAG,CAAC,CAAC,QAAQ,QAAQ,GAAG,CAAC,CAAC,QAAQ,OAAO,EAAE;CACxF,OAAO,SAAS,MAAM,MAAM,GAAA,CAAI,YAAY,IAAI,SAAS,KAAK,KAAK,IAAI;AACzE;;;;;;AAOA,SAAgB,iBAAiB,WAAkC;CACjE,MAAM,QAAQ,iCAAiC,KAAK,SAAS;CAC7D,IAAI,UAAU,MAAM,OAAO;CAC3B,MAAM,QAAQ,MAAM,MAAM,GAAA,CAAI,QAAQ,OAAO,IAAI;CACjD,OAAO,IAAI,MAAM,MAAM,GAAA,CAAI,YAAY,EAAE,KAAK;AAChD;;;;;;;;AASA,SAAgB,oBAAoB,OAAwB;CAC1D,OAAO,kBAAkB,KAAK,KAAK,KAAK,MAAM,WAAW,MAAM;AACjE;;AAGA,MAAM,uBAAuB;;;;;;;;AAS7B,SAAgB,mBAAmB,OAAwB;CACzD,OAAO,qBAAqB,KAAK,KAAK;AACxC"}
|
package/lib/wsl-GjkUifnx.js
DELETED
|
@@ -1,179 +0,0 @@
|
|
|
1
|
-
import { i as joinUnc, n as isValidWslUsername, s as parseWslUnc } from "./paths-DBaSmi7x.js";
|
|
2
|
-
import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
3
|
-
import { dirname, join } from "node:path";
|
|
4
|
-
import { homedir } from "node:os";
|
|
5
|
-
import { execFile, execFileSync } from "node:child_process";
|
|
6
|
-
import { promisify } from "node:util";
|
|
7
|
-
//#region src/shared/wsl-credentials.ts
|
|
8
|
-
/**
|
|
9
|
-
* Per-workspace WSL credentials (host side only). The dialog stores the
|
|
10
|
-
* optional Linux username of a WSL workspace under the harness home; the
|
|
11
|
-
* per-session env contributor and the WSL shell executor read it back so
|
|
12
|
-
* `wsl.exe -u <username>` can run commands as that user. Keys are canonical
|
|
13
|
-
* UNC workspace paths. This module touches node builtins, so the browser
|
|
14
|
-
* half never imports it.
|
|
15
|
-
* @module dsh-wsl-workspace/shared/wsl-credentials
|
|
16
|
-
*/
|
|
17
|
-
/** The store file lives under the harness home so both host halves share it. */
|
|
18
|
-
function storePath() {
|
|
19
|
-
return join(process.env.DSH_HOME ?? join(homedir(), ".dsh"), "wsl-workspaces.json");
|
|
20
|
-
}
|
|
21
|
-
/** Read the store; a missing or corrupt file reads as empty (never throws). */
|
|
22
|
-
function readStore() {
|
|
23
|
-
try {
|
|
24
|
-
const parsed = JSON.parse(readFileSync(storePath(), "utf8"));
|
|
25
|
-
if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed)) return {};
|
|
26
|
-
return parsed;
|
|
27
|
-
} catch {
|
|
28
|
-
return {};
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
/**
|
|
32
|
-
* Canonicalize any accepted WSL UNC spelling into the store's key form.
|
|
33
|
-
* @param path - candidate workspace path (either UNC host form).
|
|
34
|
-
* @returns the canonical UNC path, or null when the path is not a WSL UNC.
|
|
35
|
-
*/
|
|
36
|
-
function canonicalWslUnc(path) {
|
|
37
|
-
const parsed = parseWslUnc(path);
|
|
38
|
-
return parsed === null ? null : joinUnc(parsed.distro, parsed.linuxPath);
|
|
39
|
-
}
|
|
40
|
-
/**
|
|
41
|
-
* Read the stored username for a WSL workspace.
|
|
42
|
-
* @param uncPath - the workspace path (any accepted WSL UNC spelling).
|
|
43
|
-
* @returns the username, or undefined when none is stored.
|
|
44
|
-
*/
|
|
45
|
-
function getWorkspaceUsername(uncPath) {
|
|
46
|
-
const key = canonicalWslUnc(uncPath);
|
|
47
|
-
if (key === null) return void 0;
|
|
48
|
-
const username = readStore()[key]?.username;
|
|
49
|
-
return username === void 0 || username === "" ? void 0 : username;
|
|
50
|
-
}
|
|
51
|
-
/**
|
|
52
|
-
* Store (or clear) the username of a WSL workspace.
|
|
53
|
-
* @param uncPath - the workspace path (any accepted WSL UNC spelling).
|
|
54
|
-
* @param username - the username; empty or undefined clears the stored value.
|
|
55
|
-
*/
|
|
56
|
-
function setWorkspaceUsername(uncPath, username) {
|
|
57
|
-
const key = canonicalWslUnc(uncPath);
|
|
58
|
-
if (key === null) throw new Error("wsl-workspace: workspace path is not a WSL UNC path");
|
|
59
|
-
const store = readStore();
|
|
60
|
-
if (username === void 0 || username.trim() === "") delete store[key];
|
|
61
|
-
else {
|
|
62
|
-
const trimmed = username.trim();
|
|
63
|
-
if (!isValidWslUsername(trimmed)) throw new Error("wsl-workspace: username must match the Linux username pattern [A-Za-z_][A-Za-z0-9_.-]*");
|
|
64
|
-
store[key] = { username: trimmed };
|
|
65
|
-
}
|
|
66
|
-
const path = storePath();
|
|
67
|
-
mkdirSync(dirname(path), { recursive: true });
|
|
68
|
-
writeFileSync(path, JSON.stringify(store, null, 2) + "\n", "utf8");
|
|
69
|
-
}
|
|
70
|
-
//#endregion
|
|
71
|
-
//#region src/shared/wsl.ts
|
|
72
|
-
/**
|
|
73
|
-
* WSL discovery helpers (host side): enumerate installed distributions
|
|
74
|
-
* through `wsl.exe -l -q` and read the default distribution from the Lxss
|
|
75
|
-
* registry key. `wsl.exe` output is UTF-16LE on most builds, so decoding
|
|
76
|
-
* sniffs for NUL bytes before choosing an encoding.
|
|
77
|
-
* @module dsh-wsl-workspace/shared/wsl
|
|
78
|
-
*/
|
|
79
|
-
const execFileAsync = promisify(execFile);
|
|
80
|
-
/** Executable timeout for the short discovery calls. */
|
|
81
|
-
const DISCOVERY_TIMEOUT_MS = 1e4;
|
|
82
|
-
const LXSS_KEY = "HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Lxss";
|
|
83
|
-
/** Human text for an unknown rejection. */
|
|
84
|
-
function messageOf(value) {
|
|
85
|
-
return value instanceof Error ? value.message : String(value);
|
|
86
|
-
}
|
|
87
|
-
/**
|
|
88
|
-
* Decode `wsl.exe -l -q` output. Newer builds emit UTF-8; most emit UTF-16LE
|
|
89
|
-
* with NUL bytes interleaved — the NUL probe picks the right one.
|
|
90
|
-
* @param buffer - the raw captured output.
|
|
91
|
-
* @returns the decoded text.
|
|
92
|
-
*/
|
|
93
|
-
function decodeWslOutput(buffer) {
|
|
94
|
-
return buffer.includes(0) ? buffer.toString("utf16le") : buffer.toString("utf8");
|
|
95
|
-
}
|
|
96
|
-
/**
|
|
97
|
-
* List installed WSL distributions in `wsl.exe` order.
|
|
98
|
-
* @param wslPath - the `wsl.exe` executable (absolute or PATH name).
|
|
99
|
-
* @returns distribution names, blank lines dropped.
|
|
100
|
-
*/
|
|
101
|
-
async function listDistros(wslPath = "wsl.exe") {
|
|
102
|
-
let stdout;
|
|
103
|
-
try {
|
|
104
|
-
stdout = (await execFileAsync(wslPath, ["-l", "-q"], {
|
|
105
|
-
encoding: "buffer",
|
|
106
|
-
timeout: DISCOVERY_TIMEOUT_MS
|
|
107
|
-
})).stdout;
|
|
108
|
-
} catch (error) {
|
|
109
|
-
throw new Error(`wsl-workspace: cannot list WSL distributions (${messageOf(error)}); is WSL installed?`);
|
|
110
|
-
}
|
|
111
|
-
return decodeWslOutput(stdout).split(/\r?\n/).map((line) => line.trim()).filter((line) => line.length > 0);
|
|
112
|
-
}
|
|
113
|
-
/**
|
|
114
|
-
* Read the user's default distribution from the Lxss registry. Non-fatal:
|
|
115
|
-
* returns `undefined` when the value is absent or unreadable (the caller
|
|
116
|
-
* falls back to list order).
|
|
117
|
-
* @returns the default distribution name, or `undefined`.
|
|
118
|
-
*/
|
|
119
|
-
async function defaultDistro() {
|
|
120
|
-
try {
|
|
121
|
-
const value = await execFileAsync("reg.exe", [
|
|
122
|
-
"query",
|
|
123
|
-
LXSS_KEY,
|
|
124
|
-
"/v",
|
|
125
|
-
"DefaultDistribution"
|
|
126
|
-
], { timeout: DISCOVERY_TIMEOUT_MS });
|
|
127
|
-
const guid = /DefaultDistribution\s+REG_SZ\s+(\{[0-9a-fA-F-]+\})/i.exec(value.stdout)?.[1];
|
|
128
|
-
if (guid === void 0) return void 0;
|
|
129
|
-
const name = await execFileAsync("reg.exe", [
|
|
130
|
-
"query",
|
|
131
|
-
`${LXSS_KEY}\\${guid}`,
|
|
132
|
-
"/v",
|
|
133
|
-
"DistributionName"
|
|
134
|
-
], { timeout: DISCOVERY_TIMEOUT_MS });
|
|
135
|
-
const distro = /DistributionName\s+REG_SZ\s+(.+)/i.exec(name.stdout)?.[1]?.trim();
|
|
136
|
-
return distro === void 0 || distro === "" ? void 0 : distro;
|
|
137
|
-
} catch {
|
|
138
|
-
return;
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
/** Module-level cache for {@link defaultDistroSync} (one registry read per process). */
|
|
142
|
-
let syncDefaultResolved = false;
|
|
143
|
-
let syncDefault;
|
|
144
|
-
/**
|
|
145
|
-
* Synchronous variant of {@link defaultDistro} for executors that must
|
|
146
|
-
* resolve a distribution inside a synchronous plan step. Cached after the
|
|
147
|
-
* first read; non-fatal (returns `undefined` when the registry is
|
|
148
|
-
* unreadable, letting the caller fail loud with its own message).
|
|
149
|
-
* @returns the default distribution name, or `undefined`.
|
|
150
|
-
*/
|
|
151
|
-
function defaultDistroSync() {
|
|
152
|
-
if (syncDefaultResolved) return syncDefault;
|
|
153
|
-
syncDefaultResolved = true;
|
|
154
|
-
try {
|
|
155
|
-
const value = execFileSync("reg.exe", [
|
|
156
|
-
"query",
|
|
157
|
-
LXSS_KEY,
|
|
158
|
-
"/v",
|
|
159
|
-
"DefaultDistribution"
|
|
160
|
-
], { timeout: DISCOVERY_TIMEOUT_MS });
|
|
161
|
-
const guid = /DefaultDistribution\s+REG_SZ\s+(\{[0-9a-fA-F-]+\})/i.exec(String(value))?.[1];
|
|
162
|
-
if (guid === void 0) return void 0;
|
|
163
|
-
const name = execFileSync("reg.exe", [
|
|
164
|
-
"query",
|
|
165
|
-
`${LXSS_KEY}\\${guid}`,
|
|
166
|
-
"/v",
|
|
167
|
-
"DistributionName"
|
|
168
|
-
], { timeout: DISCOVERY_TIMEOUT_MS });
|
|
169
|
-
const distro = /DistributionName\s+REG_SZ\s+(.+)/i.exec(String(name))?.[1]?.trim();
|
|
170
|
-
syncDefault = distro === void 0 || distro === "" ? void 0 : distro;
|
|
171
|
-
} catch {
|
|
172
|
-
syncDefault = void 0;
|
|
173
|
-
}
|
|
174
|
-
return syncDefault;
|
|
175
|
-
}
|
|
176
|
-
//#endregion
|
|
177
|
-
export { getWorkspaceUsername as a, canonicalWslUnc as i, defaultDistroSync as n, setWorkspaceUsername as o, listDistros as r, defaultDistro as t };
|
|
178
|
-
|
|
179
|
-
//# sourceMappingURL=wsl-GjkUifnx.js.map
|
package/lib/wsl-GjkUifnx.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"wsl-GjkUifnx.js","names":[],"sources":["../src/shared/wsl-credentials.ts","../src/shared/wsl.ts"],"sourcesContent":["/**\n * Per-workspace WSL credentials (host side only). The dialog stores the\n * optional Linux username of a WSL workspace under the harness home; the\n * per-session env contributor and the WSL shell executor read it back so\n * `wsl.exe -u <username>` can run commands as that user. Keys are canonical\n * UNC workspace paths. This module touches node builtins, so the browser\n * half never imports it.\n * @module dsh-wsl-workspace/shared/wsl-credentials\n */\n\nimport { mkdirSync, readFileSync, writeFileSync } from 'node:fs'\nimport { homedir } from 'node:os'\nimport { dirname, join } from 'node:path'\nimport { isValidWslUsername, joinUnc, parseWslUnc } from './paths.ts'\n\n/** One workspace's stored credentials. */\ninterface WorkspaceEntry {\n /** The Linux user bash runs as inside the distribution (absent = distro default). */\n username?: string\n}\n\n/** The stored form: canonical UNC workspace path → credentials. */\ntype WorkspaceStore = Record<string, WorkspaceEntry>\n\n/** The store file lives under the harness home so both host halves share it. */\nfunction storePath(): string {\n const dshHome = process.env.DSH_HOME ?? join(homedir(), '.dsh')\n return join(dshHome, 'wsl-workspaces.json')\n}\n\n/** Read the store; a missing or corrupt file reads as empty (never throws). */\nfunction readStore(): WorkspaceStore {\n try {\n const parsed: unknown = JSON.parse(readFileSync(storePath(), 'utf8'))\n if (parsed === null || typeof parsed !== 'object' || Array.isArray(parsed)) return {}\n return parsed as WorkspaceStore\n } catch {\n return {}\n }\n}\n\n/**\n * Canonicalize any accepted WSL UNC spelling into the store's key form.\n * @param path - candidate workspace path (either UNC host form).\n * @returns the canonical UNC path, or null when the path is not a WSL UNC.\n */\nexport function canonicalWslUnc(path: string): string | null {\n const parsed = parseWslUnc(path)\n return parsed === null ? null : joinUnc(parsed.distro, parsed.linuxPath)\n}\n\n/**\n * Read the stored username for a WSL workspace.\n * @param uncPath - the workspace path (any accepted WSL UNC spelling).\n * @returns the username, or undefined when none is stored.\n */\nexport function getWorkspaceUsername(uncPath: string): string | undefined {\n const key = canonicalWslUnc(uncPath)\n if (key === null) return undefined\n const username = readStore()[key]?.username\n return username === undefined || username === '' ? undefined : username\n}\n\n/**\n * Store (or clear) the username of a WSL workspace.\n * @param uncPath - the workspace path (any accepted WSL UNC spelling).\n * @param username - the username; empty or undefined clears the stored value.\n */\nexport function setWorkspaceUsername(uncPath: string, username: string | undefined): void {\n const key = canonicalWslUnc(uncPath)\n if (key === null) throw new Error('wsl-workspace: workspace path is not a WSL UNC path')\n const store = readStore()\n if (username === undefined || username.trim() === '') {\n delete store[key]\n } else {\n const trimmed = username.trim()\n if (!isValidWslUsername(trimmed)) {\n throw new Error('wsl-workspace: username must match the Linux username pattern [A-Za-z_][A-Za-z0-9_.-]*')\n }\n store[key] = { username: trimmed }\n }\n const path = storePath()\n mkdirSync(dirname(path), { recursive: true })\n writeFileSync(path, JSON.stringify(store, null, 2) + '\\n', 'utf8')\n}\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":";;;;;;;;;;;;;;;;;AAyBA,SAAS,YAAoB;CAE3B,OAAO,KADS,QAAQ,IAAI,YAAY,KAAK,QAAQ,GAAG,MAAM,GACzC,qBAAqB;AAC5C;;AAGA,SAAS,YAA4B;CACnC,IAAI;EACF,MAAM,SAAkB,KAAK,MAAM,aAAa,UAAU,GAAG,MAAM,CAAC;EACpE,IAAI,WAAW,QAAQ,OAAO,WAAW,YAAY,MAAM,QAAQ,MAAM,GAAG,OAAO,CAAC;EACpF,OAAO;CACT,QAAQ;EACN,O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