src/cm/media/js/lib/yui/yui3-3.15.0/build/io-nodejs/io-nodejs-debug.js
changeset 602 e16a97fb364a
equal deleted inserted replaced
601:d334a616c023 602:e16a97fb364a
       
     1 YUI.add('io-nodejs', function (Y, NAME) {
       
     2 
       
     3 /*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 */
       
     4     /**
       
     5     * Node.js override for IO, methods are mixed into `Y.IO`
       
     6     * @module io-nodejs
       
     7     * @main io-nodejs
       
     8     */
       
     9     /**
       
    10     * Passthru to the NodeJS <a href="https://github.com/mikeal/request">request</a> module.
       
    11     * This method is return of `require('request')` so you can use it inside NodeJS without
       
    12     * the IO abstraction.
       
    13     * @method request
       
    14     * @static
       
    15     * @for IO
       
    16     */
       
    17     if (!Y.IO.request) {
       
    18         // Default Request's cookie jar to `false`. This way cookies will not be
       
    19         // maintained across requests.
       
    20         Y.IO.request = require('request').defaults({jar: false});
       
    21     }
       
    22 
       
    23     var codes = require('http').STATUS_CODES;
       
    24 
       
    25     /**
       
    26     Flatten headers object
       
    27     @method flatten
       
    28     @protected
       
    29     @for IO
       
    30     @param {Object} o The headers object
       
    31     @return {String} The flattened headers object
       
    32     */
       
    33     var flatten = function(o) {
       
    34         var str = [];
       
    35         Object.keys(o).forEach(function(name) {
       
    36             str.push(name + ': ' + o[name]);
       
    37         });
       
    38         return str.join('\n');
       
    39     };
       
    40 
       
    41     Y.log('Loading NodeJS Request Transport', 'info', 'io');
       
    42 
       
    43     /**
       
    44     NodeJS IO transport, uses the NodeJS <a href="https://github.com/mikeal/request">request</a>
       
    45     module under the hood to perform all network IO.
       
    46     @method transports.nodejs
       
    47     @for IO
       
    48     @static
       
    49     @return {Object} This object contains only a `send` method that accepts a
       
    50     `transaction object`, `uri` and the `config object`.
       
    51     @example
       
    52 
       
    53         Y.io('https://somedomain.com/url', {
       
    54             method: 'PUT',
       
    55             data: '?foo=bar',
       
    56             //Extra request module config options.
       
    57             request: {
       
    58                 maxRedirects: 100,
       
    59                 strictSSL: true,
       
    60                 multipart: [
       
    61                     {
       
    62                         'content-type': 'application/json',
       
    63                         body: JSON.stringify({
       
    64                             foo: 'bar',
       
    65                             _attachments: {
       
    66                                 'message.txt': {
       
    67                                     follows: true,
       
    68                                     length: 18,
       
    69                                     'content_type': 'text/plain'
       
    70                                 }
       
    71                             }
       
    72                         })
       
    73                     },
       
    74                     {
       
    75                         body: 'I am an attachment'
       
    76                     }
       
    77                 ]
       
    78             },
       
    79             on: {
       
    80                 success: function(id, e) {
       
    81                     Y.log(e.responseText);
       
    82                 }
       
    83             }
       
    84         });
       
    85     */
       
    86 
       
    87     Y.IO.transports.nodejs = function() {
       
    88         return {
       
    89             send: function (transaction, uri, config) {
       
    90 
       
    91                 Y.log('Starting Request Transaction', 'info', 'io');
       
    92                 config.notify('start', transaction, config);
       
    93                 config.method = config.method || 'GET';
       
    94                 config.method = config.method.toUpperCase();
       
    95 
       
    96                 var rconf = {
       
    97                     method: config.method,
       
    98                     uri: uri
       
    99                 };
       
   100 
       
   101                 if (config.data) {
       
   102                     if (Y.Lang.isString(config.data)) {
       
   103                         rconf.body = config.data;
       
   104                     }
       
   105                     if (rconf.body && rconf.method === 'GET') {
       
   106                         rconf.uri += (rconf.uri.indexOf('?') > -1 ? '&' : '?') + rconf.body;
       
   107                         rconf.body = '';
       
   108                     }
       
   109                 }
       
   110                 if (config.headers) {
       
   111                     rconf.headers = config.headers;
       
   112                 }
       
   113                 if (config.timeout) {
       
   114                     rconf.timeout = config.timeout;
       
   115                 }
       
   116                 if (config.request) {
       
   117                     Y.mix(rconf, config.request);
       
   118                 }
       
   119                 Y.log('Initiating ' + rconf.method + ' request to: ' + rconf.uri, 'info', 'io');
       
   120                 Y.IO.request(rconf, function(err, data) {
       
   121                     Y.log('Request Transaction Complete', 'info', 'io');
       
   122 
       
   123                     if (err) {
       
   124                         Y.log('An IO error occurred', 'warn', 'io');
       
   125                         transaction.c = err;
       
   126                         config.notify(((err.code === 'ETIMEDOUT') ? 'timeout' : 'failure'), transaction, config);
       
   127                         return;
       
   128                     }
       
   129                     if (data) {
       
   130                         transaction.c = {
       
   131                             status: data.statusCode,
       
   132                             statusCode: data.statusCode,
       
   133                             statusText: codes[data.statusCode],
       
   134                             headers: data.headers,
       
   135                             responseText: data.body || '',
       
   136                             responseXML: null,
       
   137                             getResponseHeader: function(name) {
       
   138                                 return this.headers[name];
       
   139                             },
       
   140                             getAllResponseHeaders: function() {
       
   141                                 return flatten(this.headers);
       
   142                             }
       
   143                         };
       
   144                     }
       
   145                     Y.log('Request Transaction Complete', 'info', 'io');
       
   146 
       
   147                     config.notify('complete', transaction, config);
       
   148                     config.notify(((data && (data.statusCode >= 200 && data.statusCode <= 299)) ? 'success' : 'failure'), transaction, config);
       
   149                 });
       
   150 
       
   151                 var ret = {
       
   152                     io: transaction
       
   153                 };
       
   154                 return ret;
       
   155             }
       
   156         };
       
   157     };
       
   158 
       
   159     Y.IO.defaultTransport('nodejs');
       
   160 
       
   161 
       
   162 
       
   163 }, '@VERSION@', {"requires": ["io-base"]});