file-type 19.6.0 → 20.0.1

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,327 +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
- | 'apk'
170
- ; // eslint-disable-line semi-style
171
-
172
- export type MimeType =
173
- | 'image/jpeg'
174
- | 'image/png'
175
- | 'image/gif'
176
- | 'image/webp'
177
- | 'image/flif'
178
- | 'image/x-xcf'
179
- | 'image/x-canon-cr2'
180
- | 'image/x-canon-cr3'
181
- | 'image/tiff'
182
- | 'image/bmp'
183
- | 'image/icns'
184
- | 'image/vnd.ms-photo'
185
- | 'image/vnd.adobe.photoshop'
186
- | 'application/x-indesign'
187
- | 'application/epub+zip'
188
- | 'application/x-xpinstall'
189
- | 'application/vnd.oasis.opendocument.text'
190
- | 'application/vnd.oasis.opendocument.spreadsheet'
191
- | 'application/vnd.oasis.opendocument.presentation'
192
- | 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
193
- | 'application/vnd.openxmlformats-officedocument.presentationml.presentation'
194
- | 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
195
- | 'application/zip'
196
- | 'application/x-tar'
197
- | 'application/x-rar-compressed'
198
- | 'application/gzip'
199
- | 'application/x-bzip2'
200
- | 'application/x-7z-compressed'
201
- | 'application/x-apple-diskimage'
202
- | 'video/mp4'
203
- | 'audio/midi'
204
- | 'video/x-matroska'
205
- | 'video/webm'
206
- | 'video/quicktime'
207
- | 'video/vnd.avi'
208
- | 'audio/wav'
209
- | 'audio/qcelp'
210
- | 'audio/x-ms-asf'
211
- | 'video/x-ms-asf'
212
- | 'application/vnd.ms-asf'
213
- | 'video/mpeg'
214
- | 'video/3gpp'
215
- | 'audio/mpeg'
216
- | 'audio/mp4' // RFC 4337
217
- | 'video/ogg'
218
- | 'audio/ogg'
219
- | 'audio/ogg; codecs=opus'
220
- | 'application/ogg'
221
- | 'audio/x-flac'
222
- | 'audio/ape'
223
- | 'audio/wavpack'
224
- | 'audio/amr'
225
- | 'application/pdf'
226
- | 'application/x-elf'
227
- | 'application/x-mach-binary'
228
- | 'application/x-msdownload'
229
- | 'application/x-shockwave-flash'
230
- | 'application/rtf'
231
- | 'application/wasm'
232
- | 'font/woff'
233
- | 'font/woff2'
234
- | 'application/vnd.ms-fontobject'
235
- | 'font/ttf'
236
- | 'font/otf'
237
- | 'image/x-icon'
238
- | 'video/x-flv'
239
- | 'application/postscript'
240
- | 'application/eps'
241
- | 'application/x-xz'
242
- | 'application/x-sqlite3'
243
- | 'application/x-nintendo-nes-rom'
244
- | 'application/x-google-chrome-extension'
245
- | 'application/vnd.ms-cab-compressed'
246
- | 'application/x-deb'
247
- | 'application/x-unix-archive'
248
- | 'application/x-rpm'
249
- | 'application/x-compress'
250
- | 'application/x-lzip'
251
- | 'application/x-cfb'
252
- | 'application/x-mie'
253
- | 'application/x-apache-arrow'
254
- | 'application/mxf'
255
- | 'video/mp2t'
256
- | 'application/x-blender'
257
- | 'image/bpg'
258
- | 'image/j2c'
259
- | 'image/jp2'
260
- | 'image/jpx'
261
- | 'image/jpm'
262
- | 'image/mj2'
263
- | 'audio/aiff'
264
- | 'application/xml'
265
- | 'application/x-mobipocket-ebook'
266
- | 'image/heif'
267
- | 'image/heif-sequence'
268
- | 'image/heic'
269
- | 'image/heic-sequence'
270
- | 'image/ktx'
271
- | 'application/dicom'
272
- | 'audio/x-musepack'
273
- | 'text/calendar'
274
- | 'text/vcard'
275
- | 'text/vtt'
276
- | 'model/gltf-binary'
277
- | 'application/vnd.tcpdump.pcap'
278
- | 'audio/x-dsf' // Non-standard
279
- | 'application/x.ms.shortcut' // Invented by us
280
- | 'application/x.apple.alias' // Invented by us
281
- | 'audio/x-voc'
282
- | 'audio/vnd.dolby.dd-raw'
283
- | 'audio/x-m4a'
284
- | 'image/apng'
285
- | 'image/x-olympus-orf'
286
- | 'image/x-sony-arw'
287
- | 'image/x-adobe-dng'
288
- | 'image/x-nikon-nef'
289
- | 'image/x-panasonic-rw2'
290
- | 'image/x-fujifilm-raf'
291
- | 'video/x-m4v'
292
- | 'video/3gpp2'
293
- | 'application/x-esri-shape'
294
- | 'audio/aac'
295
- | 'audio/x-it'
296
- | 'audio/x-s3m'
297
- | 'audio/x-xm'
298
- | 'video/MP1S'
299
- | 'video/MP2P'
300
- | 'application/vnd.sketchup.skp'
301
- | 'image/avif'
302
- | 'application/x-lzh-compressed'
303
- | 'application/pgp-encrypted'
304
- | 'application/x-asar'
305
- | 'model/stl'
306
- | 'application/vnd.ms-htmlhelp'
307
- | 'model/3mf'
308
- | 'image/jxl'
309
- | 'application/zstd'
310
- | 'image/jls'
311
- | 'application/vnd.ms-outlook'
312
- | 'image/vnd.dwg'
313
- | 'application/x-parquet'
314
- | 'application/java-vm'
315
- | 'application/x-arj'
316
- | 'application/x-cpio'
317
- | 'application/x-ace-compressed'
318
- | 'application/avro'
319
- | 'application/vnd.iccprofile'
320
- | 'application/x.autodesk.fbx'
321
- | 'application/vnd.visio'
322
- | 'application/vnd.android.package-archive'
323
- ; // eslint-disable-line semi-style
324
-
325
14
  export type FileTypeResult = {
326
15
  /**
327
16
  One of the supported [file types](https://github.com/sindresorhus/file-type#supported-file-types).
328
17
  */
329
- readonly ext: FileExtension;
18
+ readonly ext: string;
330
19
 
331
20
  /**
332
21
  The detected [MIME type](https://en.wikipedia.org/wiki/Internet_media_type).
333
22
  */
334
- readonly mime: MimeType;
23
+ readonly mime: string;
335
24
  };
