@@ -7,9 +7,7 @@ const { isDisturbed, parseHeaders, parseRangeHeader } = require('../core/util')
7
7
8
8
function calculateRetryAfterHeader ( retryAfter ) {
9
9
const current = Date . now ( )
10
- const diff = new Date ( retryAfter ) . getTime ( ) - current
11
-
12
- return diff
10
+ return new Date ( retryAfter ) . getTime ( ) - current
13
11
}
14
12
15
13
class RetryHandler {
@@ -116,11 +114,7 @@ class RetryHandler {
116
114
const { counter } = state
117
115
118
116
// Any code that is not a Undici's originated and allowed to retry
119
- if (
120
- code &&
121
- code !== 'UND_ERR_REQ_RETRY' &&
122
- ! errorCodes . includes ( code )
123
- ) {
117
+ if ( code && code !== 'UND_ERR_REQ_RETRY' && ! errorCodes . includes ( code ) ) {
124
118
cb ( err )
125
119
return
126
120
}
@@ -246,10 +240,7 @@ class RetryHandler {
246
240
start != null && Number . isFinite ( start ) ,
247
241
'content-range mismatch'
248
242
)
249
- assert (
250
- end != null && Number . isFinite ( end ) ,
251
- 'invalid content-length'
252
- )
243
+ assert ( end != null && Number . isFinite ( end ) , 'invalid content-length' )
253
244
254
245
this . start = start
255
246
this . end = end
@@ -270,6 +261,13 @@ class RetryHandler {
270
261
this . resume = resume
271
262
this . etag = headers . etag != null ? headers . etag : null
272
263
264
+ // Weak etags are not useful for comparison nor cache
265
+ // for instance not safe to assume if the response is byte-per-byte
266
+ // equal
267
+ if ( this . etag != null && this . etag . startsWith ( 'W/' ) ) {
268
+ this . etag = null
269
+ }
270
+
273
271
return this . handler . onHeaders (
274
272
statusCode ,
275
273
rawHeaders ,
@@ -308,7 +306,9 @@ class RetryHandler {
308
306
// and server error response
309
307
if ( this . retryCount - this . retryCountCheckpoint > 0 ) {
310
308
// We count the difference between the last checkpoint and the current retry count
311
- this . retryCount = this . retryCountCheckpoint + ( this . retryCount - this . retryCountCheckpoint )
309
+ this . retryCount =
310
+ this . retryCountCheckpoint +
311
+ ( this . retryCount - this . retryCountCheckpoint )
312
312
} else {
313
313
this . retryCount += 1
314
314
}
@@ -328,11 +328,18 @@ class RetryHandler {
328
328
}
329
329
330
330
if ( this . start !== 0 ) {
331
+ const headers = { range : `bytes=${ this . start } -${ this . end ?? '' } ` }
332
+
333
+ // Weak etag check - weak etags will make comparison algorithms never match
334
+ if ( this . etag != null ) {
335
+ headers [ 'if-match' ] = this . etag
336
+ }
337
+
331
338
this . opts = {
332
339
...this . opts ,
333
340
headers : {
334
341
...this . opts . headers ,
335
- range : `bytes= ${ this . start } - ${ this . end ?? '' } `
342
+ ... headers
336
343
}
337
344
}
338
345
}
0 commit comments