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

Function isLikelyUtf8Text

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

Source from the content-addressed store, hash-verified

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

Callers 1

sniffMimeTypeFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected