Compare commits
60
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e2ade5de29 | ||
|
|
b75e691a0b | ||
|
|
776a93e21b | ||
|
|
6cc9727091 | ||
|
|
2715e5e28e | ||
|
|
8a349330b0 | ||
|
|
2e9227f6d8 | ||
|
|
8e054a220d | ||
|
|
4776232377 | ||
|
|
d0f8376244 | ||
|
|
f1b69bb282 | ||
|
|
1b3314ab87 | ||
|
|
96e15a87f2 | ||
|
|
55b5d3a873 | ||
|
|
1ee2a2fe7a | ||
|
|
310218663d | ||
|
|
20718604e4 | ||
|
|
dad18cd4cc | ||
|
|
48aac3c883 | ||
|
|
268729bb62 | ||
|
|
e313aa5b9d | ||
|
|
16a1805f25 | ||
|
|
075c999435 | ||
|
|
dfa393b4d9 | ||
|
|
c39bbae091 | ||
|
|
a7798436d2 | ||
|
|
3ec3de71f4 | ||
|
|
0149937526 | ||
|
|
394e0b9e7c | ||
|
|
d26d7d35ac | ||
|
|
3e6d607e18 | ||
|
|
5d197b4513 | ||
|
|
6bbaaf60e6 | ||
|
|
7348d3723e | ||
|
|
8c0a7e7a6c | ||
|
|
f87aa4eb85 | ||
|
|
3b50214041 | ||
|
|
b9a27cea2a | ||
|
|
cd08a4bf32 | ||
|
|
994b93ae47 | ||
|
|
0fce10895f | ||
|
|
b63bae54aa | ||
|
|
355a8a4707 | ||
|
|
4cd70eda33 | ||
|
|
a67d060fc6 | ||
|
|
cab18b74cf | ||
|
|
0a477855c7 | ||
|
|
2affb3ceb2 | ||
|
|
f2d2186086 | ||
|
|
7cf60208e0 | ||
|
|
46f9e05d63 | ||
|
|
e89cec47d9 | ||
|
|
c7dc8f6c37 | ||
|
|
9bf481cc33 | ||
|
|
9f23e5a86a | ||
|
|
15bd160254 | ||
|
|
04b66fa62a | ||
|
|
b6365d6221 | ||
|
|
ea82327ee8 | ||
|
|
134d989f6e |
Vendored
+1
@@ -0,0 +1 @@
|
|||||||
|
{}
|
||||||
Vendored
+1
@@ -0,0 +1 @@
|
|||||||
|
{}
|
||||||
Vendored
+33
@@ -0,0 +1,33 @@
|
|||||||
|
{
|
||||||
|
"file-explorer": true,
|
||||||
|
"global-search": true,
|
||||||
|
"switcher": true,
|
||||||
|
"graph": true,
|
||||||
|
"backlink": true,
|
||||||
|
"canvas": true,
|
||||||
|
"outgoing-link": true,
|
||||||
|
"tag-pane": true,
|
||||||
|
"footnotes": false,
|
||||||
|
"properties": true,
|
||||||
|
"page-preview": true,
|
||||||
|
"daily-notes": true,
|
||||||
|
"templates": true,
|
||||||
|
"note-composer": true,
|
||||||
|
"command-palette": true,
|
||||||
|
"slash-command": false,
|
||||||
|
"editor-status": true,
|
||||||
|
"bookmarks": true,
|
||||||
|
"markdown-importer": false,
|
||||||
|
"zk-prefixer": false,
|
||||||
|
"random-note": false,
|
||||||
|
"outline": true,
|
||||||
|
"word-count": true,
|
||||||
|
"slides": false,
|
||||||
|
"audio-recorder": false,
|
||||||
|
"workspaces": false,
|
||||||
|
"file-recovery": true,
|
||||||
|
"publish": false,
|
||||||
|
"sync": true,
|
||||||
|
"bases": true,
|
||||||
|
"webviewer": false
|
||||||
|
}
|
||||||
Vendored
+196
@@ -0,0 +1,196 @@
|
|||||||
|
{
|
||||||
|
"main": {
|
||||||
|
"id": "89ee32072433406f",
|
||||||
|
"type": "split",
|
||||||
|
"children": [
|
||||||
|
{
|
||||||
|
"id": "21d78959e637e988",
|
||||||
|
"type": "tabs",
|
||||||
|
"children": [
|
||||||
|
{
|
||||||
|
"id": "1db1d6b317bc05ae",
|
||||||
|
"type": "leaf",
|
||||||
|
"state": {
|
||||||
|
"type": "markdown",
|
||||||
|
"state": {
|
||||||
|
"file": "README.md",
|
||||||
|
"mode": "source",
|
||||||
|
"source": false
|
||||||
|
},
|
||||||
|
"icon": "lucide-file",
|
||||||
|
"title": "README"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"direction": "vertical"
|
||||||
|
},
|
||||||
|
"left": {
|
||||||
|
"id": "6703a701fa3161c8",
|
||||||
|
"type": "split",
|
||||||
|
"children": [
|
||||||
|
{
|
||||||
|
"id": "59a1957ef4b6aa34",
|
||||||
|
"type": "tabs",
|
||||||
|
"children": [
|
||||||
|
{
|
||||||
|
"id": "af4cfba92021d1c8",
|
||||||
|
"type": "leaf",
|
||||||
|
"state": {
|
||||||
|
"type": "file-explorer",
|
||||||
|
"state": {
|
||||||
|
"sortOrder": "alphabetical",
|
||||||
|
"autoReveal": false,
|
||||||
|
"showSearch": false,
|
||||||
|
"searchQuery": ""
|
||||||
|
},
|
||||||
|
"icon": "lucide-folder-closed",
|
||||||
|
"title": "Files"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "195ed2a7004c5679",
|
||||||
|
"type": "leaf",
|
||||||
|
"state": {
|
||||||
|
"type": "search",
|
||||||
|
"state": {
|
||||||
|
"query": "",
|
||||||
|
"matchingCase": false,
|
||||||
|
"explainSearch": false,
|
||||||
|
"collapseAll": false,
|
||||||
|
"extraContext": false,
|
||||||
|
"sortOrder": "alphabetical"
|
||||||
|
},
|
||||||
|
"icon": "lucide-search",
|
||||||
|
"title": "Search"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "e0b0a93bee04adf7",
|
||||||
|
"type": "leaf",
|
||||||
|
"state": {
|
||||||
|
"type": "bookmarks",
|
||||||
|
"state": {
|
||||||
|
"showSearch": false,
|
||||||
|
"searchQuery": ""
|
||||||
|
},
|
||||||
|
"icon": "lucide-bookmark",
|
||||||
|
"title": "Bookmarks"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"direction": "horizontal",
|
||||||
|
"width": 300
|
||||||
|
},
|
||||||
|
"right": {
|
||||||
|
"id": "cdb5787bc7b074a1",
|
||||||
|
"type": "split",
|
||||||
|
"children": [
|
||||||
|
{
|
||||||
|
"id": "4f7b084c90edcafa",
|
||||||
|
"type": "tabs",
|
||||||
|
"children": [
|
||||||
|
{
|
||||||
|
"id": "87c20e5388b56742",
|
||||||
|
"type": "leaf",
|
||||||
|
"state": {
|
||||||
|
"type": "backlink",
|
||||||
|
"state": {
|
||||||
|
"file": "todo.md",
|
||||||
|
"collapseAll": false,
|
||||||
|
"extraContext": false,
|
||||||
|
"sortOrder": "alphabetical",
|
||||||
|
"showSearch": false,
|
||||||
|
"searchQuery": "",
|
||||||
|
"backlinkCollapsed": false,
|
||||||
|
"unlinkedCollapsed": true
|
||||||
|
},
|
||||||
|
"icon": "links-coming-in",
|
||||||
|
"title": "Backlinks"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "d06f3a18f218bb85",
|
||||||
|
"type": "leaf",
|
||||||
|
"state": {
|
||||||
|
"type": "outgoing-link",
|
||||||
|
"state": {
|
||||||
|
"file": "todo.md",
|
||||||
|
"linksCollapsed": false,
|
||||||
|
"unlinkedCollapsed": true
|
||||||
|
},
|
||||||
|
"icon": "links-going-out",
|
||||||
|
"title": "Outgoing links"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "5e8256b0c8cb4527",
|
||||||
|
"type": "leaf",
|
||||||
|
"state": {
|
||||||
|
"type": "tag",
|
||||||
|
"state": {
|
||||||
|
"sortOrder": "frequency",
|
||||||
|
"useHierarchy": true,
|
||||||
|
"showSearch": false,
|
||||||
|
"searchQuery": ""
|
||||||
|
},
|
||||||
|
"icon": "lucide-tags",
|
||||||
|
"title": "Tags"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "c4c977be0ce05b9d",
|
||||||
|
"type": "leaf",
|
||||||
|
"state": {
|
||||||
|
"type": "all-properties",
|
||||||
|
"state": {
|
||||||
|
"sortOrder": "frequency",
|
||||||
|
"showSearch": false,
|
||||||
|
"searchQuery": ""
|
||||||
|
},
|
||||||
|
"icon": "lucide-archive",
|
||||||
|
"title": "All properties"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "bf5df26d52eb2371",
|
||||||
|
"type": "leaf",
|
||||||
|
"state": {
|
||||||
|
"type": "outline",
|
||||||
|
"state": {
|
||||||
|
"file": "todo.md",
|
||||||
|
"followCursor": false,
|
||||||
|
"showSearch": false,
|
||||||
|
"searchQuery": ""
|
||||||
|
},
|
||||||
|
"icon": "lucide-list",
|
||||||
|
"title": "Outline"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"direction": "horizontal",
|
||||||
|
"width": 300,
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"left-ribbon": {
|
||||||
|
"hiddenItems": {
|
||||||
|
"switcher:Open quick switcher": false,
|
||||||
|
"graph:Open graph view": false,
|
||||||
|
"canvas:Create new canvas": false,
|
||||||
|
"daily-notes:Open today's daily note": false,
|
||||||
|
"templates:Insert template": false,
|
||||||
|
"command-palette:Open command palette": false,
|
||||||
|
"bases:Create new base": false
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"active": "1db1d6b317bc05ae",
|
||||||
|
"lastOpenFiles": [
|
||||||
|
"src/util/posix.ts",
|
||||||
|
"src/core/util/sort.ts"
|
||||||
|
]
|
||||||
|
}
|
||||||
Vendored
+11
-1
@@ -7,12 +7,22 @@
|
|||||||
{
|
{
|
||||||
"type": "node",
|
"type": "node",
|
||||||
"request": "launch",
|
"request": "launch",
|
||||||
"name": "Test Source Code",
|
"name": "Test Source",
|
||||||
"cwd": "${workspaceFolder}",
|
"cwd": "${workspaceFolder}",
|
||||||
"runtimeExecutable": "npm",
|
"runtimeExecutable": "npm",
|
||||||
"runtimeArgs": ["run", "test"],
|
"runtimeArgs": ["run", "test"],
|
||||||
"console": "integratedTerminal",
|
"console": "integratedTerminal",
|
||||||
"skipFiles": ["<node_internals>/**"]
|
"skipFiles": ["<node_internals>/**"]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Debug Shell",
|
||||||
|
"type": "node",
|
||||||
|
"request": "launch",
|
||||||
|
"runtimeExecutable": "${workspaceFolder}/node_modules/.bin/tsx",
|
||||||
|
"runtimeArgs": ["src/index.ts"],
|
||||||
|
"cwd": "${workspaceFolder}",
|
||||||
|
"console": "integratedTerminal",
|
||||||
|
"skipFiles": ["<node_internals>/**"]
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,41 @@
|
|||||||
# termos
|
# Termos
|
||||||
|
|
||||||
terminal emulation library that bases its functionality on the bash shell
|
This library allows running a fully contained unix-like operating system written in pure typescript.
|
||||||
|
|
||||||
|
It supports all the basic operations like saving and reading files, creating new files and folders.
|
||||||
|
It has support for users and permissions and can handle multiple user sessions at the same time.
|
||||||
|
It doesnt implement a website friendly interface on its own. Thats one of the desing choices in this project, to make it as versatile as possible. But it shares a easy to use interface that you can plug into your frontend code to interact with it properly.
|
||||||
|
|
||||||
|
> [!NOTE]
|
||||||
|
> Right now the project is still in alpha and this is just a small portion of whats to come that serves for demonstration purposes. The code and design will change in the future so most of this is not set in stone
|
||||||
|
|
||||||
|
## Prerequisites
|
||||||
|
|
||||||
|
- [Node.js](https://nodejs.org/) installed (version 26.2.0 or higher)
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
|
||||||
|
> [!NOTE]
|
||||||
|
> If youre using Windows or MacOS as your operating system then the installation process might be different.
|
||||||
|
> Refer to [this website][ref1] on how to install nodejs on your operating system.
|
||||||
|
|
||||||
|
[ref1]: https://nodejs.org/en/download
|
||||||
|
|
||||||
|
```bash
|
||||||
|
wget https://git.gornodox.dev/ChaosMaker/termos/archive/unstable-async.zip
|
||||||
|
unzip -d termos unstable-async.zip
|
||||||
|
cd termos
|
||||||
|
npm install
|
||||||
|
```
|
||||||
|
|
||||||
|
## Running the project
|
||||||
|
|
||||||
|
```bash
|
||||||
|
npm run dev
|
||||||
|
```
|
||||||
|
|
||||||
|
## Running the test suite
|
||||||
|
|
||||||
|
```bash
|
||||||
|
npm run test
|
||||||
|
```
|
||||||
@@ -11,6 +11,7 @@ export default [
|
|||||||
...functional.configs.recommended,
|
...functional.configs.recommended,
|
||||||
rules: {
|
rules: {
|
||||||
"functional/no-mixed-types": "off",
|
"functional/no-mixed-types": "off",
|
||||||
|
"@typescript-eslint/no-unused-vars": ["error", { varsIgnorePattern: "^__", argsIgnorePattern: "^__" }],
|
||||||
"@typescript-eslint/no-restricted-imports": [
|
"@typescript-eslint/no-restricted-imports": [
|
||||||
"error",
|
"error",
|
||||||
{
|
{
|
||||||
|
|||||||
Generated
+544
-18
@@ -8,13 +8,17 @@
|
|||||||
"name": "termos",
|
"name": "termos",
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
|
"dependencies": {
|
||||||
|
"zod": "^4.4.3"
|
||||||
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@eslint/js": "^10.0.1",
|
"@eslint/js": "^10.0.1",
|
||||||
"@types/node": "^26.1.0",
|
"@types/node": "^26.2.0",
|
||||||
"eslint": "^10.6.0",
|
"eslint": "^10.6.0",
|
||||||
"eslint-config-prettier": "^10.1.8",
|
"eslint-config-prettier": "^10.1.8",
|
||||||
"eslint-plugin-functional": "^10.0.0",
|
"eslint-plugin-functional": "^10.0.0",
|
||||||
"eslint-plugin-prettier": "^5.5.6",
|
"eslint-plugin-prettier": "^5.5.6",
|
||||||
|
"tsx": "^4.23.0",
|
||||||
"typescript": "^6.0.3",
|
"typescript": "^6.0.3",
|
||||||
"typescript-eslint": "^8.63.0",
|
"typescript-eslint": "^8.63.0",
|
||||||
"vite-tsconfig-paths": "^6.1.1",
|
"vite-tsconfig-paths": "^6.1.1",
|
||||||
@@ -55,6 +59,448 @@
|
|||||||
"tslib": "^2.4.0"
|
"tslib": "^2.4.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/@esbuild/aix-ppc64": {
|
||||||
|
"version": "0.28.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/@esbuild/aix-ppc64/-/aix-ppc64-0.28.1.tgz",
|
||||||
|
"integrity": "sha512-Svl7tq8k/08+p6CXPpRjQ1fKX+1odH/BQbb48fV6fj3CWHhsoIOoY87w1oHXm0qEpkIK3ZfVgp0hed3XBXzXMQ==",
|
||||||
|
"cpu": [
|
||||||
|
"ppc64"
|
||||||
|
],
|
||||||
|
"dev": true,
|
||||||
|
"license": "MIT",
|
||||||
|
"optional": true,
|
||||||
|
"os": [
|
||||||
|
"aix"
|
||||||
|
],
|
||||||
|
"engines": {
|
||||||
|
"node": ">=18"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/@esbuild/android-arm": {
|
||||||
|
"version": "0.28.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/@esbuild/android-arm/-/android-arm-0.28.1.tgz",
|
||||||
|
"integrity": "sha512-0k2F129Xdio1TdJfzJ8sy1Q47vUD2NnwdhiAf7drUN1EBTfPf4hsFCtmMgu/6m8JSzsBrlmVjudMBQqOfG8usQ==",
|
||||||
|
"cpu": [
|
||||||
|
"arm"
|
||||||
|
],
|
||||||
|
"dev": true,
|
||||||
|
"license": "MIT",
|
||||||
|
"optional": true,
|
||||||
|
"os": [
|
||||||
|
"android"
|
||||||
|
],
|
||||||
|
"engines": {
|
||||||
|
"node": ">=18"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/@esbuild/android-arm64": {
|
||||||
|
"version": "0.28.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/@esbuild/android-arm64/-/android-arm64-0.28.1.tgz",
|
||||||
|
"integrity": "sha512-34EGEbCIAgosYz6goLcopX6Mo7NyGv9tfwEM2/7Ce2VcVRk568iSvniGWcUXIy7wEDR1wzolcxcriFVrWYcwBg==",
|
||||||
|
"cpu": [
|
||||||
|
"arm64"
|
||||||
|
],
|
||||||
|
"dev": true,
|
||||||
|
"license": "MIT",
|
||||||
|
"optional": true,
|
||||||
|
"os": [
|
||||||
|
"android"
|
||||||
|
],
|
||||||
|
"engines": {
|
||||||
|
"node": ">=18"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/@esbuild/android-x64": {
|
||||||
|
"version": "0.28.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/@esbuild/android-x64/-/android-x64-0.28.1.tgz",
|
||||||
|
"integrity": "sha512-dbwY7ltSMDWsRatcRpCnES4F+im88OCUgGZjy52shC7GqHRE/cYlxNbB4Z4UpJswpcc4Qxd2oE/ufM0p61IKng==",
|
||||||
|
"cpu": [
|
||||||
|
"x64"
|
||||||
|
],
|
||||||
|
"dev": true,
|
||||||
|
"license": "MIT",
|
||||||
|
"optional": true,
|
||||||
|
"os": [
|
||||||
|
"android"
|
||||||
|
],
|
||||||
|
"engines": {
|
||||||
|
"node": ">=18"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/@esbuild/darwin-arm64": {
|
||||||
|
"version": "0.28.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/@esbuild/darwin-arm64/-/darwin-arm64-0.28.1.tgz",
|
||||||
|
"integrity": "sha512-TZbWkQY7kvTAXbXUT7uVACR5cMHsDiSz9z7ZKAX/RTq/WJEk3QyRr0wZpNhBDX+/0CtdqUIJlOiodQcta6tY3Q==",
|
||||||
|
"cpu": [
|
||||||
|
"arm64"
|
||||||
|
],
|
||||||
|
"dev": true,
|
||||||
|
"license": "MIT",
|
||||||
|
"optional": true,
|
||||||
|
"os": [
|
||||||
|
"darwin"
|
||||||
|
],
|
||||||
|
"engines": {
|
||||||
|
"node": ">=18"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/@esbuild/darwin-x64": {
|
||||||
|
"version": "0.28.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/@esbuild/darwin-x64/-/darwin-x64-0.28.1.tgz",
|
||||||
|
"integrity": "sha512-zfdzgK9ACBNZLI/CyHTOx81SyNbM6YXn7rxSgX97VjyiPl9W1i4Ka4fgKECEoFCKGpvBj5qArWIGgQjOwkgskQ==",
|
||||||
|
"cpu": [
|
||||||
|
"x64"
|
||||||
|
],
|
||||||
|
"dev": true,
|
||||||
|
"license": "MIT",
|
||||||
|
"optional": true,
|
||||||
|
"os": [
|
||||||
|
"darwin"
|
||||||
|
],
|
||||||
|
"engines": {
|
||||||
|
"node": ">=18"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/@esbuild/freebsd-arm64": {
|
||||||
|
"version": "0.28.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/@esbuild/freebsd-arm64/-/freebsd-arm64-0.28.1.tgz",
|
||||||
|
"integrity": "sha512-wG2EA8ENdEI0qhkSZMjfqrdY+ziCYCPMmtZjjIwOmXFjmyzEHn+UUxk5of+SYsjtfs3VpnlC7QLzSI5hY/rOAw==",
|
||||||
|
"cpu": [
|
||||||
|
"arm64"
|
||||||
|
],
|
||||||
|
"dev": true,
|
||||||
|
"license": "MIT",
|
||||||
|
"optional": true,
|
||||||
|
"os": [
|
||||||
|
"freebsd"
|
||||||
|
],
|
||||||
|
"engines": {
|
||||||
|
"node": ">=18"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/@esbuild/freebsd-x64": {
|
||||||
|
"version": "0.28.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/@esbuild/freebsd-x64/-/freebsd-x64-0.28.1.tgz",
|
||||||
|
"integrity": "sha512-i7dZ9vQgnvSCzi/rYCXNgtF/U+eKZNJBzu3eTQbRgHnM7tNSizLOkRFAl3qzVc/Op/u5YkHHa4pf/3DOYHthLQ==",
|
||||||
|
"cpu": [
|
||||||
|
"x64"
|
||||||
|
],
|
||||||
|
"dev": true,
|
||||||
|
"license": "MIT",
|
||||||
|
"optional": true,
|
||||||
|
"os": [
|
||||||
|
"freebsd"
|
||||||
|
],
|
||||||
|
"engines": {
|
||||||
|
"node": ">=18"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/@esbuild/linux-arm": {
|
||||||
|
"version": "0.28.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/@esbuild/linux-arm/-/linux-arm-0.28.1.tgz",
|
||||||
|
"integrity": "sha512-qVXBOHQS+d5Y722GwJzJUtOLlX7km3CraOaGormF1pDtPd2C/l1SHRPgjLunLGe51Sh5YYWKMFDyV4SxgMQYTQ==",
|
||||||
|
"cpu": [
|
||||||
|
"arm"
|
||||||
|
],
|
||||||
|
"dev": true,
|
||||||
|
"license": "MIT",
|
||||||
|
"optional": true,
|
||||||
|
"os": [
|
||||||
|
"linux"
|
||||||
|
],
|
||||||
|
"engines": {
|
||||||
|
"node": ">=18"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/@esbuild/linux-arm64": {
|
||||||
|
"version": "0.28.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/@esbuild/linux-arm64/-/linux-arm64-0.28.1.tgz",
|
||||||
|
"integrity": "sha512-yHs+0uc8+nvEAfAfxrWQKK5peSNzBc4PegcMO0EJ2hT71uA7vB8Ihg2e77R2P7SG5uYjPbHlLLmve4LLLRCf0g==",
|
||||||
|
"cpu": [
|
||||||
|
"arm64"
|
||||||
|
],
|
||||||
|
"dev": true,
|
||||||
|
"license": "MIT",
|
||||||
|
"optional": true,
|
||||||
|
"os": [
|
||||||
|
"linux"
|
||||||
|
],
|
||||||
|
"engines": {
|
||||||
|
"node": ">=18"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/@esbuild/linux-ia32": {
|
||||||
|
"version": "0.28.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/@esbuild/linux-ia32/-/linux-ia32-0.28.1.tgz",
|
||||||
|
"integrity": "sha512-d1z4ZuP0ajrfz/FhGT4vv278rX8KnPPJx8i5+AtK7TYbx9Le9F1hyzurZpkEyjkGa9dUGhQow4C1NmeGvqxN2w==",
|
||||||
|
"cpu": [
|
||||||
|
"ia32"
|
||||||
|
],
|
||||||
|
"dev": true,
|
||||||
|
"license": "MIT",
|
||||||
|
"optional": true,
|
||||||
|
"os": [
|
||||||
|
"linux"
|
||||||
|
],
|
||||||
|
"engines": {
|
||||||
|
"node": ">=18"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/@esbuild/linux-loong64": {
|
||||||
|
"version": "0.28.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/@esbuild/linux-loong64/-/linux-loong64-0.28.1.tgz",
|
||||||
|
"integrity": "sha512-M5sRjUVZrkm1OAPR3dlOYzNmN+loZKGVi1VUQGrwuqLcbR6qeAz+famMhjASeH3YVKvZz+zT1jlh/keC3Rj/lg==",
|
||||||
|
"cpu": [
|
||||||
|
"loong64"
|
||||||
|
],
|
||||||
|
"dev": true,
|
||||||
|
"license": "MIT",
|
||||||
|
"optional": true,
|
||||||
|
"os": [
|
||||||
|
"linux"
|
||||||
|
],
|
||||||
|
"engines": {
|
||||||
|
"node": ">=18"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/@esbuild/linux-mips64el": {
|
||||||
|
"version": "0.28.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/@esbuild/linux-mips64el/-/linux-mips64el-0.28.1.tgz",
|
||||||
|
"integrity": "sha512-mRObBZeHh2OxcBFPWE/FjylkRgZdYuiTR3vaTozquCGOH14iP9oN4x4Ge81CoIDYQrXmIxpFumJBu5MtZpnQJQ==",
|
||||||
|
"cpu": [
|
||||||
|
"mips64el"
|
||||||
|
],
|
||||||
|
"dev": true,
|
||||||
|
"license": "MIT",
|
||||||
|
"optional": true,
|
||||||
|
"os": [
|
||||||
|
"linux"
|
||||||
|
],
|
||||||
|
"engines": {
|
||||||
|
"node": ">=18"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/@esbuild/linux-ppc64": {
|
||||||
|
"version": "0.28.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/@esbuild/linux-ppc64/-/linux-ppc64-0.28.1.tgz",
|
||||||
|
"integrity": "sha512-slScBsMAb3GFDcdrCgLwZtPYRoH2H/youv10QiZyRjmsP48fznoveWytSgCI/R0ZcUgpc0ZhIUEx6LHts8yrfQ==",
|
||||||
|
"cpu": [
|
||||||
|
"ppc64"
|
||||||
|
],
|
||||||
|
"dev": true,
|
||||||
|
"license": "MIT",
|
||||||
|
"optional": true,
|
||||||
|
"os": [
|
||||||
|
"linux"
|
||||||
|
],
|
||||||
|
"engines": {
|
||||||
|
"node": ">=18"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/@esbuild/linux-riscv64": {
|
||||||
|
"version": "0.28.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/@esbuild/linux-riscv64/-/linux-riscv64-0.28.1.tgz",
|
||||||
|
"integrity": "sha512-kw0owk1o0GFETUJyW0jc0G4Yzs0BHZn0JDZ8JRT088vjJYX777BAs1fDGxAC+q831qOs2DTC96mNsG2opdfyyQ==",
|
||||||
|
"cpu": [
|
||||||
|
"riscv64"
|
||||||
|
],
|
||||||
|
"dev": true,
|
||||||
|
"license": "MIT",
|
||||||
|
"optional": true,
|
||||||
|
"os": [
|
||||||
|
"linux"
|
||||||
|
],
|
||||||
|
"engines": {
|
||||||
|
"node": ">=18"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/@esbuild/linux-s390x": {
|
||||||
|
"version": "0.28.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/@esbuild/linux-s390x/-/linux-s390x-0.28.1.tgz",
|
||||||
|
"integrity": "sha512-/lAIjX8aYFRByhh6L5rYtPEDRqa9de/4V/juOXcta5frjvzXO4/sqEtyytse0g3zZFuWu5cDN0MkLz2qRDD2Ag==",
|
||||||
|
"cpu": [
|
||||||
|
"s390x"
|
||||||
|
],
|
||||||
|
"dev": true,
|
||||||
|
"license": "MIT",
|
||||||
|
"optional": true,
|
||||||
|
"os": [
|
||||||
|
"linux"
|
||||||
|
],
|
||||||
|
"engines": {
|
||||||
|
"node": ">=18"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/@esbuild/linux-x64": {
|
||||||
|
"version": "0.28.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/@esbuild/linux-x64/-/linux-x64-0.28.1.tgz",
|
||||||
|
"integrity": "sha512-u/anNYF2mmVOEDwLtnQ1wOr3EZ9sTNGLWrsYGYwHWzGA3Si84IOkHXlbWTD1NB+9/1lcnweYKO54uhxZydNzfA==",
|
||||||
|
"cpu": [
|
||||||
|
"x64"
|
||||||
|
],
|
||||||
|
"dev": true,
|
||||||
|
"license": "MIT",
|
||||||
|
"optional": true,
|
||||||
|
"os": [
|
||||||
|
"linux"
|
||||||
|
],
|
||||||
|
"engines": {
|
||||||
|
"node": ">=18"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/@esbuild/netbsd-arm64": {
|
||||||
|
"version": "0.28.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/@esbuild/netbsd-arm64/-/netbsd-arm64-0.28.1.tgz",
|
||||||
|
"integrity": "sha512-oks0DYbLwWMmaakTsCb+zL4E+aHRVLom9IJZOAthMQEPiQmydXHkziYEsGYRx0uNV/IjEKGAV941JzH02pflqw==",
|
||||||
|
"cpu": [
|
||||||
|
"arm64"
|
||||||
|
],
|
||||||
|
"dev": true,
|
||||||
|
"license": "MIT",
|
||||||
|
"optional": true,
|
||||||
|
"os": [
|
||||||
|
"netbsd"
|
||||||
|
],
|
||||||
|
"engines": {
|
||||||
|
"node": ">=18"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/@esbuild/netbsd-x64": {
|
||||||
|
"version": "0.28.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/@esbuild/netbsd-x64/-/netbsd-x64-0.28.1.tgz",
|
||||||
|
"integrity": "sha512-aeL6lAnN89Hz43Mlh1G8ARasbuoYvSITDEx0tHh5b7jJnHcssqgjy9Yx430GDpmCa6OyrKoS0aNRjKundRizGg==",
|
||||||
|
"cpu": [
|
||||||
|
"x64"
|
||||||
|
],
|
||||||
|
"dev": true,
|
||||||
|
"license": "MIT",
|
||||||
|
"optional": true,
|
||||||
|
"os": [
|
||||||
|
"netbsd"
|
||||||
|
],
|
||||||
|
"engines": {
|
||||||
|
"node": ">=18"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/@esbuild/openbsd-arm64": {
|
||||||
|
"version": "0.28.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/@esbuild/openbsd-arm64/-/openbsd-arm64-0.28.1.tgz",
|
||||||
|
"integrity": "sha512-MEFJe5C3R8pwXdZ5Y21oo6m7ePiS0d9pWucn99O/wvyJZChoIQKrQDxKrGeW8F5+T0okTHesAmDeiHDTIq0V/Q==",
|
||||||
|
"cpu": [
|
||||||
|
"arm64"
|
||||||
|
],
|
||||||
|
"dev": true,
|
||||||
|
"license": "MIT",
|
||||||
|
"optional": true,
|
||||||
|
"os": [
|
||||||
|
"openbsd"
|
||||||
|
],
|
||||||
|
"engines": {
|
||||||
|
"node": ">=18"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/@esbuild/openbsd-x64": {
|
||||||
|
"version": "0.28.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/@esbuild/openbsd-x64/-/openbsd-x64-0.28.1.tgz",
|
||||||
|
"integrity": "sha512-i/ZLIOafE0Z8cI/XANJAixoJL/uRAoS2xOA3rb0xN+KK0K177cMAsQYkzHtBrtMXAKuAc7HGgcWiZ/sRC1Nxgw==",
|
||||||
|
"cpu": [
|
||||||
|
"x64"
|
||||||
|
],
|
||||||
|
"dev": true,
|
||||||
|
"license": "MIT",
|
||||||
|
"optional": true,
|
||||||
|
"os": [
|
||||||
|
"openbsd"
|
||||||
|
],
|
||||||
|
"engines": {
|
||||||
|
"node": ">=18"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/@esbuild/openharmony-arm64": {
|
||||||
|
"version": "0.28.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/@esbuild/openharmony-arm64/-/openharmony-arm64-0.28.1.tgz",
|
||||||
|
"integrity": "sha512-ge+Z7EXFNt2BO1oAMsVpiQ8EwndV9i1xXerAeTIK7AtPs3bKFXQM7nlRxDSIUIMeueR1CNXxqztLzdNeReKBJg==",
|
||||||
|
"cpu": [
|
||||||
|
"arm64"
|
||||||
|
],
|
||||||
|
"dev": true,
|
||||||
|
"license": "MIT",
|
||||||
|
"optional": true,
|
||||||
|
"os": [
|
||||||
|
"openharmony"
|
||||||
|
],
|
||||||
|
"engines": {
|
||||||
|
"node": ">=18"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/@esbuild/sunos-x64": {
|
||||||
|
"version": "0.28.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/@esbuild/sunos-x64/-/sunos-x64-0.28.1.tgz",
|
||||||
|
"integrity": "sha512-BEjgtECkL3vY+SaSQ6nzVfiALUeFxpawyp8Jmf5PtYhf1Ug40N1h/hxlhts+f1FvSvarEigdxS3BlSMI2PJLcQ==",
|
||||||
|
"cpu": [
|
||||||
|
"x64"
|
||||||
|
],
|
||||||
|
"dev": true,
|
||||||
|
"license": "MIT",
|
||||||
|
"optional": true,
|
||||||
|
"os": [
|
||||||
|
"sunos"
|
||||||
|
],
|
||||||
|
"engines": {
|
||||||
|
"node": ">=18"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/@esbuild/win32-arm64": {
|
||||||
|
"version": "0.28.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/@esbuild/win32-arm64/-/win32-arm64-0.28.1.tgz",
|
||||||
|
"integrity": "sha512-lCv9eK/H6ZJWbE7bh2nw54CZ9M2nupBxJcTsdk/QQnWkdSjKGuxmmH8/GWrlT1eMmZfn4dGcCjRte397WqfQXA==",
|
||||||
|
"cpu": [
|
||||||
|
"arm64"
|
||||||
|
],
|
||||||
|
"dev": true,
|
||||||
|
"license": "MIT",
|
||||||
|
"optional": true,
|
||||||
|
"os": [
|
||||||
|
"win32"
|
||||||
|
],
|
||||||
|
"engines": {
|
||||||
|
"node": ">=18"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/@esbuild/win32-ia32": {
|
||||||
|
"version": "0.28.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/@esbuild/win32-ia32/-/win32-ia32-0.28.1.tgz",
|
||||||
|
"integrity": "sha512-zvb/mB2bSCoJOpoCBgYKKpX6YM6mJBlBUVUtVj41DlZJVEB6/0CKlRYxP5wWl1C1ILiCoAU5wZZ4q1P3qeS6Eg==",
|
||||||
|
"cpu": [
|
||||||
|
"ia32"
|
||||||
|
],
|
||||||
|
"dev": true,
|
||||||
|
"license": "MIT",
|
||||||
|
"optional": true,
|
||||||
|
"os": [
|
||||||
|
"win32"
|
||||||
|
],
|
||||||
|
"engines": {
|
||||||
|
"node": ">=18"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/@esbuild/win32-x64": {
|
||||||
|
"version": "0.28.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/@esbuild/win32-x64/-/win32-x64-0.28.1.tgz",
|
||||||
|
"integrity": "sha512-bm4Mowrv+GXMlpWX++EcXw/iLyd1o3+bJkC2DkWXYVvgZCqD/bSj9ctZeAMC3cIxgjRVR2Dufaiu4YPxr5gW1A==",
|
||||||
|
"cpu": [
|
||||||
|
"x64"
|
||||||
|
],
|
||||||
|
"dev": true,
|
||||||
|
"license": "MIT",
|
||||||
|
"optional": true,
|
||||||
|
"os": [
|
||||||
|
"win32"
|
||||||
|
],
|
||||||
|
"engines": {
|
||||||
|
"node": ">=18"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/@eslint-community/eslint-utils": {
|
"node_modules/@eslint-community/eslint-utils": {
|
||||||
"version": "4.9.1",
|
"version": "4.9.1",
|
||||||
"resolved": "https://registry.npmjs.org/@eslint-community/eslint-utils/-/eslint-utils-4.9.1.tgz",
|
"resolved": "https://registry.npmjs.org/@eslint-community/eslint-utils/-/eslint-utils-4.9.1.tgz",
|
||||||
@@ -638,9 +1084,9 @@
|
|||||||
"license": "MIT"
|
"license": "MIT"
|
||||||
},
|
},
|
||||||
"node_modules/@types/node": {
|
"node_modules/@types/node": {
|
||||||
"version": "26.1.0",
|
"version": "26.2.0",
|
||||||
"resolved": "https://registry.npmjs.org/@types/node/-/node-26.1.0.tgz",
|
"resolved": "https://registry.npmjs.org/@types/node/-/node-26.2.0.tgz",
|
||||||
"integrity": "sha512-O0A1G3xPGy4w7AgQdAQYUlQ+BKk2Oovw8eRpofyp5KdBZULnbe+WqaOVNrm705SHphCiG4XHsACrSmPu1f+Kgw==",
|
"integrity": "sha512-5IviulTZeRNp2vAJ514cc/HUlY5nZ9fCbq9DMyC52BrhFZACo3nI0R7qBxhQmo/d27NFe96ur/b7Wwxklda+kg==",
|
||||||
"dev": true,
|
"dev": true,
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
@@ -1051,16 +1497,16 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/brace-expansion": {
|
"node_modules/brace-expansion": {
|
||||||
"version": "5.0.7",
|
"version": "5.0.9",
|
||||||
"resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-5.0.7.tgz",
|
"resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-5.0.9.tgz",
|
||||||
"integrity": "sha512-7oFy703dxfY3/NLxC1fh2SUCQ0H9rmAY+5EpDVfXjUTTs+HEwR2nYaqLv+GWcTsumwxPfiz6CzCNkwXwBUwqCA==",
|
"integrity": "sha512-ScQ4IuvIEF1TMlP7Zt+vjJ//9zlPb2SDcxWxM3bk8s6t6GGdJ7KO1dCcTidOPJKePW30LE/2cT7wCyPho9/Wxg==",
|
||||||
"dev": true,
|
"dev": true,
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"balanced-match": "^4.0.2"
|
"balanced-match": "^4.0.2"
|
||||||
},
|
},
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": "18 || 20 || >=22"
|
"node": "20 || >=22"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/chai": {
|
"node_modules/chai": {
|
||||||
@@ -1121,10 +1567,20 @@
|
|||||||
"license": "MIT"
|
"license": "MIT"
|
||||||
},
|
},
|
||||||
"node_modules/deepmerge-ts": {
|
"node_modules/deepmerge-ts": {
|
||||||
"version": "7.1.5",
|
"version": "7.1.6",
|
||||||
"resolved": "https://registry.npmjs.org/deepmerge-ts/-/deepmerge-ts-7.1.5.tgz",
|
"resolved": "https://registry.npmjs.org/deepmerge-ts/-/deepmerge-ts-7.1.6.tgz",
|
||||||
"integrity": "sha512-HOJkrhaYsweh+W+e74Yn7YStZOilkoPb6fycpwNLKzSPtruFs48nYis0zy5yJz1+ktUhHxoRDJ27RQAWLIJVJw==",
|
"integrity": "sha512-gQhL1ksGBLQbHeAo47YU6cs2ahd3Pv+8PFYoWogNZbQnNwQyOh9Ad5kbeHFiWwbKdeWQJ5Z7Y7mwb9ew2hXBLw==",
|
||||||
"dev": true,
|
"dev": true,
|
||||||
|
"funding": [
|
||||||
|
{
|
||||||
|
"type": "ko-fi",
|
||||||
|
"url": "https://ko-fi.com/rebeccastevens"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "tidelift",
|
||||||
|
"url": "https://tidelift.com/funding/github/npm/deepmerge-ts"
|
||||||
|
}
|
||||||
|
],
|
||||||
"license": "BSD-3-Clause",
|
"license": "BSD-3-Clause",
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">=16.0.0"
|
"node": ">=16.0.0"
|
||||||
@@ -1147,6 +1603,48 @@
|
|||||||
"dev": true,
|
"dev": true,
|
||||||
"license": "MIT"
|
"license": "MIT"
|
||||||
},
|
},
|
||||||
|
"node_modules/esbuild": {
|
||||||
|
"version": "0.28.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.28.1.tgz",
|
||||||
|
"integrity": "sha512-HrJrvZv5ayxBzPfwphOoNzkzOIIlifzk0KJrGK2c8R4+LKpMtpYLQeUdjnwjWv/LZlkH2laZk+4w78pi99D4Vw==",
|
||||||
|
"dev": true,
|
||||||
|
"hasInstallScript": true,
|
||||||
|
"license": "MIT",
|
||||||
|
"bin": {
|
||||||
|
"esbuild": "bin/esbuild"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">=18"
|
||||||
|
},
|
||||||
|
"optionalDependencies": {
|
||||||
|
"@esbuild/aix-ppc64": "0.28.1",
|
||||||
|
"@esbuild/android-arm": "0.28.1",
|
||||||
|
"@esbuild/android-arm64": "0.28.1",
|
||||||
|
"@esbuild/android-x64": "0.28.1",
|
||||||
|
"@esbuild/darwin-arm64": "0.28.1",
|
||||||
|
"@esbuild/darwin-x64": "0.28.1",
|
||||||
|
"@esbuild/freebsd-arm64": "0.28.1",
|
||||||
|
"@esbuild/freebsd-x64": "0.28.1",
|
||||||
|
"@esbuild/linux-arm": "0.28.1",
|
||||||
|
"@esbuild/linux-arm64": "0.28.1",
|
||||||
|
"@esbuild/linux-ia32": "0.28.1",
|
||||||
|
"@esbuild/linux-loong64": "0.28.1",
|
||||||
|
"@esbuild/linux-mips64el": "0.28.1",
|
||||||
|
"@esbuild/linux-ppc64": "0.28.1",
|
||||||
|
"@esbuild/linux-riscv64": "0.28.1",
|
||||||
|
"@esbuild/linux-s390x": "0.28.1",
|
||||||
|
"@esbuild/linux-x64": "0.28.1",
|
||||||
|
"@esbuild/netbsd-arm64": "0.28.1",
|
||||||
|
"@esbuild/netbsd-x64": "0.28.1",
|
||||||
|
"@esbuild/openbsd-arm64": "0.28.1",
|
||||||
|
"@esbuild/openbsd-x64": "0.28.1",
|
||||||
|
"@esbuild/openharmony-arm64": "0.28.1",
|
||||||
|
"@esbuild/sunos-x64": "0.28.1",
|
||||||
|
"@esbuild/win32-arm64": "0.28.1",
|
||||||
|
"@esbuild/win32-ia32": "0.28.1",
|
||||||
|
"@esbuild/win32-x64": "0.28.1"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/escape-string-regexp": {
|
"node_modules/escape-string-regexp": {
|
||||||
"version": "4.0.0",
|
"version": "4.0.0",
|
||||||
"resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-4.0.0.tgz",
|
"resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-4.0.0.tgz",
|
||||||
@@ -2008,9 +2506,9 @@
|
|||||||
"license": "MIT"
|
"license": "MIT"
|
||||||
},
|
},
|
||||||
"node_modules/nanoid": {
|
"node_modules/nanoid": {
|
||||||
"version": "3.3.15",
|
"version": "3.3.18",
|
||||||
"resolved": "https://registry.npmjs.org/nanoid/-/nanoid-3.3.15.tgz",
|
"resolved": "https://registry.npmjs.org/nanoid/-/nanoid-3.3.18.tgz",
|
||||||
"integrity": "sha512-y7Wygv/7mEOvxTuEQDB8StXdMRBWf1kR/tlhAzBRUFkB2jfcLOAxO/SHmOO2zgz1pVgK29/kyupn059/bCHdjA==",
|
"integrity": "sha512-DTg4MJbGMWkfi6VZFdNt2/caMbQy4Ou+Op/hJQvGEWcnVfoA1QA+xzRKAzw9jD6+GVOOeYr/mIcuDSdug6F6+w==",
|
||||||
"dev": true,
|
"dev": true,
|
||||||
"funding": [
|
"funding": [
|
||||||
{
|
{
|
||||||
@@ -2145,9 +2643,9 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/postcss": {
|
"node_modules/postcss": {
|
||||||
"version": "8.5.16",
|
"version": "8.5.26",
|
||||||
"resolved": "https://registry.npmjs.org/postcss/-/postcss-8.5.16.tgz",
|
"resolved": "https://registry.npmjs.org/postcss/-/postcss-8.5.26.tgz",
|
||||||
"integrity": "sha512-vuwillviilfKZsg0VGj5R/YwwcHx4SLsIOI/7K6mQkWx+l5cUHTjj5g0AasTBcyXsbfTgrwsUNmVUb5xVwyPwg==",
|
"integrity": "sha512-u82N74LFzG8ca+dD8puPnplTXoGH4fTPpVGuIbt36G3qvNlkvfD0lEAZSxaly3KX8TS/L1A1gsCEmvKmBcVbkQ==",
|
||||||
"dev": true,
|
"dev": true,
|
||||||
"funding": [
|
"funding": [
|
||||||
{
|
{
|
||||||
@@ -2165,7 +2663,7 @@
|
|||||||
],
|
],
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"nanoid": "^3.3.12",
|
"nanoid": "^3.3.17",
|
||||||
"picocolors": "^1.1.1",
|
"picocolors": "^1.1.1",
|
||||||
"source-map-js": "^1.2.1"
|
"source-map-js": "^1.2.1"
|
||||||
},
|
},
|
||||||
@@ -2428,6 +2926,25 @@
|
|||||||
"license": "0BSD",
|
"license": "0BSD",
|
||||||
"optional": true
|
"optional": true
|
||||||
},
|
},
|
||||||
|
"node_modules/tsx": {
|
||||||
|
"version": "4.23.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/tsx/-/tsx-4.23.0.tgz",
|
||||||
|
"integrity": "sha512-eUdUIaCr963q2h5u3+QwvYp0+eqPvn+egeqZUm0hwERCqqx1E3kK5ehbGCvqSE5MQAULr67ww0cA3jKc3YkM1w==",
|
||||||
|
"dev": true,
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"esbuild": "~0.28.0"
|
||||||
|
},
|
||||||
|
"bin": {
|
||||||
|
"tsx": "dist/cli.mjs"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">=18.0.0"
|
||||||
|
},
|
||||||
|
"optionalDependencies": {
|
||||||
|
"fsevents": "~2.3.3"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/type-check": {
|
"node_modules/type-check": {
|
||||||
"version": "0.4.0",
|
"version": "0.4.0",
|
||||||
"resolved": "https://registry.npmjs.org/type-check/-/type-check-0.4.0.tgz",
|
"resolved": "https://registry.npmjs.org/type-check/-/type-check-0.4.0.tgz",
|
||||||
@@ -2756,6 +3273,15 @@
|
|||||||
"funding": {
|
"funding": {
|
||||||
"url": "https://github.com/sponsors/sindresorhus"
|
"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"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+13
-5
@@ -1,10 +1,11 @@
|
|||||||
{
|
{
|
||||||
"name": "termos",
|
"name": "termos",
|
||||||
"version": "1.0.0",
|
"version": "0.0.1",
|
||||||
"description": "terminal emulation library that bases its functionality on the bash shell",
|
"description": "Emulation library for a unix-like operating-system written in pure typescript",
|
||||||
"main": "termos.ts",
|
"main": "index.ts",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "eslint . && vitest"
|
"test": "eslint . && vitest",
|
||||||
|
"dev": "npx tsx src/index.ts"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
@@ -16,14 +17,21 @@
|
|||||||
"type": "module",
|
"type": "module",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@eslint/js": "^10.0.1",
|
"@eslint/js": "^10.0.1",
|
||||||
"@types/node": "^26.1.0",
|
"@types/node": "^26.2.0",
|
||||||
"eslint": "^10.6.0",
|
"eslint": "^10.6.0",
|
||||||
"eslint-config-prettier": "^10.1.8",
|
"eslint-config-prettier": "^10.1.8",
|
||||||
"eslint-plugin-functional": "^10.0.0",
|
"eslint-plugin-functional": "^10.0.0",
|
||||||
"eslint-plugin-prettier": "^5.5.6",
|
"eslint-plugin-prettier": "^5.5.6",
|
||||||
|
"tsx": "^4.23.0",
|
||||||
"typescript": "^6.0.3",
|
"typescript": "^6.0.3",
|
||||||
"typescript-eslint": "^8.63.0",
|
"typescript-eslint": "^8.63.0",
|
||||||
"vite-tsconfig-paths": "^6.1.1",
|
"vite-tsconfig-paths": "^6.1.1",
|
||||||
"vitest": "^4.1.10"
|
"vitest": "^4.1.10"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"zod": "^4.4.3"
|
||||||
|
},
|
||||||
|
"allowScripts": {
|
||||||
|
"esbuild@0.28.1": true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,11 @@
|
|||||||
|
export interface TermosConfig {
|
||||||
|
serverAddress: string;
|
||||||
|
dbName: string;
|
||||||
|
idbName: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const defaultConfig: TermosConfig = {
|
||||||
|
serverAddress: "",
|
||||||
|
dbName: "termos",
|
||||||
|
idbName: "cache",
|
||||||
|
};
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
import type { CommandIO } from "./command_io";
|
||||||
|
import type { Result } from "./result";
|
||||||
|
import type { ShellState } from "./shell_state";
|
||||||
|
import type { ShellError } from "./util/errors";
|
||||||
|
|
||||||
|
export type CommandFn = (io: CommandIO, state: ShellState, args: string[]) => Promise<CommandOutput<ShellError>>;
|
||||||
|
|
||||||
|
export interface CommandDescriptor {
|
||||||
|
fn: CommandFn;
|
||||||
|
name: string;
|
||||||
|
flags?: readonly FlagDescriptor[];
|
||||||
|
requiresRoot?: boolean;
|
||||||
|
description?: string;
|
||||||
|
keybinds?: readonly string[];
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface FlagDescriptor {
|
||||||
|
name: string;
|
||||||
|
takesValue?: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface CommandOutput<E extends ShellError> {
|
||||||
|
readonly state: ShellState;
|
||||||
|
readonly result: Result<E>;
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
import type { Signal } from "./signal";
|
||||||
|
|
||||||
|
export interface CommandIO {
|
||||||
|
writeStdout(data: Uint8Array): void;
|
||||||
|
writeStderr(data: Uint8Array): void;
|
||||||
|
getSignal(): Promise<Signal>;
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
import type { CommandDescriptor } from "./command";
|
||||||
|
import { descriptor as cat } from "./commands/cat";
|
||||||
|
import { descriptor as cd } from "./commands/cd";
|
||||||
|
import { descriptor as clear } from "./commands/clear";
|
||||||
|
import { descriptor as echo } from "./commands/echo";
|
||||||
|
import { descriptor as ls } from "./commands/ls";
|
||||||
|
import { descriptor as pwd } from "./commands/pwd";
|
||||||
|
import { descriptor as testasync } from "./commands/testasync";
|
||||||
|
|
||||||
|
const reg = new Map<string, CommandDescriptor>();
|
||||||
|
//reg.set(descriptor.name, descriptor <- imported const from command implementation file);
|
||||||
|
reg.set(echo.name, echo);
|
||||||
|
reg.set(cd.name, cd);
|
||||||
|
reg.set(testasync.name, testasync);
|
||||||
|
reg.set(ls.name, ls);
|
||||||
|
reg.set(cat.name, cat);
|
||||||
|
reg.set(pwd.name, pwd);
|
||||||
|
reg.set(clear.name, clear);
|
||||||
|
Object.freeze(reg);
|
||||||
|
|
||||||
|
export const COMMAND_REGISTRY: ReadonlyMap<string, CommandDescriptor> = reg;
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
import type { CommandDescriptor, CommandOutput } from "@core/command";
|
||||||
|
import type { CommandIO } from "@core/command_io";
|
||||||
|
import { createSuccessResult, type ResultSuccess } from "@core/result";
|
||||||
|
import type { ShellState } from "@core/shell_state";
|
||||||
|
import type { ShellError } from "@core/util/errors";
|
||||||
|
|
||||||
|
export const COMMAND_NAME = "";
|
||||||
|
|
||||||
|
async function main(io: CommandIO, state: ShellState, args: string[]): Promise<CommandOutput<ShellError>> {
|
||||||
|
const result: ResultSuccess = createSuccessResult(COMMAND_NAME);
|
||||||
|
|
||||||
|
return {
|
||||||
|
state: state,
|
||||||
|
result,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export const descriptor: CommandDescriptor = {
|
||||||
|
fn: main,
|
||||||
|
name: COMMAND_NAME,
|
||||||
|
};
|
||||||
@@ -0,0 +1,67 @@
|
|||||||
|
import type { CommandDescriptor, CommandOutput } from "@core/command";
|
||||||
|
import type { CommandIO } from "@core/command_io";
|
||||||
|
import { TreeNodeType, type FileNode, type TreeNode } from "@core/filesystem/treenode";
|
||||||
|
import { createErrorResult, createSuccessResult, type ResultSuccess } from "@core/result";
|
||||||
|
import { getCurrentSession } from "@core/session_manager";
|
||||||
|
import type { ShellState } from "@core/shell_state";
|
||||||
|
import type { PathResolutionError, PermissionDeniedError, ShellError } from "@core/util/errors";
|
||||||
|
import { resolvePath } from "@core/util/path_resolver";
|
||||||
|
|
||||||
|
export const COMMAND_NAME = "cat";
|
||||||
|
|
||||||
|
//Need to implement escape sequences
|
||||||
|
async function main(io: CommandIO, state: ShellState, args: string[]): Promise<CommandOutput<ShellError>> {
|
||||||
|
for (const arg of args) {
|
||||||
|
const inode = resolvePath(state, arg);
|
||||||
|
if (inode === -1) {
|
||||||
|
return {
|
||||||
|
state,
|
||||||
|
result: createErrorResult(
|
||||||
|
"PathResolutionError",
|
||||||
|
"No such file or directory",
|
||||||
|
{ path: arg },
|
||||||
|
COMMAND_NAME,
|
||||||
|
),
|
||||||
|
} satisfies CommandOutput<PathResolutionError>;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Casting to TreeNode because the resolvePath already ensures the inode is correct;
|
||||||
|
const node = state.filesystem.getNode(inode) as TreeNode;
|
||||||
|
|
||||||
|
if (node.type != TreeNodeType.File) {
|
||||||
|
return {
|
||||||
|
state,
|
||||||
|
result: createErrorResult(
|
||||||
|
"PathResolutionError",
|
||||||
|
`${node.name} is a directory`,
|
||||||
|
{ path: arg },
|
||||||
|
COMMAND_NAME,
|
||||||
|
),
|
||||||
|
} satisfies CommandOutput<PathResolutionError>;
|
||||||
|
}
|
||||||
|
|
||||||
|
const session = getCurrentSession(state);
|
||||||
|
|
||||||
|
if (!state.filesystem.canRead(inode, session.user)) {
|
||||||
|
return {
|
||||||
|
state,
|
||||||
|
result: createErrorResult("PermissionDenied", "Permission denied", { resource: arg }, COMMAND_NAME),
|
||||||
|
} satisfies CommandOutput<PermissionDeniedError>;
|
||||||
|
}
|
||||||
|
|
||||||
|
node as FileNode;
|
||||||
|
io.writeStdout(node.content.toArray());
|
||||||
|
}
|
||||||
|
|
||||||
|
const result: ResultSuccess = createSuccessResult(COMMAND_NAME);
|
||||||
|
|
||||||
|
return {
|
||||||
|
state: state,
|
||||||
|
result,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export const descriptor: CommandDescriptor = {
|
||||||
|
fn: main,
|
||||||
|
name: COMMAND_NAME,
|
||||||
|
};
|
||||||
@@ -0,0 +1,76 @@
|
|||||||
|
import type { CommandDescriptor, CommandOutput } from "@core/command";
|
||||||
|
import type { CommandIO } from "@core/command_io";
|
||||||
|
import { createErrorResult, createSuccessResult } from "@core/result";
|
||||||
|
import { getCurrentSession } from "@core/session_manager";
|
||||||
|
import { ShellState, withCwd } from "@core/shell_state";
|
||||||
|
import {
|
||||||
|
type InvalidArgumentsError,
|
||||||
|
type PathResolutionError,
|
||||||
|
type PermissionDeniedError,
|
||||||
|
type ShellError,
|
||||||
|
} from "@core/util/errors";
|
||||||
|
import { resolvePath } from "@core/util/path_resolver";
|
||||||
|
|
||||||
|
export const COMMAND_NAME = "cd";
|
||||||
|
|
||||||
|
async function main(__io: CommandIO, state: ShellState, args: string[]): Promise<CommandOutput<ShellError>> {
|
||||||
|
if (args.length > 1) {
|
||||||
|
return {
|
||||||
|
state,
|
||||||
|
result: createErrorResult(
|
||||||
|
"InvalidArguments",
|
||||||
|
"The maximum ammount of arguments for this command is 1",
|
||||||
|
{ expected: "cd <path>", actual: `cd ${args.toString().replace(/[,]/g, " ")}` },
|
||||||
|
COMMAND_NAME,
|
||||||
|
),
|
||||||
|
} satisfies CommandOutput<InvalidArgumentsError>;
|
||||||
|
}
|
||||||
|
|
||||||
|
let inode: number;
|
||||||
|
const session = getCurrentSession(state);
|
||||||
|
|
||||||
|
if (args[0] === undefined) {
|
||||||
|
inode = session.user.home;
|
||||||
|
} else if (args[0] === "-") {
|
||||||
|
inode = session.pwd;
|
||||||
|
} else {
|
||||||
|
inode = resolvePath(state, args[0]);
|
||||||
|
if (inode === -1) {
|
||||||
|
return {
|
||||||
|
state,
|
||||||
|
result: createErrorResult(
|
||||||
|
"PathResolutionError",
|
||||||
|
"No such file or directory",
|
||||||
|
{ path: args[0]! },
|
||||||
|
COMMAND_NAME,
|
||||||
|
),
|
||||||
|
} satisfies CommandOutput<PathResolutionError>;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (state.filesystem.isFile(inode)) {
|
||||||
|
return {
|
||||||
|
state,
|
||||||
|
result: createErrorResult("PathResolutionError", "Not a directory", { path: args[0]! }, COMMAND_NAME),
|
||||||
|
} satisfies CommandOutput<PathResolutionError>;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!state.filesystem.canExecute(inode, session.user)) {
|
||||||
|
return {
|
||||||
|
state,
|
||||||
|
result: createErrorResult("PermissionDenied", "Permission denied", { resource: args[0]! }, COMMAND_NAME),
|
||||||
|
} satisfies CommandOutput<PermissionDeniedError>;
|
||||||
|
}
|
||||||
|
|
||||||
|
const returnState = withCwd(state, inode);
|
||||||
|
|
||||||
|
return {
|
||||||
|
state: returnState,
|
||||||
|
result: createSuccessResult(COMMAND_NAME),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export const descriptor: CommandDescriptor = {
|
||||||
|
fn: main,
|
||||||
|
name: COMMAND_NAME,
|
||||||
|
};
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
import type { CommandDescriptor, CommandOutput } from "@core/command";
|
||||||
|
import type { CommandIO } from "@core/command_io";
|
||||||
|
import { createSuccessResult, type ResultSuccess } from "@core/result";
|
||||||
|
import type { ShellState } from "@core/shell_state";
|
||||||
|
import type { ShellError } from "@core/util/errors";
|
||||||
|
|
||||||
|
export const COMMAND_NAME = "clear";
|
||||||
|
|
||||||
|
async function main(io: CommandIO, state: ShellState, __args: string[]): Promise<CommandOutput<ShellError>> {
|
||||||
|
io.writeStdout(new TextEncoder().encode("\x1b[2J\x1b[0;0H"));
|
||||||
|
|
||||||
|
const result: ResultSuccess = createSuccessResult(COMMAND_NAME);
|
||||||
|
|
||||||
|
return {
|
||||||
|
state: state,
|
||||||
|
result,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export const descriptor: CommandDescriptor = {
|
||||||
|
fn: main,
|
||||||
|
name: COMMAND_NAME,
|
||||||
|
};
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
import type { CommandDescriptor, CommandOutput } from "@core/command";
|
||||||
|
import type { CommandIO } from "@core/command_io";
|
||||||
|
import { createSuccessResult, type ResultSuccess } from "@core/result";
|
||||||
|
import type { ShellState } from "@core/shell_state";
|
||||||
|
import type { ShellError } from "@core/util/errors";
|
||||||
|
|
||||||
|
export const COMMAND_NAME = "echo";
|
||||||
|
|
||||||
|
//Need to implement escape sequences
|
||||||
|
async function main(io: CommandIO, state: ShellState, args: string[]): Promise<CommandOutput<ShellError>> {
|
||||||
|
const joined = args.join(" ");
|
||||||
|
const encoded = new TextEncoder().encode(joined + "\n");
|
||||||
|
|
||||||
|
io.writeStdout(encoded);
|
||||||
|
|
||||||
|
const result: ResultSuccess = createSuccessResult(COMMAND_NAME);
|
||||||
|
|
||||||
|
return {
|
||||||
|
state: state,
|
||||||
|
result,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export const descriptor: CommandDescriptor = {
|
||||||
|
fn: main,
|
||||||
|
name: COMMAND_NAME,
|
||||||
|
};
|
||||||
@@ -0,0 +1,490 @@
|
|||||||
|
import type { CommandDescriptor, CommandOutput } from "@core/command";
|
||||||
|
import type { CommandIO } from "@core/command_io";
|
||||||
|
import { TreeNodeType, type DirectoryNode, type TreeNode } from "@core/filesystem/treenode";
|
||||||
|
import { createErrorResult, createSuccessResult } from "@core/result";
|
||||||
|
import { getCurrentSession } from "@core/session_manager";
|
||||||
|
import type { ShellState } from "@core/shell_state";
|
||||||
|
import type { ShellError } from "@core/util/errors";
|
||||||
|
import { resolvePath } from "@core/util/path_resolver";
|
||||||
|
import { SortTreeNodeBy, sortTreeNodes } from "@core/util/sort";
|
||||||
|
import { concatUint8Arrays } from "@termos/util/concat";
|
||||||
|
import { convertToUnit } from "@termos/util/format_unit";
|
||||||
|
|
||||||
|
export const COMMAND_NAME = "ls";
|
||||||
|
|
||||||
|
async function main(io: CommandIO, state: ShellState, args: string[]): Promise<CommandOutput<ShellError>> {
|
||||||
|
const entries: { inode: number; path: string }[] = [];
|
||||||
|
const paths: string[] = [];
|
||||||
|
const e = new TextEncoder();
|
||||||
|
|
||||||
|
let options = {
|
||||||
|
all: false,
|
||||||
|
almostAll: false,
|
||||||
|
long: false,
|
||||||
|
noSort: false,
|
||||||
|
noOwner: false,
|
||||||
|
noGroup: false,
|
||||||
|
sortBy: SortTreeNodeBy.NAME,
|
||||||
|
humanReadable: false,
|
||||||
|
f: false,
|
||||||
|
x: false,
|
||||||
|
timestamp: "atime",
|
||||||
|
reverse: false,
|
||||||
|
recursive: false,
|
||||||
|
quoteName: false,
|
||||||
|
appendIndicator: false,
|
||||||
|
numericUidGid: false,
|
||||||
|
literal: false,
|
||||||
|
inode: false,
|
||||||
|
si: false,
|
||||||
|
dereference: false,
|
||||||
|
fullTime: false,
|
||||||
|
};
|
||||||
|
|
||||||
|
for (let arg of args) {
|
||||||
|
let val;
|
||||||
|
if (arg.startsWith("--")) {
|
||||||
|
arg = arg.slice(2);
|
||||||
|
|
||||||
|
if (arg.match(/\w+[=]{1}\w+/g)) {
|
||||||
|
const flag = arg.split("=")[0];
|
||||||
|
val = arg.split("=")[1] ?? "";
|
||||||
|
|
||||||
|
if (flag === "time" && isValidTimestamp(val)) {
|
||||||
|
options.timestamp = val;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (flag === "sort" && isValidSortMethod(val)) {
|
||||||
|
options.sortBy = val as SortTreeNodeBy;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
state,
|
||||||
|
result: createErrorResult("UsageError", `Invalid options`, {}, COMMAND_NAME),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (arg) {
|
||||||
|
case "all":
|
||||||
|
options.all = true;
|
||||||
|
break;
|
||||||
|
case "almost-all":
|
||||||
|
options.almostAll = true;
|
||||||
|
break;
|
||||||
|
case "no-group":
|
||||||
|
options.noGroup = true;
|
||||||
|
break;
|
||||||
|
case "human-readable":
|
||||||
|
options.humanReadable = true;
|
||||||
|
break;
|
||||||
|
case "reverse":
|
||||||
|
options.reverse = true;
|
||||||
|
break;
|
||||||
|
case "recursive":
|
||||||
|
options.recursive = true;
|
||||||
|
break;
|
||||||
|
case "quote-name":
|
||||||
|
options.quoteName = true;
|
||||||
|
break;
|
||||||
|
case "numeric-uid-gid":
|
||||||
|
options.numericUidGid = true;
|
||||||
|
break;
|
||||||
|
case "inode":
|
||||||
|
options.inode = true;
|
||||||
|
break;
|
||||||
|
case "dereference":
|
||||||
|
options.dereference = true;
|
||||||
|
break;
|
||||||
|
case "literal":
|
||||||
|
options.literal = true;
|
||||||
|
break;
|
||||||
|
case "si":
|
||||||
|
options.si = true;
|
||||||
|
options.humanReadable = true;
|
||||||
|
break;
|
||||||
|
case "full-time":
|
||||||
|
options.fullTime = true;
|
||||||
|
break;
|
||||||
|
case "help":
|
||||||
|
// handled separately
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
return {
|
||||||
|
state,
|
||||||
|
result: createErrorResult("UsageError", `Invalid options`, {}, COMMAND_NAME),
|
||||||
|
};
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} else if (arg.startsWith("-")) {
|
||||||
|
arg = arg.slice(1);
|
||||||
|
|
||||||
|
for (const c of arg) {
|
||||||
|
switch (c) {
|
||||||
|
case "l":
|
||||||
|
options.long = true;
|
||||||
|
break;
|
||||||
|
case "a":
|
||||||
|
options.all = true;
|
||||||
|
break;
|
||||||
|
case "A":
|
||||||
|
options.almostAll = true;
|
||||||
|
break;
|
||||||
|
case "c":
|
||||||
|
options.timestamp = "ctime";
|
||||||
|
break;
|
||||||
|
case "U":
|
||||||
|
options.noSort = true;
|
||||||
|
break;
|
||||||
|
case "g":
|
||||||
|
options.noOwner = true;
|
||||||
|
options.long = true;
|
||||||
|
break;
|
||||||
|
case "G":
|
||||||
|
options.noGroup = true;
|
||||||
|
break;
|
||||||
|
case "h":
|
||||||
|
options.humanReadable = true;
|
||||||
|
break;
|
||||||
|
case "f":
|
||||||
|
options.f = true;
|
||||||
|
options.noSort = true;
|
||||||
|
options.all = true;
|
||||||
|
break;
|
||||||
|
case "x":
|
||||||
|
options.x = true;
|
||||||
|
break;
|
||||||
|
case "X":
|
||||||
|
options.sortBy = SortTreeNodeBy.EXTENSION;
|
||||||
|
break;
|
||||||
|
case "u":
|
||||||
|
options.timestamp = "atime";
|
||||||
|
break;
|
||||||
|
case "t":
|
||||||
|
options.sortBy = options.timestamp as SortTreeNodeBy;
|
||||||
|
break;
|
||||||
|
case "S":
|
||||||
|
options.sortBy = SortTreeNodeBy.SIZE;
|
||||||
|
break;
|
||||||
|
case "r":
|
||||||
|
options.reverse = true;
|
||||||
|
break;
|
||||||
|
case "R":
|
||||||
|
options.recursive = true;
|
||||||
|
break;
|
||||||
|
case "Q":
|
||||||
|
options.quoteName = true;
|
||||||
|
break;
|
||||||
|
case "p":
|
||||||
|
options.appendIndicator = true;
|
||||||
|
break;
|
||||||
|
case "o":
|
||||||
|
options.noGroup = true;
|
||||||
|
options.long = true;
|
||||||
|
break;
|
||||||
|
case "n":
|
||||||
|
options.numericUidGid = true;
|
||||||
|
break;
|
||||||
|
case "N":
|
||||||
|
options.literal = true;
|
||||||
|
break;
|
||||||
|
case "i":
|
||||||
|
options.inode = true;
|
||||||
|
break;
|
||||||
|
case "L":
|
||||||
|
options.dereference = true;
|
||||||
|
break;
|
||||||
|
case "m":
|
||||||
|
// -m: comma separated list (not in options yet)
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
return {
|
||||||
|
state,
|
||||||
|
result: createErrorResult("UsageError", `Invalid options`, {}, COMMAND_NAME),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
paths.push(arg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const session = getCurrentSession(state);
|
||||||
|
|
||||||
|
if (paths.length === 0) {
|
||||||
|
entries.push({ inode: session.cwd, path: "." });
|
||||||
|
} else {
|
||||||
|
for (const path of paths) {
|
||||||
|
const inode = resolvePath(state, path);
|
||||||
|
if (inode === -1) {
|
||||||
|
return {
|
||||||
|
state,
|
||||||
|
result: createErrorResult(
|
||||||
|
"PathResolutionError",
|
||||||
|
`No such file or directory`,
|
||||||
|
{ path },
|
||||||
|
COMMAND_NAME,
|
||||||
|
),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
entries.push({ inode: inode, path });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (options.long) {
|
||||||
|
for (const entry of entries) {
|
||||||
|
listDirEntries(entry);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for (const entry of entries) {
|
||||||
|
//shortDirEntries(entry);
|
||||||
|
}
|
||||||
|
io.writeStdout(e.encode(`No support for short format yet\n`));
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
state,
|
||||||
|
result: createSuccessResult(COMMAND_NAME),
|
||||||
|
};
|
||||||
|
|
||||||
|
function listDirEntries(entry: { inode: number; path: string }): void {
|
||||||
|
const node = state.filesystem.getNode(entry.inode) as DirectoryNode;
|
||||||
|
|
||||||
|
if (options.recursive) io.writeStdout(e.encode(`${entry.path}:\n`));
|
||||||
|
io.writeStdout(e.encode(`total ${Object.entries(node.children).length}\n`));
|
||||||
|
|
||||||
|
let children: TreeNode[] = Object.entries(node.children).map(c => state.filesystem.getNode(c[1])!);
|
||||||
|
if (!options.noSort) children = sortTreeNodes(state, children, options.reverse, options.sortBy);
|
||||||
|
|
||||||
|
if (!options.all && !options.almostAll) children = children.filter(c => !c.name.startsWith("."));
|
||||||
|
|
||||||
|
if (options.long) {
|
||||||
|
longMode(io, state, children, node);
|
||||||
|
if (options.recursive) io.writeStdout(e.encode("\n"));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (options.recursive) {
|
||||||
|
for (const child of children) {
|
||||||
|
if (child.type === TreeNodeType.Directory) {
|
||||||
|
let path;
|
||||||
|
if (entry.path === "/") {
|
||||||
|
path = "/" + child.name;
|
||||||
|
} else if (entry.path.endsWith("/")) {
|
||||||
|
path = entry.path + child.name;
|
||||||
|
} else path = entry.path + "/" + child.name;
|
||||||
|
listDirEntries({ inode: child.inode, path });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function longMode(io: CommandIO, state: ShellState, children: TreeNode[], current: TreeNode): void {
|
||||||
|
const e = new TextEncoder();
|
||||||
|
const buff: Uint8Array[] = [];
|
||||||
|
const nodes: TreeNode[] = [...children];
|
||||||
|
|
||||||
|
if (options.all) {
|
||||||
|
const parentNode = state.filesystem.getNode(current.parent)!;
|
||||||
|
|
||||||
|
const parent: TreeNode = { ...parentNode, name: ".." };
|
||||||
|
nodes.unshift(parent);
|
||||||
|
|
||||||
|
const cwd: TreeNode = { ...current, name: "." };
|
||||||
|
nodes.unshift(cwd);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Calculate the string size of the biggerst node in the array;
|
||||||
|
// Sets 4 length for directories because the arbitrary size is 4096 later in the code
|
||||||
|
const maxInodeWidth = Math.max(...nodes.map(c => c.inode.toString().length));
|
||||||
|
|
||||||
|
const maxSizeWidth = Math.max(
|
||||||
|
...nodes.map(c => (c.type === TreeNodeType.File ? c.content.byteSize().toString().length : 4)),
|
||||||
|
);
|
||||||
|
|
||||||
|
const maxChildrenWidth = Math.max(
|
||||||
|
...nodes.map(c =>
|
||||||
|
c.type === TreeNodeType.Directory ? Object.entries(c.children).length.toString().length : 2,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
const namePadding = nodes.every(c => !c.name.includes(" "));
|
||||||
|
|
||||||
|
for (const node of nodes) {
|
||||||
|
//Inode
|
||||||
|
if (options.inode) {
|
||||||
|
const inode = node.inode.toString().padStart(maxInodeWidth, " ");
|
||||||
|
buff.push(e.encode(inode + " "));
|
||||||
|
//io.writeStdout(e.encode(inode + " "));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Mode
|
||||||
|
const mode: string = parseMode(node.mode);
|
||||||
|
|
||||||
|
switch (node.type) {
|
||||||
|
case TreeNodeType.Directory:
|
||||||
|
buff.push(e.encode("d" + mode + " "));
|
||||||
|
break;
|
||||||
|
case TreeNodeType.File:
|
||||||
|
buff.push(e.encode("-" + mode + " "));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Children amnt (later will be amnt of links)
|
||||||
|
const childAmnt =
|
||||||
|
node.type === TreeNodeType.Directory
|
||||||
|
? `${Object.entries(node.children).length.toString().padStart(maxChildrenWidth, " ")} `
|
||||||
|
: `${"10".padStart(maxChildrenWidth, " ")} `;
|
||||||
|
|
||||||
|
buff.push(e.encode(childAmnt));
|
||||||
|
|
||||||
|
// owner group
|
||||||
|
if (options.noGroup) {
|
||||||
|
buff.push(e.encode(`${state.users.get(node.owner)?.username} `));
|
||||||
|
} else if (options.noOwner) {
|
||||||
|
buff.push(e.encode(`${state.groups.get(node.group)?.groupname} `));
|
||||||
|
} else if (options.numericUidGid) {
|
||||||
|
buff.push(e.encode(`${node.owner} ${node.group} `));
|
||||||
|
} else {
|
||||||
|
buff.push(
|
||||||
|
e.encode(`${state.users.get(node.owner)?.username} ${state.groups.get(node.group)?.groupname} `),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
//Size
|
||||||
|
// Holy abomination, pad the stringified bytesize number with whitespaces so the actual length of the string matches across all rows
|
||||||
|
// Sets the folder size to an arbitrary 4096, could change it later to smth else
|
||||||
|
let size;
|
||||||
|
if (options.humanReadable) {
|
||||||
|
const bytes = node.type === TreeNodeType.File ? node.content.byteSize() : 4096;
|
||||||
|
size = convertToUnit(bytes, 1, options.si ? 1000 : 1024);
|
||||||
|
} else {
|
||||||
|
size =
|
||||||
|
node.type === TreeNodeType.File
|
||||||
|
? `${node.content.byteSize().toString().padStart(maxSizeWidth, " ")} `
|
||||||
|
: `${"4096".padStart(maxSizeWidth, " ")} `;
|
||||||
|
}
|
||||||
|
|
||||||
|
buff.push(e.encode(size));
|
||||||
|
|
||||||
|
//Timestamp
|
||||||
|
// for now only mtime, need a helper function for all the formatting later on
|
||||||
|
const date = getDateTimestamp(node, options.timestamp);
|
||||||
|
if (options.fullTime) {
|
||||||
|
buff.push(
|
||||||
|
// Month + 1 because January is 0
|
||||||
|
e.encode(
|
||||||
|
`${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()} ${date.getHours()}:${date.getMinutes()} `,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
buff.push(
|
||||||
|
// Month + 1 because January is 0
|
||||||
|
e.encode(`${date.getMonth() + 1}-${date.getDate()} ${date.getHours()}:${date.getMinutes()} `),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
//Name
|
||||||
|
if (options.quoteName) {
|
||||||
|
buff.push(
|
||||||
|
e.encode(
|
||||||
|
`"${node.type === TreeNodeType.Directory ? node.name + '"' + (options.appendIndicator ? "/" : "") : node.name + '"'}\n`,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
const unpadded = `${node.name.includes(" ") && !options.literal ? "'" + node.name + "'" : node.name}`;
|
||||||
|
const name = `${!namePadding ? (unpadded.includes(" ") ? unpadded : " " + unpadded) : unpadded}`;
|
||||||
|
buff.push(
|
||||||
|
e.encode(
|
||||||
|
`${node.type === TreeNodeType.Directory ? name + (options.appendIndicator ? "/" : "") : name}\n`,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const out = concatUint8Arrays(buff);
|
||||||
|
io.writeStdout(out);
|
||||||
|
}
|
||||||
|
|
||||||
|
function getDateTimestamp(node: TreeNode, ts: string): Date {
|
||||||
|
if (ts === "atime") return new Date(node.atime);
|
||||||
|
if (ts === "ctime") return new Date(node.ctime);
|
||||||
|
if (ts === "mtime") return new Date(node.mtime);
|
||||||
|
return new Date();
|
||||||
|
}
|
||||||
|
|
||||||
|
function parseMode(mode: number) {
|
||||||
|
const owner = (mode >> 6) & 7;
|
||||||
|
const group = (mode >> 3) & 7;
|
||||||
|
const other = mode & 7;
|
||||||
|
|
||||||
|
const map = (bits: number) => {
|
||||||
|
let s = "";
|
||||||
|
s += bits & 4 ? "r" : "-";
|
||||||
|
s += bits & 2 ? "w" : "-";
|
||||||
|
s += bits & 1 ? "x" : "-";
|
||||||
|
return s;
|
||||||
|
};
|
||||||
|
|
||||||
|
return map(owner) + map(group) + map(other);
|
||||||
|
}
|
||||||
|
|
||||||
|
function isValidSortMethod(value: string): value is SortTreeNodeBy {
|
||||||
|
return Object.values(SortTreeNodeBy).includes(value as SortTreeNodeBy);
|
||||||
|
}
|
||||||
|
|
||||||
|
function isValidTimestamp(
|
||||||
|
value: string,
|
||||||
|
): value is SortTreeNodeBy.ATIME | SortTreeNodeBy.MTIME | SortTreeNodeBy.CTIME {
|
||||||
|
return Object.values(SortTreeNodeBy).includes(
|
||||||
|
value as SortTreeNodeBy.ATIME | SortTreeNodeBy.MTIME | SortTreeNodeBy.CTIME,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//function normalMode(io: CommandIO, state: ShellState, flags: string[], args: string[]): void {}
|
||||||
|
|
||||||
|
export const descriptor: CommandDescriptor = {
|
||||||
|
fn: main,
|
||||||
|
flags: [
|
||||||
|
{ name: "l" }, // done
|
||||||
|
{ name: "a" }, // done
|
||||||
|
{ name: "A" }, // done
|
||||||
|
{ name: "c" },
|
||||||
|
{ name: "U" }, // done
|
||||||
|
{ name: "g" }, // done
|
||||||
|
{ name: "G" }, // done
|
||||||
|
{ name: "h" }, // done
|
||||||
|
{ name: "f" }, // done
|
||||||
|
{ name: "x" },
|
||||||
|
{ name: "X" }, // done
|
||||||
|
{ name: "u" },
|
||||||
|
{ name: "t" }, // done
|
||||||
|
{ name: "S" }, // done
|
||||||
|
{ name: "r" }, // done
|
||||||
|
{ name: "R" }, // done
|
||||||
|
{ name: "Q" }, // done
|
||||||
|
{ name: "p" }, // done
|
||||||
|
{ name: "o" }, // done
|
||||||
|
{ name: "n" }, // done
|
||||||
|
{ name: "N" }, // done
|
||||||
|
{ name: "L" },
|
||||||
|
{ name: "m" },
|
||||||
|
{ name: "i" }, // done
|
||||||
|
{ name: "sort", takesValue: true }, // done
|
||||||
|
{ name: "time", takesValue: true }, // done
|
||||||
|
{ name: "help" },
|
||||||
|
{ name: "all" }, // done
|
||||||
|
{ name: "almost-all" }, // done
|
||||||
|
{ name: "full-time" }, // done
|
||||||
|
{ name: "no-group" }, // done
|
||||||
|
{ name: "human-readable" }, // done
|
||||||
|
{ name: "reverse" }, // done
|
||||||
|
{ name: "recursive" }, // done
|
||||||
|
{ name: "quote-name" }, // done
|
||||||
|
{ name: "literal" }, // done
|
||||||
|
{ name: "numeric-uid-gid" }, // done
|
||||||
|
{ name: "inode" }, // done
|
||||||
|
{ name: "si" }, // done
|
||||||
|
{ name: "dereference" },
|
||||||
|
],
|
||||||
|
name: "ls",
|
||||||
|
};
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
import type { CommandDescriptor, CommandOutput } from "@core/command";
|
||||||
|
import type { CommandIO } from "@core/command_io";
|
||||||
|
import { createErrorResult, createSuccessResult, type ResultSuccess } from "@core/result";
|
||||||
|
import { getCurrentSession } from "@core/session_manager";
|
||||||
|
import type { ShellState } from "@core/shell_state";
|
||||||
|
import type { InvalidArgumentsError, ShellError } from "@core/util/errors";
|
||||||
|
import { resolveInode } from "@core/util/path_resolver";
|
||||||
|
|
||||||
|
export const COMMAND_NAME = "pwd";
|
||||||
|
|
||||||
|
//Need to implement escape sequences
|
||||||
|
async function main(io: CommandIO, state: ShellState, args: string[]): Promise<CommandOutput<ShellError>> {
|
||||||
|
if (args.length > 0) {
|
||||||
|
return {
|
||||||
|
state,
|
||||||
|
result: createErrorResult(
|
||||||
|
"InvalidArguments",
|
||||||
|
`Expected aruments 0, got ${args.length}.`,
|
||||||
|
{ expected: "pwd", actual: `pwd ${args.toString().replace(/[,]/g, " ")}` },
|
||||||
|
COMMAND_NAME,
|
||||||
|
),
|
||||||
|
} satisfies CommandOutput<InvalidArgumentsError>;
|
||||||
|
}
|
||||||
|
|
||||||
|
const session = getCurrentSession(state);
|
||||||
|
const path = resolveInode(state, session.cwd);
|
||||||
|
|
||||||
|
io.writeStdout(new TextEncoder().encode(path));
|
||||||
|
|
||||||
|
const result: ResultSuccess = createSuccessResult(COMMAND_NAME);
|
||||||
|
|
||||||
|
return {
|
||||||
|
state: state,
|
||||||
|
result,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export const descriptor: CommandDescriptor = {
|
||||||
|
fn: main,
|
||||||
|
name: COMMAND_NAME,
|
||||||
|
};
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
import type { CommandDescriptor, CommandOutput } from "@core/command";
|
||||||
|
import type { CommandIO } from "@core/command_io";
|
||||||
|
import type { ResultSuccess } from "@core/result";
|
||||||
|
import type { ShellState } from "@core/shell_state";
|
||||||
|
import { SIGNAL_MAP } from "@core/signal";
|
||||||
|
import type { ShellError } from "@core/util/errors";
|
||||||
|
import { createImmutableBuffer } from "@core/util/immutable_buffer";
|
||||||
|
|
||||||
|
const COMMAND_NAME = "async";
|
||||||
|
|
||||||
|
async function main(io: CommandIO, state: ShellState, __args: string[]): Promise<CommandOutput<ShellError>> {
|
||||||
|
while (true) {
|
||||||
|
const out = await io.getSignal();
|
||||||
|
|
||||||
|
if (out.type === SIGNAL_MAP.INPUT) {
|
||||||
|
const encoded = new TextEncoder().encode(("user pressed: " + out.payload + "\n") as string);
|
||||||
|
io.writeStdout(encoded);
|
||||||
|
|
||||||
|
if (out.payload === "x") {
|
||||||
|
const result: ResultSuccess = {
|
||||||
|
type: "success",
|
||||||
|
exitCode: 0,
|
||||||
|
stdout: createImmutableBuffer(new Uint8Array()),
|
||||||
|
stderr: createImmutableBuffer(new Uint8Array()),
|
||||||
|
commandName: COMMAND_NAME,
|
||||||
|
};
|
||||||
|
|
||||||
|
return {
|
||||||
|
state: state,
|
||||||
|
result,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const descriptor = {
|
||||||
|
fn: main,
|
||||||
|
name: COMMAND_NAME,
|
||||||
|
} satisfies CommandDescriptor;
|
||||||
@@ -0,0 +1,54 @@
|
|||||||
|
import type { CommandDescriptor, CommandOutput } from "@core/command";
|
||||||
|
import type { CommandIO } from "@core/command_io";
|
||||||
|
import { TreeNodeType } from "@core/filesystem/treenode";
|
||||||
|
import { createSuccessResult, type ResultSuccess } from "@core/result";
|
||||||
|
import { getCurrentSession } from "@core/session_manager";
|
||||||
|
import type { ShellState } from "@core/shell_state";
|
||||||
|
import type { ShellError } from "@core/util/errors";
|
||||||
|
import { resolvePath } from "@core/util/path_resolver";
|
||||||
|
|
||||||
|
export const COMMAND_NAME = "touch";
|
||||||
|
|
||||||
|
//Need to implement escape sequences
|
||||||
|
async function main(io: CommandIO, state: ShellState, args: string[]): Promise<CommandOutput<ShellError>> {
|
||||||
|
for (const arg of args) {
|
||||||
|
const inode = resolvePath(state, arg);
|
||||||
|
const session = getCurrentSession(state);
|
||||||
|
|
||||||
|
let newState;
|
||||||
|
|
||||||
|
if (inode === -1) {
|
||||||
|
const index = arg.lastIndexOf("/");
|
||||||
|
let parentPath;
|
||||||
|
if (index === -1) {
|
||||||
|
parentPath = "./";
|
||||||
|
} else {
|
||||||
|
parentPath = arg.slice(0, index);
|
||||||
|
}
|
||||||
|
const fileName = arg.slice(index + 1);
|
||||||
|
const parent = resolvePath(state, parentPath);
|
||||||
|
|
||||||
|
newState = state.filesystem.createNode(
|
||||||
|
parent,
|
||||||
|
fileName,
|
||||||
|
TreeNodeType.File,
|
||||||
|
0o644,
|
||||||
|
session.user.uid,
|
||||||
|
session.user.gid,
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const result: ResultSuccess = createSuccessResult(COMMAND_NAME);
|
||||||
|
|
||||||
|
return {
|
||||||
|
state: state,
|
||||||
|
result,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export const descriptor: CommandDescriptor = {
|
||||||
|
fn: main,
|
||||||
|
name: COMMAND_NAME,
|
||||||
|
};
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
import type { TreeNodeType } from "./treenode";
|
||||||
|
|
||||||
|
export type FileSystemError =
|
||||||
|
NotADirectoryError | AlreadyExistsError | NodeNotFoundError | IllegalMutationError | FileSystemInitializationError;
|
||||||
|
|
||||||
|
export interface FileSystemInitializationError {
|
||||||
|
type: "FileSystemInitializationError";
|
||||||
|
message: string;
|
||||||
|
timestamp: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface NotADirectoryError {
|
||||||
|
inode: number;
|
||||||
|
message: string;
|
||||||
|
timestamp: number;
|
||||||
|
type: "NotADirectory";
|
||||||
|
nodeType: TreeNodeType;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface AlreadyExistsError {
|
||||||
|
type: "AlreadyExists";
|
||||||
|
message: string;
|
||||||
|
timestamp: number;
|
||||||
|
parentInode: number;
|
||||||
|
name: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface NodeNotFoundError {
|
||||||
|
type: "NodeNotFound";
|
||||||
|
message: string;
|
||||||
|
inode: number;
|
||||||
|
timestamp: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface IllegalMutationError {
|
||||||
|
type: "IllegalMutation";
|
||||||
|
message: string;
|
||||||
|
timestamp: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface DirectoryLoopError {
|
||||||
|
type: "DirectoryLoop";
|
||||||
|
message: string;
|
||||||
|
timestamp: number;
|
||||||
|
descendantInode: number;
|
||||||
|
ancestorInode: number;
|
||||||
|
}
|
||||||
@@ -0,0 +1,573 @@
|
|||||||
|
import type { UserInfo } from "@core/session_manager";
|
||||||
|
import { createImmutableBuffer, type ImmutableBuffer } from "@core/util/immutable_buffer";
|
||||||
|
import { dnTraverseTree, upTraverseTree } from "@core/util/tree_traversal";
|
||||||
|
import type {
|
||||||
|
AlreadyExistsError,
|
||||||
|
DirectoryLoopError,
|
||||||
|
FileSystemInitializationError,
|
||||||
|
IllegalMutationError,
|
||||||
|
NodeNotFoundError,
|
||||||
|
NotADirectoryError,
|
||||||
|
} from "./errors";
|
||||||
|
import { TreeNodeType, type DirectoryNode, type FileNode, type TreeNode } from "./treenode";
|
||||||
|
|
||||||
|
export class FileSystem {
|
||||||
|
private _BaseFS: ReadonlyMap<number, TreeNode>;
|
||||||
|
private _OverlayFS: ReadonlyMap<number, TreeNode | null>;
|
||||||
|
|
||||||
|
private _INodeIndex: number;
|
||||||
|
|
||||||
|
private constructor(
|
||||||
|
base: ReadonlyMap<number, TreeNode>,
|
||||||
|
overlay: ReadonlyMap<number, TreeNode | null>,
|
||||||
|
index: number,
|
||||||
|
) {
|
||||||
|
this._BaseFS = base;
|
||||||
|
this._OverlayFS = overlay;
|
||||||
|
this._INodeIndex = index;
|
||||||
|
}
|
||||||
|
|
||||||
|
private resolveNode(inode: number): TreeNode | undefined {
|
||||||
|
const node = this._OverlayFS.get(inode);
|
||||||
|
if (node) return node;
|
||||||
|
if (node === null) return undefined;
|
||||||
|
|
||||||
|
return this._BaseFS.get(inode);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static init(
|
||||||
|
base: ReadonlyMap<number, TreeNode>,
|
||||||
|
overlay: ReadonlyMap<number, TreeNode | null>,
|
||||||
|
index: number,
|
||||||
|
): FileSystem {
|
||||||
|
if (base.get(0) === undefined) {
|
||||||
|
throw {
|
||||||
|
type: "FileSystemInitializationError",
|
||||||
|
message: "File System failed to initialize because root node is undefined or not assigned to inode 0",
|
||||||
|
timestamp: Date.now(),
|
||||||
|
} satisfies FileSystemInitializationError;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (base.get(0)?.type !== TreeNodeType.Directory) {
|
||||||
|
throw {
|
||||||
|
type: "FileSystemInitializationError",
|
||||||
|
message: "File System failed to initialize because root node is not a directory",
|
||||||
|
timestamp: Date.now(),
|
||||||
|
} satisfies FileSystemInitializationError;
|
||||||
|
}
|
||||||
|
|
||||||
|
return new FileSystem(base, overlay, index);
|
||||||
|
}
|
||||||
|
|
||||||
|
public getRoot(): DirectoryNode {
|
||||||
|
const node = this.resolveNode(0);
|
||||||
|
if (node === undefined) throw new Error("FileSystem corrupted! INode 0 did not resolve properly.");
|
||||||
|
return node as DirectoryNode;
|
||||||
|
}
|
||||||
|
|
||||||
|
public getNode(inode: number): TreeNode | undefined {
|
||||||
|
return this.resolveNode(inode);
|
||||||
|
}
|
||||||
|
|
||||||
|
public exists(inode: number): boolean {
|
||||||
|
return this.resolveNode(inode) !== undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
public isFile(inode: number): boolean {
|
||||||
|
const node = this.resolveNode(inode);
|
||||||
|
|
||||||
|
if (node === undefined) {
|
||||||
|
throw {
|
||||||
|
type: "NodeNotFound",
|
||||||
|
message: `Node under inode ${inode} not found`,
|
||||||
|
timestamp: Date.now(),
|
||||||
|
inode: inode,
|
||||||
|
} satisfies NodeNotFoundError;
|
||||||
|
}
|
||||||
|
|
||||||
|
return node.type === TreeNodeType.File ? true : false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public isDirectory(inode: number): boolean {
|
||||||
|
const node = this.resolveNode(inode);
|
||||||
|
|
||||||
|
if (node === undefined) {
|
||||||
|
throw {
|
||||||
|
type: "NodeNotFound",
|
||||||
|
message: `Node under inode ${inode} not found`,
|
||||||
|
timestamp: Date.now(),
|
||||||
|
inode: inode,
|
||||||
|
} satisfies NodeNotFoundError;
|
||||||
|
}
|
||||||
|
|
||||||
|
return node.type === TreeNodeType.Directory ? true : false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* public isSymlink(inode: number): boolean {
|
||||||
|
const node = this.resolveNode(inode);
|
||||||
|
|
||||||
|
if (node === undefined) {
|
||||||
|
throw {
|
||||||
|
type: "NodeNotFound",
|
||||||
|
message: `Node under inode ${inode} not found`,
|
||||||
|
timestamp: Date.now(),
|
||||||
|
inode: inode,
|
||||||
|
} satisfies NodeNotFoundError;
|
||||||
|
}
|
||||||
|
|
||||||
|
return node.type === TreeNodeType.Symlink ? true : false;
|
||||||
|
} */
|
||||||
|
|
||||||
|
public moveNode(sourceInode: number, destinationInode: number, name?: string): FileSystem {
|
||||||
|
if (sourceInode === 0) {
|
||||||
|
throw {
|
||||||
|
type: "IllegalMutation",
|
||||||
|
message: `Attempt at illegal mutation of node under inode 0 (root)`,
|
||||||
|
timestamp: Date.now(),
|
||||||
|
} satisfies IllegalMutationError;
|
||||||
|
}
|
||||||
|
|
||||||
|
const sourceNode = this.resolveNode(sourceInode);
|
||||||
|
const destinationNode = this.resolveNode(destinationInode);
|
||||||
|
|
||||||
|
if (sourceNode === undefined) {
|
||||||
|
throw {
|
||||||
|
type: "NodeNotFound",
|
||||||
|
message: `Source node under inode ${sourceInode} not found`,
|
||||||
|
timestamp: Date.now(),
|
||||||
|
inode: sourceInode,
|
||||||
|
} satisfies NodeNotFoundError;
|
||||||
|
}
|
||||||
|
|
||||||
|
const nodeName = name ?? sourceNode.name;
|
||||||
|
|
||||||
|
if (destinationNode === undefined) {
|
||||||
|
throw {
|
||||||
|
type: "NodeNotFound",
|
||||||
|
message: `Destination node under inode ${destinationInode} not found`,
|
||||||
|
timestamp: Date.now(),
|
||||||
|
inode: destinationInode,
|
||||||
|
} satisfies NodeNotFoundError;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (destinationNode.type !== TreeNodeType.Directory) {
|
||||||
|
throw {
|
||||||
|
type: "NotADirectory",
|
||||||
|
message: `Destination node under inode ${destinationInode} is not a directory`,
|
||||||
|
inode: destinationInode,
|
||||||
|
timestamp: Date.now(),
|
||||||
|
nodeType: destinationNode.type,
|
||||||
|
} satisfies NotADirectoryError;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (destinationNode.children[nodeName] !== undefined) {
|
||||||
|
throw {
|
||||||
|
type: "AlreadyExists",
|
||||||
|
message: `A node inside the destination inode ${destinationNode} under the name ${nodeName} already exists`,
|
||||||
|
timestamp: Date.now(),
|
||||||
|
parentInode: destinationInode,
|
||||||
|
name: nodeName,
|
||||||
|
} satisfies AlreadyExistsError;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (upTraverseTree(this, destinationInode).includes(sourceInode)) {
|
||||||
|
throw {
|
||||||
|
type: "DirectoryLoop",
|
||||||
|
message: "Cannot move node to a descendant of itself",
|
||||||
|
timestamp: Date.now(),
|
||||||
|
descendantInode: destinationInode,
|
||||||
|
ancestorInode: sourceInode,
|
||||||
|
} satisfies DirectoryLoopError;
|
||||||
|
}
|
||||||
|
|
||||||
|
let newNode: TreeNode;
|
||||||
|
|
||||||
|
if (sourceNode.type === TreeNodeType.File) {
|
||||||
|
const fileNode: FileNode = {
|
||||||
|
type: sourceNode.type,
|
||||||
|
inode: sourceNode.inode,
|
||||||
|
name: nodeName,
|
||||||
|
parent: destinationInode,
|
||||||
|
mode: sourceNode.mode,
|
||||||
|
owner: sourceNode.owner,
|
||||||
|
group: sourceNode.group,
|
||||||
|
mtime: sourceNode.mtime,
|
||||||
|
atime: sourceNode.atime,
|
||||||
|
ctime: Date.now(),
|
||||||
|
content: sourceNode.content,
|
||||||
|
};
|
||||||
|
|
||||||
|
newNode = fileNode;
|
||||||
|
} else {
|
||||||
|
const directoryNode: DirectoryNode = {
|
||||||
|
type: sourceNode.type,
|
||||||
|
inode: sourceNode.inode,
|
||||||
|
name: nodeName,
|
||||||
|
parent: destinationInode,
|
||||||
|
mode: sourceNode.mode,
|
||||||
|
owner: sourceNode.owner,
|
||||||
|
group: sourceNode.group,
|
||||||
|
mtime: sourceNode.mtime,
|
||||||
|
atime: sourceNode.atime,
|
||||||
|
ctime: Date.now(),
|
||||||
|
children: sourceNode.children,
|
||||||
|
};
|
||||||
|
|
||||||
|
newNode = directoryNode;
|
||||||
|
}
|
||||||
|
|
||||||
|
const map = new Map(this.Overlay);
|
||||||
|
map.set(sourceInode, Object.freeze(newNode));
|
||||||
|
|
||||||
|
const parentNode = this.resolveNode(sourceNode.parent) as DirectoryNode;
|
||||||
|
const { [sourceNode.name]: __, ...newChildren } = parentNode.children;
|
||||||
|
|
||||||
|
if (sourceNode.parent === destinationInode) {
|
||||||
|
const newDestinationNode: DirectoryNode = {
|
||||||
|
type: destinationNode.type,
|
||||||
|
inode: destinationInode,
|
||||||
|
name: destinationNode.name,
|
||||||
|
parent: destinationNode.parent,
|
||||||
|
mode: destinationNode.mode,
|
||||||
|
owner: destinationNode.owner,
|
||||||
|
group: destinationNode.group,
|
||||||
|
mtime: Date.now(),
|
||||||
|
atime: destinationNode.atime,
|
||||||
|
ctime: Date.now(),
|
||||||
|
children: { ...newChildren, [nodeName]: sourceInode },
|
||||||
|
};
|
||||||
|
|
||||||
|
map.set(destinationInode, Object.freeze(newDestinationNode));
|
||||||
|
} else {
|
||||||
|
const newParentNode: DirectoryNode = {
|
||||||
|
type: parentNode.type,
|
||||||
|
inode: parentNode.inode,
|
||||||
|
name: parentNode.name,
|
||||||
|
parent: parentNode.parent,
|
||||||
|
mode: parentNode.mode,
|
||||||
|
owner: parentNode.owner,
|
||||||
|
group: parentNode.group,
|
||||||
|
mtime: Date.now(),
|
||||||
|
atime: parentNode.atime,
|
||||||
|
ctime: Date.now(),
|
||||||
|
children: newChildren,
|
||||||
|
};
|
||||||
|
|
||||||
|
const newDestinationNode: DirectoryNode = {
|
||||||
|
type: destinationNode.type,
|
||||||
|
inode: destinationInode,
|
||||||
|
name: destinationNode.name,
|
||||||
|
parent: destinationNode.parent,
|
||||||
|
mode: destinationNode.mode,
|
||||||
|
owner: destinationNode.owner,
|
||||||
|
group: destinationNode.group,
|
||||||
|
mtime: Date.now(),
|
||||||
|
atime: destinationNode.atime,
|
||||||
|
ctime: Date.now(),
|
||||||
|
children: { ...destinationNode.children, [nodeName]: sourceInode },
|
||||||
|
};
|
||||||
|
|
||||||
|
map.set(destinationInode, Object.freeze(newDestinationNode));
|
||||||
|
map.set(sourceNode.parent, Object.freeze(newParentNode));
|
||||||
|
}
|
||||||
|
|
||||||
|
const newOverlay: ReadonlyMap<number, TreeNode | null> = map;
|
||||||
|
return FileSystem.init(this._BaseFS, newOverlay, this._INodeIndex);
|
||||||
|
}
|
||||||
|
|
||||||
|
public modifyNode(
|
||||||
|
inode: number,
|
||||||
|
changes: {
|
||||||
|
content?: ImmutableBuffer;
|
||||||
|
mode?: number;
|
||||||
|
owner?: number;
|
||||||
|
group?: number;
|
||||||
|
},
|
||||||
|
): FileSystem {
|
||||||
|
const node = this.resolveNode(inode);
|
||||||
|
if (node === undefined) {
|
||||||
|
throw {
|
||||||
|
type: "NodeNotFound",
|
||||||
|
message: `Node under inode ${inode} not found`,
|
||||||
|
timestamp: Date.now(),
|
||||||
|
inode: inode,
|
||||||
|
} satisfies NodeNotFoundError;
|
||||||
|
}
|
||||||
|
|
||||||
|
let newNode: TreeNode;
|
||||||
|
|
||||||
|
if (node.type === TreeNodeType.File) {
|
||||||
|
const fileNode: FileNode = {
|
||||||
|
type: node.type,
|
||||||
|
inode: node.inode,
|
||||||
|
name: node.name,
|
||||||
|
parent: node.parent,
|
||||||
|
mode: changes.mode ?? node.mode,
|
||||||
|
owner: changes.owner ?? node.owner,
|
||||||
|
group: changes.group ?? node.group,
|
||||||
|
mtime: changes.content ? Date.now() : node.mtime,
|
||||||
|
atime: node.atime,
|
||||||
|
ctime: Date.now(),
|
||||||
|
content: changes.content ?? node.content,
|
||||||
|
};
|
||||||
|
|
||||||
|
newNode = fileNode;
|
||||||
|
} else {
|
||||||
|
const directoryNode: DirectoryNode = {
|
||||||
|
type: node.type,
|
||||||
|
inode: node.inode,
|
||||||
|
name: node.name,
|
||||||
|
parent: node.parent,
|
||||||
|
mode: changes.mode ?? node.mode,
|
||||||
|
owner: changes.owner ?? node.owner,
|
||||||
|
group: changes.group ?? node.group,
|
||||||
|
mtime: changes.content ? Date.now() : node.mtime,
|
||||||
|
atime: node.atime,
|
||||||
|
ctime: Date.now(),
|
||||||
|
children: node.children,
|
||||||
|
};
|
||||||
|
|
||||||
|
newNode = directoryNode;
|
||||||
|
}
|
||||||
|
|
||||||
|
const map = new Map(this.Overlay);
|
||||||
|
map.set(node.inode, Object.freeze(newNode));
|
||||||
|
|
||||||
|
const newOverlay: ReadonlyMap<number, TreeNode | null> = map;
|
||||||
|
|
||||||
|
return FileSystem.init(this._BaseFS, newOverlay, this._INodeIndex);
|
||||||
|
}
|
||||||
|
|
||||||
|
public createNode(
|
||||||
|
parent: number,
|
||||||
|
name: string,
|
||||||
|
type: TreeNodeType,
|
||||||
|
mode: number,
|
||||||
|
owner: number,
|
||||||
|
group: number,
|
||||||
|
content?: ImmutableBuffer,
|
||||||
|
): FileSystem {
|
||||||
|
let node: TreeNode;
|
||||||
|
|
||||||
|
const parentNode = this.resolveNode(parent);
|
||||||
|
const time: number = Date.now();
|
||||||
|
const newInode: number = this._INodeIndex + 1;
|
||||||
|
|
||||||
|
if (parentNode === undefined) {
|
||||||
|
throw {
|
||||||
|
type: "NodeNotFound",
|
||||||
|
message: `Parent inode ${parent} not found`,
|
||||||
|
timestamp: Date.now(),
|
||||||
|
inode: parent,
|
||||||
|
} satisfies NodeNotFoundError;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (parentNode.type === TreeNodeType.File) {
|
||||||
|
throw {
|
||||||
|
type: "NotADirectory",
|
||||||
|
message: `Parent inode ${parent} is not a directory`,
|
||||||
|
timestamp: Date.now(),
|
||||||
|
inode: parent,
|
||||||
|
nodeType: parentNode.type,
|
||||||
|
} satisfies NotADirectoryError;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (parentNode.type === TreeNodeType.Directory && parentNode.children[name] !== undefined) {
|
||||||
|
throw {
|
||||||
|
type: "AlreadyExists",
|
||||||
|
message: `Child under the name of ${name} inside the parent inode ${parent} already exists`,
|
||||||
|
timestamp: Date.now(),
|
||||||
|
parentInode: parent,
|
||||||
|
name,
|
||||||
|
} satisfies AlreadyExistsError;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (type === TreeNodeType.File) {
|
||||||
|
const fileNode: FileNode = {
|
||||||
|
type,
|
||||||
|
inode: newInode,
|
||||||
|
name,
|
||||||
|
parent,
|
||||||
|
mode,
|
||||||
|
owner,
|
||||||
|
group,
|
||||||
|
mtime: time,
|
||||||
|
atime: time,
|
||||||
|
ctime: time,
|
||||||
|
content: content ?? createImmutableBuffer(new Uint8Array()),
|
||||||
|
};
|
||||||
|
|
||||||
|
node = fileNode;
|
||||||
|
} else {
|
||||||
|
const directoryNode: DirectoryNode = {
|
||||||
|
type,
|
||||||
|
inode: newInode,
|
||||||
|
name,
|
||||||
|
parent,
|
||||||
|
mode,
|
||||||
|
owner,
|
||||||
|
group,
|
||||||
|
mtime: time,
|
||||||
|
atime: time,
|
||||||
|
ctime: time,
|
||||||
|
children: {},
|
||||||
|
};
|
||||||
|
|
||||||
|
node = directoryNode;
|
||||||
|
}
|
||||||
|
|
||||||
|
const newParentNode: DirectoryNode = {
|
||||||
|
type: parentNode.type,
|
||||||
|
inode: parent,
|
||||||
|
name: parentNode.name,
|
||||||
|
parent: parentNode.parent,
|
||||||
|
mode: parentNode.mode,
|
||||||
|
owner: parentNode.owner,
|
||||||
|
group: parentNode.group,
|
||||||
|
mtime: time,
|
||||||
|
atime: parentNode.atime,
|
||||||
|
ctime: time,
|
||||||
|
children: { ...parentNode.children, [name]: newInode },
|
||||||
|
};
|
||||||
|
|
||||||
|
const map = new Map(this.Overlay);
|
||||||
|
map.set(newInode, Object.freeze(node));
|
||||||
|
map.set(parent, Object.freeze(newParentNode));
|
||||||
|
|
||||||
|
const newOverlay: ReadonlyMap<number, TreeNode | null> = map;
|
||||||
|
|
||||||
|
return FileSystem.init(this._BaseFS, newOverlay, newInode);
|
||||||
|
}
|
||||||
|
|
||||||
|
public deleteNode(inode: number): FileSystem {
|
||||||
|
if (inode === 0) {
|
||||||
|
throw {
|
||||||
|
type: "IllegalMutation",
|
||||||
|
message: `Attempt at illegal mutation of node under inode 0 (root)`,
|
||||||
|
timestamp: Date.now(),
|
||||||
|
} satisfies IllegalMutationError;
|
||||||
|
}
|
||||||
|
|
||||||
|
const node = this.resolveNode(inode);
|
||||||
|
if (node === undefined) {
|
||||||
|
throw {
|
||||||
|
type: "NodeNotFound",
|
||||||
|
message: `Node under inode ${inode} not found`,
|
||||||
|
timestamp: Date.now(),
|
||||||
|
inode: inode,
|
||||||
|
} satisfies NodeNotFoundError;
|
||||||
|
}
|
||||||
|
|
||||||
|
const time: number = Date.now();
|
||||||
|
const descendants: number[] = dnTraverseTree(this, inode);
|
||||||
|
|
||||||
|
const map: Map<number, TreeNode | null> = new Map(this._OverlayFS);
|
||||||
|
for (const entry of descendants) {
|
||||||
|
map.set(entry, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
const parentNode = this.resolveNode(node.parent) as DirectoryNode;
|
||||||
|
if (parentNode === undefined) {
|
||||||
|
throw {
|
||||||
|
type: "NodeNotFound",
|
||||||
|
message: `Parent inode ${node.parent} not found (should be impossible unless the filesystem corrupted!)`,
|
||||||
|
timestamp: Date.now(),
|
||||||
|
inode: node.parent,
|
||||||
|
} satisfies NodeNotFoundError;
|
||||||
|
}
|
||||||
|
|
||||||
|
const { [node.name]: __, ...newChildren } = parentNode.children;
|
||||||
|
|
||||||
|
const newParentNode: DirectoryNode = {
|
||||||
|
type: parentNode.type,
|
||||||
|
inode: parentNode.inode,
|
||||||
|
name: parentNode.name,
|
||||||
|
parent: parentNode.parent,
|
||||||
|
mode: parentNode.mode,
|
||||||
|
owner: parentNode.owner,
|
||||||
|
group: parentNode.group,
|
||||||
|
mtime: time,
|
||||||
|
atime: parentNode.atime,
|
||||||
|
ctime: time,
|
||||||
|
children: newChildren,
|
||||||
|
};
|
||||||
|
|
||||||
|
map.set(node.parent, Object.freeze(newParentNode));
|
||||||
|
|
||||||
|
const newOverlay: ReadonlyMap<number, TreeNode | null> = map;
|
||||||
|
return FileSystem.init(this._BaseFS, newOverlay, this._INodeIndex);
|
||||||
|
}
|
||||||
|
|
||||||
|
public canRead(inode: number, user: UserInfo): boolean {
|
||||||
|
const node = this.resolveNode(inode);
|
||||||
|
if (node === undefined) {
|
||||||
|
throw {
|
||||||
|
type: "NodeNotFound",
|
||||||
|
message: `Node under inode ${inode} not found`,
|
||||||
|
timestamp: Date.now(),
|
||||||
|
inode: inode,
|
||||||
|
} satisfies NodeNotFoundError;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (user.uid === 0) return true;
|
||||||
|
|
||||||
|
if (user.uid === node.owner) {
|
||||||
|
return (node.mode & 0o400) !== 0;
|
||||||
|
} else if (user.gid === node.group) {
|
||||||
|
return (node.mode & 0o040) !== 0;
|
||||||
|
} else {
|
||||||
|
return (node.mode & 0o004) !== 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public canWrite(inode: number, user: UserInfo): boolean {
|
||||||
|
const node = this.resolveNode(inode);
|
||||||
|
if (node === undefined) {
|
||||||
|
throw {
|
||||||
|
type: "NodeNotFound",
|
||||||
|
message: `Node under inode ${inode} not found`,
|
||||||
|
timestamp: Date.now(),
|
||||||
|
inode: inode,
|
||||||
|
} satisfies NodeNotFoundError;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (user.uid === 0) return true;
|
||||||
|
|
||||||
|
if (user.uid === node.owner) {
|
||||||
|
return (node.mode & 0o200) !== 0;
|
||||||
|
} else if (user.gid === node.group) {
|
||||||
|
return (node.mode & 0o020) !== 0;
|
||||||
|
} else {
|
||||||
|
return (node.mode & 0o002) !== 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public canExecute(inode: number, user: UserInfo): boolean {
|
||||||
|
const node = this.resolveNode(inode);
|
||||||
|
if (node === undefined) {
|
||||||
|
throw {
|
||||||
|
type: "NodeNotFound",
|
||||||
|
message: `Node under inode ${inode} not found`,
|
||||||
|
timestamp: Date.now(),
|
||||||
|
inode: inode,
|
||||||
|
} satisfies NodeNotFoundError;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (user.uid === 0) return true;
|
||||||
|
|
||||||
|
if (user.uid === node.owner) {
|
||||||
|
return (node.mode & 0o100) !== 0;
|
||||||
|
} else if (user.gid === node.group) {
|
||||||
|
return (node.mode & 0o010) !== 0;
|
||||||
|
} else {
|
||||||
|
return (node.mode & 0o001) !== 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public get Overlay(): ReadonlyMap<number, TreeNode | null> {
|
||||||
|
return this._OverlayFS;
|
||||||
|
}
|
||||||
|
public get INodeIndex(): number {
|
||||||
|
return this._INodeIndex;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
import type { ImmutableBuffer } from "@core/util/immutable_buffer";
|
||||||
|
|
||||||
|
export type TreeNode = FileNode | DirectoryNode;
|
||||||
|
|
||||||
|
export interface FileNode {
|
||||||
|
readonly type: TreeNodeType.File;
|
||||||
|
readonly inode: number;
|
||||||
|
readonly name: string;
|
||||||
|
readonly parent: number;
|
||||||
|
readonly mode: number;
|
||||||
|
readonly owner: number;
|
||||||
|
readonly group: number;
|
||||||
|
readonly mtime: number;
|
||||||
|
readonly ctime: number;
|
||||||
|
readonly atime: number;
|
||||||
|
readonly content: ImmutableBuffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface DirectoryNode {
|
||||||
|
readonly type: TreeNodeType.Directory;
|
||||||
|
readonly inode: number;
|
||||||
|
readonly name: string;
|
||||||
|
readonly parent: number;
|
||||||
|
readonly mode: number;
|
||||||
|
readonly owner: number;
|
||||||
|
readonly group: number;
|
||||||
|
readonly mtime: number;
|
||||||
|
readonly ctime: number;
|
||||||
|
readonly atime: number;
|
||||||
|
readonly children: Readonly<Record<string, number>>;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* export interface SymlinkNode {
|
||||||
|
readonly type: TreeNodeType.Symlink;
|
||||||
|
readonly inode: number;
|
||||||
|
readonly name: string;
|
||||||
|
readonly parent: number;
|
||||||
|
readonly mode: number;
|
||||||
|
readonly owner: number;
|
||||||
|
readonly group: number;
|
||||||
|
readonly mtime: number;
|
||||||
|
readonly ctime: number;
|
||||||
|
readonly atime: number;
|
||||||
|
readonly target: string;
|
||||||
|
} */
|
||||||
|
|
||||||
|
export enum TreeNodeType {
|
||||||
|
File = 0,
|
||||||
|
Directory = 1,
|
||||||
|
//Symlink = 2,
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
const HISTORY_MAX_CAPACITY = 255;
|
||||||
|
|
||||||
|
export type HistoryState = readonly string[];
|
||||||
|
|
||||||
|
export function addHistoryEntry(state: HistoryState, input: string): HistoryState {
|
||||||
|
const entries: string[] = [...state];
|
||||||
|
|
||||||
|
if (entries.length === HISTORY_MAX_CAPACITY) {
|
||||||
|
entries.shift();
|
||||||
|
entries.push(input);
|
||||||
|
} else {
|
||||||
|
entries.push(input);
|
||||||
|
}
|
||||||
|
|
||||||
|
const result: readonly string[] = entries;
|
||||||
|
return Object.freeze(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getHistoryEntry(state: HistoryState, index: number): string {
|
||||||
|
const entry = state[index];
|
||||||
|
if (entry === undefined) {
|
||||||
|
// Add a custom error object later
|
||||||
|
throw Error(`HistoryState at index ${index} is undefined`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return entry;
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
export function resolveKeybind(input: string, map: ReadonlyMap<string, string>): string | undefined {
|
||||||
|
return map.get(input);
|
||||||
|
}
|
||||||
+40
-13
@@ -1,18 +1,8 @@
|
|||||||
import type { ImmutableBuffer } from "./util/immutable_buffer";
|
import { createShellError, type ShellError } from "./util/errors";
|
||||||
|
import { createImmutableBuffer, type ImmutableBuffer } from "./util/immutable_buffer";
|
||||||
export interface ShellError {
|
|
||||||
readonly errorType: string;
|
|
||||||
readonly message: string;
|
|
||||||
readonly timestamp: number;
|
|
||||||
readonly cause?: {
|
|
||||||
readonly name: string;
|
|
||||||
readonly message: string;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface ResultError<E extends ShellError> {
|
export interface ResultError<E extends ShellError> {
|
||||||
readonly type: "error";
|
readonly type: "error";
|
||||||
readonly exitCode: number;
|
|
||||||
readonly stdout: ImmutableBuffer;
|
readonly stdout: ImmutableBuffer;
|
||||||
readonly stderr: ImmutableBuffer;
|
readonly stderr: ImmutableBuffer;
|
||||||
readonly commandName?: string;
|
readonly commandName?: string;
|
||||||
@@ -21,10 +11,47 @@ export interface ResultError<E extends ShellError> {
|
|||||||
|
|
||||||
export interface ResultSuccess {
|
export interface ResultSuccess {
|
||||||
readonly type: "success";
|
readonly type: "success";
|
||||||
readonly exitCode: number;
|
|
||||||
readonly stdout: ImmutableBuffer;
|
readonly stdout: ImmutableBuffer;
|
||||||
readonly stderr: ImmutableBuffer;
|
readonly stderr: ImmutableBuffer;
|
||||||
|
readonly exitCode: number;
|
||||||
readonly commandName?: string;
|
readonly commandName?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type Result<E extends ShellError> = ResultSuccess | ResultError<E>;
|
export type Result<E extends ShellError> = ResultSuccess | ResultError<E>;
|
||||||
|
|
||||||
|
export function createErrorResult<E extends ShellError>(
|
||||||
|
errorType: E["errorType"],
|
||||||
|
message: string,
|
||||||
|
// Omit - includes all properties of E except the ones that are already inside ShellError - basically skipping duplicates and allowing this to be a one-liner
|
||||||
|
extraProps: Omit<E, keyof ShellError>,
|
||||||
|
commandName?: string,
|
||||||
|
stdout?: ImmutableBuffer,
|
||||||
|
stderr?: ImmutableBuffer,
|
||||||
|
cause?: { name: string; message: string },
|
||||||
|
): ResultError<E> {
|
||||||
|
const error = createShellError<E>(errorType, message, extraProps, cause);
|
||||||
|
return Object.freeze({
|
||||||
|
type: "error",
|
||||||
|
stdout: stdout ?? createImmutableBuffer(new Uint8Array()),
|
||||||
|
stderr: stderr ?? createImmutableBuffer(new Uint8Array()),
|
||||||
|
error,
|
||||||
|
// commandName is either undefined or string but when undefined - has to be ommited - we achieve that with a condition wrapped in a spread
|
||||||
|
...(commandName !== undefined && { commandName }),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export function createSuccessResult(
|
||||||
|
commandName?: string,
|
||||||
|
exitCode?: number,
|
||||||
|
stdout?: ImmutableBuffer,
|
||||||
|
stderr?: ImmutableBuffer,
|
||||||
|
): ResultSuccess {
|
||||||
|
return Object.freeze({
|
||||||
|
type: "success",
|
||||||
|
stdout: stdout ?? createImmutableBuffer(new Uint8Array()),
|
||||||
|
stderr: stderr ?? createImmutableBuffer(new Uint8Array()),
|
||||||
|
exitCode: exitCode ?? 0,
|
||||||
|
// commandName is either undefined or string but when undefined - has to be ommited - we achieve that with a condition wrapped in a spread
|
||||||
|
...(commandName !== undefined && { commandName }),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,79 @@
|
|||||||
|
import { type HistoryState } from "./history_state";
|
||||||
|
import { createShellState, type ShellState } from "./shell_state";
|
||||||
|
import { createShellError, type LoginError, type UserNotFoundError } from "./util/errors";
|
||||||
|
|
||||||
|
export interface GroupInfo {
|
||||||
|
readonly groupname: string;
|
||||||
|
readonly gid: number;
|
||||||
|
readonly members: number[];
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface UserInfo {
|
||||||
|
readonly username: string;
|
||||||
|
readonly uid: number;
|
||||||
|
readonly gid: number;
|
||||||
|
readonly group: readonly number[];
|
||||||
|
readonly home: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface SessionState {
|
||||||
|
readonly user: UserInfo;
|
||||||
|
readonly envVars: ReadonlyMap<string, string>;
|
||||||
|
readonly history: HistoryState;
|
||||||
|
readonly cwd: number;
|
||||||
|
readonly pwd: number;
|
||||||
|
readonly exitCode: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function createInitialSessionState(user: UserInfo): SessionState {
|
||||||
|
return Object.freeze({
|
||||||
|
user,
|
||||||
|
envVars: Object.freeze(new Map()),
|
||||||
|
history: Object.freeze([]),
|
||||||
|
cwd: user.home,
|
||||||
|
pwd: user.home,
|
||||||
|
exitCode: 0,
|
||||||
|
} satisfies SessionState);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getCurrentSession(state: ShellState): SessionState {
|
||||||
|
return state.sessionStack[state.sessionStack.length - 1]!;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function login(
|
||||||
|
state: ShellState,
|
||||||
|
username: string,
|
||||||
|
password: string,
|
||||||
|
passwdMap: ReadonlyMap<number, string>,
|
||||||
|
): ShellState {
|
||||||
|
const user = state.users.values().find(value => value.username === username);
|
||||||
|
if (!user) {
|
||||||
|
throw createShellError<UserNotFoundError>("UserNotFound", "The specified user does not exist", { username });
|
||||||
|
}
|
||||||
|
|
||||||
|
const passwd = passwdMap.get(user.uid);
|
||||||
|
if (!passwd) {
|
||||||
|
throw createShellError<LoginError>("LoginError", "The user does not have a password mapped to their uid", {
|
||||||
|
user: user.uid,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Change this to hashed passwords later on
|
||||||
|
if (!(password === passwd)) {
|
||||||
|
throw createShellError<LoginError>("LoginError", "The password is incorrect", { user: user.uid });
|
||||||
|
}
|
||||||
|
|
||||||
|
const stack = [...state.sessionStack];
|
||||||
|
stack.push(createInitialSessionState(user));
|
||||||
|
return createShellState(state.filesystem, state.users, state.groups, stack);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function logout(state: ShellState): ShellState {
|
||||||
|
if (state.sessionStack.length > 1) {
|
||||||
|
const stack = [...state.sessionStack];
|
||||||
|
stack.pop();
|
||||||
|
|
||||||
|
return createShellState(state.filesystem, state.users, state.groups, stack);
|
||||||
|
}
|
||||||
|
return state;
|
||||||
|
}
|
||||||
@@ -0,0 +1,138 @@
|
|||||||
|
import type { FileSystem } from "./filesystem/filesystem";
|
||||||
|
import { addHistoryEntry } from "./history_state";
|
||||||
|
import type { GroupInfo, UserInfo } from "./session_manager";
|
||||||
|
import { createInitialSessionState, getCurrentSession, type SessionState } from "./session_manager";
|
||||||
|
|
||||||
|
export interface ShellState {
|
||||||
|
readonly filesystem: FileSystem;
|
||||||
|
readonly users: ReadonlyMap<number, UserInfo>;
|
||||||
|
readonly groups: ReadonlyMap<number, GroupInfo>;
|
||||||
|
readonly sessionStack: ReadonlyArray<SessionState>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function createInitialShellState(
|
||||||
|
filesystem: FileSystem,
|
||||||
|
users: ReadonlyMap<number, UserInfo>,
|
||||||
|
groups: ReadonlyMap<number, GroupInfo>,
|
||||||
|
user: UserInfo,
|
||||||
|
): ShellState {
|
||||||
|
return Object.freeze({
|
||||||
|
filesystem,
|
||||||
|
users,
|
||||||
|
groups,
|
||||||
|
sessionStack: Object.freeze([createInitialSessionState(user)]),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export function createShellState(
|
||||||
|
filesystem: FileSystem,
|
||||||
|
users: ReadonlyMap<number, UserInfo>,
|
||||||
|
groups: ReadonlyMap<number, GroupInfo>,
|
||||||
|
sessionStack: ReadonlyArray<SessionState>,
|
||||||
|
): ShellState {
|
||||||
|
return Object.freeze({
|
||||||
|
filesystem,
|
||||||
|
users,
|
||||||
|
groups,
|
||||||
|
sessionStack: Object.freeze(sessionStack),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export function withCwd(state: ShellState, inode: number): ShellState {
|
||||||
|
const session = getCurrentSession(state);
|
||||||
|
|
||||||
|
const newSession = Object.freeze({
|
||||||
|
user: session.user,
|
||||||
|
envVars: Object.freeze(session.envVars),
|
||||||
|
history: Object.freeze(session.history),
|
||||||
|
cwd: inode,
|
||||||
|
pwd: session.cwd,
|
||||||
|
exitCode: session.exitCode,
|
||||||
|
} satisfies SessionState);
|
||||||
|
|
||||||
|
const sessionStack = [...state.sessionStack];
|
||||||
|
|
||||||
|
sessionStack[sessionStack.length - 1] = newSession;
|
||||||
|
|
||||||
|
return Object.freeze({
|
||||||
|
filesystem: state.filesystem,
|
||||||
|
users: state.users,
|
||||||
|
groups: state.groups,
|
||||||
|
sessionStack: Object.freeze(sessionStack),
|
||||||
|
} satisfies ShellState);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function withHistory(state: ShellState, input: string): ShellState {
|
||||||
|
const session = getCurrentSession(state);
|
||||||
|
|
||||||
|
const newSession = Object.freeze({
|
||||||
|
user: session.user,
|
||||||
|
envVars: Object.freeze(session.envVars),
|
||||||
|
history: Object.freeze(addHistoryEntry(session.history, input)),
|
||||||
|
cwd: session.cwd,
|
||||||
|
pwd: session.pwd,
|
||||||
|
exitCode: session.exitCode,
|
||||||
|
} satisfies SessionState);
|
||||||
|
|
||||||
|
const sessionStack = [...state.sessionStack];
|
||||||
|
|
||||||
|
sessionStack[sessionStack.length - 1] = newSession;
|
||||||
|
|
||||||
|
return Object.freeze({
|
||||||
|
filesystem: state.filesystem,
|
||||||
|
users: state.users,
|
||||||
|
groups: state.groups,
|
||||||
|
sessionStack: Object.freeze(sessionStack),
|
||||||
|
} satisfies ShellState);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function withEnv(state: ShellState, key: string, value: string): ShellState {
|
||||||
|
const session = getCurrentSession(state);
|
||||||
|
|
||||||
|
const envVars = new Map(session.envVars);
|
||||||
|
envVars.set(key, value);
|
||||||
|
|
||||||
|
const newSession = Object.freeze({
|
||||||
|
user: session.user,
|
||||||
|
envVars: Object.freeze(envVars),
|
||||||
|
history: Object.freeze(session.history),
|
||||||
|
cwd: session.cwd,
|
||||||
|
pwd: session.pwd,
|
||||||
|
exitCode: session.exitCode,
|
||||||
|
} satisfies SessionState);
|
||||||
|
|
||||||
|
const sessionStack = [...state.sessionStack];
|
||||||
|
|
||||||
|
sessionStack[sessionStack.length - 1] = newSession;
|
||||||
|
|
||||||
|
return Object.freeze({
|
||||||
|
filesystem: state.filesystem,
|
||||||
|
users: state.users,
|
||||||
|
groups: state.groups,
|
||||||
|
sessionStack: Object.freeze(sessionStack),
|
||||||
|
} satisfies ShellState);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function withExitCode(state: ShellState, exitCode: number): ShellState {
|
||||||
|
const session = getCurrentSession(state);
|
||||||
|
|
||||||
|
const newSession = Object.freeze({
|
||||||
|
user: session.user,
|
||||||
|
envVars: Object.freeze(session.envVars),
|
||||||
|
history: Object.freeze(session.history),
|
||||||
|
cwd: session.cwd,
|
||||||
|
pwd: session.pwd,
|
||||||
|
exitCode: exitCode,
|
||||||
|
} satisfies SessionState);
|
||||||
|
|
||||||
|
const sessionStack = [...state.sessionStack];
|
||||||
|
|
||||||
|
sessionStack[sessionStack.length - 1] = newSession;
|
||||||
|
|
||||||
|
return Object.freeze({
|
||||||
|
filesystem: state.filesystem,
|
||||||
|
users: state.users,
|
||||||
|
groups: state.groups,
|
||||||
|
sessionStack: Object.freeze(sessionStack),
|
||||||
|
} satisfies ShellState);
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
//Payload optional because command that handles that signal
|
||||||
|
// is going to know what shape it is, therefore also able to cast it;
|
||||||
|
export type Signal = {
|
||||||
|
type: SIGNAL_MAP;
|
||||||
|
payload?: unknown;
|
||||||
|
};
|
||||||
|
|
||||||
|
export enum SIGNAL_MAP {
|
||||||
|
SIGINT = 2,
|
||||||
|
SIGTERM = 3,
|
||||||
|
SIGKILL = 9,
|
||||||
|
INPUT = 10,
|
||||||
|
}
|
||||||
@@ -0,0 +1,224 @@
|
|||||||
|
export interface ShellError {
|
||||||
|
readonly errorType: string;
|
||||||
|
readonly message: string;
|
||||||
|
readonly timestamp: number;
|
||||||
|
readonly exitCode: number;
|
||||||
|
readonly cause?: {
|
||||||
|
readonly name: string;
|
||||||
|
readonly message: string;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export function createShellError<E extends ShellError>(
|
||||||
|
errorType: E["errorType"],
|
||||||
|
message: string,
|
||||||
|
extraProps: Omit<E, keyof ShellError>,
|
||||||
|
cause?: { name: string; message: string },
|
||||||
|
): E {
|
||||||
|
let exitCode = EXIT_CODE_MAP[errorType] ?? 1;
|
||||||
|
|
||||||
|
// Special handling for signal termination
|
||||||
|
if (errorType === "SignalTermination") {
|
||||||
|
const signalError = extraProps as unknown as { signalNumber: number };
|
||||||
|
exitCode = 128 + signalError.signalNumber;
|
||||||
|
}
|
||||||
|
|
||||||
|
const base: ShellError = Object.freeze({
|
||||||
|
errorType,
|
||||||
|
message,
|
||||||
|
timestamp: Date.now(),
|
||||||
|
exitCode,
|
||||||
|
...Object.freeze(cause !== undefined && { cause }),
|
||||||
|
});
|
||||||
|
|
||||||
|
return Object.freeze({ ...base, ...extraProps }) as E;
|
||||||
|
}
|
||||||
|
|
||||||
|
const EXIT_CODE_MAP: Record<string, number> = {
|
||||||
|
GenericError: 1,
|
||||||
|
InvalidArguments: 2,
|
||||||
|
|
||||||
|
ParseError: 62,
|
||||||
|
UsageError: 63,
|
||||||
|
PathResolutionError: 64,
|
||||||
|
DataFormatError: 65,
|
||||||
|
FileNotFound: 66,
|
||||||
|
UserNotFound: 67,
|
||||||
|
LoginError: 68,
|
||||||
|
InternalSoftwareError: 70,
|
||||||
|
OperatingSystemError: 71,
|
||||||
|
SystemFileError: 72,
|
||||||
|
CannotCreateOutput: 73,
|
||||||
|
IoError: 74,
|
||||||
|
TemporaryFailure: 75,
|
||||||
|
TimeoutError: 75, // Same as TEMPFAIL
|
||||||
|
ProtocolError: 76,
|
||||||
|
PermissionDenied: 77,
|
||||||
|
ConfigurationError: 78,
|
||||||
|
CommandNotFound: 127,
|
||||||
|
NotExecutable: 126,
|
||||||
|
// SIGNAL_TERMINATION is handled dynamically (128 + signal)
|
||||||
|
};
|
||||||
|
|
||||||
|
/** Generic catch-all error (Exit 1) */
|
||||||
|
|
||||||
|
// Its actually the ShellError itself - no need for a custom type
|
||||||
|
|
||||||
|
/* export interface GenericError extends ShellError {
|
||||||
|
errorType: "GENERIC_ERROR";
|
||||||
|
} */
|
||||||
|
|
||||||
|
export interface InvalidArgumentsError extends ShellError {
|
||||||
|
errorType: "InvalidArguments";
|
||||||
|
expected: string; // e.g., "Usage: command <file>"
|
||||||
|
actual: string; // e.g., the user's input
|
||||||
|
}
|
||||||
|
|
||||||
|
// Codes 3 - 62 reserved for currently running processes, and their specific error handling.
|
||||||
|
|
||||||
|
export interface ParseError extends ShellError {
|
||||||
|
errorType: "ParseError";
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Command used incorrectly (bad flag, wrong arg count) - maps to EX_USAGE (Exit 64) */
|
||||||
|
export interface UsageError extends ShellError {
|
||||||
|
errorType: "UsageError";
|
||||||
|
usageHint: string; // e.g., "command [-a] [-b] <filename>"
|
||||||
|
}
|
||||||
|
|
||||||
|
// ===================== 2. FILE / I/O ERRORS (Exit: 65, 66, 72, 73, 74) =====================
|
||||||
|
|
||||||
|
/** Input file does not exist or is unreadable (Exit 66 - EX_NOINPUT) */
|
||||||
|
export interface FileNotFoundError extends ShellError {
|
||||||
|
errorType: "FileNotFound";
|
||||||
|
path: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface PathResolutionError extends ShellError {
|
||||||
|
errorType: "PathResolutionError";
|
||||||
|
path: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Input data has invalid format (Exit 65 - EX_DATAERR) */
|
||||||
|
export interface DataFormatError extends ShellError {
|
||||||
|
errorType: "DataFormatError";
|
||||||
|
path?: string; // Optional: which file had bad data
|
||||||
|
line?: number; // Optional: line number if applicable
|
||||||
|
}
|
||||||
|
|
||||||
|
/** System file (e.g., /etc/passwd, /etc/hosts) is broken or missing (Exit 72 - EX_OSFILE) */
|
||||||
|
export interface SystemFileError extends ShellError {
|
||||||
|
errorType: "SystemFileError";
|
||||||
|
path: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Cannot create user-specified output file (Exit 73 - EX_CANTCREAT) */
|
||||||
|
export interface CannotCreateOutputError extends ShellError {
|
||||||
|
errorType: "CannotCreateOutput";
|
||||||
|
path: string;
|
||||||
|
reason: string; // e.g., "Directory does not exist", "Read-only filesystem"
|
||||||
|
}
|
||||||
|
|
||||||
|
/** I/O error while reading/writing a file (Exit 74 - EX_IOERR) */
|
||||||
|
export interface IOError extends ShellError {
|
||||||
|
errorType: "IoError";
|
||||||
|
path: string;
|
||||||
|
operation: "read" | "write" | "seek";
|
||||||
|
}
|
||||||
|
|
||||||
|
// ===================== 3. PERMISSION / USER ERRORS (Exit: 67, 77) =====================
|
||||||
|
|
||||||
|
/** Insufficient permission to access a resource (Exit 77 - EX_NOPERM) */
|
||||||
|
export interface PermissionDeniedError extends ShellError {
|
||||||
|
errorType: "PermissionDenied";
|
||||||
|
resource: string; // File path, port, socket, etc.
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Specified user does not exist (Exit 67 - EX_NOUSER) */
|
||||||
|
export interface UserNotFoundError extends ShellError {
|
||||||
|
errorType: "UserNotFound";
|
||||||
|
username: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface LoginError extends ShellError {
|
||||||
|
errorType: "LoginError";
|
||||||
|
user: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Required service is unavailable (Exit 69 - EX_UNAVAILABLE) */
|
||||||
|
export interface ServiceUnavailableError extends ShellError {
|
||||||
|
errorType: "ServiceUnavailable";
|
||||||
|
service: string; // e.g., "database", "DNS", "SSH"
|
||||||
|
port?: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ===================== 5. PROCESS / EXECUTION ERRORS (Exit: 126, 127, 71) =====================
|
||||||
|
|
||||||
|
/** Command not found (Exit 127 - POSIX standard) */
|
||||||
|
export interface CommandNotFoundError extends ShellError {
|
||||||
|
errorType: "CommandNotFound";
|
||||||
|
command: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Command found but not executable (Exit 126 - POSIX standard) */
|
||||||
|
export interface NotExecutableError extends ShellError {
|
||||||
|
errorType: "NotExecutable";
|
||||||
|
path: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Operating system error (fork, pipe, process table full) (Exit 71 - EX_OSERR) */
|
||||||
|
export interface OperatingSystemError extends ShellError {
|
||||||
|
errorType: "OperatingSystemError";
|
||||||
|
syscall: string; // e.g., "fork", "pipe", "execve"
|
||||||
|
errno?: number; // Optional: underlying OS error code
|
||||||
|
}
|
||||||
|
|
||||||
|
// ===================== 6. TIMING / RETRY ERRORS (Exit: 75) =====================
|
||||||
|
|
||||||
|
/** Temporary failure; user should retry (Exit 75 - EX_TEMPFAIL) */
|
||||||
|
export interface TemporaryFailureError extends ShellError {
|
||||||
|
errorType: "TemporaryFailure";
|
||||||
|
retryAfter?: number; // Optional: recommended seconds to wait
|
||||||
|
resource?: string; // Optional: what resource is temporarily busy
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Operation timed out (Often mapped to EX_TEMPFAIL, Exit 75) */
|
||||||
|
export interface TimeoutError extends ShellError {
|
||||||
|
errorType: "TimeoutError";
|
||||||
|
timeoutSeconds: number;
|
||||||
|
operation: string; // e.g., "HTTP request", "Database query"
|
||||||
|
}
|
||||||
|
|
||||||
|
// ===================== 7. INTERNAL / CONFIGURATION ERRORS (Exit: 70, 78) =====================
|
||||||
|
|
||||||
|
/** Internal software error (bug, assertion failed) (Exit 70 - EX_SOFTWARE) */
|
||||||
|
export interface InternalSoftwareError extends ShellError {
|
||||||
|
errorType: "InternalSoftwareError";
|
||||||
|
component: string; // e.g., "parser", "network layer"
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Configuration error in the program or system (Exit 78 - EX_CONFIG) */
|
||||||
|
export interface ConfigurationError extends ShellError {
|
||||||
|
errorType: "ConfigurationError";
|
||||||
|
configFile?: string;
|
||||||
|
key?: string; // Which config key is wrong
|
||||||
|
}
|
||||||
|
|
||||||
|
// ===================== 8. PROTOCOL / REMOTE ERRORS (Exit: 76) =====================
|
||||||
|
|
||||||
|
/** Remote system protocol error (Exit 76 - EX_PROTOCOL) */
|
||||||
|
export interface ProtocolError extends ShellError {
|
||||||
|
errorType: "ProtocolError";
|
||||||
|
remoteHost?: string;
|
||||||
|
protocol: string; // e.g., "HTTP", "SSH", "FTP"
|
||||||
|
statusCode?: number; // Optional: e.g., HTTP 400/500
|
||||||
|
}
|
||||||
|
|
||||||
|
// ===================== 9. SIGNAL TERMINATION (Exit: 128 + signal) =====================
|
||||||
|
|
||||||
|
/** Process was killed by a signal (Exit = 128 + signalNumber) */
|
||||||
|
export interface SignalTerminationError extends ShellError {
|
||||||
|
errorType: "SignalTermination";
|
||||||
|
signalNumber: number; // e.g., 9 (SIGKILL), 15 (SIGTERM), 2 (SIGINT)
|
||||||
|
signalName: string; // e.g., "SIGTERM", "SIGKILL"
|
||||||
|
}
|
||||||
@@ -2,6 +2,7 @@ export interface ImmutableBuffer {
|
|||||||
readonly length: number;
|
readonly length: number;
|
||||||
get(index: number): number;
|
get(index: number): number;
|
||||||
slice(start?: number, end?: number): ImmutableBuffer;
|
slice(start?: number, end?: number): ImmutableBuffer;
|
||||||
|
byteSize(): number;
|
||||||
toArray(): Uint8Array;
|
toArray(): Uint8Array;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -22,10 +23,15 @@ export function createImmutableBuffer(input: Uint8Array): ImmutableBuffer {
|
|||||||
return Uint8Array.from(buffer);
|
return Uint8Array.from(buffer);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const byteSize = (): number => {
|
||||||
|
return buffer.byteLength;
|
||||||
|
};
|
||||||
|
|
||||||
const result: ImmutableBuffer = {
|
const result: ImmutableBuffer = {
|
||||||
length: buffer.length,
|
length: buffer.length,
|
||||||
get,
|
get,
|
||||||
slice,
|
slice,
|
||||||
|
byteSize,
|
||||||
toArray,
|
toArray,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,94 @@
|
|||||||
|
//type ParserError = UnclosedQuoteError;
|
||||||
|
|
||||||
|
interface UnclosedQuoteError {
|
||||||
|
type: "UnclosedQuote";
|
||||||
|
message: string;
|
||||||
|
timestamp: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function tokenize(input: string): string[] {
|
||||||
|
enum State {
|
||||||
|
Normal,
|
||||||
|
SingleQuote,
|
||||||
|
DoubleQuote,
|
||||||
|
Escape,
|
||||||
|
}
|
||||||
|
|
||||||
|
const tokens: string[] = [];
|
||||||
|
const buffer: string[] = [];
|
||||||
|
let state = State.Normal;
|
||||||
|
let escapeReturnState = State.Normal;
|
||||||
|
|
||||||
|
function flushBuffer() {
|
||||||
|
if (buffer.length > 0) {
|
||||||
|
tokens.push(buffer.join(""));
|
||||||
|
buffer.length = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const char of input) {
|
||||||
|
switch (state) {
|
||||||
|
case State.Normal:
|
||||||
|
if (char === '"') {
|
||||||
|
flushBuffer();
|
||||||
|
state = State.DoubleQuote;
|
||||||
|
} else if (char === "'") {
|
||||||
|
flushBuffer();
|
||||||
|
state = State.SingleQuote;
|
||||||
|
} else if (char === "\\") {
|
||||||
|
escapeReturnState = State.Normal;
|
||||||
|
state = State.Escape;
|
||||||
|
} else if (char === " ") {
|
||||||
|
flushBuffer();
|
||||||
|
} else {
|
||||||
|
buffer.push(char);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case State.SingleQuote:
|
||||||
|
if (char === "'") {
|
||||||
|
flushBuffer();
|
||||||
|
state = State.Normal;
|
||||||
|
} else {
|
||||||
|
buffer.push(char);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case State.DoubleQuote:
|
||||||
|
if (char === '"') {
|
||||||
|
flushBuffer();
|
||||||
|
state = State.Normal;
|
||||||
|
} else if (char === "\\") {
|
||||||
|
escapeReturnState = State.DoubleQuote;
|
||||||
|
state = State.Escape;
|
||||||
|
} else {
|
||||||
|
buffer.push(char);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case State.Escape:
|
||||||
|
if (escapeReturnState === State.DoubleQuote) {
|
||||||
|
if (char === "$" || char === "`" || char === '"' || char === "\\" || char === "\n") {
|
||||||
|
if (char !== "\n") {
|
||||||
|
buffer.push(char);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
buffer.push("\\");
|
||||||
|
buffer.push(char);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
buffer.push(char);
|
||||||
|
}
|
||||||
|
state = escapeReturnState;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (state === State.DoubleQuote || state === State.SingleQuote) {
|
||||||
|
throw {
|
||||||
|
type: "UnclosedQuote",
|
||||||
|
message: `Tokenizer returned with an unclosed quote`,
|
||||||
|
timestamp: Date.now(),
|
||||||
|
} satisfies UnclosedQuoteError;
|
||||||
|
}
|
||||||
|
|
||||||
|
flushBuffer();
|
||||||
|
return tokens;
|
||||||
|
}
|
||||||
@@ -0,0 +1,116 @@
|
|||||||
|
import { getCurrentSession } from "@core/session_manager";
|
||||||
|
import type { ShellState } from "@core/shell_state";
|
||||||
|
import type { NodeNotFoundError, NotADirectoryError } from "../filesystem/errors";
|
||||||
|
import { TreeNodeType, type DirectoryNode, type TreeNode } from "../filesystem/treenode";
|
||||||
|
|
||||||
|
export function resolvePath(state: ShellState, path: string): number {
|
||||||
|
const session = getCurrentSession(state);
|
||||||
|
const isAbsolute = path.startsWith("/");
|
||||||
|
|
||||||
|
const normalizedPath: string = path.replace(/^\/+|\/+$/g, "");
|
||||||
|
const pathComponents: string[] = normalizedPath.split("/").filter(name => name.length > 0);
|
||||||
|
|
||||||
|
if (pathComponents.length === 0 && isAbsolute) return 0;
|
||||||
|
if (pathComponents.length === 0) return session.cwd;
|
||||||
|
|
||||||
|
let currentNode = isAbsolute
|
||||||
|
? 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]!;
|
||||||
|
if (name === ".") continue;
|
||||||
|
|
||||||
|
if (name === "..") {
|
||||||
|
currentNode = state.filesystem.getNode(currentNode.parent) as DirectoryNode;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (name === "~") {
|
||||||
|
currentNode = state.filesystem.getNode(session.user.home) as DirectoryNode;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const inode = currentNode.children[name];
|
||||||
|
|
||||||
|
if (!inode)
|
||||||
|
throw {
|
||||||
|
type: "NodeNotFound",
|
||||||
|
message: `Child node in ${currentNode.name} under name ${name} not found`,
|
||||||
|
timestamp: Date.now(),
|
||||||
|
inode: currentNode.inode,
|
||||||
|
} satisfies NodeNotFoundError;
|
||||||
|
if (state.filesystem.isFile(inode) && !(i === pathComponents.length - 1))
|
||||||
|
throw {
|
||||||
|
type: "NotADirectory",
|
||||||
|
message: `Child node ${name} is not a directory`,
|
||||||
|
timestamp: Date.now(),
|
||||||
|
inode: inode,
|
||||||
|
nodeType: TreeNodeType.File,
|
||||||
|
} satisfies NotADirectoryError;
|
||||||
|
|
||||||
|
currentNode = state.filesystem.getNode(inode) as DirectoryNode;
|
||||||
|
}
|
||||||
|
|
||||||
|
return currentNode.inode;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function resolveInode(state: ShellState, inode: number): string {
|
||||||
|
const node = state.filesystem.getNode(inode);
|
||||||
|
const components: string[] = [];
|
||||||
|
|
||||||
|
if (node === undefined) return "";
|
||||||
|
|
||||||
|
let currentInode = inode;
|
||||||
|
while (currentInode !== 0) {
|
||||||
|
const node = state.filesystem.getNode(currentInode) as TreeNode;
|
||||||
|
if (!node) break;
|
||||||
|
|
||||||
|
components.push(node.name);
|
||||||
|
currentInode = node.parent;
|
||||||
|
}
|
||||||
|
if (components.length === 0) {
|
||||||
|
components.push("/");
|
||||||
|
return components.join("");
|
||||||
|
}
|
||||||
|
|
||||||
|
components.reverse().unshift("");
|
||||||
|
let result = components.join("/");
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function resolveInodeToHome(state: ShellState, inode: number): string {
|
||||||
|
const home = resolveInode(state, getCurrentSession(state).user.home);
|
||||||
|
let result = resolveInode(state, inode);
|
||||||
|
|
||||||
|
if (result.startsWith(home)) {
|
||||||
|
result = `~${result.slice(home.length)}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function resolveInodeToCwd(state: ShellState, inode: number): string {
|
||||||
|
const cwd = resolveInode(state, getCurrentSession(state).cwd);
|
||||||
|
let result = resolveInode(state, inode);
|
||||||
|
|
||||||
|
if (result.startsWith(cwd)) {
|
||||||
|
result = `.${result.slice(cwd.length)}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function resolveInodeRelative(state: ShellState, inode: number, relative: number): string {
|
||||||
|
const rel = resolveInode(state, relative);
|
||||||
|
let result = resolveInode(state, inode);
|
||||||
|
|
||||||
|
if (result.startsWith(rel)) {
|
||||||
|
result = `.${result.slice(rel.length)}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
@@ -0,0 +1,135 @@
|
|||||||
|
import type { FileNode, TreeNode } from "@core/filesystem/treenode";
|
||||||
|
import type { ShellState } from "@core/shell_state";
|
||||||
|
import { Posix } from "@termos/util/posix";
|
||||||
|
|
||||||
|
export enum SortTreeNodeBy {
|
||||||
|
NAME = "name",
|
||||||
|
INODE = "inode",
|
||||||
|
SIZE = "size",
|
||||||
|
EXTENSION = "extension",
|
||||||
|
TYPE = "type",
|
||||||
|
MTIME = "mtime",
|
||||||
|
ATIME = "atime",
|
||||||
|
CTIME = "ctime",
|
||||||
|
}
|
||||||
|
|
||||||
|
export function sortTreeNodes<T extends TreeNode | number>(
|
||||||
|
state: ShellState,
|
||||||
|
arr: T[],
|
||||||
|
r: boolean = false,
|
||||||
|
by: SortTreeNodeBy = SortTreeNodeBy.NAME,
|
||||||
|
): T[] {
|
||||||
|
if (arr.length === 0) return [];
|
||||||
|
|
||||||
|
const nodes: TreeNode[] = [];
|
||||||
|
|
||||||
|
if (typeof arr[0] === "number") {
|
||||||
|
for (const e of arr as number[]) {
|
||||||
|
const node = state.filesystem.getNode(e);
|
||||||
|
if (!node) continue;
|
||||||
|
|
||||||
|
nodes.push(node);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
nodes.push(...(arr as TreeNode[]));
|
||||||
|
}
|
||||||
|
|
||||||
|
nodeQS(nodes, r, by, 0, nodes.length - 1);
|
||||||
|
|
||||||
|
if (typeof arr[0] === "number") {
|
||||||
|
return nodes.map(n => n.inode) as T[];
|
||||||
|
} else {
|
||||||
|
return nodes as T[];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function nodeQS(arr: TreeNode[], r: boolean, by: SortTreeNodeBy, start: number, end: number): void {
|
||||||
|
if (end <= start) return;
|
||||||
|
|
||||||
|
const pivot = partitionNodeArray(arr, r, by, start, end);
|
||||||
|
nodeQS(arr, r, by, start, pivot - 1);
|
||||||
|
nodeQS(arr, r, by, pivot + 1, end);
|
||||||
|
}
|
||||||
|
|
||||||
|
function partitionNodeArray(part: TreeNode[], r: boolean, by: SortTreeNodeBy, start: number, end: number): number {
|
||||||
|
const pivot = part[end];
|
||||||
|
let i = start - 1;
|
||||||
|
|
||||||
|
for (let j = start; j <= end; j++) {
|
||||||
|
if (compareTreeNode(part[j]!, pivot!, by, r) < 0) {
|
||||||
|
i++;
|
||||||
|
const temp = part[i]!;
|
||||||
|
part[i] = part[j]!;
|
||||||
|
part[j] = temp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
i++;
|
||||||
|
const temp = part[i]!;
|
||||||
|
part[i] = part[end]!;
|
||||||
|
part[end] = temp;
|
||||||
|
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
|
function compareTreeNode(a: TreeNode, b: TreeNode, by: SortTreeNodeBy, r: boolean): number {
|
||||||
|
switch (by) {
|
||||||
|
case SortTreeNodeBy.NAME: {
|
||||||
|
const A: string = a.name.startsWith(".") ? a.name.slice(1) : a.name;
|
||||||
|
const B: string = b.name.startsWith(".") ? b.name.slice(1) : b.name;
|
||||||
|
const min = Math.min(A.length, B.length);
|
||||||
|
|
||||||
|
for (let i = 0; i < min; i++) {
|
||||||
|
const charA = Posix.GetCharWeight(A.charAt(i));
|
||||||
|
const charB = Posix.GetCharWeight(B.charAt(i));
|
||||||
|
|
||||||
|
if (charA !== charB) {
|
||||||
|
return r ? charB - charA : charA - charB;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return r ? B.length - A.length : A.length - B.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
case SortTreeNodeBy.MTIME:
|
||||||
|
case SortTreeNodeBy.ATIME:
|
||||||
|
case SortTreeNodeBy.CTIME: {
|
||||||
|
const A: number = a[by];
|
||||||
|
const B: number = b[by];
|
||||||
|
|
||||||
|
return r ? A - B : B - A;
|
||||||
|
}
|
||||||
|
|
||||||
|
case SortTreeNodeBy.SIZE: {
|
||||||
|
const A: number = (a as FileNode).content.byteSize();
|
||||||
|
const B: number = (b as FileNode).content.byteSize();
|
||||||
|
|
||||||
|
return r ? A - B : B - A;
|
||||||
|
}
|
||||||
|
|
||||||
|
case SortTreeNodeBy.EXTENSION: {
|
||||||
|
const A: string = a.name.split(".").pop() ?? "";
|
||||||
|
const B: string = b.name.split(".").pop() ?? "";
|
||||||
|
const min = Math.min(A.length, B.length);
|
||||||
|
|
||||||
|
for (let i = 0; i < min; i++) {
|
||||||
|
const charA = Posix.GetCharWeight(A.charAt(i));
|
||||||
|
const charB = Posix.GetCharWeight(B.charAt(i));
|
||||||
|
|
||||||
|
if (charA !== charB) {
|
||||||
|
return r ? charB - charA : charA - charB;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return r ? B.length - A.length : A.length - B.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
case SortTreeNodeBy.INODE: {
|
||||||
|
return r ? a.inode - b.inode : b.inode - a.inode;
|
||||||
|
}
|
||||||
|
|
||||||
|
case SortTreeNodeBy.TYPE: {
|
||||||
|
return r ? b.type - a.type : a.type - b.type;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
export function sum(a: number, b: number): number {
|
|
||||||
return a + b;
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
import type { FileSystem } from "@core/filesystem/filesystem";
|
||||||
|
import { TreeNodeType, type TreeNode } from "@core/filesystem/treenode";
|
||||||
|
|
||||||
|
export function dnTraverseTree(fs: FileSystem, root: number): number[] {
|
||||||
|
if (fs.getNode(root) === undefined) return [];
|
||||||
|
|
||||||
|
const result: number[] = [];
|
||||||
|
const stack: number[] = [];
|
||||||
|
stack.push(root);
|
||||||
|
while (stack.length > 0) {
|
||||||
|
const node = fs.getNode(stack.pop() as number);
|
||||||
|
if (node === undefined) continue;
|
||||||
|
|
||||||
|
result.push(node.inode);
|
||||||
|
|
||||||
|
if (node.type === TreeNodeType.Directory) {
|
||||||
|
const children = Object.values(node.children);
|
||||||
|
for (let i = children.length - 1; i >= 0; i--) {
|
||||||
|
stack.push(children[i] as number);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function upTraverseTree(fs: FileSystem, inode: number): number[] {
|
||||||
|
if (fs.getNode(inode) === undefined) return [];
|
||||||
|
|
||||||
|
const result: number[] = [];
|
||||||
|
result.push(inode);
|
||||||
|
|
||||||
|
let currentInode = inode;
|
||||||
|
while (currentInode !== 0) {
|
||||||
|
const node = fs.getNode(currentInode) as TreeNode;
|
||||||
|
if (!node) break;
|
||||||
|
|
||||||
|
result.push(node.parent);
|
||||||
|
currentInode = node.parent;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
File diff suppressed because one or more lines are too long
@@ -0,0 +1,52 @@
|
|||||||
|
type Listener = (payload: unknown) => void;
|
||||||
|
|
||||||
|
export class EventEmitter<TEvents> {
|
||||||
|
private Handlers: Map<keyof TEvents, Set<Listener>> = new Map();
|
||||||
|
|
||||||
|
public on<K extends keyof TEvents>(type: K, handler: (payload: TEvents[K]) => void): () => void {
|
||||||
|
const listeners = this.Handlers.get(type);
|
||||||
|
|
||||||
|
if (listeners) {
|
||||||
|
listeners.add(handler as Listener);
|
||||||
|
} else {
|
||||||
|
const set: Set<Listener> = new Set();
|
||||||
|
set.add(handler as Listener);
|
||||||
|
this.Handlers.set(type, set);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (): void => this.off(type, handler);
|
||||||
|
}
|
||||||
|
|
||||||
|
public off<K extends keyof TEvents>(type: K, handler: (payload: TEvents[K]) => void): void {
|
||||||
|
this.Handlers.get(type)?.delete(handler as Listener);
|
||||||
|
}
|
||||||
|
|
||||||
|
public once<K extends keyof TEvents>(type: K, handler: (payload: TEvents[K]) => void): () => void {
|
||||||
|
const listeners = this.Handlers.get(type);
|
||||||
|
|
||||||
|
const wrapper = (payload: TEvents[K]) => {
|
||||||
|
handler(payload);
|
||||||
|
this.off(type, wrapper);
|
||||||
|
};
|
||||||
|
|
||||||
|
if (listeners) {
|
||||||
|
listeners.add(wrapper as Listener);
|
||||||
|
} else {
|
||||||
|
const set: Set<Listener> = new Set();
|
||||||
|
set.add(wrapper as Listener);
|
||||||
|
this.Handlers.set(type, set);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (): void => this.off(type, wrapper);
|
||||||
|
}
|
||||||
|
|
||||||
|
public emit<K extends keyof TEvents>(type: K, payload: TEvents[K]): void {
|
||||||
|
const listeners = this.Handlers.get(type);
|
||||||
|
|
||||||
|
if (listeners) {
|
||||||
|
for (const listener of [...listeners]) {
|
||||||
|
listener(payload);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,177 @@
|
|||||||
|
import type { CommandDescriptor } from "@core/command";
|
||||||
|
import type { CommandIO } from "@core/command_io";
|
||||||
|
import { createErrorResult, createSuccessResult, type ResultError, type ResultSuccess } from "@core/result";
|
||||||
|
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";
|
||||||
|
|
||||||
|
export class Process {
|
||||||
|
private _SignalQueue: Signal[] = [];
|
||||||
|
private _SignalHandler: ((signal: Signal) => void) | null = null;
|
||||||
|
private _StdoutChunks: Uint8Array[] = [];
|
||||||
|
private _StderrChunks: Uint8Array[] = [];
|
||||||
|
private _Settled: boolean = false;
|
||||||
|
private _EventEmitter: EventEmitter<ShellEventMap>;
|
||||||
|
private readonly _MaxChunkByteSize: number;
|
||||||
|
|
||||||
|
readonly PID: number;
|
||||||
|
readonly Name: string;
|
||||||
|
readonly CommandDescriptor: CommandDescriptor;
|
||||||
|
readonly State: ShellState;
|
||||||
|
readonly Args: string[];
|
||||||
|
|
||||||
|
constructor(pid: number, state: ShellState, descriptor: CommandDescriptor, args: string[]) {
|
||||||
|
this.PID = pid;
|
||||||
|
this.State = state;
|
||||||
|
this.Name = descriptor.name;
|
||||||
|
this.CommandDescriptor = descriptor;
|
||||||
|
this.Args = args;
|
||||||
|
// Check if positive later and throw an error if so
|
||||||
|
this._MaxChunkByteSize = 1024;
|
||||||
|
this._EventEmitter = new EventEmitter<ShellEventMap>();
|
||||||
|
}
|
||||||
|
|
||||||
|
// WARNING, CHUNKING OUTPUT BREAKS FORMATING
|
||||||
|
private writeToOutput(chunks: Uint8Array[], eventType: "stdout" | "stderr", data: Uint8Array): void {
|
||||||
|
if (this._Settled) return;
|
||||||
|
|
||||||
|
if (data.length <= this._MaxChunkByteSize) {
|
||||||
|
chunks.push(data);
|
||||||
|
this._EventEmitter.emit(eventType, { pid: this.PID, data });
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
const arr = splitUint8Array(data, this._MaxChunkByteSize);
|
||||||
|
for (const ent of arr) {
|
||||||
|
chunks.push(ent);
|
||||||
|
this._EventEmitter.emit(eventType, { pid: this.PID, data: ent });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public execute(): void {
|
||||||
|
const io: CommandIO = {
|
||||||
|
writeStdout: (data: Uint8Array) => this.writeToOutput(this._StdoutChunks, "stdout", data),
|
||||||
|
writeStderr: (data: Uint8Array) => this.writeToOutput(this._StderrChunks, "stderr", data),
|
||||||
|
getSignal: () => {
|
||||||
|
const signal: Signal | undefined = this._SignalQueue.shift();
|
||||||
|
if (signal !== undefined) return Promise.resolve(signal);
|
||||||
|
|
||||||
|
const promise = new Promise<Signal>(resolve => {
|
||||||
|
this._SignalHandler = resolve;
|
||||||
|
});
|
||||||
|
return promise;
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
const promise = this.CommandDescriptor.fn(io, this.State, this.Args);
|
||||||
|
|
||||||
|
promise
|
||||||
|
.then(output => {
|
||||||
|
if (this._Settled) return;
|
||||||
|
|
||||||
|
let result: ResultSuccess | ResultError<ShellError>;
|
||||||
|
const stdout: Uint8Array = concatUint8Arrays(this._StdoutChunks);
|
||||||
|
const stderr: Uint8Array = concatUint8Arrays(this._StderrChunks);
|
||||||
|
|
||||||
|
if (output.result.type === "success") {
|
||||||
|
result = createSuccessResult(
|
||||||
|
this.CommandDescriptor.name,
|
||||||
|
0,
|
||||||
|
createImmutableBuffer(stdout),
|
||||||
|
createImmutableBuffer(stderr),
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
result = {
|
||||||
|
...output.result,
|
||||||
|
stdout: createImmutableBuffer(stdout),
|
||||||
|
stderr: createImmutableBuffer(stderr),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
this._EventEmitter.emit("result", { pid: this.PID, result, state: output.state });
|
||||||
|
this._Settled = true;
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
if (this._Settled) return;
|
||||||
|
|
||||||
|
const result: ResultError<ShellError> = createErrorResult(
|
||||||
|
"GenericError",
|
||||||
|
"Promised output from command function rejected",
|
||||||
|
{},
|
||||||
|
this.CommandDescriptor.name,
|
||||||
|
undefined,
|
||||||
|
undefined,
|
||||||
|
{
|
||||||
|
name: "Caught error",
|
||||||
|
message: String(error),
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
this._EventEmitter.emit("result", { pid: this.PID, result, state: this.State });
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public sendSignal(signal: Signal): void {
|
||||||
|
if (this._Settled) return;
|
||||||
|
|
||||||
|
const result: ResultError<SignalTerminationError> = createErrorResult(
|
||||||
|
"SignalTermination",
|
||||||
|
`Process ${this.Name}(${this.PID}) killed by a ${SIGNAL_MAP[signal.type]}(${signal.type})`,
|
||||||
|
{ signalNumber: signal.type, signalName: SIGNAL_MAP[signal.type] },
|
||||||
|
this.CommandDescriptor.name,
|
||||||
|
);
|
||||||
|
|
||||||
|
switch (signal.type) {
|
||||||
|
case SIGNAL_MAP.SIGKILL:
|
||||||
|
this._SignalHandler = null;
|
||||||
|
this._Settled = true;
|
||||||
|
|
||||||
|
this._EventEmitter.emit("result", { pid: this.PID, result: result, state: this.State });
|
||||||
|
return;
|
||||||
|
|
||||||
|
case SIGNAL_MAP.SIGTERM:
|
||||||
|
if (this._SignalHandler !== null) {
|
||||||
|
this._SignalHandler(signal);
|
||||||
|
this._SignalHandler = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
this._Settled = true;
|
||||||
|
this._EventEmitter.emit("result", { pid: this.PID, result: result, state: this.State });
|
||||||
|
|
||||||
|
return;
|
||||||
|
case SIGNAL_MAP.SIGINT:
|
||||||
|
if (this._SignalHandler !== null) {
|
||||||
|
this._SignalHandler(signal);
|
||||||
|
this._SignalHandler = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
this._Settled = true;
|
||||||
|
|
||||||
|
this._EventEmitter.emit("result", { pid: this.PID, result: result, state: this.State });
|
||||||
|
return;
|
||||||
|
default:
|
||||||
|
if (this._SignalHandler !== null) {
|
||||||
|
this._SignalHandler(signal);
|
||||||
|
this._SignalHandler = null;
|
||||||
|
} else {
|
||||||
|
this._SignalQueue.push(signal);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public on<K extends keyof ShellEventMap>(type: K, handler: (payload: ShellEventMap[K]) => void): () => void {
|
||||||
|
return this._EventEmitter.on(type, handler);
|
||||||
|
}
|
||||||
|
|
||||||
|
public once<K extends keyof ShellEventMap>(type: K, handler: (payload: ShellEventMap[K]) => void): () => void {
|
||||||
|
return this._EventEmitter.once(type, handler);
|
||||||
|
}
|
||||||
|
|
||||||
|
public off<K extends keyof ShellEventMap>(type: K, handler: (payload: ShellEventMap[K]) => void): void {
|
||||||
|
this._EventEmitter.off(type, handler);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,68 @@
|
|||||||
|
import type { CommandDescriptor } from "@core/command";
|
||||||
|
import type { ShellState } from "@core/shell_state";
|
||||||
|
import { SIGNAL_MAP, type Signal } from "@core/signal";
|
||||||
|
import { EventEmitter } from "./event_emitter";
|
||||||
|
import { Process } from "./process";
|
||||||
|
import type { ShellEventMap } from "./shell";
|
||||||
|
|
||||||
|
export class ProcessManager {
|
||||||
|
private _Processes: Map<number, Process> = new Map();
|
||||||
|
private _PIDIndex: number = 0;
|
||||||
|
|
||||||
|
private _EventEmitter: EventEmitter<ShellEventMap> = new EventEmitter();
|
||||||
|
|
||||||
|
public createProcess(state: ShellState, descriptor: CommandDescriptor, args: string[]): number {
|
||||||
|
const pid = ++this._PIDIndex;
|
||||||
|
const process = new Process(pid, state, descriptor, args);
|
||||||
|
this._Processes.set(pid, process);
|
||||||
|
|
||||||
|
process.on("stdout", payload => this._EventEmitter.emit("stdout", payload));
|
||||||
|
process.on("stderr", payload => this._EventEmitter.emit("stderr", payload));
|
||||||
|
process.on("result", payload => {
|
||||||
|
this._Processes.delete(payload.pid);
|
||||||
|
this._EventEmitter.emit("result", payload);
|
||||||
|
});
|
||||||
|
|
||||||
|
this._EventEmitter.emit("process", { pid, name: descriptor.name });
|
||||||
|
process.execute();
|
||||||
|
|
||||||
|
return pid;
|
||||||
|
}
|
||||||
|
|
||||||
|
public sendSignal(pid: number, signal: Signal): void {
|
||||||
|
const process = this._Processes.get(pid);
|
||||||
|
|
||||||
|
if (!process) {
|
||||||
|
//Implement a proper error object later to return to shell
|
||||||
|
console.error("PID not found");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
process.sendSignal(signal);
|
||||||
|
}
|
||||||
|
|
||||||
|
public kill(pid: number): void {
|
||||||
|
const process = this._Processes.get(pid);
|
||||||
|
|
||||||
|
if (!process) {
|
||||||
|
//Implement a proper error object later to return to shell
|
||||||
|
console.error("PID not found");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Make global signals an enum later
|
||||||
|
process.sendSignal({ type: SIGNAL_MAP.SIGKILL } satisfies Signal);
|
||||||
|
}
|
||||||
|
|
||||||
|
public on<K extends keyof ShellEventMap>(type: K, handler: (payload: ShellEventMap[K]) => void): () => void {
|
||||||
|
return this._EventEmitter.on(type, handler);
|
||||||
|
}
|
||||||
|
|
||||||
|
public once<K extends keyof ShellEventMap>(type: K, handler: (payload: ShellEventMap[K]) => void): () => void {
|
||||||
|
return this._EventEmitter.once(type, handler);
|
||||||
|
}
|
||||||
|
|
||||||
|
public off<K extends keyof ShellEventMap>(type: K, handler: (payload: ShellEventMap[K]) => void): void {
|
||||||
|
this._EventEmitter.off(type, handler);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,92 @@
|
|||||||
|
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"),
|
||||||
|
//eslint-ignore
|
||||||
|
children: z.array(z.lazy((): any => TreeNodeSchema)), // eslint-disable-line @typescript-eslint/no-explicit-any
|
||||||
|
});
|
||||||
|
|
||||||
|
const TreeNodeSchema = z.union([FileNodeSchema, DirectoryNodeSchema]);
|
||||||
|
|
||||||
|
/* export function serializeFileSystem(map: ReadonlyMap<number, TreeNode | null>): string {
|
||||||
|
|
||||||
|
} */
|
||||||
|
|
||||||
|
export function deserializeFileSystem(json: string): ReadonlyMap<number, TreeNode> {
|
||||||
|
const raw = JSON.parse(json);
|
||||||
|
const validated = TreeNodeSchema.parse(raw);
|
||||||
|
|
||||||
|
const map = new Map<number, TreeNode>();
|
||||||
|
|
||||||
|
// prettier-ignore
|
||||||
|
function parseNode(tree: any, parent: number): void { // eslint-disable-line @typescript-eslint/no-explicit-any
|
||||||
|
if (tree.type === "directory") {
|
||||||
|
const children: Record<string, number> = {};
|
||||||
|
|
||||||
|
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(Uint8Array.from(tree.content)),
|
||||||
|
};
|
||||||
|
map.set(tree.inode, Object.freeze(fileNode));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
parseNode(validated, validated.inode);
|
||||||
|
const base: ReadonlyMap<number, TreeNode> = map;
|
||||||
|
|
||||||
|
return base;
|
||||||
|
}
|
||||||
@@ -0,0 +1,150 @@
|
|||||||
|
import { COMMAND_REGISTRY } from "@core/command_registry";
|
||||||
|
import { createErrorResult, type Result } from "@core/result";
|
||||||
|
import { getCurrentSession } from "@core/session_manager";
|
||||||
|
import { withExitCode, withHistory, type ShellState } from "@core/shell_state";
|
||||||
|
import { SIGNAL_MAP } from "@core/signal";
|
||||||
|
import type { ShellError } from "@core/util/errors";
|
||||||
|
import { createImmutableBuffer } from "@core/util/immutable_buffer";
|
||||||
|
import { tokenize } from "@core/util/parser";
|
||||||
|
import { EventEmitter } from "./event_emitter";
|
||||||
|
import { ProcessManager } from "./process_manager";
|
||||||
|
|
||||||
|
export class Shell {
|
||||||
|
static #Instance: Shell | null;
|
||||||
|
private _State: Readonly<ShellState>;
|
||||||
|
private _ProcessManager: ProcessManager = new ProcessManager();
|
||||||
|
private _EventEmitter: EventEmitter<ShellEventMap> = new EventEmitter();
|
||||||
|
private _GlobalKeyMap: ReadonlyMap<string, SIGNAL_MAP>;
|
||||||
|
|
||||||
|
public BoundPID: number | null = null;
|
||||||
|
|
||||||
|
private constructor(state: ShellState) {
|
||||||
|
this._State = Object.freeze(state);
|
||||||
|
|
||||||
|
//Temporary solution for sigint keybind
|
||||||
|
const map = new Map();
|
||||||
|
map.set("ctrl+c", SIGNAL_MAP.SIGINT);
|
||||||
|
|
||||||
|
this._GlobalKeyMap = map;
|
||||||
|
|
||||||
|
this._ProcessManager.on("stdout", payload => this._EventEmitter.emit("stdout", payload));
|
||||||
|
this._ProcessManager.on("stderr", payload => this._EventEmitter.emit("stderr", payload));
|
||||||
|
this._ProcessManager.on("process", payload => this._EventEmitter.emit("process", payload));
|
||||||
|
this._ProcessManager.on("result", payload => {
|
||||||
|
this._State = payload.state;
|
||||||
|
this._State = withExitCode(
|
||||||
|
this._State,
|
||||||
|
payload.result.type === "success" ? payload.result.exitCode : payload.result.error.exitCode,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (this.BoundPID === payload.pid) this.BoundPID = null;
|
||||||
|
this._EventEmitter.emit("result", payload);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
static destroy(): void {
|
||||||
|
Shell.#Instance = null;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
static init(initialState: ShellState): Shell {
|
||||||
|
if (Shell.#Instance) {
|
||||||
|
console.warn("Shell already initialized");
|
||||||
|
return Shell.#Instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
Shell.#Instance = new Shell(initialState);
|
||||||
|
return Shell.#Instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async execute(input: string): Promise<void> {
|
||||||
|
const parsed = tokenize(input);
|
||||||
|
|
||||||
|
if (!parsed[0]) return;
|
||||||
|
|
||||||
|
this._State = withHistory(this._State, input);
|
||||||
|
const command = COMMAND_REGISTRY.get(parsed[0]);
|
||||||
|
|
||||||
|
if (!command) {
|
||||||
|
const encoded = new TextEncoder().encode(`Command ${parsed[0]} not found`);
|
||||||
|
|
||||||
|
const result = createErrorResult(
|
||||||
|
"CommandNotFound",
|
||||||
|
`Command ${parsed[0]} not found`,
|
||||||
|
{ command: parsed[0] },
|
||||||
|
parsed[0],
|
||||||
|
createImmutableBuffer(encoded),
|
||||||
|
createImmutableBuffer(new Uint8Array()),
|
||||||
|
);
|
||||||
|
|
||||||
|
this._State = withExitCode(this._State, result.error.exitCode);
|
||||||
|
this._EventEmitter.emit("result", { pid: 0, result, state: this._State });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (command.requiresRoot && getCurrentSession(this._State).user.uid != 0) {
|
||||||
|
const result = createErrorResult(
|
||||||
|
"PermissionDenied",
|
||||||
|
`Permission Denied`,
|
||||||
|
{ resource: `Execution of the ${command.name} command` },
|
||||||
|
parsed[0],
|
||||||
|
);
|
||||||
|
|
||||||
|
this._State = withExitCode(this._State, result.error.exitCode);
|
||||||
|
this._EventEmitter.emit("result", { pid: 0, result, state: this._State });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
parsed.shift();
|
||||||
|
|
||||||
|
const pid = this._ProcessManager.createProcess(this._State, command, parsed);
|
||||||
|
this.BoundPID = pid;
|
||||||
|
}
|
||||||
|
|
||||||
|
public sendInput(input: string): void {
|
||||||
|
const signal = this._GlobalKeyMap.get(input);
|
||||||
|
|
||||||
|
if (signal !== undefined) {
|
||||||
|
if (this.BoundPID !== null) {
|
||||||
|
this._ProcessManager.sendSignal(this.BoundPID, { type: signal });
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (this.BoundPID !== null) {
|
||||||
|
this._ProcessManager.sendSignal(this.BoundPID, { type: SIGNAL_MAP.INPUT, payload: input });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public get State(): Readonly<ShellState> {
|
||||||
|
return this._State;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Instance(initialState?: ShellState): Shell {
|
||||||
|
if (!Shell.#Instance) {
|
||||||
|
if (!initialState) {
|
||||||
|
throw new Error("Shell needs to be initialized first");
|
||||||
|
}
|
||||||
|
Shell.#Instance = new Shell(initialState);
|
||||||
|
}
|
||||||
|
return Shell.#Instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
public on<K extends keyof ShellEventMap>(type: K, handler: (payload: ShellEventMap[K]) => void): () => void {
|
||||||
|
return this._EventEmitter.on(type, handler);
|
||||||
|
}
|
||||||
|
|
||||||
|
public once<K extends keyof ShellEventMap>(type: K, handler: (payload: ShellEventMap[K]) => void): () => void {
|
||||||
|
return this._EventEmitter.once(type, handler);
|
||||||
|
}
|
||||||
|
|
||||||
|
public off<K extends keyof ShellEventMap>(type: K, handler: (payload: ShellEventMap[K]) => void): void {
|
||||||
|
this._EventEmitter.off(type, handler);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ShellEventMap {
|
||||||
|
process: { pid: number; name: string };
|
||||||
|
stdout: { pid: number; data: Uint8Array };
|
||||||
|
stderr: { pid: number; data: Uint8Array };
|
||||||
|
result: { pid: number; result: Result<ShellError>; state: ShellState };
|
||||||
|
}
|
||||||
@@ -0,0 +1,74 @@
|
|||||||
|
/* import { MD5 } from "./hash";
|
||||||
|
|
||||||
|
//INDEXED DB IS SO FUCKING DOGSHIT ITS INSANE
|
||||||
|
|
||||||
|
//TODO: load IDB db name from config;
|
||||||
|
|
||||||
|
export async function initializeIDB() {
|
||||||
|
const request = indexedDB.open("cache");
|
||||||
|
request.onerror = e => {
|
||||||
|
throw new Error(`Failed to open the IDB during the init - ${request.error}`);
|
||||||
|
};
|
||||||
|
|
||||||
|
request.onsuccess = e => {
|
||||||
|
const DB = request.result;
|
||||||
|
const STORE = DB.createObjectStore("fs", { autoIncrement: true });
|
||||||
|
|
||||||
|
STORE.createIndex("json", "json");
|
||||||
|
STORE.createIndex("checksum", "checksum");
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function writeFileSystemCache(cache: string, checksum: string) {
|
||||||
|
if (cache.length === 0 || checksum.length === 0) {
|
||||||
|
throw new Error(`Cache value or the checksum value cannot be empty!`);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!isChecksumMatching(cache, checksum))
|
||||||
|
throw new Error("The cache value checksum does not match the provided checksum");
|
||||||
|
|
||||||
|
const request = indexedDB.open("cache");
|
||||||
|
request.onerror = e => {
|
||||||
|
throw new Error(`Failed to open the IDB during the init - ${request.error}`);
|
||||||
|
};
|
||||||
|
|
||||||
|
request.onsuccess = e => {
|
||||||
|
const DB = request.result;
|
||||||
|
const T = DB.transaction(["cache"], "readwrite");
|
||||||
|
const STOREFS = T.objectStore("fs");
|
||||||
|
const STORECK = T.objectStore("fs");
|
||||||
|
|
||||||
|
const RFS = STOREFS.add(cache);
|
||||||
|
//const CFS =
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function fetchFileSystemCache(): Promise<{ cache: string; checksum: string }> {
|
||||||
|
const request = indexedDB.open("cache");
|
||||||
|
|
||||||
|
request.onerror = e => {
|
||||||
|
throw new Error(`Failed to open the IDB - ${request.error}`);
|
||||||
|
};
|
||||||
|
|
||||||
|
request.onsuccess = e => {
|
||||||
|
const DB = request.result;
|
||||||
|
const T = DB.transaction(["cache"], "readonly");
|
||||||
|
const STORE = T.objectStore("fs");
|
||||||
|
|
||||||
|
const R = STORE.get("1");
|
||||||
|
|
||||||
|
R.onerror = e => {
|
||||||
|
throw new Error(`Some IDB error - ${R.error}`);
|
||||||
|
};
|
||||||
|
|
||||||
|
R.onsuccess = e => {
|
||||||
|
console.log(R.result.json, R.result.checksum);
|
||||||
|
};
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function isChecksumMatching(json: string, checksum: string): boolean {
|
||||||
|
const resourceChksm = MD5.hash(json);
|
||||||
|
return resourceChksm === checksum;
|
||||||
|
}
|
||||||
|
*/
|
||||||
@@ -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;
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
export function convertToUnit(bytes: number, decimalPoint?: number, pow: 1024 | 1000 = 1024): string {
|
||||||
|
if (!+bytes) return "0";
|
||||||
|
|
||||||
|
const k: number = pow;
|
||||||
|
const dp: number = decimalPoint ? (decimalPoint < 0 ? 0 : decimalPoint) : 1;
|
||||||
|
const units: string[] = ["", "K", "M", "G", "T", "P"];
|
||||||
|
|
||||||
|
const i: number = Math.floor(Math.log(bytes) / Math.log(k));
|
||||||
|
|
||||||
|
return `${(bytes / Math.pow(k, i)).toFixed(dp)}${units[i]}`;
|
||||||
|
}
|
||||||
@@ -0,0 +1,178 @@
|
|||||||
|
export class MD5 {
|
||||||
|
private static encoder: TextEncoder = new TextEncoder();
|
||||||
|
|
||||||
|
// prettier-ignore
|
||||||
|
private static K: number[] = [
|
||||||
|
0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee,
|
||||||
|
0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501,
|
||||||
|
0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be,
|
||||||
|
0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821,
|
||||||
|
0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa,
|
||||||
|
0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8,
|
||||||
|
0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed,
|
||||||
|
0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a,
|
||||||
|
0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c,
|
||||||
|
0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70,
|
||||||
|
0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05,
|
||||||
|
0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665,
|
||||||
|
0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039,
|
||||||
|
0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1,
|
||||||
|
0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1,
|
||||||
|
0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391
|
||||||
|
];
|
||||||
|
|
||||||
|
// prettier-ignore
|
||||||
|
private static S: number[] = [
|
||||||
|
7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22,
|
||||||
|
5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20,
|
||||||
|
4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23,
|
||||||
|
6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21
|
||||||
|
];
|
||||||
|
|
||||||
|
public static hash(input: string) {
|
||||||
|
const inputArr: Uint8Array<ArrayBuffer> = this.encoder.encode(input);
|
||||||
|
const padded = MD5.pad(inputArr);
|
||||||
|
|
||||||
|
let A = 0x67452301;
|
||||||
|
let B = 0xefcdab89;
|
||||||
|
let C = 0x98badcfe;
|
||||||
|
let D = 0x10325476;
|
||||||
|
|
||||||
|
function processBlock(M: number[]) {
|
||||||
|
function F(b: number, c: number, d: number): number {
|
||||||
|
return (b & c) | (~b & d);
|
||||||
|
}
|
||||||
|
function H(b: number, c: number, d: number): number {
|
||||||
|
return b ^ c ^ d;
|
||||||
|
}
|
||||||
|
function I(b: number, c: number, d: number): number {
|
||||||
|
return c ^ (b | ~d);
|
||||||
|
}
|
||||||
|
function G(b: number, c: number, d: number): number {
|
||||||
|
return (b & d) | (c & ~d);
|
||||||
|
}
|
||||||
|
|
||||||
|
const AA = A,
|
||||||
|
BB = B,
|
||||||
|
CC = C,
|
||||||
|
DD = D;
|
||||||
|
|
||||||
|
for (let i = 0; i < 64; i++) {
|
||||||
|
let f: number, g: number;
|
||||||
|
|
||||||
|
if (i < 16) {
|
||||||
|
f = F(B, C, D);
|
||||||
|
g = i;
|
||||||
|
} else if (i < 32) {
|
||||||
|
f = G(B, C, D);
|
||||||
|
g = (5 * i + 1) % 16;
|
||||||
|
} else if (i < 48) {
|
||||||
|
f = H(B, C, D);
|
||||||
|
g = (3 * i + 5) % 16;
|
||||||
|
} else {
|
||||||
|
f = I(B, C, D);
|
||||||
|
g = (7 * i) % 16;
|
||||||
|
}
|
||||||
|
|
||||||
|
const sum = MD5.add32(A, MD5.add32(f, MD5.add32(M[g]!, MD5.K[i]!)));
|
||||||
|
const temp = MD5.add32(B, MD5.leftRotate(sum, MD5.S[i]!));
|
||||||
|
|
||||||
|
A = D;
|
||||||
|
D = C;
|
||||||
|
C = B;
|
||||||
|
B = temp;
|
||||||
|
}
|
||||||
|
|
||||||
|
A = MD5.add32(A, AA);
|
||||||
|
B = MD5.add32(B, BB);
|
||||||
|
C = MD5.add32(C, CC);
|
||||||
|
D = MD5.add32(D, DD);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (let i = 0; i < padded.length; i += 64) {
|
||||||
|
const M = MD5.sliceToWords(padded.slice(i, i + 64));
|
||||||
|
processBlock(M); // modifies A, B, C, D directly
|
||||||
|
}
|
||||||
|
|
||||||
|
const hashBytes = new Uint8Array([
|
||||||
|
...MD5.wordToBytesLE(A),
|
||||||
|
...MD5.wordToBytesLE(B),
|
||||||
|
...MD5.wordToBytesLE(C),
|
||||||
|
...MD5.wordToBytesLE(D),
|
||||||
|
]);
|
||||||
|
|
||||||
|
return MD5.toHexString(hashBytes);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static pad(input: Uint8Array): Uint8Array {
|
||||||
|
const inputBitLength: number = input.length * 8;
|
||||||
|
|
||||||
|
const out: Uint8Array = new Uint8Array([...input, 0x80]);
|
||||||
|
const NBytes: number = (56 - (out.length % 64) + 64) % 64;
|
||||||
|
|
||||||
|
const zeroArray = new Uint8Array(NBytes);
|
||||||
|
|
||||||
|
const low = inputBitLength >>> 0;
|
||||||
|
const high = (inputBitLength / 0x100000000) >>> 0;
|
||||||
|
|
||||||
|
const lengthBytes = new Uint8Array([
|
||||||
|
low & 0xff,
|
||||||
|
(low >> 8) & 0xff,
|
||||||
|
(low >> 16) & 0xff,
|
||||||
|
(low >> 24) & 0xff,
|
||||||
|
high & 0xff,
|
||||||
|
(high >> 8) & 0xff,
|
||||||
|
(high >> 16) & 0xff,
|
||||||
|
(high >> 24) & 0xff,
|
||||||
|
]);
|
||||||
|
|
||||||
|
return new Uint8Array([...out, ...zeroArray, ...lengthBytes]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* private static sliceToBlocks(input: Uint8Array): number[][] {
|
||||||
|
const blocks: number[][] = [];
|
||||||
|
|
||||||
|
for (let i = 0; i < input.length; i += 64) {
|
||||||
|
const block = input.slice(i, i + 64);
|
||||||
|
blocks.push(MD5.sliceToWords(block));
|
||||||
|
}
|
||||||
|
|
||||||
|
return blocks;
|
||||||
|
} */
|
||||||
|
|
||||||
|
private static sliceToWords(block: Uint8Array): number[] {
|
||||||
|
const words: number[] = new Array<number>(16);
|
||||||
|
|
||||||
|
for (let i = 0; i < 16; i++) {
|
||||||
|
const offset: number = i * 4;
|
||||||
|
|
||||||
|
words[i] =
|
||||||
|
(block[offset]! |
|
||||||
|
(block[offset + 1]! << 8) |
|
||||||
|
(block[offset + 2]! << 16) |
|
||||||
|
(block[offset + 3]! << 24)) >>>
|
||||||
|
0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return words;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static leftRotate(x: number, n: number): number {
|
||||||
|
x = x >>> 0;
|
||||||
|
return ((x << n) | (x >>> (32 - n))) >>> 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static add32(a: number, b: number): number {
|
||||||
|
return ((a >>> 0) + (b >>> 0)) >>> 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static wordToBytesLE(word: number): Uint8Array {
|
||||||
|
return new Uint8Array([word & 0xff, (word >>> 8) & 0xff, (word >>> 16) & 0xff, (word >>> 24) & 0xff]);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static toHexString(bytes: Uint8Array): string {
|
||||||
|
return Array.from(bytes)
|
||||||
|
.map(b => b.toString(16).padStart(2, "0"))
|
||||||
|
.join("");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,112 @@
|
|||||||
|
export class Posix {
|
||||||
|
private static readonly CharTable = {
|
||||||
|
// Space and punctuation (32-47)
|
||||||
|
" ": 32,
|
||||||
|
"!": 33,
|
||||||
|
'"': 34,
|
||||||
|
"#": 35,
|
||||||
|
$: 36,
|
||||||
|
"%": 37,
|
||||||
|
"&": 38,
|
||||||
|
"'": 39,
|
||||||
|
"(": 40,
|
||||||
|
")": 41,
|
||||||
|
"*": 42,
|
||||||
|
"+": 43,
|
||||||
|
",": 44,
|
||||||
|
"-": 45,
|
||||||
|
".": 46,
|
||||||
|
"/": 47,
|
||||||
|
|
||||||
|
// Numbers 0-9 (48-57)
|
||||||
|
"0": 48,
|
||||||
|
"1": 49,
|
||||||
|
"2": 50,
|
||||||
|
"3": 51,
|
||||||
|
"4": 52,
|
||||||
|
"5": 53,
|
||||||
|
"6": 54,
|
||||||
|
"7": 55,
|
||||||
|
"8": 56,
|
||||||
|
"9": 57,
|
||||||
|
|
||||||
|
// More punctuation (58-64)
|
||||||
|
":": 58,
|
||||||
|
";": 59,
|
||||||
|
"<": 60,
|
||||||
|
"=": 61,
|
||||||
|
">": 62,
|
||||||
|
"?": 63,
|
||||||
|
"@": 64,
|
||||||
|
|
||||||
|
// LETTERS - INTERLEAVED AaBbCc... (65-116)
|
||||||
|
A: 65,
|
||||||
|
a: 66,
|
||||||
|
B: 67,
|
||||||
|
b: 68,
|
||||||
|
C: 69,
|
||||||
|
c: 70,
|
||||||
|
D: 71,
|
||||||
|
d: 72,
|
||||||
|
E: 73,
|
||||||
|
e: 74,
|
||||||
|
F: 75,
|
||||||
|
f: 76,
|
||||||
|
G: 77,
|
||||||
|
g: 78,
|
||||||
|
H: 79,
|
||||||
|
h: 80,
|
||||||
|
I: 81,
|
||||||
|
i: 82,
|
||||||
|
J: 83,
|
||||||
|
j: 84,
|
||||||
|
K: 85,
|
||||||
|
k: 86,
|
||||||
|
L: 87,
|
||||||
|
l: 88,
|
||||||
|
M: 89,
|
||||||
|
m: 90,
|
||||||
|
N: 91,
|
||||||
|
n: 92,
|
||||||
|
O: 93,
|
||||||
|
o: 94,
|
||||||
|
P: 95,
|
||||||
|
p: 96,
|
||||||
|
Q: 97,
|
||||||
|
q: 98,
|
||||||
|
R: 99,
|
||||||
|
r: 100,
|
||||||
|
S: 101,
|
||||||
|
s: 102,
|
||||||
|
T: 103,
|
||||||
|
t: 104,
|
||||||
|
U: 105,
|
||||||
|
u: 106,
|
||||||
|
V: 107,
|
||||||
|
v: 108,
|
||||||
|
W: 109,
|
||||||
|
w: 110,
|
||||||
|
X: 111,
|
||||||
|
x: 112,
|
||||||
|
Y: 113,
|
||||||
|
y: 114,
|
||||||
|
Z: 115,
|
||||||
|
z: 116,
|
||||||
|
|
||||||
|
// Specials after letters (now 117-126)
|
||||||
|
"[": 117,
|
||||||
|
"\\": 118,
|
||||||
|
"]": 119,
|
||||||
|
"^": 120,
|
||||||
|
_: 121,
|
||||||
|
"`": 122,
|
||||||
|
"{": 123,
|
||||||
|
"|": 124,
|
||||||
|
"}": 125,
|
||||||
|
"~": 126,
|
||||||
|
} as const;
|
||||||
|
|
||||||
|
public static GetCharWeight(char: string): number {
|
||||||
|
return this.CharTable[char as keyof typeof this.CharTable];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
/*
|
||||||
|
Create an interface for the rest of the library to use to fetch nessesary data.
|
||||||
|
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 function fetchFileSystem(ep: string = `/api/v1/filesystem`): Promise<string> {
|
||||||
|
// FUCK IT NO CACHE FOR NOW, IDB IS RETARDED
|
||||||
|
const cacheETag = "suck my dick";
|
||||||
|
const cacheData = "no data here";
|
||||||
|
|
||||||
|
const headers: HeadersInit = {};
|
||||||
|
if (cacheETag) {
|
||||||
|
headers["If-None-Match"] = cacheETag;
|
||||||
|
}
|
||||||
|
|
||||||
|
const response = await fetch(ep, { headers });
|
||||||
|
|
||||||
|
let json: string;
|
||||||
|
if (response.status === 304 && cacheData) {
|
||||||
|
json = cacheData;
|
||||||
|
} else if (response.ok) {
|
||||||
|
json = await response.text();
|
||||||
|
const newETag = response.headers.get("ETag");
|
||||||
|
// cache the json string here
|
||||||
|
|
||||||
|
if (newETag) {
|
||||||
|
// cache the new checksum (etag) here
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
throw new Error(`Failed to load filesystem: ${response.status}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return json;
|
||||||
|
}
|
||||||
|
|
||||||
|
//export async function fetchUserGroups(ep: string = ): Promise<JSON> {}
|
||||||
|
|
||||||
|
//export async function fetchUsers(): Promise<JSON> {}
|
||||||
@@ -0,0 +1,61 @@
|
|||||||
|
import type { FileSystem } from "@core/filesystem/filesystem";
|
||||||
|
import { TreeNodeType } from "@core/filesystem/treenode";
|
||||||
|
|
||||||
|
export function printTree(fs: FileSystem, root: number): string {
|
||||||
|
const rootNode = fs.getNode(root);
|
||||||
|
if (rootNode === undefined) return "";
|
||||||
|
|
||||||
|
const lines: string[] = [];
|
||||||
|
|
||||||
|
lines.push(rootNode.name + ` (${rootNode.inode})`);
|
||||||
|
|
||||||
|
type Frame = {
|
||||||
|
inode: number;
|
||||||
|
prefix: string;
|
||||||
|
isLast: boolean;
|
||||||
|
};
|
||||||
|
|
||||||
|
const stack: Frame[] = [];
|
||||||
|
|
||||||
|
const getSorterChildren = (inode: number): number[] => {
|
||||||
|
const node = fs.getNode(inode);
|
||||||
|
if (!node || node.type !== TreeNodeType.Directory) return [];
|
||||||
|
|
||||||
|
const entries = Object.entries(node.children);
|
||||||
|
entries.sort(([a], [b]) => a.localeCompare(b));
|
||||||
|
return entries.map(([, childInode]) => childInode) as number[];
|
||||||
|
};
|
||||||
|
|
||||||
|
const rootChildren = getSorterChildren(root);
|
||||||
|
|
||||||
|
for (let i = rootChildren.length - 1; i >= 0; i--) {
|
||||||
|
const childInode: number = rootChildren[i] as number;
|
||||||
|
const isLast = i === rootChildren.length - 1;
|
||||||
|
|
||||||
|
stack.push({ inode: childInode, prefix: "", isLast });
|
||||||
|
}
|
||||||
|
|
||||||
|
while (stack.length > 0) {
|
||||||
|
const frame = stack.pop()!;
|
||||||
|
const node = fs.getNode(frame.inode);
|
||||||
|
if (!node) continue;
|
||||||
|
|
||||||
|
const name = node.name;
|
||||||
|
const connector = frame.isLast ? "└── " : "├── ";
|
||||||
|
lines.push(frame.prefix + connector + name + ` (${node.inode})`);
|
||||||
|
|
||||||
|
if (node.type === TreeNodeType.Directory) {
|
||||||
|
const childPrefix = frame.prefix + (frame.isLast ? " " : "│ ");
|
||||||
|
const children = getSorterChildren(frame.inode);
|
||||||
|
|
||||||
|
for (let i = children.length - 1; i >= 0; i--) {
|
||||||
|
const childInode = children[i] as number;
|
||||||
|
const childIsLast = i === children.length - 1;
|
||||||
|
|
||||||
|
stack.push({ inode: childInode, prefix: childPrefix, isLast: childIsLast });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return lines.join("\n");
|
||||||
|
}
|
||||||
@@ -0,0 +1,92 @@
|
|||||||
|
import type { CommandIO } from "@core/command_io";
|
||||||
|
import { descriptor as cd } from "@core/commands/cd";
|
||||||
|
import type { ResultError, ResultSuccess } from "@core/result";
|
||||||
|
import type { UserInfo } from "@core/session_manager";
|
||||||
|
import { getCurrentSession } from "@core/session_manager";
|
||||||
|
import { withCwd, type ShellState } from "@core/shell_state";
|
||||||
|
import type { PathResolutionError } from "@core/util/errors";
|
||||||
|
import { Fixture } from "@tests/helpers/state_fixture";
|
||||||
|
import { expect, test } from "vitest";
|
||||||
|
|
||||||
|
const stdout: Uint8Array[] = [];
|
||||||
|
const stderr: Uint8Array[] = [];
|
||||||
|
|
||||||
|
const io: CommandIO = {
|
||||||
|
writeStdout: (data: Uint8Array) => {
|
||||||
|
stdout.push(data);
|
||||||
|
},
|
||||||
|
writeStderr: (data: Uint8Array) => {
|
||||||
|
stderr.push(data);
|
||||||
|
},
|
||||||
|
getSignal: () => {
|
||||||
|
return new Promise(() => {});
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
const users: Map<number, UserInfo> = new Map();
|
||||||
|
const user: UserInfo = {
|
||||||
|
username: "root",
|
||||||
|
uid: 0,
|
||||||
|
gid: 0,
|
||||||
|
group: [0],
|
||||||
|
home: 0,
|
||||||
|
};
|
||||||
|
users.set(0, user);
|
||||||
|
|
||||||
|
const state: ShellState = Fixture.State;
|
||||||
|
|
||||||
|
test("changes cwd to home", async () => {
|
||||||
|
let newState = withCwd(state, 2);
|
||||||
|
const output = await cd.fn(io, newState, [], []);
|
||||||
|
newState = output.state;
|
||||||
|
const result = output.result;
|
||||||
|
const session = getCurrentSession(newState);
|
||||||
|
|
||||||
|
expect((result as ResultSuccess).exitCode).toBe(0);
|
||||||
|
expect(session.cwd).toBe(Fixture.user.home);
|
||||||
|
expect(session.pwd).toBe(2);
|
||||||
|
});
|
||||||
|
test("changes cwd to pwd", async () => {
|
||||||
|
let newState = withCwd(state, 2);
|
||||||
|
|
||||||
|
const output = await cd.fn(io, newState, [], ["-"]);
|
||||||
|
newState = output.state;
|
||||||
|
const result = output.result;
|
||||||
|
const session = getCurrentSession(newState);
|
||||||
|
|
||||||
|
expect((result as ResultSuccess).exitCode).toBe(0);
|
||||||
|
expect(session.cwd).toBe(0);
|
||||||
|
expect(session.pwd).toBe(2);
|
||||||
|
});
|
||||||
|
test("changes cwd to 4", async () => {
|
||||||
|
const output = await cd.fn(io, state, [], ["/folder2/subdir2"]);
|
||||||
|
const newState = output.state;
|
||||||
|
const result = output.result;
|
||||||
|
const session = getCurrentSession(newState);
|
||||||
|
|
||||||
|
expect((result as ResultSuccess).exitCode).toBe(0);
|
||||||
|
expect(session.cwd).toBe(4);
|
||||||
|
expect(session.pwd).toBe(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("returns an error result (tried to enter a non existent location)", async () => {
|
||||||
|
const output = await cd.fn(io, state, [], ["/not/real"]);
|
||||||
|
const newState = output.state;
|
||||||
|
const result = output.result;
|
||||||
|
const session = getCurrentSession(newState);
|
||||||
|
|
||||||
|
expect((result as ResultError<PathResolutionError>).error.exitCode).toBe(64);
|
||||||
|
expect(session.cwd).toBe(0);
|
||||||
|
expect(session.pwd).toBe(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("returns an error result (tried to enter a file)", async () => {
|
||||||
|
const output = await cd.fn(io, state, [], ["/some-file"]);
|
||||||
|
const newState = output.state;
|
||||||
|
const result = output.result;
|
||||||
|
const session = getCurrentSession(newState);
|
||||||
|
|
||||||
|
expect((result as ResultError<PathResolutionError>).error.exitCode).toBe(64);
|
||||||
|
expect(session.cwd).toBe(0);
|
||||||
|
expect(session.pwd).toBe(0);
|
||||||
|
});
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
import type { CommandIO } from "@core/command_io";
|
||||||
|
import { descriptor as echo } from "@core/commands/echo";
|
||||||
|
import type { ResultSuccess } from "@core/result";
|
||||||
|
import { type ShellState } from "@core/shell_state";
|
||||||
|
import { Fixture } from "@tests/helpers/state_fixture";
|
||||||
|
import { expect, test } from "vitest";
|
||||||
|
|
||||||
|
const stdout: Uint8Array[] = [];
|
||||||
|
const stderr: Uint8Array[] = [];
|
||||||
|
|
||||||
|
const io: CommandIO = {
|
||||||
|
writeStdout: (data: Uint8Array) => {
|
||||||
|
stdout.push(data);
|
||||||
|
},
|
||||||
|
writeStderr: (data: Uint8Array) => {
|
||||||
|
stderr.push(data);
|
||||||
|
},
|
||||||
|
getSignal: () => {
|
||||||
|
return new Promise(() => {});
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
const state: ShellState = Fixture.State;
|
||||||
|
|
||||||
|
test("returns a Result of type success", async () => {
|
||||||
|
const output = await echo.fn(io, state, [], ["hello", "world"]);
|
||||||
|
const string = new TextDecoder().decode(stdout[0]);
|
||||||
|
|
||||||
|
expect(output.result.type).toBe("success");
|
||||||
|
expect((output.result as ResultSuccess).exitCode).toBe(0);
|
||||||
|
// Fix: added a newline character
|
||||||
|
expect(string).toEqual("hello world\n");
|
||||||
|
});
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
import type { ResultError } from "@core/result";
|
||||||
|
import { type ShellState } from "@core/shell_state";
|
||||||
|
import { SIGNAL_MAP } from "@core/signal";
|
||||||
|
import type { SignalTerminationError } from "@core/util/errors";
|
||||||
|
import { Shell } from "@shell/shell";
|
||||||
|
import { Fixture } from "@tests/helpers/state_fixture";
|
||||||
|
import { expect, test } from "vitest";
|
||||||
|
|
||||||
|
const state: ShellState = Fixture.State;
|
||||||
|
|
||||||
|
test("returns a Result of type success", async () => {
|
||||||
|
await Shell.init(state);
|
||||||
|
|
||||||
|
let result;
|
||||||
|
|
||||||
|
Shell.Instance?.on("result", payload => {
|
||||||
|
//const decoder = new TextDecoder();
|
||||||
|
//console.log(decoder.decode(payload.result.stdout.toArray()));
|
||||||
|
//console.log(payload);
|
||||||
|
result = payload.result;
|
||||||
|
|
||||||
|
expect(result.type).toBe("error");
|
||||||
|
expect((result as ResultError<SignalTerminationError>).error.exitCode).toEqual(130);
|
||||||
|
expect((result as ResultError<SignalTerminationError>).error.signalNumber).toEqual(SIGNAL_MAP.SIGINT);
|
||||||
|
});
|
||||||
|
|
||||||
|
await Shell.Instance?.execute("async");
|
||||||
|
Shell.Instance?.sendInput("a");
|
||||||
|
Shell.Instance?.sendInput("b");
|
||||||
|
Shell.Instance?.sendInput("c");
|
||||||
|
Shell.Instance?.sendInput("d");
|
||||||
|
Shell.Instance?.sendInput(
|
||||||
|
"right now technically anything goes because its just a plain string however i plan on improving this system a bit later on",
|
||||||
|
);
|
||||||
|
Shell.Instance?.sendInput("ctrl+c");
|
||||||
|
});
|
||||||
@@ -0,0 +1,400 @@
|
|||||||
|
import { FileSystem } from "@core/filesystem/filesystem";
|
||||||
|
import { TreeNodeType, type DirectoryNode, type FileNode, type TreeNode } from "@core/filesystem/treenode";
|
||||||
|
import type { UserInfo } from "@core/session_manager";
|
||||||
|
import { createImmutableBuffer } from "@core/util/immutable_buffer";
|
||||||
|
import {
|
||||||
|
expectAlreadyExistsError,
|
||||||
|
expectDirectoryLoopError,
|
||||||
|
expectIllegalMutationError,
|
||||||
|
expectNodeNotFoundError,
|
||||||
|
expectNotADirectoryError,
|
||||||
|
} from "@tests/helpers/error_matchers";
|
||||||
|
import { buildTestFS, type FixtureNode } from "@tests/helpers/filesystems_fixture";
|
||||||
|
import { describe, expect, it } from "vitest";
|
||||||
|
|
||||||
|
const testTree: FixtureNode = {
|
||||||
|
inode: 0,
|
||||||
|
name: "root",
|
||||||
|
type: "Dir",
|
||||||
|
children: [
|
||||||
|
{
|
||||||
|
inode: 1,
|
||||||
|
name: "hello.txt",
|
||||||
|
type: "File",
|
||||||
|
content: "Goodbye World!",
|
||||||
|
children: [],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
inode: 2,
|
||||||
|
name: "sub",
|
||||||
|
type: "Dir",
|
||||||
|
children: [
|
||||||
|
{
|
||||||
|
inode: 3,
|
||||||
|
name: "world.txt",
|
||||||
|
type: "File",
|
||||||
|
content: "Goodbye!",
|
||||||
|
children: [],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
inode: 4,
|
||||||
|
name: "bye",
|
||||||
|
type: "Dir",
|
||||||
|
children: [],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
};
|
||||||
|
|
||||||
|
const fs = buildTestFS(testTree);
|
||||||
|
|
||||||
|
describe("FileSystem.init", () => {
|
||||||
|
const map: Map<number, TreeNode> = new Map();
|
||||||
|
const time = Date.now();
|
||||||
|
|
||||||
|
const root: DirectoryNode = {
|
||||||
|
type: TreeNodeType.Directory,
|
||||||
|
inode: 0,
|
||||||
|
name: "root",
|
||||||
|
parent: 0,
|
||||||
|
mode: 0o555,
|
||||||
|
owner: 0,
|
||||||
|
group: 0,
|
||||||
|
mtime: time,
|
||||||
|
atime: time,
|
||||||
|
ctime: time,
|
||||||
|
children: {},
|
||||||
|
};
|
||||||
|
map.set(0, root);
|
||||||
|
|
||||||
|
const base: ReadonlyMap<number, TreeNode> = map;
|
||||||
|
const overlay: ReadonlyMap<number, TreeNode | null> = new Map();
|
||||||
|
|
||||||
|
it("initializes correctly", () => {
|
||||||
|
const fs = FileSystem.init(base, overlay, 0);
|
||||||
|
expect(fs).toBeInstanceOf(FileSystem);
|
||||||
|
expect(fs.INodeIndex).toBe(0);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("createNode", () => {
|
||||||
|
const content = createImmutableBuffer(new TextEncoder().encode("Lorem ipsum"));
|
||||||
|
|
||||||
|
it("creates a file and updates parent", () => {
|
||||||
|
const newFS = fs.createNode(0, "readme", TreeNodeType.File, 0o644, 0, 0, content);
|
||||||
|
|
||||||
|
expect(newFS).toBeInstanceOf(FileSystem);
|
||||||
|
const newNode = newFS.getNode(5) as FileNode;
|
||||||
|
|
||||||
|
expect(newNode.type).toBe(TreeNodeType.File);
|
||||||
|
expect(newNode.name).toBe("readme");
|
||||||
|
expect(new TextDecoder().decode(newNode.content.toArray())).toBe("Lorem ipsum");
|
||||||
|
expect(newNode.parent).toBe(0);
|
||||||
|
expect(newNode.mode).toBe(0o644);
|
||||||
|
|
||||||
|
const root = newFS.getNode(0) as DirectoryNode;
|
||||||
|
|
||||||
|
expect(root.children["readme"]).toBe(5);
|
||||||
|
|
||||||
|
expect(newFS.INodeIndex).toBe(5);
|
||||||
|
expect(newFS.Overlay.size).toBe(2);
|
||||||
|
|
||||||
|
expect(fs.getNode(5)).toBeUndefined();
|
||||||
|
|
||||||
|
const oldRoot = fs.getNode(0) as DirectoryNode;
|
||||||
|
expect(oldRoot.children).toEqual({ "hello.txt": 1, sub: 2 });
|
||||||
|
});
|
||||||
|
|
||||||
|
it("fails to create a file as it alread exists", () => {
|
||||||
|
const newFS = fs.createNode(0, "readme", TreeNodeType.File, 0o644, 0, 0, content);
|
||||||
|
|
||||||
|
expectAlreadyExistsError(() => newFS.createNode(0, "readme", TreeNodeType.File, 0o644, 0, 0, content));
|
||||||
|
});
|
||||||
|
|
||||||
|
it("fails to create a file because the parent is not a directory", () => {
|
||||||
|
const newFS = fs.createNode(0, "readme", TreeNodeType.File, 0o644, 0, 0, content);
|
||||||
|
|
||||||
|
expectNotADirectoryError(() => newFS.createNode(1, "test", TreeNodeType.File, 0o644, 0, 0, content));
|
||||||
|
});
|
||||||
|
|
||||||
|
it("fails to create a file because the specified parent doesnt exist", () => {
|
||||||
|
expectNodeNotFoundError(() => fs.createNode(999, "readme", TreeNodeType.File, 0o644, 0, 0, content));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("permission method group", () => {
|
||||||
|
const content = createImmutableBuffer(new TextEncoder().encode("Lorem ipsum"));
|
||||||
|
|
||||||
|
it("returns true for user read permissions", () => {
|
||||||
|
const user: UserInfo = {
|
||||||
|
username: "root",
|
||||||
|
uid: 1000,
|
||||||
|
gid: 1000,
|
||||||
|
group: [1000],
|
||||||
|
home: 0,
|
||||||
|
};
|
||||||
|
const newFS = fs.createNode(0, "readme", TreeNodeType.File, 0o400, 1000, 1000, content);
|
||||||
|
const perms = newFS.canRead(4, user);
|
||||||
|
|
||||||
|
expect(perms).toBe(true);
|
||||||
|
});
|
||||||
|
it("returns false for user read permissions", () => {
|
||||||
|
const user: UserInfo = {
|
||||||
|
username: "root",
|
||||||
|
uid: 1000,
|
||||||
|
gid: 1000,
|
||||||
|
group: [1000],
|
||||||
|
home: 0,
|
||||||
|
};
|
||||||
|
const newFS = fs.createNode(0, "readme", TreeNodeType.File, 0o200, 1000, 1000, content);
|
||||||
|
const perms = newFS.canRead(5, user);
|
||||||
|
|
||||||
|
expect(perms).toBe(false);
|
||||||
|
});
|
||||||
|
it("returns true for group read permissions", () => {
|
||||||
|
const user: UserInfo = {
|
||||||
|
username: "root",
|
||||||
|
uid: 999,
|
||||||
|
gid: 2000,
|
||||||
|
group: [1000],
|
||||||
|
home: 0,
|
||||||
|
};
|
||||||
|
const newFS = fs.createNode(0, "readme", TreeNodeType.File, 0o040, 1000, 2000, content);
|
||||||
|
const perms = newFS.canRead(5, user);
|
||||||
|
|
||||||
|
expect(perms).toBe(true);
|
||||||
|
});
|
||||||
|
it("returns false for group read permissions", () => {
|
||||||
|
const user: UserInfo = {
|
||||||
|
username: "root",
|
||||||
|
uid: 999,
|
||||||
|
gid: 2000,
|
||||||
|
group: [1000],
|
||||||
|
home: 0,
|
||||||
|
};
|
||||||
|
const newFS = fs.createNode(0, "readme", TreeNodeType.File, 0o020, 1000, 2000, content);
|
||||||
|
const perms = newFS.canRead(5, user);
|
||||||
|
|
||||||
|
expect(perms).toBe(false);
|
||||||
|
});
|
||||||
|
it("returns true for other read permissions", () => {
|
||||||
|
const user: UserInfo = {
|
||||||
|
username: "root",
|
||||||
|
uid: 999,
|
||||||
|
gid: 3000,
|
||||||
|
group: [1000],
|
||||||
|
home: 0,
|
||||||
|
};
|
||||||
|
const newFS = fs.createNode(0, "readme", TreeNodeType.File, 0o004, 1000, 2000, content);
|
||||||
|
const perms = newFS.canRead(5, user);
|
||||||
|
|
||||||
|
expect(perms).toBe(true);
|
||||||
|
});
|
||||||
|
it("returns false for other read permissions", () => {
|
||||||
|
const user: UserInfo = {
|
||||||
|
username: "root",
|
||||||
|
uid: 999,
|
||||||
|
gid: 3000,
|
||||||
|
group: [1000],
|
||||||
|
home: 0,
|
||||||
|
};
|
||||||
|
const newFS = fs.createNode(0, "readme", TreeNodeType.File, 0o002, 1000, 2000, content);
|
||||||
|
const perms = newFS.canRead(5, user);
|
||||||
|
|
||||||
|
expect(perms).toBe(false);
|
||||||
|
});
|
||||||
|
it("returns true for user execute permissions", () => {
|
||||||
|
const user: UserInfo = {
|
||||||
|
username: "root",
|
||||||
|
uid: 1000,
|
||||||
|
gid: 2000,
|
||||||
|
group: [1000],
|
||||||
|
home: 0,
|
||||||
|
};
|
||||||
|
const newFS = fs.createNode(0, "readme", TreeNodeType.File, 0o100, 1000, 2000, content);
|
||||||
|
const perms = newFS.canExecute(5, user);
|
||||||
|
|
||||||
|
expect(perms).toBe(true);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("deleteNode", () => {
|
||||||
|
const content = createImmutableBuffer(new TextEncoder().encode("Lorem ipsum"));
|
||||||
|
|
||||||
|
const newFS = fs.createNode(0, "readme", TreeNodeType.File, 0o644, 0, 0, content);
|
||||||
|
const newNode = newFS.getNode(1) as FileNode;
|
||||||
|
|
||||||
|
it("fails to delete the root node", () => {
|
||||||
|
expectIllegalMutationError(() => fs.deleteNode(0));
|
||||||
|
});
|
||||||
|
|
||||||
|
it("fails to delete a non existant node", () => {
|
||||||
|
expectNodeNotFoundError(() => fs.deleteNode(999));
|
||||||
|
});
|
||||||
|
|
||||||
|
it("successfully deletes a single child node", () => {
|
||||||
|
const newnewFS = newFS.deleteNode(1);
|
||||||
|
const overlay = newnewFS.Overlay;
|
||||||
|
const parent = newnewFS.getNode(0) as DirectoryNode;
|
||||||
|
|
||||||
|
expect(overlay.get(1)).toBe(null);
|
||||||
|
expect(parent.children).not.toHaveProperty(newNode.name);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("successfully deletes a directory recursively", () => {
|
||||||
|
const newFS = fs.deleteNode(2);
|
||||||
|
const parent = newFS.getNode(0) as DirectoryNode;
|
||||||
|
|
||||||
|
expect(newFS.getNode(2)).toBe(undefined);
|
||||||
|
expect(newFS.getNode(3)).toBe(undefined);
|
||||||
|
expect(newFS.Overlay.size).toBeGreaterThanOrEqual(3);
|
||||||
|
expect(newFS.Overlay.get(2)).toBe(null);
|
||||||
|
expect(newFS.Overlay.get(3)).toBe(null);
|
||||||
|
expect(parent.children).not.toHaveProperty("sub");
|
||||||
|
expect(fs.getNode(2)).not.toBe(undefined);
|
||||||
|
expect(fs.Overlay.size).toBe(0);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("modifyNode", () => {
|
||||||
|
it("modifies content", () => {
|
||||||
|
const changes = {
|
||||||
|
content: createImmutableBuffer(new TextEncoder().encode("Hello, World!")),
|
||||||
|
};
|
||||||
|
|
||||||
|
const before = Date.now();
|
||||||
|
|
||||||
|
const newFS = fs.modifyNode(1, changes);
|
||||||
|
const node = newFS.getNode(1) as FileNode;
|
||||||
|
|
||||||
|
const after = Date.now();
|
||||||
|
|
||||||
|
expect(new TextDecoder().decode(node.content.toArray())).toBe("Hello, World!");
|
||||||
|
expect(newFS.Overlay.size).toBe(1);
|
||||||
|
|
||||||
|
expect(node.ctime).toBeGreaterThanOrEqual(before);
|
||||||
|
expect(node.ctime).toBeLessThanOrEqual(after);
|
||||||
|
expect(node.mtime).toBeGreaterThanOrEqual(before);
|
||||||
|
expect(node.mtime).toBeLessThanOrEqual(after);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("modifies mode", () => {
|
||||||
|
const changes = {
|
||||||
|
mode: 0o222,
|
||||||
|
};
|
||||||
|
|
||||||
|
const before = Date.now();
|
||||||
|
|
||||||
|
const newFS = fs.modifyNode(1, changes);
|
||||||
|
const node = newFS.getNode(1) as FileNode;
|
||||||
|
const oldNode = fs.getNode(1) as FileNode;
|
||||||
|
|
||||||
|
const after = Date.now();
|
||||||
|
|
||||||
|
expect(node.mode).toEqual(0o222);
|
||||||
|
expect(newFS.Overlay.size).toBe(1);
|
||||||
|
|
||||||
|
expect(node.ctime).toBeGreaterThanOrEqual(before);
|
||||||
|
expect(node.ctime).toBeLessThanOrEqual(after);
|
||||||
|
expect(node.mtime).toEqual(oldNode.mtime);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("modifies owner and group", () => {
|
||||||
|
const changes = {
|
||||||
|
owner: 1,
|
||||||
|
group: 2,
|
||||||
|
};
|
||||||
|
|
||||||
|
const before = Date.now();
|
||||||
|
|
||||||
|
const newFS = fs.modifyNode(1, changes);
|
||||||
|
const node = newFS.getNode(1) as FileNode;
|
||||||
|
const oldNode = fs.getNode(1) as FileNode;
|
||||||
|
|
||||||
|
const after = Date.now();
|
||||||
|
|
||||||
|
expect(node.owner).toBe(1);
|
||||||
|
expect(node.group).toBe(2);
|
||||||
|
expect(newFS.Overlay.size).toBe(1);
|
||||||
|
|
||||||
|
expect(node.ctime).toBeGreaterThanOrEqual(before);
|
||||||
|
expect(node.ctime).toBeLessThanOrEqual(after);
|
||||||
|
expect(node.mtime).toEqual(oldNode.mtime);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("calls modify node with empty changes", () => {
|
||||||
|
const changes = {};
|
||||||
|
|
||||||
|
const before = Date.now();
|
||||||
|
|
||||||
|
const newFS = fs.modifyNode(1, changes);
|
||||||
|
const node = newFS.getNode(1) as FileNode;
|
||||||
|
const oldNode = fs.getNode(1) as FileNode;
|
||||||
|
|
||||||
|
const after = Date.now();
|
||||||
|
|
||||||
|
expect(node.content).toEqual(oldNode.content);
|
||||||
|
expect(node.mode).toEqual(oldNode.mode);
|
||||||
|
expect(node.owner).toEqual(oldNode.owner);
|
||||||
|
expect(node.group).toEqual(oldNode.group);
|
||||||
|
expect(node.content).toEqual(oldNode.content);
|
||||||
|
|
||||||
|
expect(node.ctime).toBeGreaterThanOrEqual(before);
|
||||||
|
expect(node.ctime).toBeLessThanOrEqual(after);
|
||||||
|
expect(node.mtime).toEqual(oldNode.mtime);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("fails", () => {
|
||||||
|
const changes = {
|
||||||
|
owner: 1,
|
||||||
|
group: 2,
|
||||||
|
};
|
||||||
|
|
||||||
|
expectNodeNotFoundError(() => fs.modifyNode(999, changes));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("moveNode", () => {
|
||||||
|
it("throws because of invalid destination node", () => {
|
||||||
|
expectNodeNotFoundError(() => fs.moveNode(1, 999));
|
||||||
|
});
|
||||||
|
it("throws because of invalid source node", () => {
|
||||||
|
expectNodeNotFoundError(() => fs.moveNode(999, 2));
|
||||||
|
});
|
||||||
|
it("throws because of invalid destination type", () => {
|
||||||
|
expectNotADirectoryError(() => fs.moveNode(3, 1));
|
||||||
|
});
|
||||||
|
it("throws because of an already exising child under destination with the same name", () => {
|
||||||
|
expectAlreadyExistsError(() => fs.moveNode(1, 2, "world.txt"));
|
||||||
|
});
|
||||||
|
it("throws because it tried to move a node to a descendant of itself", () => {
|
||||||
|
expectDirectoryLoopError(() => fs.moveNode(2, 4));
|
||||||
|
});
|
||||||
|
it("throws because it tried to move root", () => {
|
||||||
|
expectIllegalMutationError(() => fs.moveNode(0, 1));
|
||||||
|
});
|
||||||
|
|
||||||
|
it("successfully moves a node to another directory", () => {
|
||||||
|
const newFS = fs.moveNode(1, 2);
|
||||||
|
const nodeSour = newFS.getNode(1) as FileNode;
|
||||||
|
const nodeDest = newFS.getNode(2) as DirectoryNode;
|
||||||
|
|
||||||
|
expect(nodeSour.parent).toBe(2);
|
||||||
|
expect(nodeDest.children[nodeSour.name]).not.toBeUndefined();
|
||||||
|
expect((newFS.getNode(0) as DirectoryNode).children[nodeSour.name]).toBeUndefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("successfully renames a node", () => {
|
||||||
|
const newFS = fs.moveNode(3, 2, "foo.foo");
|
||||||
|
const nodeSour = newFS.getNode(3) as FileNode;
|
||||||
|
const nodeDest = newFS.getNode(2) as DirectoryNode;
|
||||||
|
|
||||||
|
expect(nodeSour.parent).toBe(2);
|
||||||
|
expect(nodeSour.name).toBe("foo.foo");
|
||||||
|
|
||||||
|
expect((fs.getNode(3) as FileNode).parent).toEqual(nodeSour.parent);
|
||||||
|
expect(nodeDest.children["world.txt"]).toBeUndefined();
|
||||||
|
expect(nodeDest.children["foo.foo"]).not.toBeUndefined();
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
import { addHistoryEntry, getHistoryEntry } from "@core/history_state";
|
||||||
|
import { expect, test } from "vitest";
|
||||||
|
|
||||||
|
const historyState: readonly string[] = ['echo "Hello World!"'];
|
||||||
|
|
||||||
|
test("addHistoryEntry - successfully adds an entry to the array", () => {
|
||||||
|
const state = addHistoryEntry(historyState, "ls -l");
|
||||||
|
|
||||||
|
expect(state).toEqual(['echo "Hello World!"', "ls -l"]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("getHistoryEntry - successfully adds returns an entry ", () => {
|
||||||
|
const state = getHistoryEntry(historyState, 0);
|
||||||
|
|
||||||
|
expect(state).toBe('echo "Hello World!"');
|
||||||
|
});
|
||||||
|
|
||||||
|
test("getHistoryEntry - throws an error because of a nonexistant history entry ", () => {
|
||||||
|
expect(() => getHistoryEntry(historyState, 999)).toThrow();
|
||||||
|
});
|
||||||
@@ -0,0 +1,73 @@
|
|||||||
|
import { resolveInode, resolvePath } from "@core/util/path_resolver";
|
||||||
|
import { expectNodeNotFoundError, expectNotADirectoryError } from "@tests/helpers/error_matchers";
|
||||||
|
import { Fixture7Depth } from "@tests/helpers/state_fixture";
|
||||||
|
import { describe, expect, test } from "vitest";
|
||||||
|
|
||||||
|
const state = Fixture7Depth.State;
|
||||||
|
const fs = state.filesystem;
|
||||||
|
|
||||||
|
describe("resolvePath", () => {
|
||||||
|
test("returns an inode from an absolute path", () => {
|
||||||
|
const path = "/d1/d2/d3/d4/";
|
||||||
|
const result = resolvePath(fs, 1, path);
|
||||||
|
|
||||||
|
expect(result).toBeDefined();
|
||||||
|
expect(fs.getNode(result as number)!.name).toBe("d4");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("returns an inode from a relative path", () => {
|
||||||
|
const path = "./d2/d3/d4/";
|
||||||
|
const result = resolvePath(fs, 1, path);
|
||||||
|
|
||||||
|
expect(result).toBeDefined();
|
||||||
|
expect(fs.getNode(result as number)!.name).toBe("d4");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("returns an inode from a relative parent path", () => {
|
||||||
|
const path = "../d1/d2/d3/d4/";
|
||||||
|
const result = resolvePath(fs, 1, path);
|
||||||
|
|
||||||
|
expect(result).toBeDefined();
|
||||||
|
expect(fs.getNode(result as number)!.name).toBe("d4");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("returns inode 0", () => {
|
||||||
|
const path = "/";
|
||||||
|
const result = resolvePath(fs, 1, path);
|
||||||
|
|
||||||
|
expect(result).toBeDefined();
|
||||||
|
expect(result).toBe(0);
|
||||||
|
expect(fs.getNode(result as number)!.name).toBe("root");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("returns inode 1 (cwd)", () => {
|
||||||
|
const path = "";
|
||||||
|
const result = resolvePath(fs, 1, path);
|
||||||
|
|
||||||
|
expect(result).toBeDefined();
|
||||||
|
expect(result).toBe(1);
|
||||||
|
expect(fs.getNode(result as number)!.name).toBe("d1");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("throws because of a nonexistent path", () => {
|
||||||
|
expectNodeNotFoundError(() => resolvePath(fs, 1, "/d3"));
|
||||||
|
});
|
||||||
|
|
||||||
|
test("throws because of file path component", () => {
|
||||||
|
expectNotADirectoryError(() => resolvePath(fs, 6, "./deep_file.txt/d8"));
|
||||||
|
});
|
||||||
|
|
||||||
|
test("throws because of a nonexistent cwd inode (technicaly should never happen)", () => {
|
||||||
|
expectNodeNotFoundError(() => resolvePath(fs, 69, "/d3"));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("resolveInode", () => {
|
||||||
|
test("returns an the path from the d4 node", () => {
|
||||||
|
expect(resolveInode(state, 4, true)).toBe("/d1/d2/d3/d4");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("returns an empty string from a non existent inode", () => {
|
||||||
|
expect(resolveInode(state, 999)).toBe("");
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
import type { Result, ResultError, ResultSuccess, ShellError } from "@core/result";
|
import type { Result, ResultError, ResultSuccess } from "@core/result";
|
||||||
|
import type { ShellError } from "@core/util/errors";
|
||||||
import { createImmutableBuffer, type ImmutableBuffer } from "@core/util/immutable_buffer";
|
import { createImmutableBuffer, type ImmutableBuffer } from "@core/util/immutable_buffer";
|
||||||
import { expect, test } from "vitest";
|
import { expect, test } from "vitest";
|
||||||
|
|
||||||
@@ -36,13 +37,13 @@ test("error result passed through getstdout should throw error with 'Goodbye, Wo
|
|||||||
|
|
||||||
const result: ResultError<TestError> = {
|
const result: ResultError<TestError> = {
|
||||||
type: "error",
|
type: "error",
|
||||||
exitCode: 1,
|
|
||||||
stdout: createImmutableBuffer(new Uint8Array()),
|
stdout: createImmutableBuffer(new Uint8Array()),
|
||||||
stderr: createImmutableBuffer(new Uint8Array()),
|
stderr: createImmutableBuffer(new Uint8Array()),
|
||||||
error: {
|
error: {
|
||||||
errorType: "TestError",
|
errorType: "TestError",
|
||||||
message,
|
message,
|
||||||
timestamp: 0,
|
timestamp: 0,
|
||||||
|
exitCode: 1,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
expect(() => getStdout(result)).toThrow(message);
|
expect(() => getStdout(result)).toThrow(message);
|
||||||
|
|||||||
@@ -0,0 +1,57 @@
|
|||||||
|
import { createInitialSessionState, getCurrentSession, login, logout } from "@core/session_manager";
|
||||||
|
import { Fixture } from "@tests/helpers/state_fixture";
|
||||||
|
import { describe, expect, test } from "vitest";
|
||||||
|
|
||||||
|
const state = Fixture.State;
|
||||||
|
|
||||||
|
const passMap = new Map();
|
||||||
|
passMap.set(0, "123");
|
||||||
|
passMap.set(1000, "321");
|
||||||
|
|
||||||
|
test("createInitialSessionState - successfully creates a new session state", () => {
|
||||||
|
const state = createInitialSessionState(Fixture.user);
|
||||||
|
|
||||||
|
expect(state.cwd).toEqual(Fixture.user.home);
|
||||||
|
expect(state.pwd).toEqual(Fixture.user.home);
|
||||||
|
expect(state.exitCode).toEqual(0);
|
||||||
|
expect(state.user).toBe(Fixture.user);
|
||||||
|
expect(Object.isFrozen(state)).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("login", () => {
|
||||||
|
test("it successfully starts a new session under the user test", () => {
|
||||||
|
const newState = login(state, "user", "321", passMap);
|
||||||
|
const pop = getCurrentSession(newState);
|
||||||
|
|
||||||
|
expect(pop.user).toEqual(Fixture.user2);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("fails to start a new session. Cause: no password mapped to the uid", () => {
|
||||||
|
//TODO: Fix to expect the correct object;
|
||||||
|
expect(() => login(state, "no pass", "321", passMap)).toThrow();
|
||||||
|
});
|
||||||
|
|
||||||
|
test("fails to start a new session. Cause: Incorrect password", () => {
|
||||||
|
//TODO: Fix to expect the correct object;
|
||||||
|
expect(() => login(state, "no pass", "333", passMap)).toThrow();
|
||||||
|
});
|
||||||
|
|
||||||
|
test("fails to start a new session. Cause: Specified user does not exist", () => {
|
||||||
|
//TODO: Fix to expect the correct object;
|
||||||
|
expect(() => login(state, "no user", "321", passMap)).toThrow();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("logout", () => {
|
||||||
|
test("returns the state with the current user poped from the stack", () => {
|
||||||
|
const newState = login(state, "user", "321", passMap);
|
||||||
|
const newPop = getCurrentSession(newState);
|
||||||
|
|
||||||
|
expect(newPop.user).toEqual(Fixture.user2);
|
||||||
|
|
||||||
|
const newNewState = logout(state);
|
||||||
|
const pop = getCurrentSession(newNewState);
|
||||||
|
|
||||||
|
expect(pop.user).toEqual(Fixture.user);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,64 @@
|
|||||||
|
import { getCurrentSession } from "@core/session_manager";
|
||||||
|
import { withCwd, withEnv, withExitCode, withHistory } from "@core/shell_state";
|
||||||
|
import { Fixture } from "@tests/helpers/state_fixture";
|
||||||
|
import { expect, test } from "vitest";
|
||||||
|
|
||||||
|
const state = Fixture.State;
|
||||||
|
|
||||||
|
test("createInitialShellState - initializes successfully", () => {
|
||||||
|
expect(state.sessionStack.at(0)).toBeDefined();
|
||||||
|
|
||||||
|
expect(state.users.get(Fixture.user.uid)).toEqual(Fixture.user);
|
||||||
|
expect(Object.isFrozen(state)).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("fails - tries to mutate a frozen immutable object", () => {
|
||||||
|
expect(() => {
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
|
(state as any).sessionStack = [];
|
||||||
|
}).toThrow();
|
||||||
|
});
|
||||||
|
|
||||||
|
test("getCurrentSession - successfully returns the current (top most) session from the stack", () => {
|
||||||
|
const session = getCurrentSession(state);
|
||||||
|
|
||||||
|
expect(session.cwd).toBe(0);
|
||||||
|
expect(session.pwd).toBe(0);
|
||||||
|
expect(session.exitCode).toBe(0);
|
||||||
|
expect(session.history).toEqual([]);
|
||||||
|
expect(session.envVars.size).toBe(0);
|
||||||
|
expect(session.user).toBe(Fixture.user);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("withCwd", () => {
|
||||||
|
const nstate = withCwd(state, 1);
|
||||||
|
const result = getCurrentSession(nstate);
|
||||||
|
expect(result.cwd).toBe(1);
|
||||||
|
expect(result.pwd).toBe(0);
|
||||||
|
expect(nstate.filesystem).toBe(state.filesystem);
|
||||||
|
expect(getCurrentSession(state).cwd).toBe(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("withEnv", () => {
|
||||||
|
const nstate = withEnv(state, "foo", "Hello World!");
|
||||||
|
const result = getCurrentSession(nstate);
|
||||||
|
expect(result.envVars.get("foo")).toBe("Hello World!");
|
||||||
|
expect(nstate.filesystem).toBe(state.filesystem);
|
||||||
|
expect(getCurrentSession(state).envVars.get("foo")).toBeUndefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
test("withHistory", () => {
|
||||||
|
const nstate = withHistory(state, 'echo "Hi!"');
|
||||||
|
const result = getCurrentSession(nstate);
|
||||||
|
expect(result.history).toContain('echo "Hi!"');
|
||||||
|
expect(nstate.filesystem).toBe(state.filesystem);
|
||||||
|
expect(getCurrentSession(state).history).not.toContain('echo "Hi!"');
|
||||||
|
});
|
||||||
|
|
||||||
|
test("withExitCode", () => {
|
||||||
|
const nstate = withExitCode(state, 10);
|
||||||
|
const result = getCurrentSession(nstate);
|
||||||
|
expect(result.exitCode).toEqual(10);
|
||||||
|
expect(nstate.filesystem).toBe(state.filesystem);
|
||||||
|
expect(getCurrentSession(state).exitCode).toEqual(0);
|
||||||
|
});
|
||||||
@@ -0,0 +1,86 @@
|
|||||||
|
import { parseCommand } from "@core/util/parser";
|
||||||
|
import { describe, expect, test } from "vitest";
|
||||||
|
|
||||||
|
describe("parseCommand", () => {
|
||||||
|
test("returns an empty ParsedCommand object", () => {
|
||||||
|
expect(parseCommand("")).toEqual({ command: "", flags: [], args: [] });
|
||||||
|
});
|
||||||
|
test("returns an empty ParsedCommand object", () => {
|
||||||
|
expect(parseCommand("")).toEqual({ command: "", flags: [], args: [] });
|
||||||
|
});
|
||||||
|
test("returns an empty ParsedCommand object", () => {
|
||||||
|
expect(parseCommand("")).toEqual({ command: "", flags: [], args: [] });
|
||||||
|
});
|
||||||
|
|
||||||
|
test("returns a parsed ls command without any args", () => {
|
||||||
|
expect(parseCommand("ls")).toEqual({ command: "ls", flags: [], args: [] });
|
||||||
|
});
|
||||||
|
test("returns a parsed ls command with arg /tmp", () => {
|
||||||
|
expect(parseCommand("ls /tmp")).toEqual({ command: "ls", flags: [], args: ["/tmp"] });
|
||||||
|
});
|
||||||
|
|
||||||
|
test("returns a parsed ls command with flags l and no args", () => {
|
||||||
|
expect(parseCommand("ls -l")).toEqual({ command: "ls", flags: ["l"], args: [] });
|
||||||
|
});
|
||||||
|
test("returns a parsed command with flags l,a and no args", () => {
|
||||||
|
expect(parseCommand("ls -la")).toEqual({ command: "ls", flags: ["l", "a"], args: [] });
|
||||||
|
});
|
||||||
|
test("returns a parsed command with flags l,a and no args", () => {
|
||||||
|
expect(parseCommand("ls -l -a")).toEqual({ command: "ls", flags: ["l", "a"], args: [] });
|
||||||
|
});
|
||||||
|
test("returns a parsed command with flags l,a and a /tmp arg", () => {
|
||||||
|
expect(parseCommand("ls -la /tmp")).toEqual({ command: "ls", flags: ["l", "a"], args: ["/tmp"] });
|
||||||
|
});
|
||||||
|
|
||||||
|
test("returns a parsed grep command with a valued color flag", () => {
|
||||||
|
expect(parseCommand("grep --color=auto")).toEqual({ command: "grep", flags: ["color=auto"], args: [] });
|
||||||
|
});
|
||||||
|
test("returns a parsed grep command with a valued color and r flags", () => {
|
||||||
|
expect(parseCommand("grep --color=auto -r")).toEqual({
|
||||||
|
command: "grep",
|
||||||
|
flags: ["color=auto", "r"],
|
||||||
|
args: [],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test("returns a parsed grep command with a valued color in short form and r flags", () => {
|
||||||
|
expect(parseCommand("grep -color=auto -r")).toEqual({
|
||||||
|
command: "grep",
|
||||||
|
flags: ["color=auto", "r"],
|
||||||
|
args: [],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test("returns a parsed grep command with a valued w in short form and r flags", () => {
|
||||||
|
expect(parseCommand("grep -w=auto -r")).toEqual({
|
||||||
|
command: "grep",
|
||||||
|
flags: ["w=auto", "r"],
|
||||||
|
args: [],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test("returns a parsed echo command with a --version arg", () => {
|
||||||
|
expect(parseCommand("echo -- --version")).toEqual({ command: "echo", flags: [], args: ["--version"] });
|
||||||
|
});
|
||||||
|
test("returns a parsed echo command with a -f arg", () => {
|
||||||
|
expect(parseCommand("echo -- -f")).toEqual({ command: "echo", flags: [], args: ["-f"] });
|
||||||
|
});
|
||||||
|
test("returns a parsed echo command without any args", () => {
|
||||||
|
expect(parseCommand("echo --")).toEqual({ command: "echo", flags: [], args: [] });
|
||||||
|
});
|
||||||
|
|
||||||
|
test("returns a parsed ls command with flags l,a and a --tmp arg", () => {
|
||||||
|
expect(parseCommand("ls -la -- --tmp")).toEqual({ command: "ls", flags: ["l", "a"], args: ["--tmp"] });
|
||||||
|
});
|
||||||
|
test("returns a parsed rm command with flags r,f,no-preserve-root and a / arg", () => {
|
||||||
|
expect(parseCommand("rm -rf --no-preserve-root /")).toEqual({
|
||||||
|
command: "rm",
|
||||||
|
flags: ["r", "f", "no-preserve-root"],
|
||||||
|
args: ["/"],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test("throws an UnclosedQuote error", () => {
|
||||||
|
expect(() => parseCommand('echo "hello')).toThrow(expect.objectContaining({ type: "UnclosedQuote" }));
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,63 @@
|
|||||||
|
import { dnTraverseTree, upTraverseTree } from "@core/util/tree_traversal";
|
||||||
|
import { buildTestFS, type FixtureNode } from "@tests/helpers/filesystems_fixture";
|
||||||
|
import { depth7Tree } from "@tests/helpers/tree_fixture";
|
||||||
|
import { describe, expect, test } from "vitest";
|
||||||
|
|
||||||
|
const testTree: FixtureNode = {
|
||||||
|
inode: 0,
|
||||||
|
name: "root",
|
||||||
|
type: "Dir",
|
||||||
|
children: [
|
||||||
|
{
|
||||||
|
inode: 1,
|
||||||
|
name: "hello.txt",
|
||||||
|
type: "File",
|
||||||
|
content: "Hello, world!",
|
||||||
|
children: [],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
inode: 2,
|
||||||
|
name: "sub",
|
||||||
|
type: "Dir",
|
||||||
|
children: [
|
||||||
|
{
|
||||||
|
inode: 3,
|
||||||
|
name: "world.txt",
|
||||||
|
type: "File",
|
||||||
|
content: "Goodbye!",
|
||||||
|
children: [],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
};
|
||||||
|
|
||||||
|
const fs = buildTestFS(testTree);
|
||||||
|
const fsTwo = buildTestFS(depth7Tree);
|
||||||
|
|
||||||
|
describe("dnTraverseTree", () => {
|
||||||
|
test("successfully traverses down the tree in depth-first pre-order", () => {
|
||||||
|
const result = dnTraverseTree(fs, 0);
|
||||||
|
// This got changed from [0, 2, 3, 1] to [1, 2, 3]
|
||||||
|
// A bug in dnTraverseTree resulted in the tree traversal not respecting order of nodes, and including root.
|
||||||
|
// Including root in results caused further problems so it was decided that results wont include the passed root node;
|
||||||
|
expect(result, `Checking the result ${result} to be [1, 2, 3]`).toEqual([1, 2, 3]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("returns empty array for a non existent root inode", () => {
|
||||||
|
expect(dnTraverseTree(fs, 999)).toEqual([]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("upTraverseTree", () => {
|
||||||
|
test("successfully traverses up the tree", () => {
|
||||||
|
const result = upTraverseTree(fsTwo, 7);
|
||||||
|
expect(result, `Checking the result ${result} to be [7, 6, 5, 4, 3, 2, 1, 0]`).toEqual([
|
||||||
|
7, 6, 5, 4, 3, 2, 1, 0,
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("returns empty array for a non existent inode", () => {
|
||||||
|
expect(dnTraverseTree(fs, 999)).toEqual([]);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,73 @@
|
|||||||
|
import type {
|
||||||
|
AlreadyExistsError,
|
||||||
|
DirectoryLoopError,
|
||||||
|
IllegalMutationError,
|
||||||
|
NodeNotFoundError,
|
||||||
|
NotADirectoryError,
|
||||||
|
} from "@core/filesystem/errors";
|
||||||
|
import type { TreeNodeType } from "@core/filesystem/treenode";
|
||||||
|
import { expect } from "vitest";
|
||||||
|
|
||||||
|
export function expectThrows(fn: () => unknown, expectedShape: Record<string, unknown>) {
|
||||||
|
let thrown: unknown;
|
||||||
|
try {
|
||||||
|
fn();
|
||||||
|
} catch (e) {
|
||||||
|
thrown = e;
|
||||||
|
}
|
||||||
|
|
||||||
|
expect(thrown).toBeDefined();
|
||||||
|
expect(thrown).toMatchObject(expectedShape);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function expectNotADirectoryError(fn: () => unknown, overrides?: Partial<NotADirectoryError>) {
|
||||||
|
expectThrows(fn, {
|
||||||
|
type: "NotADirectory",
|
||||||
|
inode: expect.any(Number) as number,
|
||||||
|
message: expect.any(String) as string,
|
||||||
|
timestamp: expect.any(Number) as number,
|
||||||
|
nodeType: expect.any(Number) as TreeNodeType,
|
||||||
|
...overrides,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export function expectAlreadyExistsError(fn: () => unknown, overrides?: Partial<AlreadyExistsError>) {
|
||||||
|
expectThrows(fn, {
|
||||||
|
type: "AlreadyExists",
|
||||||
|
parentInode: expect.any(Number) as number,
|
||||||
|
name: expect.any(String) as string,
|
||||||
|
message: expect.any(String) as string,
|
||||||
|
timestamp: expect.any(Number) as number,
|
||||||
|
...overrides,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export function expectNodeNotFoundError(fn: () => unknown, overrides?: Partial<NodeNotFoundError>) {
|
||||||
|
expectThrows(fn, {
|
||||||
|
type: "NodeNotFound",
|
||||||
|
inode: expect.any(Number) as number,
|
||||||
|
message: expect.any(String) as string,
|
||||||
|
timestamp: expect.any(Number) as number,
|
||||||
|
...overrides,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export function expectIllegalMutationError(fn: () => unknown, overrides?: Partial<IllegalMutationError>) {
|
||||||
|
expectThrows(fn, {
|
||||||
|
type: "IllegalMutation",
|
||||||
|
message: expect.any(String) as string,
|
||||||
|
timestamp: expect.any(Number) as number,
|
||||||
|
...overrides,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export function expectDirectoryLoopError(fn: () => void, overrides?: Partial<DirectoryLoopError>) {
|
||||||
|
expectThrows(fn, {
|
||||||
|
type: "DirectoryLoop",
|
||||||
|
descendantInode: expect.any(Number) as number,
|
||||||
|
ancestorInode: expect.any(Number) as number,
|
||||||
|
message: expect.any(String) as string,
|
||||||
|
timestamp: expect.any(Number) as number,
|
||||||
|
...overrides,
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -0,0 +1,71 @@
|
|||||||
|
import { FileSystem } from "@core/filesystem/filesystem";
|
||||||
|
import { TreeNodeType, type DirectoryNode, type FileNode, type TreeNode } from "@core/filesystem/treenode";
|
||||||
|
import { createImmutableBuffer } from "@core/util/immutable_buffer";
|
||||||
|
|
||||||
|
export interface FixtureNode {
|
||||||
|
inode: number;
|
||||||
|
name: string;
|
||||||
|
type: "File" | "Dir";
|
||||||
|
children: FixtureNode[];
|
||||||
|
content?: string;
|
||||||
|
mode?: number;
|
||||||
|
owner?: number;
|
||||||
|
group?: number;
|
||||||
|
mtime?: number;
|
||||||
|
ctime?: number;
|
||||||
|
atime?: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function buildTestFS(tree: FixtureNode): FileSystem {
|
||||||
|
const map = new Map<number, TreeNode>();
|
||||||
|
|
||||||
|
function parse(node: FixtureNode, parent: number): void {
|
||||||
|
if (node.type === "Dir") {
|
||||||
|
for (const child of node.children) {
|
||||||
|
parse(child, node.inode);
|
||||||
|
}
|
||||||
|
|
||||||
|
const children: Record<string, number> = {};
|
||||||
|
for (const child of node.children) {
|
||||||
|
children[child.name] = child.inode;
|
||||||
|
}
|
||||||
|
|
||||||
|
const directoryNode: DirectoryNode = {
|
||||||
|
type: TreeNodeType.Directory,
|
||||||
|
inode: node.inode,
|
||||||
|
name: node.name,
|
||||||
|
parent: parent,
|
||||||
|
mode: node.mode ?? 0o777,
|
||||||
|
owner: node.owner ?? 0,
|
||||||
|
group: node.group ?? 0,
|
||||||
|
mtime: node.mtime ?? Date.now(),
|
||||||
|
ctime: node.ctime ?? Date.now(),
|
||||||
|
atime: node.atime ?? Date.now(),
|
||||||
|
children: children,
|
||||||
|
};
|
||||||
|
map.set(node.inode, Object.freeze(directoryNode));
|
||||||
|
} else {
|
||||||
|
const fileNode: FileNode = {
|
||||||
|
type: TreeNodeType.File,
|
||||||
|
inode: node.inode,
|
||||||
|
name: node.name,
|
||||||
|
parent: parent,
|
||||||
|
mode: node.mode ?? 0o777,
|
||||||
|
owner: node.owner ?? 0,
|
||||||
|
group: node.group ?? 0,
|
||||||
|
mtime: node.mtime ?? Date.now(),
|
||||||
|
ctime: node.ctime ?? Date.now(),
|
||||||
|
atime: node.atime ?? Date.now(),
|
||||||
|
content: createImmutableBuffer(new TextEncoder().encode(node.content)),
|
||||||
|
};
|
||||||
|
map.set(node.inode, Object.freeze(fileNode));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
parse(tree, 0);
|
||||||
|
|
||||||
|
const base: ReadonlyMap<number, TreeNode> = map;
|
||||||
|
const overlay: ReadonlyMap<number, TreeNode | null> = new Map();
|
||||||
|
|
||||||
|
return FileSystem.init(base, overlay, base.size - 1);
|
||||||
|
}
|
||||||
@@ -0,0 +1,91 @@
|
|||||||
|
import { FileSystem } from "@core/filesystem/filesystem";
|
||||||
|
import type { TreeNode } from "@core/filesystem/treenode";
|
||||||
|
import type { GroupInfo, UserInfo } from "@core/session_manager";
|
||||||
|
import { createInitialShellState, type ShellState } from "@core/shell_state";
|
||||||
|
import { deserializeFileSystem } from "@shell/serializer";
|
||||||
|
import { buildTestFS } from "./filesystems_fixture";
|
||||||
|
import { depth7Tree } from "./tree_fixture";
|
||||||
|
|
||||||
|
export class Fixture {
|
||||||
|
/*
|
||||||
|
root (0)
|
||||||
|
├── folder1 (1)
|
||||||
|
│ └── subdir1 (2)
|
||||||
|
├── folder2 (3)
|
||||||
|
│ ├── subdir2 (4)
|
||||||
|
│ └── subdir3 (5)
|
||||||
|
│ └── empty-foo (6)
|
||||||
|
└── some-file (7)
|
||||||
|
*/
|
||||||
|
|
||||||
|
static 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"}`;
|
||||||
|
static user: UserInfo = {
|
||||||
|
username: "root",
|
||||||
|
uid: 0,
|
||||||
|
gid: 0, // <- PRIMARY GID
|
||||||
|
group: [0], // <- GROUPS ARRAY
|
||||||
|
home: 0,
|
||||||
|
};
|
||||||
|
static user2: UserInfo = {
|
||||||
|
username: "user",
|
||||||
|
uid: 1000,
|
||||||
|
gid: 1000,
|
||||||
|
group: [1000],
|
||||||
|
home: 1,
|
||||||
|
};
|
||||||
|
|
||||||
|
static nopass: UserInfo = {
|
||||||
|
username: "no pass",
|
||||||
|
uid: 1001,
|
||||||
|
gid: 1000,
|
||||||
|
group: [1000],
|
||||||
|
home: 2,
|
||||||
|
};
|
||||||
|
|
||||||
|
static group: GroupInfo = {
|
||||||
|
groupname: "root",
|
||||||
|
gid: 0,
|
||||||
|
members: [0], // <- UID
|
||||||
|
};
|
||||||
|
static group2: GroupInfo = {
|
||||||
|
groupname: "user",
|
||||||
|
gid: 1000,
|
||||||
|
members: [1000, 1001], // <- UID
|
||||||
|
};
|
||||||
|
|
||||||
|
public static get State(): ShellState {
|
||||||
|
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"}`;
|
||||||
|
const base = deserializeFileSystem(rawJson);
|
||||||
|
|
||||||
|
const overlay: ReadonlyMap<number, TreeNode | null> = new Map();
|
||||||
|
|
||||||
|
const fs = FileSystem.init(base, overlay, base.size - 1);
|
||||||
|
|
||||||
|
const users: Map<number, UserInfo> = new Map();
|
||||||
|
const groups: Map<number, GroupInfo> = new Map();
|
||||||
|
|
||||||
|
users.set(0, Fixture.user);
|
||||||
|
users.set(1000, Fixture.user2);
|
||||||
|
users.set(1001, Fixture.nopass);
|
||||||
|
groups.set(0, Fixture.group);
|
||||||
|
groups.set(1000, Fixture.group2);
|
||||||
|
|
||||||
|
return createInitialShellState(fs, users, groups, Fixture.user);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export class Fixture7Depth extends Fixture {
|
||||||
|
public static get State(): ShellState {
|
||||||
|
const fs = buildTestFS(depth7Tree);
|
||||||
|
|
||||||
|
const users: Map<number, UserInfo> = new Map();
|
||||||
|
const groups: Map<number, GroupInfo> = new Map();
|
||||||
|
|
||||||
|
users.set(0, Fixture7Depth.user);
|
||||||
|
users.set(1000, Fixture7Depth.user2);
|
||||||
|
groups.set(0, Fixture7Depth.group);
|
||||||
|
groups.set(1000, Fixture7Depth.group2);
|
||||||
|
|
||||||
|
return createInitialShellState(fs, users, groups, Fixture7Depth.user);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,58 @@
|
|||||||
|
import type { FixtureNode } from "./filesystems_fixture";
|
||||||
|
|
||||||
|
export const depth7Tree: FixtureNode = {
|
||||||
|
inode: 0,
|
||||||
|
name: "root",
|
||||||
|
type: "Dir",
|
||||||
|
children: [
|
||||||
|
{
|
||||||
|
inode: 1,
|
||||||
|
name: "d1",
|
||||||
|
type: "Dir",
|
||||||
|
children: [
|
||||||
|
{
|
||||||
|
inode: 2,
|
||||||
|
name: "d2",
|
||||||
|
type: "Dir",
|
||||||
|
children: [
|
||||||
|
{
|
||||||
|
inode: 3,
|
||||||
|
name: "d3",
|
||||||
|
type: "Dir",
|
||||||
|
children: [
|
||||||
|
{
|
||||||
|
inode: 4,
|
||||||
|
name: "d4",
|
||||||
|
type: "Dir",
|
||||||
|
children: [
|
||||||
|
{
|
||||||
|
inode: 5,
|
||||||
|
name: "d5",
|
||||||
|
type: "Dir",
|
||||||
|
children: [
|
||||||
|
{
|
||||||
|
inode: 6,
|
||||||
|
name: "d6",
|
||||||
|
type: "Dir",
|
||||||
|
children: [
|
||||||
|
{
|
||||||
|
inode: 7,
|
||||||
|
name: "deep_file.txt",
|
||||||
|
type: "File",
|
||||||
|
content: "Deepest node at depth 7",
|
||||||
|
children: [],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
};
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
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);
|
||||||
|
//expect(true).toEqual(false);
|
||||||
|
});
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
import { getCurrentSession } from "@core/session_manager";
|
||||||
|
import { Shell } from "@shell/shell";
|
||||||
|
import { Fixture } from "@tests/helpers/state_fixture";
|
||||||
|
import { describe, expect, test } from "vitest";
|
||||||
|
|
||||||
|
const state = Fixture.State;
|
||||||
|
|
||||||
|
describe("Shell tests", () => {
|
||||||
|
test("Successfully executes the cd command", async () => {
|
||||||
|
await Shell.init(state);
|
||||||
|
|
||||||
|
let result;
|
||||||
|
|
||||||
|
Shell.Instance?.on("result", payload => {
|
||||||
|
result = payload.result;
|
||||||
|
});
|
||||||
|
|
||||||
|
await Shell.Instance?.execute("cd /folder1");
|
||||||
|
const statee = Shell.Instance?.State;
|
||||||
|
|
||||||
|
expect(result!.type).toEqual("success");
|
||||||
|
expect(getCurrentSession(statee!).history.length).toBe(1);
|
||||||
|
expect(getCurrentSession(statee!).history[0]).toEqual("cd /folder1");
|
||||||
|
expect(getCurrentSession(statee!).cwd).toBe(1);
|
||||||
|
expect(getCurrentSession(statee!).exitCode).toBe(0);
|
||||||
|
});
|
||||||
|
test("Returns a proper command error from cd", () => {});
|
||||||
|
test("Returns an empty success result as per parsing a whitespace character or inputing nothing", () => {});
|
||||||
|
});
|
||||||
@@ -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));
|
||||||
|
});
|
||||||
+3
-2
@@ -5,7 +5,7 @@
|
|||||||
"lib": ["ESNext", "WebWorker"],
|
"lib": ["ESNext", "WebWorker"],
|
||||||
"moduleResolution": "bundler",
|
"moduleResolution": "bundler",
|
||||||
"outDir": "dist",
|
"outDir": "dist",
|
||||||
"rootDir": "src",
|
"rootDir": ".",
|
||||||
"strict": true,
|
"strict": true,
|
||||||
"esModuleInterop": true,
|
"esModuleInterop": true,
|
||||||
"noEmit": true,
|
"noEmit": true,
|
||||||
@@ -22,7 +22,8 @@
|
|||||||
"exactOptionalPropertyTypes": true,
|
"exactOptionalPropertyTypes": true,
|
||||||
"sourceMap": true,
|
"sourceMap": true,
|
||||||
"ignoreDeprecations": "6.0",
|
"ignoreDeprecations": "6.0",
|
||||||
"baseUrl": ".",
|
"baseUrl": "./",
|
||||||
|
"types": ["node"],
|
||||||
"paths": {
|
"paths": {
|
||||||
"@tests/*": ["test/*"],
|
"@tests/*": ["test/*"],
|
||||||
"@shell/*": ["src/shell/*"],
|
"@shell/*": ["src/shell/*"],
|
||||||
|
|||||||
@@ -1,8 +1,6 @@
|
|||||||
import tsconfigPaths from "vite-tsconfig-paths";
|
|
||||||
import { defineConfig } from "vitest/config";
|
import { defineConfig } from "vitest/config";
|
||||||
|
|
||||||
export default defineConfig({
|
export default defineConfig({
|
||||||
plugins: [tsconfigPaths()],
|
|
||||||
resolve: { tsconfigPaths: true },
|
resolve: { tsconfigPaths: true },
|
||||||
test: {
|
test: {
|
||||||
environment: "node",
|
environment: "node",
|
||||||
|
|||||||
Reference in New Issue
Block a user