UNPKG

1.63 kBJavaScriptView Raw
1
2/**
3 * request.js
4 *
5 * Request class contains server only options
6 */
7
8var parse_url = require('url').parse;
9
10module.exports = Request;
11
12/**
13 * Request class
14 *
15 * @param Mixed input Url or Request instance
16 * @param Object init Custom options
17 * @return Void
18 */
19function Request(input, init) {
20 var url, url_parsed;
21
22 // normalize input
23 if (!(input instanceof Request)) {
24 url = input;
25 url_parsed = parse_url(url);
26 input = {};
27 } else {
28 url = input.url;
29 url_parsed = parse_url(url);
30 }
31
32 if (!url_parsed.protocol || !url_parsed.hostname) {
33 throw new Error('only absolute urls are supported');
34 }
35
36 if (url_parsed.protocol !== 'http:' && url_parsed.protocol !== 'https:') {
37 throw new Error('only http(s) protocols are supported');
38 }
39
40 // normalize init
41 init = init || {};
42
43 // fetch spec options
44 this.method = init.method || input.method || 'GET';
45 this.headers = init.headers || input.headers || {};
46 this.body = init.body || input.body;
47 this.url = url;
48
49 // server only options
50 this.follow = init.follow !== undefined ?
51 init.follow : input.follow !== undefined ?
52 input.follow : 20;
53 this.counter = init.counter || input.follow || 0;
54 this.timeout = init.timeout || input.timeout || 0;
55 this.compress = init.compress !== undefined ?
56 init.compress : input.compress !== undefined ?
57 input.compress : true;
58 this.size = init.size || input.size || 0;
59 this.agent = init.agent || input.agent;
60
61 // server request options
62 this.protocol = url_parsed.protocol;
63 this.hostname = url_parsed.hostname;
64 this.port = url_parsed.port;
65 this.path = url_parsed.path;
66 this.auth = url_parsed.auth;
67}