- moved uint8array concat and split functions to their own files

- fixed an error in the cd command as a result of a previous improvement
This commit is contained in:
2026-08-30 08:38:05 +02:00
parent f1b69bb282
commit d0f8376244
7 changed files with 58 additions and 69 deletions
+3 -3
View File
@@ -13,12 +13,12 @@
"state": {
"type": "markdown",
"state": {
"file": "todo.md",
"file": "README.md",
"mode": "source",
"source": false
},
"icon": "lucide-file",
"title": "todo"
"title": "README"
}
}
]
@@ -188,7 +188,7 @@
"bases:Create new base": false
}
},
"active": "af4cfba92021d1c8",
"active": "1db1d6b317bc05ae",
"lastOpenFiles": [
"src/util/posix.ts",
"src/core/util/sort.ts"
+2 -7
View File
@@ -10,7 +10,7 @@ import {
type PermissionDeniedError,
type ShellError,
} from "@core/util/errors";
import { resolveInode, resolvePath } from "@core/util/path_resolver";
import { resolvePath } from "@core/util/path_resolver";
export const COMMAND_NAME = "cd";
@@ -58,12 +58,7 @@ async function main(
if (state.filesystem.isFile(inode)) {
return {
state,
result: createErrorResult(
"PathResolutionError",
"No such file or directory",
{ path: resolveInode(state.filesystem, inode) },
COMMAND_NAME,
),
result: createErrorResult("PathResolutionError", "Not a directory", { path: args[0]! }, COMMAND_NAME),
} satisfies CommandOutput<PathResolutionError>;
}
+4 -31
View File
@@ -5,6 +5,7 @@ import type { ShellState } from "@core/shell_state";
import { SIGNAL_MAP, type Signal } from "@core/signal";
import type { ShellError, SignalTerminationError } from "@core/util/errors";
import { createImmutableBuffer } from "@core/util/immutable_buffer";
import { concatUint8Arrays, splitUint8Array } from "@termos/util/concat";
import { EventEmitter } from "./event_emitter";
import type { ShellEventMap } from "./shell";
@@ -36,34 +37,6 @@ export class Process {
this._EventEmitter = new EventEmitter<ShellEventMap>();
}
private concatUint8Arrays(arrays: Uint8Array[]): Uint8Array {
const size = arrays.reduce((sum, arr) => sum + arr.length, 0);
const result = new Uint8Array(size);
let offset = 0;
for (const arr of arrays) {
result.set(arr, offset);
offset += arr.length;
}
return result;
}
// Tested
private divideToChunks(data: Uint8Array): Uint8Array[] {
const nChunks: number = Math.ceil(data.length / this._MaxChunkByteSize);
const chunks: Uint8Array[] = [];
for (let i = 0; i < nChunks; i++) {
const start = this._MaxChunkByteSize * i;
const end = this._MaxChunkByteSize * (i + 1);
chunks.push(data.subarray(start, end));
}
return chunks;
}
private writeToOutput(chunks: Uint8Array[], eventType: "stdout" | "stderr", data: Uint8Array): void {
if (this._Settled) return;
@@ -72,7 +45,7 @@ export class Process {
this._EventEmitter.emit(eventType, { pid: this.PID, data });
return;
} else {
const arr = this.divideToChunks(data);
const arr = splitUint8Array(data, this._MaxChunkByteSize);
for (const ent of arr) {
chunks.push(ent);
this._EventEmitter.emit(eventType, { pid: this.PID, data: ent });
@@ -102,8 +75,8 @@ export class Process {
if (this._Settled) return;
let result: ResultSuccess | ResultError<ShellError>;
const stdout: Uint8Array = this.concatUint8Arrays(this._StdoutChunks);
const stderr: Uint8Array = this.concatUint8Arrays(this._StderrChunks);
const stdout: Uint8Array = concatUint8Arrays(this._StdoutChunks);
const stderr: Uint8Array = concatUint8Arrays(this._StderrChunks);
if (output.result.type === "success") {
result = createSuccessResult(
+3 -1
View File
@@ -33,7 +33,9 @@ const DirectoryNodeSchema = z.object({
const TreeNodeSchema = z.union([FileNodeSchema, DirectoryNodeSchema]);
export function serializeFileSystem() {}
/* export function serializeFileSystem(map: ReadonlyMap<number, TreeNode | null>): string {
} */
export function deserializeFileSystem(json: string): ReadonlyMap<number, TreeNode> {
const raw = JSON.parse(json);
+26
View File
@@ -0,0 +1,26 @@
export function concatUint8Arrays(arrays: Uint8Array[]): Uint8Array {
const size = arrays.reduce((sum, arr) => sum + arr.length, 0);
const array = new Uint8Array(size);
let offset = 0;
for (const arr of arrays) {
array.set(arr, offset);
offset += arr.length;
}
return array;
}
export function splitUint8Array(data: Uint8Array, size: number): Uint8Array[] {
const nChunks: number = Math.ceil(data.length / size);
const chunks: Uint8Array[] = [];
for (let i = 0; i < nChunks; i++) {
const start = size * i;
const end = size * (i + 1);
chunks.push(data.subarray(start, end));
}
return chunks;
}
-27
View File
@@ -1,27 +0,0 @@
import { expect, test } from "vitest";
function divideToChunks(data: Uint8Array): Uint8Array[] {
const nChunks: number = Math.ceil(data.length / 1024);
const chunks: Uint8Array[] = [];
for (let i = 0; i < nChunks; i++) {
const start = 1024 * i;
const end = 1024 * (i + 1);
chunks.push(data.slice(start, end));
}
return chunks;
}
test("divide to chunks", () => {
const data: Uint8Array = new Uint8Array(2048);
const result: Uint8Array[] = divideToChunks(data);
expect(result.length).toEqual(2);
});
test("divide to chunks", () => {
const data: Uint8Array = new Uint8Array(2049);
const result = divideToChunks(data);
expect(result.length).toEqual(3);
});
+20
View File
@@ -0,0 +1,20 @@
import { splitUint8Array } from "@termos/util/concat";
import { expect, test } from "vitest";
test("divide to chunks", () => {
const data: Uint8Array = new Uint8Array(2048);
const result = splitUint8Array(data, 1024);
expect(result.length).toEqual(2);
});
test("divide to chunks", () => {
const data: Uint8Array = new Uint8Array(2049);
const result = splitUint8Array(data, 1024);
expect(result.length).toEqual(3);
});
test("divide to chunks", () => {
const data: Uint8Array = new Uint8Array(2048);
const result = splitUint8Array(data, 53);
expect(result.length).toEqual(Math.ceil(2048 / 53));
});