Socket
Socket
Sign inDemoInstall

wspromisify

Package Overview
Dependencies
Maintainers
1
Versions
49
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

wspromisify - npm Package Compare versions

Comparing version 2.1.1 to 2.1.2

2

dist/ws.esm.js

@@ -1,1 +0,1 @@

function e(e,t,o,n){return new(o||(o=Promise))(function(s,i){function r(e){try{c(n.next(e))}catch(e){i(e)}}function l(e){try{c(n.throw(e))}catch(e){i(e)}}function c(e){e.done?s(e.value):new o(function(t){t(e.value)}).then(r,l)}c((n=n.apply(e,t||[])).next())})}const t="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",o=t.length-1;var n=e=>{const n=[];for(;e>=1;)n.push(t[e%(o+1)]),e=e/o|0;return n.join("")};const s=(e,t,o)=>e.addEventListener(t,o),i=e=>{let t=!1,o=null;return(...n)=>t?o:(t=!0,o=e(...n))},r=(e,t)=>setTimeout(t,e),l=function(t){const o=this.config;this.open=!0,this.onReadyQueue.forEach(e=>e()),this.onReadyQueue.splice(0);const{id_key:n,data_key:i}=o.server;this.messages.forEach(e=>e.send()),null!==this.reconnect_timeout&&(clearInterval(this.reconnect_timeout),this.reconnect_timeout=null),s(t,"close",()=>e(this,void 0,void 0,function*(){this.log("Closed."),this.open=!1,this.onCloseQueue.forEach(e=>e()),this.onCloseQueue=[];const t=o.reconnect;if("number"!=typeof t||isNaN(t)||this.forcibly_closed)this.ws=null,this.open=null;else{const o=()=>e(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*t))});o()}this.forcibly_closed=!1})),s(t,"message",e=>{try{const t=o.decode(e.data);if(t[n]){const e=this.queue[t[n]];if(e){const o=e.sent_time?Date.now()-e.sent_time:null;this.log("Message.",t[i],o),e.ff(t[i]),clearTimeout(e.timeout),delete this.queue[t[n]]}}}catch(t){console.error(t,`Decode error. Got: ${e.data}`)}})},c=function(e){if(!0===this.open)return e(null);const t=this.config,o=t.socket||t.adapter(`ws://${t.url}`,t.protocols);if(this.ws=o,s(o,"error",i(()=>(this.ws=null,this.log("Error status 3."),e(3)))),!o||o.readyState>1)throw new Error("WSP: Error: ready() on closing or closed state!");o.readyState?(l.call(this,o),e(null)):s(o,"open",i(()=>(this.log("Opened."),l.call(this,o),e(null))))},u={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:o})=>JSON.stringify({[o.id_key]:e,[o.data_key]:t}),decode:e=>JSON.parse(e),protocols:[],pipes:[],server:{id_key:"id",data_key:"data"}},h=e=>{const t=Object.assign(u,e),o=t.url;if("/"==o[0])try{t.url=`${location.hostname}:${location.port}${o}`}catch(e){throw new Error("WSP: URL starting with / in non-browser environment!")}return t},a=Math.pow(2,31)-1;export default class{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=h(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,o=null){const n=this.config;e=`WSP: ${e}`,null!==o?n.log(e,o,t):n.timer?n.log(e,null,t):n.log(e,t)}connect(){return e(this,void 0,void 0,function*(){return new Promise(e=>{c.call(this,e)})})}get socket(){return this.ws}ready(){return e(this,void 0,void 0,function*(){return new Promise(e=>{this.open?e():this.onReadyQueue.push(e)})})}on(e,t,o){return s(this.ws,e,e=>{o&&!o(e)||t(e)})}close(){return e(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(t,o={}){return e(this,void 0,void 0,function*(){this.log("Send.",t);const e=this.config,s={},i=e.server.data_key,l=e.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(e.pipes.forEach(e=>t=e(t)),console.log({open:this.open,encoded:e.encode(c,t,e)}),!0===this.open)this.ws.send(e.encode(c,t,e));else if(!1===this.open||l)this.messages.push({send:()=>this.ws.send(e.encode(c,t,e))}),l&&this.connect();else if(null===this.open)throw new Error("Attempting to send via closed WebSocket connection!");return new Promise((t,o)=>{this.queue[c]={ff:t,data_type:e.data_type,sent_time:e.timer?Date.now():null,timeout:r(e.timeout,()=>{this.queue[c]&&(o({"Websocket timeout expired: ":e.timeout,"for the message":s}),delete this.queue[c])})}})})}}
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])})}})})}}

@@ -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,add_event(n,"error",once(()=>(this.ws=null,this.log("Error status 3."),e(3)))),!n||n.readyState>1)throw new Error("WSP: Error: ready() on closing or closed state!");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 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;

@@ -45,3 +45,3 @@ {

},
"version": "2.1.1",
"version": "2.1.2",
"ava": {

@@ -48,0 +48,0 @@ "files": [

@@ -8,3 +8,3 @@ # WebsocketPromisify

Makes websocket's API just like REST with Promise-like API, with native Promises.
Has a lot of yummies and very lightweight (less than 5kb in gzip)!
Has a lot of yummies and very lightweight (less than 3kb in gzip)!

@@ -11,0 +11,0 @@ // If you detected some bug, want some stuff to be added, feel free to open an issue!

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc