file-type 19.5.0 → 20.0.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 CHANGED
@@ -11,325 +11,16 @@ Related issue: https://github.com/DefinitelyTyped/DefinitelyTyped/pull/60377
11
11
  */
12
12
  export type AnyWebReadableStream<G> = WebReadableStream<G> | ReadableStream<G>;
13
13
 
14
- export type FileExtension =
15
- | 'jpg'
16
- | 'png'
17
- | 'apng'
18
- | 'gif'
19
- | 'webp'
20
- | 'flif'
21
- | 'xcf'
22
- | 'cr2'
23
- | 'cr3'
24
- | 'orf'
25
- | 'arw'
26
- | 'dng'
27
- | 'nef'
28
- | 'rw2'
29
- | 'raf'
30
- | 'tif'
31
- | 'bmp'
32
- | 'icns'
33
- | 'jxr'
34
- | 'psd'
35
- | 'indd'
36
- | 'zip'
37
- | 'tar'
38
- | 'rar'
39
- | 'gz'
40
- | 'bz2'
41
- | '7z'
42
- | 'dmg'
43
- | 'mp4'
44
- | 'mid'
45
- | 'mkv'
46
- | 'webm'
47
- | 'mov'
48
- | 'avi'
49
- | 'mpg'
50
- | 'mp2'
51
- | 'mp3'
52
- | 'm4a'
53
- | 'ogg'
54
- | 'opus'
55
- | 'flac'
56
- | 'wav'
57
- | 'qcp'
58
- | 'amr'
59
- | 'pdf'
60
- | 'epub'
61
- | 'mobi'
62
- | 'elf'
63
- | 'macho'
64
- | 'exe'
65
- | 'swf'
66
- | 'rtf'
67
- | 'woff'
68
- | 'woff2'
69
- | 'eot'
70
- | 'ttf'
71
- | 'otf'
72
- | 'ico'
73
- | 'flv'
74
- | 'ps'
75
- | 'xz'
76
- | 'sqlite'
77
- | 'nes'
78
- | 'crx'
79
- | 'xpi'
80
- | 'cab'
81
- | 'deb'
82
- | 'ar'
83
- | 'rpm'
84
- | 'Z'
85
- | 'lz'
86
- | 'cfb'
87
- | 'mxf'
88
- | 'mts'
89
- | 'wasm'
90
- | 'blend'
91
- | 'bpg'
92
- | 'docx'
93
- | 'pptx'
94
- | 'xlsx'
95
- | '3gp'
96
- | '3g2'
97
- | 'j2c'
98
- | 'jp2'
99
- | 'jpm'
100
- | 'jpx'
101
- | 'mj2'
102
- | 'aif'
103
- | 'odt'
104
- | 'ods'
105
- | 'odp'
106
- | 'xml'
107
- | 'heic'
108
- | 'cur'
109
- | 'ktx'
110
- | 'ape'
111
- | 'wv'
112
- | 'asf'
113
- | 'dcm'
114
- | 'mpc'
115
- | 'ics'
116
- | 'glb'
117
- | 'pcap'
118
- | 'dsf'
119
- | 'lnk'
120
- | 'alias'
121
- | 'voc'
122
- | 'ac3'
123
- | 'm4b'
124
- | 'm4p'
125
- | 'm4v'
126
- | 'f4a'
127
- | 'f4b'
128
- | 'f4p'
129
- | 'f4v'
130
- | 'mie'
131
- | 'ogv'
132
- | 'ogm'
133
- | 'oga'
134
- | 'spx'
135
- | 'ogx'
136
- | 'arrow'
137
- | 'shp'
138
- | 'aac'
139
- | 'mp1'
140
- | 'it'
141
- | 's3m'
142
- | 'xm'
143
- | 'ai'
144
- | 'skp'
145
- | 'avif'
146
- | 'eps'
147
- | 'lzh'
148
- | 'pgp'
149
- | 'asar'
150
- | 'stl'
151
- | 'chm'
152
- | '3mf'
153
- | 'zst'
154
- | 'jxl'
155
- | 'vcf'
156
- | 'jls'
157
- | 'pst'
158
- | 'dwg'
159
- | 'parquet'
160
- | 'class'
161
- | 'arj'
162
- | 'cpio'
163
- | 'ace'
164
- | 'avro'
165
- | 'icc'
166
- | 'fbx'
167
- | 'vsdx'
168
- | 'vtt'
169
- ; // eslint-disable-line semi-style
170
-
171
- export type MimeType =
172
- | 'image/jpeg'
173
- | 'image/png'
174
- | 'image/gif'
175
- | 'image/webp'
176
- | 'image/flif'
177
- | 'image/x-xcf'
178
- | 'image/x-canon-cr2'
179
- | 'image/x-canon-cr3'
180
- | 'image/tiff'
181
- | 'image/bmp'
182
- | 'image/icns'
183
- | 'image/vnd.ms-photo'
184
- | 'image/vnd.adobe.photoshop'
185
- | 'application/x-indesign'
186
- | 'application/epub+zip'
187
- | 'application/x-xpinstall'
188
- | 'application/vnd.oasis.opendocument.text'
189
- | 'application/vnd.oasis.opendocument.spreadsheet'
190
- | 'application/vnd.oasis.opendocument.presentation'
191
- | 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
192
- | 'application/vnd.openxmlformats-officedocument.presentationml.presentation'
193
- | 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
194
- | 'application/zip'
195
- | 'application/x-tar'
196
- | 'application/x-rar-compressed'
197
- | 'application/gzip'
198
- | 'application/x-bzip2'
199
- | 'application/x-7z-compressed'
200
- | 'application/x-apple-diskimage'
201
- | 'video/mp4'
202
- | 'audio/midi'
203
- | 'video/x-matroska'
204
- | 'video/webm'
205
- | 'video/quicktime'
206
- | 'video/vnd.avi'
207
- | 'audio/wav'
208
- | 'audio/qcelp'
209
- | 'audio/x-ms-asf'
210
- | 'video/x-ms-asf'
211
- | 'application/vnd.ms-asf'
212
- | 'video/mpeg'
213
- | 'video/3gpp'
214
- | 'audio/mpeg'
215
- | 'audio/mp4' // RFC 4337
216
- | 'audio/opus'
217
- | 'video/ogg'
218
- | 'audio/ogg'
219
- | 'application/ogg'
220
- | 'audio/x-flac'
221
- | 'audio/ape'
222
- | 'audio/wavpack'
223
- | 'audio/amr'
224
- | 'application/pdf'
225
- | 'application/x-elf'
226
- | 'application/x-mach-binary'
227
- | 'application/x-msdownload'
228
- | 'application/x-shockwave-flash'
229
- | 'application/rtf'
230
- | 'application/wasm'
231
- | 'font/woff'
232
- | 'font/woff2'
233
- | 'application/vnd.ms-fontobject'
234
- | 'font/ttf'
235
- | 'font/otf'
236
- | 'image/x-icon'
237
- | 'video/x-flv'
238
- | 'application/postscript'
239
- | 'application/eps'
240
- | 'application/x-xz'
241
- | 'application/x-sqlite3'
242
- | 'application/x-nintendo-nes-rom'
243
- | 'application/x-google-chrome-extension'
244
- | 'application/vnd.ms-cab-compressed'
245
- | 'application/x-deb'
246
- | 'application/x-unix-archive'
247
- | 'application/x-rpm'
248
- | 'application/x-compress'
249
- | 'application/x-lzip'
250
- | 'application/x-cfb'
251
- | 'application/x-mie'
252
- | 'application/x-apache-arrow'
253
- | 'application/mxf'
254
- | 'video/mp2t'
255
- | 'application/x-blender'
256
- | 'image/bpg'
257
- | 'image/j2c'
258
- | 'image/jp2'
259
- | 'image/jpx'
260
- | 'image/jpm'
261
- | 'image/mj2'
262
- | 'audio/aiff'
263
- | 'application/xml'
264
- | 'application/x-mobipocket-ebook'
265
- | 'image/heif'
266
- | 'image/heif-sequence'
267
- | 'image/heic'
268
- | 'image/heic-sequence'
269
- | 'image/ktx'
270
- | 'application/dicom'
271
- | 'audio/x-musepack'
272
- | 'text/calendar'
273
- | 'text/vcard'
274
- | 'text/vtt'
275
- | 'model/gltf-binary'
276
- | 'application/vnd.tcpdump.pcap'
277
- | 'audio/x-dsf' // Non-standard
278
- | 'application/x.ms.shortcut' // Invented by us
279
- | 'application/x.apple.alias' // Invented by us
280
- | 'audio/x-voc'
281
- | 'audio/vnd.dolby.dd-raw'
282
- | 'audio/x-m4a'
283
- | 'image/apng'
284
- | 'image/x-olympus-orf'
285
- | 'image/x-sony-arw'
286
- | 'image/x-adobe-dng'
287
- | 'image/x-nikon-nef'
288
- | 'image/x-panasonic-rw2'
289
- | 'image/x-fujifilm-raf'
290
- | 'video/x-m4v'
291
- | 'video/3gpp2'
292
- | 'application/x-esri-shape'
293
- | 'audio/aac'
294
- | 'audio/x-it'
295
- | 'audio/x-s3m'
296
- | 'audio/x-xm'
297
- | 'video/MP1S'
298
- | 'video/MP2P'
299
- | 'application/vnd.sketchup.skp'
300
- | 'image/avif'
301
- | 'application/x-lzh-compressed'
302
- | 'application/pgp-encrypted'
303
- | 'application/x-asar'
304
- | 'model/stl'
305
- | 'application/vnd.ms-htmlhelp'
306
- | 'model/3mf'
307
- | 'image/jxl'
308
- | 'application/zstd'
309
- | 'image/jls'
310
- | 'application/vnd.ms-outlook'
311
- | 'image/vnd.dwg'
312
- | 'application/x-parquet'
313
- | 'application/java-vm'
314
- | 'application/x-arj'
315
- | 'application/x-cpio'
316
- | 'application/x-ace-compressed'
317
- | 'application/avro'
318
- | 'application/vnd.iccprofile'
319
- | 'application/x.autodesk.fbx'
320
- | 'application/vnd.visio'
321
- ; // eslint-disable-line semi-style
322
-
323
14
  export type FileTypeResult = {
324
15
  /**
325
16
  One of the supported [file types](https://github.com/sindresorhus/file-type#supported-file-types).
326
17
  */
327
- readonly ext: FileExtension;
18
+ readonly ext: string;
328
19
 
329
20
  /**
330
21
  The detected [MIME type](https://en.wikipedia.org/wiki/Internet_media_type).
331
22
  */
332
- readonly mime: MimeType;
23
+ readonly mime: string;
333
24
  };
