w-comor-socketio
Version:
A http and websocket communicator in nodejs and browser. Mapping functions from nodejs to other end points, like a simple RPC.
8 lines (7 loc) • 17.4 kB
JavaScript
/*!
* w-comor-socketio-server v1.0.29
* (c) 2018-2021 yuda-lyu(semisphere)
* Released under the MIT License.
*/
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e(require("@hapi/hapi"),require("@hapi/inert"),require("socket.io")):"function"==typeof define&&define.amd?define(["@hapi/hapi","@hapi/inert","socket.io"],e):(t="undefined"!=typeof globalThis?globalThis:t||self)["w-comor-socketio-server"]=e(t["@hapi/hapi"],t["@hapi/inert"],t.socket.io)}(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,a=Object.prototype,c=a.hasOwnProperty,f=a.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 y(t){return null==t?void 0===t?h:p:v&&v in Object(t)?function(t){var e=c.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 b(t){return null!=t&&"object"==typeof t}function d(t){return b(t)&&"[object Arguments]"==y(t)}var _=Object.prototype,j=_.hasOwnProperty,g=_.propertyIsEnumerable,m=d(function(){return arguments}())?d:function(t){return b(t)&&j.call(t,"callee")&&!g.call(t,"callee")},O=m,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,k=(z?z.isBuffer:void 0)||function(){return!1},x=9007199254740991,P=/^(?:0|[1-9]\d*)$/;function $(t,e){var r=typeof t;return!!(e=null==e?x:e)&&("number"==r||"symbol"!=r&&P.test(t))&&t>-1&&t%1==0&&t<e}var E=9007199254740991;function F(t){return"number"==typeof t&&t>-1&&t%1==0&&t<=E}var T={};T["[object Float32Array]"]=T["[object Float64Array]"]=T["[object Int8Array]"]=T["[object Int16Array]"]=T["[object Int32Array]"]=T["[object Uint8Array]"]=T["[object Uint8ClampedArray]"]=T["[object Uint16Array]"]=T["[object Uint32Array]"]=!0,T["[object Arguments]"]=T["[object Array]"]=T["[object ArrayBuffer]"]=T["[object Boolean]"]=T["[object DataView]"]=T["[object Date]"]=T["[object Error]"]=T["[object Function]"]=T["[object Map]"]=T["[object Number]"]=T["[object Object]"]=T["[object RegExp]"]=T["[object Set]"]=T["[object String]"]=T["[object WeakMap]"]=!1;var C,N="object"==typeof exports&&exports&&!exports.nodeType&&exports,M=N&&"object"==typeof module&&module&&!module.nodeType&&module,I=M&&M.exports===N&&n.process,B=function(){try{var t=M&&M.require&&M.require("util").types;return t||I&&I.binding&&I.binding("util")}catch(t){}}(),U=B&&B.isTypedArray,D=U?(C=U,function(t){return C(t)}):function(t){return b(t)&&F(t.length)&&!!T[y(t)]},q=Object.prototype.hasOwnProperty;function L(t,e){var r=w(t),n=!r&&O(t),o=!r&&!n&&k(t),i=!r&&!n&&!o&&D(t),u=r||n||o||i,a=u?function(t,e){for(var r=-1,n=Array(t);++r<t;)n[r]=e(r);return n}(t.length,String):[],c=a.length;for(var f in t)!e&&!q.call(t,f)||u&&("length"==f||o&&("offset"==f||"parent"==f)||i&&("buffer"==f||"byteLength"==f||"byteOffset"==f)||$(f,c))||a.push(f);return a}var G=Object.prototype;var R=function(t,e){return function(r){return t(e(r))}}(Object.keys,Object),V=R,H=Object.prototype.hasOwnProperty;function W(t){if(r=(e=t)&&e.constructor,e!==("function"==typeof r&&r.prototype||G))return V(t);var e,r,n=[];for(var o in Object(t))H.call(t,o)&&"constructor"!=o&&n.push(o);return n}function J(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(!J(t))return!1;var e=y(t);return e==Q||e==X||e==K||e==Y}function tt(t){return null!=(e=t)&&F(e.length)&&!Z(e)?L(t):W(t);var e}var et="[object Symbol]";function rt(t){return"symbol"==typeof t||b(t)&&y(t)==et}var nt=/\.|\[(?:[^[\]]*|(["'])(?:(?!\1)[^\\]|\\.)*?\1)\]/,ot=/^\w*$/;var it,ut=i["__core-js_shared__"],at=(it=/[^.]+$/.exec(ut&&ut.keys&&ut.keys.IE_PROTO||""))?"Symbol(src)_1."+it:"";var ct=Function.prototype.toString;function ft(t){if(null!=t){try{return ct.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,yt=RegExp("^"+ht.call(vt).replace(/[\\^$.*+?()[\]{}|]/g,"\\$&").replace(/hasOwnProperty|(function).*?(?=\\\()| for .+?(?=\\\])/g,"$1.*?")+"$");function bt(t){return!(!J(t)||function(t){return!!at&&at in t}(t))&&(Z(t)?yt:st).test(ft(t))}function dt(t,e){var r=function(t,e){return null==t?void 0:t[e]}(t,e);return bt(r)?r:void 0}var _t=dt(Object,"create");var jt=Object.prototype.hasOwnProperty;var gt=Object.prototype.hasOwnProperty;function mt(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 Ot(t,e){return t===e||t!=t&&e!=e}function wt(t,e){for(var r=t.length;r--;)if(Ot(t[r][0],e))return r;return-1}mt.prototype.clear=function(){this.__data__=_t?_t(null):{},this.size=0},mt.prototype.delete=function(t){var e=this.has(t)&&delete this.__data__[t];return this.size-=e?1:0,e},mt.prototype.get=function(t){var e=this.__data__;if(_t){var r=e[t];return"__lodash_hash_undefined__"===r?void 0:r}return jt.call(e,t)?e[t]:void 0},mt.prototype.has=function(t){var e=this.__data__;return _t?void 0!==e[t]:gt.call(e,t)},mt.prototype.set=function(t,e){var r=this.__data__;return this.size+=this.has(t)?0:1,r[t]=_t&&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=dt(i,"Map");function kt(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 xt(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])}}xt.prototype.clear=function(){this.size=0,this.__data__={hash:new mt,map:new(zt||At),string:new mt}},xt.prototype.delete=function(t){var e=kt(this,t).delete(t);return this.size-=e?1:0,e},xt.prototype.get=function(t){return kt(this,t).get(t)},xt.prototype.has=function(t){return kt(this,t).has(t)},xt.prototype.set=function(t,e){var r=kt(this,t),n=r.size;return r.set(t,e),this.size+=r.size==n?0:1,this};var Pt="Expected a function";function $t(t,e){if("function"!=typeof t||null!=e&&"function"!=typeof e)throw new TypeError(Pt);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($t.Cache||xt),r}$t.Cache=xt;var Et=/[^.[\]]+|\[(?:(-?\d+(?:\.\d+)?)|(["'])((?:(?!\2)[^\\]|\\.)*?)\2)\]|(?=(?:\.|\[\])(?:\.|\[\]|$))/g,Ft=/\\(\\)?/g,Tt=function(t){var e=$t(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(Et,(function(t,r,n,o){e.push(n?o.replace(Ft,"$1"):r||t)})),e})),Ct=Tt;var Nt=1/0,Mt=u?u.prototype:void 0,It=Mt?Mt.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 It?It.call(t):"";var e=t+"";return"0"==e&&1/t==-Nt?"-0":e}function Ut(t){return null==t?"":Bt(t)}function Dt(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]:Ct(Ut(t))}var qt=1/0;function Lt(t){if("string"==typeof t||rt(t))return t;var e=t+"";return"0"==e&&1/t==-qt?"-0":e}function Gt(t,e,r){var n=null==t?void 0:function(t,e){for(var r=0,n=(e=Dt(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 Rt(t){return"[object Object]"===Object.prototype.toString.call(t)}function Vt(t){return"[object String]"===Object.prototype.toString.call(t)}function Ht(t){return!(!Vt(t)||""===t)}function Wt(t){return t!=t}function Jt(t){let e=!1;if(Ht(t))e=!isNaN(Number(t));else if(function(t){return"[object Number]"===Object.prototype.toString.call(t)}(t)){if(Wt(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(J(t)){var e="function"==typeof t.valueOf?t.valueOf():t;t=J(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 ae(t){return t?(t=oe(t))===ie||t===-ie?(t<0?-1:1)*ue:t==t?t:0:0===t?t:0}function ce(t){var e=ae(t),r=e%1;return e==e?r?e-r:e:0}function fe(t){if(!Jt(t))return 0;return ae(t)}function se(t){return!!Jt(t)&&(t=fe(t),"number"==typeof(e=t)&&e==ce(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(ce(r),292))&&le(t)){var n=(Ut(t)+"e").split("e");return+((n=(Ut(e(n[0]+"e"+(+n[1]+r)))+"e").split("e"))[0]+"e"+(+n[1]-r))}return e(t)}}("round"),ve=he;function ye(t){if(!Jt(t))return 0;t=fe(t);let e=ve(t);return"0"===String(e)?0:e}function be(t){var e=this.__data__=new At(t);this.size=e.size}be.prototype.clear=function(){this.__data__=new At,this.size=0},be.prototype.delete=function(t){var e=this.__data__,r=e.delete(t);return this.size=e.size,r},be.prototype.get=function(t){return this.__data__.get(t)},be.prototype.has=function(t){return this.__data__.has(t)},be.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 xt(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 xt;++e<r;)this.add(t[e])}function _e(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,ge=2;function me(t,e,r,n,o,i){var u=r&je,a=t.length,c=e.length;if(a!=c&&!(u&&c>a))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&ge?new de:void 0;for(i.set(t,e),i.set(e,t);++l<a;){var v=t[l],y=e[l];if(n)var b=u?n(y,v,l,e,t,i):n(v,y,l,t,e,i);if(void 0!==b){if(b)continue;p=!1;break}if(h){if(!_e(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!==y&&!o(v,y,r,n,i)){p=!1;break}}return i.delete(t),i.delete(e),p}var Oe=i.Uint8Array;function we(t){var e=-1,r=Array(t.size);return t.forEach((function(t,n){r[++e]=[n,t]})),r}function Se(t){var e=-1,r=Array(t.size);return t.forEach((function(t){r[++e]=t})),r}var Ae=1,ze=2,ke="[object Boolean]",xe="[object Date]",Pe="[object Error]",$e="[object Map]",Ee="[object Number]",Fe="[object RegExp]",Te="[object Set]",Ce="[object String]",Ne="[object Symbol]",Me="[object ArrayBuffer]",Ie="[object DataView]",Be=u?u.prototype:void 0,Ue=Be?Be.valueOf:void 0;var De=Object.prototype.propertyIsEnumerable,qe=Object.getOwnPropertySymbols,Le=qe?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}(qe(t),(function(e){return De.call(t,e)})))}:function(){return[]};function Ge(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,Le)}var Re=1,Ve=Object.prototype.hasOwnProperty;var He=dt(i,"DataView"),We=dt(i,"Promise"),Je=dt(i,"Set"),Ke=dt(i,"WeakMap"),Qe="[object Map]",Xe="[object Promise]",Ye="[object Set]",Ze="[object WeakMap]",tr="[object DataView]",er=ft(He),rr=ft(zt),nr=ft(We),or=ft(Je),ir=ft(Ke),ur=y;(He&&ur(new He(new ArrayBuffer(1)))!=tr||zt&&ur(new zt)!=Qe||We&&ur(We.resolve())!=Xe||Je&&ur(new Je)!=Ye||Ke&&ur(new Ke)!=Ze)&&(ur=function(t){var e=y(t),r="[object Object]"==e?t.constructor:void 0,n=r?ft(r):"";if(n)switch(n){case er:return tr;case rr:return Qe;case nr:return Xe;case or:return Ye;case ir:return Ze}return e});var ar=ur,cr=1,fr="[object Arguments]",sr="[object Array]",lr="[object Object]",pr=Object.prototype.hasOwnProperty;function hr(t,e,r,n,o,i){var u=w(t),a=w(e),c=u?sr:ar(t),f=a?sr:ar(e),s=(c=c==fr?lr:c)==lr,l=(f=f==fr?lr:f)==lr,p=c==f;if(p&&k(t)){if(!k(e))return!1;u=!0,s=!1}if(p&&!s)return i||(i=new be),u||D(t)?me(t,e,r,n,o,i):function(t,e,r,n,o,i,u){switch(r){case Ie: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 Oe(t),new Oe(e)));case ke:case xe:case Ee:return Ot(+t,+e);case Pe:return t.name==e.name&&t.message==e.message;case Fe:case Ce:return t==e+"";case $e:var a=we;case Te:var c=n&Ae;if(a||(a=Se),t.size!=e.size&&!c)return!1;var f=u.get(t);if(f)return f==e;n|=ze,u.set(t,e);var s=me(a(t),a(e),n,o,i,u);return u.delete(t),s;case Ne:if(Ue)return Ue.call(t)==Ue.call(e)}return!1}(t,e,c,r,n,o,i);if(!(r&cr)){var h=s&&pr.call(t,"__wrapped__"),v=l&&pr.call(e,"__wrapped__");if(h||v){var y=h?t.value():t,b=v?e.value():e;return i||(i=new be),o(y,b,r,n,i)}}return!!p&&(i||(i=new be),function(t,e,r,n,o,i){var u=r&Re,a=Ge(t),c=a.length;if(c!=Ge(e).length&&!u)return!1;for(var f=c;f--;){var s=a[f];if(!(u?s in e:Ve.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<c;){var y=t[s=a[f]],b=e[s];if(n)var d=u?n(b,y,s,e,t,i):n(y,b,s,t,e,i);if(!(void 0===d?y===b||o(y,b,r,n,i):d)){h=!1;break}v||(v="constructor"==s)}if(h&&!v){var _=t.constructor,j=e.constructor;_==j||!("constructor"in t)||!("constructor"in e)||"function"==typeof _&&_ instanceof _&&"function"==typeof j&&j instanceof j||(h=!1)}return i.delete(t),i.delete(e),h}(t,e,r,n,o,i))}function vr(t,e,r,n,o){return t===e||(null==t||null==e||!b(t)&&!b(e)?t!=t&&e!=e:hr(t,e,r,n,vr,o))}function yr(t){return"[object Array]"===Object.prototype.toString.call(t)}function br(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(Rt(t)){for(let e in t)return!1;return!0}return!1}(t)||(!!function(t){return!(!Vt(t)||""!==t)}(t)||(!!function(t){return!!yr(t)&&0===t.length}(t)||!!Wt(t)))))}let dr=r.Server;return function(r){let n=Gt(r,"port");var o;se(o=n)&&ye(o)>0||(n=8080),n=ye(n);let i=Gt(r,"pathPolling"),u=[];var a,c;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(r.authenticate)?r.authenticate(t).then((function(t){e.resolve(t)})):e.resolve(!0),e}let s;c="funcs",Rt(a=r)&&(Ht(c)||Jt(c))&&c in a&&(u=tt(r.funcs)),s=r.serverHapi?r.serverHapi:t.server({port:r.port,routes:{cors:!0}});let l=null,p={};Ht(i)&&(p={path:i});try{l=new dr(s.listener,p)}catch(t){return console.log("create SocketIO catch:",t),null}let h=[];l.on("connect",(function(t){async function e(e){let n=Gt(e,"token","");if(await f(n)){let t=Gt(e,"func",""),o=Gt(e,"input");if("getFuncs"===t)Kt(r.filterFuncs)&&(u=await r.filterFuncs(n,u)),e.output={sys:"sys",funcs:u};else if(function(t,e){function r(t){return yr(t)?t:[t]}if(br(t))return!1;if(0===(t=r(t)).length)return!1;if(br(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],vr(n,o))return!0;var n,o;return!1}(u,t)){let n=await r.funcs[t](o);e.output=n}else e.output={err:`can not find: ${t}`}}else e.output={err:`can not authenticate token: ${n}`};delete e.input;try{t.send(JSON.stringify(e),(function(t){t&&console.log("Server: send output error:",t)}))}catch(t){console.log("Server: send output catch:",t)}}h.push(t.id),Kt(r.onClientChange)&&r.onClientChange(h,r),t.on("message",(function(t){let r=function(t){if(!Ht(t))return{};let e={};try{e=JSON.parse(t)}catch(t){e={}}return e}(t);e(r)})),t.on("disconnect",(function(e){h=h.filter((function(e){return e!==t.id})),Kt(r.onClientChange)&&r.onClientChange(h,r)}))})),async function(){if(!r.serverHapi){await s.register(e);let t=[{method:"GET",path:"/web.html",handler:{file:{path:"./web.html"}}},{method:"GET",path:"/dist/w-comor-socketio-client.umd.js",handler:{file:{path:"./dist/w-comor-socketio-client.umd.js"}}},{method:"GET",path:"/dist/w-comor-socketio-client.umd.js.map",handler:{file:{path:"./dist/w-comor-socketio-client.umd.js.map"}}}];s.route(t)}await s.start(),console.log(`Server running at: ${s.info.uri}`)}()}}));
//# sourceMappingURL=w-comor-socketio-server.umd.js.map