@hyper-fetch/sockets 4.0.2 → 5.0.1
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/README.md +0 -9
- package/dist/index.cjs.js +852 -1
- package/dist/index.cjs.js.map +4 -4
- package/dist/index.d.ts +264 -131
- package/dist/index.esm.js +832 -1
- package/dist/index.esm.js.map +4 -4
- package/package.json +2 -2
- package/src/adapter/adapter.bindings.ts +158 -0
- package/src/adapter/adapter.sse.ts +127 -0
- package/src/adapter/adapter.types.ts +83 -11
- package/src/adapter/adapter.utils.ts +12 -7
- package/src/adapter/adapter.websocket.ts +187 -0
- package/src/adapter/index.ts +3 -1
- package/src/emitter/emitter.ts +103 -30
- package/src/emitter/emitter.types.ts +63 -8
- package/src/listener/listener.ts +62 -21
- package/src/listener/listener.types.ts +23 -4
- package/src/socket/socket.events.ts +44 -32
- package/src/socket/socket.ts +29 -28
- package/src/socket/socket.types.ts +8 -13
- package/src/socket/socket.utils.ts +7 -7
- package/src/types/connect.types.ts +6 -0
- package/src/types/extract.types.ts +11 -12
- package/src/types/index.ts +1 -0
- package/src/adapter/adapter.ts +0 -238
package/dist/index.esm.js
CHANGED
|
@@ -1,2 +1,833 @@
|
|
|
1
|
-
var N=Object.defineProperty,q=Object.defineProperties;var F=Object.getOwnPropertyDescriptors;var x=Object.getOwnPropertySymbols;var V=Object.prototype.hasOwnProperty,B=Object.prototype.propertyIsEnumerable;var M=(t,e,n)=>e in t?N(t,e,{enumerable:!0,configurable:!0,writable:!0,value:n}):t[e]=n,h=(t,e)=>{for(var n in e||(e={}))V.call(e,n)&&M(t,n,e[n]);if(x)for(var n of x(e))B.call(e,n)&&M(t,n,e[n]);return t},y=(t,e)=>q(t,F(e));var O=(t,e,n)=>new Promise((o,r)=>{var s=a=>{try{p(n.next(a))}catch(c){r(c)}},i=a=>{try{p(n.throw(a))}catch(c){r(c)}},p=a=>a.done?o(a.value):Promise.resolve(a.value).then(s,i);p((n=n.apply(t,e)).next())});import G from"events";import{stringifyQueryParams as z,LoggerManager as H,AppManager as U,DateInterval as J}from"@hyper-fetch/core";import{DateInterval as _,parseResponse as Q}from"@hyper-fetch/core";var g=class{constructor(e){this.socket=e;this.listeners=new Map;this.open=!1;this.connecting=!1;this.forceClosed=!1;this.reconnectionAttempts=0;this.connect=()=>{var o;if(!this.socket.appManager.isOnline||this.connecting)return this.logger.warning("Cannot initialize adapter.",{connecting:this.connecting,online:this.socket.appManager.isOnline});let{reconnectTimeout:e=_.second*2}=this.socket.options.adapterOptions||{};if(this.socket.events.emitConnecting(),this.forceClosed=!1,(o=this.adapter)==null||o.close(),this.connecting=!0,this.adapter=this.init(),!this.adapter)return this.logger.warning("Cannot initialize adapter.");let n=setTimeout(()=>{this.reconnect()},e);this.adapter.onopen=r=>{this.logger.info("Connection open",{event:r}),clearTimeout(n),this.socket.__onOpenCallbacks.forEach(s=>{s(r,this.socket)}),this.open=!0,this.connecting=!1,this.reconnectionAttempts=0,this.socket.events.emitOpen(),this.heartbeat()},this.adapter&&"onclose"in this.adapter&&(this.adapter.onclose=r=>{this.logger.info("Connection closed",{event:r}),this.socket.__onCloseCallbacks.forEach(s=>{s(r,this.socket)}),this.open=!1,this.connecting=!1,this.socket.events.emitClose(),this.clearTimers()}),this.adapter.onerror=r=>{this.logger.info("Error message",{event:r}),this.socket.__onErrorCallbacks.forEach(s=>{s(r,this.socket)}),this.socket.events.emitError(r)},this.adapter.onmessage=r=>{this.logger.info("New event message",{event:r});let s=this.socket.__modifyResponse(r),i=Q(s.data);(this.listeners.get(i.name)||[]).forEach(a=>{a(i,r)}),this.socket.events.emitListenerEvent(i.name,i,r),this.heartbeat()}};this.disconnect=()=>{this.logger.debug("Disconnecting",{reconnectionAttempts:this.reconnectionAttempts}),this.open=!1,this.connecting=!1,this.forceClosed=!0,this.adapter.close(),this.clearTimers(),!(this.adapter&&"onclose"in this.adapter)&&(this.socket.__onCloseCallbacks.forEach(n=>{n({},this.socket)}),this.socket.events.emitClose())};this.reconnect=()=>(this.disconnect(),this.reconnectionAttempts<this.socket.reconnect?(this.reconnectionAttempts+=1,this.logger.debug("Reconnecting",{reconnectionAttempts:this.reconnectionAttempts}),this.connect(),this.socket.__onReconnectCallbacks.forEach(e=>{e(this.socket)}),this.socket.events.emitReconnecting(this.reconnectionAttempts),!0):(this.logger.debug("Stopped reconnecting",{reconnectionAttempts:this.reconnectionAttempts}),this.socket.__onReconnectStopCallbacks.forEach(e=>{e(this.socket)}),this.socket.events.emitReconnectingStop(this.reconnectionAttempts),!1));this.clearTimers=()=>{clearTimeout(this.pingTimer),clearTimeout(this.pongTimer)};this.sendEventMessage=e=>{this.adapter&&"send"in this.adapter&&this.adapter.send(JSON.stringify({id:e.id,name:e.name,data:e.data}))};this.heartbeat=()=>{let e=this.socket.options.adapterOptions||{},{heartbeat:n=!1,pingTimeout:o=_.second*5,pongTimeout:r=_.second*5,heartbeatMessage:s="heartbeat"}=e;this.connecting||!n||this.adapter&&"send"in this.adapter&&(this.clearTimers(),this.pingTimer=setTimeout(()=>{if(this.adapter&&"send"in this.adapter){this.logger.debug("[Heartbeat]: Start");let i="heartbeat";this.sendEventMessage({id:i,data:s,name:"heartbeat"}),this.pongTimer=setTimeout(()=>{this.logger.debug("[Heartbeat]: No response, closing connection"),this.adapter.close()},r)}},o))};this.removeListener=(e,n)=>{let o=this.listeners.get(e);return o&&o.has(n)?(this.logger.debug("Removed event listener",{event:e}),this.socket.events.emitListenerRemoveEvent(e),o.delete(n),!0):!1};this.listen=(e,n)=>((this.listeners.get(e.name)||this.listeners.set(e.name,new Set).get(e.name)).add(n),()=>this.removeListener(e.name,n));this.emit=(e,n,o)=>O(this,null,function*(){if((!this.connecting||!this.open)&&this.logger.error("Cannot emit event when connection is not open"),this.adapter&&"send"in this.adapter){if(o){let s,i=this.listen({name:n.name},p=>{p.id===e&&(o(null,p),clearTimeout(s))});s=setTimeout(()=>{i(),o(new Error("Server did not acknowledge the event"),null)},n.timeout)}let r=yield this.socket.__modifySend(n);this.sendEventMessage({id:e,data:r.data,name:r.name}),this.socket.events.emitEmitterEvent(r)}else throw new Error("Cannot emit events in SSE mode")});this.init=()=>P(e),this.logger=this.socket.loggerManager.init("Socket Adapter"),this.socket.appManager.events.onOnline(()=>{this.socket.autoConnect&&!this.open&&(this.logger.info("Auto re-connecting"),this.connect())}),this.socket.autoConnect&&(this.logger.info("Auto connecting"),this.connect())}};var P=t=>{var s,i;let e=t.queryParamsStringify(t.queryParams).substring(1),n=t.queryParamsStringify(t.auth).substring(1),o=e&&n?"&":"",r=`${t.url}?${n}${o}${e}`;return"isSSE"in t.options?window!=null&&window.EventSource?new EventSource(r,(s=t.options.adapterOptions)==null?void 0:s.eventSourceInit):null:window!=null&&window.WebSocket?new WebSocket(r,(i=t.options.adapterOptions)==null?void 0:i.protocols):null};var m=class{constructor(e,n){this.socket=e;this.listenerOptions=n;let{name:o,options:r}=n;this.name=o,this.options=r}setOptions(e){return this.clone({options:e})}clone(e){return new m(this.socket,y(h(h({},this.listenerOptions),e),{name:this.name}))}listen(e){let n=this.clone();return this.socket.adapter.listen(n,e),[()=>{this.socket.adapter.removeListener(n.name,e)},e]}};import{DateInterval as W,getUniqueRequestId as $}from"@hyper-fetch/core";var d=class{constructor(e,n,o,r){this.socket=e;this.emitterOptions=n;this.dataMapper=r;this.data=null;this.setDataMapper=e=>this.clone(void 0,e);var a,c,l;let{name:s,timeout:i=W.second*3,options:p}=n;this.name=(a=o==null?void 0:o.name)!=null?a:s,this.data=o==null?void 0:o.data,this.timeout=(c=o==null?void 0:o.timeout)!=null?c:i,this.options=(l=o==null?void 0:o.options)!=null?l:p}setOptions(e){return this.clone({options:e})}setTimeout(e){return this.clone({timeout:e})}setData(e){return this.dataMapper?this.clone({data:this.dataMapper(e)}):this.clone({data:e})}clone(e,n){let o=y(h({timeout:this.timeout,options:this.options,data:this.data},e),{name:this.name}),r=n||this.dataMapper;return new d(this.socket,this.emitterOptions,o,r)}emit(e,n){let o=this.clone(e),r=$(this.name);return this.socket.adapter.emit(r,o,n),r}};var w=class{constructor(e){this.options=e;this.emitter=new G;this.events=D(this.emitter);this.__onOpenCallbacks=[];this.__onCloseCallbacks=[];this.__onReconnectCallbacks=[];this.__onReconnectStopCallbacks=[];this.__onMessageCallbacks=[];this.__onSendCallbacks=[];this.__onErrorCallbacks=[];this.loggerManager=new H(this);this.appManager=new U;this.logger=this.loggerManager.init("Socket");this.queryParamsStringify=e=>z(e,this.queryParamsConfig);this.setDebug=e=>(this.debug=e,this);this.setLoggerSeverity=e=>(this.loggerManager.setSeverity(e),this);this.setLogger=e=>(this.loggerManager=e(this),this);this.setAuth=e=>(this.auth=e,this.adapter.reconnect(),this);this.setQuery=e=>(this.queryParams=e,this.adapter.reconnect(),this);this.__modifySend=e=>K(this.__onSendCallbacks,e);this.__modifyResponse=e=>I(this.__onMessageCallbacks,e,this);this.createListener=e=>new m(this,e);this.createEmitter=e=>{if("isSSE"in this.options)throw new Error("Cannot create emitters for SSE adapter");return new d(this,e)};let{url:n,auth:o,adapter:r,queryParams:s,autoConnect:i,reconnect:p,reconnectTime:a,queryParamsConfig:c,queryParamsStringify:l}=this.options;this.url=n,this.auth=o,this.queryParams=s,this.debug=!1,this.autoConnect=i!=null?i:!0,this.reconnect=p!=null?p:1/0,this.reconnectTime=a!=null?a:J.second*2,c&&(this.queryParamsConfig=c),l&&(this.queryParamsStringify=l),this.adapter=r||new g(this)}onOpen(e){return this.__onOpenCallbacks.push(e),this}onClose(e){return this.__onCloseCallbacks.push(e),this}onReconnect(e){return this.__onReconnectCallbacks.push(e),this}onReconnectStop(e){return this.__onReconnectStopCallbacks.push(e),this}onMessage(e){return this.__onMessageCallbacks.push(e),this}onSend(e){return this.__onSendCallbacks.push(e),this}onError(e){return this.__onErrorCallbacks.push(e),this}};var u=()=>"socket_error",T=()=>"socket_open",f=()=>"socket_close",E=()=>"socket_connecting",k=()=>"socket_reconnecting",S=()=>"socket_reconnecting_stop",v=()=>"listener_event",C=()=>"listener_remove",b=()=>"emitter_event",L=t=>`${t}_listener_remove`,A=t=>`${t}_listener_event`,R=t=>`${t}_emitter_event`,I=(t,e,n)=>{let o=e;for(let r of t)if(o=r(e,n),!o)throw new Error("Listener modifier must return data");return o},K=(t,e)=>{let n=e;for(let o of t)if(n=o(e),!n)throw new Error("Send modifier must return emitter");return n};var D=t=>({emitError:e=>{t.emit(u(),e)},emitOpen:()=>{t.emit(T())},emitClose:()=>{t.emit(f())},emitConnecting:()=>{t.emit(E())},emitReconnecting:e=>{t.emit(k(),e)},emitReconnectingStop:e=>{t.emit(S(),e)},emitListenerEvent:(e,n,o)=>{t.emit(v(),n,o),t.emit(A(e),n,o)},emitListenerRemoveEvent:e=>{t.emit(C(),e),t.emit(L(e))},emitEmitterEvent:e=>{t.emit(b(),e),t.emit(R(e.name),e)},onError:e=>(t.on(u(),e),()=>t.removeListener(u(),e)),onOpen:e=>(t.on(T(),e),()=>t.removeListener(T(),e)),onClose:e=>(t.on(f(),e),()=>t.removeListener(f(),e)),onConnecting:e=>(t.on(E(),e),()=>t.removeListener(E(),e)),onReconnecting:e=>(t.on(k(),e),()=>t.removeListener(k(),e)),onReconnectingStop:e=>(t.on(S(),e),()=>t.removeListener(S(),e)),onListenerEvent:e=>(t.on(v(),e),()=>t.removeListener(v(),e)),onEmitterEvent:e=>(t.on(b(),e),()=>t.removeListener(b(),e)),onListenerEventByName:(e,n)=>(t.on(A(e.name),n),()=>t.removeListener(A(e.name),n)),onListenerRemove:e=>(t.on(C(),e),()=>t.removeListener(C(),e)),onListenerRemoveByName:(e,n)=>(t.on(L(e.name),n),()=>t.removeListener(L(e.name),n)),onEmitterEventByName:(e,n)=>(t.on(R(e.name),n),()=>t.removeListener(R(e.name),n))});export{d as Emitter,m as Listener,w as Socket,g as SocketAdapter,P as getAdapter,f as getCloseKey,E as getConnectingKey,R as getEmitterEventByNameKey,b as getEmitterEventKey,u as getErrorKey,A as getListenerEventByNameKey,v as getListenerEventKey,L as getListenerRemoveByNameKey,C as getListenerRemoveKey,T as getOpenKey,k as getReconnectingKey,S as getReconnectingStopKey,D as getSocketEvents,K as interceptEmitter,I as interceptListener};
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defProps = Object.defineProperties;
|
|
3
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
4
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
7
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
+
var __spreadValues = (a, b) => {
|
|
9
|
+
for (var prop in b || (b = {}))
|
|
10
|
+
if (__hasOwnProp.call(b, prop))
|
|
11
|
+
__defNormalProp(a, prop, b[prop]);
|
|
12
|
+
if (__getOwnPropSymbols)
|
|
13
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
14
|
+
if (__propIsEnum.call(b, prop))
|
|
15
|
+
__defNormalProp(a, prop, b[prop]);
|
|
16
|
+
}
|
|
17
|
+
return a;
|
|
18
|
+
};
|
|
19
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
|
+
var __objRest = (source, exclude) => {
|
|
21
|
+
var target = {};
|
|
22
|
+
for (var prop in source)
|
|
23
|
+
if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
|
|
24
|
+
target[prop] = source[prop];
|
|
25
|
+
if (source != null && __getOwnPropSymbols)
|
|
26
|
+
for (var prop of __getOwnPropSymbols(source)) {
|
|
27
|
+
if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
|
|
28
|
+
target[prop] = source[prop];
|
|
29
|
+
}
|
|
30
|
+
return target;
|
|
31
|
+
};
|
|
32
|
+
var __async = (__this, __arguments, generator) => {
|
|
33
|
+
return new Promise((resolve, reject) => {
|
|
34
|
+
var fulfilled = (value) => {
|
|
35
|
+
try {
|
|
36
|
+
step(generator.next(value));
|
|
37
|
+
} catch (e) {
|
|
38
|
+
reject(e);
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
var rejected = (value) => {
|
|
42
|
+
try {
|
|
43
|
+
step(generator.throw(value));
|
|
44
|
+
} catch (e) {
|
|
45
|
+
reject(e);
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
49
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
50
|
+
});
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
// src/socket/socket.ts
|
|
54
|
+
import EventEmitter from "events";
|
|
55
|
+
import {
|
|
56
|
+
stringifyQueryParams,
|
|
57
|
+
LoggerManager,
|
|
58
|
+
AppManager,
|
|
59
|
+
DateInterval as DateInterval3
|
|
60
|
+
} from "@hyper-fetch/core";
|
|
61
|
+
|
|
62
|
+
// src/listener/listener.ts
|
|
63
|
+
var Listener = class {
|
|
64
|
+
constructor(socket, listenerOptions) {
|
|
65
|
+
this.socket = socket;
|
|
66
|
+
this.listenerOptions = listenerOptions;
|
|
67
|
+
this.connections = /* @__PURE__ */ new Set();
|
|
68
|
+
this.paramsMapper = (params) => {
|
|
69
|
+
let endpoint = this.listenerOptions.endpoint;
|
|
70
|
+
if (params) {
|
|
71
|
+
Object.entries(params).forEach(([key, value]) => {
|
|
72
|
+
endpoint = endpoint.replace(new RegExp(`:${key}`, "g"), String(value));
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
return endpoint;
|
|
76
|
+
};
|
|
77
|
+
this.listen = (_a) => {
|
|
78
|
+
var _b = _a, { callback } = _b, options = __objRest(_b, ["callback"]);
|
|
79
|
+
const instance = this.clone(options);
|
|
80
|
+
const action = (response) => {
|
|
81
|
+
this.connections.forEach((connection) => connection(response, () => this.connections.delete(connection)));
|
|
82
|
+
return callback(response);
|
|
83
|
+
};
|
|
84
|
+
this.socket.adapter.listen(instance, action);
|
|
85
|
+
const removeListener = () => {
|
|
86
|
+
this.socket.adapter.removeListener(instance.endpoint, action);
|
|
87
|
+
};
|
|
88
|
+
return removeListener;
|
|
89
|
+
};
|
|
90
|
+
const { endpoint, options } = listenerOptions;
|
|
91
|
+
this.endpoint = endpoint;
|
|
92
|
+
this.options = options;
|
|
93
|
+
}
|
|
94
|
+
setOptions(options) {
|
|
95
|
+
return this.clone({ options });
|
|
96
|
+
}
|
|
97
|
+
setParams(params) {
|
|
98
|
+
return this.clone({ params });
|
|
99
|
+
}
|
|
100
|
+
onData(callback) {
|
|
101
|
+
this.connections.add(callback);
|
|
102
|
+
return this;
|
|
103
|
+
}
|
|
104
|
+
clone(options) {
|
|
105
|
+
const newInstance = new Listener(this.socket, __spreadProps(__spreadValues(__spreadValues({}, this.listenerOptions), options), {
|
|
106
|
+
endpoint: this.paramsMapper((options == null ? void 0 : options.params) || this.params)
|
|
107
|
+
}));
|
|
108
|
+
newInstance.connections = this.connections;
|
|
109
|
+
return newInstance;
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
// src/emitter/emitter.ts
|
|
114
|
+
import { DateInterval, getUniqueRequestId } from "@hyper-fetch/core";
|
|
115
|
+
var Emitter = class {
|
|
116
|
+
constructor(socket, emitterOptions, json, dataMapper) {
|
|
117
|
+
this.socket = socket;
|
|
118
|
+
this.emitterOptions = emitterOptions;
|
|
119
|
+
this.dataMapper = dataMapper;
|
|
120
|
+
this.data = null;
|
|
121
|
+
this.connections = /* @__PURE__ */ new Set();
|
|
122
|
+
this.setDataMapper = (mapper) => {
|
|
123
|
+
return this.clone(void 0, mapper);
|
|
124
|
+
};
|
|
125
|
+
this.paramsMapper = (params) => {
|
|
126
|
+
let endpoint = this.emitterOptions.endpoint;
|
|
127
|
+
if (params) {
|
|
128
|
+
Object.entries(params).forEach(([key, value]) => {
|
|
129
|
+
endpoint = endpoint.replace(new RegExp(`:${key}`, "g"), String(value));
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
return endpoint;
|
|
133
|
+
};
|
|
134
|
+
this.getAck = (ack) => {
|
|
135
|
+
if (ack) {
|
|
136
|
+
return (response) => {
|
|
137
|
+
this.connections.forEach((connection) => connection(response, () => this.connections.delete(connection)));
|
|
138
|
+
return ack(response);
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
if (this.connections.size) {
|
|
142
|
+
return (response) => {
|
|
143
|
+
this.connections.forEach((connection) => connection(response, () => this.connections.delete(connection)));
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
return void 0;
|
|
147
|
+
};
|
|
148
|
+
this.emit = ({
|
|
149
|
+
data,
|
|
150
|
+
params,
|
|
151
|
+
options,
|
|
152
|
+
ack
|
|
153
|
+
} = {}) => {
|
|
154
|
+
const instance = this.clone(__spreadProps(__spreadValues({}, options), { data, params }));
|
|
155
|
+
const eventMessageId = getUniqueRequestId(instance.endpoint);
|
|
156
|
+
this.socket.adapter.emit(eventMessageId, instance, instance.getAck(ack));
|
|
157
|
+
return eventMessageId;
|
|
158
|
+
};
|
|
159
|
+
var _a, _b, _c;
|
|
160
|
+
const { endpoint, timeout = DateInterval.second * 2, options } = emitterOptions;
|
|
161
|
+
this.endpoint = (_a = json == null ? void 0 : json.endpoint) != null ? _a : endpoint;
|
|
162
|
+
this.data = json == null ? void 0 : json.data;
|
|
163
|
+
this.timeout = (_b = json == null ? void 0 : json.timeout) != null ? _b : timeout;
|
|
164
|
+
this.options = (_c = json == null ? void 0 : json.options) != null ? _c : options;
|
|
165
|
+
this.params = json == null ? void 0 : json.params;
|
|
166
|
+
}
|
|
167
|
+
setOptions(options) {
|
|
168
|
+
return this.clone({ options });
|
|
169
|
+
}
|
|
170
|
+
setTimeout(timeout) {
|
|
171
|
+
return this.clone({ timeout });
|
|
172
|
+
}
|
|
173
|
+
setData(data) {
|
|
174
|
+
if (this.dataMapper) {
|
|
175
|
+
return this.clone({ data: this.dataMapper(data) });
|
|
176
|
+
}
|
|
177
|
+
return this.clone({ data });
|
|
178
|
+
}
|
|
179
|
+
setParams(params) {
|
|
180
|
+
return this.clone({ params });
|
|
181
|
+
}
|
|
182
|
+
onData(callback) {
|
|
183
|
+
this.connections.add(callback);
|
|
184
|
+
return this;
|
|
185
|
+
}
|
|
186
|
+
clone(options, mapper) {
|
|
187
|
+
const json = __spreadProps(__spreadValues({
|
|
188
|
+
timeout: this.timeout,
|
|
189
|
+
options: this.options,
|
|
190
|
+
data: this.data,
|
|
191
|
+
params: (options == null ? void 0 : options.params) || this.params
|
|
192
|
+
}, options), {
|
|
193
|
+
endpoint: this.paramsMapper((options == null ? void 0 : options.params) || this.params)
|
|
194
|
+
});
|
|
195
|
+
const mapperFn = mapper || this.dataMapper;
|
|
196
|
+
const newInstance = new Emitter(
|
|
197
|
+
this.socket,
|
|
198
|
+
this.emitterOptions,
|
|
199
|
+
json,
|
|
200
|
+
mapperFn
|
|
201
|
+
);
|
|
202
|
+
newInstance.connections = this.connections;
|
|
203
|
+
return newInstance;
|
|
204
|
+
}
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
// src/adapter/adapter.websocket.ts
|
|
208
|
+
import { DateInterval as DateInterval2, parseResponse } from "@hyper-fetch/core";
|
|
209
|
+
var websocketAdapter = (socket) => {
|
|
210
|
+
const {
|
|
211
|
+
open,
|
|
212
|
+
listeners,
|
|
213
|
+
connecting,
|
|
214
|
+
reconnectionAttempts,
|
|
215
|
+
removeListener,
|
|
216
|
+
onConnect,
|
|
217
|
+
onReconnect,
|
|
218
|
+
onDisconnect,
|
|
219
|
+
onListen,
|
|
220
|
+
onEmit,
|
|
221
|
+
onOpen,
|
|
222
|
+
onClose,
|
|
223
|
+
onError,
|
|
224
|
+
onEvent
|
|
225
|
+
} = getSocketAdapterBindings(socket);
|
|
226
|
+
let pingTimer;
|
|
227
|
+
let pongTimer;
|
|
228
|
+
let adapter = getWebsocketAdapter(socket);
|
|
229
|
+
const connect = () => {
|
|
230
|
+
const enabled = onConnect();
|
|
231
|
+
if (!enabled)
|
|
232
|
+
return;
|
|
233
|
+
adapter == null ? void 0 : adapter.close();
|
|
234
|
+
adapter = getWebsocketAdapter(socket);
|
|
235
|
+
if (!adapter)
|
|
236
|
+
return;
|
|
237
|
+
const timeout = setTimeout(() => {
|
|
238
|
+
reconnect();
|
|
239
|
+
}, socket.reconnectTime);
|
|
240
|
+
adapter.onopen = () => {
|
|
241
|
+
clearTimeout(timeout);
|
|
242
|
+
onOpen();
|
|
243
|
+
onHeartbeat();
|
|
244
|
+
};
|
|
245
|
+
adapter.onclose = () => {
|
|
246
|
+
onClose();
|
|
247
|
+
clearTimers();
|
|
248
|
+
};
|
|
249
|
+
adapter.onerror = (event) => {
|
|
250
|
+
onError(new Error(event.type));
|
|
251
|
+
};
|
|
252
|
+
adapter.onmessage = (event) => {
|
|
253
|
+
const extra = parseResponse(event);
|
|
254
|
+
extra.data = parseResponse(extra.data);
|
|
255
|
+
const eventListeners = listeners.get(extra.data.endpoint) || /* @__PURE__ */ new Map();
|
|
256
|
+
eventListeners.forEach((_, action) => {
|
|
257
|
+
action({ data: extra.data.data, extra });
|
|
258
|
+
});
|
|
259
|
+
onEvent(extra.data.endpoint, extra.data.data, extra);
|
|
260
|
+
onHeartbeat();
|
|
261
|
+
};
|
|
262
|
+
};
|
|
263
|
+
const disconnect = () => {
|
|
264
|
+
onDisconnect();
|
|
265
|
+
adapter.close();
|
|
266
|
+
clearTimers();
|
|
267
|
+
};
|
|
268
|
+
const reconnect = () => {
|
|
269
|
+
onReconnect(disconnect, connect);
|
|
270
|
+
};
|
|
271
|
+
const clearTimers = () => {
|
|
272
|
+
clearTimeout(pingTimer);
|
|
273
|
+
clearTimeout(pongTimer);
|
|
274
|
+
};
|
|
275
|
+
const sendEventMessage = (payload) => {
|
|
276
|
+
adapter.send(JSON.stringify({ id: payload.id, endpoint: payload.endpoint, data: payload.data }));
|
|
277
|
+
};
|
|
278
|
+
const onHeartbeat = () => {
|
|
279
|
+
const options = socket.options.adapterOptions || {};
|
|
280
|
+
const {
|
|
281
|
+
heartbeat = false,
|
|
282
|
+
pingTimeout = DateInterval2.second * 5,
|
|
283
|
+
pongTimeout = DateInterval2.second * 5,
|
|
284
|
+
heartbeatMessage = "heartbeat"
|
|
285
|
+
} = options;
|
|
286
|
+
if (connecting || !heartbeat)
|
|
287
|
+
return;
|
|
288
|
+
clearTimers();
|
|
289
|
+
pingTimer = setTimeout(() => {
|
|
290
|
+
const id = "heartbeat";
|
|
291
|
+
sendEventMessage({ id, data: heartbeatMessage, endpoint: "heartbeat" });
|
|
292
|
+
pongTimer = setTimeout(() => {
|
|
293
|
+
adapter.close();
|
|
294
|
+
}, pongTimeout);
|
|
295
|
+
}, pingTimeout);
|
|
296
|
+
};
|
|
297
|
+
const listen = (listener, callback) => {
|
|
298
|
+
return onListen(listener, callback);
|
|
299
|
+
};
|
|
300
|
+
const emit = (eventMessageId, emitter, ack) => __async(void 0, null, function* () {
|
|
301
|
+
const enabled = onEmit(emitter);
|
|
302
|
+
if (!enabled)
|
|
303
|
+
return;
|
|
304
|
+
if (ack || emitter.connections.size) {
|
|
305
|
+
let timeout;
|
|
306
|
+
const unmount = onListen(emitter, (response) => {
|
|
307
|
+
if (response.extra.data.id === eventMessageId) {
|
|
308
|
+
ack(__spreadProps(__spreadValues({}, response), { error: null }));
|
|
309
|
+
clearTimeout(timeout);
|
|
310
|
+
unmount();
|
|
311
|
+
}
|
|
312
|
+
});
|
|
313
|
+
timeout = setTimeout(() => {
|
|
314
|
+
unmount();
|
|
315
|
+
ack({ error: new Error("Server did not acknowledge the event"), data: null, extra: null });
|
|
316
|
+
}, emitter.timeout);
|
|
317
|
+
}
|
|
318
|
+
const emitterInstance = yield socket.__modifySend(emitter);
|
|
319
|
+
sendEventMessage({ id: eventMessageId, data: emitterInstance.data, endpoint: emitterInstance.endpoint });
|
|
320
|
+
socket.events.emitEmitterEvent(emitterInstance);
|
|
321
|
+
});
|
|
322
|
+
if (socket.autoConnect) {
|
|
323
|
+
connect();
|
|
324
|
+
}
|
|
325
|
+
socket.appManager.events.onOnline(() => {
|
|
326
|
+
if (socket.autoConnect && !open) {
|
|
327
|
+
connect();
|
|
328
|
+
}
|
|
329
|
+
});
|
|
330
|
+
return {
|
|
331
|
+
open,
|
|
332
|
+
listeners,
|
|
333
|
+
reconnectionAttempts,
|
|
334
|
+
connecting,
|
|
335
|
+
listen,
|
|
336
|
+
removeListener,
|
|
337
|
+
emit,
|
|
338
|
+
connect,
|
|
339
|
+
reconnect,
|
|
340
|
+
disconnect
|
|
341
|
+
};
|
|
342
|
+
};
|
|
343
|
+
|
|
344
|
+
// src/adapter/adapter.sse.ts
|
|
345
|
+
import { parseResponse as parseResponse2 } from "@hyper-fetch/core";
|
|
346
|
+
var sseAdapter = (socket) => {
|
|
347
|
+
const {
|
|
348
|
+
open,
|
|
349
|
+
reconnectionAttempts,
|
|
350
|
+
listeners,
|
|
351
|
+
connecting,
|
|
352
|
+
removeListener,
|
|
353
|
+
onConnect,
|
|
354
|
+
onReconnect,
|
|
355
|
+
onDisconnect,
|
|
356
|
+
onListen,
|
|
357
|
+
onOpen,
|
|
358
|
+
onError,
|
|
359
|
+
onEvent,
|
|
360
|
+
onClose
|
|
361
|
+
} = getSocketAdapterBindings(socket);
|
|
362
|
+
let pingTimer;
|
|
363
|
+
let pongTimer;
|
|
364
|
+
let adapter = getSSEAdapter(socket);
|
|
365
|
+
const connect = () => {
|
|
366
|
+
const enabled = onConnect();
|
|
367
|
+
if (!enabled)
|
|
368
|
+
return;
|
|
369
|
+
adapter == null ? void 0 : adapter.close();
|
|
370
|
+
adapter = getSSEAdapter(socket);
|
|
371
|
+
if (!adapter)
|
|
372
|
+
return;
|
|
373
|
+
const timeout = setTimeout(() => {
|
|
374
|
+
reconnect();
|
|
375
|
+
}, socket.reconnectTime);
|
|
376
|
+
adapter.onopen = () => {
|
|
377
|
+
clearTimeout(timeout);
|
|
378
|
+
onOpen();
|
|
379
|
+
};
|
|
380
|
+
adapter.onerror = (event) => {
|
|
381
|
+
onError(new Error(event.type));
|
|
382
|
+
};
|
|
383
|
+
adapter.onmessage = (event) => {
|
|
384
|
+
const extra = parseResponse2(event);
|
|
385
|
+
const response = parseResponse2(event.data);
|
|
386
|
+
const data = parseResponse2(response.data);
|
|
387
|
+
const eventListeners = listeners.get(response.endpoint) || /* @__PURE__ */ new Map();
|
|
388
|
+
eventListeners.forEach((_, action) => {
|
|
389
|
+
action({ data, extra: response });
|
|
390
|
+
});
|
|
391
|
+
onEvent(response.endpoint, data, extra);
|
|
392
|
+
};
|
|
393
|
+
};
|
|
394
|
+
const disconnect = () => {
|
|
395
|
+
onDisconnect();
|
|
396
|
+
adapter.close();
|
|
397
|
+
onClose();
|
|
398
|
+
clearTimers();
|
|
399
|
+
};
|
|
400
|
+
const reconnect = () => {
|
|
401
|
+
onReconnect(disconnect, connect);
|
|
402
|
+
};
|
|
403
|
+
const clearTimers = () => {
|
|
404
|
+
clearTimeout(pingTimer);
|
|
405
|
+
clearTimeout(pongTimer);
|
|
406
|
+
};
|
|
407
|
+
const listen = (listener, callback) => {
|
|
408
|
+
return onListen(listener, callback);
|
|
409
|
+
};
|
|
410
|
+
const emit = () => __async(void 0, null, function* () {
|
|
411
|
+
throw new Error("Cannot emit events in SSE mode");
|
|
412
|
+
});
|
|
413
|
+
if (socket.autoConnect) {
|
|
414
|
+
connect();
|
|
415
|
+
}
|
|
416
|
+
socket.appManager.events.onOnline(() => {
|
|
417
|
+
if (socket.autoConnect && !open) {
|
|
418
|
+
connect();
|
|
419
|
+
}
|
|
420
|
+
});
|
|
421
|
+
return {
|
|
422
|
+
open,
|
|
423
|
+
reconnectionAttempts,
|
|
424
|
+
listeners,
|
|
425
|
+
connecting,
|
|
426
|
+
listen,
|
|
427
|
+
removeListener,
|
|
428
|
+
emit,
|
|
429
|
+
connect,
|
|
430
|
+
reconnect,
|
|
431
|
+
disconnect
|
|
432
|
+
};
|
|
433
|
+
};
|
|
434
|
+
|
|
435
|
+
// src/adapter/adapter.utils.ts
|
|
436
|
+
var getSocketUrl = (socket) => {
|
|
437
|
+
const queryParams = socket.queryParamsStringify(socket.queryParams).substring(1);
|
|
438
|
+
const authParams = socket.queryParamsStringify(socket.auth).substring(1);
|
|
439
|
+
const connector = queryParams && authParams ? "&" : "";
|
|
440
|
+
const queryPrefix = authParams || queryParams ? "?" : "";
|
|
441
|
+
const fullUrl = `${socket.url}${queryPrefix}${authParams}${connector}${queryParams}`;
|
|
442
|
+
return fullUrl;
|
|
443
|
+
};
|
|
444
|
+
var getWebsocketAdapter = (socket) => {
|
|
445
|
+
var _a;
|
|
446
|
+
if (!(window == null ? void 0 : window.WebSocket))
|
|
447
|
+
return null;
|
|
448
|
+
return new WebSocket(getSocketUrl(socket), (_a = socket.options.adapterOptions) == null ? void 0 : _a.protocols);
|
|
449
|
+
};
|
|
450
|
+
var getSSEAdapter = (socket) => {
|
|
451
|
+
var _a;
|
|
452
|
+
if (!(window == null ? void 0 : window.EventSource))
|
|
453
|
+
return null;
|
|
454
|
+
return new EventSource(getSocketUrl(socket), (_a = socket.options.adapterOptions) == null ? void 0 : _a.protocols);
|
|
455
|
+
};
|
|
456
|
+
|
|
457
|
+
// src/adapter/adapter.bindings.ts
|
|
458
|
+
var getSocketAdapterBindings = (socket, defaults) => {
|
|
459
|
+
var _a, _b, _c, _d;
|
|
460
|
+
const logger = socket.loggerManager.init("Socket Adapter");
|
|
461
|
+
const listeners = /* @__PURE__ */ new Map();
|
|
462
|
+
let open = (_a = defaults == null ? void 0 : defaults.open) != null ? _a : false;
|
|
463
|
+
let connecting = (_b = defaults == null ? void 0 : defaults.connecting) != null ? _b : false;
|
|
464
|
+
let forceClosed = (_c = defaults == null ? void 0 : defaults.forceClosed) != null ? _c : false;
|
|
465
|
+
let reconnectionAttempts = (_d = defaults == null ? void 0 : defaults.reconnectionAttempts) != null ? _d : 0;
|
|
466
|
+
const onConnect = () => {
|
|
467
|
+
if (!socket.appManager.isOnline || connecting) {
|
|
468
|
+
logger.warning("Cannot initialize adapter.", {
|
|
469
|
+
connecting,
|
|
470
|
+
online: socket.appManager.isOnline
|
|
471
|
+
});
|
|
472
|
+
return false;
|
|
473
|
+
}
|
|
474
|
+
forceClosed = false;
|
|
475
|
+
connecting = true;
|
|
476
|
+
reconnectionAttempts = 0;
|
|
477
|
+
socket.events.emitConnecting();
|
|
478
|
+
return true;
|
|
479
|
+
};
|
|
480
|
+
const onDisconnect = () => {
|
|
481
|
+
logger.debug("Disconnecting", { reconnectionAttempts });
|
|
482
|
+
open = false;
|
|
483
|
+
connecting = false;
|
|
484
|
+
forceClosed = true;
|
|
485
|
+
reconnectionAttempts = 0;
|
|
486
|
+
return true;
|
|
487
|
+
};
|
|
488
|
+
const onReconnect = (disconnect, connect) => {
|
|
489
|
+
disconnect();
|
|
490
|
+
if (reconnectionAttempts < socket.reconnect) {
|
|
491
|
+
reconnectionAttempts += 1;
|
|
492
|
+
logger.debug("Reconnecting", { reconnectionAttempts });
|
|
493
|
+
connect();
|
|
494
|
+
socket.__onReconnectCallbacks.forEach((callback) => {
|
|
495
|
+
callback(socket);
|
|
496
|
+
});
|
|
497
|
+
socket.events.emitReconnecting(reconnectionAttempts);
|
|
498
|
+
return true;
|
|
499
|
+
}
|
|
500
|
+
logger.debug("Stopped reconnecting", { reconnectionAttempts });
|
|
501
|
+
socket.__onReconnectStopCallbacks.forEach((callback) => {
|
|
502
|
+
callback(socket);
|
|
503
|
+
});
|
|
504
|
+
socket.events.emitReconnectingStop(reconnectionAttempts);
|
|
505
|
+
return false;
|
|
506
|
+
};
|
|
507
|
+
const removeListener = (endpoint, callback) => {
|
|
508
|
+
const listenerGroup = listeners.get(endpoint);
|
|
509
|
+
if (listenerGroup) {
|
|
510
|
+
const unmount = listenerGroup.get(callback);
|
|
511
|
+
logger.debug("Removed event listener", { endpoint });
|
|
512
|
+
socket.events.emitListenerRemoveEvent(endpoint);
|
|
513
|
+
listenerGroup.delete(callback);
|
|
514
|
+
unmount == null ? void 0 : unmount();
|
|
515
|
+
return true;
|
|
516
|
+
}
|
|
517
|
+
return false;
|
|
518
|
+
};
|
|
519
|
+
const onListen = (listener, callback, unmount = () => null) => {
|
|
520
|
+
const listenerGroup = listeners.get(listener.endpoint) || listeners.set(listener.endpoint, /* @__PURE__ */ new Map()).get(listener.endpoint);
|
|
521
|
+
listenerGroup.set(callback, unmount);
|
|
522
|
+
return () => removeListener(listener.endpoint, callback);
|
|
523
|
+
};
|
|
524
|
+
const onEmit = (emitter) => {
|
|
525
|
+
if (connecting || !open) {
|
|
526
|
+
logger.error("Cannot emit event when connection is not open");
|
|
527
|
+
return false;
|
|
528
|
+
}
|
|
529
|
+
socket.events.emitEmitterEvent(emitter);
|
|
530
|
+
return true;
|
|
531
|
+
};
|
|
532
|
+
const onOpen = () => {
|
|
533
|
+
logger.info("Connection open");
|
|
534
|
+
socket.__onOpenCallbacks.forEach((callback) => {
|
|
535
|
+
callback(socket);
|
|
536
|
+
});
|
|
537
|
+
open = true;
|
|
538
|
+
connecting = false;
|
|
539
|
+
socket.events.emitOpen();
|
|
540
|
+
};
|
|
541
|
+
const onClose = () => {
|
|
542
|
+
logger.info("Connection closed");
|
|
543
|
+
socket.__onCloseCallbacks.forEach((callback) => {
|
|
544
|
+
callback(socket);
|
|
545
|
+
});
|
|
546
|
+
open = false;
|
|
547
|
+
connecting = false;
|
|
548
|
+
socket.events.emitClose();
|
|
549
|
+
};
|
|
550
|
+
const onError = (event) => {
|
|
551
|
+
logger.info("Error message", { event });
|
|
552
|
+
socket.__onErrorCallbacks.forEach((callback) => {
|
|
553
|
+
callback(event, socket);
|
|
554
|
+
});
|
|
555
|
+
socket.events.emitError(event);
|
|
556
|
+
};
|
|
557
|
+
const onEvent = (endpoint, response, resposeExtra) => {
|
|
558
|
+
logger.info("New event message", { response, resposeExtra });
|
|
559
|
+
const { data, extra } = socket.__modifyResponse({ data: response, extra: resposeExtra });
|
|
560
|
+
socket.events.emitListenerEvent(endpoint, { data, extra });
|
|
561
|
+
};
|
|
562
|
+
return {
|
|
563
|
+
open,
|
|
564
|
+
connecting,
|
|
565
|
+
reconnectionAttempts,
|
|
566
|
+
forceClosed,
|
|
567
|
+
onConnect,
|
|
568
|
+
onReconnect,
|
|
569
|
+
onDisconnect,
|
|
570
|
+
onListen,
|
|
571
|
+
onEmit,
|
|
572
|
+
onOpen,
|
|
573
|
+
onClose,
|
|
574
|
+
onError,
|
|
575
|
+
onEvent,
|
|
576
|
+
listeners,
|
|
577
|
+
removeListener
|
|
578
|
+
};
|
|
579
|
+
};
|
|
580
|
+
|
|
581
|
+
// src/socket/socket.ts
|
|
582
|
+
var Socket = class {
|
|
583
|
+
constructor(options) {
|
|
584
|
+
this.options = options;
|
|
585
|
+
this.emitter = new EventEmitter();
|
|
586
|
+
this.events = getSocketEvents(this.emitter);
|
|
587
|
+
this.__onOpenCallbacks = [];
|
|
588
|
+
this.__onCloseCallbacks = [];
|
|
589
|
+
this.__onReconnectCallbacks = [];
|
|
590
|
+
this.__onReconnectStopCallbacks = [];
|
|
591
|
+
this.__onMessageCallbacks = [];
|
|
592
|
+
this.__onSendCallbacks = [];
|
|
593
|
+
this.__onErrorCallbacks = [];
|
|
594
|
+
this.loggerManager = new LoggerManager(this);
|
|
595
|
+
this.appManager = new AppManager();
|
|
596
|
+
this.logger = this.loggerManager.init("Socket");
|
|
597
|
+
this.queryParamsStringify = (queryParams) => {
|
|
598
|
+
return stringifyQueryParams(queryParams, this.queryParamsConfig);
|
|
599
|
+
};
|
|
600
|
+
this.setDebug = (debug) => {
|
|
601
|
+
this.debug = debug;
|
|
602
|
+
return this;
|
|
603
|
+
};
|
|
604
|
+
this.setLoggerSeverity = (severity) => {
|
|
605
|
+
this.loggerManager.setSeverity(severity);
|
|
606
|
+
return this;
|
|
607
|
+
};
|
|
608
|
+
this.setLogger = (callback) => {
|
|
609
|
+
this.loggerManager = callback(this);
|
|
610
|
+
return this;
|
|
611
|
+
};
|
|
612
|
+
this.setAuth = (auth) => {
|
|
613
|
+
this.auth = auth;
|
|
614
|
+
this.adapter.reconnect();
|
|
615
|
+
return this;
|
|
616
|
+
};
|
|
617
|
+
this.setQuery = (queryParams) => {
|
|
618
|
+
this.queryParams = queryParams;
|
|
619
|
+
this.adapter.reconnect();
|
|
620
|
+
return this;
|
|
621
|
+
};
|
|
622
|
+
this.__modifySend = (emitter) => {
|
|
623
|
+
return interceptEmitter(this.__onSendCallbacks, emitter);
|
|
624
|
+
};
|
|
625
|
+
this.__modifyResponse = (data) => {
|
|
626
|
+
return interceptListener(this.__onMessageCallbacks, data, this);
|
|
627
|
+
};
|
|
628
|
+
this.createListener = () => {
|
|
629
|
+
return (options) => {
|
|
630
|
+
return new Listener(this, options);
|
|
631
|
+
};
|
|
632
|
+
};
|
|
633
|
+
this.createEmitter = () => {
|
|
634
|
+
return (options) => {
|
|
635
|
+
return new Emitter(this, options);
|
|
636
|
+
};
|
|
637
|
+
};
|
|
638
|
+
const {
|
|
639
|
+
url,
|
|
640
|
+
auth,
|
|
641
|
+
adapter,
|
|
642
|
+
queryParams,
|
|
643
|
+
autoConnect,
|
|
644
|
+
reconnect,
|
|
645
|
+
reconnectTime,
|
|
646
|
+
queryParamsConfig,
|
|
647
|
+
queryParamsStringify
|
|
648
|
+
} = this.options;
|
|
649
|
+
this.url = url;
|
|
650
|
+
this.auth = auth;
|
|
651
|
+
this.queryParams = queryParams;
|
|
652
|
+
this.debug = false;
|
|
653
|
+
this.autoConnect = autoConnect != null ? autoConnect : true;
|
|
654
|
+
this.reconnect = reconnect != null ? reconnect : Infinity;
|
|
655
|
+
this.reconnectTime = reconnectTime != null ? reconnectTime : DateInterval3.second * 2;
|
|
656
|
+
if (queryParamsConfig) {
|
|
657
|
+
this.queryParamsConfig = queryParamsConfig;
|
|
658
|
+
}
|
|
659
|
+
if (queryParamsStringify) {
|
|
660
|
+
this.queryParamsStringify = queryParamsStringify;
|
|
661
|
+
}
|
|
662
|
+
this.adapter = adapter ? adapter(this) : websocketAdapter(this);
|
|
663
|
+
}
|
|
664
|
+
onOpen(callback) {
|
|
665
|
+
this.__onOpenCallbacks.push(callback);
|
|
666
|
+
return this;
|
|
667
|
+
}
|
|
668
|
+
onClose(callback) {
|
|
669
|
+
this.__onCloseCallbacks.push(callback);
|
|
670
|
+
return this;
|
|
671
|
+
}
|
|
672
|
+
onReconnect(callback) {
|
|
673
|
+
this.__onReconnectCallbacks.push(callback);
|
|
674
|
+
return this;
|
|
675
|
+
}
|
|
676
|
+
onReconnectStop(callback) {
|
|
677
|
+
this.__onReconnectStopCallbacks.push(callback);
|
|
678
|
+
return this;
|
|
679
|
+
}
|
|
680
|
+
onMessage(callback) {
|
|
681
|
+
this.__onMessageCallbacks.push(callback);
|
|
682
|
+
return this;
|
|
683
|
+
}
|
|
684
|
+
onSend(callback) {
|
|
685
|
+
this.__onSendCallbacks.push(callback);
|
|
686
|
+
return this;
|
|
687
|
+
}
|
|
688
|
+
onError(callback) {
|
|
689
|
+
this.__onErrorCallbacks.push(callback);
|
|
690
|
+
return this;
|
|
691
|
+
}
|
|
692
|
+
};
|
|
693
|
+
|
|
694
|
+
// src/socket/socket.utils.ts
|
|
695
|
+
var getErrorKey = () => "socket_error";
|
|
696
|
+
var getOpenKey = () => "socket_open";
|
|
697
|
+
var getCloseKey = () => "socket_close";
|
|
698
|
+
var getConnectingKey = () => "socket_connecting";
|
|
699
|
+
var getReconnectingKey = () => "socket_reconnecting";
|
|
700
|
+
var getReconnectingStopKey = () => "socket_reconnecting_stop";
|
|
701
|
+
var getListenerEventKey = () => `listener_event`;
|
|
702
|
+
var getListenerRemoveKey = () => `listener_remove`;
|
|
703
|
+
var getEmitterEventKey = () => `emitter_event`;
|
|
704
|
+
var getListenerRemoveByEndpointKey = (event) => `${event}_listener_remove`;
|
|
705
|
+
var getListenerEventByEndpointKey = (event) => `${event}_listener_event`;
|
|
706
|
+
var getEmitterEventByEndpointKey = (event) => `${event}_emitter_event`;
|
|
707
|
+
var interceptListener = (interceptors, data, socket) => {
|
|
708
|
+
let newResponse = data;
|
|
709
|
+
for (const interceptor of interceptors) {
|
|
710
|
+
newResponse = interceptor(data, socket);
|
|
711
|
+
if (!newResponse)
|
|
712
|
+
throw new Error("Listener modifier must return data");
|
|
713
|
+
}
|
|
714
|
+
return newResponse;
|
|
715
|
+
};
|
|
716
|
+
var interceptEmitter = (interceptors, emitter) => {
|
|
717
|
+
let newEmitter = emitter;
|
|
718
|
+
for (const interceptor of interceptors) {
|
|
719
|
+
newEmitter = interceptor(emitter);
|
|
720
|
+
if (!newEmitter)
|
|
721
|
+
throw new Error("Send modifier must return emitter");
|
|
722
|
+
}
|
|
723
|
+
return newEmitter;
|
|
724
|
+
};
|
|
725
|
+
|
|
726
|
+
// src/socket/socket.events.ts
|
|
727
|
+
var getSocketEvents = (eventEmitter) => ({
|
|
728
|
+
emitError: (event) => {
|
|
729
|
+
eventEmitter.emit(getErrorKey(), event);
|
|
730
|
+
},
|
|
731
|
+
emitOpen: () => {
|
|
732
|
+
eventEmitter.emit(getOpenKey());
|
|
733
|
+
},
|
|
734
|
+
emitClose: () => {
|
|
735
|
+
eventEmitter.emit(getCloseKey());
|
|
736
|
+
},
|
|
737
|
+
emitConnecting: () => {
|
|
738
|
+
eventEmitter.emit(getConnectingKey());
|
|
739
|
+
},
|
|
740
|
+
emitReconnecting: (attempts) => {
|
|
741
|
+
eventEmitter.emit(getReconnectingKey(), attempts);
|
|
742
|
+
},
|
|
743
|
+
emitReconnectingStop: (attempts) => {
|
|
744
|
+
eventEmitter.emit(getReconnectingStopKey(), attempts);
|
|
745
|
+
},
|
|
746
|
+
emitListenerEvent: (endpoint, { data, extra }) => {
|
|
747
|
+
eventEmitter.emit(getListenerEventKey(), { endpoint, data, extra });
|
|
748
|
+
eventEmitter.emit(getListenerEventByEndpointKey(endpoint), { endpoint, data, extra });
|
|
749
|
+
},
|
|
750
|
+
emitListenerRemoveEvent: (endpoint) => {
|
|
751
|
+
eventEmitter.emit(getListenerRemoveKey(), endpoint);
|
|
752
|
+
eventEmitter.emit(getListenerRemoveByEndpointKey(endpoint));
|
|
753
|
+
},
|
|
754
|
+
emitEmitterEvent: (emitter) => {
|
|
755
|
+
eventEmitter.emit(getEmitterEventKey(), emitter);
|
|
756
|
+
eventEmitter.emit(getEmitterEventByEndpointKey(emitter.endpoint), emitter);
|
|
757
|
+
},
|
|
758
|
+
onError: (callback) => {
|
|
759
|
+
eventEmitter.on(getErrorKey(), callback);
|
|
760
|
+
return () => eventEmitter.removeListener(getErrorKey(), callback);
|
|
761
|
+
},
|
|
762
|
+
onOpen: (callback) => {
|
|
763
|
+
eventEmitter.on(getOpenKey(), callback);
|
|
764
|
+
return () => eventEmitter.removeListener(getOpenKey(), callback);
|
|
765
|
+
},
|
|
766
|
+
onClose: (callback) => {
|
|
767
|
+
eventEmitter.on(getCloseKey(), callback);
|
|
768
|
+
return () => eventEmitter.removeListener(getCloseKey(), callback);
|
|
769
|
+
},
|
|
770
|
+
onConnecting: (callback) => {
|
|
771
|
+
eventEmitter.on(getConnectingKey(), callback);
|
|
772
|
+
return () => eventEmitter.removeListener(getConnectingKey(), callback);
|
|
773
|
+
},
|
|
774
|
+
onReconnecting: (callback) => {
|
|
775
|
+
eventEmitter.on(getReconnectingKey(), callback);
|
|
776
|
+
return () => eventEmitter.removeListener(getReconnectingKey(), callback);
|
|
777
|
+
},
|
|
778
|
+
onReconnectingStop: (callback) => {
|
|
779
|
+
eventEmitter.on(getReconnectingStopKey(), callback);
|
|
780
|
+
return () => eventEmitter.removeListener(getReconnectingStopKey(), callback);
|
|
781
|
+
},
|
|
782
|
+
onListenerEvent: (callback) => {
|
|
783
|
+
eventEmitter.on(getListenerEventKey(), callback);
|
|
784
|
+
return () => eventEmitter.removeListener(getListenerEventKey(), callback);
|
|
785
|
+
},
|
|
786
|
+
onEmitterEvent: (callback) => {
|
|
787
|
+
eventEmitter.on(getEmitterEventKey(), callback);
|
|
788
|
+
return () => eventEmitter.removeListener(getEmitterEventKey(), callback);
|
|
789
|
+
},
|
|
790
|
+
onListenerEventByEndpoint: (listener, callback) => {
|
|
791
|
+
eventEmitter.on(getListenerEventByEndpointKey(listener.endpoint), callback);
|
|
792
|
+
return () => eventEmitter.removeListener(getListenerEventByEndpointKey(listener.endpoint), callback);
|
|
793
|
+
},
|
|
794
|
+
onListenerRemove: (callback) => {
|
|
795
|
+
eventEmitter.on(getListenerRemoveKey(), callback);
|
|
796
|
+
return () => eventEmitter.removeListener(getListenerRemoveKey(), callback);
|
|
797
|
+
},
|
|
798
|
+
onListenerRemoveByEndpoint: (listener, callback) => {
|
|
799
|
+
eventEmitter.on(getListenerRemoveByEndpointKey(listener.endpoint), callback);
|
|
800
|
+
return () => eventEmitter.removeListener(getListenerRemoveByEndpointKey(listener.endpoint), callback);
|
|
801
|
+
},
|
|
802
|
+
onEmitterEventByEndpoint: (emitter, callback) => {
|
|
803
|
+
eventEmitter.on(getEmitterEventByEndpointKey(emitter.endpoint), callback);
|
|
804
|
+
return () => eventEmitter.removeListener(getEmitterEventByEndpointKey(emitter.endpoint), callback);
|
|
805
|
+
}
|
|
806
|
+
});
|
|
807
|
+
export {
|
|
808
|
+
Emitter,
|
|
809
|
+
Listener,
|
|
810
|
+
Socket,
|
|
811
|
+
getCloseKey,
|
|
812
|
+
getConnectingKey,
|
|
813
|
+
getEmitterEventByEndpointKey,
|
|
814
|
+
getEmitterEventKey,
|
|
815
|
+
getErrorKey,
|
|
816
|
+
getListenerEventByEndpointKey,
|
|
817
|
+
getListenerEventKey,
|
|
818
|
+
getListenerRemoveByEndpointKey,
|
|
819
|
+
getListenerRemoveKey,
|
|
820
|
+
getOpenKey,
|
|
821
|
+
getReconnectingKey,
|
|
822
|
+
getReconnectingStopKey,
|
|
823
|
+
getSSEAdapter,
|
|
824
|
+
getSocketAdapterBindings,
|
|
825
|
+
getSocketEvents,
|
|
826
|
+
getSocketUrl,
|
|
827
|
+
getWebsocketAdapter,
|
|
828
|
+
interceptEmitter,
|
|
829
|
+
interceptListener,
|
|
830
|
+
sseAdapter,
|
|
831
|
+
websocketAdapter
|
|
832
|
+
};
|
|
2
833
|
//# sourceMappingURL=index.esm.js.map
|