@gwakko/shared-websocket 0.13.0 → 0.14.5
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 +37 -0
- package/dist/SharedSocket.d.ts +2 -0
- package/dist/SharedWebSocket.d.ts +141 -5
- package/dist/SubscriptionManager.d.ts +1 -1
- package/dist/WorkerSocket.d.ts +2 -0
- package/dist/adapters/react.d.ts +3 -3
- package/dist/adapters/vue.d.ts +3 -3
- package/dist/{chunk-RKVYLJTQ.cjs → chunk-HIKH74NQ.cjs} +505 -69
- package/dist/chunk-HIKH74NQ.cjs.map +1 -0
- package/dist/{chunk-IK4HLA3K.js → chunk-N63ZMMWV.js} +496 -60
- package/dist/chunk-N63ZMMWV.js.map +1 -0
- package/dist/index.cjs +3 -3
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/react.cjs +8 -8
- package/dist/react.cjs.map +1 -1
- package/dist/react.js +7 -7
- package/dist/react.js.map +1 -1
- package/dist/types.d.ts +152 -1
- package/dist/vue.cjs +8 -8
- package/dist/vue.cjs.map +1 -1
- package/dist/vue.js +7 -7
- package/dist/vue.js.map +1 -1
- package/dist/worker/socket.worker.d.ts +2 -0
- package/package.json +1 -1
- package/src/MessageBus.ts +8 -1
- package/src/SharedSocket.ts +28 -3
- package/src/SharedWebSocket.ts +577 -63
- package/src/SubscriptionManager.ts +4 -4
- package/src/WorkerSocket.ts +27 -3
- package/src/adapters/react.ts +9 -9
- package/src/adapters/vue.ts +9 -9
- package/src/index.ts +3 -0
- package/src/types.ts +162 -1
- package/src/worker/socket.worker.ts +7 -0
- package/dist/chunk-IK4HLA3K.js.map +0 -1
- package/dist/chunk-RKVYLJTQ.cjs.map +0 -1
package/src/SharedSocket.ts
CHANGED
|
@@ -8,6 +8,8 @@ interface SharedSocketOptions {
|
|
|
8
8
|
reconnectMaxDelay?: number;
|
|
9
9
|
/** Max reconnect attempts before giving up (default: Infinity). */
|
|
10
10
|
reconnectMaxRetries?: number;
|
|
11
|
+
/** Close codes that mean "auth failed — stop reconnect." Default: [1008]. */
|
|
12
|
+
authFailureCloseCodes?: number[];
|
|
11
13
|
heartbeatInterval?: number;
|
|
12
14
|
sendBuffer?: number;
|
|
13
15
|
auth?: () => string | Promise<string>;
|
|
@@ -34,7 +36,8 @@ export class SharedSocket implements Disposable {
|
|
|
34
36
|
|
|
35
37
|
private reconnectAttempts = 0;
|
|
36
38
|
|
|
37
|
-
private readonly opts: Required<Omit<SharedSocketOptions, 'auth' | 'authToken' | 'authParam' | 'pingPayload' | 'serialize' | 'deserialize'>> & {
|
|
39
|
+
private readonly opts: Required<Omit<SharedSocketOptions, 'auth' | 'authToken' | 'authParam' | 'pingPayload' | 'serialize' | 'deserialize' | 'authFailureCloseCodes'>> & {
|
|
40
|
+
authFailureCloseCodes: ReadonlySet<number>;
|
|
38
41
|
auth?: () => string | Promise<string>;
|
|
39
42
|
authToken?: string;
|
|
40
43
|
authParam: string;
|
|
@@ -52,6 +55,7 @@ export class SharedSocket implements Disposable {
|
|
|
52
55
|
reconnect: options.reconnect ?? true,
|
|
53
56
|
reconnectMaxDelay: options.reconnectMaxDelay ?? 30_000,
|
|
54
57
|
reconnectMaxRetries: options.reconnectMaxRetries ?? Infinity,
|
|
58
|
+
authFailureCloseCodes: new Set(options.authFailureCloseCodes ?? [1008]),
|
|
55
59
|
heartbeatInterval: options.heartbeatInterval ?? 30_000,
|
|
56
60
|
sendBuffer: options.sendBuffer ?? 100,
|
|
57
61
|
auth: options.auth,
|
|
@@ -76,7 +80,15 @@ export class SharedSocket implements Disposable {
|
|
|
76
80
|
|
|
77
81
|
this.setState('connecting');
|
|
78
82
|
|
|
79
|
-
|
|
83
|
+
let connectUrl: string;
|
|
84
|
+
try {
|
|
85
|
+
connectUrl = await this.buildUrl();
|
|
86
|
+
} catch {
|
|
87
|
+
// auth() threw or returned no token — pause reconnect until user
|
|
88
|
+
// provides fresh creds via ws.authenticate(token) or ws.reconnect().
|
|
89
|
+
this.setState('failed');
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
80
92
|
this.ws = new WebSocket(connectUrl, this.opts.protocols);
|
|
81
93
|
|
|
82
94
|
this.ws.onopen = () => {
|
|
@@ -96,8 +108,14 @@ export class SharedSocket implements Disposable {
|
|
|
96
108
|
for (const fn of this.onMessageFns) fn(data);
|
|
97
109
|
};
|
|
98
110
|
|
|
99
|
-
this.ws.onclose = () => {
|
|
111
|
+
this.ws.onclose = (ev) => {
|
|
100
112
|
this.stopHeartbeat();
|
|
113
|
+
if (this.opts.authFailureCloseCodes.has(ev.code)) {
|
|
114
|
+
// Auth-failure close code — don't burn retries with stale creds.
|
|
115
|
+
// User must call ws.authenticate(freshToken) or ws.reconnect() to resume.
|
|
116
|
+
this.setState('failed');
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
101
119
|
if (!this.disposed && this.opts.reconnect) {
|
|
102
120
|
this.scheduleReconnect();
|
|
103
121
|
} else {
|
|
@@ -228,7 +246,14 @@ export class SharedSocket implements Disposable {
|
|
|
228
246
|
// Resolve token: callback > static > none
|
|
229
247
|
let token: string | undefined;
|
|
230
248
|
if (this.opts.auth) {
|
|
249
|
+
// If the auth callback throws, let it propagate — connect() catches and
|
|
250
|
+
// pauses reconnect until the user supplies fresh creds.
|
|
231
251
|
token = await this.opts.auth();
|
|
252
|
+
if (!token) {
|
|
253
|
+
// Configured auth callback returned no token. Treat as a fatal auth
|
|
254
|
+
// condition (don't silently connect without credentials).
|
|
255
|
+
throw new Error('SharedSocket: auth() returned no token');
|
|
256
|
+
}
|
|
232
257
|
} else if (this.opts.authToken) {
|
|
233
258
|
token = this.opts.authToken;
|
|
234
259
|
}
|