2525
2626import { createHash } from "node:crypto" ;
2727import { createReadStream } from "node:fs" ;
28- import { type IncomingMessage , request as httpRequest } from "node:http" ;
28+ import { type ClientRequest , type IncomingMessage , request as httpRequest } from "node:http" ;
2929import { request as httpsRequest } from "node:https" ;
3030import { Readable } from "node:stream" ;
3131import { pipeline } from "node:stream/promises" ;
@@ -80,17 +80,20 @@ class PutRefusedError extends Error {
8080 }
8181}
8282
83+ /** An error no second try can change: a refusal below 500, or a file that is missing or unreadable. */
84+ function isFinal ( err : unknown ) : boolean {
85+ const code = ( err as { code ?: unknown } ) . code ;
86+ return (
87+ ( err instanceof PutRefusedError && err . status < 500 ) || code === "ENOENT" || code === "EACCES"
88+ ) ;
89+ }
90+
8391async function putWithRetry ( attempt : ( ) => Promise < void > , signal ?: AbortSignal ) : Promise < void > {
8492 for ( let tried = 0 ; ; tried ++ ) {
8593 try {
8694 return await attempt ( ) ;
8795 } catch ( err ) {
88- const code = ( err as { code ?: unknown } ) . code ;
89- const final =
90- ( err instanceof PutRefusedError && err . status < 500 ) ||
91- code === "ENOENT" ||
92- code === "EACCES" ;
93- if ( final || signal ?. aborted || tried >= putTimings . retries ) throw err ;
96+ if ( isFinal ( err ) || signal ?. aborted || tried >= putTimings . retries ) throw err ;
9497 await sleep ( putTimings . backoffMs * 2 ** tried , signal ) ;
9598 }
9699 }
@@ -193,6 +196,23 @@ function bodyStream(body: Uint8Array | UploadFileSource): Readable {
193196 ) ;
194197}
195198
199+ /** Ends `req` when no byte has been accepted by its socket for stallMs (its buffer is full and the link drains
200+ * nothing). Returns the function that stops the watch. */
201+ function watchStall ( req : ClientRequest ) : ( ) => void {
202+ let written = - 1 ;
203+ let movedAt = Date . now ( ) ;
204+ const watch = setInterval (
205+ ( ) => {
206+ const now = req . socket ?. bytesWritten ?? 0 ;
207+ if ( now !== written ) [ written , movedAt ] = [ now , Date . now ( ) ] ;
208+ else if ( Date . now ( ) - movedAt >= putTimings . stallMs )
209+ req . destroy ( new Error ( `Upload stalled: no data sent for ${ putTimings . stallMs / 1000 } s` ) ) ;
210+ } ,
211+ Math . min ( 1000 , putTimings . stallMs / 3 ) ,
212+ ) ;
213+ return ( ) => clearInterval ( watch ) ;
214+ }
215+
196216async function putStreamToPresignedUrl (
197217 uploadUrl : string ,
198218 headers : Record < string , string > ,
@@ -209,18 +229,7 @@ async function putStreamToPresignedUrl(
209229 const response = new Promise < IncomingMessage > ( ( resolve , reject ) => {
210230 req . once ( "response" , resolve ) . once ( "error" , reject ) ;
211231 } ) ;
212- // No byte accepted by the socket for stallMs (its buffer is full and the link drains nothing) ends the try.
213- let written = - 1 ;
214- let movedAt = Date . now ( ) ;
215- const watch = setInterval (
216- ( ) => {
217- const now = req . socket ?. bytesWritten ?? 0 ;
218- if ( now !== written ) [ written , movedAt ] = [ now , Date . now ( ) ] ;
219- else if ( Date . now ( ) - movedAt >= putTimings . stallMs )
220- req . destroy ( new Error ( `Upload stalled: no data sent for ${ putTimings . stallMs / 1000 } s` ) ) ;
221- } ,
222- Math . min ( 1000 , putTimings . stallMs / 3 ) ,
223- ) ;
232+ const stopWatching = watchStall ( req ) ;
224233 const source = bodyStream ( body ) ;
225234 const sent = pipeline ( source , req ) ;
226235 let res : IncomingMessage ;
@@ -230,11 +239,16 @@ async function putStreamToPresignedUrl(
230239 await sent . catch ( ( ) => undefined ) ;
231240 throw err ;
232241 } finally {
233- clearInterval ( watch ) ;
242+ stopWatching ( ) ;
234243 }
244+ await settle ( res , sent ) ;
245+ }
246+
247+ /** Reads S3's answer: a refusal is thrown with its words; a refused upload can close the connection mid-body, so the
248+ * write error is the status's to explain. */
249+ async function settle ( res : IncomingMessage , sent : Promise < void > ) : Promise < void > {
235250 const status = res . statusCode ?? 0 ;
236251 const ok = status >= 200 && status < 300 ;
237- // A refused upload can close the connection mid-body; its status explains that write error.
238252 await sent . catch ( ( err : unknown ) => {
239253 if ( ok ) throw err ;
240254 } ) ;
0 commit comments