Don't load response when blocking large responses

This commit is contained in:
Gregory Schier
2025-01-10 06:27:57 -08:00
parent f694456ddc
commit 8b5b66acf0
10 changed files with 197 additions and 122 deletions

View File

@@ -1,3 +1,4 @@
import MimeType from 'whatwg-mimetype';
import type { EditorProps } from '../components/core/Editor/Editor';
export function languageFromContentType(
@@ -40,10 +41,39 @@ export function isJSON(content: string | null | undefined): boolean {
if (typeof content !== 'string') return false;
try {
JSON.parse(content)
JSON.parse(content);
return true;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
} catch (err) {
return false;
}
}
export function isProbablyTextContentType(contentType: string | null): boolean {
if (contentType == null) return false;
const mimeType = getMimeTypeFromContentType(contentType).essence;
const normalized = mimeType.toLowerCase();
// Check if it starts with "text/"
if (normalized.startsWith('text/')) {
return true;
}
// Common text mimetypes and suffixes
return [
'application/json',
'application/xml',
'application/javascript',
'application/yaml',
'+json',
'+xml',
'+yaml',
'+text',
].some((textType) => normalized === textType || normalized.endsWith(textType));
}
export function getMimeTypeFromContentType(contentType: string) {
const mimeType = new MimeType(contentType);
return mimeType;
}

View File

@@ -5,7 +5,7 @@ import type {
HttpResponse,
HttpResponseHeader,
} from '@yaakapp-internal/models';
import MimeType from 'whatwg-mimetype';
import { getMimeTypeFromContentType } from './contentType';
export const BODY_TYPE_NONE = null;
export const BODY_TYPE_GRAPHQL = 'graphql';
@@ -61,6 +61,6 @@ export function getCharsetFromContentType(headers: HttpResponseHeader[]): string
const contentType = getContentTypeHeader(headers);
if (contentType == null) return null;
const mimeType = new MimeType(contentType);
const mimeType = getMimeTypeFromContentType(contentType);
return mimeType.parameters.get('charset') ?? null;
}