text: "use strict";var Emitter=require("events").EventEmitter,util=require("util"),streams=require("../streams"),Headers=require("./headers"),Reader=require("./stream_reader"),Base=function(e,n,t){Emitter.call(this),Base.validateOptions(t||{},["maxLength","masking","requireMasking","protocols"]),this._request=e,this._reader=new Reader,this._options=t||{},this._maxLength=this._options.maxLength||this.MAX_LENGTH,this._headers=new Headers,this.__queue=[],this.readyState=0,this.url=n,this.io=new streams.IO(this),this.messages=new streams.Messages(this),this._bindEventListeners()};util.inherits(Base,Emitter),Base.validateOptions=function(e,n){for(var t in e)if(n.indexOf(t)<0)throw new Error("Unrecognized option: "+t)};var instance={MAX_LENGTH:67108863,STATES:["connecting","open","closing","closed"],_bindEventListeners:function(){var e=this;this.messages.on("error",function(){}),this.on("message",function(n){var t=e.messages;t.readable&&t.emit("data",n.data)}),this.on("error",function(n){var t=e.messages;t.readable&&t.emit("error",n)}),this.on("close",function(){var n=e.messages;n.readable&&(n.readable=n.writable=!1,n.emit("end"))})},getState:function(){return this.STATES[this.readyState]||null},addExtension:function(e){return!1},setHeader:function(e,n){return this.readyState>0?!1:(this._headers.set(e,n),!0)},start:function(){if(0!==this.readyState)return!1;var e=this._handshakeResponse();return e?(this._write(e),-1!==this._stage&&this._open(),!0):!1},text:function(e){return this.frame(e)},binary:function(e){return!1},ping:function(){return!1},pong:function(){return!1},close:function(e,n){return 1!==this.readyState?!1:(this.readyState=3,this.emit("close",new Base.CloseEvent(null,null)),!0)},_open:function(){this.readyState=1,this.__queue.forEach(function(e){this.frame.apply(this,e)},this),this.__queue=[],this.emit("open",new Base.OpenEvent)},_queue:function(e){return this.__queue.push(e),!0},_write:function(e){var n=this.io;n.readable&&n.emit("data",e)}};for(var key in instance)Base.prototype[key]=instance[key];Base.ConnectEvent=function(){},Base.OpenEvent=function(){},Base.CloseEvent=function(e,n){this.code=e,this.reason=n},Base.MessageEvent=function(e){this.data=e},module.exports=Base;