Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / socket.io-client / lib / util.js
1 /**
2  * socket.io
3  * Copyright(c) 2011 LearnBoost <dev@learnboost.com>
4  * MIT Licensed
5  */
6
7 (function (exports, global) {
8
9   /**
10    * Utilities namespace.
11    *
12    * @namespace
13    */
14
15   var util = exports.util = {};
16
17   /**
18    * Parses an URI
19    *
20    * @author Steven Levithan <stevenlevithan.com> (MIT license)
21    * @api public
22    */
23
24   var re = /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/;
25
26   var parts = ['source', 'protocol', 'authority', 'userInfo', 'user', 'password',
27                'host', 'port', 'relative', 'path', 'directory', 'file', 'query',
28                'anchor'];
29
30   util.parseUri = function (str) {
31     var m = re.exec(str || '')
32       , uri = {}
33       , i = 14;
34
35     while (i--) {
36       uri[parts[i]] = m[i] || '';
37     }
38
39     return uri;
40   };
41
42   /**
43    * Produces a unique url that identifies a Socket.IO connection.
44    *
45    * @param {Object} uri
46    * @api public
47    */
48
49   util.uniqueUri = function (uri) {
50     var protocol = uri.protocol
51       , host = uri.host
52       , port = uri.port;
53
54     if ('document' in global) {
55       host = host || document.domain;
56       port = port || (protocol == 'https'
57         && document.location.protocol !== 'https:' ? 443 : document.location.port);
58     } else {
59       host = host || 'localhost';
60
61       if (!port && protocol == 'https') {
62         port = 443;
63       }
64     }
65
66     return (protocol || 'http') + '://' + host + ':' + (port || 80);
67   };
68
69   /**
70    * Mergest 2 query strings in to once unique query string
71    *
72    * @param {String} base
73    * @param {String} addition
74    * @api public
75    */
76
77   util.query = function (base, addition) {
78     var query = util.chunkQuery(base || '')
79       , components = [];
80
81     util.merge(query, util.chunkQuery(addition || ''));
82     for (var part in query) {
83       if (query.hasOwnProperty(part)) {
84         components.push(part + '=' + query[part]);
85       }
86     }
87
88     return components.length ? '?' + components.join('&') : '';
89   };
90
91   /**
92    * Transforms a querystring in to an object
93    *
94    * @param {String} qs
95    * @api public
96    */
97
98   util.chunkQuery = function (qs) {
99     var query = {}
100       , params = qs.split('&')
101       , i = 0
102       , l = params.length
103       , kv;
104
105     for (; i < l; ++i) {
106       kv = params[i].split('=');
107       if (kv[0]) {
108         query[kv[0]] = kv[1];
109       }
110     }
111
112     return query;
113   };
114
115   /**
116    * Executes the given function when the page is loaded.
117    *
118    *     io.util.load(function () { console.log('page loaded'); });
119    *
120    * @param {Function} fn
121    * @api public
122    */
123
124   var pageLoaded = false;
125
126   util.load = function (fn) {
127     if ('document' in global && document.readyState === 'complete' || pageLoaded) {
128       return fn();
129     }
130
131     util.on(global, 'load', fn, false);
132   };
133
134   /**
135    * Adds an event.
136    *
137    * @api private
138    */
139
140   util.on = function (element, event, fn, capture) {
141     if (element.attachEvent) {
142       element.attachEvent('on' + event, fn);
143     } else if (element.addEventListener) {
144       element.addEventListener(event, fn, capture);
145     }
146   };
147
148   /**
149    * Generates the correct `XMLHttpRequest` for regular and cross domain requests.
150    *
151    * @param {Boolean} [xdomain] Create a request that can be used cross domain.
152    * @returns {XMLHttpRequest|false} If we can create a XMLHttpRequest.
153    * @api private
154    */
155
156   util.request = function (xdomain) {
157     // if node
158     var XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;
159     return new XMLHttpRequest();
160     // end node
161
162     if (xdomain && 'undefined' != typeof XDomainRequest && !util.ua.hasCORS) {
163       return new XDomainRequest();
164     }
165
166     if ('undefined' != typeof XMLHttpRequest && (!xdomain || util.ua.hasCORS)) {
167       return new XMLHttpRequest();
168     }
169
170     if (!xdomain) {
171       try {
172         return new ActiveXObject('Microsoft.XMLHTTP');
173       } catch(e) { }
174     }
175
176     return null;
177   };
178
179   /**
180    * XHR based transport constructor.
181    *
182    * @constructor
183    * @api public
184    */
185
186   /**
187    * Change the internal pageLoaded value.
188    */
189
190   if ('undefined' != typeof window) {
191     util.load(function () {
192       pageLoaded = true;
193     });
194   }
195
196   /**
197    * Defers a function to ensure a spinner is not displayed by the browser
198    *
199    * @param {Function} fn
200    * @api public
201    */
202
203   util.defer = function (fn) {
204     if (!util.ua.webkit || 'undefined' != typeof importScripts) {
205       return fn();
206     }
207
208     util.load(function () {
209       setTimeout(fn, 100);
210     });
211   };
212
213   /**
214    * Merges two objects.
215    *
216    * @api public
217    */
218
219   util.merge = function merge (target, additional, deep, lastseen) {
220     var seen = lastseen || []
221       , depth = typeof deep == 'undefined' ? 2 : deep
222       , prop;
223
224     for (prop in additional) {
225       if (additional.hasOwnProperty(prop) && util.indexOf(seen, prop) < 0) {
226         if (typeof target[prop] !== 'object' || !depth) {
227           target[prop] = additional[prop];
228           seen.push(additional[prop]);
229         } else {
230           util.merge(target[prop], additional[prop], depth - 1, seen);
231         }
232       }
233     }
234
235     return target;
236   };
237
238   /**
239    * Merges prototypes from objects
240    *
241    * @api public
242    */
243
244   util.mixin = function (ctor, ctor2) {
245     util.merge(ctor.prototype, ctor2.prototype);
246   };
247
248   /**
249    * Shortcut for prototypical and static inheritance.
250    *
251    * @api private
252    */
253
254   util.inherit = function (ctor, ctor2) {
255     function f() {};
256     f.prototype = ctor2.prototype;
257     ctor.prototype = new f;
258   };
259
260   /**
261    * Checks if the given object is an Array.
262    *
263    *     io.util.isArray([]); // true
264    *     io.util.isArray({}); // false
265    *
266    * @param Object obj
267    * @api public
268    */
269
270   util.isArray = Array.isArray || function (obj) {
271     return Object.prototype.toString.call(obj) === '[object Array]';
272   };
273
274   /**
275    * Intersects values of two arrays into a third
276    *
277    * @api public
278    */
279
280   util.intersect = function (arr, arr2) {
281     var ret = []
282       , longest = arr.length > arr2.length ? arr : arr2
283       , shortest = arr.length > arr2.length ? arr2 : arr;
284
285     for (var i = 0, l = shortest.length; i < l; i++) {
286       if (~util.indexOf(longest, shortest[i]))
287         ret.push(shortest[i]);
288     }
289
290     return ret;
291   };
292
293   /**
294    * Array indexOf compatibility.
295    *
296    * @see bit.ly/a5Dxa2
297    * @api public
298    */
299
300   util.indexOf = function (arr, o, i) {
301
302     for (var j = arr.length, i = i < 0 ? i + j < 0 ? 0 : i + j : i || 0;
303          i < j && arr[i] !== o; i++) {}
304
305     return j <= i ? -1 : i;
306   };
307
308   /**
309    * Converts enumerables to array.
310    *
311    * @api public
312    */
313
314   util.toArray = function (enu) {
315     var arr = [];
316
317     for (var i = 0, l = enu.length; i < l; i++)
318       arr.push(enu[i]);
319
320     return arr;
321   };
322
323   /**
324    * UA / engines detection namespace.
325    *
326    * @namespace
327    */
328
329   util.ua = {};
330
331   /**
332    * Whether the UA supports CORS for XHR.
333    *
334    * @api public
335    */
336
337   util.ua.hasCORS = 'undefined' != typeof XMLHttpRequest && (function () {
338     try {
339       var a = new XMLHttpRequest();
340     } catch (e) {
341       return false;
342     }
343
344     return a.withCredentials != undefined;
345   })();
346
347   /**
348    * Detect webkit.
349    *
350    * @api public
351    */
352
353   util.ua.webkit = 'undefined' != typeof navigator
354     && /webkit/i.test(navigator.userAgent);
355
356    /**
357    * Detect iPad/iPhone/iPod.
358    *
359    * @api public
360    */
361
362   util.ua.iDevice = 'undefined' != typeof navigator
363       && /iPad|iPhone|iPod/i.test(navigator.userAgent);
364
365 })('undefined' != typeof io ? io : module.exports, this);