Skip to content

Commit b3e56e0

Browse files
committed
refac
1 parent 15883e5 commit b3e56e0

1 file changed

Lines changed: 14 additions & 3 deletions

File tree

src/lib/apis/index.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -561,13 +561,24 @@ export const executeToolServer = async (
561561
responseHeaders[key] = value;
562562
});
563563

564-
const text = await res.text();
565564
let responseData;
565+
const contentType = res.headers.get('Content-Type')?.split(';')[0]?.trim() ?? '';
566566

567567
try {
568-
responseData = JSON.parse(text);
568+
responseData = await res.clone().json();
569569
} catch {
570-
responseData = text;
570+
if (contentType.startsWith('text/') || !contentType) {
571+
responseData = await res.text();
572+
} else {
573+
const buf = await res.arrayBuffer();
574+
const bytes = new Uint8Array(buf);
575+
let binary = '';
576+
for (let i = 0; i < bytes.length; i++) {
577+
binary += String.fromCharCode(bytes[i]);
578+
}
579+
const b64 = btoa(binary);
580+
responseData = `data:${contentType};base64,${b64}`;
581+
}
571582
}
572583
return [responseData, responseHeaders];
573584
} catch (err: any) {

0 commit comments

Comments
 (0)