dsh-wsl-workspace 0.2.2 → 0.2.3

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/src/client/api.ts CHANGED
@@ -1,104 +1,124 @@
1
- /**
2
- * Thin fetch client for the Host plugin route. The browser calls
3
- * POST /wsl-workspace/api with a `{ method, params }` envelope and the Host
4
- * answers `{ ok: true, value }` or `{ ok: false, error }`.
5
- */
6
-
7
- /** Relative route the Host half registers (same-origin with the web server). */
8
- const ENDPOINT = '/wsl-workspace/api'
9
-
10
- /** One directory entry as the Host lists it. */
11
- export interface WslDirEntry {
12
- name: string
13
- kind: 'directory' | 'file' | 'other'
14
- }
15
-
16
- /** One directory level plus its breadcrumb ancestry. */
17
- export interface WslDirListing {
18
- /** The listed absolute Linux path. */
19
- path: string
20
- /** Parent Linux path, or null at the filesystem root. */
21
- parent: string | null
22
- /** The level's children (in name order; the client filters to directories). */
23
- entries: WslDirEntry[]
24
- }
25
-
26
- /** Existence/directory check result for one Linux path. */
27
- export interface WslPathCheck {
28
- exists: boolean
29
- isDirectory: boolean
30
- }
31
-
32
- /** Wire envelope the Host route answers with. */
33
- type Envelope<T> = { ok: true; value: T } | { ok: false; error: string }
34
-
35
- /** Human text for an unknown rejection, reusing the repository's idiom. */
36
- function errorMessage(value: unknown): string {
37
- return value instanceof Error ? value.message : String(value)
38
- }
39
-
40
- /**
41
- * Perform one POST call and unwrap the envelope.
42
- * @param method - the Host method name.
43
- * @param params - the method payload.
44
- * @returns the unwrapped value, or throws an Error on network or `ok:false`.
45
- */
46
- async function call<T>(method: string, params: Record<string, unknown> = {}): Promise<T> {
47
- let response: Response
48
- try {
49
- response = await fetch(ENDPOINT, {
50
- method: 'POST',
51
- headers: { 'content-type': 'application/json' },
52
- body: JSON.stringify({ method, params }),
53
- })
54
- } catch (error) {
55
- // The transport refused before answering (offline, origin mismatch, 404).
56
- throw new Error(`wsl-workspace request failed: ${errorMessage(error)}`)
57
- }
58
- let envelope: Envelope<T>
59
- try {
60
- envelope = (await response.json()) as Envelope<T>
61
- } catch {
62
- // A non-JSON body means a proxy/loader answered instead of the Host route.
63
- throw new Error(`wsl-workspace answered non-JSON (${response.status})`)
64
- }
65
- if (!envelope.ok) throw new Error(envelope.error)
66
- return envelope.value
67
- }
68
-
69
- /**
70
- * List the WSL distros installed on the host.
71
- * @returns distro names in registry order.
72
- */
73
- export async function listDistros(): Promise<string[]> {
74
- return call<string[]>('listDistros', {})
75
- }
76
-
77
- /**
78
- * List one directory level inside a distro.
79
- * @param distro - distro name.
80
- * @param path - absolute Linux directory to list.
81
- * @returns the level's listing with ancestry.
82
- */
83
- export async function listDir(distro: string, path: string): Promise<WslDirListing> {
84
- return call<WslDirListing>('listDir', { distro, path })
85
- }
86
-
87
- /**
88
- * Check whether a Linux path exists and is a directory.
89
- * @param distro - distro name.
90
- * @param path - absolute Linux path.
91
- * @returns existence and directory facts.
92
- */
93
- export async function check(distro: string, path: string): Promise<WslPathCheck> {
94
- return call<WslPathCheck>('check', { distro, path })
95
- }
96
-
97
- /**
98
- * Store (or clear, with an empty string) the username of one WSL workspace.
99
- * @param path - the workspace UNC path.
100
- * @param username - the Linux username; empty string clears the stored value.
101
- */
102
- export async function setWorkspaceUser(path: string, username: string): Promise<void> {
103
- return call<void>('setUser', { path, username })
104
- }
1
+ /**
2
+ * Thin fetch client for the Host plugin route. The browser calls
3
+ * POST /wsl-workspace/api with a `{ method, params }` envelope and the Host
4
+ * answers `{ ok: true, value }` or `{ ok: false, error }`.
5
+ */
6
+
7
+ /** Relative route the Host half registers (same-origin with the web server). */
8
+ const ENDPOINT = '/wsl-workspace/api'
9
+
10
+ /** One directory entry as the Host lists it. */
11
+ export interface WslDirEntry {
12
+ name: string
13
+ kind: 'directory' | 'file' | 'other'
14
+ }
15
+
16
+ /** One directory level plus its breadcrumb ancestry. */
17
+ export interface WslDirListing {
18
+ /** The listed absolute Linux path. */
19
+ path: string
20
+ /** Parent Linux path, or null at the filesystem root. */
21
+ parent: string | null
22
+ /** The level's children (in name order; the client filters to directories). */
23
+ entries: WslDirEntry[]
24
+ }
25
+
26
+ /** Existence/directory check result for one Linux path. */
27
+ export interface WslPathCheck {
28
+ exists: boolean
29
+ isDirectory: boolean
30
+ }
31
+
32
+ /** Wire envelope the Host route answers with. */
33
+ type Envelope<T> = { ok: true; value: T } | { ok: false; error: string }
34
+
35
+ /** Human text for an unknown rejection, reusing the repository's idiom. */
36
+ function errorMessage(value: unknown): string {
37
+ return value instanceof Error ? value.message : String(value)
38
+ }
39
+
40
+ /**
41
+ * Perform one POST call and unwrap the envelope.
42
+ * @param method - the Host method name.
43
+ * @param params - the method payload.
44
+ * @returns the unwrapped value, or throws an Error on network or `ok:false`.
45
+ */
46
+ async function call<T>(method: string, params: Record<string, unknown> = {}): Promise<T> {
47
+ let response: Response
48
+ try {
49
+ response = await fetch(ENDPOINT, {
50
+ method: 'POST',
51
+ headers: { 'content-type': 'application/json' },
52
+ body: JSON.stringify({ method, params }),
53
+ })
54
+ } catch (error) {
55
+ // The transport refused before answering (offline, origin mismatch, 404).
56
+ throw new Error(`wsl-workspace request failed: ${errorMessage(error)}`)
57
+ }
58
+ let envelope: Envelope<T>
59
+ try {
60
+ envelope = (await response.json()) as Envelope<T>
61
+ } catch {
62
+ // A non-JSON body means a proxy/loader answered instead of the Host route.
63
+ throw new Error(`wsl-workspace answered non-JSON (${response.status})`)
64
+ }
65
+ if (!envelope.ok) throw new Error(envelope.error)
66
+ return envelope.value
67
+ }
68
+
69
+ /**
70
+ * List the WSL distros installed on the host.
71
+ * @returns distro names in registry order.
72
+ */
73
+ export async function listDistros(): Promise<string[]> {
74
+ return call<string[]>('listDistros', {})
75
+ }
76
+
77
+ /**
78
+ * List one directory level inside a distro.
79
+ * @param distro - distro name.
80
+ * @param path - absolute Linux directory to list.
81
+ * @returns the level's listing with ancestry.
82
+ */
83
+ export async function listDir(distro: string, path: string): Promise<WslDirListing> {
84
+ return call<WslDirListing>('listDir', { distro, path })
85
+ }
86
+
87
+ /**
88
+ * Check whether a Linux path exists and is a directory.
89
+ * @param distro - distro name.
90
+ * @param path - absolute Linux path.
91
+ * @returns existence and directory facts.
92
+ */
93
+ export async function check(distro: string, path: string): Promise<WslPathCheck> {
94
+ return call<WslPathCheck>('check', { distro, path })
95
+ }
96
+
97
+ /**
98
+ * Store (or clear, with an empty string) the username of one WSL workspace.
99
+ * @param path - the workspace UNC path.
100
+ * @param username - the Linux username; empty string clears the stored value.
101
+ */
102
+ export async function setWorkspaceUser(path: string, username: string): Promise<void> {
103
+ return call<void>('setUser', { path, username })
104
+ }
105
+
106
+ /**
107
+ * Register a `/mnt/<drive>` WSL workspace under its Windows drive path,
108
+ * recording the distro (and optional username) for the session env.
109
+ * @param linuxPath - the `/mnt/<drive>/…` Linux path.
110
+ * @param distro - the WSL distribution the workspace belongs to.
111
+ * @param username - optional Linux username.
112
+ */
113
+ export async function registerWindows(linuxPath: string, distro: string, username: string): Promise<void> {
114
+ return call<void>('registerWindows', { linuxPath, distro, username })
115
+ }
116
+
117
+ /**
118
+ * List every registered WSL workspace key (canonical UNC and Windows drive
119
+ * spellings). The client uses the drive keys to recognize `/mnt` workspaces
120
+ * across page reloads.
121
+ */
122
+ export async function listWorkspaces(): Promise<string[]> {
123
+ return call<string[]>('listWorkspaces', {})
124
+ }