{"id":16845676,"url":"https://github.com/gjtorikian/isbinaryfile","last_synced_at":"2025-05-14T12:11:08.056Z","repository":{"id":509619,"uuid":"6126674","full_name":"gjtorikian/isBinaryFile","owner":"gjtorikian","description":"Detects if a file is binary in Node.js. Similar to Perl's -B","archived":false,"fork":false,"pushed_at":"2024-10-23T16:09:02.000Z","size":3003,"stargazers_count":165,"open_issues_count":5,"forks_count":23,"subscribers_count":5,"default_branch":"main","last_synced_at":"2025-04-11T04:59:26.234Z","etag":null,"topics":["nodejs"],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gjtorikian.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"gjtorikian"}},"created_at":"2012-10-08T15:33:30.000Z","updated_at":"2025-03-13T05:02:00.000Z","dependencies_parsed_at":"2023-07-05T15:02:15.349Z","dependency_job_id":"4ff54e85-cba3-4884-beaf-9b1850991b32","html_url":"https://github.com/gjtorikian/isBinaryFile","commit_stats":{"total_commits":161,"total_committers":19,"mean_commits":8.473684210526315,"dds":0.6335403726708074,"last_synced_commit":"51b8c3a0b460e39cb6420ce8b7fe1e596887668a"},"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gjtorikian%2FisBinaryFile","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gjtorikian%2FisBinaryFile/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gjtorikian%2FisBinaryFile/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gjtorikian%2FisBinaryFile/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gjtorikian","download_url":"https://codeload.github.com/gjtorikian/isBinaryFile/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248345273,"owners_count":21088244,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["nodejs"],"created_at":"2024-10-13T12:59:37.284Z","updated_at":"2025-04-11T04:59:33.590Z","avatar_url":"https://github.com/gjtorikian.png","language":"TypeScript","readme":"# isBinaryFile\n\nDetects if a file is binary in Node.js using ✨promises✨. Similar to [Perl's `-B` switch](http://stackoverflow.com/questions/899206/how-does-perl-know-a-file-is-binary), in that:\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\u003cboolean\u003e` (or just `boolean` for `*Sync`). `true` if the file is binary, `false` otherwise.\n\n### isBinaryFile(filepath)\n\n* `filepath` -  a `string` indicating the path to the file.\n\n### isBinaryFile(bytes[, size])\n\n* `bytes` - a `Buffer` of the file's contents.\n* `size` - an optional `number` indicating the file size.\n\n### isBinaryFileSync(filepath)\n\n* `filepath` - a `string` indicating the path to the file.\n\n\n### isBinaryFileSync(bytes[, size])\n\n* `bytes` - a `Buffer` of the file's contents.\n* `size` - an optional `number` indicating the file size.\n\n### Examples\n\nHere's an arbitrary usage:\n\n```javascript\nconst isBinaryFile = require(\"isbinaryfile\").isBinaryFile;\nconst fs = require(\"fs\");\n\nconst filename = \"fixtures/pdf.pdf\";\nconst data = fs.readFileSync(filename);\nconst stat = fs.lstatSync(filename);\n\nisBinaryFile(data, stat.size).then((result) =\u003e {\n  if (result) {\n    console.log(\"It is binary!\")\n  }\n  else {\n    console.log(\"No it is not.\")\n  }\n});\n\nconst isBinaryFileSync = require(\"isbinaryfile\").isBinaryFileSync;\nconst bytes = fs.readFileSync(filename);\nconst size = fs.lstatSync(filename).size;\nconsole.log(isBinaryFileSync(bytes, size)); // true or false\n```\n\n## Testing\n\nRun `npm install`, then run `npm test`.\n","funding_links":["https://github.com/sponsors/gjtorikian"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgjtorikian%2Fisbinaryfile","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgjtorikian%2Fisbinaryfile","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgjtorikian%2Fisbinaryfile/lists"}