[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/yuilib/3.17.2/io-nodejs/ -> io-nodejs-debug.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      Y.log('Loading NodeJS Request Transport', 'info', 'io');
  49  
  50      /**
  51      NodeJS IO transport, uses the NodeJS <a href="https://github.com/mikeal/request">request</a>
  52      module under the hood to perform all network IO.
  53      @method transports.nodejs
  54      @for IO
  55      @static
  56      @return {Object} This object contains only a `send` method that accepts a
  57      `transaction object`, `uri` and the `config object`.
  58      @example
  59  
  60          Y.io('https://somedomain.com/url', {
  61              method: 'PUT',
  62              data: '?foo=bar',
  63              //Extra request module config options.
  64              request: {
  65                  maxRedirects: 100,
  66                  strictSSL: true,
  67                  multipart: [
  68                      {
  69                          'content-type': 'application/json',
  70                          body: JSON.stringify({
  71                              foo: 'bar',
  72                              _attachments: {
  73                                  'message.txt': {
  74                                      follows: true,
  75                                      length: 18,
  76                                      'content_type': 'text/plain'
  77                                  }
  78                              }
  79                          })
  80                      },
  81                      {
  82                          body: 'I am an attachment'
  83                      }
  84                  ]
  85              },
  86              on: {
  87                  success: function(id, e) {
  88                      Y.log(e.responseText);
  89                  }
  90              }
  91          });
  92      */
  93  
  94      Y.IO.transports.nodejs = function() {
  95          return {
  96              send: function (transaction, uri, config) {
  97  
  98                  Y.log('Starting Request Transaction', 'info', 'io');
  99                  config.notify('start', transaction, config);
 100                  config.method = config.method || 'GET';
 101                  config.method = config.method.toUpperCase();
 102  
 103                  var rconf = {
 104                      method: config.method,
 105                      uri: uri
 106                  };
 107  
 108                  if (config.data) {
 109                      if (Y.Lang.isString(config.data)) {
 110                          rconf.body = config.data;
 111                      }
 112                      if (rconf.body && rconf.method === 'GET') {
 113                          rconf.uri += (rconf.uri.indexOf('?') > -1 ? '&' : '?') + rconf.body;
 114                          rconf.body = '';
 115                      }
 116                  }
 117                  if (config.headers) {
 118                      rconf.headers = config.headers;
 119                  }
 120                  if (config.timeout) {
 121                      rconf.timeout = config.timeout;
 122                  }
 123                  if (config.request) {
 124                      Y.mix(rconf, config.request);
 125                  }
 126                  Y.log('Initiating ' + rconf.method + ' request to: ' + rconf.uri, 'info', 'io');
 127                  Y.IO.request(rconf, function(err, data) {
 128                      Y.log('Request Transaction Complete', 'info', 'io');
 129  
 130                      if (err) {
 131                          Y.log('An IO error occurred', 'warn', 'io');
 132                          transaction.c = err;
 133                          config.notify(((err.code === 'ETIMEDOUT') ? 'timeout' : 'failure'), transaction, config);
 134                          return;
 135                      }
 136                      if (data) {
 137                          transaction.c = {
 138                              status: data.statusCode,
 139                              statusCode: data.statusCode,
 140                              statusText: codes[data.statusCode],
 141                              headers: data.headers,
 142                              responseText: data.body || '',
 143                              responseXML: null,
 144                              getResponseHeader: function(name) {
 145                                  return this.headers[name];
 146                              },
 147                              getAllResponseHeaders: function() {
 148                                  return flatten(this.headers);
 149                              }
 150                          };
 151                      }
 152                      Y.log('Request Transaction Complete', 'info', 'io');
 153  
 154                      config.notify('complete', transaction, config);
 155                      config.notify(((data && (data.statusCode >= 200 && data.statusCode <= 299)) ? 'success' : 'failure'), transaction, config);
 156                  });
 157  
 158                  var ret = {
 159                      io: transaction
 160                  };
 161                  return ret;
 162              }
 163          };
 164      };
 165  
 166      Y.IO.defaultTransport('nodejs');
 167  
 168  
 169  
 170  }, '3.17.2', {"requires": ["io-base"]});


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