Skip to content

Commit 13a8640

Browse files
committed
fix(browser): observe fetch settlement after synchronous abort
1 parent 09b2943 commit 13a8640

2 files changed

Lines changed: 81 additions & 1 deletion

File tree

‎src/browser/WebSourceMapFetcher.test.ts‎

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import process from "node:process";
2+
13
import { expect, it } from "vitest";
24

35
import { fetchWebSourceMaps } from "./WebSourceMapFetcher.js";
@@ -9,6 +11,11 @@ const input = analyzeWebBundleInputSchema.parse({
911
target_id: "page-1",
1012
fetch_source_maps: true,
1113
});
14+
const request = {
15+
scriptKey: `scr_${"1".repeat(64)}`,
16+
declaredUrl: "https://app.example.test/app.js.map",
17+
fetchUrl: "https://app.example.test/app.js.map",
18+
};
1219

1320
it("reports an empty selected request set without starting fetch work", async () => {
1421
const result = await fetchWebSourceMaps([], input, undefined, {
@@ -34,3 +41,76 @@ it("honors cancellation before returning an empty request set", async () => {
3441
reason,
3542
);
3643
});
44+
45+
it("cancels a response body that arrives after its fetch host aborts synchronously", async () => {
46+
const controller = new AbortController();
47+
const reason = new Error("fetch host aborted during admission");
48+
let resolveFetch!: (response: Response) => void;
49+
const fetchPromise = new Promise<Response>((resolve) => {
50+
resolveFetch = resolve;
51+
});
52+
let markCancelled!: () => void;
53+
const cancelled = new Promise<void>((resolve) => {
54+
markCancelled = resolve;
55+
});
56+
let cancellationReason: unknown;
57+
const body = new ReadableStream<Uint8Array>({
58+
cancel: (cause) => {
59+
cancellationReason = cause;
60+
markCancelled();
61+
},
62+
});
63+
const fetchInput = analyzeWebBundleInputSchema.parse({
64+
cdp_endpoint: "http://127.0.0.1:9222",
65+
allowed_origins: [new URL(request.fetchUrl).origin],
66+
target_id: "page-1",
67+
fetch_source_maps: true,
68+
});
69+
const pending = fetchWebSourceMaps([request], fetchInput, controller.signal, {
70+
fetch: () => {
71+
controller.abort(reason);
72+
return fetchPromise;
73+
},
74+
});
75+
76+
await expect(pending).rejects.toBe(reason);
77+
resolveFetch(new Response(body));
78+
await cancelled;
79+
expect(cancellationReason).toBe(reason);
80+
});
81+
82+
it("observes a fetch rejection that arrives after its host aborts synchronously", async () => {
83+
const controller = new AbortController();
84+
const reason = new Error("fetch host aborted during admission");
85+
const lateFailure = new Error("late fetch failure");
86+
let rejectFetch!: (cause: unknown) => void;
87+
const fetchPromise = new Promise<Response>((_resolve, reject) => {
88+
rejectFetch = reject;
89+
});
90+
const fetchInput = analyzeWebBundleInputSchema.parse({
91+
cdp_endpoint: "http://127.0.0.1:9222",
92+
allowed_origins: [new URL(request.fetchUrl).origin],
93+
target_id: "page-1",
94+
fetch_source_maps: true,
95+
});
96+
const pending = fetchWebSourceMaps([request], fetchInput, controller.signal, {
97+
fetch: () => {
98+
controller.abort(reason);
99+
return fetchPromise;
100+
},
101+
});
102+
let unhandled: unknown;
103+
const onUnhandled = (cause: unknown): void => {
104+
unhandled = cause;
105+
};
106+
process.on("unhandledRejection", onUnhandled);
107+
108+
try {
109+
await expect(pending).rejects.toBe(reason);
110+
rejectFetch(lateFailure);
111+
await new Promise<void>((resolve) => setImmediate(resolve));
112+
expect(unhandled).toBeUndefined();
113+
} finally {
114+
process.off("unhandledRejection", onUnhandled);
115+
}
116+
});

‎src/browser/WebSourceMapFetcher.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,6 @@ const promiseWithAbort = (
522522
signal: AbortSignal | undefined,
523523
): Promise<Response> => {
524524
if (signal === undefined) return promise;
525-
if (signal.aborted) return Promise.reject(signal.reason);
526525
return new Promise((resolve, reject) => {
527526
let settled = false;
528527
const cleanup = (): void => signal.removeEventListener("abort", abort);
@@ -555,6 +554,7 @@ const promiseWithAbort = (
555554
reject(cause);
556555
},
557556
);
557+
if (signal.aborted) abort();
558558
});
559559
};
560560

0 commit comments

Comments
 (0)