UNPKG

w-comor-mqtt

Version:

A websocket communicator in nodejs and browser. Mapping functions from nodejs to other end points, like a simple RPC.

8 lines (7 loc) 17.3 kB
/*! * w-comor-mqtt-server v1.0.36 * (c) 2018-2021 yuda-lyu(semisphere) * Released under the MIT License. */ !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e(require("aedes"),require("aedes-server-factory"),require("net")):"function"==typeof define&&define.amd?define(["aedes","aedes-server-factory","net"],e):(t="undefined"!=typeof globalThis?globalThis:t||self)["w-comor-mqtt-server"]=e(t.aedes,t["aedes-server-factory"],t.net)}(this,(function(t,e,r){"use strict";var n="object"==typeof global&&global&&global.Object===Object&&global,o="object"==typeof self&&self&&self.Object===Object&&self,i=n||o||Function("return this")(),u=i.Symbol,c=Object.prototype,a=c.hasOwnProperty,f=c.toString,s=u?u.toStringTag:void 0;var l=Object.prototype.toString;var p="[object Null]",h="[object Undefined]",v=u?u.toStringTag:void 0;function b(t){return null==t?void 0===t?h:p:v&&v in Object(t)?function(t){var e=a.call(t,s),r=t[s];try{t[s]=void 0;var n=!0}catch(t){}var o=f.call(t);return n&&(e?t[s]=r:delete t[s]),o}(t):function(t){return l.call(t)}(t)}function y(t){return null!=t&&"object"==typeof t}function _(t){return y(t)&&"[object Arguments]"==b(t)}var d=Object.prototype,g=d.hasOwnProperty,j=d.propertyIsEnumerable,O=_(function(){return arguments}())?_:function(t){return y(t)&&g.call(t,"callee")&&!j.call(t,"callee")},m=O,w=Array.isArray;var S="object"==typeof exports&&exports&&!exports.nodeType&&exports,A=S&&"object"==typeof module&&module&&!module.nodeType&&module,z=A&&A.exports===S?i.Buffer:void 0,x=(z?z.isBuffer:void 0)||function(){return!1},$=9007199254740991,F=/^(?:0|[1-9]\d*)$/;function P(t,e){var r=typeof t;return!!(e=null==e?$:e)&&("number"==r||"symbol"!=r&&F.test(t))&&t>-1&&t%1==0&&t<e}var C=9007199254740991;function k(t){return"number"==typeof t&&t>-1&&t%1==0&&t<=C}var E={};E["[object Float32Array]"]=E["[object Float64Array]"]=E["[object Int8Array]"]=E["[object Int16Array]"]=E["[object Int32Array]"]=E["[object Uint8Array]"]=E["[object Uint8ClampedArray]"]=E["[object Uint16Array]"]=E["[object Uint32Array]"]=!0,E["[object Arguments]"]=E["[object Array]"]=E["[object ArrayBuffer]"]=E["[object Boolean]"]=E["[object DataView]"]=E["[object Date]"]=E["[object Error]"]=E["[object Function]"]=E["[object Map]"]=E["[object Number]"]=E["[object Object]"]=E["[object RegExp]"]=E["[object Set]"]=E["[object String]"]=E["[object WeakMap]"]=!1;var N,T="object"==typeof exports&&exports&&!exports.nodeType&&exports,q=T&&"object"==typeof module&&module&&!module.nodeType&&module,M=q&&q.exports===T&&n.process,B=function(){try{var t=q&&q.require&&q.require("util").types;return t||M&&M.binding&&M.binding("util")}catch(t){}}(),D=B&&B.isTypedArray,I=D?(N=D,function(t){return N(t)}):function(t){return y(t)&&k(t.length)&&!!E[b(t)]},U=Object.prototype.hasOwnProperty;function L(t,e){var r=w(t),n=!r&&m(t),o=!r&&!n&&x(t),i=!r&&!n&&!o&&I(t),u=r||n||o||i,c=u?function(t,e){for(var r=-1,n=Array(t);++r<t;)n[r]=e(r);return n}(t.length,String):[],a=c.length;for(var f in t)!e&&!U.call(t,f)||u&&("length"==f||o&&("offset"==f||"parent"==f)||i&&("buffer"==f||"byteLength"==f||"byteOffset"==f)||P(f,a))||c.push(f);return c}var R=Object.prototype;var V=function(t,e){return function(r){return t(e(r))}}(Object.keys,Object),W=V,J=Object.prototype.hasOwnProperty;function G(t){if(r=(e=t)&&e.constructor,e!==("function"==typeof r&&r.prototype||R))return W(t);var e,r,n=[];for(var o in Object(t))J.call(t,o)&&"constructor"!=o&&n.push(o);return n}function H(t){var e=typeof t;return null!=t&&("object"==e||"function"==e)}var K="[object AsyncFunction]",Q="[object Function]",X="[object GeneratorFunction]",Y="[object Proxy]";function Z(t){if(!H(t))return!1;var e=b(t);return e==Q||e==X||e==K||e==Y}function tt(t){return null!=(e=t)&&k(e.length)&&!Z(e)?L(t):G(t);var e}var et="[object Symbol]";function rt(t){return"symbol"==typeof t||y(t)&&b(t)==et}var nt=/\.|\[(?:[^[\]]*|(["'])(?:(?!\1)[^\\]|\\.)*?\1)\]/,ot=/^\w*$/;var it,ut=i["__core-js_shared__"],ct=(it=/[^.]+$/.exec(ut&&ut.keys&&ut.keys.IE_PROTO||""))?"Symbol(src)_1."+it:"";var at=Function.prototype.toString;function ft(t){if(null!=t){try{return at.call(t)}catch(t){}try{return t+""}catch(t){}}return""}var st=/^\[object .+?Constructor\]$/,lt=Function.prototype,pt=Object.prototype,ht=lt.toString,vt=pt.hasOwnProperty,bt=RegExp("^"+ht.call(vt).replace(/[\\^$.*+?()[\]{}|]/g,"\\$&").replace(/hasOwnProperty|(function).*?(?=\\\()| for .+?(?=\\\])/g,"$1.*?")+"$");function yt(t){return!(!H(t)||function(t){return!!ct&&ct in t}(t))&&(Z(t)?bt:st).test(ft(t))}function _t(t,e){var r=function(t,e){return null==t?void 0:t[e]}(t,e);return yt(r)?r:void 0}var dt=_t(Object,"create");var gt=Object.prototype.hasOwnProperty;var jt=Object.prototype.hasOwnProperty;function Ot(t){var e=-1,r=null==t?0:t.length;for(this.clear();++e<r;){var n=t[e];this.set(n[0],n[1])}}function mt(t,e){return t===e||t!=t&&e!=e}function wt(t,e){for(var r=t.length;r--;)if(mt(t[r][0],e))return r;return-1}Ot.prototype.clear=function(){this.__data__=dt?dt(null):{},this.size=0},Ot.prototype.delete=function(t){var e=this.has(t)&&delete this.__data__[t];return this.size-=e?1:0,e},Ot.prototype.get=function(t){var e=this.__data__;if(dt){var r=e[t];return"__lodash_hash_undefined__"===r?void 0:r}return gt.call(e,t)?e[t]:void 0},Ot.prototype.has=function(t){var e=this.__data__;return dt?void 0!==e[t]:jt.call(e,t)},Ot.prototype.set=function(t,e){var r=this.__data__;return this.size+=this.has(t)?0:1,r[t]=dt&&void 0===e?"__lodash_hash_undefined__":e,this};var St=Array.prototype.splice;function At(t){var e=-1,r=null==t?0:t.length;for(this.clear();++e<r;){var n=t[e];this.set(n[0],n[1])}}At.prototype.clear=function(){this.__data__=[],this.size=0},At.prototype.delete=function(t){var e=this.__data__,r=wt(e,t);return!(r<0)&&(r==e.length-1?e.pop():St.call(e,r,1),--this.size,!0)},At.prototype.get=function(t){var e=this.__data__,r=wt(e,t);return r<0?void 0:e[r][1]},At.prototype.has=function(t){return wt(this.__data__,t)>-1},At.prototype.set=function(t,e){var r=this.__data__,n=wt(r,t);return n<0?(++this.size,r.push([t,e])):r[n][1]=e,this};var zt=_t(i,"Map");function xt(t,e){var r,n,o=t.__data__;return("string"==(n=typeof(r=e))||"number"==n||"symbol"==n||"boolean"==n?"__proto__"!==r:null===r)?o["string"==typeof e?"string":"hash"]:o.map}function $t(t){var e=-1,r=null==t?0:t.length;for(this.clear();++e<r;){var n=t[e];this.set(n[0],n[1])}}$t.prototype.clear=function(){this.size=0,this.__data__={hash:new Ot,map:new(zt||At),string:new Ot}},$t.prototype.delete=function(t){var e=xt(this,t).delete(t);return this.size-=e?1:0,e},$t.prototype.get=function(t){return xt(this,t).get(t)},$t.prototype.has=function(t){return xt(this,t).has(t)},$t.prototype.set=function(t,e){var r=xt(this,t),n=r.size;return r.set(t,e),this.size+=r.size==n?0:1,this};var Ft="Expected a function";function Pt(t,e){if("function"!=typeof t||null!=e&&"function"!=typeof e)throw new TypeError(Ft);var r=function(){var n=arguments,o=e?e.apply(this,n):n[0],i=r.cache;if(i.has(o))return i.get(o);var u=t.apply(this,n);return r.cache=i.set(o,u)||i,u};return r.cache=new(Pt.Cache||$t),r}Pt.Cache=$t;var Ct=/[^.[\]]+|\[(?:(-?\d+(?:\.\d+)?)|(["'])((?:(?!\2)[^\\]|\\.)*?)\2)\]|(?=(?:\.|\[\])(?:\.|\[\]|$))/g,kt=/\\(\\)?/g,Et=function(t){var e=Pt(t,(function(t){return 500===r.size&&r.clear(),t})),r=e.cache;return e}((function(t){var e=[];return 46===t.charCodeAt(0)&&e.push(""),t.replace(Ct,(function(t,r,n,o){e.push(n?o.replace(kt,"$1"):r||t)})),e})),Nt=Et;var Tt=1/0,qt=u?u.prototype:void 0,Mt=qt?qt.toString:void 0;function Bt(t){if("string"==typeof t)return t;if(w(t))return function(t,e){for(var r=-1,n=null==t?0:t.length,o=Array(n);++r<n;)o[r]=e(t[r],r,t);return o}(t,Bt)+"";if(rt(t))return Mt?Mt.call(t):"";var e=t+"";return"0"==e&&1/t==-Tt?"-0":e}function Dt(t){return null==t?"":Bt(t)}function It(t,e){return w(t)?t:function(t,e){if(w(t))return!1;var r=typeof t;return!("number"!=r&&"symbol"!=r&&"boolean"!=r&&null!=t&&!rt(t))||ot.test(t)||!nt.test(t)||null!=e&&t in Object(e)}(t,e)?[t]:Nt(Dt(t))}var Ut=1/0;function Lt(t){if("string"==typeof t||rt(t))return t;var e=t+"";return"0"==e&&1/t==-Ut?"-0":e}function Rt(t,e,r){var n=null==t?void 0:function(t,e){for(var r=0,n=(e=It(e,t)).length;null!=t&&r<n;)t=t[Lt(e[r++])];return r&&r==n?t:void 0}(t,e);return void 0===n?r:n}function Vt(t){return"[object Object]"===Object.prototype.toString.call(t)}function Wt(t){return"[object String]"===Object.prototype.toString.call(t)}function Jt(t){return!(!Wt(t)||""===t)}function Gt(t){return t!=t}function Ht(t){let e=!1;if(Jt(t))e=!isNaN(Number(t));else if(function(t){return"[object Number]"===Object.prototype.toString.call(t)}(t)){if(Gt(t))return!1;e=!0}return e}function Kt(t){let e=Object.prototype.toString.call(t);return"[object Function]"===e||"[object AsyncFunction]"===e}var Qt=/\s/;var Xt=/^\s+/;function Yt(t){return t?t.slice(0,function(t){for(var e=t.length;e--&&Qt.test(t.charAt(e)););return e}(t)+1).replace(Xt,""):t}var Zt=NaN,te=/^[-+]0x[0-9a-f]+$/i,ee=/^0b[01]+$/i,re=/^0o[0-7]+$/i,ne=parseInt;function oe(t){if("number"==typeof t)return t;if(rt(t))return Zt;if(H(t)){var e="function"==typeof t.valueOf?t.valueOf():t;t=H(e)?e+"":e}if("string"!=typeof t)return 0===t?t:+t;t=Yt(t);var r=ee.test(t);return r||re.test(t)?ne(t.slice(2),r?2:8):te.test(t)?Zt:+t}var ie=1/0,ue=17976931348623157e292;function ce(t){return t?(t=oe(t))===ie||t===-ie?(t<0?-1:1)*ue:t==t?t:0:0===t?t:0}function ae(t){var e=ce(t),r=e%1;return e==e?r?e-r:e:0}function fe(t){if(!Ht(t))return 0;return ce(t)}function se(t){return!!Ht(t)&&(t=fe(t),"number"==typeof(e=t)&&e==ae(e));var e}var le=i.isFinite,pe=Math.min;var he=function(t){var e=Math[t];return function(t,r){if(t=oe(t),(r=null==r?0:pe(ae(r),292))&&le(t)){var n=(Dt(t)+"e").split("e");return+((n=(Dt(e(n[0]+"e"+(+n[1]+r)))+"e").split("e"))[0]+"e"+(+n[1]-r))}return e(t)}}("round"),ve=he;function be(t){if(!Ht(t))return 0;t=fe(t);let e=ve(t);return"0"===String(e)?0:e}function ye(t){if(!se(t))return!1;return be(t)>0}function _e(t){var e=this.__data__=new At(t);this.size=e.size}_e.prototype.clear=function(){this.__data__=new At,this.size=0},_e.prototype.delete=function(t){var e=this.__data__,r=e.delete(t);return this.size=e.size,r},_e.prototype.get=function(t){return this.__data__.get(t)},_e.prototype.has=function(t){return this.__data__.has(t)},_e.prototype.set=function(t,e){var r=this.__data__;if(r instanceof At){var n=r.__data__;if(!zt||n.length<199)return n.push([t,e]),this.size=++r.size,this;r=this.__data__=new $t(n)}return r.set(t,e),this.size=r.size,this};function de(t){var e=-1,r=null==t?0:t.length;for(this.__data__=new $t;++e<r;)this.add(t[e])}function ge(t,e){for(var r=-1,n=null==t?0:t.length;++r<n;)if(e(t[r],r,t))return!0;return!1}de.prototype.add=de.prototype.push=function(t){return this.__data__.set(t,"__lodash_hash_undefined__"),this},de.prototype.has=function(t){return this.__data__.has(t)};var je=1,Oe=2;function me(t,e,r,n,o,i){var u=r&je,c=t.length,a=e.length;if(c!=a&&!(u&&a>c))return!1;var f=i.get(t),s=i.get(e);if(f&&s)return f==e&&s==t;var l=-1,p=!0,h=r&Oe?new de:void 0;for(i.set(t,e),i.set(e,t);++l<c;){var v=t[l],b=e[l];if(n)var y=u?n(b,v,l,e,t,i):n(v,b,l,t,e,i);if(void 0!==y){if(y)continue;p=!1;break}if(h){if(!ge(e,(function(t,e){if(u=e,!h.has(u)&&(v===t||o(v,t,r,n,i)))return h.push(e);var u}))){p=!1;break}}else if(v!==b&&!o(v,b,r,n,i)){p=!1;break}}return i.delete(t),i.delete(e),p}var we=i.Uint8Array;function Se(t){var e=-1,r=Array(t.size);return t.forEach((function(t,n){r[++e]=[n,t]})),r}function Ae(t){var e=-1,r=Array(t.size);return t.forEach((function(t){r[++e]=t})),r}var ze=1,xe=2,$e="[object Boolean]",Fe="[object Date]",Pe="[object Error]",Ce="[object Map]",ke="[object Number]",Ee="[object RegExp]",Ne="[object Set]",Te="[object String]",qe="[object Symbol]",Me="[object ArrayBuffer]",Be="[object DataView]",De=u?u.prototype:void 0,Ie=De?De.valueOf:void 0;var Ue=Object.prototype.propertyIsEnumerable,Le=Object.getOwnPropertySymbols,Re=Le?function(t){return null==t?[]:(t=Object(t),function(t,e){for(var r=-1,n=null==t?0:t.length,o=0,i=[];++r<n;){var u=t[r];e(u,r,t)&&(i[o++]=u)}return i}(Le(t),(function(e){return Ue.call(t,e)})))}:function(){return[]};function Ve(t){return function(t,e,r){var n=e(t);return w(t)?n:function(t,e){for(var r=-1,n=e.length,o=t.length;++r<n;)t[o+r]=e[r];return t}(n,r(t))}(t,tt,Re)}var We=1,Je=Object.prototype.hasOwnProperty;var Ge=_t(i,"DataView"),He=_t(i,"Promise"),Ke=_t(i,"Set"),Qe=_t(i,"WeakMap"),Xe="[object Map]",Ye="[object Promise]",Ze="[object Set]",tr="[object WeakMap]",er="[object DataView]",rr=ft(Ge),nr=ft(zt),or=ft(He),ir=ft(Ke),ur=ft(Qe),cr=b;(Ge&&cr(new Ge(new ArrayBuffer(1)))!=er||zt&&cr(new zt)!=Xe||He&&cr(He.resolve())!=Ye||Ke&&cr(new Ke)!=Ze||Qe&&cr(new Qe)!=tr)&&(cr=function(t){var e=b(t),r="[object Object]"==e?t.constructor:void 0,n=r?ft(r):"";if(n)switch(n){case rr:return er;case nr:return Xe;case or:return Ye;case ir:return Ze;case ur:return tr}return e});var ar=cr,fr=1,sr="[object Arguments]",lr="[object Array]",pr="[object Object]",hr=Object.prototype.hasOwnProperty;function vr(t,e,r,n,o,i){var u=w(t),c=w(e),a=u?lr:ar(t),f=c?lr:ar(e),s=(a=a==sr?pr:a)==pr,l=(f=f==sr?pr:f)==pr,p=a==f;if(p&&x(t)){if(!x(e))return!1;u=!0,s=!1}if(p&&!s)return i||(i=new _e),u||I(t)?me(t,e,r,n,o,i):function(t,e,r,n,o,i,u){switch(r){case Be:if(t.byteLength!=e.byteLength||t.byteOffset!=e.byteOffset)return!1;t=t.buffer,e=e.buffer;case Me:return!(t.byteLength!=e.byteLength||!i(new we(t),new we(e)));case $e:case Fe:case ke:return mt(+t,+e);case Pe:return t.name==e.name&&t.message==e.message;case Ee:case Te:return t==e+"";case Ce:var c=Se;case Ne:var a=n&ze;if(c||(c=Ae),t.size!=e.size&&!a)return!1;var f=u.get(t);if(f)return f==e;n|=xe,u.set(t,e);var s=me(c(t),c(e),n,o,i,u);return u.delete(t),s;case qe:if(Ie)return Ie.call(t)==Ie.call(e)}return!1}(t,e,a,r,n,o,i);if(!(r&fr)){var h=s&&hr.call(t,"__wrapped__"),v=l&&hr.call(e,"__wrapped__");if(h||v){var b=h?t.value():t,y=v?e.value():e;return i||(i=new _e),o(b,y,r,n,i)}}return!!p&&(i||(i=new _e),function(t,e,r,n,o,i){var u=r&We,c=Ve(t),a=c.length;if(a!=Ve(e).length&&!u)return!1;for(var f=a;f--;){var s=c[f];if(!(u?s in e:Je.call(e,s)))return!1}var l=i.get(t),p=i.get(e);if(l&&p)return l==e&&p==t;var h=!0;i.set(t,e),i.set(e,t);for(var v=u;++f<a;){var b=t[s=c[f]],y=e[s];if(n)var _=u?n(y,b,s,e,t,i):n(b,y,s,t,e,i);if(!(void 0===_?b===y||o(b,y,r,n,i):_)){h=!1;break}v||(v="constructor"==s)}if(h&&!v){var d=t.constructor,g=e.constructor;d==g||!("constructor"in t)||!("constructor"in e)||"function"==typeof d&&d instanceof d&&"function"==typeof g&&g instanceof g||(h=!1)}return i.delete(t),i.delete(e),h}(t,e,r,n,o,i))}function br(t,e,r,n,o){return t===e||(null==t||null==e||!y(t)&&!y(e)?t!=t&&e!=e:vr(t,e,r,n,br,o))}function yr(t){return"[object Array]"===Object.prototype.toString.call(t)}function _r(t){return!!function(t){return"[object Undefined]"===Object.prototype.toString.call(t)}(t)||(!!function(t){return"[object Null]"===Object.prototype.toString.call(t)}(t)||(!!function(t){if(Vt(t)){for(let e in t)return!1;return!0}return!1}(t)||(!!function(t){return!(!Wt(t)||""!==t)}(t)||(!!function(t){return!!yr(t)&&0===t.length}(t)||!!Gt(t)))))}return function(n){let o=Rt(n,"port");ye(o)||(o=8080),o=be(o);let i=Rt(n,"portWeb");ye(i)||(i=o+10),i=be(i);let u=[];var c,a;function f(t){let e=function(){let t,e,r=new Promise((function(){t=arguments[0],e=arguments[1]}));return r.resolve=t,r.reject=e,r}();return Kt(n.authenticate)?n.authenticate(t).then((function(t){e.resolve(t)})):e.resolve(!0),e}a="funcs",Vt(c=n)&&(Jt(a)||Ht(a))&&a in c&&(u=tt(n.funcs));let s=t(),l=[];s.on("client",(function(t){l.push(t),Kt(n.onClientChange)&&n.onClientChange(l,n)})),s.on("clientDisconnected",(function(t){l=l.filter((function(e){return e!==t})),Kt(n.onClientChange)&&n.onClientChange(l,n)})),s.on("subscribe",(function(t,e){})),s.on("unsubscribe",(function(t,e){})),s.on("publish",(function(t,e){let r=t.topic,o=t.payload.toString("utf8");if(!(o.indexOf('"output"')>=0)&&r.indexOf("topic|")>=0){(async function(t,e){let r=Rt(e,"token","");if(await f(r)){let t=Rt(e,"func",""),o=Rt(e,"input");if("getFuncs"===t)Kt(n.filterFuncs)&&(u=await n.filterFuncs(r,u)),e.output={sys:"sys",funcs:u};else if(function(t,e){function r(t){return yr(t)?t:[t]}if(_r(t))return!1;if(0===(t=r(t)).length)return!1;if(_r(t))return!1;if(0===(e=r(e)).length)return!1;for(let r=0;r<t.length;r++)for(let i=0;i<e.length;i++)if(n=t[r],o=e[i],br(n,o))return!0;var n,o;return!1}(u,t))if(Kt(n.funcs[t])){let r=await n.funcs[t](o);e.output=r}else e.output={err:`${t} is not a function`};else e.output={err:`can not find: ${t}`}}else e.output={err:`can not authenticate token: ${r}`};delete e.input,function(t,e){let r={topic:t,payload:JSON.stringify(e),qos:2,retain:!1};s.publish(r,(function(t){t&&console.log("publish: error:",t)}))}(t,e)})(r,function(t){if(!Jt(t))return{};let e={};try{e=JSON.parse(t)}catch(t){e={}}return e}(o)).catch((t=>{console.log("execFunction catch",t)}))}})),r.createServer(s.handle).listen(o,(function(){s.authenticate=async function(t,e,r,n){let o=e;n(null,await f(o))},console.log(`Server running for node at: mqtt://localhost:${o}`)})),e.createServer(s,{ws:!0}).listen(i,(function(){console.log(`Server running for browser at: mqtt://localhost:${i}`)}))}})); //# sourceMappingURL=w-comor-mqtt-server.umd.js.map