(bytes: Buffer)
| 223 | } |
| 224 | |
| 225 | function 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 | |
| 249 | function createLoadedFile(args: { |
| 250 | data: Buffer; |
no outgoing calls
no test coverage detected