-
Notifications
You must be signed in to change notification settings - Fork 195
/
Copy pathTweetStream.ts
367 lines (306 loc) · 11.3 KB
/
TweetStream.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
import { EventEmitter } from 'events';
import type { IncomingMessage, ClientRequest } from 'http';
import RequestHandlerHelper from '../client-mixins/request-handler.helper';
import { TRequestFullStreamData } from '../client-mixins/request-maker.mixin';
import { ETwitterStreamEvent } from '../types';
import TweetStreamEventCombiner from './TweetStreamEventCombiner';
import TweetStreamParser, { EStreamParserEvent } from './TweetStreamParser';
interface ITweetStreamError {
type: ETwitterStreamEvent.ConnectionError | ETwitterStreamEvent.TweetParseError
| ETwitterStreamEvent.ReconnectError | ETwitterStreamEvent.DataError | ETwitterStreamEvent.ConnectError;
error: any;
message?: string;
}
export interface IConnectTweetStreamParams {
autoReconnect: boolean;
autoReconnectRetries: number | 'unlimited';
/** Check for 'lost connection' status every `keepAliveTimeout` milliseconds. Defaults to 2 minutes (`120000`). */
keepAliveTimeout: number | 'disable';
nextRetryTimeout?: TStreamConnectRetryFn;
}
/** Returns a number of milliseconds to wait for {tryOccurence} (starting from 1) */
export type TStreamConnectRetryFn = (tryOccurence: number) => number;
// In seconds
const basicRetriesAttempt = [5, 15, 30, 60, 90, 120];
// Default retry function
const basicReconnectRetry: TStreamConnectRetryFn =
tryOccurence => tryOccurence > basicRetriesAttempt.length
? 120000
: basicRetriesAttempt[tryOccurence - 1] * 1000;
export class TweetStream<T = any> extends EventEmitter {
public autoReconnect = false;
public autoReconnectRetries = 5;
// 2 minutes without any Twitter signal
public keepAliveTimeoutMs = 1000 * 120;
public nextRetryTimeout = basicReconnectRetry;
protected retryTimeout?: NodeJS.Timeout;
protected keepAliveTimeout?: NodeJS.Timeout;
protected parser = new TweetStreamParser();
protected connectionProcessRunning = false;
protected req?: ClientRequest;
protected res?: IncomingMessage;
constructor(
protected requestData: TRequestFullStreamData,
req?: ClientRequest,
res?: IncomingMessage,
) {
super();
this.onKeepAliveTimeout = this.onKeepAliveTimeout.bind(this);
this.initEventsFromParser();
if (req && res) {
this.req = req;
this.res = res;
this.initEventsFromRequest();
}
}
// Event typings
on(event: ETwitterStreamEvent.Data, handler: (data: T) => any): this;
on(event: ETwitterStreamEvent.DataError, handler: (error: any) => any): this;
on(event: ETwitterStreamEvent.Error, handler: (errorPayload: ITweetStreamError) => any): this;
on(event: ETwitterStreamEvent.Connected, handler: () => any): this;
on(event: ETwitterStreamEvent.ConnectionLost, handler: () => any): this;
on(event: ETwitterStreamEvent.ConnectionError, handler: (error: Error) => any): this;
on(event: ETwitterStreamEvent.TweetParseError, handler: (error: Error) => any): this;
on(event: ETwitterStreamEvent.ConnectionClosed, handler: () => any): this;
on(event: ETwitterStreamEvent.DataKeepAlive, handler: () => any): this;
on(event: ETwitterStreamEvent.ReconnectAttempt, handler: (tries: number) => any): this;
on(event: ETwitterStreamEvent.ReconnectError, handler: (tries: number) => any): this;
on(event: ETwitterStreamEvent.ReconnectLimitExceeded, handler: () => any): this;
on(event: ETwitterStreamEvent.Reconnected, handler: () => any): this;
on(event: string | symbol, handler: (...args: any[]) => any): this;
on(event: string | symbol, handler: (...args: any[]) => any) {
return super.on(event, handler);
}
protected initEventsFromRequest() {
if (!this.req || !this.res) {
throw new Error('TweetStream error: You cannot init TweetStream without a request and response object.');
}
const errorHandler = (err: any) => {
this.emit(ETwitterStreamEvent.ConnectionError, err);
this.emit(ETwitterStreamEvent.Error, {
type: ETwitterStreamEvent.ConnectionError,
error: err,
message: 'Connection lost or closed by Twitter.',
});
this.onConnectionError();
};
this.req.on('error', errorHandler);
this.res.on('error', errorHandler);
// Usually, connection should not be closed by Twitter!
this.res.on('close', () => errorHandler(new Error('Connection closed by Twitter.')));
this.res.on('data', (chunk: Buffer) => {
this.resetKeepAliveTimeout();
if (chunk.toString() === '\r\n') {
return this.emit(ETwitterStreamEvent.DataKeepAlive);
}
this.parser.push(chunk.toString());
});
// Starts the keep alive timeout
this.resetKeepAliveTimeout();
}
protected initEventsFromParser() {
const payloadIsError = this.requestData.payloadIsError;
this.parser.on(EStreamParserEvent.ParsedData, (eventData: any) => {
if (payloadIsError && payloadIsError(eventData)) {
this.emit(ETwitterStreamEvent.DataError, eventData);
this.emit(ETwitterStreamEvent.Error, {
type: ETwitterStreamEvent.DataError,
error: eventData,
message: 'Twitter sent a payload that is detected as an error payload.',
});
}
else {
this.emit(ETwitterStreamEvent.Data, eventData);
}
});
this.parser.on(EStreamParserEvent.ParseError, (error: any) => {
this.emit(ETwitterStreamEvent.TweetParseError, error);
this.emit(ETwitterStreamEvent.Error, {
type: ETwitterStreamEvent.TweetParseError,
error,
message: 'Failed to parse stream data.',
});
});
}
protected resetKeepAliveTimeout() {
this.unbindKeepAliveTimeout();
if (this.keepAliveTimeoutMs !== Infinity) {
this.keepAliveTimeout = setTimeout(this.onKeepAliveTimeout, this.keepAliveTimeoutMs);
}
}
protected onKeepAliveTimeout() {
this.emit(ETwitterStreamEvent.ConnectionLost);
this.onConnectionError();
}
protected unbindTimeouts() {
this.unbindRetryTimeout();
this.unbindKeepAliveTimeout();
}
protected unbindKeepAliveTimeout() {
if (this.keepAliveTimeout) {
clearTimeout(this.keepAliveTimeout);
this.keepAliveTimeout = undefined;
}
}
protected unbindRetryTimeout() {
if (this.retryTimeout) {
clearTimeout(this.retryTimeout);
this.retryTimeout = undefined;
}
}
protected closeWithoutEmit() {
this.unbindTimeouts();
if (this.res) {
this.res.removeAllListeners();
}
if (this.req) {
this.req.removeAllListeners();
if (!this.req.destroyed) {
// Close connection silentely
this.req.destroy();
}
}
}
/** Terminate connection to Twitter. */
close() {
this.emit(ETwitterStreamEvent.ConnectionClosed);
this.closeWithoutEmit();
}
/** Unbind all listeners, and close connection. */
destroy() {
this.removeAllListeners();
this.close();
}
/**
* Make a new request that creates a new `TweetStream` instance with
* the same parameters, and bind current listeners to new stream.
*/
async clone() {
const newRequest = new RequestHandlerHelper<T>(this.requestData);
const newStream = await newRequest.makeRequestAsStream();
// Clone attached listeners
const listenerNames = this.eventNames();
for (const listener of listenerNames) {
const callbacks = this.listeners(listener);
for (const callback of callbacks) {
newStream.on(listener, callback as any);
}
}
return newStream;
}
/** Start initial stream connection, setup options on current instance and returns itself. */
async connect(options: Partial<IConnectTweetStreamParams> = {}) {
if (typeof options.autoReconnect !== 'undefined') {
this.autoReconnect = options.autoReconnect;
}
if (typeof options.autoReconnectRetries !== 'undefined') {
this.autoReconnectRetries = options.autoReconnectRetries === 'unlimited'
? Infinity
: options.autoReconnectRetries;
}
if (typeof options.keepAliveTimeout !== 'undefined') {
this.keepAliveTimeoutMs = options.keepAliveTimeout === 'disable'
? Infinity
: options.keepAliveTimeout;
}
if (typeof options.nextRetryTimeout !== 'undefined') {
this.nextRetryTimeout = options.nextRetryTimeout;
}
// Make the connection
this.unbindTimeouts();
try {
await this.reconnect();
} catch (e) {
this.emit(ETwitterStreamEvent.ConnectError, 0);
this.emit(ETwitterStreamEvent.Error, {
type: ETwitterStreamEvent.ConnectError,
error: e,
message: 'Connect error - Initial connection just failed.',
});
// Only make a reconnection attempt if autoReconnect is true!
// Otherwise, let error be propagated
if (this.autoReconnect) {
this.makeAutoReconnectRetry(0);
} else {
throw e;
}
}
return this;
}
/** Make a new request to (re)connect to Twitter. */
async reconnect() {
if (this.connectionProcessRunning) {
throw new Error('Connection process is already running.');
}
this.connectionProcessRunning = true;
try {
let initialConnection = true;
if (this.req) {
initialConnection = false;
this.closeWithoutEmit();
}
const { req, res } = await new RequestHandlerHelper(this.requestData).makeRequestAndResolveWhenReady();
this.req = req;
this.res = res;
this.emit(initialConnection ? ETwitterStreamEvent.Connected : ETwitterStreamEvent.Reconnected);
this.parser.reset();
this.initEventsFromRequest();
} finally {
this.connectionProcessRunning = false;
}
}
protected async onConnectionError(retryOccurence = 0) {
this.unbindTimeouts();
// Close the request if necessary
this.closeWithoutEmit();
// Terminate stream by events if necessary (no auto-reconnect or retries exceeded)
if (!this.autoReconnect) {
this.emit(ETwitterStreamEvent.ConnectionClosed);
return;
}
if (retryOccurence >= this.autoReconnectRetries) {
this.emit(ETwitterStreamEvent.ReconnectLimitExceeded);
this.emit(ETwitterStreamEvent.ConnectionClosed);
return;
}
// If all other conditions fails, do a reconnect attempt
try {
this.emit(ETwitterStreamEvent.ReconnectAttempt, retryOccurence);
await this.reconnect();
} catch (e) {
this.emit(ETwitterStreamEvent.ReconnectError, retryOccurence);
this.emit(ETwitterStreamEvent.Error, {
type: ETwitterStreamEvent.ReconnectError,
error: e,
message: `Reconnect error - ${retryOccurence + 1} attempts made yet.`,
});
this.makeAutoReconnectRetry(retryOccurence);
}
}
protected makeAutoReconnectRetry(retryOccurence: number) {
const nextRetry = this.nextRetryTimeout(retryOccurence + 1);
this.retryTimeout = setTimeout(() => {
this.onConnectionError(retryOccurence + 1);
}, nextRetry);
}
async *[Symbol.asyncIterator](): AsyncGenerator<T, void, undefined> {
const eventCombiner = new TweetStreamEventCombiner(this);
try {
while (true) {
if (!this.req || this.req.aborted) {
throw new Error('Connection closed');
}
if (eventCombiner.hasStack()) {
yield* eventCombiner.popStack();
}
const { type, payload } = await eventCombiner.nextEvent();
if (type === 'error') {
throw payload;
}
}
} finally {
eventCombiner.destroy();
}
}
}
export default TweetStream;