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

+6 -4
View File
@@ -5,11 +5,13 @@
export interface JSRuntimeFS { export interface JSRuntimeFS {
// Core file operations with proper overloads for type safety // Core file operations with proper overloads for type safety
readFile(path: string): Promise<Uint8Array>; readFile(path: string): Promise<Uint8Array>;
readFile(path: string, encoding: 'utf8'): Promise<string>; readFile(path: string, options: 'utf8'): Promise<string>;
readFile(path: string, encoding: string): Promise<string>; readFile(path: string, options: string): Promise<string>;
readFile(path: string, encoding?: string): Promise<string | Uint8Array>; readFile(path: string, options: { encoding: 'utf8' }): Promise<string>;
readFile(path: string, options: { encoding: string }): Promise<string>;
readFile(path: string, options?: string | { encoding?: string }): Promise<string | Uint8Array>;
writeFile(path: string, data: string | Uint8Array, encoding?: string): Promise<void>; writeFile(path: string, data: string | Uint8Array, options?: string | { encoding?: string }): Promise<void>;
// readdir overloads for different return types // readdir overloads for different return types
readdir(path: string): Promise<string[]>; readdir(path: string): Promise<string[]>;
+9 -5
View File
@@ -9,14 +9,18 @@ export class LightningFSAdapter implements JSRuntimeFS {
constructor(private lightningFS: LightningFS.PromisifiedFS) {} constructor(private lightningFS: LightningFS.PromisifiedFS) {}
async readFile(path: string): Promise<Uint8Array>; async readFile(path: string): Promise<Uint8Array>;
async readFile(path: string, encoding: 'utf8'): Promise<string>; async readFile(path: string, options: 'utf8'): Promise<string>;
async readFile(path: string, encoding: string): Promise<string>; async readFile(path: string, options: string): Promise<string>;
async readFile(path: string, encoding?: string): Promise<string | Uint8Array>; async readFile(path: string, options: { encoding: 'utf8' }): Promise<string>;
async readFile(path: string, encoding?: string): Promise<string | Uint8Array> { 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 encoding = typeof options === 'string' ? options : options?.encoding;
return this.lightningFS.readFile(path, encoding); return this.lightningFS.readFile(path, encoding);
} }
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 encoding = typeof options === 'string' ? options : options?.encoding;
return this.lightningFS.writeFile(path, data, encoding); return this.lightningFS.writeFile(path, data, encoding);
} }
+7 -3
View File
@@ -41,15 +41,19 @@ class MockFS implements JSRuntimeFS {
private dirs: Set<string> = new Set(); private dirs: Set<string> = new Set();
async readFile(path: string): Promise<Uint8Array>; async readFile(path: string): Promise<Uint8Array>;
async readFile(path: string, encoding: "utf8"): Promise<string>; async readFile(path: string, options: 'utf8'): Promise<string>;
async readFile(path: string, encoding: string): Promise<string | Uint8Array>; async readFile(path: string, options: string): Promise<string>;
async readFile(path: string, encoding?: string): Promise<string | Uint8Array> { 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); const content = this.files.get(path);
if (content === undefined) { if (content === undefined) {
const error = new Error(`ENOENT: no such file or directory, open '${path}'`) as NodeJS.ErrnoException; const error = new Error(`ENOENT: no such file or directory, open '${path}'`) as NodeJS.ErrnoException;
error.code = 'ENOENT'; error.code = 'ENOENT';
throw error; throw error;
} }
const encoding = typeof options === 'string' ? options : options?.encoding;
if (encoding === 'utf8') { if (encoding === 'utf8') {
if (typeof content === 'string') { if (typeof content === 'string') {
return content; return content;
+8 -4
View File
@@ -13,12 +13,16 @@ class MockFS implements JSRuntimeFS {
} }
async readFile(path: string): Promise<Uint8Array>; async readFile(path: string): Promise<Uint8Array>;
async readFile(path: string, encoding: 'utf8'): Promise<string>; async readFile(path: string, options: 'utf8'): Promise<string>;
async readFile(path: string, encoding: string): Promise<string>; async readFile(path: string, options: string): Promise<string>;
async readFile(path: string, encoding?: string): Promise<string | Uint8Array> { 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 data = this.files.get(path); const data = this.files.get(path);
if (!data) throw new Error(`File not found: ${path}`); if (!data) throw new Error(`File not found: ${path}`);
const encoding = typeof options === 'string' ? options : options?.encoding;
if (encoding === 'utf8') { if (encoding === 'utf8') {
return typeof data === 'string' ? data : new TextDecoder().decode(data); return typeof data === 'string' ? data : new TextDecoder().decode(data);
} }
@@ -26,7 +30,7 @@ class MockFS implements JSRuntimeFS {
return data; return data;
} }
async writeFile(path: string, data: string | Uint8Array): Promise<void> { async writeFile(path: string, data: string | Uint8Array, _options?: string | { encoding?: string }): Promise<void> {
this.files.set(path, data); this.files.set(path, data);
} }
+8 -4
View File
@@ -26,15 +26,19 @@ class MockFS implements JSRuntimeFS {
} }
async readFile(path: string): Promise<Uint8Array>; async readFile(path: string): Promise<Uint8Array>;
async readFile(path: string, encoding: 'utf8'): Promise<string>; async readFile(path: string, options: 'utf8'): Promise<string>;
async readFile(path: string, encoding: string): Promise<string>; async readFile(path: string, options: string): Promise<string>;
async readFile(path: string, encoding?: string): Promise<string | Uint8Array> { 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 file = this.files.get(path); const file = this.files.get(path);
if (!file || file.isDir) throw new Error(`File not found: ${path}`); if (!file || file.isDir) throw new Error(`File not found: ${path}`);
const encoding = typeof options === 'string' ? options : options?.encoding;
return encoding === 'utf8' ? file.content.toString() : new Uint8Array(); return encoding === 'utf8' ? file.content.toString() : new Uint8Array();
} }
async writeFile(path: string, data: string | Uint8Array): Promise<void> { async writeFile(path: string, data: string | Uint8Array, _options?: string | { encoding?: string }): Promise<void> {
this.files.set(path, { content: data, mtimeMs: Date.now(), isDir: false }); this.files.set(path, { content: data, mtimeMs: Date.now(), isDir: false });
} }
@@ -14,18 +14,21 @@ class MockFS implements JSRuntimeFS {
} }
async readFile(path: string): Promise<Uint8Array>; async readFile(path: string): Promise<Uint8Array>;
async readFile(path: string, encoding: 'utf8'): Promise<string>; async readFile(path: string, options: 'utf8'): Promise<string>;
async readFile(path: string, encoding: string): Promise<string>; async readFile(path: string, options: string): Promise<string>;
async readFile(path: string, encoding?: string): Promise<string | Uint8Array>; async readFile(path: string, options: { encoding: 'utf8' }): Promise<string>;
async readFile(path: string, encoding?: string): Promise<string | Uint8Array> { 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); const content = this.files.get(path);
if (content === undefined) { if (content === undefined) {
throw new Error(`File not found: ${path}`); throw new Error(`File not found: ${path}`);
} }
const encoding = typeof options === 'string' ? options : options?.encoding;
return encoding ? content : new TextEncoder().encode(content); return encoding ? content : new TextEncoder().encode(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); const content = typeof data === 'string' ? data : new TextDecoder().decode(data);
this.files.set(path, content); this.files.set(path, content);
} }
+8 -5
View File
@@ -41,15 +41,18 @@ class MockFS implements JSRuntimeFS {
} }
async readFile(path: string): Promise<Uint8Array>; async readFile(path: string): Promise<Uint8Array>;
async readFile(path: string, encoding: 'utf8'): Promise<string>; async readFile(path: string, options: 'utf8'): Promise<string>;
async readFile(path: string, encoding: string): Promise<string>; async readFile(path: string, options: string): Promise<string>;
async readFile(path: string, encoding?: string): Promise<string | Uint8Array>; async readFile(path: string, options: { encoding: 'utf8' }): Promise<string>;
async readFile(path: string, encoding?: string): Promise<string | Uint8Array> { 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); const content = this.files.get(path);
if (content === undefined) { if (content === undefined) {
throw new Error(`File not found: ${path}`); throw new Error(`File not found: ${path}`);
} }
const encoding = typeof options === 'string' ? options : options?.encoding;
// If no encoding specified, return Uint8Array // If no encoding specified, return Uint8Array
if (!encoding) { if (!encoding) {
// For binary files, return mock binary data with null bytes // For binary files, return mock binary data with null bytes
@@ -70,7 +73,7 @@ class MockFS implements JSRuntimeFS {
return content; 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); const content = typeof data === 'string' ? data : new TextDecoder().decode(data);
this.files.set(path, content); this.files.set(path, content);
} }
+8 -5
View File
@@ -13,18 +13,21 @@ class MockFS implements JSRuntimeFS {
} }
async readFile(path: string): Promise<Uint8Array>; async readFile(path: string): Promise<Uint8Array>;
async readFile(path: string, encoding: 'utf8'): Promise<string>; async readFile(path: string, options: 'utf8'): Promise<string>;
async readFile(path: string, encoding: string): Promise<string>; async readFile(path: string, options: string): Promise<string>;
async readFile(path: string, encoding?: string): Promise<string | Uint8Array>; async readFile(path: string, options: { encoding: 'utf8' }): Promise<string>;
async readFile(path: string, encoding?: string): Promise<string | Uint8Array> { 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); const content = this.files.get(path);
if (content === undefined) { if (content === undefined) {
throw new Error(`File not found: ${path}`); throw new Error(`File not found: ${path}`);
} }
const encoding = typeof options === 'string' ? options : options?.encoding;
return encoding ? content : new TextEncoder().encode(content); return encoding ? content : new TextEncoder().encode(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); const content = typeof data === 'string' ? data : new TextDecoder().decode(data);
this.files.set(path, content); this.files.set(path, content);
} }