forked from sindresorhus/file-type
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test-d.ts
67 lines (56 loc) · 1.93 KB
/
index.test-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
import {Buffer} from 'node:buffer';
import fs from 'node:fs';
import {expectType} from 'tsd';
import {
fileTypeFromBlob,
FileTypeResult as FileTypeResultBrowser,
} from './browser.js';
import {
fileTypeFromBuffer,
fileTypeFromFile,
fileTypeFromStream,
fileTypeStream,
supportedExtensions,
supportedMimeTypes,
FileTypeResult,
ReadableStreamWithFileType,
FileExtension,
MimeType,
} from './index.js';
expectType<Promise<FileTypeResult | undefined>>(fileTypeFromBuffer(Buffer.from([0xFF, 0xD8, 0xFF])));
expectType<Promise<FileTypeResult | undefined>>(fileTypeFromBuffer(new Uint8Array([0xFF, 0xD8, 0xFF])));
expectType<Promise<FileTypeResult | undefined>>(fileTypeFromBuffer(new ArrayBuffer(42)));
(async () => {
const result = await fileTypeFromBuffer(Buffer.from([0xFF, 0xD8, 0xFF]));
if (result !== undefined) {
expectType<FileExtension>(result.ext);
expectType<MimeType>(result.mime);
}
})();
(async () => {
expectType<FileTypeResult | undefined>(await fileTypeFromFile('myFile'));
const result = await fileTypeFromFile('myFile');
if (result !== undefined) {
expectType<FileExtension>(result.ext);
expectType<MimeType>(result.mime);
}
})();
(async () => {
const stream = fs.createReadStream('myFile');
expectType<FileTypeResult | undefined>(await fileTypeFromStream(stream));
const result = await fileTypeFromStream(stream);
if (result !== undefined) {
expectType<FileExtension>(result.ext);
expectType<MimeType>(result.mime);
}
})();
expectType<ReadonlySet<FileExtension>>(supportedExtensions);
expectType<ReadonlySet<MimeType>>(supportedMimeTypes);
const readableStream = fs.createReadStream('file.png');
const streamWithFileType = fileTypeStream(readableStream);
expectType<Promise<ReadableStreamWithFileType>>(streamWithFileType);
(async () => {
expectType<FileTypeResult | undefined>((await streamWithFileType).fileType);
})();
// Browser
expectType<Promise<FileTypeResultBrowser | undefined>>(fileTypeFromBlob(new Blob()));