2 YUI 3.13.0 (build 508226d)
3 Copyright 2013 Yahoo! Inc. All rights reserved.
4 Licensed under the BSD License.
5 http://yuilibrary.com/license/
8 YUI.add('io-nodejs', function (Y, NAME) {
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 */
12 * Node.js override for IO, methods are mixed into `Y.IO`
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
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});
30 var codes = require('http').STATUS_CODES;
33 Flatten headers object
37 @param {Object} o The headers object
38 @return {String} The flattened headers object
40 var flatten = function(o) {
42 Object.keys(o).forEach(function(name) {
43 str.push(name + ': ' + o[name]);
45 return str.join('\n');
48 Y.log('Loading NodeJS Request Transport', 'info', 'io');
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
56 @return {Object} This object contains only a `send` method that accepts a
57 `transaction object`, `uri` and the `config object`.
60 Y.io('https://somedomain.com/url', {
63 //Extra request module config options.
69 'content-type': 'application/json',
70 body: JSON.stringify({
76 'content_type': 'text/plain'
82 body: 'I am an attachment'
87 success: function(id, e) {
88 Y.log(e.responseText);
94 Y.IO.transports.nodejs = function() {
96 send: function (transaction, uri, config) {
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();
104 method: config.method,
109 if (Y.Lang.isString(config.data)) {
110 rconf.body = config.data;
112 if (rconf.body && rconf.method === 'GET') {
113 rconf.uri += (rconf.uri.indexOf('?') > -1 ? '&' : '?') + rconf.body;
117 if (config.headers) {
118 rconf.headers = config.headers;
120 if (config.timeout) {
121 rconf.timeout = config.timeout;
123 if (config.request) {
124 Y.mix(rconf, config.request);
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');
131 Y.log('An IO error occurred', 'warn', 'io');
133 config.notify(((err.code === 'ETIMEDOUT') ? 'timeout' : 'failure'), transaction, config);
138 status: data.statusCode,
139 statusCode: data.statusCode,
140 statusText: codes[data.statusCode],
141 headers: data.headers,
142 responseText: data.body || '',
144 getResponseHeader: function(name) {
145 return this.headers[name];
147 getAllResponseHeaders: function() {
148 return flatten(this.headers);
152 Y.log('Request Transaction Complete', 'info', 'io');
154 config.notify('complete', transaction, config);
155 config.notify(((data && (data.statusCode >= 200 && data.statusCode <= 299)) ? 'success' : 'failure'), transaction, config);
166 Y.IO.defaultTransport('nodejs');
170 }, '3.13.0', {"requires": ["io-base"]});