some bs work, not finished (reference notes board on email for info)

This commit is contained in:
2026-09-09 10:43:37 +02:00
parent e2ade5de29
commit ec13299e95
5 changed files with 212 additions and 21 deletions
+14 -7
View File
@@ -3,24 +3,31 @@ import type { ShellState } from "@core/shell_state";
import type { NodeNotFoundError, NotADirectoryError } from "../filesystem/errors";
import { TreeNodeType, type DirectoryNode, type TreeNode } from "../filesystem/treenode";
/*
/home/grzegorz/Documents/homework
""
*/
export function resolvePath(state: ShellState, path: string): number {
const session = getCurrentSession(state);
const isAbsolute = path.startsWith("/");
const isAbsolute = path.startsWith("/"); // true
const normalizedPath: string = path.replace(/^\/+|\/+$/g, "");
const pathComponents: string[] = normalizedPath.split("/").filter(name => name.length > 0);
const normalizedPath: string = path.replace(/^\/+|\/+$/g, ""); // home/grzegorz/Documents/homework
const pathComponents: string[] = normalizedPath.split("/").filter(name => name.length > 0); // ["home", "grzegorz", "Documents", "homework"]
// Fix to dynamically get the root inode VV
if (pathComponents.length === 0 && isAbsolute) return 0;
// Move behaviour into cd built-in
if (pathComponents.length === 0) return session.cwd;
let currentNode = isAbsolute
? state.filesystem.getRoot()
: (state.filesystem.getNode(session.cwd) as DirectoryNode);
let currentNode =
isAbsolute === true ? state.filesystem.getRoot() : (state.filesystem.getNode(session.cwd) as DirectoryNode);
if (!currentNode) return -1;
for (let i = 0; i < pathComponents.length; i++) {
const name = pathComponents[i]!;
const name: string = pathComponents[i]!;
if (name === ".") continue;
if (name === "..") {
+131
View File
@@ -0,0 +1,131 @@
export class ReadableStream {
private _threshold: number;
private _buffer: Uint8Array[] = [];
private _state: "idle" | "open" | "waiting" | "closed" | "errored" = "idle";
private _pending: ((value: Uint8Array | null) => void) | null = null;
private _byteLength: number = 0;
private _error: Error | null = null;
// Default - 32 chunks of 64KB -> Buffer of 2MB
constructor(threshold: number = 1024 * 64 * 32) {
this._threshold = threshold;
}
public push(chunk: Uint8Array): boolean {
if (this._state === "closed" || this._state === "errored") {
return false;
}
if (this._pending) {
const pending = this._pending;
this._pending = null;
pending(chunk);
this._state = this._buffer.length > 0 ? "open" : "idle";
return true;
}
this._buffer.push(chunk);
this._byteLength += chunk.byteLength;
this._state = "open";
return this._byteLength < this._threshold;
}
public close(): void {
if (this._state === "errored") return;
this._state = "closed";
if (this._pending) {
this._pending(null);
this._pending = null;
}
}
public error(err: Error): void {
this._error = err;
this._state = "errored";
if (this._pending) {
this._pending(null);
this._pending = null;
}
this._buffer = [];
this._byteLength = 0;
}
public read(): Promise<Uint8Array | null> {
if ((this._state = "errored")) {
return Promise.reject(this._error || new Error("Stream encountered an error"));
}
if (this._buffer.length > 0) {
const chunk: Uint8Array = this._buffer.shift()!;
this._byteLength -= chunk.byteLength;
if (this._buffer.length === 0) {
this._state = "idle";
}
return Promise.resolve(chunk);
}
if ((this._state = "closed")) {
return Promise.resolve(null);
}
this._state = "waiting";
return new Promise((resolve, reject) => {
this._pending = resolve;
if (this._error) {
reject(this._error);
this._pending = null;
}
});
}
public pipe(writable: WritableStream): Promise<void> {
return new Promise(async (resolve, reject) => {
try {
for await (const chunk of this) {
await writable.write(chunk);
}
await writable.close();
resolve();
} catch (err) {
reject(err);
}
});
}
async *[Symbol.asyncIterator](): AsyncIterator<Uint8Array> {
while (true) {
const chunk = await this.read();
if (chunk === null) break;
yield chunk;
}
}
public get byteLength(): number {
return this._byteLength;
}
public get isFull(): boolean {
return this._byteLength >= this._threshold;
}
public get state(): string {
return this._state;
}
}
export class WritableStream {
write(chunk: Uint8Array): void {}
close(): void {}
}
export class DuplexStream {}
export class TransformStream {}