Socket
Socket
Sign inDemoInstall

graphql-ws

Package Overview
Dependencies
1
Maintainers
1
Versions
103
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 5.7.0 to 5.8.0

27

lib/client.d.ts

@@ -188,3 +188,3 @@ /**

*
* @default 0 // close immediately
* @default 0
*/

@@ -266,2 +266,6 @@ lazyCloseTimeout?: number;

*
* In addition to the aforementioned close events, any _non-CloseEvent_ connection problem
* is considered fatal by default. However, this specific behaviour can be altered by using
* the `shouldRetry` option.
*
* These events are reported immediately and the client will not reconnect.

@@ -278,6 +282,23 @@ *

*
* @default Randomised exponential backoff
* @default 'Randomised exponential backoff'
*/
retryWait?: (retries: number) => Promise<void>;
/**
* Check if the close event or connection error is fatal. If you return `false`,
* the client will fail immediately without additional retries; however, if you
* return `true`, the client will keep retrying until the `retryAttempts` have
* been exceeded.
*
* The argument is whatever has been thrown during the connection phase.
*
* Beware, the library classifies a few close events as fatal regardless of
* what is returned here. They are listed in the documentation of the `retryAttempts`
* option.
*
* @default 'Only `CloseEvent`s'
*/
shouldRetry?: (errOrCloseEvent: unknown) => boolean;
/**
* @deprecated Use `shouldRetry` instead.
*
* Check if the close event or connection error is fatal. If you return `true`,

@@ -295,3 +316,3 @@ * the client will fail immediately without additional retries; however, if you

*
* @default Non close events
* @default 'Any non-`CloseEvent`'
*/

@@ -298,0 +319,0 @@ isFatalConnectionProblem?: (errOrCloseEvent: unknown) => boolean;

7

lib/client.js

@@ -41,3 +41,3 @@ "use strict";

Math.floor(Math.random() * (3000 - 300) + 300)));
}, isFatalConnectionProblem = (errOrCloseEvent) =>
}, shouldRetry = isLikeCloseEvent, isFatalConnectionProblem = (errOrCloseEvent) =>
// non `CloseEvent`s are fatal by default

@@ -330,3 +330,6 @@ !isLikeCloseEvent(errOrCloseEvent), on, webSocketImpl,

throw errOrCloseEvent;
// throw fatal connection problems immediately
// throw non-retryable connection problems
if (!shouldRetry(errOrCloseEvent))
throw errOrCloseEvent;
// @deprecated throw fatal connection problems immediately
if (isFatalConnectionProblem(errOrCloseEvent))

@@ -333,0 +336,0 @@ throw errOrCloseEvent;

