@webpodjs/react 0.0.0-stage → 0.1.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/README.md +31 -2
- package/dist/context.d.ts +28 -0
- package/dist/context.js +45 -0
- package/dist/context.js.map +1 -0
- package/dist/file-tree.d.ts +16 -0
- package/dist/file-tree.js +68 -0
- package/dist/file-tree.js.map +1 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +7 -0
- package/dist/index.js.map +1 -0
- package/dist/preview.d.ts +39 -0
- package/dist/preview.js +63 -0
- package/dist/preview.js.map +1 -0
- package/dist/process.d.ts +17 -0
- package/dist/process.js +57 -0
- package/dist/process.js.map +1 -0
- package/dist/terminal.d.ts +20 -0
- package/dist/terminal.js +80 -0
- package/dist/terminal.js.map +1 -0
- package/package.json +51 -4
package/README.md
CHANGED
|
@@ -1,3 +1,32 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @webpodjs/react
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
React components for webpod pods. With `@webpodjs/vite-plugin`:
|
|
4
|
+
|
|
5
|
+
```jsx
|
|
6
|
+
import "@xterm/xterm/css/xterm.css";
|
|
7
|
+
import { bootPod, preview } from "virtual:webpod";
|
|
8
|
+
import { PodFileTree, PodPreview, PodProvider, PodTerminal, useProcess, usePod } from "@webpodjs/react";
|
|
9
|
+
|
|
10
|
+
export function App() {
|
|
11
|
+
return (
|
|
12
|
+
<PodProvider boot={() => bootPod({ image: "node", id: "my-app" })} preview={preview} fallback={<p>Booting…</p>}>
|
|
13
|
+
<PodFileTree root="/app" onSelect={(path) => console.log(path)} />
|
|
14
|
+
<PodTerminal command="sh" cwd="/app" />
|
|
15
|
+
<PodPreview port={3000} />
|
|
16
|
+
</PodProvider>
|
|
17
|
+
);
|
|
18
|
+
}
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
| | |
|
|
22
|
+
|---|---|
|
|
23
|
+
| `<PodProvider boot preview? fallback? error?>` | Boots the pod once per mount (`boot()`, e.g. `bootPod` from `virtual:webpod` or `Pod.boot(...)`), disposes of it on unmount. `preview`: `pod.net.expose` options for `PodPreview`. |
|
|
24
|
+
| `usePod()` | `{ pod, status: "booting" \| "ready" \| "error", error }` |
|
|
25
|
+
| `<PodTerminal command? args? cwd? env? options? onExit?>` | `command` (default `sh`) on a PTY, in xterm.js, resized with its container (FitAddon). Import `@xterm/xterm/css/xterm.css` once. |
|
|
26
|
+
| `<PodPreview port path? waitForPort? timeout? options? fallback? reloadKey? onReady?>` | Waits until the guest port accepts connections, then shows it in an iframe through `pod.net.expose` and the preview service worker. On a preview origin (`relay` in the options, the vite plugin's `previewOrigin`) the iframe is sandboxed (`PREVIEW_SANDBOX`: no top-level navigation). Without one, the page in the iframe runs on the app's origin with the app's privileges: only preview code you trust there (`docs/deploy.md`, "Previews and isolation"). |
|
|
27
|
+
| `<PodFileTree root onSelect? selected? filter?>` | Lazily loaded directory tree, kept current with `pod.fs.watch`. |
|
|
28
|
+
| `useProcess(command, args?, options?, deps?)` | Runs a command while mounted: `{ output, running, exit, error, kill }`. |
|
|
29
|
+
| `waitForPort(pod, port, { timeout?, interval?, signal? })` | Resolves once the guest port accepts TCP connections. |
|
|
30
|
+
|
|
31
|
+
`npm test` renders them in Chromium (Playwright) on the SDK's in-memory backend; it is skipped
|
|
32
|
+
when no Playwright browser is installed.
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { ReactNode } from "react";
|
|
2
|
+
import type { ExposeOptions, Pod } from "@webpodjs/sdk";
|
|
3
|
+
export type PodStatus = "booting" | "ready" | "error";
|
|
4
|
+
export interface PodState {
|
|
5
|
+
pod: Pod | null;
|
|
6
|
+
status: PodStatus;
|
|
7
|
+
error: Error | null;
|
|
8
|
+
/** Options for `pod.net.expose` (e.g. `preview` from `virtual:webpod`), used by PodPreview. */
|
|
9
|
+
preview?: ExposeOptions;
|
|
10
|
+
}
|
|
11
|
+
export interface PodProviderProps {
|
|
12
|
+
/** Boots the pod, e.g. `() => bootPod({ image: "node" })` from `virtual:webpod`. Called once per mount. */
|
|
13
|
+
boot: () => Promise<Pod>;
|
|
14
|
+
/** Options for `pod.net.expose` used by PodPreview (`preview` from `virtual:webpod`). */
|
|
15
|
+
preview?: ExposeOptions;
|
|
16
|
+
/** Rendered instead of the children until the pod is ready. */
|
|
17
|
+
fallback?: ReactNode;
|
|
18
|
+
/** Rendered when booting fails. */
|
|
19
|
+
error?: (error: Error) => ReactNode;
|
|
20
|
+
children?: ReactNode;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Boots a pod for its subtree and disposes of it on unmount. StrictMode's simulated unmount and
|
|
24
|
+
* remount reuse the same boot (a second pod with the same id could not open its disks).
|
|
25
|
+
*/
|
|
26
|
+
export declare function PodProvider({ boot, preview, fallback, error, children }: PodProviderProps): import("react").JSX.Element;
|
|
27
|
+
/** The pod of the nearest PodProvider (`pod` is null until it has booted). */
|
|
28
|
+
export declare function usePod(): PodState;
|
package/dist/context.js
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { createContext, useContext, useEffect, useRef, useState } from "react";
|
|
3
|
+
const PodContext = createContext(null);
|
|
4
|
+
/**
|
|
5
|
+
* Boots a pod for its subtree and disposes of it on unmount. StrictMode's simulated unmount and
|
|
6
|
+
* remount reuse the same boot (a second pod with the same id could not open its disks).
|
|
7
|
+
*/
|
|
8
|
+
export function PodProvider({ boot, preview, fallback, error, children }) {
|
|
9
|
+
const [state, setState] = useState({ pod: null, status: "booting", error: null, preview });
|
|
10
|
+
const booting = useRef({ promise: null, mounted: 0 });
|
|
11
|
+
useEffect(() => {
|
|
12
|
+
const b = booting.current;
|
|
13
|
+
b.mounted++;
|
|
14
|
+
const promise = (b.promise ??= boot());
|
|
15
|
+
let live = true;
|
|
16
|
+
promise.then((p) => live && setState({ pod: p, status: "ready", error: null, preview }), (e) => live && setState({ pod: null, status: "error", error: e instanceof Error ? e : new Error(String(e)), preview }));
|
|
17
|
+
return () => {
|
|
18
|
+
live = false;
|
|
19
|
+
b.mounted--;
|
|
20
|
+
// Dispose only if no remount follows right away.
|
|
21
|
+
setTimeout(() => {
|
|
22
|
+
if (b.mounted > 0 || b.promise !== promise)
|
|
23
|
+
return;
|
|
24
|
+
b.promise = null;
|
|
25
|
+
promise.then((p) => p.dispose(), () => { });
|
|
26
|
+
}, 0);
|
|
27
|
+
};
|
|
28
|
+
// Boots once per mount; a new `boot` function does not reboot.
|
|
29
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
30
|
+
}, []);
|
|
31
|
+
let body = children;
|
|
32
|
+
if (state.status === "booting" && fallback !== undefined)
|
|
33
|
+
body = fallback;
|
|
34
|
+
if (state.status === "error" && error)
|
|
35
|
+
body = error(state.error);
|
|
36
|
+
return _jsx(PodContext.Provider, { value: state, children: body });
|
|
37
|
+
}
|
|
38
|
+
/** The pod of the nearest PodProvider (`pod` is null until it has booted). */
|
|
39
|
+
export function usePod() {
|
|
40
|
+
const s = useContext(PodContext);
|
|
41
|
+
if (!s)
|
|
42
|
+
throw new Error("usePod: no PodProvider above this component");
|
|
43
|
+
return s;
|
|
44
|
+
}
|
|
45
|
+
//# sourceMappingURL=context.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.js","sourceRoot":"","sources":["../src/context.tsx"],"names":[],"mappings":";AAAA,OAAO,EAAE,aAAa,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAc/E,MAAM,UAAU,GAAG,aAAa,CAAkB,IAAI,CAAC,CAAC;AAcxD;;;GAGG;AACH,MAAM,UAAU,WAAW,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,QAAQ,EAAE,KAAK,EAAE,QAAQ,EAAoB;IACxF,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC,GAAG,QAAQ,CAAW,EAAE,GAAG,EAAE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G,EAAE,GAAE,CAAC,CAAC,CAAC;YAC7C,CAAC,EAAE,CAAC,CAAC,CAAC;QACR,CAAC,CAAC;QACF,+DAA+D;QAC/D,uDAAuD;IACzD,CAAC,EAAE,EAAE,CAAC,CAAC;IACP,IAAI,IAAI,GAAc,QAAQ,CAAC;IAC/B,IAAI,KAAK,CAAC,MAAM,KAAK,SAAS,IAAI,QAAQ,KAAK,SAAS;QAAE,IAAI,GAAG,QAAQ,CAAC;IAC1E,IAAI,KAAK,CAAC,MAAM,KAAK,OAAO,IAAI,KAAK;QAAE,IAAI,GAAG,KAAK,CAAC,KAAK,CAAC,KAAM,CAAC,CAAC;IAClE,OAAO,KAAC,UAAU,CAAC,QAAQ,IAAC,KAAK,EAAE,KAAK,YAAG,IAAI,GAAuB,CAAC;AACzE,CAAC;AAED,8EAA8E;AAC9E,MAAM,UAAU,MAAM;IACpB,MAAM,CAAC,GAAG,UAAU,CAAC,UAAU,CAAC,CAAC;IACjC,IAAI,CAAC,CAAC;QAAE,MAAM,IAAI,KAAK,CAAC,6CAA6C,CAAC,CAAC;IACvE,OAAO,CAAC,CAAC;AACX,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { CSSProperties } from "react";
|
|
2
|
+
import type { DirEntry } from "@webpodjs/sdk";
|
|
3
|
+
export interface PodFileTreeProps {
|
|
4
|
+
/** Directory to show. */
|
|
5
|
+
root: string;
|
|
6
|
+
/** Called with the absolute path of a clicked file. */
|
|
7
|
+
onSelect?: (path: string) => void;
|
|
8
|
+
/** Highlighted path. */
|
|
9
|
+
selected?: string;
|
|
10
|
+
/** Entries to leave out (default: none). */
|
|
11
|
+
filter?: (entry: DirEntry, path: string) => boolean;
|
|
12
|
+
className?: string;
|
|
13
|
+
style?: CSSProperties;
|
|
14
|
+
}
|
|
15
|
+
/** A lazily loaded directory tree of the pod's file system, kept current with `pod.fs.watch`. */
|
|
16
|
+
export declare function PodFileTree({ root, onSelect, selected, filter, className, style }: PodFileTreeProps): import("react").JSX.Element;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { jsxs as _jsxs, jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { useCallback, useEffect, useState } from "react";
|
|
3
|
+
import { usePod } from "./context.js";
|
|
4
|
+
const join = (dir, name) => (dir.endsWith("/") ? dir + name : `${dir}/${name}`);
|
|
5
|
+
const parent = (path) => path.slice(0, Math.max(path.lastIndexOf("/"), 1));
|
|
6
|
+
function sorted(entries) {
|
|
7
|
+
return [...entries].sort((a, b) => Number(b.type === "dir") - Number(a.type === "dir") || a.name.localeCompare(b.name));
|
|
8
|
+
}
|
|
9
|
+
/** A lazily loaded directory tree of the pod's file system, kept current with `pod.fs.watch`. */
|
|
10
|
+
export function PodFileTree({ root, onSelect, selected, filter, className, style }) {
|
|
11
|
+
const { pod } = usePod();
|
|
12
|
+
const [dirs, setDirs] = useState({});
|
|
13
|
+
const [open, setOpen] = useState(() => new Set([root]));
|
|
14
|
+
const load = useCallback(async (dir) => {
|
|
15
|
+
if (!pod)
|
|
16
|
+
return;
|
|
17
|
+
try {
|
|
18
|
+
const entries = sorted(await pod.fs.readdir(dir));
|
|
19
|
+
setDirs((d) => ({ ...d, [dir]: entries }));
|
|
20
|
+
}
|
|
21
|
+
catch {
|
|
22
|
+
setDirs((d) => {
|
|
23
|
+
const { [dir]: _gone, ...rest } = d;
|
|
24
|
+
return rest;
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
}, [pod]);
|
|
28
|
+
useEffect(() => {
|
|
29
|
+
if (!pod)
|
|
30
|
+
return;
|
|
31
|
+
void load(root);
|
|
32
|
+
let watcher = null;
|
|
33
|
+
let live = true;
|
|
34
|
+
pod.fs
|
|
35
|
+
.watch(root, (events) => {
|
|
36
|
+
const touched = new Set(events.map((e) => (e.type === "overflow" ? e.path : parent(e.path))));
|
|
37
|
+
for (const dir of touched)
|
|
38
|
+
void load(dir);
|
|
39
|
+
}, { recursive: true })
|
|
40
|
+
.then((w) => (live ? (watcher = w) : void w.close()), () => { });
|
|
41
|
+
return () => {
|
|
42
|
+
live = false;
|
|
43
|
+
void watcher?.close();
|
|
44
|
+
};
|
|
45
|
+
}, [pod, root, load]);
|
|
46
|
+
const toggle = (dir) => {
|
|
47
|
+
setOpen((o) => {
|
|
48
|
+
const n = new Set(o);
|
|
49
|
+
if (n.has(dir))
|
|
50
|
+
n.delete(dir);
|
|
51
|
+
else {
|
|
52
|
+
n.add(dir);
|
|
53
|
+
if (!dirs[dir])
|
|
54
|
+
void load(dir);
|
|
55
|
+
}
|
|
56
|
+
return n;
|
|
57
|
+
});
|
|
58
|
+
};
|
|
59
|
+
const render = (dir, depth) => (_jsx("ul", { style: { listStyle: "none", margin: 0, paddingLeft: depth ? 12 : 0 }, children: (dirs[dir] ?? [])
|
|
60
|
+
.filter((e) => !filter || filter(e, join(dir, e.name)))
|
|
61
|
+
.map((e) => {
|
|
62
|
+
const path = join(dir, e.name);
|
|
63
|
+
const isDir = e.type === "dir";
|
|
64
|
+
return (_jsxs("li", { children: [_jsxs("button", { type: "button", "data-path": path, onClick: () => (isDir ? toggle(path) : onSelect?.(path)), style: { all: "unset", cursor: "pointer", display: "block", padding: "1px 4px", fontWeight: path === selected ? "bold" : undefined }, children: [isDir ? (open.has(path) ? "▾ " : "▸ ") : " ", e.name] }), isDir && open.has(path) && render(path, depth + 1)] }, e.name));
|
|
65
|
+
}) }));
|
|
66
|
+
return (_jsx("div", { className: className, style: { fontFamily: "monospace", fontSize: 13, overflow: "auto", ...style }, children: render(root, 0) }));
|
|
67
|
+
}
|
|
68
|
+
//# sourceMappingURL=file-tree.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"file-tree.js","sourceRoot":"","sources":["../src/file-tree.tsx"],"names":[],"mappings":";AAAA,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAGzD,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;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type { PodProviderProps, PodState, PodStatus } from "./context.js";
|
|
3
|
+
export { PodTerminal } from "./terminal.js";
|
|
4
|
+
export type { PodTerminalProps } from "./terminal.js";
|
|
5
|
+
export { PREVIEW_SANDBOX, PodPreview, waitForPort } from "./preview.js";
|
|
6
|
+
export type { PodPreviewProps, WaitForPortOptions } from "./preview.js";
|
|
7
|
+
export { PodFileTree } from "./file-tree.js";
|
|
8
|
+
export type { PodFileTreeProps } from "./file-tree.js";
|
|
9
|
+
export { useProcess } from "./process.js";
|
|
10
|
+
export type { ProcessState } from "./process.js";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
// @webpodjs/react: React components for webpod pods.
|
|
2
|
+
export { PodProvider, usePod } from "./context.js";
|
|
3
|
+
export { PodTerminal } from "./terminal.js";
|
|
4
|
+
export { PREVIEW_SANDBOX, PodPreview, waitForPort } from "./preview.js";
|
|
5
|
+
export { PodFileTree } from "./file-tree.js";
|
|
6
|
+
export { useProcess } from "./process.js";
|
|
7
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,qDAAqD;AACrD,OAAO,EAAE,WAAW,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AAEnD,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAE5C,OAAO,EAAE,eAAe,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAExE,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAE7C,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { CSSProperties, ReactNode } from "react";
|
|
2
|
+
import type { ExposeOptions, Pod } from "@webpodjs/sdk";
|
|
3
|
+
export interface WaitForPortOptions {
|
|
4
|
+
/** Give up after this many ms (rejects with ETIMEDOUT). Default 120000. */
|
|
5
|
+
timeout?: number;
|
|
6
|
+
/** Between attempts, ms. Default 500. */
|
|
7
|
+
interval?: number;
|
|
8
|
+
signal?: AbortSignal;
|
|
9
|
+
}
|
|
10
|
+
/** Resolves once something accepts TCP connections on guest port `port`. */
|
|
11
|
+
export declare function waitForPort(pod: Pod, port: number, options?: WaitForPortOptions): Promise<void>;
|
|
12
|
+
export interface PodPreviewProps {
|
|
13
|
+
/** Guest port of the web server to show. */
|
|
14
|
+
port: number;
|
|
15
|
+
/** Path under the preview root, e.g. `"docs/"`. */
|
|
16
|
+
path?: string;
|
|
17
|
+
/** Wait until the port accepts connections before loading. Default true. */
|
|
18
|
+
waitForPort?: boolean;
|
|
19
|
+
/** Wait at most this long, ms. Default 120000. */
|
|
20
|
+
timeout?: number;
|
|
21
|
+
/** `pod.net.expose` options; default the PodProvider's `preview`. */
|
|
22
|
+
options?: ExposeOptions;
|
|
23
|
+
/** Shown while waiting for the port. */
|
|
24
|
+
fallback?: ReactNode;
|
|
25
|
+
/** A new value reloads the frame. */
|
|
26
|
+
reloadKey?: unknown;
|
|
27
|
+
onReady?: (url: string) => void;
|
|
28
|
+
title?: string;
|
|
29
|
+
className?: string;
|
|
30
|
+
style?: CSSProperties;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* `sandbox` of a preview on an origin of its own (`relay`): everything a web app needs, but not
|
|
34
|
+
* navigating the app's page away (phishing). Not on the app's origin, where a script allowed
|
|
35
|
+
* there could lift it.
|
|
36
|
+
*/
|
|
37
|
+
export declare const PREVIEW_SANDBOX = "allow-scripts allow-same-origin allow-forms allow-modals allow-popups allow-popups-to-escape-sandbox allow-downloads";
|
|
38
|
+
/** An iframe on a web server running in the pod (`pod.net.expose` + the preview service worker). */
|
|
39
|
+
export declare function PodPreview({ port, path, waitForPort: wait, timeout, options, fallback, reloadKey, onReady, title, className, style }: PodPreviewProps): import("react").JSX.Element;
|
package/dist/preview.js
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { jsxs as _jsxs, Fragment as _Fragment, jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { useEffect, useState } from "react";
|
|
3
|
+
import { usePod } from "./context.js";
|
|
4
|
+
/** Resolves once something accepts TCP connections on guest port `port`. */
|
|
5
|
+
export async function waitForPort(pod, port, options = {}) {
|
|
6
|
+
const deadline = Date.now() + (options.timeout ?? 120_000);
|
|
7
|
+
for (;;) {
|
|
8
|
+
options.signal?.throwIfAborted();
|
|
9
|
+
try {
|
|
10
|
+
const socket = await pod.net.connect(port);
|
|
11
|
+
socket.abort();
|
|
12
|
+
return;
|
|
13
|
+
}
|
|
14
|
+
catch (e) {
|
|
15
|
+
if (e.code !== "ECONNREFUSED")
|
|
16
|
+
throw e;
|
|
17
|
+
}
|
|
18
|
+
if (Date.now() > deadline)
|
|
19
|
+
throw Object.assign(new Error(`nothing listens on port ${port}`), { code: "ETIMEDOUT" });
|
|
20
|
+
await new Promise((r) => setTimeout(r, options.interval ?? 500));
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* `sandbox` of a preview on an origin of its own (`relay`): everything a web app needs, but not
|
|
25
|
+
* navigating the app's page away (phishing). Not on the app's origin, where a script allowed
|
|
26
|
+
* there could lift it.
|
|
27
|
+
*/
|
|
28
|
+
export const PREVIEW_SANDBOX = "allow-scripts allow-same-origin allow-forms allow-modals allow-popups allow-popups-to-escape-sandbox allow-downloads";
|
|
29
|
+
/** An iframe on a web server running in the pod (`pod.net.expose` + the preview service worker). */
|
|
30
|
+
export function PodPreview({ port, path = "", waitForPort: wait = true, timeout, options, fallback, reloadKey, onReady, title, className, style }) {
|
|
31
|
+
const { pod, preview } = usePod();
|
|
32
|
+
const [url, setUrl] = useState(null);
|
|
33
|
+
const [error, setError] = useState(null);
|
|
34
|
+
useEffect(() => {
|
|
35
|
+
if (!pod)
|
|
36
|
+
return;
|
|
37
|
+
const ac = new AbortController();
|
|
38
|
+
setUrl(null);
|
|
39
|
+
setError(null);
|
|
40
|
+
(async () => {
|
|
41
|
+
if (wait)
|
|
42
|
+
await waitForPort(pod, port, { timeout, signal: ac.signal });
|
|
43
|
+
const base = await pod.net.expose(port, options ?? preview);
|
|
44
|
+
if (ac.signal.aborted)
|
|
45
|
+
return;
|
|
46
|
+
const u = base + path.replace(/^\//, "");
|
|
47
|
+
setUrl(u);
|
|
48
|
+
onReady?.(u);
|
|
49
|
+
})().catch((e) => {
|
|
50
|
+
if (!ac.signal.aborted)
|
|
51
|
+
setError(e instanceof Error ? e : new Error(String(e)));
|
|
52
|
+
});
|
|
53
|
+
return () => ac.abort();
|
|
54
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
55
|
+
}, [pod, port, path, wait]);
|
|
56
|
+
if (error)
|
|
57
|
+
return _jsxs("div", { className: className, style: style, children: ["Preview of port ", port, " failed: ", error.message] });
|
|
58
|
+
if (!url)
|
|
59
|
+
return _jsx(_Fragment, { children: fallback ?? _jsxs("div", { className: className, style: style, children: ["Waiting for port ", port, "\u2026"] }) });
|
|
60
|
+
const own = new URL(url, location.href).origin !== location.origin;
|
|
61
|
+
return (_jsx("iframe", { src: url, title: title ?? `port ${port}`, sandbox: own ? PREVIEW_SANDBOX : undefined, className: className, style: { border: 0, width: "100%", height: "100%", ...style } }, String(reloadKey ?? "")));
|
|
62
|
+
}
|
|
63
|
+
//# sourceMappingURL=preview.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"preview.js","sourceRoot":"","sources":["../src/preview.tsx"],"names":[],"mappings":";AAAA,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAG5C,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AAUtC,4EAA4E;AAC5E,MAAM,CAAC,KAAK,UAAU,WAAW,CAAC,GAAQ,EAAE,IAAY,EAAE,UAA8B,EAAE;IACxF,MAAM,QAAQ,GAAG,IAAI,CAAC,GAAG,EAAE,GAAG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AAO,IAAO,CAAC;IAChH,IAAI,CAAC,GAAG;QAAE,OAAO,4BAAG,QAAQ,IAAI,eAAK,SAAS,EAAE,SAAS,EAAE,KAAK,EAAE,KAAK,kCAAoB,IAAI,cAAQ,GAAI,CAAC;IAC5G,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,GAAG,EAAE,QAAQ,CAAC,IAAI,CAAC,CAAC,MAAM,KAAK,QAAQ,CAAC,MAAM,CAAC;IACnE,OAAO,CACL,iBAEE,GAAG,EAAE,GAAG,EACR,KAAK,EAAE,KAAK,IAAI,QAAQ,IAAI,EAAE,EAC9B,OAAO,EAAE,GAAG,CAAC,CAAC,CAAC,eAAe,CAAC,CAAC,CAAC,SAAS,EAC1C,SAAS,EAAE,SAAS,EACpB,KAAK,EAAE,EAAE,MAAM,EAAE,CAAC,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,GAAG,KAAK,EAAE,IALxD,MAAM,CAAC,SAAS,IAAI,EAAE,CAAC,CAM5B,CACH,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { ExitStatus, SpawnOptions } from "@webpodjs/sdk";
|
|
2
|
+
export interface ProcessState {
|
|
3
|
+
/** stdout and stderr so far, as text (the last `maxOutput` characters). */
|
|
4
|
+
output: string;
|
|
5
|
+
running: boolean;
|
|
6
|
+
exit: ExitStatus | null;
|
|
7
|
+
error: Error | null;
|
|
8
|
+
/** Sends a signal (default SIGTERM). */
|
|
9
|
+
kill: (signal?: Parameters<import("@webpodjs/sdk").PodProcess["kill"]>[0]) => void;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Runs `command` in the pod while the component is mounted (again when `command`, `args` or
|
|
13
|
+
* `deps` change) and collects its output. `null` command: nothing runs.
|
|
14
|
+
*/
|
|
15
|
+
export declare function useProcess(command: string | null, args?: string[], options?: SpawnOptions & {
|
|
16
|
+
maxOutput?: number;
|
|
17
|
+
}, deps?: unknown[]): ProcessState;
|
package/dist/process.js
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { useEffect, useRef, useState } from "react";
|
|
2
|
+
import { usePod } from "./context.js";
|
|
3
|
+
/**
|
|
4
|
+
* Runs `command` in the pod while the component is mounted (again when `command`, `args` or
|
|
5
|
+
* `deps` change) and collects its output. `null` command: nothing runs.
|
|
6
|
+
*/
|
|
7
|
+
export function useProcess(command, args = [], options = {}, deps = []) {
|
|
8
|
+
const { pod } = usePod();
|
|
9
|
+
const [output, setOutput] = useState("");
|
|
10
|
+
const [running, setRunning] = useState(false);
|
|
11
|
+
const [exit, setExit] = useState(null);
|
|
12
|
+
const [error, setError] = useState(null);
|
|
13
|
+
const kill = useRef(() => { });
|
|
14
|
+
useEffect(() => {
|
|
15
|
+
if (!pod || command === null)
|
|
16
|
+
return;
|
|
17
|
+
const max = options.maxOutput ?? 100_000;
|
|
18
|
+
setOutput("");
|
|
19
|
+
setExit(null);
|
|
20
|
+
setError(null);
|
|
21
|
+
setRunning(true);
|
|
22
|
+
let live = true;
|
|
23
|
+
const proc = pod.spawn(command, args, options);
|
|
24
|
+
kill.current = (signal) => void proc.kill(signal ?? "SIGTERM").catch(() => { });
|
|
25
|
+
const collect = async (stream) => {
|
|
26
|
+
const dec = new TextDecoder();
|
|
27
|
+
const reader = stream.getReader();
|
|
28
|
+
for (;;) {
|
|
29
|
+
const { done, value } = await reader.read();
|
|
30
|
+
if (done || !live)
|
|
31
|
+
break;
|
|
32
|
+
const text = dec.decode(value, { stream: true });
|
|
33
|
+
setOutput((o) => (o + text).slice(-max));
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
void collect(proc.stdout).catch(() => { });
|
|
37
|
+
void collect(proc.stderr).catch(() => { });
|
|
38
|
+
proc.exit.then((s) => {
|
|
39
|
+
if (!live)
|
|
40
|
+
return;
|
|
41
|
+
setExit(s);
|
|
42
|
+
setRunning(false);
|
|
43
|
+
}, (e) => {
|
|
44
|
+
if (!live)
|
|
45
|
+
return;
|
|
46
|
+
setError(e instanceof Error ? e : new Error(String(e)));
|
|
47
|
+
setRunning(false);
|
|
48
|
+
});
|
|
49
|
+
return () => {
|
|
50
|
+
live = false;
|
|
51
|
+
void proc.kill("SIGTERM").catch(() => { });
|
|
52
|
+
};
|
|
53
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
54
|
+
}, [pod, command, JSON.stringify(args), ...deps]);
|
|
55
|
+
return { output, running, exit, error, kill: (s) => kill.current(s) };
|
|
56
|
+
}
|
|
57
|
+
//# sourceMappingURL=process.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"process.js","sourceRoot":"","sources":["../src/process.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAEpD,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AAYtC;;;GAGG;AACH,MAAM,UAAU,UAAU,CAAC,OAAsB,EAAE,OAAiB,EAAE,EAAE,UAAiD,EAAE,EAAE,OAAkB,EAAE;IAC/I,MAAM,EAAE,GAAG,EAAE,GAAG,MAAM,EAAE,CAAC;IACzB,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,GAAG,QAAQ,CAAC,EAAE,CAAC,CAAC;IACzC,MAAM,CAAC,OAAO,EAAE,UAAU,CAAC,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAC;IAC9C,MAAM,CAAC,IAAI,EAAE,OAAO,CAAC,GAAG,QAAQ,CAAoB,IAAI,CAAC,CAAC;IAC1D,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC,GAAG,QAAQ,CAAe,IAAI,CAAC,CAAC;IACvD,MAAM,IAAI,GAAG,MAAM,CAAuB,GAAG,EAAE,GAAE,CAAC,CAAC,CAAC;IACpD,SAAS,CAAC,GAAG,EAAE;QACb,IAAI,CAAC,GAAG,IAAI,OAAO,KAAK,IAAI;YAAE,OAAO;QACrC,MAAM,G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AG,EAAE,GAAE,CAAC,CAAC,CAAC;QAC1C,IAAI,CAAC,IAAI,CAAC,IAAI,CACZ,CAAC,CAAC,EAAE,EAAE;YACJ,IAAI,CAAC,IAAI;gBAAE,OAAO;YAClB,OAAO,CAAC,CAAC,CAAC,CAAC;YACX,UAAU,CAAC,KAAK,CAAC,CAAC;QACpB,CAAC,EACD,CAAC,CAAU,EAAE,EAAE;YACb,IAAI,CAAC,IAAI;gBAAE,OAAO;YAClB,QAAQ,CAAC,CAAC,YAAY,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;YACxD,UAAU,CAAC,KAAK,CAAC,CAAC;QACpB,CAAC,CACF,CAAC;QACF,OAAO,GAAG,EAAE;YACV,IAAI,GAAG,KAAK,CAAC;YACb,KAAK,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,KAAK,CAAC,GAAG,EAAE,GAAE,CAAC,CAAC,CAAC;QAC5C,CAAC,CAAC;QACF,uDAAuD;IACzD,CAAC,EAAE,CAAC,GAAG,EAAE,OAAO,EAAE,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,EAAE,GAAG,IAAI,CAAC,CAAC,CAAC;IAClD,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,CAAC;AACxE,CAAC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { CSSProperties } from "react";
|
|
2
|
+
import type { ITerminalOptions } from "@xterm/xterm";
|
|
3
|
+
import type { ExitStatus } from "@webpodjs/sdk";
|
|
4
|
+
export interface PodTerminalProps {
|
|
5
|
+
/** Program to run on a PTY. Default `sh`. */
|
|
6
|
+
command?: string;
|
|
7
|
+
args?: string[];
|
|
8
|
+
cwd?: string;
|
|
9
|
+
env?: Record<string, string>;
|
|
10
|
+
/** xterm.js options (theme, font, ...). */
|
|
11
|
+
options?: ITerminalOptions;
|
|
12
|
+
onExit?: (status: ExitStatus) => void;
|
|
13
|
+
className?: string;
|
|
14
|
+
style?: CSSProperties;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* An interactive terminal: `command` on a PTY in the pod, shown with xterm.js, resized with its
|
|
18
|
+
* container. Import `@xterm/xterm/css/xterm.css` once in the app.
|
|
19
|
+
*/
|
|
20
|
+
export declare function PodTerminal({ command, args, cwd, env, options, onExit, className, style }: PodTerminalProps): import("react").JSX.Element;
|
package/dist/terminal.js
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { useEffect, useRef } from "react";
|
|
3
|
+
import { Terminal } from "@xterm/xterm";
|
|
4
|
+
import { FitAddon } from "@xterm/addon-fit";
|
|
5
|
+
import { usePod } from "./context.js";
|
|
6
|
+
/**
|
|
7
|
+
* An interactive terminal: `command` on a PTY in the pod, shown with xterm.js, resized with its
|
|
8
|
+
* container. Import `@xterm/xterm/css/xterm.css` once in the app.
|
|
9
|
+
*/
|
|
10
|
+
export function PodTerminal({ command = "sh", args = [], cwd, env, options, onExit, className, style }) {
|
|
11
|
+
const { pod } = usePod();
|
|
12
|
+
const host = useRef(null);
|
|
13
|
+
const exitRef = useRef(onExit);
|
|
14
|
+
exitRef.current = onExit;
|
|
15
|
+
useEffect(() => {
|
|
16
|
+
if (!pod || !host.current)
|
|
17
|
+
return;
|
|
18
|
+
const term = new Terminal({ cursorBlink: true, convertEol: false, ...options });
|
|
19
|
+
const fit = new FitAddon();
|
|
20
|
+
term.loadAddon(fit);
|
|
21
|
+
term.open(host.current);
|
|
22
|
+
fit.fit();
|
|
23
|
+
const proc = pod.spawn(command, args, { cwd, env, pty: { cols: term.cols, rows: term.rows } });
|
|
24
|
+
const stdin = proc.stdin.getWriter();
|
|
25
|
+
const enc = new TextEncoder();
|
|
26
|
+
const input = term.onData((d) => void stdin.write(enc.encode(d)).catch(() => { }));
|
|
27
|
+
const resize = term.onResize(({ cols, rows }) => void proc.resize({ cols, rows }).catch(() => { }));
|
|
28
|
+
// Fitted in the next frame: resizing the terminal inside the observer's callback changes the
|
|
29
|
+
// layout it observes (WebKit then reports "ResizeObserver loop completed with undelivered
|
|
30
|
+
// notifications" as a page error).
|
|
31
|
+
let frame = 0;
|
|
32
|
+
const observer = new ResizeObserver(() => {
|
|
33
|
+
cancelAnimationFrame(frame);
|
|
34
|
+
frame = requestAnimationFrame(() => {
|
|
35
|
+
try {
|
|
36
|
+
fit.fit();
|
|
37
|
+
}
|
|
38
|
+
catch {
|
|
39
|
+
// detached
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
observer.observe(host.current);
|
|
44
|
+
let done = false;
|
|
45
|
+
void (async () => {
|
|
46
|
+
const reader = proc.stdout.getReader();
|
|
47
|
+
try {
|
|
48
|
+
for (;;) {
|
|
49
|
+
const { done: end, value } = await reader.read();
|
|
50
|
+
if (end)
|
|
51
|
+
break;
|
|
52
|
+
term.write(value);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
catch {
|
|
56
|
+
// process gone
|
|
57
|
+
}
|
|
58
|
+
})();
|
|
59
|
+
proc.exit.then((s) => {
|
|
60
|
+
done = true;
|
|
61
|
+
exitRef.current?.(s);
|
|
62
|
+
}, () => {
|
|
63
|
+
done = true;
|
|
64
|
+
});
|
|
65
|
+
return () => {
|
|
66
|
+
observer.disconnect();
|
|
67
|
+
cancelAnimationFrame(frame);
|
|
68
|
+
input.dispose();
|
|
69
|
+
resize.dispose();
|
|
70
|
+
if (!done)
|
|
71
|
+
void proc.kill("SIGHUP").catch(() => { });
|
|
72
|
+
stdin.releaseLock();
|
|
73
|
+
term.dispose();
|
|
74
|
+
};
|
|
75
|
+
// Restarts the process when what runs changes.
|
|
76
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
77
|
+
}, [pod, command, JSON.stringify(args), cwd, JSON.stringify(env)]);
|
|
78
|
+
return _jsx("div", { ref: host, className: className, style: { width: "100%", height: "100%", ...style } });
|
|
79
|
+
}
|
|
80
|
+
//# sourceMappingURL=terminal.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"terminal.js","sourceRoot":"","sources":["../src/terminal.tsx"],"names":[],"mappings":";AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,OAAO,CAAC;AAE1C,OAAO,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAExC,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAE5C,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AAetC;;;GAGG;AACH,MAAM,UAAU,WAAW,CAAC,EAAE,OAAO,GAAG,IAAI,EAAE,IAAI,GAAG,EAAE,EAAE,GAAG,EAAE,GAAG,EAAE,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,KAAK,EAAoB;IACtH,MAAM,EAAE,GAAG,EAAE,GAAG,MAAM,EAAE,CAAC;IACzB,MAAM,IAAI,GAAG,MAAM,CAAiB,IAAI,CAAC,CAAC;IAC1C,MAAM,OAAO,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC;IAC/B,OAAO,CAAC,OAAO,GAAG,MAAM,CAAC;IACzB,SAAS,CAAC,GAAG,EAAE;QACb,IAAI,CAAC,GAAG,IAAI,CAAC,IAAI,CAAC,OAAO;YAAE,OAAO;QAClC,MAAM,IAAI,GAAG,IAAI,QAAQ,CAAC,EAAE,WAAW,EAAE,IAAI,EAAE,UAAU,EAAE,KAAK,EAAE,GAAG,OAAO,EAAE,CAAC,CAAC;QAChF,MAAM,GAAG,GAAG,IAAI,QAAQ,EAAE,CAAC;QAC3B,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC;QACpB,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,OAAO,CAA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AAC,GAAG,CAAC,CAAC,CAAC,CAAC;IACnE,OAAO,cAAK,GAAG,EAAE,IAAI,EAAE,SAAS,EAAE,SAAS,EAAE,KAAK,EAAE,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,GAAG,KAAK,EAAE,GAAI,CAAC;AACtG,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,53 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webpodjs/react",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "React components for webpod: PodProvider, PodTerminal, PodPreview, PodFileTree, useProcess",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"webpod",
|
|
7
|
+
"react",
|
|
8
|
+
"terminal",
|
|
9
|
+
"xterm"
|
|
10
|
+
],
|
|
11
|
+
"license": "Apache-2.0",
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "git+https://github.com/mirrorange/webpod.git",
|
|
15
|
+
"directory": "packages/react"
|
|
16
|
+
},
|
|
17
|
+
"homepage": "https://github.com/mirrorange/webpod/tree/main/packages/react#readme",
|
|
18
|
+
"bugs": "https://github.com/mirrorange/webpod/issues",
|
|
19
|
+
"type": "module",
|
|
20
|
+
"main": "dist/index.js",
|
|
21
|
+
"types": "dist/index.d.ts",
|
|
22
|
+
"exports": {
|
|
23
|
+
".": {
|
|
24
|
+
"types": "./dist/index.d.ts",
|
|
25
|
+
"default": "./dist/index.js"
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
"files": [
|
|
29
|
+
"dist"
|
|
30
|
+
],
|
|
31
|
+
"sideEffects": false,
|
|
32
|
+
"publishConfig": {
|
|
33
|
+
"access": "public"
|
|
34
|
+
},
|
|
35
|
+
"scripts": {
|
|
36
|
+
"build": "tsc -p .",
|
|
37
|
+
"test": "node --test test/*.test.js"
|
|
38
|
+
},
|
|
39
|
+
"dependencies": {
|
|
40
|
+
"@xterm/addon-fit": "^0.11.0",
|
|
41
|
+
"@xterm/xterm": "^6.0.0"
|
|
42
|
+
},
|
|
43
|
+
"peerDependencies": {
|
|
44
|
+
"@webpodjs/sdk": "^0.1.1",
|
|
45
|
+
"react": ">=18"
|
|
46
|
+
},
|
|
47
|
+
"devDependencies": {
|
|
48
|
+
"@types/react": "^19.0.0",
|
|
49
|
+
"react": "^19.0.0",
|
|
50
|
+
"react-dom": "^19.0.0",
|
|
51
|
+
"vite": "^8.4.0"
|
|
52
|
+
}
|
|
53
|
+
}
|