{"name":"isbinaryfile","description":"Detects if a file is binary in Node.js. Similar to Perl's -B.","version":"6.0.0","type":"module","keywords":["text","binary","encoding","istext","is text","isbinary","is binary","is text or binary","is text or binary file","isbinaryfile","is binary file","istextfile","is text file"],"devDependencies":{"@types/jest":"^30","@types/node":"^24","jest":"^30.2.0","oxlint":"^1.31","prettier":"^3","ts-jest":"^29.1.4","typescript":"^5"},"engines":{"node":">= 24.0.0"},"license":"MIT","main":"./lib/index.js","types":"./lib/index.d.ts","exports":{".":{"types":"./lib/index.d.ts","default":"./lib/index.js"}},"maintainers":[{"name":"gjtorikian","email":"gjtorikian@gmail.com"}],"funding":"https://github.com/sponsors/gjtorikian/","repository":{"type":"git","url":"git+https://github.com/gjtorikian/isBinaryFile.git"},"scripts":{"build":"tsc","format":"prettier --write \"src/**/*.ts\" \"test/**/*.ts\"","lint":"oxlint src test","prepare":"npm run build","prepublishOnly":"npm test && npm run lint","test":"NODE_OPTIONS='--experimental-vm-modules' jest --config jestconfig.json","watch":"tsc -w"},"gitHead":"16d556d065fa056f7e7b775ccbdb5a0e26072051","_id":"isbinaryfile@6.0.0","bugs":{"url":"https://github.com/gjtorikian/isBinaryFile/issues"},"homepage":"https://github.com/gjtorikian/isBinaryFile#readme","_nodeVersion":"24.11.1","_npmVersion":"11.6.2","dist":{"integrity":"sha512-2FN2B8MAqKv6d5TaKsLvMrwMcghxwHTpcKy0L5mhNbRqjNqo2++SpCqN6eG1lCC1GmTQgvrYJYXv2+Chvyevag==","shasum":"adcae341a39375f3862ba706594e8450060b62f2","tarball":"http://registry-npm.ywwl.com/isbinaryfile/download/isbinaryfile-6.0.0.tgz","fileCount":7,"unpackedSize":20123,"attestations":{"url":"https://registry.npmjs.org/-/npm/v1/attestations/isbinaryfile@6.0.0","provenance":{"predicateType":"https://slsa.dev/provenance/v1"}},"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEUCIG/lJr8NA5eQfq8N3MKJGWOAOgj5Q+9/Bq3xvT1svVe9AiEApAUE2FwxWtznxLeXvqCxcPLIu3CVZJfFXjotxfa/oWU="}],"size":6146,"noattachment":false,"key":"/isbinaryfile/-/isbinaryfile-6.0.0.tgz"},"_npmUser":{"name":"GitHub Actions","email":"npm-oidc-no-reply@github.com","trustedPublisher":{"id":"github","oidcConfigId":"oidc:1888d37b-5819-4d28-827d-e91d2dada78d"}},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/isbinaryfile_6.0.0_1764969021847_0.27768692324971944"},"_hasShrinkwrap":false,"readme":"# isBinaryFile\n\nDetects if a file is binary in Node.js. Similar to [Perl's `-B` switch](http://stackoverflow.com/questions/899206/how-does-perl-know-a-file-is-binary), in that:\n\n- it reads the first few thousand bytes of a file\n- checks for a `null` byte; if it's found, it's binary\n- flags non-ASCII characters. After a certain number of \"weird\" characters, the file is flagged as binary\n\nMuch of the logic is pretty much ported from [ag](https://github.com/ggreer/the_silver_searcher).\n\nNote: if the file doesn't exist or is a directory, an error is thrown.\n\n## Installation\n\n```\nnpm install isbinaryfile\n```\n\n## Usage\n\nReturns `Promise<boolean>` (or just `boolean` for `*Sync`). `true` if the file is binary, `false` otherwise.\n\n### isBinaryFile(filepath[, options])\n\n- `filepath` - a `string` indicating the path to the file.\n- `options` - an optional object with the following properties:\n  - `encoding` - an encoding hint (see [Encoding Hints](#encoding-hints) below)\n\n### isBinaryFile(bytes[, options])\n\n- `bytes` - a `Buffer` of the file's contents.\n- `options` - an optional object with the following properties:\n  - `size` - the size of the buffer (defaults to `bytes.length`)\n  - `encoding` - an encoding hint (see [Encoding Hints](#encoding-hints) below)\n\n### isBinaryFileSync(filepath[, options])\n\nSynchronous version of `isBinaryFile`.\n\n### isBinaryFileSync(bytes[, options])\n\nSynchronous version of `isBinaryFile` for buffers.\n\n### Examples\n\nHere's an arbitrary usage:\n\n```javascript\nimport { isBinaryFile, isBinaryFileSync } from 'isbinaryfile';\nimport fs from 'fs';\n\nconst filename = 'fixtures/pdf.pdf';\n\n// Async with file path\nconst result = await isBinaryFile(filename);\nif (result) {\n  console.log('It is binary!');\n} else {\n  console.log('No it is not.');\n}\n\n// Sync with buffer\nconst bytes = fs.readFileSync(filename);\nconsole.log(isBinaryFileSync(bytes)); // true or false\n\n// With explicit size option\nconst partialBuffer = Buffer.alloc(100);\nfs.readSync(fs.openSync(filename, 'r'), partialBuffer, 0, 100, 0);\nconsole.log(isBinaryFileSync(partialBuffer, { size: 100 }));\n```\n\n### Encoding Hints\n\nFor files that use non-UTF-8 encodings, you can provide encoding hints to improve detection accuracy:\n\n```javascript\nimport { isBinaryFile, isBinaryFileSync } from 'isbinaryfile';\n\n// UTF-16 files without BOM are auto-detected in most cases\nconst result1 = await isBinaryFile('utf16-file.txt');\n\n// Or provide explicit encoding hint\nconst result2 = await isBinaryFile('utf16-file.txt', { encoding: 'utf-16' });\n\n// ISO-8859-1 / Latin-1 encoded files\nconst result3 = isBinaryFileSync('german-text.txt', { encoding: 'latin1' });\n\n// CJK encoded files (Big5, GB2312, EUC-KR, etc.)\nconst result4 = isBinaryFileSync('chinese-big5.txt', { encoding: 'big5' });\nconst result5 = isBinaryFileSync('korean-text.txt', { encoding: 'euc-kr' });\n\n// Generic CJK hint when exact encoding is unknown\nconst result6 = isBinaryFileSync('asian-text.txt', { encoding: 'cjk' });\n```\n\n#### Supported Encoding Hints\n\n| Hint         | Description                                |\n| ------------ | ------------------------------------------ |\n| `utf-16`     | UTF-16 (auto-detect endianness)            |\n| `utf-16le`   | UTF-16 Little Endian                       |\n| `utf-16be`   | UTF-16 Big Endian                          |\n| `latin1`     | ISO-8859-1 / Latin-1                       |\n| `iso-8859-1` | Alias for latin1                           |\n| `cjk`        | Generic CJK (use when encoding is unknown) |\n| `big5`       | Traditional Chinese                        |\n| `gb2312`     | Simplified Chinese                         |\n| `gbk`        | Extended GB2312                            |\n| `euc-kr`     | Korean                                     |\n| `shift-jis`  | Japanese                                   |\n\n**Note:** UTF-16 without BOM is automatically detected in most cases without needing a hint.\n\n## Testing\n\nRun `npm test`.\n","publish_time":1764969021980,"_cnpm_publish_time":1764969021980,"dist-tags":{"latest":"6.0.0"}}