From 3ec3de71f45412b9b571ca7ecd63b919f32d666c Mon Sep 17 00:00:00 2001 From: orion Date: Wed, 12 Aug 2026 11:01:07 +0200 Subject: [PATCH] shell work, json deserializer working, not fully tested but should produce a correct base map --- package-lock.json | 34 +++++++++----- package.json | 3 ++ src/config.ts | 9 ++++ src/index.ts | 13 ++++++ src/shell/db_manager.ts | 12 +++++ src/shell/serializer.ts | 88 +++++++++++++++++++++++++++++++++++ src/shell/shell.ts | 9 +++- test/shell/serializer.test.ts | 9 ++++ 8 files changed, 164 insertions(+), 13 deletions(-) create mode 100644 src/config.ts create mode 100644 src/shell/serializer.ts create mode 100644 test/shell/serializer.test.ts diff --git a/package-lock.json b/package-lock.json index 3a44f60..bdef4db 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8,6 +8,9 @@ "name": "termos", "version": "1.0.0", "license": "ISC", + "dependencies": { + "zod": "^4.4.3" + }, "devDependencies": { "@eslint/js": "^10.0.1", "@types/node": "^26.1.0", @@ -1494,16 +1497,16 @@ } }, "node_modules/brace-expansion": { - "version": "5.0.7", - "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-5.0.7.tgz", - "integrity": "sha512-7oFy703dxfY3/NLxC1fh2SUCQ0H9rmAY+5EpDVfXjUTTs+HEwR2nYaqLv+GWcTsumwxPfiz6CzCNkwXwBUwqCA==", + "version": "5.0.9", + "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-5.0.9.tgz", + "integrity": "sha512-ScQ4IuvIEF1TMlP7Zt+vjJ//9zlPb2SDcxWxM3bk8s6t6GGdJ7KO1dCcTidOPJKePW30LE/2cT7wCyPho9/Wxg==", "dev": true, "license": "MIT", "dependencies": { "balanced-match": "^4.0.2" }, "engines": { - "node": "18 || 20 || >=22" + "node": "20 || >=22" } }, "node_modules/chai": { @@ -2493,9 +2496,9 @@ "license": "MIT" }, "node_modules/nanoid": { - "version": "3.3.15", - "resolved": "https://registry.npmjs.org/nanoid/-/nanoid-3.3.15.tgz", - "integrity": "sha512-y7Wygv/7mEOvxTuEQDB8StXdMRBWf1kR/tlhAzBRUFkB2jfcLOAxO/SHmOO2zgz1pVgK29/kyupn059/bCHdjA==", + "version": "3.3.18", + "resolved": "https://registry.npmjs.org/nanoid/-/nanoid-3.3.18.tgz", + "integrity": "sha512-DTg4MJbGMWkfi6VZFdNt2/caMbQy4Ou+Op/hJQvGEWcnVfoA1QA+xzRKAzw9jD6+GVOOeYr/mIcuDSdug6F6+w==", "dev": true, "funding": [ { @@ -2630,9 +2633,9 @@ } }, "node_modules/postcss": { - "version": "8.5.16", - "resolved": "https://registry.npmjs.org/postcss/-/postcss-8.5.16.tgz", - "integrity": "sha512-vuwillviilfKZsg0VGj5R/YwwcHx4SLsIOI/7K6mQkWx+l5cUHTjj5g0AasTBcyXsbfTgrwsUNmVUb5xVwyPwg==", + "version": "8.5.26", + "resolved": "https://registry.npmjs.org/postcss/-/postcss-8.5.26.tgz", + "integrity": "sha512-u82N74LFzG8ca+dD8puPnplTXoGH4fTPpVGuIbt36G3qvNlkvfD0lEAZSxaly3KX8TS/L1A1gsCEmvKmBcVbkQ==", "dev": true, "funding": [ { @@ -2650,7 +2653,7 @@ ], "license": "MIT", "dependencies": { - "nanoid": "^3.3.12", + "nanoid": "^3.3.17", "picocolors": "^1.1.1", "source-map-js": "^1.2.1" }, @@ -3260,6 +3263,15 @@ "funding": { "url": "https://github.com/sponsors/sindresorhus" } + }, + "node_modules/zod": { + "version": "4.4.3", + "resolved": "https://registry.npmjs.org/zod/-/zod-4.4.3.tgz", + "integrity": "sha512-ytENFjIJFl2UwYglde2jchW2Hwm4GJFLDiSXWdTrJQBIN9Fcyp7n4DhxJEiWNAJMV1/BqWfW/kkg71UDcHJyTQ==", + "license": "MIT", + "funding": { + "url": "https://github.com/sponsors/colinhacks" + } } } } diff --git a/package.json b/package.json index 62a8782..f8594ae 100644 --- a/package.json +++ b/package.json @@ -26,5 +26,8 @@ "typescript-eslint": "^8.63.0", "vite-tsconfig-paths": "^6.1.1", "vitest": "^4.1.10" + }, + "dependencies": { + "zod": "^4.4.3" } } diff --git a/src/config.ts b/src/config.ts new file mode 100644 index 0000000..404a3fe --- /dev/null +++ b/src/config.ts @@ -0,0 +1,9 @@ +export interface TermosConfig { + serverAddress: string; + dbName: string; +} + +export const defaultConfig: TermosConfig = { + serverAddress: "", + dbName: "termos", +}; diff --git a/src/index.ts b/src/index.ts index 824e1d9..9fc19e0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,2 +1,15 @@ +import { defaultConfig, TermosConfig } from "./config"; + +let config: TermosConfig = { ...defaultConfig }; + +namespace Termos { + export function init(config: Partial) { + config = { ...defaultConfig, ...config}; + console.log("termos initialized"); + } +} + const message: string = "Hello, World!"; console.log(message); + +export { Termos }; diff --git a/src/shell/db_manager.ts b/src/shell/db_manager.ts index de17c3b..c6c7493 100644 --- a/src/shell/db_manager.ts +++ b/src/shell/db_manager.ts @@ -3,3 +3,15 @@ To do that, implement a termos config that will contain the table and db names, aswell as other relevant information - maybe a flag for headless mode (uses the built-in console frontend module - need to evaluate this idea more) */ + +export async fetchFileSystem(): Promise { + +} + +export async fetchUserGroups(): Promise { + +} + +export async fetchUsers(): Promise { + +} diff --git a/src/shell/serializer.ts b/src/shell/serializer.ts new file mode 100644 index 0000000..7f8317e --- /dev/null +++ b/src/shell/serializer.ts @@ -0,0 +1,88 @@ +import { createImmutableBuffer } from "@core/util/immutable_buffer"; +import { z } from "zod"; +import { TreeNodeType, type DirectoryNode, type FileNode, type TreeNode } from "../core/filesystem/treenode"; + +const FileNodeSchema = z.object({ + inode: z.number(), + name: z.string(), + mode: z.number(), + parent: z.number(), + owner: z.number(), + group: z.number(), + mtime: z.number(), + ctime: z.number(), + atime: z.number(), + type: z.literal("regular"), + content: z.array(z.number()), +}); + +const DirectoryNodeSchema = z.object({ + inode: z.number(), + name: z.string(), + mode: z.number(), + parent: z.number(), + owner: z.number(), + group: z.number(), + mtime: z.number(), + ctime: z.number(), + atime: z.number(), + type: z.literal("directory"), + children: z.array(z.lazy((): any => TreeNodeSchema)), +}); + +const TreeNodeSchema = z.union([FileNodeSchema, DirectoryNodeSchema]); + +export function serializeFileSystem() {} + +export function deserializeFileSystem(json: string): ReadonlyMap { + const raw = JSON.parse(json); + const validated = TreeNodeSchema.parse(raw); + + const map = new Map(); + + function parseNode(tree: any, parent: number): void { + if (tree.type === "directory") { + const children: Record = {}; + + for (const child of tree.children) { + parseNode(child, tree.inode); + children[child.name] = child.inode; + } + + const directoryNode: DirectoryNode = { + type: TreeNodeType.Directory, + inode: tree.inode, + name: tree.name, + parent, + mode: tree.mode, + owner: tree.owner, + group: tree.group, + mtime: tree.mtime, + ctime: tree.ctime, + atime: tree.atime, + children, + }; + map.set(tree.inode, Object.freeze(directoryNode)); + } else { + const fileNode: FileNode = { + type: TreeNodeType.File, + inode: tree.inode, + name: tree.name, + parent, + mode: tree.mode, + owner: tree.owner, + group: tree.group, + mtime: tree.mtime, + ctime: tree.ctime, + atime: tree.atime, + content: createImmutableBuffer(new Uint8Array()), + }; + map.set(tree.inode, Object.freeze(fileNode)); + } + } + + parseNode(validated, validated.inode); + const base: ReadonlyMap = map; + + return base; +} diff --git a/src/shell/shell.ts b/src/shell/shell.ts index 7739b88..ea62a65 100644 --- a/src/shell/shell.ts +++ b/src/shell/shell.ts @@ -1,5 +1,6 @@ +import { FileSystem } from "@core/filesystem/filesystem"; import type { Result } from "@core/result"; -import { type ShellState } from "@core/shell_state"; +import { createInitialShellState, type ShellState } from "@core/shell_state"; import type { ShellError } from "@core/util/errors"; export class Shell { @@ -19,7 +20,11 @@ export class Shell { if (Shell.#promise) return Shell.#promise; - const state; //Create initial shell state, + const fs = FileSystem.init() + const users = + const sessionStack = + + const state: ShellState = createInitialShellState(); //Create initial shell state, Shell.#promise = (async () => { try { diff --git a/test/shell/serializer.test.ts b/test/shell/serializer.test.ts new file mode 100644 index 0000000..5c5667f --- /dev/null +++ b/test/shell/serializer.test.ts @@ -0,0 +1,9 @@ +import { deserializeFileSystem } from "@shell/serializer"; +import { test } from "vitest"; + +const rawJson = `{"atime": 1786341244,"children": [{"atime": 1786341264,"children": [{"atime": 1786341330,"children": [],"content": null,"ctime": 1786341330,"group": 1000,"inode": 2,"mode": 493,"mtime": 1786341330,"name": "subdir1","owner": 1000,"parent": 1,"type": "directory"}],"content": null,"ctime": 1786341330,"group": 1000,"inode": 1,"mode": 493,"mtime": 1786341330,"name": "folder1","owner": 1000,"parent": 0,"type": "directory"},{"atime": 1786341269,"children": [{"atime": 1786341344,"children": [],"content": null,"ctime": 1786341344,"group": 1000,"inode": 4,"mode": 493,"mtime": 1786341344,"name": "subdir2","owner": 1000,"parent": 3,"type": "directory"},{"atime": 1786341345,"children": [{"atime": 1786341356,"children": [],"content": [],"ctime": 1786341356,"group": 1000,"inode": 6,"mode": 420,"mtime": 1786341356,"name": "empty-foo","owner": 1000,"parent": 5,"type": "regular"}],"content": null,"ctime": 1786341356,"group": 1000,"inode": 5,"mode": 493,"mtime": 1786341356,"name": "subdir3","owner": 1000,"parent": 3,"type": "directory"}],"content": null,"ctime": 1786341345,"group": 1000,"inode": 3,"mode": 493,"mtime": 1786341345,"name": "folder2","owner": 1000,"parent": 0,"type": "directory"},{"atime": 1786341312,"children": [],"content": [72,101,108,108,111,44,32,87,111,114,108,100,33,10],"ctime": 1786341312,"group": 1000,"inode": 7,"mode": 420,"mtime": 1786341312,"name": "some-file","owner": 1000,"parent": 0,"type": "regular"}],"content": null,"ctime": 1786341312,"group": 1000,"inode": 0,"mode": 493,"mtime": 1786341312,"name": "root","owner": 1000,"parent": 0,"type": "directory"}`; + +test("deserialize json into the correct tree structure", () => { + const base = deserializeFileSystem(rawJson); + console.log(base); +});