2015-01-26 01:02:34 -08:00
|
|
|
/**
|
2020-03-13 08:06:25 -07:00
|
|
|
* Index.js
|
2015-01-26 01:02:34 -08:00
|
|
|
*
|
2015-01-27 05:11:26 -08:00
|
|
|
* a request API compatible with window.fetch
|
2018-03-04 19:38:57 -08:00
|
|
|
*
|
|
|
|
* All spec algorithm step numbers are based on https://fetch.spec.whatwg.org/commit-snapshots/ae716822cb3a61843226cd090eefc6589446c1d2/.
|
2015-01-26 01:02:34 -08:00
|
|
|
*/
|
|
|
|
|
2018-07-22 09:58:03 -07:00
|
|
|
import http from 'http';
|
|
|
|
import https from 'https';
|
|
|
|
import zlib from 'zlib';
|
2020-03-13 08:06:25 -07:00
|
|
|
import Stream, {PassThrough, pipeline as pump} from 'stream';
|
|
|
|
import dataURIToBuffer from 'data-uri-to-buffer';
|
2018-07-22 09:58:03 -07:00
|
|
|
|
2020-05-20 23:50:31 -07:00
|
|
|
import Body, {writeToStream, getTotalBytes} from './body.js';
|
|
|
|
import Response from './response.js';
|
|
|
|
import Headers, {createHeadersLenient} from './headers.js';
|
|
|
|
import Request, {getNodeRequestOptions} from './request.js';
|
|
|
|
import FetchError from './errors/fetch-error.js';
|
|
|
|
import AbortError from './errors/abort-error.js';
|
2020-05-22 21:00:02 -07:00
|
|
|
import {isRedirect} from './utils/is-redirect.js';
|
2020-05-20 23:50:31 -07:00
|
|
|
|
|
|
|
export {default as Headers} from './headers.js';
|
|
|
|
export {default as Request} from './request.js';
|
|
|
|
export {default as Response} from './response.js';
|
|
|
|
export {default as FetchError} from './errors/fetch-error.js';
|
|
|
|
export {default as AbortError} from './errors/abort-error.js';
|
2020-05-22 21:00:02 -07:00
|
|
|
export {isRedirect};
|
2018-11-05 01:42:51 -08:00
|
|
|
|
2015-01-26 01:02:34 -08:00
|
|
|
/**
|
2016-10-10 11:50:04 -07:00
|
|
|
* Fetch function
|
2015-01-26 01:02:34 -08:00
|
|
|
*
|
2015-06-03 21:05:01 -07:00
|
|
|
* @param Mixed url Absolute url or Request instance
|
2015-01-26 01:02:34 -08:00
|
|
|
* @param Object opts Fetch options
|
|
|
|
* @return Promise
|
|
|
|
*/
|
2020-03-13 08:06:25 -07:00
|
|
|
export default function fetch(url, options_) {
|
|
|
|
// Allow custom promise
|
2016-10-10 11:50:04 -07:00
|
|
|
if (!fetch.Promise) {
|
|
|
|
throw new Error('native promise missing, set fetch.Promise to your favorite alternative');
|
2015-01-26 02:15:07 -08:00
|
|
|
}
|
2015-01-26 01:02:34 -08:00
|
|
|
|
2020-03-13 08:06:25 -07:00
|
|
|
// Regex for data uri
|
|
|
|
const dataUriRegex = /^\s*data:([a-z]+\/[a-z]+(;[a-z-]+=[a-z-]+)?)?(;base64)?,[\w!$&',()*+;=\-.~:@/?%\s]*\s*$/i;
|
|
|
|
|
|
|
|
// If valid data uri
|
|
|
|
if (dataUriRegex.test(url)) {
|
|
|
|
const data = dataURIToBuffer(url);
|
|
|
|
const res = new Response(data, {headers: {'Content-Type': data.type}});
|
|
|
|
return fetch.Promise.resolve(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If invalid data uri
|
|
|
|
if (url.toString().startsWith('data:')) {
|
|
|
|
const request = new Request(url, options_);
|
|
|
|
return fetch.Promise.reject(new FetchError(`[${request.method}] ${request.url} invalid URL`, 'system'));
|
2019-09-07 14:44:40 -07:00
|
|
|
}
|
|
|
|
|
2016-10-10 11:50:04 -07:00
|
|
|
Body.Promise = fetch.Promise;
|
2015-01-26 09:46:32 -08:00
|
|
|
|
2020-03-13 08:06:25 -07:00
|
|
|
// Wrap http.request into fetch
|
2016-10-10 11:50:04 -07:00
|
|
|
return new fetch.Promise((resolve, reject) => {
|
2020-03-13 08:06:25 -07:00
|
|
|
// Build request object
|
|
|
|
const request = new Request(url, options_);
|
2016-11-23 13:39:35 -08:00
|
|
|
const options = getNodeRequestOptions(request);
|
2015-01-26 02:15:07 -08:00
|
|
|
|
2017-04-08 18:33:46 -07:00
|
|
|
const send = (options.protocol === 'https:' ? https : http).request;
|
2020-03-13 08:06:25 -07:00
|
|
|
const {signal} = request;
|
2018-11-12 20:40:11 -08:00
|
|
|
let response = null;
|
|
|
|
|
2020-03-13 08:06:25 -07:00
|
|
|
const abort = () => {
|
|
|
|
const error = new AbortError('The operation was aborted.');
|
2018-11-12 20:40:11 -08:00
|
|
|
reject(error);
|
|
|
|
if (request.body && request.body instanceof Stream.Readable) {
|
|
|
|
request.body.destroy(error);
|
|
|
|
}
|
2020-03-13 08:06:25 -07:00
|
|
|
|
|
|
|
if (!response || !response.body) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-11-12 20:40:11 -08:00
|
|
|
response.body.emit('error', error);
|
2020-03-13 08:06:25 -07:00
|
|
|
};
|
2018-11-12 20:40:11 -08:00
|
|
|
|
|
|
|
if (signal && signal.aborted) {
|
|
|
|
abort();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const abortAndFinalize = () => {
|
|
|
|
abort();
|
|
|
|
finalize();
|
2020-03-13 08:06:25 -07:00
|
|
|
};
|
2015-01-26 02:15:07 -08:00
|
|
|
|
2020-03-13 08:06:25 -07:00
|
|
|
// Send request
|
|
|
|
const request_ = send(options);
|
2015-01-27 05:11:26 -08:00
|
|
|
|
2018-11-12 20:40:11 -08:00
|
|
|
if (signal) {
|
|
|
|
signal.addEventListener('abort', abortAndFinalize);
|
|
|
|
}
|
|
|
|
|
2018-03-24 20:48:33 -07:00
|
|
|
function finalize() {
|
2020-03-13 08:06:25 -07:00
|
|
|
request_.abort();
|
|
|
|
if (signal) {
|
|
|
|
signal.removeEventListener('abort', abortAndFinalize);
|
|
|
|
}
|
2018-03-24 20:48:33 -07:00
|
|
|
}
|
|
|
|
|
2016-11-23 13:39:35 -08:00
|
|
|
if (request.timeout) {
|
2020-03-13 08:06:25 -07:00
|
|
|
request_.setTimeout(request.timeout, () => {
|
|
|
|
finalize();
|
|
|
|
reject(new FetchError(`network timeout at: ${request.url}`, 'request-timeout'));
|
2015-02-01 04:36:17 -08:00
|
|
|
});
|
|
|
|
}
|
2015-01-26 02:15:07 -08:00
|
|
|
|
2020-03-13 08:06:25 -07:00
|
|
|
request_.on('error', err => {
|
2016-11-23 13:39:35 -08:00
|
|
|
reject(new FetchError(`request to ${request.url} failed, reason: ${err.message}`, 'system', err));
|
2018-03-24 20:48:33 -07:00
|
|
|
finalize();
|
2015-01-26 05:28:23 -08:00
|
|
|
});
|
|
|
|
|
2020-03-13 08:06:25 -07:00
|
|
|
request_.on('response', res => {
|
2020-05-22 12:19:05 -07:00
|
|
|
request_.setTimeout(0);
|
2018-03-04 19:29:59 -08:00
|
|
|
const headers = createHeadersLenient(res.headers);
|
2015-01-26 09:46:32 -08:00
|
|
|
|
2018-03-04 19:29:59 -08:00
|
|
|
// HTTP fetch step 5
|
2020-05-22 21:00:02 -07:00
|
|
|
if (isRedirect(res.statusCode)) {
|
2018-03-04 19:29:59 -08:00
|
|
|
// HTTP fetch step 5.2
|
|
|
|
const location = headers.get('Location');
|
|
|
|
|
|
|
|
// HTTP fetch step 5.3
|
2020-03-13 08:06:25 -07:00
|
|
|
const locationURL = location === null ? null : new URL(location, request.url);
|
2018-03-04 19:29:59 -08:00
|
|
|
|
|
|
|
// HTTP fetch step 5.5
|
|
|
|
switch (request.redirect) {
|
|
|
|
case 'error':
|
2019-10-10 13:26:58 -07:00
|
|
|
reject(new FetchError(`uri requested responds with a redirect, redirect mode is set to error: ${request.url}`, 'no-redirect'));
|
2018-03-24 20:48:33 -07:00
|
|
|
finalize();
|
2018-03-04 19:29:59 -08:00
|
|
|
return;
|
|
|
|
case 'manual':
|
2020-03-13 08:06:25 -07:00
|
|
|
// Node-fetch-specific step: make manual redirect a bit easier to use by setting the Location header value to the resolved URL.
|
2018-03-04 19:29:59 -08:00
|
|
|
if (locationURL !== null) {
|
2020-03-13 08:06:25 -07:00
|
|
|
// Handle corrupted header
|
2018-11-12 22:35:09 -08:00
|
|
|
try {
|
|
|
|
headers.set('Location', locationURL);
|
2020-03-13 08:06:25 -07:00
|
|
|
} catch (error) {
|
2018-11-12 22:35:09 -08:00
|
|
|
// istanbul ignore next: nodejs server prevent invalid response headers, we can't test this through normal request
|
2020-03-13 08:06:25 -07:00
|
|
|
reject(error);
|
2018-11-12 22:35:09 -08:00
|
|
|
}
|
2018-03-04 19:29:59 -08:00
|
|
|
}
|
2020-03-13 08:06:25 -07:00
|
|
|
|
2018-03-04 19:29:59 -08:00
|
|
|
break;
|
2020-03-13 08:06:25 -07:00
|
|
|
case 'follow': {
|
2018-03-04 19:29:59 -08:00
|
|
|
// HTTP-redirect fetch step 2
|
|
|
|
if (locationURL === null) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// HTTP-redirect fetch step 5
|
|
|
|
if (request.counter >= request.follow) {
|
|
|
|
reject(new FetchError(`maximum redirect reached at: ${request.url}`, 'max-redirect'));
|
2018-03-24 20:48:33 -07:00
|
|
|
finalize();
|
2018-03-04 19:29:59 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// HTTP-redirect fetch step 6 (counter increment)
|
|
|
|
// Create a new Request object.
|
2020-03-13 08:06:25 -07:00
|
|
|
const requestOptions = {
|
2018-03-04 19:29:59 -08:00
|
|
|
headers: new Headers(request.headers),
|
|
|
|
follow: request.follow,
|
|
|
|
counter: request.counter + 1,
|
|
|
|
agent: request.agent,
|
|
|
|
compress: request.compress,
|
|
|
|
method: request.method,
|
2018-11-12 20:40:11 -08:00
|
|
|
body: request.body,
|
|
|
|
signal: request.signal,
|
2019-04-26 09:46:53 -07:00
|
|
|
timeout: request.timeout
|
2018-03-04 19:29:59 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
// HTTP-redirect fetch step 9
|
|
|
|
if (res.statusCode !== 303 && request.body && getTotalBytes(request) === null) {
|
2018-03-24 20:48:33 -07:00
|
|
|
reject(new FetchError('Cannot follow redirect with body being a readable stream', 'unsupported-redirect'));
|
|
|
|
finalize();
|
|
|
|
return;
|
2018-03-04 19:29:59 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// HTTP-redirect fetch step 11
|
|
|
|
if (res.statusCode === 303 || ((res.statusCode === 301 || res.statusCode === 302) && request.method === 'POST')) {
|
2020-03-13 08:06:25 -07:00
|
|
|
requestOptions.method = 'GET';
|
|
|
|
requestOptions.body = undefined;
|
|
|
|
requestOptions.headers.delete('content-length');
|
2018-03-04 19:29:59 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// HTTP-redirect fetch step 15
|
2020-03-13 08:06:25 -07:00
|
|
|
resolve(fetch(new Request(locationURL, requestOptions)));
|
2018-03-24 20:48:33 -07:00
|
|
|
finalize();
|
2018-03-04 19:29:59 -08:00
|
|
|
return;
|
2020-03-13 08:06:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
// Do nothing
|
2015-09-28 07:30:41 -07:00
|
|
|
}
|
2016-04-05 11:47:23 -07:00
|
|
|
}
|
|
|
|
|
2020-03-13 08:06:25 -07:00
|
|
|
// Prepare response
|
2018-11-12 20:40:11 -08:00
|
|
|
res.once('end', () => {
|
2020-03-13 08:06:25 -07:00
|
|
|
if (signal) {
|
|
|
|
signal.removeEventListener('abort', abortAndFinalize);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
let body = pump(res, new PassThrough(), error => {
|
|
|
|
reject(error);
|
2018-11-12 20:40:11 -08:00
|
|
|
});
|
|
|
|
|
2020-03-13 08:06:25 -07:00
|
|
|
const responseOptions = {
|
2018-03-04 20:29:12 -08:00
|
|
|
url: request.url,
|
|
|
|
status: res.statusCode,
|
|
|
|
statusText: res.statusMessage,
|
2020-03-13 08:06:25 -07:00
|
|
|
headers,
|
2018-03-04 20:29:12 -08:00
|
|
|
size: request.size,
|
2019-04-30 22:05:32 -07:00
|
|
|
timeout: request.timeout,
|
2020-03-13 08:06:25 -07:00
|
|
|
counter: request.counter,
|
|
|
|
highWaterMark: request.highWaterMark
|
2016-08-03 02:31:46 -07:00
|
|
|
};
|
|
|
|
|
2018-03-04 19:38:57 -08:00
|
|
|
// HTTP-network fetch step 12.1.1.3
|
2017-02-26 21:27:27 -08:00
|
|
|
const codings = headers.get('Content-Encoding');
|
|
|
|
|
2018-03-04 19:38:57 -08:00
|
|
|
// HTTP-network fetch step 12.1.1.4: handle content codings
|
2016-08-03 02:31:46 -07:00
|
|
|
|
|
|
|
// in following scenarios we ignore compression support
|
|
|
|
// 1. compression support is disabled
|
|
|
|
// 2. HEAD request
|
2017-02-26 21:27:27 -08:00
|
|
|
// 3. no Content-Encoding header
|
2016-08-03 02:31:46 -07:00
|
|
|
// 4. no content response (204)
|
|
|
|
// 5. content not modified response (304)
|
2017-02-26 21:27:27 -08:00
|
|
|
if (!request.compress || request.method === 'HEAD' || codings === null || res.statusCode === 204 || res.statusCode === 304) {
|
2020-03-13 08:06:25 -07:00
|
|
|
response = new Response(body, responseOptions);
|
2018-11-12 20:40:11 -08:00
|
|
|
resolve(response);
|
2016-08-03 02:31:46 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-15 04:45:04 -07:00
|
|
|
// For Node v6+
|
2017-02-22 00:05:55 -08:00
|
|
|
// Be less strict when decoding compressed responses, since sometimes
|
|
|
|
// servers send slightly invalid responses that are still accepted
|
|
|
|
// by common browsers.
|
|
|
|
// Always using Z_SYNC_FLUSH is what cURL does.
|
|
|
|
const zlibOptions = {
|
|
|
|
flush: zlib.Z_SYNC_FLUSH,
|
|
|
|
finishFlush: zlib.Z_SYNC_FLUSH
|
|
|
|
};
|
|
|
|
|
2020-03-13 08:06:25 -07:00
|
|
|
// For gzip
|
|
|
|
if (codings === 'gzip' || codings === 'x-gzip') {
|
|
|
|
body = pump(body, zlib.createGunzip(zlibOptions), error => {
|
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
response = new Response(body, responseOptions);
|
2018-11-12 20:40:11 -08:00
|
|
|
resolve(response);
|
2016-08-03 02:31:46 -07:00
|
|
|
return;
|
2017-02-26 21:27:27 -08:00
|
|
|
}
|
2016-08-03 02:31:46 -07:00
|
|
|
|
2020-03-13 08:06:25 -07:00
|
|
|
// For deflate
|
|
|
|
if (codings === 'deflate' || codings === 'x-deflate') {
|
|
|
|
// Handle the infamous raw deflate response from old servers
|
2016-08-03 02:31:46 -07:00
|
|
|
// a hack for old IIS and Apache servers
|
2020-03-13 08:06:25 -07:00
|
|
|
const raw = pump(res, new PassThrough(), error => {
|
|
|
|
reject(error);
|
|
|
|
});
|
2016-10-10 11:50:04 -07:00
|
|
|
raw.once('data', chunk => {
|
2020-03-13 08:06:25 -07:00
|
|
|
// See http://stackoverflow.com/questions/37519828
|
2016-08-03 02:31:46 -07:00
|
|
|
if ((chunk[0] & 0x0F) === 0x08) {
|
2020-03-13 08:06:25 -07:00
|
|
|
body = pump(body, zlib.createInflate(), error => {
|
|
|
|
reject(error);
|
|
|
|
});
|
2016-08-03 02:31:46 -07:00
|
|
|
} else {
|
2020-03-13 08:06:25 -07:00
|
|
|
body = pump(body, zlib.createInflateRaw(), error => {
|
|
|
|
reject(error);
|
|
|
|
});
|
2016-08-03 02:31:46 -07:00
|
|
|
}
|
2020-03-13 08:06:25 -07:00
|
|
|
|
|
|
|
response = new Response(body, responseOptions);
|
2018-11-12 20:40:11 -08:00
|
|
|
resolve(response);
|
2016-08-03 02:31:46 -07:00
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
2015-01-26 02:15:07 -08:00
|
|
|
|
2020-03-13 08:06:25 -07:00
|
|
|
// For br
|
2020-05-22 19:48:14 -07:00
|
|
|
if (codings === 'br') {
|
2020-03-13 08:06:25 -07:00
|
|
|
body = pump(body, zlib.createBrotliDecompress(), error => {
|
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
response = new Response(body, responseOptions);
|
2019-04-26 09:20:15 -07:00
|
|
|
resolve(response);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-03-13 08:06:25 -07:00
|
|
|
// Otherwise, use response as-is
|
|
|
|
response = new Response(body, responseOptions);
|
2018-11-12 20:40:11 -08:00
|
|
|
resolve(response);
|
2015-01-26 02:15:07 -08:00
|
|
|
});
|
|
|
|
|
2020-03-13 08:06:25 -07:00
|
|
|
writeToStream(request_, request);
|
2015-01-26 02:15:07 -08:00
|
|
|
});
|
2020-03-13 08:06:25 -07:00
|
|
|
}
|
2015-01-26 01:02:34 -08:00
|
|
|
|
2020-03-13 08:06:25 -07:00
|
|
|
// Expose Promise
|
2016-10-10 11:50:04 -07:00
|
|
|
fetch.Promise = global.Promise;
|