File tree Expand file tree Collapse file tree 4 files changed +63
-2
lines changed Expand file tree Collapse file tree 4 files changed +63
-2
lines changed Original file line number Diff line number Diff line change
1
+ ---
2
+ " @remix-run/web-fetch " : patch
3
+ ---
4
+
5
+ allow clone of request and responses will null body
Original file line number Diff line number Diff line change @@ -273,7 +273,7 @@ async function consumeBody(data) {
273
273
* Clone body given Res/Req instance
274
274
*
275
275
* @param {Body } instance Response or Request instance
276
- * @return {ReadableStream<Uint8Array> }
276
+ * @return {ReadableStream<Uint8Array> | null }
277
277
*/
278
278
export const clone = instance => {
279
279
const { body} = instance ;
@@ -283,7 +283,10 @@ export const clone = instance => {
283
283
throw new Error ( 'cannot clone body after it is used' ) ;
284
284
}
285
285
286
- // @ts -expect-error - could be null
286
+ if ( ! body ) {
287
+ return null ;
288
+ }
289
+
287
290
const [ left , right ] = body . tee ( ) ;
288
291
instance [ INTERNALS ] . body = left ;
289
292
return right ;
Original file line number Diff line number Diff line change @@ -258,6 +258,37 @@ describe('Request', () => {
258
258
} ) ;
259
259
} ) ;
260
260
261
+ it ( 'should support clone() method with null body' , ( ) => {
262
+ const url = base ;
263
+
264
+ const agent = new http . Agent ( ) ;
265
+ const { signal} = new AbortController ( ) ;
266
+ const request = new Request ( url , {
267
+ method : 'POST' ,
268
+ redirect : 'manual' ,
269
+ headers : {
270
+ b : '2'
271
+ } ,
272
+ follow : 3 ,
273
+ compress : false ,
274
+ agent,
275
+ signal
276
+ } ) ;
277
+ const cl = request . clone ( ) ;
278
+ expect ( cl . url ) . to . equal ( url ) ;
279
+ expect ( cl . method ) . to . equal ( 'POST' ) ;
280
+ expect ( cl . redirect ) . to . equal ( 'manual' ) ;
281
+ expect ( cl . headers . get ( 'b' ) ) . to . equal ( '2' ) ;
282
+ expect ( cl . follow ) . to . equal ( 3 ) ;
283
+ expect ( cl . compress ) . to . equal ( false ) ;
284
+ expect ( cl . method ) . to . equal ( 'POST' ) ;
285
+ expect ( cl . counter ) . to . equal ( 0 ) ;
286
+ expect ( cl . agent ) . to . equal ( agent ) ;
287
+ expect ( cl . signal ) . to . equal ( signal ) ;
288
+ // Clone body should be null
289
+ expect ( cl . body ) . to . equal ( null ) ;
290
+ } ) ;
291
+
261
292
it ( 'should support ArrayBuffer as body' , ( ) => {
262
293
const encoder = new TextEncoder ( ) ;
263
294
const request = new Request ( base , {
Original file line number Diff line number Diff line change @@ -129,6 +129,28 @@ describe('Response', () => {
129
129
} ) ;
130
130
} ) ;
131
131
132
+ it ( 'should support clone() method with null body' , ( ) => {
133
+ const res = new Response ( null , {
134
+ headers : {
135
+ a : '1'
136
+ } ,
137
+ url : base ,
138
+ status : 346 ,
139
+ statusText : 'production'
140
+ } ) ;
141
+ const cl = res . clone ( ) ;
142
+ expect ( cl . headers . get ( 'a' ) ) . to . equal ( '1' ) ;
143
+ expect ( cl . url ) . to . equal ( base ) ;
144
+ expect ( cl . status ) . to . equal ( 346 ) ;
145
+ expect ( cl . statusText ) . to . equal ( 'production' ) ;
146
+ expect ( cl . ok ) . to . be . false ;
147
+ // Clone body should also be null
148
+ expect ( cl . body ) . to . equal ( null ) ;
149
+ return cl . text ( ) . then ( result => {
150
+ expect ( result ) . to . equal ( '' ) ;
151
+ } ) ;
152
+ } ) ;
153
+
132
154
it ( 'should support stream as body' , ( ) => {
133
155
const body = streamFromString ( 'a=1' ) ;
134
156
const res = new Response ( body ) ;
You can’t perform that action at this time.
0 commit comments