~ubuntu-branches/ubuntu/utopic/moodle/utopic

« back to all changes in this revision

Viewing changes to lib/yuilib/3.9.1/build/io-nodejs/io-nodejs.js

  • Committer: Package Import Robot
  • Author(s): Thijs Kinkhorst
  • Date: 2014-05-12 16:10:38 UTC
  • mfrom: (36.1.3 sid)
  • Revision ID: package-import@ubuntu.com-20140512161038-puyqf65k4e0s8ytz
Tags: 2.6.3-1
New upstream release.

Show diffs side-by-side

added added

removed removed

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