-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
wsLink.ts
497 lines (453 loc) · 13.3 KB
/
wsLink.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
import type { Observer, UnsubscribeFn } from '@trpc/server/observable';
import { observable } from '@trpc/server/observable';
import type {
AnyRouter,
inferClientTypes,
inferRouterError,
MaybePromise,
ProcedureType,
TRPCClientIncomingMessage,
TRPCClientIncomingRequest,
TRPCClientOutgoingMessage,
TRPCRequestMessage,
TRPCResponseMessage,
} from '@trpc/server/unstable-core-do-not-import';
import { transformResult } from '@trpc/server/unstable-core-do-not-import';
import { TRPCClientError } from '../TRPCClientError';
import type { TransformerOptions } from '../unstable-internals';
import { getTransformer } from '../unstable-internals';
import type { Operation, TRPCLink } from './types';
const run = <TResult>(fn: () => TResult): TResult => fn();
type WSCallbackResult<TRouter extends AnyRouter, TOutput> = TRPCResponseMessage<
TOutput,
inferRouterError<TRouter>
>;
type WSCallbackObserver<TRouter extends AnyRouter, TOutput> = Observer<
WSCallbackResult<TRouter, TOutput>,
TRPCClientError<TRouter>
>;
const exponentialBackoff = (attemptIndex: number) =>
attemptIndex === 0 ? 0 : Math.min(1000 * 2 ** attemptIndex, 30000);
export interface WebSocketClientOptions {
/**
* The URL to connect to (can be a function that returns a URL)
*/
url: string | (() => MaybePromise<string>);
/**
* Ponyfill which WebSocket implementation to use
*/
WebSocket?: typeof WebSocket;
/**
* The number of milliseconds before a reconnect is attempted.
* @default {@link exponentialBackoff}
*/
retryDelayMs?: typeof exponentialBackoff;
/**
* Triggered when a WebSocket connection is established
*/
onOpen?: () => void;
/**
* Triggered when a WebSocket connection is closed
*/
onClose?: (cause?: { code?: number }) => void;
/**
* Lazy mode will close the WebSocket automatically after a period of inactivity (no messages sent or received and no pending requests)
*/
lazy?: {
/**
* Enable lazy mode
* @default false
*/
enabled: boolean;
/**
* Close the WebSocket after this many milliseconds
* @default 0
*/
closeMs: number;
};
}
type LazyOptions = Required<NonNullable<WebSocketClientOptions['lazy']>>;
const lazyDefaults: LazyOptions = {
enabled: false,
closeMs: 0,
};
export function createWSClient(opts: WebSocketClientOptions) {
const {
url,
WebSocket: WebSocketImpl = WebSocket,
retryDelayMs: retryDelayFn = exponentialBackoff,
onOpen,
onClose,
} = opts;
const lazyOpts: LazyOptions = {
...lazyDefaults,
...opts.lazy,
};
/* istanbul ignore next -- @preserve */
if (!WebSocketImpl) {
throw new Error(
"No WebSocket implementation found - you probably don't want to use this on the server, but if you do you need to pass a `WebSocket`-ponyfill",
);
}
/**
* outgoing messages buffer whilst not open
*/
let outgoing: TRPCClientOutgoingMessage[] = [];
/**
* pending outgoing requests that are awaiting callback
*/
type TCallbacks = WSCallbackObserver<AnyRouter, unknown>;
type TRequest = {
/**
* Reference to the WebSocket instance this request was made to
*/
connection: Connection | null;
type: ProcedureType;
callbacks: TCallbacks;
op: Operation;
};
const pendingRequests: Record<number | string, TRequest> =
Object.create(null);
let connectAttempt = 0;
let connectTimer: ReturnType<typeof setTimeout> | undefined = undefined;
let connectionIndex = 0;
let lazyDisconnectTimer: ReturnType<typeof setTimeout> | undefined =
undefined;
let activeConnection: null | Connection = lazyOpts.enabled
? null
: createConnection();
type Connection = {
id: number;
} & (
| {
state: 'open';
ws: WebSocket;
}
| {
state: 'closed';
ws: WebSocket;
}
| {
state: 'connecting';
ws?: WebSocket;
}
);
/**
* tries to send the list of messages
*/
function dispatch() {
if (!activeConnection) {
activeConnection = createConnection();
return;
}
// using a timeout to batch messages
setTimeout(() => {
if (activeConnection?.state !== 'open') {
return;
}
for (const pending of Object.values(pendingRequests)) {
if (!pending.connection) {
pending.connection = activeConnection;
}
}
if (outgoing.length === 1) {
// single send
activeConnection.ws.send(JSON.stringify(outgoing.pop()));
} else {
// batch send
activeConnection.ws.send(JSON.stringify(outgoing));
}
// clear
outgoing = [];
startLazyDisconnectTimer();
});
}
function tryReconnect(conn: Connection) {
if (!!connectTimer) {
return;
}
conn.state = 'connecting';
const timeout = retryDelayFn(connectAttempt++);
reconnectInMs(timeout);
}
function hasPendingRequests(conn?: Connection) {
const requests = Object.values(pendingRequests);
if (!conn) {
return requests.length > 0;
}
return requests.some((req) => req.connection === conn);
}
function reconnect() {
if (lazyOpts.enabled && !hasPendingRequests()) {
// Skip reconnecting if there are pending requests and we're in lazy mode
return;
}
const oldConnection = activeConnection;
activeConnection = createConnection();
oldConnection && closeIfNoPending(oldConnection);
}
function reconnectInMs(ms: number) {
if (connectTimer) {
return;
}
connectTimer = setTimeout(reconnect, ms);
}
function closeIfNoPending(conn: Connection) {
// disconnect as soon as there are are no pending requests
if (!hasPendingRequests(conn)) {
conn.ws?.close();
}
}
function resumeSubscriptionOnReconnect(req: TRequest) {
if (outgoing.some((r) => r.id === req.op.id)) {
return;
}
request(req.op, req.callbacks);
}
const startLazyDisconnectTimer = () => {
if (!lazyOpts.enabled) {
return;
}
clearTimeout(lazyDisconnectTimer);
lazyDisconnectTimer = setTimeout(() => {
if (!activeConnection) {
return;
}
if (!hasPendingRequests(activeConnection)) {
activeConnection.ws?.close();
activeConnection = null;
}
}, lazyOpts.closeMs);
};
function createConnection(): Connection {
const self: Connection = {
id: ++connectionIndex,
state: 'connecting',
} as Connection;
clearTimeout(lazyDisconnectTimer);
const onError = () => {
self.state = 'closed';
if (self === activeConnection) {
tryReconnect(self);
}
};
run(async () => {
const urlString = typeof url === 'function' ? await url() : url;
const ws = new WebSocketImpl(urlString);
self.ws = ws;
clearTimeout(connectTimer);
connectTimer = undefined;
ws.addEventListener('open', () => {
/* istanbul ignore next -- @preserve */
if (activeConnection?.ws !== ws) {
return;
}
connectAttempt = 0;
self.state = 'open';
onOpen?.();
dispatch();
});
ws.addEventListener('error', onError);
const handleIncomingRequest = (req: TRPCClientIncomingRequest) => {
if (self !== activeConnection) {
return;
}
if (req.method === 'reconnect') {
reconnect();
// notify subscribers
for (const pendingReq of Object.values(pendingRequests)) {
if (pendingReq.type === 'subscription') {
resumeSubscriptionOnReconnect(pendingReq);
}
}
}
};
const handleIncomingResponse = (data: TRPCResponseMessage) => {
const req = data.id !== null && pendingRequests[data.id];
if (!req) {
// do something?
return;
}
req.callbacks.next?.(data);
if (self === activeConnection && req.connection !== activeConnection) {
// gracefully replace old connection with this
const oldConn = req.connection;
req.connection = self;
oldConn && closeIfNoPending(oldConn);
}
if (
'result' in data &&
data.result.type === 'stopped' &&
activeConnection === self
) {
req.callbacks.complete();
}
};
ws.addEventListener('message', ({ data }) => {
startLazyDisconnectTimer();
const msg = JSON.parse(data) as TRPCClientIncomingMessage;
if ('method' in msg) {
handleIncomingRequest(msg);
} else {
handleIncomingResponse(msg);
}
if (self !== activeConnection) {
// when receiving a message, we close old connection that has no pending requests
closeIfNoPending(self);
}
});
ws.addEventListener('close', ({ code }) => {
if (self.state === 'open') {
onClose?.({ code });
}
self.state = 'closed';
if (activeConnection === self) {
// connection might have been replaced already
tryReconnect(self);
}
for (const [key, req] of Object.entries(pendingRequests)) {
if (req.connection !== self) {
continue;
}
if (self.state === 'closed') {
// If the connection was closed, we just call `complete()` on the request
delete pendingRequests[key];
req.callbacks.complete?.();
continue;
}
// The connection was closed either unexpectedly or because of a reconnect
if (req.type === 'subscription') {
// Subscriptions will resume after we've reconnected
resumeSubscriptionOnReconnect(req);
} else {
// Queries and mutations will error if interrupted
delete pendingRequests[key];
req.callbacks.error?.(
TRPCClientError.from(
new TRPCWebSocketClosedError('WebSocket closed prematurely'),
),
);
}
}
});
}).catch(onError);
return self;
}
function request(op: Operation, callbacks: TCallbacks): UnsubscribeFn {
const { type, input, path, id } = op;
const envelope: TRPCRequestMessage = {
id,
method: type,
params: {
input,
path,
},
};
pendingRequests[id] = {
connection: null,
type,
callbacks,
op,
};
// enqueue message
outgoing.push(envelope);
dispatch();
return () => {
const callbacks = pendingRequests[id]?.callbacks;
delete pendingRequests[id];
outgoing = outgoing.filter((msg) => msg.id !== id);
callbacks?.complete?.();
if (activeConnection?.state === 'open' && op.type === 'subscription') {
outgoing.push({
id,
method: 'subscription.stop',
});
dispatch();
}
startLazyDisconnectTimer();
};
}
return {
close: () => {
connectAttempt = 0;
for (const req of Object.values(pendingRequests)) {
if (req.type === 'subscription') {
req.callbacks.complete();
} else if (!req.connection) {
// close pending requests that aren't attached to a connection yet
req.callbacks.error(
TRPCClientError.from(
new Error('Closed before connection was established'),
),
);
}
}
activeConnection && closeIfNoPending(activeConnection);
clearTimeout(connectTimer);
connectTimer = undefined;
activeConnection = null;
},
request,
get connection() {
return activeConnection;
},
};
}
export type TRPCWebSocketClient = ReturnType<typeof createWSClient>;
export type WebSocketLinkOptions<TRouter extends AnyRouter> = {
client: TRPCWebSocketClient;
} & TransformerOptions<inferClientTypes<TRouter>>;
class TRPCWebSocketClosedError extends Error {
constructor(message: string) {
super(message);
this.name = 'TRPCWebSocketClosedError';
Object.setPrototypeOf(this, TRPCWebSocketClosedError.prototype);
}
}
/**
* @link https://trpc.io/docs/v11/client/links/wsLink
*/
export function wsLink<TRouter extends AnyRouter>(
opts: WebSocketLinkOptions<TRouter>,
): TRPCLink<TRouter> {
const transformer = getTransformer(opts.transformer);
return () => {
const { client } = opts;
return ({ op }) => {
return observable((observer) => {
const { type, path, id, context } = op;
const input = transformer.input.serialize(op.input);
const unsub = client.request(
{ type, path, input, id, context },
{
error(err) {
observer.error(err as TRPCClientError<any>);
unsub();
},
complete() {
observer.complete();
},
next(message) {
const transformed = transformResult(message, transformer.output);
if (!transformed.ok) {
observer.error(TRPCClientError.from(transformed.error));
return;
}
observer.next({
result: transformed.result,
});
if (op.type !== 'subscription') {
// if it isn't a subscription we don't care about next response
unsub();
observer.complete();
}
},
},
);
return () => {
unsub();
};
});
};
};
}