@vivari/react 1.0.0 → 1.0.2
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 +423 -40
- package/dist/Vivari.d.ts +36 -12
- package/dist/Vivari.d.ts.map +1 -1
- package/dist/VivariPreview.d.ts +27 -0
- package/dist/VivariPreview.d.ts.map +1 -0
- package/dist/context.d.ts +28 -0
- package/dist/context.d.ts.map +1 -0
- package/dist/index.d.ts +14 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +771 -129
- package/dist/useSpawn.d.ts +46 -0
- package/dist/useSpawn.d.ts.map +1 -0
- package/dist/useVivari.d.ts +58 -10
- package/dist/useVivari.d.ts.map +1 -1
- package/dist/useVivariDir.d.ts +30 -0
- package/dist/useVivariDir.d.ts.map +1 -0
- package/dist/useVivariFile.d.ts +29 -0
- package/dist/useVivariFile.d.ts.map +1 -0
- package/package.json +11 -10
package/README.md
CHANGED
|
@@ -1,20 +1,21 @@
|
|
|
1
1
|
# @vivari/react
|
|
2
2
|
|
|
3
|
-
React bindings for [Vivari](../core)
|
|
4
|
-
the browser, embedded in your React app.
|
|
3
|
+
React bindings for [Vivari](../core). Run Node, Bun and Python projects fully
|
|
4
|
+
client-side in the browser, embedded in your React app.
|
|
5
5
|
|
|
6
6
|
```bash
|
|
7
7
|
npm install @vivari/react @vivari/core react
|
|
8
8
|
```
|
|
9
9
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
`@vivari/core` and `react` are **peer dependencies**: you install them, and you
|
|
11
|
+
get exactly one copy of each. (Two copies of core would mean two kernel worker
|
|
12
|
+
bundles and two Wasm asset sets in your build.) The package is ESM-only.
|
|
13
13
|
|
|
14
|
-
|
|
14
|
+
> Like all of Vivari, this needs a **cross-origin isolated** page. See
|
|
15
|
+
> [Cross-origin isolation](#cross-origin-isolation) below — it is the wall most
|
|
16
|
+
> people hit first.
|
|
15
17
|
|
|
16
|
-
|
|
17
|
-
resulting dev-server preview in an `<iframe>`.
|
|
18
|
+
## Quick start
|
|
18
19
|
|
|
19
20
|
```tsx
|
|
20
21
|
import { Vivari } from "@vivari/react";
|
|
@@ -31,8 +32,8 @@ export function Playground() {
|
|
|
31
32
|
<Vivari
|
|
32
33
|
files={files}
|
|
33
34
|
run="npm run dev"
|
|
34
|
-
onServerReady={(port, url) => console.log("ready", port, url)}
|
|
35
35
|
onOutput={(chunk) => console.log(chunk)}
|
|
36
|
+
onError={({ phase, error }) => console.error(phase, error)}
|
|
36
37
|
style={{ width: "100%", height: 480, border: 0 }}
|
|
37
38
|
fallback={<p>Booting Vivari…</p>}
|
|
38
39
|
/>
|
|
@@ -40,49 +41,431 @@ export function Playground() {
|
|
|
40
41
|
}
|
|
41
42
|
```
|
|
42
43
|
|
|
43
|
-
|
|
44
|
+
`fallback` is the **pending** slot only. Failures — a page that isn't
|
|
45
|
+
cross-origin isolated, a failed `npm install`, a spawn that throws — render a
|
|
46
|
+
built-in notice instead, and call `onError`. Nothing is swallowed. Use
|
|
47
|
+
`renderError` to replace that notice.
|
|
44
48
|
|
|
45
|
-
|
|
49
|
+
## Composing the pieces
|
|
46
50
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
| `files` | `FileSystemTree` | – |
|
|
50
|
-
| `install` | `string \| string[] \| false` | `["npm", "install"]` |
|
|
51
|
-
| `run` | `string \| string[]` | – |
|
|
52
|
-
| `onReady` | `(vivari) => void` | – |
|
|
53
|
-
| `onServerReady` | `(port, url) => void` | – |
|
|
54
|
-
| `onOutput` | `(chunk) => void` | – |
|
|
55
|
-
| `showPreview` | `boolean` | `true` |
|
|
56
|
-
| `className` | `string` | – |
|
|
57
|
-
| `style` | `CSSProperties` | – |
|
|
58
|
-
| `fallback` | `ReactNode` | `null` |
|
|
51
|
+
`<Vivari>` is a thin composition of a boot hook and a preview component. When you
|
|
52
|
+
need your own layout, use them directly — one kernel is shared across the tree:
|
|
59
53
|
|
|
60
|
-
|
|
54
|
+
```tsx
|
|
55
|
+
import {
|
|
56
|
+
VivariProvider,
|
|
57
|
+
VivariPreview,
|
|
58
|
+
useVivariFile,
|
|
59
|
+
useSpawn,
|
|
60
|
+
} from "@vivari/react";
|
|
61
|
+
|
|
62
|
+
function Editor() {
|
|
63
|
+
const [source, setSource, { status, save }] = useVivariFile("/src/App.jsx");
|
|
64
|
+
return (
|
|
65
|
+
<textarea
|
|
66
|
+
value={source}
|
|
67
|
+
onChange={(e) => setSource(e.target.value)} // debounced write-behind
|
|
68
|
+
onKeyDown={(e) => (e.metaKey || e.ctrlKey) && e.key === "s" && save()}
|
|
69
|
+
data-status={status}
|
|
70
|
+
/>
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function DevServer() {
|
|
75
|
+
const { run, status } = useSpawn("npm", ["run", "dev"]);
|
|
76
|
+
return (
|
|
77
|
+
<>
|
|
78
|
+
<button onClick={() => run()} disabled={status === "running"}>Start</button>
|
|
79
|
+
<VivariPreview port={5173} style={{ width: "100%", height: 480, border: 0 }} />
|
|
80
|
+
</>
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export function App() {
|
|
85
|
+
return (
|
|
86
|
+
<VivariProvider>
|
|
87
|
+
<Editor />
|
|
88
|
+
<DevServer />
|
|
89
|
+
</VivariProvider>
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
```
|
|
61
93
|
|
|
62
|
-
|
|
94
|
+
You can also pass `children` to `<Vivari>` to keep its install/run orchestration
|
|
95
|
+
while taking over rendering. A function child receives the live boot state:
|
|
63
96
|
|
|
64
97
|
```tsx
|
|
65
|
-
|
|
98
|
+
<Vivari files={files} run="npm run dev">
|
|
99
|
+
{(state) =>
|
|
100
|
+
state.status === "ready"
|
|
101
|
+
? <VivariPreview port={5173} vivari={state.vivari} style={{ height: 480 }} />
|
|
102
|
+
: <p>{state.status}…</p>}
|
|
103
|
+
</Vivari>
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
`<Vivari>` publishes nothing to the tree below it, so a nested `<VivariPreview>`
|
|
107
|
+
takes the instance from `state` rather than from context. Under a
|
|
108
|
+
`<VivariProvider>` you can leave `vivari` off and let it resolve on its own.
|
|
109
|
+
|
|
110
|
+
## Cross-origin isolation
|
|
111
|
+
|
|
112
|
+
Vivari needs `SharedArrayBuffer`, which browsers only expose on a cross-origin
|
|
113
|
+
isolated page. Serve your HTML documents (and `sw.js`) with:
|
|
114
|
+
|
|
115
|
+
```
|
|
116
|
+
Cross-Origin-Opener-Policy: same-origin
|
|
117
|
+
Cross-Origin-Embedder-Policy: require-corp
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
Check before you render:
|
|
121
|
+
|
|
122
|
+
```tsx
|
|
123
|
+
import { isCrossOriginIsolated } from "@vivari/react";
|
|
124
|
+
|
|
125
|
+
if (!isCrossOriginIsolated()) {
|
|
126
|
+
// Vivari cannot boot here. The hooks report this as
|
|
127
|
+
// `status: "unsupported"` with `reason: "not-cross-origin-isolated"`.
|
|
128
|
+
}
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
### Next.js (App Router)
|
|
66
132
|
|
|
67
|
-
|
|
68
|
-
|
|
133
|
+
```js
|
|
134
|
+
// next.config.js
|
|
135
|
+
const isolation = [
|
|
136
|
+
{ key: "Cross-Origin-Opener-Policy", value: "same-origin" },
|
|
137
|
+
{ key: "Cross-Origin-Embedder-Policy", value: "require-corp" },
|
|
138
|
+
];
|
|
139
|
+
|
|
140
|
+
module.exports = {
|
|
141
|
+
async headers() {
|
|
142
|
+
// Scope this to the playground route, NOT to /:path* — see the warning below.
|
|
143
|
+
return [{ source: "/playground/:path*", headers: isolation }];
|
|
144
|
+
},
|
|
145
|
+
};
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
Two Next-specific gotchas:
|
|
149
|
+
|
|
150
|
+
1. **`require-corp` breaks cross-origin subresources.** Under it, every
|
|
151
|
+
cross-origin resource must opt in with `Cross-Origin-Resource-Policy` (or
|
|
152
|
+
CORS). That means remote-loader `next/image` sources, third-party analytics,
|
|
153
|
+
embedded fonts and most `<script src>` tags from other origins will fail on an
|
|
154
|
+
isolated page. Scope the headers to the route that hosts Vivari rather than
|
|
155
|
+
applying them site-wide.
|
|
156
|
+
2. **Next does not serve the Service Worker with the header Vivari needs.** Put
|
|
157
|
+
`sw.js` in `public/` and add a header rule giving it
|
|
158
|
+
`Service-Worker-Allowed: /`, or pass `boot={{ serviceWorkerUrl: false }}` if
|
|
159
|
+
you don't need in-browser server previews. The default is `"/sw.js"`.
|
|
160
|
+
|
|
161
|
+
### Vite
|
|
162
|
+
|
|
163
|
+
```ts
|
|
164
|
+
const isolation = {
|
|
165
|
+
"Cross-Origin-Opener-Policy": "same-origin",
|
|
166
|
+
"Cross-Origin-Embedder-Policy": "require-corp",
|
|
167
|
+
};
|
|
168
|
+
export default defineConfig({ server: { headers: isolation }, preview: { headers: isolation } });
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
More recipes (Cloudflare, Netlify, Nginx, Express) are in the
|
|
172
|
+
[core docs](../core/README.md).
|
|
173
|
+
|
|
174
|
+
## Server rendering
|
|
175
|
+
|
|
176
|
+
Every entry point carries `"use client"`, so importing this package from a
|
|
177
|
+
Next.js Server Component works. During SSR/prerender the hooks report
|
|
178
|
+
`status: "idle"` and boot nothing; the kernel starts in an effect on the client.
|
|
179
|
+
|
|
180
|
+
## API
|
|
181
|
+
|
|
182
|
+
### `<Vivari>`
|
|
183
|
+
|
|
184
|
+
Extends the standard `<iframe>` props (`className`, `style`, `id`, `allow`,
|
|
185
|
+
`sandbox`, `loading`, …), which reach the preview frame. `ref` forwards to it.
|
|
186
|
+
|
|
187
|
+
| prop | type | default |
|
|
188
|
+
| --- | --- | --- |
|
|
189
|
+
| `files` | `FileSystemTree` | – |
|
|
190
|
+
| `install` | `string \| string[] \| false` | `["npm", "install"]` |
|
|
191
|
+
| `run` | `string \| string[]` | – |
|
|
192
|
+
| `boot` | `BootOptions` | `{}` |
|
|
193
|
+
| `instanceKey` | `string` | enclosing provider's, else `"default"` |
|
|
194
|
+
| `autoBoot` | `boolean` | `true` |
|
|
195
|
+
| `onReady` | `(vivari: VivariInstance) => void` | – |
|
|
196
|
+
| `onServerReady` | `(port, url) => void` | – |
|
|
197
|
+
| `onOutput` | `(chunk: string) => void` | – |
|
|
198
|
+
| `onError` | `(failure: VivariFailure) => void` | – |
|
|
199
|
+
| `showPreview` | `boolean` | `true` |
|
|
200
|
+
| `previewPort` | `number` | first port that listens |
|
|
201
|
+
| `previewPath` | `string` | `"/"` |
|
|
202
|
+
| `fallback` | `ReactNode` | `null` |
|
|
203
|
+
| `renderError` | `(failure: VivariFailure) => ReactNode` | built-in notice |
|
|
204
|
+
| `children` | `ReactNode \| (state) => ReactNode` | – |
|
|
205
|
+
|
|
206
|
+
`VivariFailure` is `{ phase, error }` where `phase` is `"unsupported"`,
|
|
207
|
+
`"boot"`, `"mount"`, `"install"` or `"run"`. The `"unsupported"` variant also
|
|
208
|
+
carries a `reason`.
|
|
209
|
+
|
|
210
|
+
Commands given as a string are split on whitespace; use the array form for
|
|
211
|
+
anything containing quotes.
|
|
212
|
+
|
|
213
|
+
### `<VivariPreview>`
|
|
214
|
+
|
|
215
|
+
The preview `<iframe>` on its own. It renders `fallback` until the in-VM server
|
|
216
|
+
is genuinely serving, then points the frame at it and calls
|
|
217
|
+
`vivari.attachPreview(frame)` to run the inbound half of the HMR/SSE tunnel that
|
|
218
|
+
Vite's client needs — without that, Vite HMR never gets past
|
|
219
|
+
`[vite] connecting…`.
|
|
220
|
+
|
|
221
|
+
| prop | type | default |
|
|
222
|
+
| --- | --- | --- |
|
|
223
|
+
| `port` | `number` | first server that serves |
|
|
224
|
+
| `path` | `string` | `"/"` |
|
|
225
|
+
| `vivari` | `VivariInstance \| null` | nearest provider's |
|
|
226
|
+
| `reloadKey` | `string \| number` | – |
|
|
227
|
+
| `onServerReady` | `(port, url) => void` | – |
|
|
228
|
+
| `fallback` | `ReactNode` | – |
|
|
229
|
+
|
|
230
|
+
Any other prop (`className`, `style`, `title`, `allow`, `sandbox`, …) goes
|
|
231
|
+
straight to the `<iframe>`, and `ref` forwards to the element.
|
|
232
|
+
|
|
233
|
+
It follows core's `server-ready` event, which fires when the kernel has
|
|
234
|
+
confirmed the port is answering — not on the raw `listen`, which a dev server
|
|
235
|
+
emits several times while it rebinds. Each `server-ready` re-points the frame,
|
|
236
|
+
so a dev server that dies and restarts reconnects rather than being ignored.
|
|
237
|
+
The flip side is that a `<VivariPreview>` mounted *after* a server started
|
|
238
|
+
missed the event: keep it mounted, or bump `reloadKey`.
|
|
239
|
+
|
|
240
|
+
Change `reloadKey` to reload the frame without remounting it — remounting would
|
|
241
|
+
tear down the HMR socket.
|
|
242
|
+
|
|
243
|
+
### `useVivari(options?)`
|
|
244
|
+
|
|
245
|
+
```tsx
|
|
246
|
+
const { status, vivari, error, boot, restart } = useVivari();
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
`status` is a discriminated union — `"idle" | "booting" | "ready" | "error" |
|
|
250
|
+
"unsupported"` — so `status === "ready"` narrows `vivari` to a non-null instance
|
|
251
|
+
and no defensive null check is needed:
|
|
252
|
+
|
|
253
|
+
```tsx
|
|
254
|
+
const state = useVivari();
|
|
255
|
+
if (state.status === "unsupported") return <FixYourHeaders reason={state.reason} />;
|
|
256
|
+
if (state.status !== "ready") return <p>{state.status}…</p>;
|
|
257
|
+
await state.vivari.spawn("node", ["-e", "console.log(2 + 2)"]); // no `!`
|
|
258
|
+
```
|
|
259
|
+
|
|
260
|
+
Options are `BootOptions` plus:
|
|
261
|
+
|
|
262
|
+
| option | type | default |
|
|
263
|
+
| --- | --- | --- |
|
|
264
|
+
| `instanceKey` | `string` | `"default"` |
|
|
265
|
+
| `autoBoot` | `boolean` | `true` |
|
|
266
|
+
|
|
267
|
+
Instances are **shared and ref-counted per `instanceKey`**. Two components with
|
|
268
|
+
the same key share one kernel, and React StrictMode's double-mount boots exactly
|
|
269
|
+
once. Boot options are read when the boot starts; later changes apply on the
|
|
270
|
+
next `restart()`.
|
|
271
|
+
|
|
272
|
+
Pass `autoBoot: false` to defer the boot until a user gesture — worth doing for a
|
|
273
|
+
playground below the fold, so a visitor who never scrolls never pays for a
|
|
274
|
+
kernel:
|
|
275
|
+
|
|
276
|
+
```tsx
|
|
277
|
+
const { status, boot } = useVivari({ autoBoot: false });
|
|
278
|
+
return status === "idle" ? <button onClick={boot}>Run this example</button> : …;
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
### `<VivariProvider>` / `useVivariContext()` / `useVivariInstance()`
|
|
282
|
+
|
|
283
|
+
`<VivariProvider>` takes the same options as `useVivari` and publishes the result
|
|
284
|
+
to the subtree. `useVivariContext()` reads it and throws outside a provider;
|
|
285
|
+
`useVivariContext({ optional: true })` returns `null` instead.
|
|
286
|
+
`useVivariInstance(explicit?)` resolves the instance a hook should act on and is
|
|
287
|
+
what `useSpawn` / `useVivariFile` / `<VivariPreview>` use internally.
|
|
288
|
+
|
|
289
|
+
### `useSpawn(command, args?, options?)`
|
|
290
|
+
|
|
291
|
+
```tsx
|
|
292
|
+
const { run, kill, write, status, output, exitCode, process } = useSpawn("node", ["index.js"]);
|
|
293
|
+
```
|
|
294
|
+
|
|
295
|
+
`command` and `args` are read when a run starts, so an inline array is fine and
|
|
296
|
+
changing them does not restart anything — call `run()` for that. Any running
|
|
297
|
+
process is killed on unmount.
|
|
298
|
+
|
|
299
|
+
| option | type | default |
|
|
300
|
+
| --- | --- | --- |
|
|
301
|
+
| `auto` | `boolean` | `false` |
|
|
302
|
+
| `collect` | `boolean` | `false` |
|
|
303
|
+
| `onOutput` | `(chunk: string) => void` | – |
|
|
304
|
+
| `onExit` | `(code: number) => void` | – |
|
|
305
|
+
| `onError` | `(error: Error) => void` | – |
|
|
306
|
+
| `vivari` | `VivariInstance \| null` | nearest provider's |
|
|
307
|
+
| `cwd`, `env` | from `SpawnOptions` | – |
|
|
308
|
+
|
|
309
|
+
`collect` is off by default on purpose: `npm install` emits thousands of chunks
|
|
310
|
+
and a re-render per chunk is a perf disaster. Use `onOutput` (zero re-renders) to
|
|
311
|
+
drive a terminal, and turn `collect` on only when you want to render the log into
|
|
312
|
+
a `<pre>`.
|
|
313
|
+
|
|
314
|
+
`output` is the interleaved stdout+stderr stream, which is what a terminal
|
|
315
|
+
wants. To style them differently, read the separate `process.stdout` and
|
|
316
|
+
`process.stderr` streams off the returned `process` handle — reading either one
|
|
317
|
+
does not consume `output`.
|
|
318
|
+
|
|
319
|
+
#### xterm.js recipe
|
|
320
|
+
|
|
321
|
+
There is no `<VivariTerminal>` component, deliberately — xterm.js is a heavy,
|
|
322
|
+
opinionated dependency and every embedder wants different theming, addons and
|
|
323
|
+
fit behaviour. `useSpawn` reduces the glue to this:
|
|
324
|
+
|
|
325
|
+
```tsx
|
|
326
|
+
import { useEffect, useRef } from "react";
|
|
327
|
+
import { Terminal } from "@xterm/xterm";
|
|
328
|
+
import { FitAddon } from "@xterm/addon-fit";
|
|
329
|
+
import { useSpawn, useVivari } from "@vivari/react";
|
|
330
|
+
|
|
331
|
+
function NodeTerminal({ file }: { file: string }) {
|
|
332
|
+
const host = useRef<HTMLDivElement>(null);
|
|
333
|
+
const term = useRef<Terminal | null>(null);
|
|
334
|
+
|
|
335
|
+
// A terminal renders no preview, so it needs no Service Worker.
|
|
336
|
+
const { vivari, status: bootStatus } = useVivari({ serviceWorkerUrl: false });
|
|
337
|
+
|
|
338
|
+
const { run, write, status } = useSpawn("node", [file], {
|
|
339
|
+
vivari, // this component owns the instance
|
|
340
|
+
onOutput: (chunk) => term.current?.write(chunk),
|
|
341
|
+
onExit: (code) => term.current?.writeln(`\r\n[exited with code ${code}]`),
|
|
342
|
+
});
|
|
69
343
|
|
|
70
344
|
useEffect(() => {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
345
|
+
const t = new Terminal({ convertEol: true, cursorBlink: true });
|
|
346
|
+
const fit = new FitAddon();
|
|
347
|
+
t.loadAddon(fit);
|
|
348
|
+
if (host.current) t.open(host.current);
|
|
349
|
+
fit.fit();
|
|
350
|
+
term.current = t;
|
|
351
|
+
const onData = t.onData(write); // forward keystrokes to stdin
|
|
352
|
+
return () => { onData.dispose(); t.dispose(); };
|
|
353
|
+
}, [write]);
|
|
354
|
+
|
|
355
|
+
return (
|
|
356
|
+
<>
|
|
357
|
+
<button
|
|
358
|
+
onClick={() => run()}
|
|
359
|
+
disabled={bootStatus !== "ready" || status === "running"}
|
|
360
|
+
>
|
|
361
|
+
Run
|
|
362
|
+
</button>
|
|
363
|
+
<div ref={host} />
|
|
364
|
+
</>
|
|
365
|
+
);
|
|
80
366
|
}
|
|
81
367
|
```
|
|
82
368
|
|
|
83
|
-
|
|
84
|
-
|
|
369
|
+
There is no `<VivariProvider>` here, so this component boots its own kernel and
|
|
370
|
+
hands the instance to `useSpawn` through the `vivari` option. Without it the
|
|
371
|
+
hook has nothing to act on and every `run()` fails. Inside a provider, drop both
|
|
372
|
+
the `useVivari` call and the option and the hook resolves the instance itself.
|
|
373
|
+
Either way `vivari` is null until the boot lands, which is what `bootStatus`
|
|
374
|
+
gates the button on: `useSpawn` reads the instance when a run starts, not when
|
|
375
|
+
the hook is called.
|
|
376
|
+
|
|
377
|
+
`run`, `kill` and `write` keep a stable identity, so they are safe in dependency
|
|
378
|
+
arrays.
|
|
379
|
+
|
|
380
|
+
### `useVivariFile(path, options?)`
|
|
381
|
+
|
|
382
|
+
```tsx
|
|
383
|
+
const [source, setSource, { status, error, save, reload }] = useVivariFile("/src/App.jsx", {
|
|
384
|
+
debounce: 250,
|
|
385
|
+
});
|
|
386
|
+
```
|
|
387
|
+
|
|
388
|
+
Reads the file into React state and writes edits back with a debounce. `save()`
|
|
389
|
+
cancels the pending debounce and writes immediately (the Cmd+S path), and a
|
|
390
|
+
pending write is **flushed on unmount and before switching paths** — so the last
|
|
391
|
+
edit before navigating away isn't lost.
|
|
392
|
+
|
|
393
|
+
| option | type | default |
|
|
394
|
+
| --- | --- | --- |
|
|
395
|
+
| `debounce` | `number` (ms, `0` = write every change) | `250` |
|
|
396
|
+
| `initialContents` | `string` | – |
|
|
397
|
+
| `vivari` | `VivariInstance \| null` | nearest provider's |
|
|
398
|
+
| `onError` | `(error: Error) => void` | – |
|
|
399
|
+
|
|
400
|
+
Text only. For binary content use `vivari.fs.readFile(path)` with no encoding.
|
|
401
|
+
|
|
402
|
+
### `useVivariDir(path, options?)`
|
|
403
|
+
|
|
404
|
+
```tsx
|
|
405
|
+
const { entries, status, error, refresh } = useVivariDir("/src");
|
|
406
|
+
|
|
407
|
+
return (
|
|
408
|
+
<ul>
|
|
409
|
+
{entries.map((e) => (
|
|
410
|
+
<li key={e.name}>{e.isDirectory() ? `${e.name}/` : e.name}</li>
|
|
411
|
+
))}
|
|
412
|
+
</ul>
|
|
413
|
+
);
|
|
414
|
+
```
|
|
415
|
+
|
|
416
|
+
A directory listing that stays current as the VFS changes underneath it — the
|
|
417
|
+
file-tree half of an IDE, without hand-rolling a watcher.
|
|
418
|
+
|
|
419
|
+
| option | type | default |
|
|
420
|
+
| --- | --- | --- |
|
|
421
|
+
| `debounce` | `number` (ms, `0` = re-read on every event) | `100` |
|
|
422
|
+
| `vivari` | `VivariInstance \| null` | nearest provider's |
|
|
423
|
+
| `onError` | `(error: Error) => void` | – |
|
|
424
|
+
|
|
425
|
+
`entries` are `DirEnt`s (`name`, `isFile()`, `isDirectory()`). The watch is
|
|
426
|
+
non-recursive, since only changes directly inside `path` can alter its listing,
|
|
427
|
+
and re-reads are debounced because core reports every change with no coalescing
|
|
428
|
+
— one `npm install` fires thousands of events. A slow read can never overwrite a
|
|
429
|
+
newer one. Render one hook per expanded directory rather than one recursive
|
|
430
|
+
watch over the root.
|
|
431
|
+
|
|
432
|
+
### Re-exports
|
|
433
|
+
|
|
434
|
+
Every type on the wrapped surface is re-exported, so you never need a second
|
|
435
|
+
import from `@vivari/core` just to annotate a variable: `BootOptions`,
|
|
436
|
+
`DirEnt`, `DirectoryNode`, `Encoding`, `ErrorListener`, `ExportedFile`,
|
|
437
|
+
`ExportOptions`, `ExportResult`, `FileNode`, `FileSystemAPI`, `FileSystemTree`,
|
|
438
|
+
`FsChangeEvent`, `FsChangeKind`, `FsChangeListener`, `FsOperationOptions`,
|
|
439
|
+
`FSWatcher`, `KernelMessage`, `ListenerOptions`, `MountOptions`, `OutputStream`,
|
|
440
|
+
`PortKind`, `PortListener`, `ServerReadyListener`, `SpawnOptions`, `Stats`,
|
|
441
|
+
`Unsubscribe`, `VivariEventMap`, `VivariErrorCode`, `VivariFsErrorCode`,
|
|
442
|
+
`VivariListener`, `VivariProcess`, `VivariRuntimeErrorCode`, `WatchOptions`,
|
|
443
|
+
`VivariInstance`.
|
|
444
|
+
|
|
445
|
+
Plus four values: `isCrossOriginIsolated`, `resetVfs`, and the `VivariError` /
|
|
446
|
+
`VivariFsError` classes (so `instanceof` and `error.code` work without a second
|
|
447
|
+
import).
|
|
448
|
+
|
|
449
|
+
`KernelBridge` is deliberately not re-exported — it is core's escape hatch
|
|
450
|
+
(`vivari.internal`), and reaching for it should be an explicit `@vivari/core`
|
|
451
|
+
import rather than something these bindings normalise.
|
|
452
|
+
|
|
453
|
+
## A note on the name `Vivari`
|
|
454
|
+
|
|
455
|
+
`Vivari` in this package is the React **component**. The core **class** — what
|
|
456
|
+
`Vivari.boot()` returns and what every hook hands you — is re-exported as the
|
|
457
|
+
type `VivariInstance`, because in React you only ever see it in type position:
|
|
458
|
+
the hooks construct it for you.
|
|
459
|
+
|
|
460
|
+
To call `Vivari.boot()` yourself, import the class from `@vivari/core` directly.
|
|
461
|
+
It's a peer dependency, so you already have exactly one copy of it.
|
|
462
|
+
|
|
463
|
+
```tsx
|
|
464
|
+
import { Vivari } from "@vivari/react"; // the <Vivari> component
|
|
465
|
+
import type { VivariInstance } from "@vivari/react"; // the instance type
|
|
466
|
+
import { Vivari as VivariClass } from "@vivari/core"; // the class, if you need it
|
|
467
|
+
```
|
|
85
468
|
|
|
86
469
|
## License
|
|
87
470
|
|
|
88
|
-
MIT © Duc Trung Mai
|
|
471
|
+
MIT © Duc Trung Mai
|
package/dist/Vivari.d.ts
CHANGED
|
@@ -1,7 +1,24 @@
|
|
|
1
|
-
import { type
|
|
1
|
+
import { type IframeHTMLAttributes, type ReactNode } from "react";
|
|
2
2
|
import type { BootOptions, FileSystemTree, Vivari as VivariInstance } from "@vivari/core";
|
|
3
|
-
import { type
|
|
4
|
-
|
|
3
|
+
import { type UseVivariResult, type VivariUnsupportedReason } from "./useVivari";
|
|
4
|
+
/** Which step of the embed's lifecycle failed. */
|
|
5
|
+
export type VivariPhase = "unsupported" | "boot" | "mount" | "install" | "run";
|
|
6
|
+
export type VivariFailure = {
|
|
7
|
+
phase: "unsupported";
|
|
8
|
+
error: Error;
|
|
9
|
+
reason: VivariUnsupportedReason;
|
|
10
|
+
} | {
|
|
11
|
+
phase: "boot" | "mount" | "install" | "run";
|
|
12
|
+
error: Error;
|
|
13
|
+
reason?: undefined;
|
|
14
|
+
};
|
|
15
|
+
export interface VivariProps extends Omit<IframeHTMLAttributes<HTMLIFrameElement>, "src" | "children" | "onError"> {
|
|
16
|
+
/** Boot options for the underlying kernel. */
|
|
17
|
+
boot?: BootOptions;
|
|
18
|
+
/** Share a kernel with the rest of the tree. Defaults to the enclosing provider's. */
|
|
19
|
+
instanceKey?: string;
|
|
20
|
+
/** `false` defers the boot until the render prop's `boot()` is called. */
|
|
21
|
+
autoBoot?: boolean;
|
|
5
22
|
/** Files to mount into the VM before running commands. */
|
|
6
23
|
files?: FileSystemTree;
|
|
7
24
|
/** Install step. Default `["npm", "install"]`; pass `false` to skip. */
|
|
@@ -10,25 +27,32 @@ export interface VivariProps extends BootOptions {
|
|
|
10
27
|
run?: string | string[];
|
|
11
28
|
/** Called once the instance is booted and files are mounted. */
|
|
12
29
|
onReady?: (vivari: VivariInstance) => void;
|
|
13
|
-
/** Called when
|
|
30
|
+
/** Called when a server starts listening inside the VM (the raw kernel event). */
|
|
14
31
|
onServerReady?: (port: number, url: string) => void;
|
|
15
32
|
/** Called for each stdout/stderr chunk of the install + run commands. */
|
|
16
33
|
onOutput?: (chunk: string) => void;
|
|
17
|
-
/**
|
|
34
|
+
/** Called for every failure, at any phase. Nothing is swallowed. */
|
|
35
|
+
onError?: (failure: VivariFailure) => void;
|
|
36
|
+
/** Render the preview `<iframe>`. Default `true`. */
|
|
18
37
|
showPreview?: boolean;
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
/**
|
|
38
|
+
/** Preview a specific port instead of the first one that listens. */
|
|
39
|
+
previewPort?: number;
|
|
40
|
+
/** Path within the previewed server. Default `/`. */
|
|
41
|
+
previewPath?: string;
|
|
42
|
+
/** Shown while booting / installing / before a preview URL exists. */
|
|
22
43
|
fallback?: ReactNode;
|
|
44
|
+
/** Replace the built-in failure UI. */
|
|
45
|
+
renderError?: (failure: VivariFailure) => ReactNode;
|
|
46
|
+
/** Take over rendering. A function receives the live boot state. */
|
|
47
|
+
children?: ReactNode | ((state: UseVivariResult) => ReactNode);
|
|
23
48
|
}
|
|
24
49
|
/**
|
|
25
|
-
*
|
|
26
|
-
*
|
|
50
|
+
* Boots Vivari, mounts `files`, runs `install` then `run`, and renders the
|
|
51
|
+
* resulting dev-server preview in an `<iframe>`.
|
|
27
52
|
*
|
|
28
53
|
* <Vivari files={tree} run="npm run dev" style={{ height: 480 }} />
|
|
29
54
|
*
|
|
30
55
|
* The host page must be cross-origin isolated (COOP + COEP).
|
|
31
56
|
*/
|
|
32
|
-
export declare
|
|
33
|
-
export type { VivariStatus };
|
|
57
|
+
export declare const Vivari: import("react").ForwardRefExoticComponent<VivariProps & import("react").RefAttributes<HTMLIFrameElement>>;
|
|
34
58
|
//# sourceMappingURL=Vivari.d.ts.map
|
package/dist/Vivari.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Vivari.d.ts","sourceRoot":"","sources":["../src/Vivari.tsx"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"Vivari.d.ts","sourceRoot":"","sources":["../src/Vivari.tsx"],"names":[],"mappings":"AASA,OAAO,EAML,KAAK,oBAAoB,EACzB,KAAK,SAAS,EACf,MAAM,OAAO,CAAC;AACf,OAAO,KAAK,EACV,WAAW,EACX,cAAc,EACd,MAAM,IAAI,cAAc,EAEzB,MAAM,cAAc,CAAC;AAEtB,OAAO,EAAa,KAAK,eAAe,EAAE,KAAK,uBAAuB,EAAE,MAAM,aAAa,CAAC;AAG5F,kDAAkD;AAClD,MAAM,MAAM,WAAW,GAAG,aAAa,GAAG,MAAM,GAAG,OAAO,GAAG,SAAS,GAAG,KAAK,CAAC;AAE/E,MAAM,MAAM,aAAa,GACrB;IAAE,KAAK,EAAE,aAAa,CAAC;IAAC,KAAK,EAAE,KAAK,CAAC;IAAC,MAAM,EAAE,uBAAuB,CAAA;CAAE,GACvE;IAAE,KAAK,EAAE,MAAM,GAAG,OAAO,GAAG,SAAS,GAAG,KAAK,CAAC;IAAC,KAAK,EAAE,KAAK,CAAC;IAAC,MAAM,CAAC,EAAE,SAAS,CAAA;CAAE,CAAC;AAEtF,MAAM,WAAW,WAGf,SAAQ,IAAI,CAAC,oBAAoB,CAAC,iBAAiB,CAAC,EAAE,KAAK,GAAG,UAAU,GAAG,SAAS,CAAC;IACrF,8CAA8C;IAC9C,IAAI,CAAC,EAAE,WAAW,CAAC;IACnB,sFAAsF;IACtF,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,0EAA0E;IAC1E,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,0DAA0D;IAC1D,KAAK,CAAC,EAAE,cAAc,CAAC;IACvB,wEAAwE;IACxE,OAAO,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,KAAK,CAAC;IACpC,qFAAqF;IACrF,GAAG,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IACxB,gEAAgE;IAChE,OAAO,CAAC,EAAE,CAAC,MAAM,EAAE,cAAc,KAAK,IAAI,CAAC;IAC3C,kFAAkF;IAClF,aAAa,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,KAAK,IAAI,CAAC;IACpD,yEAAyE;IACzE,QAAQ,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;IACnC,oEAAoE;IACpE,OAAO,CAAC,EAAE,CAAC,OAAO,EAAE,aAAa,KAAK,IAAI,CAAC;IAC3C,qDAAqD;IACrD,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB,qEAAqE;IACrE,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,qDAAqD;IACrD,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,sEAAsE;IACtE,QAAQ,CAAC,EAAE,SAAS,CAAC;IACrB,uCAAuC;IACvC,WAAW,CAAC,EAAE,CAAC,OAAO,EAAE,aAAa,KAAK,SAAS,CAAC;IACpD,oEAAoE;IACpE,QAAQ,CAAC,EAAE,SAAS,GAAG,CAAC,CAAC,KAAK,EAAE,eAAe,KAAK,SAAS,CAAC,CAAC;CAChE;AAsCD;;;;;;;GAOG;AACH,eAAO,MAAM,MAAM,2GAyKjB,CAAC"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { type IframeHTMLAttributes, type ReactNode } from "react";
|
|
2
|
+
import type { Vivari as VivariInstance } from "@vivari/core";
|
|
3
|
+
export interface VivariPreviewProps extends Omit<IframeHTMLAttributes<HTMLIFrameElement>, "src" | "children"> {
|
|
4
|
+
/** The in-VM port to show. Omit to follow the first server that starts serving. */
|
|
5
|
+
port?: number;
|
|
6
|
+
/** Path within the dev server. Default `/`. */
|
|
7
|
+
path?: string;
|
|
8
|
+
/** Act on this instance instead of the nearest `<VivariProvider>`'s. */
|
|
9
|
+
vivari?: VivariInstance | null;
|
|
10
|
+
/** Change this to reload the frame (a "Reload preview" button). */
|
|
11
|
+
reloadKey?: string | number;
|
|
12
|
+
/** Fired each time the frame is pointed at a serving URL. */
|
|
13
|
+
onServerReady?: (port: number, url: string) => void;
|
|
14
|
+
/** Rendered until the first server is serving. */
|
|
15
|
+
fallback?: ReactNode;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Render an in-VM server's preview.
|
|
19
|
+
*
|
|
20
|
+
* <VivariPreview port={5173} style={{ width: "100%", height: 480, border: 0 }} />
|
|
21
|
+
*
|
|
22
|
+
* Waits for `server-ready`, so a component mounted before the server starts just
|
|
23
|
+
* shows `fallback` until it does. One mounted *after* a server started won't see
|
|
24
|
+
* that event — give it a `reloadKey` bump, or keep it mounted.
|
|
25
|
+
*/
|
|
26
|
+
export declare const VivariPreview: import("react").ForwardRefExoticComponent<VivariPreviewProps & import("react").RefAttributes<HTMLIFrameElement>>;
|
|
27
|
+
//# sourceMappingURL=VivariPreview.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"VivariPreview.d.ts","sourceRoot":"","sources":["../src/VivariPreview.tsx"],"names":[],"mappings":"AAcA,OAAO,EAML,KAAK,oBAAoB,EACzB,KAAK,SAAS,EACf,MAAM,OAAO,CAAC;AACf,OAAO,KAAK,EAAE,MAAM,IAAI,cAAc,EAAE,MAAM,cAAc,CAAC;AAG7D,MAAM,WAAW,kBACf,SAAQ,IAAI,CAAC,oBAAoB,CAAC,iBAAiB,CAAC,EAAE,KAAK,GAAG,UAAU,CAAC;IACzE,mFAAmF;IACnF,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,+CAA+C;IAC/C,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,wEAAwE;IACxE,MAAM,CAAC,EAAE,cAAc,GAAG,IAAI,CAAC;IAC/B,mEAAmE;IACnE,SAAS,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;IAC5B,6DAA6D;IAC7D,aAAa,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,KAAK,IAAI,CAAC;IACpD,kDAAkD;IAClD,QAAQ,CAAC,EAAE,SAAS,CAAC;CACtB;AASD;;;;;;;;GAQG;AACH,eAAO,MAAM,aAAa,kHA0EzB,CAAC"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { type ReactNode } from "react";
|
|
2
|
+
import type { Vivari as VivariInstance } from "@vivari/core";
|
|
3
|
+
import { type UseVivariOptions, type UseVivariResult } from "./useVivari";
|
|
4
|
+
export interface VivariProviderProps extends UseVivariOptions {
|
|
5
|
+
children?: ReactNode;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Boot an instance and publish it to the subtree. Renders no DOM of its own.
|
|
9
|
+
*
|
|
10
|
+
* <VivariProvider compress={false}>
|
|
11
|
+
* <Editor />
|
|
12
|
+
* <VivariPreview port={5173} />
|
|
13
|
+
* </VivariProvider>
|
|
14
|
+
*/
|
|
15
|
+
export declare function VivariProvider({ children, ...options }: VivariProviderProps): import("react").JSX.Element;
|
|
16
|
+
/** Read the nearest provider's state. Throws when there isn't one. */
|
|
17
|
+
export declare function useVivariContext(): UseVivariResult;
|
|
18
|
+
/** Read the nearest provider's state, or `null` when there isn't one. */
|
|
19
|
+
export declare function useVivariContext(options: {
|
|
20
|
+
optional: true;
|
|
21
|
+
}): UseVivariResult | null;
|
|
22
|
+
/**
|
|
23
|
+
* Resolve the instance a hook should act on: an explicitly passed one, else the
|
|
24
|
+
* nearest provider's. Returns `null` until the VM is ready, so hooks can always
|
|
25
|
+
* be called unconditionally and simply idle until there is something to act on.
|
|
26
|
+
*/
|
|
27
|
+
export declare function useVivariInstance(explicit?: VivariInstance | null): VivariInstance | null;
|
|
28
|
+
//# sourceMappingURL=context.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../src/context.tsx"],"names":[],"mappings":"AAUA,OAAO,EAA6B,KAAK,SAAS,EAAE,MAAM,OAAO,CAAC;AAClE,OAAO,KAAK,EAAE,MAAM,IAAI,cAAc,EAAE,MAAM,cAAc,CAAC;AAC7D,OAAO,EAAa,KAAK,gBAAgB,EAAE,KAAK,eAAe,EAAE,MAAM,aAAa,CAAC;AAIrF,MAAM,WAAW,mBAAoB,SAAQ,gBAAgB;IAC3D,QAAQ,CAAC,EAAE,SAAS,CAAC;CACtB;AAED;;;;;;;GAOG;AACH,wBAAgB,cAAc,CAAC,EAAE,QAAQ,EAAE,GAAG,OAAO,EAAE,EAAE,mBAAmB,+BAK3E;AAED,sEAAsE;AACtE,wBAAgB,gBAAgB,IAAI,eAAe,CAAC;AACpD,yEAAyE;AACzE,wBAAgB,gBAAgB,CAAC,OAAO,EAAE;IAAE,QAAQ,EAAE,IAAI,CAAA;CAAE,GAAG,eAAe,GAAG,IAAI,CAAC;AAStF;;;;GAIG;AACH,wBAAgB,iBAAiB,CAAC,QAAQ,CAAC,EAAE,cAAc,GAAG,IAAI,GAAG,cAAc,GAAG,IAAI,CAGzF"}
|