wspromisify
Advanced tools
Comparing version 2.1.2 to 2.1.3
@@ -1,1 +0,1 @@ | ||
function t(t,e,o,n){return new(o||(o=Promise))(function(s,i){function l(t){try{c(n.next(t))}catch(t){i(t)}}function r(t){try{c(n.throw(t))}catch(t){i(t)}}function c(t){t.done?s(t.value):new o(function(e){e(t.value)}).then(l,r)}c((n=n.apply(t,e||[])).next())})}const e="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",o=e.length-1;var n=t=>{const n=[];for(;t>=1;)n.push(e[t%(o+1)]),t=t/o|0;return n.join("")};const s=(t,e,o)=>t.addEventListener(e,o),i=t=>{let e=!1,o=null;return(...n)=>e?o:(e=!0,o=t(...n))},l=(t,e)=>setTimeout(e,t),r=function(e){const o=this.config;this.open=!0,this.onReadyQueue.forEach(t=>t()),this.onReadyQueue.splice(0);const{id_key:n,data_key:i}=o.server;this.messages.forEach(t=>t.send()),null!==this.reconnect_timeout&&(clearInterval(this.reconnect_timeout),this.reconnect_timeout=null),s(e,"close",()=>t(this,void 0,void 0,function*(){this.log("Closed."),this.open=!1,this.onCloseQueue.forEach(t=>t()),this.onCloseQueue=[];const e=o.reconnect;if("number"!=typeof e||isNaN(e)||this.forcibly_closed)this.ws=null,this.open=null;else{const o=()=>t(this,void 0,void 0,function*(){this.log("Trying to reconnect..."),null!==this.ws&&(this.ws.close(),this.ws=null),null!==(yield this.connect())&&(this.reconnect_timeout=setTimeout(o,1e3*e))});o()}this.forcibly_closed=!1})),s(e,"message",t=>{try{const e=o.decode(t.data);if(e[n]){const t=this.queue[e[n]];if(t){const o=t.sent_time?Date.now()-t.sent_time:null;this.log("Message.",e[i],o),t.ff(e[i]),clearTimeout(t.timeout),delete this.queue[e[n]]}}}catch(e){console.error(e,`Decode error. Got: ${t.data}`)}})},c=function(t){if(!0===this.open)return t(null);const e=this.config,o=e.socket||e.adapter(`ws://${e.url}`,e.protocols);if(this.ws=o,!o||o.readyState>1)return this.ws=null,this.log("Error: ready() on closing or closed state! Status 2."),t(2);s(o,"error",i(()=>(this.ws=null,this.log("Error status 3."),t(3)))),o.readyState?(r.call(this,o),t(null)):s(o,"open",i(()=>(this.log("Opened."),r.call(this,o),t(null))))},u={data_type:"json",log:()=>null,timer:!1,url:"localhost",timeout:1400,reconnect:2,lazy:!1,socket:null,adapter:(t,e)=>new WebSocket(t,e),encode:(t,e,{server:o})=>JSON.stringify({[o.id_key]:t,[o.data_key]:e}),decode:t=>JSON.parse(t),protocols:[],pipes:[],server:{id_key:"id",data_key:"data"}},h=t=>{const e=Object.assign(u,t),o=e.url;if("/"==o[0])try{e.url=`${location.hostname}:${location.port}${o}`}catch(t){throw new Error("WSP: URL starting with / in non-browser environment!")}return e},a=Math.pow(2,31)-1;export default class{constructor(t={}){this.open=null,this.ws=null,this.forcibly_closed=!1,this.reconnect_timeout=null,this.queue={},this.messages=[],this.onReadyQueue=[],this.onCloseQueue=[],this.config={},this.config=h(t),this.init_flush(),this.open=!1,this.reconnect_timeout=null,this.forcibly_closed=!1,this.config.lazy||this.connect()}init_flush(){this.queue={},this.messages=[]}log(t,e=null,o=null){const n=this.config;t=`WSP: ${t}`,null!==o?n.log(t,o,e):n.timer?n.log(t,null,e):n.log(t,e)}connect(){return t(this,void 0,void 0,function*(){return new Promise(t=>{c.call(this,t)})})}get socket(){return this.ws}ready(){return t(this,void 0,void 0,function*(){return new Promise(t=>{this.open?t():this.onReadyQueue.push(t)})})}on(t,e,o){return s(this.ws,t,t=>{o&&!o(t)||e(t)})}close(){return t(this,void 0,void 0,function*(){return new Promise((t,e)=>{null===this.ws?e("WSP: closing a non-inited socket!"):(this.open=null,this.onCloseQueue.push(()=>{this.init_flush(),this.ws=null,this.forcibly_closed=!0,t()}),this.ws.close())})})}send(e,o={}){return t(this,void 0,void 0,function*(){this.log("Send.",e);const t=this.config,s={},i=t.server.data_key,r=t.lazy&&!this.open,c=n(Math.random()*(a-10)|0);if("object"==typeof o.top){if(o.top[i])throw new Error("Attempting to set data key/token via send() options!");Object.assign(s,o.top)}if(t.pipes.forEach(t=>e=t(e)),console.log({open:this.open,encoded:t.encode(c,e,t)}),!0===this.open)this.ws.send(t.encode(c,e,t));else if(!1===this.open||r)this.messages.push({send:()=>this.ws.send(t.encode(c,e,t))}),r&&this.connect();else if(null===this.open)throw new Error("Attempting to send via closed WebSocket connection!");return new Promise((e,o)=>{this.queue[c]={ff:e,data_type:t.data_type,sent_time:t.timer?Date.now():null,timeout:l(t.timeout,()=>{this.queue[c]&&(o({"Websocket timeout expired: ":t.timeout,"for the message":s}),delete this.queue[c])})}})})}} | ||
function t(t,e,o,s){return new(o||(o=Promise))(function(n,i){function l(t){try{c(s.next(t))}catch(t){i(t)}}function r(t){try{c(s.throw(t))}catch(t){i(t)}}function c(t){t.done?n(t.value):new o(function(e){e(t.value)}).then(l,r)}c((s=s.apply(t,e||[])).next())})}const e="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",o=e.length-1;var s=t=>{const s=[];for(;t>=1;)s.push(e[t%(o+1)]),t=t/o|0;return s.join("")};const n=(t,e,o)=>t.addEventListener(e,o),i=t=>{let e=!1,o=null;return(...s)=>e?o:(e=!0,o=t(...s))},l=(t,e)=>setTimeout(e,t),r=function(e){const o=this.config;this.open=!0,this.onReadyQueue.forEach(t=>t()),this.onReadyQueue.splice(0);const{id_key:s,data_key:i}=o.server;this.messages.forEach(t=>t.send()),null!==this.reconnect_timeout&&(clearInterval(this.reconnect_timeout),this.reconnect_timeout=null),n(e,"close",()=>t(this,void 0,void 0,function*(){this.log("Closed."),this.open=!1,this.onCloseQueue.forEach(t=>t()),this.onCloseQueue=[];const e=o.reconnect;if("number"!=typeof e||isNaN(e)||this.forcibly_closed)this.ws=null,this.open=null;else{const o=()=>t(this,void 0,void 0,function*(){this.log("Trying to reconnect..."),null!==this.ws&&(this.ws.close(),this.ws=null),null!==(yield this.connect())&&(this.reconnect_timeout=setTimeout(o,1e3*e))});o()}this.forcibly_closed=!1})),n(e,"message",t=>{try{const e=o.decode(t.data);if(e[s]){const t=this.queue[e[s]];if(t){const o=t.sent_time?Date.now()-t.sent_time:null;this.log("Message.",e[i],o),t.ff(e[i]),clearTimeout(t.timeout),delete this.queue[e[s]]}}}catch(e){console.error(e,`Decode error. Got: ${t.data}`)}})},c=function(t){if(!0===this.open)return t(null);const e=this.config,o=e.socket||e.adapter(`ws://${e.url}`,e.protocols);if(this.ws=o,!o||o.readyState>1)return this.ws=null,this.log("Error: ready() on closing or closed state! Status 2."),t(2);n(o,"error",i(()=>(this.ws=null,this.log("Error status 3."),t(3)))),o.readyState?(r.call(this,o),t(null)):n(o,"open",i(()=>(this.log("Opened."),r.call(this,o),t(null))))},u={data_type:"json",log:()=>null,timer:!1,url:"localhost",timeout:1400,reconnect:2,lazy:!1,socket:null,adapter:(t,e)=>new WebSocket(t,e),encode:(t,e,{server:o})=>JSON.stringify({[o.id_key]:t,[o.data_key]:e}),decode:t=>JSON.parse(t),protocols:[],pipes:[],server:{id_key:"id",data_key:"data"}},h=t=>{const e=Object.assign({},u,t),o=e.url;if("/"==o[0])try{e.url=`${location.hostname}:${location.port}${o}`}catch(t){throw new Error("WSP: URL starting with / in non-browser environment!")}return e},a=Math.pow(2,31)-1;export default class{constructor(t={}){this.open=null,this.ws=null,this.forcibly_closed=!1,this.reconnect_timeout=null,this.queue={},this.messages=[],this.onReadyQueue=[],this.onCloseQueue=[],this.config={},this.config=h(t),this.init_flush(),this.open=!1,this.reconnect_timeout=null,this.forcibly_closed=!1,this.config.lazy||this.connect()}init_flush(){this.queue={},this.messages=[]}log(t,e=null,o=null){const s=this.config;t=`WSP: ${t}`,null!==o?s.log(t,o,e):s.timer?s.log(t,null,e):s.log(t,e)}connect(){return t(this,void 0,void 0,function*(){return new Promise(t=>{c.call(this,t)})})}get socket(){return this.ws}ready(){return t(this,void 0,void 0,function*(){return new Promise(t=>{this.open?t():this.onReadyQueue.push(t)})})}on(t,e,o){return n(this.ws,t,t=>{o&&!o(t)||e(t)})}close(){return t(this,void 0,void 0,function*(){return new Promise((t,e)=>{null===this.ws?e("WSP: closing a non-inited socket!"):(this.open=null,this.onCloseQueue.push(()=>{this.init_flush(),this.ws=null,this.forcibly_closed=!0,t()}),this.ws.close())})})}send(e,o={}){return t(this,void 0,void 0,function*(){this.log("Send.",e);const t=this.config,n={},i=t.server.data_key,r=t.lazy&&!this.open,c=s(Math.random()*(a-10)|0);if("object"==typeof o.top){if(o.top[i])throw new Error("Attempting to set data key/token via send() options!");Object.assign(n,o.top)}if(t.pipes.forEach(t=>e=t(e)),!0===this.open)this.ws.send(t.encode(c,e,t));else if(!1===this.open||r)this.messages.push({send:()=>this.ws.send(t.encode(c,e,t))}),r&&this.connect();else if(null===this.open)throw new Error("Attempting to send via closed WebSocket connection!");return new Promise((e,o)=>{this.queue[c]={ff:e,data_type:t.data_type,sent_time:t.timer?Date.now():null,timeout:l(t.timeout,()=>{this.queue[c]&&(o({"Websocket timeout expired: ":t.timeout,"for the message":n}),delete this.queue[c])})}})})}} |
@@ -1,1 +0,1 @@ | ||
"use strict";function __awaiter(e,t,n,o){return new(n||(n=Promise))(function(i,s){function c(e){try{l(o.next(e))}catch(e){s(e)}}function r(e){try{l(o.throw(e))}catch(e){s(e)}}function l(e){e.done?i(e.value):new n(function(t){t(e.value)}).then(c,r)}l((o=o.apply(e,t||[])).next())})}const abc="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",ln=abc.length-1;var packNumber=e=>{const t=[];for(;e>=1;)t.push(abc[e%(ln+1)]),e=e/ln|0;return t.join("")};const add_event=(e,t,n)=>e.addEventListener(t,n),once=e=>{let t=!1,n=null;return(...o)=>t?n:(t=!0,n=e(...o))},sett=(e,t)=>setTimeout(t,e),init=function(e){const t=this.config;this.open=!0,this.onReadyQueue.forEach(e=>e()),this.onReadyQueue.splice(0);const{id_key:n,data_key:o}=t.server;this.messages.forEach(e=>e.send()),null!==this.reconnect_timeout&&(clearInterval(this.reconnect_timeout),this.reconnect_timeout=null),add_event(e,"close",()=>__awaiter(this,void 0,void 0,function*(){this.log("Closed."),this.open=!1,this.onCloseQueue.forEach(e=>e()),this.onCloseQueue=[];const e=t.reconnect;if("number"!=typeof e||isNaN(e)||this.forcibly_closed)this.ws=null,this.open=null;else{const t=()=>__awaiter(this,void 0,void 0,function*(){this.log("Trying to reconnect..."),null!==this.ws&&(this.ws.close(),this.ws=null),null!==(yield this.connect())&&(this.reconnect_timeout=setTimeout(t,1e3*e))});t()}this.forcibly_closed=!1})),add_event(e,"message",e=>{try{const i=t.decode(e.data);if(i[n]){const e=this.queue[i[n]];if(e){const t=e.sent_time?Date.now()-e.sent_time:null;this.log("Message.",i[o],t),e.ff(i[o]),clearTimeout(e.timeout),delete this.queue[i[n]]}}}catch(t){console.error(t,`Decode error. Got: ${e.data}`)}})},connectLib=function(e){if(!0===this.open)return e(null);const t=this.config,n=t.socket||t.adapter(`ws://${t.url}`,t.protocols);if(this.ws=n,!n||n.readyState>1)return this.ws=null,this.log("Error: ready() on closing or closed state! Status 2."),e(2);add_event(n,"error",once(()=>(this.ws=null,this.log("Error status 3."),e(3)))),n.readyState?(init.call(this,n),e(null)):add_event(n,"open",once(()=>(this.log("Opened."),init.call(this,n),e(null))))},default_config={data_type:"json",log:()=>null,timer:!1,url:"localhost",timeout:1400,reconnect:2,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"}},enrichConfig=e=>{const t=Object.assign(default_config,e),n=t.url;if("/"==n[0])try{t.url=`${location.hostname}:${location.port}${n}`}catch(e){throw new Error("WSP: URL starting with / in non-browser environment!")}return t},MAX_32=Math.pow(2,31)-1;class WebSocketClient{constructor(e={}){this.open=null,this.ws=null,this.forcibly_closed=!1,this.reconnect_timeout=null,this.queue={},this.messages=[],this.onReadyQueue=[],this.onCloseQueue=[],this.config={},this.config=enrichConfig(e),this.init_flush(),this.open=!1,this.reconnect_timeout=null,this.forcibly_closed=!1,this.config.lazy||this.connect()}init_flush(){this.queue={},this.messages=[]}log(e,t=null,n=null){const o=this.config;e=`WSP: ${e}`,null!==n?o.log(e,n,t):o.timer?o.log(e,null,t):o.log(e,t)}connect(){return __awaiter(this,void 0,void 0,function*(){return new Promise(e=>{connectLib.call(this,e)})})}get socket(){return this.ws}ready(){return __awaiter(this,void 0,void 0,function*(){return new Promise(e=>{this.open?e():this.onReadyQueue.push(e)})})}on(e,t,n){return add_event(this.ws,e,e=>{n&&!n(e)||t(e)})}close(){return __awaiter(this,void 0,void 0,function*(){return new Promise((e,t)=>{null===this.ws?t("WSP: closing a non-inited socket!"):(this.open=null,this.onCloseQueue.push(()=>{this.init_flush(),this.ws=null,this.forcibly_closed=!0,e()}),this.ws.close())})})}send(e,t={}){return __awaiter(this,void 0,void 0,function*(){this.log("Send.",e);const n=this.config,o={},i=n.server.data_key,s=n.lazy&&!this.open,c=packNumber(Math.random()*(MAX_32-10)|0);if("object"==typeof t.top){if(t.top[i])throw new Error("Attempting to set data key/token via send() options!");Object.assign(o,t.top)}if(n.pipes.forEach(t=>e=t(e)),console.log({open:this.open,encoded:n.encode(c,e,n)}),!0===this.open)this.ws.send(n.encode(c,e,n));else if(!1===this.open||s)this.messages.push({send:()=>this.ws.send(n.encode(c,e,n))}),s&&this.connect();else if(null===this.open)throw new Error("Attempting to send via closed WebSocket connection!");return new Promise((e,t)=>{this.queue[c]={ff:e,data_type:n.data_type,sent_time:n.timer?Date.now():null,timeout:sett(n.timeout,()=>{this.queue[c]&&(t({"Websocket timeout expired: ":n.timeout,"for the message":o}),delete this.queue[c])})}})})}}module.exports=WebSocketClient; | ||
"use strict";function __awaiter(e,t,n,o){return new(n||(n=Promise))(function(i,s){function r(e){try{l(o.next(e))}catch(e){s(e)}}function c(e){try{l(o.throw(e))}catch(e){s(e)}}function l(e){e.done?i(e.value):new n(function(t){t(e.value)}).then(r,c)}l((o=o.apply(e,t||[])).next())})}const abc="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",ln=abc.length-1;var packNumber=e=>{const t=[];for(;e>=1;)t.push(abc[e%(ln+1)]),e=e/ln|0;return t.join("")};const add_event=(e,t,n)=>e.addEventListener(t,n),once=e=>{let t=!1,n=null;return(...o)=>t?n:(t=!0,n=e(...o))},sett=(e,t)=>setTimeout(t,e),init=function(e){const t=this.config;this.open=!0,this.onReadyQueue.forEach(e=>e()),this.onReadyQueue.splice(0);const{id_key:n,data_key:o}=t.server;this.messages.forEach(e=>e.send()),null!==this.reconnect_timeout&&(clearInterval(this.reconnect_timeout),this.reconnect_timeout=null),add_event(e,"close",()=>__awaiter(this,void 0,void 0,function*(){this.log("Closed."),this.open=!1,this.onCloseQueue.forEach(e=>e()),this.onCloseQueue=[];const e=t.reconnect;if("number"!=typeof e||isNaN(e)||this.forcibly_closed)this.ws=null,this.open=null;else{const t=()=>__awaiter(this,void 0,void 0,function*(){this.log("Trying to reconnect..."),null!==this.ws&&(this.ws.close(),this.ws=null),null!==(yield this.connect())&&(this.reconnect_timeout=setTimeout(t,1e3*e))});t()}this.forcibly_closed=!1})),add_event(e,"message",e=>{try{const i=t.decode(e.data);if(i[n]){const e=this.queue[i[n]];if(e){const t=e.sent_time?Date.now()-e.sent_time:null;this.log("Message.",i[o],t),e.ff(i[o]),clearTimeout(e.timeout),delete this.queue[i[n]]}}}catch(t){console.error(t,`Decode error. Got: ${e.data}`)}})},connectLib=function(e){if(!0===this.open)return e(null);const t=this.config,n=t.socket||t.adapter(`ws://${t.url}`,t.protocols);if(this.ws=n,!n||n.readyState>1)return this.ws=null,this.log("Error: ready() on closing or closed state! Status 2."),e(2);add_event(n,"error",once(()=>(this.ws=null,this.log("Error status 3."),e(3)))),n.readyState?(init.call(this,n),e(null)):add_event(n,"open",once(()=>(this.log("Opened."),init.call(this,n),e(null))))},default_config={data_type:"json",log:()=>null,timer:!1,url:"localhost",timeout:1400,reconnect:2,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"}},enrichConfig=e=>{const t=Object.assign({},default_config,e),n=t.url;if("/"==n[0])try{t.url=`${location.hostname}:${location.port}${n}`}catch(e){throw new Error("WSP: URL starting with / in non-browser environment!")}return t},MAX_32=Math.pow(2,31)-1;class WebSocketClient{constructor(e={}){this.open=null,this.ws=null,this.forcibly_closed=!1,this.reconnect_timeout=null,this.queue={},this.messages=[],this.onReadyQueue=[],this.onCloseQueue=[],this.config={},this.config=enrichConfig(e),this.init_flush(),this.open=!1,this.reconnect_timeout=null,this.forcibly_closed=!1,this.config.lazy||this.connect()}init_flush(){this.queue={},this.messages=[]}log(e,t=null,n=null){const o=this.config;e=`WSP: ${e}`,null!==n?o.log(e,n,t):o.timer?o.log(e,null,t):o.log(e,t)}connect(){return __awaiter(this,void 0,void 0,function*(){return new Promise(e=>{connectLib.call(this,e)})})}get socket(){return this.ws}ready(){return __awaiter(this,void 0,void 0,function*(){return new Promise(e=>{this.open?e():this.onReadyQueue.push(e)})})}on(e,t,n){return add_event(this.ws,e,e=>{n&&!n(e)||t(e)})}close(){return __awaiter(this,void 0,void 0,function*(){return new Promise((e,t)=>{null===this.ws?t("WSP: closing a non-inited socket!"):(this.open=null,this.onCloseQueue.push(()=>{this.init_flush(),this.ws=null,this.forcibly_closed=!0,e()}),this.ws.close())})})}send(e,t={}){return __awaiter(this,void 0,void 0,function*(){this.log("Send.",e);const n=this.config,o={},i=n.server.data_key,s=n.lazy&&!this.open,r=packNumber(Math.random()*(MAX_32-10)|0);if("object"==typeof t.top){if(t.top[i])throw new Error("Attempting to set data key/token via send() options!");Object.assign(o,t.top)}if(n.pipes.forEach(t=>e=t(e)),!0===this.open)this.ws.send(n.encode(r,e,n));else if(!1===this.open||s)this.messages.push({send:()=>this.ws.send(n.encode(r,e,n))}),s&&this.connect();else if(null===this.open)throw new Error("Attempting to send via closed WebSocket connection!");return new Promise((e,t)=>{this.queue[r]={ff:e,data_type:n.data_type,sent_time:n.timer?Date.now():null,timeout:sett(n.timeout,()=>{this.queue[r]&&(t({"Websocket timeout expired: ":n.timeout,"for the message":o}),delete this.queue[r])})}})})}}module.exports=WebSocketClient; |
@@ -45,3 +45,3 @@ { | ||
}, | ||
"version": "2.1.2", | ||
"version": "2.1.3", | ||
"ava": { | ||
@@ -48,0 +48,0 @@ "files": [ |
@@ -30,2 +30,3 @@ import './types' | ||
const full_config: wsc.Config = Object.assign( | ||
{}, | ||
default_config, | ||
@@ -32,0 +33,0 @@ config |
@@ -114,3 +114,2 @@ | ||
console.log({open: this.open, encoded: config.encode(message_id, message_data, config)}) | ||
if(this.open === true) { | ||
@@ -117,0 +116,0 @@ this.ws.send(config.encode(message_id, message_data, config)) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
61730