diff --git a/bower.json b/bower.json index ee8a362..25d96d0 100644 --- a/bower.json +++ b/bower.json @@ -1,7 +1,7 @@ { "name": "socketcluster-client", "main": "socketcluster.js", - "version": "6.5.0", + "version": "7.0.0", "homepage": "https://github.com/SocketCluster/socketcluster-client", "description": "SocketCluster JavaScript client", "authors": [ diff --git a/index.js b/index.js index 749658c..05ea899 100644 --- a/index.js +++ b/index.js @@ -16,4 +16,4 @@ module.exports.destroy = function (options) { module.exports.connections = SCSocketCreator.connections; -module.exports.version = '6.5.0'; +module.exports.version = '7.0.0'; diff --git a/lib/scsocket.js b/lib/scsocket.js index 6f21f88..d9df2ee 100644 --- a/lib/scsocket.js +++ b/lib/scsocket.js @@ -501,6 +501,9 @@ SCSocket.prototype._onSCOpen = function (status) { this._changeToUnauthenticatedStateAndClearTokens(); } } else { + // This can happen if auth.loadToken (in sctransport.js) fails with + // an error - This means that the signedAuthToken cannot be loaded by + // the auth engine and therefore, we need to unauthenticate the socket. this._changeToUnauthenticatedStateAndClearTokens(); } diff --git a/package.json b/package.json index cf54869..522038d 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "socketcluster-client", "description": "SocketCluster JavaScript client", - "version": "6.5.0", + "version": "7.0.0", "homepage": "http://socketcluster.io", "contributors": [ { diff --git a/socketcluster.js b/socketcluster.js index b47b346..3742028 100644 --- a/socketcluster.js +++ b/socketcluster.js @@ -422,7 +422,7 @@ module.exports.destroy = function (options) { module.exports.connections = SCSocketCreator.connections; -module.exports.version = '6.5.0'; +module.exports.version = '7.0.0'; },{"./lib/scsocket":6,"./lib/scsocketcreator":7,"component-emitter":14}],4:[function(require,module,exports){ (function (global){ @@ -807,8 +807,6 @@ SCSocket.prototype.connect = SCSocket.prototype.open = function () { this.state = this.CONNECTING; Emitter.prototype.emit.call(this, 'connecting'); - this._changeToUnauthenticatedState(); - if (this.transport) { this.transport.off(); } @@ -868,18 +866,6 @@ SCSocket.prototype.destroy = function () { this.disconnect(); }; -SCSocket.prototype._changeToUnauthenticatedState = function () { - if (this.authState != this.UNAUTHENTICATED) { - var oldState = this.authState; - this.authState = this.UNAUTHENTICATED; - var stateChangeData = { - oldState: oldState, - newState: this.authState - }; - Emitter.prototype.emit.call(this, 'authStateChange', stateChangeData); - } -}; - SCSocket.prototype._changeToUnauthenticatedStateAndClearTokens = function () { if (this.authState != this.UNAUTHENTICATED) { var oldState = this.authState; @@ -979,28 +965,41 @@ SCSocket.prototype.getSignedAuthToken = function () { SCSocket.prototype.authenticate = function (signedAuthToken, callback) { var self = this; - this._changeToUnauthenticatedState(); - this.emit('#authenticate', signedAuthToken, function (err, authStatus) { - if (authStatus && authStatus.authError) { - authStatus.authError = scErrors.hydrateError(authStatus.authError); + + if (authStatus && authStatus.isAuthenticated != null) { + // If authStatus is correctly formatted (has an isAuthenticated property), + // then we will rehydrate the authError. + if (authStatus.authError) { + authStatus.authError = scErrors.hydrateError(authStatus.authError); + } + } else { + // Some errors like BadConnectionError and TimeoutError will not pass a valid + // authStatus object to the current function, so we need to create it ourselves. + authStatus = { + isAuthenticated: self.authState, + authError: null + }; } if (err) { - self._changeToUnauthenticatedStateAndClearTokens(); + if (err.name != 'BadConnectionError' && err.name != 'TimeoutError') { + // In case of a bad/closed connection or a timeout, we maintain the last + // known auth state since those errors don't mean that the token is invalid. + + self._changeToUnauthenticatedStateAndClearTokens(); + } callback && callback(err, authStatus); } else { self.auth.saveToken(self.authTokenName, signedAuthToken, {}, function (err) { - callback && callback(err, authStatus); if (err) { - self._changeToUnauthenticatedStateAndClearTokens(); self._onSCError(err); + } + if (authStatus.isAuthenticated) { + self._changeToAuthenticatedState(signedAuthToken); } else { - if (authStatus.isAuthenticated) { - self._changeToAuthenticatedState(signedAuthToken); - } else { - self._changeToUnauthenticatedStateAndClearTokens(); - } + self._changeToUnauthenticatedStateAndClearTokens(); } + callback && callback(err, authStatus); }); } }); @@ -1049,6 +1048,9 @@ SCSocket.prototype._onSCOpen = function (status) { this._changeToUnauthenticatedStateAndClearTokens(); } } else { + // This can happen if auth.loadToken (in sctransport.js) fails with + // an error - This means that the signedAuthToken cannot be loaded by + // the auth engine and therefore, we need to unauthenticate the socket. this._changeToUnauthenticatedStateAndClearTokens(); } @@ -1134,8 +1136,8 @@ SCSocket.prototype._onSCClose = function (code, data, openAbort) { this.pendingReconnectTimeout = null; clearTimeout(this._reconnectTimeoutRef); - this._changeToUnauthenticatedState(); this._suspendSubscriptions(); + this._abortAllPendingEventsDueToBadConnection(openAbort ? 'connectAbort' : 'disconnect'); // Try to reconnect // on server ping timeout (4000) @@ -1174,8 +1176,6 @@ SCSocket.prototype._onSCClose = function (code, data, openAbort) { var err = new SocketProtocolError(SCSocket.errorStatuses[code] || failureMessage, code); this._onSCError(err); } - - this._abortAllPendingEventsDueToBadConnection(openAbort ? 'connectAbort' : 'disconnect'); }; SCSocket.prototype._onSCEvent = function (event, data, res) { diff --git a/socketcluster.min.js b/socketcluster.min.js index a1579c6..f6ef165 100644 --- a/socketcluster.min.js +++ b/socketcluster.min.js @@ -1,4 +1,4 @@ -(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.socketCluster=f()}})(function(){var define,module,exports;return function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;omaxTimeout){throw new InvalidArgumentsError("The "+propertyName+" value provided exceeded the maximum amount allowed")}};verifyDuration("connectTimeout");verifyDuration("ackTimeout");verifyDuration("pingTimeout");this._localEvents={connect:1,connectAbort:1,disconnect:1,message:1,error:1,raw:1,fail:1,kickOut:1,subscribe:1,unsubscribe:1,subscribeStateChange:1,authStateChange:1,authenticate:1,deauthenticate:1,removeAuthToken:1,subscribeRequest:1};this.connectAttempts=0;this._emitBuffer=new LinkedList;this.channels={};this.options=opts;this._cid=1;this.options.callIdGenerator=function(){return self._cid++};if(this.options.autoReconnect){if(this.options.autoReconnectOptions==null){this.options.autoReconnectOptions={}}var reconnectOptions=this.options.autoReconnectOptions;if(reconnectOptions.initialDelay==null){reconnectOptions.initialDelay=1e4}if(reconnectOptions.randomness==null){reconnectOptions.randomness=1e4}if(reconnectOptions.multiplier==null){reconnectOptions.multiplier=1.5}if(reconnectOptions.maxDelay==null){reconnectOptions.maxDelay=6e4}}if(this.options.subscriptionRetryOptions==null){this.options.subscriptionRetryOptions={}}if(this.options.authEngine){this.auth=this.options.authEngine}else{this.auth=new AuthEngine}if(this.options.codecEngine){this.codec=this.options.codecEngine}else{this.codec=formatter}this.options.path=this.options.path.replace(/\/$/,"")+"/";this.options.query=opts.query||{};if(typeof this.options.query=="string"){this.options.query=querystring.parse(this.options.query)}if(this.options.autoConnect){this.connect()}this._channelEmitter=new Emitter;if(isBrowser&&this.disconnectOnUnload){this._unloadHandler=function(){self.disconnect()};global.addEventListener("beforeunload",this._unloadHandler,false)}};SCSocket.prototype=Object.create(Emitter.prototype);SCSocket.CONNECTING=SCSocket.prototype.CONNECTING=SCTransport.prototype.CONNECTING;SCSocket.OPEN=SCSocket.prototype.OPEN=SCTransport.prototype.OPEN;SCSocket.CLOSED=SCSocket.prototype.CLOSED=SCTransport.prototype.CLOSED;SCSocket.AUTHENTICATED=SCSocket.prototype.AUTHENTICATED="authenticated";SCSocket.UNAUTHENTICATED=SCSocket.prototype.UNAUTHENTICATED="unauthenticated";SCSocket.PENDING=SCSocket.prototype.PENDING="pending";SCSocket.ignoreStatuses=scErrors.socketProtocolIgnoreStatuses;SCSocket.errorStatuses=scErrors.socketProtocolErrorStatuses;SCSocket.prototype._privateEventHandlerMap={"#publish":function(data){var undecoratedChannelName=this._undecorateChannelName(data.channel);var isSubscribed=this.isSubscribed(undecoratedChannelName,true);if(isSubscribed){this._channelEmitter.emit(undecoratedChannelName,data.data)}},"#kickOut":function(data){var undecoratedChannelName=this._undecorateChannelName(data.channel);var channel=this.channels[undecoratedChannelName];if(channel){Emitter.prototype.emit.call(this,"kickOut",data.message,undecoratedChannelName);channel.emit("kickOut",data.message,undecoratedChannelName);this._triggerChannelUnsubscribe(channel)}},"#setAuthToken":function(data,response){var self=this;if(data){var triggerAuthenticate=function(err){if(err){response.error(err);self._onSCError(err)}else{self._changeToAuthenticatedState(data.token);response.end()}};this.auth.saveToken(this.authTokenName,data.token,{},triggerAuthenticate)}else{response.error(new InvalidMessageError("No token data provided by #setAuthToken event"))}},"#removeAuthToken":function(data,response){var self=this;this.auth.removeToken(this.authTokenName,function(err,oldToken){if(err){response.error(err);self._onSCError(err)}else{Emitter.prototype.emit.call(self,"removeAuthToken",oldToken);self._changeToUnauthenticatedStateAndClearTokens();response.end()}})},"#disconnect":function(data){this.transport.close(data.code,data.data)}};SCSocket.prototype.getState=function(){return this.state};SCSocket.prototype.getBytesReceived=function(){return this.transport.getBytesReceived()};SCSocket.prototype.deauthenticate=function(callback){var self=this;this.auth.removeToken(this.authTokenName,function(err,oldToken){if(err){self._onSCError(err)}else{Emitter.prototype.emit.call(self,"removeAuthToken",oldToken);if(self.state!=self.CLOSED){self.emit("#removeAuthToken")}self._changeToUnauthenticatedStateAndClearTokens()}callback&&callback(err)})};SCSocket.prototype.connect=SCSocket.prototype.open=function(){var self=this;if(this.state==this.CLOSED){this.pendingReconnect=false;this.pendingReconnectTimeout=null;clearTimeout(this._reconnectTimeoutRef);this.state=this.CONNECTING;Emitter.prototype.emit.call(this,"connecting");this._changeToUnauthenticatedState();if(this.transport){this.transport.off()}this.transport=new SCTransport(this.auth,this.codec,this.options);this.transport.on("open",function(status){self.state=self.OPEN;self._onSCOpen(status)});this.transport.on("error",function(err){self._onSCError(err)});this.transport.on("close",function(code,data){self.state=self.CLOSED;self._onSCClose(code,data)});this.transport.on("openAbort",function(code,data){self.state=self.CLOSED;self._onSCClose(code,data,true)});this.transport.on("event",function(event,data,res){self._onSCEvent(event,data,res)})}};SCSocket.prototype.reconnect=function(){this.disconnect();this.connect()};SCSocket.prototype.disconnect=function(code,data){code=code||1e3;if(typeof code!="number"){throw new InvalidArgumentsError("If specified, the code argument must be a number")}if(this.state==this.OPEN||this.state==this.CONNECTING){this.transport.close(code,data)}else{this.pendingReconnect=false;this.pendingReconnectTimeout=null;clearTimeout(this._reconnectTimeoutRef)}};SCSocket.prototype.destroy=function(){if(this._unloadHandler){global.removeEventListener("beforeunload",this._unloadHandler,false)}this.disconnect()};SCSocket.prototype._changeToUnauthenticatedState=function(){if(this.authState!=this.UNAUTHENTICATED){var oldState=this.authState;this.authState=this.UNAUTHENTICATED;var stateChangeData={oldState:oldState,newState:this.authState};Emitter.prototype.emit.call(this,"authStateChange",stateChangeData)}};SCSocket.prototype._changeToUnauthenticatedStateAndClearTokens=function(){if(this.authState!=this.UNAUTHENTICATED){var oldState=this.authState;this.authState=this.UNAUTHENTICATED;this.signedAuthToken=null;this.authToken=null;var stateChangeData={oldState:oldState,newState:this.authState};Emitter.prototype.emit.call(this,"authStateChange",stateChangeData);if(oldState==this.AUTHENTICATED){Emitter.prototype.emit.call(this,"deauthenticate")}Emitter.prototype.emit.call(this,"authTokenChange",this.signedAuthToken)}};SCSocket.prototype._changeToAuthenticatedState=function(signedAuthToken){this.signedAuthToken=signedAuthToken;this.authToken=this._extractAuthTokenData(signedAuthToken);if(this.authState!=this.AUTHENTICATED){var oldState=this.authState;this.authState=this.AUTHENTICATED;var stateChangeData={oldState:oldState,newState:this.authState,signedAuthToken:signedAuthToken,authToken:this.authToken};if(!this.preparingPendingSubscriptions){this.processPendingSubscriptions()}Emitter.prototype.emit.call(this,"authStateChange",stateChangeData);Emitter.prototype.emit.call(this,"authenticate",signedAuthToken)}Emitter.prototype.emit.call(this,"authTokenChange",signedAuthToken)};SCSocket.prototype.decodeBase64=function(encodedString){var decodedString;if(typeof Buffer=="undefined"){if(global.atob){decodedString=global.atob(encodedString)}else{decodedString=base64.decode(encodedString)}}else{var buffer=new Buffer(encodedString,"base64");decodedString=buffer.toString("utf8")}return decodedString};SCSocket.prototype.encodeBase64=function(decodedString){var encodedString;if(typeof Buffer=="undefined"){if(global.btoa){encodedString=global.btoa(decodedString)}else{encodedString=base64.encode(decodedString)}}else{var buffer=new Buffer(decodedString,"utf8");encodedString=buffer.toString("base64")}return encodedString};SCSocket.prototype._extractAuthTokenData=function(signedAuthToken){var tokenParts=(signedAuthToken||"").split(".");var encodedTokenData=tokenParts[1];if(encodedTokenData!=null){var tokenData=encodedTokenData;try{tokenData=this.decodeBase64(tokenData);return JSON.parse(tokenData)}catch(e){return tokenData}}return null};SCSocket.prototype.getAuthToken=function(){return this.authToken};SCSocket.prototype.getSignedAuthToken=function(){return this.signedAuthToken};SCSocket.prototype.authenticate=function(signedAuthToken,callback){var self=this;this._changeToUnauthenticatedState();this.emit("#authenticate",signedAuthToken,function(err,authStatus){if(authStatus&&authStatus.authError){authStatus.authError=scErrors.hydrateError(authStatus.authError)}if(err){self._changeToUnauthenticatedStateAndClearTokens();callback&&callback(err,authStatus)}else{self.auth.saveToken(self.authTokenName,signedAuthToken,{},function(err){callback&&callback(err,authStatus);if(err){self._changeToUnauthenticatedStateAndClearTokens();self._onSCError(err)}else{if(authStatus.isAuthenticated){self._changeToAuthenticatedState(signedAuthToken)}else{self._changeToUnauthenticatedStateAndClearTokens()}}})}})};SCSocket.prototype._tryReconnect=function(initialDelay){var self=this;var exponent=this.connectAttempts++;var reconnectOptions=this.options.autoReconnectOptions;var timeout;if(initialDelay==null||exponent>0){var initialTimeout=Math.round(reconnectOptions.initialDelay+(reconnectOptions.randomness||0)*Math.random());timeout=Math.round(initialTimeout*Math.pow(reconnectOptions.multiplier,exponent))}else{timeout=initialDelay}if(timeout>reconnectOptions.maxDelay){timeout=reconnectOptions.maxDelay}clearTimeout(this._reconnectTimeoutRef);this.pendingReconnect=true;this.pendingReconnectTimeout=timeout;this._reconnectTimeoutRef=setTimeout(function(){self.connect()},timeout)};SCSocket.prototype._onSCOpen=function(status){var self=this;this.preparingPendingSubscriptions=true;if(status){this.id=status.id;this.pingTimeout=status.pingTimeout;this.transport.pingTimeout=this.pingTimeout;if(status.isAuthenticated){this._changeToAuthenticatedState(status.authToken)}else{this._changeToUnauthenticatedStateAndClearTokens()}}else{this._changeToUnauthenticatedStateAndClearTokens()}this.connectAttempts=0;if(this.options.autoSubscribeOnConnect){this.processPendingSubscriptions()}Emitter.prototype.emit.call(this,"connect",status,function(){self.processPendingSubscriptions()});this._flushEmitBuffer()};SCSocket.prototype._onSCError=function(err){var self=this;setTimeout(function(){if(self.listeners("error").length<1){throw err}else{Emitter.prototype.emit.call(self,"error",err)}},0)};SCSocket.prototype._suspendSubscriptions=function(){var channel,newState;for(var channelName in this.channels){if(this.channels.hasOwnProperty(channelName)){channel=this.channels[channelName];if(channel.state==channel.SUBSCRIBED||channel.state==channel.PENDING){newState=channel.PENDING}else{newState=channel.UNSUBSCRIBED}this._triggerChannelUnsubscribe(channel,newState)}}};SCSocket.prototype._abortAllPendingEventsDueToBadConnection=function(failureType){var currentNode=this._emitBuffer.head;var nextNode;while(currentNode){nextNode=currentNode.next;var eventObject=currentNode.data;clearTimeout(eventObject.timeout);delete eventObject.timeout;currentNode.detach();currentNode=nextNode;var callback=eventObject.callback;if(callback){delete eventObject.callback;var errorMessage="Event '"+eventObject.event+"' was aborted due to a bad connection";var error=new BadConnectionError(errorMessage,failureType);callback.call(eventObject,error,eventObject)}}};SCSocket.prototype._onSCClose=function(code,data,openAbort){var self=this;this.id=null;if(this.transport){this.transport.off()}this.pendingReconnect=false;this.pendingReconnectTimeout=null;clearTimeout(this._reconnectTimeoutRef);this._changeToUnauthenticatedState();this._suspendSubscriptions();if(this.options.autoReconnect){if(code==4e3||code==4001||code==1005){this._tryReconnect(0)}else if(code!=1e3&&code<4500){this._tryReconnect()}}if(openAbort){Emitter.prototype.emit.call(self,"connectAbort",code,data)}else{Emitter.prototype.emit.call(self,"disconnect",code,data)}if(!SCSocket.ignoreStatuses[code]){var failureMessage;if(data){failureMessage="Socket connection failed: "+data}else{failureMessage="Socket connection failed for unknown reasons"}var err=new SocketProtocolError(SCSocket.errorStatuses[code]||failureMessage,code);this._onSCError(err)}this._abortAllPendingEventsDueToBadConnection(openAbort?"connectAbort":"disconnect")};SCSocket.prototype._onSCEvent=function(event,data,res){var handler=this._privateEventHandlerMap[event];if(handler){handler.call(this,data,res)}else{Emitter.prototype.emit.call(this,event,data,function(){res&&res.callback.apply(res,arguments)})}};SCSocket.prototype.decode=function(message){return this.transport.decode(message)};SCSocket.prototype.encode=function(object){return this.transport.encode(object)};SCSocket.prototype._flushEmitBuffer=function(){var currentNode=this._emitBuffer.head;var nextNode;while(currentNode){nextNode=currentNode.next;var eventObject=currentNode.data;currentNode.detach();this.transport.emitObject(eventObject);currentNode=nextNode}};SCSocket.prototype._handleEventAckTimeout=function(eventObject,eventNode){if(eventNode){eventNode.detach()}delete eventObject.timeout;var callback=eventObject.callback;if(callback){delete eventObject.callback;var error=new TimeoutError("Event response for '"+eventObject.event+"' timed out");callback.call(eventObject,error,eventObject)}};SCSocket.prototype._emit=function(event,data,callback){var self=this;if(this.state==this.CLOSED){this.connect()}var eventObject={event:event,callback:callback};var eventNode=new LinkedList.Item;if(this.options.cloneData){eventObject.data=clone(data)}else{eventObject.data=data}eventNode.data=eventObject;eventObject.timeout=setTimeout(function(){self._handleEventAckTimeout(eventObject,eventNode)},this.ackTimeout);this._emitBuffer.append(eventNode);if(this.state==this.OPEN){this._flushEmitBuffer()}};SCSocket.prototype.send=function(data){this.transport.send(data)};SCSocket.prototype.emit=function(event,data,callback){if(this._localEvents[event]==null){this._emit(event,data,callback)}else{Emitter.prototype.emit.call(this,event,data)}};SCSocket.prototype.publish=function(channelName,data,callback){var pubData={channel:this._decorateChannelName(channelName),data:data};this.emit("#publish",pubData,callback)};SCSocket.prototype._triggerChannelSubscribe=function(channel,subscriptionOptions){var channelName=channel.name;if(channel.state!=channel.SUBSCRIBED){var oldState=channel.state;channel.state=channel.SUBSCRIBED;var stateChangeData={channel:channelName,oldState:oldState,newState:channel.state,subscriptionOptions:subscriptionOptions};channel.emit("subscribeStateChange",stateChangeData);channel.emit("subscribe",channelName,subscriptionOptions);Emitter.prototype.emit.call(this,"subscribeStateChange",stateChangeData);Emitter.prototype.emit.call(this,"subscribe",channelName,subscriptionOptions)}};SCSocket.prototype._triggerChannelSubscribeFail=function(err,channel,subscriptionOptions){var channelName=channel.name;var meetsAuthRequirements=!channel.waitForAuth||this.authState==this.AUTHENTICATED;if(channel.state!=channel.UNSUBSCRIBED&&meetsAuthRequirements){channel.state=channel.UNSUBSCRIBED;channel.emit("subscribeFail",err,channelName,subscriptionOptions);Emitter.prototype.emit.call(this,"subscribeFail",err,channelName,subscriptionOptions)}};SCSocket.prototype._cancelPendingSubscribeCallback=function(channel){if(channel._pendingSubscriptionCid!=null){this.transport.cancelPendingResponse(channel._pendingSubscriptionCid);delete channel._pendingSubscriptionCid}};SCSocket.prototype._decorateChannelName=function(channelName){if(this.channelPrefix){channelName=this.channelPrefix+channelName}return channelName};SCSocket.prototype._undecorateChannelName=function(decoratedChannelName){if(this.channelPrefix&&decoratedChannelName.indexOf(this.channelPrefix)==0){return decoratedChannelName.replace(this.channelPrefix,"")}return decoratedChannelName};SCSocket.prototype._trySubscribe=function(channel){var self=this;var meetsAuthRequirements=!channel.waitForAuth||this.authState==this.AUTHENTICATED;if(this.state==this.OPEN&&!this.preparingPendingSubscriptions&&channel._pendingSubscriptionCid==null&&meetsAuthRequirements){var options={noTimeout:true};var subscriptionOptions={channel:this._decorateChannelName(channel.name)};if(channel.waitForAuth){options.waitForAuth=true;subscriptionOptions.waitForAuth=options.waitForAuth}if(channel.data){subscriptionOptions.data=channel.data}channel._pendingSubscriptionCid=this.transport.emit("#subscribe",subscriptionOptions,options,function(err){delete channel._pendingSubscriptionCid;if(err){self._triggerChannelSubscribeFail(err,channel,subscriptionOptions)}else{self._triggerChannelSubscribe(channel,subscriptionOptions)}});Emitter.prototype.emit.call(this,"subscribeRequest",channel.name,subscriptionOptions)}};SCSocket.prototype.subscribe=function(channelName,options){var channel=this.channels[channelName];if(!channel){channel=new SCChannel(channelName,this,options);this.channels[channelName]=channel}else if(options){channel.setOptions(options)}if(channel.state==channel.UNSUBSCRIBED){channel.state=channel.PENDING;this._trySubscribe(channel)}return channel};SCSocket.prototype._triggerChannelUnsubscribe=function(channel,newState){var channelName=channel.name;var oldState=channel.state;if(newState){channel.state=newState}else{channel.state=channel.UNSUBSCRIBED}this._cancelPendingSubscribeCallback(channel);if(oldState==channel.SUBSCRIBED){var stateChangeData={channel:channelName,oldState:oldState,newState:channel.state};channel.emit("subscribeStateChange",stateChangeData);channel.emit("unsubscribe",channelName);Emitter.prototype.emit.call(this,"subscribeStateChange",stateChangeData);Emitter.prototype.emit.call(this,"unsubscribe",channelName)}};SCSocket.prototype._tryUnsubscribe=function(channel){var self=this;if(this.state==this.OPEN){var options={noTimeout:true};this._cancelPendingSubscribeCallback(channel);var decoratedChannelName=this._decorateChannelName(channel.name);this.transport.emit("#unsubscribe",decoratedChannelName,options)}};SCSocket.prototype.unsubscribe=function(channelName){var channel=this.channels[channelName];if(channel){if(channel.state!=channel.UNSUBSCRIBED){this._triggerChannelUnsubscribe(channel);this._tryUnsubscribe(channel)}}};SCSocket.prototype.channel=function(channelName,options){var currentChannel=this.channels[channelName];if(!currentChannel){currentChannel=new SCChannel(channelName,this,options);this.channels[channelName]=currentChannel} -return currentChannel};SCSocket.prototype.destroyChannel=function(channelName){var channel=this.channels[channelName];channel.unwatch();channel.unsubscribe();delete this.channels[channelName]};SCSocket.prototype.subscriptions=function(includePending){var subs=[];var channel,includeChannel;for(var channelName in this.channels){if(this.channels.hasOwnProperty(channelName)){channel=this.channels[channelName];if(includePending){includeChannel=channel&&(channel.state==channel.SUBSCRIBED||channel.state==channel.PENDING)}else{includeChannel=channel&&channel.state==channel.SUBSCRIBED}if(includeChannel){subs.push(channelName)}}}return subs};SCSocket.prototype.isSubscribed=function(channelName,includePending){var channel=this.channels[channelName];if(includePending){return!!channel&&(channel.state==channel.SUBSCRIBED||channel.state==channel.PENDING)}return!!channel&&channel.state==channel.SUBSCRIBED};SCSocket.prototype.processPendingSubscriptions=function(){var self=this;this.preparingPendingSubscriptions=false;var pendingChannels=[];for(var i in this.channels){if(this.channels.hasOwnProperty(i)){var channel=this.channels[i];if(channel.state==channel.PENDING){pendingChannels.push(channel)}}}pendingChannels.sort(function(a,b){var ap=a.priority||0;var bp=b.priority||0;if(ap>bp){return-1}if(ap>(-2*bitCounter&6))}}return output};var encode=function(input){input=String(input);if(/[^\0-\xFF]/.test(input)){error("The string to be encoded contains characters outside of the "+"Latin1 range.")}var padding=input.length%3;var output="";var position=-1;var a;var b;var c;var d;var buffer;var length=input.length-padding;while(++position>18&63)+TABLE.charAt(buffer>>12&63)+TABLE.charAt(buffer>>6&63)+TABLE.charAt(buffer&63)}if(padding==2){a=input.charCodeAt(position)<<8;b=input.charCodeAt(++position);buffer=a+b;output+=TABLE.charAt(buffer>>10)+TABLE.charAt(buffer>>4&63)+TABLE.charAt(buffer<<2&63)+"="}else if(padding==1){buffer=input.charCodeAt(position);output+=TABLE.charAt(buffer>>2)+TABLE.charAt(buffer<<4&63)+"=="}return output};var base64={encode:encode,decode:decode,version:"0.1.0"};if(typeof define=="function"&&typeof define.amd=="object"&&define.amd){define(function(){return base64})}else if(freeExports&&!freeExports.nodeType){if(freeModule){freeModule.exports=base64}else{for(var key in base64){base64.hasOwnProperty(key)&&(freeExports[key]=base64[key])}}}else{root.base64=base64}})(this)}).call(this,typeof global!=="undefined"?global:typeof self!=="undefined"?self:typeof window!=="undefined"?window:{})},{}],11:[function(require,module,exports){"use strict";exports.byteLength=byteLength;exports.toByteArray=toByteArray;exports.fromByteArray=fromByteArray;var lookup=[];var revLookup=[];var Arr=typeof Uint8Array!=="undefined"?Uint8Array:Array;var code="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";for(var i=0,len=code.length;i0){throw new Error("Invalid string. Length must be a multiple of 4")}return b64[len-2]==="="?2:b64[len-1]==="="?1:0}function byteLength(b64){return b64.length*3/4-placeHoldersCount(b64)}function toByteArray(b64){var i,j,l,tmp,placeHolders,arr;var len=b64.length;placeHolders=placeHoldersCount(b64);arr=new Arr(len*3/4-placeHolders);l=placeHolders>0?len-4:len;var L=0;for(i=0,j=0;i>16&255;arr[L++]=tmp>>8&255;arr[L++]=tmp&255}if(placeHolders===2){tmp=revLookup[b64.charCodeAt(i)]<<2|revLookup[b64.charCodeAt(i+1)]>>4;arr[L++]=tmp&255}else if(placeHolders===1){tmp=revLookup[b64.charCodeAt(i)]<<10|revLookup[b64.charCodeAt(i+1)]<<4|revLookup[b64.charCodeAt(i+2)]>>2;arr[L++]=tmp>>8&255;arr[L++]=tmp&255}return arr}function tripletToBase64(num){return lookup[num>>18&63]+lookup[num>>12&63]+lookup[num>>6&63]+lookup[num&63]}function encodeChunk(uint8,start,end){var tmp;var output=[];for(var i=start;ilen2?len2:i+maxChunkLength))}if(extraBytes===1){tmp=uint8[len-1];output+=lookup[tmp>>2];output+=lookup[tmp<<4&63];output+="=="}else if(extraBytes===2){tmp=(uint8[len-2]<<8)+uint8[len-1];output+=lookup[tmp>>10];output+=lookup[tmp>>4&63];output+=lookup[tmp<<2&63];output+="="}parts.push(output);return parts.join("")}},{}],12:[function(require,module,exports){(function(global){"use strict";var base64=require("base64-js");var ieee754=require("ieee754");var isArray=require("isarray");exports.Buffer=Buffer;exports.SlowBuffer=SlowBuffer;exports.INSPECT_MAX_BYTES=50;Buffer.TYPED_ARRAY_SUPPORT=global.TYPED_ARRAY_SUPPORT!==undefined?global.TYPED_ARRAY_SUPPORT:typedArraySupport();exports.kMaxLength=kMaxLength();function typedArraySupport(){try{var arr=new Uint8Array(1);arr.__proto__={__proto__:Uint8Array.prototype,foo:function(){return 42}};return arr.foo()===42&&typeof arr.subarray==="function"&&arr.subarray(1,1).byteLength===0}catch(e){return false}}function kMaxLength(){return Buffer.TYPED_ARRAY_SUPPORT?2147483647:1073741823}function createBuffer(that,length){if(kMaxLength()=kMaxLength()){throw new RangeError("Attempt to allocate Buffer larger than maximum "+"size: 0x"+kMaxLength().toString(16)+" bytes")}return length|0}function SlowBuffer(length){if(+length!=length){length=0}return Buffer.alloc(+length)}Buffer.isBuffer=function isBuffer(b){return!!(b!=null&&b._isBuffer)};Buffer.compare=function compare(a,b){if(!Buffer.isBuffer(a)||!Buffer.isBuffer(b)){throw new TypeError("Arguments must be Buffers")}if(a===b)return 0;var x=a.length;var y=b.length;for(var i=0,len=Math.min(x,y);i>>1;case"base64":return base64ToBytes(string).length;default:if(loweredCase)return utf8ToBytes(string).length;encoding=(""+encoding).toLowerCase();loweredCase=true}}}Buffer.byteLength=byteLength;function slowToString(encoding,start,end){var loweredCase=false;if(start===undefined||start<0){start=0}if(start>this.length){return""}if(end===undefined||end>this.length){end=this.length}if(end<=0){return""}end>>>=0;start>>>=0;if(end<=start){return""}if(!encoding)encoding="utf8";while(true){switch(encoding){case"hex":return hexSlice(this,start,end);case"utf8":case"utf-8":return utf8Slice(this,start,end);case"ascii":return asciiSlice(this,start,end);case"latin1":case"binary":return latin1Slice(this,start,end);case"base64":return base64Slice(this,start,end);case"ucs2":case"ucs-2":case"utf16le":case"utf-16le":return utf16leSlice(this,start,end);default:if(loweredCase)throw new TypeError("Unknown encoding: "+encoding);encoding=(encoding+"").toLowerCase();loweredCase=true}}}Buffer.prototype._isBuffer=true;function swap(b,n,m){var i=b[n];b[n]=b[m];b[m]=i}Buffer.prototype.swap16=function swap16(){var len=this.length;if(len%2!==0){throw new RangeError("Buffer size must be a multiple of 16-bits")}for(var i=0;i0){str=this.toString("hex",0,max).match(/.{2}/g).join(" ");if(this.length>max)str+=" ... "}return""};Buffer.prototype.compare=function compare(target,start,end,thisStart,thisEnd){if(!Buffer.isBuffer(target)){throw new TypeError("Argument must be a Buffer")}if(start===undefined){start=0}if(end===undefined){end=target?target.length:0}if(thisStart===undefined){thisStart=0}if(thisEnd===undefined){thisEnd=this.length}if(start<0||end>target.length||thisStart<0||thisEnd>this.length){throw new RangeError("out of range index")}if(thisStart>=thisEnd&&start>=end){return 0}if(thisStart>=thisEnd){return-1}if(start>=end){return 1}start>>>=0;end>>>=0;thisStart>>>=0;thisEnd>>>=0;if(this===target)return 0;var x=thisEnd-thisStart;var y=end-start;var len=Math.min(x,y);var thisCopy=this.slice(thisStart,thisEnd);var targetCopy=target.slice(start,end);for(var i=0;i2147483647){byteOffset=2147483647}else if(byteOffset<-2147483648){byteOffset=-2147483648}byteOffset=+byteOffset;if(isNaN(byteOffset)){byteOffset=dir?0:buffer.length-1}if(byteOffset<0)byteOffset=buffer.length+byteOffset;if(byteOffset>=buffer.length){if(dir)return-1;else byteOffset=buffer.length-1}else if(byteOffset<0){if(dir)byteOffset=0;else return-1}if(typeof val==="string"){val=Buffer.from(val,encoding)}if(Buffer.isBuffer(val)){if(val.length===0){return-1}return arrayIndexOf(buffer,val,byteOffset,encoding,dir)}else if(typeof val==="number"){val=val&255;if(Buffer.TYPED_ARRAY_SUPPORT&&typeof Uint8Array.prototype.indexOf==="function"){if(dir){return Uint8Array.prototype.indexOf.call(buffer,val,byteOffset)}else{return Uint8Array.prototype.lastIndexOf.call(buffer,val,byteOffset)}}return arrayIndexOf(buffer,[val],byteOffset,encoding,dir)}throw new TypeError("val must be string, number or Buffer")}function arrayIndexOf(arr,val,byteOffset,encoding,dir){var indexSize=1;var arrLength=arr.length;var valLength=val.length;if(encoding!==undefined){encoding=String(encoding).toLowerCase();if(encoding==="ucs2"||encoding==="ucs-2"||encoding==="utf16le"||encoding==="utf-16le"){if(arr.length<2||val.length<2){return-1}indexSize=2;arrLength/=2;valLength/=2;byteOffset/=2}}function read(buf,i){if(indexSize===1){return buf[i]}else{return buf.readUInt16BE(i*indexSize)}}var i;if(dir){var foundIndex=-1;for(i=byteOffset;iarrLength)byteOffset=arrLength-valLength;for(i=byteOffset;i>=0;i--){var found=true;for(var j=0;jremaining){length=remaining}}var strLen=string.length;if(strLen%2!==0)throw new TypeError("Invalid hex string");if(length>strLen/2){length=strLen/2}for(var i=0;iremaining)length=remaining -;if(string.length>0&&(length<0||offset<0)||offset>this.length){throw new RangeError("Attempt to write outside buffer bounds")}if(!encoding)encoding="utf8";var loweredCase=false;for(;;){switch(encoding){case"hex":return hexWrite(this,string,offset,length);case"utf8":case"utf-8":return utf8Write(this,string,offset,length);case"ascii":return asciiWrite(this,string,offset,length);case"latin1":case"binary":return latin1Write(this,string,offset,length);case"base64":return base64Write(this,string,offset,length);case"ucs2":case"ucs-2":case"utf16le":case"utf-16le":return ucs2Write(this,string,offset,length);default:if(loweredCase)throw new TypeError("Unknown encoding: "+encoding);encoding=(""+encoding).toLowerCase();loweredCase=true}}};Buffer.prototype.toJSON=function toJSON(){return{type:"Buffer",data:Array.prototype.slice.call(this._arr||this,0)}};function base64Slice(buf,start,end){if(start===0&&end===buf.length){return base64.fromByteArray(buf)}else{return base64.fromByteArray(buf.slice(start,end))}}function utf8Slice(buf,start,end){end=Math.min(buf.length,end);var res=[];var i=start;while(i239?4:firstByte>223?3:firstByte>191?2:1;if(i+bytesPerSequence<=end){var secondByte,thirdByte,fourthByte,tempCodePoint;switch(bytesPerSequence){case 1:if(firstByte<128){codePoint=firstByte}break;case 2:secondByte=buf[i+1];if((secondByte&192)===128){tempCodePoint=(firstByte&31)<<6|secondByte&63;if(tempCodePoint>127){codePoint=tempCodePoint}}break;case 3:secondByte=buf[i+1];thirdByte=buf[i+2];if((secondByte&192)===128&&(thirdByte&192)===128){tempCodePoint=(firstByte&15)<<12|(secondByte&63)<<6|thirdByte&63;if(tempCodePoint>2047&&(tempCodePoint<55296||tempCodePoint>57343)){codePoint=tempCodePoint}}break;case 4:secondByte=buf[i+1];thirdByte=buf[i+2];fourthByte=buf[i+3];if((secondByte&192)===128&&(thirdByte&192)===128&&(fourthByte&192)===128){tempCodePoint=(firstByte&15)<<18|(secondByte&63)<<12|(thirdByte&63)<<6|fourthByte&63;if(tempCodePoint>65535&&tempCodePoint<1114112){codePoint=tempCodePoint}}}}if(codePoint===null){codePoint=65533;bytesPerSequence=1}else if(codePoint>65535){codePoint-=65536;res.push(codePoint>>>10&1023|55296);codePoint=56320|codePoint&1023}res.push(codePoint);i+=bytesPerSequence}return decodeCodePointsArray(res)}var MAX_ARGUMENTS_LENGTH=4096;function decodeCodePointsArray(codePoints){var len=codePoints.length;if(len<=MAX_ARGUMENTS_LENGTH){return String.fromCharCode.apply(String,codePoints)}var res="";var i=0;while(ilen)end=len;var out="";for(var i=start;ilen){start=len}if(end<0){end+=len;if(end<0)end=0}else if(end>len){end=len}if(endlength)throw new RangeError("Trying to access beyond buffer length")}Buffer.prototype.readUIntLE=function readUIntLE(offset,byteLength,noAssert){offset=offset|0;byteLength=byteLength|0;if(!noAssert)checkOffset(offset,byteLength,this.length);var val=this[offset];var mul=1;var i=0;while(++i0&&(mul*=256)){val+=this[offset+--byteLength]*mul}return val};Buffer.prototype.readUInt8=function readUInt8(offset,noAssert){if(!noAssert)checkOffset(offset,1,this.length);return this[offset]};Buffer.prototype.readUInt16LE=function readUInt16LE(offset,noAssert){if(!noAssert)checkOffset(offset,2,this.length);return this[offset]|this[offset+1]<<8};Buffer.prototype.readUInt16BE=function readUInt16BE(offset,noAssert){if(!noAssert)checkOffset(offset,2,this.length);return this[offset]<<8|this[offset+1]};Buffer.prototype.readUInt32LE=function readUInt32LE(offset,noAssert){if(!noAssert)checkOffset(offset,4,this.length);return(this[offset]|this[offset+1]<<8|this[offset+2]<<16)+this[offset+3]*16777216};Buffer.prototype.readUInt32BE=function readUInt32BE(offset,noAssert){if(!noAssert)checkOffset(offset,4,this.length);return this[offset]*16777216+(this[offset+1]<<16|this[offset+2]<<8|this[offset+3])};Buffer.prototype.readIntLE=function readIntLE(offset,byteLength,noAssert){offset=offset|0;byteLength=byteLength|0;if(!noAssert)checkOffset(offset,byteLength,this.length);var val=this[offset];var mul=1;var i=0;while(++i=mul)val-=Math.pow(2,8*byteLength);return val};Buffer.prototype.readIntBE=function readIntBE(offset,byteLength,noAssert){offset=offset|0;byteLength=byteLength|0;if(!noAssert)checkOffset(offset,byteLength,this.length);var i=byteLength;var mul=1;var val=this[offset+--i];while(i>0&&(mul*=256)){val+=this[offset+--i]*mul}mul*=128;if(val>=mul)val-=Math.pow(2,8*byteLength);return val};Buffer.prototype.readInt8=function readInt8(offset,noAssert){if(!noAssert)checkOffset(offset,1,this.length);if(!(this[offset]&128))return this[offset];return(255-this[offset]+1)*-1};Buffer.prototype.readInt16LE=function readInt16LE(offset,noAssert){if(!noAssert)checkOffset(offset,2,this.length);var val=this[offset]|this[offset+1]<<8;return val&32768?val|4294901760:val};Buffer.prototype.readInt16BE=function readInt16BE(offset,noAssert){if(!noAssert)checkOffset(offset,2,this.length);var val=this[offset+1]|this[offset]<<8;return val&32768?val|4294901760:val};Buffer.prototype.readInt32LE=function readInt32LE(offset,noAssert){if(!noAssert)checkOffset(offset,4,this.length);return this[offset]|this[offset+1]<<8|this[offset+2]<<16|this[offset+3]<<24};Buffer.prototype.readInt32BE=function readInt32BE(offset,noAssert){if(!noAssert)checkOffset(offset,4,this.length);return this[offset]<<24|this[offset+1]<<16|this[offset+2]<<8|this[offset+3]};Buffer.prototype.readFloatLE=function readFloatLE(offset,noAssert){if(!noAssert)checkOffset(offset,4,this.length);return ieee754.read(this,offset,true,23,4)};Buffer.prototype.readFloatBE=function readFloatBE(offset,noAssert){if(!noAssert)checkOffset(offset,4,this.length);return ieee754.read(this,offset,false,23,4)};Buffer.prototype.readDoubleLE=function readDoubleLE(offset,noAssert){if(!noAssert)checkOffset(offset,8,this.length);return ieee754.read(this,offset,true,52,8)};Buffer.prototype.readDoubleBE=function readDoubleBE(offset,noAssert){if(!noAssert)checkOffset(offset,8,this.length);return ieee754.read(this,offset,false,52,8)};function checkInt(buf,value,offset,ext,max,min){if(!Buffer.isBuffer(buf))throw new TypeError('"buffer" argument must be a Buffer instance');if(value>max||valuebuf.length)throw new RangeError("Index out of range")}Buffer.prototype.writeUIntLE=function writeUIntLE(value,offset,byteLength,noAssert){value=+value;offset=offset|0;byteLength=byteLength|0;if(!noAssert){var maxBytes=Math.pow(2,8*byteLength)-1;checkInt(this,value,offset,byteLength,maxBytes,0)}var mul=1;var i=0;this[offset]=value&255;while(++i=0&&(mul*=256)){this[offset+i]=value/mul&255}return offset+byteLength};Buffer.prototype.writeUInt8=function writeUInt8(value,offset,noAssert){value=+value;offset=offset|0;if(!noAssert)checkInt(this,value,offset,1,255,0);if(!Buffer.TYPED_ARRAY_SUPPORT)value=Math.floor(value);this[offset]=value&255;return offset+1};function objectWriteUInt16(buf,value,offset,littleEndian){if(value<0)value=65535+value+1;for(var i=0,j=Math.min(buf.length-offset,2);i>>(littleEndian?i:1-i)*8}}Buffer.prototype.writeUInt16LE=function writeUInt16LE(value,offset,noAssert){value=+value;offset=offset|0;if(!noAssert)checkInt(this,value,offset,2,65535,0);if(Buffer.TYPED_ARRAY_SUPPORT){this[offset]=value&255;this[offset+1]=value>>>8}else{objectWriteUInt16(this,value,offset,true)}return offset+2};Buffer.prototype.writeUInt16BE=function writeUInt16BE(value,offset,noAssert){value=+value;offset=offset|0;if(!noAssert)checkInt(this,value,offset,2,65535,0);if(Buffer.TYPED_ARRAY_SUPPORT){this[offset]=value>>>8;this[offset+1]=value&255}else{objectWriteUInt16(this,value,offset,false)}return offset+2};function objectWriteUInt32(buf,value,offset,littleEndian){if(value<0)value=4294967295+value+1;for(var i=0,j=Math.min(buf.length-offset,4);i>>(littleEndian?i:3-i)*8&255}}Buffer.prototype.writeUInt32LE=function writeUInt32LE(value,offset,noAssert){value=+value;offset=offset|0;if(!noAssert)checkInt(this,value,offset,4,4294967295,0);if(Buffer.TYPED_ARRAY_SUPPORT){this[offset+3]=value>>>24;this[offset+2]=value>>>16;this[offset+1]=value>>>8;this[offset]=value&255}else{objectWriteUInt32(this,value,offset,true)}return offset+4};Buffer.prototype.writeUInt32BE=function writeUInt32BE(value,offset,noAssert){value=+value;offset=offset|0;if(!noAssert)checkInt(this,value,offset,4,4294967295,0);if(Buffer.TYPED_ARRAY_SUPPORT){this[offset]=value>>>24;this[offset+1]=value>>>16;this[offset+2]=value>>>8;this[offset+3]=value&255}else{objectWriteUInt32(this,value,offset,false)}return offset+4};Buffer.prototype.writeIntLE=function writeIntLE(value,offset,byteLength,noAssert){value=+value;offset=offset|0;if(!noAssert){var limit=Math.pow(2,8*byteLength-1);checkInt(this,value,offset,byteLength,limit-1,-limit)}var i=0;var mul=1;var sub=0;this[offset]=value&255;while(++i>0)-sub&255}return offset+byteLength};Buffer.prototype.writeIntBE=function writeIntBE(value,offset,byteLength,noAssert){value=+value;offset=offset|0;if(!noAssert){var limit=Math.pow(2,8*byteLength-1);checkInt(this,value,offset,byteLength,limit-1,-limit)}var i=byteLength-1;var mul=1;var sub=0;this[offset+i]=value&255;while(--i>=0&&(mul*=256)){if(value<0&&sub===0&&this[offset+i+1]!==0){sub=1}this[offset+i]=(value/mul>>0)-sub&255}return offset+byteLength};Buffer.prototype.writeInt8=function writeInt8(value,offset,noAssert){value=+value;offset=offset|0;if(!noAssert)checkInt(this,value,offset,1,127,-128);if(!Buffer.TYPED_ARRAY_SUPPORT)value=Math.floor(value);if(value<0)value=255+value+1;this[offset]=value&255;return offset+1};Buffer.prototype.writeInt16LE=function writeInt16LE(value,offset,noAssert){value=+value;offset=offset|0;if(!noAssert)checkInt(this,value,offset,2,32767,-32768);if(Buffer.TYPED_ARRAY_SUPPORT){this[offset]=value&255;this[offset+1]=value>>>8}else{objectWriteUInt16(this,value,offset,true)}return offset+2};Buffer.prototype.writeInt16BE=function writeInt16BE(value,offset,noAssert){value=+value;offset=offset|0;if(!noAssert)checkInt(this,value,offset,2,32767,-32768);if(Buffer.TYPED_ARRAY_SUPPORT){this[offset]=value>>>8;this[offset+1]=value&255}else{objectWriteUInt16(this,value,offset,false)}return offset+2};Buffer.prototype.writeInt32LE=function writeInt32LE(value,offset,noAssert){value=+value;offset=offset|0;if(!noAssert)checkInt(this,value,offset,4,2147483647,-2147483648);if(Buffer.TYPED_ARRAY_SUPPORT){this[offset]=value&255;this[offset+1]=value>>>8;this[offset+2]=value>>>16;this[offset+3]=value>>>24}else{objectWriteUInt32(this,value,offset,true)}return offset+4};Buffer.prototype.writeInt32BE=function writeInt32BE(value,offset,noAssert){value=+value;offset=offset|0;if(!noAssert)checkInt(this,value,offset,4,2147483647,-2147483648);if(value<0)value=4294967295+value+1;if(Buffer.TYPED_ARRAY_SUPPORT){this[offset]=value>>>24;this[offset+1]=value>>>16;this[offset+2]=value>>>8;this[offset+3]=value&255}else{objectWriteUInt32(this,value,offset,false)}return offset+4};function checkIEEE754(buf,value,offset,ext,max,min){if(offset+ext>buf.length)throw new RangeError("Index out of range");if(offset<0)throw new RangeError("Index out of range")}function writeFloat(buf,value,offset,littleEndian,noAssert){if(!noAssert){checkIEEE754(buf,value,offset,4,3.4028234663852886e38,-3.4028234663852886e38)}ieee754.write(buf,value,offset,littleEndian,23,4);return offset+4}Buffer.prototype.writeFloatLE=function writeFloatLE(value,offset,noAssert){return writeFloat(this,value,offset,true,noAssert)};Buffer.prototype.writeFloatBE=function writeFloatBE(value,offset,noAssert){return writeFloat(this,value,offset,false,noAssert)};function writeDouble(buf,value,offset,littleEndian,noAssert){if(!noAssert){checkIEEE754(buf,value,offset,8,1.7976931348623157e308,-1.7976931348623157e308)}ieee754.write(buf,value,offset,littleEndian,52,8);return offset+8}Buffer.prototype.writeDoubleLE=function writeDoubleLE(value,offset,noAssert){return writeDouble(this,value,offset,true,noAssert)};Buffer.prototype.writeDoubleBE=function writeDoubleBE(value,offset,noAssert){return writeDouble(this,value,offset,false,noAssert)};Buffer.prototype.copy=function copy(target,targetStart,start,end){if(!start)start=0;if(!end&&end!==0)end=this.length;if(targetStart>=target.length)targetStart=target.length;if(!targetStart)targetStart=0;if(end>0&&end=this.length)throw new RangeError("sourceStart out of bounds");if(end<0)throw new RangeError("sourceEnd out of bounds");if(end>this.length)end=this.length;if(target.length-targetStart=0;--i){target[i+targetStart]=this[i+start]}}else if(len<1e3||!Buffer.TYPED_ARRAY_SUPPORT){for(i=0;i>>0;end=end===undefined?this.length:end>>>0;if(!val)val=0;var i;if(typeof val==="number"){for(i=start;i55295&&codePoint<57344){if(!leadSurrogate){if(codePoint>56319){if((units-=3)>-1)bytes.push(239,191,189);continue}else if(i+1===length){if((units-=3)>-1)bytes.push(239,191,189);continue}leadSurrogate=codePoint;continue}if(codePoint<56320){if((units-=3)>-1)bytes.push(239,191,189);leadSurrogate=codePoint;continue}codePoint=(leadSurrogate-55296<<10|codePoint-56320)+65536}else if(leadSurrogate){if((units-=3)>-1)bytes.push(239,191,189)}leadSurrogate=null;if(codePoint<128){if((units-=1)<0)break;bytes.push(codePoint)}else if(codePoint<2048){if((units-=2)<0)break;bytes.push(codePoint>>6|192,codePoint&63|128)}else if(codePoint<65536){if((units-=3)<0)break;bytes.push(codePoint>>12|224,codePoint>>6&63|128,codePoint&63|128)}else if(codePoint<1114112){if((units-=4)<0)break;bytes.push(codePoint>>18|240,codePoint>>12&63|128,codePoint>>6&63|128,codePoint&63|128)}else{throw new Error("Invalid code point")}}return bytes}function asciiToBytes(str){var byteArray=[];for(var i=0;i>8;lo=c%256;byteArray.push(lo);byteArray.push(hi)}return byteArray}function base64ToBytes(str){return base64.toByteArray(base64clean(str))}function blitBuffer(src,dst,offset,length){for(var i=0;i=dst.length||i>=src.length)break;dst[i+offset]=src[i]}return i}function isnan(val){return val!==val}}).call(this,typeof global!=="undefined"?global:typeof self!=="undefined"?self:typeof window!=="undefined"?window:{})},{"base64-js":11,ieee754:15,isarray:16}],13:[function(require,module,exports){(function(Buffer){var clone=function(){"use strict";function _instanceof(obj,type){return type!=null&&obj instanceof type}var nativeMap;try{nativeMap=Map}catch(_){nativeMap=function(){}}var nativeSet;try{nativeSet=Set}catch(_){nativeSet=function(){}}var nativePromise;try{nativePromise=Promise}catch(_){nativePromise=function(){}}function clone(parent,circular,depth,prototype,includeNonEnumerable){if(typeof circular==="object"){depth=circular.depth;prototype=circular.prototype;includeNonEnumerable=circular.includeNonEnumerable;circular=circular.circular}var allParents=[];var allChildren=[];var useBuffer=typeof Buffer!="undefined";if(typeof circular=="undefined")circular=true;if(typeof depth=="undefined")depth=Infinity;function _clone(parent,depth){if(parent===null)return null;if(depth===0)return parent;var child;var proto;if(typeof parent!="object"){return parent}if(_instanceof(parent,nativeMap)){child=new nativeMap}else if(_instanceof(parent,nativeSet)){child=new nativeSet}else if(_instanceof(parent,nativePromise)){child=new nativePromise(function(resolve,reject){parent.then(function(value){resolve(_clone(value,depth-1))},function(err){reject(_clone(err,depth-1))})})}else if(clone.__isArray(parent)){child=[]}else if(clone.__isRegExp(parent)){child=new RegExp(parent.source,__getRegExpFlags(parent));if(parent.lastIndex)child.lastIndex=parent.lastIndex}else if(clone.__isDate(parent)){child=new Date(parent.getTime())}else if(useBuffer&&Buffer.isBuffer(parent)){child=new Buffer(parent.length);parent.copy(child);return child}else if(_instanceof(parent,Error)){child=Object.create(parent)}else{if(typeof prototype=="undefined"){proto=Object.getPrototypeOf(parent);child=Object.create(proto)}else{child=Object.create(prototype);proto=prototype}}if(circular){var index=allParents.indexOf(parent);if(index!=-1){return allChildren[index]}allParents.push(parent);allChildren.push(child)}if(_instanceof(parent,nativeMap)){parent.forEach(function(value,key){var keyChild=_clone(key,depth-1);var valueChild=_clone(value,depth-1);child.set(keyChild,valueChild)})}if(_instanceof(parent,nativeSet)){parent.forEach(function(value){var entryChild=_clone(value,depth-1);child.add(entryChild)})}for(var i in parent){var attrs;if(proto){attrs=Object.getOwnPropertyDescriptor(proto,i)}if(attrs&&attrs.set==null){continue}child[i]=_clone(parent[i],depth-1)}if(Object.getOwnPropertySymbols){var symbols=Object.getOwnPropertySymbols(parent);for(var i=0;i>1;var nBits=-7;var i=isLE?nBytes-1:0;var d=isLE?-1:1;var s=buffer[offset+i];i+=d;e=s&(1<<-nBits)-1;s>>=-nBits;nBits+=eLen;for(;nBits>0;e=e*256+buffer[offset+i],i+=d,nBits-=8){}m=e&(1<<-nBits)-1;e>>=-nBits;nBits+=mLen;for(;nBits>0;m=m*256+buffer[offset+i],i+=d,nBits-=8){}if(e===0){e=1-eBias}else if(e===eMax){return m?NaN:(s?-1:1)*Infinity}else{m=m+Math.pow(2,mLen);e=e-eBias}return(s?-1:1)*m*Math.pow(2,e-mLen)};exports.write=function(buffer,value,offset,isLE,mLen,nBytes){var e,m,c;var eLen=nBytes*8-mLen-1;var eMax=(1<>1;var rt=mLen===23?Math.pow(2,-24)-Math.pow(2,-77):0;var i=isLE?0:nBytes-1;var d=isLE?1:-1;var s=value<0||value===0&&1/value<0?1:0;value=Math.abs(value);if(isNaN(value)||value===Infinity){m=isNaN(value)?1:0;e=eMax}else{e=Math.floor(Math.log(value)/Math.LN2);if(value*(c=Math.pow(2,-e))<1){e--;c*=2}if(e+eBias>=1){value+=rt/c}else{value+=rt*Math.pow(2,1-eBias)}if(value*c>=2){e++;c/=2}if(e+eBias>=eMax){m=0;e=eMax}else if(e+eBias>=1){m=(value*c-1)*Math.pow(2,mLen);e=e+eBias}else{m=value*Math.pow(2,eBias-1)*Math.pow(2,mLen);e=0}}for(;mLen>=8;buffer[offset+i]=m&255,i+=d,m/=256,mLen-=8){}e=e<0;buffer[offset+i]=e&255,i+=d,e/=256,eLen-=8){}buffer[offset+i-d]|=s*128}},{}],16:[function(require,module,exports){var toString={}.toString;module.exports=Array.isArray||function(arr){return toString.call(arr)=="[object Array]"}},{}],17:[function(require,module,exports){"use strict";var errorMessage;errorMessage="An argument without append, prepend, "+"or detach methods was given to `List";function List(){if(arguments.length){return List.from(arguments)}}var ListPrototype;ListPrototype=List.prototype;List.of=function(){return List.from.call(this,arguments)};List.from=function(items){var list=new this,length,iterator,item;if(items&&(length=items.length)){iterator=-1;while(++iterator0&&len>maxKeys){len=maxKeys}for(var i=0;i=0){kstr=x.substr(0,idx);vstr=x.substr(idx+1)}else{kstr=x;vstr=""}k=decodeURIComponent(kstr);v=decodeURIComponent(vstr);if(!hasOwnProperty(obj,k)){obj[k]=v}else if(isArray(obj[k])){obj[k].push(v)}else{obj[k]=[obj[k],v]}}return obj};var isArray=Array.isArray||function(xs){return Object.prototype.toString.call(xs)==="[object Array]"}},{}],20:[function(require,module,exports){"use strict";var stringifyPrimitive=function(v){switch(typeof v){case"string":return v;case"boolean":return v?"true":"false";case"number":return isFinite(v)?v:"";default:return""}};module.exports=function(obj,sep,eq,name){sep=sep||"&";eq=eq||"=";if(obj===null){obj=undefined}if(typeof obj==="object"){return map(objectKeys(obj),function(k){var ks=encodeURIComponent(stringifyPrimitive(k))+eq;if(isArray(obj[k])){return map(obj[k],function(v){return ks+encodeURIComponent(stringifyPrimitive(v))}).join(sep)}else{return ks+encodeURIComponent(stringifyPrimitive(obj[k]))}}).join(sep)}if(!name)return"";return encodeURIComponent(stringifyPrimitive(name))+eq+encodeURIComponent(stringifyPrimitive(obj))};var isArray=Array.isArray||function(xs){return Object.prototype.toString.call(xs)==="[object Array]"};function map(xs,f){if(xs.map)return xs.map(f);var res=[];for(var i=0;i>2];base64+=base64Chars[(bytes[i]&3)<<4|bytes[i+1]>>4];base64+=base64Chars[(bytes[i+1]&15)<<2|bytes[i+2]>>6];base64+=base64Chars[bytes[i+2]&63]}if(len%3===2){base64=base64.substring(0,base64.length-1)+"="}else if(len%3===1){base64=base64.substring(0,base64.length-2)+"=="}return base64};var binaryToBase64Replacer=function(key,value){if(global.ArrayBuffer&&value instanceof global.ArrayBuffer){return{base64:true,data:arrayBufferToBase64(value)}}else if(global.Buffer){if(value instanceof global.Buffer){return{base64:true,data:value.toString("base64")}}if(value&&value.type=="Buffer"&&value.data instanceof Array){var rehydratedBuffer;if(global.Buffer.from){rehydratedBuffer=global.Buffer.from(value.data)}else{rehydratedBuffer=new global.Buffer(value.data)}return{base64:true,data:rehydratedBuffer.toString("base64")}}}return value};module.exports.decode=function(input){if(input==null){return null}if(input=="#1"||input=="#2"){return input}var message=input.toString();try{return JSON.parse(message)}catch(err){}return message};module.exports.encode=function(object){if(object=="#1"||object=="#2"){return object}return JSON.stringify(object,binaryToBase64Replacer)}}).call(this,typeof global!=="undefined"?global:typeof self!=="undefined"?self:typeof window!=="undefined"?window:{})},{}]},{},[3])(3)}); \ No newline at end of file +(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.socketCluster=f()}})(function(){var define,module,exports;return function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;omaxTimeout){throw new InvalidArgumentsError("The "+propertyName+" value provided exceeded the maximum amount allowed")}};verifyDuration("connectTimeout");verifyDuration("ackTimeout");verifyDuration("pingTimeout");this._localEvents={connect:1,connectAbort:1,disconnect:1,message:1,error:1,raw:1,fail:1,kickOut:1,subscribe:1,unsubscribe:1,subscribeStateChange:1,authStateChange:1,authenticate:1,deauthenticate:1,removeAuthToken:1,subscribeRequest:1};this.connectAttempts=0;this._emitBuffer=new LinkedList;this.channels={};this.options=opts;this._cid=1;this.options.callIdGenerator=function(){return self._cid++};if(this.options.autoReconnect){if(this.options.autoReconnectOptions==null){this.options.autoReconnectOptions={}}var reconnectOptions=this.options.autoReconnectOptions;if(reconnectOptions.initialDelay==null){reconnectOptions.initialDelay=1e4}if(reconnectOptions.randomness==null){reconnectOptions.randomness=1e4}if(reconnectOptions.multiplier==null){reconnectOptions.multiplier=1.5}if(reconnectOptions.maxDelay==null){reconnectOptions.maxDelay=6e4}}if(this.options.subscriptionRetryOptions==null){this.options.subscriptionRetryOptions={}}if(this.options.authEngine){this.auth=this.options.authEngine}else{this.auth=new AuthEngine}if(this.options.codecEngine){this.codec=this.options.codecEngine}else{this.codec=formatter}this.options.path=this.options.path.replace(/\/$/,"")+"/";this.options.query=opts.query||{};if(typeof this.options.query=="string"){this.options.query=querystring.parse(this.options.query)}if(this.options.autoConnect){this.connect()}this._channelEmitter=new Emitter;if(isBrowser&&this.disconnectOnUnload){this._unloadHandler=function(){self.disconnect()};global.addEventListener("beforeunload",this._unloadHandler,false)}};SCSocket.prototype=Object.create(Emitter.prototype);SCSocket.CONNECTING=SCSocket.prototype.CONNECTING=SCTransport.prototype.CONNECTING;SCSocket.OPEN=SCSocket.prototype.OPEN=SCTransport.prototype.OPEN;SCSocket.CLOSED=SCSocket.prototype.CLOSED=SCTransport.prototype.CLOSED;SCSocket.AUTHENTICATED=SCSocket.prototype.AUTHENTICATED="authenticated";SCSocket.UNAUTHENTICATED=SCSocket.prototype.UNAUTHENTICATED="unauthenticated";SCSocket.PENDING=SCSocket.prototype.PENDING="pending";SCSocket.ignoreStatuses=scErrors.socketProtocolIgnoreStatuses;SCSocket.errorStatuses=scErrors.socketProtocolErrorStatuses;SCSocket.prototype._privateEventHandlerMap={"#publish":function(data){var undecoratedChannelName=this._undecorateChannelName(data.channel);var isSubscribed=this.isSubscribed(undecoratedChannelName,true);if(isSubscribed){this._channelEmitter.emit(undecoratedChannelName,data.data)}},"#kickOut":function(data){var undecoratedChannelName=this._undecorateChannelName(data.channel);var channel=this.channels[undecoratedChannelName];if(channel){Emitter.prototype.emit.call(this,"kickOut",data.message,undecoratedChannelName);channel.emit("kickOut",data.message,undecoratedChannelName);this._triggerChannelUnsubscribe(channel)}},"#setAuthToken":function(data,response){var self=this;if(data){var triggerAuthenticate=function(err){if(err){response.error(err);self._onSCError(err)}else{self._changeToAuthenticatedState(data.token);response.end()}};this.auth.saveToken(this.authTokenName,data.token,{},triggerAuthenticate)}else{response.error(new InvalidMessageError("No token data provided by #setAuthToken event"))}},"#removeAuthToken":function(data,response){var self=this;this.auth.removeToken(this.authTokenName,function(err,oldToken){if(err){response.error(err);self._onSCError(err)}else{Emitter.prototype.emit.call(self,"removeAuthToken",oldToken);self._changeToUnauthenticatedStateAndClearTokens();response.end()}})},"#disconnect":function(data){this.transport.close(data.code,data.data)}};SCSocket.prototype.getState=function(){return this.state};SCSocket.prototype.getBytesReceived=function(){return this.transport.getBytesReceived()};SCSocket.prototype.deauthenticate=function(callback){var self=this;this.auth.removeToken(this.authTokenName,function(err,oldToken){if(err){self._onSCError(err)}else{Emitter.prototype.emit.call(self,"removeAuthToken",oldToken);if(self.state!=self.CLOSED){self.emit("#removeAuthToken")}self._changeToUnauthenticatedStateAndClearTokens()}callback&&callback(err)})};SCSocket.prototype.connect=SCSocket.prototype.open=function(){var self=this;if(this.state==this.CLOSED){this.pendingReconnect=false;this.pendingReconnectTimeout=null;clearTimeout(this._reconnectTimeoutRef);this.state=this.CONNECTING;Emitter.prototype.emit.call(this,"connecting");if(this.transport){this.transport.off()}this.transport=new SCTransport(this.auth,this.codec,this.options);this.transport.on("open",function(status){self.state=self.OPEN;self._onSCOpen(status)});this.transport.on("error",function(err){self._onSCError(err)});this.transport.on("close",function(code,data){self.state=self.CLOSED;self._onSCClose(code,data)});this.transport.on("openAbort",function(code,data){self.state=self.CLOSED;self._onSCClose(code,data,true)});this.transport.on("event",function(event,data,res){self._onSCEvent(event,data,res)})}};SCSocket.prototype.reconnect=function(){this.disconnect();this.connect()};SCSocket.prototype.disconnect=function(code,data){code=code||1e3;if(typeof code!="number"){throw new InvalidArgumentsError("If specified, the code argument must be a number")}if(this.state==this.OPEN||this.state==this.CONNECTING){this.transport.close(code,data)}else{this.pendingReconnect=false;this.pendingReconnectTimeout=null;clearTimeout(this._reconnectTimeoutRef)}};SCSocket.prototype.destroy=function(){if(this._unloadHandler){global.removeEventListener("beforeunload",this._unloadHandler,false)}this.disconnect()};SCSocket.prototype._changeToUnauthenticatedStateAndClearTokens=function(){if(this.authState!=this.UNAUTHENTICATED){var oldState=this.authState;this.authState=this.UNAUTHENTICATED;this.signedAuthToken=null;this.authToken=null;var stateChangeData={oldState:oldState,newState:this.authState};Emitter.prototype.emit.call(this,"authStateChange",stateChangeData);if(oldState==this.AUTHENTICATED){Emitter.prototype.emit.call(this,"deauthenticate")}Emitter.prototype.emit.call(this,"authTokenChange",this.signedAuthToken)}};SCSocket.prototype._changeToAuthenticatedState=function(signedAuthToken){this.signedAuthToken=signedAuthToken;this.authToken=this._extractAuthTokenData(signedAuthToken);if(this.authState!=this.AUTHENTICATED){var oldState=this.authState;this.authState=this.AUTHENTICATED;var stateChangeData={oldState:oldState,newState:this.authState,signedAuthToken:signedAuthToken,authToken:this.authToken};if(!this.preparingPendingSubscriptions){this.processPendingSubscriptions()}Emitter.prototype.emit.call(this,"authStateChange",stateChangeData);Emitter.prototype.emit.call(this,"authenticate",signedAuthToken)}Emitter.prototype.emit.call(this,"authTokenChange",signedAuthToken)};SCSocket.prototype.decodeBase64=function(encodedString){var decodedString;if(typeof Buffer=="undefined"){if(global.atob){decodedString=global.atob(encodedString)}else{decodedString=base64.decode(encodedString)}}else{var buffer=new Buffer(encodedString,"base64");decodedString=buffer.toString("utf8")}return decodedString};SCSocket.prototype.encodeBase64=function(decodedString){var encodedString;if(typeof Buffer=="undefined"){if(global.btoa){encodedString=global.btoa(decodedString)}else{encodedString=base64.encode(decodedString)}}else{var buffer=new Buffer(decodedString,"utf8");encodedString=buffer.toString("base64")}return encodedString};SCSocket.prototype._extractAuthTokenData=function(signedAuthToken){var tokenParts=(signedAuthToken||"").split(".");var encodedTokenData=tokenParts[1];if(encodedTokenData!=null){var tokenData=encodedTokenData;try{tokenData=this.decodeBase64(tokenData);return JSON.parse(tokenData)}catch(e){return tokenData}}return null};SCSocket.prototype.getAuthToken=function(){return this.authToken};SCSocket.prototype.getSignedAuthToken=function(){return this.signedAuthToken};SCSocket.prototype.authenticate=function(signedAuthToken,callback){var self=this;this.emit("#authenticate",signedAuthToken,function(err,authStatus){if(authStatus&&authStatus.isAuthenticated!=null){if(authStatus.authError){authStatus.authError=scErrors.hydrateError(authStatus.authError)}}else{authStatus={isAuthenticated:self.authState,authError:null}}if(err){if(err.name!="BadConnectionError"&&err.name!="TimeoutError"){self._changeToUnauthenticatedStateAndClearTokens()}callback&&callback(err,authStatus)}else{self.auth.saveToken(self.authTokenName,signedAuthToken,{},function(err){if(err){self._onSCError(err)}if(authStatus.isAuthenticated){self._changeToAuthenticatedState(signedAuthToken)}else{self._changeToUnauthenticatedStateAndClearTokens()}callback&&callback(err,authStatus)})}})};SCSocket.prototype._tryReconnect=function(initialDelay){var self=this;var exponent=this.connectAttempts++;var reconnectOptions=this.options.autoReconnectOptions;var timeout;if(initialDelay==null||exponent>0){var initialTimeout=Math.round(reconnectOptions.initialDelay+(reconnectOptions.randomness||0)*Math.random());timeout=Math.round(initialTimeout*Math.pow(reconnectOptions.multiplier,exponent))}else{timeout=initialDelay}if(timeout>reconnectOptions.maxDelay){timeout=reconnectOptions.maxDelay}clearTimeout(this._reconnectTimeoutRef);this.pendingReconnect=true;this.pendingReconnectTimeout=timeout;this._reconnectTimeoutRef=setTimeout(function(){self.connect()},timeout)};SCSocket.prototype._onSCOpen=function(status){var self=this;this.preparingPendingSubscriptions=true;if(status){this.id=status.id;this.pingTimeout=status.pingTimeout;this.transport.pingTimeout=this.pingTimeout;if(status.isAuthenticated){this._changeToAuthenticatedState(status.authToken)}else{this._changeToUnauthenticatedStateAndClearTokens()}}else{this._changeToUnauthenticatedStateAndClearTokens()}this.connectAttempts=0;if(this.options.autoSubscribeOnConnect){this.processPendingSubscriptions()}Emitter.prototype.emit.call(this,"connect",status,function(){self.processPendingSubscriptions()});this._flushEmitBuffer()};SCSocket.prototype._onSCError=function(err){var self=this;setTimeout(function(){if(self.listeners("error").length<1){throw err}else{Emitter.prototype.emit.call(self,"error",err)}},0)};SCSocket.prototype._suspendSubscriptions=function(){var channel,newState;for(var channelName in this.channels){if(this.channels.hasOwnProperty(channelName)){channel=this.channels[channelName];if(channel.state==channel.SUBSCRIBED||channel.state==channel.PENDING){newState=channel.PENDING}else{newState=channel.UNSUBSCRIBED}this._triggerChannelUnsubscribe(channel,newState)}}};SCSocket.prototype._abortAllPendingEventsDueToBadConnection=function(failureType){var currentNode=this._emitBuffer.head;var nextNode;while(currentNode){nextNode=currentNode.next;var eventObject=currentNode.data;clearTimeout(eventObject.timeout);delete eventObject.timeout;currentNode.detach();currentNode=nextNode;var callback=eventObject.callback;if(callback){delete eventObject.callback;var errorMessage="Event '"+eventObject.event+"' was aborted due to a bad connection";var error=new BadConnectionError(errorMessage,failureType);callback.call(eventObject,error,eventObject)}}};SCSocket.prototype._onSCClose=function(code,data,openAbort){var self=this;this.id=null;if(this.transport){this.transport.off()}this.pendingReconnect=false;this.pendingReconnectTimeout=null;clearTimeout(this._reconnectTimeoutRef);this._suspendSubscriptions();this._abortAllPendingEventsDueToBadConnection(openAbort?"connectAbort":"disconnect");if(this.options.autoReconnect){if(code==4e3||code==4001||code==1005){this._tryReconnect(0)}else if(code!=1e3&&code<4500){this._tryReconnect()}}if(openAbort){Emitter.prototype.emit.call(self,"connectAbort",code,data)}else{Emitter.prototype.emit.call(self,"disconnect",code,data)}if(!SCSocket.ignoreStatuses[code]){var failureMessage;if(data){failureMessage="Socket connection failed: "+data}else{failureMessage="Socket connection failed for unknown reasons"}var err=new SocketProtocolError(SCSocket.errorStatuses[code]||failureMessage,code);this._onSCError(err)}};SCSocket.prototype._onSCEvent=function(event,data,res){var handler=this._privateEventHandlerMap[event];if(handler){handler.call(this,data,res)}else{Emitter.prototype.emit.call(this,event,data,function(){res&&res.callback.apply(res,arguments)})}};SCSocket.prototype.decode=function(message){return this.transport.decode(message)};SCSocket.prototype.encode=function(object){return this.transport.encode(object)};SCSocket.prototype._flushEmitBuffer=function(){var currentNode=this._emitBuffer.head;var nextNode;while(currentNode){nextNode=currentNode.next;var eventObject=currentNode.data;currentNode.detach();this.transport.emitObject(eventObject);currentNode=nextNode}};SCSocket.prototype._handleEventAckTimeout=function(eventObject,eventNode){if(eventNode){eventNode.detach()}delete eventObject.timeout;var callback=eventObject.callback;if(callback){delete eventObject.callback;var error=new TimeoutError("Event response for '"+eventObject.event+"' timed out");callback.call(eventObject,error,eventObject)}};SCSocket.prototype._emit=function(event,data,callback){var self=this;if(this.state==this.CLOSED){this.connect()}var eventObject={event:event,callback:callback};var eventNode=new LinkedList.Item;if(this.options.cloneData){eventObject.data=clone(data)}else{eventObject.data=data}eventNode.data=eventObject;eventObject.timeout=setTimeout(function(){self._handleEventAckTimeout(eventObject,eventNode)},this.ackTimeout);this._emitBuffer.append(eventNode);if(this.state==this.OPEN){this._flushEmitBuffer()}};SCSocket.prototype.send=function(data){this.transport.send(data)};SCSocket.prototype.emit=function(event,data,callback){if(this._localEvents[event]==null){this._emit(event,data,callback)}else{Emitter.prototype.emit.call(this,event,data)}};SCSocket.prototype.publish=function(channelName,data,callback){var pubData={channel:this._decorateChannelName(channelName),data:data};this.emit("#publish",pubData,callback)};SCSocket.prototype._triggerChannelSubscribe=function(channel,subscriptionOptions){var channelName=channel.name;if(channel.state!=channel.SUBSCRIBED){var oldState=channel.state;channel.state=channel.SUBSCRIBED;var stateChangeData={channel:channelName,oldState:oldState,newState:channel.state,subscriptionOptions:subscriptionOptions};channel.emit("subscribeStateChange",stateChangeData);channel.emit("subscribe",channelName,subscriptionOptions);Emitter.prototype.emit.call(this,"subscribeStateChange",stateChangeData);Emitter.prototype.emit.call(this,"subscribe",channelName,subscriptionOptions)}};SCSocket.prototype._triggerChannelSubscribeFail=function(err,channel,subscriptionOptions){var channelName=channel.name;var meetsAuthRequirements=!channel.waitForAuth||this.authState==this.AUTHENTICATED;if(channel.state!=channel.UNSUBSCRIBED&&meetsAuthRequirements){channel.state=channel.UNSUBSCRIBED;channel.emit("subscribeFail",err,channelName,subscriptionOptions);Emitter.prototype.emit.call(this,"subscribeFail",err,channelName,subscriptionOptions)}};SCSocket.prototype._cancelPendingSubscribeCallback=function(channel){if(channel._pendingSubscriptionCid!=null){this.transport.cancelPendingResponse(channel._pendingSubscriptionCid);delete channel._pendingSubscriptionCid}};SCSocket.prototype._decorateChannelName=function(channelName){if(this.channelPrefix){channelName=this.channelPrefix+channelName}return channelName};SCSocket.prototype._undecorateChannelName=function(decoratedChannelName){if(this.channelPrefix&&decoratedChannelName.indexOf(this.channelPrefix)==0){return decoratedChannelName.replace(this.channelPrefix,"")}return decoratedChannelName};SCSocket.prototype._trySubscribe=function(channel){var self=this;var meetsAuthRequirements=!channel.waitForAuth||this.authState==this.AUTHENTICATED;if(this.state==this.OPEN&&!this.preparingPendingSubscriptions&&channel._pendingSubscriptionCid==null&&meetsAuthRequirements){var options={noTimeout:true};var subscriptionOptions={channel:this._decorateChannelName(channel.name)};if(channel.waitForAuth){options.waitForAuth=true;subscriptionOptions.waitForAuth=options.waitForAuth}if(channel.data){subscriptionOptions.data=channel.data}channel._pendingSubscriptionCid=this.transport.emit("#subscribe",subscriptionOptions,options,function(err){delete channel._pendingSubscriptionCid;if(err){self._triggerChannelSubscribeFail(err,channel,subscriptionOptions)}else{self._triggerChannelSubscribe(channel,subscriptionOptions)}});Emitter.prototype.emit.call(this,"subscribeRequest",channel.name,subscriptionOptions)}};SCSocket.prototype.subscribe=function(channelName,options){var channel=this.channels[channelName];if(!channel){channel=new SCChannel(channelName,this,options);this.channels[channelName]=channel}else if(options){channel.setOptions(options)}if(channel.state==channel.UNSUBSCRIBED){channel.state=channel.PENDING;this._trySubscribe(channel)}return channel};SCSocket.prototype._triggerChannelUnsubscribe=function(channel,newState){var channelName=channel.name;var oldState=channel.state;if(newState){channel.state=newState}else{channel.state=channel.UNSUBSCRIBED}this._cancelPendingSubscribeCallback(channel);if(oldState==channel.SUBSCRIBED){var stateChangeData={channel:channelName,oldState:oldState,newState:channel.state};channel.emit("subscribeStateChange",stateChangeData);channel.emit("unsubscribe",channelName);Emitter.prototype.emit.call(this,"subscribeStateChange",stateChangeData);Emitter.prototype.emit.call(this,"unsubscribe",channelName)}};SCSocket.prototype._tryUnsubscribe=function(channel){var self=this;if(this.state==this.OPEN){var options={noTimeout:true};this._cancelPendingSubscribeCallback(channel);var decoratedChannelName=this._decorateChannelName(channel.name);this.transport.emit("#unsubscribe",decoratedChannelName,options)}};SCSocket.prototype.unsubscribe=function(channelName){var channel=this.channels[channelName];if(channel){if(channel.state!=channel.UNSUBSCRIBED){this._triggerChannelUnsubscribe(channel);this._tryUnsubscribe(channel)}}};SCSocket.prototype.channel=function(channelName,options){var currentChannel=this.channels[channelName];if(!currentChannel){currentChannel=new SCChannel(channelName,this,options);this.channels[channelName]=currentChannel}return currentChannel};SCSocket.prototype.destroyChannel=function(channelName){var channel=this.channels[channelName];channel.unwatch();channel.unsubscribe();delete this.channels[channelName]};SCSocket.prototype.subscriptions=function(includePending){var subs=[];var channel,includeChannel +;for(var channelName in this.channels){if(this.channels.hasOwnProperty(channelName)){channel=this.channels[channelName];if(includePending){includeChannel=channel&&(channel.state==channel.SUBSCRIBED||channel.state==channel.PENDING)}else{includeChannel=channel&&channel.state==channel.SUBSCRIBED}if(includeChannel){subs.push(channelName)}}}return subs};SCSocket.prototype.isSubscribed=function(channelName,includePending){var channel=this.channels[channelName];if(includePending){return!!channel&&(channel.state==channel.SUBSCRIBED||channel.state==channel.PENDING)}return!!channel&&channel.state==channel.SUBSCRIBED};SCSocket.prototype.processPendingSubscriptions=function(){var self=this;this.preparingPendingSubscriptions=false;var pendingChannels=[];for(var i in this.channels){if(this.channels.hasOwnProperty(i)){var channel=this.channels[i];if(channel.state==channel.PENDING){pendingChannels.push(channel)}}}pendingChannels.sort(function(a,b){var ap=a.priority||0;var bp=b.priority||0;if(ap>bp){return-1}if(ap>(-2*bitCounter&6))}}return output};var encode=function(input){input=String(input);if(/[^\0-\xFF]/.test(input)){error("The string to be encoded contains characters outside of the "+"Latin1 range.")}var padding=input.length%3;var output="";var position=-1;var a;var b;var c;var d;var buffer;var length=input.length-padding;while(++position>18&63)+TABLE.charAt(buffer>>12&63)+TABLE.charAt(buffer>>6&63)+TABLE.charAt(buffer&63)}if(padding==2){a=input.charCodeAt(position)<<8;b=input.charCodeAt(++position);buffer=a+b;output+=TABLE.charAt(buffer>>10)+TABLE.charAt(buffer>>4&63)+TABLE.charAt(buffer<<2&63)+"="}else if(padding==1){buffer=input.charCodeAt(position);output+=TABLE.charAt(buffer>>2)+TABLE.charAt(buffer<<4&63)+"=="}return output};var base64={encode:encode,decode:decode,version:"0.1.0"};if(typeof define=="function"&&typeof define.amd=="object"&&define.amd){define(function(){return base64})}else if(freeExports&&!freeExports.nodeType){if(freeModule){freeModule.exports=base64}else{for(var key in base64){base64.hasOwnProperty(key)&&(freeExports[key]=base64[key])}}}else{root.base64=base64}})(this)}).call(this,typeof global!=="undefined"?global:typeof self!=="undefined"?self:typeof window!=="undefined"?window:{})},{}],11:[function(require,module,exports){"use strict";exports.byteLength=byteLength;exports.toByteArray=toByteArray;exports.fromByteArray=fromByteArray;var lookup=[];var revLookup=[];var Arr=typeof Uint8Array!=="undefined"?Uint8Array:Array;var code="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";for(var i=0,len=code.length;i0){throw new Error("Invalid string. Length must be a multiple of 4")}return b64[len-2]==="="?2:b64[len-1]==="="?1:0}function byteLength(b64){return b64.length*3/4-placeHoldersCount(b64)}function toByteArray(b64){var i,j,l,tmp,placeHolders,arr;var len=b64.length;placeHolders=placeHoldersCount(b64);arr=new Arr(len*3/4-placeHolders);l=placeHolders>0?len-4:len;var L=0;for(i=0,j=0;i>16&255;arr[L++]=tmp>>8&255;arr[L++]=tmp&255}if(placeHolders===2){tmp=revLookup[b64.charCodeAt(i)]<<2|revLookup[b64.charCodeAt(i+1)]>>4;arr[L++]=tmp&255}else if(placeHolders===1){tmp=revLookup[b64.charCodeAt(i)]<<10|revLookup[b64.charCodeAt(i+1)]<<4|revLookup[b64.charCodeAt(i+2)]>>2;arr[L++]=tmp>>8&255;arr[L++]=tmp&255}return arr}function tripletToBase64(num){return lookup[num>>18&63]+lookup[num>>12&63]+lookup[num>>6&63]+lookup[num&63]}function encodeChunk(uint8,start,end){var tmp;var output=[];for(var i=start;ilen2?len2:i+maxChunkLength))}if(extraBytes===1){tmp=uint8[len-1];output+=lookup[tmp>>2];output+=lookup[tmp<<4&63];output+="=="}else if(extraBytes===2){tmp=(uint8[len-2]<<8)+uint8[len-1];output+=lookup[tmp>>10];output+=lookup[tmp>>4&63];output+=lookup[tmp<<2&63];output+="="}parts.push(output);return parts.join("")}},{}],12:[function(require,module,exports){(function(global){"use strict";var base64=require("base64-js");var ieee754=require("ieee754");var isArray=require("isarray");exports.Buffer=Buffer;exports.SlowBuffer=SlowBuffer;exports.INSPECT_MAX_BYTES=50;Buffer.TYPED_ARRAY_SUPPORT=global.TYPED_ARRAY_SUPPORT!==undefined?global.TYPED_ARRAY_SUPPORT:typedArraySupport();exports.kMaxLength=kMaxLength();function typedArraySupport(){try{var arr=new Uint8Array(1);arr.__proto__={__proto__:Uint8Array.prototype,foo:function(){return 42}};return arr.foo()===42&&typeof arr.subarray==="function"&&arr.subarray(1,1).byteLength===0}catch(e){return false}}function kMaxLength(){return Buffer.TYPED_ARRAY_SUPPORT?2147483647:1073741823}function createBuffer(that,length){if(kMaxLength()=kMaxLength()){throw new RangeError("Attempt to allocate Buffer larger than maximum "+"size: 0x"+kMaxLength().toString(16)+" bytes")}return length|0}function SlowBuffer(length){if(+length!=length){length=0}return Buffer.alloc(+length)}Buffer.isBuffer=function isBuffer(b){return!!(b!=null&&b._isBuffer)};Buffer.compare=function compare(a,b){if(!Buffer.isBuffer(a)||!Buffer.isBuffer(b)){throw new TypeError("Arguments must be Buffers")}if(a===b)return 0;var x=a.length;var y=b.length;for(var i=0,len=Math.min(x,y);i>>1;case"base64":return base64ToBytes(string).length;default:if(loweredCase)return utf8ToBytes(string).length;encoding=(""+encoding).toLowerCase();loweredCase=true}}}Buffer.byteLength=byteLength;function slowToString(encoding,start,end){var loweredCase=false;if(start===undefined||start<0){start=0}if(start>this.length){return""}if(end===undefined||end>this.length){end=this.length}if(end<=0){return""}end>>>=0;start>>>=0;if(end<=start){return""}if(!encoding)encoding="utf8";while(true){switch(encoding){case"hex":return hexSlice(this,start,end);case"utf8":case"utf-8":return utf8Slice(this,start,end);case"ascii":return asciiSlice(this,start,end);case"latin1":case"binary":return latin1Slice(this,start,end);case"base64":return base64Slice(this,start,end);case"ucs2":case"ucs-2":case"utf16le":case"utf-16le":return utf16leSlice(this,start,end);default:if(loweredCase)throw new TypeError("Unknown encoding: "+encoding);encoding=(encoding+"").toLowerCase();loweredCase=true}}}Buffer.prototype._isBuffer=true;function swap(b,n,m){var i=b[n];b[n]=b[m];b[m]=i}Buffer.prototype.swap16=function swap16(){var len=this.length;if(len%2!==0){throw new RangeError("Buffer size must be a multiple of 16-bits")}for(var i=0;i0){str=this.toString("hex",0,max).match(/.{2}/g).join(" ");if(this.length>max)str+=" ... "}return""};Buffer.prototype.compare=function compare(target,start,end,thisStart,thisEnd){if(!Buffer.isBuffer(target)){throw new TypeError("Argument must be a Buffer")}if(start===undefined){start=0}if(end===undefined){end=target?target.length:0}if(thisStart===undefined){thisStart=0}if(thisEnd===undefined){thisEnd=this.length}if(start<0||end>target.length||thisStart<0||thisEnd>this.length){throw new RangeError("out of range index")}if(thisStart>=thisEnd&&start>=end){return 0}if(thisStart>=thisEnd){return-1}if(start>=end){return 1}start>>>=0;end>>>=0;thisStart>>>=0;thisEnd>>>=0;if(this===target)return 0;var x=thisEnd-thisStart;var y=end-start;var len=Math.min(x,y);var thisCopy=this.slice(thisStart,thisEnd);var targetCopy=target.slice(start,end);for(var i=0;i2147483647){byteOffset=2147483647}else if(byteOffset<-2147483648){byteOffset=-2147483648}byteOffset=+byteOffset;if(isNaN(byteOffset)){byteOffset=dir?0:buffer.length-1}if(byteOffset<0)byteOffset=buffer.length+byteOffset;if(byteOffset>=buffer.length){if(dir)return-1;else byteOffset=buffer.length-1}else if(byteOffset<0){if(dir)byteOffset=0;else return-1}if(typeof val==="string"){val=Buffer.from(val,encoding)}if(Buffer.isBuffer(val)){if(val.length===0){return-1}return arrayIndexOf(buffer,val,byteOffset,encoding,dir)}else if(typeof val==="number"){val=val&255;if(Buffer.TYPED_ARRAY_SUPPORT&&typeof Uint8Array.prototype.indexOf==="function"){if(dir){return Uint8Array.prototype.indexOf.call(buffer,val,byteOffset)}else{return Uint8Array.prototype.lastIndexOf.call(buffer,val,byteOffset)}}return arrayIndexOf(buffer,[val],byteOffset,encoding,dir)}throw new TypeError("val must be string, number or Buffer")}function arrayIndexOf(arr,val,byteOffset,encoding,dir){var indexSize=1;var arrLength=arr.length;var valLength=val.length;if(encoding!==undefined){encoding=String(encoding).toLowerCase();if(encoding==="ucs2"||encoding==="ucs-2"||encoding==="utf16le"||encoding==="utf-16le"){if(arr.length<2||val.length<2){return-1}indexSize=2;arrLength/=2;valLength/=2;byteOffset/=2}}function read(buf,i){if(indexSize===1){return buf[i]}else{return buf.readUInt16BE(i*indexSize)}}var i;if(dir){var foundIndex=-1;for(i=byteOffset;iarrLength)byteOffset=arrLength-valLength;for(i=byteOffset;i>=0;i--){var found=true;for(var j=0;jremaining){length=remaining}}var strLen=string.length;if(strLen%2!==0)throw new TypeError("Invalid hex string");if(length>strLen/2){length=strLen/2}for(var i=0;iremaining)length=remaining;if(string.length>0&&(length<0||offset<0)||offset>this.length){throw new RangeError("Attempt to write outside buffer bounds")}if(!encoding)encoding="utf8";var loweredCase=false;for(;;){switch(encoding){case"hex":return hexWrite(this,string,offset,length);case"utf8":case"utf-8":return utf8Write(this,string,offset,length);case"ascii": +return asciiWrite(this,string,offset,length);case"latin1":case"binary":return latin1Write(this,string,offset,length);case"base64":return base64Write(this,string,offset,length);case"ucs2":case"ucs-2":case"utf16le":case"utf-16le":return ucs2Write(this,string,offset,length);default:if(loweredCase)throw new TypeError("Unknown encoding: "+encoding);encoding=(""+encoding).toLowerCase();loweredCase=true}}};Buffer.prototype.toJSON=function toJSON(){return{type:"Buffer",data:Array.prototype.slice.call(this._arr||this,0)}};function base64Slice(buf,start,end){if(start===0&&end===buf.length){return base64.fromByteArray(buf)}else{return base64.fromByteArray(buf.slice(start,end))}}function utf8Slice(buf,start,end){end=Math.min(buf.length,end);var res=[];var i=start;while(i239?4:firstByte>223?3:firstByte>191?2:1;if(i+bytesPerSequence<=end){var secondByte,thirdByte,fourthByte,tempCodePoint;switch(bytesPerSequence){case 1:if(firstByte<128){codePoint=firstByte}break;case 2:secondByte=buf[i+1];if((secondByte&192)===128){tempCodePoint=(firstByte&31)<<6|secondByte&63;if(tempCodePoint>127){codePoint=tempCodePoint}}break;case 3:secondByte=buf[i+1];thirdByte=buf[i+2];if((secondByte&192)===128&&(thirdByte&192)===128){tempCodePoint=(firstByte&15)<<12|(secondByte&63)<<6|thirdByte&63;if(tempCodePoint>2047&&(tempCodePoint<55296||tempCodePoint>57343)){codePoint=tempCodePoint}}break;case 4:secondByte=buf[i+1];thirdByte=buf[i+2];fourthByte=buf[i+3];if((secondByte&192)===128&&(thirdByte&192)===128&&(fourthByte&192)===128){tempCodePoint=(firstByte&15)<<18|(secondByte&63)<<12|(thirdByte&63)<<6|fourthByte&63;if(tempCodePoint>65535&&tempCodePoint<1114112){codePoint=tempCodePoint}}}}if(codePoint===null){codePoint=65533;bytesPerSequence=1}else if(codePoint>65535){codePoint-=65536;res.push(codePoint>>>10&1023|55296);codePoint=56320|codePoint&1023}res.push(codePoint);i+=bytesPerSequence}return decodeCodePointsArray(res)}var MAX_ARGUMENTS_LENGTH=4096;function decodeCodePointsArray(codePoints){var len=codePoints.length;if(len<=MAX_ARGUMENTS_LENGTH){return String.fromCharCode.apply(String,codePoints)}var res="";var i=0;while(ilen)end=len;var out="";for(var i=start;ilen){start=len}if(end<0){end+=len;if(end<0)end=0}else if(end>len){end=len}if(endlength)throw new RangeError("Trying to access beyond buffer length")}Buffer.prototype.readUIntLE=function readUIntLE(offset,byteLength,noAssert){offset=offset|0;byteLength=byteLength|0;if(!noAssert)checkOffset(offset,byteLength,this.length);var val=this[offset];var mul=1;var i=0;while(++i0&&(mul*=256)){val+=this[offset+--byteLength]*mul}return val};Buffer.prototype.readUInt8=function readUInt8(offset,noAssert){if(!noAssert)checkOffset(offset,1,this.length);return this[offset]};Buffer.prototype.readUInt16LE=function readUInt16LE(offset,noAssert){if(!noAssert)checkOffset(offset,2,this.length);return this[offset]|this[offset+1]<<8};Buffer.prototype.readUInt16BE=function readUInt16BE(offset,noAssert){if(!noAssert)checkOffset(offset,2,this.length);return this[offset]<<8|this[offset+1]};Buffer.prototype.readUInt32LE=function readUInt32LE(offset,noAssert){if(!noAssert)checkOffset(offset,4,this.length);return(this[offset]|this[offset+1]<<8|this[offset+2]<<16)+this[offset+3]*16777216};Buffer.prototype.readUInt32BE=function readUInt32BE(offset,noAssert){if(!noAssert)checkOffset(offset,4,this.length);return this[offset]*16777216+(this[offset+1]<<16|this[offset+2]<<8|this[offset+3])};Buffer.prototype.readIntLE=function readIntLE(offset,byteLength,noAssert){offset=offset|0;byteLength=byteLength|0;if(!noAssert)checkOffset(offset,byteLength,this.length);var val=this[offset];var mul=1;var i=0;while(++i=mul)val-=Math.pow(2,8*byteLength);return val};Buffer.prototype.readIntBE=function readIntBE(offset,byteLength,noAssert){offset=offset|0;byteLength=byteLength|0;if(!noAssert)checkOffset(offset,byteLength,this.length);var i=byteLength;var mul=1;var val=this[offset+--i];while(i>0&&(mul*=256)){val+=this[offset+--i]*mul}mul*=128;if(val>=mul)val-=Math.pow(2,8*byteLength);return val};Buffer.prototype.readInt8=function readInt8(offset,noAssert){if(!noAssert)checkOffset(offset,1,this.length);if(!(this[offset]&128))return this[offset];return(255-this[offset]+1)*-1};Buffer.prototype.readInt16LE=function readInt16LE(offset,noAssert){if(!noAssert)checkOffset(offset,2,this.length);var val=this[offset]|this[offset+1]<<8;return val&32768?val|4294901760:val};Buffer.prototype.readInt16BE=function readInt16BE(offset,noAssert){if(!noAssert)checkOffset(offset,2,this.length);var val=this[offset+1]|this[offset]<<8;return val&32768?val|4294901760:val};Buffer.prototype.readInt32LE=function readInt32LE(offset,noAssert){if(!noAssert)checkOffset(offset,4,this.length);return this[offset]|this[offset+1]<<8|this[offset+2]<<16|this[offset+3]<<24};Buffer.prototype.readInt32BE=function readInt32BE(offset,noAssert){if(!noAssert)checkOffset(offset,4,this.length);return this[offset]<<24|this[offset+1]<<16|this[offset+2]<<8|this[offset+3]};Buffer.prototype.readFloatLE=function readFloatLE(offset,noAssert){if(!noAssert)checkOffset(offset,4,this.length);return ieee754.read(this,offset,true,23,4)};Buffer.prototype.readFloatBE=function readFloatBE(offset,noAssert){if(!noAssert)checkOffset(offset,4,this.length);return ieee754.read(this,offset,false,23,4)};Buffer.prototype.readDoubleLE=function readDoubleLE(offset,noAssert){if(!noAssert)checkOffset(offset,8,this.length);return ieee754.read(this,offset,true,52,8)};Buffer.prototype.readDoubleBE=function readDoubleBE(offset,noAssert){if(!noAssert)checkOffset(offset,8,this.length);return ieee754.read(this,offset,false,52,8)};function checkInt(buf,value,offset,ext,max,min){if(!Buffer.isBuffer(buf))throw new TypeError('"buffer" argument must be a Buffer instance');if(value>max||valuebuf.length)throw new RangeError("Index out of range")}Buffer.prototype.writeUIntLE=function writeUIntLE(value,offset,byteLength,noAssert){value=+value;offset=offset|0;byteLength=byteLength|0;if(!noAssert){var maxBytes=Math.pow(2,8*byteLength)-1;checkInt(this,value,offset,byteLength,maxBytes,0)}var mul=1;var i=0;this[offset]=value&255;while(++i=0&&(mul*=256)){this[offset+i]=value/mul&255}return offset+byteLength};Buffer.prototype.writeUInt8=function writeUInt8(value,offset,noAssert){value=+value;offset=offset|0;if(!noAssert)checkInt(this,value,offset,1,255,0);if(!Buffer.TYPED_ARRAY_SUPPORT)value=Math.floor(value);this[offset]=value&255;return offset+1};function objectWriteUInt16(buf,value,offset,littleEndian){if(value<0)value=65535+value+1;for(var i=0,j=Math.min(buf.length-offset,2);i>>(littleEndian?i:1-i)*8}}Buffer.prototype.writeUInt16LE=function writeUInt16LE(value,offset,noAssert){value=+value;offset=offset|0;if(!noAssert)checkInt(this,value,offset,2,65535,0);if(Buffer.TYPED_ARRAY_SUPPORT){this[offset]=value&255;this[offset+1]=value>>>8}else{objectWriteUInt16(this,value,offset,true)}return offset+2};Buffer.prototype.writeUInt16BE=function writeUInt16BE(value,offset,noAssert){value=+value;offset=offset|0;if(!noAssert)checkInt(this,value,offset,2,65535,0);if(Buffer.TYPED_ARRAY_SUPPORT){this[offset]=value>>>8;this[offset+1]=value&255}else{objectWriteUInt16(this,value,offset,false)}return offset+2};function objectWriteUInt32(buf,value,offset,littleEndian){if(value<0)value=4294967295+value+1;for(var i=0,j=Math.min(buf.length-offset,4);i>>(littleEndian?i:3-i)*8&255}}Buffer.prototype.writeUInt32LE=function writeUInt32LE(value,offset,noAssert){value=+value;offset=offset|0;if(!noAssert)checkInt(this,value,offset,4,4294967295,0);if(Buffer.TYPED_ARRAY_SUPPORT){this[offset+3]=value>>>24;this[offset+2]=value>>>16;this[offset+1]=value>>>8;this[offset]=value&255}else{objectWriteUInt32(this,value,offset,true)}return offset+4};Buffer.prototype.writeUInt32BE=function writeUInt32BE(value,offset,noAssert){value=+value;offset=offset|0;if(!noAssert)checkInt(this,value,offset,4,4294967295,0);if(Buffer.TYPED_ARRAY_SUPPORT){this[offset]=value>>>24;this[offset+1]=value>>>16;this[offset+2]=value>>>8;this[offset+3]=value&255}else{objectWriteUInt32(this,value,offset,false)}return offset+4};Buffer.prototype.writeIntLE=function writeIntLE(value,offset,byteLength,noAssert){value=+value;offset=offset|0;if(!noAssert){var limit=Math.pow(2,8*byteLength-1);checkInt(this,value,offset,byteLength,limit-1,-limit)}var i=0;var mul=1;var sub=0;this[offset]=value&255;while(++i>0)-sub&255}return offset+byteLength};Buffer.prototype.writeIntBE=function writeIntBE(value,offset,byteLength,noAssert){value=+value;offset=offset|0;if(!noAssert){var limit=Math.pow(2,8*byteLength-1);checkInt(this,value,offset,byteLength,limit-1,-limit)}var i=byteLength-1;var mul=1;var sub=0;this[offset+i]=value&255;while(--i>=0&&(mul*=256)){if(value<0&&sub===0&&this[offset+i+1]!==0){sub=1}this[offset+i]=(value/mul>>0)-sub&255}return offset+byteLength};Buffer.prototype.writeInt8=function writeInt8(value,offset,noAssert){value=+value;offset=offset|0;if(!noAssert)checkInt(this,value,offset,1,127,-128);if(!Buffer.TYPED_ARRAY_SUPPORT)value=Math.floor(value);if(value<0)value=255+value+1;this[offset]=value&255;return offset+1};Buffer.prototype.writeInt16LE=function writeInt16LE(value,offset,noAssert){value=+value;offset=offset|0;if(!noAssert)checkInt(this,value,offset,2,32767,-32768);if(Buffer.TYPED_ARRAY_SUPPORT){this[offset]=value&255;this[offset+1]=value>>>8}else{objectWriteUInt16(this,value,offset,true)}return offset+2};Buffer.prototype.writeInt16BE=function writeInt16BE(value,offset,noAssert){value=+value;offset=offset|0;if(!noAssert)checkInt(this,value,offset,2,32767,-32768);if(Buffer.TYPED_ARRAY_SUPPORT){this[offset]=value>>>8;this[offset+1]=value&255}else{objectWriteUInt16(this,value,offset,false)}return offset+2};Buffer.prototype.writeInt32LE=function writeInt32LE(value,offset,noAssert){value=+value;offset=offset|0;if(!noAssert)checkInt(this,value,offset,4,2147483647,-2147483648);if(Buffer.TYPED_ARRAY_SUPPORT){this[offset]=value&255;this[offset+1]=value>>>8;this[offset+2]=value>>>16;this[offset+3]=value>>>24}else{objectWriteUInt32(this,value,offset,true)}return offset+4};Buffer.prototype.writeInt32BE=function writeInt32BE(value,offset,noAssert){value=+value;offset=offset|0;if(!noAssert)checkInt(this,value,offset,4,2147483647,-2147483648);if(value<0)value=4294967295+value+1;if(Buffer.TYPED_ARRAY_SUPPORT){this[offset]=value>>>24;this[offset+1]=value>>>16;this[offset+2]=value>>>8;this[offset+3]=value&255}else{objectWriteUInt32(this,value,offset,false)}return offset+4};function checkIEEE754(buf,value,offset,ext,max,min){if(offset+ext>buf.length)throw new RangeError("Index out of range");if(offset<0)throw new RangeError("Index out of range")}function writeFloat(buf,value,offset,littleEndian,noAssert){if(!noAssert){checkIEEE754(buf,value,offset,4,3.4028234663852886e38,-3.4028234663852886e38)}ieee754.write(buf,value,offset,littleEndian,23,4);return offset+4}Buffer.prototype.writeFloatLE=function writeFloatLE(value,offset,noAssert){return writeFloat(this,value,offset,true,noAssert)};Buffer.prototype.writeFloatBE=function writeFloatBE(value,offset,noAssert){return writeFloat(this,value,offset,false,noAssert)};function writeDouble(buf,value,offset,littleEndian,noAssert){if(!noAssert){checkIEEE754(buf,value,offset,8,1.7976931348623157e308,-1.7976931348623157e308)}ieee754.write(buf,value,offset,littleEndian,52,8);return offset+8}Buffer.prototype.writeDoubleLE=function writeDoubleLE(value,offset,noAssert){return writeDouble(this,value,offset,true,noAssert)};Buffer.prototype.writeDoubleBE=function writeDoubleBE(value,offset,noAssert){return writeDouble(this,value,offset,false,noAssert)};Buffer.prototype.copy=function copy(target,targetStart,start,end){if(!start)start=0;if(!end&&end!==0)end=this.length;if(targetStart>=target.length)targetStart=target.length;if(!targetStart)targetStart=0;if(end>0&&end=this.length)throw new RangeError("sourceStart out of bounds");if(end<0)throw new RangeError("sourceEnd out of bounds");if(end>this.length)end=this.length;if(target.length-targetStart=0;--i){target[i+targetStart]=this[i+start]}}else if(len<1e3||!Buffer.TYPED_ARRAY_SUPPORT){for(i=0;i>>0;end=end===undefined?this.length:end>>>0;if(!val)val=0;var i;if(typeof val==="number"){for(i=start;i55295&&codePoint<57344){if(!leadSurrogate){if(codePoint>56319){if((units-=3)>-1)bytes.push(239,191,189);continue}else if(i+1===length){if((units-=3)>-1)bytes.push(239,191,189);continue}leadSurrogate=codePoint;continue}if(codePoint<56320){if((units-=3)>-1)bytes.push(239,191,189);leadSurrogate=codePoint;continue}codePoint=(leadSurrogate-55296<<10|codePoint-56320)+65536}else if(leadSurrogate){if((units-=3)>-1)bytes.push(239,191,189)}leadSurrogate=null;if(codePoint<128){if((units-=1)<0)break;bytes.push(codePoint)}else if(codePoint<2048){if((units-=2)<0)break;bytes.push(codePoint>>6|192,codePoint&63|128)}else if(codePoint<65536){if((units-=3)<0)break;bytes.push(codePoint>>12|224,codePoint>>6&63|128,codePoint&63|128)}else if(codePoint<1114112){if((units-=4)<0)break;bytes.push(codePoint>>18|240,codePoint>>12&63|128,codePoint>>6&63|128,codePoint&63|128)}else{throw new Error("Invalid code point")}}return bytes}function asciiToBytes(str){var byteArray=[];for(var i=0;i>8;lo=c%256;byteArray.push(lo);byteArray.push(hi)}return byteArray}function base64ToBytes(str){return base64.toByteArray(base64clean(str))}function blitBuffer(src,dst,offset,length){for(var i=0;i=dst.length||i>=src.length)break;dst[i+offset]=src[i]}return i}function isnan(val){return val!==val}}).call(this,typeof global!=="undefined"?global:typeof self!=="undefined"?self:typeof window!=="undefined"?window:{})},{"base64-js":11,ieee754:15,isarray:16}],13:[function(require,module,exports){(function(Buffer){var clone=function(){"use strict";function _instanceof(obj,type){return type!=null&&obj instanceof type}var nativeMap;try{nativeMap=Map}catch(_){nativeMap=function(){}}var nativeSet;try{nativeSet=Set}catch(_){nativeSet=function(){}}var nativePromise;try{nativePromise=Promise}catch(_){nativePromise=function(){}}function clone(parent,circular,depth,prototype,includeNonEnumerable){if(typeof circular==="object"){depth=circular.depth;prototype=circular.prototype;includeNonEnumerable=circular.includeNonEnumerable;circular=circular.circular}var allParents=[];var allChildren=[];var useBuffer=typeof Buffer!="undefined";if(typeof circular=="undefined")circular=true;if(typeof depth=="undefined")depth=Infinity;function _clone(parent,depth){if(parent===null)return null;if(depth===0)return parent;var child;var proto;if(typeof parent!="object"){return parent}if(_instanceof(parent,nativeMap)){child=new nativeMap}else if(_instanceof(parent,nativeSet)){child=new nativeSet}else if(_instanceof(parent,nativePromise)){child=new nativePromise(function(resolve,reject){parent.then(function(value){resolve(_clone(value,depth-1))},function(err){reject(_clone(err,depth-1))})})}else if(clone.__isArray(parent)){child=[]}else if(clone.__isRegExp(parent)){child=new RegExp(parent.source,__getRegExpFlags(parent));if(parent.lastIndex)child.lastIndex=parent.lastIndex}else if(clone.__isDate(parent)){child=new Date(parent.getTime())}else if(useBuffer&&Buffer.isBuffer(parent)){child=new Buffer(parent.length);parent.copy(child);return child}else if(_instanceof(parent,Error)){child=Object.create(parent)}else{if(typeof prototype=="undefined"){proto=Object.getPrototypeOf(parent);child=Object.create(proto)}else{child=Object.create(prototype);proto=prototype}}if(circular){var index=allParents.indexOf(parent);if(index!=-1){return allChildren[index]}allParents.push(parent);allChildren.push(child)}if(_instanceof(parent,nativeMap)){parent.forEach(function(value,key){var keyChild=_clone(key,depth-1);var valueChild=_clone(value,depth-1);child.set(keyChild,valueChild)})}if(_instanceof(parent,nativeSet)){parent.forEach(function(value){var entryChild=_clone(value,depth-1);child.add(entryChild)})}for(var i in parent){var attrs;if(proto){attrs=Object.getOwnPropertyDescriptor(proto,i)}if(attrs&&attrs.set==null){continue}child[i]=_clone(parent[i],depth-1)}if(Object.getOwnPropertySymbols){var symbols=Object.getOwnPropertySymbols(parent);for(var i=0;i>1;var nBits=-7;var i=isLE?nBytes-1:0;var d=isLE?-1:1;var s=buffer[offset+i];i+=d;e=s&(1<<-nBits)-1;s>>=-nBits;nBits+=eLen;for(;nBits>0;e=e*256+buffer[offset+i],i+=d,nBits-=8){}m=e&(1<<-nBits)-1;e>>=-nBits;nBits+=mLen;for(;nBits>0;m=m*256+buffer[offset+i],i+=d,nBits-=8){}if(e===0){e=1-eBias}else if(e===eMax){return m?NaN:(s?-1:1)*Infinity}else{m=m+Math.pow(2,mLen);e=e-eBias}return(s?-1:1)*m*Math.pow(2,e-mLen)};exports.write=function(buffer,value,offset,isLE,mLen,nBytes){var e,m,c;var eLen=nBytes*8-mLen-1;var eMax=(1<>1;var rt=mLen===23?Math.pow(2,-24)-Math.pow(2,-77):0;var i=isLE?0:nBytes-1;var d=isLE?1:-1;var s=value<0||value===0&&1/value<0?1:0;value=Math.abs(value);if(isNaN(value)||value===Infinity){m=isNaN(value)?1:0;e=eMax}else{e=Math.floor(Math.log(value)/Math.LN2);if(value*(c=Math.pow(2,-e))<1){e--;c*=2}if(e+eBias>=1){value+=rt/c}else{value+=rt*Math.pow(2,1-eBias)}if(value*c>=2){e++;c/=2}if(e+eBias>=eMax){m=0;e=eMax}else if(e+eBias>=1){m=(value*c-1)*Math.pow(2,mLen);e=e+eBias}else{m=value*Math.pow(2,eBias-1)*Math.pow(2,mLen);e=0}}for(;mLen>=8;buffer[offset+i]=m&255,i+=d,m/=256,mLen-=8){}e=e<0;buffer[offset+i]=e&255,i+=d,e/=256,eLen-=8){}buffer[offset+i-d]|=s*128}},{}],16:[function(require,module,exports){var toString={}.toString;module.exports=Array.isArray||function(arr){return toString.call(arr)=="[object Array]"}},{}],17:[function(require,module,exports){"use strict";var errorMessage;errorMessage="An argument without append, prepend, "+"or detach methods was given to `List";function List(){if(arguments.length){return List.from(arguments)}}var ListPrototype;ListPrototype=List.prototype;List.of=function(){return List.from.call(this,arguments)};List.from=function(items){var list=new this,length,iterator,item;if(items&&(length=items.length)){iterator=-1;while(++iterator0&&len>maxKeys){len=maxKeys}for(var i=0;i=0){kstr=x.substr(0,idx);vstr=x.substr(idx+1)}else{kstr=x;vstr=""}k=decodeURIComponent(kstr);v=decodeURIComponent(vstr);if(!hasOwnProperty(obj,k)){obj[k]=v}else if(isArray(obj[k])){obj[k].push(v)}else{obj[k]=[obj[k],v]}}return obj};var isArray=Array.isArray||function(xs){return Object.prototype.toString.call(xs)==="[object Array]"}},{}],20:[function(require,module,exports){"use strict";var stringifyPrimitive=function(v){switch(typeof v){case"string":return v;case"boolean":return v?"true":"false";case"number":return isFinite(v)?v:"";default:return""}};module.exports=function(obj,sep,eq,name){sep=sep||"&";eq=eq||"=";if(obj===null){obj=undefined}if(typeof obj==="object"){return map(objectKeys(obj),function(k){var ks=encodeURIComponent(stringifyPrimitive(k))+eq;if(isArray(obj[k])){return map(obj[k],function(v){return ks+encodeURIComponent(stringifyPrimitive(v))}).join(sep)}else{return ks+encodeURIComponent(stringifyPrimitive(obj[k]))}}).join(sep)}if(!name)return"";return encodeURIComponent(stringifyPrimitive(name))+eq+encodeURIComponent(stringifyPrimitive(obj))};var isArray=Array.isArray||function(xs){return Object.prototype.toString.call(xs)==="[object Array]"};function map(xs,f){if(xs.map)return xs.map(f);var res=[];for(var i=0;i>2];base64+=base64Chars[(bytes[i]&3)<<4|bytes[i+1]>>4] +;base64+=base64Chars[(bytes[i+1]&15)<<2|bytes[i+2]>>6];base64+=base64Chars[bytes[i+2]&63]}if(len%3===2){base64=base64.substring(0,base64.length-1)+"="}else if(len%3===1){base64=base64.substring(0,base64.length-2)+"=="}return base64};var binaryToBase64Replacer=function(key,value){if(global.ArrayBuffer&&value instanceof global.ArrayBuffer){return{base64:true,data:arrayBufferToBase64(value)}}else if(global.Buffer){if(value instanceof global.Buffer){return{base64:true,data:value.toString("base64")}}if(value&&value.type=="Buffer"&&value.data instanceof Array){var rehydratedBuffer;if(global.Buffer.from){rehydratedBuffer=global.Buffer.from(value.data)}else{rehydratedBuffer=new global.Buffer(value.data)}return{base64:true,data:rehydratedBuffer.toString("base64")}}}return value};module.exports.decode=function(input){if(input==null){return null}if(input=="#1"||input=="#2"){return input}var message=input.toString();try{return JSON.parse(message)}catch(err){}return message};module.exports.encode=function(object){if(object=="#1"||object=="#2"){return object}return JSON.stringify(object,binaryToBase64Replacer)}}).call(this,typeof global!=="undefined"?global:typeof self!=="undefined"?self:typeof window!=="undefined"?window:{})},{}]},{},[3])(3)}); \ No newline at end of file diff --git a/test/integration.js b/test/integration.js index 1397a75..0cee77b 100644 --- a/test/integration.js +++ b/test/integration.js @@ -20,11 +20,13 @@ var serverOptions = { var allowedUsers = { bob: true, + kate: true, alice: true }; var server, client; -var validSignedAuthToken; +var validSignedAuthTokenBob = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6ImJvYiIsImlhdCI6MTUwMjY0OTU2MiwiZXhwIjoxNTAyNzM1OTYyfQ.t9oprRhjDOBTHPx11hCkZLVcDqvqKDzmbUGh21VdYr0'; +var validSignedAuthTokenKate = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6ImthdGUiLCJpYXQiOjE1MDI2NTA1OTQsImV4cCI6MTUwMjczNjk5NH0.BJ16x_tf278uU9vkwVIfnydD78DgSBlupf8VuiefBhM'; var invalidSignedAuthToken = 'fakebGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.fakec2VybmFtZSI6ImJvYiIsImlhdCI6MTUwMjYyNTIxMywiZXhwIjoxNTAyNzExNjEzfQ.fakemYcOOjM9bzmS4UYRvlWSk_lm3WGHvclmFjLbyOk'; var connectionHandler = function (socket) { @@ -61,15 +63,7 @@ describe('integration tests', function () { }); server.once('ready', function () { - var tempClient = socketClusterClient.connect(clientOptions); - tempClient.once('authenticate', function (signedAuthToken) { - validSignedAuthToken = signedAuthToken; - tempClient.once('disconnect', function () { - done(); - }); - tempClient.destroy(); - }); - tempClient.emit('login', {username: 'bob'}); + done(); }); }); @@ -80,10 +74,13 @@ describe('integration tests', function () { afterEach('shut down client after each test', function (done) { global.localStorage.removeItem('socketCluster.authToken'); - if (client) { + if (client && client.state != client.CLOSED) { client.once('disconnect', function () { done(); }); + client.once('connectAbort', function () { + done(); + }); client.destroy(); } else { done(); @@ -101,7 +98,7 @@ describe('integration tests', function () { }); it('should be authenticated on connect if previous JWT token is present', function (done) { - global.localStorage.setItem('socketCluster.authToken', validSignedAuthToken); + global.localStorage.setItem('socketCluster.authToken', validSignedAuthTokenBob); client = socketClusterClient.connect(clientOptions); client.once('connect', function (statusA) { assert.equal(client.authState, 'authenticated'); @@ -112,19 +109,33 @@ describe('integration tests', function () { }); it('should send back error if JWT is invalid during handshake', function (done) { + global.localStorage.setItem('socketCluster.authToken', validSignedAuthTokenBob); client = socketClusterClient.connect(clientOptions); - client.emit('login', {username: 'bob'}); + client.once('connect', function (statusA) { + assert.notEqual(statusA, null); + assert.equal(statusA.isAuthenticated, true); + assert.equal(statusA.authError, null); + + assert.notEqual(client.signedAuthToken, null); + assert.notEqual(client.authToken, null); + // Change the setAuthKey to invalidate the current token. client.emit('setAuthKey', 'differentAuthKey', function (err) { - assert.equal(err == null, true); + assert.equal(err, null); client.once('disconnect', function () { client.once('connect', function (statusB) { - assert.equal(statusB.isAuthenticated, false); assert.notEqual(statusB.authError, null); assert.equal(statusB.authError.name, 'AuthTokenInvalidError'); + + // When authentication fails, the auth token properties on the client + // socket should be set to null; that way it's not going to keep + // throwing the same error every time the socket tries to connect. + assert.equal(client.signedAuthToken, null); + assert.equal(client.authToken, null); + // Set authKey back to what it was. client.emit('setAuthKey', serverOptions.authKey, function (err) { assert.equal(err == null, true); @@ -251,12 +262,69 @@ describe('integration tests', function () { }, 1000); }); - it('after disconnecting while being authenticated, socket should be put in pending authState and then switch to authenticated state after invoking authenticate method with a valid signed auth token', function (done) { + it('should deal with auth engine errors related to saveToken function', function (done) { + global.localStorage.setItem('socketCluster.authToken', validSignedAuthTokenBob); + client = socketClusterClient.connect(clientOptions); + + var caughtError; + client.on('error', function (err) { + caughtError = err; + }); + + client.once('connect', function () { + var oldSaveTokenFunction = client.auth.saveToken; + client.auth.saveToken = function (tokenName, tokenValue, options, callback) { + var err = new Error('Failed to save token'); + err.name = 'FailedToSaveTokenError'; + callback(err); + }; + assert.notEqual(client.authToken, null); + assert.equal(client.authToken.username, 'bob'); + + client.authenticate(validSignedAuthTokenKate, function (err, authStatus) { + assert.notEqual(authStatus, null); + // The error here comes from the auth engine and does not prevent the + // authentication from taking place, it only prevents the token from being + // stored correctly on the client. + assert.equal(authStatus.isAuthenticated, true); + // authError should be null because the error comes from the client-side auth engine + // whereas authError is for server-side errors (e.g. JWT errors). + assert.equal(authStatus.authError, null); + + assert.notEqual(client.authToken, null); + assert.equal(client.authToken.username, 'kate'); + setTimeout(function () { + assert.notEqual(caughtError, null); + assert.equal(caughtError.name, 'FailedToSaveTokenError'); + client.auth.saveToken = oldSaveTokenFunction; + done(); + }, 10); + }); + }); + }); + + it('should gracefully handle authenticate abortion due to disconnection', function (done) { + client = socketClusterClient.connect(clientOptions); + + client.once('connect', function (statusA) { + client.authenticate(validSignedAuthTokenBob, function (err, authStatus) { + assert.notEqual(err, null); + assert.equal(err.name, 'BadConnectionError'); + assert.notEqual(authStatus, null); + assert.notEqual(authStatus.isAuthenticated, null); + // authError should be null because the error which occurred is not related + // specifically to authentication. + assert.equal(authStatus.authError, null); + done(); + }); + client.disconnect(); + }); + }); + + it('should go through the correct sequence of authentication state changes when dealing with disconnections; part 1', function (done) { client = socketClusterClient.connect(clientOptions); var expectedAuthStateChanges = [ - 'unauthenticated->authenticated', - 'authenticated->unauthenticated', 'unauthenticated->authenticated' ]; var authStateChanges = []; @@ -264,22 +332,24 @@ describe('integration tests', function () { authStateChanges.push(status.oldState + '->' + status.newState); }); + assert.equal(client.authState, 'unauthenticated'); + client.once('connect', function (statusA) { client.once('authenticate', function (newSignedToken) { client.once('disconnect', function () { - client.once('authenticate', function (newSignedToken) { + assert.equal(client.authState, 'authenticated'); + client.authenticate(newSignedToken, function (newSignedToken) { assert.equal(client.authState, 'authenticated'); assert.equal(JSON.stringify(authStateChanges), JSON.stringify(expectedAuthStateChanges)); client.off('authStateChange'); done(); }); - assert.equal(client.authState, 'unauthenticated'); - client.authenticate(newSignedToken); - assert.equal(client.authState, 'unauthenticated'); + assert.equal(client.authState, 'authenticated'); }); assert.equal(client.authState, 'authenticated'); client.disconnect(); - assert.equal(client.authState, 'unauthenticated'); + // In case of disconnection, the socket maintains the last known auth state. + assert.equal(client.authState, 'authenticated'); }); assert.equal(client.authState, 'unauthenticated'); client.emit('login', {username: 'bob'}); @@ -287,6 +357,109 @@ describe('integration tests', function () { }); }); + it('should go through the correct sequence of authentication state changes when dealing with disconnections; part 2', function (done) { + global.localStorage.setItem('socketCluster.authToken', validSignedAuthTokenBob); + client = socketClusterClient.connect(clientOptions); + + var expectedAuthStateChanges = [ + 'unauthenticated->authenticated', + 'authenticated->unauthenticated', + 'unauthenticated->authenticated', + 'authenticated->unauthenticated' + ]; + var authStateChanges = []; + client.on('authStateChange', function (status) { + authStateChanges.push(status.oldState + '->' + status.newState); + }); + + assert.equal(client.authState, 'unauthenticated'); + + client.once('connect', function (statusA) { + assert.equal(client.authState, 'authenticated'); + client.deauthenticate(); + assert.equal(client.authState, 'unauthenticated'); + client.authenticate(validSignedAuthTokenBob, function (err) { + assert.equal(err, null); + assert.equal(client.authState, 'authenticated'); + client.once('disconnect', function () { + assert.equal(client.authState, 'authenticated'); + client.deauthenticate(); + assert.equal(client.authState, 'unauthenticated'); + assert.equal(JSON.stringify(authStateChanges), JSON.stringify(expectedAuthStateChanges)); + done(); + }); + client.disconnect(); + }); + assert.equal(client.authState, 'unauthenticated'); + }); + }); + + it('should go through the correct sequence of authentication state changes when dealing with disconnections; part 3', function (done) { + global.localStorage.setItem('socketCluster.authToken', validSignedAuthTokenBob); + client = socketClusterClient.connect(clientOptions); + + var expectedAuthStateChanges = [ + 'unauthenticated->authenticated', + 'authenticated->unauthenticated' + ]; + var authStateChanges = []; + client.on('authStateChange', function (status) { + authStateChanges.push(status.oldState + '->' + status.newState); + }); + + assert.equal(client.authState, 'unauthenticated'); + + client.once('connect', function (statusA) { + assert.equal(client.authState, 'authenticated'); + client.authenticate(invalidSignedAuthToken, function (err) { + assert.notEqual(err, null); + assert.equal(err.name, 'AuthTokenInvalidError'); + assert.equal(client.authState, 'unauthenticated'); + assert.equal(JSON.stringify(authStateChanges), JSON.stringify(expectedAuthStateChanges)); + done(); + }); + assert.equal(client.authState, 'authenticated'); + }); + }); + + it('should go through the correct sequence of authentication state changes when authenticating as a user while already authenticated as another user', function (done) { + global.localStorage.setItem('socketCluster.authToken', validSignedAuthTokenBob); + client = socketClusterClient.connect(clientOptions); + + var expectedAuthStateChanges = [ + 'unauthenticated->authenticated' + ]; + var authStateChanges = []; + client.on('authStateChange', function (status) { + authStateChanges.push(status.oldState + '->' + status.newState); + }); + + var expectedAuthTokenChanges = [ + validSignedAuthTokenBob, + validSignedAuthTokenKate + ]; + var authTokenChanges = []; + client.on('authTokenChange', function (newSignedAuthToken) { + authTokenChanges.push(newSignedAuthToken); + }); + + assert.equal(client.authState, 'unauthenticated'); + + client.once('connect', function (statusA) { + assert.equal(client.authState, 'authenticated'); + assert.equal(client.authToken.username, 'bob'); + client.authenticate(validSignedAuthTokenKate, function (err) { + assert.equal(err, null); + assert.equal(client.authState, 'authenticated'); + assert.equal(client.authToken.username, 'kate'); + assert.equal(JSON.stringify(authStateChanges), JSON.stringify(expectedAuthStateChanges)); + assert.equal(JSON.stringify(authTokenChanges), JSON.stringify(expectedAuthTokenChanges)); + done(); + }); + assert.equal(client.authState, 'authenticated'); + }); + }); + it('should wait for socket to be authenticated before subscribing to waitForAuth channel', function (done) { client = socketClusterClient.connect(clientOptions); var privateChannel = client.subscribe('priv', {waitForAuth: true}); @@ -302,7 +475,7 @@ describe('integration tests', function () { assert.equal(privateChannel.state, 'subscribed'); done(); }); - client.authenticate(validSignedAuthToken); + client.authenticate(validSignedAuthTokenBob); }); client.disconnect(); }); @@ -311,7 +484,7 @@ describe('integration tests', function () { }); it('subscriptions (including those with waitForAuth option) should have priority over the authenticate action', function (done) { - global.localStorage.setItem('socketCluster.authToken', validSignedAuthToken); + global.localStorage.setItem('socketCluster.authToken', validSignedAuthTokenBob); client = socketClusterClient.connect(clientOptions); var expectedAuthStateChanges = [ @@ -360,6 +533,5 @@ describe('integration tests', function () { done(); }, 1000); }); - }); });