dojo/request/node.js

Maintainability

62.41

Lines of code

195

Created with Raphaël 2.1.002550751002015-1-52014-12-42014-12-3

2015-5-18
Maintainability: 62.41

Created with Raphaël 2.1.00501001502002015-1-52014-12-42014-12-3

2015-5-18
Lines of Code: 195

Difficulty

28.60

Estimated Errors

1.12

Function weight

By Complexity

Created with Raphaël 2.1.0node16

By SLOC

Created with Raphaël 2.1.0<anonymous>184
1
define([
2
   'require',
3
   './util',
4
   './handlers',
5
   '../errors/RequestTimeoutError',
6
   '../node!http',
7
   '../node!https',
8
   '../node!url',
9
   '../node!stream'/*=====,
10
    '../request',
11
    '../_base/declare' =====*/
12
], function(require, util, handlers, RequestTimeoutError, http, https, URL, stream/*=====, request, declare =====*/){
13
    var Stream = stream.Stream,
14
        undefined;
Column: 9 "Expected an identifier and instead saw 'undefined' (a reserved word)."
15
 
16
    var defaultOptions = {
17
        method: 'GET',
18
        query: null,
19
        data: undefined,
20
        headers: {}
21
    };
22
    function node(url, options){
23
        var response = util.parseArgs(url, util.deepCreate(defaultOptions, options), options && options.data instanceof Stream);
24
        url = response.url;
25
        options = response.options;
26
 
27
        var def = util.deferred(
28
            response,
29
            function(dfd, response){
30
                response.clientRequest.abort();
31
            }
32
        );
33
 
34
        url = URL.parse(url);
35
 
36
        var reqOptions = response.requestOptions = {
37
            hostname: url.hostname,
38
            port: url.port,
39
            socketPath: options.socketPath,
40
            method: options.method,
41
            headers: options.headers,
42
            agent: options.agent,
43
            pfx: options.pfx,
44
            key: options.key,
45
            passphrase: options.passphrase,
46
            cert: options.cert,
47
            ca: options.ca,
48
            ciphers: options.ciphers,
49
            rejectUnauthorized: options.rejectUnauthorized === false ? false : true
50
        };
51
        if(url.path){
52
            reqOptions.path = url.path;
53
        }
54
        if(options.user || options.password){
55
            reqOptions.auth = (options.user||'') + ':' + (options.password||'');
56
        }
57
        var req = response.clientRequest = (url.protocol === 'https:' ? https : http).request(reqOptions);
58
 
59
        if(options.socketOptions){
60
            if('timeout' in options.socketOptions){
61
                req.setTimeout(options.socketOptions.timeout);
62
            }
63
            if('noDelay' in options.socketOptions){
64
                req.setNoDelay(options.socketOptions.noDelay);
65
            }
66
            if('keepAlive' in options.socketOptions){
67
                var initialDelay = options.socketOptions.keepAlive;
68
                req.setKeepAlive(initialDelay >= 0, initialDelay || 0);
69
            }
70
        }
71
 
72
        req.on('socket', function(){
73
            response.hasSocket = true;
74
            def.progress(response);
75
        });
76
 
77
        req.on('response', function(clientResponse){
78
            response.clientResponse = clientResponse;
79
            response.status = clientResponse.statusCode;
80
            response.getHeader = function(headerName){
81
                return clientResponse.headers[headerName.toLowerCase()] || null;
82
            };
83
 
84
            var body = [];
85
            clientResponse.on('data', function(chunk){
86
                body.push(chunk);
87
 
88
                // TODO: progress updates via the deferred
89
            });
90
            clientResponse.on('end', function(){
91
                if(timeout){
92
                    clearTimeout(timeout);
93
                }
94
                response.text = body.join('');
95
                try{
96
                    handlers(response);
97
                    def.resolve(response);
98
                }catch(error){
99
                    def.reject(error);
100
                }
101
            });
102
        });
103
 
104
        req.on('error', def.reject);
105
 
106
        if(options.data){
107
            if(typeof options.data === 'string'){
108
                req.end(options.data);
109
            }else{
110
                options.data.pipe(req);
111
            }
112
        }else{
113
            req.end();
114
        }
115
 
116
        if(options.timeout){
117
            var timeout = setTimeout(function(){
118
                def.cancel(new RequestTimeoutError(response));
119
            }, options.timeout);
120
        }
121
 
122
        return def.promise;
123
    }
124
 
125
    /*=====
126
    node = function(url, options){
127
        // summary:
128
        //      Sends a request using the included http or https interface from node.js
129
        //      with the given URL and options.
130
        // url: String
131
        //      URL to request
132
        // options: dojo/request/node.__Options?
133
        //      Options for the request.
134
        // returns: dojo/request.__Promise
135
    };
136
    node.__BaseOptions = declare(request.__BaseOptions, {
137
        // data: String|Object|Stream?
138
        //      Data to transfer. This is ignored for GET and DELETE
139
        //      requests.
140
        // headers: Object?
141
        //      Headers to use for the request.
142
        // user: String?
143
        //      Username to use during the request.
144
        // password: String?
145
        //      Password to use during the request.
146
    });
147
    node.__MethodOptions = declare(null, {
148
        // method: String?
149
        //      The HTTP method to use to make the request. Must be
150
        //      uppercase. Default is `"GET"`.
151
    });
152
    node.__Options = declare([node.__BaseOptions, node.__MethodOptions]);
153
 
154
    node.get = function(url, options){
155
        // summary:
156
        //      Send an HTTP GET request using XMLHttpRequest with the given URL and options.
157
        // url: String
158
        //      URL to request
159
        // options: dojo/request/node.__BaseOptions?
160
        //      Options for the request.
161
        // returns: dojo/request.__Promise
162
    };
163
    node.post = function(url, options){
164
        // summary:
165
        //      Send an HTTP POST request using XMLHttpRequest with the given URL and options.
166
        // url: String
167
        //      URL to request
168
        // options: dojo/request/node.__BaseOptions?
169
        //      Options for the request.
170
        // returns: dojo/request.__Promise
171
    };
172
    node.put = function(url, options){
173
        // summary:
174
        //      Send an HTTP PUT request using XMLHttpRequest with the given URL and options.
175
        // url: String
176
        //      URL to request
177
        // options: dojo/request/node.__BaseOptions?
178
        //      Options for the request.
179
        // returns: dojo/request.__Promise
180
    };
181
    node.del = function(url, options){
182
        // summary:
183
        //      Send an HTTP DELETE request using XMLHttpRequest with the given URL and options.
184
        // url: String
185
        //      URL to request
186
        // options: dojo/request/node.__BaseOptions?
187
        //      Options for the request.
188
        // returns: dojo/request.__Promise
189
    };
190
    =====*/
191
 
192
    util.addCommonMethods(node);
193
 
194
    return node;
195
});