Accept options object in JSRuntimeFS

This commit is contained in:
Alex Gleason committed 2025-10-03 23:56:17 -05:00
1 parent a4c0498e5b
commit d9956e60eb
8 files changed
+62 -35

No files matched your search

+8 -5
View File
@@ -41,15 +41,18 @@ class MockFS implements JSRuntimeFS {
}
async readFile(path: string): Promise<Uint8Array>;
async readFile(path: string, encoding: 'utf8'): Promise<string>;
async readFile(path: string, encoding: string): Promise<string>;
async readFile(path: string, encoding?: string): Promise<string | Uint8Array>;
async readFile(path: string, encoding?: string): Promise<string | Uint8Array> {
async readFile(path: string, options: 'utf8'): Promise<string>;
async readFile(path: string, options: string): Promise<string>;
async readFile(path: string, options: { encoding: 'utf8' }): Promise<string>;
async readFile(path: string, options: { encoding: string }): Promise<string>;
async readFile(path: string, options?: string | { encoding?: string }): Promise<string | Uint8Array>;
async readFile(path: string, options?: string | { encoding?: string }): Promise<string | Uint8Array> {
const content = this.files.get(path);
if (content === undefined) {
throw new Error(`File not found: ${path}`);
}
const encoding = typeof options === 'string' ? options : options?.encoding;
// If no encoding specified, return Uint8Array
if (!encoding) {
// For binary files, return mock binary data with null bytes
@@ -70,7 +73,7 @@ class MockFS implements JSRuntimeFS {
return content;
}
async writeFile(path: string, data: string | Uint8Array, _encoding?: string): Promise<void> {
async writeFile(path: string, data: string | Uint8Array, _options?: string | { encoding?: string }): Promise<void> {
const content = typeof data === 'string' ? data : new TextDecoder().decode(data);
this.files.set(path, content);
}