UNPKG

myojs

Version:

JavaScript/ES2015/ES6 client for the Thalmic Labs Myo

25 lines 72.6 kB
!function(e,t){if("object"==typeof exports&&"object"==typeof module)module.exports=t(require("util"),require("events"),require("crypto"),require("http"),require("url"),require("fs"),require("https"),require("stream"),require("tls"),require("zlib"));else if("function"==typeof define&&define.amd)define(["util","events","crypto","http","url","fs","https","stream","tls","zlib"],t);else{var n="object"==typeof exports?t(require("util"),require("events"),require("crypto"),require("http"),require("url"),require("fs"),require("https"),require("stream"),require("tls"),require("zlib")):t(e.util,e.events,e.crypto,e.http,e.url,e.fs,e.https,e.stream,e.tls,e.zlib);for(var r in n)("object"==typeof exports?exports:e)[r]=n[r]}}(this,function(e,t,n,r,i,o,s,a,c,u){return function(e){function t(r){if(n[r])return n[r].exports;var i=n[r]={exports:{},id:r,loaded:!1};return e[r].call(i.exports,i,i.exports,t),i.loaded=!0,i.exports}var n={};return t.m=e,t.c=n,t.p="",t(0)}([function(e,t,n){"use strict";function r(e){return e&&e.__esModule?e:{default:e}}function i(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(t,"__esModule",{value:!0});var o=function(){function e(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}return function(t,n,r){return n&&e(t.prototype,n),r&&e(t,r),t}}(),s=n(10),a=r(s),c=n(21),u=r(c),f=n(6),l=r(f),h=n(4),d=r(h),p=n(7),y=r(p),v=n(8),m=r(v),_=n(9),g=r(_),w=n(5),b=r(w),x=function(){function e(){i(this,e)}return o(e,null,[{key:"BaseConnection",value:function(){return a.default}},{key:"CircularBuffer",value:function(){return d.default}},{key:"Hub",value:function(){return new u.default}},{key:"Myo",value:function(){return l.default}},{key:"Frame",value:function(){return b.default}},{key:"Pose",value:function(){return y.default}},{key:"Quaternion",value:function(){return m.default}},{key:"Vector3",value:function(){return g.default}}]),e}();t.default=x,global.Myo=x},function(e,t){e.exports=require("util")},function(e,t){e.exports=require("events")},function(e,t,n){function r(e,t,n){if(this instanceof r==!1)throw new TypeError("Classes can't be function-called");this._options=e||{},this._isServer=!!t,this._inflate=null,this._deflate=null,this.params=null,this._maxPayload=n||0}var i=n(35),o=[8,9,10,11,12,13,14,15],s=15,a=8;r.extensionName="permessage-deflate",r.prototype.offer=function(){var e={};return this._options.serverNoContextTakeover&&(e.server_no_context_takeover=!0),this._options.clientNoContextTakeover&&(e.client_no_context_takeover=!0),this._options.serverMaxWindowBits&&(e.server_max_window_bits=this._options.serverMaxWindowBits),this._options.clientMaxWindowBits?e.client_max_window_bits=this._options.clientMaxWindowBits:null==this._options.clientMaxWindowBits&&(e.client_max_window_bits=!0),e},r.prototype.accept=function(e){e=this.normalizeParams(e);var t;return t=this._isServer?this.acceptAsServer(e):this.acceptAsClient(e),this.params=t,t},r.prototype.cleanup=function(){this._inflate&&(this._inflate.writeInProgress?this._inflate.pendingClose=!0:(this._inflate.close&&this._inflate.close(),this._inflate=null)),this._deflate&&(this._deflate.writeInProgress?this._deflate.pendingClose=!0:(this._deflate.close&&this._deflate.close(),this._deflate=null))},r.prototype.acceptAsServer=function(e){var t={},n=e.some(function(e){if(t={},(this._options.serverNoContextTakeover!==!1||!e.server_no_context_takeover)&&(this._options.serverMaxWindowBits!==!1||!e.server_max_window_bits)&&!("number"==typeof this._options.serverMaxWindowBits&&"number"==typeof e.server_max_window_bits&&this._options.serverMaxWindowBits>e.server_max_window_bits)&&("number"!=typeof this._options.clientMaxWindowBits||e.client_max_window_bits))return(this._options.serverNoContextTakeover||e.server_no_context_takeover)&&(t.server_no_context_takeover=!0),this._options.clientNoContextTakeover&&(t.client_no_context_takeover=!0),this._options.clientNoContextTakeover!==!1&&e.client_no_context_takeover&&(t.client_no_context_takeover=!0),"number"==typeof this._options.serverMaxWindowBits?t.server_max_window_bits=this._options.serverMaxWindowBits:"number"==typeof e.server_max_window_bits&&(t.server_max_window_bits=e.server_max_window_bits),"number"==typeof this._options.clientMaxWindowBits?t.client_max_window_bits=this._options.clientMaxWindowBits:this._options.clientMaxWindowBits!==!1&&"number"==typeof e.client_max_window_bits&&(t.client_max_window_bits=e.client_max_window_bits),!0},this);if(!n)throw new Error("Doesn't support the offered configuration");return t},r.prototype.acceptAsClient=function(e){var t=e[0];if(null!=this._options.clientNoContextTakeover&&this._options.clientNoContextTakeover===!1&&t.client_no_context_takeover)throw new Error('Invalid value for "client_no_context_takeover"');if(null!=this._options.clientMaxWindowBits){if(this._options.clientMaxWindowBits===!1&&t.client_max_window_bits)throw new Error('Invalid value for "client_max_window_bits"');if("number"==typeof this._options.clientMaxWindowBits&&(!t.client_max_window_bits||t.client_max_window_bits>this._options.clientMaxWindowBits))throw new Error('Invalid value for "client_max_window_bits"')}return t},r.prototype.normalizeParams=function(e){return e.map(function(e){return Object.keys(e).forEach(function(t){var n=e[t];if(n.length>1)throw new Error("Multiple extension parameters for "+t);switch(n=n[0],t){case"server_no_context_takeover":case"client_no_context_takeover":if(n!==!0)throw new Error("invalid extension parameter value for "+t+" ("+n+")");e[t]=!0;break;case"server_max_window_bits":case"client_max_window_bits":if("string"==typeof n&&(n=parseInt(n,10),!~o.indexOf(n)))throw new Error("invalid extension parameter value for "+t+" ("+n+")");if(!this._isServer&&n===!0)throw new Error("Missing extension parameter value for "+t);e[t]=n;break;default:throw new Error("Not defined extension parameter ("+t+")")}},this),e},this)},r.prototype.decompress=function(e,t,n){function r(e){a(),n(e)}function o(e){if(void 0!==f._maxPayload&&null!==f._maxPayload&&f._maxPayload>0&&(h+=e.length,h>f._maxPayload)){l=[],a();var t={type:1009};return void n(t)}l.push(e)}function a(){f._inflate&&(f._inflate.removeListener("error",r),f._inflate.removeListener("data",o),f._inflate.writeInProgress=!1,(t&&f.params[c+"_no_context_takeover"]||f._inflate.pendingClose)&&(f._inflate.close&&f._inflate.close(),f._inflate=null))}var c=this._isServer?"client":"server";if(!this._inflate){var u=this.params[c+"_max_window_bits"];this._inflate=i.createInflateRaw({windowBits:"number"==typeof u?u:s})}this._inflate.writeInProgress=!0;var f=this,l=[],h=0;this._inflate.on("error",r).on("data",o),this._inflate.write(e),t&&this._inflate.write(new Buffer([0,0,255,255])),this._inflate.flush(function(){a(),n(null,Buffer.concat(l))})},r.prototype.compress=function(e,t,n){function r(e){c(),n(e)}function o(e){h.push(e)}function c(){l._deflate&&(l._deflate.removeListener("error",r),l._deflate.removeListener("data",o),l._deflate.writeInProgress=!1,(t&&l.params[u+"_no_context_takeover"]||l._deflate.pendingClose)&&(l._deflate.close&&l._deflate.close(),l._deflate=null))}var u=this._isServer?"server":"client";if(!this._deflate){var f=this.params[u+"_max_window_bits"];this._deflate=i.createDeflateRaw({flush:i.Z_SYNC_FLUSH,windowBits:"number"==typeof f?f:s,memLevel:this._options.memLevel||a})}this._deflate.writeInProgress=!0;var l=this,h=[];this._deflate.on("error",r).on("data",o),this._deflate.write(e),this._deflate.flush(function(){c();var e=Buffer.concat(h);t&&(e=e.slice(0,e.length-4)),n(null,e)})},e.exports=r},function(e,t){"use strict";function n(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(t,"__esModule",{value:!0});var r=function(){function e(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}return function(t,n,r){return n&&e(t.prototype,n),r&&e(t,r),t}}(),i=function(){function e(t){n(this,e),this.pos=0,this._buf=[],this.size=t}return r(e,[{key:"get",value:function(e){return e&&null!==e||(e=0),e>=this.size?null:e>=this._buf.length?null:this._buf[(this.pos-e-1)%this.size]}},{key:"push",value:function(e){return this._buf[this.pos%this.size]=e,this.pos++}}]),e}();t.default=i},function(e,t,n){"use strict";function r(e){return e&&e.__esModule?e:{default:e}}function i(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(t,"__esModule",{value:!0});var o="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},s=function(){function e(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}return function(t,n,r){return n&&e(t.prototype,n),r&&e(t,r),t}}(),a=n(7),c=r(a),u=n(8),f=r(u),l=n(9),h=r(l),d=function(){function e(t){if(i(this,e),!t)throw new Error("Missing constructor arguments");if("object"!==("undefined"==typeof t?"undefined":o(t)))throw new Error("Constructor parameter needs to be an object");if(!t.hasOwnProperty("id")||t.id!==parseInt(t.id,10))throw new Error("Frame id needs to be of type integer");if(!t.hasOwnProperty("timestamp")||"string"!=typeof t.timestamp)throw new Error("Timestamp needs to be of type string");this.id=t.id,this.timestamp=t.timestamp,t.euler&&(this.euler=t.euler),t.rssi&&(this.rssi=t.rssi),t.event&&(this.event=t.event),t.pose?this.pose=new c.default(t.pose):this.pose=c.default.invalid(),t.rotation?this.rotation=new f.default(t.rotation):this.rotation=f.default.invalid(),t.accel?this.accel=new h.default(t.accel):this.accel=h.default.invalid(),t.gyro?this.gyro=new h.default(t.gyro):this.gyro=h.default.invalid(),t.emg?this.emg=t.emg:this.emg=[],this.data=t,this.type="frame"}return s(e,[{key:"toString",value:function(){return"[Frame id:"+this.id+" timestamp:"+this.timestamp+" accel:"+this.accel.toString()+"]"}}]),e}();t.default=d},function(e,t){"use strict";function n(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(t,"__esModule",{value:!0});var r=function(){function e(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}return function(t,n,r){return n&&e(t.prototype,n),r&&e(t,r),t}}(),i=function(){function e(t){if(n(this,e),!t)throw new Error("Missing context");this.context=t}return r(e,[{key:"requestRssi",value:function(){this.context.send({requestRssi:!0})}},{key:"vibrate",value:function(t){switch(t){case e.VIBRATION_SHORT:this.context.send({command:"vibrate",args:[e.VIBRATION_SHORT]});break;case e.VIBRATION_MEDIUM:this.context.send({command:"vibrate",args:[e.VIBRATION_MEDIUM]});break;case e.VIBRATION_LONG:this.context.send({command:"vibrate",args:[e.VIBRATION_LONG]});break;default:throw new Error("Valid values are: Myo.VIBRATION_SHORT, Myo.VIBRATION_MEDIUM, Myo.VIBRATION_LONG")}}},{key:"unlock",value:function(t){switch(t){case e.UNLOCK_TIMED:this.context.send({command:"unlock",args:[e.UNLOCK_TIMED]});break;case e.UNLOCK_HOLD:this.context.send({command:"unlock",args:[e.UNLOCK_HOLD]});break;default:throw new Error("Valid values are: Myo.UNLOCK_TIMED, Myo.UNLOCK_HOLD")}}},{key:"lock",value:function(){this.context.send({command:"lock"})}},{key:"notifyUserAction",value:function(t){switch(t){case e.USER_ACTION_SINGLE:this.context.send({command:"notifyUserAction",args:[e.USER_ACTION_SINGLE]});break;default:throw new Error("Valid values are: Myo.USER_ACTION_SINGLE")}}}]),e}();t.default=i,i.VIBRATION_SHORT=0,i.VIBRATION_MEDIUM=1,i.VIBRATION_LONG=2,i.UNLOCK_TIMED=0,i.UNLOCK_HOLD=1,i.USER_ACTION_SINGLE=0},function(e,t){"use strict";function n(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(t,"__esModule",{value:!0});var r="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},i=function(){function e(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}return function(t,n,r){return n&&e(t.prototype,n),r&&e(t,r),t}}(),o=function(){function e(t){if(n(this,e),"object"!==("undefined"==typeof t?"undefined":r(t))||"[object Array]"===Object.prototype.toString.call(t))throw new Error("Constructor parameter needs to be an object");if(this.type=t.type,this.valid=!t.hasOwnProperty("invalid"),this.valid&&(!t.hasOwnProperty("type")||t.type!==parseInt(t.type,10)))throw new Error("Pose type needs to be of type integer")}return i(e,[{key:"isEqualTo",value:function(e){return this.type===e.type}},{key:"toString",value:function(){if(!this.valid)return"[Pose invalid]";switch(this.type){case e.POSE_REST:return"[Pose type:"+this.type.toString()+" POSE_REST]";case e.POSE_FIST:return"[Pose type:"+this.type.toString()+" POSE_FIST]";case e.POSE_WAVE_IN:return"[Pose type:"+this.type.toString()+" POSE_WAVE_IN]";case e.POSE_WAVE_OUT:return"[Pose type:"+this.type.toString()+" POSE_WAVE_OUT]";case e.POSE_FINGERS_SPREAD:return"[Pose type:"+this.type.toString()+" POSE_FINGERS_SPREAD]";case e.POSE_DOUBLE_TAP:return"[Pose type:"+this.type.toString()+" POSE_DOUBLE_TAP]";default:return"[Pose type:"+this.type.toString()+"]"}}}],[{key:"invalid",value:function(){return new e({invalid:!0})}}]),e}();t.default=o,o.POSE_REST=0,o.POSE_FIST=1,o.POSE_WAVE_IN=2,o.POSE_WAVE_OUT=3,o.POSE_FINGERS_SPREAD=4,o.POSE_DOUBLE_TAP=5},function(e,t){"use strict";function n(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(t,"__esModule",{value:!0});var r=function(){function e(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}return function(t,n,r){return n&&e(t.prototype,n),r&&e(t,r),t}}(),i=function(){function e(t){if(n(this,e),this.valid=!t.hasOwnProperty("invalid"),this.valid){if("[object Array]"!==Object.prototype.toString.call(t))throw new Error("Components needs to be an array");if(isNaN(t[0])||isNaN(t[1])||isNaN(t[2])||isNaN(t[3]))throw new Error("Component values needs to be integers or numbers");this.x=t[0],this.y=t[1],this.z=t[2],this.w=t[3]}else this.x=NaN,this.y=NaN,this.z=NaN,this.w=NaN}return r(e,[{key:"normalized",value:function(){var t=Math.sqrt(this.x*this.x+this.y*this.y+this.z*this.z+this.w*this.w);return new e([this.x/t,this.y/t,this.z/t,this.w/t])}},{key:"conjugate",value:function(){return new e([-this.x,-this.y,-this.z,this.w])}},{key:"toEuler",value:function(){var e=0,t=0,n=0,r=0,i=0,o=0,s=0,a=0,c=0;return a=this.w*this.w,i=this.x*this.x,o=this.y*this.y,s=this.z*this.z,c=i+o+s+a,e=this.x*this.y+this.z*this.w,e>.499*c?(t=2*Math.atan2(this.x,this.w),n=Math.PI/2,r=0):e<-.499*c?(t=-2*Math.atan2(this.x,this.w),n=-Math.PI/2,r=0):(t=Math.atan2(2*this.y*this.w-2*this.x*this.z,i-o-s+a),n=Math.asin(2*e/c),r=Math.atan2(2*this.x*this.w-2*this.y*this.z,-i+o-s+a)),{heading:t,attitude:n,bank:r}}},{key:"roll",value:function(){return Math.atan2(2*this.y*this.w-2*this.x*this.z,1-2*this.y*this.y-2*this.z*this.z)}},{key:"pitch",value:function(){return Math.atan2(2*this.x*this.w-2*this.y*this.z,1-2*this.x*this.x-2*this.z*this.z)}},{key:"yaw",value:function(){return Math.asin(2*this.x*this.y+2*this.z*this.w)}},{key:"toString",value:function(){return this.valid?"[Quaternion x:"+this.x+" y:"+this.y+" z:"+this.z+" w:"+this.w+"]":"[Quaternion invalid]"}}],[{key:"invalid",value:function(){return new e({invalid:!0})}}]),e}();t.default=i},function(e,t){"use strict";function n(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(t,"__esModule",{value:!0});var r="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},i=function(){function e(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}return function(t,n,r){return n&&e(t.prototype,n),r&&e(t,r),t}}(),o=function(){function e(t){if(n(this,e),!t)throw new Error("Missing constructor arguments");if("object"!==("undefined"==typeof t?"undefined":r(t)))throw new Error("Constructor parameter needs to be an object");if(this.valid=!t.hasOwnProperty("invalid"),this.valid){if(!t||"[object Array]"!==Object.prototype.toString.call(t))throw new Error("Components needs to be an array");if(isNaN(t[0])||isNaN(t[1])||isNaN(t[2]))throw new Error("Component values needs to be integers or numbers");this.x=t[0],this.y=t[1],this.z=t[2]}else this.x=NaN,this.y=NaN,this.z=NaN}return i(e,[{key:"opposite",value:function(){return new e([-this.x,-this.y,-this.z])}},{key:"plus",value:function(t){return new e([this.x+t.x,this.y+t.y,this.z+t.z])}},{key:"plusAssign",value:function(e){return this.x+=e.x,this.y+=e.y,this.z+=e.z,this}},{key:"minus",value:function(t){return new e([this.x-t.x,this.y-t.y,this.z-t.z])}},{key:"minusAssign",value:function(e){return this.x-=e.x,this.y-=e.y,this.z-=e.z,this}},{key:"multiply",value:function(t){return new e([this.x*t,this.y*t,this.z*t])}},{key:"multiplyAssign",value:function(e){return this.x*=e,this.y*=e,this.z*=e,this}},{key:"divide",value:function(t){return new e([this.x/t,this.y/t,this.z/t])}},{key:"divideAssign",value:function(e){return this.x/=e,this.y/=e,this.z/=e,this}},{key:"isEqualTo",value:function(e){return!(this.x!==e.x||this.y!==e.y||this.z!==e.z)}},{key:"angleTo",value:function(e){var t=this.magnitudeSquared()*e.magnitudeSquared(),n=0;return n=t<=0?0:Math.acos(this.dot(e)/Math.sqrt(t))}},{key:"cross",value:function(t){return new e([this.y*t.z-this.z*t.y,this.z*t.x-this.x*t.z,this.x*t.y-this.y*t.x])}},{key:"distanceTo",value:function(e){return Math.sqrt((this.x-e.x)*(this.x-e.x)+(this.y-e.y)*(this.y-e.y)+(this.z-e.z)*(this.z-e.z))}},{key:"dot",value:function(e){return this.x*e.x+this.y*e.y+this.z*e.z}},{key:"isValid",value:function(){return this.x<=Number.MAX_VALUE&&this.x>=-Number.MAX_VALUE&&this.y<=Number.MAX_VALUE&&this.y>=-Number.MAX_VALUE&&this.z<=Number.MAX_VALUE&&this.z>=-Number.MAX_VALUE}},{key:"magnitude",value:function(){return Math.sqrt(this.x*this.x+this.y*this.y+this.z*this.z)}},{key:"magnitudeSquared",value:function(){return this.x*this.x+this.y*this.y+this.z*this.z}},{key:"normalized",value:function(){var t=this.magnitudeSquared();return t<=0?new e([0,0,0]):(t=1/Math.sqrt(t),new e([this.x*t,this.y*t,this.z*t]))}},{key:"pitch",value:function(){return Math.atan2(this.y,-this.z)}},{key:"yaw",value:function(){return Math.atan2(this.x,-this.z)}},{key:"roll",value:function(){return Math.atan2(this.x,-this.y)}},{key:"toString",value:function(){return this.valid?"[Vector3 x:"+this.x+" y:"+this.y+" z:"+this.z+"]":"[Vector3 invalid]"}}],[{key:"zero",value:function(){return new e([0,0,0])}},{key:"xAxis",value:function(){return new e([1,0,0])}},{key:"yAxis",value:function(){return new e([0,1,0])}},{key:"zAxis",value:function(){return new e([0,0,1])}},{key:"left",value:function(){return new e([-1,0,0])}},{key:"right",value:function(){return e.xAxis()}},{key:"down",value:function(){return new e([0,-1,0])}},{key:"up",value:function(){return e.yAxis()}},{key:"forward",value:function(){return new e([0,0,-1])}},{key:"backward",value:function(){return e.zAxis()}},{key:"invalid",value:function(){return new e({invalid:!0})}}]),e}();t.default=o},function(e,t,n){"use strict";function r(e){return e&&e.__esModule?e:{default:e}}function i(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function o(e,t){if(!e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!t||"object"!=typeof t&&"function"!=typeof t?e:t}function s(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function, not "+typeof t);e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),t&&(Object.setPrototypeOf?Object.setPrototypeOf(e,t):e.__proto__=t)}Object.defineProperty(t,"__esModule",{value:!0});var a="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},c=function(){function e(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}return function(t,n,r){return n&&e(t.prototype,n),r&&e(t,r),t}}(),u=n(2),f=n(5),l=r(f),h=function(e){function t(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},n=e.host,r=void 0===n?"127.0.0.1":n,s=e.port,a=void 0===s?6450:s;i(this,t);var c=o(this,(t.__proto__||Object.getPrototypeOf(t)).call(this));if("string"!=typeof r)throw new Error("Host needs to be of type string");if(a!==parseInt(a,10))throw new Error("Port needs to be of type integer");return c.host=r,c.port=a,c.connected=!1,c}return s(t,e),c(t,[{key:"getUrl",value:function(){return"ws://"+this.host+":"+this.port+"/"}},{key:"handleOpen",value:function(){var e="";return this.connected?e="connected":(this.send({command:"requestDeviceInfo"}),e="connecting"),e}},{key:"handleClose",value:function(){var e="disconnected";return this.connected&&(this.disconnect(),this.startReconnection(),e="disconnecting"),e}},{key:"startReconnection",value:function(){var e=this,t="";return this.reconnectionTimer?t="already reconnecting":(this.reconnectionTimer=setInterval(function(){e.reconnect()},500),t="reconnecting"),t}},{key:"stopReconnection",value:function(){this.reconnectionTimer=clearInterval(this.reconnectionTimer)}},{key:"disconnect",value:function(e){e||this.stopReconnection(),this.socket&&(this.socket.close(),delete this.socket,this.connected&&(this.connected=!1,this.emit("disconnect")))}},{key:"reconnect",value:function(){var e="";return this.connected?(this.stopReconnection(),e="stopReconnection"):(this.disconnect(!0),this.connect(),e="connect"),e}},{key:"handleData",value:function(e){var t={};if(!e)throw new Error("No data received");try{t=JSON.parse(e)}catch(e){throw new Error("Invalid JSON")}if(!this.connected&&t.hasOwnProperty("frame")){var n=t.frame;if(n.hasOwnProperty("deviceInfo")){var r=n.deviceInfo;return this.emit("deviceInfo",r),this.connected=!0,void this.emit("connect")}}if(this.connected&&t.hasOwnProperty("frame")){var i=new l.default(t.frame);this.emit(i.type,i),i.pose&&this.emit("pose",i.pose),i.event&&this.emit("event",i.event)}}},{key:"connect",value:function(){var e=this,t="undefined"!=typeof window;if(this.socket)return"socket already created";if(this.emit("ready"),t)this.socket=new WebSocket(this.getUrl());else{var r=n(23);this.socket=new r(this.getUrl())}return this.socket.onopen=function(){e.handleOpen()},this.socket.onclose=function(t){e.handleClose(t.code,t.reason)},this.socket.onmessage=function(t){e.handleData(t.data)},this.socket.onerror=function(t){e.handleClose("connectError",t.data)},!0}},{key:"send",value:function(e){if("object"!==("undefined"==typeof e?"undefined":a(e))||"string"==typeof e)throw new Error("Parameter needs to be an object");this.socket.send(JSON.stringify(e))}}]),t}(u.EventEmitter);t.default=h},function(e,t,n){function r(e){var t={},n=this.value={};Object.keys(e).forEach(function(r){t[r]=e[r],Object.defineProperty(n,r,{get:function(){return t[r]},configurable:!1,enumerable:!0})}),this.reset=function(){return Object.keys(e).forEach(function(n){t[n]=e[n]}),this},this.merge=function(e,n){if(e=e||{},"[object Array]"===Object.prototype.toString.call(n)){for(var r=[],i=0,o=n.length;i<o;++i){var s=n[i];s in e||r.push(s)}if(r.length>0)throw r.length>1?new Error("options "+r.slice(0,r.length-1).join(", ")+" and "+r[r.length-1]+" must be defined"):new Error("option "+r[0]+" must be defined")}return Object.keys(e).forEach(function(n){n in t&&(t[n]=e[n])}),this},this.copy=function(t){var r={};return Object.keys(e).forEach(function(e){t.indexOf(e)!==-1&&(r[e]=n[e])}),r},this.read=function(e,t){if("function"==typeof t){var n=this;i.readFile(e,function(e,r){if(e)return t(e);var i=JSON.parse(r);n.merge(i),t()})}else{var r=JSON.parse(i.readFileSync(e));this.merge(r)}return this},this.isDefined=function(e){return"undefined"!=typeof n[e]},this.isDefinedAndNonNull=function(e){return"undefined"!=typeof n[e]&&null!==n[e]},Object.freeze(n),Object.freeze(this)}/*! * Copyright(c) 2011 Einar Otto Stangvik <einaros@gmail.com> * MIT Licensed */ var i=n(31);e.exports=r},function(e,t,n){"use strict";/*! * ws: a node.js websocket client * Copyright(c) 2011 Einar Otto Stangvik <einaros@gmail.com> * MIT Licensed */ try{e.exports=n(!function(){var e=new Error('Cannot find module "bufferutil"');throw e.code="MODULE_NOT_FOUND",e}())}catch(t){e.exports=n(25)}},function(e,t){/*! * ws: a node.js websocket client * Copyright(c) 2011 Einar Otto Stangvik <einaros@gmail.com> * MIT Licensed */ e.exports={isValidErrorCode:function(e){return e>=1e3&&e<=1011&&1004!=e&&1005!=e&&1006!=e||e>=3e3&&e<=4999},1e3:"normal",1001:"going away",1002:"protocol error",1003:"unsupported data",1004:"reserved",1005:"reserved for extensions",1006:"reserved for extensions",1007:"inconsistent or invalid data",1008:"policy violation",1009:"message too big",1010:"extension handshake missing",1011:"an unexpected condition prevented the request from being fulfilled"}},function(e,t,n){function r(e){e=e||"";var t={};return e.split(",").forEach(function(e){var n=e.split(";"),r=n.shift().trim(),i=t[r]=t[r]||[],o={};n.forEach(function(e){var t=e.trim().split("="),n=t[0],r=t[1];"undefined"==typeof r?r=!0:('"'===r[0]&&(r=r.slice(1)),'"'===r[r.length-1]&&(r=r.slice(0,r.length-1))),(o[n]=o[n]||[]).push(r)}),i.push(o)}),t}function i(e){return Object.keys(e).map(function(t){var n=e[t];return o.isArray(n)||(n=[n]),n.map(function(e){return[t].concat(Object.keys(e).map(function(t){var n=e[t];return o.isArray(n)||(n=[n]),n.map(function(e){return e===!0?t:t+"="+e}).join("; ")})).join("; ")}).join(", ")}).join(", ")}var o=n(1);t.parse=r,t.format=i},function(e,t,n){function r(e,t){if(this instanceof r==!1)throw new TypeError("Classes can't be function-called");"number"==typeof e&&(t=e,e={});var n=-1;this.fragmentedBufferPool=new f(1024,function(e,t){return e.used+t},function(e){return n=n>=0?Math.ceil((n+e.used)/2):e.used});var i=-1;this.unfragmentedBufferPool=new f(1024,function(e,t){return e.used+t},function(e){return i=i>=0?Math.ceil((i+e.used)/2):e.used}),this.extensions=e||{},this.maxPayload=t||0,this.currentPayloadLength=0,this.state={activeFragmentedOperation:null,lastFragment:!1,masked:!1,opcode:0,fragmentedOperation:!1},this.overflow=[],this.headerBuffer=new Buffer(10),this.expectOffset=0,this.expectBuffer=null,this.expectHandler=null,this.currentMessage=[],this.currentMessageLength=0,this.messageHandlers=[],this.expectHeader(2,this.processPacket),this.dead=!1,this.processing=!1,this.onerror=function(){},this.ontext=function(){},this.onbinary=function(){},this.onclose=function(){},this.onping=function(){},this.onpong=function(){}}function i(e){return(this[e]<<8)+this[e+1]}function o(e){return(this[e]<<24)+(this[e+1]<<16)+(this[e+2]<<8)+this[e+3]}function s(e,t,n,r){switch(e){default:t.copy(n,r,0,e);break;case 16:n[r+15]=t[15];case 15:n[r+14]=t[14];case 14:n[r+13]=t[13];case 13:n[r+12]=t[12];case 12:n[r+11]=t[11];case 11:n[r+10]=t[10];case 10:n[r+9]=t[9];case 9:n[r+8]=t[8];case 8:n[r+7]=t[7];case 7:n[r+6]=t[6];case 6:n[r+5]=t[5];case 5:n[r+4]=t[4];case 4:n[r+3]=t[3];case 3:n[r+2]=t[2];case 2:n[r+1]=t[1];case 1:n[r]=t[0]}}function a(e){var t={};for(var n in e)e.hasOwnProperty(n)&&(t[n]=e[n]);return t}/*! * ws: a node.js websocket client * Copyright(c) 2011 Einar Otto Stangvik <einaros@gmail.com> * MIT Licensed */ var c=(n(1),n(29).Validation),u=n(13),f=n(24),l=n(12).BufferUtil,h=n(3);e.exports=r,r.prototype.add=function(e){if(!this.dead){var t=e.length;if(0!=t){if(null==this.expectBuffer)return void this.overflow.push(e);var n=Math.min(t,this.expectBuffer.length-this.expectOffset);for(s(n,e,this.expectBuffer,this.expectOffset),this.expectOffset+=n,n<t&&this.overflow.push(e.slice(n));this.expectBuffer&&this.expectOffset==this.expectBuffer.length;){var r=this.expectBuffer;this.expectBuffer=null,this.expectOffset=0,this.expectHandler.call(this,r)}}}},r.prototype.cleanup=function(){this.dead=!0,this.overflow=null,this.headerBuffer=null,this.expectBuffer=null,this.expectHandler=null,this.unfragmentedBufferPool=null,this.fragmentedBufferPool=null,this.state=null,this.currentMessage=null,this.onerror=null,this.ontext=null,this.onbinary=null,this.onclose=null,this.onping=null,this.onpong=null},r.prototype.expectHeader=function(e,t){if(0==e)return void t(null);this.expectBuffer=this.headerBuffer.slice(this.expectOffset,this.expectOffset+e),this.expectHandler=t;for(var n=e;n>0&&this.overflow.length>0;){var r=this.overflow.pop();n<r.length&&this.overflow.push(r.slice(n));var i=Math.min(r.length,n);s(i,r,this.expectBuffer,this.expectOffset),this.expectOffset+=i,n-=i}},r.prototype.expectData=function(e,t){if(0==e)return void t(null);this.expectBuffer=this.allocateFromPool(e,this.state.fragmentedOperation),this.expectHandler=t;for(var n=e;n>0&&this.overflow.length>0;){var r=this.overflow.pop();n<r.length&&this.overflow.push(r.slice(n));var i=Math.min(r.length,n);s(i,r,this.expectBuffer,this.expectOffset),this.expectOffset+=i,n-=i}},r.prototype.allocateFromPool=function(e,t){return(t?this.fragmentedBufferPool:this.unfragmentedBufferPool).get(e)},r.prototype.processPacket=function(e){if(this.extensions[h.extensionName]){if(0!=(48&e[0]))return void this.error("reserved fields (2, 3) must be empty",1002)}else if(0!=(112&e[0]))return void this.error("reserved fields must be empty",1002);this.state.lastFragment=128==(128&e[0]),this.state.masked=128==(128&e[1]);var t=64==(64&e[0]),n=15&e[0];if(0===n){if(t)return void this.error("continuation frame cannot have the Per-message Compressed bits",1002);if(this.state.fragmentedOperation=!0,this.state.opcode=this.state.activeFragmentedOperation,1!=this.state.opcode&&2!=this.state.opcode)return void this.error("continuation frame cannot follow current opcode",1002)}else{if(n<3&&null!=this.state.activeFragmentedOperation)return void this.error("data frames after the initial data frame must have opcode 0",1002);if(n>=8&&t)return void this.error("control frames cannot have the Per-message Compressed bits",1002);this.state.compressed=t,this.state.opcode=n,this.state.lastFragment===!1?(this.state.fragmentedOperation=!0,this.state.activeFragmentedOperation=n):this.state.fragmentedOperation=!1}var r=d[this.state.opcode];"undefined"==typeof r?this.error("no handler for opcode "+this.state.opcode,1002):r.start.call(this,e)},r.prototype.endPacket=function(){this.dead||(this.state.fragmentedOperation?this.state.lastFragment&&this.fragmentedBufferPool.reset(!0):this.unfragmentedBufferPool.reset(!0),this.expectOffset=0,this.expectBuffer=null,this.expectHandler=null,this.state.lastFragment&&this.state.opcode===this.state.activeFragmentedOperation&&(this.state.activeFragmentedOperation=null),this.currentPayloadLength=0,this.state.lastFragment=!1,this.state.opcode=null!=this.state.activeFragmentedOperation?this.state.activeFragmentedOperation:0,this.state.masked=!1,this.expectHeader(2,this.processPacket))},r.prototype.reset=function(){this.dead||(this.state={activeFragmentedOperation:null,lastFragment:!1,masked:!1,opcode:0,fragmentedOperation:!1},this.fragmentedBufferPool.reset(!0),this.unfragmentedBufferPool.reset(!0),this.expectOffset=0,this.expectBuffer=null,this.expectHandler=null,this.overflow=[],this.currentMessage=[],this.currentMessageLength=0,this.messageHandlers=[],this.currentPayloadLength=0)},r.prototype.unmask=function(e,t,n){return null!=e&&null!=t&&l.unmask(t,e),n?t:null!=t?t.toString("utf8"):""},r.prototype.error=function(e,t){if(!this.dead)return this.reset(),"string"==typeof e?this.onerror(new Error(e),t):e.constructor==Error?this.onerror(e,t):this.onerror(new Error("An error occured"),t),this},r.prototype.flush=function(){if(!this.processing&&!this.dead){var e=this.messageHandlers.shift();if(e){this.processing=!0;var t=this;e(function(){t.processing=!1,t.flush()})}}},r.prototype.applyExtensions=function(e,t,n,r){var i=this;n?this.extensions[h.extensionName].decompress(e,t,function(e,t){if(!i.dead)return e?void r(new Error("invalid compressed data")):void r(null,t)}):r(null,e)},r.prototype.maxPayloadExceeded=function(e){if(void 0===this.maxPayload||null===this.maxPayload||this.maxPayload<1)return!1;var t=this.currentPayloadLength+e;return t<this.maxPayload?(this.currentPayloadLength=t,!1):(this.error("payload cannot exceed "+this.maxPayload+" bytes",1009),this.messageBuffer=[],this.cleanup(),!0)};var d={1:{start:function(e){var t=this,n=127&e[1];if(n<126){if(t.maxPayloadExceeded(n))return void t.error("Maximumpayload exceeded in compressed text message. Aborting...",1009);d[1].getData.call(t,n)}else 126==n?t.expectHeader(2,function(e){var n=i.call(e,0);return t.maxPayloadExceeded(n)?void t.error("Maximumpayload exceeded in compressed text message. Aborting...",1009):void d[1].getData.call(t,n)}):127==n&&t.expectHeader(8,function(e){if(0!=o.call(e,0))return void t.error("packets with length spanning more than 32 bit is currently not supported",1008);var n=o.call(e,4);return t.maxPayloadExceeded(n)?void t.error("Maximumpayload exceeded in compressed text message. Aborting...",1009):void d[1].getData.call(t,o.call(e,4))})},getData:function(e){var t=this;t.state.masked?t.expectHeader(4,function(n){var r=n;t.expectData(e,function(e){d[1].finish.call(t,r,e)})}):t.expectData(e,function(e){d[1].finish.call(t,null,e)})},finish:function(e,t){var n=this,r=this.unmask(e,t,!0)||new Buffer(0),i=a(this.state);this.messageHandlers.push(function(e){n.applyExtensions(r,i.lastFragment,i.compressed,function(t,r){if(t)return 1009===t.type?n.error("Maximumpayload exceeded in compressed text message. Aborting...",1009):n.error(t.message,1007);if(null!=r){if(!(0==n.maxPayload||n.maxPayload>0&&n.currentMessageLength+r.length<n.maxPayload))return n.currentMessage=null,n.currentMessage=[],n.currentMessageLength=0,void n.error(new Error("Maximum payload exceeded. maxPayload: "+n.maxPayload),1009);n.currentMessage.push(r),n.currentMessageLength+=r.length}if(i.lastFragment){var o=Buffer.concat(n.currentMessage);if(n.currentMessage=[],n.currentMessageLength=0,!c.isValidUTF8(o))return void n.error("invalid utf8 sequence",1007);n.ontext(o.toString("utf8"),{masked:i.masked,buffer:o})}e()})}),this.flush(),this.endPacket()}},2:{start:function(e){var t=this,n=127&e[1];if(n<126){if(t.maxPayloadExceeded(n))return void t.error("Max payload exceeded in compressed text message. Aborting...",1009);d[2].getData.call(t,n)}else 126==n?t.expectHeader(2,function(e){var n=i.call(e,0);return t.maxPayloadExceeded(n)?void t.error("Max payload exceeded in compressed text message. Aborting...",1009):void d[2].getData.call(t,n)}):127==n&&t.expectHeader(8,function(e){if(0!=o.call(e,0))return void t.error("packets with length spanning more than 32 bit is currently not supported",1008);var n=o.call(e,4,!0);return t.maxPayloadExceeded(n)?void t.error("Max payload exceeded in compressed text message. Aborting...",1009):void d[2].getData.call(t,n)})},getData:function(e){var t=this;t.state.masked?t.expectHeader(4,function(n){var r=n;t.expectData(e,function(e){d[2].finish.call(t,r,e)})}):t.expectData(e,function(e){d[2].finish.call(t,null,e)})},finish:function(e,t){var n=this,r=this.unmask(e,t,!0)||new Buffer(0),i=a(this.state);this.messageHandlers.push(function(e){n.applyExtensions(r,i.lastFragment,i.compressed,function(t,r){if(t)return 1009===t.type?n.error("Max payload exceeded in compressed binary message. Aborting...",1009):n.error(t.message,1007);if(null!=r){if(!(0==n.maxPayload||n.maxPayload>0&&n.currentMessageLength+r.length<n.maxPayload))return n.currentMessage=null,n.currentMessage=[],n.currentMessageLength=0,void n.error(new Error("Maximum payload exceeded"),1009);n.currentMessage.push(r),n.currentMessageLength+=r.length}if(i.lastFragment){var o=Buffer.concat(n.currentMessage);n.currentMessage=[],n.currentMessageLength=0,n.onbinary(o,{masked:i.masked,buffer:o})}e()})}),this.flush(),this.endPacket()}},8:{start:function(e){var t=this;if(0==t.state.lastFragment)return void t.error("fragmented close is not supported",1002);var n=127&e[1];n<126?d[8].getData.call(t,n):t.error("control frames cannot have more than 125 bytes of data",1002)},getData:function(e){var t=this;t.state.masked?t.expectHeader(4,function(n){var r=n;t.expectData(e,function(e){d[8].finish.call(t,r,e)})}):t.expectData(e,function(e){d[8].finish.call(t,null,e)})},finish:function(e,t){var n=this;t=n.unmask(e,t,!0);var r=a(this.state);this.messageHandlers.push(function(){if(t&&1==t.length)return void n.error("close packets with data must be at least two bytes long",1002);var e=t&&t.length>1?i.call(t,0):1e3;if(!u.isValidErrorCode(e))return void n.error("invalid error code",1002);var o="";if(t&&t.length>2){var s=t.slice(2);if(!c.isValidUTF8(s))return void n.error("invalid utf8 sequence",1007);o=s.toString("utf8")}n.onclose(e,o,{masked:r.masked}),n.reset()}),this.flush()}},9:{start:function(e){var t=this;if(0==t.state.lastFragment)return void t.error("fragmented ping is not supported",1002);var n=127&e[1];n<126?d[9].getData.call(t,n):t.error("control frames cannot have more than 125 bytes of data",1002)},getData:function(e){var t=this;t.state.masked?t.expectHeader(4,function(n){var r=n;t.expectData(e,function(e){d[9].finish.call(t,r,e)})}):t.expectData(e,function(e){d[9].finish.call(t,null,e)})},finish:function(e,t){var n=this;t=this.unmask(e,t,!0);var r=a(this.state);this.messageHandlers.push(function(e){n.onping(t,{masked:r.masked,binary:!0}),e()}),this.flush(),this.endPacket()}},10:{start:function(e){var t=this;if(0==t.state.lastFragment)return void t.error("fragmented pong is not supported",1002);var n=127&e[1];n<126?d[10].getData.call(t,n):t.error("control frames cannot have more than 125 bytes of data",1002)},getData:function(e){var t=this;this.state.masked?this.expectHeader(4,function(n){var r=n;t.expectData(e,function(e){d[10].finish.call(t,r,e)})}):this.expectData(e,function(e){d[10].finish.call(t,null,e)})},finish:function(e,t){var n=this;t=n.unmask(e,t,!0);var r=a(this.state);this.messageHandlers.push(function(e){n.onpong(t,{masked:r.masked,binary:!0}),e()}),this.flush(),this.endPacket()}}}},function(e,t,n){function r(e,t){if(this instanceof r==!1)throw new TypeError("Classes can't be function-called");c.EventEmitter.call(this),this._socket=e,this.extensions=t||{},this.firstFragment=!0,this.compress=!1,this.messageHandlers=[],this.processing=!1}function i(e,t){this[t]=(65280&e)>>8,this[t+1]=255&e}function o(e,t){this[t]=(4278190080&e)>>24,this[t+1]=(16711680&e)>>16,this[t+2]=(65280&e)>>8,this[t+3]=255&e}function s(e){for(var t=new Uint8Array(e.buffer||e),n=e.byteLength||e.length,r=e.byteOffset||0,i=new Buffer(n),o=0;o<n;++o)i[o]=t[r+o];return i}function a(){return new Buffer([~~(255*Math.random()),~~(255*Math.random()),~~(255*Math.random()),~~(255*Math.random())])}/*! * ws: a node.js websocket client * Copyright(c) 2011 Einar Otto Stangvik <einaros@gmail.com> * MIT Licensed */ var c=n(2),u=n(1),f=(c.EventEmitter,n(13)),l=n(12).BufferUtil,h=n(3);u.inherits(r,c.EventEmitter),r.prototype.close=function(e,t,n,r){if("undefined"!=typeof e&&("number"!=typeof e||!f.isValidErrorCode(e)))throw new Error("first argument must be a valid error code number");e=e||1e3;var o=new Buffer(2+(t?Buffer.byteLength(t):0));i.call(o,e,0),o.length>2&&o.write(t,2);var s=this;this.messageHandlers.push(function(e){s.frameAndSend(8,o,!0,n),e(),"function"==typeof r&&r()}),this.flush()},r.prototype.ping=function(e,t){var n=t&&t.mask,r=this;this.messageHandlers.push(function(t){r.frameAndSend(9,e||"",!0,n),t()}),this.flush()},r.prototype.pong=function(e,t){var n=t&&t.mask,r=this;this.messageHandlers.push(function(t){r.frameAndSend(10,e||"",!0,n),t()}),this.flush()},r.prototype.send=function(e,t,n){var r=!t||t.fin!==!1,i=t&&t.mask,o=t&&t.compress,s=t&&t.binary?2:1;this.firstFragment===!1?(s=0,o=!1):(this.firstFragment=!1,this.compress=o),r&&(this.firstFragment=!0);var a=this.compress,c=this;this.messageHandlers.push(function(t){c.applyExtensions(e,r,a,function(e,a){return e?void("function"==typeof n?n(e):c.emit("error",e)):(c.frameAndSend(s,a,r,i,o,n),void t())})}),this.flush()},r.prototype.frameAndSend=function(e,t,n,r,c,u){var f=!1;if(t){Buffer.isBuffer(t)||(f=!0,!t||"undefined"==typeof t.byteLength&&"undefined"==typeof t.buffer?("number"==typeof t&&(t=t.toString()),t=new Buffer(t)):t=s(t));var h=t.length,d=r?6:2,p=h;h>=65536?(d+=8,p=127):h>125&&(d+=2,p=126);var y=h<32768||r&&!f,v=y?h+d:d,m=new Buffer(v);switch(m[0]=n?128|e:e,c&&(m[0]|=64),p){case 126:i.call(m,h,2);break;case 127:o.call(m,0,2),o.call(m,h,6)}if(r){m[1]=128|p;var _=a();if(m[d-4]=_[0],m[d-3]=_[1],m[d-2]=_[2],m[d-1]=_[3],y){l.mask(t,_,m,d,h);try{this._socket.write(m,"binary",u)}catch(e){"function"==typeof u?u(e):this.emit("error",e)}}else{l.mask(t,_,t,0,h);try{this._socket.write(m,"binary"),this._socket.write(t,"binary",u)}catch(e){"function"==typeof u?u(e):this.emit("error",e)}}}else if(m[1]=p,y){t.copy(m,d);try{this._socket.write(m,"binary",u)}catch(e){"function"==typeof u?u(e):this.emit("error",e)}}else try{this._socket.write(m,"binary"),this._socket.write(t,"binary",u)}catch(e){"function"==typeof u?u(e):this.emit("error",e)}}else try{this._socket.write(new Buffer([e|(n?128:0),0|(r?128:0)].concat(r?[0,0,0,0]:[])),"binary",u)}catch(e){"function"==typeof u?u(e):this.emit("error",e)}},r.prototype.flush=function(){if(!this.processing){var e=this.messageHandlers.shift();if(e){this.processing=!0;var t=this;e(function(){t.processing=!1,t.flush()})}}},r.prototype.applyExtensions=function(e,t,n,r){n&&e?((e.buffer||e)instanceof ArrayBuffer&&(e=s(e)),this.extensions[h.extensionName].compress(e,t,r)):r(null,e)},e.exports=r},function(e,t,n){"use strict";function r(e,t,n){return this instanceof r==!1?new r(e,t,n):(A.call(this),t&&!Array.isArray(t)&&"object"==typeof t&&(n=t,t=null),"string"==typeof t&&(t=[t]),Array.isArray(t)||(t=[]),this._socket=null,this._ultron=null,this._closeReceived=!1,this.bytesReceived=0,this.readyState=null,this.supports={},this.extensions={},this._binaryType="nodebuffer",void(Array.isArray(e)?c.apply(this,e.concat(n)):u.apply(this,[e,t,n])))}function i(e,t,n){this.type="message",this.data=e,this.target=n,this.binary=t}function o(e,t,n){this.type="close",this.wasClean="undefined"==typeof e||1e3===e,this.code=e,this.reason=t,this.target=n}function s(e){this.type="open",this.target=e}function a(e,t,n){var r=t;return t&&(e&&443!=n||!e&&80!=n)&&(r=r+":"+n),r}function c(e,t,n,i){i=new x({protocolVersion:M,protocol:null,extensions:{},maxPayload:0}).merge(i),this.protocol=i.value.protocol,this.protocolVersion=i.value.protocolVersion,this.extensions=i.value.extensions,this.supports.binary="hixie-76"!==this.protocolVersion,this.upgradeReq=e,this.readyState=r.CONNECTING,this._isServer=!0,this.maxPayload=i.value.maxPayload,"hixie-76"===i.value.protocolVersion?f.call(this,O,S,t,n):f.call(this,E,k,t,n)}function u(e,t,n){if(n=new x({origin:null,protocolVersion:M,host:null,headers:null,protocol:t.join(","),agent:null,pfx:null,key:null,passphrase:null,cert:null,ca:null,ciphers:null,rejectUnauthorized:null,perMessageDeflate:!0,localAddress:null}).merge(n),8!==n.value.protocolVersion&&13!==n.value.protocolVersion)throw new Error("unsupported protocol version");var i=y.parse(e),o="ws+unix:"===i.protocol;if(!i.host&&!o)throw new Error("invalid url");var s,c="wss:"===i.protocol||"https:"===i.protocol,u=c?_:m,l=i.port||(c?443:80),h=i.auth,d={};n.value.perMessageDeflate&&(s=new N(typeof n.value.perMessageDeflate!==!0?n.value.perMessageDeflate:{},!1),d[N.extensionName]=s.offer()),this._isServer=!1,this.url=e,this.protocolVersion=n.value.protocolVersion,this.supports.binary="hixie-76"!==this.protocolVersion;var v=new Buffer(n.value.protocolVersion+"-"+Date.now()).toString("base64"),w=g.createHash("sha1");w.update(v+"258EAFA5-E914-47DA-95CA-C5AB0DC85B11");var b=w.digest("base64"),S=n.value.agent,O=a(c,i.hostname,l),A={port:l,host:i.hostname,headers:{Connection:"Upgrade",Upgrade:"websocket",Host:O,"Sec-WebSocket-Version":n.value.protocolVersion,"Sec-WebSocket-Key":v}};if(h&&(A.headers.Authorization="Basic "+new Buffer(h).toString("base64")),n.value.protocol&&(A.headers["Sec-WebSocket-Protocol"]=n.value.protocol),n.value.host&&(A.headers.Host=n.value.host),n.value.headers)for(var C in n.value.headers)n.value.headers.hasOwnProperty(C)&&(A.headers[C]=n.value.headers[C]);Object.keys(d).length&&(A.headers["Sec-WebSocket-Extensions"]=P.format(d)),(n.isDefinedAndNonNull("pfx")||n.isDefinedAndNonNull("key")||n.isDefinedAndNonNull("passphrase")||n.isDefinedAndNonNull("cert")||n.isDefinedAndNonNull("ca")||n.isDefinedAndNonNull("ciphers")||n.isDefinedAndNonNull("rejectUnauthorized"))&&(n.isDefinedAndNonNull("pfx")&&(A.pfx=n.value.pfx),n.isDefinedAndNonNull("key")&&(A.key=n.value.key),n.isDefinedAndNonNull("passphrase")&&(A.passphrase=n.value.passphrase),n.isDefinedAndNonNull("cert")&&(A.cert=n.value.cert),n.isDefinedAndNonNull("ca")&&(A.ca=n.value.ca),n.isDefinedAndNonNull("ciphers")&&(A.ciphers=n.value.ciphers),n.isDefinedAndNonNull("rejectUnauthorized")&&(A.rejectUnauthorized=n.value.rejectUnauthorized),S||(S=new u.Agent(A))),A.path=i.path||"/",S&&(A.agent=S),o&&(A.socketPath=i.pathname),n.value.localAddress&&(A.localAddress=n.value.localAddress),n.value.origin&&(n.value.protocolVersion<13?A.headers["Sec-WebSocket-Origin"]=n.value.origin:A.headers.Origin=n.value.origin);var T=this,B=u.request(A);B.on("error",function(e){T.emit("error",e),p.call(T,e)}),B.once("response",function(e){var t;T.emit("unexpected-response",B,e)||(t=new Error("unexpected server response ("+e.statusCode+")"),B.abort(),T.emit("error",t)),p.call(T,t)}),B.once("upgrade",function(e,t,i){if(T.readyState===r.CLOSED)return T.emit("close"),T.removeAllListeners(),void t.end();var o=e.headers["sec-websocket-accept"];if("undefined"==typeof o||o!==b)return T.emit("error","invalid server key"),T.removeAllListeners(),void t.end();var a=e.headers["sec-websocket-protocol"],c=(n.value.protocol||"").split(/, */),u=null;if(!n.value.protocol&&a?u="server sent a subprotocol even though none requested":n.value.protocol&&!a?u="server sent no subprotocol even though requested":a&&c.indexOf(a)===-1&&(u="server responded with an invalid protocol"),u)return T.emit("error",u),T.removeAllListeners(),void t.end();a&&(T.protocol=a);var l=P.parse(e.headers["sec-websocket-extensions"]);if(s&&l[N.extensionName]){try{s.accept(l[N.extensionName])}catch(e){return T.emit("error","invalid extension parameter"),T.removeAllListeners(),void t.end()}T.extensions[N.extensionName]=s}f.call(T,E,k,t,i),B.removeAllListeners(),B=null,S=null}),B.end(),this.readyState=r.CONNECTING}function f(e,t,n,i){function o(e){c||u.readyState===r.CLOSED||(c=!0,n.removeListener("data",o),a.on("data",s),i&&i.length>0&&(s(i),i=null),e&&s(e))}function s(e){u.bytesReceived+=e.length,u._receiver.add(e)}var a=this._ultron=new b(n),c=!1,u=this;n.setTimeout(0),n.setNoDelay(!0),this._receiver=new e(this.extensions,this.maxPayload),this._socket=n,a.on("end",p.bind(this)),a.on("close",p.bind(this)),a.on("error",p.bind(this)),a.on("data",o),process.nextTick(o),u._receiver.ontext=function(e,t){t=t||{},u.emit("message",e,t)},u._receiver.onbinary=function(e,t){t=t||{},t.binary=!0,u.emit("message",e,t)},u._receiver.onping=function(e,t){t=t||{},u.pong(e,{mask:!u._isServer,binary:t.binary===!0},!0),u.emit("ping",e,t)},u._receiver.onpong=function(e,t){u.emit("pong",e,t||{})},u._receiver.onclose=function(e,t,n){n=n||{},u._closeReceived=!0,u.close(e,t)},u._receiver.onerror=function(e,t){u.close("undefined"!=typeof t?t:1002,""),u.emit("error",e instanceof Error?e:new Error(e))},this._sender=new t(n,this.extensions),this._sender.on("error",function(e){u.close(1002,""),u.emit("error",e)}),this.readyState=r.OPEN,this.emit("open")}function l(e){e._queue=e._queue||[]}function h(e){var t=e._queue;if("undefined"!=typeof t){delete e._queue;for(var n=0,r=t.length;n<r;++n)t[n]()}}function d(e,t,n,i){t.on("data",function(t){return e.readyState!==r.OPEN?void("function"==typeof i?i(new Error("not opened")):(delete e._queue,e.emit("error",new Error("not opened")))):(n.fin=!1,void e._sender.send(t,n))}),t.on("end",function(){return e.readyState!==r.OPEN?void("function"==typeof i?i(new Error("not opened")):(delete e._queue,e.emit("error",new Error("not opened")))):(n.fin=!0,e._sender.send(null,n),void("function"==typeof i&&i(null)))})}function p(e){if(this.readyState!==r.CLOSED){if(this.readyState=r.CLOSED,clearTimeout(this._closeTimer),this._closeTimer=null,!e&&this._closeReceived||(this._closeCode=1006),this.emit("close",this._closeCode||1e3,this._closeMessage||""),this._socket){this._ultron&&this._ultron.destroy(),this._socket.on("error",function(){try{this.destroy()}catch(e){}});try{e?this._socket.destroy():this._socket.end()}catch(e){}this._socket=null,this._ultron=null}this._sender&&(this._sender.removeAllListeners(),this._sender=null),this._receiver&&(this._receiver.cleanup(),this._receiver=null),this.extensions[N.extensionName]&&this.extensions[N.extensionName].cleanup(),this.extensions=null,this.removeAllListeners(),this.on("