MCPcopy Index your code
hub / github.com/coder/mux / isLikelyTextFile

Function isLikelyTextFile

src/node/utils/attachments/readAttachmentFromPath.ts:225–247  ·  view source on GitHub ↗
(bytes: Buffer)

Source from the content-addressed store, hash-verified

223}
224
225function isLikelyTextFile(bytes: Buffer): boolean {
226 if (bytes.length === 0) {
227 return true;
228 }
229 if (bytes.includes(0)) {
230 return false;
231 }
232
233 const text = bytes.toString("utf8");
234 if (text.includes("\uFFFD")) {
235 return false;
236 }
237
238 let controlCharacterCount = 0;
239 for (const char of text) {
240 const code = char.charCodeAt(0);
241 if (code < 32 && char !== "\n" && char !== "\r" && char !== "\t") {
242 controlCharacterCount++;
243 }
244 }
245
246 return controlCharacterCount / text.length < 0.05;
247}
248
249function createLoadedFile(args: {
250 data: Buffer;

Callers 1

readAttachFileFromPathFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected