@hyper-fetch/orpc 9.9.0 → 10.0.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/README.md +35 -1
- package/__tests__/e2e/e2e.orpc-realtime-server.ts +181 -0
- package/__tests__/e2e/e2e.react-realtime.spec.tsx +181 -0
- package/__tests__/e2e/e2e.realtime.spec.ts +310 -0
- package/__tests__/features/socket/socket.schema.spec.ts +179 -0
- package/__tests__/features/stream-adapter/stream-adapter.environment.spec.ts +163 -0
- package/__tests__/features/stream-adapter/stream-adapter.lifecycle.spec.ts +509 -0
- package/__tests__/features/stream-adapter/stream-adapter.listener-lifecycle.spec.ts +291 -0
- package/__tests__/features/stream-adapter/stream-adapter.protocol.spec.ts +609 -0
- package/__tests__/features/stream-adapter/stream-adapter.subscriptions.spec.ts +270 -0
- package/__tests__/features/websocket-adapter/websocket-adapter.environment.spec.ts +147 -0
- package/__tests__/features/websocket-adapter/websocket-adapter.handshake-error.spec.ts +77 -0
- package/__tests__/features/websocket-adapter/websocket-adapter.lifecycle.spec.ts +326 -0
- package/__tests__/features/websocket-adapter/websocket-adapter.listener-lifecycle.spec.ts +222 -0
- package/__tests__/features/websocket-adapter/websocket-adapter.protocol.spec.ts +415 -0
- package/__tests__/features/websocket-adapter/websocket-adapter.subscriptions.spec.ts +172 -0
- package/__tests__/utils/controlled-fetch.ts +160 -0
- package/__tests__/utils/controlled-websocket.ts +140 -0
- package/__tests__/utils/socket-error.ts +7 -0
- package/__tests__/utils/stream-socket.ts +54 -0
- package/__tests__/utils/websocket-socket.ts +45 -0
- package/__tests__/vitest.setup.ts +20 -0
- package/dist/adapter/adapter.schema.d.ts +32 -0
- package/dist/adapter/adapter.schema.d.ts.map +1 -1
- package/dist/adapter/adapter.utils.d.ts +22 -4
- package/dist/adapter/adapter.utils.d.ts.map +1 -1
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.mjs +932 -11
- package/dist/index.mjs.map +1 -1
- package/dist/socket/index.d.ts +3 -0
- package/dist/socket/index.d.ts.map +1 -0
- package/dist/socket/socket.subscriptions.d.ts +31 -0
- package/dist/socket/socket.subscriptions.d.ts.map +1 -0
- package/dist/socket/socket.types.d.ts +62 -0
- package/dist/socket/socket.types.d.ts.map +1 -0
- package/dist/socket/socket.utils.d.ts +56 -0
- package/dist/socket/socket.utils.d.ts.map +1 -0
- package/dist/stream-adapter/index.d.ts +3 -0
- package/dist/stream-adapter/index.d.ts.map +1 -0
- package/dist/stream-adapter/stream-adapter.d.ts +21 -0
- package/dist/stream-adapter/stream-adapter.d.ts.map +1 -0
- package/dist/stream-adapter/stream-adapter.types.d.ts +22 -0
- package/dist/stream-adapter/stream-adapter.types.d.ts.map +1 -0
- package/dist/stream-adapter/stream-adapter.utils.d.ts +22 -0
- package/dist/stream-adapter/stream-adapter.utils.d.ts.map +1 -0
- package/dist/websocket-adapter/index.d.ts +3 -0
- package/dist/websocket-adapter/index.d.ts.map +1 -0
- package/dist/websocket-adapter/websocket-adapter.d.ts +21 -0
- package/dist/websocket-adapter/websocket-adapter.d.ts.map +1 -0
- package/dist/websocket-adapter/websocket-adapter.types.d.ts +22 -0
- package/dist/websocket-adapter/websocket-adapter.types.d.ts.map +1 -0
- package/dist/websocket-adapter/websocket-adapter.utils.d.ts +35 -0
- package/dist/websocket-adapter/websocket-adapter.utils.d.ts.map +1 -0
- package/package.json +3 -1
- package/src/adapter/adapter.schema.ts +57 -0
- package/src/adapter/adapter.utils.ts +40 -10
- package/src/index.ts +3 -0
- package/src/socket/index.ts +2 -0
- package/src/socket/socket.subscriptions.ts +178 -0
- package/src/socket/socket.types.ts +64 -0
- package/src/socket/socket.utils.ts +110 -0
- package/src/stream-adapter/index.ts +2 -0
- package/src/stream-adapter/stream-adapter.ts +381 -0
- package/src/stream-adapter/stream-adapter.types.ts +31 -0
- package/src/stream-adapter/stream-adapter.utils.ts +98 -0
- package/src/websocket-adapter/index.ts +2 -0
- package/src/websocket-adapter/websocket-adapter.ts +417 -0
- package/src/websocket-adapter/websocket-adapter.types.ts +31 -0
- package/src/websocket-adapter/websocket-adapter.utils.ts +119 -0
|
@@ -0,0 +1,415 @@
|
|
|
1
|
+
import { ErrorEvent, ORPCError } from "@orpc/client";
|
|
2
|
+
import type { StandardRPCCustomJsonSerializer } from "@orpc/client/standard";
|
|
3
|
+
|
|
4
|
+
import { flushPromises } from "../../utils/controlled-fetch";
|
|
5
|
+
import type { ControlledWebSocket } from "../../utils/controlled-websocket";
|
|
6
|
+
import { installControlledWebSocket } from "../../utils/controlled-websocket";
|
|
7
|
+
import { asSocketError } from "../../utils/socket-error";
|
|
8
|
+
import type { Planet } from "../../utils/stream-socket";
|
|
9
|
+
import { serialize } from "../../utils/stream-socket";
|
|
10
|
+
import { createWebsocketSocket, websocketUrl } from "../../utils/websocket-socket";
|
|
11
|
+
|
|
12
|
+
const defaultPayload = { id: 1 };
|
|
13
|
+
|
|
14
|
+
describe("Orpc Websocket Adapter [ Protocol ]", () => {
|
|
15
|
+
let Controlled = installControlledWebSocket();
|
|
16
|
+
let setup = createWebsocketSocket();
|
|
17
|
+
let ws: ControlledWebSocket;
|
|
18
|
+
|
|
19
|
+
const open = async () => {
|
|
20
|
+
await flushPromises();
|
|
21
|
+
ws = Controlled.last;
|
|
22
|
+
ws.serverOpen();
|
|
23
|
+
await flushPromises();
|
|
24
|
+
return ws;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
/** Subscribes and starts the iterator on the server, returns the request id */
|
|
28
|
+
const subscribe = (payload: { id: number } = defaultPayload) => {
|
|
29
|
+
const callback = vi.fn();
|
|
30
|
+
const unsubscribe = setup.onUpdate.setPayload(payload).listen(({ data, extra }) => callback(data, extra));
|
|
31
|
+
const request = ws.requests.at(-1);
|
|
32
|
+
if (!request) {
|
|
33
|
+
throw new Error("No request was sent");
|
|
34
|
+
}
|
|
35
|
+
return { callback, unsubscribe, id: request.i };
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
const subscribeRunning = (payload: { id: number } = defaultPayload) => {
|
|
39
|
+
const subscription = subscribe(payload);
|
|
40
|
+
ws.serverStartIterator(subscription.id);
|
|
41
|
+
return subscription;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
beforeEach(async () => {
|
|
45
|
+
vi.useFakeTimers();
|
|
46
|
+
Controlled = installControlledWebSocket();
|
|
47
|
+
setup = createWebsocketSocket();
|
|
48
|
+
await open();
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
afterEach(async () => {
|
|
52
|
+
await setup.socket.disconnect();
|
|
53
|
+
vi.useRealTimers();
|
|
54
|
+
vi.unstubAllGlobals();
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
describe("when connecting", () => {
|
|
58
|
+
it("should open one connection to the socket url", () => {
|
|
59
|
+
expect(Controlled.instances).toHaveLength(1);
|
|
60
|
+
expect(ws.url).toBe(websocketUrl);
|
|
61
|
+
expect(ws.binaryType).toBe("arraybuffer");
|
|
62
|
+
expect(setup.socket.adapter.connected).toBeTrue();
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it("should pass the query params and the protocols", async () => {
|
|
66
|
+
await setup.socket.disconnect();
|
|
67
|
+
setup = createWebsocketSocket({ adapterOptions: { protocols: ["orpc"] } });
|
|
68
|
+
setup.socket.setQueryParams({ token: "abc" });
|
|
69
|
+
await open();
|
|
70
|
+
|
|
71
|
+
expect(ws.url).toBe(`${websocketUrl}?token=abc`);
|
|
72
|
+
expect(ws.protocols).toStrictEqual(["orpc"]);
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
describe("when subscribing", () => {
|
|
77
|
+
it("should send the request of the procedure", () => {
|
|
78
|
+
subscribe({ id: 7 });
|
|
79
|
+
|
|
80
|
+
expect(ws.messages).toStrictEqual([{ i: "1", p: { u: "/planet/onUpdate", b: { json: { id: 7 } } } }]);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it("should send an empty input for procedures without one", () => {
|
|
84
|
+
setup.socket.createListener<number>()({ topic: "ticks" }).listen(vi.fn());
|
|
85
|
+
|
|
86
|
+
expect(ws.messages).toStrictEqual([{ i: "1", p: { u: "/ticks", b: {} } }]);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it("should give every request its own id", () => {
|
|
90
|
+
subscribe({ id: 1 });
|
|
91
|
+
subscribe({ id: 2 });
|
|
92
|
+
setup.socket.createListener<number>()({ topic: "chat/room messages/onMessage" }).listen(vi.fn());
|
|
93
|
+
|
|
94
|
+
expect(ws.requests.map(({ i, p }) => [i, p?.u])).toStrictEqual([
|
|
95
|
+
["1", "/planet/onUpdate"],
|
|
96
|
+
["2", "/planet/onUpdate"],
|
|
97
|
+
["3", "/chat/room%20messages/onMessage"],
|
|
98
|
+
]);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it("should send the headers of the adapter", async () => {
|
|
102
|
+
await setup.socket.disconnect();
|
|
103
|
+
setup = createWebsocketSocket({ adapterOptions: { headers: { authorization: "Bearer token" } } });
|
|
104
|
+
await open();
|
|
105
|
+
|
|
106
|
+
subscribe();
|
|
107
|
+
|
|
108
|
+
expect(ws.requests[0].p?.h).toStrictEqual({ authorization: "Bearer token" });
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it("should report an input with files and send nothing", () => {
|
|
112
|
+
const upload = setup.socket.createListener<number, { file: Blob }>()({ topic: "upload/progress" });
|
|
113
|
+
|
|
114
|
+
upload.listen(vi.fn(), { payload: { file: new Blob(["x"]) } });
|
|
115
|
+
|
|
116
|
+
expect(ws.sent).toStrictEqual([]);
|
|
117
|
+
expect(setup.errors.map(({ message }) => message)).toStrictEqual([
|
|
118
|
+
'Files in the input of "upload/progress" are not supported by the oRPC WebSocket adapter',
|
|
119
|
+
]);
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it("should report a frame that can't be sent", () => {
|
|
123
|
+
ws.readyState = ws.CLOSING;
|
|
124
|
+
setup.onUpdate.setPayload({ id: 1 }).listen(vi.fn());
|
|
125
|
+
ws.readyState = ws.OPEN;
|
|
126
|
+
|
|
127
|
+
expect(setup.errors.map(({ message }) => message)).toStrictEqual([
|
|
128
|
+
"[ControlledWebSocket] Cannot send - the connection is not open",
|
|
129
|
+
]);
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it("should use the custom serializers of the adapter", async () => {
|
|
133
|
+
class Money {
|
|
134
|
+
constructor(public cents: number) {}
|
|
135
|
+
}
|
|
136
|
+
const money: StandardRPCCustomJsonSerializer = {
|
|
137
|
+
type: 21,
|
|
138
|
+
condition: (data) => data instanceof Money,
|
|
139
|
+
serialize: (data: Money) => data.cents,
|
|
140
|
+
deserialize: (cents: number) => new Money(cents),
|
|
141
|
+
};
|
|
142
|
+
await setup.socket.disconnect();
|
|
143
|
+
setup = createWebsocketSocket({ customJsonSerializers: [money] });
|
|
144
|
+
await open();
|
|
145
|
+
const callback = vi.fn();
|
|
146
|
+
const listener = setup.socket.createListener<{ price: Money }, { min: Money }>()({ topic: "prices" });
|
|
147
|
+
|
|
148
|
+
listener.listen(({ data }) => callback(data), { payload: { min: new Money(5) } });
|
|
149
|
+
ws.serverStartIterator("1");
|
|
150
|
+
ws.serverEvent("1", { data: serialize({ price: new Money(150), at: new Date(0), total: 2n }, [money]) });
|
|
151
|
+
|
|
152
|
+
expect(ws.requests[0].p?.b).toStrictEqual({ json: { min: 5 }, meta: [[21, "min"]] });
|
|
153
|
+
expect(callback).toHaveBeenCalledExactlyOnceWith({ price: new Money(150), at: new Date(0), total: 2n });
|
|
154
|
+
});
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
describe("when the server sends events", () => {
|
|
158
|
+
it("should deliver the value with the event meta", () => {
|
|
159
|
+
const { id, callback } = subscribeRunning();
|
|
160
|
+
|
|
161
|
+
ws.serverEvent(id, { data: serialize({ id: 1, name: "Earth" }), meta: { id: "1", retry: 100 } });
|
|
162
|
+
ws.serverEvent(id, { data: serialize({ id: 1, name: "Mars" }) });
|
|
163
|
+
|
|
164
|
+
expect(callback.mock.calls).toStrictEqual([
|
|
165
|
+
[
|
|
166
|
+
{ id: 1, name: "Earth" },
|
|
167
|
+
{ id: "1", retry: 100 },
|
|
168
|
+
],
|
|
169
|
+
[
|
|
170
|
+
{ id: 1, name: "Mars" },
|
|
171
|
+
{ id: undefined, retry: undefined },
|
|
172
|
+
],
|
|
173
|
+
]);
|
|
174
|
+
expect(setup.errors).toStrictEqual([]);
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
it("should keep the special types of the output", () => {
|
|
178
|
+
const { id, callback } = subscribeRunning();
|
|
179
|
+
const value = { at: new Date(0), total: 10n, scores: new Map([["a", 1]]), tags: new Set(["x"]) };
|
|
180
|
+
|
|
181
|
+
ws.serverEvent(id, { data: serialize(value) });
|
|
182
|
+
|
|
183
|
+
expect(callback.mock.calls[0][0]).toStrictEqual(value);
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
it("should deliver events sent before the iterator response", () => {
|
|
187
|
+
const { id, callback } = subscribe();
|
|
188
|
+
|
|
189
|
+
ws.serverEvent(id, { data: serialize({ id: 1, name: "early" }) });
|
|
190
|
+
|
|
191
|
+
expect(callback).toHaveBeenCalledOnce();
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
it("should end the subscription on the done event without aborting it", () => {
|
|
195
|
+
const { id, callback } = subscribeRunning();
|
|
196
|
+
|
|
197
|
+
ws.serverEvent(id, { event: "done", data: serialize("returned") });
|
|
198
|
+
ws.serverEvent(id, { data: serialize({ id: 1, name: "late" }) });
|
|
199
|
+
|
|
200
|
+
expect(callback).not.toHaveBeenCalled();
|
|
201
|
+
expect(ws.aborts).toStrictEqual([]);
|
|
202
|
+
expect(setup.errors).toStrictEqual([]);
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
it("should report the defined ORPCError of an error event and end the subscription", () => {
|
|
206
|
+
const { id, callback } = subscribeRunning();
|
|
207
|
+
const error = { defined: true, code: "NOT_FOUND", status: 404, message: "Gone", data: { id: 1 } };
|
|
208
|
+
|
|
209
|
+
ws.serverEvent(id, { event: "error", data: serialize(error), meta: { id: "4" } });
|
|
210
|
+
ws.serverEvent(id, { data: serialize({ id: 1, name: "late" }) });
|
|
211
|
+
|
|
212
|
+
expect(callback).not.toHaveBeenCalled();
|
|
213
|
+
const reported = asSocketError(setup.errors[0]);
|
|
214
|
+
expect(reported.cause).toBeInstanceOf(ORPCError);
|
|
215
|
+
expect(reported).toMatchObject({ shape: error, topic: "planet/onUpdate", payload: { id: 1 }, code: "NOT_FOUND" });
|
|
216
|
+
expect(ws.aborts).toStrictEqual([]);
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
it("should report an error event without the oRPC shape as an ErrorEvent", () => {
|
|
220
|
+
const { id } = subscribeRunning();
|
|
221
|
+
|
|
222
|
+
ws.serverEvent(id, { event: "error" });
|
|
223
|
+
|
|
224
|
+
const { cause, shape } = asSocketError(setup.errors[0]);
|
|
225
|
+
expect(shape).toBeNull();
|
|
226
|
+
expect(cause).toBeInstanceOf(ErrorEvent);
|
|
227
|
+
expect((cause as ErrorEvent).data).toBeUndefined();
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
it("should report a failing listener and keep delivering", () => {
|
|
231
|
+
const { id, callback } = subscribeRunning();
|
|
232
|
+
setup.onUpdate.setPayload({ id: 1 }).listen(() => {
|
|
233
|
+
throw new Error("Listener failed");
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
ws.serverEvent(id, { data: serialize({ id: 1, name: "a" }) });
|
|
237
|
+
ws.serverEvent(id, { data: serialize({ id: 1, name: "b" }) });
|
|
238
|
+
|
|
239
|
+
expect(callback).toHaveBeenCalledTimes(2);
|
|
240
|
+
expect(setup.errors.map(({ message }) => message)).toStrictEqual(["Listener failed", "Listener failed"]);
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
it("should ignore messages of unknown requests and the server abort signal", () => {
|
|
244
|
+
const { id, callback } = subscribeRunning();
|
|
245
|
+
|
|
246
|
+
ws.serverEvent("99", { data: serialize({ id: 1, name: "other" }) });
|
|
247
|
+
ws.serverMessage({ i: id, t: 4 });
|
|
248
|
+
ws.serverEvent(id, { data: serialize({ id: 1, name: "own" }) });
|
|
249
|
+
|
|
250
|
+
expect(callback).toHaveBeenCalledExactlyOnceWith({ id: 1, name: "own" }, { id: undefined, retry: undefined });
|
|
251
|
+
expect(setup.errors).toStrictEqual([]);
|
|
252
|
+
});
|
|
253
|
+
|
|
254
|
+
it.each([
|
|
255
|
+
["an ArrayBuffer", (bytes: Uint8Array) => bytes.buffer],
|
|
256
|
+
["a typed array view", (bytes: Uint8Array) => new Uint8Array([7, ...bytes]).subarray(1)],
|
|
257
|
+
])("should read a binary frame sent as %s", (_, toFrame) => {
|
|
258
|
+
const { id, callback } = subscribeRunning();
|
|
259
|
+
const json = JSON.stringify({ i: id, t: 3, p: { e: "message", d: serialize({ id: 1, name: "binary" }) } });
|
|
260
|
+
const bytes = new Uint8Array([...new TextEncoder().encode(json), 255, 1, 2, 3]);
|
|
261
|
+
|
|
262
|
+
ws.serverSend(toFrame(bytes));
|
|
263
|
+
|
|
264
|
+
expect(callback).toHaveBeenCalledExactlyOnceWith({ id: 1, name: "binary" }, { id: undefined, retry: undefined });
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
it("should read a binary frame without the file part", () => {
|
|
268
|
+
const { id, callback } = subscribeRunning();
|
|
269
|
+
const json = JSON.stringify({ i: id, t: 3, p: { e: "message", d: serialize(1) } });
|
|
270
|
+
|
|
271
|
+
ws.serverSend(new TextEncoder().encode(json));
|
|
272
|
+
|
|
273
|
+
expect(callback).toHaveBeenCalledOnce();
|
|
274
|
+
});
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
describe("when a frame is malformed", () => {
|
|
278
|
+
it.each([
|
|
279
|
+
["a frame that is not JSON", "{oops"],
|
|
280
|
+
["a message without the id", JSON.stringify({ t: 3, p: { e: "message" } })],
|
|
281
|
+
["a message that is not an object", JSON.stringify(5)],
|
|
282
|
+
["a message without the payload", JSON.stringify({ i: "1", t: 3 })],
|
|
283
|
+
["an unknown event", JSON.stringify({ i: "1", t: 3, p: { e: "ping" } })],
|
|
284
|
+
["a response with an invalid status", JSON.stringify({ i: "1", p: { s: "200" } })],
|
|
285
|
+
["a response with invalid headers", JSON.stringify({ i: "1", p: { h: "text/event-stream" } })],
|
|
286
|
+
["data that is not the serialized object", JSON.stringify({ i: "1", t: 3, p: { e: "message", d: 5 } })],
|
|
287
|
+
["a frame that is neither text nor binary", { unexpected: true }],
|
|
288
|
+
])("should report %s and keep the subscription", (_, frame) => {
|
|
289
|
+
const { id, callback } = subscribeRunning();
|
|
290
|
+
|
|
291
|
+
ws.serverSend(frame);
|
|
292
|
+
ws.serverEvent(id, { data: serialize({ id: 1, name: "next" }) });
|
|
293
|
+
|
|
294
|
+
expect(setup.errors).toHaveLength(1);
|
|
295
|
+
expect(callback).toHaveBeenCalledExactlyOnceWith({ id: 1, name: "next" }, { id: undefined, retry: undefined });
|
|
296
|
+
});
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
describe("when the server rejects the call", () => {
|
|
300
|
+
it("should report the ORPCError of the response and release the request", () => {
|
|
301
|
+
const { id, callback } = subscribe();
|
|
302
|
+
const error = { defined: false, code: "UNAUTHORIZED", status: 401, message: "Missing token", data: undefined };
|
|
303
|
+
|
|
304
|
+
ws.serverMessage({ i: id, p: { s: 401, b: serialize(error) } });
|
|
305
|
+
ws.serverEvent(id, { data: serialize({ id: 1, name: "late" }) });
|
|
306
|
+
|
|
307
|
+
const reported = asSocketError(setup.errors[0]);
|
|
308
|
+
expect(reported.cause).toBeInstanceOf(ORPCError);
|
|
309
|
+
expect(reported.shape).toStrictEqual(error);
|
|
310
|
+
expect(reported.topic).toBe("planet/onUpdate");
|
|
311
|
+
expect(callback).not.toHaveBeenCalled();
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
it("should name the error after the status for an unknown procedure", () => {
|
|
315
|
+
const { id } = subscribe();
|
|
316
|
+
|
|
317
|
+
ws.serverMessage({ i: id, p: { s: 404, b: "No procedure matched" } });
|
|
318
|
+
|
|
319
|
+
const error = asSocketError(setup.errors[0]);
|
|
320
|
+
expect(error.code).toBe("NOT_FOUND");
|
|
321
|
+
expect(error.data).toBe("No procedure matched");
|
|
322
|
+
});
|
|
323
|
+
|
|
324
|
+
it("should report a procedure that is not an event iterator", () => {
|
|
325
|
+
const { id } = subscribe();
|
|
326
|
+
|
|
327
|
+
ws.serverMessage({ i: id, p: { b: serialize({ id: 1, name: "a" }) } });
|
|
328
|
+
|
|
329
|
+
expect(setup.errors.map(({ message }) => message)).toStrictEqual([
|
|
330
|
+
'The procedure "planet/onUpdate" did not return an event iterator',
|
|
331
|
+
]);
|
|
332
|
+
});
|
|
333
|
+
|
|
334
|
+
it.each([
|
|
335
|
+
["a file", { "content-type": "text/event-stream", "content-disposition": "inline" }],
|
|
336
|
+
["a list of content types", { "content-type": ["text/event-stream"], "content-disposition": [] }],
|
|
337
|
+
])("should tell an event stream from %s", (_, headers) => {
|
|
338
|
+
const { id } = subscribe();
|
|
339
|
+
|
|
340
|
+
ws.serverMessage({ i: id, p: { h: headers } });
|
|
341
|
+
|
|
342
|
+
const isIterator = !("content-disposition" in headers) || Array.isArray(headers["content-disposition"]);
|
|
343
|
+
expect(setup.errors).toHaveLength(isIterator ? 0 : 1);
|
|
344
|
+
});
|
|
345
|
+
});
|
|
346
|
+
|
|
347
|
+
describe("when reporting the errors of a subscription", () => {
|
|
348
|
+
it("should tell which subscription a rejected input belongs to", () => {
|
|
349
|
+
const upload = setup.socket.createListener<number, { file: Blob }>()({ topic: "upload/progress" });
|
|
350
|
+
const file = new Blob(["x"]);
|
|
351
|
+
|
|
352
|
+
upload.listen(vi.fn(), { payload: { file } });
|
|
353
|
+
|
|
354
|
+
expect(asSocketError(setup.errors[0])).toMatchObject({
|
|
355
|
+
topic: "upload/progress",
|
|
356
|
+
payload: { file },
|
|
357
|
+
shape: null,
|
|
358
|
+
});
|
|
359
|
+
});
|
|
360
|
+
|
|
361
|
+
it("should tell which subscription a frame that can't be sent belongs to", () => {
|
|
362
|
+
ws.readyState = ws.CLOSING;
|
|
363
|
+
setup.onUpdate.setPayload({ id: 5 }).listen(vi.fn());
|
|
364
|
+
ws.readyState = ws.OPEN;
|
|
365
|
+
|
|
366
|
+
expect(asSocketError(setup.errors[0])).toMatchObject({ topic: "planet/onUpdate", payload: { id: 5 } });
|
|
367
|
+
});
|
|
368
|
+
|
|
369
|
+
it("should tell which subscription a procedure that is not an event iterator belongs to", () => {
|
|
370
|
+
const { id } = subscribe({ id: 6 });
|
|
371
|
+
|
|
372
|
+
ws.serverMessage({ i: id, p: { b: serialize(1) } });
|
|
373
|
+
|
|
374
|
+
expect(asSocketError(setup.errors[0])).toMatchObject({ topic: "planet/onUpdate", payload: { id: 6 } });
|
|
375
|
+
});
|
|
376
|
+
|
|
377
|
+
it("should tell which subscription malformed data belongs to", () => {
|
|
378
|
+
const { id } = subscribeRunning({ id: 7 });
|
|
379
|
+
|
|
380
|
+
ws.serverEvent(id, { data: 5 });
|
|
381
|
+
|
|
382
|
+
expect(asSocketError(setup.errors[0])).toMatchObject({ topic: "planet/onUpdate", payload: { id: 7 } });
|
|
383
|
+
});
|
|
384
|
+
|
|
385
|
+
it("should report a frame that can't be read without a topic", () => {
|
|
386
|
+
ws.serverSend("{oops");
|
|
387
|
+
|
|
388
|
+
const error = asSocketError(setup.errors[0]);
|
|
389
|
+
expect(error).toMatchObject({ topic: null, payload: undefined, shape: null });
|
|
390
|
+
expect(error.cause).toBeInstanceOf(SyntaxError);
|
|
391
|
+
});
|
|
392
|
+
|
|
393
|
+
it("should report the error thrown by a listener as it is", () => {
|
|
394
|
+
const { id } = subscribeRunning();
|
|
395
|
+
const thrown = new Error("Listener failed");
|
|
396
|
+
setup.onUpdate.setPayload({ id: 1 }).listen(() => {
|
|
397
|
+
throw thrown;
|
|
398
|
+
});
|
|
399
|
+
|
|
400
|
+
ws.serverEvent(id, { data: serialize({ id: 1, name: "a" }) });
|
|
401
|
+
|
|
402
|
+
expect(setup.errors).toStrictEqual([thrown]);
|
|
403
|
+
});
|
|
404
|
+
});
|
|
405
|
+
|
|
406
|
+
describe("when emitting", () => {
|
|
407
|
+
it("should throw, the adapter only receives events", () => {
|
|
408
|
+
const emitter = setup.socket.createEmitter<Planet>()({ topic: "planet/update" });
|
|
409
|
+
|
|
410
|
+
expect(() => emitter.emit({ payload: { id: 1, name: "a" } })).toThrow(
|
|
411
|
+
"Cannot emit events in the oRPC WebSocket adapter, it only receives events of subscriptions",
|
|
412
|
+
);
|
|
413
|
+
});
|
|
414
|
+
});
|
|
415
|
+
});
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import { flushPromises } from "../../utils/controlled-fetch";
|
|
2
|
+
import type { ControlledWebSocket } from "../../utils/controlled-websocket";
|
|
3
|
+
import { installControlledWebSocket } from "../../utils/controlled-websocket";
|
|
4
|
+
import { serialize } from "../../utils/stream-socket";
|
|
5
|
+
import { createWebsocketSocket } from "../../utils/websocket-socket";
|
|
6
|
+
|
|
7
|
+
const defaultPayload = { id: 1 };
|
|
8
|
+
|
|
9
|
+
describe("Orpc Websocket Adapter [ Subscriptions ]", () => {
|
|
10
|
+
let Controlled = installControlledWebSocket();
|
|
11
|
+
let setup = createWebsocketSocket();
|
|
12
|
+
let ws: ControlledWebSocket;
|
|
13
|
+
|
|
14
|
+
const subscribe = (payload: { id: number } = defaultPayload) => {
|
|
15
|
+
const callback = vi.fn();
|
|
16
|
+
const unsubscribe = setup.onUpdate.setPayload(payload).listen(({ data }) => callback(data));
|
|
17
|
+
return { callback, unsubscribe };
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
beforeEach(async () => {
|
|
21
|
+
vi.useFakeTimers();
|
|
22
|
+
Controlled = installControlledWebSocket();
|
|
23
|
+
setup = createWebsocketSocket();
|
|
24
|
+
await flushPromises();
|
|
25
|
+
ws = Controlled.last;
|
|
26
|
+
ws.serverOpen();
|
|
27
|
+
await flushPromises();
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
afterEach(async () => {
|
|
31
|
+
await setup.socket.disconnect();
|
|
32
|
+
vi.useRealTimers();
|
|
33
|
+
vi.unstubAllGlobals();
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it("should send one request for listeners with the same payload", () => {
|
|
37
|
+
const first = subscribe();
|
|
38
|
+
const second = subscribe({ id: 1 });
|
|
39
|
+
ws.serverStartIterator("1");
|
|
40
|
+
|
|
41
|
+
ws.serverEvent("1", { data: serialize({ id: 1, name: "a" }) });
|
|
42
|
+
|
|
43
|
+
expect(ws.requests).toHaveLength(1);
|
|
44
|
+
expect(first.callback).toHaveBeenCalledExactlyOnceWith({ id: 1, name: "a" });
|
|
45
|
+
expect(second.callback).toHaveBeenCalledExactlyOnceWith({ id: 1, name: "a" });
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it("should keep the messages of different payloads apart", () => {
|
|
49
|
+
const one = subscribe({ id: 1 });
|
|
50
|
+
const two = subscribe({ id: 2 });
|
|
51
|
+
|
|
52
|
+
ws.serverEvent("2", { data: serialize({ id: 2, name: "two" }) });
|
|
53
|
+
ws.serverEvent("1", { data: serialize({ id: 1, name: "one" }) });
|
|
54
|
+
|
|
55
|
+
expect(one.callback).toHaveBeenCalledExactlyOnceWith({ id: 1, name: "one" });
|
|
56
|
+
expect(two.callback).toHaveBeenCalledExactlyOnceWith({ id: 2, name: "two" });
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it("should abort the request when the last listener unsubscribes", () => {
|
|
60
|
+
const first = subscribe();
|
|
61
|
+
const second = subscribe();
|
|
62
|
+
ws.serverStartIterator("1");
|
|
63
|
+
|
|
64
|
+
first.unsubscribe();
|
|
65
|
+
expect(ws.aborts).toStrictEqual([]);
|
|
66
|
+
|
|
67
|
+
second.unsubscribe();
|
|
68
|
+
ws.serverEvent("1", { data: serialize({ id: 1, name: "late" }) });
|
|
69
|
+
|
|
70
|
+
expect(ws.messages.at(-1)).toStrictEqual({ i: "1", t: 4 });
|
|
71
|
+
expect(second.callback).not.toHaveBeenCalled();
|
|
72
|
+
expect(setup.errors).toStrictEqual([]);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it("should abort only the request of the payload that lost its listeners", () => {
|
|
76
|
+
const one = subscribe({ id: 1 });
|
|
77
|
+
subscribe({ id: 2 });
|
|
78
|
+
|
|
79
|
+
one.unsubscribe();
|
|
80
|
+
|
|
81
|
+
expect(ws.aborts).toStrictEqual(["1"]);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it("should send a new request without the last event id after every listener left", () => {
|
|
85
|
+
const { unsubscribe } = subscribe();
|
|
86
|
+
ws.serverEvent("1", { data: serialize({ id: 1, name: "a" }), meta: { id: "3" } });
|
|
87
|
+
|
|
88
|
+
unsubscribe();
|
|
89
|
+
subscribe();
|
|
90
|
+
|
|
91
|
+
expect(ws.requests.map(({ i, p }) => [i, p?.h])).toStrictEqual([
|
|
92
|
+
["1", undefined],
|
|
93
|
+
["2", undefined],
|
|
94
|
+
]);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it("should start a new subscription without the last event id after the server ended one", () => {
|
|
98
|
+
const first = subscribe();
|
|
99
|
+
ws.serverEvent("1", { data: serialize({ id: 1, name: "a" }), meta: { id: "6" } });
|
|
100
|
+
ws.serverEvent("1", { event: "done" });
|
|
101
|
+
|
|
102
|
+
const second = subscribe();
|
|
103
|
+
ws.serverEvent("2", { data: serialize({ id: 1, name: "b" }) });
|
|
104
|
+
|
|
105
|
+
expect(ws.requests.map(({ i, p }) => [i, p?.h])).toStrictEqual([
|
|
106
|
+
["1", undefined],
|
|
107
|
+
["2", undefined],
|
|
108
|
+
]);
|
|
109
|
+
expect(ws.aborts).toStrictEqual([]);
|
|
110
|
+
expect(first.callback.mock.calls).toStrictEqual([[{ id: 1, name: "a" }]]);
|
|
111
|
+
expect(second.callback).toHaveBeenCalledExactlyOnceWith({ id: 1, name: "b" });
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it("should not deliver the rest of the messages after the last listener left in a callback", () => {
|
|
115
|
+
const callback = vi.fn();
|
|
116
|
+
const unsubscribe = setup.onUpdate.setPayload({ id: 1 }).listen(({ data }) => {
|
|
117
|
+
callback(data);
|
|
118
|
+
unsubscribe();
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
ws.serverEvent("1", { data: serialize({ id: 1, name: "a" }) });
|
|
122
|
+
ws.serverEvent("1", { data: serialize({ id: 1, name: "b" }) });
|
|
123
|
+
|
|
124
|
+
expect(callback).toHaveBeenCalledExactlyOnceWith({ id: 1, name: "a" });
|
|
125
|
+
expect(ws.aborts).toStrictEqual(["1"]);
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it("should keep two requests for one callback listening with two payloads", () => {
|
|
129
|
+
const callback = vi.fn();
|
|
130
|
+
const stopOne = setup.onUpdate.listen(callback, { payload: { id: 1 } });
|
|
131
|
+
setup.onUpdate.listen(callback, { payload: { id: 2 } });
|
|
132
|
+
|
|
133
|
+
stopOne();
|
|
134
|
+
ws.serverEvent("1", { data: serialize({ id: 1, name: "one" }) });
|
|
135
|
+
ws.serverEvent("2", { data: serialize({ id: 2, name: "two" }) });
|
|
136
|
+
|
|
137
|
+
expect(ws.requests.map(({ p }) => p?.b)).toStrictEqual([{ json: { id: 1 } }, { json: { id: 2 } }]);
|
|
138
|
+
expect(ws.aborts).toStrictEqual(["1"]);
|
|
139
|
+
expect(callback).toHaveBeenCalledExactlyOnceWith({
|
|
140
|
+
data: { id: 2, name: "two" },
|
|
141
|
+
extra: { id: undefined, retry: undefined },
|
|
142
|
+
});
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
it("should keep the request until every listen call of the same callback unsubscribed", () => {
|
|
146
|
+
const callback = vi.fn();
|
|
147
|
+
const first = setup.onUpdate.listen(callback, { payload: { id: 1 } });
|
|
148
|
+
const second = setup.onUpdate.listen(callback, { payload: { id: 1 } });
|
|
149
|
+
|
|
150
|
+
first();
|
|
151
|
+
ws.serverEvent("1", { data: serialize({ id: 1, name: "a" }) });
|
|
152
|
+
expect(ws.aborts).toStrictEqual([]);
|
|
153
|
+
|
|
154
|
+
second();
|
|
155
|
+
expect(ws.requests).toHaveLength(1);
|
|
156
|
+
expect(ws.aborts).toStrictEqual(["1"]);
|
|
157
|
+
expect(callback).toHaveBeenCalledOnce();
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
it("should report an abort that can't be sent", () => {
|
|
161
|
+
const { unsubscribe } = subscribe();
|
|
162
|
+
ws.readyState = ws.CLOSING;
|
|
163
|
+
|
|
164
|
+
unsubscribe();
|
|
165
|
+
ws.readyState = ws.OPEN;
|
|
166
|
+
|
|
167
|
+
expect(setup.errors.map(({ message }) => message)).toStrictEqual([
|
|
168
|
+
"[ControlledWebSocket] Cannot send - the connection is not open",
|
|
169
|
+
]);
|
|
170
|
+
expect(setup.errors[0]).toMatchObject({ name: "OrpcSocketError", topic: "planet/onUpdate", payload: { id: 1 } });
|
|
171
|
+
});
|
|
172
|
+
});
|