file-type 20.0.1 → 20.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/core.d.ts +20 -5
- package/core.js +41 -32
- package/package.json +3 -2
- package/readme.md +23 -7
- package/supported.js +2 -0
package/core.d.ts
CHANGED
|
@@ -116,13 +116,28 @@ export declare function fileTypeFromBlob(blob: Blob): Promise<FileTypeResult | u
|
|
|
116
116
|
/**
|
|
117
117
|
A custom file type detector.
|
|
118
118
|
|
|
119
|
+
Custom file type detectors are plugins designed to extend the default detection capabilities.
|
|
120
|
+
They allow support for uncommon file types, non-binary formats, or customized detection behavior.
|
|
121
|
+
|
|
122
|
+
Detectors can be added via the constructor options or by modifying `FileTypeParser#detectors` directly.
|
|
123
|
+
Detectors provided through the constructor are executed before the default ones.
|
|
124
|
+
|
|
119
125
|
Detectors can be added via the constructor options or by directly modifying `FileTypeParser#detectors`.
|
|
120
126
|
|
|
121
|
-
|
|
127
|
+
### Example adding a detector
|
|
128
|
+
|
|
129
|
+
```js
|
|
130
|
+
import {FileTypeParser} from 'file-type';
|
|
131
|
+
import {detectXml} from '@file-type/xml';
|
|
132
|
+
|
|
133
|
+
const parser = new FileTypeParser({customDetectors: [detectXml]});
|
|
134
|
+
const fileType = await parser.fromFile('sample.kml');
|
|
135
|
+
console.log(fileType);
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
### Available-third party file-type detectors
|
|
122
139
|
|
|
123
|
-
|
|
124
|
-
- Introducing new `FileTypeResult` entries.
|
|
125
|
-
- Modifying the detection behavior of existing `FileTypeResult` types.
|
|
140
|
+
- [@file-type/xml](https://github.com/Borewit/file-type-xml): Detects common XML file types, such as GLM, KML, MusicXML, RSS, SVG, and XHTML
|
|
126
141
|
|
|
127
142
|
### Detector execution flow
|
|
128
143
|
|
|
@@ -131,7 +146,7 @@ If a detector returns `undefined`, the following rules apply:
|
|
|
131
146
|
1. **No Tokenizer Interaction**: If the detector does not modify the tokenizer's position, the next detector in the sequence is executed.
|
|
132
147
|
2. **Tokenizer Interaction**: If the detector modifies the tokenizer's position (`tokenizer.position` is advanced), no further detectors are executed. In this case, the file type remains `undefined`, as subsequent detectors cannot evaluate the content. This is an exceptional scenario, as it prevents any other detectors from determining the file type.
|
|
133
148
|
|
|
134
|
-
### Example
|
|
149
|
+
### Example writing a custom detector
|
|
135
150
|
|
|
136
151
|
Below is an example of a custom detector array. This can be passed to the `FileTypeParser` via the `fileTypeOptions` argument.
|
|
137
152
|
|
package/core.js
CHANGED
|
@@ -476,13 +476,6 @@ export class FileTypeParser {
|
|
|
476
476
|
};
|
|
477
477
|
}
|
|
478
478
|
|
|
479
|
-
if (this.checkString('WEBP', {offset: 8})) {
|
|
480
|
-
return {
|
|
481
|
-
ext: 'webp',
|
|
482
|
-
mime: 'image/webp',
|
|
483
|
-
};
|
|
484
|
-
}
|
|
485
|
-
|
|
486
479
|
// Musepack, SV8
|
|
487
480
|
if (this.checkString('MPCK')) {
|
|
488
481
|
return {
|
|
@@ -851,31 +844,6 @@ export class FileTypeParser {
|
|
|
851
844
|
}
|
|
852
845
|
}
|
|
853
846
|
|
|
854
|
-
// RIFF file format which might be AVI, WAV, QCP, etc
|
|
855
|
-
if (this.check([0x52, 0x49, 0x46, 0x46])) {
|
|
856
|
-
if (this.check([0x41, 0x56, 0x49], {offset: 8})) {
|
|
857
|
-
return {
|
|
858
|
-
ext: 'avi',
|
|
859
|
-
mime: 'video/vnd.avi',
|
|
860
|
-
};
|
|
861
|
-
}
|
|
862
|
-
|
|
863
|
-
if (this.check([0x57, 0x41, 0x56, 0x45], {offset: 8})) {
|
|
864
|
-
return {
|
|
865
|
-
ext: 'wav',
|
|
866
|
-
mime: 'audio/wav',
|
|
867
|
-
};
|
|
868
|
-
}
|
|
869
|
-
|
|
870
|
-
// QLCM, QCP file
|
|
871
|
-
if (this.check([0x51, 0x4C, 0x43, 0x4D], {offset: 8})) {
|
|
872
|
-
return {
|
|
873
|
-
ext: 'qcp',
|
|
874
|
-
mime: 'audio/qcelp',
|
|
875
|
-
};
|
|
876
|
-
}
|
|
877
|
-
}
|
|
878
|
-
|
|
879
847
|
if (this.checkString('SQLi')) {
|
|
880
848
|
return {
|
|
881
849
|
ext: 'sqlite',
|
|
@@ -1053,6 +1021,15 @@ export class FileTypeParser {
|
|
|
1053
1021
|
};
|
|
1054
1022
|
}
|
|
1055
1023
|
|
|
1024
|
+
if (this.checkString('.RMF')) {
|
|
1025
|
+
return {
|
|
1026
|
+
ext: 'rm',
|
|
1027
|
+
mime: 'application/vnd.rn-realmedia',
|
|
1028
|
+
};
|
|
1029
|
+
}
|
|
1030
|
+
|
|
1031
|
+
// -- 5-byte signatures --
|
|
1032
|
+
|
|
1056
1033
|
if (this.checkString('DRACO')) {
|
|
1057
1034
|
return {
|
|
1058
1035
|
ext: 'drc',
|
|
@@ -1307,6 +1284,38 @@ export class FileTypeParser {
|
|
|
1307
1284
|
|
|
1308
1285
|
// -- 12-byte signatures --
|
|
1309
1286
|
|
|
1287
|
+
// RIFF file format which might be AVI, WAV, QCP, etc
|
|
1288
|
+
if (this.check([0x52, 0x49, 0x46, 0x46])) {
|
|
1289
|
+
if (this.checkString('WEBP', {offset: 8})) {
|
|
1290
|
+
return {
|
|
1291
|
+
ext: 'webp',
|
|
1292
|
+
mime: 'image/webp',
|
|
1293
|
+
};
|
|
1294
|
+
}
|
|
1295
|
+
|
|
1296
|
+
if (this.check([0x41, 0x56, 0x49], {offset: 8})) {
|
|
1297
|
+
return {
|
|
1298
|
+
ext: 'avi',
|
|
1299
|
+
mime: 'video/vnd.avi',
|
|
1300
|
+
};
|
|
1301
|
+
}
|
|
1302
|
+
|
|
1303
|
+
if (this.check([0x57, 0x41, 0x56, 0x45], {offset: 8})) {
|
|
1304
|
+
return {
|
|
1305
|
+
ext: 'wav',
|
|
1306
|
+
mime: 'audio/wav',
|
|
1307
|
+
};
|
|
1308
|
+
}
|
|
1309
|
+
|
|
1310
|
+
// QLCM, QCP file
|
|
1311
|
+
if (this.check([0x51, 0x4C, 0x43, 0x4D], {offset: 8})) {
|
|
1312
|
+
return {
|
|
1313
|
+
ext: 'qcp',
|
|
1314
|
+
mime: 'audio/qcelp',
|
|
1315
|
+
};
|
|
1316
|
+
}
|
|
1317
|
+
}
|
|
1318
|
+
|
|
1310
1319
|
if (this.check([0x49, 0x49, 0x55, 0x00, 0x18, 0x00, 0x00, 0x00, 0x88, 0xE7, 0x74, 0xD8])) {
|
|
1311
1320
|
return {
|
|
1312
1321
|
ext: 'rw2',
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "file-type",
|
|
3
|
-
"version": "20.0
|
|
3
|
+
"version": "20.2.0",
|
|
4
4
|
"description": "Detect the file type of a file, stream, or data",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": "sindresorhus/file-type",
|
|
@@ -234,7 +234,8 @@
|
|
|
234
234
|
"dotm",
|
|
235
235
|
"potm",
|
|
236
236
|
"pptm",
|
|
237
|
-
"jar"
|
|
237
|
+
"jar",
|
|
238
|
+
"rm"
|
|
238
239
|
],
|
|
239
240
|
"dependencies": {
|
|
240
241
|
"@tokenizer/inflate": "^0.2.6",
|
package/readme.md
CHANGED
|
@@ -20,6 +20,8 @@ npm install file-type
|
|
|
20
20
|
|
|
21
21
|
If you use it with Webpack, you need the latest Webpack version and ensure you configure it correctly for ESM.
|
|
22
22
|
|
|
23
|
+
File type detection is based on binary signatures (magic numbers) and should be treated as a best-effort hint, not a guarantee.
|
|
24
|
+
|
|
23
25
|
## Usage
|
|
24
26
|
|
|
25
27
|
### Node.js
|
|
@@ -341,15 +343,28 @@ Returns a `Set<string>` of supported MIME types.
|
|
|
341
343
|
|
|
342
344
|
## Custom detectors
|
|
343
345
|
|
|
344
|
-
|
|
346
|
+
Custom file type detectors are plugins designed to extend the default detection capabilities.
|
|
347
|
+
They allow support for uncommon file types, non-binary formats, or customized detection behavior.
|
|
348
|
+
|
|
349
|
+
Detectors can be added via the constructor options or by modifying `FileTypeParser#detectors` directly.
|
|
350
|
+
Detectors provided through the constructor are executed before the default ones.
|
|
345
351
|
|
|
346
352
|
Detectors can be added via the constructor options or by directly modifying `FileTypeParser#detectors`.
|
|
347
353
|
|
|
348
|
-
|
|
354
|
+
### Example adding a detector
|
|
355
|
+
|
|
356
|
+
```js
|
|
357
|
+
import {FileTypeParser} from 'file-type';
|
|
358
|
+
import {detectXml} from '@file-type/xml';
|
|
359
|
+
|
|
360
|
+
const parser = new FileTypeParser({customDetectors: [detectXml]});
|
|
361
|
+
const fileType = await parser.fromFile('sample.kml');
|
|
362
|
+
console.log(fileType);
|
|
363
|
+
```
|
|
364
|
+
|
|
365
|
+
### Available third-party file-type detectors
|
|
349
366
|
|
|
350
|
-
|
|
351
|
-
- Introducing new `FileTypeResult` entries.
|
|
352
|
-
- Modifying the detection behavior of existing `FileTypeResult` types.
|
|
367
|
+
- [@file-type/xml](https://github.com/Borewit/file-type-xml): Detects common XML file types, such as GLM, KML, MusicXML, RSS, SVG, and XHTML
|
|
353
368
|
|
|
354
369
|
### Detector execution flow
|
|
355
370
|
|
|
@@ -358,7 +373,7 @@ If a detector returns `undefined`, the following rules apply:
|
|
|
358
373
|
1. **No Tokenizer Interaction**: If the detector does not modify the tokenizer's position, the next detector in the sequence is executed.
|
|
359
374
|
2. **Tokenizer Interaction**: If the detector modifies the tokenizer's position (`tokenizer.position` is advanced), no further detectors are executed. In this case, the file type remains `undefined`, as subsequent detectors cannot evaluate the content. This is an exceptional scenario, as it prevents any other detectors from determining the file type.
|
|
360
375
|
|
|
361
|
-
###
|
|
376
|
+
### Writing your own custom detector
|
|
362
377
|
|
|
363
378
|
Below is an example of a custom detector array. This can be passed to the `FileTypeParser` via the `fileTypeOptions` argument.
|
|
364
379
|
|
|
@@ -550,6 +565,7 @@ abortController.abort(); // Abort file-type reading from the Blob stream.
|
|
|
550
565
|
- [`qcp`](https://en.wikipedia.org/wiki/QCP) - Tagged and chunked data
|
|
551
566
|
- [`raf`](https://en.wikipedia.org/wiki/Raw_image_format) - Fujifilm RAW image file
|
|
552
567
|
- [`rar`](https://en.wikipedia.org/wiki/RAR_(file_format)) - Archive file
|
|
568
|
+
- [`rm`](https://en.wikipedia.org/wiki/RealMedia) - RealMedia
|
|
553
569
|
- [`rpm`](https://fileinfo.com/extension/rpm) - Red Hat Package Manager file
|
|
554
570
|
- [`rtf`](https://en.wikipedia.org/wiki/Rich_Text_Format) - Rich Text Format
|
|
555
571
|
- [`rw2`](https://en.wikipedia.org/wiki/Raw_image_format) - Panasonic RAW image file
|
|
@@ -595,7 +611,7 @@ The following file types will not be accepted:
|
|
|
595
611
|
- `.ppt` - Microsoft PowerPoint97-2003 Document
|
|
596
612
|
- `.msi` - Microsoft Windows Installer
|
|
597
613
|
- `.csv` - [Reason.](https://github.com/sindresorhus/file-type/issues/264#issuecomment-568439196)
|
|
598
|
-
- `.svg` -
|
|
614
|
+
- `.svg` - Supported by [third-party detector](#available-third-party-file-type-detectors).
|
|
599
615
|
|
|
600
616
|
#### tokenizer
|
|
601
617
|
|
package/supported.js
CHANGED
|
@@ -171,6 +171,7 @@ export const extensions = [
|
|
|
171
171
|
'potm',
|
|
172
172
|
'pptm',
|
|
173
173
|
'jar',
|
|
174
|
+
'rm',
|
|
174
175
|
];
|
|
175
176
|
|
|
176
177
|
export const mimeTypes = [
|
|
@@ -341,4 +342,5 @@ export const mimeTypes = [
|
|
|
341
342
|
'application/vnd.ms-powerpoint.template.macroEnabled.12',
|
|
342
343
|
'application/vnd.ms-powerpoint.presentation.macroEnabled.12',
|
|
343
344
|
'application/java-archive',
|
|
345
|
+
'application/vnd.rn-realmedia',
|
|
344
346
|
];
|