-
-
Notifications
You must be signed in to change notification settings - Fork 357
/
index.js
80 lines (67 loc) · 2.42 KB
/
index.js
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
/**
Node.js specific entry point.
*/
import {ReadableStream as WebReadableStream} from 'node:stream/web';
import {pipeline, PassThrough, Readable} from 'node:stream';
import * as strtok3 from 'strtok3';
import {FileTypeParser as DefaultFileTypeParser, reasonableDetectionSizeInBytes} from './core.js';
export class FileTypeParser extends DefaultFileTypeParser {
async fromStream(stream) {
const tokenizer = await (stream instanceof WebReadableStream ? strtok3.fromWebStream(stream, this.tokenizerOptions) : strtok3.fromStream(stream, this.tokenizerOptions));
try {
return await super.fromTokenizer(tokenizer);
} finally {
await tokenizer.close();
}
}
async fromFile(path) {
const tokenizer = await strtok3.fromFile(path);
try {
return await super.fromTokenizer(tokenizer);
} finally {
await tokenizer.close();
}
}
async toDetectionStream(readableStream, options = {}) {
if (!(readableStream instanceof Readable)) {
return super.toDetectionStream(readableStream, options);
}
const {sampleSize = reasonableDetectionSizeInBytes} = options;
return new Promise((resolve, reject) => {
readableStream.on('error', reject);
readableStream.once('readable', () => {
(async () => {
try {
// Set up output stream
const pass = new PassThrough();
const outputStream = pipeline ? pipeline(readableStream, pass, () => {}) : readableStream.pipe(pass);
// Read the input stream and detect the filetype
const chunk = readableStream.read(sampleSize) ?? readableStream.read() ?? new Uint8Array(0);
try {
pass.fileType = await this.fromBuffer(chunk);
} catch (error) {
if (error instanceof strtok3.EndOfStreamError) {
pass.fileType = undefined;
} else {
reject(error);
}
}
resolve(outputStream);
} catch (error) {
reject(error);
}
})();
});
});
}
}
export async function fileTypeFromFile(path, fileTypeOptions) {
return (new FileTypeParser(fileTypeOptions)).fromFile(path, fileTypeOptions);
}
export async function fileTypeFromStream(stream, fileTypeOptions) {
return (new FileTypeParser(fileTypeOptions)).fromStream(stream);
}
export async function fileTypeStream(readableStream, options = {}) {
return new FileTypeParser(options).toDetectionStream(readableStream, options);
}
export {fileTypeFromTokenizer, fileTypeFromBuffer, fileTypeFromBlob, supportedMimeTypes, supportedExtensions} from './core.js';