-
Notifications
You must be signed in to change notification settings - Fork 329
Expand file tree
/
Copy pathfetch.js
More file actions
227 lines (198 loc) · 7.4 KB
/
fetch.js
File metadata and controls
227 lines (198 loc) · 7.4 KB
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
import { URL } from 'url';
import { h1NoCache, noCache, createUrl, AbortController, AbortError, FetchError } from '@adobe/fetch';
import log from '../logging.js';
const fetchKeepAlive = noCache({
h1: {
keepAlive: true
},
rejectUnauthorized: false // By default skip auth check for all.
}).fetch;
const fetchH1KeepAlive = h1NoCache({
h1: {
keepAlive: true
},
rejectUnauthorized: false // By default skip auth check for all.
}).fetch;
const fetchAuthorized = noCache().fetch; // `rejectUnauthorized: true` - by `fetch` default.
const fetchH1Authorized = h1NoCache().fetch; // `rejectUnauthorized: true` - by `fetch` default.
const { fetch } = noCache({
rejectUnauthorized: false // By default skip auth check for all.
});
const fetchH1 = h1NoCache({
rejectUnauthorized: false // By default skip auth check for all.
}).fetch;
function doFetch(fetch_func, h1_fetch_func, options) {
const fetch_options = Object.assign({}, options);
// Implement `qs` (get params).
var uri = options.qs ? createUrl(options.uri, options.qs) : options.uri;
// Remove hash part of url.
uri = uri.replace(/#.*/gi, '');
const abortController = new HostAbortController(uri);
// Allow request abort before finish.
fetch_options.signal = abortController.signal;
// Implement `timeout`.
const timeoutTimerId = setTimeout(() => {
abortController.abort();
}, options.timeout || CONFIG.RESPONSE_TIMEOUT);
const a_fetch_func = options.disable_http2 ? h1_fetch_func: fetch_func;
return new Promise((resolve, reject) => {
a_fetch_func(uri, fetch_options)
.then(response => {
var stream = response.body;
stream.on('end', () => {
clearTimeout(timeoutTimerId);
});
abortController.onResponse(stream);
stream.status = response.status;
if (response.url && response.url !== uri) { // Set as final destination url if Fetch follows 301/302 re-directs
stream.url = response.url;
}
stream.headers = response.headers.plain();
stream.abortController = abortController;
stream.h2 = response.httpVersion === '2.0';
resolve(stream);
})
.catch(error => {
clearTimeout(timeoutTimerId);
if (!options.disable_http2 && error.code && /^ERR_HTTP2/.test(error.code)) {
log(' -- doFetch http2 error', error.code, uri);
resolve(doFetch(fetch_func, h1_fetch_func, Object.assign({}, options, {disable_http2: true})));
} else {
if (!options.disable_http2 && error.code && error instanceof FetchError && error.code === 'ABORT_ERR') {
// Special case, when shared session request aborted by htmlparser logic.
/**
* https://polldaddy.com/poll/7451882/?s=twitter
* https://app.everviz.com/show/O0Cy7Dyt
*/
log(' -- doFetch h2 aborted error', uri);
resolve(doFetch(fetch_func, h1_fetch_func, Object.assign({}, options, {disable_http2: true})));
} else {
if (error instanceof AbortError) {
// `AbortError` before `response` occurs only on timeout.
error = 'timeout';
}
reject(error);
}
}
});
});
}
export function fetchStreamKeepAlive(options) {
return doFetch(fetchKeepAlive, fetchH1KeepAlive, options);
}
export function fetchStream(options) {
return doFetch(fetch, fetchH1, options);
};
export function fetchStreamAuthorized(options) {
return doFetch(fetchAuthorized, fetchH1Authorized, options);
};
export function fetchData(options) {
var json = options.json;
delete options.json;
var res;
const fetch_options = Object.assign({}, options);
const uri = options.qs ? createUrl(options.uri, options.qs) : options.uri;
const abortController = new HostAbortController(uri);
// Allow request abort before finish.
fetch_options.signal = abortController.signal;
// Implement `timeout`.
const timeoutTimerId = setTimeout(() => {
abortController.abort();
}, options.timeout || CONFIG.RESPONSE_TIMEOUT);
const a_fetch_func = options.disable_http2 ? fetchH1: fetch;
return new Promise((resolve, reject) => {
a_fetch_func(uri, fetch_options)
.then(response => {
var stream = response.body;
// TODO: looks like HEAD request has no END event.
stream.on('end', () => {
clearTimeout(timeoutTimerId);
});
abortController.onResponse(stream);
res = response;
if (json !== false) {
// If `json` not forbidden, read `content-type`.
json = json || (response.headers.get('content-type').indexOf('application/json') > -1);
}
if (json) {
return response.json();
} else {
return response.text();
}
})
.then(data => {
resolve({
status: res.status,
headers: res.headers.plain(),
data: data
});
})
.catch((error) => {
clearTimeout(timeoutTimerId);
reject(error);
});
});
};
const hostsCache = {};
function addController(ctrl) {
hostsCache[ctrl.host] = hostsCache[ctrl.host] || [];
hostsCache[ctrl.host].push(ctrl);
}
function tryAbortHost(host) {
var controllers = hostsCache[host];
if (controllers) {
const hasWaitingRequests = controllers.some(ctrl => ctrl.waiting);
// If all aborted or finished.
if (!hasWaitingRequests) {
while (controllers.length) {
let ctrl = controllers.pop();
ctrl.forceAbort();
}
}
}
}
function removeController(ctrl) {
var controllers = hostsCache[ctrl.host];
if (controllers) {
const idx = controllers.indexOf(ctrl);
controllers.splice(idx, 1);
}
}
class HostAbortController {
constructor(url) {
this.aborted = false;
this.responded = false;
this.url = url;
const parsedUrl = new URL(url);
this.host = parsedUrl.protocol + parsedUrl.hostname;
this.abortController = new AbortController();
addController(this);
}
onResponse(stream) {
this.stream = stream;
if (this.aborted) {
stream.pause();
}
stream.on('end', () => {
this.finished = true;
removeController(this);
tryAbortHost(this.host);
});
}
abort() {
this.stream && this.stream.pause();
this.aborted = true;
tryAbortHost(this.host);
}
forceAbort() {
if (this.aborted && !this.finished) {
this.abortController.abort();
}
}
get waiting() {
return !(this.aborted || this.finished);
}
get signal() {
return this.abortController.signal;
}
}