Skip to content

Commit dd729f5

Browse files
SYLVIANNORUKAmatthewkeilnazarhussainnflaig
authored
chore: update any-signal to latest version (#7628)
- Update any-signal to latest version [v4.1.1](https://github.com/jacobheun/any-signal/releases/tag/v4.1.1) - Immediately reject if signal is already aborted without executing `asyncFn` --------- Co-authored-by: Matthew Keil <github@mail.matthewkeil.com> Co-authored-by: Nazar Hussain <nazarhussain@gmail.com> Co-authored-by: matthewkeil <me@matthewkeil.com> Co-authored-by: Nico Flaig <nflaig@protonmail.com>
1 parent c91cd9c commit dd729f5

File tree

4 files changed

+18
-9
lines changed

4 files changed

+18
-9
lines changed

packages/utils/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"types": "lib/index.d.ts",
3838
"dependencies": {
3939
"@chainsafe/as-sha256": "^1.1.0",
40-
"any-signal": "3.0.1",
40+
"any-signal": "^4.1.1",
4141
"bigint-buffer": "^1.1.5",
4242
"case": "^1.6.3",
4343
"js-yaml": "^4.1.0"

packages/utils/src/timeout.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
import {anySignal} from "any-signal";
2-
import {TimeoutError} from "./errors.js";
2+
import {ErrorAborted, TimeoutError} from "./errors.js";
33
import {sleep} from "./sleep.js";
44

55
export async function withTimeout<T>(
66
asyncFn: (timeoutAndParentSignal?: AbortSignal) => Promise<T>,
77
timeoutMs: number,
88
signal?: AbortSignal
99
): Promise<T> {
10+
if (signal?.aborted) {
11+
throw new ErrorAborted();
12+
}
13+
1014
const timeoutAbortController = new AbortController();
1115
const timeoutAndParentSignal = anySignal([timeoutAbortController.signal, ...(signal ? [signal] : [])]);
1216

packages/utils/test/unit/timeout.test.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {afterEach, describe, expect, it} from "vitest";
1+
import {afterEach, describe, expect, it, vi} from "vitest";
22
import {ErrorAborted, TimeoutError} from "../../src/errors.js";
33
import {withTimeout} from "../../src/timeout.js";
44

@@ -71,4 +71,15 @@ describe("withTimeout", () => {
7171
ErrorAborted
7272
);
7373
});
74+
75+
it("Should immediately reject if signal is already aborted without executing asyncFn", async () => {
76+
const controller = new AbortController();
77+
controller.abort();
78+
79+
const asyncFnSpy = vi.fn(() => pause(shortTimeoutMs, data));
80+
81+
await expect(withTimeout(asyncFnSpy, longTimeoutMs, controller.signal)).rejects.toThrow(ErrorAborted);
82+
83+
expect(asyncFnSpy).not.toHaveBeenCalled();
84+
});
7485
});

yarn.lock

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4113,11 +4113,6 @@ ansi-styles@^6.1.0:
41134113
resolved "https://registry.yarnpkg.com/ansi-styles/-/ansi-styles-6.2.1.tgz#0e62320cf99c21afff3b3012192546aacbfb05c5"
41144114
integrity sha512-bN798gFfQX+viw3R7yrGWRqnrN2oRkEkUjjl4JNn4E8GxxbjtG3FbrEIIY3l8/hrwUwIeCZvi4QuOTP4MErVug==
41154115

4116-
any-signal@3.0.1:
4117-
version "3.0.1"
4118-
resolved "https://registry.yarnpkg.com/any-signal/-/any-signal-3.0.1.tgz#49cae34368187a3472e31de28fb5cb1430caa9a6"
4119-
integrity sha512-xgZgJtKEa9YmDqXodIgl7Fl1C8yNXr8w6gXjqK3LW4GcEiYT+6AQfJSE/8SPsEpLLmcvbv8YU+qet94UewHxqg==
4120-
41214116
any-signal@^4.1.1:
41224117
version "4.1.1"
41234118
resolved "https://registry.yarnpkg.com/any-signal/-/any-signal-4.1.1.tgz#928416c355c66899e6b2a91cad4488f0324bae03"
@@ -13664,4 +13659,3 @@ zod@^3.21.4:
1366413659
version "3.23.0"
1366513660
resolved "https://registry.yarnpkg.com/zod/-/zod-3.23.0.tgz#a25dab5052702834233e0041e9dd8b6a8480e744"
1366613661
integrity sha512-OFLT+LTocvabn6q76BTwVB0hExEBS0IduTr3cqZyMqEDbOnYmcU+y0tUAYbND4uwclpBGi4I4UUBGzylWpjLGA==
13667-

0 commit comments

Comments
 (0)