@@ -9,6 +9,11 @@ import { noneMatch } from "./if-none-match"
9
9
import { rangePrecondition } from "./if-range"
10
10
import { isUnmodifiedSince } from "./if-unmodified-since"
11
11
12
+ function clearPreconditionHeadersFromResponse ( response : HasOutgoingHeaders ) {
13
+ response . removeHeader ( "etag" )
14
+ response . removeHeader ( "last-modified" )
15
+ }
16
+
12
17
/**
13
18
* Checks that the provided response's associated request passes its
14
19
* [preconditions]{@link https://datatracker.ietf.org/doc/html/rfc9110#name-conditional-requests},
@@ -44,6 +49,7 @@ export function validatePreconditions(
44
49
if ( ifMatch != null ) {
45
50
if ( ! Object . hasOwnProperty . call ( current , "etag" ) ) current . etag = res . getHeader ( "etag" )
46
51
if ( ! someMatch ( current . etag , ifMatch ) ) {
52
+ clearPreconditionHeadersFromResponse ( res )
47
53
throw new ClientError ( "If-Match Precondition Failed" , {
48
54
statusCode : HttpStatus . PreconditionFailed ,
49
55
code : "ERR_PRECONDITION_FAILED_IF_MATCH" ,
@@ -60,6 +66,7 @@ export function validatePreconditions(
60
66
if ( ifMatch == null && ifUnmodifiedSince != null ) {
61
67
if ( ! Object . hasOwnProperty . call ( current , "lastModified" ) ) current . lastModified = res . getHeader ( "last-modified" )
62
68
if ( ! isUnmodifiedSince ( current . lastModified , ifUnmodifiedSince ) ) {
69
+ clearPreconditionHeadersFromResponse ( res )
63
70
throw new ClientError ( "If-Unmodified-Since Precondition Failed" , {
64
71
statusCode : HttpStatus . PreconditionFailed ,
65
72
code : "ERR_PRECONDITION_FAILED_IF_UNMODIFIED_SINCE" ,
@@ -77,13 +84,15 @@ export function validatePreconditions(
77
84
const failed = ! noneMatch ( current . etag , ifNoneMatch )
78
85
79
86
if ( failed && ( method === "GET" || method === "HEAD" ) ) {
87
+ clearPreconditionHeadersFromResponse ( res )
80
88
throw new NotModifiedError ( "If-None-Match Precondition Failed" , {
81
89
code : "ERR_PRECONDITION_FAILED_IF_NONE_MATCH" ,
82
90
expected : true ,
83
91
} )
84
92
}
85
93
86
94
if ( failed ) {
95
+ clearPreconditionHeadersFromResponse ( res )
87
96
throw new ClientError ( "If-None-Match Precondition Failed" , {
88
97
statusCode : HttpStatus . PreconditionFailed ,
89
98
code : "ERR_PRECONDITION_FAILED_IF_NONE_MATCH" ,
@@ -100,6 +109,7 @@ export function validatePreconditions(
100
109
if ( ( method === "GET" || method === "HEAD" ) && ifNoneMatch == null && ifModifiedSince != null ) {
101
110
if ( ! Object . hasOwnProperty . call ( current , "lastModified" ) ) current . lastModified = res . getHeader ( "last-modified" )
102
111
if ( ! hasBeenModifiedSince ( current . lastModified , ifModifiedSince ) ) {
112
+ clearPreconditionHeadersFromResponse ( res )
103
113
throw new NotModifiedError ( "If-Modified-Since Precondition Failed" , {
104
114
code : "ERR_PRECONDITION_FAILED_IF_MODIFIED_SINCE" ,
105
115
expected : true ,
0 commit comments