This article details the architecture of yOS, a Unix-like operating system built entirely within a web browser. It explores how core OS concepts like a lean kernel, virtual filesystem (VFS) backed by IndexedDB, and inter-process communication (IPC) via domain sockets are re-imagined and implemented using web technologies like JavaScript/TypeScript and Svelte.
Read original on Dev.to #systemdesignThe article presents yOS, a browser-based operating system, as a practical example of applying traditional OS architectural principles within a web environment. This showcases how concepts typically found in low-level system programming can be translated and implemented using web technologies to achieve similar functionalities.
yOS is built on several key components that mimic a traditional Unix-like OS structure, adapted for the browser context:
The IndexedDB VFS is a core system design element, demonstrating how persistent storage can be achieved in a browser environment. It maps Unix file operations to IndexedDB transactions, managing INodes and data blocks. This design choice highlights trade-offs between web-native storage capabilities and the desire to emulate a traditional filesystem structure.
/* VFS Controller _readBlock, _readINode and similar calls utilize IDBDriver for persistent file operations */
async open(path: string) {
const { parent, name } = await this._resolvePath(path);
const parentDir = this._decodeDirBlock(await this._readBlock(parent.id));
let node: INode;
if(parentDir[name] === undefined) {
node = await this._createFile(parent, name, FileType.FILE);
} else {
node = await this._readINode(parentDir[name]);
if(node.type !== FileType.FILE) {
throw new Error(`${name} is not a file`);
}
}
return { id: this._fdCounter++, inode: node.id, position: 0 } as FileDescriptor;
}
async read(fd: FileDescriptor, length: number): Promise<Uint8Array> {
const inode = await this._readINode(fd.inode);
if(inode.type !== FileType.FILE) {
throw new Error("Cannot read from a directory");
}
const block = await this._readBlock(inode.id);
const data = block.slice(fd.position, fd.position + length);
fd.position += data.length;
return data;
}
/* IndexedDB driver */
async read(store: string, key: IDBValidKey) {
return new Promise<any>((resolve, reject) => {
const transaction = this._db.transaction([store], "readonly");
const objectStore = transaction.objectStore(store);
const request = objectStore.get(key);
request.onerror = (event) => {
reject(new Error(`IndexedDB error: ${request.error}`));
};
request.onsuccess = (event) => {
resolve(request.result);
};
});
}System Design Lessons from yOS
This project offers a unique perspective on system design, demonstrating that fundamental operating system concepts can be applied and implemented within resource-constrained or unconventional environments like a web browser. It challenges engineers to think about how core OS services can be abstracted and delivered using widely available web technologies.