- 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:
Vendored
+3
-3
@@ -13,12 +13,12 @@
|
|||||||
"state": {
|
"state": {
|
||||||
"type": "markdown",
|
"type": "markdown",
|
||||||
"state": {
|
"state": {
|
||||||
"file": "todo.md",
|
"file": "README.md",
|
||||||
"mode": "source",
|
"mode": "source",
|
||||||
"source": false
|
"source": false
|
||||||
},
|
},
|
||||||
"icon": "lucide-file",
|
"icon": "lucide-file",
|
||||||
"title": "todo"
|
"title": "README"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
@@ -188,7 +188,7 @@
|
|||||||
"bases:Create new base": false
|
"bases:Create new base": false
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"active": "af4cfba92021d1c8",
|
"active": "1db1d6b317bc05ae",
|
||||||
"lastOpenFiles": [
|
"lastOpenFiles": [
|
||||||
"src/util/posix.ts",
|
"src/util/posix.ts",
|
||||||
"src/core/util/sort.ts"
|
"src/core/util/sort.ts"
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ import {
|
|||||||
type PermissionDeniedError,
|
type PermissionDeniedError,
|
||||||
type ShellError,
|
type ShellError,
|
||||||
} from "@core/util/errors";
|
} from "@core/util/errors";
|
||||||
import { resolveInode, resolvePath } from "@core/util/path_resolver";
|
import { resolvePath } from "@core/util/path_resolver";
|
||||||
|
|
||||||
export const COMMAND_NAME = "cd";
|
export const COMMAND_NAME = "cd";
|
||||||
|
|
||||||
@@ -58,12 +58,7 @@ async function main(
|
|||||||
if (state.filesystem.isFile(inode)) {
|
if (state.filesystem.isFile(inode)) {
|
||||||
return {
|
return {
|
||||||
state,
|
state,
|
||||||
result: createErrorResult(
|
result: createErrorResult("PathResolutionError", "Not a directory", { path: args[0]! }, COMMAND_NAME),
|
||||||
"PathResolutionError",
|
|
||||||
"No such file or directory",
|
|
||||||
{ path: resolveInode(state.filesystem, inode) },
|
|
||||||
COMMAND_NAME,
|
|
||||||
),
|
|
||||||
} satisfies CommandOutput<PathResolutionError>;
|
} satisfies CommandOutput<PathResolutionError>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+4
-31
@@ -5,6 +5,7 @@ import type { ShellState } from "@core/shell_state";
|
|||||||
import { SIGNAL_MAP, type Signal } from "@core/signal";
|
import { SIGNAL_MAP, type Signal } from "@core/signal";
|
||||||
import type { ShellError, SignalTerminationError } from "@core/util/errors";
|
import type { ShellError, SignalTerminationError } from "@core/util/errors";
|
||||||
import { createImmutableBuffer } from "@core/util/immutable_buffer";
|
import { createImmutableBuffer } from "@core/util/immutable_buffer";
|
||||||
|
import { concatUint8Arrays, splitUint8Array } from "@termos/util/concat";
|
||||||
import { EventEmitter } from "./event_emitter";
|
import { EventEmitter } from "./event_emitter";
|
||||||
import type { ShellEventMap } from "./shell";
|
import type { ShellEventMap } from "./shell";
|
||||||
|
|
||||||
@@ -36,34 +37,6 @@ export class Process {
|
|||||||
this._EventEmitter = new EventEmitter<ShellEventMap>();
|
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 {
|
private writeToOutput(chunks: Uint8Array[], eventType: "stdout" | "stderr", data: Uint8Array): void {
|
||||||
if (this._Settled) return;
|
if (this._Settled) return;
|
||||||
|
|
||||||
@@ -72,7 +45,7 @@ export class Process {
|
|||||||
this._EventEmitter.emit(eventType, { pid: this.PID, data });
|
this._EventEmitter.emit(eventType, { pid: this.PID, data });
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
const arr = this.divideToChunks(data);
|
const arr = splitUint8Array(data, this._MaxChunkByteSize);
|
||||||
for (const ent of arr) {
|
for (const ent of arr) {
|
||||||
chunks.push(ent);
|
chunks.push(ent);
|
||||||
this._EventEmitter.emit(eventType, { pid: this.PID, data: ent });
|
this._EventEmitter.emit(eventType, { pid: this.PID, data: ent });
|
||||||
@@ -102,8 +75,8 @@ export class Process {
|
|||||||
if (this._Settled) return;
|
if (this._Settled) return;
|
||||||
|
|
||||||
let result: ResultSuccess | ResultError<ShellError>;
|
let result: ResultSuccess | ResultError<ShellError>;
|
||||||
const stdout: Uint8Array = this.concatUint8Arrays(this._StdoutChunks);
|
const stdout: Uint8Array = concatUint8Arrays(this._StdoutChunks);
|
||||||
const stderr: Uint8Array = this.concatUint8Arrays(this._StderrChunks);
|
const stderr: Uint8Array = concatUint8Arrays(this._StderrChunks);
|
||||||
|
|
||||||
if (output.result.type === "success") {
|
if (output.result.type === "success") {
|
||||||
result = createSuccessResult(
|
result = createSuccessResult(
|
||||||
|
|||||||
@@ -33,7 +33,9 @@ const DirectoryNodeSchema = z.object({
|
|||||||
|
|
||||||
const TreeNodeSchema = z.union([FileNodeSchema, DirectoryNodeSchema]);
|
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> {
|
export function deserializeFileSystem(json: string): ReadonlyMap<number, TreeNode> {
|
||||||
const raw = JSON.parse(json);
|
const raw = JSON.parse(json);
|
||||||
|
|||||||
@@ -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;
|
||||||
|
}
|
||||||
@@ -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);
|
|
||||||
});
|
|
||||||
@@ -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));
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user