ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav GlassYUI.add('io-nodejs', function(Y) {
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass
d2c4d6671bde02c4f15e2dd6a9d39e59b2761633Dav Glass/*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 */
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass /**
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass * Passthru to the NodeJS <a href="https://github.com/mikeal/request">request</a> module.
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass * This method is return of `require('request')` so you can use it inside NodeJS without
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass * the IO abstraction.
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass * @method request
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass * @static
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass */
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass if (!Y.IO.request) {
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass Y.IO.request = require('request');
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass }
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass Y.log('Loading NodeJS Request Transport', 'info', 'io');
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass /**
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass NodeJS IO transport, uses the NodeJS <a href="https://github.com/mikeal/request">request</a>
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass module under the hood to perform all network IO.
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass @method transports.nodejs
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass @static
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass @returns {Object} This object contains only a `send` method that accepts a
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass `transaction object`, `uri` and the `config object`.
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass @example
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass Y.io('https://somedomain.com/url', {
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass method: 'PUT',
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass data: '?foo=bar',
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass //Extra request module config options.
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass request: {
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass maxRedirects: 100,
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass strictSSL: true,
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass multipart: [
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass {
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass 'content-type': 'application/json',
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass body: JSON.stringify({
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass foo: 'bar',
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass _attachments: {
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass 'message.txt': {
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass follows: true,
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass length: 18,
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass 'content_type': 'text/plain'
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass }
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass }
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass })
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass },
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass {
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass body: 'I am an attachment'
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass }
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass ]
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass },
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass on: {
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass success: function(id, e) {
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass Y.log(e.responseText);
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass }
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass }
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass });
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass */
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass
d2c4d6671bde02c4f15e2dd6a9d39e59b2761633Dav Glass var flatten = function(o) {
d2c4d6671bde02c4f15e2dd6a9d39e59b2761633Dav Glass var str = [];
d2c4d6671bde02c4f15e2dd6a9d39e59b2761633Dav Glass Object.keys(o).forEach(function(name) {
d2c4d6671bde02c4f15e2dd6a9d39e59b2761633Dav Glass str.push(name + ': ' + o[name]);
d2c4d6671bde02c4f15e2dd6a9d39e59b2761633Dav Glass });
d2c4d6671bde02c4f15e2dd6a9d39e59b2761633Dav Glass return str.join('\n');
d2c4d6671bde02c4f15e2dd6a9d39e59b2761633Dav Glass };
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass
d2c4d6671bde02c4f15e2dd6a9d39e59b2761633Dav Glass Y.IO.transports.nodejs = function() {
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass return {
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass send: function (transaction, uri, config) {
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass Y.log('Starting Request Transaction', 'info', 'io');
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass config.notify('start', transaction, config);
d2c4d6671bde02c4f15e2dd6a9d39e59b2761633Dav Glass config.method = config.method || 'GET';
d2c4d6671bde02c4f15e2dd6a9d39e59b2761633Dav Glass
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass var rconf = {
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass method: config.method,
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass uri: uri
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass };
d2c4d6671bde02c4f15e2dd6a9d39e59b2761633Dav Glass if (config.data) {
d2c4d6671bde02c4f15e2dd6a9d39e59b2761633Dav Glass rconf.body = config.data;
d2c4d6671bde02c4f15e2dd6a9d39e59b2761633Dav Glass }
d2c4d6671bde02c4f15e2dd6a9d39e59b2761633Dav Glass if (config.headers) {
d2c4d6671bde02c4f15e2dd6a9d39e59b2761633Dav Glass rconf.headers = config.headers;
d2c4d6671bde02c4f15e2dd6a9d39e59b2761633Dav Glass }
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass if (config.timeout) {
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass rconf.timeout = config.timeout;
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass }
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass if (config.request) {
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass Y.mix(rconf, config.request);
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass }
d2c4d6671bde02c4f15e2dd6a9d39e59b2761633Dav Glass Y.log('Initiating ' + rconf.method + ' request to: ' + rconf.uri, 'info', 'io');
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass Y.IO.request(rconf, function(err, data) {
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass Y.log('Request Transaction Complete', 'info', 'io');
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass if (err) {
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass Y.log('An IO error occurred', 'warn', 'io');
d2c4d6671bde02c4f15e2dd6a9d39e59b2761633Dav Glass transaction.c = err;
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass config.notify(((err.code === 'ETIMEDOUT') ? 'timeout' : 'failure'), transaction, config);
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass return;
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass }
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass if (data) {
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass transaction.c = {
d2c4d6671bde02c4f15e2dd6a9d39e59b2761633Dav Glass status: data.statusCode,
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass statusCode: data.statusCode,
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass headers: data.headers,
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass responseText: data.body,
d2c4d6671bde02c4f15e2dd6a9d39e59b2761633Dav Glass responseXML: null,
d2c4d6671bde02c4f15e2dd6a9d39e59b2761633Dav Glass getResponseHeader: function(name) {
d2c4d6671bde02c4f15e2dd6a9d39e59b2761633Dav Glass return this.headers[name];
d2c4d6671bde02c4f15e2dd6a9d39e59b2761633Dav Glass },
d2c4d6671bde02c4f15e2dd6a9d39e59b2761633Dav Glass getAllResponseHeaders: function() {
d2c4d6671bde02c4f15e2dd6a9d39e59b2761633Dav Glass return flatten(this.headers);
d2c4d6671bde02c4f15e2dd6a9d39e59b2761633Dav Glass }
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass };
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass }
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass Y.log('Request Transaction Complete', 'info', 'io');
d2c4d6671bde02c4f15e2dd6a9d39e59b2761633Dav Glass
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass config.notify('complete', transaction, config);
e7a8de4b7486f0890b36c9effa2703630dd6faa5Dav Glass config.notify(((data && (data.statusCode >= 200 && data.statusCode <= 299)) ? 'success' : 'failure'), transaction, config);
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass });
d2c4d6671bde02c4f15e2dd6a9d39e59b2761633Dav Glass
d2c4d6671bde02c4f15e2dd6a9d39e59b2761633Dav Glass var ret = {
d2c4d6671bde02c4f15e2dd6a9d39e59b2761633Dav Glass io: transaction
d2c4d6671bde02c4f15e2dd6a9d39e59b2761633Dav Glass };
d2c4d6671bde02c4f15e2dd6a9d39e59b2761633Dav Glass return ret;
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass }
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass };
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass };
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass Y.IO.defaultTransport('nodejs');
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass
ec16856c3004ac9791cefede79e459e2f4a1d1c0Dav Glass}, '@VERSION@' ,{requires:['io-base']});