@rstreamlabs/react 1.7.5 → 1.7.6

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.
@@ -36,38 +36,73 @@ __export(providers_exports, {
36
36
  module.exports = __toCommonJS(providers_exports);
37
37
 
38
38
  // src/hooks/use-rstream.ts
39
- var import_rstream = require("@rstreamlabs/rstream");
39
+ var import_tunnels = require("@rstreamlabs/tunnels");
40
40
  var React = __toESM(require("react"));
41
41
  function hasAuth(options) {
42
42
  return !!options && !!options.auth;
43
43
  }
44
+ function credentialsKey(credentials) {
45
+ if (!credentials) {
46
+ return "";
47
+ }
48
+ if ("token" in credentials) {
49
+ return `token:${credentials.token}`;
50
+ }
51
+ return `client:${credentials.clientId}:${credentials.clientSecret}`;
52
+ }
53
+ function watchConnectionKey(options) {
54
+ if (!hasAuth(options)) {
55
+ return "disabled";
56
+ }
57
+ return JSON.stringify({
58
+ apiUrl: options.apiUrl ?? null,
59
+ auth: typeof options.auth === "function" ? "function" : `token:${options.auth}`,
60
+ controlPlaneCredentials: credentialsKey(options.controlPlaneCredentials),
61
+ engine: options.engine ?? null,
62
+ projectEndpoint: options.projectEndpoint ?? null,
63
+ transport: options.transport ?? "sse"
64
+ });
65
+ }
44
66
  function useRstream(options) {
45
67
  const [state, setState] = React.useState("disconnected");
46
68
  const [error, setError] = React.useState(null);
47
69
  const { reconnectTimeout = 1e3, errorTimeout = 5e3 } = options || {};
48
70
  const [clients, setClients] = React.useState([]);
49
71
  const [tunnels, setTunnels] = React.useState([]);
72
+ const authEnabled = hasAuth(options);
73
+ const connectionKey = watchConnectionKey(options);
74
+ const optionsRef = React.useRef(options);
75
+ React.useEffect(() => {
76
+ optionsRef.current = options;
77
+ }, [options]);
78
+ const getWatchOptions = React.useEffectEvent(() => {
79
+ const current = optionsRef.current;
80
+ return hasAuth(current) ? current : void 0;
81
+ });
50
82
  React.useEffect(() => {
51
- if (!hasAuth(options)) return;
52
- let active = true;
53
- let watch = null;
54
- let timeout = null;
83
+ if (!authEnabled) return;
84
+ const runtime = {
85
+ active: true,
86
+ timeout: null,
87
+ watch: null
88
+ };
55
89
  const schedule = () => {
56
- if (!active) return;
57
- if (timeout) return;
90
+ if (!runtime.active) return;
91
+ if (runtime.timeout) return;
58
92
  setState("connecting");
59
- timeout = setTimeout(() => {
60
- if (!active) return;
61
- timeout = null;
93
+ runtime.timeout = setTimeout(() => {
94
+ if (!runtime.active) return;
95
+ runtime.timeout = null;
62
96
  run();
63
97
  }, reconnectTimeout);
64
98
  };
65
99
  const run = async () => {
66
- if (!hasAuth(options)) return;
100
+ const watchOptions = getWatchOptions();
101
+ if (!watchOptions) return;
67
102
  setState("connecting");
68
- watch = new import_rstream.Watch(options, {
103
+ runtime.watch = new import_tunnels.Watch(watchOptions, {
69
104
  onEvent: (event) => {
70
- if (!active) return;
105
+ if (!runtime.active) return;
71
106
  if (event.type === "state.initial") {
72
107
  setClients(event.object.clients);
73
108
  setTunnels(event.object.tunnels);
@@ -110,36 +145,36 @@ function useRstream(options) {
110
145
  }
111
146
  },
112
147
  onConnect: () => {
113
- if (!active) return;
148
+ if (!runtime.active) return;
114
149
  setState("connected");
115
150
  },
116
151
  onClose: () => {
117
- if (!active) return;
118
- watch = null;
152
+ if (!runtime.active) return;
153
+ runtime.watch = null;
119
154
  schedule();
120
155
  }
121
156
  });
122
157
  try {
123
- await watch.connect();
158
+ await runtime.watch.connect();
124
159
  } catch {
125
160
  schedule();
126
161
  }
127
162
  };
128
163
  run();
129
164
  return () => {
130
- active = false;
131
- if (watch) {
132
- watch.disconnect();
133
- watch = null;
165
+ runtime.active = false;
166
+ if (runtime.watch) {
167
+ runtime.watch.disconnect();
168
+ runtime.watch = null;
134
169
  }
135
- if (timeout) {
136
- clearTimeout(timeout);
137
- timeout = null;
170
+ if (runtime.timeout) {
171
+ clearTimeout(runtime.timeout);
172
+ runtime.timeout = null;
138
173
  }
139
174
  };
140
- }, [options, reconnectTimeout]);
175
+ }, [authEnabled, connectionKey, reconnectTimeout]);
141
176
  React.useEffect(() => {
142
- if (options?.auth) {
177
+ if (authEnabled) {
143
178
  if (error && error.type === "danger") return;
144
179
  if (state !== "connected") {
145
180
  const timeout = setTimeout(() => {
@@ -157,13 +192,13 @@ function useRstream(options) {
157
192
  } else {
158
193
  setError(null);
159
194
  }
160
- }, [options, state, error, errorTimeout]);
195
+ }, [authEnabled, state, error, errorTimeout]);
161
196
  React.useEffect(() => {
162
- if (options?.auth === void 0 || state !== "connected") {
197
+ if (!authEnabled || state !== "connected") {
163
198
  setClients([]);
164
199
  setTunnels([]);
165
200
  }
166
- }, [options, state]);
201
+ }, [authEnabled, connectionKey, state]);
167
202
  return { state, error, tunnels, clients };
168
203
  }
169
204
 
@@ -1,8 +1,8 @@
1
1
  import {
2
2
  RstreamProvider,
3
3
  useRstreamContext
4
- } from "../chunk-CCMP5O6Z.mjs";
5
- import "../chunk-CTKB6CWW.mjs";
4
+ } from "../chunk-DFYVJASW.mjs";
5
+ import "../chunk-G47GUMJ6.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": "1.7.5",
3
+ "version": "1.7.6",
4
4
  "description": "React hooks and components for building rstream-enabled UIs.",
5
5
  "license": "Apache-2.0",
6
6
  "main": "./dist/index.js",
@@ -50,7 +50,7 @@
50
50
  "typescript": "5.9.3"
51
51
  },
52
52
  "dependencies": {
53
- "@rstreamlabs/rstream": "2.0.0",
53
+ "@rstreamlabs/tunnels": "1.0.1",
54
54
  "@rstreamlabs/webtty": "1.6.3",
55
55
  "@xterm/addon-fit": "^0.11.0",
56
56
  "@xterm/addon-unicode11": "^0.9.0",