visualCaptcha.js
function( index, response, isRetina ) {
        var fs = require( 'fs' ),
            imageOption = this.getImageOptionAtIndex( index ),
            imageFileName = imageOption ? imageOption.path : '',// If there's no imageOption, we set the file name as empty
            imageFilePath = __dirname + '/images/' + imageFileName,
            mime = require( 'mime' ),
            mimeType,
            stream;

        // Force boolean for isRetina
        if ( ! isRetina ) {
            isRetina = false;
        } else {
            isRetina = true;
        }

        // If retina is requested, change the file name
        if ( isRetina ) {
            imageFileName = imageFileName.replace( /\.png/gi, '@2x.png' );
            imageFilePath = imageFilePath.replace( /\.png/gi, '@2x.png' );
        }

        // If the index is non-existent, the file name will be empty, same as if the options weren't generated
        if ( imageFileName ) {
            fs.exists( imageFilePath, function( exists ) {
                if ( exists ) {
                    mimeType = mime.getType( imageFilePath );

                    // Set the appropriate mime type
                    response.set( 'content-type', mimeType );

                    // Make sure this is not cached
                    response.set( 'cache-control', 'no-cache, no-store, must-revalidate' );
                    response.set( 'pragma', 'no-cache' );
                    response.set( 'expires', 0 );

                    stream = fs.createReadStream( imageFilePath );
                    var responseData = [];

                    if ( stream ) {
                        stream.on( 'data', function( chunk ) {
                          responseData.push( chunk );
                        });

                        stream.on( 'end', function() {
                            if ( ! response.headerSent ) {
                                var finalData = Buffer.concat( responseData );
                                response.write( finalData );

                                // Add some noise randomly, so images can't be saved and matched easily by filesize or checksum
                                var noiseData = crypto.randomBytes(Math.round((Math.random() * 1999)) + 501).toString('hex');
                                response.write( noiseData );

                                response.end();
                            }
                        });
                    } else {
                        response.status( 404 ).send( 'Not Found' );
                    }
                } else {
                    response.status( 404 ).send( 'Not Found' );
                }
            });
        } else {
            response.status( 404 ).send( 'Not Found' );
        }


    }
function( index, response, isRetina ) {
        var fs = require( 'fs' ),
            imageOption = this.getImageOptionAtIndex( index ),
            imageFileName = imageOption ? imageOption.path : '',// If there's no imageOption, we set the file name as empty
            imageFilePath = __dirname + '/images/' + imageFileName,
            mime = require( 'mime' ),
            mimeType,
            stream;

        // Force boolean for isRetina
        if ( ! isRetina ) {
            isRetina = false;
        } else {
            isRetina = true;
        }

        // If retina is requested, change the file name
        if ( isRetina ) {
            imageFileName = imageFileName.replace( /\.png/gi, '@2x.png' );
            imageFilePath = imageFilePath.replace( /\.png/gi, '@2x.png' );
        }

        // If the index is non-existent, the file name will be empty, same as if the options weren't generated
        if ( imageFileName ) {
            fs.exists( imageFilePath, function( exists ) {
                if ( exists ) {
                    mimeType = mime.getType( imageFilePath );

                    // Set the appropriate mime type
                    response.set( 'content-type', mimeType );

                    // Make sure this is not cached
                    response.set( 'cache-control', 'no-cache, no-store, must-revalidate' );
                    response.set( 'pragma', 'no-cache' );
                    response.set( 'expires', 0 );

                    stream = fs.createReadStream( imageFilePath );
                    var responseData = [];

                    if ( stream ) {
                        stream.on( 'data', function( chunk ) {
                          responseData.push( chunk );
                        });

                        stream.on( 'end', function() {
                            if ( ! response.headerSent ) {
                                var finalData = Buffer.concat( responseData );
                                response.write( finalData );

                                // Add some noise randomly, so images can't be saved and matched easily by filesize or checksum
                                var noiseData = crypto.randomBytes(Math.round((Math.random() * 1999)) + 501).toString('hex');
                                response.write( noiseData );

                                response.end();
                            }
                        });
                    } else {
                        response.status( 404 ).send( 'Not Found' );
                    }
                } else {
                    response.status( 404 ).send( 'Not Found' );
                }
            });
        } else {
            response.status( 404 ).send( 'Not Found' );
        }


    }
function ( index , response , isRetina ) { var fs = require ( 'fs' ) , imageOption = this . getImageOptionAtIndex ( index ) , imageFileName = imageOption ? imageOption . path : '' , // If there's no imageOption, we set the file name as empty imageFilePath = __dirname + '/images/' + imageFileName , mime = require ( 'mime' ) , mimeType , stream ; // Force boolean for isRetina if ( ! isRetina ) { isRetina = false ; } else { isRetina = true ; } // If retina is requested, change the file name if ( isRetina ) { imageFileName = imageFileName . replace ( / \.png / gi , '@2x.png' ) ; imageFilePath = imageFilePath . replace ( / \.png / gi , '@2x.png' ) ; } // If the index is non-existent, the file name will be empty, same as if the options weren't generated if ( imageFileName ) { fs . exists ( imageFilePath , function ( exists ) { if ( exists ) { mimeType = mime . getType ( imageFilePath ) ; // Set the appropriate mime type response . set ( 'content-type' , mimeType ) ; // Make sure this is not cached response . set ( 'cache-control' , 'no-cache, no-store, must-revalidate' ) ; response . set ( 'pragma' , 'no-cache' ) ; response . set ( 'expires' , 0 ) ; stream = fs . createReadStream ( imageFilePath ) ; var responseData = [ ] ; if ( stream ) { stream . on ( 'data' , function ( chunk ) { responseData . push ( chunk ) ; } ) ; stream . on ( 'end' , function ( ) { if ( ! response . headerSent ) { var finalData = Buffer . concat ( responseData ) ; response . write ( finalData ) ; // Add some noise randomly, so images can't be saved and matched easily by filesize or checksum var noiseData = crypto . randomBytes ( Math . round ( ( Math . random ( ) * 1999 ) ) + 501 ) . toString ( 'hex' ) ; response . write ( noiseData ) ; response . end ( ) ; } } ) ; } else { response . status ( 404 ) . send ( 'Not Found' ) ; } } else { response . status ( 404 ) . send ( 'Not Found' ) ; } } ) ; } else { response . status ( 404 ) . send ( 'Not Found' ) ; } }
Stream image file given an index in the session visualCaptcha images array @param index of the image in the session images array to send @param response Node's response object @paran isRetina boolean. Defaults to false
Stream image file given an index in the session visualCaptcha images array