Skip to content

Commit

Permalink
Improve signature ordering detection requirements (#518)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
Borewit and sindresorhus authored Dec 28, 2021
1 parent 1a553e7 commit d89a4a1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
6 changes: 5 additions & 1 deletion .github/pull_request_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ If you're adding support for a new file type, please follow the below steps:
- Add a fixture file named `fixture.<extension>` to the `fixture` directory.
- Add the file extension to the `extensions` array in `supported.js`.
- Add the file's MIME type to the `types` array in `supported.js`.
- Add the file type detection logic to the `core.js` file.
- Add the file type detection logic to the `core.js` file
- Respect the sequence:
- Signature with shorter sample size (counted from offset 0 until the last required byte position) will be executed first.
- Only the initial determination for the file type counts for the sequence.
- Existing signatures requiring same sample length (same *signature group*) will be tested prior to your new detections. Yours will be last. (rational: common formats first).
- Add the file extension to the `FileType` type in `core.d.ts`.
- Add the file's MIME type to the `MimeType` type in `core.d.ts`.
- Add the file extension to the `Supported file types` section in the readme, in the format ```- [`<extension>`](URL) - Format name```, for example, ```- [`png`](https://en.wikipedia.org/wiki/Portable_Network_Graphics) - Portable Network Graphics```
Expand Down
28 changes: 14 additions & 14 deletions core.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,13 @@ class FileTypeParser {

// -- 3-byte signatures --

if (this.check([0x47, 0x49, 0x46])) {
return {
ext: 'gif',
mime: 'image/gif',
};
}

if (this.check([0xFF, 0xD8, 0xFF])) {
return {
ext: 'jpg',
Expand Down Expand Up @@ -214,20 +221,6 @@ class FileTypeParser {

// -- 4-byte signatures --

if (this.check([0x7F, 0x45, 0x4C, 0x46])) {
return {
ext: 'elf',
mime: 'application/x-elf',
};
}

if (this.check([0x47, 0x49, 0x46])) {
return {
ext: 'gif',
mime: 'image/gif',
};
}

if (this.checkString('FLIF')) {
return {
ext: 'flif',
Expand Down Expand Up @@ -798,6 +791,13 @@ class FileTypeParser {
};
}

if (this.check([0x7F, 0x45, 0x4C, 0x46])) {
return {
ext: 'elf',
mime: 'application/x-elf',
};
}

// -- 5-byte signatures --

if (this.check([0x4F, 0x54, 0x54, 0x4F, 0x00])) {
Expand Down

0 comments on commit d89a4a1

Please sign in to comment.