0.6.1 • Published 3 years ago

stream-file-type v0.6.1

Weekly downloads
4,914
License
MIT
Repository
github
Last release
3 years ago

Stream File Type

Get the file type by inspecting a stream.

Usage

import fs from 'node:fs'
import FileType from 'stream-file-type'

const input = fs.createReadStream('cat.jpg')
const detector = new FileType()

// Listen for event...
detector.on('file-type', (fileType) => {
  if (fileType === null) {
    console.log(`The mime type of "cat.jpg" could not be determined`)
  } else {
    console.log(`The file "cat.jpg" has the "${fileType.mime}" mime type`)
  }
})

// ...or get a Promise
detector.fileTypePromise().then((fileType) => {
  if (fileType === null) {
    console.log(`The mime type of "cat.jpg" could not be determined`)
  } else {
    console.log(`The file "cat.jpg" has the "${fileType.mime}" mime type`)
  }
})

input.pipe(detector).resume()

API

new FileType() => DuplexStream

Returns a new DuplexStream that will detect the file type of the content passing thru. All the data is passed as-is right thru the stream, and can be further piped to another destination.

When enough bytes have come thru to determine the file type (currently 4100) the event file-type will be emitted with the result of the detection. The result will either be null or an object with ext and mime.

FileType#fileTypePromise() => Promise

Returns a Promise of the detected file type. If the file-type event has already been emitted, the promise will be resolved with the result, otherwise the promise will be resolved when the file-type is detected.

0.6.1

3 years ago

0.6.0

3 years ago

0.5.0

4 years ago

0.4.0

4 years ago

0.3.2

5 years ago

0.3.1

5 years ago

0.3.0

5 years ago

0.2.0

5 years ago

0.1.1

7 years ago

0.1.0

7 years ago