1
YUI.add('io-nodejs', function (Y, NAME) {
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 */
5
* Node.js override for IO, methods are mixed into `Y.IO`
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
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});
23
var codes = require('http').STATUS_CODES;
26
Flatten headers object
30
@param {Object} o The headers object
31
@return {String} The flattened headers object
33
var flatten = function(o) {
35
Object.keys(o).forEach(function(name) {
36
str.push(name + ': ' + o[name]);
38
return str.join('\n');
41
Y.log('Loading NodeJS Request Transport', 'info', 'io');
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
49
@return {Object} This object contains only a `send` method that accepts a
50
`transaction object`, `uri` and the `config object`.
53
Y.io('https://somedomain.com/url', {
56
//Extra request module config options.
62
'content-type': 'application/json',
63
body: JSON.stringify({
69
'content_type': 'text/plain'
75
body: 'I am an attachment'
80
success: function(id, e) {
81
Y.log(e.responseText);
87
Y.IO.transports.nodejs = function() {
89
send: function (transaction, uri, config) {
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();
97
method: config.method,
102
if (Y.Lang.isString(config.data)) {
103
rconf.body = config.data;
105
if (rconf.body && rconf.method === 'GET') {
106
rconf.uri += (rconf.uri.indexOf('?') > -1 ? '&' : '?') + rconf.body;
110
if (config.headers) {
111
rconf.headers = config.headers;
113
if (config.timeout) {
114
rconf.timeout = config.timeout;
116
if (config.request) {
117
Y.mix(rconf, config.request);
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');
124
Y.log('An IO error occurred', 'warn', 'io');
126
config.notify(((err.code === 'ETIMEDOUT') ? 'timeout' : 'failure'), transaction, config);
131
status: data.statusCode,
132
statusCode: data.statusCode,
133
statusText: codes[data.statusCode],
134
headers: data.headers,
135
responseText: data.body || '',
137
getResponseHeader: function(name) {
138
return this.headers[name];
140
getAllResponseHeaders: function() {
141
return flatten(this.headers);
145
Y.log('Request Transaction Complete', 'info', 'io');
147
config.notify('complete', transaction, config);
148
config.notify(((data && (data.statusCode >= 200 && data.statusCode <= 299)) ? 'success' : 'failure'), transaction, config);
159
Y.IO.defaultTransport('nodejs');
163
}, '@VERSION@', {"requires": ["io-base"]});