@hyper-fetch/sockets 9.1.0 → 9.2.0
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.sse.codec.spec.ts +101 -0
- package/__tests__/features/adapter/adapter.websocket.codec.spec.ts +208 -0
- package/__tests__/features/socket/socket.methods.spec.ts +40 -0
- package/dist/adapter-sse/sse-adapter.d.ts.map +1 -1
- package/dist/adapter-sse/sse-adapter.types.d.ts +11 -0
- package/dist/adapter-sse/sse-adapter.types.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 +23 -0
- package/dist/adapter-websockets/websocket-adapter.types.d.ts.map +1 -1
- package/dist/index.mjs +63 -16
- package/dist/index.mjs.map +1 -1
- package/dist/socket/socket.d.ts +6 -0
- package/dist/socket/socket.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/adapter-sse/sse-adapter.ts +23 -4
- package/src/adapter-sse/sse-adapter.types.ts +9 -0
- package/src/adapter-websockets/websocket-adapter.ts +31 -6
- package/src/adapter-websockets/websocket-adapter.types.ts +20 -0
- package/src/socket/socket.ts +10 -0
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { createSseMockingServer } from "@hyper-fetch/testing";
|
|
2
|
+
import { waitFor } from "@testing-library/dom";
|
|
3
|
+
import type { SSEAdapterOptionsType, ServerSentEventsAdapterType } from "adapter-sse";
|
|
4
|
+
import { ServerSentEventsAdapter } from "adapter-sse";
|
|
5
|
+
|
|
6
|
+
import { createSocket } from "../../utils/socket.utils";
|
|
7
|
+
|
|
8
|
+
const decode: SSEAdapterOptionsType["decode"] = (event) => {
|
|
9
|
+
const message = JSON.parse(event.data);
|
|
10
|
+
if (message.type === "ping") {
|
|
11
|
+
return null;
|
|
12
|
+
}
|
|
13
|
+
return { topic: message.type, data: message.payload };
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
describe("SSE Adapter [ Decode ]", () => {
|
|
17
|
+
const { startServer, getSource } = createSseMockingServer();
|
|
18
|
+
|
|
19
|
+
const send = (data: string) => getSource().emitMessage(new MessageEvent("message", { data }));
|
|
20
|
+
|
|
21
|
+
const setup = async (adapterOptions?: SSEAdapterOptionsType) => {
|
|
22
|
+
const socket = createSocket<ServerSentEventsAdapterType>({
|
|
23
|
+
adapter: ServerSentEventsAdapter,
|
|
24
|
+
adapterOptions,
|
|
25
|
+
});
|
|
26
|
+
await startServer();
|
|
27
|
+
return socket;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
beforeEach(() => {
|
|
31
|
+
vi.resetAllMocks();
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it("should receive { topic, data } messages by default", async () => {
|
|
35
|
+
const socket = await setup();
|
|
36
|
+
const spy = vi.fn();
|
|
37
|
+
socket
|
|
38
|
+
.createListener()({ topic: "user" })
|
|
39
|
+
.listen(({ data }) => spy(data));
|
|
40
|
+
|
|
41
|
+
send(JSON.stringify({ topic: "user", data: { name: "Alice" } }));
|
|
42
|
+
|
|
43
|
+
await waitFor(() => {
|
|
44
|
+
expect(spy).toHaveBeenCalledWith({ name: "Alice" });
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it("should map incoming messages to listener topics with custom decode", async () => {
|
|
49
|
+
const socket = await setup({ decode });
|
|
50
|
+
const spy = vi.fn();
|
|
51
|
+
socket
|
|
52
|
+
.createListener()({ topic: "user" })
|
|
53
|
+
.listen(({ data }) => spy(data));
|
|
54
|
+
|
|
55
|
+
send(JSON.stringify({ type: "user", payload: { name: "Alice" } }));
|
|
56
|
+
|
|
57
|
+
await waitFor(() => {
|
|
58
|
+
expect(spy).toHaveBeenCalledWith({ name: "Alice" });
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it("should ignore messages decoded to null", async () => {
|
|
63
|
+
const socket = await setup({ decode });
|
|
64
|
+
const eventSpy = vi.fn();
|
|
65
|
+
socket.events.onListenerEvent(eventSpy);
|
|
66
|
+
|
|
67
|
+
send(JSON.stringify({ type: "ping" }));
|
|
68
|
+
send(JSON.stringify({ type: "user", payload: 1 }));
|
|
69
|
+
|
|
70
|
+
await waitFor(() => {
|
|
71
|
+
expect(eventSpy).toHaveBeenCalledTimes(1);
|
|
72
|
+
});
|
|
73
|
+
expect(eventSpy).toHaveBeenCalledWith(expect.objectContaining({ topic: "user", data: 1 }));
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it("should report decoder errors and keep the connection", async () => {
|
|
77
|
+
const error = new Error("Malformed message");
|
|
78
|
+
const socket = await setup({
|
|
79
|
+
decode: (event) => {
|
|
80
|
+
if (event.data === "broken") {
|
|
81
|
+
throw error;
|
|
82
|
+
}
|
|
83
|
+
return decode(event);
|
|
84
|
+
},
|
|
85
|
+
});
|
|
86
|
+
const errorSpy = vi.fn();
|
|
87
|
+
const spy = vi.fn();
|
|
88
|
+
socket.onError(errorSpy);
|
|
89
|
+
socket.createListener()({ topic: "user" }).listen(spy);
|
|
90
|
+
|
|
91
|
+
send("broken");
|
|
92
|
+
send(JSON.stringify({ type: "user", payload: 1 }));
|
|
93
|
+
|
|
94
|
+
await waitFor(() => {
|
|
95
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
|
96
|
+
});
|
|
97
|
+
expect(errorSpy).toHaveBeenCalledTimes(1);
|
|
98
|
+
expect(errorSpy).toHaveBeenCalledWith({ error });
|
|
99
|
+
expect(socket.adapter.connected).toBe(true);
|
|
100
|
+
});
|
|
101
|
+
});
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
import { createWebsocketMockingServer, waitForConnection } from "@hyper-fetch/testing";
|
|
2
|
+
import { waitFor } from "@testing-library/dom";
|
|
3
|
+
import type { WebsocketAdapterOptionsType } from "adapter-websockets";
|
|
4
|
+
|
|
5
|
+
import { createSocket } from "../../utils/socket.utils";
|
|
6
|
+
|
|
7
|
+
const decode: WebsocketAdapterOptionsType["decode"] = (event) => {
|
|
8
|
+
const message = JSON.parse(event.data);
|
|
9
|
+
if (message.type === "ping") {
|
|
10
|
+
return null;
|
|
11
|
+
}
|
|
12
|
+
return { topic: message.type, data: message.payload };
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
const encode: WebsocketAdapterOptionsType["encode"] = ({ topic, payload }) => JSON.stringify({ type: topic, payload });
|
|
16
|
+
|
|
17
|
+
describe("Websocket Adapter [ Encode / Decode ]", () => {
|
|
18
|
+
const { getServer, startServer, stopServer } = createWebsocketMockingServer();
|
|
19
|
+
|
|
20
|
+
beforeEach(() => {
|
|
21
|
+
vi.resetAllMocks();
|
|
22
|
+
startServer();
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
afterEach(() => {
|
|
26
|
+
stopServer();
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
describe("When using the default message format", () => {
|
|
30
|
+
it("should receive { topic, data } frames", async () => {
|
|
31
|
+
const socket = createSocket();
|
|
32
|
+
await waitForConnection(socket);
|
|
33
|
+
const spy = vi.fn();
|
|
34
|
+
socket
|
|
35
|
+
.createListener<{ name: string }>()({ topic: "user" })
|
|
36
|
+
.listen(({ data }) => spy(data));
|
|
37
|
+
|
|
38
|
+
getServer().send(JSON.stringify({ topic: "user", data: { name: "Alice" } }));
|
|
39
|
+
|
|
40
|
+
await waitFor(() => {
|
|
41
|
+
expect(spy).toHaveBeenCalledWith({ name: "Alice" });
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it("should send { topic, data } frames", async () => {
|
|
46
|
+
const socket = createSocket();
|
|
47
|
+
await waitForConnection(socket);
|
|
48
|
+
|
|
49
|
+
socket
|
|
50
|
+
.createEmitter<{ name: string }>()({ topic: "user" })
|
|
51
|
+
.emit({ payload: { name: "Alice" } });
|
|
52
|
+
|
|
53
|
+
await expect(getServer()).toReceiveMessage(JSON.stringify({ topic: "user", data: { name: "Alice" } }));
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
describe("When using custom decode", () => {
|
|
58
|
+
it("should map incoming frames to listener topics", async () => {
|
|
59
|
+
const socket = createSocket({ adapterOptions: { decode } });
|
|
60
|
+
await waitForConnection(socket);
|
|
61
|
+
const spy = vi.fn();
|
|
62
|
+
const other = vi.fn();
|
|
63
|
+
socket
|
|
64
|
+
.createListener<{ name: string }>()({ topic: "user" })
|
|
65
|
+
.listen(({ data }) => spy(data));
|
|
66
|
+
socket.createListener()({ topic: "other" }).listen(other);
|
|
67
|
+
|
|
68
|
+
getServer().send(JSON.stringify({ type: "user", payload: { name: "Alice" } }));
|
|
69
|
+
|
|
70
|
+
await waitFor(() => {
|
|
71
|
+
expect(spy).toHaveBeenCalledWith({ name: "Alice" });
|
|
72
|
+
});
|
|
73
|
+
expect(other).not.toHaveBeenCalled();
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it("should pass the original MessageEvent as extra", async () => {
|
|
77
|
+
const socket = createSocket({ adapterOptions: { decode } });
|
|
78
|
+
await waitForConnection(socket);
|
|
79
|
+
const spy = vi.fn();
|
|
80
|
+
socket
|
|
81
|
+
.createListener()({ topic: "user" })
|
|
82
|
+
.listen(({ extra }) => spy(extra));
|
|
83
|
+
const frame = JSON.stringify({ type: "user", payload: {} });
|
|
84
|
+
|
|
85
|
+
getServer().send(frame);
|
|
86
|
+
|
|
87
|
+
await waitFor(() => {
|
|
88
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
|
89
|
+
});
|
|
90
|
+
expect(spy.mock.calls[0][0].type).toBe("message");
|
|
91
|
+
expect(spy.mock.calls[0][0].data).toBe(frame);
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it("should ignore frames decoded to null", async () => {
|
|
95
|
+
const socket = createSocket({ adapterOptions: { decode } });
|
|
96
|
+
await waitForConnection(socket);
|
|
97
|
+
const spy = vi.fn();
|
|
98
|
+
const eventSpy = vi.fn();
|
|
99
|
+
socket.events.onListenerEvent(eventSpy);
|
|
100
|
+
socket.createListener()({ topic: "user" }).listen(spy);
|
|
101
|
+
|
|
102
|
+
getServer().send(JSON.stringify({ type: "ping" }));
|
|
103
|
+
getServer().send(JSON.stringify({ type: "user", payload: { name: "Alice" } }));
|
|
104
|
+
|
|
105
|
+
await waitFor(() => {
|
|
106
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
|
107
|
+
});
|
|
108
|
+
expect(eventSpy).toHaveBeenCalledTimes(1);
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it("should report decoder errors and keep the connection", async () => {
|
|
112
|
+
const error = new Error("Malformed frame");
|
|
113
|
+
const socket = createSocket({
|
|
114
|
+
adapterOptions: {
|
|
115
|
+
decode: (event) => {
|
|
116
|
+
if (event.data === "broken") {
|
|
117
|
+
throw error;
|
|
118
|
+
}
|
|
119
|
+
return decode(event);
|
|
120
|
+
},
|
|
121
|
+
},
|
|
122
|
+
});
|
|
123
|
+
await waitForConnection(socket);
|
|
124
|
+
const errorSpy = vi.fn();
|
|
125
|
+
const spy = vi.fn();
|
|
126
|
+
socket.onError(errorSpy);
|
|
127
|
+
socket.createListener()({ topic: "user" }).listen(spy);
|
|
128
|
+
|
|
129
|
+
getServer().send("broken");
|
|
130
|
+
getServer().send(JSON.stringify({ type: "user", payload: {} }));
|
|
131
|
+
|
|
132
|
+
await waitFor(() => {
|
|
133
|
+
expect(spy).toHaveBeenCalledTimes(1);
|
|
134
|
+
});
|
|
135
|
+
expect(errorSpy).toHaveBeenCalledTimes(1);
|
|
136
|
+
expect(errorSpy).toHaveBeenCalledWith({ error });
|
|
137
|
+
expect(socket.adapter.connected).toBe(true);
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
it("should use the decoder returned by onConnect", async () => {
|
|
141
|
+
// Registered synchronously - the initial connection attempt runs the interceptors asynchronously
|
|
142
|
+
const socket = createSocket().onConnect(({ connection }) => ({
|
|
143
|
+
...connection,
|
|
144
|
+
adapterOptions: { ...connection.adapterOptions, decode },
|
|
145
|
+
}));
|
|
146
|
+
await waitForConnection(socket);
|
|
147
|
+
const spy = vi.fn();
|
|
148
|
+
socket
|
|
149
|
+
.createListener()({ topic: "user" })
|
|
150
|
+
.listen(({ data }) => spy(data));
|
|
151
|
+
|
|
152
|
+
getServer().send(JSON.stringify({ type: "user", payload: 1 }));
|
|
153
|
+
|
|
154
|
+
await waitFor(() => {
|
|
155
|
+
expect(spy).toHaveBeenCalledWith(1);
|
|
156
|
+
});
|
|
157
|
+
});
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
describe("When using custom encode", () => {
|
|
161
|
+
it("should encode emitted messages", async () => {
|
|
162
|
+
const socket = createSocket({ adapterOptions: { encode } });
|
|
163
|
+
await waitForConnection(socket);
|
|
164
|
+
|
|
165
|
+
socket
|
|
166
|
+
.createEmitter<{ name: string }>()({ topic: "user" })
|
|
167
|
+
.emit({ payload: { name: "Alice" } });
|
|
168
|
+
|
|
169
|
+
await expect(getServer()).toReceiveMessage(JSON.stringify({ type: "user", payload: { name: "Alice" } }));
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
it("should encode heartbeat messages", async () => {
|
|
173
|
+
const socket = createSocket({
|
|
174
|
+
adapterOptions: {
|
|
175
|
+
encode,
|
|
176
|
+
heartbeat: true,
|
|
177
|
+
heartbeatMessage: "ping",
|
|
178
|
+
pingTimeout: 10,
|
|
179
|
+
pongTimeout: 1000,
|
|
180
|
+
},
|
|
181
|
+
});
|
|
182
|
+
await waitForConnection(socket);
|
|
183
|
+
|
|
184
|
+
await expect(getServer()).toReceiveMessage(JSON.stringify({ type: "heartbeat", payload: "ping" }));
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
it("should report encoder errors without sending the frame", async () => {
|
|
188
|
+
const error = new Error("Cannot encode");
|
|
189
|
+
const socket = createSocket({
|
|
190
|
+
adapterOptions: {
|
|
191
|
+
encode: () => {
|
|
192
|
+
throw error;
|
|
193
|
+
},
|
|
194
|
+
},
|
|
195
|
+
});
|
|
196
|
+
await waitForConnection(socket);
|
|
197
|
+
const errorSpy = vi.fn();
|
|
198
|
+
socket.onError(errorSpy);
|
|
199
|
+
const sendSpy = vi.fn();
|
|
200
|
+
getServer().on("message", sendSpy);
|
|
201
|
+
|
|
202
|
+
await socket.adapter.emit(socket.createEmitter()({ topic: "user" }));
|
|
203
|
+
|
|
204
|
+
expect(errorSpy).toHaveBeenCalledWith({ error });
|
|
205
|
+
expect(sendSpy).not.toHaveBeenCalled();
|
|
206
|
+
});
|
|
207
|
+
});
|
|
208
|
+
});
|
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { LoggerManager } from "@hyper-fetch/core";
|
|
2
|
+
import { createWebsocketMockingServer, waitForConnection } from "@hyper-fetch/testing";
|
|
3
|
+
import { waitFor } from "@testing-library/dom";
|
|
2
4
|
import { WebsocketAdapter } from "adapter-websockets/websocket-adapter";
|
|
3
5
|
import { Socket } from "socket";
|
|
4
6
|
|
|
@@ -22,6 +24,44 @@ describe("Socket [ Methods ]", () => {
|
|
|
22
24
|
expect(queryParams).toBe("testing");
|
|
23
25
|
});
|
|
24
26
|
|
|
27
|
+
it("should allow to set url without touching the live connection", async () => {
|
|
28
|
+
const socket = new Socket({ url: "ws://localhost:1234", adapterOptions: { autoConnect: false } });
|
|
29
|
+
const reconnectSpy = vi.spyOn(socket.adapter, "reconnect");
|
|
30
|
+
|
|
31
|
+
expect(socket.setUrl("ws://localhost:4321")).toBe(socket);
|
|
32
|
+
expect(socket.url).toBe("ws://localhost:4321");
|
|
33
|
+
expect(reconnectSpy).not.toHaveBeenCalled();
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it("should connect to the new url on reconnect and keep listeners", async () => {
|
|
37
|
+
// startServer() cleans every mocked server, so the second one is created only after the first started
|
|
38
|
+
const first = createWebsocketMockingServer("ws://localhost:1234");
|
|
39
|
+
first.startServer();
|
|
40
|
+
const second = createWebsocketMockingServer("ws://localhost:4321");
|
|
41
|
+
|
|
42
|
+
const socket = new Socket({ url: "ws://localhost:1234" });
|
|
43
|
+
const listener = socket.createListener<{ value: number }>()({ topic: "topic" });
|
|
44
|
+
const spy = vi.fn();
|
|
45
|
+
listener.listen(({ data }) => spy(data));
|
|
46
|
+
await waitForConnection(socket);
|
|
47
|
+
await first.getServer().connected;
|
|
48
|
+
|
|
49
|
+
socket.setUrl("ws://localhost:4321");
|
|
50
|
+
await socket.reconnect();
|
|
51
|
+
await second.getServer().connected;
|
|
52
|
+
await waitForConnection(socket);
|
|
53
|
+
|
|
54
|
+
second.emitListenerEvent(listener, { value: 2 });
|
|
55
|
+
|
|
56
|
+
await waitFor(() => {
|
|
57
|
+
expect(spy).toHaveBeenCalledWith({ value: 2 });
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
await socket.disconnect();
|
|
61
|
+
first.stopServer();
|
|
62
|
+
second.stopServer();
|
|
63
|
+
});
|
|
64
|
+
|
|
25
65
|
it("should allow to activate debug", async () => {
|
|
26
66
|
const socket = new Socket({ url: "ws://localhost:1234" });
|
|
27
67
|
socket.setDebug(true);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sse-adapter.d.ts","sourceRoot":"","sources":["../../src/adapter-sse/sse-adapter.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAc,eAAe,EAAE,MAAM,mBAAmB,CAAC;AACrE,OAAO,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAC;AAEzD,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAKhD,OAAO,KAAK,EAAE,qBAAqB,
|
|
1
|
+
{"version":3,"file":"sse-adapter.d.ts","sourceRoot":"","sources":["../../src/adapter-sse/sse-adapter.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAc,eAAe,EAAE,MAAM,mBAAmB,CAAC;AACrE,OAAO,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAC;AAEzD,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAKhD,OAAO,KAAK,EAAE,qBAAqB,EAAyB,MAAM,qBAAqB,CAAC;AAGxF;;;;GAIG;AAEH,MAAM,MAAM,2BAA2B,GAAG,aAAa,CACrD,YAAY,CAAC,GAAG,CAAC,EACjB,qBAAqB,EACrB,SAAS,EACT,SAAS,EACT,eAAe,GAAG,MAAM,EACxB,OAAO,oBAAoB,CAC5B,CAAC;AAEF,8FAA8F;AAC9F,eAAO,MAAM,uBAAuB,QAAO,2BAwLtC,CAAC"}
|
|
@@ -1,5 +1,16 @@
|
|
|
1
|
+
/** Listener event decoded from an incoming message */
|
|
2
|
+
export type SSEDecodedMessageType = {
|
|
3
|
+
topic: string;
|
|
4
|
+
data: unknown;
|
|
5
|
+
};
|
|
1
6
|
export type SSEAdapterOptionsType = {
|
|
2
7
|
eventSourceInit?: EventSourceInit;
|
|
3
8
|
autoConnect?: boolean;
|
|
9
|
+
/**
|
|
10
|
+
* Turns an incoming message into a listener event - use it when the server does not send `{ topic, data }`
|
|
11
|
+
* (e.g. `{ type, payload }`). Return `null` to ignore the message.
|
|
12
|
+
* Defaults to parsing the message as JSON and reading its `topic` and `data` fields.
|
|
13
|
+
*/
|
|
14
|
+
decode?: (event: MessageEvent) => SSEDecodedMessageType | null;
|
|
4
15
|
};
|
|
5
16
|
//# sourceMappingURL=sse-adapter.types.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sse-adapter.types.d.ts","sourceRoot":"","sources":["../../src/adapter-sse/sse-adapter.types.ts"],"names":[],"mappings":"AAEA,MAAM,MAAM,qBAAqB,GAAG;IAClC,eAAe,CAAC,EAAE,eAAe,CAAC;IAClC,WAAW,CAAC,EAAE,OAAO,CAAC;
|
|
1
|
+
{"version":3,"file":"sse-adapter.types.d.ts","sourceRoot":"","sources":["../../src/adapter-sse/sse-adapter.types.ts"],"names":[],"mappings":"AAEA,sDAAsD;AACtD,MAAM,MAAM,qBAAqB,GAAG;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,OAAO,CAAA;CAAE,CAAC;AAErE,MAAM,MAAM,qBAAqB,GAAG;IAClC,eAAe,CAAC,EAAE,eAAe,CAAC;IAClC,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB;;;;OAIG;IACH,MAAM,CAAC,EAAE,CAAC,KAAK,EAAE,YAAY,KAAK,qBAAqB,GAAG,IAAI,CAAC;CAChE,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"websocket-adapter.d.ts","sourceRoot":"","sources":["../../src/adapter-websockets/websocket-adapter.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAc,eAAe,EAAE,MAAM,mBAAmB,CAAC;AACrE,OAAO,EAAE,oBAAoB,EAAQ,MAAM,mBAAmB,CAAC;AAE/D,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAMhD,OAAO,KAAK,EAAE,2BAA2B,
|
|
1
|
+
{"version":3,"file":"websocket-adapter.d.ts","sourceRoot":"","sources":["../../src/adapter-websockets/websocket-adapter.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAc,eAAe,EAAE,MAAM,mBAAmB,CAAC;AACrE,OAAO,EAAE,oBAAoB,EAAQ,MAAM,mBAAmB,CAAC;AAE/D,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAMhD,OAAO,KAAK,EAAE,2BAA2B,EAA+B,MAAM,2BAA2B,CAAC;AAG1G;;;;GAIG;AAEH,MAAM,MAAM,oBAAoB,GAAG,aAAa,CAC9C,YAAY,CAAC,GAAG,CAAC,EACjB,2BAA2B,EAC3B,SAAS,EACT,SAAS,EACT,eAAe,GAAG,MAAM,EACxB,OAAO,oBAAoB,CAC5B,CAAC;AAEF,8EAA8E;AAC9E,eAAO,MAAM,gBAAgB,QAAO,oBAiR/B,CAAC"}
|
|
@@ -1,3 +1,15 @@
|
|
|
1
|
+
/** Listener event decoded from an incoming frame */
|
|
2
|
+
export type WebsocketDecodedMessageType = {
|
|
3
|
+
topic: string;
|
|
4
|
+
data: unknown;
|
|
5
|
+
};
|
|
6
|
+
/** Emitted message to be encoded into an outgoing frame */
|
|
7
|
+
export type WebsocketEncodeMessageType = {
|
|
8
|
+
topic: string;
|
|
9
|
+
payload: unknown;
|
|
10
|
+
};
|
|
11
|
+
/** Frame accepted by `WebSocket.send` */
|
|
12
|
+
export type WebsocketFrameType = Parameters<WebSocket["send"]>[0];
|
|
1
13
|
export type WebsocketAdapterOptionsType = {
|
|
2
14
|
/**
|
|
3
15
|
* Subprotocols passed to the `WebSocket` constructor (`Sec-WebSocket-Protocol` header).
|
|
@@ -9,5 +21,16 @@ export type WebsocketAdapterOptionsType = {
|
|
|
9
21
|
heartbeatMessage?: string;
|
|
10
22
|
heartbeat?: boolean;
|
|
11
23
|
autoConnect?: boolean;
|
|
24
|
+
/**
|
|
25
|
+
* Turns an incoming frame into a listener event - use it when the server does not send `{ topic, data }`
|
|
26
|
+
* (e.g. `{ type, payload }`). Return `null` to ignore the frame (pings, unknown messages).
|
|
27
|
+
* Defaults to parsing the frame as JSON and reading its `topic` and `data` fields.
|
|
28
|
+
*/
|
|
29
|
+
decode?: (event: MessageEvent) => WebsocketDecodedMessageType | null;
|
|
30
|
+
/**
|
|
31
|
+
* Turns an emitted message (and the heartbeat ping) into the frame passed to `websocket.send`.
|
|
32
|
+
* Defaults to `JSON.stringify({ topic, data: payload })`.
|
|
33
|
+
*/
|
|
34
|
+
encode?: (message: WebsocketEncodeMessageType) => WebsocketFrameType;
|
|
12
35
|
};
|
|
13
36
|
//# sourceMappingURL=websocket-adapter.types.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"websocket-adapter.types.d.ts","sourceRoot":"","sources":["../../src/adapter-websockets/websocket-adapter.types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,2BAA2B,GAAG;IACxC;;;OAGG;IACH,SAAS,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IAC9B,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,WAAW,CAAC,EAAE,OAAO,CAAC;
|
|
1
|
+
{"version":3,"file":"websocket-adapter.types.d.ts","sourceRoot":"","sources":["../../src/adapter-websockets/websocket-adapter.types.ts"],"names":[],"mappings":"AAAA,oDAAoD;AACpD,MAAM,MAAM,2BAA2B,GAAG;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,OAAO,CAAA;CAAE,CAAC;AAE3E,2DAA2D;AAC3D,MAAM,MAAM,0BAA0B,GAAG;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,OAAO,CAAA;CAAE,CAAC;AAE7E,yCAAyC;AACzC,MAAM,MAAM,kBAAkB,GAAG,UAAU,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AAElE,MAAM,MAAM,2BAA2B,GAAG;IACxC;;;OAGG;IACH,SAAS,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IAC9B,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB;;;;OAIG;IACH,MAAM,CAAC,EAAE,CAAC,KAAK,EAAE,YAAY,KAAK,2BAA2B,GAAG,IAAI,CAAC;IACrE;;;OAGG;IACH,MAAM,CAAC,EAAE,CAAC,OAAO,EAAE,0BAA0B,KAAK,kBAAkB,CAAC;CACtE,CAAC"}
|
package/dist/index.mjs
CHANGED
|
@@ -493,13 +493,13 @@ var WebsocketAdapter = () => new SocketAdapter({ name: "websockets" }).setQueryP
|
|
|
493
493
|
}, socket.reconnectTime);
|
|
494
494
|
});
|
|
495
495
|
newWebsocket.addEventListener("message", (newEvent) => {
|
|
496
|
-
const { topic, data, event } = parseMessageEvent(newEvent);
|
|
497
|
-
onEvent({
|
|
498
|
-
topic,
|
|
499
|
-
data,
|
|
500
|
-
extra: event
|
|
501
|
-
});
|
|
502
496
|
onHeartbeat();
|
|
497
|
+
const message = decodeMessage(newEvent);
|
|
498
|
+
if (message) onEvent({
|
|
499
|
+
topic: message.topic,
|
|
500
|
+
data: message.data,
|
|
501
|
+
extra: newEvent
|
|
502
|
+
});
|
|
503
503
|
});
|
|
504
504
|
return new Promise((resolve) => {
|
|
505
505
|
if (newWebsocket.readyState === WebSocket.OPEN) {
|
|
@@ -567,13 +567,36 @@ var WebsocketAdapter = () => new SocketAdapter({ name: "websockets" }).setQueryP
|
|
|
567
567
|
clearTimeout(pingTimer);
|
|
568
568
|
clearTimeout(pongTimer);
|
|
569
569
|
};
|
|
570
|
+
/** Maps an incoming frame to the listener event, reporting decoder failures instead of throwing */
|
|
571
|
+
const decodeMessage = (event) => {
|
|
572
|
+
try {
|
|
573
|
+
if (options?.decode) return options.decode(event);
|
|
574
|
+
const { topic, data } = parseMessageEvent(event);
|
|
575
|
+
return {
|
|
576
|
+
topic,
|
|
577
|
+
data
|
|
578
|
+
};
|
|
579
|
+
} catch (error) {
|
|
580
|
+
onError({ error });
|
|
581
|
+
return null;
|
|
582
|
+
}
|
|
583
|
+
};
|
|
570
584
|
const sendEventMessage = ({ topic, payload }) => {
|
|
571
585
|
if (!websocket) return false;
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
586
|
+
try {
|
|
587
|
+
const frame = options?.encode ? options.encode({
|
|
588
|
+
topic,
|
|
589
|
+
payload
|
|
590
|
+
}) : JSON.stringify({
|
|
591
|
+
topic,
|
|
592
|
+
data: payload
|
|
593
|
+
});
|
|
594
|
+
websocket.send(frame);
|
|
595
|
+
return true;
|
|
596
|
+
} catch (error) {
|
|
597
|
+
onError({ error });
|
|
598
|
+
return false;
|
|
599
|
+
}
|
|
577
600
|
};
|
|
578
601
|
const onHeartbeat = () => {
|
|
579
602
|
const { heartbeat = false, pingTimeout = Time.SEC * 5, pongTimeout = Time.SEC * 5, heartbeatMessage = "heartbeat" } = options || (
|
|
@@ -1272,6 +1295,15 @@ var Socket = class {
|
|
|
1272
1295
|
await this.adapter.reconnect();
|
|
1273
1296
|
};
|
|
1274
1297
|
/**
|
|
1298
|
+
* Set the url used by the next connection attempts - e.g. when switching tenants, regions or environments at runtime.
|
|
1299
|
+
* Call `reconnect()` to apply it to a live connection, or use `onConnect` to resolve it per attempt.
|
|
1300
|
+
* Listeners stay registered, so they keep receiving events after reconnecting.
|
|
1301
|
+
*/
|
|
1302
|
+
setUrl = (url) => {
|
|
1303
|
+
this.url = url;
|
|
1304
|
+
return this;
|
|
1305
|
+
};
|
|
1306
|
+
/**
|
|
1275
1307
|
* Set the default query params used by the next connection attempts.
|
|
1276
1308
|
* Call `reconnect()` to apply them to a live connection, or use `onConnect` to resolve them per attempt.
|
|
1277
1309
|
*/
|
|
@@ -1629,6 +1661,20 @@ var getServerSentEventsAdapter = (url, adapterOptions) => {
|
|
|
1629
1661
|
var ServerSentEventsAdapter = () => new SocketAdapter({ name: "sse" }).setQueryParamsMapper(stringifyQueryParams).setConnector(({ socket, adapter, getQueryParams, onConnect, onConnectFailed, onReconnect, onDisconnect, onListen, onConnected, onDisconnected, onError, onEvent }) => {
|
|
1630
1662
|
let sse;
|
|
1631
1663
|
const autoConnect = typeof socket.adapter.adapterOptions?.autoConnect === "boolean" ? socket.adapter.adapterOptions?.autoConnect : true;
|
|
1664
|
+
/** Maps an incoming message to the listener event, reporting decoder failures instead of throwing */
|
|
1665
|
+
const decodeMessage = (event, decode) => {
|
|
1666
|
+
try {
|
|
1667
|
+
if (decode) return decode(event);
|
|
1668
|
+
const { topic, data } = parseMessageEvent(event);
|
|
1669
|
+
return {
|
|
1670
|
+
topic,
|
|
1671
|
+
data
|
|
1672
|
+
};
|
|
1673
|
+
} catch (error) {
|
|
1674
|
+
onError({ error });
|
|
1675
|
+
return null;
|
|
1676
|
+
}
|
|
1677
|
+
};
|
|
1632
1678
|
const connect = async () => {
|
|
1633
1679
|
const connection = await onConnect();
|
|
1634
1680
|
if (!connection) return socket.adapter.connected;
|
|
@@ -1651,12 +1697,13 @@ var ServerSentEventsAdapter = () => new SocketAdapter({ name: "sse" }).setQueryP
|
|
|
1651
1697
|
const error = getSocketError(event);
|
|
1652
1698
|
onError({ error: new Error(error) });
|
|
1653
1699
|
});
|
|
1700
|
+
const { decode } = connection.adapterOptions || {};
|
|
1654
1701
|
eventSource.addEventListener("message", (newEvent) => {
|
|
1655
|
-
const
|
|
1656
|
-
onEvent({
|
|
1657
|
-
topic,
|
|
1658
|
-
data,
|
|
1659
|
-
extra:
|
|
1702
|
+
const message = decodeMessage(newEvent, decode);
|
|
1703
|
+
if (message) onEvent({
|
|
1704
|
+
topic: message.topic,
|
|
1705
|
+
data: message.data,
|
|
1706
|
+
extra: newEvent
|
|
1660
1707
|
});
|
|
1661
1708
|
});
|
|
1662
1709
|
return new Promise((resolve) => {
|