UNPKG

nanomsg-browser

Version:

A convinient wrapper for nanomsg in the browser

1 lines 4.78 kB
"use strict";function _createForOfIteratorHelper(e){if("undefined"==typeof Symbol||null==e[Symbol.iterator]){if(Array.isArray(e)||(e=_unsupportedIterableToArray(e))){var n=0,o=function(){};return{s:o,n:function(){return n>=e.length?{done:!0}:{done:!1,value:e[n++]}},e:function(e){throw e},f:o}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var r,t,i=!0,s=!1;return{s:function(){r=e[Symbol.iterator]()},n:function(){var e=r.next();return i=e.done,e},e:function(e){s=!0,t=e},f:function(){try{i||null==r.return||r.return()}finally{if(s)throw t}}}}function _unsupportedIterableToArray(e,n){if(e){if("string"==typeof e)return _arrayLikeToArray(e,n);var o=Object.prototype.toString.call(e).slice(8,-1);return"Object"===o&&e.constructor&&(o=e.constructor.name),"Map"===o||"Set"===o?Array.from(e):"Arguments"===o||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(o)?_arrayLikeToArray(e,n):void 0}}function _arrayLikeToArray(e,n){(null==n||n>e.length)&&(n=e.length);for(var o=0,r=new Array(n);o<n;o++)r[o]=e[o];return r}function _classCallCheck(e,n){if(!(e instanceof n))throw new TypeError("Cannot call a class as a function")}function _defineProperties(e,n){for(var o=0;o<n.length;o++){var r=n[o];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}function _createClass(e,n,o){return n&&_defineProperties(e.prototype,n),o&&_defineProperties(e,o),e}var Protocol={REQ:"rep.sp.nanomsg.org",PAIR:"pair.sp.nanomsg.org",SUB:"pub.sp.nanomsg.org",BUS:"bus.sp.nanomsg.org"},Socket=function(){function n(e){_classCallCheck(this,n),this.config=Object.assign({protocol:null,debug:!1,reconnectTime:1e3,receiveArrayBuffer:!1,sendArrayBuffer:!1},e),this.wss=new Map,this.reqIdHeader=null,this.promise=null,this.cbs={data:[],end:[],error:[]},this.reqIdHeader=new Uint8Array(4),window.crypto.getRandomValues(this.reqIdHeader),this.reqIdHeader[0]|=128}return _createClass(n,[{key:"connect",value:function(o){var r=this;return new Promise(function(e,n){r.wss.has(o)?e():(r.config.debug&&console.log("nanomsg connect to: "+o),function n(){try{r.__setupSocketConnection(o,e)}catch(e){console.exception(e),r.wss.delete(o),setTimeout(n,r.config.reconnectTime)}}())})}},{key:"__setupSocketConnection",value:function(e,n){var r=this,o=new WebSocket(e,[this.config.protocol]);o.initialUrl=e,this.config.receiveArrayBuffer&&(o.binaryType="arraybuffer"),this.wss.set(e,o),o.onopen=function(){r.config.debug&&console.log("nanomsg connected: "+e),n()},o.onmessage=function(e){var n,o=null,o=r.config.protocol===Protocol.REQ?e.data.slice(4):e.data;r.config.receiveArrayBuffer?r.__resolveNewData(o):((n=new FileReader).onload=function(e){r.__resolveNewData(e.target.result)},n.readAsText(o))},o.onerror=function(n){r.config.debug&&console.exception("nanomsg error",n),r.cbs.error.forEach(function(e){return e(n)})},o.onclose=function(){r.config.debug&&console.log("nanomsg close: "+o.initialUrl),r.wss.has(o.initialUrl)&&(r.config.debug&&console.log("nanomsg reconnect: "+o.initialUrl),r.wss.delete(o.initialUrl),r.connect(o.initialUrl)),r.cbs.end.forEach(function(e){return e(o.initialUrl)})}}},{key:"__resolveNewData",value:function(n){n&&(this.promise&&(this.promise.resolve(n),this.promise=null),this.cbs.data.forEach(function(e){return e(n)}))}},{key:"disconnect",value:function(e){var n=this.wss.get(e);n&&(this.wss.delete(e),n.close())}},{key:"bind",value:function(){throw new Error("WHOAAAA!!! Nice try, but NO, we do not support these!")}},{key:"on",value:function(e,n){var o=this.cbs[e];o&&o.push(n)}},{key:"send",value:function(e){var n,o,r=this;if(this.config.protocol===Protocol.SUB)throw new Exception("SUB socket can not send");if(this.wss.size<1)throw new Exception("you are not connected to any socket");if(this.config.sendArrayBuffer&&("string"==typeof e||e instanceof String))if("undefined"==typeof TextEncoder){for(var t=new Uint8Array(e.length),i=0;i<e.length;++i)t[i]=e.charCodeAt(i);e=t}else{e=(new TextEncoder).encode(e)}this.config.protocol===Protocol.REQ&&(n=e.length||e.byteLength,(o=new Uint8Array(n+4)).set(this.reqIdHeader,0),o.set(e,4),e=o),this.config.debug&&console.log("nanomsg send =>",e);var s,a=_createForOfIteratorHelper(this.wss.values());try{for(a.s();!(s=a.n()).done;){var c=s.value;c.readyState===WebSocket.OPEN?c.send(e):1<c.readyState&&(this.wss.has(c.url)&&this.wss.delete(c.url),this.config.debug&&console.warn("nanomsg: could not send, because of closed connection ("+c.url+")"))}}catch(e){a.e(e)}finally{a.f()}if(-1!==[Protocol.REQ,Protocol.PAIR].indexOf(this.config.protocol))return new Promise(function(e,n){r.promise={resolve:e,reject:n}})}}]),n}();module.exports={Protocol:Protocol,Socket:Socket};