import { useQuery } from "@tanstack/react-query"; import { formatSize } from "@yaakapp-internal/lib/formatSize"; import type { Flow, FlowBody } from "@yaakapp-internal/proxy-lib"; import { Banner, Button, CsvViewer, HexdumpViewer, Icon, ImageViewer, languageFromContentType, LoadingIcon, MultipartViewer, SvgViewer, TextViewer, WebPageViewer, } from "@yaakapp-internal/ui"; import type { CodeEditorProps } from "@yaakapp-internal/ui/editor"; import type { ReactNode } from "react"; import { useCallback, useEffect, useMemo, useState } from "react"; import { bodyAsText, bodyImageType, filterText, findHeader, emptyBodyText, flowBodyQueryOptions, formatText, mimeEssence, } from "../lib/body"; import { useSaveFlowBody } from "../lib/flowActions"; interface Props { flow: Flow; which: FlowBody; pretty: boolean; } export function useFlowBody(flow: Flow, which: FlowBody, enabled = true) { return useQuery({ ...flowBodyQueryOptions(flow, which), enabled }); } export function FlowBodyViewer({ flow, which, pretty }: Props) { const body = useFlowBody(flow, which); if (body.isPending) { return ( ); } if (body.error) { return {body.error.message}; } const { bytes, truncated, storedSize, decodeError, note } = body.data; if (bytes.length === 0 && decodeError == null) { return {emptyBodyText(which, note)}; } const headers = which === "request" ? flow.requestHeaders : flow.responseHeaders; const contentType = findHeader(headers, "content-type"); const mime = mimeEssence(contentType) ?? ""; const wireSize = which === "request" ? flow.requestSize : flow.responseSize; const wireTruncated = which === "request" ? flow.requestTruncated : flow.responseTruncated; // An image by its bytes or type, else text whatever the content type says, so a page sent with // an odd one still reads: `viewerText`'s rule. const imageType = decodeError == null ? bodyImageType(bytes, truncated, contentType) : null; const text = decodeError == null && imageType == null ? bodyAsText(bytes, truncated, contentType) : null; let viewer: ReactNode; if (decodeError != null) { // What failed to decode is as it was on the wire, compressed, so never text. viewer = ; } else if (mime.startsWith("multipart/") && pretty) { const boundary = contentType?.match(/;\s*boundary="?([^";]+)"?/i)?.[1] ?? "unknown"; viewer = ( ); } else if (imageType != null) { viewer = ; } else if (text == null) { viewer = ; } else if (mime.startsWith("image/svg")) { viewer = ; } else if (/csv|tab-separated/.test(mime) && pretty) { viewer = ; } else { const language = languageFromContentType(contentType, text); if (language === "html" && pretty && which === "response") { viewer = ; } else { viewer = ( ); } } if (!truncated && decodeError == null) return viewer; return (
{decodeError != null && ( {decodeError}. Showing the body as it was on the wire. )} {truncated && ( {wireTruncated || decodeError != null ? `Showing the first ${formatSize(storedSize)} of ${formatSize(wireSize)}, the capture limit. The app received all of it.` : `Showing the first ${formatSize(bytes.length)} of the decoded body, the capture limit. The app received all of it.`} )}
{viewer}
); } /** Bytes shown in the hexdump; the rest are in the saved file */ const HEXDUMP_LIMIT = 256 * 1024; /** A body that isn't text: a hexdump, and a way to save it */ function BinaryBody({ flow, which, bytes, }: { flow: Flow; which: FlowBody; bytes: Uint8Array; }) { const saveBody = useSaveFlowBody(); const shown = useMemo(() => bytes.subarray(0, HEXDUMP_LIMIT), [bytes]); return (
Binary body, {formatSize(bytes.length)} {shown.length < bytes.length && `, first ${formatSize(shown.length)} shown`}
); } /** A text body, filtered with the syntax for the language detected from it, not just its header */ function FlowTextViewer({ flow, which, text, language, pretty, }: Props & { text: string; language: CodeEditorProps["language"] }) { const runFilter = useCallback( (expression: string) => filterText(flow.id, which, expression, language), [flow.id, which, language], ); return ( ); } function ImageBody({ bytes, mime }: { bytes: Uint8Array; mime: string }) { const [size, setSize] = useState<{ width: number; height: number } | null>(null); useEffect(() => { let cancelled = false; createImageBitmap(new Blob([bytes], { type: mime })).then( (bitmap) => { if (!cancelled) setSize({ width: bitmap.width, height: bitmap.height }); bitmap.close(); }, () => { if (!cancelled) setSize(null); }, ); return () => { cancelled = true; }; }, [bytes, mime]); return (
{size != null && `${size.width} × ${size.height} • `} {mime} • {formatSize(bytes.length)}
); } export function EmptyState({ children }: { children: ReactNode }) { return (
{children}
); }