1+ import process from "node:process" ;
2+
13import { expect , it } from "vitest" ;
24
35import { 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
1320it ( "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+ } ) ;
0 commit comments