@@ -63,31 +63,20 @@ function Socket(uri, opts){
6363
6464 if ( uri ) {
6565 uri = parseuri ( uri ) ;
66- opts . host = uri . host ;
66+ opts . hostname = uri . host ;
6767 opts . secure = uri . protocol == 'https' || uri . protocol == 'wss' ;
6868 opts . port = uri . port ;
6969 if ( uri . query ) opts . query = uri . query ;
70+ } else if ( opts . host ) {
71+ opts . hostname = parseuri ( opts . host ) . host ;
7072 }
7173
7274 this . secure = null != opts . secure ? opts . secure :
7375 ( global . location && 'https:' == location . protocol ) ;
7476
75- if ( opts . host ) {
76- var match = opts . host . match ( / ( \[ .+ \] ) ( .+ ) ? / )
77- , pieces ;
78-
79- if ( match ) {
80- opts . hostname = match [ 1 ] ;
81- if ( match [ 2 ] ) opts . port = match [ 2 ] . slice ( 1 ) ;
82- } else {
83- pieces = opts . host . split ( ':' ) ;
84- opts . hostname = pieces . shift ( ) ;
85- if ( pieces . length ) opts . port = pieces . pop ( ) ;
86- }
87-
88- // if `host` does not include a port and one is not specified manually,
89- // use the protocol default
90- if ( ! opts . port ) opts . port = this . secure ? '443' : '80' ;
77+ if ( opts . hostname && ! opts . port ) {
78+ // if no port is specified manually, use the protocol default
79+ opts . port = this . secure ? '443' : '80' ;
9180 }
9281
9382 this . agent = opts . agent || false ;
@@ -810,13 +799,6 @@ function Transport (opts) {
810799
811800Emitter ( Transport . prototype ) ;
812801
813- /**
814- * A counter used to prevent collisions in the timestamps used
815- * for cache busting.
816- */
817-
818- Transport . timestamps = 0 ;
819-
820802/**
821803 * Emits an error.
822804 *
@@ -1640,6 +1622,7 @@ var Transport = _dereq_('../transport');
16401622var parseqs = _dereq_ ( 'parseqs' ) ;
16411623var parser = _dereq_ ( 'engine.io-parser' ) ;
16421624var inherit = _dereq_ ( 'component-inherit' ) ;
1625+ var yeast = _dereq_ ( 'yeast' ) ;
16431626var debug = _dereq_ ( 'debug' ) ( 'engine.io-client:polling' ) ;
16441627
16451628/**
@@ -1855,7 +1838,7 @@ Polling.prototype.uri = function(){
18551838
18561839 // cache busting is forced
18571840 if ( false !== this . timestampRequests ) {
1858- query [ this . timestampParam ] = + new Date + '-' + Transport . timestamps ++ ;
1841+ query [ this . timestampParam ] = yeast ( ) ;
18591842 }
18601843
18611844 if ( ! this . supportsBinary && ! query . sid ) {
@@ -1875,10 +1858,11 @@ Polling.prototype.uri = function(){
18751858 query = '?' + query ;
18761859 }
18771860
1878- return schema + '://' + this . hostname + port + this . path + query ;
1861+ var ipv6 = this . hostname . indexOf ( ':' ) !== - 1 ;
1862+ return schema + '://' + ( ipv6 ? '[' + this . hostname + ']' : this . hostname ) + port + this . path + query ;
18791863} ;
18801864
1881- } , { "../transport" :4 , "component-inherit" :15 , "debug" :16 , "engine.io-parser" :18 , "parseqs" :27 , "xmlhttprequest-ssl" :10 } ] , 9 :[ function ( _dereq_ , module , exports ) {
1865+ } , { "../transport" :4 , "component-inherit" :15 , "debug" :16 , "engine.io-parser" :18 , "parseqs" :27 , "xmlhttprequest-ssl" :10 , "yeast" : 31 } ] , 9 :[ function ( _dereq_ , module , exports ) {
18821866( function ( global ) {
18831867/**
18841868 * Module dependencies.
@@ -1888,6 +1872,7 @@ var Transport = _dereq_('../transport');
18881872var parser = _dereq_ ( 'engine.io-parser' ) ;
18891873var parseqs = _dereq_ ( 'parseqs' ) ;
18901874var inherit = _dereq_ ( 'component-inherit' ) ;
1875+ var yeast = _dereq_ ( 'yeast' ) ;
18911876var debug = _dereq_ ( 'debug' ) ( 'engine.io-client:websocket' ) ;
18921877
18931878/**
@@ -2134,7 +2119,7 @@ WS.prototype.uri = function(){
21342119
21352120 // append timestamp to URI
21362121 if ( this . timestampRequests ) {
2137- query [ this . timestampParam ] = + new Date ;
2122+ query [ this . timestampParam ] = yeast ( ) ;
21382123 }
21392124
21402125 // communicate binary support capabilities
@@ -2149,7 +2134,8 @@ WS.prototype.uri = function(){
21492134 query = '?' + query ;
21502135 }
21512136
2152- return schema + '://' + this . hostname + port + this . path + query ;
2137+ var ipv6 = this . hostname . indexOf ( ':' ) !== - 1 ;
2138+ return schema + '://' + ( ipv6 ? '[' + this . hostname + ']' : this . hostname ) + port + this . path + query ;
21532139} ;
21542140
21552141/**
@@ -2164,7 +2150,7 @@ WS.prototype.check = function(){
21642150} ;
21652151
21662152} ) . call ( this , typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : { } )
2167- } , { "../transport" :4 , "component-inherit" :15 , "debug" :16 , "engine.io-parser" :18 , "parseqs" :27 , "ws" :30 } ] , 10 :[ function ( _dereq_ , module , exports ) {
2153+ } , { "../transport" :4 , "component-inherit" :15 , "debug" :16 , "engine.io-parser" :18 , "parseqs" :27 , "ws" :30 , "yeast" : 31 } ] , 10 :[ function ( _dereq_ , module , exports ) {
21682154// browser shim for xmlhttprequest module
21692155var hasCORS = _dereq_ ( 'has-cors' ) ;
21702156
@@ -4218,5 +4204,75 @@ function ws(uri, protocols, opts) {
42184204
42194205if ( WebSocket ) ws . prototype = WebSocket . prototype ;
42204206
4207+ } , { } ] , 31 :[ function ( _dereq_ , module , exports ) {
4208+ 'use strict' ;
4209+
4210+ var alphabet = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_' . split ( '' )
4211+ , length = 64
4212+ , map = { }
4213+ , seed = 0
4214+ , i = 0
4215+ , prev ;
4216+
4217+ /**
4218+ * Return a string representing the specified number.
4219+ *
4220+ * @param {Number } num The number to convert.
4221+ * @returns {String } The string representation of the number.
4222+ * @api public
4223+ */
4224+ function encode ( num ) {
4225+ var encoded = '' ;
4226+
4227+ do {
4228+ encoded = alphabet [ num % length ] + encoded ;
4229+ num = Math . floor ( num / length ) ;
4230+ } while ( num > 0 ) ;
4231+
4232+ return encoded ;
4233+ }
4234+
4235+ /**
4236+ * Return the integer value specified by the given string.
4237+ *
4238+ * @param {String } str The string to convert.
4239+ * @returns {Number } The integer value represented by the string.
4240+ * @api public
4241+ */
4242+ function decode ( str ) {
4243+ var decoded = 0 ;
4244+
4245+ for ( i = 0 ; i < str . length ; i ++ ) {
4246+ decoded = decoded * length + map [ str . charAt ( i ) ] ;
4247+ }
4248+
4249+ return decoded ;
4250+ }
4251+
4252+ /**
4253+ * Yeast: A tiny growing id generator.
4254+ *
4255+ * @returns {String } A unique id.
4256+ * @api public
4257+ */
4258+ function yeast ( ) {
4259+ var now = encode ( + new Date ( ) ) ;
4260+
4261+ if ( now !== prev ) return seed = 0 , prev = now ;
4262+ return now + '.' + encode ( seed ++ ) ;
4263+ }
4264+
4265+ //
4266+ // Map each character to its index.
4267+ //
4268+ for ( ; i < length ; i ++ ) map [ alphabet [ i ] ] = i ;
4269+
4270+ //
4271+ // Expose the `yeast`, `encode` and `decode` functions.
4272+ //
4273+ yeast . encode = encode ;
4274+ yeast . decode = decode ;
4275+ module . exports = yeast ;
4276+
42214277} , { } ] } , { } , [ 1 ] ) ( 1 )
42224278} ) ;
0 commit comments