@hudhod/webcontainer 0.1.0
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 +27 -0
- package/dist/index.d.ts +37 -0
- package/dist/index.js +164 -0
- package/package.json +36 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 hudhod
|
|
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
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# @hudhod/webcontainer
|
|
2
|
+
|
|
3
|
+
WebContainer adapters for the headless Hudhod runtime.
|
|
4
|
+
|
|
5
|
+
This package is browser-only. It provides `WebContainerFileSystemProvider`, `WebContainerProcessSpawner`, and `createWebContainerServices()` for use with `createHudhodRuntime()` or `createHudhodReactHost()`.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
pnpm add @hudhod/webcontainer @hudhod/core @webcontainer/api
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
WebContainers require `SharedArrayBuffer` and cross-origin isolation. Do not import this package from Node or server-rendered code.
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
import { createHudhodReactHost } from "@hudhod/react";
|
|
19
|
+
import { createWebContainerServices } from "@hudhod/webcontainer";
|
|
20
|
+
|
|
21
|
+
const host = createHudhodReactHost({
|
|
22
|
+
...createWebContainerServices(container),
|
|
23
|
+
ui: myUiAdapter,
|
|
24
|
+
});
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
The package is optional. Desktop, remote, and test hosts can instead supply their own `FileSystemProvider` and `ProcessSpawner` implementations from `@hudhod/core`.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { FileSystemProvider, ProcessSpawner, ProviderEntry, ProviderStat, ProviderWatchOptions, SpawnedProcess, SpawnerOptions } from "@hudhod/core";
|
|
2
|
+
import { WebContainer } from "@webcontainer/api";
|
|
3
|
+
import { Disposable, FileChangeEvent } from "@hudhod/sdk";
|
|
4
|
+
|
|
5
|
+
//#region src/index.d.ts
|
|
6
|
+
/** Adapts WebContainer's filesystem to Hudhod's provider contract. */
|
|
7
|
+
declare class WebContainerFileSystemProvider implements FileSystemProvider {
|
|
8
|
+
#private;
|
|
9
|
+
readonly name = "webcontainer";
|
|
10
|
+
constructor(container: WebContainer);
|
|
11
|
+
readFile(path: string): Promise<Uint8Array>;
|
|
12
|
+
writeFile(path: string, data: Uint8Array): Promise<void>;
|
|
13
|
+
createDirectory(path: string): Promise<void>;
|
|
14
|
+
delete(path: string, options: {
|
|
15
|
+
recursive: boolean;
|
|
16
|
+
}): Promise<void>;
|
|
17
|
+
rename(from: string, to: string, options: {
|
|
18
|
+
overwrite: boolean;
|
|
19
|
+
}): Promise<void>;
|
|
20
|
+
stat(path: string): Promise<ProviderStat>;
|
|
21
|
+
readDirectory(path: string): Promise<ProviderEntry[]>;
|
|
22
|
+
watch(path: string, options: ProviderWatchOptions, listener: (events: readonly FileChangeEvent[]) => void): Disposable;
|
|
23
|
+
}
|
|
24
|
+
/** Adapts WebContainer process spawning to Hudhod's process contract. */
|
|
25
|
+
declare class WebContainerProcessSpawner implements ProcessSpawner {
|
|
26
|
+
#private;
|
|
27
|
+
readonly name = "webcontainer";
|
|
28
|
+
constructor(container: WebContainer);
|
|
29
|
+
spawn(command: string, args: readonly string[], options: SpawnerOptions): Promise<SpawnedProcess>;
|
|
30
|
+
}
|
|
31
|
+
/** Creates the standard filesystem and process adapters for a WebContainer instance. */
|
|
32
|
+
declare function createWebContainerServices(container: WebContainer): {
|
|
33
|
+
fileSystemProvider: FileSystemProvider;
|
|
34
|
+
processSpawner: ProcessSpawner;
|
|
35
|
+
};
|
|
36
|
+
//#endregion
|
|
37
|
+
export { WebContainerFileSystemProvider, WebContainerProcessSpawner, createWebContainerServices };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import { ROOT, basename, directoryNotEmpty, dirname, fileExists, fileNotFound, joinPath, notADirectory, toDisposable } from "@hudhod/core";
|
|
2
|
+
|
|
3
|
+
//#region src/index.ts
|
|
4
|
+
/** Adapts WebContainer's filesystem to Hudhod's provider contract. */
|
|
5
|
+
var WebContainerFileSystemProvider = class {
|
|
6
|
+
name = "webcontainer";
|
|
7
|
+
#container;
|
|
8
|
+
constructor(container) {
|
|
9
|
+
this.#container = container;
|
|
10
|
+
}
|
|
11
|
+
async readFile(path) {
|
|
12
|
+
try {
|
|
13
|
+
return await this.#container.fs.readFile(path);
|
|
14
|
+
} catch (error) {
|
|
15
|
+
throw translate(error, path);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
async writeFile(path, data) {
|
|
19
|
+
try {
|
|
20
|
+
await this.#container.fs.writeFile(path, data);
|
|
21
|
+
} catch (error) {
|
|
22
|
+
throw translate(error, path);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
async createDirectory(path) {
|
|
26
|
+
try {
|
|
27
|
+
await this.#container.fs.mkdir(path, { recursive: true });
|
|
28
|
+
} catch (error) {
|
|
29
|
+
if (isErrno(error, "EEXIST")) {
|
|
30
|
+
if ((await this.stat(path)).type === "directory") return;
|
|
31
|
+
throw fileExists(path);
|
|
32
|
+
}
|
|
33
|
+
throw translate(error, path);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
async delete(path, options) {
|
|
37
|
+
try {
|
|
38
|
+
await this.#container.fs.rm(path, { recursive: options.recursive });
|
|
39
|
+
} catch (error) {
|
|
40
|
+
if (isErrno(error, "ENOTEMPTY")) throw directoryNotEmpty(path);
|
|
41
|
+
throw translate(error, path);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
async rename(from, to, options) {
|
|
45
|
+
if (!options.overwrite && await this.#exists(to)) throw fileExists(to);
|
|
46
|
+
try {
|
|
47
|
+
await this.#container.fs.rename(from, to);
|
|
48
|
+
} catch (error) {
|
|
49
|
+
throw translate(error, from);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
async stat(path) {
|
|
53
|
+
if (path === ROOT) return {
|
|
54
|
+
type: "directory",
|
|
55
|
+
size: 0,
|
|
56
|
+
mtime: 0
|
|
57
|
+
};
|
|
58
|
+
const name = basename(path);
|
|
59
|
+
let entries;
|
|
60
|
+
try {
|
|
61
|
+
entries = await this.readDirectory(dirname(path));
|
|
62
|
+
} catch {
|
|
63
|
+
throw fileNotFound(path);
|
|
64
|
+
}
|
|
65
|
+
const entry = entries.find((candidate) => candidate.name === name);
|
|
66
|
+
if (!entry) throw fileNotFound(path);
|
|
67
|
+
if (entry.type === "directory") return {
|
|
68
|
+
type: "directory",
|
|
69
|
+
size: 0,
|
|
70
|
+
mtime: 0
|
|
71
|
+
};
|
|
72
|
+
return {
|
|
73
|
+
type: "file",
|
|
74
|
+
size: (await this.readFile(path)).byteLength,
|
|
75
|
+
mtime: 0
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
async readDirectory(path) {
|
|
79
|
+
try {
|
|
80
|
+
return (await this.#container.fs.readdir(path, { withFileTypes: true })).map((entry) => ({
|
|
81
|
+
name: entry.name,
|
|
82
|
+
type: entry.isDirectory() ? "directory" : "file"
|
|
83
|
+
}));
|
|
84
|
+
} catch (error) {
|
|
85
|
+
if (isErrno(error, "ENOTDIR")) throw notADirectory(path);
|
|
86
|
+
throw translate(error, path);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
watch(path, options, listener) {
|
|
90
|
+
const watcher = this.#container.fs.watch(path, { recursive: options.recursive }, (event, filename) => {
|
|
91
|
+
if (typeof filename !== "string") return;
|
|
92
|
+
const changed = joinPath(path, filename);
|
|
93
|
+
if (event === "change") {
|
|
94
|
+
listener([{
|
|
95
|
+
type: "changed",
|
|
96
|
+
path: changed
|
|
97
|
+
}]);
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
this.#exists(changed).then((exists) => listener([{
|
|
101
|
+
type: exists ? "created" : "deleted",
|
|
102
|
+
path: changed
|
|
103
|
+
}]));
|
|
104
|
+
});
|
|
105
|
+
return toDisposable(() => watcher.close());
|
|
106
|
+
}
|
|
107
|
+
async #exists(path) {
|
|
108
|
+
try {
|
|
109
|
+
await this.stat(path);
|
|
110
|
+
return true;
|
|
111
|
+
} catch {
|
|
112
|
+
return false;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
/** Adapts WebContainer process spawning to Hudhod's process contract. */
|
|
117
|
+
var WebContainerProcessSpawner = class {
|
|
118
|
+
name = "webcontainer";
|
|
119
|
+
#container;
|
|
120
|
+
constructor(container) {
|
|
121
|
+
this.#container = container;
|
|
122
|
+
}
|
|
123
|
+
async spawn(command, args, options) {
|
|
124
|
+
const process = await this.#container.spawn(command, [...args], {
|
|
125
|
+
...options.cwd ? { cwd: options.cwd } : {},
|
|
126
|
+
...options.env ? { env: { ...options.env } } : {},
|
|
127
|
+
...options.terminal ? { terminal: { ...options.terminal } } : {}
|
|
128
|
+
});
|
|
129
|
+
let killed = false;
|
|
130
|
+
return {
|
|
131
|
+
output: process.output,
|
|
132
|
+
input: process.input,
|
|
133
|
+
exit: process.exit,
|
|
134
|
+
kill() {
|
|
135
|
+
if (killed) return;
|
|
136
|
+
killed = true;
|
|
137
|
+
process.kill();
|
|
138
|
+
},
|
|
139
|
+
resize(dimensions) {
|
|
140
|
+
if (options.terminal) process.resize(dimensions);
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
};
|
|
145
|
+
/** Creates the standard filesystem and process adapters for a WebContainer instance. */
|
|
146
|
+
function createWebContainerServices(container) {
|
|
147
|
+
return {
|
|
148
|
+
fileSystemProvider: new WebContainerFileSystemProvider(container),
|
|
149
|
+
processSpawner: new WebContainerProcessSpawner(container)
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
function isErrno(error, code) {
|
|
153
|
+
return typeof error === "object" && error !== null && error.code === code;
|
|
154
|
+
}
|
|
155
|
+
function translate(error, path) {
|
|
156
|
+
if (isErrno(error, "ENOENT")) return fileNotFound(path);
|
|
157
|
+
if (isErrno(error, "ENOTDIR")) return notADirectory(path);
|
|
158
|
+
if (isErrno(error, "EEXIST")) return fileExists(path);
|
|
159
|
+
if (isErrno(error, "ENOTEMPTY")) return directoryNotEmpty(path);
|
|
160
|
+
return error;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
//#endregion
|
|
164
|
+
export { WebContainerFileSystemProvider, WebContainerProcessSpawner, createWebContainerServices };
|
package/package.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@hudhod/webcontainer",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "WebContainer adapters for the headless Hudhod runtime.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"files": [
|
|
7
|
+
"dist"
|
|
8
|
+
],
|
|
9
|
+
"type": "module",
|
|
10
|
+
"sideEffects": false,
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"types": "./dist/index.d.ts",
|
|
14
|
+
"default": "./dist/index.js"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"publishConfig": {
|
|
18
|
+
"access": "public"
|
|
19
|
+
},
|
|
20
|
+
"dependencies": {
|
|
21
|
+
"@hudhod/core": "0.1.0",
|
|
22
|
+
"@hudhod/sdk": "0.1.0"
|
|
23
|
+
},
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"@webcontainer/api": "^1.6.4",
|
|
26
|
+
"tsdown": "^0.15.6",
|
|
27
|
+
"typescript": "^5.7.2"
|
|
28
|
+
},
|
|
29
|
+
"peerDependencies": {
|
|
30
|
+
"@webcontainer/api": "^1.6.4"
|
|
31
|
+
},
|
|
32
|
+
"scripts": {
|
|
33
|
+
"build": "tsdown",
|
|
34
|
+
"typecheck": "tsc --noEmit"
|
|
35
|
+
}
|
|
36
|
+
}
|