Skip to content

Commit d89a4a1

Browse files
Improve signature ordering detection requirements (#518)
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
1 parent 1a553e7 commit d89a4a1

File tree

2 files changed

+19
-15
lines changed

2 files changed

+19
-15
lines changed

Diff for: .github/pull_request_template.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ If you're adding support for a new file type, please follow the below steps:
44
- Add a fixture file named `fixture.<extension>` to the `fixture` directory.
55
- Add the file extension to the `extensions` array in `supported.js`.
66
- Add the file's MIME type to the `types` array in `supported.js`.
7-
- Add the file type detection logic to the `core.js` file.
7+
- Add the file type detection logic to the `core.js` file
8+
- Respect the sequence:
9+
- Signature with shorter sample size (counted from offset 0 until the last required byte position) will be executed first.
10+
- Only the initial determination for the file type counts for the sequence.
11+
- 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).
812
- Add the file extension to the `FileType` type in `core.d.ts`.
913
- Add the file's MIME type to the `MimeType` type in `core.d.ts`.
1014
- 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```

Diff for: core.js

+14-14
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,13 @@ class FileTypeParser {
151151

152152
// -- 3-byte signatures --
153153

154+
if (this.check([0x47, 0x49, 0x46])) {
155+
return {
156+
ext: 'gif',
157+
mime: 'image/gif',
158+
};
159+
}
160+
154161
if (this.check([0xFF, 0xD8, 0xFF])) {
155162
return {
156163
ext: 'jpg',
@@ -214,20 +221,6 @@ class FileTypeParser {
214221

215222
// -- 4-byte signatures --
216223

217-
if (this.check([0x7F, 0x45, 0x4C, 0x46])) {
218-
return {
219-
ext: 'elf',
220-
mime: 'application/x-elf',
221-
};
222-
}
223-
224-
if (this.check([0x47, 0x49, 0x46])) {
225-
return {
226-
ext: 'gif',
227-
mime: 'image/gif',
228-
};
229-
}
230-
231224
if (this.checkString('FLIF')) {
232225
return {
233226
ext: 'flif',
@@ -798,6 +791,13 @@ class FileTypeParser {
798791
};
799792
}
800793

794+
if (this.check([0x7F, 0x45, 0x4C, 0x46])) {
795+
return {
796+
ext: 'elf',
797+
mime: 'application/x-elf',
798+
};
799+
}
800+
801801
// -- 5-byte signatures --
802802

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

0 commit comments

Comments
 (0)