MCPcopy Create free account
hub / github.com/UsefulSoftwareCo/executor / isLikelyUtf8Text

Function isLikelyUtf8Text

packages/plugins/openapi/src/sdk/invoke.ts:425–442  ·  view source on GitHub ↗
(bytes: Uint8Array)

Source from the content-addressed store, hash-verified

423 prefix.every((byte, index) => bytes[index] === byte);
424
425const isLikelyUtf8Text = (bytes: Uint8Array): boolean => {
426 if (bytes.length === 0) return false;
427 let text: string;
428 // oxlint-disable-next-line executor/no-try-catch-or-throw -- boundary: TextDecoder throws while probing arbitrary binary content
429 try {
430 text = new TextDecoder("utf-8", { fatal: true }).decode(bytes);
431 } catch {
432 return false;
433 }
434 let suspicious = 0;
435 for (let index = 0; index < text.length; index += 1) {
436 const code = text.charCodeAt(index);
437 const allowedControl = code === 0x09 || code === 0x0a || code === 0x0c || code === 0x0d;
438 if (code === 0x00) return false;
439 if (code < 0x20 && !allowedControl) suspicious += 1;
440 }
441 return suspicious / Math.max(1, text.length) <= 0.02;
442};
443
444const sniffMimeType = (bytes: Uint8Array): string | null => {
445 if (startsWithBytes(bytes, [0xff, 0xd8, 0xff])) return "image/jpeg";

Callers 1

sniffMimeTypeFunction · 0.85

Calls 1

decodeMethod · 0.80

Tested by

no test coverage detected