UNPKG

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