wspromisify 2.12.0 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bundle.cjs +1 -1
- package/dist/bundle.d.ts +70 -25
- package/dist/bundle.mjs +1 -1
- package/package.json +5 -4
- package/pnpm-workspace.yaml +2 -0
- package/tsconfig.json +1 -1
package/dist/bundle.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});const e=Symbol("Placeholder"),t=t=>{let n=0;for(const s of t)s!==e&&n++;return n},n=(t,n)=>{const s=t.length,i=t.slice(),o=n.length;let r=o,c=0;for(;r&&c<s;c++)i[c]===e&&(i[c]=n[o-r],r--);for(c=s;r;c++,r--)i[c]=n[o-r];return i},s=(e,i,o)=>{const r=e.length-i.length-t(o);if(r<1)return e(...n(i,o));{const t=(...t)=>s(e,n(i,o),t);return t.$args_left=r,t}},i=e=>(...n)=>e.length>t(n)?s(e,[],n):e(...n);function o(t){return function(n,...s){return s.length>0?n===e?(t=>function(n){return n===e?t:t(n)})((e=>t(e,s[0]))):t(n,s[0]):e=>t(n,e)}}function r(e){return i(e)}const c=e=>e.length,l=/^(.*?)(8|16|32|64)(Clamped)?Array$/,u=void 0,a=1/0,h=e=>typeof e,d=e=>null===e,f=e=>"number"==h(e);const p=e=>d(e)||(e=>e===u)(e),g={u:"U",b:"B",n:"N",s:"S",f:"F"},m=Symbol(),y=e=>{const t=h(e);return"object"===t?d(e)?"Null":e.constructor.name:g[t[0]]+t.slice(1)},_=o(((e,t)=>y(t)===e)),w=o(((e,t)=>e===t)),b=o(((e,t)=>{const n=y(e);if(w(n,y(t))&&(w(n,"Object")||w(n,"Array")||(e=>l.test(e))(n))){if(d(e)||d(t))return w(e,t);if(w(e,t))return!0;for(const n of[e,t])for(const s in n)if(!(w(n,t)&&s in e||w(n,e)&&s in t&&b(e[s],t[s])))return!1;return!0}return w(e,t)})),k=o(((e,t)=>(t.push(e),t))),S=r(((e,t,n)=>n.reduce(e,t))),v=o(((e,t)=>{const n=(e=>Array.isArray(e))(t);let s,i;n&&(s=0,i=[]);for(let s in t)e(t[s],s)||(n?i.push(+s):delete t[s]);if(n)for(const e of i)t.splice(e-s++,1);return t})),P=i(((e,t,n,s)=>e(s)?t(s):n(s))),Q=(...t)=>(...n)=>{let s,i=!0;for(let o=c(t)-1;o>-1;o--)i?(i=!1,s=t[o](...n)):s=s===e?t[o]():t[o](s);return s},E=o(((e,t)=>t[e])),R=r(((e,t,n)=>n.slice(e,f(t)?t:a))),A=E(0);R(1,a);const C=o(((e,t)=>t.find(e))),j=e=>()=>e,N=o(((e,t)=>(e(t),t))),W=j(!0),$=j(!1),q=o(((e,t)=>t(...e))),O=e=>(...t)=>{const n=e(...t),s=function(e){return"function"===h(e)}(n);return s&&n.$args_left?O(n):(e=>!e)(n)},T=o(((e,t)=>S(((t,n)=>C((t=>e(n,t)),t)?t:k(n,t)),[],t)));T(b);const x=e=>{let t,n=!1;return function(...s){return n?t:(n=!0,t=e(...s))}},z=(e,t,n)=>c(t)?p(n)?e:Q((s=>s in n?z(e,R(1,a,t),n[s]):e),A)(t):n,B=r(z);B(u),Q(P(b(m),$,W),B(m));const F=r(((e,t,n)=>t(n)&&e(n))),M=O,I=Symbol("Placeholder"),L=e=>{let t=0;for(const n of e)n!==I&&t++;return t},D=(e,t)=>{const n=e.length,s=e.slice(),i=t.length;let o=i,r=0;for(;o&&r<n;r++)s[r]===I&&(s[r]=t[i-o],o--);for(r=n;o;r++,o--)s[r]=t[i-o];return s},U=(e,t,n)=>{const s=e.length-t.length-L(n);if(s<1)return e(...D(t,n));{const i=(...s)=>U(e,D(t,n),s);return i.$args_left=s,i}},J=e=>(...t)=>e.length>L(t)?U(e,[],t):e(...t);function G(e){return function(t,...n){return n.length>0?t===I?(e=>function(t){return t===I?e:e(t)})((t=>e(t,n[0]))):e(t,n[0]):n=>e(t,n)}}function H(e){return J(e)}const K=e=>e.length,V=/^(.*?)(8|16|32|64)(Clamped)?Array$/,X=void 0,Y=1/0,Z=e=>typeof e,ee=e=>null===e,te={u:"U",b:"B",n:"N",s:"S",f:"F"},ne=Symbol(),se=e=>{const t=Z(e);return"object"===t?ee(e)?"Null":e.constructor.name:te[t[0]]+t.slice(1)},ie=G(((e,t)=>se(t)===e)),oe=G(((e,t)=>e===t)),re=G(((e,t)=>{const n=se(e);if(oe(n,se(t))&&(oe(n,"Object")||oe(n,"Array")||(s=n,V.test(s)))){if(ee(e)||ee(t))return oe(e,t);if(oe(e,t))return!0;for(const n of[e,t])for(const s in n)if(!(oe(n,t)&&s in e||oe(n,e)&&s in t&&re(e[s],t[s])))return!1;return!0}var s;return oe(e,t)})),ce=G(((e,t)=>(t.push(e),t))),le=H(((e,t,n)=>n.reduce(e,t))),ue=J(((e,t,n,s)=>e(s)?t(s):n(s))),ae=H(((e,t,n)=>ue(e,t,ye,n))),he=(...e)=>(...t)=>{let n,s=!0;for(let i=K(e)-1;i>-1;i--)s?(s=!1,n=e[i](...t)):n=n===I?e[i]():e[i](n);return n},de=G(((e,t)=>t[e])),fe=H(((e,t,n)=>n.slice(e,(e=>"number"==Z(e))(t)?t:Y))),pe=de(0);fe(1,Y);const ge=G(((e,t)=>t.find(e))),me=e=>()=>e,ye=e=>e,_e=G(((e,t)=>t.split(e))),we=me(!0),be=me(!1),ke=G(((e,t)=>le(((t,n)=>ge((t=>e(n,t)),t)?t:ce(n,t)),[],t)))(re),Se=(e,t,n)=>K(t)?(e=>ee(e)||(e=>e===X)(e))(n)?e:he((s=>s in n?Se(e,fe(1,Y,t),n[s]):e),pe)(t):n,ve=H(Se);ve(X),he(ue(re(ne),be,we),ve(ne));const Pe=G(((e,t)=>t.map(e))),{floor:Qe}=Math,Ee="0123456789abcdefghijklmnopqrstuvwxyz",Re=ie("String"),Ae=ae(Re,_e("")),Ce=he((e=>Object.fromEntries(e)),Pe(((e,t)=>[e,t])),Ae);class je{is_str;delim;abc;abclen;c2pos;standard;setABC(e,t=""){if(this.is_str=Re(e),this.delim=t,!he(oe(K(n=e)),K,ke,Ae)(n))throw new Error("Not all chars are unique!");var n;this.abc=e,this.abclen=K(e),this.standard=!!this.is_str&&Ee.startsWith(e),this.c2pos=Ce(e)}zip(e){const{abc:t,abclen:n,delim:s}=this;let i="",o=!0;for(;e>0;)i=t[e%n]+(o?"":s)+i,e=Qe(e/n),o=!1;return i||"0"}unzip(e){const{standard:t,abclen:n,c2pos:s,delim:i,is_str:o}=this;if("0"===e)return 0;if(t)return parseInt(e,n);const r=o?e:e.split(i),c=K(r);let l=0;for(let e=0;e<c;e++)l+=s[r[e]]*n**(c-e-1);return l}constructor(e,t){t?this.setABC(e,t):this.setABC(e||Ee+"ABCDEFGHIJKLMNOPQRSTUVWXYZ")}}const Ne=new je;Ne.setABC.bind(Ne),Ne.zip.bind(Ne),Ne.unzip.bind(Ne);const We=(()=>{try{return WebSocket||null}catch{return null}})(),$e=(e,t,n)=>e.addEventListener(t,n),qe=(e,t)=>setTimeout(t,e),Oe={data_type:"json",log:()=>null,timer:!1,url:"localhost",timeout:1400,reconnect:2,reconnection_attempts:1/0,reconnect_on_to:!0,max_idle_time:1/0,lazy:!1,socket:null,adapter:(e,t)=>new WebSocket(e,t),encode:(e,t,{server:n})=>JSON.stringify({[n.id_key]:e,[n.data_key]:t}),decode:e=>JSON.parse(e),protocols:[],pipes:[],server:{id_key:"id",data_key:"data"},ping:{interval:55,content:{}}},{random:Te}=Math,xe=new je,ze=q([]),Be=F(_("Number"),M(isNaN)),Fe={_is_ping:!0},Me=e=>e&&clearTimeout(e),Ie=e=>{const t=xe.zip(2147483637*Te()|0);return t in e?Ie(e):t},Le=(e,...t)=>{for(const n of e)n(...t);return e},De=e=>(e.splice(0),e);class Ue{ws=null;intentionally_closed=!1;reconnect_timeout=null;queue={};onReadyQueue=[];onCloseQueue=[];onReadyFailQueue=[];handlers={open:[],close:[],message:[],error:[],timeout:[]};config={};ping_timer=null;idle_timer=null;get opened(){return 1===this.ws?.readyState}init_flush(){v($,this.queue)}call(e,...t){for(const n of this.handlers[e])n(...t)}log(e,t=null,n=null){const{config:s}=this;setTimeout((()=>{null===n?s.timer?s.log(e,null,t):s.log(e,t):s.log(e,n,t)}))}resetPing(){const{config:{ping:e},ping_timer:t}=this;e&&(p(t)||clearTimeout(t),this.ping_timer=qe(1e3*e.interval,(async()=>{const{ping_timer:t,opened:n}=this;n?(await this.send(e.content,Fe),this.resetPing()):clearTimeout(t)})))}resetIdle(){const{config:{max_idle_time:e},idle_timer:t}=this;e!==1/0&&(p(t)||clearTimeout(t),this.idle_timer=qe(1e3*e,(()=>this.opened&&this.close())))}_reconnecting=!1;async reconnect(e){if(this._reconnecting||this.intentionally_closed||!e)return;this.log("reconnect"),this._reconnecting=!0,p(this.ws)||(this.ws.close(),this.ws=null);const t=await this.connect();!p(t)&&e>0?this.reconnect_timeout=setTimeout(this.reconnect.bind(this,e-1),1e3*this.config.reconnect):(this._reconnecting=!1,p(t)?(De(Le(this.onReadyQueue)),De(this.onReadyFailQueue)):(De(Le(this.onReadyFailQueue)),De(this.onReadyQueue)))}initSocket(e){const{queue:t,config:n}=this;this.ws=e,De(Le(this.onReadyQueue));const{id_key:s,data_key:i}=n.server;this.call("open",e);for(const n in t)e.send(t[n].msg);null!==this.reconnect_timeout&&(clearInterval(this.reconnect_timeout),this.reconnect_timeout=null),this.resetPing(),this.resetIdle(),$e(e,"close",(async(...e)=>{this.ws=null,this.onCloseQueue.forEach(ze),this.onCloseQueue.splice(0),this.call("close",...e);let{reconnect:t,reconnection_attempts:s}=n;Be(t)&&this.reconnect(s)})),$e(e,"message",(e=>{try{const t=n.decode(e.data);this.call("message",{...e,data:t});let o=!1;if(_("Object",t)&&s in t){const e=this.queue[t[s]];if(e){const n=e.sent_time?Date.now()-e.sent_time:null;this.log("message",t[i],n),e.ff(t[i]),o=!0}}o||this.log("message-ext",t)}catch(t){console.error(t,`WSP: Decode error. Got: ${e.data}`)}this.resetPing()}))}opening=!1;connect(){return new Promise((e=>{if(this.opened||this.opening)return e(null);this.opening=!0;const t=this.config,n=t.socket||t.adapter(t.url,t.protocols);if(!n||n.readyState>1)return this.opening=!1,this.ws=null,this.log("error","ready() on closing or closed state! status 2."),e(2);const s=x((t=>{this.opening=!1,e(t)}));$e(n,"error",x((e=>{this.ws=null,this.log("error","status 3. Err: "+(e.message||e)),this.call("error",e),s(3)}))),n.readyState?(this.initSocket(n),s(null)):$e(n,"open",x((()=>{this.log("open"),this.initSocket(n),s(null)})))}))}get socket(){return this.ws}async ready(){return new Promise((e=>{this.config.lazy||this.opened?e():this.onReadyQueue.push(e)}))}on(e,t,n=W,s=!1){const i=e=>n(e)&&t(e);return s?$e(this.ws,e,i):this.handlers[e].push(i),i}off(e,t,n=!1){if(n)return((e,t,n)=>e.removeEventListener(t,n))(this.ws,e,t);const s=this.handlers[e],i=s.indexOf(t);~i&&s.splice(i,1)}async close(){return new Promise(((e,t)=>{null===this.ws?t("WSP: closing a non-inited socket!"):(this.onCloseQueue.push((()=>{this.init_flush(),e(null)})),this.ws.close(),this.ws=null,this.intentionally_closed=!0)}))}open(){if(!this.opened)return this.intentionally_closed=!1,this.connect()}addEventListener(e,t,n={}){return this.on(e,t,n.predicate,n.raw)}removeEventListener(e,t,n={}){return this.off(e,t,n.raw)}async prepareMessage(e,t={}){this.log(t._is_ping?"ping":"send",e);const{config:n,queue:s}=this,{pipes:i,server:{data_key:o}}=n,{top:r,_is_ping:c}=t,l=Ie(s);if("object"==typeof r&&r[o])throw new Error(`Attempting to set data key/token via ${t._is_ping?"ping":"send"}() options!`);for(const t of i)e=t(e);const[u,a]=await Promise.all([n.encode(l,e,n),this.connect()]);if(a)throw new Error("ERR while opening connection #"+a);const h=N((()=>delete this.queue[l]));return{id:l,msg:u,timeout:i=>qe(n.timeout,(()=>{if(l in s){this.call("timeout",e),h();const{reconnect_on_to:s}=n,o=()=>i({"Websocket timeout expired":n.timeout,"for the message":e});s?(this.onReadyQueue.push((()=>this.send(e,t))),this.onReadyFailQueue.push(o),this.reconnect(n.reconnection_attempts)):o()}})),cleanup:h,send:()=>this.opened&&(this.ws.send(u),this.resetPing(),!c&&this.resetIdle())}}async send(e,t={}){const{id:n,msg:s,timeout:i,cleanup:o,send:r}=await this.prepareMessage(e,t),{queue:c,config:l}=this;return new Promise(((e,t)=>{const o=i(t);c[n]={msg:s,data_type:l.data_type,sent_time:l.timer?Date.now():null,ff(t){Me(o),e(t)}},r()})).finally(o)}async*stream(e,t={}){const{id:n,msg:s,timeout:i,cleanup:o,send:r}=await this.prepareMessage(e,t),{queue:c,config:l}=this;let u,a=!1,h=null;for(c[n]={msg:s,ff:e=>{e?.done&&(delete e.done,a=!0,setTimeout(o)),u(e)},data_type:l.data_type,sent_time:l.timer?Date.now():null},r();!a;)yield await new Promise(((e,t)=>{h=i(t),u=e})).catch((e=>o(e))).finally((()=>{Me(h),h=null}))}constructor(e={}){this.config=(e=>{if(null===We&&!("adapter"in e))throw new Error("\n This platform has no native WebSocket implementation.\n Please use 'ws' package as an adapter.\n See https://github.com/houd1ni/WebsocketPromisify/issues/23\n ");const t=Object.assign({},Oe,e),n=t.url;if("/"==n[0])try{const e=location.protocol.includes("s:")?"wss":"ws";t.url=`${e}://${location.hostname}:${location.port}${n}`}catch(e){throw new Error("WSP: URL starting with / in non-browser environment!")}return t})(e),this.config.lazy||this.connect()}}exports.WebSocketClient=Ue,exports.default=Ue;
|
|
1
|
+
"use strict";const t=Symbol("Placeholder"),e=e=>{let n=0;for(const s of e)s!==t&&n++;return n},n=(e,n)=>{const s=e.length,i=e.slice(),r=n.length;let o=r,c=0;for(;o&&c<s;c++)i[c]===t&&(i[c]=n[r-o],o--);for(c=s;o;c++,o--)i[c]=n[r-o];return i},s=(t,i,r)=>{const o=t.length-i.length-e(r);if(o<1)return t(...n(i,r));{const e=(...e)=>s(t,n(i,r),e);return e.$args_left=o,e}},i=t=>(...n)=>t.length>e(n)?s(t,[],n):t(...n);function r(e){return function(n,...s){return s.length>0?n===t?(e=>function(n){return n===t?e:e(n)})(t=>e(t,s[0])):e(n,s[0]):t=>e(n,t)}}function o(t){return i(t)}const c=t=>t.length,a=/^(.*?)(8|16|32|64)(Clamped)?Array$/,l={__proto__:!0,constructor:!0,prototype:!0},h=void 0,u=1/0,d=t=>typeof t,f=t=>null===t,m=t=>"number"==d(t),p=t=>f(t)||(t=>t===h)(t),g=t=>!(t in l),{isNaN:_}=Number,y={u:"U",b:"B",n:"N",s:"S",f:"F",o:"O"},b=Symbol(),w=t=>y[t[0]]+t.slice(1),k=t=>{const e=d(t);return"object"===e?f(t)?"Null":t.constructor?.name||w(e):"number"===e&&_(t)?"NaN":w(e)},S=r((t,e)=>k(e)===t),v=r((t,e)=>t===e),P=r((t,e)=>{const n=k(t);if(v(n,k(e))&&(v(n,"Object")||v(n,"Array")||(t=>a.test(t))(n))){if(f(t)||f(e))return v(t,e);if(v(t,e))return!0;for(const n of[t,e])for(const s in n)if(!(v(n,e)&&s in t||v(n,t)&&s in e&&P(t[s],e[s])))return!1;return!0}return v(t,e)}),A=t=>()=>t,E=r((t,e)=>(e.push(t),e)),N=o((t,e,n)=>n.reduce(t,e)),j=t=>r((e,n)=>{for(let s in n)if(g(s))switch(k(n[s])){case"Array":if(t>1&&"Array"===k(e[s]))switch(t){case 2:const i=e[s],r=n[s];for(const e in r)i[e]?j(t)(i[e],r[e]):i[e]=r[e];break;case 3:e[s].push(...n[s])}else e[s]=n[s];break;case"Object":if("Object"===k(e[s])){j(t)(e[s],n[s]);break}default:e[s]=n[s]}return e}),q=j(1),z=(t,e)=>{const n=c(e);for(let s=t;s<n;s++)e[s]=e[s+1];return e.length=n-1,e},O=new Set,x=r((t,e)=>{let n,s=c(e);for(let i=0;i<s;i++){n=t(e[i]),O.has(n)?(z(i,e),s--,i--):O.add(n)}return O.clear(),e});x(t=>t);const W=i((t,e,n,s)=>t(s)?e(s):n(s)),$=(...e)=>(...n)=>{let s,i=!0;for(let r=c(e)-1;r>-1;r--)i?(i=!1,s=e[r](...n)):s=s===t?e[r]():e[r](s);return s},C=r((t,e)=>e[t]),M=o((t,e,n)=>n.slice(t,m(e)?e:u)),B=C(0);M(1,u);const T=r((t,e)=>t*e),L=r((t,e)=>e.find(t)),R=r((t,e)=>(t(e),e)),I=A(!0),U=A(!1),Z=()=>{},D=r((t,e)=>N((e,n)=>L(e=>t(n,e),e)?e:E(n,e),[],e));D(P);const F=t=>{let e,n=!1;return function(...s){return n?e:(n=!0,e=t(...s))}},J=(t,e,n)=>c(e)?p(n)?t:$(s=>s in n?J(t,M(1,u,e),n[s]):t,B)(e):n,G=o(J);G(h),$(W(P(b),U,I),G(b));const H=Symbol("Placeholder"),K=t=>{let e=0;for(const n of t)n!==H&&e++;return e},Q=(t,e)=>{const n=t.length,s=t.slice(),i=e.length;let r=i,o=0;for(;r&&o<n;o++)s[o]===H&&(s[o]=e[i-r],r--);for(o=n;r;o++,r--)s[o]=e[i-r];return s},V=(t,e,n)=>{const s=t.length-e.length-K(n);if(s<1)return t(...Q(e,n));{const i=(...s)=>V(t,Q(e,n),s);return i.$args_left=s,i}},X=t=>(...e)=>t.length>K(e)?V(t,[],e):t(...e);function Y(t){return function(e,...n){return n.length>0?e===H?(t=>function(e){return e===H?t:t(e)})(e=>t(e,n[0])):t(e,n[0]):n=>t(e,n)}}function tt(t){return X(t)}const et=t=>t.length,nt=/^(.*?)(8|16|32|64)(Clamped)?Array$/,st=void 0,it=1/0,rt=t=>typeof t,ot=t=>null===t,ct={u:"U",b:"B",n:"N",s:"S",f:"F"},at=Symbol(),lt=t=>{const e=rt(t);return"object"===e?ot(t)?"Null":t.constructor.name:ct[e[0]]+e.slice(1)},ht=Y((t,e)=>lt(e)===t),ut=Y((t,e)=>t===e),dt=Y((t,e)=>{const n=lt(t);if(ut(n,lt(e))&&(ut(n,"Object")||ut(n,"Array")||(s=n,nt.test(s)))){if(ot(t)||ot(e))return ut(t,e);if(ut(t,e))return!0;for(const n of[t,e])for(const s in n)if(!(ut(n,e)&&s in t||ut(n,t)&&s in e&&dt(t[s],e[s])))return!1;return!0}var s;return ut(t,e)}),ft=Y((t,e)=>(e.push(t),e)),mt=tt((t,e,n)=>n.reduce(t,e)),pt=X((t,e,n,s)=>t(s)?e(s):n(s)),gt=tt((t,e,n)=>pt(t,e,vt,n)),_t=(...t)=>(...e)=>{let n,s=!0;for(let i=et(t)-1;i>-1;i--)s?(s=!1,n=t[i](...e)):n=n===H?t[i]():t[i](n);return n},yt=Y((t,e)=>e[t]),bt=tt((t,e,n)=>n.slice(t,(t=>"number"==rt(t))(e)?e:it)),wt=yt(0);bt(1,it);const kt=Y((t,e)=>e.find(t)),St=t=>()=>t,vt=t=>t,Pt=Y((t,e)=>e.split(t)),At=St(!0),Et=St(!1),Nt=Y((t,e)=>mt((e,n)=>kt(e=>t(n,e),e)?e:ft(n,e),[],e))(dt),jt=(t,e,n)=>et(e)?(t=>ot(t)||(t=>t===st)(t))(n)?t:_t(s=>s in n?jt(t,bt(1,it,e),n[s]):t,wt)(e):n,qt=tt(jt);qt(st),_t(pt(dt(at),Et,At),qt(at));const zt=Y((t,e)=>e.map(t)),{floor:Ot}=Math,xt="0123456789abcdefghijklmnopqrstuvwxyz",Wt=ht("String"),$t=gt(Wt,Pt("")),Ct=_t(t=>Object.fromEntries(t),zt((t,e)=>[t,e]),$t);class Mt{is_str;delim;abc;abclen;c2pos;standard;setABC(t,e=""){if(this.is_str=Wt(t),this.delim=e,!_t(ut(et(n=t)),et,Nt,$t)(n))throw new Error("Not all chars are unique!");var n;this.abc=t,this.abclen=et(t),this.standard=!!this.is_str&&xt.startsWith(t),this.c2pos=Ct(t)}zip(t){const{abc:e,abclen:n,delim:s}=this;let i="",r=!0;for(;t>0;)i=e[t%n]+(r?"":s)+i,t=Ot(t/n),r=!1;return i||"0"}unzip(t){const{standard:e,abclen:n,c2pos:s,delim:i,is_str:r}=this;if("0"===t)return 0;if(e)return parseInt(t,n);const o=r?t:t.split(i),c=et(o);let a=0;for(let t=0;t<c;t++)a+=s[o[t]]*n**(c-t-1);return a}constructor(t,e){e?this.setABC(t,e):this.setABC(t||xt+"ABCDEFGHIJKLMNOPQRSTUVWXYZ")}}const Bt=new Mt;Bt.setABC.bind(Bt),Bt.zip.bind(Bt),Bt.unzip.bind(Bt);const Tt=(()=>{try{return WebSocket||null}catch{return null}})(),Lt=(t,e,n)=>t.addEventListener(e,n),Rt=(t,e)=>setTimeout(e,t),{min:It,random:Ut}=Math,Zt=t=>{if(null===Tt&&!("adapter"in t))throw new Error("\n This platform has no native WebSocket implementation.\n Please use 'ws' package as an adapter.\n See https://github.com/houd1ni/WebsocketPromisify/issues/23\n ");const e=q({data_type:"json",log:()=>null,timer:!1,url:"",timeout:1.4,reconnect:{stop_after:45,on_timeout:!0,on_break:!0,time_fn:({base:t,max:e,jitter:n},s)=>It(e,t**(s+Ut()*n)),params:{base:2,max:20,jitter:.1}},max_idle_time:1/0,lazy:!1,socket:null,adapter:(t,e)=>new WebSocket(t,e),encode:(t,e,{server:n})=>JSON.stringify({[n.id_key]:t,[n.data_key]:e}),decode:t=>JSON.parse(t),protocols:[],pipes:[],server:{id_key:"id",data_key:"data"},ping:{interval:55,out:"ping",in:"pong"}},t),n=e.url;if("/"==n[0])try{const t=location.protocol.includes("s:")?"wss":"ws";e.url=`${t}://${location.hostname}:${location.port}${n}`}catch(t){throw new Error("WSP: URL starting with / in non-browser environment!")}return e},{random:Dt}=Math,Ft=null,Jt=1/0,Gt=Promise.resolve(Ft),Ht="message",Kt="message-ext",Qt=new Mt,Vt=()=>Date.now()/1e3,Xt=T(1e3),Yt=t=>t&&clearTimeout(t),te=t=>null===t,ee=S("String"),ne=S("Object"),se=(t,e,n,s=.5)=>{const i=Xt(s),r=setTimeout(()=>{const s=t.indexOf(e);~s&&(t.splice(s),n(`could not close in ${i}ms!`))},1e3*i);t.push((...t)=>{Yt(r),e(...t)})},ie=t=>{const e=Qt.zip(2147483637*Dt()|0);return e in t?ie(t):e},re=(t,...e)=>{for(const n of t)n(...e);return t},oe=t=>(t.splice(0),t),ce=(t,e)=>e(t);exports.WebSocketClient=class{ws=Ft;intentionally_closed=!1;reconnect_timeout=Ft;queue={send:new Map,on_ready:[],on_close:[],on_ready_fail:[]};handlers={open:[],close:[],message:[],[Kt]:[],error:[],timeout:[]};config={};ping_timer=Ft;idle_timer=Ft;zombie_timer=Ft;router=ce;get opened(){return 1===this.ws?.readyState}call(t,...e){for(const n of this.handlers[t])n(...e)}log(t,e=Ft,n=Ft){const{config:s}=this;setTimeout(()=>{te(n)?s.timer?s.log(t,Ft,e):s.log(t,e):s.log(t,n,e)})}resetPing(){const{config:{ping:t},ping_timer:e}=this;t&&(Yt(e),this.ping_timer=Rt(Xt(t.interval),async()=>{const{ping_timer:e,opened:n}=this;n?(this.ws.send(t.out),this.resetPing()):Yt(e)}))}resetZombieProbe(){const{config:t}=this;if(t.ping){const e=t.ping.timeout;Yt(this.zombie_timer),e!==1/0&&(this.zombie_timer=Rt(Xt(e||t.timeout),()=>this.close().catch(Z)))}}resetIdle(){const{config:{max_idle_time:t},idle_timer:e}=this;t!==1/0&&(Yt(e),this.idle_timer=Rt(Xt(t),()=>this.opened&&this.close()))}_reconnecting=!1;reconnect_start=0;async reconnect(t=0){if(this._reconnecting&&0===t)return;this.log("reconnect"),this._reconnecting=!0,this.reconnect_start=Vt(),p(this.ws)||this.terminate();const{queue:e}=this;if(t>0&&p(await this.connect()))oe(re(e.on_ready)),oe(e.on_ready_fail),this._reconnecting=!1,this.reconnect_timeout=Ft;else{const{stop_after:n,time_fn:s,params:i}=this.config.reconnect;Vt()-this.reconnect_start>n?(this.terminate(),oe(re(e.on_ready_fail)),oe(e.on_ready),this._reconnecting=!1,this.reconnect_timeout=Ft):this.reconnect_timeout=Rt(Xt(s(i,t)),this.reconnect.bind(this,t+1))}}resetReconnect(){te(this.reconnect_timeout)||(Yt(this.reconnect_timeout),this.reconnect_timeout=Ft)}initSocket(t){const{queue:e,config:n,router:s}=this;this.ws=t,oe(re(this.queue.on_ready));const{id_key:i,data_key:r}=n.server;this.call("open",t);for(const{msg:n}of e.send.values())t.send(n);this.resetReconnect(),this.resetZombieProbe(),this.resetPing(),this.resetIdle(),Lt(t,"close",async(...t)=>{this.ws=Ft,oe(re(e.on_close)),this.call("close",...t),!this.intentionally_closed&&n.reconnect.on_break&&this.reconnect()});const{ping:o}=n,c=t=>{try{const e=n.decode(t),{send:s}=this.queue;let o=!1;if(ne(e)&&i in e){const t=e[i];if(s.has(t)){const n=s.get(t),i=e[r],c=n.sent_time?Vt()-n.sent_time:Ft;this.log(Ht,i,c),n.ff(i),o=!0}}const c=o?Ht:Kt;this.log(c,e),this.call(c,{data:e})}catch(e){console.error(e,`WSP: Decode error. Got: ${t}`)}};Lt(t,Ht,t=>{const e=ee(t.data)?t.data:new Uint8Array(t.data);this.resetZombieProbe(),this.resetPing(),o&&P(e,o.in)||s(e,c)})}_opening=!1;connect(){return this.opened||this._opening||this.opened||this._opening?Gt:new Promise(t=>{this._opening=!0;const e=this.config,n=e.socket||e.adapter(e.url,e.protocols);if(p(n)||n.readyState>1)return this._opening=!1,this.ws=Ft,this.log("error","ready() on closing or closed state! status 2."),t(2);const s=F(e=>{this._opening=!1,t(e)});Lt(n,"error",F(t=>{this.ws=Ft,this.log("error","status 3. Err: "+(t.message||t)),this.call("error",t),s(3)})),n.readyState?(this.initSocket(n),s(Ft)):Lt(n,"open",F(()=>{this.log("open"),this.initSocket(n),s(Ft)}))})}get socket(){return this.ws}async ready(t=Jt){return new Promise((e,n)=>{const{on_ready:s}=this.queue;this.config.lazy||this.opened?e():t===Jt?s.push(e):se(s,e,n)})}on(t,e,n=I,s=!1){const i=t=>n(t)&&e(t);return s?Lt(this.ws,t,i):this.handlers[t].push(i),i}off(t,e,n=!1){if(n)return((t,e,n)=>t.removeEventListener(e,n))(this.ws,t,e);const s=this.handlers[t],i=s.indexOf(e);~i&&s.splice(i,1)}terminate(){this.ws?.close(),this.ws=Ft,this.intentionally_closed=!0}async close(t=.5){return new Promise((e,n)=>{te(this.ws)?e(Ft):(se(this.queue.on_close,e,n,t),this.terminate())})}open(){if(!this.opened)return this.intentionally_closed=!1,this.connect()}addEventListener(t,e,n={}){return this.on(t,e,n.predicate,n.raw)}removeEventListener(t,e,n={}){return this.off(t,e,n.raw)}async prepareMessage(t,e={}){this.log("send",t);const{config:n,queue:{send:s,on_ready_fail:i}}=this,{pipes:r,server:{data_key:o}}=n,{top:c}=e,a=ie(s);if(ne(c)&&o in c)throw new Error("\n Attempting to set data key/token via send() options!\n ");for(const e of r)t=e(t);const[l,h]=await Promise.all([n.encode(a,t,n),this.connect()]);if(h)throw new Error("ERR while opening connection > "+h);const u=c?.timeout||n.timeout,d=R(()=>s.delete(a));return{id:a,msg:l,timeout:e=>Rt(Xt(u),()=>{if(s.has(a)){this.call("timeout",t),d();const s=()=>e({"Websocket timeout expired":u,"for the message":t});n.reconnect.on_timeout?(i.push(s),this.reconnect()):s()}}),cleanup:d,send:()=>this.opened&&(this.ws.send(l),this.resetPing(),this.resetIdle())}}async send(t,e={}){const{id:n,msg:s,timeout:i,cleanup:r,send:o}=await this.prepareMessage(t,e),{queue:{send:c},config:a}=this;return new Promise((t,e)=>{const r=i(e);c.set(n,{msg:s,data_type:a.data_type,sent_time:a.timer?Vt():Ft,ff(e){Yt(r),t(e)}}),o()}).finally(r)}async*stream(t,e={}){const{id:n,msg:s,timeout:i,cleanup:r,send:o}=await this.prepareMessage(t,e),{queue:{send:c},config:a}=this;let l,h=!1,u=Ft;for(c.set(n,{msg:s,ff:t=>{t?.done&&(delete t.done,h=!0,setTimeout(r)),l(t)},data_type:a.data_type,sent_time:a.timer?Vt():Ft}),o();!h;)yield await new Promise((t,e)=>{u=i(e),l=t}).catch(r).finally(()=>Yt(u))}route(t){this.router=t}constructor(t){this.config=Zt(t),this.config.lazy||this.connect()}};
|
package/dist/bundle.d.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
// Generated by dts-bundle-generator v9.5.1
|
|
2
2
|
|
|
3
|
+
import { AnyFunc } from 'pepka';
|
|
4
|
+
|
|
3
5
|
declare namespace wsc {
|
|
4
6
|
interface DataObject {
|
|
5
7
|
[key: string]: any;
|
|
@@ -18,6 +20,11 @@ declare namespace wsc {
|
|
|
18
20
|
type Predicate = (data: any) => boolean;
|
|
19
21
|
type DataPipe = (message: any) => any;
|
|
20
22
|
type DataType = "json" | "string";
|
|
23
|
+
interface TimeFnParams {
|
|
24
|
+
base: number;
|
|
25
|
+
max: number;
|
|
26
|
+
jitter: number;
|
|
27
|
+
}
|
|
21
28
|
interface Config {
|
|
22
29
|
data_type: DataType;
|
|
23
30
|
log(event: string, time?: number | null, message?: any): void;
|
|
@@ -25,9 +32,20 @@ declare namespace wsc {
|
|
|
25
32
|
timer: boolean;
|
|
26
33
|
url: string;
|
|
27
34
|
timeout: number;
|
|
28
|
-
reconnect:
|
|
29
|
-
|
|
30
|
-
|
|
35
|
+
reconnect: {
|
|
36
|
+
stop_after: number;
|
|
37
|
+
on_timeout: boolean;
|
|
38
|
+
on_break: boolean;
|
|
39
|
+
time_fn: import("pepka").AnyFunc<number, [
|
|
40
|
+
params: wsc.TimeFnParams,
|
|
41
|
+
attempt: number
|
|
42
|
+
]>;
|
|
43
|
+
params: {
|
|
44
|
+
base: number;
|
|
45
|
+
max: number;
|
|
46
|
+
jitter: number;
|
|
47
|
+
};
|
|
48
|
+
};
|
|
31
49
|
max_idle_time: number;
|
|
32
50
|
lazy: boolean;
|
|
33
51
|
socket: Socket | null;
|
|
@@ -44,14 +62,15 @@ declare namespace wsc {
|
|
|
44
62
|
};
|
|
45
63
|
ping: {
|
|
46
64
|
interval: number;
|
|
47
|
-
|
|
48
|
-
|
|
65
|
+
timeout?: number;
|
|
66
|
+
in: string | Uint8Array;
|
|
67
|
+
out: string | Uint8Array;
|
|
68
|
+
} | false;
|
|
49
69
|
}
|
|
50
|
-
type UserConfig =
|
|
70
|
+
type UserConfig = import("type-fest").PartialDeep<Config> & Pick<Config, "url">;
|
|
51
71
|
type SendOptions = Partial<{
|
|
52
72
|
top: any;
|
|
53
73
|
data_type: DataType;
|
|
54
|
-
_is_ping: boolean;
|
|
55
74
|
}>;
|
|
56
75
|
interface Message {
|
|
57
76
|
msg: any;
|
|
@@ -60,55 +79,81 @@ declare namespace wsc {
|
|
|
60
79
|
sent_time: number | null;
|
|
61
80
|
}
|
|
62
81
|
}
|
|
63
|
-
|
|
82
|
+
declare const label_message_ext = "message-ext";
|
|
83
|
+
export type EventHandler<T extends keyof WebSocketEventMap> = AnyFunc<any, [
|
|
84
|
+
WebSocketEventMap[T]
|
|
85
|
+
]>;
|
|
86
|
+
export type RouteHandler<T> = AnyFunc<any, [
|
|
87
|
+
data: T,
|
|
88
|
+
next: AnyFunc
|
|
89
|
+
]>;
|
|
90
|
+
export type EventHandlers = {
|
|
91
|
+
open: EventHandler<"open">[];
|
|
92
|
+
close: EventHandler<"close">[];
|
|
93
|
+
error: EventHandler<"error">[];
|
|
94
|
+
message: AnyFunc<any, [
|
|
95
|
+
WebSocketEventMap["message"] & {
|
|
96
|
+
data: any;
|
|
97
|
+
}
|
|
98
|
+
]>[];
|
|
99
|
+
[label_message_ext]: AnyFunc<any, [
|
|
100
|
+
WebSocketEventMap["message"] & {
|
|
101
|
+
data: any;
|
|
102
|
+
}
|
|
103
|
+
]>[];
|
|
104
|
+
timeout: AnyFunc<any, [
|
|
105
|
+
data: any
|
|
106
|
+
]>[];
|
|
107
|
+
};
|
|
108
|
+
export declare class WebSocketClient<T extends Uint8Array | string = string> {
|
|
64
109
|
private ws;
|
|
65
110
|
private intentionally_closed;
|
|
66
111
|
private reconnect_timeout;
|
|
67
112
|
private queue;
|
|
68
|
-
private onReadyQueue;
|
|
69
|
-
private onCloseQueue;
|
|
70
|
-
private onReadyFailQueue;
|
|
71
113
|
private handlers;
|
|
72
114
|
private config;
|
|
73
115
|
private ping_timer;
|
|
74
116
|
private idle_timer;
|
|
117
|
+
private zombie_timer;
|
|
118
|
+
private router;
|
|
75
119
|
private get opened();
|
|
76
|
-
private init_flush;
|
|
77
120
|
private call;
|
|
78
121
|
private log;
|
|
79
122
|
private resetPing;
|
|
123
|
+
private resetZombieProbe;
|
|
80
124
|
private resetIdle;
|
|
81
125
|
private _reconnecting;
|
|
126
|
+
private reconnect_start;
|
|
82
127
|
private reconnect;
|
|
128
|
+
private resetReconnect;
|
|
83
129
|
private initSocket;
|
|
84
|
-
private
|
|
130
|
+
private _opening;
|
|
131
|
+
/** returns status if won't open or null if ok. */
|
|
85
132
|
private connect;
|
|
86
133
|
get socket(): wsc.Socket | null;
|
|
87
|
-
ready(): Promise<void>;
|
|
88
|
-
on(event_name:
|
|
89
|
-
off(event_name:
|
|
90
|
-
|
|
134
|
+
ready(timeout?: number): Promise<void>;
|
|
135
|
+
on(event_name: keyof EventHandlers, handler: wsc.EventHandler, predicate?: wsc.Predicate, raw?: boolean): wsc.EventHandler;
|
|
136
|
+
off(event_name: keyof EventHandlers, handler: wsc.EventHandler, raw?: boolean): void;
|
|
137
|
+
private terminate;
|
|
138
|
+
close(timeout?: number): wsc.AsyncErrCode;
|
|
91
139
|
open(): Promise<number | null> | undefined;
|
|
92
|
-
addEventListener(e:
|
|
140
|
+
addEventListener(e: keyof EventHandlers, cb: wsc.EventHandler, opts?: {
|
|
93
141
|
predicate?: wsc.Predicate;
|
|
94
142
|
raw?: boolean;
|
|
95
143
|
}): wsc.EventHandler;
|
|
96
|
-
removeEventListener(e:
|
|
144
|
+
removeEventListener(e: keyof EventHandlers, handler: wsc.EventHandler, opts?: {
|
|
97
145
|
predicate?: wsc.Predicate;
|
|
98
146
|
raw?: boolean;
|
|
99
147
|
}): void;
|
|
100
148
|
private prepareMessage;
|
|
101
|
-
/** .send(your_data) wraps request to server with {id: `
|
|
149
|
+
/** .send(your_data) wraps request to server with {id: `unique_id`, data: `actually your data`},
|
|
102
150
|
returns a Promise that will be rejected after a timeout or
|
|
103
151
|
resolved if server returns the same signature: {id: `same_hash`, data: `response data`}.
|
|
104
152
|
*/
|
|
105
153
|
send<RequestDataType = any, ResponseDataType = any>(message_data: RequestDataType, opts?: wsc.SendOptions): Promise<ResponseDataType>;
|
|
106
154
|
stream<RequestDataType = any, ResponseDataType = any>(message_data: RequestDataType, opts?: wsc.SendOptions): AsyncGenerator<ResponseDataType, void, unknown>;
|
|
107
|
-
|
|
155
|
+
route(handler: RouteHandler<T>): void;
|
|
156
|
+
constructor(user_config: wsc.UserConfig);
|
|
108
157
|
}
|
|
109
158
|
|
|
110
|
-
export {
|
|
111
|
-
WebSocketClient as default,
|
|
112
|
-
};
|
|
113
|
-
|
|
114
159
|
export {};
|
package/dist/bundle.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
const e=Symbol("Placeholder"),t=t=>{let n=0;for(const s of t)s!==e&&n++;return n},n=(t,n)=>{const s=t.length,i=t.slice(),o=n.length;let r=o,c=0;for(;r&&c<s;c++)i[c]===e&&(i[c]=n[o-r],r--);for(c=s;r;c++,r--)i[c]=n[o-r];return i},s=(e,i,o)=>{const r=e.length-i.length-t(o);if(r<1)return e(...n(i,o));{const t=(...t)=>s(e,n(i,o),t);return t.$args_left=r,t}},i=e=>(...n)=>e.length>t(n)?s(e,[],n):e(...n);function o(t){return function(n,...s){return s.length>0?n===e?(t=>function(n){return n===e?t:t(n)})((e=>t(e,s[0]))):t(n,s[0]):e=>t(n,e)}}function r(e){return i(e)}const c=e=>e.length,l=/^(.*?)(8|16|32|64)(Clamped)?Array$/,u=void 0,a=1/0,h=e=>typeof e,d=e=>null===e,f=e=>"number"==h(e);const p=e=>d(e)||(e=>e===u)(e),g={u:"U",b:"B",n:"N",s:"S",f:"F"},m=Symbol(),y=e=>{const t=h(e);return"object"===t?d(e)?"Null":e.constructor.name:g[t[0]]+t.slice(1)},_=o(((e,t)=>y(t)===e)),w=o(((e,t)=>e===t)),b=o(((e,t)=>{const n=y(e);if(w(n,y(t))&&(w(n,"Object")||w(n,"Array")||(e=>l.test(e))(n))){if(d(e)||d(t))return w(e,t);if(w(e,t))return!0;for(const n of[e,t])for(const s in n)if(!(w(n,t)&&s in e||w(n,e)&&s in t&&b(e[s],t[s])))return!1;return!0}return w(e,t)})),k=o(((e,t)=>(t.push(e),t))),S=r(((e,t,n)=>n.reduce(e,t))),v=o(((e,t)=>{const n=(e=>Array.isArray(e))(t);let s,i;n&&(s=0,i=[]);for(let s in t)e(t[s],s)||(n?i.push(+s):delete t[s]);if(n)for(const e of i)t.splice(e-s++,1);return t})),P=i(((e,t,n,s)=>e(s)?t(s):n(s))),Q=(...t)=>(...n)=>{let s,i=!0;for(let o=c(t)-1;o>-1;o--)i?(i=!1,s=t[o](...n)):s=s===e?t[o]():t[o](s);return s},E=o(((e,t)=>t[e])),R=r(((e,t,n)=>n.slice(e,f(t)?t:a))),A=E(0);R(1,a);const C=o(((e,t)=>t.find(e))),N=e=>()=>e,$=o(((e,t)=>(e(t),t))),j=N(!0),q=N(!1),T=o(((e,t)=>t(...e))),W=e=>(...t)=>{const n=e(...t),s=function(e){return"function"===h(e)}(n);return s&&n.$args_left?W(n):(e=>!e)(n)},z=o(((e,t)=>S(((t,n)=>C((t=>e(n,t)),t)?t:k(n,t)),[],t)));z(b);const O=e=>{let t,n=!1;return function(...s){return n?t:(n=!0,t=e(...s))}},x=(e,t,n)=>c(t)?p(n)?e:Q((s=>s in n?x(e,R(1,a,t),n[s]):e),A)(t):n,B=r(x);B(u),Q(P(b(m),q,j),B(m));const F=r(((e,t,n)=>t(n)&&e(n))),I=W,L=Symbol("Placeholder"),M=e=>{let t=0;for(const n of e)n!==L&&t++;return t},D=(e,t)=>{const n=e.length,s=e.slice(),i=t.length;let o=i,r=0;for(;o&&r<n;r++)s[r]===L&&(s[r]=t[i-o],o--);for(r=n;o;r++,o--)s[r]=t[i-o];return s},U=(e,t,n)=>{const s=e.length-t.length-M(n);if(s<1)return e(...D(t,n));{const i=(...s)=>U(e,D(t,n),s);return i.$args_left=s,i}},J=e=>(...t)=>e.length>M(t)?U(e,[],t):e(...t);function G(e){return function(t,...n){return n.length>0?t===L?(e=>function(t){return t===L?e:e(t)})((t=>e(t,n[0]))):e(t,n[0]):n=>e(t,n)}}function H(e){return J(e)}const K=e=>e.length,V=/^(.*?)(8|16|32|64)(Clamped)?Array$/,X=void 0,Y=1/0,Z=e=>typeof e,ee=e=>null===e,te={u:"U",b:"B",n:"N",s:"S",f:"F"},ne=Symbol(),se=e=>{const t=Z(e);return"object"===t?ee(e)?"Null":e.constructor.name:te[t[0]]+t.slice(1)},ie=G(((e,t)=>se(t)===e)),oe=G(((e,t)=>e===t)),re=G(((e,t)=>{const n=se(e);if(oe(n,se(t))&&(oe(n,"Object")||oe(n,"Array")||(s=n,V.test(s)))){if(ee(e)||ee(t))return oe(e,t);if(oe(e,t))return!0;for(const n of[e,t])for(const s in n)if(!(oe(n,t)&&s in e||oe(n,e)&&s in t&&re(e[s],t[s])))return!1;return!0}var s;return oe(e,t)})),ce=G(((e,t)=>(t.push(e),t))),le=H(((e,t,n)=>n.reduce(e,t))),ue=J(((e,t,n,s)=>e(s)?t(s):n(s))),ae=H(((e,t,n)=>ue(e,t,ye,n))),he=(...e)=>(...t)=>{let n,s=!0;for(let i=K(e)-1;i>-1;i--)s?(s=!1,n=e[i](...t)):n=n===L?e[i]():e[i](n);return n},de=G(((e,t)=>t[e])),fe=H(((e,t,n)=>n.slice(e,(e=>"number"==Z(e))(t)?t:Y))),pe=de(0);fe(1,Y);const ge=G(((e,t)=>t.find(e))),me=e=>()=>e,ye=e=>e,_e=G(((e,t)=>t.split(e))),we=me(!0),be=me(!1),ke=G(((e,t)=>le(((t,n)=>ge((t=>e(n,t)),t)?t:ce(n,t)),[],t)))(re),Se=(e,t,n)=>K(t)?(e=>ee(e)||(e=>e===X)(e))(n)?e:he((s=>s in n?Se(e,fe(1,Y,t),n[s]):e),pe)(t):n,ve=H(Se);ve(X),he(ue(re(ne),be,we),ve(ne));const Pe=G(((e,t)=>t.map(e))),{floor:Qe}=Math,Ee="0123456789abcdefghijklmnopqrstuvwxyz",Re=ie("String"),Ae=ae(Re,_e("")),Ce=he((e=>Object.fromEntries(e)),Pe(((e,t)=>[e,t])),Ae);class Ne{is_str;delim;abc;abclen;c2pos;standard;setABC(e,t=""){if(this.is_str=Re(e),this.delim=t,!he(oe(K(n=e)),K,ke,Ae)(n))throw new Error("Not all chars are unique!");var n;this.abc=e,this.abclen=K(e),this.standard=!!this.is_str&&Ee.startsWith(e),this.c2pos=Ce(e)}zip(e){const{abc:t,abclen:n,delim:s}=this;let i="",o=!0;for(;e>0;)i=t[e%n]+(o?"":s)+i,e=Qe(e/n),o=!1;return i||"0"}unzip(e){const{standard:t,abclen:n,c2pos:s,delim:i,is_str:o}=this;if("0"===e)return 0;if(t)return parseInt(e,n);const r=o?e:e.split(i),c=K(r);let l=0;for(let e=0;e<c;e++)l+=s[r[e]]*n**(c-e-1);return l}constructor(e,t){t?this.setABC(e,t):this.setABC(e||Ee+"ABCDEFGHIJKLMNOPQRSTUVWXYZ")}}const $e=new Ne;$e.setABC.bind($e),$e.zip.bind($e),$e.unzip.bind($e);const je=(()=>{try{return WebSocket||null}catch{return null}})(),qe=(e,t,n)=>e.addEventListener(t,n),Te=(e,t)=>setTimeout(t,e),We={data_type:"json",log:()=>null,timer:!1,url:"localhost",timeout:1400,reconnect:2,reconnection_attempts:1/0,reconnect_on_to:!0,max_idle_time:1/0,lazy:!1,socket:null,adapter:(e,t)=>new WebSocket(e,t),encode:(e,t,{server:n})=>JSON.stringify({[n.id_key]:e,[n.data_key]:t}),decode:e=>JSON.parse(e),protocols:[],pipes:[],server:{id_key:"id",data_key:"data"},ping:{interval:55,content:{}}},{random:ze}=Math,Oe=new Ne,xe=T([]),Be=F(_("Number"),I(isNaN)),Fe={_is_ping:!0},Ie=e=>e&&clearTimeout(e),Le=e=>{const t=Oe.zip(2147483637*ze()|0);return t in e?Le(e):t},Me=(e,...t)=>{for(const n of e)n(...t);return e},De=e=>(e.splice(0),e);class Ue{ws=null;intentionally_closed=!1;reconnect_timeout=null;queue={};onReadyQueue=[];onCloseQueue=[];onReadyFailQueue=[];handlers={open:[],close:[],message:[],error:[],timeout:[]};config={};ping_timer=null;idle_timer=null;get opened(){return 1===this.ws?.readyState}init_flush(){v(q,this.queue)}call(e,...t){for(const n of this.handlers[e])n(...t)}log(e,t=null,n=null){const{config:s}=this;setTimeout((()=>{null===n?s.timer?s.log(e,null,t):s.log(e,t):s.log(e,n,t)}))}resetPing(){const{config:{ping:e},ping_timer:t}=this;e&&(p(t)||clearTimeout(t),this.ping_timer=Te(1e3*e.interval,(async()=>{const{ping_timer:t,opened:n}=this;n?(await this.send(e.content,Fe),this.resetPing()):clearTimeout(t)})))}resetIdle(){const{config:{max_idle_time:e},idle_timer:t}=this;e!==1/0&&(p(t)||clearTimeout(t),this.idle_timer=Te(1e3*e,(()=>this.opened&&this.close())))}_reconnecting=!1;async reconnect(e){if(this._reconnecting||this.intentionally_closed||!e)return;this.log("reconnect"),this._reconnecting=!0,p(this.ws)||(this.ws.close(),this.ws=null);const t=await this.connect();!p(t)&&e>0?this.reconnect_timeout=setTimeout(this.reconnect.bind(this,e-1),1e3*this.config.reconnect):(this._reconnecting=!1,p(t)?(De(Me(this.onReadyQueue)),De(this.onReadyFailQueue)):(De(Me(this.onReadyFailQueue)),De(this.onReadyQueue)))}initSocket(e){const{queue:t,config:n}=this;this.ws=e,De(Me(this.onReadyQueue));const{id_key:s,data_key:i}=n.server;this.call("open",e);for(const n in t)e.send(t[n].msg);null!==this.reconnect_timeout&&(clearInterval(this.reconnect_timeout),this.reconnect_timeout=null),this.resetPing(),this.resetIdle(),qe(e,"close",(async(...e)=>{this.ws=null,this.onCloseQueue.forEach(xe),this.onCloseQueue.splice(0),this.call("close",...e);let{reconnect:t,reconnection_attempts:s}=n;Be(t)&&this.reconnect(s)})),qe(e,"message",(e=>{try{const t=n.decode(e.data);this.call("message",{...e,data:t});let o=!1;if(_("Object",t)&&s in t){const e=this.queue[t[s]];if(e){const n=e.sent_time?Date.now()-e.sent_time:null;this.log("message",t[i],n),e.ff(t[i]),o=!0}}o||this.log("message-ext",t)}catch(t){console.error(t,`WSP: Decode error. Got: ${e.data}`)}this.resetPing()}))}opening=!1;connect(){return new Promise((e=>{if(this.opened||this.opening)return e(null);this.opening=!0;const t=this.config,n=t.socket||t.adapter(t.url,t.protocols);if(!n||n.readyState>1)return this.opening=!1,this.ws=null,this.log("error","ready() on closing or closed state! status 2."),e(2);const s=O((t=>{this.opening=!1,e(t)}));qe(n,"error",O((e=>{this.ws=null,this.log("error","status 3. Err: "+(e.message||e)),this.call("error",e),s(3)}))),n.readyState?(this.initSocket(n),s(null)):qe(n,"open",O((()=>{this.log("open"),this.initSocket(n),s(null)})))}))}get socket(){return this.ws}async ready(){return new Promise((e=>{this.config.lazy||this.opened?e():this.onReadyQueue.push(e)}))}on(e,t,n=j,s=!1){const i=e=>n(e)&&t(e);return s?qe(this.ws,e,i):this.handlers[e].push(i),i}off(e,t,n=!1){if(n)return((e,t,n)=>e.removeEventListener(t,n))(this.ws,e,t);const s=this.handlers[e],i=s.indexOf(t);~i&&s.splice(i,1)}async close(){return new Promise(((e,t)=>{null===this.ws?t("WSP: closing a non-inited socket!"):(this.onCloseQueue.push((()=>{this.init_flush(),e(null)})),this.ws.close(),this.ws=null,this.intentionally_closed=!0)}))}open(){if(!this.opened)return this.intentionally_closed=!1,this.connect()}addEventListener(e,t,n={}){return this.on(e,t,n.predicate,n.raw)}removeEventListener(e,t,n={}){return this.off(e,t,n.raw)}async prepareMessage(e,t={}){this.log(t._is_ping?"ping":"send",e);const{config:n,queue:s}=this,{pipes:i,server:{data_key:o}}=n,{top:r,_is_ping:c}=t,l=Le(s);if("object"==typeof r&&r[o])throw new Error(`Attempting to set data key/token via ${t._is_ping?"ping":"send"}() options!`);for(const t of i)e=t(e);const[u,a]=await Promise.all([n.encode(l,e,n),this.connect()]);if(a)throw new Error("ERR while opening connection #"+a);const h=$((()=>delete this.queue[l]));return{id:l,msg:u,timeout:i=>Te(n.timeout,(()=>{if(l in s){this.call("timeout",e),h();const{reconnect_on_to:s}=n,o=()=>i({"Websocket timeout expired":n.timeout,"for the message":e});s?(this.onReadyQueue.push((()=>this.send(e,t))),this.onReadyFailQueue.push(o),this.reconnect(n.reconnection_attempts)):o()}})),cleanup:h,send:()=>this.opened&&(this.ws.send(u),this.resetPing(),!c&&this.resetIdle())}}async send(e,t={}){const{id:n,msg:s,timeout:i,cleanup:o,send:r}=await this.prepareMessage(e,t),{queue:c,config:l}=this;return new Promise(((e,t)=>{const o=i(t);c[n]={msg:s,data_type:l.data_type,sent_time:l.timer?Date.now():null,ff(t){Ie(o),e(t)}},r()})).finally(o)}async*stream(e,t={}){const{id:n,msg:s,timeout:i,cleanup:o,send:r}=await this.prepareMessage(e,t),{queue:c,config:l}=this;let u,a=!1,h=null;for(c[n]={msg:s,ff:e=>{e?.done&&(delete e.done,a=!0,setTimeout(o)),u(e)},data_type:l.data_type,sent_time:l.timer?Date.now():null},r();!a;)yield await new Promise(((e,t)=>{h=i(t),u=e})).catch((e=>o(e))).finally((()=>{Ie(h),h=null}))}constructor(e={}){this.config=(e=>{if(null===je&&!("adapter"in e))throw new Error("\n This platform has no native WebSocket implementation.\n Please use 'ws' package as an adapter.\n See https://github.com/houd1ni/WebsocketPromisify/issues/23\n ");const t=Object.assign({},We,e),n=t.url;if("/"==n[0])try{const e=location.protocol.includes("s:")?"wss":"ws";t.url=`${e}://${location.hostname}:${location.port}${n}`}catch(e){throw new Error("WSP: URL starting with / in non-browser environment!")}return t})(e),this.config.lazy||this.connect()}}export{Ue as WebSocketClient,Ue as default};
|
|
1
|
+
const t=Symbol("Placeholder"),e=e=>{let n=0;for(const s of e)s!==t&&n++;return n},n=(e,n)=>{const s=e.length,i=e.slice(),r=n.length;let o=r,c=0;for(;o&&c<s;c++)i[c]===t&&(i[c]=n[r-o],o--);for(c=s;o;c++,o--)i[c]=n[r-o];return i},s=(t,i,r)=>{const o=t.length-i.length-e(r);if(o<1)return t(...n(i,r));{const e=(...e)=>s(t,n(i,r),e);return e.$args_left=o,e}},i=t=>(...n)=>t.length>e(n)?s(t,[],n):t(...n);function r(e){return function(n,...s){return s.length>0?n===t?(e=>function(n){return n===t?e:e(n)})(t=>e(t,s[0])):e(n,s[0]):t=>e(n,t)}}function o(t){return i(t)}const c=t=>t.length,a=/^(.*?)(8|16|32|64)(Clamped)?Array$/,l={__proto__:!0,constructor:!0,prototype:!0},h=void 0,u=1/0,d=t=>typeof t,f=t=>null===t,m=t=>"number"==d(t),p=t=>f(t)||(t=>t===h)(t),g=t=>!(t in l),{isNaN:_}=Number,y={u:"U",b:"B",n:"N",s:"S",f:"F",o:"O"},b=Symbol(),w=t=>y[t[0]]+t.slice(1),k=t=>{const e=d(t);return"object"===e?f(t)?"Null":t.constructor?.name||w(e):"number"===e&&_(t)?"NaN":w(e)},S=r((t,e)=>k(e)===t),v=r((t,e)=>t===e),P=r((t,e)=>{const n=k(t);if(v(n,k(e))&&(v(n,"Object")||v(n,"Array")||(t=>a.test(t))(n))){if(f(t)||f(e))return v(t,e);if(v(t,e))return!0;for(const n of[t,e])for(const s in n)if(!(v(n,e)&&s in t||v(n,t)&&s in e&&P(t[s],e[s])))return!1;return!0}return v(t,e)}),A=t=>()=>t,E=r((t,e)=>(e.push(t),e)),N=o((t,e,n)=>n.reduce(t,e)),j=t=>r((e,n)=>{for(let s in n)if(g(s))switch(k(n[s])){case"Array":if(t>1&&"Array"===k(e[s]))switch(t){case 2:const i=e[s],r=n[s];for(const e in r)i[e]?j(t)(i[e],r[e]):i[e]=r[e];break;case 3:e[s].push(...n[s])}else e[s]=n[s];break;case"Object":if("Object"===k(e[s])){j(t)(e[s],n[s]);break}default:e[s]=n[s]}return e}),q=j(1),z=(t,e)=>{const n=c(e);for(let s=t;s<n;s++)e[s]=e[s+1];return e.length=n-1,e},O=new Set,x=r((t,e)=>{let n,s=c(e);for(let i=0;i<s;i++){n=t(e[i]),O.has(n)?(z(i,e),s--,i--):O.add(n)}return O.clear(),e});x(t=>t);const $=i((t,e,n,s)=>t(s)?e(s):n(s)),W=(...e)=>(...n)=>{let s,i=!0;for(let r=c(e)-1;r>-1;r--)i?(i=!1,s=e[r](...n)):s=s===t?e[r]():e[r](s);return s},M=r((t,e)=>e[t]),B=o((t,e,n)=>n.slice(t,m(e)?e:u)),C=M(0);B(1,u);const T=r((t,e)=>t*e),L=r((t,e)=>e.find(t)),R=r((t,e)=>(t(e),e)),I=A(!0),U=A(!1),Z=()=>{},D=r((t,e)=>N((e,n)=>L(e=>t(n,e),e)?e:E(n,e),[],e));D(P);const F=t=>{let e,n=!1;return function(...s){return n?e:(n=!0,e=t(...s))}},J=(t,e,n)=>c(e)?p(n)?t:W(s=>s in n?J(t,B(1,u,e),n[s]):t,C)(e):n,G=o(J);G(h),W($(P(b),U,I),G(b));const H=Symbol("Placeholder"),K=t=>{let e=0;for(const n of t)n!==H&&e++;return e},Q=(t,e)=>{const n=t.length,s=t.slice(),i=e.length;let r=i,o=0;for(;r&&o<n;o++)s[o]===H&&(s[o]=e[i-r],r--);for(o=n;r;o++,r--)s[o]=e[i-r];return s},V=(t,e,n)=>{const s=t.length-e.length-K(n);if(s<1)return t(...Q(e,n));{const i=(...s)=>V(t,Q(e,n),s);return i.$args_left=s,i}},X=t=>(...e)=>t.length>K(e)?V(t,[],e):t(...e);function Y(t){return function(e,...n){return n.length>0?e===H?(t=>function(e){return e===H?t:t(e)})(e=>t(e,n[0])):t(e,n[0]):n=>t(e,n)}}function tt(t){return X(t)}const et=t=>t.length,nt=/^(.*?)(8|16|32|64)(Clamped)?Array$/,st=void 0,it=1/0,rt=t=>typeof t,ot=t=>null===t,ct={u:"U",b:"B",n:"N",s:"S",f:"F"},at=Symbol(),lt=t=>{const e=rt(t);return"object"===e?ot(t)?"Null":t.constructor.name:ct[e[0]]+e.slice(1)},ht=Y((t,e)=>lt(e)===t),ut=Y((t,e)=>t===e),dt=Y((t,e)=>{const n=lt(t);if(ut(n,lt(e))&&(ut(n,"Object")||ut(n,"Array")||(s=n,nt.test(s)))){if(ot(t)||ot(e))return ut(t,e);if(ut(t,e))return!0;for(const n of[t,e])for(const s in n)if(!(ut(n,e)&&s in t||ut(n,t)&&s in e&&dt(t[s],e[s])))return!1;return!0}var s;return ut(t,e)}),ft=Y((t,e)=>(e.push(t),e)),mt=tt((t,e,n)=>n.reduce(t,e)),pt=X((t,e,n,s)=>t(s)?e(s):n(s)),gt=tt((t,e,n)=>pt(t,e,vt,n)),_t=(...t)=>(...e)=>{let n,s=!0;for(let i=et(t)-1;i>-1;i--)s?(s=!1,n=t[i](...e)):n=n===H?t[i]():t[i](n);return n},yt=Y((t,e)=>e[t]),bt=tt((t,e,n)=>n.slice(t,(t=>"number"==rt(t))(e)?e:it)),wt=yt(0);bt(1,it);const kt=Y((t,e)=>e.find(t)),St=t=>()=>t,vt=t=>t,Pt=Y((t,e)=>e.split(t)),At=St(!0),Et=St(!1),Nt=Y((t,e)=>mt((e,n)=>kt(e=>t(n,e),e)?e:ft(n,e),[],e))(dt),jt=(t,e,n)=>et(e)?(t=>ot(t)||(t=>t===st)(t))(n)?t:_t(s=>s in n?jt(t,bt(1,it,e),n[s]):t,wt)(e):n,qt=tt(jt);qt(st),_t(pt(dt(at),Et,At),qt(at));const zt=Y((t,e)=>e.map(t)),{floor:Ot}=Math,xt="0123456789abcdefghijklmnopqrstuvwxyz",$t=ht("String"),Wt=gt($t,Pt("")),Mt=_t(t=>Object.fromEntries(t),zt((t,e)=>[t,e]),Wt);class Bt{is_str;delim;abc;abclen;c2pos;standard;setABC(t,e=""){if(this.is_str=$t(t),this.delim=e,!_t(ut(et(n=t)),et,Nt,Wt)(n))throw new Error("Not all chars are unique!");var n;this.abc=t,this.abclen=et(t),this.standard=!!this.is_str&&xt.startsWith(t),this.c2pos=Mt(t)}zip(t){const{abc:e,abclen:n,delim:s}=this;let i="",r=!0;for(;t>0;)i=e[t%n]+(r?"":s)+i,t=Ot(t/n),r=!1;return i||"0"}unzip(t){const{standard:e,abclen:n,c2pos:s,delim:i,is_str:r}=this;if("0"===t)return 0;if(e)return parseInt(t,n);const o=r?t:t.split(i),c=et(o);let a=0;for(let t=0;t<c;t++)a+=s[o[t]]*n**(c-t-1);return a}constructor(t,e){e?this.setABC(t,e):this.setABC(t||xt+"ABCDEFGHIJKLMNOPQRSTUVWXYZ")}}const Ct=new Bt;Ct.setABC.bind(Ct),Ct.zip.bind(Ct),Ct.unzip.bind(Ct);const Tt=(()=>{try{return WebSocket||null}catch{return null}})(),Lt=(t,e,n)=>t.addEventListener(e,n),Rt=(t,e)=>setTimeout(e,t),{min:It,random:Ut}=Math,Zt=t=>{if(null===Tt&&!("adapter"in t))throw new Error("\n This platform has no native WebSocket implementation.\n Please use 'ws' package as an adapter.\n See https://github.com/houd1ni/WebsocketPromisify/issues/23\n ");const e=q({data_type:"json",log:()=>null,timer:!1,url:"",timeout:1.4,reconnect:{stop_after:45,on_timeout:!0,on_break:!0,time_fn:({base:t,max:e,jitter:n},s)=>It(e,t**(s+Ut()*n)),params:{base:2,max:20,jitter:.1}},max_idle_time:1/0,lazy:!1,socket:null,adapter:(t,e)=>new WebSocket(t,e),encode:(t,e,{server:n})=>JSON.stringify({[n.id_key]:t,[n.data_key]:e}),decode:t=>JSON.parse(t),protocols:[],pipes:[],server:{id_key:"id",data_key:"data"},ping:{interval:55,out:"ping",in:"pong"}},t),n=e.url;if("/"==n[0])try{const t=location.protocol.includes("s:")?"wss":"ws";e.url=`${t}://${location.hostname}:${location.port}${n}`}catch(t){throw new Error("WSP: URL starting with / in non-browser environment!")}return e},{random:Dt}=Math,Ft=null,Jt=1/0,Gt=Promise.resolve(Ft),Ht="message",Kt="message-ext",Qt=new Bt,Vt=()=>Date.now()/1e3,Xt=T(1e3),Yt=t=>t&&clearTimeout(t),te=t=>null===t,ee=S("String"),ne=S("Object"),se=(t,e,n,s=.5)=>{const i=Xt(s),r=setTimeout(()=>{const s=t.indexOf(e);~s&&(t.splice(s),n(`could not close in ${i}ms!`))},1e3*i);t.push((...t)=>{Yt(r),e(...t)})},ie=t=>{const e=Qt.zip(2147483637*Dt()|0);return e in t?ie(t):e},re=(t,...e)=>{for(const n of t)n(...e);return t},oe=t=>(t.splice(0),t),ce=(t,e)=>e(t);class ae{ws=Ft;intentionally_closed=!1;reconnect_timeout=Ft;queue={send:new Map,on_ready:[],on_close:[],on_ready_fail:[]};handlers={open:[],close:[],message:[],[Kt]:[],error:[],timeout:[]};config={};ping_timer=Ft;idle_timer=Ft;zombie_timer=Ft;router=ce;get opened(){return 1===this.ws?.readyState}call(t,...e){for(const n of this.handlers[t])n(...e)}log(t,e=Ft,n=Ft){const{config:s}=this;setTimeout(()=>{te(n)?s.timer?s.log(t,Ft,e):s.log(t,e):s.log(t,n,e)})}resetPing(){const{config:{ping:t},ping_timer:e}=this;t&&(Yt(e),this.ping_timer=Rt(Xt(t.interval),async()=>{const{ping_timer:e,opened:n}=this;n?(this.ws.send(t.out),this.resetPing()):Yt(e)}))}resetZombieProbe(){const{config:t}=this;if(t.ping){const e=t.ping.timeout;Yt(this.zombie_timer),e!==1/0&&(this.zombie_timer=Rt(Xt(e||t.timeout),()=>this.close().catch(Z)))}}resetIdle(){const{config:{max_idle_time:t},idle_timer:e}=this;t!==1/0&&(Yt(e),this.idle_timer=Rt(Xt(t),()=>this.opened&&this.close()))}_reconnecting=!1;reconnect_start=0;async reconnect(t=0){if(this._reconnecting&&0===t)return;this.log("reconnect"),this._reconnecting=!0,this.reconnect_start=Vt(),p(this.ws)||this.terminate();const{queue:e}=this;if(t>0&&p(await this.connect()))oe(re(e.on_ready)),oe(e.on_ready_fail),this._reconnecting=!1,this.reconnect_timeout=Ft;else{const{stop_after:n,time_fn:s,params:i}=this.config.reconnect;Vt()-this.reconnect_start>n?(this.terminate(),oe(re(e.on_ready_fail)),oe(e.on_ready),this._reconnecting=!1,this.reconnect_timeout=Ft):this.reconnect_timeout=Rt(Xt(s(i,t)),this.reconnect.bind(this,t+1))}}resetReconnect(){te(this.reconnect_timeout)||(Yt(this.reconnect_timeout),this.reconnect_timeout=Ft)}initSocket(t){const{queue:e,config:n,router:s}=this;this.ws=t,oe(re(this.queue.on_ready));const{id_key:i,data_key:r}=n.server;this.call("open",t);for(const{msg:n}of e.send.values())t.send(n);this.resetReconnect(),this.resetZombieProbe(),this.resetPing(),this.resetIdle(),Lt(t,"close",async(...t)=>{this.ws=Ft,oe(re(e.on_close)),this.call("close",...t),!this.intentionally_closed&&n.reconnect.on_break&&this.reconnect()});const{ping:o}=n,c=t=>{try{const e=n.decode(t),{send:s}=this.queue;let o=!1;if(ne(e)&&i in e){const t=e[i];if(s.has(t)){const n=s.get(t),i=e[r],c=n.sent_time?Vt()-n.sent_time:Ft;this.log(Ht,i,c),n.ff(i),o=!0}}const c=o?Ht:Kt;this.log(c,e),this.call(c,{data:e})}catch(e){console.error(e,`WSP: Decode error. Got: ${t}`)}};Lt(t,Ht,t=>{const e=ee(t.data)?t.data:new Uint8Array(t.data);this.resetZombieProbe(),this.resetPing(),o&&P(e,o.in)||s(e,c)})}_opening=!1;connect(){return this.opened||this._opening||this.opened||this._opening?Gt:new Promise(t=>{this._opening=!0;const e=this.config,n=e.socket||e.adapter(e.url,e.protocols);if(p(n)||n.readyState>1)return this._opening=!1,this.ws=Ft,this.log("error","ready() on closing or closed state! status 2."),t(2);const s=F(e=>{this._opening=!1,t(e)});Lt(n,"error",F(t=>{this.ws=Ft,this.log("error","status 3. Err: "+(t.message||t)),this.call("error",t),s(3)})),n.readyState?(this.initSocket(n),s(Ft)):Lt(n,"open",F(()=>{this.log("open"),this.initSocket(n),s(Ft)}))})}get socket(){return this.ws}async ready(t=Jt){return new Promise((e,n)=>{const{on_ready:s}=this.queue;this.config.lazy||this.opened?e():t===Jt?s.push(e):se(s,e,n)})}on(t,e,n=I,s=!1){const i=t=>n(t)&&e(t);return s?Lt(this.ws,t,i):this.handlers[t].push(i),i}off(t,e,n=!1){if(n)return((t,e,n)=>t.removeEventListener(e,n))(this.ws,t,e);const s=this.handlers[t],i=s.indexOf(e);~i&&s.splice(i,1)}terminate(){this.ws?.close(),this.ws=Ft,this.intentionally_closed=!0}async close(t=.5){return new Promise((e,n)=>{te(this.ws)?e(Ft):(se(this.queue.on_close,e,n,t),this.terminate())})}open(){if(!this.opened)return this.intentionally_closed=!1,this.connect()}addEventListener(t,e,n={}){return this.on(t,e,n.predicate,n.raw)}removeEventListener(t,e,n={}){return this.off(t,e,n.raw)}async prepareMessage(t,e={}){this.log("send",t);const{config:n,queue:{send:s,on_ready_fail:i}}=this,{pipes:r,server:{data_key:o}}=n,{top:c}=e,a=ie(s);if(ne(c)&&o in c)throw new Error("\n Attempting to set data key/token via send() options!\n ");for(const e of r)t=e(t);const[l,h]=await Promise.all([n.encode(a,t,n),this.connect()]);if(h)throw new Error("ERR while opening connection > "+h);const u=c?.timeout||n.timeout,d=R(()=>s.delete(a));return{id:a,msg:l,timeout:e=>Rt(Xt(u),()=>{if(s.has(a)){this.call("timeout",t),d();const s=()=>e({"Websocket timeout expired":u,"for the message":t});n.reconnect.on_timeout?(i.push(s),this.reconnect()):s()}}),cleanup:d,send:()=>this.opened&&(this.ws.send(l),this.resetPing(),this.resetIdle())}}async send(t,e={}){const{id:n,msg:s,timeout:i,cleanup:r,send:o}=await this.prepareMessage(t,e),{queue:{send:c},config:a}=this;return new Promise((t,e)=>{const r=i(e);c.set(n,{msg:s,data_type:a.data_type,sent_time:a.timer?Vt():Ft,ff(e){Yt(r),t(e)}}),o()}).finally(r)}async*stream(t,e={}){const{id:n,msg:s,timeout:i,cleanup:r,send:o}=await this.prepareMessage(t,e),{queue:{send:c},config:a}=this;let l,h=!1,u=Ft;for(c.set(n,{msg:s,ff:t=>{t?.done&&(delete t.done,h=!0,setTimeout(r)),l(t)},data_type:a.data_type,sent_time:a.timer?Vt():Ft}),o();!h;)yield await new Promise((t,e)=>{u=i(e),l=t}).catch(r).finally(()=>Yt(u))}route(t){this.router=t}constructor(t){this.config=Zt(t),this.config.lazy||this.connect()}}export{ae as WebSocketClient};
|
package/package.json
CHANGED
|
@@ -41,7 +41,7 @@
|
|
|
41
41
|
"prod": "npm run gentypes && npm run prod:es && npm run prod:cjs",
|
|
42
42
|
"all": "npm run dev && npm run prod"
|
|
43
43
|
},
|
|
44
|
-
"version": "
|
|
44
|
+
"version": "3.0.0",
|
|
45
45
|
"type": "module",
|
|
46
46
|
"exports": {
|
|
47
47
|
".": {
|
|
@@ -57,9 +57,9 @@
|
|
|
57
57
|
"@rollup/plugin-replace": "^6.0.3",
|
|
58
58
|
"@rollup/plugin-terser": "^1.0.0",
|
|
59
59
|
"@types/express": "^5.0.6",
|
|
60
|
-
"@types/node": "^25.
|
|
60
|
+
"@types/node": "^25.7.0",
|
|
61
61
|
"@types/ws": "^8.18.1",
|
|
62
|
-
"codecov": "^3.
|
|
62
|
+
"codecov": "^3.8.3",
|
|
63
63
|
"cross-env": "^10.1.0",
|
|
64
64
|
"dts-bundle-generator": "^9.5.1",
|
|
65
65
|
"nyc": "^18.0.0",
|
|
@@ -67,9 +67,10 @@
|
|
|
67
67
|
"rollup-plugin-typescript2": "^0.37.0",
|
|
68
68
|
"ts-node": "^10.9.2",
|
|
69
69
|
"tsx": "^4.21.0",
|
|
70
|
+
"type-fest": "^5.6.0",
|
|
70
71
|
"typescript": "^6.0.3",
|
|
71
72
|
"uvu": "^0.5.6",
|
|
72
|
-
"ws": "^8.20.
|
|
73
|
+
"ws": "^8.20.1"
|
|
73
74
|
},
|
|
74
75
|
"types": "./dist/bundle.d.ts",
|
|
75
76
|
"dependencies": {
|
package/tsconfig.json
CHANGED