{
"name": "graphql-ws",
"version": "5.7.0",
"version": "5.8.0",
"description": "Coherent, zero-dependency, lazy, simple, GraphQL over WebSocket Protocol compliant server and client",

@@ -92,3 +92,3 @@ "keywords": [

"@babel/preset-typescript": "^7.16.7",
"@rollup/plugin-typescript": "^8.3.1",
"@rollup/plugin-typescript": "^8.3.2",
"@semantic-release/changelog": "^6.0.1",

@@ -98,11 +98,11 @@ "@semantic-release/git": "^10.0.1",

"@types/ws": "^8.5.3",
"@typescript-eslint/eslint-plugin": "^5.18.0",
"@typescript-eslint/parser": "^5.18.0",
"@typescript-eslint/eslint-plugin": "^5.20.0",
"@typescript-eslint/parser": "^5.20.0",
"babel-jest": "^27.5.1",
"eslint": "^8.12.0",
"eslint": "^8.14.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-prettier": "^4.0.0",
"fastify": "^3.28.0",
"fastify-websocket": "^4.2.1",
"glob": "^7.2.0",
"fastify-websocket": "^4.2.2",
"glob": "^8.0.1",
"graphql": "^16.3.0",

@@ -112,11 +112,11 @@ "jest": "^27.5.1",

"replacestream": "^4.0.3",
"rollup": "^2.70.1",
"rollup": "^2.70.2",
"rollup-plugin-terser": "^7.0.2",
"semantic-release": "^19.0.2",
"subscriptions-transport-ws": "^0.11.0",
"tslib": "^2.3.1",
"typedoc": "^0.22.14",
"typedoc-plugin-markdown": "^3.11.14",
"tslib": "^2.4.0",
"typedoc": "^0.22.15",
"typedoc-plugin-markdown": "^3.12.1",
"typescript": "^4.6.3",
"uWebSockets.js": "uNetworking/uWebSockets.js#v20.6.0",
"uWebSockets.js": "uNetworking/uWebSockets.js#v20.8.0",
"ws": "^8.5.0",

@@ -123,0 +123,0 @@ "ws7": "npm:ws@^7.5.7"

@@ -10,3 +10,3 @@ <div align="center">

<i>Use [Server-sent events (SSE)](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events) instead? Check out <b>[graphql-sse](https://github.com/enisdenjo/graphql-sse)</b>!</i>
<i>Use [Server-Sent Events (SSE)](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events) instead? Check out <b>[graphql-sse](https://github.com/enisdenjo/graphql-sse)</b>!</i>

@@ -347,2 +347,3 @@ <br />

connectionParams: () => {
// Note: getSession() is a placeholder function created by you
const session = getSession();

@@ -431,2 +432,3 @@ if (!session) {

connectionParams: () => {
// Note: getSession() is a placeholder function created by you
const session = getSession();

@@ -570,2 +572,24 @@ if (!session) {

<details id="retry-non-close-events">
<summary><a href="#retry-non-close-events">🔗</a> Client usage with retry on any connection problem</summary>
```typescript
import { createClient } from 'graphql-ws';
import { waitForHealthy } from './my-servers';
const client = createClient({
url: 'ws://any.retry:4000/graphql',
// by default the client will immediately fail on any non-fatal
// `CloseEvent` problem thrown during the connection phase
//
// see `retryAttempts` documentation about which `CloseEvent`s are
// considered fatal regardless
shouldRetry: () => true,
// or pre v5.8.0:
// isFatalConnectionProblem: () => false,
});
```
</details>
<details id="retry-strategy">

@@ -578,6 +602,4 @@ <summary><a href="#retry-strategy">🔗</a> Client usage with custom retry timeout strategy</summary>

const url = 'ws://i.want.retry:4000/control/graphql';
const client = createClient({
url,
url: 'ws://i.want.retry:4000/control/graphql',
retryWait: async function waitForServerHealthyBeforeRetry() {

@@ -584,0 +606,0 @@ // if you have a server healthcheck, you can wait for it to become

@@ -200,3 +200,3 @@ (function (global, factory) {

Math.floor(Math.random() * (3000 - 300) + 300)));
}, isFatalConnectionProblem = (errOrCloseEvent) =>
}, shouldRetry = isLikeCloseEvent, isFatalConnectionProblem = (errOrCloseEvent) =>
// non `CloseEvent`s are fatal by default

@@ -489,3 +489,6 @@ !isLikeCloseEvent(errOrCloseEvent), on, webSocketImpl,

throw errOrCloseEvent;
// throw fatal connection problems immediately
// throw non-retryable connection problems
if (!shouldRetry(errOrCloseEvent))
throw errOrCloseEvent;
// @deprecated throw fatal connection problems immediately
if (isFatalConnectionProblem(errOrCloseEvent))

@@ -492,0 +495,0 @@ throw errOrCloseEvent;

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

!function(e,o){"object"==typeof exports&&"undefined"!=typeof module?o(exports):"function"==typeof define&&define.amd?define(["exports"],o):o((e="undefined"!=typeof globalThis?globalThis:e||self).graphqlWs={})}(this,(function(e){"use strict";const o=Object.prototype.hasOwnProperty;function n(e){return"object"==typeof e&&null!==e}function t(e,n){return o.call(e,n)}function r(e,t){return o.call(e,t)&&n(e[t])}function i(e,n){return o.call(e,n)&&"string"==typeof e[n]}function a(e,o){return e.length<124?e:o}const s="graphql-transport-ws";var l,c;function d(o){if(n(o)){if(!i(o,"type"))return!1;switch(o.type){case e.MessageType.ConnectionInit:case e.MessageType.ConnectionAck:case e.MessageType.Ping:case e.MessageType.Pong:return!t(o,"payload")||void 0===o.payload||n(o.payload);case e.MessageType.Subscribe:return i(o,"id")&&r(o,"payload")&&(!t(o.payload,"operationName")||void 0===o.payload.operationName||null===o.payload.operationName||"string"==typeof o.payload.operationName)&&i(o.payload,"query")&&(!t(o.payload,"variables")||void 0===o.payload.variables||null===o.payload.variables||r(o.payload,"variables"))&&(!t(o.payload,"extensions")||void 0===o.payload.extensions||null===o.payload.extensions||r(o.payload,"extensions"));case e.MessageType.Next:return i(o,"id")&&r(o,"payload");case e.MessageType.Error:return i(o,"id")&&(a=o.payload,Array.isArray(a)&&a.length>0&&a.every((e=>"message"in e)));case e.MessageType.Complete:return i(o,"id");default:return!1}}var a;return!1}function p(e,o){if(d(e))return e;if("string"!=typeof e)throw new Error("Message not parsable");const n=JSON.parse(e,o);if(!d(n))throw new Error("Invalid message");return n}function u(e,o){if(!d(e))throw new Error("Cannot stringify invalid message");return JSON.stringify(e,o)}function y(e){return n(e)&&"code"in e&&"reason"in e}e.CloseCode=void 0,(l=e.CloseCode||(e.CloseCode={}))[l.InternalServerError=4500]="InternalServerError",l[l.InternalClientError=4005]="InternalClientError",l[l.BadRequest=4400]="BadRequest",l[l.BadResponse=4004]="BadResponse",l[l.Unauthorized=4401]="Unauthorized",l[l.Forbidden=4403]="Forbidden",l[l.SubprotocolNotAcceptable=4406]="SubprotocolNotAcceptable",l[l.ConnectionInitialisationTimeout=4408]="ConnectionInitialisationTimeout",l[l.ConnectionAcknowledgementTimeout=4504]="ConnectionAcknowledgementTimeout",l[l.SubscriberAlreadyExists=4409]="SubscriberAlreadyExists",l[l.TooManyInitialisationRequests=4429]="TooManyInitialisationRequests",e.MessageType=void 0,(c=e.MessageType||(e.MessageType={})).ConnectionInit="connection_init",c.ConnectionAck="connection_ack",c.Ping="ping",c.Pong="pong",c.Subscribe="subscribe",c.Next="next",c.Error="error",c.Complete="complete",e.DEPRECATED_GRAPHQL_WS_PROTOCOL="graphql-ws",e.GRAPHQL_TRANSPORT_WS_PROTOCOL=s,e.createClient=function(o){const{url:n,connectionParams:t,lazy:r=!0,onNonLazyError:i=console.error,lazyCloseTimeout:l=0,keepAlive:c=0,disablePong:d,connectionAckWaitTimeout:g=0,retryAttempts:f=5,retryWait:m=async function(e){let o=1e3;for(let n=0;n<e;n++)o*=2;await new Promise((e=>setTimeout(e,o+Math.floor(2700*Math.random()+300))))},isFatalConnectionProblem:C=(e=>!y(e)),on:b,webSocketImpl:w,generateID:T=function(){return"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,(e=>{const o=16*Math.random()|0;return("x"==e?o:3&o|8).toString(16)}))},jsonMessageReplacer:x,jsonMessageReviver:v}=o;let M;if(w){if(!("function"==typeof(S=w)&&"constructor"in S&&"CLOSED"in S&&"CLOSING"in S&&"CONNECTING"in S&&"OPEN"in S))throw new Error("Invalid WebSocket implementation provided");M=w}else"undefined"!=typeof WebSocket?M=WebSocket:"undefined"!=typeof global?M=global.WebSocket||global.MozWebSocket:"undefined"!=typeof window&&(M=window.WebSocket||window.MozWebSocket);var S;if(!M)throw new Error("WebSocket implementation missing; on Node you can `import WebSocket from 'ws';` and pass `webSocketImpl: WebSocket` to `createClient`");const h=M,E=(()=>{const e=(()=>{const e={};return{on:(o,n)=>(e[o]=n,()=>{delete e[o]}),emit(o){var n;"id"in o&&(null===(n=e[o.id])||void 0===n||n.call(e,o))}}})(),o={connecting:(null==b?void 0:b.connecting)?[b.connecting]:[],opened:(null==b?void 0:b.opened)?[b.opened]:[],connected:(null==b?void 0:b.connected)?[b.connected]:[],ping:(null==b?void 0:b.ping)?[b.ping]:[],pong:(null==b?void 0:b.pong)?[b.pong]:[],message:(null==b?void 0:b.message)?[e.emit,b.message]:[e.emit],closed:(null==b?void 0:b.closed)?[b.closed]:[],error:(null==b?void 0:b.error)?[b.error]:[]};return{onMessage:e.on,on(e,n){const t=o[e];return t.push(n),()=>{t.splice(t.indexOf(n),1)}},emit(e,...n){for(const t of[...o[e]])t(...n)}}})();function P(e){const o=[E.on("error",(n=>{o.forEach((e=>e())),e(n)})),E.on("closed",(n=>{o.forEach((e=>e())),e(n)}))]}let N,I=0,k=!1,A=0,O=!1;async function R(){const[o,r]=await(null!=N?N:N=new Promise(((o,r)=>(async()=>{if(k){if(await m(A),!I)return N=void 0,r({code:1e3,reason:"All Subscriptions Gone"});A++}E.emit("connecting");const i=new h("function"==typeof n?await n():n,s);let l,f;function C(){isFinite(c)&&c>0&&(clearTimeout(f),f=setTimeout((()=>{i.readyState===h.OPEN&&(i.send(u({type:e.MessageType.Ping})),E.emit("ping",!1,void 0))}),c))}P((e=>{N=void 0,clearTimeout(l),clearTimeout(f),r(e),y(e)&&4499===e.code&&(i.close(4499,"Terminated"),i.onerror=null,i.onclose=null)})),i.onerror=e=>E.emit("error",e),i.onclose=e=>E.emit("closed",e),i.onopen=async()=>{try{E.emit("opened",i);const o="function"==typeof t?await t():t;if(i.readyState!==h.OPEN)return;i.send(u(o?{type:e.MessageType.ConnectionInit,payload:o}:{type:e.MessageType.ConnectionInit},x)),isFinite(g)&&g>0&&(l=setTimeout((()=>{i.close(e.CloseCode.ConnectionAcknowledgementTimeout,"Connection acknowledgement timeout")}),g)),C()}catch(o){E.emit("error",o),i.close(e.CloseCode.InternalClientError,a(o instanceof Error?o.message:new Error(o).message,"Internal client error"))}};let b=!1;i.onmessage=({data:n})=>{try{const t=p(n,v);if(E.emit("message",t),"ping"===t.type||"pong"===t.type)return E.emit(t.type,!0,t.payload),void("pong"===t.type?C():d||(i.send(u(t.payload?{type:e.MessageType.Pong,payload:t.payload}:{type:e.MessageType.Pong})),E.emit("pong",!1,t.payload)));if(b)return;if(t.type!==e.MessageType.ConnectionAck)throw new Error(`First message cannot be of type ${t.type}`);clearTimeout(l),b=!0,E.emit("connected",i,t.payload),k=!1,A=0,o([i,new Promise(((e,o)=>P(o)))])}catch(o){i.onmessage=null,E.emit("error",o),i.close(e.CloseCode.BadResponse,a(o instanceof Error?o.message:new Error(o).message,"Bad response"))}}})())));o.readyState===h.CLOSING&&await r;let i=()=>{};const f=new Promise((e=>i=e));return[o,i,Promise.race([f.then((()=>{if(!I){const e=()=>o.close(1e3,"Normal Closure");isFinite(l)&&l>0?setTimeout((()=>{I||o.readyState!==h.OPEN||e()}),l):e()}})),r])]}function W(o){if(y(o)&&(n=o.code,![1e3,1001,1006,1005,1012,1013,1013].includes(n)&&n>=1e3&&n<=1999||[e.CloseCode.InternalServerError,e.CloseCode.InternalClientError,e.CloseCode.BadRequest,e.CloseCode.BadResponse,e.CloseCode.Unauthorized,e.CloseCode.SubprotocolNotAcceptable,e.CloseCode.SubscriberAlreadyExists,e.CloseCode.TooManyInitialisationRequests].includes(o.code)))throw o;var n;if(O)return!1;if(y(o)&&1e3===o.code)return I>0;if(!f||A>=f)throw o;if(C(o))throw o;return k=!0}return r||(async()=>{for(I++;;)try{const[,,e]=await R();await e}catch(e){try{if(!W(e))return}catch(e){return null==i?void 0:i(e)}}})(),{on:E.on,subscribe(o,n){const t=T();let r=!1,i=!1,a=()=>{I--,r=!0};return(async()=>{for(I++;;)try{const[s,l,c]=await R();if(r)return l();const d=E.onMessage(t,(o=>{switch(o.type){case e.MessageType.Next:return void n.next(o.payload);case e.MessageType.Error:return i=!0,r=!0,n.error(o.payload),void a();case e.MessageType.Complete:return r=!0,void a()}}));return s.send(u({id:t,type:e.MessageType.Subscribe,payload:o},x)),a=()=>{r||s.readyState!==h.OPEN||s.send(u({id:t,type:e.MessageType.Complete},x)),I--,r=!0,l()},void await c.finally(d)}catch(e){if(!W(e))return}})().then((()=>{i||n.complete()})).catch((e=>{n.error(e)})),()=>{r||a()}},async dispose(){if(O=!0,N){const[e]=await N;e.close(1e3,"Normal Closure")}},terminate(){N&&E.emit("closed",{code:4499,reason:"Terminated",wasClean:!1})}}},e.isMessage=d,e.parseMessage=p,e.stringifyMessage=u,Object.defineProperty(e,"__esModule",{value:!0})}));
!function(e,o){"object"==typeof exports&&"undefined"!=typeof module?o(exports):"function"==typeof define&&define.amd?define(["exports"],o):o((e="undefined"!=typeof globalThis?globalThis:e||self).graphqlWs={})}(this,(function(e){"use strict";const o=Object.prototype.hasOwnProperty;function n(e){return"object"==typeof e&&null!==e}function t(e,n){return o.call(e,n)}function r(e,t){return o.call(e,t)&&n(e[t])}function i(e,n){return o.call(e,n)&&"string"==typeof e[n]}function a(e,o){return e.length<124?e:o}const s="graphql-transport-ws";var l,c;function d(o){if(n(o)){if(!i(o,"type"))return!1;switch(o.type){case e.MessageType.ConnectionInit:case e.MessageType.ConnectionAck:case e.MessageType.Ping:case e.MessageType.Pong:return!t(o,"payload")||void 0===o.payload||n(o.payload);case e.MessageType.Subscribe:return i(o,"id")&&r(o,"payload")&&(!t(o.payload,"operationName")||void 0===o.payload.operationName||null===o.payload.operationName||"string"==typeof o.payload.operationName)&&i(o.payload,"query")&&(!t(o.payload,"variables")||void 0===o.payload.variables||null===o.payload.variables||r(o.payload,"variables"))&&(!t(o.payload,"extensions")||void 0===o.payload.extensions||null===o.payload.extensions||r(o.payload,"extensions"));case e.MessageType.Next:return i(o,"id")&&r(o,"payload");case e.MessageType.Error:return i(o,"id")&&(a=o.payload,Array.isArray(a)&&a.length>0&&a.every((e=>"message"in e)));case e.MessageType.Complete:return i(o,"id");default:return!1}}var a;return!1}function p(e,o){if(d(e))return e;if("string"!=typeof e)throw new Error("Message not parsable");const n=JSON.parse(e,o);if(!d(n))throw new Error("Invalid message");return n}function u(e,o){if(!d(e))throw new Error("Cannot stringify invalid message");return JSON.stringify(e,o)}function y(e){return n(e)&&"code"in e&&"reason"in e}e.CloseCode=void 0,(l=e.CloseCode||(e.CloseCode={}))[l.InternalServerError=4500]="InternalServerError",l[l.InternalClientError=4005]="InternalClientError",l[l.BadRequest=4400]="BadRequest",l[l.BadResponse=4004]="BadResponse",l[l.Unauthorized=4401]="Unauthorized",l[l.Forbidden=4403]="Forbidden",l[l.SubprotocolNotAcceptable=4406]="SubprotocolNotAcceptable",l[l.ConnectionInitialisationTimeout=4408]="ConnectionInitialisationTimeout",l[l.ConnectionAcknowledgementTimeout=4504]="ConnectionAcknowledgementTimeout",l[l.SubscriberAlreadyExists=4409]="SubscriberAlreadyExists",l[l.TooManyInitialisationRequests=4429]="TooManyInitialisationRequests",e.MessageType=void 0,(c=e.MessageType||(e.MessageType={})).ConnectionInit="connection_init",c.ConnectionAck="connection_ack",c.Ping="ping",c.Pong="pong",c.Subscribe="subscribe",c.Next="next",c.Error="error",c.Complete="complete",e.DEPRECATED_GRAPHQL_WS_PROTOCOL="graphql-ws",e.GRAPHQL_TRANSPORT_WS_PROTOCOL=s,e.createClient=function(o){const{url:n,connectionParams:t,lazy:r=!0,onNonLazyError:i=console.error,lazyCloseTimeout:l=0,keepAlive:c=0,disablePong:d,connectionAckWaitTimeout:g=0,retryAttempts:f=5,retryWait:m=async function(e){let o=1e3;for(let n=0;n<e;n++)o*=2;await new Promise((e=>setTimeout(e,o+Math.floor(2700*Math.random()+300))))},shouldRetry:C=y,isFatalConnectionProblem:b=(e=>!y(e)),on:w,webSocketImpl:T,generateID:x=function(){return"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,(e=>{const o=16*Math.random()|0;return("x"==e?o:3&o|8).toString(16)}))},jsonMessageReplacer:v,jsonMessageReviver:h}=o;let M;if(T){if(!("function"==typeof(S=T)&&"constructor"in S&&"CLOSED"in S&&"CLOSING"in S&&"CONNECTING"in S&&"OPEN"in S))throw new Error("Invalid WebSocket implementation provided");M=T}else"undefined"!=typeof WebSocket?M=WebSocket:"undefined"!=typeof global?M=global.WebSocket||global.MozWebSocket:"undefined"!=typeof window&&(M=window.WebSocket||window.MozWebSocket);var S;if(!M)throw new Error("WebSocket implementation missing; on Node you can `import WebSocket from 'ws';` and pass `webSocketImpl: WebSocket` to `createClient`");const E=M,P=(()=>{const e=(()=>{const e={};return{on:(o,n)=>(e[o]=n,()=>{delete e[o]}),emit(o){var n;"id"in o&&(null===(n=e[o.id])||void 0===n||n.call(e,o))}}})(),o={connecting:(null==w?void 0:w.connecting)?[w.connecting]:[],opened:(null==w?void 0:w.opened)?[w.opened]:[],connected:(null==w?void 0:w.connected)?[w.connected]:[],ping:(null==w?void 0:w.ping)?[w.ping]:[],pong:(null==w?void 0:w.pong)?[w.pong]:[],message:(null==w?void 0:w.message)?[e.emit,w.message]:[e.emit],closed:(null==w?void 0:w.closed)?[w.closed]:[],error:(null==w?void 0:w.error)?[w.error]:[]};return{onMessage:e.on,on(e,n){const t=o[e];return t.push(n),()=>{t.splice(t.indexOf(n),1)}},emit(e,...n){for(const t of[...o[e]])t(...n)}}})();function N(e){const o=[P.on("error",(n=>{o.forEach((e=>e())),e(n)})),P.on("closed",(n=>{o.forEach((e=>e())),e(n)}))]}let I,k=0,A=!1,O=0,R=!1;async function W(){const[o,r]=await(null!=I?I:I=new Promise(((o,r)=>(async()=>{if(A){if(await m(O),!k)return I=void 0,r({code:1e3,reason:"All Subscriptions Gone"});O++}P.emit("connecting");const i=new E("function"==typeof n?await n():n,s);let l,f;function C(){isFinite(c)&&c>0&&(clearTimeout(f),f=setTimeout((()=>{i.readyState===E.OPEN&&(i.send(u({type:e.MessageType.Ping})),P.emit("ping",!1,void 0))}),c))}N((e=>{I=void 0,clearTimeout(l),clearTimeout(f),r(e),y(e)&&4499===e.code&&(i.close(4499,"Terminated"),i.onerror=null,i.onclose=null)})),i.onerror=e=>P.emit("error",e),i.onclose=e=>P.emit("closed",e),i.onopen=async()=>{try{P.emit("opened",i);const o="function"==typeof t?await t():t;if(i.readyState!==E.OPEN)return;i.send(u(o?{type:e.MessageType.ConnectionInit,payload:o}:{type:e.MessageType.ConnectionInit},v)),isFinite(g)&&g>0&&(l=setTimeout((()=>{i.close(e.CloseCode.ConnectionAcknowledgementTimeout,"Connection acknowledgement timeout")}),g)),C()}catch(o){P.emit("error",o),i.close(e.CloseCode.InternalClientError,a(o instanceof Error?o.message:new Error(o).message,"Internal client error"))}};let b=!1;i.onmessage=({data:n})=>{try{const t=p(n,h);if(P.emit("message",t),"ping"===t.type||"pong"===t.type)return P.emit(t.type,!0,t.payload),void("pong"===t.type?C():d||(i.send(u(t.payload?{type:e.MessageType.Pong,payload:t.payload}:{type:e.MessageType.Pong})),P.emit("pong",!1,t.payload)));if(b)return;if(t.type!==e.MessageType.ConnectionAck)throw new Error(`First message cannot be of type ${t.type}`);clearTimeout(l),b=!0,P.emit("connected",i,t.payload),A=!1,O=0,o([i,new Promise(((e,o)=>N(o)))])}catch(o){i.onmessage=null,P.emit("error",o),i.close(e.CloseCode.BadResponse,a(o instanceof Error?o.message:new Error(o).message,"Bad response"))}}})())));o.readyState===E.CLOSING&&await r;let i=()=>{};const f=new Promise((e=>i=e));return[o,i,Promise.race([f.then((()=>{if(!k){const e=()=>o.close(1e3,"Normal Closure");isFinite(l)&&l>0?setTimeout((()=>{k||o.readyState!==E.OPEN||e()}),l):e()}})),r])]}function q(o){if(y(o)&&(n=o.code,![1e3,1001,1006,1005,1012,1013,1013].includes(n)&&n>=1e3&&n<=1999||[e.CloseCode.InternalServerError,e.CloseCode.InternalClientError,e.CloseCode.BadRequest,e.CloseCode.BadResponse,e.CloseCode.Unauthorized,e.CloseCode.SubprotocolNotAcceptable,e.CloseCode.SubscriberAlreadyExists,e.CloseCode.TooManyInitialisationRequests].includes(o.code)))throw o;var n;if(R)return!1;if(y(o)&&1e3===o.code)return k>0;if(!f||O>=f)throw o;if(!C(o))throw o;if(b(o))throw o;return A=!0}return r||(async()=>{for(k++;;)try{const[,,e]=await W();await e}catch(e){try{if(!q(e))return}catch(e){return null==i?void 0:i(e)}}})(),{on:P.on,subscribe(o,n){const t=x();let r=!1,i=!1,a=()=>{k--,r=!0};return(async()=>{for(k++;;)try{const[s,l,c]=await W();if(r)return l();const d=P.onMessage(t,(o=>{switch(o.type){case e.MessageType.Next:return void n.next(o.payload);case e.MessageType.Error:return i=!0,r=!0,n.error(o.payload),void a();case e.MessageType.Complete:return r=!0,void a()}}));return s.send(u({id:t,type:e.MessageType.Subscribe,payload:o},v)),a=()=>{r||s.readyState!==E.OPEN||s.send(u({id:t,type:e.MessageType.Complete},v)),k--,r=!0,l()},void await c.finally(d)}catch(e){if(!q(e))return}})().then((()=>{i||n.complete()})).catch((e=>{n.error(e)})),()=>{r||a()}},async dispose(){if(R=!0,I){const[e]=await I;e.close(1e3,"Normal Closure")}},terminate(){I&&P.emit("closed",{code:4499,reason:"Terminated",wasClean:!1})}}},e.isMessage=d,e.parseMessage=p,e.stringifyMessage=u,Object.defineProperty(e,"__esModule",{value:!0})}));

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc