https://github.com/binded/file-type-stream
Wrapper over file-type that makes using it with streams easier
https://github.com/binded/file-type-stream
Last synced: about 1 year ago
JSON representation
Wrapper over file-type that makes using it with streams easier
- Host: GitHub
- URL: https://github.com/binded/file-type-stream
- Owner: binded
- License: mit
- Archived: true
- Created: 2016-09-29T23:02:54.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2016-09-30T00:14:55.000Z (over 9 years ago)
- Last Synced: 2025-04-20T14:17:51.613Z (about 1 year ago)
- Language: JavaScript
- Homepage:
- Size: 23.4 KB
- Stars: 8
- Watchers: 7
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# file-type-stream
[](https://travis-ci.org/blockai/file-type-stream)
Wrapper over [file-type](https://github.com/sindresorhus/file-type) that
makes using it with streams easier.
## Install
```bash
npm install --save file-type-stream
```
Requires Node v6+
## Usage
```
fileTypeStream(callback)
```
See [./test](./test) directory for usage examples.
```javascript
import fileTypeStream from 'file-type-stream'
import { PassThrough } from 'stream'
// ...
// s3Client = ....
// ...
const through = new PassThrough()
readStream.pipe(fileTypeStream((type) => {
// since transform streams buffer up to 16kb which
// is enough to detect the file type, we get
// the file type even through through hasn't been piped
// anywhere yet.
console.log('ext', type.ext)
console.log('mime', type.mime)
// E.g.:
// We can use that info to open a write stream on aws s3
// with the correct content type
client.upload({
ContentType: type.mime,
Bucket: 'some-bucket',
Key: 'some-key',
Body: through,
}, null, (err) => {
// Upload completed!
})
})).pipe(through)
```
The code above can be written a bit more intuitively with async/await:
```javascript
import fileTypeStream from 'file-type-stream'
import { PassThrough } from 'stream'
// ...
// s3Client = ....
// ...
const upload = async (rs) => {
const Body = new PassThrough()
const { mime } = await new Promise(resolve => {
rs.pipe(fileTypeStream(resolve)).pipe(Body)
})
const response = client.upload({
Body,
ContentType: mime,
Bucket: 'some-bucket',
Key: 'some-key',
}).toPromise()
console.log(response)
}
```