@@ -16,7 +16,7 @@ interface DownloadResult {
1616const CONTENT_READY_MAX_ATTEMPTS = 6 ;
1717const CONTENT_READY_BASE_DELAY_MS = 500 ;
1818const CONTENT_READY_MAX_DELAY_MS = 4000 ;
19- const CONTENT_READY_TIMEOUT_MS = 15_000 ;
19+ const CONTENT_DOWNLOAD_TIMEOUT_MS = 15_000 ;
2020const LINE_CONTENT_BASE_URL = "https://api-data.line.me/v2/bot/message" ;
2121
2222class RetryableLineMediaFetchError extends MediaFetchError {
@@ -37,6 +37,8 @@ function lineContentUrl(messageId: string): string {
3737async function * lineResponseBodyChunks (
3838 response : Response ,
3939 messageId : string ,
40+ signal : AbortSignal ,
41+ onComplete : ( ) => void ,
4042) : AsyncIterable < Uint8Array > {
4143 const body = response . body ;
4244 if ( ! body ) {
@@ -52,13 +54,17 @@ async function* lineResponseBodyChunks(
5254 try {
5355 chunk = await reader . read ( ) ;
5456 } catch ( err ) {
57+ if ( signal . aborted ) {
58+ throw signal . reason ;
59+ }
5560 throw new RetryableLineMediaFetchError (
5661 `LINE media response stream failed for message ${ messageId } ` ,
5762 { cause : err } ,
5863 ) ;
5964 }
6065 if ( chunk . done ) {
6166 completed = true ;
67+ onComplete ( ) ;
6268 return ;
6369 }
6470 if ( chunk . value . byteLength > 0 ) {
@@ -67,7 +73,7 @@ async function* lineResponseBodyChunks(
6773 }
6874 } finally {
6975 if ( ! completed ) {
70- await reader . cancel ( ) . catch ( ( ) => undefined ) ;
76+ await reader . cancel ( signal . aborted ? signal . reason : undefined ) . catch ( ( ) => undefined ) ;
7177 }
7278 try {
7379 reader . releaseLock ( ) ;
@@ -78,16 +84,14 @@ async function* lineResponseBodyChunks(
7884async function fetchLineContentWhenReady (
7985 messageId : string ,
8086 channelAccessToken : string ,
87+ signal : AbortSignal ,
8188) : Promise < Response > {
82- const controller = new AbortController ( ) ;
83- const deadline = setTimeout ( ( ) => controller . abort ( ) , CONTENT_READY_TIMEOUT_MS ) ;
84- deadline . unref ( ) ;
8589 try {
8690 for ( let attempt = 0 ; attempt < CONTENT_READY_MAX_ATTEMPTS ; attempt ++ ) {
8791 const response = await fetchWithRuntimeDispatcherOrMockedGlobal ( lineContentUrl ( messageId ) , {
8892 headers : { Authorization : `Bearer ${ channelAccessToken } ` } ,
8993 redirect : "error" ,
90- signal : controller . signal ,
94+ signal,
9195 } ) ;
9296 if ( response . status === 200 ) {
9397 if ( ! response . body ) {
@@ -107,24 +111,19 @@ async function fetchLineContentWhenReady(
107111 ) ;
108112 }
109113 if ( attempt < CONTENT_READY_MAX_ATTEMPTS - 1 ) {
110- await delay ( contentBackoffDelayMs ( attempt ) , undefined , { signal : controller . signal } ) ;
114+ await delay ( contentBackoffDelayMs ( attempt ) , undefined , { signal } ) ;
111115 }
112116 }
113117 } catch ( err ) {
114- if ( controller . signal . aborted ) {
115- throw new RetryableLineMediaFetchError (
116- `LINE media for message ${ messageId } did not become ready within ${ CONTENT_READY_TIMEOUT_MS / 1000 } seconds` ,
117- { cause : err } ,
118- ) ;
118+ if ( signal . aborted ) {
119+ throw signal . reason ;
119120 }
120121 if ( err instanceof MediaFetchError ) {
121122 throw err ;
122123 }
123124 throw new RetryableLineMediaFetchError ( `LINE media download failed for message ${ messageId } ` , {
124125 cause : err ,
125126 } ) ;
126- } finally {
127- clearTimeout ( deadline ) ;
128127 }
129128
130129 throw new MediaFetchError (
@@ -160,27 +159,49 @@ export async function downloadLineMedia(
160159 messageId : string ,
161160 channelAccessToken : string ,
162161 maxBytes = 10 * 1024 * 1024 ,
163- options ?: { originalFilename ?: string } ,
162+ options ?: { originalFilename ?: string ; signal ?: AbortSignal } ,
164163) : Promise < DownloadResult > {
165- const response = await fetchLineContentWhenReady ( messageId , channelAccessToken ) ;
166- let saved : Awaited < ReturnType < typeof saveMediaStream > > ;
167- try {
168- saved = await saveMediaStream (
169- lineResponseBodyChunks ( response , messageId ) ,
170- response . headers . get ( "content-type" ) ?? undefined ,
171- "inbound" ,
172- maxBytes ,
173- options ?. originalFilename ,
164+ options ?. signal ?. throwIfAborted ( ) ;
165+ const deadlineAbort = new AbortController ( ) ;
166+ const signal = options ?. signal
167+ ? AbortSignal . any ( [ options . signal , deadlineAbort . signal ] )
168+ : deadlineAbort . signal ;
169+ // Header resolution does not finish the request; only EOF retires this deadline.
170+ const deadline = setTimeout ( ( ) => {
171+ deadlineAbort . abort (
172+ new RetryableLineMediaFetchError (
173+ `LINE media for message ${ messageId } did not download within ${ CONTENT_DOWNLOAD_TIMEOUT_MS / 1000 } seconds` ,
174+ ) ,
174175 ) ;
175- } catch ( err ) {
176- await response . body ?. cancel ( ) . catch ( ( ) => undefined ) ;
177- throw err ;
178- }
179- logVerbose ( `line: persisted media ${ messageId } to ${ saved . path } (${ saved . size } bytes)` ) ;
176+ } , CONTENT_DOWNLOAD_TIMEOUT_MS ) ;
177+ deadline . unref ( ) ;
178+ try {
179+ const response = await fetchLineContentWhenReady ( messageId , channelAccessToken , signal ) ;
180+ let saved : Awaited < ReturnType < typeof saveMediaStream > > ;
181+ try {
182+ saved = await saveMediaStream (
183+ lineResponseBodyChunks ( response , messageId , signal , ( ) => clearTimeout ( deadline ) ) ,
184+ response . headers . get ( "content-type" ) ?? undefined ,
185+ "inbound" ,
186+ maxBytes ,
187+ options ?. originalFilename ,
188+ ) ;
189+ } catch ( err ) {
190+ await response . body ?. cancel ( signal . aborted ? signal . reason : err ) . catch ( ( ) => undefined ) ;
191+ if ( signal . aborted ) {
192+ throw signal . reason ;
193+ }
194+ throw err ;
195+ }
196+ options ?. signal ?. throwIfAborted ( ) ;
197+ logVerbose ( `line: persisted media ${ messageId } to ${ saved . path } (${ saved . size } bytes)` ) ;
180198
181- return {
182- path : saved . path ,
183- contentType : saved . contentType ,
184- size : saved . size ,
185- } ;
199+ return {
200+ path : saved . path ,
201+ contentType : saved . contentType ,
202+ size : saved . size ,
203+ } ;
204+ } finally {
205+ clearTimeout ( deadline ) ;
206+ }
186207}
0 commit comments