mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-06 00:27:24 +00:00
Remove WebContainers
This commit is contained in:
9 files changed
+4
-281
No files matched your search
+2
-58
@@ -1,25 +1,6 @@
|
||||
/**
|
||||
* JavaScript runtime interface for executing code and managing files
|
||||
*/
|
||||
export interface JSRuntime {
|
||||
/**
|
||||
* Filesystem access
|
||||
*/
|
||||
fs(): Promise<JSRuntimeFS>;
|
||||
|
||||
/**
|
||||
* Spawn a process in the runtime environment
|
||||
* @param command Command to execute
|
||||
* @param args Command arguments
|
||||
* @param options Spawn options
|
||||
* @returns Process handle
|
||||
*/
|
||||
spawn(command: string, args?: string[], options?: SpawnOptions): Promise<JSRuntimeChildProcess>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filesystem interface compatible with both WebContainer and LightningFS
|
||||
* This interface unifies the APIs of both filesystem implementations
|
||||
* Filesystem interface for LightningFS
|
||||
* This interface provides a unified API for filesystem operations
|
||||
*/
|
||||
export interface JSRuntimeFS {
|
||||
// Core file operations with proper overloads for type safety
|
||||
@@ -64,41 +45,4 @@ export interface DirectoryEntry {
|
||||
name: string;
|
||||
isDirectory(): boolean;
|
||||
isFile(): boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Unified child process interface
|
||||
*/
|
||||
export interface JSRuntimeChildProcess {
|
||||
/**
|
||||
* A promise for the exit code of the process.
|
||||
*/
|
||||
exit: Promise<number>;
|
||||
/**
|
||||
* An input stream for the attached pseudoterminal device.
|
||||
*/
|
||||
input: WritableStream<string>;
|
||||
/**
|
||||
* A stream that receives all terminal output, including the stdout and stderr emitted by the spawned process
|
||||
* _and_ its descendants.
|
||||
*/
|
||||
output: ReadableStream<string>;
|
||||
/**
|
||||
* Kills the process.
|
||||
*/
|
||||
kill(): void;
|
||||
}
|
||||
|
||||
/**
|
||||
* Options that control process spawning.
|
||||
*/
|
||||
export interface SpawnOptions {
|
||||
/**
|
||||
* Current working directory for the process, relative to the workdir of this instance.
|
||||
*/
|
||||
cwd?: string;
|
||||
/**
|
||||
* Environment variables to set for the process.
|
||||
*/
|
||||
env?: Record<string, string | number | boolean>;
|
||||
}
|
||||
@@ -1,165 +0,0 @@
|
||||
import { WebContainer } from '@webcontainer/api';
|
||||
import type { JSRuntime, JSRuntimeFS, JSRuntimeChildProcess, DirectoryEntry, SpawnOptions } from './JSRuntime';
|
||||
|
||||
/**
|
||||
* WebContainer adapter that implements the JSRuntime interface
|
||||
*/
|
||||
export class WebContainerAdapter implements JSRuntime {
|
||||
private webcontainer: WebContainer | null = null;
|
||||
private bootPromise: Promise<WebContainer> | null = null;
|
||||
|
||||
/**
|
||||
* Get the filesystem interface
|
||||
*/
|
||||
async fs(): Promise<JSRuntimeFS> {
|
||||
const webcontainer = await this.ensureBooted();
|
||||
return new WebContainerFSAdapter(webcontainer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure WebContainer is booted
|
||||
*/
|
||||
private async ensureBooted(): Promise<WebContainer> {
|
||||
if (this.webcontainer) {
|
||||
return this.webcontainer;
|
||||
}
|
||||
|
||||
if (!this.bootPromise) {
|
||||
this.bootPromise = WebContainer.boot();
|
||||
}
|
||||
|
||||
this.webcontainer = await this.bootPromise;
|
||||
return this.webcontainer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Spawn a process in the WebContainer
|
||||
* Automatically boots the container on first call
|
||||
*/
|
||||
async spawn(command: string, args: string[] = [], options: SpawnOptions = {}): Promise<JSRuntimeChildProcess> {
|
||||
const webcontainer = await this.ensureBooted();
|
||||
return webcontainer.spawn(command, args, options);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* WebContainer filesystem adapter that implements JSRuntimeFS
|
||||
*/
|
||||
class WebContainerFSAdapter implements JSRuntimeFS {
|
||||
constructor(private webcontainer: WebContainer) {}
|
||||
|
||||
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> {
|
||||
if (encoding) {
|
||||
return this.webcontainer.fs.readFile(path, encoding as 'utf8');
|
||||
} else {
|
||||
return this.webcontainer.fs.readFile(path);
|
||||
}
|
||||
}
|
||||
|
||||
async writeFile(path: string, data: string | Uint8Array, encoding?: string): Promise<void> {
|
||||
if (encoding) {
|
||||
return this.webcontainer.fs.writeFile(path, data, encoding as 'utf8');
|
||||
} else {
|
||||
return this.webcontainer.fs.writeFile(path, data);
|
||||
}
|
||||
}
|
||||
|
||||
async readdir(path: string): Promise<string[]>;
|
||||
async readdir(path: string, options: { withFileTypes: true }): Promise<DirectoryEntry[]>;
|
||||
async readdir(path: string, options?: { withFileTypes?: boolean }): Promise<string[] | DirectoryEntry[]>;
|
||||
async readdir(path: string, options?: { withFileTypes?: boolean }): Promise<string[] | DirectoryEntry[]> {
|
||||
if (options?.withFileTypes) {
|
||||
return this.webcontainer.fs.readdir(path, { withFileTypes: true });
|
||||
} else {
|
||||
return this.webcontainer.fs.readdir(path);
|
||||
}
|
||||
}
|
||||
|
||||
async mkdir(path: string, options?: { recursive?: boolean }): Promise<void> {
|
||||
if (options?.recursive) {
|
||||
await this.webcontainer.fs.mkdir(path, { recursive: true });
|
||||
} else {
|
||||
await this.webcontainer.fs.mkdir(path);
|
||||
}
|
||||
}
|
||||
|
||||
async stat(path: string): Promise<{
|
||||
isDirectory(): boolean;
|
||||
isFile(): boolean;
|
||||
size?: number;
|
||||
mtimeMs?: number;
|
||||
}> {
|
||||
// WebContainer doesn't have a stat method, so we'll implement a workaround
|
||||
try {
|
||||
// Try to read as directory first
|
||||
await this.webcontainer.fs.readdir(path);
|
||||
return {
|
||||
isDirectory: () => true,
|
||||
isFile: () => false,
|
||||
size: undefined,
|
||||
mtimeMs: undefined,
|
||||
};
|
||||
} catch {
|
||||
// If readdir fails, assume it's a file
|
||||
try {
|
||||
const content = await this.webcontainer.fs.readFile(path) as string | Uint8Array;
|
||||
const size = typeof content === 'string' ? content.length : content.byteLength;
|
||||
return {
|
||||
isDirectory: () => false,
|
||||
isFile: () => true,
|
||||
size,
|
||||
mtimeMs: Date.now(), // WebContainer doesn't provide modification time
|
||||
};
|
||||
} catch {
|
||||
return {
|
||||
isDirectory: () => false,
|
||||
isFile: () => true,
|
||||
size: undefined,
|
||||
mtimeMs: undefined,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async lstat(path: string): Promise<{
|
||||
isDirectory(): boolean;
|
||||
isFile(): boolean;
|
||||
size?: number;
|
||||
mtimeMs?: number;
|
||||
}> {
|
||||
// WebContainer doesn't have a lstat method, so we can use stat directly
|
||||
return this.stat(path);
|
||||
}
|
||||
|
||||
async unlink(_path: string): Promise<void> {
|
||||
// WebContainer doesn't expose unlink, so we'll throw an error for now
|
||||
throw new Error('unlink operation not supported in WebContainer');
|
||||
}
|
||||
|
||||
async rmdir(_path: string): Promise<void> {
|
||||
// WebContainer doesn't expose rmdir, so we'll throw an error for now
|
||||
throw new Error('rmdir operation not supported in WebContainer');
|
||||
}
|
||||
|
||||
async rename(oldPath: string, newPath: string): Promise<void> {
|
||||
// WebContainer doesn't expose rename, so we'll implement it by copying and deleting
|
||||
const content = await this.webcontainer.fs.readFile(oldPath);
|
||||
await this.webcontainer.fs.writeFile(newPath, content);
|
||||
// Note: We can't delete the old file since WebContainer doesn't expose unlink
|
||||
console.warn('WebContainer rename: copied file but could not delete original');
|
||||
}
|
||||
|
||||
async readlink(_path: string): Promise<string> {
|
||||
// WebContainer doesn't support symlinks, so we'll throw an error
|
||||
throw new Error('readlink operation not supported in WebContainer');
|
||||
}
|
||||
|
||||
async symlink(_target: string, _path: string): Promise<void> {
|
||||
// WebContainer doesn't support symlinks, so we'll throw an error
|
||||
throw new Error('symlink operation not supported in WebContainer');
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user