|
1 YUI.add('io-nodejs', function (Y, NAME) { |
|
2 |
|
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 */ |
|
4 /** |
|
5 * Node.js override for IO, methods are mixed into `Y.IO` |
|
6 * @module io-nodejs |
|
7 * @main io-nodejs |
|
8 */ |
|
9 /** |
|
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 |
|
12 * the IO abstraction. |
|
13 * @method request |
|
14 * @static |
|
15 * @for IO |
|
16 */ |
|
17 if (!Y.IO.request) { |
|
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}); |
|
21 } |
|
22 |
|
23 var codes = require('http').STATUS_CODES; |
|
24 |
|
25 /** |
|
26 Flatten headers object |
|
27 @method flatten |
|
28 @protected |
|
29 @for IO |
|
30 @param {Object} o The headers object |
|
31 @return {String} The flattened headers object |
|
32 */ |
|
33 var flatten = function(o) { |
|
34 var str = []; |
|
35 Object.keys(o).forEach(function(name) { |
|
36 str.push(name + ': ' + o[name]); |
|
37 }); |
|
38 return str.join('\n'); |
|
39 }; |
|
40 |
|
41 |
|
42 /** |
|
43 NodeJS IO transport, uses the NodeJS <a href="https://github.com/mikeal/request">request</a> |
|
44 module under the hood to perform all network IO. |
|
45 @method transports.nodejs |
|
46 @for IO |
|
47 @static |
|
48 @return {Object} This object contains only a `send` method that accepts a |
|
49 `transaction object`, `uri` and the `config object`. |
|
50 @example |
|
51 |
|
52 Y.io('https://somedomain.com/url', { |
|
53 method: 'PUT', |
|
54 data: '?foo=bar', |
|
55 //Extra request module config options. |
|
56 request: { |
|
57 maxRedirects: 100, |
|
58 strictSSL: true, |
|
59 multipart: [ |
|
60 { |
|
61 'content-type': 'application/json', |
|
62 body: JSON.stringify({ |
|
63 foo: 'bar', |
|
64 _attachments: { |
|
65 'message.txt': { |
|
66 follows: true, |
|
67 length: 18, |
|
68 'content_type': 'text/plain' |
|
69 } |
|
70 } |
|
71 }) |
|
72 }, |
|
73 { |
|
74 body: 'I am an attachment' |
|
75 } |
|
76 ] |
|
77 }, |
|
78 on: { |
|
79 success: function(id, e) { |
|
80 } |
|
81 } |
|
82 }); |
|
83 */ |
|
84 |
|
85 Y.IO.transports.nodejs = function() { |
|
86 return { |
|
87 send: function (transaction, uri, config) { |
|
88 |
|
89 config.notify('start', transaction, config); |
|
90 config.method = config.method || 'GET'; |
|
91 config.method = config.method.toUpperCase(); |
|
92 |
|
93 var rconf = { |
|
94 method: config.method, |
|
95 uri: uri |
|
96 }; |
|
97 |
|
98 if (config.data) { |
|
99 if (Y.Lang.isString(config.data)) { |
|
100 rconf.body = config.data; |
|
101 } |
|
102 if (rconf.body && rconf.method === 'GET') { |
|
103 rconf.uri += (rconf.uri.indexOf('?') > -1 ? '&' : '?') + rconf.body; |
|
104 rconf.body = ''; |
|
105 } |
|
106 } |
|
107 if (config.headers) { |
|
108 rconf.headers = config.headers; |
|
109 } |
|
110 if (config.timeout) { |
|
111 rconf.timeout = config.timeout; |
|
112 } |
|
113 if (config.request) { |
|
114 Y.mix(rconf, config.request); |
|
115 } |
|
116 Y.IO.request(rconf, function(err, data) { |
|
117 |
|
118 if (err) { |
|
119 transaction.c = err; |
|
120 config.notify(((err.code === 'ETIMEDOUT') ? 'timeout' : 'failure'), transaction, config); |
|
121 return; |
|
122 } |
|
123 if (data) { |
|
124 transaction.c = { |
|
125 status: data.statusCode, |
|
126 statusCode: data.statusCode, |
|
127 statusText: codes[data.statusCode], |
|
128 headers: data.headers, |
|
129 responseText: data.body || '', |
|
130 responseXML: null, |
|
131 getResponseHeader: function(name) { |
|
132 return this.headers[name]; |
|
133 }, |
|
134 getAllResponseHeaders: function() { |
|
135 return flatten(this.headers); |
|
136 } |
|
137 }; |
|
138 } |
|
139 |
|
140 config.notify('complete', transaction, config); |
|
141 config.notify(((data && (data.statusCode >= 200 && data.statusCode <= 299)) ? 'success' : 'failure'), transaction, config); |
|
142 }); |
|
143 |
|
144 var ret = { |
|
145 io: transaction |
|
146 }; |
|
147 return ret; |
|
148 } |
|
149 }; |
|
150 }; |
|
151 |
|
152 Y.IO.defaultTransport('nodejs'); |
|
153 |
|
154 |
|
155 |
|
156 }, '@VERSION@', {"requires": ["io-base"]}); |