@rstreamlabs/react 2.1.2 → 2.1.4

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 CHANGED
@@ -4,8 +4,8 @@ rstream connects services and devices through outbound tunnels and exposes their
4
4
 
5
5
  Use this package in browser applications and dashboards that need live tunnel
6
6
  state or embedded WebTTY sessions. It builds on
7
- [`@rstreamlabs/tunnels`](../tunnels/README.md) for watch streams and
8
- [`@rstreamlabs/webtty`](../webtty/README.md) for the browser-side WebTTY
7
+ [`@rstreamlabs/tunnels`](https://github.com/rstreamlabs/rstream-js/blob/main/packages/tunnels/README.md) for watch streams and
8
+ [`@rstreamlabs/webtty`](https://github.com/rstreamlabs/rstream-js/blob/main/packages/webtty/README.md) for the browser-side WebTTY
9
9
  protocol.
10
10
 
11
11
  ## Install
@@ -27,92 +27,75 @@ The package requires React 19.2.8 or newer within React 19. It uses the applicat
27
27
 
28
28
  ## Live Tunnel State
29
29
 
30
- `useRstream()` wraps the engine watch stream and keeps tunnel/client arrays in
31
- React state.
30
+ Use [the browser token route](https://github.com/rstreamlabs/rstream-js/blob/main/docs/browser/000-watch.md#connect-a-browser)
31
+ to get an authorized Engine and short-lived watch token. For this example,
32
+ install `zod` alongside the React SDK. Pass in the Engine returned by the
33
+ initial bootstrap. Use the token callback for subsequent connections.
32
34
 
33
35
  ```tsx
34
36
  "use client";
35
37
 
36
38
  import { useRstream } from "@rstreamlabs/react/hooks";
39
+ import { useCallback } from "react";
40
+ import { z } from "zod";
37
41
 
38
- async function createWatchToken() {
39
- const response = await fetch("/api/rstream/watch-token", {
40
- method: "POST",
42
+ const credentials = z.object({ token: z.string(), engine: z.string() });
43
+
44
+ async function watchToken(engine: string) {
45
+ const response = await fetch("/api/rstream/watch", {
46
+ credentials: "same-origin",
41
47
  cache: "no-store",
42
48
  });
43
- if (!response.ok) {
44
- throw new Error("Failed to create watch token.");
45
- }
46
- const { token } = await response.json();
47
- return token;
49
+ if (!response.ok) throw new Error("Watch access was refused");
50
+ const issued = credentials.parse(await response.json());
51
+ if (issued.engine !== engine) throw new Error("The selected Engine changed");
52
+ return issued.token;
48
53
  }
49
54
 
50
- export function TunnelInventory() {
55
+ export function TunnelInventory({ engine }: { engine: string }) {
56
+ const auth = useCallback(() => watchToken(engine), [engine]);
51
57
  const { state, error, tunnels } = useRstream({
52
- auth: createWatchToken,
53
- engine: "project-endpoint.cluster.example.rstream.test:443",
58
+ auth,
59
+ engine,
54
60
  transport: "sse",
55
61
  });
56
62
 
57
63
  if (error) return <p>{error.message}</p>;
58
64
  return (
59
65
  <ul data-state={state}>
60
- {tunnels
61
- .filter((tunnel) => tunnel.id !== undefined)
62
- .map((tunnel) => (
63
- <li key={tunnel.id}>{tunnel.name ?? tunnel.id}</li>
64
- ))}
66
+ {tunnels.flatMap((tunnel) =>
67
+ tunnel.id ? [<li key={tunnel.id}>{tunnel.name ?? tunnel.id}</li>] : [],
68
+ )}
65
69
  </ul>
66
70
  );
67
71
  }
68
72
  ```
69
73
 
70
- The hook reconnects automatically and clears state when authentication is not
71
- configured.
72
-
73
- The returned arrays use `ListedClient` and `ListedTunnel`, whose fields may
74
- be absent under field selection. Select `id` for keyed, actionable rows, or
75
- render anonymous projections without assuming an identifier exists. A denied
76
- category is empty and does not prevent the other category from updating.
77
- Every incoming snapshot replaces both arrays. Previously visible data is
78
- hidden while reconnecting or switching authentication.
74
+ The hook reconnects and obtains fresh credentials. Use a stable callback
75
+ for the same Engine and remount the view when the selected project changes.
76
+ Watch items are field projections. Select `id` for rows the viewer can
77
+ act on. Each new snapshot replaces the current view.
79
78
 
80
79
  ## Shared Provider
81
80
 
82
- Use `RstreamProvider` when several components need the same watch stream.
81
+ Use `RstreamProvider` when several components share one subscription.
82
+ Pass the same options and read the view with `useRstreamContext()`.
83
83
 
84
84
  ```tsx
85
- "use client";
86
-
87
85
  import { RstreamProvider } from "@rstreamlabs/react/providers";
88
86
  import { useRstreamContext } from "@rstreamlabs/react/providers";
87
+ import { useCallback } from "react";
89
88
 
90
- async function createWatchToken() {
91
- const response = await fetch("/api/rstream/watch-token", {
92
- method: "POST",
93
- cache: "no-store",
94
- });
95
- if (!response.ok) {
96
- throw new Error("Failed to create watch token.");
97
- }
98
- const { token } = await response.json();
99
- return token;
100
- }
101
-
102
- function TunnelsTable() {
89
+ function TunnelCount() {
103
90
  const { tunnels } = useRstreamContext();
104
- return <pre>{JSON.stringify(tunnels, null, 2)}</pre>;
91
+ return <p>{tunnels.length} tunnels</p>;
105
92
  }
106
93
 
107
- export function Dashboard() {
94
+ export function Dashboard({ engine }: { engine: string }) {
95
+ const auth = useCallback(() => watchToken(engine), [engine]);
108
96
  return (
109
- <RstreamProvider
110
- options={{
111
- auth: createWatchToken,
112
- engine: "project-endpoint.cluster.example.rstream.test:443",
113
- }}
114
- >
115
- <TunnelsTable />
97
+ <RstreamProvider options={{ engine, auth }}>
98
+ <TunnelCount />
116
99
  </RstreamProvider>
117
100
  );
118
101
  }
@@ -1,5 +1,5 @@
1
1
  // src/hooks/use-rstream.ts
2
- import { Watch } from "@rstreamlabs/tunnels";
2
+ import { Watch } from "@rstreamlabs/tunnels/watch";
3
3
  import * as React from "react";
4
4
  function hasAuth(options) {
5
5
  return !!options && !!options.auth;
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  useRstream
3
- } from "./chunk-AHLVBJB3.mjs";
3
+ } from "./chunk-MAO57HME.mjs";
4
4
 
5
5
  // src/providers/rstream.tsx
6
6
  import * as React from "react";
@@ -35,7 +35,7 @@ __export(hooks_exports, {
35
35
  module.exports = __toCommonJS(hooks_exports);
36
36
 
37
37
  // src/hooks/use-rstream.ts
38
- var import_tunnels = require("@rstreamlabs/tunnels");
38
+ var import_watch = require("@rstreamlabs/tunnels/watch");
39
39
  var React = __toESM(require("react"));
40
40
  function hasAuth(options) {
41
41
  return !!options && !!options.auth;
@@ -100,7 +100,7 @@ function useRstream(options) {
100
100
  const watchOptions = getWatchOptions();
101
101
  if (!watchOptions) return;
102
102
  setState("connecting");
103
- runtime.watch = new import_tunnels.Watch(watchOptions, {
103
+ runtime.watch = new import_watch.Watch(watchOptions, {
104
104
  onEvent: (event) => {
105
105
  if (!runtime.active) return;
106
106
  if (event.type === "state.initial") {
@@ -1,7 +1,7 @@
1
1
  import "../chunk-2JFL7TS5.mjs";
2
2
  import {
3
3
  useRstream
4
- } from "../chunk-AHLVBJB3.mjs";
4
+ } from "../chunk-MAO57HME.mjs";
5
5
  export {
6
6
  useRstream
7
7
  };
package/dist/index.js CHANGED
@@ -280,7 +280,7 @@ Process exited with code ${code}.`);
280
280
  }
281
281
 
282
282
  // src/hooks/use-rstream.ts
283
- var import_tunnels = require("@rstreamlabs/tunnels");
283
+ var import_watch = require("@rstreamlabs/tunnels/watch");
284
284
  var React2 = __toESM(require("react"));
285
285
  function hasAuth(options) {
286
286
  return !!options && !!options.auth;
@@ -345,7 +345,7 @@ function useRstream(options) {
345
345
  const watchOptions = getWatchOptions();
346
346
  if (!watchOptions) return;
347
347
  setState("connecting");
348
- runtime.watch = new import_tunnels.Watch(watchOptions, {
348
+ runtime.watch = new import_watch.Watch(watchOptions, {
349
349
  onEvent: (event) => {
350
350
  if (!runtime.active) return;
351
351
  if (event.type === "state.initial") {
package/dist/index.mjs CHANGED
@@ -5,10 +5,10 @@ import "./chunk-2JFL7TS5.mjs";
5
5
  import {
6
6
  RstreamProvider,
7
7
  useRstreamContext
8
- } from "./chunk-L4AWANNU.mjs";
8
+ } from "./chunk-TUNL7LFU.mjs";
9
9
  import {
10
10
  useRstream
11
- } from "./chunk-AHLVBJB3.mjs";
11
+ } from "./chunk-MAO57HME.mjs";
12
12
  export {
13
13
  RstreamProvider,
14
14
  WebTTYTerminal,
@@ -36,7 +36,7 @@ __export(providers_exports, {
36
36
  module.exports = __toCommonJS(providers_exports);
37
37
 
38
38
  // src/hooks/use-rstream.ts
39
- var import_tunnels = require("@rstreamlabs/tunnels");
39
+ var import_watch = require("@rstreamlabs/tunnels/watch");
40
40
  var React = __toESM(require("react"));
41
41
  function hasAuth(options) {
42
42
  return !!options && !!options.auth;
@@ -101,7 +101,7 @@ function useRstream(options) {
101
101
  const watchOptions = getWatchOptions();
102
102
  if (!watchOptions) return;
103
103
  setState("connecting");
104
- runtime.watch = new import_tunnels.Watch(watchOptions, {
104
+ runtime.watch = new import_watch.Watch(watchOptions, {
105
105
  onEvent: (event) => {
106
106
  if (!runtime.active) return;
107
107
  if (event.type === "state.initial") {
@@ -1,8 +1,8 @@
1
1
  import {
2
2
  RstreamProvider,
3
3
  useRstreamContext
4
- } from "../chunk-L4AWANNU.mjs";
5
- import "../chunk-AHLVBJB3.mjs";
4
+ } from "../chunk-TUNL7LFU.mjs";
5
+ import "../chunk-MAO57HME.mjs";
6
6
  export {
7
7
  RstreamProvider,
8
8
  useRstreamContext
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rstreamlabs/react",
3
- "version": "2.1.2",
3
+ "version": "2.1.4",
4
4
  "description": "React hooks and components for building rstream-enabled UIs.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -58,9 +58,9 @@
58
58
  "react-dom": "^19.3.1"
59
59
  },
60
60
  "dependencies": {
61
- "@rstreamlabs/rstream": "3.12.0",
62
- "@rstreamlabs/tunnels": "4.1.0",
63
- "@rstreamlabs/webtty": "1.12.2",
61
+ "@rstreamlabs/rstream": "3.12.2",
62
+ "@rstreamlabs/tunnels": "4.1.2",
63
+ "@rstreamlabs/webtty": "1.12.3",
64
64
  "@xterm/addon-fit": "^0.11.0",
65
65
  "@xterm/addon-unicode11": "^0.9.0",
66
66
  "@xterm/addon-web-links": "^0.12.0",