-
-
Notifications
You must be signed in to change notification settings - Fork 387
Extend file type (updated) #603
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
sindresorhus
merged 36 commits into
sindresorhus:main
from
FredrikSchaefer:extend-file-type-updated
Nov 10, 2023
Merged
Changes from all commits
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
bda3f46
Allow specification of custom detectors + readme update
6007eff
Simplify logic in runCustomDetectors
c3dba6e
add custom detectors to fileTypeFromStream
fab97ae
fix linting issue
c7c3190
Execute custom detectors before default ones
4bcddff
add tests
733bfac
fix docs
37e1e57
compatibility with Node.js 14 and 16
bfd18b1
Remove blank space
FredrikSchaefer ee4cb2c
Wrap custom detectors into file type options
ad6d44f
Merge branch 'extend-file-type-updated' of github.com:FredrikSchaefer…
29930bf
Adjust fileTypeFromFile(...) to recent changes
FredrikSchaefer 7ea6efd
Moved custom detectors from function to constructor argument
748ffee
fix fileTypeStream (add back fileTypeOptions)
2adec69
Update documentation
0d1464c
add check for illegal tokenizer position change
6b6188c
Update core.d.ts
FredrikSchaefer 6806753
Update core.d.ts
FredrikSchaefer 61e052e
Update readme.md (move custom detectors section as suggested by revie…
eed198d
Remove fileType prefix from class member functions
ff84f3e
Make runCustomDetectors private
326ccd1
Add class based approach to fileTypeStream
011fa53
Change error handling for read operations of custom detectors
b346f7c
Remove obsolete @throws from documentation
9e24ed9
Make usage of FileTypeParser class consistent
a926bf2
Rename stream(...) to toDetectingStream(...)
5e2a0fd
Fix error handling
f38565d
Suggested changes to simplify code
Borewit e25c294
Merge pull request #2 from sindresorhus/extend-file-type-updated-sugg…
FredrikSchaefer 080ac75
Fix TypeScript declaration
de706c5
Remove comments from unit tests and redundant empty line
Borewit 331502d
Make code examples executable.
Borewit 9d85f05
Remove empty comment lines
Borewit ede94d9
Remove unused `fileTypeOptions` parameter from typings
Borewit ca6e449
Adjust number code and comment style suggestions
Borewit a50e37a
Update core.d.ts
sindresorhus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
UNICORN FILE CONTENT |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -189,6 +189,10 @@ console.log(await fileTypeFromBlob(blob)); | |||||||||
//=> {ext: 'txt', mime: 'plain/text'} | ||||||||||
``` | ||||||||||
|
||||||||||
#### blob | ||||||||||
|
||||||||||
Type: [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob) | ||||||||||
|
||||||||||
### fileTypeFromTokenizer(tokenizer) | ||||||||||
|
||||||||||
Detect the file type from an `ITokenizer` source. | ||||||||||
|
@@ -305,6 +309,48 @@ Returns a `Set<string>` of supported file extensions. | |||||||||
|
||||||||||
Returns a `Set<string>` of supported MIME types. | ||||||||||
|
||||||||||
## Custom detectors | ||||||||||
|
||||||||||
A custom detector is a function that allows specifying custom detection mechanisms. | ||||||||||
|
||||||||||
An iterable of detectors can be provided via the `fileTypeOptions` argument for the `FileTypeParser.constructor`. | ||||||||||
|
||||||||||
The detectors are called before the default detections in the provided order. | ||||||||||
|
||||||||||
Custom detectors can be used to add new `FileTypeResults` or to modify return behaviour of existing FileTypeResult detections. | ||||||||||
|
||||||||||
If the detector returns `undefined`, there are 2 possible scenarios: | ||||||||||
|
||||||||||
1. The detector has not read from the tokenizer, it will be proceeded with the next available detector. | ||||||||||
2. The detector has read from the tokenizer (`tokenizer.position` has been increased). | ||||||||||
In that case no further detectors will be executed and the final conclusion is that file-type returns undefined. | ||||||||||
Note that this an exceptional scenario, as the detector takes the opportunity from any other detector to determine the file type. | ||||||||||
Comment on lines
+326
to
+327
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
|
||||||||||
|
||||||||||
Example detector array which can be extended and provided to each public method via the `fileTypeOptions` argument: | ||||||||||
```js | ||||||||||
import {FileTypeParser} from 'file-type'; | ||||||||||
|
||||||||||
const customDetectors = [ | ||||||||||
async tokenizer => { | ||||||||||
const unicornHeader = [85, 78, 73, 67, 79, 82, 78]; // "UNICORN" as decimal string | ||||||||||
const buffer = Buffer.alloc(7); | ||||||||||
await tokenizer.peekBuffer(buffer, {length: unicornHeader.length, mayBeLess: true}); | ||||||||||
|
||||||||||
if (unicornHeader.every((value, index) => value === buffer[index])) { | ||||||||||
return {ext: 'unicorn', mime: 'application/unicorn'}; | ||||||||||
} | ||||||||||
Borewit marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
|
||||||||||
return undefined; | ||||||||||
}, | ||||||||||
]; | ||||||||||
|
||||||||||
const buffer = Buffer.from("UNICORN"); | ||||||||||
const parser = new FileTypeParser({customDetectors}); | ||||||||||
const fileType = await parser.fromBuffer(buffer); | ||||||||||
console.log(fileType); | ||||||||||
``` | ||||||||||
|
||||||||||
## Supported file types | ||||||||||
|
||||||||||
- [`3g2`](https://en.wikipedia.org/wiki/3GP_and_3G2#3G2) - Multimedia container format defined by the 3GPP2 for 3G CDMA2000 multimedia services | ||||||||||
|
@@ -469,6 +515,20 @@ The following file types will not be accepted: | |||||||||
- `.csv` - [Reason.](https://github.com/sindresorhus/file-type/issues/264#issuecomment-568439196) | ||||||||||
- `.svg` - Detecting it requires a full-blown parser. Check out [`is-svg`](https://github.com/sindresorhus/is-svg) for something that mostly works. | ||||||||||
|
||||||||||
#### tokenizer | ||||||||||
|
||||||||||
Type: [`ITokenizer`](https://github.com/Borewit/strtok3#tokenizer) | ||||||||||
|
||||||||||
Usable as source of the examined file. | ||||||||||
|
||||||||||
#### fileType | ||||||||||
|
||||||||||
Type: FileTypeResult | ||||||||||
|
||||||||||
Object having an `ext` (extension) and `mime` (mime type) property. | ||||||||||
|
||||||||||
Detected by the standard detections or a previous custom detection. Undefined if no matching fileTypeResult could be found. | ||||||||||
|
||||||||||
## Related | ||||||||||
|
||||||||||
- [file-type-cli](https://github.com/sindresorhus/file-type-cli) - CLI for this module | ||||||||||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.