336
25
 
337
26
  /**
@@ -387,12 +76,12 @@ export function fileTypeFromTokenizer(tokenizer: ITokenizer): Promise<FileTypeRe
387
76
  /**
388
77
  Supported file extensions.
389
78
  */
390
- export const supportedExtensions: ReadonlySet<FileExtension>;
79
+ export const supportedExtensions: ReadonlySet<string>;
391
80
 
392
81
  /**
393
82
  Supported MIME types.
394
83
  */
395
- export const supportedMimeTypes: ReadonlySet<MimeType>;
84
+ export const supportedMimeTypes: ReadonlySet<string>;
396
85
 
397
86
  export type StreamOptions = {
398
87
  /**
@@ -425,31 +114,35 @@ console.log(await fileTypeFromBlob(blob));
425
114
  export declare function fileTypeFromBlob(blob: Blob): Promise<FileTypeResult | undefined>;
426
115
 
427
116
  /**
428
- 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.
429
122
 
430
- 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.
431
126
 
432
- The detectors are called before the default detections in the provided order.
127
+ ### Detector execution flow
433
128
 
434
- 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:
435
130
 
436
- 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.
437
133
 
438
- 1. The detector has not read from the tokenizer, it will be proceeded with the next available detector.
439
- 2. The detector has read from the tokenizer (`tokenizer.position` has been increased).
440
- In that case no further detectors will be executed and the final conclusion is that file-type returns undefined.
441
- Note that this an exceptional scenario, as the detector takes the opportunity from any other detector to determine the file type.
134
+ ### Example usage
442
135
 
443
- 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.
444
137
 
445
138
  ```
446
139
  import {FileTypeParser} from 'file-type';
447
140
 
448
141
  const customDetectors = [
449
142
  async tokenizer => {
450
- 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
451
144
 
452
- const buffer = Buffer.alloc(7);
145
+ const buffer = new Uint8Array(unicornHeader.length);
453
146
  await tokenizer.peekBuffer(buffer, {length: unicornHeader.length, mayBeLess: true});
454
147
  if (unicornHeader.every((value, index) => value === buffer[index])) {
455
148
  return {ext: 'unicorn', mime: 'application/unicorn'};
@@ -459,17 +152,20 @@ const customDetectors = [
459
152
  },
460
153
  ];
461
154
 
462
- const buffer = Buffer.from('UNICORN');
155
+ const buffer = new Uint8Array([85, 78, 73, 67, 79, 82, 78]);
463
156
  const parser = new FileTypeParser({customDetectors});
464
157
  const fileType = await parser.fromBuffer(buffer);
465
- console.log(fileType);
158
+ console.log(fileType); // {ext: 'unicorn', mime: 'application/unicorn'}
466
159
  ```
467
160
 
468
- @param tokenizer - The [tokenizer](https://github.com/Borewit/strtok3#tokenizer) used to read the file content from.
469
- @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.
470
- @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.
471
164
  */
472
- 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
+ };
473
169
 
474
170
  export type FileTypeOptions = {
475
171
  customDetectors?: Iterable<Detector>;
@@ -491,9 +187,14 @@ This method can be handy to put in a stream pipeline, but it comes with a price.
491
187
  export function fileTypeStream(webStream: AnyWebReadableStream<Uint8Array>, options?: StreamOptions): Promise<AnyWebReadableByteStreamWithFileType>;
492
188
 
493
189
  export declare class FileTypeParser {
494
- 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[];
495
196
 
496
- constructor(options?: {customDetectors?: Iterable<Detector>; signal: AbortSignal});
197
+ constructor(options?: {customDetectors?: Iterable<Detector>; signal?: AbortSignal});
497
198
 
498
199
  /**
499
200
  Works the same way as {@link fileTypeFromBuffer}, additionally taking into account custom detectors (if any were provided to the constructor).