@iotize/tap
Version:
IoTize Device client for Javascript
16 lines (14 loc) • 9.53 kB
JavaScript
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("buffer"),require("rxjs"),require("@iotize/common/debug"),require("@iotize/common/byte-converter"),require("@iotize/common/error")):"function"==typeof define&&define.amd?define("@iotize/tap/relay/impl",["exports","buffer","rxjs","@iotize/common/debug","@iotize/common/byte-converter","@iotize/common/error"],e):e(((t="undefined"!=typeof globalThis?globalThis:t||self).iotize=t.iotize||{},t.iotize.tap=t.iotize.tap||{},t.iotize.tap.relay=t.iotize.tap.relay||{},t.iotize.tap.relay.impl={}),t.buffer,t.rxjs,t.debug$1,t.byteConverter,t.error)}(this,(function(t,e,o,r,n,i){"use strict";
/*! *****************************************************************************
Copyright (c) Microsoft Corporation.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */var c=function(t,e){return c=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var o in e)Object.prototype.hasOwnProperty.call(e,o)&&(t[o]=e[o])},c(t,e)};function s(t,e){var o,r,n,i,c={label:0,sent:function(){if(1&n[0])throw n[1];return n[1]},trys:[],ops:[]};return i={next:s(0),throw:s(1),return:s(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function s(i){return function(s){return function(i){if(o)throw new TypeError("Generator is already executing.");for(;c;)try{if(o=1,r&&(n=2&i[0]?r.return:i[0]?r.throw||((n=r.return)&&n.call(r),0):r.next)&&!(n=n.call(r,i[1])).done)return n;switch(r=0,n&&(i=[2&i[0],n.value]),i[0]){case 0:case 1:n=i;break;case 4:return c.label++,{value:i[1],done:!1};case 5:c.label++,r=i[1],i=[0];continue;case 7:i=c.ops.pop(),c.trys.pop();continue;default:if(!(n=c.trys,(n=n.length>0&&n[n.length-1])||6!==i[0]&&2!==i[0])){c=0;continue}if(3===i[0]&&(!n||i[1]>n[0]&&i[1]<n[3])){c.label=i[1];break}if(6===i[0]&&c.label<n[1]){c.label=n[1],n=i;break}if(n&&c.label<n[2]){c.label=n[2],c.ops.push(i);break}n[2]&&c.ops.pop(),c.trys.pop();continue}i=e.call(t,c)}catch(t){i=[6,t],r=0}finally{o=n=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,s])}}}Object.create;Object.create;var u,a=r.createDebugger("@iotize/tap/relay"),l=this&&this.__awaiter||function(t,e,o,r){return new(o||(o=Promise))((function(n,i){function c(t){try{u(r.next(t))}catch(t){i(t)}}function s(t){try{u(r.throw(t))}catch(t){i(t)}}function u(t){var e;t.done?n(t.value):(e=t.value,e instanceof o?e:new o((function(t){t(e)}))).then(c,s)}u((r=r.apply(t,e||[])).next())}))},f="Relay",p=function(){function t(t,e){this._server=t,this._params=e||{port:2e3},this._clients=[]}return Object.defineProperty(t.prototype,"server",{get:function(){return this._server},enumerable:!1,configurable:!0}),t.prototype.addClient=function(t){return this._clients.push(t),this},t.prototype.removeClient=function(t){var e=this._clients.indexOf(t);return e>=0&&(t.close(),a(f,"Removing client "+t.getClientName()+"..."),delete this._clients[e]),this},t.prototype.start=function(){var t=this;return this._subscription=new o.Subject,this._server.connections().subscribe((function(e){return l(t,void 0,void 0,(function(){var t,o=this;return s(this,(function(r){switch(r.label){case 0:return t=e.getClientName(),a(f,"[NEW_CLIENT] "+t),[4,e.ready()];case 1:return r.sent(),a(f,"[NEW_CLIENT] "+t+" IS READY"),this.addClient(e),e.events().subscribe({next:function(r){switch(r.type){case"data":var n=o.messageToLog(r.payload);a(f,"[MESSAGE] "+n+" from "+t),o.broadcastMessage(r.payload,e);break;case"error":a(f,"[ERROR] "+t+" ",r.payload),o.removeClient(e);break;case"end":a(f,"[CLIENTEND] "+t),o.removeClient(e)}}}),[2]}}))}))})),this._server.listen(this._params)},t.prototype.stop=function(){return l(this,void 0,void 0,(function(){return s(this,(function(t){switch(t.label){case 0:return this._clients.forEach((function(t){t.close()})),this._server?[4,this._server.stop()]:[3,2];case 1:t.sent(),t.label=2;case 2:return this._subscription&&this._subscription.complete(),[2]}}))}))},t.prototype.broadcastMessage=function(t,e){var o=this.messageToLog(t);a(f,"[BROADCAST] "+o+" from "+e.getClientName()),0!=this._clients.length?this._clients.forEach((function(o){try{if(o===e)return void a(f,"\t- Skip sender "+o.getClientName());o.isWritable()?(a(f,"\t- Sent to "+o.getClientName()),o.write(t)):a(f,"Client "+o.getClientName()+" is not writable. Cannot send to it")}catch(t){a("Cannot broadcast to "+o.getClientName(),t)}})):a(f,"There is no client yet")},t.prototype.messageToLog=function(t){return"string"==typeof t?'"'+t+'"':t instanceof e.Buffer?n.bufferToHexString(t):t},t}(),h=this&&this.__awaiter||function(t,e,o,r){return new(o||(o=Promise))((function(n,i){function c(t){try{u(r.next(t))}catch(t){i(t)}}function s(t){try{u(r.throw(t))}catch(t){i(t)}}function u(t){var e;t.done?n(t.value):(e=t.value,e instanceof o?e:new o((function(t){t(e)}))).then(c,s)}u((r=r.apply(t,e||[])).next())}))},m="Relay";t.ComRelayError=function(t){function e(e,o){return t.call(this,o,e)||this}return function(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Class extends value "+String(e)+" is not a constructor or null");function o(){this.constructor=t}c(t,e),t.prototype=null===e?Object.create(e):(o.prototype=e.prototype,new o)}(e,t),e.illegalArgument=function(t){return new e(e.Code.IllegalArgmentError,t)},e}(i.CodeError),u=t.ComRelayError||(t.ComRelayError={}),(u.Code||(u.Code={})).IllegalArgmentError="IllegalArgmentError";var d=function(){function e(t,e){t&&this.setSourceProtocol(t),e&&this.setTargetProtocol(e)}return Object.defineProperty(e.prototype,"sourceProtocol",{get:function(){if(!this.mSourceProtocol)throw t.ComRelayError.illegalArgument("Missing source protocol");return this.mSourceProtocol},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"targetProtocol",{get:function(){if(!this.mTargetProtocol)throw t.ComRelayError.illegalArgument("Missing target protocol");return this.mTargetProtocol},enumerable:!1,configurable:!0}),e.prototype.setSourceProtocol=function(t){return this.mSourceProtocol&&this.removeSourceProtocol(),this.mSourceProtocol=t,this},e.prototype.removeSourceProtocol=function(){return this._sourceProtocolSubscription&&(this._sourceProtocolSubscription.unsubscribe(),this._sourceProtocolSubscription=void 0),this.mSourceProtocol=void 0,this},e.prototype.setTargetProtocol=function(t){return this.mTargetProtocol=t,this},e.prototype.start=function(t){return h(this,void 0,void 0,(function(){var e=this;return s(this,(function(o){return[2,(r=t&&t.timeout||-1,n=function(t,o){return h(e,void 0,void 0,(function(){var e;return s(this,(function(r){switch(r.label){case 0:return r.trys.push([0,5,,6]),this.sourceProtocol.isConnected()?[3,2]:(a(m,"Connecting to source protocol..."),[4,this.sourceProtocol.connect().toPromise()]);case 1:r.sent(),r.label=2;case 2:return this.targetProtocol.isConnected()?[3,4]:(a(m,"Connecting to target protocol..."),[4,this.targetProtocol.connect().toPromise()]);case 3:r.sent(),r.label=4;case 4:return a(m,"Source/Target are now connected. Listening to messages..."),this.listenToDataStream(),t(),[3,6];case 5:return e=r.sent(),o(e),[3,6];case 6:return[2]}}))}))},new Promise((function(t,e){a(m,"promiseTimout start with "+r+" milliseconds"),n(t,e),r>=0&&setTimeout((function(){e("Promise timed out after "+r+" ms")}),r)})))];var r,n}))}))},e.prototype.stop=function(t){var e,o,r,n;!(null==t?void 0:t.keepTargetProtocolConnected)&&(null===(e=this.mTargetProtocol)||void 0===e?void 0:e.isConnected())&&(null===(o=this.mTargetProtocol)||void 0===o||o.disconnect()),!(null==t?void 0:t.keepSourceProtocolConnected)&&(null===(r=this.mSourceProtocol)||void 0===r?void 0:r.isConnected())&&(null===(n=this.mSourceProtocol)||void 0===n||n.disconnect())},e.prototype.listenToDataStream=function(){var e=this;return new Promise((function(o,r){if(!e.mSourceProtocol)return r(t.ComRelayError.illegalArgument("Missing source protocol"));e._sourceProtocolSubscription=e.mSourceProtocol.receiveStream().subscribe({next:function(t){if(t)if(e.mTargetProtocol)try{a(m,"Received new message",n.bufferToHexString(t)),e.mTargetProtocol.send(t).toPromise().then((function(t){return t=t||new Uint8Array,a(m,"Target protocol responded with: ",n.bufferToHexString(t)),e.sourceProtocol.write(t)})).then((function(){a(m,"Successfully sent response!")})).catch((function(t){a(m,"Cannot transfer response to source protocol: "+t+" JSON="+JSON.stringify(t),t)}))}catch(t){a(m,"Source cannot transfer data to target due to error: "+t,t)}else a(m,"Target protocol is null. Cannot redirect message to it");else a(m,"Received null event...")},error:function(t){a(m,"Error occured "+t)},complete:function(){o()}})}))},e.prototype.onStop=function(){return this},e}();t.ComRelay=d,t.RelayServer=p,Object.defineProperty(t,"__esModule",{value:!0})}));
//# sourceMappingURL=iotize-tap-relay-impl.umd.min.js.map