shell-dsl 0.0.28 → 0.0.30
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 +23 -0
- package/dist/cjs/package.json +1 -1
- package/dist/cjs/src/fs/index.cjs +4 -1
- package/dist/cjs/src/fs/index.cjs.map +3 -3
- package/dist/cjs/src/fs/real-fs.cjs +30 -15
- package/dist/cjs/src/fs/real-fs.cjs.map +3 -3
- package/dist/cjs/src/fs/web-fs.cjs +295 -0
- package/dist/cjs/src/fs/web-fs.cjs.map +10 -0
- package/dist/cjs/src/index.cjs +3 -1
- package/dist/cjs/src/index.cjs.map +3 -3
- package/dist/cjs/src/shell-promise.cjs +2 -2
- package/dist/cjs/src/shell-promise.cjs.map +3 -3
- package/dist/mjs/package.json +1 -1
- package/dist/mjs/src/fs/index.mjs +4 -1
- package/dist/mjs/src/fs/index.mjs.map +3 -3
- package/dist/mjs/src/fs/real-fs.mjs +30 -15
- package/dist/mjs/src/fs/real-fs.mjs.map +3 -3
- package/dist/mjs/src/fs/web-fs.mjs +255 -0
- package/dist/mjs/src/fs/web-fs.mjs.map +10 -0
- package/dist/mjs/src/index.mjs +6 -2
- package/dist/mjs/src/index.mjs.map +3 -3
- package/dist/mjs/src/shell-promise.mjs +2 -2
- package/dist/mjs/src/shell-promise.mjs.map +3 -3
- package/dist/types/src/fs/index.d.ts +2 -1
- package/dist/types/src/fs/real-fs.d.ts +12 -0
- package/dist/types/src/fs/web-fs.d.ts +5 -0
- package/dist/types/src/index.d.ts +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -695,6 +695,29 @@ Omit the mount path for unrestricted access, but this is the same as just passin
|
|
|
695
695
|
const fs = new FileSystem(); // Full filesystem access same as fs from node:fs
|
|
696
696
|
```
|
|
697
697
|
|
|
698
|
+
### Web Filesystem
|
|
699
|
+
|
|
700
|
+
Use `WebFileSystem` when you already have a `FileSystemDirectoryHandle` in the browser, including an OPFS root from `navigator.storage.getDirectory()`:
|
|
701
|
+
|
|
702
|
+
```ts
|
|
703
|
+
import { WebFileSystem } from "shell-dsl";
|
|
704
|
+
|
|
705
|
+
const root = await navigator.storage.getDirectory();
|
|
706
|
+
const fs = new WebFileSystem(root, {
|
|
707
|
+
"secrets/**": "excluded",
|
|
708
|
+
"docs/**": "read-only",
|
|
709
|
+
});
|
|
710
|
+
```
|
|
711
|
+
|
|
712
|
+
For advanced use, you can inject the web adapter into `FileSystem` directly:
|
|
713
|
+
|
|
714
|
+
```ts
|
|
715
|
+
import { FileSystem, createWebUnderlyingFS } from "shell-dsl";
|
|
716
|
+
|
|
717
|
+
const root = await navigator.storage.getDirectory();
|
|
718
|
+
const fs = new FileSystem("/", {}, createWebUnderlyingFS(root));
|
|
719
|
+
```
|
|
720
|
+
|
|
698
721
|
|
|
699
722
|
## Low-Level API
|
|
700
723
|
|
package/dist/cjs/package.json
CHANGED
|
@@ -39,7 +39,9 @@ var __export = (target, all) => {
|
|
|
39
39
|
// src/fs/index.ts
|
|
40
40
|
var exports_fs = {};
|
|
41
41
|
__export(exports_fs, {
|
|
42
|
+
createWebUnderlyingFS: () => import_web_fs.createWebUnderlyingFS,
|
|
42
43
|
createVirtualFS: () => import_memfs_adapter.createVirtualFS,
|
|
44
|
+
WebFileSystem: () => import_web_fs.WebFileSystem,
|
|
43
45
|
ReadOnlyFileSystem: () => import_readonly_fs.ReadOnlyFileSystem,
|
|
44
46
|
FileSystem: () => import_real_fs.FileSystem
|
|
45
47
|
});
|
|
@@ -47,5 +49,6 @@ module.exports = __toCommonJS(exports_fs);
|
|
|
47
49
|
var import_memfs_adapter = require("./memfs-adapter.cjs");
|
|
48
50
|
var import_real_fs = require("./real-fs.cjs");
|
|
49
51
|
var import_readonly_fs = require("./readonly-fs.cjs");
|
|
52
|
+
var import_web_fs = require("./web-fs.cjs");
|
|
50
53
|
|
|
51
|
-
//# debugId=
|
|
54
|
+
//# debugId=C93B6DA797F0B48764756E2164756E21
|
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../../src/fs/index.ts"],
|
|
4
4
|
"sourcesContent": [
|
|
5
|
-
"export { createVirtualFS } from \"./memfs-adapter.cjs\";\nexport { FileSystem, type Permission, type PermissionRules, type UnderlyingFS } from \"./real-fs.cjs\";\nexport { ReadOnlyFileSystem } from \"./readonly-fs.cjs\";\n"
|
|
5
|
+
"export { createVirtualFS } from \"./memfs-adapter.cjs\";\nexport { FileSystem, type PathOps, type Permission, type PermissionRules, type UnderlyingFS } from \"./real-fs.cjs\";\nexport { ReadOnlyFileSystem } from \"./readonly-fs.cjs\";\nexport { WebFileSystem, createWebUnderlyingFS } from \"./web-fs.cjs\";\n"
|
|
6
6
|
],
|
|
7
|
-
"mappings": "
|
|
8
|
-
"debugId": "
|
|
7
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAgC,IAAhC;AACmG,IAAnG;AACmC,IAAnC;AACqD,IAArD;",
|
|
8
|
+
"debugId": "C93B6DA797F0B48764756E2164756E21",
|
|
9
9
|
"names": []
|
|
10
10
|
}
|
|
@@ -69,15 +69,28 @@ module.exports = __toCommonJS(exports_real_fs);
|
|
|
69
69
|
var path = __toESM(require("path"));
|
|
70
70
|
var nodeFs = __toESM(require("node:fs/promises"));
|
|
71
71
|
var defaultFS = { promises: nodeFs };
|
|
72
|
+
var nodePathOps = {
|
|
73
|
+
separator: path.sep,
|
|
74
|
+
resolve: (...paths) => path.resolve(...paths),
|
|
75
|
+
normalize: (filePath) => path.normalize(filePath),
|
|
76
|
+
join: (...paths) => path.join(...paths),
|
|
77
|
+
relative: (from, to) => path.relative(from, to),
|
|
78
|
+
isAbsolute: (filePath) => path.isAbsolute(filePath),
|
|
79
|
+
dirname: (filePath) => path.dirname(filePath),
|
|
80
|
+
basename: (filePath) => path.basename(filePath)
|
|
81
|
+
};
|
|
72
82
|
|
|
73
83
|
class FileSystem {
|
|
74
84
|
mountBase;
|
|
75
85
|
rules;
|
|
86
|
+
pathOps;
|
|
76
87
|
underlyingFs;
|
|
77
88
|
constructor(mountPath, permissions, fs) {
|
|
78
|
-
|
|
89
|
+
const underlyingFs = fs ?? defaultFS;
|
|
90
|
+
this.pathOps = underlyingFs.pathOps ?? nodePathOps;
|
|
91
|
+
this.mountBase = mountPath ? this.pathOps.resolve(mountPath) : null;
|
|
79
92
|
this.rules = this.compileRules(permissions ?? {});
|
|
80
|
-
this.underlyingFs =
|
|
93
|
+
this.underlyingFs = underlyingFs;
|
|
81
94
|
}
|
|
82
95
|
compileRules(permissions) {
|
|
83
96
|
return Object.entries(permissions).map(([pattern, permission]) => ({
|
|
@@ -127,7 +140,7 @@ class FileSystem {
|
|
|
127
140
|
}
|
|
128
141
|
resolveSafePath(virtualPath) {
|
|
129
142
|
if (this.mountBase === null) {
|
|
130
|
-
return
|
|
143
|
+
return this.pathOps.resolve(virtualPath);
|
|
131
144
|
}
|
|
132
145
|
const segments = virtualPath.split("/").filter(Boolean);
|
|
133
146
|
let depth = 0;
|
|
@@ -141,11 +154,13 @@ class FileSystem {
|
|
|
141
154
|
depth++;
|
|
142
155
|
}
|
|
143
156
|
}
|
|
144
|
-
const normalized =
|
|
157
|
+
const normalized = this.pathOps.normalize(virtualPath);
|
|
145
158
|
const relativePath = normalized.startsWith("/") ? normalized.slice(1) : normalized;
|
|
146
|
-
const realPath =
|
|
147
|
-
const resolved =
|
|
148
|
-
|
|
159
|
+
const realPath = this.pathOps.join(this.mountBase, relativePath);
|
|
160
|
+
const resolved = this.pathOps.resolve(realPath);
|
|
161
|
+
const relativeFromMount = this.pathOps.relative(this.mountBase, resolved);
|
|
162
|
+
const escapesMount = relativeFromMount === ".." || relativeFromMount.startsWith(`..${this.pathOps.separator}`) || this.pathOps.isAbsolute(relativeFromMount);
|
|
163
|
+
if (escapesMount) {
|
|
149
164
|
throw new Error(`Path traversal blocked: "${virtualPath}" escapes mount point`);
|
|
150
165
|
}
|
|
151
166
|
return resolved;
|
|
@@ -205,13 +220,13 @@ class FileSystem {
|
|
|
205
220
|
await this.underlyingFs.promises.rm(realPath, opts);
|
|
206
221
|
}
|
|
207
222
|
resolve(...paths) {
|
|
208
|
-
return
|
|
223
|
+
return this.pathOps.resolve("/", ...paths);
|
|
209
224
|
}
|
|
210
225
|
dirname(filePath) {
|
|
211
|
-
return
|
|
226
|
+
return this.pathOps.dirname(filePath);
|
|
212
227
|
}
|
|
213
228
|
basename(filePath) {
|
|
214
|
-
return
|
|
229
|
+
return this.pathOps.basename(filePath);
|
|
215
230
|
}
|
|
216
231
|
async glob(pattern, opts) {
|
|
217
232
|
const cwd = opts?.cwd ?? "/";
|
|
@@ -244,8 +259,8 @@ class FileSystem {
|
|
|
244
259
|
}
|
|
245
260
|
async matchPattern(pattern, cwd) {
|
|
246
261
|
const parts = pattern.split("/").filter((p) => p !== "");
|
|
247
|
-
const
|
|
248
|
-
const startDir =
|
|
262
|
+
const isAbsolute2 = pattern.startsWith("/");
|
|
263
|
+
const startDir = isAbsolute2 ? "/" : cwd;
|
|
249
264
|
return this.matchParts(parts, startDir);
|
|
250
265
|
}
|
|
251
266
|
async matchParts(parts, currentPath) {
|
|
@@ -261,7 +276,7 @@ class FileSystem {
|
|
|
261
276
|
const realPath = this.resolveSafePath(currentPath);
|
|
262
277
|
const entries = await this.underlyingFs.promises.readdir(realPath);
|
|
263
278
|
for (const entry of entries) {
|
|
264
|
-
const entryPath =
|
|
279
|
+
const entryPath = this.pathOps.join(currentPath, String(entry));
|
|
265
280
|
try {
|
|
266
281
|
const entryRealPath = this.resolveSafePath(entryPath);
|
|
267
282
|
const stat = await this.underlyingFs.promises.stat(entryRealPath);
|
|
@@ -282,7 +297,7 @@ class FileSystem {
|
|
|
282
297
|
for (const entry of entries) {
|
|
283
298
|
const entryName = String(entry);
|
|
284
299
|
if (regex.test(entryName)) {
|
|
285
|
-
const entryPath =
|
|
300
|
+
const entryPath = this.pathOps.join(currentPath, entryName);
|
|
286
301
|
if (rest.length === 0) {
|
|
287
302
|
results.push(entryPath);
|
|
288
303
|
} else {
|
|
@@ -330,4 +345,4 @@ class FileSystem {
|
|
|
330
345
|
}
|
|
331
346
|
}
|
|
332
347
|
|
|
333
|
-
//# debugId=
|
|
348
|
+
//# debugId=BBDA08F815F6373F64756E2164756E21
|
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../../src/fs/real-fs.ts"],
|
|
4
4
|
"sourcesContent": [
|
|
5
|
-
"import * as path from \"path\";\nimport * as nodeFs from \"node:fs/promises\";\nimport type { VirtualFS, FileStat } from \"../types.cjs\";\n\nexport type Permission = \"read-write\" | \"read-only\" | \"excluded\";\nexport type PermissionRules = Record<string, Permission>;\n\n// Minimal interface for the underlying fs (compatible with node:fs and memfs)\nexport interface UnderlyingFS {\n promises: {\n readFile(path: string): Promise<Buffer | Uint8Array | string>;\n readdir(path: string): Promise<string[]>;\n stat(path: string): Promise<{\n isFile(): boolean;\n isDirectory(): boolean;\n size: number;\n mtime: Date;\n }>;\n writeFile(path: string, data: Buffer | string): Promise<void>;\n appendFile(path: string, data: Buffer | string): Promise<void>;\n mkdir(path: string, opts?: { recursive?: boolean }): Promise<string | undefined | void>;\n rm(path: string, opts?: { recursive?: boolean; force?: boolean }): Promise<void>;\n };\n}\n\n// Default: use real node:fs\nconst defaultFS: UnderlyingFS = { promises: nodeFs };\n\ninterface CompiledRule {\n pattern: string;\n permission: Permission;\n specificity: number;\n}\n\nexport class FileSystem implements VirtualFS {\n private readonly mountBase: string | null;\n private readonly rules: CompiledRule[];\n protected readonly underlyingFs: UnderlyingFS;\n\n constructor(mountPath?: string, permissions?: PermissionRules, fs?: UnderlyingFS) {\n this.mountBase = mountPath ? path.resolve(mountPath) : null;\n this.rules = this.compileRules(permissions ?? {});\n this.underlyingFs = fs ?? defaultFS;\n }\n\n private compileRules(permissions: PermissionRules): CompiledRule[] {\n return Object.entries(permissions)\n .map(([pattern, permission]) => ({\n pattern,\n permission,\n specificity: this.calculateSpecificity(pattern),\n }))\n .sort((a, b) => b.specificity - a.specificity); // highest first\n }\n\n private calculateSpecificity(pattern: string): number {\n const segments = pattern.split(\"/\").filter(Boolean);\n let score = segments.length * 1000; // segment count is primary\n\n for (const seg of segments) {\n if (seg === \"**\") score += 0;\n else if (seg.includes(\"*\")) score += 1;\n else score += 10; // literal segment\n }\n return score;\n }\n\n public getPermission(virtualPath: string): Permission {\n const normalized = virtualPath.replace(/^\\/+/, \"\"); // strip leading slashes\n\n for (const rule of this.rules) {\n if (this.matchGlob(rule.pattern, normalized)) {\n return rule.permission;\n }\n }\n return \"read-write\"; // default\n }\n\n private matchGlob(pattern: string, filePath: string): boolean {\n // Convert glob to regex\n // ** matches any path segments, * matches within segment\n const regex = pattern\n .split(\"/\")\n .map((seg) => {\n if (seg === \"**\") return \".*\";\n return seg.replace(/\\*/g, \"[^/]*\").replace(/\\?/g, \"[^/]\");\n })\n .join(\"/\");\n return new RegExp(`^${regex}$`).test(filePath);\n }\n\n public checkPermission(virtualPath: string, operation: \"read\" | \"write\"): void {\n const perm = this.getPermission(virtualPath);\n\n if (perm === \"excluded\") {\n throw new Error(`Access denied: \"${virtualPath}\" is excluded`);\n }\n if (operation === \"write\" && perm === \"read-only\") {\n throw new Error(`Access denied: \"${virtualPath}\" is read-only`);\n }\n }\n\n private resolveSafePath(virtualPath: string): string {\n if (this.mountBase === null) {\n return path.resolve(virtualPath);\n }\n\n // Check for path traversal by tracking depth\n const segments = virtualPath.split(\"/\").filter(Boolean);\n let depth = 0;\n for (const seg of segments) {\n if (seg === \"..\") {\n depth--;\n if (depth < 0) {\n throw new Error(`Path traversal blocked: \"${virtualPath}\" escapes mount point`);\n }\n } else if (seg !== \".\") {\n depth++;\n }\n }\n\n const normalized = path.normalize(virtualPath);\n const relativePath = normalized.startsWith(\"/\") ? normalized.slice(1) : normalized;\n const realPath = path.join(this.mountBase, relativePath);\n const resolved = path.resolve(realPath);\n\n // Double-check containment (defense in depth)\n if (!resolved.startsWith(this.mountBase + path.sep) && resolved !== this.mountBase) {\n throw new Error(`Path traversal blocked: \"${virtualPath}\" escapes mount point`);\n }\n\n return resolved;\n }\n\n // Read operations\n async readFile(filePath: string): Promise<Buffer>;\n async readFile(filePath: string, encoding: BufferEncoding): Promise<string>;\n async readFile(filePath: string, encoding?: BufferEncoding): Promise<Buffer | string> {\n this.checkPermission(filePath, \"read\");\n const realPath = this.resolveSafePath(filePath);\n const content = await this.underlyingFs.promises.readFile(realPath);\n const buf = Buffer.from(content);\n return encoding ? buf.toString(encoding) : buf;\n }\n\n async readdir(dirPath: string): Promise<string[]> {\n this.checkPermission(dirPath, \"read\");\n const realPath = this.resolveSafePath(dirPath);\n const entries = await this.underlyingFs.promises.readdir(realPath);\n return entries.map(String);\n }\n\n async stat(filePath: string): Promise<FileStat> {\n this.checkPermission(filePath, \"read\");\n const realPath = this.resolveSafePath(filePath);\n const stats = await this.underlyingFs.promises.stat(realPath);\n return {\n isFile: () => stats.isFile(),\n isDirectory: () => stats.isDirectory(),\n size: stats.size,\n mtime: stats.mtime,\n };\n }\n\n async exists(filePath: string): Promise<boolean> {\n try {\n this.checkPermission(filePath, \"read\");\n const realPath = this.resolveSafePath(filePath);\n await this.underlyingFs.promises.stat(realPath);\n return true;\n } catch {\n return false;\n }\n }\n\n // Write operations\n async writeFile(filePath: string, data: Buffer | string): Promise<void> {\n this.checkPermission(filePath, \"write\");\n const realPath = this.resolveSafePath(filePath);\n await this.underlyingFs.promises.writeFile(realPath, data);\n }\n\n async appendFile(filePath: string, data: Buffer | string): Promise<void> {\n this.checkPermission(filePath, \"write\");\n const realPath = this.resolveSafePath(filePath);\n await this.underlyingFs.promises.appendFile(realPath, data);\n }\n\n async mkdir(dirPath: string, opts?: { recursive?: boolean }): Promise<void> {\n this.checkPermission(dirPath, \"write\");\n const realPath = this.resolveSafePath(dirPath);\n await this.underlyingFs.promises.mkdir(realPath, opts);\n }\n\n async rm(filePath: string, opts?: { recursive?: boolean; force?: boolean }): Promise<void> {\n this.checkPermission(filePath, \"write\");\n const realPath = this.resolveSafePath(filePath);\n await this.underlyingFs.promises.rm(realPath, opts);\n }\n\n // Path utilities (no permission check needed)\n resolve(...paths: string[]): string {\n return path.resolve(\"/\", ...paths);\n }\n\n dirname(filePath: string): string {\n return path.dirname(filePath);\n }\n\n basename(filePath: string): string {\n return path.basename(filePath);\n }\n\n // Glob with permission filtering\n async glob(pattern: string, opts?: { cwd?: string }): Promise<string[]> {\n const cwd = opts?.cwd ?? \"/\";\n this.checkPermission(cwd, \"read\");\n\n const matches = await this.expandGlob(pattern, cwd);\n\n // Filter out excluded paths\n return matches.filter((p) => this.getPermission(p) !== \"excluded\").sort();\n }\n\n // Glob expansion (similar to memfs-adapter implementation)\n private async expandGlob(pattern: string, cwd: string): Promise<string[]> {\n // Handle brace expansion first\n const patterns = this.expandBraces(pattern);\n const allMatches: string[] = [];\n\n for (const pat of patterns) {\n const matches = await this.matchPattern(pat, cwd);\n allMatches.push(...matches);\n }\n\n // Remove duplicates and sort\n return [...new Set(allMatches)].sort();\n }\n\n private expandBraces(pattern: string): string[] {\n const braceMatch = pattern.match(/\\{([^{}]+)\\}/);\n if (!braceMatch) return [pattern];\n\n const before = pattern.slice(0, braceMatch.index);\n const after = pattern.slice(braceMatch.index! + braceMatch[0].length);\n const options = braceMatch[1]!.split(\",\");\n\n const results: string[] = [];\n for (const opt of options) {\n const expanded = this.expandBraces(before + opt + after);\n results.push(...expanded);\n }\n return results;\n }\n\n private async matchPattern(pattern: string, cwd: string): Promise<string[]> {\n const parts = pattern.split(\"/\").filter((p) => p !== \"\");\n const isAbsolute = pattern.startsWith(\"/\");\n const startDir = isAbsolute ? \"/\" : cwd;\n\n return this.matchParts(parts, startDir);\n }\n\n private async matchParts(parts: string[], currentPath: string): Promise<string[]> {\n if (parts.length === 0) {\n return [currentPath];\n }\n\n const [part, ...rest] = parts;\n\n // Handle ** (recursive glob)\n if (part === \"**\") {\n const results: string[] = [];\n\n // Match current directory\n const withoutStar = await this.matchParts(rest, currentPath);\n results.push(...withoutStar);\n\n // Recurse into subdirectories\n try {\n const realPath = this.resolveSafePath(currentPath);\n const entries = await this.underlyingFs.promises.readdir(realPath);\n for (const entry of entries) {\n const entryPath = path.posix.join(currentPath, String(entry));\n try {\n const entryRealPath = this.resolveSafePath(entryPath);\n const stat = await this.underlyingFs.promises.stat(entryRealPath);\n if (stat.isDirectory()) {\n const subMatches = await this.matchParts(parts, entryPath);\n results.push(...subMatches);\n }\n } catch {\n // Skip inaccessible entries\n }\n }\n } catch {\n // Directory not readable\n }\n\n return results;\n }\n\n // Handle regular glob patterns\n const regex = this.globToRegex(part!);\n\n try {\n const realPath = this.resolveSafePath(currentPath);\n const entries = await this.underlyingFs.promises.readdir(realPath);\n const results: string[] = [];\n\n for (const entry of entries) {\n const entryName = String(entry);\n if (regex.test(entryName)) {\n const entryPath = path.posix.join(currentPath, entryName);\n if (rest.length === 0) {\n results.push(entryPath);\n } else {\n try {\n const entryRealPath = this.resolveSafePath(entryPath);\n const stat = await this.underlyingFs.promises.stat(entryRealPath);\n if (stat.isDirectory()) {\n const subMatches = await this.matchParts(rest, entryPath);\n results.push(...subMatches);\n }\n } catch {\n // Skip inaccessible entries\n }\n }\n }\n }\n\n return results;\n } catch {\n return [];\n }\n }\n\n private globToRegex(pattern: string): RegExp {\n let regex = \"^\";\n for (let i = 0; i < pattern.length; i++) {\n const char = pattern[i]!;\n if (char === \"*\") {\n regex += \"[^/]*\";\n } else if (char === \"?\") {\n regex += \"[^/]\";\n } else if (char === \"[\") {\n // Character class\n let j = i + 1;\n let classContent = \"\";\n while (j < pattern.length && pattern[j] !== \"]\") {\n classContent += pattern[j];\n j++;\n }\n regex += `[${classContent}]`;\n i = j;\n } else if (\".+^${}()|\\\\\".includes(char)) {\n regex += \"\\\\\" + char;\n } else {\n regex += char;\n }\n }\n regex += \"$\";\n return new RegExp(regex);\n }\n}\n"
|
|
5
|
+
"import * as path from \"path\";\nimport * as nodeFs from \"node:fs/promises\";\nimport type { VirtualFS, FileStat } from \"../types.cjs\";\n\nexport type Permission = \"read-write\" | \"read-only\" | \"excluded\";\nexport type PermissionRules = Record<string, Permission>;\nexport interface PathOps {\n readonly separator: string;\n resolve(...paths: string[]): string;\n normalize(path: string): string;\n join(...paths: string[]): string;\n relative(from: string, to: string): string;\n isAbsolute(path: string): boolean;\n dirname(path: string): string;\n basename(path: string): string;\n}\n\n// Minimal interface for the underlying fs (compatible with node:fs and memfs)\nexport interface UnderlyingFS {\n pathOps?: PathOps;\n promises: {\n readFile(path: string): Promise<Buffer | Uint8Array | string>;\n readdir(path: string): Promise<string[]>;\n stat(path: string): Promise<{\n isFile(): boolean;\n isDirectory(): boolean;\n size: number;\n mtime: Date;\n }>;\n writeFile(path: string, data: Buffer | string): Promise<void>;\n appendFile(path: string, data: Buffer | string): Promise<void>;\n mkdir(path: string, opts?: { recursive?: boolean }): Promise<string | undefined | void>;\n rm(path: string, opts?: { recursive?: boolean; force?: boolean }): Promise<void>;\n };\n}\n\n// Default: use real node:fs\nconst defaultFS: UnderlyingFS = { promises: nodeFs };\nconst nodePathOps: PathOps = {\n separator: path.sep,\n resolve: (...paths) => path.resolve(...paths),\n normalize: (filePath) => path.normalize(filePath),\n join: (...paths) => path.join(...paths),\n relative: (from, to) => path.relative(from, to),\n isAbsolute: (filePath) => path.isAbsolute(filePath),\n dirname: (filePath) => path.dirname(filePath),\n basename: (filePath) => path.basename(filePath),\n};\n\ninterface CompiledRule {\n pattern: string;\n permission: Permission;\n specificity: number;\n}\n\nexport class FileSystem implements VirtualFS {\n private readonly mountBase: string | null;\n private readonly rules: CompiledRule[];\n private readonly pathOps: PathOps;\n protected readonly underlyingFs: UnderlyingFS;\n\n constructor(mountPath?: string, permissions?: PermissionRules, fs?: UnderlyingFS) {\n const underlyingFs = fs ?? defaultFS;\n this.pathOps = underlyingFs.pathOps ?? nodePathOps;\n this.mountBase = mountPath ? this.pathOps.resolve(mountPath) : null;\n this.rules = this.compileRules(permissions ?? {});\n this.underlyingFs = underlyingFs;\n }\n\n private compileRules(permissions: PermissionRules): CompiledRule[] {\n return Object.entries(permissions)\n .map(([pattern, permission]) => ({\n pattern,\n permission,\n specificity: this.calculateSpecificity(pattern),\n }))\n .sort((a, b) => b.specificity - a.specificity); // highest first\n }\n\n private calculateSpecificity(pattern: string): number {\n const segments = pattern.split(\"/\").filter(Boolean);\n let score = segments.length * 1000; // segment count is primary\n\n for (const seg of segments) {\n if (seg === \"**\") score += 0;\n else if (seg.includes(\"*\")) score += 1;\n else score += 10; // literal segment\n }\n return score;\n }\n\n public getPermission(virtualPath: string): Permission {\n const normalized = virtualPath.replace(/^\\/+/, \"\"); // strip leading slashes\n\n for (const rule of this.rules) {\n if (this.matchGlob(rule.pattern, normalized)) {\n return rule.permission;\n }\n }\n return \"read-write\"; // default\n }\n\n private matchGlob(pattern: string, filePath: string): boolean {\n // Convert glob to regex\n // ** matches any path segments, * matches within segment\n const regex = pattern\n .split(\"/\")\n .map((seg) => {\n if (seg === \"**\") return \".*\";\n return seg.replace(/\\*/g, \"[^/]*\").replace(/\\?/g, \"[^/]\");\n })\n .join(\"/\");\n return new RegExp(`^${regex}$`).test(filePath);\n }\n\n public checkPermission(virtualPath: string, operation: \"read\" | \"write\"): void {\n const perm = this.getPermission(virtualPath);\n\n if (perm === \"excluded\") {\n throw new Error(`Access denied: \"${virtualPath}\" is excluded`);\n }\n if (operation === \"write\" && perm === \"read-only\") {\n throw new Error(`Access denied: \"${virtualPath}\" is read-only`);\n }\n }\n\n private resolveSafePath(virtualPath: string): string {\n if (this.mountBase === null) {\n return this.pathOps.resolve(virtualPath);\n }\n\n // Check for path traversal by tracking depth\n const segments = virtualPath.split(\"/\").filter(Boolean);\n let depth = 0;\n for (const seg of segments) {\n if (seg === \"..\") {\n depth--;\n if (depth < 0) {\n throw new Error(`Path traversal blocked: \"${virtualPath}\" escapes mount point`);\n }\n } else if (seg !== \".\") {\n depth++;\n }\n }\n\n const normalized = this.pathOps.normalize(virtualPath);\n const relativePath = normalized.startsWith(\"/\") ? normalized.slice(1) : normalized;\n const realPath = this.pathOps.join(this.mountBase, relativePath);\n const resolved = this.pathOps.resolve(realPath);\n\n // Double-check containment (defense in depth), including root mounts.\n const relativeFromMount = this.pathOps.relative(this.mountBase, resolved);\n const escapesMount =\n relativeFromMount === \"..\" ||\n relativeFromMount.startsWith(`..${this.pathOps.separator}`) ||\n this.pathOps.isAbsolute(relativeFromMount);\n if (escapesMount) {\n throw new Error(`Path traversal blocked: \"${virtualPath}\" escapes mount point`);\n }\n\n return resolved;\n }\n\n // Read operations\n async readFile(filePath: string): Promise<Buffer>;\n async readFile(filePath: string, encoding: BufferEncoding): Promise<string>;\n async readFile(filePath: string, encoding?: BufferEncoding): Promise<Buffer | string> {\n this.checkPermission(filePath, \"read\");\n const realPath = this.resolveSafePath(filePath);\n const content = await this.underlyingFs.promises.readFile(realPath);\n const buf = Buffer.from(content);\n return encoding ? buf.toString(encoding) : buf;\n }\n\n async readdir(dirPath: string): Promise<string[]> {\n this.checkPermission(dirPath, \"read\");\n const realPath = this.resolveSafePath(dirPath);\n const entries = await this.underlyingFs.promises.readdir(realPath);\n return entries.map(String);\n }\n\n async stat(filePath: string): Promise<FileStat> {\n this.checkPermission(filePath, \"read\");\n const realPath = this.resolveSafePath(filePath);\n const stats = await this.underlyingFs.promises.stat(realPath);\n return {\n isFile: () => stats.isFile(),\n isDirectory: () => stats.isDirectory(),\n size: stats.size,\n mtime: stats.mtime,\n };\n }\n\n async exists(filePath: string): Promise<boolean> {\n try {\n this.checkPermission(filePath, \"read\");\n const realPath = this.resolveSafePath(filePath);\n await this.underlyingFs.promises.stat(realPath);\n return true;\n } catch {\n return false;\n }\n }\n\n // Write operations\n async writeFile(filePath: string, data: Buffer | string): Promise<void> {\n this.checkPermission(filePath, \"write\");\n const realPath = this.resolveSafePath(filePath);\n await this.underlyingFs.promises.writeFile(realPath, data);\n }\n\n async appendFile(filePath: string, data: Buffer | string): Promise<void> {\n this.checkPermission(filePath, \"write\");\n const realPath = this.resolveSafePath(filePath);\n await this.underlyingFs.promises.appendFile(realPath, data);\n }\n\n async mkdir(dirPath: string, opts?: { recursive?: boolean }): Promise<void> {\n this.checkPermission(dirPath, \"write\");\n const realPath = this.resolveSafePath(dirPath);\n await this.underlyingFs.promises.mkdir(realPath, opts);\n }\n\n async rm(filePath: string, opts?: { recursive?: boolean; force?: boolean }): Promise<void> {\n this.checkPermission(filePath, \"write\");\n const realPath = this.resolveSafePath(filePath);\n await this.underlyingFs.promises.rm(realPath, opts);\n }\n\n // Path utilities (no permission check needed)\n resolve(...paths: string[]): string {\n return this.pathOps.resolve(\"/\", ...paths);\n }\n\n dirname(filePath: string): string {\n return this.pathOps.dirname(filePath);\n }\n\n basename(filePath: string): string {\n return this.pathOps.basename(filePath);\n }\n\n // Glob with permission filtering\n async glob(pattern: string, opts?: { cwd?: string }): Promise<string[]> {\n const cwd = opts?.cwd ?? \"/\";\n this.checkPermission(cwd, \"read\");\n\n const matches = await this.expandGlob(pattern, cwd);\n\n // Filter out excluded paths\n return matches.filter((p) => this.getPermission(p) !== \"excluded\").sort();\n }\n\n // Glob expansion (similar to memfs-adapter implementation)\n private async expandGlob(pattern: string, cwd: string): Promise<string[]> {\n // Handle brace expansion first\n const patterns = this.expandBraces(pattern);\n const allMatches: string[] = [];\n\n for (const pat of patterns) {\n const matches = await this.matchPattern(pat, cwd);\n allMatches.push(...matches);\n }\n\n // Remove duplicates and sort\n return [...new Set(allMatches)].sort();\n }\n\n private expandBraces(pattern: string): string[] {\n const braceMatch = pattern.match(/\\{([^{}]+)\\}/);\n if (!braceMatch) return [pattern];\n\n const before = pattern.slice(0, braceMatch.index);\n const after = pattern.slice(braceMatch.index! + braceMatch[0].length);\n const options = braceMatch[1]!.split(\",\");\n\n const results: string[] = [];\n for (const opt of options) {\n const expanded = this.expandBraces(before + opt + after);\n results.push(...expanded);\n }\n return results;\n }\n\n private async matchPattern(pattern: string, cwd: string): Promise<string[]> {\n const parts = pattern.split(\"/\").filter((p) => p !== \"\");\n const isAbsolute = pattern.startsWith(\"/\");\n const startDir = isAbsolute ? \"/\" : cwd;\n\n return this.matchParts(parts, startDir);\n }\n\n private async matchParts(parts: string[], currentPath: string): Promise<string[]> {\n if (parts.length === 0) {\n return [currentPath];\n }\n\n const [part, ...rest] = parts;\n\n // Handle ** (recursive glob)\n if (part === \"**\") {\n const results: string[] = [];\n\n // Match current directory\n const withoutStar = await this.matchParts(rest, currentPath);\n results.push(...withoutStar);\n\n // Recurse into subdirectories\n try {\n const realPath = this.resolveSafePath(currentPath);\n const entries = await this.underlyingFs.promises.readdir(realPath);\n for (const entry of entries) {\n const entryPath = this.pathOps.join(currentPath, String(entry));\n try {\n const entryRealPath = this.resolveSafePath(entryPath);\n const stat = await this.underlyingFs.promises.stat(entryRealPath);\n if (stat.isDirectory()) {\n const subMatches = await this.matchParts(parts, entryPath);\n results.push(...subMatches);\n }\n } catch {\n // Skip inaccessible entries\n }\n }\n } catch {\n // Directory not readable\n }\n\n return results;\n }\n\n // Handle regular glob patterns\n const regex = this.globToRegex(part!);\n\n try {\n const realPath = this.resolveSafePath(currentPath);\n const entries = await this.underlyingFs.promises.readdir(realPath);\n const results: string[] = [];\n\n for (const entry of entries) {\n const entryName = String(entry);\n if (regex.test(entryName)) {\n const entryPath = this.pathOps.join(currentPath, entryName);\n if (rest.length === 0) {\n results.push(entryPath);\n } else {\n try {\n const entryRealPath = this.resolveSafePath(entryPath);\n const stat = await this.underlyingFs.promises.stat(entryRealPath);\n if (stat.isDirectory()) {\n const subMatches = await this.matchParts(rest, entryPath);\n results.push(...subMatches);\n }\n } catch {\n // Skip inaccessible entries\n }\n }\n }\n }\n\n return results;\n } catch {\n return [];\n }\n }\n\n private globToRegex(pattern: string): RegExp {\n let regex = \"^\";\n for (let i = 0; i < pattern.length; i++) {\n const char = pattern[i]!;\n if (char === \"*\") {\n regex += \"[^/]*\";\n } else if (char === \"?\") {\n regex += \"[^/]\";\n } else if (char === \"[\") {\n // Character class\n let j = i + 1;\n let classContent = \"\";\n while (j < pattern.length && pattern[j] !== \"]\") {\n classContent += pattern[j];\n j++;\n }\n regex += `[${classContent}]`;\n i = j;\n } else if (\".+^${}()|\\\\\".includes(char)) {\n regex += \"\\\\\" + char;\n } else {\n regex += char;\n }\n }\n regex += \"$\";\n return new RegExp(regex);\n }\n}\n"
|
|
6
6
|
],
|
|
7
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAsB,IAAtB;AACwB,IAAxB;
|
|
8
|
-
"debugId": "
|
|
7
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAsB,IAAtB;AACwB,IAAxB;AAoCA,IAAM,YAA0B,EAAE,UAAU,OAAO;AACnD,IAAM,cAAuB;AAAA,EAC3B,WAAgB;AAAA,EAChB,SAAS,IAAI,UAAe,aAAQ,GAAG,KAAK;AAAA,EAC5C,WAAW,CAAC,aAAkB,eAAU,QAAQ;AAAA,EAChD,MAAM,IAAI,UAAe,UAAK,GAAG,KAAK;AAAA,EACtC,UAAU,CAAC,MAAM,OAAY,cAAS,MAAM,EAAE;AAAA,EAC9C,YAAY,CAAC,aAAkB,gBAAW,QAAQ;AAAA,EAClD,SAAS,CAAC,aAAkB,aAAQ,QAAQ;AAAA,EAC5C,UAAU,CAAC,aAAkB,cAAS,QAAQ;AAChD;AAAA;AAQO,MAAM,WAAgC;AAAA,EAC1B;AAAA,EACA;AAAA,EACA;AAAA,EACE;AAAA,EAEnB,WAAW,CAAC,WAAoB,aAA+B,IAAmB;AAAA,IAChF,MAAM,eAAe,MAAM;AAAA,IAC3B,KAAK,UAAU,aAAa,WAAW;AAAA,IACvC,KAAK,YAAY,YAAY,KAAK,QAAQ,QAAQ,SAAS,IAAI;AAAA,IAC/D,KAAK,QAAQ,KAA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AS;AAAA,gBACxD,QAAQ,KAAK,GAAG,UAAU;AAAA,cAC5B;AAAA,cACA,MAAM;AAAA;AAAA,QAIZ;AAAA,MACF;AAAA,MAEA,OAAO;AAAA,MACP,MAAM;AAAA,MACN,OAAO,CAAC;AAAA;AAAA;AAAA,EAIJ,WAAW,CAAC,SAAyB;AAAA,IAC3C,IAAI,QAAQ;AAAA,IACZ,SAAS,IAAI,EAAG,IAAI,QAAQ,QAAQ,KAAK;AAAA,MACvC,MAAM,OAAO,QAAQ;AAAA,MACrB,IAAI,SAAS,KAAK;AAAA,QAChB,SAAS;AAAA,MACX,EAAO,SAAI,SAAS,KAAK;AAAA,QACvB,SAAS;AAAA,MACX,EAAO,SAAI,SAAS,KAAK;AAAA,QAEvB,IAAI,IAAI,IAAI;AAAA,QACZ,IAAI,eAAe;AAAA,QACnB,OAAO,IAAI,QAAQ,UAAU,QAAQ,OAAO,KAAK;AAAA,UAC/C,gBAAgB,QAAQ;AAAA,UACxB;AAAA,QACF;AAAA,QACA,SAAS,IAAI;AAAA,QACb,IAAI;AAAA,MACN,EAAO,SAAI,cAAc,SAAS,IAAI,GAAG;AAAA,QACvC,SAAS,OAAO;AAAA,MAClB,EAAO;AAAA,QACL,SAAS;AAAA;AAAA,IAEb;AAAA,IACA,SAAS;AAAA,IACT,OAAO,IAAI,OAAO,KAAK;AAAA;AAE3B;",
|
|
8
|
+
"debugId": "BBDA08F815F6373F64756E2164756E21",
|
|
9
9
|
"names": []
|
|
10
10
|
}
|
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
function __accessProp(key) {
|
|
6
|
+
return this[key];
|
|
7
|
+
}
|
|
8
|
+
var __toCommonJS = (from) => {
|
|
9
|
+
var entry = (__moduleCache ??= new WeakMap).get(from), desc;
|
|
10
|
+
if (entry)
|
|
11
|
+
return entry;
|
|
12
|
+
entry = __defProp({}, "__esModule", { value: true });
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (var key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(entry, key))
|
|
16
|
+
__defProp(entry, key, {
|
|
17
|
+
get: __accessProp.bind(from, key),
|
|
18
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
__moduleCache.set(from, entry);
|
|
22
|
+
return entry;
|
|
23
|
+
};
|
|
24
|
+
var __moduleCache;
|
|
25
|
+
var __returnValue = (v) => v;
|
|
26
|
+
function __exportSetter(name, newValue) {
|
|
27
|
+
this[name] = __returnValue.bind(null, newValue);
|
|
28
|
+
}
|
|
29
|
+
var __export = (target, all) => {
|
|
30
|
+
for (var name in all)
|
|
31
|
+
__defProp(target, name, {
|
|
32
|
+
get: all[name],
|
|
33
|
+
enumerable: true,
|
|
34
|
+
configurable: true,
|
|
35
|
+
set: __exportSetter.bind(all, name)
|
|
36
|
+
});
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
// src/fs/web-fs.ts
|
|
40
|
+
var exports_web_fs = {};
|
|
41
|
+
__export(exports_web_fs, {
|
|
42
|
+
createWebUnderlyingFS: () => createWebUnderlyingFS,
|
|
43
|
+
WebFileSystem: () => WebFileSystem
|
|
44
|
+
});
|
|
45
|
+
module.exports = __toCommonJS(exports_web_fs);
|
|
46
|
+
var import_real_fs = require("./real-fs.cjs");
|
|
47
|
+
var DIRECTORY_MTIME = new Date(0);
|
|
48
|
+
var WEB_PATH_OPS = {
|
|
49
|
+
separator: "/",
|
|
50
|
+
resolve(...paths) {
|
|
51
|
+
return normalizeWebPath(paths.join("/"));
|
|
52
|
+
},
|
|
53
|
+
normalize: normalizeWebPath,
|
|
54
|
+
join(...paths) {
|
|
55
|
+
const nonEmpty = paths.filter((segment) => segment.length > 0);
|
|
56
|
+
if (nonEmpty.length === 0) {
|
|
57
|
+
return ".";
|
|
58
|
+
}
|
|
59
|
+
return normalizeWebPath(nonEmpty.join("/"));
|
|
60
|
+
},
|
|
61
|
+
relative(from, to) {
|
|
62
|
+
const fromSegments = getPathSegments(from);
|
|
63
|
+
const toSegments = getPathSegments(to);
|
|
64
|
+
let shared = 0;
|
|
65
|
+
while (shared < fromSegments.length && shared < toSegments.length && fromSegments[shared] === toSegments[shared]) {
|
|
66
|
+
shared++;
|
|
67
|
+
}
|
|
68
|
+
const up = new Array(fromSegments.length - shared).fill("..");
|
|
69
|
+
const down = toSegments.slice(shared);
|
|
70
|
+
return [...up, ...down].join("/");
|
|
71
|
+
},
|
|
72
|
+
isAbsolute(path) {
|
|
73
|
+
return path.startsWith("/");
|
|
74
|
+
},
|
|
75
|
+
dirname(path) {
|
|
76
|
+
const normalized = normalizeWebPath(path);
|
|
77
|
+
if (normalized === "/") {
|
|
78
|
+
return "/";
|
|
79
|
+
}
|
|
80
|
+
const segments = getPathSegments(normalized);
|
|
81
|
+
if (segments.length <= 1) {
|
|
82
|
+
return "/";
|
|
83
|
+
}
|
|
84
|
+
return `/${segments.slice(0, -1).join("/")}`;
|
|
85
|
+
},
|
|
86
|
+
basename(path) {
|
|
87
|
+
const normalized = normalizeWebPath(path);
|
|
88
|
+
if (normalized === "/") {
|
|
89
|
+
return "";
|
|
90
|
+
}
|
|
91
|
+
const segments = getPathSegments(normalized);
|
|
92
|
+
return segments[segments.length - 1] ?? "";
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
function createWebUnderlyingFS(root) {
|
|
96
|
+
return {
|
|
97
|
+
pathOps: WEB_PATH_OPS,
|
|
98
|
+
promises: {
|
|
99
|
+
async readFile(path) {
|
|
100
|
+
const { parentSegments, name } = splitParent(path);
|
|
101
|
+
const parent = await walkDirectory(root, parentSegments, false);
|
|
102
|
+
const fileHandle = await parent.getFileHandle(name, { create: false });
|
|
103
|
+
const file = await fileHandle.getFile();
|
|
104
|
+
return Buffer.from(await file.arrayBuffer());
|
|
105
|
+
},
|
|
106
|
+
async readdir(path) {
|
|
107
|
+
const dir = await walkDirectory(root, getPathSegments(path), false);
|
|
108
|
+
const entries = [];
|
|
109
|
+
for await (const [name] of dir.entries()) {
|
|
110
|
+
entries.push(name);
|
|
111
|
+
}
|
|
112
|
+
return entries;
|
|
113
|
+
},
|
|
114
|
+
async stat(path) {
|
|
115
|
+
const segments = getPathSegments(path);
|
|
116
|
+
if (segments.length === 0) {
|
|
117
|
+
return createDirectoryStat();
|
|
118
|
+
}
|
|
119
|
+
const { parentSegments, name } = splitParent(path);
|
|
120
|
+
const parent = await walkDirectory(root, parentSegments, false);
|
|
121
|
+
try {
|
|
122
|
+
const fileHandle = await parent.getFileHandle(name, { create: false });
|
|
123
|
+
const file = await fileHandle.getFile();
|
|
124
|
+
return {
|
|
125
|
+
isFile: () => true,
|
|
126
|
+
isDirectory: () => false,
|
|
127
|
+
size: file.size,
|
|
128
|
+
mtime: new Date(file.lastModified ?? 0)
|
|
129
|
+
};
|
|
130
|
+
} catch (error) {
|
|
131
|
+
if (!isNotFoundOrTypeMismatch(error))
|
|
132
|
+
throw error;
|
|
133
|
+
}
|
|
134
|
+
try {
|
|
135
|
+
await parent.getDirectoryHandle(name, { create: false });
|
|
136
|
+
return createDirectoryStat();
|
|
137
|
+
} catch (error) {
|
|
138
|
+
if (!isNotFoundOrTypeMismatch(error))
|
|
139
|
+
throw error;
|
|
140
|
+
throw error;
|
|
141
|
+
}
|
|
142
|
+
},
|
|
143
|
+
async writeFile(path, data) {
|
|
144
|
+
const { parentSegments, name } = splitParent(path);
|
|
145
|
+
const parent = await walkDirectory(root, parentSegments, false);
|
|
146
|
+
const fileHandle = await parent.getFileHandle(name, { create: true });
|
|
147
|
+
const writable = await fileHandle.createWritable();
|
|
148
|
+
await writable.write(toWritableData(data));
|
|
149
|
+
await writable.close();
|
|
150
|
+
},
|
|
151
|
+
async appendFile(path, data) {
|
|
152
|
+
const { parentSegments, name } = splitParent(path);
|
|
153
|
+
const parent = await walkDirectory(root, parentSegments, false);
|
|
154
|
+
const fileHandle = await parent.getFileHandle(name, { create: true });
|
|
155
|
+
const file = await fileHandle.getFile();
|
|
156
|
+
const writable = await fileHandle.createWritable({ keepExistingData: true });
|
|
157
|
+
await writable.write({
|
|
158
|
+
type: "write",
|
|
159
|
+
position: file.size,
|
|
160
|
+
data: toWritableData(data)
|
|
161
|
+
});
|
|
162
|
+
await writable.close();
|
|
163
|
+
},
|
|
164
|
+
async mkdir(path, opts) {
|
|
165
|
+
const segments = getPathSegments(path);
|
|
166
|
+
if (segments.length === 0) {
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
if (opts?.recursive) {
|
|
170
|
+
await walkDirectory(root, segments, true);
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
const parent = await walkDirectory(root, segments.slice(0, -1), false);
|
|
174
|
+
const name = segments[segments.length - 1];
|
|
175
|
+
const exists = await entryExists(parent, name);
|
|
176
|
+
if (exists) {
|
|
177
|
+
throw new Error(`EEXIST: file already exists, mkdir '${normalizeWebPath(path)}'`);
|
|
178
|
+
}
|
|
179
|
+
await parent.getDirectoryHandle(name, { create: true });
|
|
180
|
+
},
|
|
181
|
+
async rm(path, opts) {
|
|
182
|
+
const segments = getPathSegments(path);
|
|
183
|
+
if (segments.length === 0) {
|
|
184
|
+
throw new Error("EPERM: operation not permitted, rm '/'");
|
|
185
|
+
}
|
|
186
|
+
const parent = await walkDirectory(root, segments.slice(0, -1), false);
|
|
187
|
+
const name = segments[segments.length - 1];
|
|
188
|
+
try {
|
|
189
|
+
await parent.removeEntry(name, { recursive: opts?.recursive });
|
|
190
|
+
} catch (error) {
|
|
191
|
+
if (opts?.force && isNotFoundError(error)) {
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
throw error;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
class WebFileSystem extends import_real_fs.FileSystem {
|
|
202
|
+
constructor(root, permissions) {
|
|
203
|
+
super("/", permissions, createWebUnderlyingFS(root));
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
function createDirectoryStat() {
|
|
207
|
+
return {
|
|
208
|
+
isFile: () => false,
|
|
209
|
+
isDirectory: () => true,
|
|
210
|
+
size: 0,
|
|
211
|
+
mtime: DIRECTORY_MTIME
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
function normalizeWebPath(path) {
|
|
215
|
+
const normalized = path.replace(/\\/g, "/");
|
|
216
|
+
const rawSegments = (normalized.startsWith("/") ? normalized : `/${normalized}`).split("/").filter(Boolean);
|
|
217
|
+
const segments = [];
|
|
218
|
+
for (const segment of rawSegments) {
|
|
219
|
+
if (segment === ".")
|
|
220
|
+
continue;
|
|
221
|
+
if (segment === "..") {
|
|
222
|
+
segments.pop();
|
|
223
|
+
continue;
|
|
224
|
+
}
|
|
225
|
+
segments.push(segment);
|
|
226
|
+
}
|
|
227
|
+
return `/${segments.join("/")}`;
|
|
228
|
+
}
|
|
229
|
+
function getPathSegments(path) {
|
|
230
|
+
const normalized = normalizeWebPath(path);
|
|
231
|
+
return normalized.split("/").filter(Boolean);
|
|
232
|
+
}
|
|
233
|
+
function splitParent(path) {
|
|
234
|
+
const segments = getPathSegments(path);
|
|
235
|
+
if (segments.length === 0) {
|
|
236
|
+
throw new Error(`Invalid file path: "${path}"`);
|
|
237
|
+
}
|
|
238
|
+
return {
|
|
239
|
+
parentSegments: segments.slice(0, -1),
|
|
240
|
+
name: segments[segments.length - 1]
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
async function walkDirectory(root, segments, create) {
|
|
244
|
+
let current = root;
|
|
245
|
+
for (const segment of segments) {
|
|
246
|
+
current = await current.getDirectoryHandle(segment, { create });
|
|
247
|
+
}
|
|
248
|
+
return current;
|
|
249
|
+
}
|
|
250
|
+
async function entryExists(dir, name) {
|
|
251
|
+
try {
|
|
252
|
+
await dir.getFileHandle(name, { create: false });
|
|
253
|
+
return true;
|
|
254
|
+
} catch (error) {
|
|
255
|
+
if (isTypeMismatchError(error))
|
|
256
|
+
return true;
|
|
257
|
+
if (!isNotFoundError(error))
|
|
258
|
+
throw error;
|
|
259
|
+
}
|
|
260
|
+
try {
|
|
261
|
+
await dir.getDirectoryHandle(name, { create: false });
|
|
262
|
+
return true;
|
|
263
|
+
} catch (error) {
|
|
264
|
+
if (isTypeMismatchError(error))
|
|
265
|
+
return true;
|
|
266
|
+
if (!isNotFoundError(error))
|
|
267
|
+
throw error;
|
|
268
|
+
return false;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
function isNotFoundOrTypeMismatch(error) {
|
|
272
|
+
return isNotFoundError(error) || isTypeMismatchError(error);
|
|
273
|
+
}
|
|
274
|
+
function isNotFoundError(error) {
|
|
275
|
+
return getErrorName(error) === "NotFoundError";
|
|
276
|
+
}
|
|
277
|
+
function isTypeMismatchError(error) {
|
|
278
|
+
return getErrorName(error) === "TypeMismatchError";
|
|
279
|
+
}
|
|
280
|
+
function getErrorName(error) {
|
|
281
|
+
if (!error || typeof error !== "object")
|
|
282
|
+
return;
|
|
283
|
+
const named = error;
|
|
284
|
+
return typeof named.name === "string" ? named.name : undefined;
|
|
285
|
+
}
|
|
286
|
+
function toWritableData(data) {
|
|
287
|
+
if (typeof data === "string") {
|
|
288
|
+
return data;
|
|
289
|
+
}
|
|
290
|
+
const out = new Uint8Array(data.length);
|
|
291
|
+
out.set(data);
|
|
292
|
+
return out.buffer;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
//# debugId=63C0A71C805BC57D64756E2164756E21
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../../src/fs/web-fs.ts"],
|
|
4
|
+
"sourcesContent": [
|
|
5
|
+
"import { FileSystem, type PathOps, type PermissionRules, type UnderlyingFS } from \"./real-fs.cjs\";\n\nconst DIRECTORY_MTIME = new Date(0);\nconst WEB_PATH_OPS: PathOps = {\n separator: \"/\",\n resolve(...paths: string[]): string {\n return normalizeWebPath(paths.join(\"/\"));\n },\n normalize: normalizeWebPath,\n join(...paths: string[]): string {\n const nonEmpty = paths.filter((segment) => segment.length > 0);\n if (nonEmpty.length === 0) {\n return \".\";\n }\n return normalizeWebPath(nonEmpty.join(\"/\"));\n },\n relative(from: string, to: string): string {\n const fromSegments = getPathSegments(from);\n const toSegments = getPathSegments(to);\n let shared = 0;\n\n while (\n shared < fromSegments.length &&\n shared < toSegments.length &&\n fromSegments[shared] === toSegments[shared]\n ) {\n shared++;\n }\n\n const up = new Array(fromSegments.length - shared).fill(\"..\");\n const down = toSegments.slice(shared);\n return [...up, ...down].join(\"/\");\n },\n isAbsolute(path: string): boolean {\n return path.startsWith(\"/\");\n },\n dirname(path: string): string {\n const normalized = normalizeWebPath(path);\n if (normalized === \"/\") {\n return \"/\";\n }\n\n const segments = getPathSegments(normalized);\n if (segments.length <= 1) {\n return \"/\";\n }\n\n return `/${segments.slice(0, -1).join(\"/\")}`;\n },\n basename(path: string): string {\n const normalized = normalizeWebPath(path);\n if (normalized === \"/\") {\n return \"\";\n }\n\n const segments = getPathSegments(normalized);\n return segments[segments.length - 1] ?? \"\";\n },\n};\n\nexport function createWebUnderlyingFS(root: FileSystemDirectoryHandle): UnderlyingFS {\n return {\n pathOps: WEB_PATH_OPS,\n promises: {\n async readFile(path: string): Promise<Buffer> {\n const { parentSegments, name } = splitParent(path);\n const parent = await walkDirectory(root, parentSegments, false);\n const fileHandle = await parent.getFileHandle(name, { create: false });\n const file = await fileHandle.getFile();\n return Buffer.from(await file.arrayBuffer());\n },\n\n async readdir(path: string): Promise<string[]> {\n const dir = await walkDirectory(root, getPathSegments(path), false);\n const entries: string[] = [];\n for await (const [name] of dir.entries()) {\n entries.push(name);\n }\n return entries;\n },\n\n async stat(path: string): Promise<{\n isFile(): boolean;\n isDirectory(): boolean;\n size: number;\n mtime: Date;\n }> {\n const segments = getPathSegments(path);\n\n if (segments.length === 0) {\n return createDirectoryStat();\n }\n\n const { parentSegments, name } = splitParent(path);\n const parent = await walkDirectory(root, parentSegments, false);\n\n try {\n const fileHandle = await parent.getFileHandle(name, { create: false });\n const file = await fileHandle.getFile();\n return {\n isFile: () => true,\n isDirectory: () => false,\n size: file.size,\n mtime: new Date(file.lastModified ?? 0),\n };\n } catch (error) {\n if (!isNotFoundOrTypeMismatch(error)) throw error;\n }\n\n try {\n await parent.getDirectoryHandle(name, { create: false });\n return createDirectoryStat();\n } catch (error) {\n if (!isNotFoundOrTypeMismatch(error)) throw error;\n throw error;\n }\n },\n\n async writeFile(path: string, data: Buffer | string): Promise<void> {\n const { parentSegments, name } = splitParent(path);\n const parent = await walkDirectory(root, parentSegments, false);\n const fileHandle = await parent.getFileHandle(name, { create: true });\n const writable = await fileHandle.createWritable();\n await writable.write(toWritableData(data));\n await writable.close();\n },\n\n async appendFile(path: string, data: Buffer | string): Promise<void> {\n const { parentSegments, name } = splitParent(path);\n const parent = await walkDirectory(root, parentSegments, false);\n const fileHandle = await parent.getFileHandle(name, { create: true });\n const file = await fileHandle.getFile();\n const writable = await fileHandle.createWritable({ keepExistingData: true });\n await writable.write({\n type: \"write\",\n position: file.size,\n data: toWritableData(data),\n });\n await writable.close();\n },\n\n async mkdir(path: string, opts?: { recursive?: boolean }): Promise<void> {\n const segments = getPathSegments(path);\n if (segments.length === 0) {\n return;\n }\n\n if (opts?.recursive) {\n await walkDirectory(root, segments, true);\n return;\n }\n\n const parent = await walkDirectory(root, segments.slice(0, -1), false);\n const name = segments[segments.length - 1]!;\n const exists = await entryExists(parent, name);\n if (exists) {\n throw new Error(`EEXIST: file already exists, mkdir '${normalizeWebPath(path)}'`);\n }\n await parent.getDirectoryHandle(name, { create: true });\n },\n\n async rm(path: string, opts?: { recursive?: boolean; force?: boolean }): Promise<void> {\n const segments = getPathSegments(path);\n if (segments.length === 0) {\n throw new Error(\"EPERM: operation not permitted, rm '/'\");\n }\n\n const parent = await walkDirectory(root, segments.slice(0, -1), false);\n const name = segments[segments.length - 1]!;\n try {\n await parent.removeEntry(name, { recursive: opts?.recursive });\n } catch (error) {\n if (opts?.force && isNotFoundError(error)) {\n return;\n }\n throw error;\n }\n },\n },\n };\n}\n\nexport class WebFileSystem extends FileSystem {\n constructor(root: FileSystemDirectoryHandle, permissions?: PermissionRules) {\n super(\"/\", permissions, createWebUnderlyingFS(root));\n }\n}\n\nfunction createDirectoryStat() {\n return {\n isFile: () => false,\n isDirectory: () => true,\n size: 0,\n mtime: DIRECTORY_MTIME,\n };\n}\n\nfunction normalizeWebPath(path: string): string {\n const normalized = path.replace(/\\\\/g, \"/\");\n const rawSegments = (normalized.startsWith(\"/\") ? normalized : `/${normalized}`)\n .split(\"/\")\n .filter(Boolean);\n\n const segments: string[] = [];\n for (const segment of rawSegments) {\n if (segment === \".\") continue;\n if (segment === \"..\") {\n segments.pop();\n continue;\n }\n segments.push(segment);\n }\n\n return `/${segments.join(\"/\")}`;\n}\n\nfunction getPathSegments(path: string): string[] {\n const normalized = normalizeWebPath(path);\n return normalized.split(\"/\").filter(Boolean);\n}\n\nfunction splitParent(path: string): { parentSegments: string[]; name: string } {\n const segments = getPathSegments(path);\n if (segments.length === 0) {\n throw new Error(`Invalid file path: \"${path}\"`);\n }\n return {\n parentSegments: segments.slice(0, -1),\n name: segments[segments.length - 1]!,\n };\n}\n\nasync function walkDirectory(\n root: FileSystemDirectoryHandle,\n segments: string[],\n create: boolean\n): Promise<FileSystemDirectoryHandle> {\n let current = root;\n for (const segment of segments) {\n current = await current.getDirectoryHandle(segment, { create });\n }\n return current;\n}\n\nasync function entryExists(dir: FileSystemDirectoryHandle, name: string): Promise<boolean> {\n try {\n await dir.getFileHandle(name, { create: false });\n return true;\n } catch (error) {\n if (isTypeMismatchError(error)) return true;\n if (!isNotFoundError(error)) throw error;\n }\n\n try {\n await dir.getDirectoryHandle(name, { create: false });\n return true;\n } catch (error) {\n if (isTypeMismatchError(error)) return true;\n if (!isNotFoundError(error)) throw error;\n return false;\n }\n}\n\nfunction isNotFoundOrTypeMismatch(error: unknown): boolean {\n return isNotFoundError(error) || isTypeMismatchError(error);\n}\n\nfunction isNotFoundError(error: unknown): boolean {\n return getErrorName(error) === \"NotFoundError\";\n}\n\nfunction isTypeMismatchError(error: unknown): boolean {\n return getErrorName(error) === \"TypeMismatchError\";\n}\n\nfunction getErrorName(error: unknown): string | undefined {\n if (!error || typeof error !== \"object\") return undefined;\n const named = error as { name?: unknown };\n return typeof named.name === \"string\" ? named.name : undefined;\n}\n\nfunction toWritableData(data: Buffer | string): string | ArrayBuffer {\n if (typeof data === \"string\") {\n return data;\n }\n const out = new Uint8Array(data.length);\n out.set(data);\n return out.buffer;\n}\n"
|
|
6
|
+
],
|
|
7
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAkF,IAAlF;AAEA,IAAM,kBAAkB,IAAI,KAAK,CAAC;AAClC,IAAM,eAAwB;AAAA,EAC5B,WAAW;AAAA,EACX,OAAO,IAAI,OAAyB;AAAA,IAClC,OAAO,iBAAiB,MAAM,KAAK,GAAG,CAAC;AAAA;AAAA,EAEzC,WAAW;AAAA,EACX,IAAI,IAAI,OAAyB;AAAA,IAC/B,MAAM,WAAW,MAAM,OAAO,CAAC,YAAY,QAAQ,SAAS,CAAC;AAAA,IAC7D,IAAI,SAAS,WAAW,GAAG;AAAA,MACzB,OAAO;AAAA,IACT;AAAA,IACA,OAAO,iBAAiB,SAAS,KAAK,GAAG,CAAC;AAAA;AAAA,EAE5C,QAAQ,CAAC,MAAc,IAAoB;AAAA,IACzC,MAAM,eAAe,gBAAgB,IAAI;AAAA,IACzC,MAAM,aAAa,gBAAgB,EAAE;AAAA,IACrC,IAAI,SAAS;AAAA,IAEb,OACE,SAAS,aAAa,UACtB,SAAS,WAAW,UACpB,aA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K,KAAK,oBAAoB,KAAK;AAAA;AAG5D,SAAS,eAAe,CAAC,OAAyB;AAAA,EAChD,OAAO,aAAa,KAAK,MAAM;AAAA;AAGjC,SAAS,mBAAmB,CAAC,OAAyB;AAAA,EACpD,OAAO,aAAa,KAAK,MAAM;AAAA;AAGjC,SAAS,YAAY,CAAC,OAAoC;AAAA,EACxD,IAAI,CAAC,SAAS,OAAO,UAAU;AAAA,IAAU;AAAA,EACzC,MAAM,QAAQ;AAAA,EACd,OAAO,OAAO,MAAM,SAAS,WAAW,MAAM,OAAO;AAAA;AAGvD,SAAS,cAAc,CAAC,MAA6C;AAAA,EACnE,IAAI,OAAO,SAAS,UAAU;AAAA,IAC5B,OAAO;AAAA,EACT;AAAA,EACA,MAAM,MAAM,IAAI,WAAW,KAAK,MAAM;AAAA,EACtC,IAAI,IAAI,IAAI;AAAA,EACZ,OAAO,IAAI;AAAA;",
|
|
8
|
+
"debugId": "63C0A71C805BC57D64756E2164756E21",
|
|
9
|
+
"names": []
|
|
10
|
+
}
|
package/dist/cjs/src/index.cjs
CHANGED
|
@@ -61,12 +61,14 @@ __export(exports_src, {
|
|
|
61
61
|
isAndNode: () => import_parser2.isAndNode,
|
|
62
62
|
escapeForInterpolation: () => import_utils.escapeForInterpolation,
|
|
63
63
|
escape: () => import_utils.escape,
|
|
64
|
+
createWebUnderlyingFS: () => import_fs2.createWebUnderlyingFS,
|
|
64
65
|
createVirtualFS: () => import_fs.createVirtualFS,
|
|
65
66
|
createStdout: () => import_io2.createStdout,
|
|
66
67
|
createStdin: () => import_io.createStdin,
|
|
67
68
|
createStderr: () => import_io2.createStderr,
|
|
68
69
|
createShellDSL: () => import_shell_dsl.createShellDSL,
|
|
69
70
|
createPipe: () => import_io2.createPipe,
|
|
71
|
+
WebFileSystem: () => import_fs2.WebFileSystem,
|
|
70
72
|
StdinImpl: () => import_io.StdinImpl,
|
|
71
73
|
ShellPromise: () => import_shell_promise.ShellPromise,
|
|
72
74
|
ShellError: () => import_errors.ShellError,
|
|
@@ -98,4 +100,4 @@ var import_io = require("./io/index.cjs");
|
|
|
98
100
|
var import_io2 = require("./io/index.cjs");
|
|
99
101
|
var import_utils = require("./utils/index.cjs");
|
|
100
102
|
|
|
101
|
-
//# debugId=
|
|
103
|
+
//# debugId=3F6524945419391564756E2164756E21
|