node-fetch/src/request.js

170 lines
4.2 KiB
JavaScript
Raw Normal View History

2016-04-05 10:43:12 -07:00
2015-06-03 21:05:01 -07:00
/**
* request.js
*
* Request class contains server only options
*/
import Headers from './headers.js';
2016-11-23 13:39:35 -08:00
import Body, { clone, extractContentType, getTotalBytes } from './body';
const { format: format_url, parse: parse_url } = require('url');
2016-11-23 13:39:35 -08:00
const PARSED_URL = Symbol('url');
2015-06-03 21:05:01 -07:00
/**
* Request class
*
* @param Mixed input Url or Request instance
* @param Object init Custom options
* @return Void
*/
export default class Request {
constructor(input, init = {}) {
let parsedURL;
2015-06-03 21:05:01 -07:00
// normalize input
if (!(input instanceof Request)) {
2016-10-12 19:56:47 -07:00
if (input && input.href) {
// in order to support Node.js' Url objects; though WHATWG's URL objects
// will fall into this branch also (since their `toString()` will return
// `href` property anyway)
parsedURL = parse_url(input.href);
} else {
// coerce input to a string before attempting to parse
parsedURL = parse_url(`${input}`);
2016-10-12 19:56:47 -07:00
}
input = {};
} else {
parsedURL = parse_url(input.url);
}
2015-06-03 21:05:01 -07:00
let method = init.method || input.method || 'GET';
method = method.toUpperCase();
if ((init.body != null || input instanceof Request && input.body !== null) &&
(method === 'GET' || method === 'HEAD')) {
throw new TypeError('Request with GET/HEAD method cannot have body');
}
let inputBody = init.body != null ?
init.body :
input instanceof Request && input.body !== null ?
clone(input) :
null;
Body.call(this, inputBody, {
timeout: init.timeout || input.timeout || 0,
size: init.size || input.size || 0
});
2015-06-03 21:05:01 -07:00
// fetch spec options
this.method = method;
this.redirect = init.redirect || input.redirect || 'follow';
this.headers = new Headers(init.headers || input.headers || {});
2015-06-03 21:40:01 -07:00
if (init.body != null) {
const contentType = extractContentType(this);
if (contentType !== null && !this.headers.has('Content-Type')) {
this.headers.append('Content-Type', contentType);
}
}
// server only options
this.follow = init.follow !== undefined ?
init.follow : input.follow !== undefined ?
input.follow : 20;
this.compress = init.compress !== undefined ?
init.compress : input.compress !== undefined ?
input.compress : true;
this.counter = init.counter || input.counter || 0;
this.agent = init.agent || input.agent;
2016-11-23 13:39:35 -08:00
this[PARSED_URL] = parsedURL;
Object.defineProperty(this, Symbol.toStringTag, {
value: 'Request',
writable: false,
enumerable: false,
configurable: true
});
}
get url() {
2016-11-23 13:39:35 -08:00
return format_url(this[PARSED_URL]);
}
/**
* Clone this request
*
* @return Request
*/
clone() {
return new Request(this);
}
}
2017-02-26 14:42:09 -08:00
Body.mixIn(Request.prototype);
Object.defineProperty(Request.prototype, Symbol.toStringTag, {
value: 'RequestPrototype',
writable: false,
enumerable: false,
configurable: true
});
2016-11-23 13:39:35 -08:00
export function getNodeRequestOptions(request) {
2017-02-26 15:30:43 -08:00
const parsedURL = request[PARSED_URL];
2016-11-23 13:39:35 -08:00
const headers = new Headers(request.headers);
// fetch step 3
if (!headers.has('Accept')) {
headers.set('Accept', '*/*');
2016-11-23 13:39:35 -08:00
}
// Basic fetch
2017-02-26 15:30:43 -08:00
if (!parsedURL.protocol || !parsedURL.hostname) {
throw new TypeError('Only absolute URLs are supported');
}
if (!/^https?:$/.test(parsedURL.protocol)) {
throw new TypeError('Only HTTP(S) protocols are supported');
2016-11-23 13:39:35 -08:00
}
// HTTP-network-or-cache fetch steps 5-9
let contentLengthValue = null;
if (request.body == null && /^(POST|PUT)$/i.test(request.method)) {
contentLengthValue = '0';
2016-11-23 13:39:35 -08:00
}
if (request.body != null) {
2016-11-23 13:39:35 -08:00
const totalBytes = getTotalBytes(request);
if (typeof totalBytes === 'number') {
contentLengthValue = String(totalBytes);
2016-11-23 13:39:35 -08:00
}
}
if (contentLengthValue) {
headers.set('Content-Length', contentLengthValue);
}
2016-11-23 13:39:35 -08:00
// HTTP-network-or-cache fetch step 12
if (!headers.has('User-Agent')) {
headers.set('User-Agent', 'node-fetch/1.0 (+https://github.com/bitinn/node-fetch)');
}
// HTTP-network-or-cache fetch step 16
if (request.compress) {
headers.set('Accept-Encoding', 'gzip,deflate');
}
if (!headers.has('Connection') && !request.agent) {
headers.set('Connection', 'close');
}
// HTTP-network fetch step 4
// chunked encoding is handled by Node.js
2016-11-23 13:39:35 -08:00
2017-02-26 15:30:43 -08:00
return Object.assign({}, parsedURL, {
2016-11-23 13:39:35 -08:00
method: request.method,
headers: headers.raw(),
2016-11-23 13:39:35 -08:00
agent: request.agent
});
}