]> git.wh0rd.org Git - tt-rss.git/blob - lib/dojo/request/node.js.uncompressed.js
upgrade dojo to 1.8.3 (refs #570)
[tt-rss.git] / lib / dojo / request / node.js.uncompressed.js
1 define("dojo/request/node", [
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;
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                                 handlers(response);
96                                 def.resolve(response);
97                         });
98                 });
99
100                 req.on('error', def.reject);
101
102                 if(options.data){
103                         if(typeof options.data === "string"){
104                                 req.end(options.data);
105                         }else{
106                                 options.data.pipe(req);
107                         }
108                 }else{
109                         req.end();
110                 }
111
112                 if(options.timeout){
113                         var timeout = setTimeout(function(){
114                                 def.cancel(new RequestTimeoutError(response));
115                         }, options.timeout);
116                 }
117
118                 return def.promise;
119         }
120
121         /*=====
122         node = function(url, options){
123                 // summary:
124                 //              Sends a request using the included http or https interface from node.js
125                 //              with the given URL and options.
126                 // url: String
127                 //              URL to request
128                 // options: dojo/request/node.__Options?
129                 //              Options for the request.
130                 // returns: dojo/request.__Promise
131         };
132         node.__BaseOptions = declare(request.__BaseOptions, {
133                 // data: String|Object|Stream?
134                 //              Data to transfer. This is ignored for GET and DELETE
135                 //              requests.
136                 // headers: Object?
137                 //              Headers to use for the request.
138                 // user: String?
139                 //              Username to use during the request.
140                 // password: String?
141                 //              Password to use during the request.
142         });
143         node.__MethodOptions = declare(null, {
144                 // method: String?
145                 //              The HTTP method to use to make the request. Must be
146                 //              uppercase. Default is `"GET"`.
147         });
148         node.__Options = declare([node.__BaseOptions, node.__MethodOptions]);
149
150         node.get = function(url, options){
151                 // summary:
152                 //              Send an HTTP GET request using XMLHttpRequest with the given URL and options.
153                 // url: String
154                 //              URL to request
155                 // options: dojo/request/node.__BaseOptions?
156                 //              Options for the request.
157                 // returns: dojo/request.__Promise
158         };
159         node.post = function(url, options){
160                 // summary:
161                 //              Send an HTTP POST request using XMLHttpRequest with the given URL and options.
162                 // url: String
163                 //              URL to request
164                 // options: dojo/request/node.__BaseOptions?
165                 //              Options for the request.
166                 // returns: dojo/request.__Promise
167         };
168         node.put = function(url, options){
169                 // summary:
170                 //              Send an HTTP PUT request using XMLHttpRequest with the given URL and options.
171                 // url: String
172                 //              URL to request
173                 // options: dojo/request/node.__BaseOptions?
174                 //              Options for the request.
175                 // returns: dojo/request.__Promise
176         };
177         node.del = function(url, options){
178                 // summary:
179                 //              Send an HTTP DELETE request using XMLHttpRequest with the given URL and options.
180                 // url: String
181                 //              URL to request
182                 // options: dojo/request/node.__BaseOptions?
183                 //              Options for the request.
184                 // returns: dojo/request.__Promise
185         };
186         =====*/
187
188         util.addCommonMethods(node);
189
190         return node;
191 });