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

Function isLikelyUtf8Text

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

Source from the content-addressed store, hash-verified

377 prefix.every((byte, index) => bytes[index] === byte);
378
379const isLikelyUtf8Text = (bytes: Uint8Array): boolean => {
380 if (bytes.length === 0) return false;
381 let text: string;
382 // oxlint-disable-next-line executor/no-try-catch-or-throw -- boundary: TextDecoder throws while probing arbitrary binary content
383 try {
384 text = new TextDecoder("utf-8", { fatal: true }).decode(bytes);
385 } catch {
386 return false;
387 }
388 let suspicious = 0;
389 for (let index = 0; index < text.length; index += 1) {
390 const code = text.charCodeAt(index);
391 const allowedControl = code === 0x09 || code === 0x0a || code === 0x0c || code === 0x0d;
392 if (code === 0x00) return false;
393 if (code < 0x20 && !allowedControl) suspicious += 1;
394 }
395 return suspicious / Math.max(1, text.length) <= 0.02;
396};
397
398const sniffMimeType = (bytes: Uint8Array): string | null => {
399 if (startsWithBytes(bytes, [0xff, 0xd8, 0xff])) return "image/jpeg";

Callers 1

sniffMimeTypeFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected