Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / socket.io-client / components / learnboost-engine.io-client / lib / transports / polling.js
1 /**
2  * Module dependencies.
3  */
4
5 var Transport = require('../transport')
6   , util = require('../util')
7   , parser = require('../parser')
8   , debug = require('debug')('engine.io-client:polling');
9
10 /**
11  * Module exports.
12  */
13
14 module.exports = Polling;
15
16 /**
17  * Global reference.
18  */
19
20 var global = 'undefined' != typeof window ? window : global;
21
22 /**
23  * Polling interface.
24  *
25  * @param {Object} opts
26  * @api private
27  */
28
29 function Polling(opts){
30   Transport.call(this, opts);
31 }
32
33 /**
34  * Inherits from Transport.
35  */
36
37 util.inherits(Polling, Transport);
38
39 /**
40  * Transport name.
41  */
42
43 Polling.prototype.name = 'polling';
44
45 /**
46  * Opens the socket (triggers polling). We write a PING message to determine
47  * when the transport is open.
48  *
49  * @api private
50  */
51
52 Polling.prototype.doOpen = function(){
53   this.poll();
54 };
55
56 /**
57  * Pauses polling.
58  *
59  * @param {Function} callback upon buffers are flushed and transport is paused
60  * @api private
61  */
62
63 Polling.prototype.pause = function(onPause){
64   var pending = 0;
65   var self = this;
66
67   this.readyState = 'pausing';
68
69   function pause(){
70     debug('paused');
71     self.readyState = 'paused';
72     onPause();
73   }
74
75   if (this.polling || !this.writable) {
76     var total = 0;
77
78     if (this.polling) {
79       debug('we are currently polling - waiting to pause');
80       total++;
81       this.once('pollComplete', function(){
82         debug('pre-pause polling complete');
83         --total || pause();
84       });
85     }
86
87     if (!this.writable) {
88       debug('we are currently writing - waiting to pause');
89       total++;
90       this.once('drain', function(){
91         debug('pre-pause writing complete');
92         --total || pause();
93       });
94     }
95   } else {
96     pause();
97   }
98 };
99
100 /**
101  * Starts polling cycle.
102  *
103  * @api public
104  */
105
106 Polling.prototype.poll = function(){
107   debug('polling');
108   this.polling = true;
109   this.doPoll();
110   this.emit('poll');
111 };
112
113 /**
114  * Overloads onData to detect payloads.
115  *
116  * @api private
117  */
118
119 Polling.prototype.onData = function(data){
120   debug('polling got data %s', data);
121   // decode payload
122   var packets = parser.decodePayload(data);
123
124   for (var i = 0, l = packets.length; i < l; i++) {
125     // if its the first message we consider the trnasport open
126     if ('opening' == this.readyState) {
127       this.onOpen();
128     }
129
130     // if its a close packet, we close the ongoing requests
131     if ('close' == packets[i].type) {
132       this.onClose();
133       return;
134     }
135
136     // otherwise bypass onData and handle the message
137     this.onPacket(packets[i]);
138   }
139
140   // if we got data we're not polling
141   this.polling = false;
142   this.emit('pollComplete');
143
144   if ('open' == this.readyState) {
145     this.poll();
146   } else {
147     debug('ignoring poll - transport state "%s"', this.readyState);
148   }
149 };
150
151 /**
152  * For polling, send a close packet.
153  *
154  * @api private
155  */
156
157 Polling.prototype.doClose = function(){
158   debug('sending close packet');
159   this.send([{ type: 'close' }]);
160 };
161
162 /**
163  * Writes a packets payload.
164  *
165  * @param {Array} data packets
166  * @param {Function} drain callback
167  * @api private
168  */
169
170 Polling.prototype.write = function(packets){
171   var self = this;
172   this.writable = false;
173   this.doWrite(parser.encodePayload(packets), function(){
174     self.writable = true;
175     self.emit('drain');
176   });
177 };
178
179 /**
180  * Generates uri for connection.
181  *
182  * @api private
183  */
184
185 Polling.prototype.uri = function(){
186   var query = this.query || {};
187   var schema = this.secure ? 'https' : 'http';
188   var port = '';
189
190   // cache busting is forced for IE / android / iOS6 ಠ_ಠ
191   if (global.ActiveXObject || util.ua.android || util.ua.ios6
192     || this.timestampRequests) {
193     query[this.timestampParam] = +new Date;
194   }
195
196   query = util.qs(query);
197
198   // avoid port if default for schema
199   if (this.port && (('https' == schema && this.port != 443)
200     || ('http' == schema && this.port != 80))) {
201     port = ':' + this.port;
202   }
203
204   // prepend ? to query
205   if (query.length) {
206     query = '?' + query;
207   }
208
209   return schema + '://' + this.host + port + this.path + query;
210 };