1.0.2 • Published 7 years ago
file-type-stream2 v1.0.2
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-stream2Examples:
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 nullDocumentation:
Module exported members
fileTypeStreamfunction that returns Duplex stream file type detector.FileTypeStream2class (for typing purposes only).DuplexOptionsinterface.FileTypeResultinterface.
function fileTypeStream( callback?: (fileTypeResult: FileTypeResult) => void, opts?: DuplexOptions ): FileTypeStream2
This function is exported directly from module.It returns FileTypeStream2 instance that is native Duplex stream (details below).
- Arg0
[callback] (fileTypeResult: {mime: string, ext: string}) => void: When mime type of file detected thencallbackwill be called with properFileTypeResultobject ({mime: string, ext: string}).FileTypeStream2::onFileTypealso can be used to provide additonalcallback- in this case both callbacks would be called in preceding order: onFileType callback and then fileTypeStream callback. - Arg1
[opts] DuplexOptions: Options for returned FileTypeStream2 object (instance ofDuplexstream).Do NOT set toobjectModebecause file type detection will not be possible (expect unhandled error).Use only if you are aware of what you are doing (eg. set customhihgWaterMark).
class FileTypeStream2 extends Duplex stream
Internal class returned by fileTypeStream function. Documented only for development purposes.
It is called internally byconstructor( callback?: (fileTypeResult: FileTypeResult) => void, opts?: DupexOptions )fileTypeStreamfunction for you.- Arg0
[callback] (fileTypeResult: FileTypeResult) => void: When mime type of file detected thencallbackwill be called with properfileTypeResultobject ({mime: string, ext: string}) (onFileTypemethod also can be used to provide additonalcallback). - Arg1
[opts] DuplexOptions: Options forDuplexstream. Do NOT set toobjectModebecause file type detection will not be possible (expect unhandled error).Use only if you are aware of what you are doing (eg. set customhihgWaterMark).
- Arg0
When mime type of file detected thenonFileType( callback: (fileTypeResult: FileTypeResult) => void )callbackwill be called with properfileTypeResultstring. This method is equivalent ofon("fileType", callback).- Arg0
[callback] (fileTypeResult: FileTypeResult) => void: When mime type of file detected thencallbackwill be called with properfileTypeResultobject ({mime: string, ext: string}) (The same callback can be provided also viaconstructor).
- Arg0
interface FileTypeResult
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).
