-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathfilesystem.ts
More file actions
113 lines (101 loc) · 3.97 KB
/
Copy pathfilesystem.ts
File metadata and controls
113 lines (101 loc) · 3.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
/**
* FileSystem provider interface for the Copilot SDK.
* Allows integrators to provide custom filesystem implementations
* (e.g., in-memory for WASM/testing, or proxied to host).
*/
export interface FileSystemProvider {
readFile(path: string): Promise<string>;
writeFile(path: string, content: string): Promise<void>;
exists(path: string): Promise<boolean>;
readDir(path: string): Promise<string[]>;
mkdir(path: string, options?: { recursive?: boolean }): Promise<void>;
remove(path: string): Promise<void>;
}
/**
* In-memory filesystem implementation for WASM, testing, and sandboxed scenarios.
* Files are stored in a Map, no disk I/O.
*
* @example
* ```typescript
* const fs = new InMemoryFileSystem();
* await fs.writeFile("/doc.txt", "Hello world");
* const content = await fs.readFile("/doc.txt");
* ```
*/
export class InMemoryFileSystem implements FileSystemProvider {
private files: Map<string, string> = new Map();
constructor(initialFiles?: Record<string, string>) {
if (initialFiles) {
for (const [path, content] of Object.entries(initialFiles)) {
this.files.set(this.normalize(path), content);
}
}
}
private normalize(path: string): string {
// Normalize path separators and remove trailing slashes
return path.replace(/\\/g, "/").replace(/\/+$/, "") || "/";
}
async readFile(path: string): Promise<string> {
const normalized = this.normalize(path);
const content = this.files.get(normalized);
if (content === undefined) {
throw new Error(`ENOENT: no such file: ${normalized}`);
}
return content;
}
async writeFile(path: string, content: string): Promise<void> {
this.files.set(this.normalize(path), content);
}
async exists(path: string): Promise<boolean> {
const normalized = this.normalize(path);
// Check exact match or any file under this directory
if (this.files.has(normalized)) return true;
const prefix = normalized.endsWith("/") ? normalized : normalized + "/";
for (const key of this.files.keys()) {
if (key.startsWith(prefix)) return true;
}
return false;
}
async readDir(path: string): Promise<string[]> {
const normalized = this.normalize(path);
const prefix = normalized === "/" ? "/" : normalized + "/";
const entries = new Set<string>();
for (const key of this.files.keys()) {
if (key.startsWith(prefix)) {
const relative = key.slice(prefix.length);
const firstSegment = relative.split("/")[0];
if (firstSegment) entries.add(firstSegment);
}
}
return [...entries].sort();
}
async mkdir(_path: string, _options?: { recursive?: boolean }): Promise<void> {
// No-op for in-memory FS — directories are implicit
}
async remove(path: string): Promise<void> {
const normalized = this.normalize(path);
this.files.delete(normalized);
// Also remove any files under this directory
const prefix = normalized + "/";
for (const key of this.files.keys()) {
if (key.startsWith(prefix)) {
this.files.delete(key);
}
}
}
/** Get all files (for testing/debugging) */
getAllFiles(): Record<string, string> {
const result: Record<string, string> = {};
for (const [path, content] of this.files) {
result[path] = content;
}
return result;
}
/** Synchronous write for initialization */
writeSync(path: string, content: string): void {
this.files.set(this.normalize(path), content);
}
}