MDL-32843 import YUI 3.5.1
[moodle.git] / lib / yui / 3.5.1 / build / io-nodejs / io-nodejs.js
blob13f18c9ccc0b3d54b3c66d0f2d583cb9d7b1d473
1 /*
2 YUI 3.5.1 (build 22)
3 Copyright 2012 Yahoo! Inc. All rights reserved.
4 Licensed under the BSD License.
5 http://yuilibrary.com/license/
6 */
7 YUI.add('io-nodejs', function(Y) {
9 /*global Y: false, Buffer: false, clearInterval: false, clearTimeout: false, console: false, exports: false, global: false, module: false, process: false, querystring: false, require: false, setInterval: false, setTimeout: false, __filename: false, __dirname: false */   
11     /**
12     * Passthru to the NodeJS <a href="https://github.com/mikeal/request">request</a> module.
13     * This method is return of `require('request')` so you can use it inside NodeJS without
14     * the IO abstraction.
15     * @method request
16     * @static
17     */
18     if (!Y.IO.request) {
19         Y.IO.request = require('request');
20     }
23     /**
24     NodeJS IO transport, uses the NodeJS <a href="https://github.com/mikeal/request">request</a>
25     module under the hood to perform all network IO.
26     @method transports.nodejs
27     @static
28     @returns {Object} This object contains only a `send` method that accepts a
29     `transaction object`, `uri` and the `config object`.
30     @example
31         
32         Y.io('https://somedomain.com/url', {
33             method: 'PUT',
34             data: '?foo=bar',
35             //Extra request module config options.
36             request: {
37                 maxRedirects: 100,
38                 strictSSL: true,
39                 multipart: [
40                     {
41                         'content-type': 'application/json',
42                         body: JSON.stringify({
43                             foo: 'bar',
44                             _attachments: {
45                                 'message.txt': {
46                                     follows: true,
47                                     length: 18,
48                                     'content_type': 'text/plain'
49                                 }
50                             }
51                         })
52                     },
53                     {
54                         body: 'I am an attachment'
55                     }
56                 ] 
57             },
58             on: {
59                 success: function(id, e) {
60                 }
61             }
62         });
63     */
65     var flatten = function(o) {
66         var str = [];
67         Object.keys(o).forEach(function(name) {
68             str.push(name + ': ' + o[name]);
69         });
70         return str.join('\n');
71     };
73     Y.IO.transports.nodejs = function() {
74         return {
75             send: function (transaction, uri, config) {
77                 config.notify('start', transaction, config);
78                 config.method = config.method || 'GET';
80                 var rconf = {
81                     method: config.method,
82                     uri: uri
83                 };
84                 if (config.data) {
85                     rconf.body = config.data;
86                 }
87                 if (config.headers) {
88                     rconf.headers = config.headers;
89                 }
90                 if (config.timeout) {
91                     rconf.timeout = config.timeout;
92                 }
93                 if (config.request) {
94                     Y.mix(rconf, config.request);
95                 }
96                 Y.IO.request(rconf, function(err, data) {
98                     if (err) {
99                         transaction.c = err;
100                         config.notify(((err.code === 'ETIMEDOUT') ? 'timeout' : 'failure'), transaction, config);
101                         return;
102                     }
103                     if (data) {
104                         transaction.c = {
105                             status: data.statusCode,
106                             statusCode: data.statusCode,
107                             headers: data.headers,
108                             responseText: data.body,
109                             responseXML: null,
110                             getResponseHeader: function(name) {
111                                 return this.headers[name];
112                             },
113                             getAllResponseHeaders: function() {
114                                 return flatten(this.headers);
115                             }
116                         };
117                     }
119                     config.notify('complete', transaction, config);
120                     config.notify(((data && (data.statusCode >= 200 && data.statusCode <= 299)) ? 'success' : 'failure'), transaction, config);
121                 });
122                 
123                 var ret = {
124                     io: transaction
125                 };
126                 return ret;
127             }
128         };
129     };
131     Y.IO.defaultTransport('nodejs');
135 }, '3.5.1' ,{requires:['io-base']});