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