@@ -229,40 +229,44 @@ export async function fetch(
229
229
rid
230
230
} )
231
231
232
- const readableStreamBody = new ReadableStream ( {
233
- start : ( controller ) => {
234
- const streamChannel = new Channel < ArrayBuffer | number [ ] > ( )
235
- streamChannel . onmessage = ( res : ArrayBuffer | number [ ] ) => {
236
- // close early if aborted
237
- if ( signal ?. aborted ) {
238
- controller . error ( ERROR_REQUEST_CANCELLED )
239
- return
240
- }
232
+ // no body for 204, 205 and 304
233
+ // see https://searchfox.org/mozilla-central/source/dom/fetch/Response.cpp#258
234
+ const body = [ 204 , 205 , 304 ] . includes ( status )
235
+ ? null
236
+ : new ReadableStream ( {
237
+ start : ( controller ) => {
238
+ const streamChannel = new Channel < ArrayBuffer | number [ ] > ( )
239
+ streamChannel . onmessage = ( res : ArrayBuffer | number [ ] ) => {
240
+ // close early if aborted
241
+ if ( signal ?. aborted ) {
242
+ controller . error ( ERROR_REQUEST_CANCELLED )
243
+ return
244
+ }
241
245
242
- const resUint8 = new Uint8Array ( res )
243
- const lastByte = resUint8 [ resUint8 . byteLength - 1 ]
244
- const actualRes = resUint8 . slice ( 0 , resUint8 . byteLength - 1 )
246
+ const resUint8 = new Uint8Array ( res )
247
+ const lastByte = resUint8 [ resUint8 . byteLength - 1 ]
248
+ const actualRes = resUint8 . slice ( 0 , resUint8 . byteLength - 1 )
245
249
246
- // close when the signal to close (last byte is 1) is sent from the IPC.
247
- if ( lastByte == 1 ) {
248
- controller . close ( )
249
- return
250
- }
250
+ // close when the signal to close (last byte is 1) is sent from the IPC.
251
+ if ( lastByte == 1 ) {
252
+ controller . close ( )
253
+ return
254
+ }
251
255
252
- controller . enqueue ( actualRes )
253
- }
256
+ controller . enqueue ( actualRes )
257
+ }
254
258
255
- // run a non-blocking body stream fetch
256
- invoke ( 'plugin:http|fetch_read_body' , {
257
- rid : responseRid ,
258
- streamChannel
259
- } ) . catch ( ( e ) => {
260
- controller . error ( e )
259
+ // run a non-blocking body stream fetch
260
+ invoke ( 'plugin:http|fetch_read_body' , {
261
+ rid : responseRid ,
262
+ streamChannel
263
+ } ) . catch ( ( e ) => {
264
+ controller . error ( e )
265
+ } )
266
+ }
261
267
} )
262
- }
263
- } )
264
268
265
- const res = new Response ( status !== 204 ? readableStreamBody : null , {
269
+ const res = new Response ( body , {
266
270
status,
267
271
statusText
268
272
} )
0 commit comments