forked from sindresorhus/file-type
-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.d.ts
384 lines (354 loc) · 9.03 KB
/
core.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/// <reference types="node"/>
import {Readable as ReadableStream} from 'stream';
import {ITokenizer} from 'strtok3/lib/core';
declare namespace core {
type FileExtension =
| 'jpg'
| 'png'
| 'apng'
| 'gif'
| 'webp'
| 'flif'
| 'xcf'
| 'cr2'
| 'cr3'
| 'orf'
| 'arw'
| 'dng'
| 'nef'
| 'rw2'
| 'raf'
| 'tif'
| 'bmp'
| 'icns'
| 'jxr'
| 'psd'
| 'indd'
| 'zip'
| 'tar'
| 'rar'
| 'gz'
| 'bz2'
| '7z'
| 'dmg'
| 'mp4'
| 'mid'
| 'mkv'
| 'webm'
| 'mov'
| 'avi'
| 'mpg'
| 'mp2'
| 'mp3'
| 'm4a'
| 'ogg'
| 'opus'
| 'flac'
| 'wav'
| 'qcp'
| 'amr'
| 'pdf'
| 'epub'
| 'mobi'
| 'exe'
| 'swf'
| 'rtf'
| 'woff'
| 'woff2'
| 'eot'
| 'ttf'
| 'otf'
| 'ico'
| 'flv'
| 'ps'
| 'xz'
| 'sqlite'
| 'nes'
| 'crx'
| 'xpi'
| 'cab'
| 'deb'
| 'ar'
| 'rpm'
| 'Z'
| 'lz'
| 'cfb'
| 'mxf'
| 'mts'
| 'wasm'
| 'blend'
| 'bpg'
| 'docx'
| 'pptx'
| 'xlsx'
| '3gp'
| '3g2'
| 'jp2'
| 'jpm'
| 'jpx'
| 'mj2'
| 'aif'
| 'odt'
| 'ods'
| 'odp'
| 'xml'
| 'heic'
| 'cur'
| 'ktx'
| 'ape'
| 'wv'
| 'asf'
| 'dcm'
| 'mpc'
| 'ics'
| 'glb'
| 'pcap'
| 'dsf'
| 'lnk'
| 'alias'
| 'voc'
| 'ac3'
| 'm4b'
| 'm4p'
| 'm4v'
| 'f4a'
| 'f4b'
| 'f4p'
| 'f4v'
| 'mie'
| 'ogv'
| 'ogm'
| 'oga'
| 'spx'
| 'ogx'
| 'arrow'
| 'shp'
| 'aac'
| 'mp1'
| 'it'
| 's3m'
| 'xm'
| 'ai'
| 'skp'
| 'avif'
| 'eps'
| 'lzh'
| 'pgp'
| 'asar'
| 'stl'
| 'chm'
| '3mf'
| 'zst'
| 'vcf';
type MimeType =
| 'image/jpeg'
| 'image/png'
| 'image/gif'
| 'image/webp'
| 'image/flif'
| 'image/x-xcf'
| 'image/x-canon-cr2'
| 'image/x-canon-cr3'
| 'image/tiff'
| 'image/bmp'
| 'image/icns'
| 'image/vnd.ms-photo'
| 'image/vnd.adobe.photoshop'
| 'application/x-indesign'
| 'application/epub+zip'
| 'application/x-xpinstall'
| 'application/vnd.oasis.opendocument.text'
| 'application/vnd.oasis.opendocument.spreadsheet'
| 'application/vnd.oasis.opendocument.presentation'
| 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
| 'application/vnd.openxmlformats-officedocument.presentationml.presentation'
| 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
| 'application/zip'
| 'application/x-tar'
| 'application/x-rar-compressed'
| 'application/gzip'
| 'application/x-bzip2'
| 'application/x-7z-compressed'
| 'application/x-apple-diskimage'
| 'video/mp4'
| 'audio/midi'
| 'video/x-matroska'
| 'video/webm'
| 'video/quicktime'
| 'video/vnd.avi'
| 'audio/vnd.wave'
| 'audio/qcelp'
| 'audio/x-ms-asf'
| 'video/x-ms-asf'
| 'application/vnd.ms-asf'
| 'video/mpeg'
| 'video/3gpp'
| 'audio/mpeg'
| 'audio/mp4' // RFC 4337
| 'audio/opus'
| 'video/ogg'
| 'audio/ogg'
| 'application/ogg'
| 'audio/x-flac'
| 'audio/ape'
| 'audio/wavpack'
| 'audio/amr'
| 'application/pdf'
| 'application/x-msdownload'
| 'application/x-shockwave-flash'
| 'application/rtf'
| 'application/wasm'
| 'font/woff'
| 'font/woff2'
| 'application/vnd.ms-fontobject'
| 'font/ttf'
| 'font/otf'
| 'image/x-icon'
| 'video/x-flv'
| 'application/postscript'
| 'application/eps'
| 'application/x-xz'
| 'application/x-sqlite3'
| 'application/x-nintendo-nes-rom'
| 'application/x-google-chrome-extension'
| 'application/vnd.ms-cab-compressed'
| 'application/x-deb'
| 'application/x-unix-archive'
| 'application/x-rpm'
| 'application/x-compress'
| 'application/x-lzip'
| 'application/x-cfb'
| 'application/x-mie'
| 'application/x-apache-arrow'
| 'application/mxf'
| 'video/mp2t'
| 'application/x-blender'
| 'image/bpg'
| 'image/jp2'
| 'image/jpx'
| 'image/jpm'
| 'image/mj2'
| 'audio/aiff'
| 'application/xml'
| 'application/x-mobipocket-ebook'
| 'image/heif'
| 'image/heif-sequence'
| 'image/heic'
| 'image/heic-sequence'
| 'image/ktx'
| 'application/dicom'
| 'audio/x-musepack'
| 'text/calendar'
| 'text/vcard'
| 'model/gltf-binary'
| 'application/vnd.tcpdump.pcap'
| 'audio/x-dsf' // Non-standard
| 'application/x.ms.shortcut' // Invented by us
| 'application/x.apple.alias' // Invented by us
| 'audio/x-voc'
| 'audio/vnd.dolby.dd-raw'
| 'audio/x-m4a'
| 'image/apng'
| 'image/x-olympus-orf'
| 'image/x-sony-arw'
| 'image/x-adobe-dng'
| 'image/x-nikon-nef'
| 'image/x-panasonic-rw2'
| 'image/x-fujifilm-raf'
| 'video/x-m4v'
| 'video/3gpp2'
| 'application/x-esri-shape'
| 'audio/aac'
| 'audio/x-it'
| 'audio/x-s3m'
| 'audio/x-xm'
| 'video/MP1S'
| 'video/MP2P'
| 'application/vnd.sketchup.skp'
| 'image/avif'
| 'application/x-lzh-compressed'
| 'application/pgp-encrypted'
| 'application/x-asar'
| 'model/stl'
| 'application/vnd.ms-htmlhelp'
| 'model/3mf'
| 'application/zstd';
interface FileTypeResult {
/**
One of the supported [file types](https://github.com/sindresorhus/file-type#supported-file-types).
*/
readonly ext: FileExtension;
/**
The detected [MIME type](https://en.wikipedia.org/wiki/Internet_media_type).
*/
readonly mime: MimeType;
}
type ReadableStreamWithFileType = ReadableStream & {
readonly fileType?: FileTypeResult;
};
/**
Detect the file type of a `Buffer`, `Uint8Array`, or `ArrayBuffer`.
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.
If file access is available, it is recommended to use `.fromFile()` instead.
@param buffer - A buffer representing file data. It works best if the buffer contains the entire file, it may work with a smaller portion as well.
@returns The detected file type and MIME type, or `undefined` when there is no match.
*/
function fromBuffer(buffer: Buffer | Uint8Array | ArrayBuffer): Promise<core.FileTypeResult | undefined>;
/**
Detect the file type of a Node.js [readable stream](https://nodejs.org/api/stream.html#stream_class_stream_readable).
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.
@param stream - A readable stream representing file data.
@returns The detected file type and MIME type, or `undefined` when there is no match.
*/
function fromStream(stream: ReadableStream): Promise<core.FileTypeResult | undefined>;
/**
Detect the file type from an [`ITokenizer`](https://github.com/Borewit/strtok3#tokenizer) source.
This method is used internally, but can also be used for a special "tokenizer" reader.
A tokenizer propagates the internal read functions, allowing alternative transport mechanisms, to access files, to be implemented and used.
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.
```
import {makeTokenizer} = require('@tokenizer/http');
import FileType = require('file-type');
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';
(async () => {
const httpTokenizer = await makeTokenizer(audioTrackUrl);
const fileType = await FileType.fromTokenizer(httpTokenizer);
console.log(fileType);
//=> {ext: 'mp3', mime: 'audio/mpeg'}
})();
```
@param tokenizer - File source implementing the tokenizer interface.
@returns The detected file type and MIME type, or `undefined` when there is no match.
*/
function fromTokenizer(tokenizer: ITokenizer): Promise<core.FileTypeResult | undefined>;
/**
Supported file extensions.
*/
const extensions: Set<core.FileExtension>;
/**
Supported MIME types.
*/
const mimeTypes: readonly core.MimeType[];
/**
Detect the file type of a readable stream.
@param readableStream - A [readable stream](https://nodejs.org/api/stream.html#stream_class_stream_readable) containing a file to examine.
@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 `FileType.fromFile()`.
@example
```
import * as fs from 'fs';
import * as crypto from 'crypto';
import fileType = require('file-type');
(async () => {
const read = fs.createReadStream('encrypted.enc');
const decipher = crypto.createDecipheriv(alg, key, iv);
const stream = await fileType.stream(read.pipe(decipher));
console.log(stream.fileType);
//=> {ext: 'mov', mime: 'video/quicktime'}
const write = fs.createWriteStream(`decrypted.${stream.fileType.ext}`);
stream.pipe(write);
})();
```
*/
function stream(readableStream: ReadableStream): Promise<core.ReadableStreamWithFileType>
}
export = core;