1 | const DevNull = require('dev-null-stream')
|
2 | const axios = require('axios');
|
3 | const EventEmitter = require('events');
|
4 | const TweetParser = require('./parse_stream');
|
5 | const http = require('http');
|
6 | const https = require('https');
|
7 | const { pipeline } = require('stream/promises');
|
8 | const RATE_LIMIT_WINDOW = 15 * 60 * 1000;
|
9 | const DATA_TIMEOUT = 30000;
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | function isTimedout(error) {
|
16 | return error.code === 'ECONNABORTED' || error.isTimeout
|
17 | }
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | function retriableStatus(resp) {
|
24 | let status = resp.status;
|
25 | const valid_statuses = [429, 420, 504, 503, 502, 500];
|
26 | return valid_statuses.includes(status);
|
27 | }
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 | function defaultNaN(num, def) {
|
34 | if(isNaN(num)) {
|
35 | return def;
|
36 | } else {
|
37 | return num;
|
38 | }
|
39 | }
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 | const sleep = (milliseconds) => { return new Promise(resolve => setTimeout(resolve, milliseconds)); }
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 | function rateLimiting(resp, last_retry) {
|
55 | const now = Date.now()
|
56 | const fallback_rate = now + RATE_LIMIT_WINDOW;
|
57 | let backoff, ratelimit, remaining;
|
58 |
|
59 | if(resp && resp.headers) {
|
60 | const headers = resp.headers;
|
61 | remaining = defaultNaN(parseInt(headers['x-rate-limit-remaining']), 0);
|
62 | ratelimit = defaultNaN(parseInt(headers['x-rate-limit-reset']), fallback_rate / 1000);
|
63 | ratelimit = new Date(ratelimit * 1000);
|
64 | } else {
|
65 | remaining = -1;
|
66 | ratelimit = fallback_rate;
|
67 | }
|
68 |
|
69 | if(remaining === 0 || resp && (resp.status === 429 || resp.status === 420)) {
|
70 | backoff = Math.min(ratelimit - now, RATE_LIMIT_WINDOW);
|
71 | } else {
|
72 | let delta = Math.min(RATE_LIMIT_WINDOW, (now - last_retry)) / RATE_LIMIT_WINDOW;
|
73 |
|
74 | backoff = Math.max(Math.floor(delta * RATE_LIMIT_WINDOW), 1000);
|
75 | }
|
76 |
|
77 | return backoff;
|
78 | }
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 | class StreamClient extends EventEmitter {
|
95 | |
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 | constructor({token, timeout = DATA_TIMEOUT, stream_timeout = DATA_TIMEOUT}) {
|
102 | super();
|
103 | this.url = 'tweets/sample/stream';
|
104 | this.timeout = timeout;
|
105 | this.twitrClient = axios.create({
|
106 | baseURL: 'https://api.twitter.com/2',
|
107 | headers: { 'Authorization': `Bearer ${token}`},
|
108 | timeout: timeout
|
109 | });
|
110 | this.stream_timeout = stream_timeout;
|
111 | }
|
112 |
|
113 | |
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 | async connect({ params = {}, max_reconnects = -1, writeable_stream = null} = {}) {
|
124 | let reconnects = -1;
|
125 | let last_try = Date.now();
|
126 | let last_error;
|
127 |
|
128 | while(max_reconnects === -1 || reconnects <= max_reconnects)
|
129 | {
|
130 | reconnects += 1;
|
131 | this.params = params;
|
132 | this.max_reconnects = max_reconnects;
|
133 | try {
|
134 | await this.buildConnection(this.params, writeable_stream);
|
135 | } catch(request_error) {
|
136 | let resp = request_error.response;
|
137 | if(axios.isCancel(request_error)) {
|
138 | return Promise.resolve();
|
139 | } else if(isTimedout(request_error) || resp && retriableStatus(resp)) {
|
140 | let timeout_wait = rateLimiting(resp, last_try);
|
141 | this.emit('reconnect', request_error, timeout_wait);
|
142 | if(!this.skip_sleep) {
|
143 | await sleep(timeout_wait);
|
144 | }
|
145 | } else {
|
146 | return Promise.reject(request_error);
|
147 | }
|
148 | }
|
149 | last_try = Date.now();
|
150 | }
|
151 |
|
152 | let reject_error = new Error({
|
153 | message: `Max reconnects exceeded (${reconnects})`,
|
154 | wrapped_error: last_error
|
155 | })
|
156 | reject_error.reconnects = reconnects;
|
157 | return Promise.reject(reject_error);
|
158 | }
|
159 |
|
160 | |
161 |
|
162 |
|
163 |
|
164 | disconnect() {
|
165 | this.emit('disconnected');
|
166 | if(this.cancelToken) {
|
167 | this.cancelToken.cancel('Disconnecting stream');
|
168 | this.cancelToken = null;
|
169 | return true;
|
170 | } else {
|
171 | return false;
|
172 | }
|
173 | }
|
174 |
|
175 | |
176 |
|
177 |
|
178 |
|
179 |
|
180 | buildStreamPromise(resp, writable_stream) {
|
181 | const hose = resp.data;
|
182 | const timer = setTimeout(() => {
|
183 | const e = new Error(`Timed out after ${this.stream_timeout / 1000} seconds of no data`);
|
184 | e.isTimeout = true;
|
185 | hose.destroy(e);
|
186 | clearTimeout(timer);
|
187 | }, this.stream_timeout);
|
188 | const jsonStream = new TweetParser({
|
189 | emitter: this,
|
190 | timer: timer
|
191 | });
|
192 | const streams = [hose, jsonStream];
|
193 | if(writable_stream) {
|
194 | streams.push(writable_stream);
|
195 | } else {
|
196 | streams.push(new DevNull());
|
197 | }
|
198 | let listener = () => {
|
199 | hose.end();
|
200 | }
|
201 |
|
202 | this.once('disconnected', listener);
|
203 | let thicc = pipeline(...streams);
|
204 | thicc.finally(() => clearTimeout(timer));
|
205 | this.emit('connected');
|
206 | return thicc;
|
207 | }
|
208 |
|
209 | |
210 |
|
211 |
|
212 |
|
213 |
|
214 |
|
215 | buildConnection(params, writable_stream) {
|
216 | if(this.cancelToken) {
|
217 | this.disconnect();
|
218 | }
|
219 | this.cancelToken = axios.CancelToken.source();
|
220 |
|
221 | let streamReq = this.twitrClient.get(this.url, {
|
222 | responseType: 'stream',
|
223 | cancelToken: this.cancelToken.token,
|
224 | params: params,
|
225 | decompress: true,
|
226 | httpAgent: new http.Agent({ keepAlive: true }),
|
227 | httpsAgent: new https.Agent({ keepAlive: true }),
|
228 | timeout: 0,
|
229 | maxContentLength: Infinity,
|
230 | });
|
231 |
|
232 | return streamReq.then((resp) => {
|
233 | return this.buildStreamPromise(resp, writable_stream);
|
234 | });
|
235 | }
|
236 | }
|
237 |
|
238 | module.exports = StreamClient;
|