Show gRPC as ASCII (#31)

* Moved try-catch up one block.

* Display grpc as ASCII.
This commit is contained in:
nimrod-up9
2021-05-05 17:23:10 +03:00
committed by GitHub
parent ed0d7ef8db
commit 938ca26cd4

View File

@@ -69,19 +69,23 @@ export const HAREntryBodySection: React.FC<HAREntryBodySectionProps> = ({
}) => { }) => {
const MAXIMUM_BYTES_TO_HIGHLIGHT = 10000; // The maximum of chars to highlight in body, in case the response can be megabytes const MAXIMUM_BYTES_TO_HIGHLIGHT = 10000; // The maximum of chars to highlight in body, in case the response can be megabytes
const supportedLanguages = [['html', 'html'], ['json', 'json'], ['application/grpc', 'json']]; // [[indicator, languageToUse],...] const supportedLanguages = [['html', 'html'], ['json', 'json'], ['application/grpc', 'json']]; // [[indicator, languageToUse],...]
const jsonLikeFormats = ['json', 'application/grpc']; const jsonLikeFormats = ['json'];
const binaryFormats = ['application/grpc'];
const [isWrapped, setIsWrapped] = useState(false); const [isWrapped, setIsWrapped] = useState(false);
const formatTextBody = (body): string => { const formatTextBody = (body): string => {
const chunk = body.slice(0, MAXIMUM_BYTES_TO_HIGHLIGHT); const chunk = body.slice(0, MAXIMUM_BYTES_TO_HIGHLIGHT);
const bodyBuf = encoding === 'base64' ? atob(chunk) : chunk; const bodyBuf = encoding === 'base64' ? atob(chunk) : chunk;
if (jsonLikeFormats.some(format => content?.mimeType?.indexOf(format) > -1)) { try {
try { if (jsonLikeFormats.some(format => content?.mimeType?.indexOf(format) > -1)) {
return JSON.stringify(JSON.parse(bodyBuf), null, 2); return JSON.stringify(JSON.parse(bodyBuf), null, 2);
} catch (error) { } else if (binaryFormats.some(format => content?.mimeType?.indexOf(format) > -1)) {
console.error(error); // Replace all non printable characters (ASCII)
return atob(bodyBuf).replace(/[^ -~]/g, '.')
} }
} catch (error) {
console.error(error);
} }
return bodyBuf; return bodyBuf;
} }