file-type 16.5.4 → 21.3.4
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 +221 -354
- package/core.js +2599 -1165
- package/index.d.ts +89 -18
- package/index.js +154 -23
- package/package.json +96 -21
- package/readme.md +467 -270
- package/supported.js +359 -278
- package/util.js +36 -16
- package/browser.d.ts +0 -50
- package/browser.js +0 -49
package/core.d.ts
CHANGED
|
@@ -1,386 +1,253 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
declare namespace core {
|
|
6
|
-
type FileExtension =
|
|
7
|
-
| 'jpg'
|
|
8
|
-
| 'png'
|
|
9
|
-
| 'apng'
|
|
10
|
-
| 'gif'
|
|
11
|
-
| 'webp'
|
|
12
|
-
| 'flif'
|
|
13
|
-
| 'xcf'
|
|
14
|
-
| 'cr2'
|
|
15
|
-
| 'cr3'
|
|
16
|
-
| 'orf'
|
|
17
|
-
| 'arw'
|
|
18
|
-
| 'dng'
|
|
19
|
-
| 'nef'
|
|
20
|
-
| 'rw2'
|
|
21
|
-
| 'raf'
|
|
22
|
-
| 'tif'
|
|
23
|
-
| 'bmp'
|
|
24
|
-
| 'icns'
|
|
25
|
-
| 'jxr'
|
|
26
|
-
| 'psd'
|
|
27
|
-
| 'indd'
|
|
28
|
-
| 'zip'
|
|
29
|
-
| 'tar'
|
|
30
|
-
| 'rar'
|
|
31
|
-
| 'gz'
|
|
32
|
-
| 'bz2'
|
|
33
|
-
| '7z'
|
|
34
|
-
| 'dmg'
|
|
35
|
-
| 'mp4'
|
|
36
|
-
| 'mid'
|
|
37
|
-
| 'mkv'
|
|
38
|
-
| 'webm'
|
|
39
|
-
| 'mov'
|
|
40
|
-
| 'avi'
|
|
41
|
-
| 'mpg'
|
|
42
|
-
| 'mp2'
|
|
43
|
-
| 'mp3'
|
|
44
|
-
| 'm4a'
|
|
45
|
-
| 'ogg'
|
|
46
|
-
| 'opus'
|
|
47
|
-
| 'flac'
|
|
48
|
-
| 'wav'
|
|
49
|
-
| 'qcp'
|
|
50
|
-
| 'amr'
|
|
51
|
-
| 'pdf'
|
|
52
|
-
| 'epub'
|
|
53
|
-
| 'mobi'
|
|
54
|
-
| 'exe'
|
|
55
|
-
| 'swf'
|
|
56
|
-
| 'rtf'
|
|
57
|
-
| 'woff'
|
|
58
|
-
| 'woff2'
|
|
59
|
-
| 'eot'
|
|
60
|
-
| 'ttf'
|
|
61
|
-
| 'otf'
|
|
62
|
-
| 'ico'
|
|
63
|
-
| 'flv'
|
|
64
|
-
| 'ps'
|
|
65
|
-
| 'xz'
|
|
66
|
-
| 'sqlite'
|
|
67
|
-
| 'nes'
|
|
68
|
-
| 'crx'
|
|
69
|
-
| 'xpi'
|
|
70
|
-
| 'cab'
|
|
71
|
-
| 'deb'
|
|
72
|
-
| 'ar'
|
|
73
|
-
| 'rpm'
|
|
74
|
-
| 'Z'
|
|
75
|
-
| 'lz'
|
|
76
|
-
| 'cfb'
|
|
77
|
-
| 'mxf'
|
|
78
|
-
| 'mts'
|
|
79
|
-
| 'wasm'
|
|
80
|
-
| 'blend'
|
|
81
|
-
| 'bpg'
|
|
82
|
-
| 'docx'
|
|
83
|
-
| 'pptx'
|
|
84
|
-
| 'xlsx'
|
|
85
|
-
| '3gp'
|
|
86
|
-
| '3g2'
|
|
87
|
-
| 'jp2'
|
|
88
|
-
| 'jpm'
|
|
89
|
-
| 'jpx'
|
|
90
|
-
| 'mj2'
|
|
91
|
-
| 'aif'
|
|
92
|
-
| 'odt'
|
|
93
|
-
| 'ods'
|
|
94
|
-
| 'odp'
|
|
95
|
-
| 'xml'
|
|
96
|
-
| 'heic'
|
|
97
|
-
| 'cur'
|
|
98
|
-
| 'ktx'
|
|
99
|
-
| 'ape'
|
|
100
|
-
| 'wv'
|
|
101
|
-
| 'asf'
|
|
102
|
-
| 'dcm'
|
|
103
|
-
| 'mpc'
|
|
104
|
-
| 'ics'
|
|
105
|
-
| 'glb'
|
|
106
|
-
| 'pcap'
|
|
107
|
-
| 'dsf'
|
|
108
|
-
| 'lnk'
|
|
109
|
-
| 'alias'
|
|
110
|
-
| 'voc'
|
|
111
|
-
| 'ac3'
|
|
112
|
-
| 'm4b'
|
|
113
|
-
| 'm4p'
|
|
114
|
-
| 'm4v'
|
|
115
|
-
| 'f4a'
|
|
116
|
-
| 'f4b'
|
|
117
|
-
| 'f4p'
|
|
118
|
-
| 'f4v'
|
|
119
|
-
| 'mie'
|
|
120
|
-
| 'ogv'
|
|
121
|
-
| 'ogm'
|
|
122
|
-
| 'oga'
|
|
123
|
-
| 'spx'
|
|
124
|
-
| 'ogx'
|
|
125
|
-
| 'arrow'
|
|
126
|
-
| 'shp'
|
|
127
|
-
| 'aac'
|
|
128
|
-
| 'mp1'
|
|
129
|
-
| 'it'
|
|
130
|
-
| 's3m'
|
|
131
|
-
| 'xm'
|
|
132
|
-
| 'ai'
|
|
133
|
-
| 'skp'
|
|
134
|
-
| 'avif'
|
|
135
|
-
| 'eps'
|
|
136
|
-
| 'lzh'
|
|
137
|
-
| 'pgp'
|
|
138
|
-
| 'asar'
|
|
139
|
-
| 'stl'
|
|
140
|
-
| 'chm'
|
|
141
|
-
| '3mf'
|
|
142
|
-
| 'zst'
|
|
143
|
-
| 'jxl'
|
|
144
|
-
| 'vcf';
|
|
145
|
-
|
|
146
|
-
type MimeType =
|
|
147
|
-
| 'image/jpeg'
|
|
148
|
-
| 'image/png'
|
|
149
|
-
| 'image/gif'
|
|
150
|
-
| 'image/webp'
|
|
151
|
-
| 'image/flif'
|
|
152
|
-
| 'image/x-xcf'
|
|
153
|
-
| 'image/x-canon-cr2'
|
|
154
|
-
| 'image/x-canon-cr3'
|
|
155
|
-
| 'image/tiff'
|
|
156
|
-
| 'image/bmp'
|
|
157
|
-
| 'image/icns'
|
|
158
|
-
| 'image/vnd.ms-photo'
|
|
159
|
-
| 'image/vnd.adobe.photoshop'
|
|
160
|
-
| 'application/x-indesign'
|
|
161
|
-
| 'application/epub+zip'
|
|
162
|
-
| 'application/x-xpinstall'
|
|
163
|
-
| 'application/vnd.oasis.opendocument.text'
|
|
164
|
-
| 'application/vnd.oasis.opendocument.spreadsheet'
|
|
165
|
-
| 'application/vnd.oasis.opendocument.presentation'
|
|
166
|
-
| 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
|
|
167
|
-
| 'application/vnd.openxmlformats-officedocument.presentationml.presentation'
|
|
168
|
-
| 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
|
|
169
|
-
| 'application/zip'
|
|
170
|
-
| 'application/x-tar'
|
|
171
|
-
| 'application/x-rar-compressed'
|
|
172
|
-
| 'application/gzip'
|
|
173
|
-
| 'application/x-bzip2'
|
|
174
|
-
| 'application/x-7z-compressed'
|
|
175
|
-
| 'application/x-apple-diskimage'
|
|
176
|
-
| 'video/mp4'
|
|
177
|
-
| 'audio/midi'
|
|
178
|
-
| 'video/x-matroska'
|
|
179
|
-
| 'video/webm'
|
|
180
|
-
| 'video/quicktime'
|
|
181
|
-
| 'video/vnd.avi'
|
|
182
|
-
| 'audio/vnd.wave'
|
|
183
|
-
| 'audio/qcelp'
|
|
184
|
-
| 'audio/x-ms-asf'
|
|
185
|
-
| 'video/x-ms-asf'
|
|
186
|
-
| 'application/vnd.ms-asf'
|
|
187
|
-
| 'video/mpeg'
|
|
188
|
-
| 'video/3gpp'
|
|
189
|
-
| 'audio/mpeg'
|
|
190
|
-
| 'audio/mp4' // RFC 4337
|
|
191
|
-
| 'audio/opus'
|
|
192
|
-
| 'video/ogg'
|
|
193
|
-
| 'audio/ogg'
|
|
194
|
-
| 'application/ogg'
|
|
195
|
-
| 'audio/x-flac'
|
|
196
|
-
| 'audio/ape'
|
|
197
|
-
| 'audio/wavpack'
|
|
198
|
-
| 'audio/amr'
|
|
199
|
-
| 'application/pdf'
|
|
200
|
-
| 'application/x-msdownload'
|
|
201
|
-
| 'application/x-shockwave-flash'
|
|
202
|
-
| 'application/rtf'
|
|
203
|
-
| 'application/wasm'
|
|
204
|
-
| 'font/woff'
|
|
205
|
-
| 'font/woff2'
|
|
206
|
-
| 'application/vnd.ms-fontobject'
|
|
207
|
-
| 'font/ttf'
|
|
208
|
-
| 'font/otf'
|
|
209
|
-
| 'image/x-icon'
|
|
210
|
-
| 'video/x-flv'
|
|
211
|
-
| 'application/postscript'
|
|
212
|
-
| 'application/eps'
|
|
213
|
-
| 'application/x-xz'
|
|
214
|
-
| 'application/x-sqlite3'
|
|
215
|
-
| 'application/x-nintendo-nes-rom'
|
|
216
|
-
| 'application/x-google-chrome-extension'
|
|
217
|
-
| 'application/vnd.ms-cab-compressed'
|
|
218
|
-
| 'application/x-deb'
|
|
219
|
-
| 'application/x-unix-archive'
|
|
220
|
-
| 'application/x-rpm'
|
|
221
|
-
| 'application/x-compress'
|
|
222
|
-
| 'application/x-lzip'
|
|
223
|
-
| 'application/x-cfb'
|
|
224
|
-
| 'application/x-mie'
|
|
225
|
-
| 'application/x-apache-arrow'
|
|
226
|
-
| 'application/mxf'
|
|
227
|
-
| 'video/mp2t'
|
|
228
|
-
| 'application/x-blender'
|
|
229
|
-
| 'image/bpg'
|
|
230
|
-
| 'image/jp2'
|
|
231
|
-
| 'image/jpx'
|
|
232
|
-
| 'image/jpm'
|
|
233
|
-
| 'image/mj2'
|
|
234
|
-
| 'audio/aiff'
|
|
235
|
-
| 'application/xml'
|
|
236
|
-
| 'application/x-mobipocket-ebook'
|
|
237
|
-
| 'image/heif'
|
|
238
|
-
| 'image/heif-sequence'
|
|
239
|
-
| 'image/heic'
|
|
240
|
-
| 'image/heic-sequence'
|
|
241
|
-
| 'image/ktx'
|
|
242
|
-
| 'application/dicom'
|
|
243
|
-
| 'audio/x-musepack'
|
|
244
|
-
| 'text/calendar'
|
|
245
|
-
| 'text/vcard'
|
|
246
|
-
| 'model/gltf-binary'
|
|
247
|
-
| 'application/vnd.tcpdump.pcap'
|
|
248
|
-
| 'audio/x-dsf' // Non-standard
|
|
249
|
-
| 'application/x.ms.shortcut' // Invented by us
|
|
250
|
-
| 'application/x.apple.alias' // Invented by us
|
|
251
|
-
| 'audio/x-voc'
|
|
252
|
-
| 'audio/vnd.dolby.dd-raw'
|
|
253
|
-
| 'audio/x-m4a'
|
|
254
|
-
| 'image/apng'
|
|
255
|
-
| 'image/x-olympus-orf'
|
|
256
|
-
| 'image/x-sony-arw'
|
|
257
|
-
| 'image/x-adobe-dng'
|
|
258
|
-
| 'image/x-nikon-nef'
|
|
259
|
-
| 'image/x-panasonic-rw2'
|
|
260
|
-
| 'image/x-fujifilm-raf'
|
|
261
|
-
| 'video/x-m4v'
|
|
262
|
-
| 'video/3gpp2'
|
|
263
|
-
| 'application/x-esri-shape'
|
|
264
|
-
| 'audio/aac'
|
|
265
|
-
| 'audio/x-it'
|
|
266
|
-
| 'audio/x-s3m'
|
|
267
|
-
| 'audio/x-xm'
|
|
268
|
-
| 'video/MP1S'
|
|
269
|
-
| 'video/MP2P'
|
|
270
|
-
| 'application/vnd.sketchup.skp'
|
|
271
|
-
| 'image/avif'
|
|
272
|
-
| 'application/x-lzh-compressed'
|
|
273
|
-
| 'application/pgp-encrypted'
|
|
274
|
-
| 'application/x-asar'
|
|
275
|
-
| 'model/stl'
|
|
276
|
-
| 'application/vnd.ms-htmlhelp'
|
|
277
|
-
| 'model/3mf'
|
|
278
|
-
| 'image/jxl'
|
|
279
|
-
| 'application/zstd';
|
|
280
|
-
|
|
281
|
-
interface FileTypeResult {
|
|
282
|
-
/**
|
|
283
|
-
One of the supported [file types](https://github.com/sindresorhus/file-type#supported-file-types).
|
|
284
|
-
*/
|
|
285
|
-
readonly ext: FileExtension;
|
|
286
|
-
|
|
287
|
-
/**
|
|
288
|
-
The detected [MIME type](https://en.wikipedia.org/wiki/Internet_media_type).
|
|
289
|
-
*/
|
|
290
|
-
readonly mime: MimeType;
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
type ReadableStreamWithFileType = ReadableStream & {
|
|
294
|
-
readonly fileType?: FileTypeResult;
|
|
295
|
-
};
|
|
1
|
+
/**
|
|
2
|
+
Typings for primary entry point, Node.js specific typings can be found in index.d.ts
|
|
3
|
+
*/
|
|
296
4
|
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
The file type is detected by checking the [magic number](https://en.wikipedia.org/wiki/Magic_number_(programming)#Magic_numbers_in_files) of the buffer.
|
|
5
|
+
import type {ReadableStream as WebReadableStream} from 'node:stream/web';
|
|
6
|
+
import type {ITokenizer, AnyWebByteStream} from 'strtok3';
|
|
301
7
|
|
|
302
|
-
|
|
8
|
+
/**
|
|
9
|
+
Either the Node.js ReadableStream or the `lib.dom.d.ts` ReadableStream.
|
|
10
|
+
Related issue: https://github.com/DefinitelyTyped/DefinitelyTyped/pull/60377
|
|
11
|
+
*/
|
|
12
|
+
export type AnyWebReadableStream<G> = WebReadableStream<G> | ReadableStream<G>;
|
|
303
13
|
|
|
304
|
-
|
|
305
|
-
|
|
14
|
+
export type FileTypeResult = {
|
|
15
|
+
/**
|
|
16
|
+
One of the supported [file types](https://github.com/sindresorhus/file-type#supported-file-types).
|
|
306
17
|
*/
|
|
307
|
-
|
|
18
|
+
readonly ext: string;
|
|
308
19
|
|
|
309
20
|
/**
|
|
310
|
-
|
|
21
|
+
The detected [MIME type](https://en.wikipedia.org/wiki/Internet_media_type).
|
|
22
|
+
*/
|
|
23
|
+
readonly mime: string;
|
|
24
|
+
};
|
|
311
25
|
|
|
312
|
-
|
|
26
|
+
/**
|
|
27
|
+
Detect the file type of a `Uint8Array` or `ArrayBuffer`.
|
|
313
28
|
|
|
314
|
-
|
|
315
|
-
@returns The detected file type and MIME type, or `undefined` when there is no match.
|
|
316
|
-
*/
|
|
317
|
-
function fromStream(stream: ReadableStream): Promise<core.FileTypeResult | undefined>;
|
|
29
|
+
The file type is detected by checking the [magic number](https://en.wikipedia.org/wiki/Magic_number_(programming)#Magic_numbers_in_files) of the buffer.
|
|
318
30
|
|
|
319
|
-
|
|
320
|
-
Detect the file type from an [`ITokenizer`](https://github.com/Borewit/strtok3#tokenizer) source.
|
|
31
|
+
If file access is available, it is recommended to use `.fromFile()` instead.
|
|
321
32
|
|
|
322
|
-
|
|
33
|
+
@param buffer - An Uint8Array or ArrayBuffer representing file data. It works best if the buffer contains the entire file. It may work with a smaller portion as well.
|
|
34
|
+
@param options - Options to override default behavior.
|
|
35
|
+
@returns The detected file type, or `undefined` when there is no match.
|
|
36
|
+
*/
|
|
37
|
+
export function fileTypeFromBuffer(buffer: Uint8Array | ArrayBuffer, options?: FileTypeOptions): Promise<FileTypeResult | undefined>;
|
|
323
38
|
|
|
324
|
-
|
|
39
|
+
/**
|
|
40
|
+
Detect the file type of a [web `ReadableStream`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream).
|
|
325
41
|
|
|
326
|
-
|
|
42
|
+
The file type is detected by checking the [magic number](https://en.wikipedia.org/wiki/Magic_number_(programming)#Magic_numbers_in_files) of the buffer.
|
|
327
43
|
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
44
|
+
@param stream - A [web `ReadableStream`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream) streaming a file to examine.
|
|
45
|
+
@param options - Options to override default behavior.
|
|
46
|
+
@returns A `Promise` for an object with the detected file type, or `undefined` when there is no match.
|
|
47
|
+
*/
|
|
48
|
+
export function fileTypeFromStream(stream: AnyWebByteStream, options?: FileTypeOptions): Promise<FileTypeResult | undefined>;
|
|
331
49
|
|
|
332
|
-
|
|
50
|
+
/**
|
|
51
|
+
Detect the file type from an [`ITokenizer`](https://github.com/Borewit/strtok3#tokenizer) source.
|
|
333
52
|
|
|
334
|
-
|
|
335
|
-
const httpTokenizer = await makeTokenizer(audioTrackUrl);
|
|
336
|
-
const fileType = await FileType.fromTokenizer(httpTokenizer);
|
|
53
|
+
This method is used internally, but can also be used for a special "tokenizer" reader.
|
|
337
54
|
|
|
338
|
-
|
|
339
|
-
//=> {ext: 'mp3', mime: 'audio/mpeg'}
|
|
340
|
-
})();
|
|
341
|
-
```
|
|
55
|
+
A tokenizer propagates the internal read functions, allowing alternative transport mechanisms, to access files, to be implemented and used.
|
|
342
56
|
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
57
|
+
@param tokenizer - File source implementing the tokenizer interface.
|
|
58
|
+
@param options - Options to override default behavior.
|
|
59
|
+
@returns The detected file type, or `undefined` when there is no match.
|
|
60
|
+
|
|
61
|
+
An example is [`@tokenizer/http`](https://github.com/Borewit/tokenizer-http), which requests data using [HTTP-range-requests](https://developer.mozilla.org/en-US/docs/Web/HTTP/Range_requests). A difference with a conventional stream and the [*tokenizer*](https://github.com/Borewit/strtok3#tokenizer), is that it is able to *ignore* (seek, fast-forward) in the stream. For example, you may only need and read the first 6 bytes, and the last 128 bytes, which may be an advantage in case reading the entire file would take longer.
|
|
347
62
|
|
|
63
|
+
@example
|
|
64
|
+
```
|
|
65
|
+
import {makeTokenizer} from '@tokenizer/http';
|
|
66
|
+
import {fileTypeFromTokenizer} from 'file-type';
|
|
67
|
+
|
|
68
|
+
const audioTrackUrl = 'https://test-audio.netlify.com/Various%20Artists%20-%202009%20-%20netBloc%20Vol%2024_%20tiuqottigeloot%20%5BMP3-V2%5D/01%20-%20Diablo%20Swing%20Orchestra%20-%20Heroines.mp3';
|
|
69
|
+
|
|
70
|
+
const httpTokenizer = await makeTokenizer(audioTrackUrl);
|
|
71
|
+
const fileType = await fileTypeFromTokenizer(httpTokenizer);
|
|
72
|
+
|
|
73
|
+
console.log(fileType);
|
|
74
|
+
//=> {ext: 'mp3', mime: 'audio/mpeg'}
|
|
75
|
+
```
|
|
76
|
+
*/
|
|
77
|
+
export function fileTypeFromTokenizer(tokenizer: ITokenizer, options?: FileTypeOptions): Promise<FileTypeResult | undefined>;
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
Supported file extensions.
|
|
81
|
+
*/
|
|
82
|
+
export const supportedExtensions: ReadonlySet<string>;
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
Supported MIME types.
|
|
86
|
+
*/
|
|
87
|
+
export const supportedMimeTypes: ReadonlySet<string>;
|
|
88
|
+
|
|
89
|
+
export type StreamOptions = {
|
|
348
90
|
/**
|
|
349
|
-
|
|
91
|
+
The default sample size in bytes.
|
|
92
|
+
|
|
93
|
+
@default 4100
|
|
350
94
|
*/
|
|
351
|
-
|
|
95
|
+
readonly sampleSize?: number;
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
Detect the file type of a [`Blob`](https://nodejs.org/api/buffer.html#class-blob) or [`File`](https://developer.mozilla.org/en-US/docs/Web/API/File).
|
|
100
|
+
|
|
101
|
+
@param blob - The [`Blob`](https://nodejs.org/api/buffer.html#class-blob) used for file detection.
|
|
102
|
+
@param options - Options to override default behavior.
|
|
103
|
+
@returns The detected file type, or `undefined` when there is no match.
|
|
104
|
+
|
|
105
|
+
@example
|
|
106
|
+
```
|
|
107
|
+
import {fileTypeFromBlob} from 'file-type';
|
|
108
|
+
|
|
109
|
+
const blob = new Blob(['<?xml version="1.0" encoding="ISO-8859-1" ?>'], {
|
|
110
|
+
type: 'text/plain',
|
|
111
|
+
endings: 'native'
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
console.log(await fileTypeFromBlob(blob));
|
|
115
|
+
//=> {ext: 'txt', mime: 'text/plain'}
|
|
116
|
+
```
|
|
117
|
+
*/
|
|
118
|
+
export declare function fileTypeFromBlob(blob: Blob, options?: FileTypeOptions): Promise<FileTypeResult | undefined>;
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
A custom file type detector.
|
|
122
|
+
|
|
123
|
+
Custom file type detectors are plugins designed to extend the default detection capabilities.
|
|
124
|
+
They allow support for uncommon file types, non-binary formats, or customized detection behavior.
|
|
125
|
+
|
|
126
|
+
Detectors can be added via the constructor options or by modifying `FileTypeParser#detectors` directly.
|
|
127
|
+
Detectors provided through the constructor are executed before the default ones.
|
|
128
|
+
|
|
129
|
+
Detectors can be added via the constructor options or by directly modifying `FileTypeParser#detectors`.
|
|
130
|
+
|
|
131
|
+
### Example adding a detector
|
|
132
|
+
|
|
133
|
+
```js
|
|
134
|
+
import {FileTypeParser} from 'file-type';
|
|
135
|
+
import {detectXml} from '@file-type/xml';
|
|
136
|
+
|
|
137
|
+
const parser = new FileTypeParser({customDetectors: [detectXml]});
|
|
138
|
+
const fileType = await parser.fromFile('sample.kml');
|
|
139
|
+
console.log(fileType);
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
### Available-third party file-type detectors
|
|
143
|
+
|
|
144
|
+
- [@file-type/xml](https://github.com/Borewit/file-type-xml): Detects common XML file types, such as GLM, KML, MusicXML, RSS, SVG, and XHTML
|
|
145
|
+
|
|
146
|
+
### Detector execution flow
|
|
147
|
+
|
|
148
|
+
If a detector returns `undefined`, the following rules apply:
|
|
149
|
+
|
|
150
|
+
1. **No Tokenizer Interaction**: If the detector does not modify the tokenizer's position, the next detector in the sequence is executed.
|
|
151
|
+
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.
|
|
152
|
+
|
|
153
|
+
### Example writing a custom detector
|
|
154
|
+
|
|
155
|
+
Below is an example of a custom detector array. This can be passed to the `FileTypeParser` via the `fileTypeOptions` argument.
|
|
156
|
+
|
|
157
|
+
```
|
|
158
|
+
import {FileTypeParser} from 'file-type';
|
|
159
|
+
|
|
160
|
+
const customDetectors = [
|
|
161
|
+
async tokenizer => {
|
|
162
|
+
const unicornHeader = [85, 78, 73, 67, 79, 82, 78]; // "UNICORN" in ASCII decimal
|
|
163
|
+
|
|
164
|
+
const buffer = new Uint8Array(unicornHeader.length);
|
|
165
|
+
await tokenizer.peekBuffer(buffer, {length: unicornHeader.length, mayBeLess: true});
|
|
166
|
+
if (unicornHeader.every((value, index) => value === buffer[index])) {
|
|
167
|
+
return {ext: 'unicorn', mime: 'application/unicorn'};
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
return undefined;
|
|
171
|
+
},
|
|
172
|
+
];
|
|
173
|
+
|
|
174
|
+
const buffer = new Uint8Array([85, 78, 73, 67, 79, 82, 78]);
|
|
175
|
+
const parser = new FileTypeParser({customDetectors});
|
|
176
|
+
const fileType = await parser.fromBuffer(buffer);
|
|
177
|
+
console.log(fileType); // {ext: 'unicorn', mime: 'application/unicorn'}
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
@param tokenizer - The [tokenizer](https://github.com/Borewit/strtok3#tokenizer) used to read file content.
|
|
181
|
+
@param fileType - The file type detected by standard or previous custom detectors, or `undefined` if no match is found.
|
|
182
|
+
@returns The detected file type, or `undefined` if no match is found.
|
|
183
|
+
*/
|
|
184
|
+
export type Detector = {
|
|
185
|
+
id: string;
|
|
186
|
+
detect: (tokenizer: ITokenizer, fileType?: FileTypeResult) => Promise<FileTypeResult | undefined>;
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
export type FileTypeOptions = {
|
|
190
|
+
customDetectors?: Iterable<Detector>;
|
|
352
191
|
|
|
353
192
|
/**
|
|
354
|
-
|
|
193
|
+
Specifies the byte tolerance for locating the first MPEG audio frame (e.g. `.mp1`, `.mp2`, `.mp3`, `.aac`).
|
|
194
|
+
|
|
195
|
+
Allows detection to handle slight sync offsets between the expected and actual frame start. Common in malformed or incorrectly muxed files, which, while technically invalid, do occur in the wild.
|
|
196
|
+
|
|
197
|
+
A tolerance of 10 bytes covers most cases.
|
|
198
|
+
|
|
199
|
+
@default 0
|
|
355
200
|
*/
|
|
356
|
-
|
|
201
|
+
mpegOffsetTolerance?: number;
|
|
202
|
+
};
|
|
203
|
+
|
|
204
|
+
export declare class TokenizerPositionError extends Error {
|
|
205
|
+
constructor(message?: string);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
export type AnyWebReadableByteStreamWithFileType = AnyWebReadableStream<Uint8Array> & {
|
|
209
|
+
readonly fileType?: FileTypeResult;
|
|
210
|
+
};
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
Workaround for using `bundler` as the module-resolution in TypeScript.
|
|
214
|
+
*/
|
|
215
|
+
export function fileTypeFromFile(filePath: string, options?: {customDetectors?: Iterable<Detector>}): Promise<FileTypeResult | undefined>;
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
Returns a `Promise` which resolves to the original readable stream argument, but with an added `fileType` property, which is an object like the one returned from `fileTypeFromFile()`.
|
|
357
219
|
|
|
220
|
+
This method can be handy to put in a stream pipeline, but it comes with a price. Internally `stream()` builds up a buffer of `sampleSize` bytes, used as a sample, to determine the file type. The sample size impacts the file detection resolution. A smaller sample size will result in lower probability of the best file type detection.
|
|
221
|
+
*/
|
|
222
|
+
export function fileTypeStream(webStream: AnyWebReadableStream<Uint8Array>, options?: StreamOptions): Promise<AnyWebReadableByteStreamWithFileType>;
|
|
223
|
+
|
|
224
|
+
export declare class FileTypeParser {
|
|
358
225
|
/**
|
|
359
|
-
|
|
226
|
+
File type detectors.
|
|
360
227
|
|
|
361
|
-
|
|
362
|
-
|
|
228
|
+
Initialized with a single entry holding the built-in detector function.
|
|
229
|
+
*/
|
|
230
|
+
detectors: Detector[];
|
|
363
231
|
|
|
364
|
-
|
|
365
|
-
```
|
|
366
|
-
import * as fs from 'fs';
|
|
367
|
-
import * as crypto from 'crypto';
|
|
368
|
-
import fileType = require('file-type');
|
|
232
|
+
constructor(options?: {customDetectors?: Iterable<Detector>; signal?: AbortSignal});
|
|
369
233
|
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
234
|
+
/**
|
|
235
|
+
Works the same way as {@link fileTypeFromBuffer}, additionally taking into account custom detectors (if any were provided to the constructor).
|
|
236
|
+
*/
|
|
237
|
+
fromBuffer(buffer: Uint8Array | ArrayBuffer): Promise<FileTypeResult | undefined>;
|
|
374
238
|
|
|
375
|
-
|
|
376
|
-
|
|
239
|
+
/**
|
|
240
|
+
Works the same way as {@link fileTypeFromTokenizer}, additionally taking into account custom detectors (if any were provided to the constructor).
|
|
241
|
+
*/
|
|
242
|
+
fromTokenizer(tokenizer: ITokenizer): Promise<FileTypeResult | undefined>;
|
|
377
243
|
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
})();
|
|
381
|
-
```
|
|
244
|
+
/**
|
|
245
|
+
Works the same way as {@link fileTypeFromBlob}, additionally taking into account custom detectors (if any were provided to the constructor).
|
|
382
246
|
*/
|
|
383
|
-
|
|
384
|
-
}
|
|
247
|
+
fromBlob(blob: Blob): Promise<FileTypeResult | undefined>;
|
|
385
248
|
|
|
386
|
-
|
|
249
|
+
/**
|
|
250
|
+
Works the same way as {@link fileTypeStream}, additionally taking into account custom detectors (if any were provided to the constructor).
|
|
251
|
+
*/
|
|
252
|
+
toDetectionStream(webStream: AnyWebReadableStream<Uint8Array>, options?: StreamOptions): Promise<AnyWebReadableByteStreamWithFileType>;
|
|
253
|
+
}
|