About:
- This module detects mime type of stream basing on it's content. If unable to detect it signals
"application/octet-stream"
. For detection it uses file-type module. - Improved (and fixed) version of (1)file-type-stream and (2)stream-file-type modules.
- Api consistent only with (1)file-type-stream so if you want to switch from (2)stream-file-type you need some small adaptations (check examples section).
- If you want just to detect file type (without reading whole file or piping stream) check detect only file type section below.
Installation and requirements:
Requires Node.js v5.10.0
$ npm install file-type-stream2
Examples:
Explanation of 'how it works' step by step (examples in next section):
readableStream
.pipe( fileTypeStream( (fileType) => {
writableStream.fileType = fileType} ))
.pipe(writableStream);
- Pipe readable stream to FileTypeStream2 instance.
- FileTypeStream2 will collect bytes until it will be able to recognize file type by it's content.
-
IF (received at leas 4100 bytes OR input stream ends) AND (still unable to detect file type) THEN (set
fileType = {ext: "", mime: "application/octet-stream"
). - When file type recognized
- Signal file type by event or/and callback (details in example code below).
- Pass through collected data and rest of incoming stream (start acting like PassThrough stream).
Example with callback:
import { fileTypeStream } from "file-type-stream2";
// const { fileTypeStream } from "file-type-stream2";
readStream
.pipe( fileTypeStream( function(fileType) {
writeStream.mime = fileType.mime;
writeStream.ext = fileType.ext;
}))
.pipe(writeStream); // writeStream will NOT receive any data before file type recognized.
Example with event:
import { fileTypeStream } from "file-type-stream2";
let fts = fileTypeStream();
fts.onFileType( function(fileType) { // Equivalent of fts.on("fileType", function(fileType){...})
writeStream.mime = fileType.mime;
writeStream.ext = fileType.ext;
});
readStream
.pipe( fts )
.pipe( writeStream ); // writeStream will NOT receive any data before file type recognized.
Mixed example:
import { fileTypeStream } from "file-type-stream2";
let fts = fileTypeStream( function(fileType) {
writeStream.mime = fileType.mime; // *This will be SECOND
writeStream.ext = fileType.ext;
});
fts.onFileType( function(fileType) {
writeStream.mime = fileType.mime; // *This will be FIRST
writeStream.ext = fileType.ext;
});
readStream
.pipe( fts )
.pipe( writeStream ); // writeStream will NOT receive any data before file type recognized.
Detect only file type:
If you want only detect file type basing on it's content then this example is for you. You just have to use file-type module:
import fileType from "file-type";
// let fileType = require("file-type");
let first_4100_bytes_buffer = getFirstBytesSomehow(); // you can be less efficient and read whole file into buffer
let ft = fileType(first_4100_bytes_buffer);
ft = ft && ft.mime; // mime type string or null
Documentation:
Module exported members
-
fileTypeStream
function that returns Duplex stream file type detector. -
FileTypeStream2
class (for typing purposes only). -
DuplexOptions
interface. -
FileTypeResult
interface.
fileTypeStream( callback?: (fileTypeResult: FileTypeResult) => void, opts?: DuplexOptions ): FileTypeStream2
function This function is exported directly from module.
It returns FileTypeStream2 instance that is native Duplex stream (details below).
- Arg[0]
[callback] (fileTypeResult: {mime: string, ext: string}) => void
:
When mime type of file detected thencallback
will be called with properFileTypeResult
object ({mime: string, ext: string}
).FileTypeStream2::onFileType
also can be used to provide additonalcallback
- in this case both callbacks would be called in preceding order: onFileType callback and then fileTypeStream callback. - Arg[1]
[opts] DuplexOptions
:
Options for returned FileTypeStream2 object (instance ofDuplex
stream).
Do NOT set toobjectMode
because file type detection will not be possible (expect unhandled error).
Use only if you are aware of what you are doing (eg. set customhihgWaterMark
).
FileTypeStream2
extends Duplex
stream
class Internal class returned by fileTypeStream
function. Documented only for development purposes.
-
It is called internally byconstructor( callback?: (fileTypeResult: FileTypeResult) => void, opts?: DupexOptions )
fileTypeStream
function for you.- Arg[0]
[callback] (fileTypeResult: FileTypeResult) => void
:
When mime type of file detected thencallback
will be called with properfileTypeResult
object ({mime: string, ext: string}
) (onFileType
method also can be used to provide additonalcallback
).
- Arg[1]
[opts] DuplexOptions
:
Options forDuplex
stream. Do NOT set toobjectMode
because file type detection will not be possible (expect unhandled error).Use only if you are aware of what you are doing (eg. set customhihgWaterMark
).
- Arg[0]
-
When mime type of file detected thenonFileType( callback: (fileTypeResult: FileTypeResult) => void )
callback
will be called with properfileTypeResult
string. This method is equivalent ofon("fileType", callback)
.- Arg[0]
[callback] (fileTypeResult: FileTypeResult) => void
:
When mime type of file detected thencallback
will be called with properfileTypeResult
object ({mime: string, ext: string}
) (The same callback can be provided also viaconstructor
).
- Arg[0]
FileTypeResult
interface Interface returned from file-type module.
Argument provided for your callbacks.
-
File extension of specific mime type (if mime unknown then expect empty stringext: string
""
value). -
Mime type of file (if mime unknown then expectmime: string
:"application/octet-stream"
value).