@hyper-fetch/orpc 9.8.1 → 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,609 @@
|
|
|
1
|
+
import { ErrorEvent, ORPCError } from "@orpc/client";
|
|
2
|
+
import type { StandardRPCCustomJsonSerializer } from "@orpc/client/standard";
|
|
3
|
+
|
|
4
|
+
import type { ControlledStreamCall } from "../../utils/controlled-fetch";
|
|
5
|
+
import { createControlledFetch, flushPromises } from "../../utils/controlled-fetch";
|
|
6
|
+
import { asSocketError } from "../../utils/socket-error";
|
|
7
|
+
import type { Planet } from "../../utils/stream-socket";
|
|
8
|
+
import { createStreamSocket, serialize, streamUrl } from "../../utils/stream-socket";
|
|
9
|
+
|
|
10
|
+
const defaultPayload = { id: 1 };
|
|
11
|
+
|
|
12
|
+
describe("Orpc Stream Adapter [ Protocol ]", () => {
|
|
13
|
+
let server = createControlledFetch();
|
|
14
|
+
let setup = createStreamSocket();
|
|
15
|
+
|
|
16
|
+
const subscribe = async (payload: { id: number } = defaultPayload) => {
|
|
17
|
+
const callback = vi.fn();
|
|
18
|
+
const unsubscribe = setup.onUpdate.setPayload(payload).listen(({ data, extra }) => callback(data, extra));
|
|
19
|
+
await flushPromises();
|
|
20
|
+
return { callback, unsubscribe, call: server.last() };
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const openStream = async (payload: { id: number } = defaultPayload) => {
|
|
24
|
+
const subscription = await subscribe(payload);
|
|
25
|
+
subscription.call.open();
|
|
26
|
+
await flushPromises();
|
|
27
|
+
return subscription;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const flushed = async <T>(value: T) => {
|
|
31
|
+
await flushPromises();
|
|
32
|
+
return value;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
beforeEach(() => {
|
|
36
|
+
vi.useFakeTimers();
|
|
37
|
+
server = createControlledFetch();
|
|
38
|
+
setup = createStreamSocket();
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
afterEach(async () => {
|
|
42
|
+
await setup.socket.disconnect();
|
|
43
|
+
vi.useRealTimers();
|
|
44
|
+
vi.restoreAllMocks();
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
describe("when starting a subscription", () => {
|
|
48
|
+
it("should call the procedure with the serialized input", async () => {
|
|
49
|
+
const { call } = await subscribe({ id: 7 });
|
|
50
|
+
|
|
51
|
+
expect(call.url).toBe(`${streamUrl}/planet/onUpdate`);
|
|
52
|
+
expect(call.init.method).toBe("POST");
|
|
53
|
+
expect(call.headers).toStrictEqual({ "Content-Type": "application/json" });
|
|
54
|
+
expect(call.body).toStrictEqual({ json: { id: 7 } });
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it("should send an empty input for procedures without one", async () => {
|
|
58
|
+
const ticks = setup.socket.createListener<number>()({ topic: "ticks" });
|
|
59
|
+
ticks.listen(vi.fn());
|
|
60
|
+
await flushPromises();
|
|
61
|
+
|
|
62
|
+
expect(server.last().url).toBe(`${streamUrl}/ticks`);
|
|
63
|
+
expect(server.last().body).toStrictEqual({});
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it("should encode the special types of the input", async () => {
|
|
67
|
+
const at = new Date("2024-01-01T00:00:00.000Z");
|
|
68
|
+
const listener = setup.socket.createListener<number, { at: Date; total: bigint }>()({ topic: "range" });
|
|
69
|
+
listener.listen(vi.fn(), { payload: { at, total: 2n } });
|
|
70
|
+
await flushPromises();
|
|
71
|
+
|
|
72
|
+
expect(server.last().body).toStrictEqual({
|
|
73
|
+
json: { at: "2024-01-01T00:00:00.000Z", total: "2" },
|
|
74
|
+
meta: [
|
|
75
|
+
[1, "at"],
|
|
76
|
+
[0, "total"],
|
|
77
|
+
],
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it("should encode every segment of a nested procedure path", async () => {
|
|
82
|
+
setup.socket.createListener<number>()({ topic: "chat/room messages/onMessage" }).listen(vi.fn());
|
|
83
|
+
await flushPromises();
|
|
84
|
+
|
|
85
|
+
expect(server.last().url).toBe(`${streamUrl}/chat/room%20messages/onMessage`);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it("should send the headers and the fetch options of the adapter", async () => {
|
|
89
|
+
await setup.socket.disconnect();
|
|
90
|
+
setup = createStreamSocket({
|
|
91
|
+
adapterOptions: { headers: { authorization: "Bearer token" }, fetchOptions: { credentials: "include" } },
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
const { call } = await subscribe();
|
|
95
|
+
|
|
96
|
+
expect(call.headers).toStrictEqual({ "Content-Type": "application/json", authorization: "Bearer token" });
|
|
97
|
+
expect(call.init.credentials).toBe("include");
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it("should append the query params of the socket without the reserved data param", async () => {
|
|
101
|
+
await setup.socket.disconnect();
|
|
102
|
+
setup = createStreamSocket({ url: `${streamUrl}/?tenant=acme` });
|
|
103
|
+
setup.socket.setQueryParams({ token: "1", data: "ignored" });
|
|
104
|
+
|
|
105
|
+
// The query params are read when connecting
|
|
106
|
+
await setup.socket.reconnect();
|
|
107
|
+
const { call } = await subscribe();
|
|
108
|
+
|
|
109
|
+
expect(call.url).toBe(`${streamUrl}/planet/onUpdate?tenant=acme&token=1`);
|
|
110
|
+
});
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
describe("when the server sends messages", () => {
|
|
114
|
+
it("should deliver the value with the event meta", async () => {
|
|
115
|
+
const { call, callback } = await openStream();
|
|
116
|
+
|
|
117
|
+
call.send({ data: serialize({ id: 1, name: "Earth" }), id: "1", retry: 100 });
|
|
118
|
+
await flushPromises();
|
|
119
|
+
|
|
120
|
+
expect(callback).toHaveBeenCalledExactlyOnceWith({ id: 1, name: "Earth" }, { id: "1", retry: 100 });
|
|
121
|
+
expect(setup.errors).toStrictEqual([]);
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it("should deliver the messages in order", async () => {
|
|
125
|
+
const { call, callback } = await openStream();
|
|
126
|
+
|
|
127
|
+
call.send({ data: serialize({ id: 1, name: "a" }), id: "1" });
|
|
128
|
+
call.send({ data: serialize({ id: 1, name: "b" }) });
|
|
129
|
+
call.send({ data: serialize({ id: 1, name: "c" }), id: "3" });
|
|
130
|
+
await flushPromises();
|
|
131
|
+
|
|
132
|
+
expect(callback.mock.calls).toStrictEqual([
|
|
133
|
+
[
|
|
134
|
+
{ id: 1, name: "a" },
|
|
135
|
+
{ id: "1", retry: undefined },
|
|
136
|
+
],
|
|
137
|
+
[
|
|
138
|
+
{ id: 1, name: "b" },
|
|
139
|
+
{ id: undefined, retry: undefined },
|
|
140
|
+
],
|
|
141
|
+
[
|
|
142
|
+
{ id: 1, name: "c" },
|
|
143
|
+
{ id: "3", retry: undefined },
|
|
144
|
+
],
|
|
145
|
+
]);
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
it("should keep the special types of the output", async () => {
|
|
149
|
+
const { call, callback } = await openStream();
|
|
150
|
+
const value = {
|
|
151
|
+
at: new Date(0),
|
|
152
|
+
total: 10n,
|
|
153
|
+
scores: new Map([["a", 1]]),
|
|
154
|
+
tags: new Set(["x"]),
|
|
155
|
+
sparse: [1, undefined],
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
call.send({ data: serialize(value) });
|
|
159
|
+
await flushPromises();
|
|
160
|
+
|
|
161
|
+
expect(callback.mock.calls[0][0]).toStrictEqual(value);
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
it("should use the custom serializers of the adapter", async () => {
|
|
165
|
+
class Money {
|
|
166
|
+
constructor(public cents: number) {}
|
|
167
|
+
}
|
|
168
|
+
const money: StandardRPCCustomJsonSerializer = {
|
|
169
|
+
type: 21,
|
|
170
|
+
condition: (data) => data instanceof Money,
|
|
171
|
+
serialize: (data: Money) => data.cents,
|
|
172
|
+
deserialize: (cents: number) => new Money(cents),
|
|
173
|
+
};
|
|
174
|
+
await setup.socket.disconnect();
|
|
175
|
+
setup = createStreamSocket({ customJsonSerializers: [money] });
|
|
176
|
+
const listener = setup.socket.createListener<{ price: Money }, { min: Money }>()({ topic: "prices" });
|
|
177
|
+
const callback = vi.fn();
|
|
178
|
+
listener.listen(({ data }) => callback(data), { payload: { min: new Money(5) } });
|
|
179
|
+
await flushPromises();
|
|
180
|
+
const call = server.last();
|
|
181
|
+
|
|
182
|
+
expect(call.body).toStrictEqual({ json: { min: 5 }, meta: [[21, "min"]] });
|
|
183
|
+
call.open();
|
|
184
|
+
call.send({ data: serialize({ price: new Money(150) }, [money]) });
|
|
185
|
+
await flushPromises();
|
|
186
|
+
|
|
187
|
+
expect(callback).toHaveBeenCalledExactlyOnceWith({ price: new Money(150) });
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
it("should read a message without the event field as a message", async () => {
|
|
191
|
+
const { call, callback } = await openStream();
|
|
192
|
+
|
|
193
|
+
call.write(`data: ${JSON.stringify(serialize({ id: 1, name: "a" }))}\n\n`);
|
|
194
|
+
await flushPromises();
|
|
195
|
+
|
|
196
|
+
expect(callback).toHaveBeenCalledExactlyOnceWith({ id: 1, name: "a" }, { id: undefined, retry: undefined });
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
it("should join data split over many lines", async () => {
|
|
200
|
+
const { call, callback } = await openStream();
|
|
201
|
+
|
|
202
|
+
call.write('event: message\ndata: {"json":\ndata:{"id":1,"name":"a"}}\n\n');
|
|
203
|
+
await flushPromises();
|
|
204
|
+
|
|
205
|
+
expect(callback).toHaveBeenCalledExactlyOnceWith({ id: 1, name: "a" }, { id: undefined, retry: undefined });
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
it("should ignore the keep-alive comments and unknown fields", async () => {
|
|
209
|
+
const { call, callback } = await openStream();
|
|
210
|
+
|
|
211
|
+
call.write(": \n\n");
|
|
212
|
+
call.write(": keep-alive\n\n");
|
|
213
|
+
call.write("foo: bar\n\n");
|
|
214
|
+
call.write(`event: message\nfoo: bar\nidle\ndata: ${JSON.stringify(serialize({ id: 1, name: "a" }))}\n\n`);
|
|
215
|
+
await flushPromises();
|
|
216
|
+
|
|
217
|
+
expect(callback).toHaveBeenCalledOnce();
|
|
218
|
+
expect(setup.errors).toStrictEqual([]);
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
it("should ignore an invalid retry", async () => {
|
|
222
|
+
const { call, callback } = await openStream();
|
|
223
|
+
|
|
224
|
+
call.write(`retry: -1\ndata: {}\n\n`);
|
|
225
|
+
call.write(`retry: 1.5\ndata: {}\n\n`);
|
|
226
|
+
call.write(`retry: abc\ndata: {}\n\n`);
|
|
227
|
+
await flushPromises();
|
|
228
|
+
|
|
229
|
+
expect(callback.mock.calls.map(([, extra]) => extra.retry)).toStrictEqual([undefined, undefined, undefined]);
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
it("should read the value of a field without the space after the colon", async () => {
|
|
233
|
+
const { call, callback } = await openStream();
|
|
234
|
+
|
|
235
|
+
call.write(`id:7\ndata:{}\n\n`);
|
|
236
|
+
await flushPromises();
|
|
237
|
+
|
|
238
|
+
expect(callback).toHaveBeenCalledExactlyOnceWith(undefined, { id: "7", retry: undefined });
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
it.each([
|
|
242
|
+
["\r\n", "CRLF"],
|
|
243
|
+
["\r", "CR"],
|
|
244
|
+
])("should read %j line endings", async (ending) => {
|
|
245
|
+
const { call, callback } = await openStream();
|
|
246
|
+
|
|
247
|
+
call.write(`event: message${ending}id: 1${ending}data: {"json":1}${ending}${ending}`);
|
|
248
|
+
call.write(`data: {"json":2}${ending}${ending}`);
|
|
249
|
+
await flushPromises();
|
|
250
|
+
|
|
251
|
+
expect(callback.mock.calls).toStrictEqual([
|
|
252
|
+
[1, { id: "1", retry: undefined }],
|
|
253
|
+
[2, { id: undefined, retry: undefined }],
|
|
254
|
+
]);
|
|
255
|
+
});
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
describe("when the events are split between chunks", () => {
|
|
259
|
+
const frame = `event: message\nid: 1\ndata: ${JSON.stringify(serialize({ id: 1, name: "Zażółć" }))}\n\n`;
|
|
260
|
+
|
|
261
|
+
it.each([1, 5, 15, frame.length - 2, frame.length - 1])(
|
|
262
|
+
"should read an event split after %s characters",
|
|
263
|
+
async (at) => {
|
|
264
|
+
const { call, callback } = await openStream();
|
|
265
|
+
|
|
266
|
+
call.write(frame.slice(0, at));
|
|
267
|
+
await flushPromises();
|
|
268
|
+
expect(callback).not.toHaveBeenCalled();
|
|
269
|
+
|
|
270
|
+
call.write(frame.slice(at));
|
|
271
|
+
await flushPromises();
|
|
272
|
+
expect(callback).toHaveBeenCalledExactlyOnceWith({ id: 1, name: "Zażółć" }, { id: "1", retry: undefined });
|
|
273
|
+
},
|
|
274
|
+
);
|
|
275
|
+
|
|
276
|
+
it("should read an event written one character at a time", async () => {
|
|
277
|
+
const { call, callback } = await openStream();
|
|
278
|
+
|
|
279
|
+
[...frame].forEach((character) => call.write(character));
|
|
280
|
+
// Every chunk takes a few promise ticks to read
|
|
281
|
+
await flushPromises(frame.length * 5);
|
|
282
|
+
|
|
283
|
+
expect(callback).toHaveBeenCalledOnce();
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
it("should read a multi-byte character split between chunks", async () => {
|
|
287
|
+
const { call, callback } = await openStream();
|
|
288
|
+
const bytes = new TextEncoder().encode(frame);
|
|
289
|
+
// The first byte of "ż", a two-byte character
|
|
290
|
+
const index = bytes.indexOf(197);
|
|
291
|
+
|
|
292
|
+
call.writeBytes(bytes.slice(0, index + 1));
|
|
293
|
+
call.writeBytes(bytes.slice(index + 1));
|
|
294
|
+
await flushPromises();
|
|
295
|
+
|
|
296
|
+
expect(callback).toHaveBeenCalledExactlyOnceWith({ id: 1, name: "Zażółć" }, { id: "1", retry: undefined });
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
it("should read a CRLF delimiter split between chunks", async () => {
|
|
300
|
+
const { call, callback } = await openStream();
|
|
301
|
+
|
|
302
|
+
call.write('data: {"json":1}\r\n\r');
|
|
303
|
+
await flushPromises();
|
|
304
|
+
call.write('\ndata: {"json":2}\r\n\r\n');
|
|
305
|
+
await flushPromises();
|
|
306
|
+
|
|
307
|
+
expect(callback.mock.calls.map(([data]) => data)).toStrictEqual([1, 2]);
|
|
308
|
+
});
|
|
309
|
+
|
|
310
|
+
it("should not read the line feed of a CRLF split after a complete message as a new line", async () => {
|
|
311
|
+
const { call, callback } = await openStream();
|
|
312
|
+
|
|
313
|
+
call.write('data: {"json":1}\r\n\r');
|
|
314
|
+
await flushPromises();
|
|
315
|
+
call.write("\n");
|
|
316
|
+
call.close();
|
|
317
|
+
await flushPromises();
|
|
318
|
+
|
|
319
|
+
expect(callback.mock.calls.map(([data]) => data)).toStrictEqual([1]);
|
|
320
|
+
expect(setup.errors).toStrictEqual([]);
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
it("should keep a line feed that starts a chunk after a complete message without CR", async () => {
|
|
324
|
+
const { call, callback } = await openStream();
|
|
325
|
+
|
|
326
|
+
call.write('data: {"json":1}\n\n');
|
|
327
|
+
await flushPromises();
|
|
328
|
+
call.write('\ndata: {"json":2}\n\n');
|
|
329
|
+
await flushPromises();
|
|
330
|
+
|
|
331
|
+
expect(callback.mock.calls.map(([data]) => data)).toStrictEqual([1, 2]);
|
|
332
|
+
});
|
|
333
|
+
|
|
334
|
+
it("should read many events in one chunk", async () => {
|
|
335
|
+
const { call, callback } = await openStream();
|
|
336
|
+
|
|
337
|
+
call.write(`${frame}${frame}${frame}`);
|
|
338
|
+
await flushPromises();
|
|
339
|
+
|
|
340
|
+
expect(callback).toHaveBeenCalledTimes(3);
|
|
341
|
+
});
|
|
342
|
+
});
|
|
343
|
+
|
|
344
|
+
describe("when the server ends the iterator", () => {
|
|
345
|
+
it.each(["done", "close"])("should end the stream on the %s event", async (event) => {
|
|
346
|
+
const { call, callback } = await openStream();
|
|
347
|
+
|
|
348
|
+
call.write(`event: ${event}\ndata: {"json":"returned"}\n\nevent: message\ndata: {"json":1}\n\n`);
|
|
349
|
+
await flushPromises();
|
|
350
|
+
vi.advanceTimersByTime(60_000);
|
|
351
|
+
await flushPromises();
|
|
352
|
+
|
|
353
|
+
// The returned value is not delivered and nothing after the end is read
|
|
354
|
+
expect(callback).not.toHaveBeenCalled();
|
|
355
|
+
expect(call.aborted).toBeTrue();
|
|
356
|
+
expect(server.calls).toHaveLength(1);
|
|
357
|
+
expect(setup.errors).toStrictEqual([]);
|
|
358
|
+
});
|
|
359
|
+
|
|
360
|
+
it("should end the stream when the server closes it", async () => {
|
|
361
|
+
const { call } = await openStream();
|
|
362
|
+
|
|
363
|
+
call.close();
|
|
364
|
+
await flushPromises();
|
|
365
|
+
vi.advanceTimersByTime(60_000);
|
|
366
|
+
await flushPromises();
|
|
367
|
+
|
|
368
|
+
expect(server.calls).toHaveLength(1);
|
|
369
|
+
expect(setup.errors).toStrictEqual([]);
|
|
370
|
+
});
|
|
371
|
+
|
|
372
|
+
it("should end an event stream without a body", async () => {
|
|
373
|
+
const { call } = await subscribe();
|
|
374
|
+
|
|
375
|
+
call.respond({ body: null, headers: { "content-type": "text/event-stream" } });
|
|
376
|
+
await flushPromises();
|
|
377
|
+
vi.advanceTimersByTime(60_000);
|
|
378
|
+
await flushPromises();
|
|
379
|
+
|
|
380
|
+
expect(server.calls).toHaveLength(1);
|
|
381
|
+
expect(setup.errors).toStrictEqual([]);
|
|
382
|
+
});
|
|
383
|
+
});
|
|
384
|
+
|
|
385
|
+
describe("when the server sends an error", () => {
|
|
386
|
+
it("should report the defined ORPCError and end the stream", async () => {
|
|
387
|
+
const { call, callback } = await openStream();
|
|
388
|
+
const error = { defined: true, code: "NOT_FOUND", status: 404, message: "Gone", data: { id: 1 } };
|
|
389
|
+
|
|
390
|
+
call.send({ event: "error", data: serialize(error), id: "9" });
|
|
391
|
+
await flushPromises();
|
|
392
|
+
vi.advanceTimersByTime(60_000);
|
|
393
|
+
await flushPromises();
|
|
394
|
+
|
|
395
|
+
expect(callback).not.toHaveBeenCalled();
|
|
396
|
+
expect(setup.errors).toHaveLength(1);
|
|
397
|
+
const reported = asSocketError(setup.errors[0]);
|
|
398
|
+
expect(reported.shape).toStrictEqual(error);
|
|
399
|
+
expect(reported.cause).toBeInstanceOf(ORPCError);
|
|
400
|
+
expect((reported.cause as ORPCError<string, unknown>).toJSON()).toStrictEqual(error);
|
|
401
|
+
expect(reported).toMatchObject({
|
|
402
|
+
message: "Gone",
|
|
403
|
+
name: "OrpcSocketError",
|
|
404
|
+
topic: "planet/onUpdate",
|
|
405
|
+
payload: { id: 1 },
|
|
406
|
+
defined: true,
|
|
407
|
+
code: "NOT_FOUND",
|
|
408
|
+
status: 404,
|
|
409
|
+
data: { id: 1 },
|
|
410
|
+
});
|
|
411
|
+
expect(server.calls).toHaveLength(1);
|
|
412
|
+
});
|
|
413
|
+
|
|
414
|
+
it("should report an error without the oRPC shape as an ErrorEvent", async () => {
|
|
415
|
+
const { call } = await openStream();
|
|
416
|
+
|
|
417
|
+
call.send({ event: "error", data: serialize({ reason: "custom" }) });
|
|
418
|
+
await flushPromises();
|
|
419
|
+
|
|
420
|
+
expect(setup.errors).toHaveLength(1);
|
|
421
|
+
const reported = asSocketError(setup.errors[0]);
|
|
422
|
+
expect(reported.shape).toBeNull();
|
|
423
|
+
expect(reported).toMatchObject({ defined: false, code: undefined, status: undefined, data: undefined });
|
|
424
|
+
expect(reported.cause).toBeInstanceOf(ErrorEvent);
|
|
425
|
+
expect((reported.cause as ErrorEvent).data).toStrictEqual({ reason: "custom" });
|
|
426
|
+
});
|
|
427
|
+
|
|
428
|
+
it("should report an error event without data", async () => {
|
|
429
|
+
const { call } = await openStream();
|
|
430
|
+
|
|
431
|
+
call.write("event: error\n\n");
|
|
432
|
+
await flushPromises();
|
|
433
|
+
|
|
434
|
+
const { cause } = asSocketError(setup.errors[0]);
|
|
435
|
+
expect(cause).toBeInstanceOf(ErrorEvent);
|
|
436
|
+
expect((cause as ErrorEvent).data).toBeUndefined();
|
|
437
|
+
});
|
|
438
|
+
});
|
|
439
|
+
|
|
440
|
+
describe("when a message is malformed", () => {
|
|
441
|
+
it.each([
|
|
442
|
+
["data that is not JSON", "data: {not json\n\n"],
|
|
443
|
+
["data that is not the serialized object", "data: 5\n\n"],
|
|
444
|
+
["data with invalid meta", 'data: {"json":"x","meta":[[0]]}\n\n'],
|
|
445
|
+
["an error event that is not JSON", "event: error\ndata: {not json\n\n"],
|
|
446
|
+
])("should report %s and keep reading", async (_, text) => {
|
|
447
|
+
const { call, callback } = await openStream();
|
|
448
|
+
|
|
449
|
+
call.write(text);
|
|
450
|
+
call.send({ data: serialize({ id: 1, name: "next" }) });
|
|
451
|
+
await flushPromises();
|
|
452
|
+
|
|
453
|
+
expect(setup.errors).toHaveLength(1);
|
|
454
|
+
expect(callback).toHaveBeenCalledExactlyOnceWith({ id: 1, name: "next" }, { id: undefined, retry: undefined });
|
|
455
|
+
});
|
|
456
|
+
|
|
457
|
+
it("should end the stream after an error event with invalid data", async () => {
|
|
458
|
+
const { call, callback } = await openStream();
|
|
459
|
+
|
|
460
|
+
call.write('event: error\ndata: {"json":"x","meta":[[0]]}\n\n');
|
|
461
|
+
call.send({ data: serialize({ id: 1, name: "next" }) });
|
|
462
|
+
await flushPromises();
|
|
463
|
+
|
|
464
|
+
expect(setup.errors).toHaveLength(1);
|
|
465
|
+
expect(callback).not.toHaveBeenCalled();
|
|
466
|
+
});
|
|
467
|
+
|
|
468
|
+
it("should report a failing listener and deliver to the other listeners", async () => {
|
|
469
|
+
const { call, callback } = await openStream();
|
|
470
|
+
setup.onUpdate.setPayload({ id: 1 }).listen(() => {
|
|
471
|
+
throw new Error("Listener failed");
|
|
472
|
+
});
|
|
473
|
+
|
|
474
|
+
call.send({ data: serialize({ id: 1, name: "a" }) });
|
|
475
|
+
call.send({ data: serialize({ id: 1, name: "b" }) });
|
|
476
|
+
await flushPromises();
|
|
477
|
+
|
|
478
|
+
expect(callback).toHaveBeenCalledTimes(2);
|
|
479
|
+
expect(setup.errors.map(({ message }) => message)).toStrictEqual(["Listener failed", "Listener failed"]);
|
|
480
|
+
});
|
|
481
|
+
});
|
|
482
|
+
|
|
483
|
+
describe("when the server rejects the call", () => {
|
|
484
|
+
const respond = async (call: ControlledStreamCall, response: Parameters<ControlledStreamCall["respond"]>[0]) => {
|
|
485
|
+
call.respond(response);
|
|
486
|
+
await flushPromises();
|
|
487
|
+
vi.advanceTimersByTime(60_000);
|
|
488
|
+
return flushed(call);
|
|
489
|
+
};
|
|
490
|
+
|
|
491
|
+
it("should report the ORPCError of the response and not start again", async () => {
|
|
492
|
+
const { call } = await subscribe();
|
|
493
|
+
const error = { defined: false, code: "UNAUTHORIZED", status: 401, message: "Missing token", data: undefined };
|
|
494
|
+
|
|
495
|
+
await respond(call, { status: 401, body: JSON.stringify(serialize(error)) });
|
|
496
|
+
|
|
497
|
+
expect(setup.errors).toHaveLength(1);
|
|
498
|
+
const reported = asSocketError(setup.errors[0]);
|
|
499
|
+
expect(reported.shape).toStrictEqual(error);
|
|
500
|
+
expect(reported.cause).toBeInstanceOf(ORPCError);
|
|
501
|
+
expect(reported.topic).toBe("planet/onUpdate");
|
|
502
|
+
expect(server.calls).toHaveLength(1);
|
|
503
|
+
});
|
|
504
|
+
|
|
505
|
+
it("should name the error after the status when the body is not an oRPC error", async () => {
|
|
506
|
+
const { call } = await subscribe();
|
|
507
|
+
|
|
508
|
+
await respond(call, { status: 404, body: "No procedure matched", headers: { "content-type": "text/plain" } });
|
|
509
|
+
|
|
510
|
+
const error = asSocketError(setup.errors[0]);
|
|
511
|
+
expect(error.cause).toBeInstanceOf(ORPCError);
|
|
512
|
+
expect(error.code).toBe("NOT_FOUND");
|
|
513
|
+
expect(error.status).toBe(404);
|
|
514
|
+
expect(error.data).toBe("No procedure matched");
|
|
515
|
+
});
|
|
516
|
+
|
|
517
|
+
it("should keep the raw body of an error with invalid meta", async () => {
|
|
518
|
+
const { call } = await subscribe();
|
|
519
|
+
const body = { json: "x", meta: [[0]] };
|
|
520
|
+
|
|
521
|
+
await respond(call, { status: 503, body: JSON.stringify(body) });
|
|
522
|
+
|
|
523
|
+
const error = asSocketError(setup.errors[0]);
|
|
524
|
+
expect(error.code).toBe("SERVICE_UNAVAILABLE");
|
|
525
|
+
expect(error.data).toStrictEqual(body);
|
|
526
|
+
});
|
|
527
|
+
|
|
528
|
+
it("should report a procedure that is not an event iterator", async () => {
|
|
529
|
+
const { call } = await subscribe();
|
|
530
|
+
|
|
531
|
+
await respond(call, { body: JSON.stringify(serialize({ id: 1, name: "a" })) });
|
|
532
|
+
|
|
533
|
+
expect(setup.errors.map(({ message }) => message)).toStrictEqual([
|
|
534
|
+
'The procedure "planet/onUpdate" did not return an event iterator',
|
|
535
|
+
]);
|
|
536
|
+
expect(call.aborted).toBeTrue();
|
|
537
|
+
expect(server.calls).toHaveLength(1);
|
|
538
|
+
});
|
|
539
|
+
|
|
540
|
+
it("should report a response without the content type", async () => {
|
|
541
|
+
const { call } = await subscribe();
|
|
542
|
+
|
|
543
|
+
await respond(call, { body: null, headers: {} });
|
|
544
|
+
|
|
545
|
+
expect(setup.errors).toHaveLength(1);
|
|
546
|
+
expect(server.calls).toHaveLength(1);
|
|
547
|
+
});
|
|
548
|
+
});
|
|
549
|
+
|
|
550
|
+
describe("when reporting the errors of a subscription", () => {
|
|
551
|
+
it.each([
|
|
552
|
+
["a call that is not an event iterator", (call: ControlledStreamCall) => call.respond({ body: "{}" })],
|
|
553
|
+
["a transport failure", (call: ControlledStreamCall) => call.fail(new TypeError("fetch failed"))],
|
|
554
|
+
[
|
|
555
|
+
"malformed data",
|
|
556
|
+
(call: ControlledStreamCall) => {
|
|
557
|
+
call.open();
|
|
558
|
+
call.write("data: {oops\n\n");
|
|
559
|
+
},
|
|
560
|
+
],
|
|
561
|
+
])("should tell which subscription failed with %s", async (_, act) => {
|
|
562
|
+
const { call } = await subscribe({ id: 3 });
|
|
563
|
+
|
|
564
|
+
act(call);
|
|
565
|
+
await flushPromises();
|
|
566
|
+
|
|
567
|
+
const error = asSocketError(setup.errors[0]);
|
|
568
|
+
expect(error).toMatchObject({ topic: "planet/onUpdate", payload: { id: 3 }, shape: null, defined: false });
|
|
569
|
+
expect(error.cause).toBeInstanceOf(Error);
|
|
570
|
+
expect(error.message).toBe((error.cause as Error).message);
|
|
571
|
+
});
|
|
572
|
+
|
|
573
|
+
it("should report the stream that stopped restarting", async () => {
|
|
574
|
+
await setup.socket.disconnect();
|
|
575
|
+
setup = createStreamSocket({ reconnect: 0 });
|
|
576
|
+
const { call } = await subscribe({ id: 4 });
|
|
577
|
+
|
|
578
|
+
call.fail();
|
|
579
|
+
await flushPromises();
|
|
580
|
+
|
|
581
|
+
const stopped = asSocketError(setup.errors[1]);
|
|
582
|
+
expect(stopped.message).toBe('Stopped restarting the stream of "planet/onUpdate" after 0 attempts');
|
|
583
|
+
expect(stopped.payload).toStrictEqual({ id: 4 });
|
|
584
|
+
});
|
|
585
|
+
|
|
586
|
+
it("should report the error thrown by a listener as it is", async () => {
|
|
587
|
+
const { call } = await openStream();
|
|
588
|
+
const thrown = new Error("Listener failed");
|
|
589
|
+
setup.onUpdate.setPayload({ id: 1 }).listen(() => {
|
|
590
|
+
throw thrown;
|
|
591
|
+
});
|
|
592
|
+
|
|
593
|
+
call.send({ data: serialize({ id: 1, name: "a" }) });
|
|
594
|
+
await flushPromises();
|
|
595
|
+
|
|
596
|
+
expect(setup.errors).toStrictEqual([thrown]);
|
|
597
|
+
});
|
|
598
|
+
});
|
|
599
|
+
|
|
600
|
+
describe("when emitting", () => {
|
|
601
|
+
it("should throw, the adapter only receives events", () => {
|
|
602
|
+
const emitter = setup.socket.createEmitter<Planet>()({ topic: "planet/update" });
|
|
603
|
+
|
|
604
|
+
expect(() => emitter.emit({ payload: { id: 1, name: "a" } })).toThrow(
|
|
605
|
+
"Cannot emit events in the oRPC stream adapter, it only receives events of subscriptions",
|
|
606
|
+
);
|
|
607
|
+
});
|
|
608
|
+
});
|
|
609
|
+
});
|