Skip to content

Commit

Permalink
feat: add support for if-match on retry handler (#3144)
Browse files Browse the repository at this point in the history
  • Loading branch information
metcoder95 authored Apr 26, 2024
1 parent 77a7947 commit 3f927b8
Show file tree
Hide file tree
Showing 2 changed files with 339 additions and 14 deletions.
35 changes: 21 additions & 14 deletions lib/handler/retry-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ const { isDisturbed, parseHeaders, parseRangeHeader } = require('../core/util')

function calculateRetryAfterHeader (retryAfter) {
const current = Date.now()
const diff = new Date(retryAfter).getTime() - current

return diff
return new Date(retryAfter).getTime() - current
}

class RetryHandler {
Expand Down Expand Up @@ -116,11 +114,7 @@ class RetryHandler {
const { counter } = state

// Any code that is not a Undici's originated and allowed to retry
if (
code &&
code !== 'UND_ERR_REQ_RETRY' &&
!errorCodes.includes(code)
) {
if (code && code !== 'UND_ERR_REQ_RETRY' && !errorCodes.includes(code)) {
cb(err)
return
}
Expand Down Expand Up @@ -246,10 +240,7 @@ class RetryHandler {
start != null && Number.isFinite(start),
'content-range mismatch'
)
assert(
end != null && Number.isFinite(end),
'invalid content-length'
)
assert(end != null && Number.isFinite(end), 'invalid content-length')

this.start = start
this.end = end
Expand All @@ -270,6 +261,13 @@ class RetryHandler {
this.resume = resume
this.etag = headers.etag != null ? headers.etag : null

// Weak etags are not useful for comparison nor cache
// for instance not safe to assume if the response is byte-per-byte
// equal
if (this.etag != null && this.etag.startsWith('W/')) {
this.etag = null
}

return this.handler.onHeaders(
statusCode,
rawHeaders,
Expand Down Expand Up @@ -308,7 +306,9 @@ class RetryHandler {
// and server error response
if (this.retryCount - this.retryCountCheckpoint > 0) {
// We count the difference between the last checkpoint and the current retry count
this.retryCount = this.retryCountCheckpoint + (this.retryCount - this.retryCountCheckpoint)
this.retryCount =
this.retryCountCheckpoint +
(this.retryCount - this.retryCountCheckpoint)
} else {
this.retryCount += 1
}
Expand All @@ -328,11 +328,18 @@ class RetryHandler {
}

if (this.start !== 0) {
const headers = { range: `bytes=${this.start}-${this.end ?? ''}` }

// Weak etag check - weak etags will make comparison algorithms never match
if (this.etag != null) {
headers['if-match'] = this.etag
}

this.opts = {
...this.opts,
headers: {
...this.opts.headers,
range: `bytes=${this.start}-${this.end ?? ''}`
...headers
}
}
}
Expand Down
Loading

0 comments on commit 3f927b8

Please sign in to comment.