Skip to content

Commit 2de72d8

Browse files
committed
Ensure stallDetector is supplied for HEAD and POST requests
1 parent e4175c3 commit 2de72d8

File tree

1 file changed

+28
-15
lines changed

1 file changed

+28
-15
lines changed

lib/upload.ts

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,9 @@ export class BaseUpload {
407407

408408
let res: HttpResponse
409409
try {
410-
res = await this._sendRequest(req)
410+
// Create stall detector for final concatenation POST request
411+
const stallDetector = this._createStallDetector()
412+
res = await this._sendRequest(req, undefined, stallDetector)
411413
} catch (err) {
412414
if (!(err instanceof Error)) {
413415
throw new Error(`tus: value thrown that is not an error: ${err}`)
@@ -638,7 +640,9 @@ export class BaseUpload {
638640
) {
639641
req.setHeader('Upload-Complete', '?0')
640642
}
641-
res = await this._sendRequest(req)
643+
// Create stall detector for POST request
644+
const stallDetector = this._createStallDetector()
645+
res = await this._sendRequest(req, undefined, stallDetector)
642646
}
643647
} catch (err) {
644648
if (!(err instanceof Error)) {
@@ -700,7 +704,9 @@ export class BaseUpload {
700704

701705
let res: HttpResponse
702706
try {
703-
res = await this._sendRequest(req)
707+
// Create stall detector for HEAD request
708+
const stallDetector = this._createStallDetector()
709+
res = await this._sendRequest(req, undefined, stallDetector)
704710
} catch (err) {
705711
if (!(err instanceof Error)) {
706712
throw new Error(`tus: value thrown that is not an error: ${err}`)
@@ -852,23 +858,15 @@ export class BaseUpload {
852858
}
853859

854860
/**
855-
* _addChunktoRequest reads a chunk from the source and sends it using the
856-
* supplied request object. It will not handle the response.
861+
* Create a stall detector if stall detection is enabled and supported.
857862
*
858863
* @api private
859864
*/
860-
private async _addChunkToRequest(req: HttpRequest): Promise<HttpResponse> {
861-
const start = this._offset
862-
let end = this._offset + this.options.chunkSize
863-
864-
// Create stall detector for this request if stall detection is enabled and supported
865-
// but don't start it yet - we'll start it after onBeforeRequest completes
866-
let stallDetector: StallDetector | undefined
867-
865+
private _createStallDetector(): StallDetector | undefined {
868866
if (this.options.stallDetection?.enabled) {
869867
// Only enable stall detection if the HTTP stack supports progress events
870868
if (this.options.httpStack.supportsProgressEvents()) {
871-
stallDetector = new StallDetector(this.options.stallDetection, (reason: string) => {
869+
return new StallDetector(this.options.stallDetection, (reason: string) => {
872870
// Handle stall by aborting the current request
873871
// The abort will cause the request to fail, which will be caught
874872
// in _performUpload and wrapped in a DetailedError for proper retry handling
@@ -878,13 +876,28 @@ export class BaseUpload {
878876
}
879877
// Don't call _retryOrEmitError here - let the natural error flow handle it
880878
})
881-
// Don't start yet - will be started after onBeforeRequest
882879
} else {
883880
log(
884881
'tus: stall detection is enabled but the HTTP stack does not support progress events, it will be disabled for this upload',
885882
)
886883
}
887884
}
885+
return undefined
886+
}
887+
888+
/**
889+
* _addChunktoRequest reads a chunk from the source and sends it using the
890+
* supplied request object. It will not handle the response.
891+
*
892+
* @api private
893+
*/
894+
private async _addChunkToRequest(req: HttpRequest): Promise<HttpResponse> {
895+
const start = this._offset
896+
let end = this._offset + this.options.chunkSize
897+
898+
// Create stall detector for this request if stall detection is enabled and supported
899+
// but don't start it yet - we'll start it after onBeforeRequest completes
900+
const stallDetector = this._createStallDetector()
888901

889902
req.setProgressHandler((bytesSent) => {
890903
// Update per-request stall detector if active

0 commit comments

Comments
 (0)