@kmmz1127/wash 0.0.0-stage → 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +148 -3
- package/dist/command-fs.d.ts +4 -0
- package/dist/command-fs.js +42 -0
- package/dist/fs.d.ts +32 -0
- package/dist/fs.js +8 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +3 -0
- package/dist/just-bash-fs.d.ts +55 -0
- package/dist/just-bash-fs.js +235 -0
- package/dist/just-bash-shell.d.ts +13 -0
- package/dist/just-bash-shell.js +360 -0
- package/dist/line-editor.d.ts +35 -0
- package/dist/line-editor.js +198 -0
- package/dist/mountable-fs.d.ts +7 -0
- package/dist/mountable-fs.js +58 -0
- package/dist/shell.d.ts +74 -0
- package/dist/shell.js +1 -0
- package/dist/terminal-input.d.ts +35 -0
- package/dist/terminal-input.js +98 -0
- package/dist/terminal.d.ts +2 -0
- package/dist/terminal.js +3 -0
- package/docs/shell-io-and-mounts.md +48 -0
- package/package.json +43 -6
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Mizuto, Kaminakano
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,148 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
# Wash
|
|
2
|
+
|
|
3
|
+
**Wash(Web Unix-like Shell)** は、ブラウザ内で動作する軽量なUNIX風シェルを目指すプロジェクト。
|
|
4
|
+
|
|
5
|
+
このリポジトリはVS Codeに依存しないシェル本体を提供する。VS Codeのターミナルとの接続とワークスペースのマウントは [vscode-wash](https://github.com/kmmz532/vscode-wash) が担当する。
|
|
6
|
+
|
|
7
|
+
## 組み込み
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
npm install @kmmz1127/wash
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { createJustBashShell } from "@kmmz1127/wash";
|
|
15
|
+
|
|
16
|
+
const shell = await createJustBashShell({
|
|
17
|
+
aliases: { ll: "ls -l" },
|
|
18
|
+
});
|
|
19
|
+
const { stdout, stderr, exitCode } = await shell.exec("ll /tmp");
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
npm パッケージには ESM と TypeScript の型定義が含まれます。端末の入力処理は `@kmmz1127/wash/terminal` からも読み込めます。
|
|
23
|
+
|
|
24
|
+
- `commands` で追加コマンド、`mounts` でホストのファイルシステムを渡せます。
|
|
25
|
+
- 追加コマンドは、実行コンテキストの `fs` でWashのパス空間のファイルを操作できます。相対パスはカレントディレクトリを基準とし、読み取り専用のマウントへの変更は `EROFS` で拒否されます。
|
|
26
|
+
- 追加コマンドは、実行コンテキストの `exec(line, { stdin, signal })` で同じシェルに1行を実行させられます。`cd` や `export` の結果は呼び出し元へ引き継がれません。
|
|
27
|
+
- `exec(line, signal, terminal)` に中断シグナルと対話端末を渡せます。パイプやリダイレクトの入力は文字列で受け取ります。
|
|
28
|
+
- `exec` の結果の `exited` は、`ls; exit` のように行の中の `exit` でシェルが終了したときに `true` になります。前の行の終了コードは `$?` で参照できます。
|
|
29
|
+
- `complete(text)` は、カーソルより前の入力からコマンド名またはパスの補完候補を返します。コマンドの引数の位置では、登録コマンドの `complete(args, ctx)` に候補を問い合わせます。エラーまたは1秒以内に応答しない場合は、パスの補完に切り替えます。
|
|
30
|
+
- `registerCommand(command)` と `addDefaultAlias(name, expansion)` で、作成済みのシェルにコマンドと既定の別名を追加できます。どちらも返り値の関数で解除します。利用者が `unalias` で解除した既定の別名は、そのシェルでは追加し直しません。
|
|
31
|
+
- `wash/terminal` は、行編集(`LineEditor`)、実行中のプログラムへの行入力(`TerminalLineInput`)、キー入力の分解、文字幅の計算を提供します。シェルの実装を読み込まないため、端末の入力処理だけを使う場合に利用します。
|
|
32
|
+
- `aliases` は新規シェルへ渡す既定値です。シェル内で追加・解除した別名はそのシェルで有効です。
|
|
33
|
+
|
|
34
|
+
[入出力とマウントの仕様](docs/shell-io-and-mounts.md)
|
|
35
|
+
|
|
36
|
+
## 基本方針
|
|
37
|
+
|
|
38
|
+
- Linux全体をWASM上で動かすのではなく、必要なシェル機能を軽量に実装する。
|
|
39
|
+
- VS Codeに依存しないTypeScript製のシェルエンジンを目指す。
|
|
40
|
+
- シェルエンジン、仮想ファイルシステム、コマンド群、入出力を分離する。
|
|
41
|
+
- UNIX哲学に沿い、コマンドはできるだけ独立した単位として扱う。
|
|
42
|
+
- Like-CではWASMコンパイラをコマンドとして利用できるようにする。
|
|
43
|
+
|
|
44
|
+
## シェルエンジン
|
|
45
|
+
|
|
46
|
+
シェルの構文解析・実行と標準コマンドには、TypeScript製のbashエミュレータ [just-bash](https://github.com/vercel-labs/just-bash) を利用する。
|
|
47
|
+
|
|
48
|
+
- 文法はbash互換とし、Wash独自のスクリプト文法は定義しない。
|
|
49
|
+
- just-bashへの依存は `src/just-bash-shell.ts` に限定する。利用側は `src/shell.ts` のインターフェース(`WashShell`、`WashCommand`)のみを参照する。
|
|
50
|
+
- just-bashで要件を満たせないと判断した場合は、上記インターフェースを維持したままシェルエンジンを置き換える。
|
|
51
|
+
|
|
52
|
+
just-bashを利用するうえでの制約は次のとおり。
|
|
53
|
+
|
|
54
|
+
- ブラウザ版のビルドが `node:zlib` を参照するため、拡張機能のビルド時に代替モジュールへ置き換える。gzip関連の機能は利用できない。
|
|
55
|
+
- `exec` の呼び出しごとにシェルの状態が初期化されるため、カレントディレクトリと環境変数はWash側で引き継ぐ。
|
|
56
|
+
|
|
57
|
+
`alias greet='echo hello'` で作った別名は、同じターミナルの次の入力行から使えます。`unalias greet` で解除できます。
|
|
58
|
+
|
|
59
|
+
## ファイルシステム
|
|
60
|
+
|
|
61
|
+
Washは独立した仮想ルートを持つ。VS Codeで開いているプロジェクトをWashのルートとして直接扱わない。
|
|
62
|
+
|
|
63
|
+
~~~text
|
|
64
|
+
/
|
|
65
|
+
├── bin/ # 標準コマンド。ユーザーが編集・追加できる
|
|
66
|
+
├── usr/
|
|
67
|
+
│ └── bin/ # 追加コマンド
|
|
68
|
+
├── etc/ # Washの設定
|
|
69
|
+
├── tmp/ # 一時ファイル
|
|
70
|
+
├── home/
|
|
71
|
+
│ └── user/
|
|
72
|
+
│ └── bin/ # ユーザー独自コマンド
|
|
73
|
+
└── workspace/ # VS Codeで開いているプロジェクトのマウント先
|
|
74
|
+
~~~
|
|
75
|
+
|
|
76
|
+
### 領域の扱い
|
|
77
|
+
|
|
78
|
+
| 領域 | 保存先・実体 | 方針 |
|
|
79
|
+
|---|---|---|
|
|
80
|
+
| /bin | just-bashの組み込みコマンド | 編集しても動作は変わらない |
|
|
81
|
+
| /usr/bin | Wash専用の仮想ストレージ | 利用側やユーザーがコマンドを追加できる |
|
|
82
|
+
| /etc、/home | Wash専用の仮想ストレージ | ワークスペースとは独立して永続化 |
|
|
83
|
+
| /tmp | メモリ上 | セッション終了時に破棄できる |
|
|
84
|
+
| /workspace | VS Code Workspace API経由 | 開いているプロジェクトを明示的にマウントする |
|
|
85
|
+
|
|
86
|
+
**重要:Washの仮想システム領域とVS Codeのワークスペースは必ず分離する。**
|
|
87
|
+
|
|
88
|
+
- Washの仮想領域を操作しても、プロジェクト内に bin/ を作成・変更しない。
|
|
89
|
+
- /workspace 以外からVS Codeのプロジェクトを変更できない。
|
|
90
|
+
- /workspace への書き込みは、通常のプロジェクトファイル編集として扱う。
|
|
91
|
+
- 仮想領域とワークスペース間のコピーは、ユーザーがコマンドで明示的に指示した場合に限る。
|
|
92
|
+
- /workspace のマウントや書き込み権限は、ユーザーが確認・制御できるようにする。
|
|
93
|
+
|
|
94
|
+
## コマンドモデル
|
|
95
|
+
|
|
96
|
+
コマンドは原則として独立した実行単位とし、仮想ファイルシステム上のファイルとして配置する。
|
|
97
|
+
|
|
98
|
+
- /bin:標準コマンド
|
|
99
|
+
- /usr/bin:追加コマンド
|
|
100
|
+
- /home/user/bin:ユーザー独自コマンド
|
|
101
|
+
- PATH:コマンドを検索するディレクトリの一覧
|
|
102
|
+
|
|
103
|
+
標準コマンドはjust-bashの組み込みコマンドとして提供する。/bin にはコマンド名を示すファイルのみが配置され、このファイルを編集しても組み込みコマンドの動作は変わらない。
|
|
104
|
+
|
|
105
|
+
標準コマンドの動作を変更する場合や自作コマンドを追加する場合は、/home/user/bin などPATH上で /bin より前にあるディレクトリにスクリプトを配置する。同名のコマンドはPATHの順序に従い、/bin の組み込みコマンドより優先される。
|
|
106
|
+
|
|
107
|
+
~~~sh
|
|
108
|
+
#!/bin/bash
|
|
109
|
+
|
|
110
|
+
echo "Hello from my command!"
|
|
111
|
+
~~~
|
|
112
|
+
|
|
113
|
+
このファイルをPATH上のディレクトリに配置し、実行権限を付与すると、ファイル名でコマンドとして実行できる。
|
|
114
|
+
|
|
115
|
+
ブラウザ環境にはOSの実行ファイルや通常のプロセス起動機構がないため、コマンドファイルはWash側で解釈・実行する。任意のJavaScriptをファイルから無制限に実行する方式は、セキュリティと実行環境の分離を検討するまで採用しない。
|
|
116
|
+
|
|
117
|
+
### シェルの実行機能
|
|
118
|
+
|
|
119
|
+
パイプ、リダイレクト、コマンド連結、変数展開、制御構文、および ls・cat・grep などの標準コマンドは、just-bashの機能を利用する。
|
|
120
|
+
|
|
121
|
+
Wash側では次の機能を実装する。
|
|
122
|
+
|
|
123
|
+
- 外部のファイルシステムをマウントするためのインターフェース(`WashFileSystem`)
|
|
124
|
+
- /etc、/home、/usr/bin の永続化
|
|
125
|
+
- 利用側が追加するコマンドの登録(`WashCommand`)
|
|
126
|
+
|
|
127
|
+
VS Code Workspace APIへ接続するファイルシステム、Pseudoterminalとの接続、行編集、コマンド履歴はvscode-washで実装する。
|
|
128
|
+
|
|
129
|
+
## アーキテクチャの方向性
|
|
130
|
+
|
|
131
|
+
- Wash本体はVS Code非依存のTypeScriptライブラリとする。
|
|
132
|
+
- 仮想ファイルシステムへのアクセスは抽象化し、ストレージ実装とWorkspaceマウントを分離する。
|
|
133
|
+
- コマンドは共通の実行コンテキストを受け取り、stdin / stdout / stderrを通じて連携する。
|
|
134
|
+
- Like-Cなどの利用側は、必要に応じて専用コマンドを追加できるようにする。
|
|
135
|
+
|
|
136
|
+
## 未決定事項
|
|
137
|
+
|
|
138
|
+
- 仮想ファイルシステムの具体的な実装と永続化方式
|
|
139
|
+
- stdin / stdout / stderrのストリーム表現と非同期処理
|
|
140
|
+
- Workspaceのマウント、読み書き権限、ユーザー確認のUI
|
|
141
|
+
|
|
142
|
+
Wash本体は、just-bashを利用したシェルと外部ファイルシステムのマウントを実装している。vscode-washでは `/etc`、`/home`、`/usr/bin` を拡張ストレージにマウントし、`wash.workspaceMount` でワークスペースへのアクセスを制御できる。永続領域での実行権限の保存と、対話的な標準入出力は未実装。
|
|
143
|
+
|
|
144
|
+
## 開発ドキュメント
|
|
145
|
+
|
|
146
|
+
- [要件定義](docs/requirements.md)
|
|
147
|
+
- [基本設計](docs/design.md)
|
|
148
|
+
- [タスク一覧](TASK.md)
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { WashFsError } from "./fs.js";
|
|
2
|
+
const CODES = new Set(["ENOENT", "EEXIST", "ENOTDIR", "EISDIR", "EACCES", "EROFS", "ENOTEMPTY", "EIO"]);
|
|
3
|
+
/** just-bashのエラーを、メッセージ先頭のコードからWashFsErrorへ変換する。 */
|
|
4
|
+
function toWashError(error) {
|
|
5
|
+
if (error instanceof WashFsError)
|
|
6
|
+
return error;
|
|
7
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
8
|
+
const code = /^([A-Z]+):/.exec(message)?.[1];
|
|
9
|
+
return new WashFsError(code && CODES.has(code) ? code : "EIO", message);
|
|
10
|
+
}
|
|
11
|
+
async function call(run) {
|
|
12
|
+
try {
|
|
13
|
+
return await run();
|
|
14
|
+
}
|
|
15
|
+
catch (error) {
|
|
16
|
+
throw toWashError(error);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
/** 追加コマンドへ渡すファイルシステム。Washのパス空間を扱い、相対パスはカレントディレクトリを基準にする。 */
|
|
20
|
+
export function commandFileSystem(fs, cwd) {
|
|
21
|
+
const resolve = (path) => fs.resolvePath(cwd, path);
|
|
22
|
+
const type = (entry) => entry.isDirectory ? "directory" : "file";
|
|
23
|
+
return {
|
|
24
|
+
stat: (path) => call(async () => {
|
|
25
|
+
const stat = await fs.stat(resolve(path));
|
|
26
|
+
return { type: type(stat), size: stat.size, mtime: stat.mtime, mode: stat.mode, ...(stat.identity ? { identity: stat.identity } : {}) };
|
|
27
|
+
}),
|
|
28
|
+
chmod: (path, mode) => call(() => fs.chmod(resolve(path), mode)),
|
|
29
|
+
readFile: (path) => call(() => fs.readFileBuffer(resolve(path))),
|
|
30
|
+
writeFile: (path, data) => call(() => fs.writeFile(resolve(path), data)),
|
|
31
|
+
readDirectory: (path) => call(async () => {
|
|
32
|
+
const directory = resolve(path);
|
|
33
|
+
if (fs.readdirWithFileTypes) {
|
|
34
|
+
return (await fs.readdirWithFileTypes(directory)).map((entry) => [entry.name, type(entry)]);
|
|
35
|
+
}
|
|
36
|
+
return Promise.all((await fs.readdir(directory)).map(async (name) => [name, type(await fs.stat(fs.resolvePath(directory, name)))]));
|
|
37
|
+
}),
|
|
38
|
+
createDirectory: (path) => call(() => fs.mkdir(resolve(path), { recursive: true })),
|
|
39
|
+
delete: (path, options) => call(() => fs.rm(resolve(path), { recursive: options.recursive })),
|
|
40
|
+
rename: (from, to) => call(() => fs.mv(resolve(from), resolve(to))),
|
|
41
|
+
};
|
|
42
|
+
}
|
package/dist/fs.d.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Washにマウントするファイルシステム。シェルエンジンにもVS Codeにも依存しない。
|
|
3
|
+
* パスはマウント先を基準とした絶対パス("/" がマウントのルート)で渡される。
|
|
4
|
+
*/
|
|
5
|
+
export interface WashFileSystem {
|
|
6
|
+
readonly readOnly?: boolean;
|
|
7
|
+
stat(path: string): Promise<WashFileStat>;
|
|
8
|
+
chmod?(path: string, mode: number): Promise<void>;
|
|
9
|
+
readFile(path: string): Promise<Uint8Array>;
|
|
10
|
+
writeFile(path: string, data: Uint8Array): Promise<void>;
|
|
11
|
+
readDirectory(path: string): Promise<[name: string, type: WashFileType][]>;
|
|
12
|
+
createDirectory(path: string): Promise<void>;
|
|
13
|
+
delete(path: string, options: {
|
|
14
|
+
recursive: boolean;
|
|
15
|
+
}): Promise<void>;
|
|
16
|
+
rename(from: string, to: string): Promise<void>;
|
|
17
|
+
}
|
|
18
|
+
export type WashFileType = "file" | "directory";
|
|
19
|
+
export interface WashFileStat {
|
|
20
|
+
type: WashFileType;
|
|
21
|
+
size: number;
|
|
22
|
+
mtime: Date;
|
|
23
|
+
mode?: number;
|
|
24
|
+
/** Globally unique identity, shared by paths that refer to the same file when known. */
|
|
25
|
+
identity?: string;
|
|
26
|
+
}
|
|
27
|
+
export type WashFsErrorCode = "ENOENT" | "EEXIST" | "ENOTDIR" | "EISDIR" | "EACCES" | "EROFS" | "ENOTEMPTY" | "EIO";
|
|
28
|
+
/** WashFileSystemの実装が投げるエラー。 */
|
|
29
|
+
export declare class WashFsError extends Error {
|
|
30
|
+
readonly code: WashFsErrorCode;
|
|
31
|
+
constructor(code: WashFsErrorCode, message?: string);
|
|
32
|
+
}
|
package/dist/fs.js
ADDED
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { WashFsError, type WashFileStat, type WashFileSystem, type WashFileType, type WashFsErrorCode } from "./fs.js";
|
|
2
|
+
export type { Completion, ExecResult, ShellExecResult, WashCommand, WashCommandContext, WashShell, WashTerminal } from "./shell.js";
|
|
3
|
+
export { createJustBashShell, type JustBashShellOptions } from "./just-bash-shell.js";
|
|
4
|
+
export * from "./terminal.js";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type { IFileSystem } from "just-bash";
|
|
2
|
+
import { type WashFileSystem } from "./fs.js";
|
|
3
|
+
type FsStat = Awaited<ReturnType<IFileSystem["stat"]>>;
|
|
4
|
+
type FileContent = Parameters<IFileSystem["writeFile"]>[1];
|
|
5
|
+
type Encoding = "utf8" | "utf-8" | "ascii" | "binary" | "base64" | "hex" | "latin1";
|
|
6
|
+
/** WashFileSystemをjust-bashのIFileSystemへ変換する。 */
|
|
7
|
+
export declare class JustBashFsAdapter implements IFileSystem {
|
|
8
|
+
private readonly fs;
|
|
9
|
+
constructor(fs: WashFileSystem);
|
|
10
|
+
get preservesModes(): boolean;
|
|
11
|
+
get readOnly(): boolean;
|
|
12
|
+
private call;
|
|
13
|
+
private writable;
|
|
14
|
+
private statOrUndefined;
|
|
15
|
+
readFile(path: string, options?: {
|
|
16
|
+
encoding?: Encoding | null;
|
|
17
|
+
} | Encoding): Promise<string>;
|
|
18
|
+
readFileBuffer(path: string): Promise<Uint8Array>;
|
|
19
|
+
writeFile(path: string, content: FileContent, options?: {
|
|
20
|
+
encoding?: Encoding;
|
|
21
|
+
} | Encoding): Promise<void>;
|
|
22
|
+
appendFile(path: string, content: FileContent, options?: {
|
|
23
|
+
encoding?: Encoding;
|
|
24
|
+
} | Encoding): Promise<void>;
|
|
25
|
+
exists(path: string): Promise<boolean>;
|
|
26
|
+
stat(path: string): Promise<FsStat>;
|
|
27
|
+
lstat(path: string): Promise<FsStat>;
|
|
28
|
+
mkdir(path: string, options?: {
|
|
29
|
+
recursive?: boolean;
|
|
30
|
+
}): Promise<void>;
|
|
31
|
+
readdir(path: string): Promise<string[]>;
|
|
32
|
+
readdirWithFileTypes(path: string): Promise<{
|
|
33
|
+
name: string;
|
|
34
|
+
isFile: boolean;
|
|
35
|
+
isDirectory: boolean;
|
|
36
|
+
isSymbolicLink: boolean;
|
|
37
|
+
}[]>;
|
|
38
|
+
rm(path: string, options?: {
|
|
39
|
+
recursive?: boolean;
|
|
40
|
+
force?: boolean;
|
|
41
|
+
}): Promise<void>;
|
|
42
|
+
cp(src: string, dest: string, options?: {
|
|
43
|
+
recursive?: boolean;
|
|
44
|
+
}): Promise<void>;
|
|
45
|
+
mv(src: string, dest: string): Promise<void>;
|
|
46
|
+
resolvePath(base: string, path: string): string;
|
|
47
|
+
getAllPaths(): string[];
|
|
48
|
+
chmod(path: string, mode: number): Promise<void>;
|
|
49
|
+
symlink(_target: string, linkPath: string): Promise<void>;
|
|
50
|
+
link(_existingPath: string, newPath: string): Promise<void>;
|
|
51
|
+
readlink(path: string): Promise<string>;
|
|
52
|
+
realpath(path: string): Promise<string>;
|
|
53
|
+
utimes(path: string): Promise<void>;
|
|
54
|
+
}
|
|
55
|
+
export {};
|
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
import { WashFsError } from "./fs.js";
|
|
2
|
+
const MESSAGES = {
|
|
3
|
+
ENOENT: "no such file or directory",
|
|
4
|
+
EEXIST: "file already exists",
|
|
5
|
+
ENOTDIR: "not a directory",
|
|
6
|
+
EISDIR: "illegal operation on a directory",
|
|
7
|
+
EACCES: "permission denied",
|
|
8
|
+
EROFS: "read-only file system",
|
|
9
|
+
ENOTEMPTY: "directory not empty",
|
|
10
|
+
ENOSYS: "function not implemented",
|
|
11
|
+
EIO: "i/o error",
|
|
12
|
+
EINVAL: "invalid argument",
|
|
13
|
+
};
|
|
14
|
+
function fsError(code, op, path) {
|
|
15
|
+
return new Error(`${code}: ${MESSAGES[code] ?? code}, ${op} '${path}'`);
|
|
16
|
+
}
|
|
17
|
+
/** WashFileSystemをjust-bashのIFileSystemへ変換する。 */
|
|
18
|
+
export class JustBashFsAdapter {
|
|
19
|
+
fs;
|
|
20
|
+
constructor(fs) {
|
|
21
|
+
this.fs = fs;
|
|
22
|
+
}
|
|
23
|
+
get preservesModes() {
|
|
24
|
+
return !this.fs.readOnly && this.fs.chmod !== undefined;
|
|
25
|
+
}
|
|
26
|
+
get readOnly() {
|
|
27
|
+
return this.fs.readOnly === true;
|
|
28
|
+
}
|
|
29
|
+
async call(op, path, run) {
|
|
30
|
+
try {
|
|
31
|
+
return await run();
|
|
32
|
+
}
|
|
33
|
+
catch (error) {
|
|
34
|
+
throw fsError(error instanceof WashFsError ? error.code : "EIO", op, path);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
writable(op, path) {
|
|
38
|
+
if (this.fs.readOnly)
|
|
39
|
+
throw fsError("EROFS", op, path);
|
|
40
|
+
}
|
|
41
|
+
async statOrUndefined(path) {
|
|
42
|
+
try {
|
|
43
|
+
return await this.fs.stat(path);
|
|
44
|
+
}
|
|
45
|
+
catch (error) {
|
|
46
|
+
if (error instanceof WashFsError && error.code === "ENOENT")
|
|
47
|
+
return undefined;
|
|
48
|
+
throw error;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
async readFile(path, options) {
|
|
52
|
+
const bytes = await this.readFileBuffer(path);
|
|
53
|
+
const encoding = typeof options === "string" ? options : (options?.encoding ?? "utf8");
|
|
54
|
+
return decode(bytes, encoding);
|
|
55
|
+
}
|
|
56
|
+
async readFileBuffer(path) {
|
|
57
|
+
return this.call("open", path, () => this.fs.readFile(path));
|
|
58
|
+
}
|
|
59
|
+
async writeFile(path, content, options) {
|
|
60
|
+
this.writable("open", path);
|
|
61
|
+
const encoding = typeof options === "string" ? options : options?.encoding;
|
|
62
|
+
await this.call("open", path, () => this.fs.writeFile(path, encode(content, encoding)));
|
|
63
|
+
}
|
|
64
|
+
async appendFile(path, content, options) {
|
|
65
|
+
this.writable("open", path);
|
|
66
|
+
const encoding = typeof options === "string" ? options : options?.encoding;
|
|
67
|
+
await this.call("open", path, async () => {
|
|
68
|
+
const current = (await this.statOrUndefined(path)) ? await this.fs.readFile(path) : new Uint8Array();
|
|
69
|
+
const added = encode(content, encoding);
|
|
70
|
+
const merged = new Uint8Array(current.length + added.length);
|
|
71
|
+
merged.set(current);
|
|
72
|
+
merged.set(added, current.length);
|
|
73
|
+
await this.fs.writeFile(path, merged);
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
async exists(path) {
|
|
77
|
+
return (await this.statOrUndefined(path).catch(() => undefined)) !== undefined;
|
|
78
|
+
}
|
|
79
|
+
async stat(path) {
|
|
80
|
+
const stat = await this.call("stat", path, () => this.fs.stat(path));
|
|
81
|
+
return toFsStat(stat);
|
|
82
|
+
}
|
|
83
|
+
lstat(path) {
|
|
84
|
+
return this.stat(path);
|
|
85
|
+
}
|
|
86
|
+
async mkdir(path, options) {
|
|
87
|
+
this.writable("mkdir", path);
|
|
88
|
+
const existing = await this.call("mkdir", path, () => this.statOrUndefined(path));
|
|
89
|
+
if (existing) {
|
|
90
|
+
if (options?.recursive && existing.type === "directory")
|
|
91
|
+
return;
|
|
92
|
+
throw fsError("EEXIST", "mkdir", path);
|
|
93
|
+
}
|
|
94
|
+
if (!options?.recursive && !(await this.exists(parent(path))))
|
|
95
|
+
throw fsError("ENOENT", "mkdir", path);
|
|
96
|
+
await this.call("mkdir", path, () => this.fs.createDirectory(path));
|
|
97
|
+
}
|
|
98
|
+
async readdir(path) {
|
|
99
|
+
const entries = await this.call("scandir", path, () => this.fs.readDirectory(path));
|
|
100
|
+
return entries.map(([name]) => name).sort();
|
|
101
|
+
}
|
|
102
|
+
async readdirWithFileTypes(path) {
|
|
103
|
+
const entries = await this.call("scandir", path, () => this.fs.readDirectory(path));
|
|
104
|
+
return entries
|
|
105
|
+
.sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0))
|
|
106
|
+
.map(([name, type]) => ({ name, isFile: type === "file", isDirectory: type === "directory", isSymbolicLink: false }));
|
|
107
|
+
}
|
|
108
|
+
async rm(path, options) {
|
|
109
|
+
this.writable("rm", path);
|
|
110
|
+
const stat = await this.call("rm", path, () => this.statOrUndefined(path));
|
|
111
|
+
if (!stat) {
|
|
112
|
+
if (options?.force)
|
|
113
|
+
return;
|
|
114
|
+
throw fsError("ENOENT", "rm", path);
|
|
115
|
+
}
|
|
116
|
+
if (stat.type === "directory" && !options?.recursive && (await this.readdir(path)).length > 0) {
|
|
117
|
+
throw fsError("ENOTEMPTY", "rm", path);
|
|
118
|
+
}
|
|
119
|
+
await this.call("rm", path, () => this.fs.delete(path, { recursive: options?.recursive ?? false }));
|
|
120
|
+
}
|
|
121
|
+
async cp(src, dest, options) {
|
|
122
|
+
this.writable("cp", dest);
|
|
123
|
+
const stat = await this.stat(src);
|
|
124
|
+
if (stat.isFile) {
|
|
125
|
+
await this.writeFile(dest, await this.readFileBuffer(src));
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
if (!options?.recursive)
|
|
129
|
+
throw fsError("EISDIR", "cp", src);
|
|
130
|
+
const sourcePath = normalize(src);
|
|
131
|
+
const destinationPath = normalize(dest);
|
|
132
|
+
const sourcePrefix = sourcePath === "/" ? "/" : sourcePath + "/";
|
|
133
|
+
if (destinationPath === sourcePath || destinationPath.startsWith(sourcePrefix)) {
|
|
134
|
+
throw fsError("EINVAL", "cp", dest);
|
|
135
|
+
}
|
|
136
|
+
await this.mkdir(dest, { recursive: true });
|
|
137
|
+
for (const name of await this.readdir(src)) {
|
|
138
|
+
await this.cp(join(src, name), join(dest, name), options);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
async mv(src, dest) {
|
|
142
|
+
this.writable("rename", dest);
|
|
143
|
+
await this.call("rename", src, () => this.fs.rename(src, dest));
|
|
144
|
+
}
|
|
145
|
+
resolvePath(base, path) {
|
|
146
|
+
return normalize(path.startsWith("/") ? path : `${base}/${path}`);
|
|
147
|
+
}
|
|
148
|
+
getAllPaths() {
|
|
149
|
+
return [];
|
|
150
|
+
}
|
|
151
|
+
async chmod(path, mode) {
|
|
152
|
+
this.writable("chmod", path);
|
|
153
|
+
const chmod = this.fs.chmod;
|
|
154
|
+
if (chmod) {
|
|
155
|
+
await this.call("chmod", path, () => chmod.call(this.fs, path, mode));
|
|
156
|
+
}
|
|
157
|
+
else {
|
|
158
|
+
await this.stat(path);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
async symlink(_target, linkPath) {
|
|
162
|
+
throw fsError("ENOSYS", "symlink", linkPath);
|
|
163
|
+
}
|
|
164
|
+
async link(_existingPath, newPath) {
|
|
165
|
+
throw fsError("ENOSYS", "link", newPath);
|
|
166
|
+
}
|
|
167
|
+
async readlink(path) {
|
|
168
|
+
throw fsError("EINVAL", "readlink", path);
|
|
169
|
+
}
|
|
170
|
+
async realpath(path) {
|
|
171
|
+
await this.stat(path);
|
|
172
|
+
return normalize(path);
|
|
173
|
+
}
|
|
174
|
+
async utimes(path) {
|
|
175
|
+
await this.stat(path);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
function toFsStat(stat) {
|
|
179
|
+
const isDirectory = stat.type === "directory";
|
|
180
|
+
return {
|
|
181
|
+
isFile: !isDirectory,
|
|
182
|
+
isDirectory,
|
|
183
|
+
isSymbolicLink: false,
|
|
184
|
+
mode: stat.mode ?? (isDirectory ? 0o755 : 0o644),
|
|
185
|
+
identity: stat.identity,
|
|
186
|
+
size: stat.size,
|
|
187
|
+
mtime: stat.mtime,
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
function encode(content, encoding) {
|
|
191
|
+
if (typeof content !== "string")
|
|
192
|
+
return content;
|
|
193
|
+
if (encoding === "binary" || encoding === "latin1")
|
|
194
|
+
return Uint8Array.from(content, (c) => c.charCodeAt(0));
|
|
195
|
+
if (encoding === "ascii")
|
|
196
|
+
return Uint8Array.from(content, (c) => c.charCodeAt(0) & 0x7f);
|
|
197
|
+
if (encoding === "base64")
|
|
198
|
+
return Uint8Array.from(atob(content.replace(/[^A-Za-z0-9+/]/g, "")), (c) => c.charCodeAt(0));
|
|
199
|
+
if (encoding === "hex") {
|
|
200
|
+
const digits = content.match(/^(?:[0-9a-fA-F]{2})*/)[0];
|
|
201
|
+
return Uint8Array.from({ length: digits.length / 2 }, (_, i) => parseInt(digits.slice(i * 2, i * 2 + 2), 16));
|
|
202
|
+
}
|
|
203
|
+
return new TextEncoder().encode(content);
|
|
204
|
+
}
|
|
205
|
+
function decode(bytes, encoding) {
|
|
206
|
+
if (encoding === "binary" || encoding === "latin1" || encoding === "ascii" || encoding === "base64") {
|
|
207
|
+
let text = "";
|
|
208
|
+
for (let i = 0; i < bytes.length; i += 0x8000)
|
|
209
|
+
text += String.fromCharCode(...bytes.subarray(i, i + 0x8000));
|
|
210
|
+
if (encoding === "ascii")
|
|
211
|
+
return text.replace(/[\x80-\xff]/g, (c) => String.fromCharCode(c.charCodeAt(0) & 0x7f));
|
|
212
|
+
return encoding === "base64" ? btoa(text) : text;
|
|
213
|
+
}
|
|
214
|
+
if (encoding === "hex")
|
|
215
|
+
return Array.from(bytes, (byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
216
|
+
return new TextDecoder().decode(bytes);
|
|
217
|
+
}
|
|
218
|
+
function normalize(path) {
|
|
219
|
+
const parts = [];
|
|
220
|
+
for (const part of path.split("/")) {
|
|
221
|
+
if (part === "" || part === ".")
|
|
222
|
+
continue;
|
|
223
|
+
if (part === "..")
|
|
224
|
+
parts.pop();
|
|
225
|
+
else
|
|
226
|
+
parts.push(part);
|
|
227
|
+
}
|
|
228
|
+
return `/${parts.join("/")}`;
|
|
229
|
+
}
|
|
230
|
+
function parent(path) {
|
|
231
|
+
return normalize(`${path}/..`);
|
|
232
|
+
}
|
|
233
|
+
function join(base, name) {
|
|
234
|
+
return normalize(`${base}/${name}`);
|
|
235
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { WashFileSystem } from "./fs.js";
|
|
2
|
+
import type { WashCommand, WashShell } from "./shell.js";
|
|
3
|
+
export interface JustBashShellOptions {
|
|
4
|
+
/** パスとFSの組。例: { "/workspace": workspaceFs } */
|
|
5
|
+
mounts?: Record<string, WashFileSystem>;
|
|
6
|
+
commands?: WashCommand[];
|
|
7
|
+
/** Default aliases used when the name is not already a command. */
|
|
8
|
+
aliases?: Readonly<Record<string, string>>;
|
|
9
|
+
/** 起動時のカレントディレクトリ。省略時はホームディレクトリ。 */
|
|
10
|
+
cwd?: string;
|
|
11
|
+
}
|
|
12
|
+
/** just-bashを利用したWashShellの実装。just-bashへの依存はこのファイルに閉じ込める。 */
|
|
13
|
+
export declare function createJustBashShell(options?: JustBashShellOptions): Promise<WashShell>;
|