dsh-extra-paths 0.2.1 → 0.3.1
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 +192 -28
- package/README.zh.md +165 -21
- package/lib/argv.js +96 -24
- package/lib/argv.js.map +1 -1
- package/lib/client.js +1175 -467
- package/lib/config.js +140 -44
- package/lib/config.js.map +1 -1
- package/lib/context.js +48 -12
- package/lib/context.js.map +1 -1
- package/lib/denial.js +132 -0
- package/lib/denial.js.map +1 -0
- package/lib/index.js +40 -5
- package/lib/index.js.map +1 -1
- package/lib/name-rule.js +217 -0
- package/lib/name-rule.js.map +1 -0
- package/lib/paths.js +155 -27
- package/lib/paths.js.map +1 -1
- package/lib/pattern.js +128 -62
- package/lib/pattern.js.map +1 -1
- package/lib/platform.js +325 -0
- package/lib/platform.js.map +1 -0
- package/lib/preview.js +47 -20
- package/lib/preview.js.map +1 -1
- package/lib/resolve.js +345 -64
- package/lib/resolve.js.map +1 -1
- package/lib/schema.js +19 -6
- package/lib/schema.js.map +1 -1
- package/lib/shell-dialect.js +92 -0
- package/lib/shell-dialect.js.map +1 -0
- package/lib/tool-guard.js +214 -0
- package/lib/tool-guard.js.map +1 -0
- package/lib/types/argv.d.ts +50 -12
- package/lib/types/config.d.ts +29 -13
- package/lib/types/context.d.ts +6 -1
- package/lib/types/denial.d.ts +88 -0
- package/lib/types/index.d.ts +8 -4
- package/lib/types/name-rule.d.ts +112 -0
- package/lib/types/paths.d.ts +55 -10
- package/lib/types/pattern.d.ts +43 -5
- package/lib/types/platform.d.ts +148 -0
- package/lib/types/preview.d.ts +39 -3
- package/lib/types/resolve.d.ts +129 -23
- package/lib/types/schema.d.ts +24 -8
- package/lib/types/shell-dialect.d.ts +60 -0
- package/lib/types/tool-guard.d.ts +103 -0
- package/lib/types/wrap-fs.d.ts +86 -4
- package/lib/types/wrap-sandbox.d.ts +26 -6
- package/lib/wrap-fs.js +323 -47
- package/lib/wrap-fs.js.map +1 -1
- package/lib/wrap-sandbox.js +149 -12
- package/lib/wrap-sandbox.js.map +1 -1
- package/package.json +1 -1
package/lib/pattern.js
CHANGED
|
@@ -13,10 +13,17 @@
|
|
|
13
13
|
* Matching only ever yields directories that EXIST, which is what makes "the path
|
|
14
14
|
* contains `.worktrees`" a structural condition rather than a spelling.
|
|
15
15
|
*
|
|
16
|
+
* A Windows spelling is parsed in its OWN dialect first (`C:\a\**`, `\\server\…`):
|
|
17
|
+
* separators are normalised for the grammar, and the literal anchor is translated
|
|
18
|
+
* through `expandConfiguredPath`, so the same configuration also works on the
|
|
19
|
+
* other platform. A spelling whose anchor cannot be expressed on this host is
|
|
20
|
+
* `foreign`: inactive with one warning, never a save failure.
|
|
21
|
+
*
|
|
16
22
|
* Safety envelope, enforced here and validated again at save time:
|
|
17
23
|
* - the literal prefix before the first wildcard is the anchor; it must be
|
|
18
|
-
* absolute after `~` expansion, must not
|
|
19
|
-
*
|
|
24
|
+
* absolute in its own dialect after `~` expansion, must not resolve to a
|
|
25
|
+
* volume root (including a translated WSL drive root), and must not CONTAIN
|
|
26
|
+
* the home directory — a pattern can never walk `/`, `/Users` or `~`;
|
|
20
27
|
* - the walk never descends into `node_modules` or `.git`;
|
|
21
28
|
* - every cap (depth, directories scanned, matches) makes the WHOLE pattern yield
|
|
22
29
|
* nothing (fail closed) instead of a partial set: a half-expanded grant would
|
|
@@ -24,10 +31,8 @@
|
|
|
24
31
|
* @module dsh-extra-paths/pattern
|
|
25
32
|
*/
|
|
26
33
|
import { readdirSync, statSync } from 'node:fs';
|
|
27
|
-
import {
|
|
28
|
-
import {
|
|
29
|
-
import { isFilesystemRoot } from './config.js';
|
|
30
|
-
import { canonicalizeRoot, expandPath, isUnder } from './paths.js';
|
|
34
|
+
import { isAbsoluteSpelling, normalizePatternSpelling } from './platform.js';
|
|
35
|
+
import { canonicalizeRoot, discoverPlatformFacts, expandConfiguredPath, isHostVolumeRootPath, isUnder, pathApiOf, } from './paths.js';
|
|
31
36
|
/** Caps that keep one pattern's expansion bounded. */
|
|
32
37
|
export const PATTERN_LIMITS = {
|
|
33
38
|
/** Directory entries examined in one expansion. */
|
|
@@ -46,20 +51,27 @@ export const PATTERN_LIMITS = {
|
|
|
46
51
|
const PRUNED = new Set(['node_modules', '.git']);
|
|
47
52
|
/** Whether a spelling is a pattern rather than a literal path. */
|
|
48
53
|
export function isPattern(raw) {
|
|
49
|
-
|
|
54
|
+
// The wildcard may sit after Windows separators, so detection runs on the
|
|
55
|
+
// normalised spelling — a `\` is a legal POSIX filename character and is left
|
|
56
|
+
// untouched there.
|
|
57
|
+
return normalizePatternSpelling(raw).includes('*');
|
|
50
58
|
}
|
|
51
59
|
/**
|
|
52
|
-
* Parse a pattern spelling.
|
|
53
|
-
* @param raw - the configured spelling.
|
|
60
|
+
* Parse a pattern spelling and keep the reason when it cannot be walked.
|
|
61
|
+
* @param raw - the configured spelling (POSIX or Windows).
|
|
54
62
|
* @param workspaceRoot - the base a relative anchor resolves against.
|
|
55
|
-
* @param
|
|
56
|
-
*
|
|
63
|
+
* @param facts - host facts (defaults to the discovered ones); the anchor is
|
|
64
|
+
* translated into the host's dialect with these.
|
|
65
|
+
* @returns the pattern, or a rejection that says whether it is merely foreign.
|
|
57
66
|
*/
|
|
58
|
-
export function
|
|
59
|
-
|
|
67
|
+
export function parsePatternOutcome(raw, workspaceRoot, facts = discoverPlatformFacts()) {
|
|
68
|
+
// The grammar is separator-agnostic: a Windows spelling keeps its dialect for
|
|
69
|
+
// translation, but '*' detection and segment splitting see `/`.
|
|
70
|
+
const spelling = normalizePatternSpelling(raw);
|
|
71
|
+
const segments = spelling.split('/').filter(segment => segment !== '');
|
|
60
72
|
const firstWildcard = segments.findIndex(segment => segment.includes('*'));
|
|
61
73
|
if (firstWildcard < 0)
|
|
62
|
-
return
|
|
74
|
+
return { ok: false };
|
|
63
75
|
// Trailing `..` run: applied AFTER matching, so it must not be part of the walk.
|
|
64
76
|
let end = segments.length;
|
|
65
77
|
let up = 0;
|
|
@@ -69,38 +81,78 @@ export function parsePattern(raw, workspaceRoot, home = homedir()) {
|
|
|
69
81
|
}
|
|
70
82
|
const walk = segments.slice(firstWildcard, end);
|
|
71
83
|
if (walk.length === 0)
|
|
72
|
-
return
|
|
73
|
-
// The anchor spelling keeps its leading
|
|
74
|
-
// filtered segments would turn `/a/b/**` into the RELATIVE `a/b` and
|
|
75
|
-
// the walk against the session workspace instead of `/a/b` — the defect
|
|
76
|
-
// guard was extracted from.
|
|
77
|
-
const
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
//
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
84
|
+
return { ok: false };
|
|
85
|
+
// The anchor spelling keeps its own leading `/`, `~` or drive prefix. Rebuilding
|
|
86
|
+
// it from the filtered segments would turn `/a/b/**` into the RELATIVE `a/b` and
|
|
87
|
+
// resolve the walk against the session workspace instead of `/a/b` — the defect
|
|
88
|
+
// this guard was extracted from.
|
|
89
|
+
const anchorPrefix = spelling.slice(0, spelling.indexOf('*'));
|
|
90
|
+
// Absoluteness is judged on the RAW prefix: `/\*\*` strips to an empty
|
|
91
|
+
// anchor and must stay an absolute (and rejected) root, not become a
|
|
92
|
+
// workspace-relative sweep.
|
|
93
|
+
const absoluteAnchor = isAbsoluteSpelling(anchorPrefix);
|
|
94
|
+
const anchorSpelling = anchorPrefix.replace(/\/+$/, '');
|
|
95
|
+
// A workspace-ROOT anchor (leading wildcard, no literal prefix) must not be a
|
|
96
|
+
// pure sweep: `**` / `**/*` alone would walk everything under the workspace.
|
|
97
|
+
// Patterns with a literal anchor keep the documented trailing-`**` meaning.
|
|
98
|
+
if (!absoluteAnchor && anchorSpelling === ''
|
|
99
|
+
&& !walk.some(segment => !segment.includes('*') && segment !== '..')) {
|
|
100
|
+
return { ok: false };
|
|
101
|
+
}
|
|
102
|
+
let anchor;
|
|
103
|
+
if (absoluteAnchor) {
|
|
104
|
+
// `/**`, `~/**` and `/srv/**` would walk the filesystem, a whole home
|
|
105
|
+
// directory, or a top-level directory: an ABSOLUTE anchor needs two literal
|
|
106
|
+
// segments.
|
|
107
|
+
if (firstWildcard < 2)
|
|
108
|
+
return { ok: false };
|
|
109
|
+
const expanded = expandConfiguredPath(anchorSpelling, workspaceRoot, facts);
|
|
110
|
+
if (!expanded.ok)
|
|
111
|
+
return { ok: false, foreign: expanded.foreign };
|
|
112
|
+
anchor = canonicalizeRoot(expanded.path, facts);
|
|
113
|
+
}
|
|
114
|
+
else {
|
|
115
|
+
// Workspace-relative anchor: an empty prefix (leading wildcard, `**/.git`)
|
|
116
|
+
// walks the workspace itself; a literal prefix walks its subdirectory. The
|
|
117
|
+
// safety checks below apply exactly as for an absolute anchor.
|
|
118
|
+
const expanded = expandConfiguredPath(anchorSpelling === '' ? '.' : anchorSpelling, workspaceRoot, facts);
|
|
119
|
+
if (!expanded.ok)
|
|
120
|
+
return { ok: false, foreign: expanded.foreign };
|
|
121
|
+
anchor = canonicalizeRoot(expanded.path, facts);
|
|
122
|
+
}
|
|
123
|
+
// The anchor is host-native now: the literal-root rejection must run AFTER
|
|
124
|
+
// translation, so `C:\` inside WSL (⇒ `/mnt/c`) is refused as the drive root it
|
|
125
|
+
// is instead of being walked as an ordinary directory.
|
|
126
|
+
if (isHostVolumeRootPath(anchor, facts))
|
|
127
|
+
return { ok: false };
|
|
128
|
+
const homeRoot = canonicalizeRoot(facts.home, facts);
|
|
88
129
|
// An anchor that IS or CONTAINS the home directory would walk far too much.
|
|
89
|
-
if (anchor === homeRoot || isUnder(homeRoot, anchor))
|
|
90
|
-
return
|
|
91
|
-
return { source: raw, anchor, segments: walk, up };
|
|
130
|
+
if (anchor === homeRoot || isUnder(homeRoot, anchor, facts))
|
|
131
|
+
return { ok: false };
|
|
132
|
+
return { ok: true, pattern: { source: raw, anchor, segments: walk, up, platform: facts } };
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Parse a pattern spelling.
|
|
136
|
+
* @param raw - the configured spelling.
|
|
137
|
+
* @param workspaceRoot - the base a relative anchor resolves against.
|
|
138
|
+
* @param facts - host facts (defaults to the discovered ones).
|
|
139
|
+
* @returns the parsed pattern, or undefined when it cannot be walked safely.
|
|
140
|
+
*/
|
|
141
|
+
export function parsePattern(raw, workspaceRoot, facts = discoverPlatformFacts()) {
|
|
142
|
+
const outcome = parsePatternOutcome(raw, workspaceRoot, facts);
|
|
143
|
+
return outcome.ok ? outcome.pattern : undefined;
|
|
92
144
|
}
|
|
93
145
|
/**
|
|
94
|
-
*
|
|
95
|
-
*
|
|
146
|
+
* List one directory's direct children, including hidden entries and the pruned
|
|
147
|
+
* names (`node_modules`, `.git`). Pruning is applied by the CALLER: the names
|
|
148
|
+
* themselves must stay matchable — a pattern has to be able to FIND a `.git`
|
|
149
|
+
* directory, it just must never descend inside one. A final LITERAL segment may
|
|
150
|
+
* match a file, so non-directory entries are reported too.
|
|
96
151
|
* @param directory - the directory to list.
|
|
97
|
-
* @param
|
|
98
|
-
*
|
|
99
|
-
* a checked-out tool tree, and skipping them is what keeps the walk bounded.
|
|
100
|
-
* The pattern's own literal segments are matched at their level regardless, so
|
|
101
|
-
* `.worktrees` itself is still found.
|
|
152
|
+
* @param api - the host's `node:path` arithmetic.
|
|
153
|
+
* @returns the child entries, sorted by name; empty when unreadable.
|
|
102
154
|
*/
|
|
103
|
-
function
|
|
155
|
+
function listChildren(directory, api) {
|
|
104
156
|
let names;
|
|
105
157
|
try {
|
|
106
158
|
names = readdirSync(directory);
|
|
@@ -109,21 +161,16 @@ function childDirectories(directory, visibleOnly = false) {
|
|
|
109
161
|
return [];
|
|
110
162
|
}
|
|
111
163
|
const children = [];
|
|
112
|
-
for (const name of names) {
|
|
113
|
-
|
|
114
|
-
continue;
|
|
115
|
-
if (visibleOnly && name.startsWith('.'))
|
|
116
|
-
continue;
|
|
117
|
-
const child = join(directory, name);
|
|
164
|
+
for (const name of names.sort()) {
|
|
165
|
+
const child = api.join(directory, name);
|
|
118
166
|
try {
|
|
119
|
-
|
|
120
|
-
children.push(child);
|
|
167
|
+
children.push({ name, path: child, isDirectory: statSync(child).isDirectory() });
|
|
121
168
|
}
|
|
122
169
|
catch {
|
|
123
170
|
// A vanishing entry is not an error: the next pass simply will not see it.
|
|
124
171
|
}
|
|
125
172
|
}
|
|
126
|
-
return children
|
|
173
|
+
return children;
|
|
127
174
|
}
|
|
128
175
|
/**
|
|
129
176
|
* Expand a pattern against the filesystem.
|
|
@@ -132,6 +179,7 @@ function childDirectories(directory, visibleOnly = false) {
|
|
|
132
179
|
* @returns the matches, or `{ capped: true }` when a cap was hit.
|
|
133
180
|
*/
|
|
134
181
|
export function expandPattern(pattern, limits = PATTERN_LIMITS) {
|
|
182
|
+
const api = pathApiOf(pattern.platform);
|
|
135
183
|
const matches = new Set();
|
|
136
184
|
let scanned = 0;
|
|
137
185
|
let capped = false;
|
|
@@ -171,20 +219,36 @@ export function expandPattern(pattern, limits = PATTERN_LIMITS) {
|
|
|
171
219
|
truncated = true;
|
|
172
220
|
return;
|
|
173
221
|
}
|
|
174
|
-
// …or one more level, keeping `**` active. Hidden directories
|
|
175
|
-
// here only: this is the branch that would
|
|
176
|
-
// source tree below the anchor.
|
|
177
|
-
for (const child of
|
|
178
|
-
|
|
222
|
+
// …or one more level, keeping `**` active. Hidden directories and the
|
|
223
|
+
// pruned names are skipped here only: this is the branch that would
|
|
224
|
+
// otherwise wander into every source tree below the anchor.
|
|
225
|
+
for (const child of listChildren(directory, api)) {
|
|
226
|
+
if (!child.isDirectory || child.name.startsWith('.') || PRUNED.has(child.name))
|
|
227
|
+
continue;
|
|
228
|
+
walk(child.path, index, depth + 1);
|
|
179
229
|
if (capped)
|
|
180
230
|
return;
|
|
181
231
|
}
|
|
182
232
|
return;
|
|
183
233
|
}
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
234
|
+
const isLast = index === pattern.segments.length - 1;
|
|
235
|
+
for (const child of listChildren(directory, api)) {
|
|
236
|
+
const matchesSegment = segment === '*' ? child.isDirectory : segment === child.name;
|
|
237
|
+
if (!matchesSegment)
|
|
238
|
+
continue;
|
|
239
|
+
if (isLast) {
|
|
240
|
+
// A final LITERAL segment may be a file (`**/.env`); a final `*` keeps
|
|
241
|
+
// the historical directories-only meaning.
|
|
242
|
+
if (matches.size >= limits.maxMatches) {
|
|
243
|
+
stop('matches');
|
|
244
|
+
return;
|
|
245
|
+
}
|
|
246
|
+
matches.add(child.path);
|
|
247
|
+
continue;
|
|
248
|
+
}
|
|
249
|
+
if (!child.isDirectory || PRUNED.has(child.name))
|
|
250
|
+
continue;
|
|
251
|
+
walk(child.path, index + 1, depth);
|
|
188
252
|
if (capped)
|
|
189
253
|
return;
|
|
190
254
|
}
|
|
@@ -197,10 +261,12 @@ export function expandPattern(pattern, limits = PATTERN_LIMITS) {
|
|
|
197
261
|
.map(match => {
|
|
198
262
|
let result = match;
|
|
199
263
|
for (let step = 0; step < up; step += 1)
|
|
200
|
-
result = dirname(result);
|
|
201
|
-
return canonicalizeRoot(result);
|
|
264
|
+
result = api.dirname(result);
|
|
265
|
+
return canonicalizeRoot(result, pattern.platform);
|
|
202
266
|
})
|
|
203
|
-
|
|
267
|
+
// A trailing `..` run can climb to a volume root; that is as unacceptable as
|
|
268
|
+
// configuring one directly (translated WSL drive roots included).
|
|
269
|
+
.filter(candidate => !isHostVolumeRootPath(candidate, pattern.platform));
|
|
204
270
|
return { matches: [...new Set(resolved)].sort(), capped: false, ...(truncated ? { truncated: true } : {}), scanned };
|
|
205
271
|
}
|
|
206
272
|
//# sourceMappingURL=pattern.js.map
|
package/lib/pattern.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pattern.js","sourceRoot":"","sources":["../src/pattern.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"pattern.js","sourceRoot":"","sources":["../src/pattern.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;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IAAI,CAAC,OAAO,CAAC,MAAM,EAAE,CAAC,EAAE,CAAC,CAAC,CAAA;IAC1B,IAAI,MAAM;QAAE,OAAO,EAAE,OAAO,EAAE,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,GAAG,CAAC,GAAG,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,OAAO,EAAE,CAAA;IAEhG,MAAM,EAAE,GAAG,OAAO,CAAC,EAAE,CAAA;IACrB,MAAM,QAAQ,GAAG,CAAC,GAAG,OAAO,CAAC;SAC1B,GAAG,CAAC,KAAK,CAAC,EAAE;QACX,IAAI,MAAM,GAAG,KAAK,CAAA;QAClB,KAAK,IAAI,IAAI,GAAG,CAAC,EAAE,IAAI,GAAG,EAAE,EAAE,IAAI,IAAI,CAAC;YAAE,MAAM,GAAG,GAAG,CAAC,OAAO,CAAC,MAAM,CAAC,CAAA;QACrE,OAAO,gBAAgB,CAAC,MAAM,EAAE,OAAO,CAAC,QAAQ,CAAC,CAAA;IACnD,CAAC,CAAC;QACF,6EAA6E;QAC7E,kEAAkE;SACjE,MAAM,CAAC,SAAS,CAAC,EAAE,CAAC,CAAC,oBAAoB,CAAC,SAAS,EAAE,OAAO,CAAC,QAAQ,CAAC,CAAC,CAAA;IAC1E,OAAO,EAAE,OAAO,EAAE,CAAC,GAAG,IAAI,GAAG,CAAC,QAAQ,CAAC,CAAC,CAAC,IAAI,EAAE,EAAE,MAAM,EAAE,KAAK,EAAE,GAAG,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,OAAO,EAAE,CAAA;AACtH,CAAC"}
|
package/lib/platform.js
ADDED
|
@@ -0,0 +1,325 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Path-dialect vocabulary, spelling classification and the Windows ⇄ WSL
|
|
3
|
+
* translation. Everything here is a PURE string function: no `node:fs`, no
|
|
4
|
+
* `node:path`, no `process` — the caller injects the host facts, which keeps the
|
|
5
|
+
* module importable from the browser client (it is bundled by tsdown) and makes
|
|
6
|
+
* the Windows logic unit-testable on a POSIX CI machine.
|
|
7
|
+
*
|
|
8
|
+
* Why this module exists:
|
|
9
|
+
* - A configuration file is meant to be the SAME on every machine (dotfiles /
|
|
10
|
+
* a repository). A path spelled `C:\Users\me\work` must be understood inside
|
|
11
|
+
* WSL as `/mnt/c/Users/me/work`, and `/mnt/c/Users/me/work` must be understood
|
|
12
|
+
* on a native Windows host. The host half does the translation — the browser
|
|
13
|
+
* client must never infer a platform (under WSL the browser is Windows Chrome
|
|
14
|
+
* while the host is Linux, and a remote web client is a different machine).
|
|
15
|
+
* - A spelling that cannot be expressed on the current host becomes `foreign`
|
|
16
|
+
* and the entry is inactive with a warning; it must NOT fail the save, or one
|
|
17
|
+
* shared configuration could not be used on two platforms.
|
|
18
|
+
*
|
|
19
|
+
* @module dsh-extra-paths/platform
|
|
20
|
+
*/
|
|
21
|
+
/** The default WSL drive mount root. */
|
|
22
|
+
export const DEFAULT_WSL_MOUNT_ROOT = '/mnt';
|
|
23
|
+
/** Extended-length prefix `\\?\` (and `\\?\UNC\`). */
|
|
24
|
+
const EXTENDED_PREFIX = /^\\\\\?\\/;
|
|
25
|
+
const EXTENDED_UNC_PREFIX = /^\\\\\?\\UNC\\/i;
|
|
26
|
+
/** `C:\` or `C:/` — a drive-rooted absolute path. */
|
|
27
|
+
const WIN_DRIVE_ABSOLUTE = /^[A-Za-z]:[\\/]/;
|
|
28
|
+
/** `C:foo` — drive-RELATIVE (resolved against that drive's current directory). */
|
|
29
|
+
const WIN_DRIVE_RELATIVE = /^[A-Za-z]:(?![\\/])/;
|
|
30
|
+
/** `\\server\share\x` — a UNC path (two leading backslashes, a server name). */
|
|
31
|
+
const WIN_UNC = /^\\\\[^\\]/;
|
|
32
|
+
/** `//server/share/x` — UNC spelled with forward separators. */
|
|
33
|
+
const WIN_UNC_FORWARD = /^\/\/[^/]+\/[^/]+/;
|
|
34
|
+
/** A single leading separator that is neither `\\` nor `/`: `\foo` (current drive). */
|
|
35
|
+
const WIN_CURRENT_DRIVE = /^\\(?!\\)/;
|
|
36
|
+
/** Strip a Windows extended-length prefix, which only adds noise to comparison. */
|
|
37
|
+
export function stripWindowsPrefix(spelling) {
|
|
38
|
+
if (EXTENDED_UNC_PREFIX.test(spelling))
|
|
39
|
+
return `\\\\${spelling.slice('\\\\?\\UNC\\'.length)}`;
|
|
40
|
+
if (EXTENDED_PREFIX.test(spelling))
|
|
41
|
+
return spelling.slice('\\\\?\\'.length);
|
|
42
|
+
return spelling;
|
|
43
|
+
}
|
|
44
|
+
/** The logical host dialect. */
|
|
45
|
+
export function hostDialect(facts) {
|
|
46
|
+
return facts.platform === 'win32' ? 'windows' : 'posix';
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Classify one spelling without consulting the host.
|
|
50
|
+
* @param spelling - the raw configured spelling (trimmed by the caller).
|
|
51
|
+
* @returns what kind of path it is.
|
|
52
|
+
*/
|
|
53
|
+
export function classifySpelling(spelling) {
|
|
54
|
+
if (spelling === '~')
|
|
55
|
+
return 'home';
|
|
56
|
+
if (spelling.startsWith('~/'))
|
|
57
|
+
return 'home';
|
|
58
|
+
if (spelling.startsWith('~\\'))
|
|
59
|
+
return 'windows-home';
|
|
60
|
+
const stripped = stripWindowsPrefix(spelling);
|
|
61
|
+
if (WIN_DRIVE_ABSOLUTE.test(stripped) || WIN_UNC.test(stripped))
|
|
62
|
+
return 'windows-absolute';
|
|
63
|
+
if (WIN_UNC_FORWARD.test(stripped))
|
|
64
|
+
return 'windows-absolute';
|
|
65
|
+
if (WIN_DRIVE_RELATIVE.test(stripped))
|
|
66
|
+
return 'windows-drive-relative';
|
|
67
|
+
if (spelling.startsWith('/'))
|
|
68
|
+
return 'posix-absolute';
|
|
69
|
+
if (WIN_CURRENT_DRIVE.test(stripped))
|
|
70
|
+
return 'windows-current-drive';
|
|
71
|
+
return 'relative';
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Whether a spelling is a BARE NAME (`.git`, `.env`, `node_modules`): one path
|
|
75
|
+
* component, no separator, no wildcard, not the `.`/`..` relative specials and
|
|
76
|
+
* not a `~` spelling. A bare name is a NAME RULE — it matches that component at
|
|
77
|
+
* any depth under the rule's scope roots rather than naming one literal path.
|
|
78
|
+
*
|
|
79
|
+
* The `./name` spelling is the escape hatch for a literal single-component
|
|
80
|
+
* relative path (it contains a separator, so it stays a normal relative path).
|
|
81
|
+
* @param spelling - the raw configured spelling.
|
|
82
|
+
* @returns true when the spelling is a name rule.
|
|
83
|
+
*/
|
|
84
|
+
export function isBareNameSpelling(spelling) {
|
|
85
|
+
if (spelling === '' || spelling === '.' || spelling === '..')
|
|
86
|
+
return false;
|
|
87
|
+
if (spelling.startsWith('~'))
|
|
88
|
+
return false;
|
|
89
|
+
if (spelling.includes('*'))
|
|
90
|
+
return false;
|
|
91
|
+
// `C:foo` is a drive-relative PATH, not a component name: the colon spelling
|
|
92
|
+
// must keep flowing into the path validator that rejects it deliberately.
|
|
93
|
+
if (/^[A-Za-z]:/.test(spelling))
|
|
94
|
+
return false;
|
|
95
|
+
return !spelling.includes('/') && !spelling.includes('\\');
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Whether a spelling is absolute in its own dialect (a `~` spelling counts:
|
|
99
|
+
* the caller expands home first).
|
|
100
|
+
* @param spelling - the raw configured spelling.
|
|
101
|
+
* @returns true for home, POSIX-absolute and Windows-absolute spellings.
|
|
102
|
+
*/
|
|
103
|
+
export function isAbsoluteSpelling(spelling) {
|
|
104
|
+
const kind = classifySpelling(spelling);
|
|
105
|
+
return kind === 'home' || kind === 'windows-home' || kind === 'posix-absolute' || kind === 'windows-absolute';
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Normalize a spelling for the pattern grammar: Windows spellings use `/` as the
|
|
109
|
+
* separator for parsing, and the extended-length prefix is dropped first.
|
|
110
|
+
* POSIX spellings are returned verbatim: on POSIX a backslash is a legal
|
|
111
|
+
* FILENAME character and must never be turned into a separator.
|
|
112
|
+
* @param spelling - the raw configured spelling.
|
|
113
|
+
* @returns a spelling whose separators are `/` when it is a Windows path.
|
|
114
|
+
*/
|
|
115
|
+
export function normalizePatternSpelling(spelling) {
|
|
116
|
+
const stripped = stripWindowsPrefix(spelling);
|
|
117
|
+
if (WIN_DRIVE_ABSOLUTE.test(stripped.slice(0, 3))
|
|
118
|
+
|| WIN_UNC.test(stripped)
|
|
119
|
+
|| WIN_UNC_FORWARD.test(stripped)
|
|
120
|
+
|| spelling.startsWith('~\\')) {
|
|
121
|
+
return stripped.replaceAll('\\', '/');
|
|
122
|
+
}
|
|
123
|
+
return spelling;
|
|
124
|
+
}
|
|
125
|
+
/** Collapse `.`, `..` and duplicate separators in an absolute POSIX spelling. */
|
|
126
|
+
export function collapsePosix(path) {
|
|
127
|
+
const segments = [];
|
|
128
|
+
for (const segment of path.split('/')) {
|
|
129
|
+
if (segment === '' || segment === '.')
|
|
130
|
+
continue;
|
|
131
|
+
if (segment === '..') {
|
|
132
|
+
segments.pop();
|
|
133
|
+
continue;
|
|
134
|
+
}
|
|
135
|
+
segments.push(segment);
|
|
136
|
+
}
|
|
137
|
+
return `/${segments.join('/')}`;
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Collapse an absolute Windows spelling (drive or UNC) into its native form.
|
|
141
|
+
* The result uses `\` separators; case is preserved (Windows comparison folds
|
|
142
|
+
* case later, but the host's `realpath`/ACL layers still want the real spelling).
|
|
143
|
+
* @param path - an absolute Windows spelling using either separator.
|
|
144
|
+
* @returns the collapsed spelling, e.g. `C:\Users\me\x` or `\\server\share\x`.
|
|
145
|
+
*/
|
|
146
|
+
export function collapseWindows(path) {
|
|
147
|
+
const stripped = stripWindowsPrefix(path).replaceAll('/', '\\');
|
|
148
|
+
if (WIN_UNC.test(stripped)) {
|
|
149
|
+
// Keep the two leading backslashes, then collapse the share part normally.
|
|
150
|
+
const parts = stripped.slice(2).split('\\');
|
|
151
|
+
const server = parts.shift() ?? '';
|
|
152
|
+
const share = parts.shift() ?? '';
|
|
153
|
+
const rest = collapseWindowsSegments(parts);
|
|
154
|
+
return `\\\\${server}\\${share}${rest === '' ? '' : `\\${rest}`}`;
|
|
155
|
+
}
|
|
156
|
+
const drive = stripped.slice(0, 2);
|
|
157
|
+
const rest = collapseWindowsSegments(stripped.slice(2).split('\\'));
|
|
158
|
+
return `${drive}\\${rest}`;
|
|
159
|
+
}
|
|
160
|
+
/** Collapse one segment list, clamping `..` at the start. */
|
|
161
|
+
function collapseWindowsSegments(segments) {
|
|
162
|
+
const out = [];
|
|
163
|
+
for (const segment of segments) {
|
|
164
|
+
if (segment === '' || segment === '.')
|
|
165
|
+
continue;
|
|
166
|
+
if (segment === '..') {
|
|
167
|
+
out.pop();
|
|
168
|
+
continue;
|
|
169
|
+
}
|
|
170
|
+
out.push(segment);
|
|
171
|
+
}
|
|
172
|
+
return out.join('\\');
|
|
173
|
+
}
|
|
174
|
+
/** Join a home directory and a `~`-relative rest in the host's dialect. */
|
|
175
|
+
function homeJoin(home, rest, dialect) {
|
|
176
|
+
const separator = dialect === 'windows' ? '\\' : '/';
|
|
177
|
+
const trimmed = rest.replace(dialect === 'windows' ? /^[\\/]+/ : /^\/+/, '');
|
|
178
|
+
if (trimmed === '')
|
|
179
|
+
return dialect === 'windows' ? collapseWindows(home) : collapsePosix(home);
|
|
180
|
+
return dialect === 'windows' ? collapseWindows(`${home}\\${trimmed}`) : collapsePosix(`${home}/${trimmed}`);
|
|
181
|
+
}
|
|
182
|
+
/** Resolve a relative spelling against the workspace base in one dialect. */
|
|
183
|
+
function resolveRelative(base, spelling, dialect) {
|
|
184
|
+
if (dialect === 'windows')
|
|
185
|
+
return collapseWindows(`${base}\\${spelling}`);
|
|
186
|
+
return collapsePosix(`${base}/${spelling}`);
|
|
187
|
+
}
|
|
188
|
+
/** Build a `foreign` outcome: the spelling has no meaning on this host. */
|
|
189
|
+
function foreign(reason) {
|
|
190
|
+
return { ok: false, foreign: reason };
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Translate a Windows absolute spelling into its WSL spelling.
|
|
194
|
+
* @param spelling - a drive-rooted or UNC spelling (any separator).
|
|
195
|
+
* @param mountRoot - the WSL mount root, normally `/mnt`.
|
|
196
|
+
* @returns the POSIX spelling, or undefined for a share UNC path (no mapping).
|
|
197
|
+
*/
|
|
198
|
+
export function windowsToWsl(spelling, mountRoot = DEFAULT_WSL_MOUNT_ROOT) {
|
|
199
|
+
const stripped = stripWindowsPrefix(spelling).replaceAll('\\', '/');
|
|
200
|
+
const drive = /^([A-Za-z]):(?:\/|$)/.exec(stripped);
|
|
201
|
+
if (drive === null)
|
|
202
|
+
return undefined;
|
|
203
|
+
const letter = (drive[1] ?? '').toLowerCase();
|
|
204
|
+
const rest = stripped.slice(drive[0].length);
|
|
205
|
+
const root = mountRoot.replace(/\/+$/, '');
|
|
206
|
+
const tail = rest.replace(/^\/+/, '');
|
|
207
|
+
return tail === '' ? `${root}/${letter}` : `${root}/${letter}/${tail}`;
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Translate a POSIX spelling that names a Windows drive into its Windows form.
|
|
211
|
+
* Two conventions are recognized: WSL's `/mnt/<letter>/…` and the MSYS/git-bash
|
|
212
|
+
* `/c/…` form. The caller verifies the result by resolving it (existence is the
|
|
213
|
+
* only proof that the drive really exists on this host).
|
|
214
|
+
* @param spelling - an absolute POSIX spelling.
|
|
215
|
+
* @param mountRoot - the WSL drive mount root, normally `/mnt`.
|
|
216
|
+
* @returns the Windows spelling, or undefined when the spelling is not a drive.
|
|
217
|
+
*/
|
|
218
|
+
export function posixToWindows(spelling, mountRoot = DEFAULT_WSL_MOUNT_ROOT) {
|
|
219
|
+
const root = mountRoot.replace(/\/+$/, '');
|
|
220
|
+
const escape = (value) => value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
221
|
+
const mounted = new RegExp(`^${escape(root)}/([A-Za-z])(?:/(.*))?$`).exec(spelling);
|
|
222
|
+
if (mounted !== null)
|
|
223
|
+
return windowsFromParts(mounted[1] ?? '', mounted[2]);
|
|
224
|
+
// A bare `/c/...` is ambiguous, but it is the MSYS/git-bash convention and the
|
|
225
|
+
// caller's existence check rejects it on hosts without that drive.
|
|
226
|
+
const msys = /^\/([A-Za-z])(?:\/(.*))?$/.exec(spelling);
|
|
227
|
+
if (msys !== null)
|
|
228
|
+
return windowsFromParts(msys[1] ?? '', msys[2]);
|
|
229
|
+
return undefined;
|
|
230
|
+
}
|
|
231
|
+
/** Assemble a Windows drive path from a drive letter and a `/`-separated rest. */
|
|
232
|
+
function windowsFromParts(letter, rest) {
|
|
233
|
+
const drive = letter.toUpperCase();
|
|
234
|
+
return rest === undefined || rest === '' ? `${drive}:\\` : `${drive}:\\${rest.replaceAll('/', '\\')}`;
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* Whether a spelling names a filesystem/volume root, which would disable the
|
|
238
|
+
* sandbox outright. Covers POSIX `/`, a drive root (`C:`, `C:\`, `C:/`) and a
|
|
239
|
+
* UNC SHARE root (`\\server\share`), which is as broad as a drive for a share.
|
|
240
|
+
* @param spelling - a path spelling (either dialect).
|
|
241
|
+
* @returns true when the spelling is a volume root.
|
|
242
|
+
*/
|
|
243
|
+
export function isVolumeRootSpelling(spelling) {
|
|
244
|
+
const stripped = stripWindowsPrefix(spelling);
|
|
245
|
+
if (spelling === '/' || stripped === '\\')
|
|
246
|
+
return true;
|
|
247
|
+
if (/^[A-Za-z]:[\\/]?$/.test(stripped))
|
|
248
|
+
return true;
|
|
249
|
+
return /^[\\/]{2}[^\\/]+[\\/][^\\/]+[\\/]?$/.test(stripped);
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* Whether a canonical, host-native path names a volume root, including the WSL
|
|
253
|
+
* spelling of a Windows drive root (`/mnt/c`). The literal-root rejection runs
|
|
254
|
+
* on the CANONICAL path, so translation (`C:\` ⇒ `/mnt/c`) must be caught here.
|
|
255
|
+
* @param path - a host-native absolute path.
|
|
256
|
+
* @param facts - the host facts.
|
|
257
|
+
* @returns true for a volume root.
|
|
258
|
+
*/
|
|
259
|
+
export function isHostVolumeRoot(path, facts) {
|
|
260
|
+
if (isVolumeRootSpelling(path))
|
|
261
|
+
return true;
|
|
262
|
+
if (!facts.wsl)
|
|
263
|
+
return false;
|
|
264
|
+
const root = facts.wslMountRoot.replace(/\/+$/, '');
|
|
265
|
+
return new RegExp(`^${root.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}/[A-Za-z]/?$`).test(path);
|
|
266
|
+
}
|
|
267
|
+
/**
|
|
268
|
+
* Resolve one configured spelling to a HOST-NATIVE absolute path.
|
|
269
|
+
*
|
|
270
|
+
* Translation happens only when the spelling's dialect differs from the host's:
|
|
271
|
+
* a Windows spelling inside WSL becomes `/mnt/<drive>/…`, a `/mnt/<drive>/…`
|
|
272
|
+
* spelling on a native Windows host becomes `<Drive>:\…`. A spelling with no
|
|
273
|
+
* mapping on this host is `foreign`: the caller marks the entry inactive with a
|
|
274
|
+
* warning instead of failing the whole configuration.
|
|
275
|
+
* @param spelling - the configured spelling (already trimmed).
|
|
276
|
+
* @param workspaceRoot - the session workspace a relative spelling resolves against.
|
|
277
|
+
* @param facts - host platform facts.
|
|
278
|
+
* @returns the host-native absolute path, or the reason it is foreign.
|
|
279
|
+
*/
|
|
280
|
+
export function expandPath(spelling, workspaceRoot, facts) {
|
|
281
|
+
const dialect = hostDialect(facts);
|
|
282
|
+
const kind = classifySpelling(spelling);
|
|
283
|
+
switch (kind) {
|
|
284
|
+
case 'home':
|
|
285
|
+
return { ok: true, path: homeJoin(facts.home, spelling.slice(1), dialect), dialect, translated: false };
|
|
286
|
+
case 'windows-home':
|
|
287
|
+
if (dialect === 'windows')
|
|
288
|
+
return { ok: true, path: homeJoin(facts.home, spelling.slice(1), dialect), dialect, translated: false };
|
|
289
|
+
return foreign(`the Windows home spelling ${JSON.stringify(spelling)} is only understood on a Windows host`);
|
|
290
|
+
case 'posix-absolute':
|
|
291
|
+
if (dialect === 'posix')
|
|
292
|
+
return { ok: true, path: collapsePosix(spelling), dialect, translated: false };
|
|
293
|
+
{
|
|
294
|
+
const mapped = posixToWindows(spelling, facts.wslMountRoot);
|
|
295
|
+
if (mapped !== undefined)
|
|
296
|
+
return { ok: true, path: collapseWindows(mapped), dialect, translated: true };
|
|
297
|
+
return foreign(`the POSIX path ${JSON.stringify(spelling)} cannot be expressed on a Windows host`);
|
|
298
|
+
}
|
|
299
|
+
case 'windows-absolute':
|
|
300
|
+
if (dialect === 'windows')
|
|
301
|
+
return { ok: true, path: collapseWindows(spelling), dialect, translated: false };
|
|
302
|
+
if (facts.wsl) {
|
|
303
|
+
const mapped = windowsToWsl(spelling, facts.wslMountRoot);
|
|
304
|
+
if (mapped !== undefined)
|
|
305
|
+
return { ok: true, path: collapsePosix(mapped), dialect, translated: true };
|
|
306
|
+
return foreign(`the UNC path ${JSON.stringify(spelling)} has no reliable WSL mapping`);
|
|
307
|
+
}
|
|
308
|
+
return foreign(`the Windows path ${JSON.stringify(spelling)} is only understood on a Windows host or inside WSL`);
|
|
309
|
+
case 'windows-drive-relative':
|
|
310
|
+
return foreign(`the drive-relative spelling ${JSON.stringify(spelling)} is ambiguous and refused`);
|
|
311
|
+
case 'windows-current-drive':
|
|
312
|
+
if (dialect === 'windows')
|
|
313
|
+
return foreign(`the current-drive spelling ${JSON.stringify(spelling)} is ambiguous and refused`);
|
|
314
|
+
// On POSIX a leading backslash is an ordinary filename character.
|
|
315
|
+
return { ok: true, path: resolveRelative(workspaceRoot, spelling, dialect), dialect, translated: false };
|
|
316
|
+
case 'relative':
|
|
317
|
+
default:
|
|
318
|
+
return { ok: true, path: resolveRelative(workspaceRoot, spelling, dialect), dialect, translated: false };
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
/** Marker used in warnings for entries that only apply on another platform. */
|
|
322
|
+
export function foreignPathMessage(spelling, reason) {
|
|
323
|
+
return `extra-paths: ${JSON.stringify(spelling)} is not applicable on this host (${reason}); the entry stays inactive until the configuration is used on a matching host`;
|
|
324
|
+
}
|
|
325
|
+
//# sourceMappingURL=platform.js.map
|