@hyper-fetch/sockets 8.2.4 → 9.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/__tests__/features/adapter/adapter.bindings.spec.ts +139 -0
- package/__tests__/features/adapter/adapter.sse.spec.ts +37 -3
- package/__tests__/features/adapter/adapter.websocket.connect.spec.ts +271 -0
- package/__tests__/features/adapter/adapter.websocket.spec.ts +30 -0
- package/__tests__/features/socket/socket.base.spec.ts +10 -2
- package/__tests__/features/socket/socket.callbacks.spec.ts +25 -1
- package/__tests__/features/socket/socket.methods.spec.ts +1 -1
- package/__tests__/features/socket/socket.utils.spec.ts +38 -1
- package/dist/adapter/adapter.bindings.d.ts +7 -4
- package/dist/adapter/adapter.bindings.d.ts.map +1 -1
- package/dist/adapter-sse/sse-adapter.d.ts.map +1 -1
- package/dist/adapter-websockets/websocket-adapter.d.ts.map +1 -1
- package/dist/adapter-websockets/websocket-adapter.types.d.ts +5 -1
- package/dist/adapter-websockets/websocket-adapter.types.d.ts.map +1 -1
- package/dist/adapter-websockets/websocket-adapter.utils.d.ts.map +1 -1
- package/dist/index.mjs +149 -57
- package/dist/index.mjs.map +1 -1
- package/dist/socket/socket.d.ts +18 -1
- package/dist/socket/socket.d.ts.map +1 -1
- package/dist/socket/socket.types.d.ts +18 -0
- package/dist/socket/socket.types.d.ts.map +1 -1
- package/dist/socket/socket.utils.d.ts +6 -1
- package/dist/socket/socket.utils.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/adapter/adapter.bindings.ts +74 -15
- package/src/adapter-sse/sse-adapter.ts +32 -23
- package/src/adapter-websockets/websocket-adapter.ts +48 -30
- package/src/adapter-websockets/websocket-adapter.types.ts +5 -1
- package/src/adapter-websockets/websocket-adapter.utils.ts +3 -1
- package/src/socket/socket.ts +24 -1
- package/src/socket/socket.types.ts +20 -0
- package/src/socket/socket.utils.ts +25 -3
package/dist/socket/socket.d.ts
CHANGED
|
@@ -4,7 +4,7 @@ import { WebsocketAdapterType } from '../adapter-websockets/websocket-adapter';
|
|
|
4
4
|
import { EmitterInstance, EmitterOptionsType, Emitter } from '../emitter';
|
|
5
5
|
import { default as EventEmitter } from 'events';
|
|
6
6
|
import { ListenerOptionsType, Listener } from '../listener';
|
|
7
|
-
import { SocketOptionsType, ReconnectCallbackType, ReconnectFailedCallbackType, OpenCallbackType, CloseCallbackType, MessageCallbackType, SendCallbackType, ErrorCallbackType } from '.';
|
|
7
|
+
import { SocketOptionsType, ConnectCallbackType, SocketConnectionType, ReconnectCallbackType, ReconnectFailedCallbackType, OpenCallbackType, CloseCallbackType, MessageCallbackType, SendCallbackType, ErrorCallbackType } from '.';
|
|
8
8
|
import { ExtractAdapterExtraType, ExtractAdapterQueryParamsType } from '../types';
|
|
9
9
|
export declare class Socket<Adapter extends SocketAdapterInstance = WebsocketAdapterType> {
|
|
10
10
|
options: SocketOptionsType<Adapter>;
|
|
@@ -89,6 +89,7 @@ export declare class Socket<Adapter extends SocketAdapterInstance = WebsocketAda
|
|
|
89
89
|
reconnectTime: number;
|
|
90
90
|
debug: boolean;
|
|
91
91
|
autoConnect: boolean;
|
|
92
|
+
unstable_onConnectCallbacks: ConnectCallbackType<Adapter>[];
|
|
92
93
|
unstable_onConnectedCallbacks: OpenCallbackType[];
|
|
93
94
|
unstable_onDisconnectCallbacks: CloseCallbackType[];
|
|
94
95
|
unstable_onReconnectCallbacks: ReconnectCallbackType[];
|
|
@@ -113,6 +114,10 @@ export declare class Socket<Adapter extends SocketAdapterInstance = WebsocketAda
|
|
|
113
114
|
* This method reconnect the socket to the server
|
|
114
115
|
*/
|
|
115
116
|
reconnect: () => Promise<void>;
|
|
117
|
+
/**
|
|
118
|
+
* Set the default query params used by the next connection attempts.
|
|
119
|
+
* Call `reconnect()` to apply them to a live connection, or use `onConnect` to resolve them per attempt.
|
|
120
|
+
*/
|
|
116
121
|
setQueryParams: (queryParams: ExtractAdapterQueryParamsType<Adapter>) => this;
|
|
117
122
|
/**
|
|
118
123
|
* This method enables the logger usage and display the logs in console
|
|
@@ -129,6 +134,14 @@ export declare class Socket<Adapter extends SocketAdapterInstance = WebsocketAda
|
|
|
129
134
|
/**
|
|
130
135
|
* Callbacks
|
|
131
136
|
*/
|
|
137
|
+
/**
|
|
138
|
+
* Triggered before every connection attempt (initial, automatic and manual reconnect).
|
|
139
|
+
* Receives the connection details (`url`, `queryParams`, `adapterOptions`) and must return them, optionally
|
|
140
|
+
* modified. Use it to resolve values that change over time - like auth tokens - right when they are needed.
|
|
141
|
+
* @param callback
|
|
142
|
+
* @returns
|
|
143
|
+
*/
|
|
144
|
+
onConnect(callback: ConnectCallbackType<Adapter>): this;
|
|
132
145
|
/**
|
|
133
146
|
* Triggered when connection is opened
|
|
134
147
|
* @param callback
|
|
@@ -176,6 +189,10 @@ export declare class Socket<Adapter extends SocketAdapterInstance = WebsocketAda
|
|
|
176
189
|
* Interceptors
|
|
177
190
|
* ********************
|
|
178
191
|
*/
|
|
192
|
+
unstable__modifyConnection: (data: {
|
|
193
|
+
connection: SocketConnectionType<Adapter>;
|
|
194
|
+
attempt: number;
|
|
195
|
+
}) => Promise<SocketConnectionType<Adapter>>;
|
|
179
196
|
unstable__modifySend: (emitter: EmitterInstance) => EmitterInstance;
|
|
180
197
|
unstable__modifyResponse: (data: {
|
|
181
198
|
data: any;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"socket.d.ts","sourceRoot":"","sources":["../../src/socket/socket.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAClD,OAAO,EAAE,aAAa,EAAE,UAAU,EAAQ,MAAM,mBAAmB,CAAC;AACpE,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,SAAS,CAAC;AACrD,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,sCAAsC,CAAC;AAEjF,OAAO,KAAK,EAAE,eAAe,EAAE,kBAAkB,EAAE,MAAM,SAAS,CAAC;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AAkB,CAAC,QAAQ,EAAE,OAAO,CAAC,+DAG/E;CACH"}
|
|
@@ -11,6 +11,24 @@ export type SocketOptionsType<Adapter extends SocketAdapterInstance> = {
|
|
|
11
11
|
adapterOptions?: ExtractAdapterOptionsType<Adapter>;
|
|
12
12
|
queryParams?: ExtractAdapterQueryParamsType<Adapter>;
|
|
13
13
|
};
|
|
14
|
+
/**
|
|
15
|
+
* Everything an adapter needs to open a connection. Built from the socket's static configuration
|
|
16
|
+
* and passed through the `onConnect` interceptors right before every connection attempt.
|
|
17
|
+
*/
|
|
18
|
+
export type SocketConnectionType<Adapter extends SocketAdapterInstance> = {
|
|
19
|
+
url: string;
|
|
20
|
+
queryParams: ExtractAdapterQueryParamsType<Adapter> | undefined;
|
|
21
|
+
adapterOptions: ExtractAdapterOptionsType<Adapter> | undefined;
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Interceptor invoked before every connection attempt (initial, automatic reconnect, manual reconnect).
|
|
25
|
+
* Receives the connection details and must return them - modified or not. May be async.
|
|
26
|
+
* `attempt` is `0` for a fresh connection and the reconnection attempt number otherwise.
|
|
27
|
+
*/
|
|
28
|
+
export type ConnectCallbackType<Adapter extends SocketAdapterInstance> = (data: {
|
|
29
|
+
connection: SocketConnectionType<Adapter>;
|
|
30
|
+
attempt: number;
|
|
31
|
+
}) => SocketConnectionType<Adapter> | Promise<SocketConnectionType<Adapter>>;
|
|
14
32
|
export type ReconnectCallbackType = () => void;
|
|
15
33
|
export type ReconnectFailedCallbackType = () => void;
|
|
16
34
|
export type OpenCallbackType = () => void;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"socket.types.d.ts","sourceRoot":"","sources":["../../src/socket/socket.types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,SAAS,CAAC;AACrD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAC/C,OAAO,KAAK,EAAE,MAAM,EAAE,M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AAM,IAAI,CAAC;AAC1C,MAAM,MAAM,iBAAiB,GAAG,MAAM,IAAI,CAAC;AAC3C,MAAM,MAAM,mBAAmB,CAAC,KAAK,IAAI,CAAC,IAAI,EAAE;IAAE,KAAK,EAAE,KAAK,CAAA;CAAE,KAAK,KAAK,CAAC;AAC3E,MAAM,MAAM,gBAAgB,CAAC,WAAW,SAAS,eAAe,IAAI,CAAC,IAAI,EAAE;IAAE,OAAO,EAAE,WAAW,CAAA;CAAE,KAAK,eAAe,CAAC;AACxH,MAAM,MAAM,iBAAiB,CAAC,KAAK,IAAI,CAAC,IAAI,EAAE;IAAE,KAAK,EAAE,KAAK,CAAA;CAAE,KAAK,IAAI,CAAC"}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
import { SocketAdapterInstance } from '../adapter';
|
|
1
2
|
import { EmitterInstance } from '../emitter';
|
|
2
|
-
import { MessageCallbackType, SendCallbackType } from './socket.types';
|
|
3
|
+
import { ConnectCallbackType, MessageCallbackType, SendCallbackType, SocketConnectionType } from './socket.types';
|
|
3
4
|
export declare const getErrorKey: () => string;
|
|
4
5
|
export declare const getOpenKey: () => string;
|
|
5
6
|
export declare const getCloseKey: () => string;
|
|
@@ -22,4 +23,8 @@ export declare const interceptListener: (interceptors: MessageCallbackType<any>[
|
|
|
22
23
|
extra: any;
|
|
23
24
|
};
|
|
24
25
|
export declare const interceptEmitter: <EmitterType extends EmitterInstance>(interceptors: SendCallbackType<EmitterType>[], emitter: EmitterType) => EmitterType;
|
|
26
|
+
export declare const interceptConnection: <Adapter extends SocketAdapterInstance>(interceptors: ConnectCallbackType<Adapter>[], data: {
|
|
27
|
+
connection: SocketConnectionType<Adapter>;
|
|
28
|
+
attempt: number;
|
|
29
|
+
}) => Promise<SocketConnectionType<Adapter>>;
|
|
25
30
|
//# sourceMappingURL=socket.utils.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"socket.utils.d.ts","sourceRoot":"","sources":["../../src/socket/socket.ut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jB,GAAG;WAAS,GAAG;CAWzG,CAAC;AAEF,eAAO,MAAM,gBAAgB,GAAI,WAAW,SAAS,eAAe,EAClE,cAAc,gBAAgB,CAAC,WAAW,CAAC,EAAE,EAC7C,SAAS,WAAW,gBAWrB,CAAC;AAEF,eAAO,MAAM,mBAAmB,GAAU,OAAO,SAAS,qBAAqB,EAC7E,cAAc,mBAAmB,CAAC,OAAO,CAAC,EAAE,EAC5C,MAAM;IAAE,UAAU,EAAE,oBAAoB,CAAC,OAAO,CAAC,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,KACnE,OAAO,CAAC,oBAAoB,CAAC,OAAO,CAAC,CAYvC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hyper-fetch/sockets",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "9.0.1",
|
|
4
4
|
"description": "Sockets connection for hyper-fetch",
|
|
5
5
|
"author": "Maciej Pyrc <maciekpyrc@gmail.com>, Kacper Skawina <kacper.skawina@gmail.com>",
|
|
6
6
|
"homepage": "https://hyperfetch.bettertyped.com/",
|
|
@@ -1,16 +1,52 @@
|
|
|
1
1
|
import type { SocketAdapterInstance } from "adapter";
|
|
2
2
|
import type { EmitterInstance } from "emitter";
|
|
3
3
|
import type { ListenerCallbackType, ListenerOfAdapter } from "listener";
|
|
4
|
-
import type { Socket } from "socket";
|
|
5
|
-
import type { ExtractAdapterExtraType } from "types";
|
|
4
|
+
import type { Socket, SocketConnectionType } from "socket";
|
|
5
|
+
import type { ExtractAdapterExtraType, ExtractAdapterQueryParamsType } from "types";
|
|
6
6
|
|
|
7
7
|
export const getAdapterBindings = <T extends SocketAdapterInstance>(socket: Socket<T>) => {
|
|
8
8
|
const { adapter } = socket;
|
|
9
9
|
const logger = socket.loggerManager.initialize(socket, "Socket Bindings");
|
|
10
10
|
|
|
11
|
+
// Every connection attempt gets an id, so an attempt that was cancelled or superseded while
|
|
12
|
+
// its connection was being prepared (async interceptors) can be discarded safely.
|
|
13
|
+
let connectionId = 0;
|
|
14
|
+
|
|
15
|
+
const onError = ({ error }: { error: Error }) => {
|
|
16
|
+
logger.info({ title: "Error message", type: "system", extra: { error } });
|
|
17
|
+
socket.unstable_onErrorCallbacks.forEach((callback) => {
|
|
18
|
+
callback({ error });
|
|
19
|
+
});
|
|
20
|
+
socket.events.emitError({ error });
|
|
21
|
+
};
|
|
22
|
+
|
|
11
23
|
// Methods
|
|
12
24
|
|
|
13
|
-
|
|
25
|
+
/**
|
|
26
|
+
* Marks the current connection attempt as failed before any transport was established
|
|
27
|
+
* (e.g. an `onConnect` interceptor threw or the transport could not be created).
|
|
28
|
+
*/
|
|
29
|
+
const onConnectFailed = ({ error }: { error: Error }) => {
|
|
30
|
+
logger.error({ title: "Connection attempt failed", type: "system", extra: { error } });
|
|
31
|
+
socket.adapter.setConnecting(false);
|
|
32
|
+
socket.events.emitConnecting({ connecting: false });
|
|
33
|
+
onError({ error });
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Opens a connection attempt. Returns the connection details (after `onConnect` interceptors) that the adapter
|
|
38
|
+
* should use to establish the connection, or `null` when no attempt should be made right now
|
|
39
|
+
* (already connected, offline, another attempt in progress or the interceptors failed).
|
|
40
|
+
*/
|
|
41
|
+
const onConnect = async (): Promise<SocketConnectionType<T> | null> => {
|
|
42
|
+
if (adapter.connected) {
|
|
43
|
+
logger.debug({
|
|
44
|
+
title: "Already connected, use reconnect to establish a new connection",
|
|
45
|
+
type: "system",
|
|
46
|
+
extra: {},
|
|
47
|
+
});
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
14
50
|
if (!socket.appManager.isOnline || adapter.connecting) {
|
|
15
51
|
logger.warning({
|
|
16
52
|
title: "Cannot initialize adapter.",
|
|
@@ -20,14 +56,43 @@ export const getAdapterBindings = <T extends SocketAdapterInstance>(socket: Sock
|
|
|
20
56
|
online: socket.appManager.isOnline,
|
|
21
57
|
},
|
|
22
58
|
});
|
|
23
|
-
return
|
|
59
|
+
return null;
|
|
24
60
|
}
|
|
25
61
|
|
|
62
|
+
// Set by onReconnect before it calls connect - 0 means a fresh connection
|
|
63
|
+
const attempt = adapter.reconnectionAttempts;
|
|
64
|
+
connectionId += 1;
|
|
65
|
+
const currentConnectionId = connectionId;
|
|
66
|
+
|
|
26
67
|
socket.adapter.setForceClosed(false);
|
|
27
68
|
socket.adapter.setConnecting(true);
|
|
28
69
|
socket.adapter.setReconnectionAttempts(0);
|
|
29
70
|
socket.events.emitConnecting({ connecting: true });
|
|
30
|
-
|
|
71
|
+
|
|
72
|
+
const defaults: SocketConnectionType<T> = {
|
|
73
|
+
url: socket.url,
|
|
74
|
+
queryParams: adapter.queryParams,
|
|
75
|
+
adapterOptions: adapter.adapterOptions,
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
let connection: SocketConnectionType<T>;
|
|
79
|
+
try {
|
|
80
|
+
// Yield once so callbacks chained right after socket creation (`new Socket().onConnect(...)`)
|
|
81
|
+
// are registered before the interceptors of the first (auto) connection are collected
|
|
82
|
+
await Promise.resolve();
|
|
83
|
+
connection = await socket.unstable__modifyConnection({ connection: defaults, attempt });
|
|
84
|
+
} catch (error) {
|
|
85
|
+
onConnectFailed({ error: error instanceof Error ? error : new Error(String(error)) });
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const cancelled = currentConnectionId !== connectionId || !adapter.connecting;
|
|
90
|
+
if (cancelled) {
|
|
91
|
+
logger.debug({ title: "Connection attempt cancelled while preparing", type: "system", extra: { attempt } });
|
|
92
|
+
return null;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return connection;
|
|
31
96
|
};
|
|
32
97
|
|
|
33
98
|
const onDisconnect = (): boolean => {
|
|
@@ -156,14 +221,6 @@ export const getAdapterBindings = <T extends SocketAdapterInstance>(socket: Sock
|
|
|
156
221
|
});
|
|
157
222
|
};
|
|
158
223
|
|
|
159
|
-
const onError = ({ error }: { error: Error }) => {
|
|
160
|
-
logger.info({ title: "Error message", type: "system", extra: { error } });
|
|
161
|
-
socket.unstable_onErrorCallbacks.forEach((callback) => {
|
|
162
|
-
callback({ error });
|
|
163
|
-
});
|
|
164
|
-
socket.events.emitError({ error });
|
|
165
|
-
};
|
|
166
|
-
|
|
167
224
|
const onEvent = ({ topic, data, extra }: { topic: string; data: any; extra: ExtractAdapterExtraType<T> }) => {
|
|
168
225
|
logger.info({ title: "New event message", type: "system", extra: { topic, data, extra } });
|
|
169
226
|
|
|
@@ -172,8 +229,9 @@ export const getAdapterBindings = <T extends SocketAdapterInstance>(socket: Sock
|
|
|
172
229
|
socket.events.emitListenerEvent({ topic, data: modifiedData, extra: modifiedExtra });
|
|
173
230
|
};
|
|
174
231
|
|
|
175
|
-
|
|
176
|
-
|
|
232
|
+
/** Maps query params to the format required by the adapter (defaults to the adapter's stored query params) */
|
|
233
|
+
const getQueryParams = (queryParams: ExtractAdapterQueryParamsType<T> | undefined = adapter.queryParams) =>
|
|
234
|
+
socket.adapter.unstable_queryParamsMapper(queryParams, socket.adapter.queryParamsConfig);
|
|
177
235
|
|
|
178
236
|
return {
|
|
179
237
|
socket,
|
|
@@ -181,6 +239,7 @@ export const getAdapterBindings = <T extends SocketAdapterInstance>(socket: Sock
|
|
|
181
239
|
logger,
|
|
182
240
|
getQueryParams,
|
|
183
241
|
onConnect,
|
|
242
|
+
onConnectFailed,
|
|
184
243
|
onReconnect,
|
|
185
244
|
onDisconnect,
|
|
186
245
|
onListen,
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-use-before-define */
|
|
2
|
-
import type { QueryParamsType } from "@hyper-fetch/core";
|
|
2
|
+
import type { EmptyTypes, QueryParamsType } from "@hyper-fetch/core";
|
|
3
3
|
import { stringifyQueryParams } from "@hyper-fetch/core";
|
|
4
4
|
import type { SocketData } from "adapter";
|
|
5
5
|
import { SocketAdapter } from "adapter/adapter";
|
|
@@ -44,6 +44,7 @@ export const ServerSentEventsAdapter = (): ServerSentEventsAdapterType =>
|
|
|
44
44
|
adapter,
|
|
45
45
|
getQueryParams,
|
|
46
46
|
onConnect,
|
|
47
|
+
onConnectFailed,
|
|
47
48
|
onReconnect,
|
|
48
49
|
onDisconnect,
|
|
49
50
|
onListen,
|
|
@@ -59,22 +60,21 @@ export const ServerSentEventsAdapter = (): ServerSentEventsAdapterType =>
|
|
|
59
60
|
? socket.adapter.adapterOptions?.autoConnect
|
|
60
61
|
: true;
|
|
61
62
|
|
|
62
|
-
const connect = () => {
|
|
63
|
-
const
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
return Promise.resolve(false);
|
|
63
|
+
const connect = async (): Promise<boolean> => {
|
|
64
|
+
const connection = await onConnect();
|
|
65
|
+
if (!connection) {
|
|
66
|
+
return socket.adapter.connected;
|
|
67
67
|
}
|
|
68
68
|
|
|
69
69
|
sse?.clearListeners();
|
|
70
70
|
sse?.close();
|
|
71
71
|
|
|
72
|
-
const
|
|
72
|
+
const url = getSocketUrl(connection.url, getQueryParams(connection.queryParams));
|
|
73
|
+
const eventSource = createEventSource(url, connection.adapterOptions);
|
|
73
74
|
sse = eventSource;
|
|
74
75
|
|
|
75
|
-
// Make sure we picked good environment
|
|
76
76
|
if (!eventSource) {
|
|
77
|
-
return
|
|
77
|
+
return false;
|
|
78
78
|
}
|
|
79
79
|
|
|
80
80
|
// Reconnection timeout
|
|
@@ -127,22 +127,18 @@ export const ServerSentEventsAdapter = (): ServerSentEventsAdapterType =>
|
|
|
127
127
|
};
|
|
128
128
|
|
|
129
129
|
const disconnect = async (): Promise<boolean> => {
|
|
130
|
-
if (!sse) {
|
|
131
|
-
adapter.setConnected(false);
|
|
132
|
-
adapter.setConnecting(false);
|
|
133
|
-
return true;
|
|
134
|
-
}
|
|
135
130
|
const currentSse = sse;
|
|
131
|
+
const hasTransport = currentSse && currentSse.readyState !== EventSource.CLOSED;
|
|
136
132
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
adapter.setConnecting(false);
|
|
140
|
-
return true;
|
|
141
|
-
}
|
|
142
|
-
|
|
133
|
+
// When there is nothing to close, only a connection attempt may still be preparing (onConnect) - cancel it
|
|
134
|
+
const wasConnecting = adapter.connecting;
|
|
143
135
|
onDisconnect();
|
|
144
|
-
|
|
145
|
-
|
|
136
|
+
if (hasTransport) {
|
|
137
|
+
currentSse.close();
|
|
138
|
+
}
|
|
139
|
+
if (hasTransport || wasConnecting) {
|
|
140
|
+
onDisconnected();
|
|
141
|
+
}
|
|
146
142
|
|
|
147
143
|
return true;
|
|
148
144
|
};
|
|
@@ -151,6 +147,20 @@ export const ServerSentEventsAdapter = (): ServerSentEventsAdapterType =>
|
|
|
151
147
|
onReconnect({ disconnect, connect });
|
|
152
148
|
};
|
|
153
149
|
|
|
150
|
+
/** Creates the transport, reporting a failed attempt when the environment or the connection details are invalid */
|
|
151
|
+
const createEventSource = (url: string, adapterOptions: SSEAdapterOptionsType | EmptyTypes) => {
|
|
152
|
+
try {
|
|
153
|
+
const instance = getServerSentEventsAdapter(url, adapterOptions);
|
|
154
|
+
if (!instance) {
|
|
155
|
+
onConnectFailed({ error: new Error("EventSource is not available in this environment") });
|
|
156
|
+
}
|
|
157
|
+
return instance;
|
|
158
|
+
} catch (error) {
|
|
159
|
+
onConnectFailed({ error: error as Error });
|
|
160
|
+
return null;
|
|
161
|
+
}
|
|
162
|
+
};
|
|
163
|
+
|
|
154
164
|
const listen = (
|
|
155
165
|
listener: ListenerOfAdapter<ServerSentEventsAdapterType>,
|
|
156
166
|
callback: ListenerCallbackType<ServerSentEventsAdapterType, any>,
|
|
@@ -163,7 +173,6 @@ export const ServerSentEventsAdapter = (): ServerSentEventsAdapterType =>
|
|
|
163
173
|
};
|
|
164
174
|
|
|
165
175
|
// Initialize
|
|
166
|
-
|
|
167
176
|
if (autoConnect) {
|
|
168
177
|
connect();
|
|
169
178
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-use-before-define */
|
|
2
|
-
import type { QueryParamsType } from "@hyper-fetch/core";
|
|
2
|
+
import type { EmptyTypes, QueryParamsType } from "@hyper-fetch/core";
|
|
3
3
|
import { stringifyQueryParams, Time } from "@hyper-fetch/core";
|
|
4
4
|
import type { SocketData } from "adapter";
|
|
5
5
|
import { SocketAdapter } from "adapter/adapter";
|
|
@@ -42,9 +42,9 @@ export const WebsocketAdapter = (): WebsocketAdapterType =>
|
|
|
42
42
|
.setConnector(
|
|
43
43
|
({
|
|
44
44
|
socket,
|
|
45
|
-
logger,
|
|
46
45
|
getQueryParams,
|
|
47
46
|
onConnect,
|
|
47
|
+
onConnectFailed,
|
|
48
48
|
onReconnect,
|
|
49
49
|
onDisconnect,
|
|
50
50
|
onListen,
|
|
@@ -60,34 +60,29 @@ export const WebsocketAdapter = (): WebsocketAdapterType =>
|
|
|
60
60
|
: true;
|
|
61
61
|
|
|
62
62
|
let websocket: ReturnType<typeof getWebsocketAdapter> | undefined;
|
|
63
|
+
// Options of the current connection attempt (after onConnect interceptors)
|
|
64
|
+
let options: WebsocketAdapterOptionsType | EmptyTypes = socket.adapter.adapterOptions;
|
|
63
65
|
|
|
64
66
|
let pingTimer: ReturnType<typeof setTimeout> | undefined;
|
|
65
67
|
let pongTimer: ReturnType<typeof setTimeout> | undefined;
|
|
66
68
|
let timeout: ReturnType<typeof setTimeout> | undefined;
|
|
67
69
|
|
|
68
70
|
const connect = async (): Promise<boolean> => {
|
|
69
|
-
const
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
return false;
|
|
71
|
+
const connection = await onConnect();
|
|
72
|
+
if (!connection) {
|
|
73
|
+
return socket.adapter.connected;
|
|
73
74
|
}
|
|
74
75
|
|
|
75
76
|
clearTimeout(timeout);
|
|
76
77
|
websocket?.clearListeners();
|
|
77
78
|
websocket?.close(1000);
|
|
78
79
|
|
|
79
|
-
|
|
80
|
+
options = connection.adapterOptions;
|
|
81
|
+
const url = getSocketUrl(connection.url, getQueryParams(connection.queryParams));
|
|
82
|
+
const newWebsocket = createWebsocket(url, options);
|
|
80
83
|
websocket = newWebsocket;
|
|
81
84
|
|
|
82
|
-
// Make sure we picked good environment
|
|
83
85
|
if (!newWebsocket) {
|
|
84
|
-
logger.error({
|
|
85
|
-
title: "Cannot connect to websocket",
|
|
86
|
-
type: "system",
|
|
87
|
-
extra: {
|
|
88
|
-
websocket,
|
|
89
|
-
},
|
|
90
|
-
});
|
|
91
86
|
return false;
|
|
92
87
|
}
|
|
93
88
|
|
|
@@ -160,20 +155,23 @@ export const WebsocketAdapter = (): WebsocketAdapterType =>
|
|
|
160
155
|
};
|
|
161
156
|
|
|
162
157
|
const disconnect = async (): Promise<boolean> => {
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
return true;
|
|
167
|
-
}
|
|
158
|
+
// Manual disconnect cancels a scheduled automatic reconnect
|
|
159
|
+
clearTimeout(timeout);
|
|
160
|
+
|
|
168
161
|
const currentWebsocket = websocket;
|
|
169
|
-
const
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
162
|
+
const hasTransport = currentWebsocket && currentWebsocket.readyState !== WebSocket.CLOSED;
|
|
163
|
+
|
|
164
|
+
if (!hasTransport) {
|
|
165
|
+
// Nothing to close - only a connection attempt may still be preparing (onConnect), cancel it
|
|
166
|
+
const wasConnecting = socket.adapter.connecting;
|
|
167
|
+
onDisconnect();
|
|
168
|
+
if (wasConnecting) {
|
|
169
|
+
onDisconnected();
|
|
175
170
|
}
|
|
171
|
+
return true;
|
|
172
|
+
}
|
|
176
173
|
|
|
174
|
+
const promise = new Promise<boolean>((resolve) => {
|
|
177
175
|
const resolveDisconnected = () => {
|
|
178
176
|
resolve(true);
|
|
179
177
|
currentWebsocket.removeEventListener("close", resolveDisconnected);
|
|
@@ -192,13 +190,29 @@ export const WebsocketAdapter = (): WebsocketAdapterType =>
|
|
|
192
190
|
await onReconnect({ disconnect, connect });
|
|
193
191
|
};
|
|
194
192
|
|
|
193
|
+
/** Creates the transport, reporting a failed attempt when the environment or the connection details are invalid */
|
|
194
|
+
const createWebsocket = (url: string, adapterOptions: WebsocketAdapterOptionsType | EmptyTypes) => {
|
|
195
|
+
try {
|
|
196
|
+
const instance = getWebsocketAdapter(url, adapterOptions);
|
|
197
|
+
if (!instance) {
|
|
198
|
+
onConnectFailed({ error: new Error("WebSocket is not available in this environment") });
|
|
199
|
+
}
|
|
200
|
+
return instance;
|
|
201
|
+
} catch (error) {
|
|
202
|
+
onConnectFailed({ error: error as Error });
|
|
203
|
+
return null;
|
|
204
|
+
}
|
|
205
|
+
};
|
|
206
|
+
|
|
195
207
|
const clearTimers = () => {
|
|
196
208
|
clearTimeout(pingTimer);
|
|
197
209
|
clearTimeout(pongTimer);
|
|
198
210
|
};
|
|
199
211
|
|
|
200
212
|
const sendEventMessage = ({ topic, payload }: Pick<EmitterInstance, "topic" | "payload">) => {
|
|
201
|
-
if (!websocket) {
|
|
213
|
+
if (!websocket) {
|
|
214
|
+
return false;
|
|
215
|
+
}
|
|
202
216
|
websocket!.send(JSON.stringify({ topic, data: payload }));
|
|
203
217
|
return true;
|
|
204
218
|
};
|
|
@@ -209,11 +223,13 @@ export const WebsocketAdapter = (): WebsocketAdapterType =>
|
|
|
209
223
|
pingTimeout = Time.SEC * 5,
|
|
210
224
|
pongTimeout = Time.SEC * 5,
|
|
211
225
|
heartbeatMessage = "heartbeat",
|
|
212
|
-
} =
|
|
226
|
+
} = options ||
|
|
213
227
|
/* istanbul ignore next */
|
|
214
228
|
{};
|
|
215
229
|
|
|
216
|
-
if (socket.adapter.connecting || !heartbeat) {
|
|
230
|
+
if (socket.adapter.connecting || !heartbeat) {
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
217
233
|
clearTimers();
|
|
218
234
|
pingTimer = setTimeout(() => {
|
|
219
235
|
sendEventMessage({ topic: "heartbeat", payload: heartbeatMessage });
|
|
@@ -233,7 +249,9 @@ export const WebsocketAdapter = (): WebsocketAdapterType =>
|
|
|
233
249
|
|
|
234
250
|
const emit = async (emitter: EmitterInstance) => {
|
|
235
251
|
const mappedEmitter = await onEmit({ emitter });
|
|
236
|
-
if (!mappedEmitter) {
|
|
252
|
+
if (!mappedEmitter) {
|
|
253
|
+
return;
|
|
254
|
+
}
|
|
237
255
|
|
|
238
256
|
return sendEventMessage(mappedEmitter);
|
|
239
257
|
};
|
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
export type WebsocketAdapterOptionsType = {
|
|
2
|
-
|
|
2
|
+
/**
|
|
3
|
+
* Subprotocols passed to the `WebSocket` constructor (`Sec-WebSocket-Protocol` header).
|
|
4
|
+
* To resolve them per connection attempt (e.g. short-lived auth tokens) use `socket.onConnect`.
|
|
5
|
+
*/
|
|
6
|
+
protocols?: string | string[];
|
|
3
7
|
pingTimeout?: number;
|
|
4
8
|
pongTimeout?: number;
|
|
5
9
|
heartbeatMessage?: string;
|
|
@@ -6,7 +6,9 @@ export const getWebsocketAdapter = (url: string, adapterOptions: WebsocketAdapte
|
|
|
6
6
|
/** istanbul ignore next */
|
|
7
7
|
try {
|
|
8
8
|
/** istanbul ignore next */
|
|
9
|
-
if (!window?.WebSocket) {
|
|
9
|
+
if (!window?.WebSocket) {
|
|
10
|
+
return null;
|
|
11
|
+
}
|
|
10
12
|
} catch {
|
|
11
13
|
return null;
|
|
12
14
|
}
|
package/src/socket/socket.ts
CHANGED
|
@@ -10,6 +10,8 @@ import type { ListenerOptionsType } from "listener";
|
|
|
10
10
|
import { Listener } from "listener";
|
|
11
11
|
import type {
|
|
12
12
|
SocketOptionsType,
|
|
13
|
+
ConnectCallbackType,
|
|
14
|
+
SocketConnectionType,
|
|
13
15
|
ReconnectCallbackType,
|
|
14
16
|
ReconnectFailedCallbackType,
|
|
15
17
|
OpenCallbackType,
|
|
@@ -18,7 +20,7 @@ import type {
|
|
|
18
20
|
SendCallbackType,
|
|
19
21
|
ErrorCallbackType,
|
|
20
22
|
} from "socket";
|
|
21
|
-
import { getSocketEvents, interceptListener, interceptEmitter } from "socket";
|
|
23
|
+
import { getSocketEvents, interceptListener, interceptEmitter, interceptConnection } from "socket";
|
|
22
24
|
import type { ExtractAdapterExtraType, ExtractAdapterQueryParamsType } from "types";
|
|
23
25
|
|
|
24
26
|
export class Socket<Adapter extends SocketAdapterInstance = WebsocketAdapterType> {
|
|
@@ -32,6 +34,7 @@ export class Socket<Adapter extends SocketAdapterInstance = WebsocketAdapterType
|
|
|
32
34
|
autoConnect: boolean;
|
|
33
35
|
|
|
34
36
|
// Callbacks
|
|
37
|
+
unstable_onConnectCallbacks: ConnectCallbackType<Adapter>[] = [];
|
|
35
38
|
unstable_onConnectedCallbacks: OpenCallbackType[] = [];
|
|
36
39
|
unstable_onDisconnectCallbacks: CloseCallbackType[] = [];
|
|
37
40
|
unstable_onReconnectCallbacks: ReconnectCallbackType[] = [];
|
|
@@ -87,6 +90,10 @@ export class Socket<Adapter extends SocketAdapterInstance = WebsocketAdapterType
|
|
|
87
90
|
await this.adapter.reconnect();
|
|
88
91
|
};
|
|
89
92
|
|
|
93
|
+
/**
|
|
94
|
+
* Set the default query params used by the next connection attempts.
|
|
95
|
+
* Call `reconnect()` to apply them to a live connection, or use `onConnect` to resolve them per attempt.
|
|
96
|
+
*/
|
|
90
97
|
setQueryParams = (queryParams: ExtractAdapterQueryParamsType<Adapter>) => {
|
|
91
98
|
this.adapter.setQueryParams(queryParams);
|
|
92
99
|
return this;
|
|
@@ -120,6 +127,18 @@ export class Socket<Adapter extends SocketAdapterInstance = WebsocketAdapterType
|
|
|
120
127
|
* Callbacks
|
|
121
128
|
*/
|
|
122
129
|
|
|
130
|
+
/**
|
|
131
|
+
* Triggered before every connection attempt (initial, automatic and manual reconnect).
|
|
132
|
+
* Receives the connection details (`url`, `queryParams`, `adapterOptions`) and must return them, optionally
|
|
133
|
+
* modified. Use it to resolve values that change over time - like auth tokens - right when they are needed.
|
|
134
|
+
* @param callback
|
|
135
|
+
* @returns
|
|
136
|
+
*/
|
|
137
|
+
onConnect(callback: ConnectCallbackType<Adapter>) {
|
|
138
|
+
this.unstable_onConnectCallbacks.push(callback);
|
|
139
|
+
return this;
|
|
140
|
+
}
|
|
141
|
+
|
|
123
142
|
/**
|
|
124
143
|
* Triggered when connection is opened
|
|
125
144
|
* @param callback
|
|
@@ -195,6 +214,10 @@ export class Socket<Adapter extends SocketAdapterInstance = WebsocketAdapterType
|
|
|
195
214
|
* ********************
|
|
196
215
|
*/
|
|
197
216
|
|
|
217
|
+
unstable__modifyConnection = (data: { connection: SocketConnectionType<Adapter>; attempt: number }) => {
|
|
218
|
+
return interceptConnection(this.unstable_onConnectCallbacks, data);
|
|
219
|
+
};
|
|
220
|
+
|
|
198
221
|
unstable__modifySend = (emitter: EmitterInstance) => {
|
|
199
222
|
return interceptEmitter(this.unstable_onSendCallbacks, emitter);
|
|
200
223
|
};
|
|
@@ -14,6 +14,26 @@ export type SocketOptionsType<Adapter extends SocketAdapterInstance> = {
|
|
|
14
14
|
queryParams?: ExtractAdapterQueryParamsType<Adapter>;
|
|
15
15
|
};
|
|
16
16
|
|
|
17
|
+
/**
|
|
18
|
+
* Everything an adapter needs to open a connection. Built from the socket's static configuration
|
|
19
|
+
* and passed through the `onConnect` interceptors right before every connection attempt.
|
|
20
|
+
*/
|
|
21
|
+
export type SocketConnectionType<Adapter extends SocketAdapterInstance> = {
|
|
22
|
+
url: string;
|
|
23
|
+
queryParams: ExtractAdapterQueryParamsType<Adapter> | undefined;
|
|
24
|
+
adapterOptions: ExtractAdapterOptionsType<Adapter> | undefined;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Interceptor invoked before every connection attempt (initial, automatic reconnect, manual reconnect).
|
|
29
|
+
* Receives the connection details and must return them - modified or not. May be async.
|
|
30
|
+
* `attempt` is `0` for a fresh connection and the reconnection attempt number otherwise.
|
|
31
|
+
*/
|
|
32
|
+
export type ConnectCallbackType<Adapter extends SocketAdapterInstance> = (data: {
|
|
33
|
+
connection: SocketConnectionType<Adapter>;
|
|
34
|
+
attempt: number;
|
|
35
|
+
}) => SocketConnectionType<Adapter> | Promise<SocketConnectionType<Adapter>>;
|
|
36
|
+
|
|
17
37
|
export type ReconnectCallbackType = () => void;
|
|
18
38
|
export type ReconnectFailedCallbackType = () => void;
|
|
19
39
|
export type OpenCallbackType = () => void;
|