334
25
 
335
26
  /**
@@ -385,12 +76,12 @@ export function fileTypeFromTokenizer(tokenizer: ITokenizer): Promise<FileTypeRe
385
76
  /**
386
77
  Supported file extensions.
387
78
  */
388
- export const supportedExtensions: ReadonlySet<FileExtension>;
79
+ export const supportedExtensions: ReadonlySet<string>;
389
80
 
390
81
  /**
391
82
  Supported MIME types.
392
83
  */
393
- export const supportedMimeTypes: ReadonlySet<MimeType>;
84
+ export const supportedMimeTypes: ReadonlySet<string>;
394
85
 
395
86
  export type StreamOptions = {
396
87
  /**
@@ -423,31 +114,35 @@ console.log(await fileTypeFromBlob(blob));
423
114
  export declare function fileTypeFromBlob(blob: Blob): Promise<FileTypeResult | undefined>;
424
115
 
425
116
  /**
426
- Function that allows specifying custom detection mechanisms.
117
+ A custom file type detector.
118
+
119
+ Detectors can be added via the constructor options or by directly modifying `FileTypeParser#detectors`.
120
+
121
+ Detectors provided through the constructor options are executed before the default detectors.
427
122
 
428
- An iterable of detectors can be provided via the `fileTypeOptions` argument for the {@link FileTypeParser.constructor}.
123
+ Custom detectors allow for:
124
+ - Introducing new `FileTypeResult` entries.
125
+ - Modifying the detection behavior of existing `FileTypeResult` types.
429
126
 
430
- The detectors are called before the default detections in the provided order.
127
+ ### Detector execution flow
431
128
 
432
- Custom detectors can be used to add new `FileTypeResults` or to modify return behavior of existing `FileTypeResult` detections.
129
+ If a detector returns `undefined`, the following rules apply:
433
130
 
434
- If the detector returns `undefined`, there are 2 possible scenarios:
131
+ 1. **No Tokenizer Interaction**: If the detector does not modify the tokenizer's position, the next detector in the sequence is executed.
132
+ 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.
435
133
 
436
- 1. The detector has not read from the tokenizer, it will be proceeded with the next available detector.
437
- 2. The detector has read from the tokenizer (`tokenizer.position` has been increased).
438
- In that case no further detectors will be executed and the final conclusion is that file-type returns undefined.
439
- Note that this an exceptional scenario, as the detector takes the opportunity from any other detector to determine the file type.
134
+ ### Example usage
440
135
 
441
- Example detector array which can be extended and provided via the fileTypeOptions argument:
136
+ Below is an example of a custom detector array. This can be passed to the `FileTypeParser` via the `fileTypeOptions` argument.
442
137
 
443
138
  ```
444
139
  import {FileTypeParser} from 'file-type';
445
140
 
446
141
  const customDetectors = [
447
142
  async tokenizer => {
448
- const unicornHeader = [85, 78, 73, 67, 79, 82, 78]; // 'UNICORN' as decimal string
143
+ const unicornHeader = [85, 78, 73, 67, 79, 82, 78]; // "UNICORN" in ASCII decimal
449
144
 
450
- const buffer = Buffer.alloc(7);
145
+ const buffer = new Uint8Array(unicornHeader.length);
451
146
  await tokenizer.peekBuffer(buffer, {length: unicornHeader.length, mayBeLess: true});
452
147
  if (unicornHeader.every((value, index) => value === buffer[index])) {
453
148
  return {ext: 'unicorn', mime: 'application/unicorn'};
@@ -457,17 +152,20 @@ const customDetectors = [
457
152
  },
458
153
  ];
459
154
 
460
- const buffer = Buffer.from('UNICORN');
155
+ const buffer = new Uint8Array([85, 78, 73, 67, 79, 82, 78]);
461
156
  const parser = new FileTypeParser({customDetectors});
462
157
  const fileType = await parser.fromBuffer(buffer);
463
- console.log(fileType);
158
+ console.log(fileType); // {ext: 'unicorn', mime: 'application/unicorn'}
464
159
  ```
465
160
 
466
- @param tokenizer - The [tokenizer](https://github.com/Borewit/strtok3#tokenizer) used to read the file content from.
467
- @param fileType - The file type detected by the standard detections or a previous custom detection, or `undefined`` if no matching file type could be found.
468
- @returns The detected file type, or `undefined` when there is no match.
161
+ @param tokenizer - The [tokenizer](https://github.com/Borewit/strtok3#tokenizer) used to read file content.
162
+ @param fileType - The file type detected by standard or previous custom detectors, or `undefined` if no match is found.
163
+ @returns The detected file type, or `undefined` if no match is found.
469
164
  */
470
- export type Detector = (tokenizer: ITokenizer, fileType?: FileTypeResult) => Promise<FileTypeResult | undefined>;
165
+ export type Detector = {
166
+ id: string;
167
+ detect: (tokenizer: ITokenizer, fileType?: FileTypeResult) => Promise<FileTypeResult | undefined>;
168
+ };
471
169
 
472
170
  export type FileTypeOptions = {
473
171
  customDetectors?: Iterable<Detector>;
@@ -489,9 +187,14 @@ This method can be handy to put in a stream pipeline, but it comes with a price.
489
187
  export function fileTypeStream(webStream: AnyWebReadableStream<Uint8Array>, options?: StreamOptions): Promise<AnyWebReadableByteStreamWithFileType>;
490
188
 
491
189
  export declare class FileTypeParser {
492
- detectors: Iterable<Detector>;
190
+ /**
191
+ File type detectors.
192
+
193
+ Initialized with a single entry holding the built-in detector function.
194
+ */
195
+ detectors: Detector[];
493
196
 
494
- constructor(options?: {customDetectors?: Iterable<Detector>});
197
+ constructor(options?: {customDetectors?: Iterable<Detector>; signal?: AbortSignal});
495
198
 
496
199
  /**
497
200
  Works the same way as {@link fileTypeFromBuffer}, additionally taking into account custom detectors (if any were provided to the constructor).