forked from LinusU/stream-file-type
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
46 lines (38 loc) · 897 Bytes
/
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
import { PassThrough, Transform } from 'node:stream'
import fileType from 'file-type'
const kResult = Symbol('result')
const kStream = Symbol('stream')
export default class FileType extends Transform {
constructor () {
super()
this[kStream] = new PassThrough()
this[kResult] = fileType.fromStream(this[kStream]).then(
(value) => {
this[kStream] = null
this.emit('file-type', value || null)
return value || null
},
() => {
this[kStream] = null
this.emit('file-type', null)
return null
}
)
}
fileTypePromise () {
return this[kResult]
}
_transform (chunk, _, cb) {
if (this[kStream] != null) {
this[kStream].write(chunk)
}
cb(null, chunk)
}
_flush (cb) {
if (this[kStream] != null) {
this[kStream].end(() => cb(null))
} else {
cb(null)
}
}
}