npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

pipe-validation-stream

v1.0.10

Published

Easily validate streamed data using pipe

Downloads

28

Readme

pipe-validation-stream

Easily validate streamed data in NodeJs

Introduction

Ever needed to pipe data from one stream to another only if some validation passes? If so - this is the package for you. With this package you can create a validation-stream, and validate the streamed data while piping it from one stream to another.

Installation

Install with npm:

    npm install pipe-validation-stream

API

Using ValidationStream is pretty easy. All you have to do is to create a ValidationStream using the constructor, and then you may use it as any other stream (pipe streams to it, pipe it to other streams, write to it, register to stream events, etc...). The constructor gets 2 parameters:

  • requiredBytesCount - The required amount of bytes needed for validation. The validation callback will be called only after at least requiredBytesCount bytes have been written to the stream.
  • validationCallback - The callback to be called for validation. The callback gets one parameter, which is a buffer with length of requiredBytesCount. The callback should return a boolean value, which is the validation result. You may return a promise as well if your validation is async.

In addition to the common stream API, ValidationStream may raise three kind of custom events:

  • ValidationStream.VALIDATION_FAILED_EVENT - This event will be raised if the validation failed. Of course, if the validation fails, no data will be written to the next stream (assuming the ValidationStream is piped to another stream).
  • ValidationStream.VALIDATION_ERROR_EVENT - This event will be raised if an error occurred during validation.
  • ValidationStream.DISPOSE_EVENT - This event will be raised when there is no more use of the ValidationStream, and in this code you may release references to it, or actually execute any kind of cleaning code you want. This event is guaranteed to be raised even if the validation has failed, or if an error was raised somewhere along the way, or if the stream has closed, etc.

Examples

Filter all files that start with the text "validation" (in order to execute this test, change listOfFiles to point to real files):


let ValidationStream = require('pipe-validation-stream');
let fs = require('fs');

let listOfFiles = ['a.txt', 'b.mp4', 'c.sh'];

for (let i = 0; i < listOfFiles.length; ++i) {
        let inputStream = fs.createReadStream(listOfFiles[i]);
        let outputStream = fs.createWriteStream(listOfFiles[i] + ".verified");
        let validationStream = new ValidationStream.ValidationStream(10, function(data) { 
                        return data.toString() === "validation";
                });

        validationStream.on(ValidationStream.VALIDATION_FAILED_EVENT, function() {
            console.log("Validation failed for: ", listOfFiles[i]);
            fs.unlink(listOfFiles[i]);
        })

        inputStream.pipe(validationStream).pipe(outputStream);
}

Now, lets get fancier: validate the true type of a file WHILE REQUESTING IT (i.e. - the file won't be saved on the computer unless the validation passes). We are validating that the requested file is really a mp4 file:

let ValidationStream = require('pipe-validation-stream');
let fs = require('fs');
let mmmagic = require('mmmagic');
let Promise = require('bluebird');
let Magic = Promise.promisifyAll(new(mmmagic.Magic)(mmmagic.MAGIC_MIME_TYPE));
let request = require('request');
let path = require('path');

let listOfUrls = ['http://www.w3schools.com/html/mov_bbb.ogg', 'http://www.w3schools.com/html/mov_bbb.mp4', "http://google.com"];

// 262 bytes are required in order to truly identify the true type of a file (by checking its 'file' header)
const REQUIRED_BYTES_COUNT_FOR_TRUE_TYPE = 262;

for (let i = 0; i < listOfUrls.length; ++i) {
    let urlRequest = request(listOfUrls[i]);
    let verifiedFileName = path.basename(listOfUrls[i]);
    let outputStream = fs.createWriteStream(verifiedFileName);
    let validationStream = new ValidationStream.ValidationStream(REQUIRED_BYTES_COUNT_FOR_TRUE_TYPE, function(data) {
        return Magic.detectAsync(data).then(function(identificationResult) {
            console.log("The true type of ", listOfUrls[i], " is ", identificationResult);
            return identificationResult === "video/mp4";
        });
    });

    validationStream.on(ValidationStream.VALIDATION_FAILED_EVENT, function() {
        console.log("Validation failed for: ", listOfUrls[i]);
        fs.unlinkSync(verifiedFileName);
    })

    urlRequest.pipe(validationStream).pipe(outputStream);
}

The previous example uses the NPMs: request, bluebird, mmmagic.

Tests

Execute the following command:

    npm test