[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/yuilib/3.17.2/io-nodejs/ -> io-nodejs.js (source)

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


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1