@classytic/arc-next 0.6.0 → 0.7.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/dist/ws.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use client";
2
2
 
3
- import { buildStreamUrl } from "./client.js";
3
+ import { ArcApiError, _getAuthErrorHandler, _runAuthRecovery, buildStreamUrl } from "./client.js";
4
4
  import { useQueryClient } from "@tanstack/react-query";
5
5
  import { useCallback, useEffect, useMemo, useRef, useState } from "react";
6
6
 
@@ -69,9 +69,15 @@ function connectWs(options = {}) {
69
69
  if (wildcard) for (const fn of wildcard) fn(message);
70
70
  };
71
71
  const connect = () => {
72
- if (ws) try {
73
- ws.close();
74
- } catch {}
72
+ if (ws) {
73
+ ws.onclose = null;
74
+ ws.onerror = null;
75
+ ws.onmessage = null;
76
+ ws.onopen = null;
77
+ try {
78
+ ws.close();
79
+ } catch {}
80
+ }
75
81
  if (heartbeatTimer) {
76
82
  clearInterval(heartbeatTimer);
77
83
  heartbeatTimer = null;
@@ -81,6 +87,7 @@ function connectWs(options = {}) {
81
87
  ws = protocols !== void 0 ? new WebSocket(wsUrl, protocols) : new WebSocket(wsUrl);
82
88
  ws.onopen = () => {
83
89
  reconnectAttempts = 0;
90
+ wsAuthRetries = 0;
84
91
  connected = true;
85
92
  options.onConnectionChange?.(true);
86
93
  for (const resource of subscriptions) sendRaw({
@@ -104,7 +111,7 @@ function connectWs(options = {}) {
104
111
  dispatch(parsed);
105
112
  };
106
113
  ws.onerror = () => {};
107
- ws.onclose = () => {
114
+ ws.onclose = (event) => {
108
115
  connected = false;
109
116
  options.onConnectionChange?.(false);
110
117
  if (heartbeatTimer) {
@@ -112,6 +119,35 @@ function connectWs(options = {}) {
112
119
  heartbeatTimer = null;
113
120
  }
114
121
  if (manualClose) return;
122
+ const { handler, maxAuthRetries } = _getAuthErrorHandler();
123
+ const isAuthClose = event.code === 1008 || event.code === 3401 || event.code === 4001 || event.code === 4401;
124
+ if (handler && isAuthClose && wsAuthRetries < maxAuthRetries) {
125
+ wsAuthRetries += 1;
126
+ _runAuthRecovery(handler, {
127
+ error: new ArcApiError(event.reason || `WebSocket closed with auth code ${event.code}`, {
128
+ status: 401,
129
+ statusText: "WebSocket auth failure",
130
+ json: {
131
+ code: "arc.websocket.unauthorized",
132
+ wsCloseCode: event.code,
133
+ reason: event.reason
134
+ },
135
+ endpoint: url ?? path,
136
+ method: "GET"
137
+ }),
138
+ request: {
139
+ method: "GET",
140
+ endpoint: url ?? path
141
+ },
142
+ attempt: wsAuthRetries
143
+ }).then(({ decision }) => {
144
+ if (decision === "retry") {
145
+ reconnectAttempts = 0;
146
+ connect();
147
+ }
148
+ }).catch(() => {});
149
+ return;
150
+ }
115
151
  if (reconnectAttempts < maxReconnectAttempts) {
116
152
  reconnectAttempts += 1;
117
153
  const delay = Math.min(reconnectDelay * Math.pow(1.5, reconnectAttempts - 1), 3e4);
@@ -119,6 +155,7 @@ function connectWs(options = {}) {
119
155
  }
120
156
  };
121
157
  };
158
+ let wsAuthRetries = 0;
122
159
  connect();
123
160
  return {
124
161
  isConnected: () => connected,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@classytic/arc-next",
3
- "version": "0.6.0",
3
+ "version": "0.7.1",
4
4
  "description": "React + TanStack Query SDK for Arc resources",
5
5
  "type": "module",
6
6
  "sideEffects": false,
@@ -123,12 +123,17 @@
123
123
  },
124
124
  "peerDependencies": {
125
125
  "@classytic/repo-core": ">=0.4.0",
126
- "@tanstack/react-query": ">=5.0.0",
126
+ "@tanstack/react-query": ">=5.62.0",
127
127
  "react": ">=19.0.0"
128
128
  },
129
+ "peerDependenciesMeta": {
130
+ "react": { "optional": false },
131
+ "@tanstack/react-query": { "optional": false },
132
+ "@classytic/repo-core": { "optional": false }
133
+ },
129
134
  "devDependencies": {
130
135
  "@classytic/dev-tools": "^0.2.0",
131
- "@classytic/repo-core": "^0.4.0",
136
+ "@classytic/repo-core": "^0.5.0",
132
137
  "@tanstack/react-query": "^5.97.0",
133
138
  "@testing-library/jest-dom": "^6.9.1",
134
139
  "@testing-library/react": "^16.3.2",