This repository has been archived by the owner on Apr 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 345
/
client.ts
321 lines (271 loc) · 9.23 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
import * as Backoff from 'backo2';
import {EventEmitter, ListenerFn} from 'eventemitter3';
declare let window: any;
const _global = typeof global !== 'undefined' ? global : (typeof window !== 'undefined' ? window : {});
const NativeWebSocket = _global.WebSocket || _global.MozWebSocket;
import {
SUBSCRIPTION_FAIL,
SUBSCRIPTION_DATA,
SUBSCRIPTION_START,
SUBSCRIPTION_SUCCESS,
SUBSCRIPTION_END,
KEEPALIVE,
INIT,
INIT_FAIL,
INIT_SUCCESS,
} from './messageTypes';
import { GRAPHQL_SUBSCRIPTIONS } from './protocols';
import isString = require('lodash.isstring');
import isObject = require('lodash.isobject');
export * from './helpers';
export interface SubscriptionOptions {
query: string;
variables?: Object;
operationName?: string;
context?: any;
}
export interface Subscription {
options: SubscriptionOptions;
handler: (error: Error[], result?: any) => void;
}
export interface Subscriptions {
[id: string]: Subscription;
}
export type ConnectionParams = {[paramName: string]: any};
export interface ClientOptions {
connectionParams?: ConnectionParams;
timeout?: number;
reconnect?: boolean;
reconnectionAttempts?: number;
connectionCallback?: (error: Error[], result?: any) => void;
}
const DEFAULT_SUBSCRIPTION_TIMEOUT = 5000;
export class SubscriptionClient {
public client: any;
public subscriptions: Subscriptions;
private url: string;
private maxId: number;
private connectionParams: ConnectionParams;
private subscriptionTimeout: number;
private waitingSubscriptions: {[id: string]: boolean}; // subscriptions waiting for SUBSCRIPTION_SUCCESS
private unsentMessagesQueue: Array<any>; // queued messages while websocket is opening.
private reconnect: boolean;
private reconnecting: boolean;
private reconnectionAttempts: number;
private reconnectSubscriptions: Subscriptions;
private backoff: any;
private connectionCallback: any;
private eventEmitter: EventEmitter;
private wsImpl: any;
constructor(url: string, options?: ClientOptions, webSocketImpl?: any) {
const {
connectionCallback = undefined,
connectionParams = {},
timeout = DEFAULT_SUBSCRIPTION_TIMEOUT,
reconnect = false,
reconnectionAttempts = Infinity,
} = (options || {});
this.wsImpl = webSocketImpl || NativeWebSocket;
if (!this.wsImpl) {
throw new Error('Unable to find native implementation, or alternative implementation for WebSocket!');
}
this.connectionParams = connectionParams;
this.connectionCallback = connectionCallback;
this.url = url;
this.subscriptions = {};
this.maxId = 0;
this.subscriptionTimeout = timeout;
this.waitingSubscriptions = {};
this.unsentMessagesQueue = [];
this.reconnect = reconnect;
this.reconnectSubscriptions = {};
this.reconnecting = false;
this.reconnectionAttempts = reconnectionAttempts;
this.backoff = new Backoff({ jitter: 0.5 });
this.eventEmitter = new EventEmitter();
this.connect();
}
public get status() {
return this.client.readyState;
}
public close() {
this.client.close();
}
public subscribe(options: SubscriptionOptions, handler: (error: Error[], result?: any) => void) {
const { query, variables, operationName, context } = options;
if (!query) {
throw new Error('Must provide `query` to subscribe.');
}
if (!handler) {
throw new Error('Must provide `handler` to subscribe.');
}
if (
!isString(query) ||
( operationName && !isString(operationName)) ||
( variables && !isObject(variables))
) {
throw new Error('Incorrect option types to subscribe. `subscription` must be a string,' +
'`operationName` must be a string, and `variables` must be an object.');
}
const subId = this.generateSubscriptionId();
let message = Object.assign(options, {type: SUBSCRIPTION_START, id: subId});
this.sendMessage(message);
this.subscriptions[subId] = {options, handler};
this.waitingSubscriptions[subId] = true;
setTimeout( () => {
if (this.waitingSubscriptions[subId]) {
handler([new Error('Subscription timed out - no response from server')]);
this.unsubscribe(subId);
}
}, this.subscriptionTimeout);
return subId;
}
public on(eventName: string, callback: ListenerFn, context?: any): Function {
const handler = this.eventEmitter.on(eventName, callback, context);
return () => {
handler.off(eventName, callback, context);
};
}
public onConnect(callback: ListenerFn, context?: any): Function {
return this.on('connect', callback, context);
}
public onDisconnect(callback: ListenerFn, context?: any): Function {
return this.on('disconnect', callback, context);
}
public onReconnect(callback: ListenerFn, context?: any): Function {
return this.on('reconnect', callback, context);
}
public unsubscribe(id: number) {
delete this.subscriptions[id];
delete this.waitingSubscriptions[id];
let message = { id: id, type: SUBSCRIPTION_END};
this.sendMessage(message);
}
public unsubscribeAll() {
Object.keys(this.subscriptions).forEach( subId => {
this.unsubscribe(parseInt(subId));
});
}
// send message, or queue it if connection is not open
private sendMessage(message: Object) {
switch (this.client.readyState) {
case this.client.OPEN:
// TODO: throw error if message isn't json serializable?
this.client.send(JSON.stringify(message));
break;
case this.client.CONNECTING:
this.unsentMessagesQueue.push(message);
break;
case this.client.CLOSING:
case this.client.CLOSED:
default:
if (!this.reconnecting) {
throw new Error('Client is not connected to a websocket.');
}
}
}
private generateSubscriptionId() {
const id = this.maxId;
this.maxId += 1;
return id;
}
// ensure we have an array of errors
private formatErrors(errors: any) {
if (Array.isArray(errors)) {
return errors;
}
if (errors && errors.message) {
return [errors];
}
return [{ message: 'Unknown error' }];
}
private tryReconnect() {
if (!this.reconnect) {
return;
}
if (this.backoff.attempts > this.reconnectionAttempts) {
return;
}
if (!this.reconnecting) {
this.reconnectSubscriptions = this.subscriptions;
this.subscriptions = {};
this.waitingSubscriptions = {};
this.reconnecting = true;
}
const delay = this.backoff.duration();
setTimeout(() => {
this.connect(true);
}, delay);
}
private connect(isReconnect: boolean = false) {
this.client = new this.wsImpl(this.url, GRAPHQL_SUBSCRIPTIONS);
this.client.onopen = () => {
this.eventEmitter.emit(isReconnect ? 'reconnect' : 'connect');
this.reconnecting = false;
this.backoff.reset();
// Send INIT message, no need to wait for connection to success (reduce roundtrips)
this.sendMessage({type: INIT, payload: this.connectionParams});
Object.keys(this.reconnectSubscriptions).forEach((key) => {
const { options, handler } = this.reconnectSubscriptions[key];
this.subscribe(options, handler);
});
this.unsentMessagesQueue.forEach((message) => {
this.client.send(JSON.stringify(message));
});
this.unsentMessagesQueue = [];
};
this.client.onclose = () => {
this.eventEmitter.emit('disconnect');
this.tryReconnect();
};
this.client.onerror = () => {
// Capture and ignore errors to prevent unhandled exceptions, wait for
// onclose to fire before attempting a reconnect.
};
this.client.onmessage = ({ data }: {data: any}) => {
let parsedMessage: any;
try {
parsedMessage = JSON.parse(data);
} catch (e) {
throw new Error(`Message must be JSON-parseable. Got: ${data}`);
}
const subId = parsedMessage.id;
if ([KEEPALIVE, INIT_SUCCESS, INIT_FAIL].indexOf(parsedMessage.type) === -1 && !this.subscriptions[subId]) {
this.unsubscribe(subId);
if (parsedMessage.type === KEEPALIVE) {
return;
}
}
// console.log('MSG', JSON.stringify(parsedMessage, null, 2));
switch (parsedMessage.type) {
case INIT_FAIL:
if (this.connectionCallback) {
this.connectionCallback(parsedMessage.payload.error);
}
break;
case INIT_SUCCESS:
if (this.connectionCallback) {
this.connectionCallback();
}
break;
case SUBSCRIPTION_SUCCESS:
delete this.waitingSubscriptions[subId];
break;
case SUBSCRIPTION_FAIL:
this.subscriptions[subId].handler(this.formatErrors(parsedMessage.payload.errors), null);
delete this.subscriptions[subId];
delete this.waitingSubscriptions[subId];
break;
case SUBSCRIPTION_DATA:
const payloadData = parsedMessage.payload.data || null;
const payloadErrors = parsedMessage.payload.errors ? this.formatErrors(parsedMessage.payload.errors) : null;
this.subscriptions[subId].handler(payloadErrors, payloadData);
break;
case KEEPALIVE:
break;
default:
throw new Error('Invalid message type!');
}
};
}
}