Skip to content

Commit 45fb7df

Browse files
committed
fix: default timeout of RPC requests
1 parent b5d7cbd commit 45fb7df

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212

1313
# Change Log
1414

15+
## 0.3.1
16+
17+
### 🐞 Fix
18+
19+
- Fix default timeout of RPC requests.
20+
1521
## 0.3.0
1622

1723
### ✨ New

src/rpc/__tests__/client.test.ts

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,14 +157,55 @@ describe("RpcClient", () => {
157157
}
158158
});
159159

160+
/**
161+
* Asserts the timeout monitor is correctly configured.
162+
*/
163+
describe("timeout", () => {
164+
it("has a default", async () => {
165+
// Arrange.
166+
vi.spyOn(globalThis, "setTimeout");
167+
168+
// Act.
169+
await client.request("test");
170+
await client.request({
171+
method: "test",
172+
params: {
173+
name: "Elgato",
174+
},
175+
});
176+
177+
// Assert.
178+
expect(setTimeout).toHaveBeenCalledTimes(2);
179+
expect(setTimeout).toHaveBeenNthCalledWith<Parameters<typeof setTimeout>>(1, expect.any(Function), 30000);
180+
expect(setTimeout).toHaveBeenNthCalledWith<Parameters<typeof setTimeout>>(2, expect.any(Function), 30000);
181+
});
182+
183+
it("uses specified timeout", async () => {
184+
// Arrange.
185+
vi.spyOn(globalThis, "setTimeout");
186+
187+
// Act.
188+
await client.request({
189+
method: "test",
190+
params: {
191+
name: "Elgato",
192+
},
193+
timeout: 1,
194+
});
195+
196+
// Assert.
197+
expect(setTimeout).toHaveBeenCalledExactlyOnceWith<Parameters<typeof setTimeout>>(expect.any(Function), 1);
198+
});
199+
});
200+
160201
/**
161202
* Asserts errors are appropriately mapped from a response.
162203
*/
163204
it("maps errors", async () => {
164205
// Arrange, act.
165206
const res = await client.request("err");
166207

167-
// Asserts.
208+
// Assert.
168209
expect(res.ok).toBe(false);
169210
if (!res.ok) {
170211
expect(res.error.code).toBe(1);

src/rpc/client.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ import type { RpcSender } from "./sender.js";
1010
* Client capable of sending requests to, and receiving responses from, a server.
1111
*/
1212
export class RpcClient {
13+
/**
14+
* The timeout, in milliseconds, used when the request does not have a timeout specified; default 30 seconds.
15+
*/
16+
static readonly #DEFAULT_TIMEOUT = 30000;
17+
1318
/**
1419
* The client's options.
1520
*/
@@ -163,7 +168,7 @@ export class RpcClient {
163168
request: RpcRequestOptions,
164169
): Promise<RpcResponse<TResult>> {
165170
const id = crypto.randomUUID();
166-
const { method, params, timeout } = request;
171+
const { method, params, timeout = RpcClient.#DEFAULT_TIMEOUT } = request;
167172

168173
// Initialize the response handler.
169174
const response = new Promise<RpcResponse<TResult>>((resolve) => {

0 commit comments

Comments
 (0)