Skip to content
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

Add the ability to register third-party extensions #399

Closed
wants to merge 3 commits into from

Conversation

Borewit
Copy link
Collaborator

@Borewit Borewit commented Sep 20, 2020

FileType.addDetection(detection)

Register a detection function. This function fill be called after the file-type, and other detections completed.
the detection(tokenizer, fileType) method has two arguments:

  • tokenizer
  • fileType, which is undefined or the return value of a detected file type.

Resolves #340.

Issues:

  • Not possible to overrule existing file-type detection

@Borewit Borewit self-assigned this Sep 20, 2020
@Borewit Borewit added API change Major change, dependents may need to update their code enhancement Add new functionality and removed API change Major change, dependents may need to update their code labels Sep 20, 2020
@Borewit Borewit changed the title Implement ability to extend file-type with 3rd party extensions. Enhancement to register 3rd party extensions Sep 20, 2020
@@ -268,6 +268,16 @@ Returns a set of supported file extensions.

Returns a set of supported MIME types.

### FileType.addDetection(detection)

Register a detection function. This function will be called after `file-type` and other detections complete.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What other detections?

Copy link

@jonyyz jonyyz Oct 26, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Proprietary software file extensions that are not well known. The use case would be a file that could be standard MIME type or proprietary/used-defined MIME types

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or even plain text files that the users will be willing to support. Say, there's not enough confidence to be able to detect an svg or HTML to include it in the core, but the users could accept that and provide their own detection which will be enough for their use case.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's the whole point of this PR. That's not what I'm asking. I'm saying that "other detections" is not clear enough in the docs.

Copy link

@jonyyz jonyyz Nov 24, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think "other" is probably intended to leave it open for extension. Examples could be provided but are probably not going to be exhaustive. Perhaps what you're asking for is some examples? That was the point of my comment and others.

- [tokenizer](https://github.com/Borewit/strtok3#tokenizer)
- `fileType`, which is `undefined` or the return value of a detected file type.

If the detector returns `undefined`, the `tokenizer.position` should be 0. That allows other detectors to parse the file.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why cannot file-type reset the position automatically?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because underlying source can be a stream. A stream can only be read once.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then it should be made clear that the tokenizer.position should be 0 should not be done if it's a stream.


type Detection = (tokenizer: ITokenizer, fileType? : core.FileTypeResult) => Promise<core.FileTypeResult | undefined>;

function addDetection(detection: Detection): void;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs the same docs as the readme.

@sindresorhus sindresorhus changed the title Enhancement to register 3rd party extensions Add the ability to register third-party extensions Sep 28, 2020
@Borewit
Copy link
Collaborator Author

Borewit commented Nov 26, 2020

Sorry I am a bit unresponsive this time. I very busy at the moment. Thanks for all your feedback.

Base automatically changed from master to main January 23, 2021 11:11
@sindresorhus
Copy link
Owner

Gentle bump in case you have time and just forgot about this. If you're still busy, feel free to ignore.

@Borewit Borewit mentioned this pull request Jul 5, 2021
@sindresorhus
Copy link
Owner

I'm going to close this as it has been open for a very long time without any activity. A new PR can be opened if when/if you get back to this ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Add new functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Extending file-type
4 participants