@sixb/client 0.2.5 → 0.2.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.
- package/dist/agent-streams.d.ts +11 -2
- package/dist/agent-streams.d.ts.map +1 -1
- package/dist/artifact-live.js +1 -1
- package/dist/browser.js +1 -1
- package/dist/{chunk-ktqfenfd.js → chunk-3dxa8d3r.js} +46 -6
- package/dist/chunk-3dxa8d3r.js.map +10 -0
- package/dist/{chunk-zta9jf24.js → chunk-4t68c4gf.js} +10 -3
- package/dist/{chunk-zta9jf24.js.map → chunk-4t68c4gf.js.map} +3 -3
- package/dist/{chunk-tm588tr7.js → chunk-6caavk8p.js} +1 -1
- package/dist/{chunk-pn4b10k4.js → chunk-pzz6gtn5.js} +2 -2
- package/dist/{chunk-60fdvjxa.js → chunk-vydypkf1.js} +39 -2
- package/dist/chunk-vydypkf1.js.map +10 -0
- package/dist/connectors/connection.d.ts +12 -0
- package/dist/connectors/connection.d.ts.map +1 -1
- package/dist/connectors/index.d.ts +1 -1
- package/dist/connectors/index.d.ts.map +1 -1
- package/dist/connectors/queries.d.ts.map +1 -1
- package/dist/generated/@tanstack/react-query.gen.d.ts +1068 -153
- package/dist/generated/@tanstack/react-query.gen.d.ts.map +1 -1
- package/dist/generated/index.d.ts +2 -2
- package/dist/generated/index.d.ts.map +1 -1
- package/dist/generated/sdk.gen.d.ts +27 -3
- package/dist/generated/sdk.gen.d.ts.map +1 -1
- package/dist/generated/types.gen.d.ts +1011 -51
- package/dist/generated/types.gen.d.ts.map +1 -1
- package/dist/hooks.js +87 -5
- package/dist/hooks.js.map +4 -4
- package/dist/index.js +13 -5
- package/dist/index.js.map +1 -1
- package/dist/logs.js +3 -3
- package/dist/models.js +10 -2
- package/dist/models.js.map +1 -1
- package/dist/query.js +2 -2
- package/dist/users-hooks.d.ts.map +1 -1
- package/dist/ws-socket.d.ts +2 -0
- package/dist/ws-socket.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/agent-streams.ts +22 -0
- package/src/connectors/connection.ts +59 -0
- package/src/connectors/index.ts +1 -0
- package/src/generated/@tanstack/react-query.gen.ts +131 -2
- package/src/generated/index.ts +20 -0
- package/src/generated/sdk.gen.ts +93 -2
- package/src/generated/types.gen.ts +1451 -37
- package/src/ws-socket.ts +62 -7
- package/dist/chunk-60fdvjxa.js.map +0 -10
- package/dist/chunk-ktqfenfd.js.map +0 -10
- /package/dist/{chunk-tm588tr7.js.map → chunk-6caavk8p.js.map} +0 -0
- /package/dist/{chunk-pn4b10k4.js.map → chunk-pzz6gtn5.js.map} +0 -0
package/src/ws-socket.ts
CHANGED
|
@@ -22,6 +22,11 @@ import { getSixbSessionAccessToken, SixbSessionEndedError } from "./session"
|
|
|
22
22
|
const DEFAULT_SIXB_API_BASE_URL = "http://localhost:3002"
|
|
23
23
|
const DEFAULT_RECONNECT_DELAY_MS = 1000
|
|
24
24
|
const DEFAULT_READY_TIMEOUT_MS = 10_000
|
|
25
|
+
/** RFC 6455 Service Restart: sent when a server drains so clients hop silently to the new slot. */
|
|
26
|
+
export const SERVICE_RESTART_CLOSE_CODE = 1012
|
|
27
|
+
const SERVICE_RESTART_MIN_DELAY_MS = 50
|
|
28
|
+
const SERVICE_RESTART_JITTER_MS = 100
|
|
29
|
+
const SERVICE_RESTART_GRACE_MS = 1_500
|
|
25
30
|
|
|
26
31
|
export interface ReconnectingSocketState {
|
|
27
32
|
readonly connected: boolean
|
|
@@ -112,12 +117,22 @@ export function createReconnectingSocket(options: ReconnectingSocketOptions): Re
|
|
|
112
117
|
let state = INITIAL_STATE
|
|
113
118
|
let stopped = false
|
|
114
119
|
let openedOnce = false
|
|
120
|
+
let handingOff = false
|
|
115
121
|
let socket: WebSocket | null = null
|
|
116
122
|
let reconnectTimer: ReturnType<typeof setTimeout> | null = null
|
|
123
|
+
let handoffTimer: ReturnType<typeof setTimeout> | null = null
|
|
117
124
|
let connectionGeneration = 0
|
|
118
125
|
// Reconnects since the last connection that was ready, for the backoff.
|
|
119
126
|
let failures = 0
|
|
120
127
|
|
|
128
|
+
const clearHandoff = () => {
|
|
129
|
+
handingOff = false
|
|
130
|
+
if (handoffTimer) {
|
|
131
|
+
clearTimeout(handoffTimer)
|
|
132
|
+
handoffTimer = null
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
121
136
|
const setState = (next: ReconnectingSocketState) => {
|
|
122
137
|
state = next
|
|
123
138
|
options.onStateChange?.(next)
|
|
@@ -130,12 +145,17 @@ export function createReconnectingSocket(options: ReconnectingSocketOptions): Re
|
|
|
130
145
|
},
|
|
131
146
|
}
|
|
132
147
|
|
|
133
|
-
const scheduleReconnect = () => {
|
|
148
|
+
const scheduleReconnect = (serviceRestart = false) => {
|
|
134
149
|
if (!reconnect || stopped || reconnectTimer) return
|
|
135
|
-
const delay =
|
|
136
|
-
? Math.min(
|
|
137
|
-
|
|
138
|
-
|
|
150
|
+
const delay = serviceRestart
|
|
151
|
+
? Math.min(
|
|
152
|
+
reconnectDelayMs,
|
|
153
|
+
SERVICE_RESTART_MIN_DELAY_MS + Math.floor(Math.random() * SERVICE_RESTART_JITTER_MS)
|
|
154
|
+
)
|
|
155
|
+
: options.maxReconnectDelayMs
|
|
156
|
+
? Math.min(options.maxReconnectDelayMs, reconnectDelayMs * 2 ** failures)
|
|
157
|
+
: reconnectDelayMs
|
|
158
|
+
if (!serviceRestart) failures += 1
|
|
139
159
|
reconnectTimer = setTimeout(() => {
|
|
140
160
|
reconnectTimer = null
|
|
141
161
|
void connect()
|
|
@@ -145,7 +165,11 @@ export function createReconnectingSocket(options: ReconnectingSocketOptions): Re
|
|
|
145
165
|
const connect = async () => {
|
|
146
166
|
if (stopped) return
|
|
147
167
|
const generation = ++connectionGeneration
|
|
148
|
-
setState({
|
|
168
|
+
setState({
|
|
169
|
+
connected: false,
|
|
170
|
+
reconnecting: !handingOff && (openedOnce || state.reconnecting),
|
|
171
|
+
error: null,
|
|
172
|
+
})
|
|
149
173
|
|
|
150
174
|
let protocols: readonly string[] | undefined
|
|
151
175
|
let accessToken: string | null = null
|
|
@@ -161,6 +185,7 @@ export function createReconnectingSocket(options: ReconnectingSocketOptions): Re
|
|
|
161
185
|
}
|
|
162
186
|
} catch (error) {
|
|
163
187
|
if (stopped || generation !== connectionGeneration) return
|
|
188
|
+
clearHandoff()
|
|
164
189
|
sink.reportError(error instanceof Error ? error.message : String(error))
|
|
165
190
|
const retry =
|
|
166
191
|
reconnect &&
|
|
@@ -193,13 +218,17 @@ export function createReconnectingSocket(options: ReconnectingSocketOptions): Re
|
|
|
193
218
|
ws.onopen = () => {
|
|
194
219
|
if (stopped) return
|
|
195
220
|
openedOnce = true
|
|
196
|
-
if (!options.readyWhen)
|
|
221
|
+
if (!options.readyWhen) {
|
|
222
|
+
failures = 0
|
|
223
|
+
clearHandoff()
|
|
224
|
+
}
|
|
197
225
|
setState({ connected: true, reconnecting: false, error: null })
|
|
198
226
|
if (!options.subscribeWhen) subscribe()
|
|
199
227
|
if (options.readyWhen) {
|
|
200
228
|
readyTimer = setTimeout(
|
|
201
229
|
() => {
|
|
202
230
|
if (stopped || socket !== ws) return
|
|
231
|
+
clearHandoff()
|
|
203
232
|
try {
|
|
204
233
|
sink.reportError(
|
|
205
234
|
options.readyTimeoutMessage ?? "Websocket protocol handshake timed out."
|
|
@@ -218,6 +247,7 @@ export function createReconnectingSocket(options: ReconnectingSocketOptions): Re
|
|
|
218
247
|
if (options.subscribeWhen?.(messageEvent.data)) subscribe()
|
|
219
248
|
if (options.readyWhen?.(messageEvent.data)) {
|
|
220
249
|
clearReadyTimer()
|
|
250
|
+
clearHandoff()
|
|
221
251
|
failures = 0
|
|
222
252
|
}
|
|
223
253
|
options.onMessage(messageEvent.data, sink)
|
|
@@ -225,6 +255,7 @@ export function createReconnectingSocket(options: ReconnectingSocketOptions): Re
|
|
|
225
255
|
|
|
226
256
|
ws.onerror = () => {
|
|
227
257
|
if (stopped) return
|
|
258
|
+
clearHandoff()
|
|
228
259
|
sink.reportError(options.connectionErrorMessage)
|
|
229
260
|
}
|
|
230
261
|
|
|
@@ -237,6 +268,7 @@ export function createReconnectingSocket(options: ReconnectingSocketOptions): Re
|
|
|
237
268
|
|
|
238
269
|
// The shared link ended: the server would refuse every reconnect.
|
|
239
270
|
if (event?.code === SHARED_ACCESS_ENDED_CLOSE_CODE) {
|
|
271
|
+
clearHandoff()
|
|
240
272
|
stopped = true
|
|
241
273
|
sink.reportError(SHARED_ACCESS_ENDED)
|
|
242
274
|
setState({ connected: false, reconnecting: false, error: SHARED_ACCESS_ENDED })
|
|
@@ -244,6 +276,28 @@ export function createReconnectingSocket(options: ReconnectingSocketOptions): Re
|
|
|
244
276
|
return
|
|
245
277
|
}
|
|
246
278
|
|
|
279
|
+
// Planned server restart (1012): hop immediately with jitter and suppress the reconnecting
|
|
280
|
+
// indicator during the brief handoff unless the new server fails to answer in time.
|
|
281
|
+
if (
|
|
282
|
+
reconnect &&
|
|
283
|
+
event?.code === SERVICE_RESTART_CLOSE_CODE &&
|
|
284
|
+
!handingOff &&
|
|
285
|
+
failures === 0
|
|
286
|
+
) {
|
|
287
|
+
handingOff = true
|
|
288
|
+
handoffTimer = setTimeout(() => {
|
|
289
|
+
handoffTimer = null
|
|
290
|
+
if (!stopped && handingOff && !state.connected) {
|
|
291
|
+
handingOff = false
|
|
292
|
+
setState({ connected: false, reconnecting: true, error: state.error })
|
|
293
|
+
}
|
|
294
|
+
}, SERVICE_RESTART_GRACE_MS)
|
|
295
|
+
setState({ connected: false, reconnecting: false, error: state.error })
|
|
296
|
+
scheduleReconnect(true)
|
|
297
|
+
return
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
clearHandoff()
|
|
247
301
|
setState({ connected: false, reconnecting: reconnect, error: state.error })
|
|
248
302
|
scheduleReconnect()
|
|
249
303
|
}
|
|
@@ -261,6 +315,7 @@ export function createReconnectingSocket(options: ReconnectingSocketOptions): Re
|
|
|
261
315
|
close() {
|
|
262
316
|
stopped = true
|
|
263
317
|
connectionGeneration += 1
|
|
318
|
+
clearHandoff()
|
|
264
319
|
if (reconnectTimer) {
|
|
265
320
|
clearTimeout(reconnectTimer)
|
|
266
321
|
reconnectTimer = null
|