1.0.3 • Published 3 years ago

identify-stream v1.0.3

Weekly downloads
3
License
ISC
Repository
github
Last release
3 years ago

identify-stream

Identify the contents of a Node stream based on the presence of a file signature (magic number). Can be extended to check for additional file formats.

Build Status Coverage Status

Install

$ npm install --save identify-stream

Basic Usage

const identifyStream = new IdentifyStream();
const inputStream = fs.createReadStream('./input.png');
const outputStream = fs.createWriteStream('./output.png');

inputStream.pipe(identifyStream).pipe(outputStream);

identifyStream.on('complete', (result) => {
  console.log(result); // {name: "PNG Image", extension: "png", mime: "image/png"}
});

Detecting Subtypes

Some file formats have two or more different variants, each with their own distinct file signature. Where this is the case the result object will feature an additional subtype property as seen below:

const identifyStream = new IdentifyStream();
const inputStream = fs.createReadStream('./input.gif');
const outputStream = fs.createWriteStream('./output.gig');

inputStream.pipe(identifyStream).pipe(outputStream);

identifyStream.on('complete', (result) => {
  console.log(result); // {name: "GIF Image", extension: "gif", mime: "image/gif", subtype: "87a"}
});

Detecting Custom Formats

Instances of IdentifyStream may be extended to detect additional file formats. See Defining Custom Formats for more information.

const identifyStream = new IdentifyStream({
  formats: {
    extension: 'pseudo format',
    extension: 'pseudo',
    mime: 'application/x-custom',
    signature: [{
      value: '7fa92c',
      offset: 0
    }];
  }
});
const inputStream = fs.createReadStream('./input.pseudo');
const outputStream = fs.createWriteStream('./output.pseudo');

inputStream.pipe(identifyStream).pipe(outputStream);

identifyStream.on('complete', (result) => {
  console.log(result); // {name: "pseudo format", extension: "pseudo", mime: "application/x-custom"}
});

API

new IdentityStream(options)

  • options

    • formats {Format | <Format[]} An optional list of custom file formats to detect (See below).

    • highWaterMark {Number} The node streams highWaterMark setting. Default: 16384

Defining Custom Formats

Instances of IdentifyStream may be configured to detect file formats beyond those already supported. To do so, use the formats option to provide one or more Format objects. See /data/formats.json for examples.

Format

Each Format object should have either a signature or a subtypes property.

PropertyTypeDescription
extensionStringThe file formats's extension
mimeStringThe file formats's MIME type.
signatureSignature  Signature[]One or more Signature objects. Identity-Stream will only match with this format if all signatures are present in the stream.
subtypesSubtype  Subtype[]One or more Subtype objects. Identity-Stream will match with this format if any subtype's signature matches.

SubType

PropertyTypeDescription
nameStringThe name of this subtype
signatureSignature  Signature[]One or more Signature objects. Identity-Stream will only match with this subtype if all signatures are present in the stream.

Signature

PropertyTypeDescription
valueStringThe signature to check for (in hex format).
offsetNumberThe offset of the signatue in bytes.

Events

complete

The complete event is emitted when the stream has succesfully identified the stream file format, or ruled out all known file formats. Callback functions attached to this event receive an object describing streamed file, or null if the format is not recognized.

error

The error event is emitted when the stream encounters an unexpected situation, for example if it is connected to an object stream.

Supported File Types

ExtensionMIME type
7zapplication/x-7z-compressed
avivideo/avi
bmpimage/bmp
bz2application/bzip2
exeapplication/octet-stream
flacaudio/flac
gifimage/gif
gzapplication/gzip
icoimage/ico
jpgimage/jpeg
jpfimage/jpx
m4aaudio/m4a
movvideo/quicktime
mp3audio/mpeg
mp4video/mp4
oggaudio/ogg
pdfapplication/pdf
pngimage/png
psdimage/psd
rtfapplication/rtf
tiffimage/tiff
wavaudio/wav
webmvideo/webm
webpimage/webp
xzapplication/x-xz
zipapplication/zip

License

ISC