Skip to content

Commit 1164b25

Browse files
amrbashirgezihuzi
authored andcommitted
feat(http): handle 205, 304 null body (tauri-apps#2647)
followup to tauri-apps#2636 ref: tauri-apps#2636 (comment)
1 parent 38fcca2 commit 1164b25

File tree

2 files changed

+33
-29
lines changed

2 files changed

+33
-29
lines changed

plugins/http/api-iife.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugins/http/guest-js/index.ts

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -229,40 +229,44 @@ export async function fetch(
229229
rid
230230
})
231231

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+
}
241245

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)
245249

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+
}
251255

252-
controller.enqueue(actualRes)
253-
}
256+
controller.enqueue(actualRes)
257+
}
254258

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+
}
261267
})
262-
}
263-
})
264268

265-
const res = new Response(status !== 204 ? readableStreamBody : null, {
269+
const res = new Response(body, {
266270
status,
267271
statusText
268272
})

0 commit comments

Comments
 (0)