-
-
Notifications
You must be signed in to change notification settings - Fork 163
/
Copy pathclient.ts
1103 lines (1033 loc) · 35.7 KB
/
client.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
*
* client
*
*/
import {
CloseCode,
ConnectionAckMessage,
ConnectionInitMessage,
Disposable,
FormattedExecutionPatchResult,
FormattedExecutionResult,
GRAPHQL_TRANSPORT_WS_PROTOCOL,
ID,
JSONMessageReplacer,
JSONMessageReviver,
Message,
MessageType,
parseMessage,
PingMessage,
PongMessage,
Sink,
stringifyMessage,
SubscribePayload,
} from './common';
import { isObject, limitCloseReason } from './utils';
/** This file is the entry point for browsers, re-export common elements. */
export * from './common';
/**
* WebSocket started connecting.
*
* @category Client
*/
export type EventConnecting = 'connecting';
/**
* WebSocket has opened.
*
* @category Client
*/
export type EventOpened = 'opened';
/**
* Open WebSocket connection has been acknowledged.
*
* @category Client
*/
export type EventConnected = 'connected';
/**
* `PingMessage` has been received or sent.
*
* @category Client
*/
export type EventPing = 'ping';
/**
* `PongMessage` has been received or sent.
*
* @category Client
*/
export type EventPong = 'pong';
/**
* A message has been received.
*
* @category Client
*/
export type EventMessage = 'message';
/**
* WebSocket connection has closed.
*
* @category Client
*/
export type EventClosed = 'closed';
/**
* WebSocket connection had an error or client had an internal error.
*
* @category Client
*/
export type EventError = 'error';
/**
* All events that could occur.
*
* @category Client
*/
export type Event =
| EventConnecting
| EventOpened
| EventConnected
| EventPing
| EventPong
| EventMessage
| EventClosed
| EventError;
/** @category Client */
export type EventConnectingListener = (isRetry: boolean) => void;
/**
* The first argument is actually the `WebSocket`, but to avoid
* bundling DOM typings because the client can run in Node env too,
* you should assert the websocket type during implementation.
*
* @category Client
*/
export type EventOpenedListener = (socket: unknown) => void;
/**
* The first argument is actually the `WebSocket`, but to avoid
* bundling DOM typings because the client can run in Node env too,
* you should assert the websocket type during implementation.
*
* Also, the second argument is the optional payload that the server may
* send through the `ConnectionAck` message.
*
* @category Client
*/
export type EventConnectedListener = (
socket: unknown,
payload: ConnectionAckMessage['payload'],
wasRetry: boolean,
) => void;
/**
* The first argument communicates whether the ping was received from the server.
* If `false`, the ping was sent by the client.
*
* @category Client
*/
export type EventPingListener = (
received: boolean,
payload: PingMessage['payload'],
) => void;
/**
* The first argument communicates whether the pong was received from the server.
* If `false`, the pong was sent by the client.
*
* @category Client
*/
export type EventPongListener = (
received: boolean,
payload: PongMessage['payload'],
) => void;
/**
* Called for all **valid** messages received by the client. Mainly useful for
* debugging and logging received messages.
*
* @category Client
*/
export type EventMessageListener = (message: Message) => void;
/**
* The argument is actually the websocket `CloseEvent`, but to avoid
* bundling DOM typings because the client can run in Node env too,
* you should assert the websocket type during implementation.
*
* @category Client
*/
export type EventClosedListener = (event: unknown) => void;
/**
* Events dispatched from the WebSocket `onerror` are handled in this listener,
* as well as all internal client errors that could throw.
*
* @category Client
*/
export type EventErrorListener = (error: unknown) => void;
/** @category Client */
export type EventListener<E extends Event> = E extends EventConnecting
? EventConnectingListener
: E extends EventOpened
? EventOpenedListener
: E extends EventConnected
? EventConnectedListener
: E extends EventPing
? EventPingListener
: E extends EventPong
? EventPongListener
: E extends EventMessage
? EventMessageListener
: E extends EventClosed
? EventClosedListener
: E extends EventError
? EventErrorListener
: never;
/**
* Configuration used for the GraphQL over WebSocket client.
*
* @category Client
*/
export interface ClientOptions<
P extends ConnectionInitMessage['payload'] = ConnectionInitMessage['payload'],
> {
/**
* URL of the GraphQL over WebSocket Protocol compliant server to connect.
*
* If the option is a function, it will be called on every WebSocket connection attempt.
* Returning a promise is supported too and the connecting phase will stall until it
* resolves with the URL.
*
* A good use-case for having a function is when using the URL for authentication,
* where subsequent reconnects (due to auth) may have a refreshed identity token in
* the URL.
*/
url: string | (() => Promise<string> | string);
/**
* Optional parameters, passed through the `payload` field with the `ConnectionInit` message,
* that the client specifies when establishing a connection with the server. You can use this
* for securely passing arguments for authentication.
*
* If you decide to return a promise, keep in mind that the server might kick you off if it
* takes too long to resolve! Check the `connectionInitWaitTimeout` on the server for more info.
*
* Throwing an error from within this function will close the socket with the `Error` message
* in the close event reason.
*/
connectionParams?: P | (() => Promise<P> | P);
/**
* Controls when should the connection be established.
*
* - `false`: Establish a connection immediately. Use `onNonLazyError` to handle errors.
* - `true`: Establish a connection on first subscribe and close on last unsubscribe. Use
* the subscription sink's `error` to handle errors.
*
* @default true
*/
lazy?: boolean;
/**
* Used ONLY when the client is in non-lazy mode (`lazy = false`). When
* using this mode, the errors might have no sinks to report to; however,
* to avoid swallowing errors, consider using `onNonLazyError`, which will
* be called when either:
* - An unrecoverable error/close event occurs
* - Silent retry attempts have been exceeded
*
* After a client has errored out, it will NOT perform any automatic actions.
*
* The argument can be a websocket `CloseEvent` or an `Error`. To avoid bundling
* DOM types, you should derive and assert the correct type. When receiving:
* - A `CloseEvent`: retry attempts have been exceeded or the specific
* close event is labeled as fatal (read more in `retryAttempts`).
* - An `Error`: some internal issue has occured, all internal errors are
* fatal by nature.
*
* @default console.error
*/
onNonLazyError?: (errorOrCloseEvent: unknown) => void;
/**
* How long should the client wait before closing the socket after the last operation has
* completed. This is meant to be used in combination with `lazy`. You might want to have
* a calmdown time before actually closing the connection. Kinda' like a lazy close "debounce".
*
* @default 0
*/
lazyCloseTimeout?: number;
/**
* The timeout between dispatched keep-alive messages, namely server pings. Internally
* dispatches the `PingMessage` type to the server and expects a `PongMessage` in response.
* This helps with making sure that the connection with the server is alive and working.
*
* Timeout countdown starts from the moment the socket was opened and subsequently
* after every received `PongMessage`.
*
* Note that NOTHING will happen automatically with the client if the server never
* responds to a `PingMessage` with a `PongMessage`. If you want the connection to close,
* you should implement your own logic on top of the client. A simple example looks like this:
*
* ```js
* import { createClient } from 'graphql-ws';
*
* let activeSocket, timedOut;
* createClient({
* url: 'ws://i.time.out:4000/after-5/seconds',
* keepAlive: 10_000, // ping server every 10 seconds
* on: {
* connected: (socket) => (activeSocket = socket),
* ping: (received) => {
* if (!received) // sent
* timedOut = setTimeout(() => {
* if (activeSocket.readyState === WebSocket.OPEN)
* activeSocket.close(4408, 'Request Timeout');
* }, 5_000); // wait 5 seconds for the pong and then close the connection
* },
* pong: (received) => {
* if (received) clearTimeout(timedOut); // pong is received, clear connection close timeout
* },
* },
* });
* ```
*
* @default 0
*/
keepAlive?: number;
/**
* The amount of time for which the client will wait
* for `ConnectionAck` message.
*
* Set the value to `Infinity`, `''`, `0`, `null` or `undefined` to skip waiting.
*
* If the wait timeout has passed and the server
* has not responded with `ConnectionAck` message,
* the client will terminate the socket by
* dispatching a close event `4418: Connection acknowledgement timeout`
*
* @default 0
*/
connectionAckWaitTimeout?: number;
/**
* Disable sending the `PongMessage` automatically.
*
* Useful for when integrating your own custom client pinger that performs
* custom actions before responding to a ping, or to pass along the optional pong
* message payload. Please check the readme recipes for a concrete example.
*/
disablePong?: boolean;
/**
* How many times should the client try to reconnect on abnormal socket closure before it errors out?
*
* The library classifies the following close events as fatal:
* - _All internal WebSocket fatal close codes (check `isFatalInternalCloseCode` in `src/client.ts` for exact list)_
* - `4500: Internal server error`
* - `4005: Internal client error`
* - `4400: Bad request`
* - `4004: Bad response`
* - `4401: Unauthorized` _tried subscribing before connect ack_
* - `4406: Subprotocol not acceptable`
* - `4409: Subscriber for <id> already exists` _distinction is very important_
* - `4429: Too many initialisation requests`
*
* In addition to the aforementioned close events, any _non-CloseEvent_ connection problem
* is considered fatal by default. However, this specific behaviour can be altered by using
* the `shouldRetry` option.
*
* These events are reported immediately and the client will not reconnect.
*
* @default 5
*/
retryAttempts?: number;
/**
* Control the wait time between retries. You may implement your own strategy
* by timing the resolution of the returned promise with the retries count.
* `retries` argument counts actual connection attempts, so it will begin with
* 0 after the first retryable disconnect.
*
* @default 'Randomised exponential backoff'
*/
retryWait?: (retries: number) => Promise<void>;
/**
* Check if the close event or connection error is fatal. If you return `false`,
* the client will fail immediately without additional retries; however, if you
* return `true`, the client will keep retrying until the `retryAttempts` have
* been exceeded.
*
* The argument is whatever has been thrown during the connection phase.
*
* Beware, the library classifies a few close events as fatal regardless of
* what is returned here. They are listed in the documentation of the `retryAttempts`
* option.
*
* @default 'Only `CloseEvent`s'
*/
shouldRetry?: (errOrCloseEvent: unknown) => boolean;
/**
* Register listeners before initialising the client. This way
* you can ensure to catch all client relevant emitted events.
*
* The listeners passed in will **always** be the first ones
* to get the emitted event before other registered listeners.
*/
on?: Partial<{ [event in Event]: EventListener<event> }>;
/**
* A custom WebSocket implementation to use instead of the
* one provided by the global scope. Mostly useful for when
* using the client outside of the browser environment.
*/
webSocketImpl?: unknown;
/**
* A custom ID generator for identifying subscriptions.
*
* The default generates a v4 UUID to be used as the ID using `Math`
* as the random number generator. Supply your own generator
* in case you need more uniqueness.
*
* Reference: https://gist.github.com/jed/982883
*/
generateID?: (payload: SubscribePayload) => ID;
/**
* An optional override for the JSON.parse function used to hydrate
* incoming messages to this client. Useful for parsing custom datatypes
* out of the incoming JSON.
*/
jsonMessageReviver?: JSONMessageReviver;
/**
* An optional override for the JSON.stringify function used to serialize
* outgoing messages from this client. Useful for serializing custom
* datatypes out to the client.
*/
jsonMessageReplacer?: JSONMessageReplacer;
}
/** @category Client */
export interface Client extends Disposable {
/**
* Listens on the client which dispatches events about the socket state.
*/
on<E extends Event>(event: E, listener: EventListener<E>): () => void;
/**
* Subscribes through the WebSocket following the config parameters. It
* uses the `sink` to emit received data or errors. Returns a _cleanup_
* function used for dropping the subscription and cleaning stuff up.
*/
subscribe<Data = Record<string, unknown>, Extensions = unknown>(
payload: SubscribePayload,
sink: Sink<
| FormattedExecutionResult<Data, Extensions>
| FormattedExecutionPatchResult<Data, Extensions>
>,
): () => void;
/**
* Subscribes and iterates over emitted results from the WebSocket
* through the returned async iterator.
*/
iterate<Data = Record<string, unknown>, Extensions = unknown>(
payload: SubscribePayload,
): AsyncIterableIterator<
| FormattedExecutionResult<Data, Extensions>
| FormattedExecutionPatchResult<Data, Extensions>
>;
/**
* Terminates the WebSocket abruptly and immediately.
*
* A close event `4499: Terminated` is issued to the current WebSocket and a
* synthetic {@link TerminatedCloseEvent} is immediately emitted without waiting for
* the one coming from `WebSocket.onclose`.
*
* Terminating is not considered fatal and a connection retry will occur as expected.
*
* Useful in cases where the WebSocket is stuck and not emitting any events;
* can happen on iOS Safari, see: https://github.com/enisdenjo/graphql-ws/discussions/290.
*/
terminate(): void;
}
/**
* Creates a disposable GraphQL over WebSocket client.
*
* @category Client
*/
export function createClient<
P extends ConnectionInitMessage['payload'] = ConnectionInitMessage['payload'],
>(options: ClientOptions<P>): Client {
const {
url,
connectionParams,
lazy = true,
onNonLazyError = console.error,
lazyCloseTimeout: lazyCloseTimeoutMs = 0,
keepAlive = 0,
disablePong,
connectionAckWaitTimeout = 0,
retryAttempts = 5,
retryWait = async function randomisedExponentialBackoff(retries) {
let retryDelay = 1000; // start with 1s delay
for (let i = 0; i < retries; i++) {
retryDelay *= 2;
}
await new Promise((resolve) =>
setTimeout(
resolve,
retryDelay +
// add random timeout from 300ms to 3s
Math.floor(Math.random() * (3000 - 300) + 300),
),
);
},
shouldRetry = isLikeCloseEvent,
on,
webSocketImpl,
/**
* Generates a v4 UUID to be used as the ID using `Math`
* as the random number generator. Supply your own generator
* in case you need more uniqueness.
*
* Reference: https://gist.github.com/jed/982883
*/
generateID = function generateUUID() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
const r = (Math.random() * 16) | 0,
v = c == 'x' ? r : (r & 0x3) | 0x8;
return v.toString(16);
});
},
jsonMessageReplacer: replacer,
jsonMessageReviver: reviver,
} = options;
let ws;
if (webSocketImpl) {
if (!isWebSocket(webSocketImpl)) {
throw new Error('Invalid WebSocket implementation provided');
}
ws = webSocketImpl;
} else if (typeof WebSocket !== 'undefined') {
ws = WebSocket;
} else if (typeof global !== 'undefined') {
ws =
global.WebSocket ||
// @ts-expect-error: Support more browsers
global.MozWebSocket;
} else if (typeof window !== 'undefined') {
ws =
window.WebSocket ||
// @ts-expect-error: Support more browsers
window.MozWebSocket;
}
if (!ws)
throw new Error(
"WebSocket implementation missing; on Node you can `import WebSocket from 'ws';` and pass `webSocketImpl: WebSocket` to `createClient`",
);
const WebSocketImpl = ws;
// websocket status emitter, subscriptions are handled differently
const emitter = (() => {
const message = (() => {
const listeners: { [key: string]: EventMessageListener } = {};
return {
on(id: string, listener: EventMessageListener) {
listeners[id] = listener;
return () => {
delete listeners[id];
};
},
emit(message: Message) {
if ('id' in message) listeners[message.id]?.(message);
},
};
})();
const listeners: { [event in Event]: EventListener<event>[] } = {
connecting: on?.connecting ? [on.connecting] : [],
opened: on?.opened ? [on.opened] : [],
connected: on?.connected ? [on.connected] : [],
ping: on?.ping ? [on.ping] : [],
pong: on?.pong ? [on.pong] : [],
message: on?.message ? [message.emit, on.message] : [message.emit],
closed: on?.closed ? [on.closed] : [],
error: on?.error ? [on.error] : [],
};
return {
onMessage: message.on,
on<E extends Event>(event: E, listener: EventListener<E>) {
const l = listeners[event] as EventListener<E>[];
l.push(listener);
return () => {
l.splice(l.indexOf(listener), 1);
};
},
emit<E extends Event>(event: E, ...args: Parameters<EventListener<E>>) {
// we copy the listeners so that unlistens dont "pull the rug under our feet"
for (const listener of [...listeners[event]]) {
// @ts-expect-error: The args should fit
listener(...args);
}
},
};
})();
// invokes the callback either when an error or closed event is emitted,
// first one that gets called prevails, other emissions are ignored
function errorOrClosed(cb: (errOrEvent: unknown) => void) {
const listening = [
// errors are fatal and more critical than close events, throw them first
emitter.on('error', (err) => {
listening.forEach((unlisten) => unlisten());
cb(err);
}),
// closes can be graceful and not fatal, throw them second (if error didnt throw)
emitter.on('closed', (event) => {
listening.forEach((unlisten) => unlisten());
cb(event);
}),
];
}
type Connected = [socket: WebSocket, throwOnClose: Promise<void>];
let connecting: Promise<Connected> | undefined,
locks = 0,
lazyCloseTimeout: ReturnType<typeof setTimeout>,
retrying = false,
retries = 0,
disposed = false;
async function connect(): Promise<
[
socket: WebSocket,
release: () => void,
waitForReleaseOrThrowOnClose: Promise<void>,
]
> {
// clear the lazy close timeout immediatelly so that close gets debounced
// see: https://github.com/enisdenjo/graphql-ws/issues/388
clearTimeout(lazyCloseTimeout);
const [socket, throwOnClose] = await (connecting ??
(connecting = new Promise<Connected>((connected, denied) =>
(async () => {
if (retrying) {
await retryWait(retries);
// subscriptions might complete while waiting for retry
if (!locks) {
connecting = undefined;
return denied({ code: 1000, reason: 'All Subscriptions Gone' });
}
retries++;
}
emitter.emit('connecting', retrying);
const socket = new WebSocketImpl(
typeof url === 'function' ? await url() : url,
GRAPHQL_TRANSPORT_WS_PROTOCOL,
);
let connectionAckTimeout: ReturnType<typeof setTimeout>,
queuedPing: ReturnType<typeof setTimeout>;
function enqueuePing() {
if (isFinite(keepAlive) && keepAlive > 0) {
clearTimeout(queuedPing); // in case where a pong was received before a ping (this is valid behaviour)
queuedPing = setTimeout(() => {
if (socket.readyState === WebSocketImpl.OPEN) {
socket.send(stringifyMessage({ type: MessageType.Ping }));
emitter.emit('ping', false, undefined);
}
}, keepAlive);
}
}
errorOrClosed((errOrEvent) => {
connecting = undefined;
clearTimeout(connectionAckTimeout);
clearTimeout(queuedPing);
denied(errOrEvent);
if (errOrEvent instanceof TerminatedCloseEvent) {
socket.close(4499, 'Terminated'); // close event is artificial and emitted manually, see `Client.terminate()` below
socket.onerror = null;
socket.onclose = null;
}
});
socket.onerror = (err) => emitter.emit('error', err);
socket.onclose = (event) => emitter.emit('closed', event);
socket.onopen = async () => {
try {
emitter.emit('opened', socket);
const payload =
typeof connectionParams === 'function'
? await connectionParams()
: connectionParams;
// connectionParams might take too long causing the server to kick off the client
// the necessary error/close event is already reported - simply stop execution
if (socket.readyState !== WebSocketImpl.OPEN) return;
socket.send(
stringifyMessage<MessageType.ConnectionInit>(
payload
? {
type: MessageType.ConnectionInit,
payload,
}
: {
type: MessageType.ConnectionInit,
// payload is completely absent if not provided
},
replacer,
),
);
if (
isFinite(connectionAckWaitTimeout) &&
connectionAckWaitTimeout > 0
) {
connectionAckTimeout = setTimeout(() => {
socket.close(
CloseCode.ConnectionAcknowledgementTimeout,
'Connection acknowledgement timeout',
);
}, connectionAckWaitTimeout);
}
enqueuePing(); // enqueue ping (noop if disabled)
} catch (err) {
emitter.emit('error', err);
socket.close(
CloseCode.InternalClientError,
limitCloseReason(
err instanceof Error ? err.message : String(err),
'Internal client error',
),
);
}
};
let acknowledged = false;
socket.onmessage = ({ data }) => {
try {
const message = parseMessage(data, reviver);
emitter.emit('message', message);
if (message.type === 'ping' || message.type === 'pong') {
emitter.emit(message.type, true, message.payload); // received
if (message.type === 'pong') {
enqueuePing(); // enqueue next ping (noop if disabled)
} else if (!disablePong) {
// respond with pong on ping
socket.send(
stringifyMessage(
message.payload
? {
type: MessageType.Pong,
payload: message.payload,
}
: {
type: MessageType.Pong,
// payload is completely absent if not provided
},
),
);
emitter.emit('pong', false, message.payload);
}
return; // ping and pongs can be received whenever
}
if (acknowledged) return; // already connected and acknowledged
if (message.type !== MessageType.ConnectionAck)
throw new Error(
`First message cannot be of type ${message.type}`,
);
clearTimeout(connectionAckTimeout);
acknowledged = true;
emitter.emit('connected', socket, message.payload, retrying); // connected = socket opened + acknowledged
retrying = false; // future lazy connects are not retries
retries = 0; // reset the retries on connect
connected([
socket,
new Promise<void>((_, reject) => errorOrClosed(reject)),
]);
} catch (err) {
socket.onmessage = null; // stop reading messages as soon as reading breaks once
emitter.emit('error', err);
socket.close(
CloseCode.BadResponse,
limitCloseReason(
err instanceof Error ? err.message : String(err),
'Bad response',
),
);
}
};
})(),
)));
// if the provided socket is in a closing state, wait for the throw on close
if (socket.readyState === WebSocketImpl.CLOSING) await throwOnClose;
let release = () => {
// releases this connection
};
const released = new Promise<void>((resolve) => (release = resolve));
return [
socket,
release,
Promise.race([
// wait for
released.then(() => {
if (!locks) {
// and if no more locks are present, complete the connection
const complete = () => socket.close(1000, 'Normal Closure');
if (isFinite(lazyCloseTimeoutMs) && lazyCloseTimeoutMs > 0) {
// if the keepalive is set, allow for the specified calmdown time and
// then complete if the socket is still open.
lazyCloseTimeout = setTimeout(() => {
if (socket.readyState === WebSocketImpl.OPEN) complete();
}, lazyCloseTimeoutMs);
} else {
// otherwise complete immediately
complete();
}
}
}),
// or
throwOnClose,
]),
];
}
/**
* Checks the `connect` problem and evaluates if the client should retry.
*/
function shouldRetryConnectOrThrow(errOrCloseEvent: unknown): boolean {
// some close codes are worth reporting immediately
if (
isLikeCloseEvent(errOrCloseEvent) &&
(isFatalInternalCloseCode(errOrCloseEvent.code) ||
[
CloseCode.InternalServerError,
CloseCode.InternalClientError,
CloseCode.BadRequest,
CloseCode.BadResponse,
CloseCode.Unauthorized,
// CloseCode.Forbidden, might grant access out after retry
CloseCode.SubprotocolNotAcceptable,
// CloseCode.ConnectionInitialisationTimeout, might not time out after retry
// CloseCode.ConnectionAcknowledgementTimeout, might not time out after retry
CloseCode.SubscriberAlreadyExists,
CloseCode.TooManyInitialisationRequests,
// 4499, // Terminated, probably because the socket froze, we want to retry
].includes(errOrCloseEvent.code))
)
throw errOrCloseEvent;
// client was disposed, no retries should proceed regardless
if (disposed) return false;
// normal closure (possibly all subscriptions have completed)
// if no locks were acquired in the meantime, shouldnt try again
if (isLikeCloseEvent(errOrCloseEvent) && errOrCloseEvent.code === 1000)
return locks > 0;
// retries are not allowed or we tried to many times, report error
if (!retryAttempts || retries >= retryAttempts) throw errOrCloseEvent;
// throw non-retryable connection problems
if (!shouldRetry(errOrCloseEvent)) throw errOrCloseEvent;
// looks good, start retrying
return (retrying = true);
}
// in non-lazy (hot?) mode always hold one connection lock to persist the socket
if (!lazy) {
(async () => {
locks++;
for (;;) {
try {
const [, , throwOnClose] = await connect();
await throwOnClose; // will always throw because releaser is not used
} catch (errOrCloseEvent) {
try {
if (!shouldRetryConnectOrThrow(errOrCloseEvent)) return;
} catch (errOrCloseEvent) {
// report thrown error, no further retries
return onNonLazyError?.(errOrCloseEvent);
}
}
}
})();
}
function subscribe(payload: SubscribePayload, sink: Sink) {
const id = generateID(payload);
let done = false,
errored = false,
releaser = () => {
// for handling completions before connect
locks--;
done = true;
};
(async () => {
locks++;
for (;;) {
try {
const [socket, release, waitForReleaseOrThrowOnClose] =
await connect();
// if done while waiting for connect, release the connection lock right away
if (done) return release();
const unlisten = emitter.onMessage(id, (message) => {
switch (message.type) {
case MessageType.Next: {
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- payload will fit type
sink.next(message.payload as any);
return;
}
case MessageType.Error: {
(errored = true), (done = true);
sink.error(message.payload);
releaser();
return;
}
case MessageType.Complete: {
done = true;
releaser(); // release completes the sink
return;
}
}
});
socket.send(
stringifyMessage<MessageType.Subscribe>(
{
id,
type: MessageType.Subscribe,
payload,
},
replacer,
),
);
releaser = () => {
if (!done && socket.readyState === WebSocketImpl.OPEN)
// if not completed already and socket is open, send complete message to server on release
socket.send(
stringifyMessage<MessageType.Complete>(
{
id,
type: MessageType.Complete,
},
replacer,
),
);
locks--;
done = true;
release();
};
// either the releaser will be called, connection completed and
// the promise resolved or the socket closed and the promise rejected.
// whatever happens though, we want to stop listening for messages
await waitForReleaseOrThrowOnClose.finally(unlisten);
return; // completed, shouldnt try again
} catch (errOrCloseEvent) {
if (!shouldRetryConnectOrThrow(errOrCloseEvent)) return;
}
}
})()
.then(() => {
// delivering either an error or a complete terminates the sequence
if (!errored) sink.complete();
}) // resolves on release or normal closure
.catch((err) => {
sink.error(err);
}); // rejects on close events and errors
return () => {
// dispose only of active subscriptions
if (!done) releaser();
};
}
return {
on: emitter.on,
subscribe,
iterate(request) {
const pending: any[] = [];
const deferred = {
done: false,
error: null as unknown,
resolve: () => {
// noop
},
};
const dispose = subscribe(request, {
next(val) {
pending.push(val);
deferred.resolve();
},
error(err) {
deferred.done = true;
deferred.error = err;
deferred.resolve();
},
complete() {
deferred.done = true;
deferred.resolve();
},
});
const iterator = (async function* iterator() {
for (;;) {
if (!pending.length) {
// only wait if there are no pending messages available
await new Promise<void>((resolve) => (deferred.resolve = resolve));
}