Body viewer and decode fixes (#812)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Gregory SchierandClaude Opus 5.5 authored and GitHub committed 2026-11-04 21:21:05 -07:00
1 parent 87a30200f9
commit 4ad0533901
26 files changed
+1633 -91

No files matched your search

@@ -0,0 +1,20 @@
import { hexy } from "hexy";
import { describe, expect, test, vi } from "vite-plus/test";
import { hexdump } from "./HexdumpViewer";
vi.mock("hexy", () => ({ hexy: vi.fn((data: number[]) => `dump of ${data.length} bytes`) }));
describe("hexdump", () => {
test("makes the hexdump of the same bytes once", () => {
const message = [0x01, 0x02, 0x03];
expect(hexdump(message)).toBe("dump of 3 bytes");
expect(hexdump(message)).toBe("dump of 3 bytes");
expect(hexy).toHaveBeenCalledTimes(1);
// Other bytes, even equal ones, get their own.
expect(hexdump([0x01, 0x02, 0x03])).toBe("dump of 3 bytes");
expect(hexy).toHaveBeenCalledTimes(2);
expect(hexdump([])).toBe("");
expect(hexy).toHaveBeenCalledTimes(2);
});
});
@@ -0,0 +1,50 @@
import { hexy } from "hexy";
import type { ReactNode } from "react";
import { useMemo } from "react";
import type { CodeEditorProps } from "../editor/CodeEditor";
import { LazyCodeEditor } from "../editor/LazyCodeEditor";
/** Hexdumps already made, by the bytes they were made of, for as long as those bytes live */
const hexdumps = new WeakMap<Uint8Array | number[], string>();
/**
* Bytes as a hexdump: offset, hex and ASCII columns, one line per 16 bytes. Made once for the
* same bytes, so a viewer and its copy action share one.
*/
export function hexdump(data: Uint8Array | number[]): string {
if (data.length === 0) return "";
let dump = hexdumps.get(data);
if (dump == null) {
dump = hexy(data);
hexdumps.set(data, dump);
}
return dump;
}
export interface HexdumpViewerProps {
data: Uint8Array | number[];
className?: string;
/** Renders the editor, for a host whose editor builds on CodeEditor */
renderEditor?: (props: CodeEditorProps) => ReactNode;
}
/** A read-only hexdump of binary data */
export function HexdumpViewer({
data,
className,
renderEditor = renderCodeEditor,
}: HexdumpViewerProps) {
const text = useMemo(() => hexdump(data), [data]);
return renderEditor({
language: "text",
defaultValue: text,
wrapLines: false,
readOnly: true,
stateKey: null,
className,
});
}
function renderCodeEditor(props: CodeEditorProps) {
return <LazyCodeEditor {...props} />;
}
@@ -89,6 +89,8 @@ export function TextViewer({
],
queryFn: () => (filterText ?? rejectFilter)(filter.appliedFilter ?? ""),
enabled: hasFilter && filter.appliedFilter != null,
// An expression that fails, fails every time, so the error shows at once.
retry: false,
});
const canFilter = hasFilter && (language === "json" || language === "xml" || language === "html");
@@ -195,6 +197,7 @@ export function TextViewer({
}
return formatText(text, language);
},
retry: false,
}).data;
if (formattedBody == null) {
return null;