Determine if a filename and/or buffer is text or binary. Smarter detection than the other solutions.
Installations
npm install istextorbinary
Developer Guide
Typescript
Yes
Module System
ESM, UMD
Min. Node Version
>=4
Node Version
20.10.0
NPM Version
10.2.3
Score
99.1
Supply Chain
100
Quality
76.2
Maintenance
100
Vulnerability
100
License
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
TypeScript (95.82%)
JavaScript (4.18%)
Developer
bevry
Download Statistics
Total Downloads
340,258,684
Last Day
220,638
Last Week
1,150,394
Last Month
3,894,506
Last Year
58,984,029
GitHub Statistics
150 Stars
157 Commits
17 Forks
4 Watching
2 Branches
7 Contributors
Bundle Size
3.81 kB
Minified
1.61 kB
Minified + Gzipped
Sponsor this package
Package Meta Information
Latest Version
9.5.0
Package Id
istextorbinary@9.5.0
Unpacked Size
101.42 kB
Size
17.10 kB
File Count
20
NPM Version
10.2.3
Node Version
20.10.0
Publised On
29 Dec 2023
Total Downloads
Cumulative downloads
Total Downloads
340,258,684
Last day
3.2%
220,638
Compared to previous day
Last week
2.5%
1,150,394
Compared to previous week
Last month
-28%
3,894,506
Compared to previous month
Last year
17.6%
58,984,029
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
3
Is Text or Binary?
Determine if a filename and/or buffer is text or binary. Smarter detection than the other solutions.
Determination works like so:
- Extension Check: If filename is available, check if any of its extensions (from right to left) are an text extension or a binary extension, this is near instant.
- Contents Check: If no filename was provided, or the extension check was indeterminate, then check the contents of the buffer.
The extension check will check each of the filename's extensions, from right to left. This is done as certain applications utilise multiple extensions for transformations, such as app.x.y
may tell a compiler to transform from x
format to y
format, in this case perhaps x
is not a recognized extension but y
is, in which case we can make use of that to provide superior accuracy and convenience compared to just checking the rightmost extension.
The contents check (with the default options) will check 24 bytes at the start, middle, and end of the buffer. History has shown that checking all three locations is mandatory for accuracy, and that anything less is not accurate. This technique offers superior performance while still offering superior accuracy. Alternatives generally just do 1000 bytes at the start, which is slower, and inaccurate.
One cannot just do the contents check alone because UTF16 characters are indistinguishable from binary which would return an inaccurate result, hence why the combination is necessary for accuracy, with performance for known extensions a side-effect.
As such, this library's combination of extension check (if filename is provided), then contents check (if buffer is provided), offers superior performance and accuracy to alternatives.
Ever since 2012, this module's superior accuracy and performance has been essential to the operation of DocPad and its other dependents.
Usage
1import { isText, isBinary, getEncoding } from 'istextorbinary'
or
1const { isText, isBinary, getEncoding } = require('istextorbinary')
then
1isText(aFilename) // returns true if a text file otherwise false, checks only filename 2isText(null, aBuffer) // returns true if a text file otherwise false, checks only buffer 3isText(aFilename, aBuffer) // returns true if a text file otherwise false, checks filename then buffer 4isText(null, null) // returns null 5 6isBinary(aFilename) // returns true if a binary file otherwise false, checks only filename 7isBinary(null, aBuffer) // returns true if a binary file otherwise false, checks only buffer 8isBinary(aFilename, aBuffer) // returns true if a binary file otherwise false, checks filename then buffer 9isBinary(null, null) // returns null 10 11getEncoding(aBuffer) // returns 'binary' if it contained non-utf8 characters, otherwise returns 'utf8'
Install
npm
- Install:
npm install --save istextorbinary
- Import:
import * as pkg from ('istextorbinary')
- Require:
const pkg = require('istextorbinary')
Deno
1import * as pkg from 'https://unpkg.com/istextorbinary@^9.5.0/edition-deno/index.ts'
Skypack
1<script type="module"> 2 import * as pkg from '//cdn.skypack.dev/istextorbinary@^9.5.0' 3</script>
unpkg
1<script type="module"> 2 import * as pkg from '//unpkg.com/istextorbinary@^9.5.0' 3</script>
jspm
1<script type="module"> 2 import * as pkg from '//dev.jspm.io/istextorbinary@9.5.0' 3</script>
Editions
This package is published with the following editions:
istextorbinary
aliasesistextorbinary/index.cjs
which uses the Editions Autoloader to automatically select the correct edition for the consumer's environmentistextorbinary/source/index.ts
is TypeScript source code with Import for modulesistextorbinary/edition-browsers/index.js
is TypeScript compiled against ES2022 for web browsers with Import for modulesistextorbinary/edition-es2022/index.js
is TypeScript compiled against ES2022 for Node.js 14 || 16 || 18 || 20 || 21 with Require for modulesistextorbinary/edition-es2017/index.js
is TypeScript compiled against ES2017 for Node.js 6 || 8 || 10 || 12 || 14 || 16 || 18 || 20 || 21 with Require for modulesistextorbinary/edition-es5/index.js
is TypeScript compiled against ES5 for Node.js 4 || 6 || 8 || 10 || 12 || 14 || 16 || 18 || 20 || 21 with Require for modulesistextorbinary/edition-es2017-esm/index.js
is TypeScript compiled against ES2017 for Node.js 12 || 14 || 16 || 18 || 20 || 21 with Import for modulesistextorbinary/edition-types/index.d.ts
is TypeScript compiled Types with Import for modulesistextorbinary/edition-deno/index.ts
is TypeScript source code made to be compatible with Deno
History
Discover the release history by heading on over to the HISTORY.md
file.
Backers
Code
Discover how to contribute via the CONTRIBUTING.md
file.
Authors
- Benjamin Lupton — Accelerating collaborative wisdom.
Maintainers
- Benjamin Lupton — Accelerating collaborative wisdom.
- Michael Duane Mooring — We are the space generation; and if you don't know, https://www.spaceforce.mil https://www.virgingalactic.com https://www.spacex.com now you know.
- Rob Loach
Contributors
- Benjamin Lupton — view contributions
- Ian Sibner — view contributions
- Kukhyeon Heo — view contributions
- Michael Duane Mooring — view contributions
- Rob Loach — view contributions
- Sean — view contributions
- shinnn — view contributions
Finances
Sponsors
- Andrew Nesbitt — Software engineer and researcher
- Balsa — We're Balsa, and we're building tools for builders.
- Codecov — Empower developers with tools to improve code quality and testing.
- Poonacha Medappa
- Rob Morris
- Sentry — Real-time crash reporting for your web apps, mobile apps, and games.
- Syntax — Syntax Podcast
Donors
- Andrew Nesbitt
- Armen Mkrtchian
- Balsa
- Chad
- Codecov
- dr.dimitru
- Elliott Ditman
- entroniq
- GitHub
- Hunter Beast
- Jean-Luc Geering
- Michael Duane Mooring
- Michael Harry Scepaniak
- Mohammed Shah
- Mr. Henry
- Nermal
- Pleo
- Poonacha Medappa
- Rob Morris
- Robert de Forest
- Sentry
- ServieJS
- Skunk Team
- Syntax
- WriterJohnBuck
License
Unless stated otherwise all works are:
- Copyright © Benjamin Lupton
and licensed under:
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
security policy file detected
Details
- Info: security policy file detected: SECURITY.md:1
- Info: Found linked content: SECURITY.md:1
- Info: Found disclosure, vulnerability, and/or timelines in security policy: SECURITY.md:1
- Info: Found text in security policy: SECURITY.md:1
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
- Info: project has a license file: LICENSE.md:0
- Warn: project license file does not contain an FSF or OSI license.
Reason
2 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
Reason
SAST tool is not run on all commits -- score normalized to 5
Details
- Warn: 7 commits out of 13 are checked with a SAST tool
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
Found 1/18 approved changesets -- score normalized to 0
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/bevry.yml:21: update your workflow using https://app.stepsecurity.io/secureworkflow/bevry/istextorbinary/bevry.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/bevry.yml:23: update your workflow using https://app.stepsecurity.io/secureworkflow/bevry/istextorbinary/bevry.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/bevry.yml:27: update your workflow using https://app.stepsecurity.io/secureworkflow/bevry/istextorbinary/bevry.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/bevry.yml:39: update your workflow using https://app.stepsecurity.io/secureworkflow/bevry/istextorbinary/bevry.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/bevry.yml:52: update your workflow using https://app.stepsecurity.io/secureworkflow/bevry/istextorbinary/bevry.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/bevry.yml:54: update your workflow using https://app.stepsecurity.io/secureworkflow/bevry/istextorbinary/bevry.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/bevry.yml:58: update your workflow using https://app.stepsecurity.io/secureworkflow/bevry/istextorbinary/bevry.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/bevry.yml:69: update your workflow using https://app.stepsecurity.io/secureworkflow/bevry/istextorbinary/bevry.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/bevry.yml:74: update your workflow using https://app.stepsecurity.io/secureworkflow/bevry/istextorbinary/bevry.yml/master?enable=pin
- Info: 0 out of 5 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 4 third-party GitHubAction dependencies pinned
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: jobLevel 'contents' permission set to 'write': .github/workflows/bevry.yml:80
- Warn: no topLevel permission defined: .github/workflows/bevry.yml:1
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Score
4.2
/10
Last Scanned on 2025-01-13
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More