From 3f81555fbf183db388579116318411d47b8bdf24 Mon Sep 17 00:00:00 2001 From: ppbl <33046279+ppbl@users.noreply.github.com> Date: Wed, 21 Feb 2024 10:20:46 +0800 Subject: [PATCH] fix(http): response with status code 204 (#960) * Fix response construction with status code 204 * run pnpm build --- plugins/http/guest-js/index.ts | 28 +++++++++++++++++++--------- plugins/http/src/api-iife.js | 2 +- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/plugins/http/guest-js/index.ts b/plugins/http/guest-js/index.ts index 240a89e8..bba654f1 100644 --- a/plugins/http/guest-js/index.ts +++ b/plugins/http/guest-js/index.ts @@ -155,15 +155,25 @@ export async function fetch( rid, }); - const body = await invoke("plugin:http|fetch_read_body", { - rid: responseRid, - }); - - const res = new Response(new Uint8Array(body), { - headers, - status, - statusText, - }); + const body = await invoke( + "plugin:http|fetch_read_body", + { + rid: responseRid, + }, + ); + + const res = new Response( + body instanceof ArrayBuffer && body.byteLength + ? body + : body instanceof Array && body.length + ? new Uint8Array(body) + : null, + { + headers, + status, + statusText, + }, + ); // url is read only but seems like we can do this Object.defineProperty(res, "url", { value: url }); diff --git a/plugins/http/src/api-iife.js b/plugins/http/src/api-iife.js index d86fb195..22311a7c 100644 --- a/plugins/http/src/api-iife.js +++ b/plugins/http/src/api-iife.js @@ -1 +1 @@ -if("__TAURI__"in window){var __TAURI_PLUGIN_HTTP__=function(e){"use strict";async function t(e,t={},r){return window.__TAURI_INTERNALS__.invoke(e,t,r)}return"function"==typeof SuppressedError&&SuppressedError,e.fetch=async function(e,r){const n=r?.maxRedirections,i=r?.connectTimeout,a=r?.proxy;r&&(delete r.maxRedirections,delete r.connectTimeout,delete r.proxy);const o=r?.signal,s=new Request(e,r),u=await s.arrayBuffer(),c=u.byteLength?Array.from(new Uint8Array(u)):null,d=await t("plugin:http|fetch",{clientConfig:{method:s.method,url:s.url,headers:Array.from(s.headers.entries()),data:c,maxRedirections:n,connectTimeout:i,proxy:a}});o?.addEventListener("abort",(()=>{t("plugin:http|fetch_cancel",{rid:d})}));const{status:_,statusText:p,url:f,headers:l,rid:h}=await t("plugin:http|fetch_send",{rid:d}),y=await t("plugin:http|fetch_read_body",{rid:h}),T=new Response(new Uint8Array(y),{headers:l,status:_,statusText:p});return Object.defineProperty(T,"url",{value:f}),T},e}({});Object.defineProperty(window.__TAURI__,"http",{value:__TAURI_PLUGIN_HTTP__})} +if("__TAURI__"in window){var __TAURI_PLUGIN_HTTP__=function(e){"use strict";async function t(e,t={},n){return window.__TAURI_INTERNALS__.invoke(e,t,n)}return"function"==typeof SuppressedError&&SuppressedError,e.fetch=async function(e,n){const r=n?.maxRedirections,i=n?.connectTimeout,a=n?.proxy;n&&(delete n.maxRedirections,delete n.connectTimeout,delete n.proxy);const o=n?.signal,s=new Request(e,n),u=await s.arrayBuffer(),c=u.byteLength?Array.from(new Uint8Array(u)):null,d=await t("plugin:http|fetch",{clientConfig:{method:s.method,url:s.url,headers:Array.from(s.headers.entries()),data:c,maxRedirections:r,connectTimeout:i,proxy:a}});o?.addEventListener("abort",(()=>{t("plugin:http|fetch_cancel",{rid:d})}));const{status:_,statusText:f,url:l,headers:p,rid:h}=await t("plugin:http|fetch_send",{rid:d}),y=await t("plugin:http|fetch_read_body",{rid:h}),T=new Response(y instanceof ArrayBuffer&&y.byteLength?y:y instanceof Array&&y.length?new Uint8Array(y):null,{headers:p,status:_,statusText:f});return Object.defineProperty(T,"url",{value:l}),T},e}({});Object.defineProperty(window.__TAURI__,"http",{value:__TAURI_PLUGIN_HTTP__})}