@camera.ui/transport 0.0.6 → 0.0.7

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.
@@ -15,5 +15,6 @@ export interface SocketioTransport extends Transport {
15
15
  readonly manager: Manager | null;
16
16
  socket(namespace?: string): Socket | null;
17
17
  ensureSocket(namespace: string): Socket | null;
18
+ reviveDeadSockets(): void;
18
19
  }
19
20
  export declare function createSocketioTransport(options?: SocketioTransportOptions): SocketioTransport;
@@ -47,17 +47,46 @@ function createSocketioTransport(options = {}) {
47
47
  };
48
48
  emitter.emit("down", { reason });
49
49
  }
50
+ function isAuthError(msg) {
51
+ const m = msg.toLowerCase();
52
+ return m.includes("auth") || m.includes("unauthorized");
53
+ }
54
+ function bindCommonAuthEvents(socket) {
55
+ let rotatedOut = false;
56
+ socket.on("unauthenticated", () => {
57
+ const fresh = currentTarget ? buildAuth(currentTarget) : null;
58
+ const current = socket.auth?.token;
59
+ if (fresh && current !== fresh.token) {
60
+ socket.auth = fresh;
61
+ rotatedOut = true;
62
+ } else emitter.emit("auth-error", { message: "unauthenticated" });
63
+ });
64
+ socket.on("disconnect", () => {
65
+ if (rotatedOut) {
66
+ rotatedOut = false;
67
+ socket.connect();
68
+ }
69
+ });
70
+ }
50
71
  function bindMainSocketEvents(socket) {
51
72
  socket.on("connect", () => markUp());
52
73
  socket.on("disconnect", (reason) => markDown(reason));
53
74
  socket.on("connect_error", (err) => {
54
75
  const msg = err?.message ?? "connect_error";
55
- if (msg.toLowerCase().includes("auth") || msg.toLowerCase().includes("unauthorized")) {
76
+ if (isAuthError(msg)) {
56
77
  emitter.emit("auth-error", { message: msg });
57
78
  return;
58
79
  }
59
80
  markDown(msg);
60
81
  });
82
+ bindCommonAuthEvents(socket);
83
+ }
84
+ function bindSecondarySocketEvents(socket) {
85
+ socket.on("connect_error", (err) => {
86
+ const msg = err?.message ?? "connect_error";
87
+ if (isAuthError(msg)) emitter.emit("auth-error", { message: msg });
88
+ });
89
+ bindCommonAuthEvents(socket);
61
90
  }
62
91
  function openSocket(namespace, target) {
63
92
  const sock = io(`${target.endpoint.url}${namespace}`, {
@@ -101,7 +130,10 @@ function createSocketioTransport(options = {}) {
101
130
  }
102
131
  function rebindAuth(target) {
103
132
  const auth = buildAuth(target);
104
- for (const sock of sockets.values()) sock.auth = auth;
133
+ for (const sock of sockets.values()) {
134
+ sock.auth = auth;
135
+ if (!sock.connected) sock.connect();
136
+ }
105
137
  }
106
138
  async function apply(target) {
107
139
  if (disposed) throw new Error("socketio-transport disposed");
@@ -138,9 +170,20 @@ function createSocketioTransport(options = {}) {
138
170
  function ensureSocket(namespace) {
139
171
  if (!currentTarget) return null;
140
172
  let sock = sockets.get(namespace);
141
- if (!sock) sock = openSocket(namespace, currentTarget);
173
+ if (!sock) {
174
+ sock = openSocket(namespace, currentTarget);
175
+ bindSecondarySocketEvents(sock);
176
+ }
142
177
  return sock;
143
178
  }
179
+ function reviveDeadSockets() {
180
+ if (!currentTarget) return;
181
+ const auth = buildAuth(currentTarget);
182
+ for (const sock of sockets.values()) if (!sock.connected) {
183
+ sock.auth = auth;
184
+ sock.connect();
185
+ }
186
+ }
144
187
  return {
145
188
  get spec() {
146
189
  return spec;
@@ -153,7 +196,8 @@ function createSocketioTransport(options = {}) {
153
196
  on,
154
197
  dispose,
155
198
  socket,
156
- ensureSocket
199
+ ensureSocket,
200
+ reviveDeadSockets
157
201
  };
158
202
  }
159
203
  //#endregion
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@camera.ui/transport",
3
- "version": "0.0.6",
3
+ "version": "0.0.7",
4
4
  "description": "camera.ui transport layer — framework-agnostic connection kernel, reducer state, pluggable transports (HTTP/WS/Socket.IO/NATS), lifecycle effects and worker bridge",
5
5
  "author": "seydx (https://github.com/cameraui/clients)",
6
6
  "type": "module",