visualCaptcha.js
function( response, fileType ) {
        var fs = require( 'fs' ),
            mime = require( 'mime' ),
            audioOption = this.getValidAudioOption(),
            audioFileName = audioOption ? audioOption.path : '',// If there's no audioOption, we set the file name as empty
            audioFilePath = __dirname + '/audios/' + audioFileName,
            mimeType,
            stream;

        // If the file name is empty, we skip any work and return a 404 response
        if ( audioFileName ) {
            // We need to replace '.mp3' with '.ogg' if the fileType === 'ogg'
            if ( fileType === 'ogg' ) {
                audioFileName = audioFileName.replace( /\.mp3/gi, '.ogg' );
                audioFilePath = audioFilePath.replace( /\.mp3/gi, '.ogg' );
            } else {
                fileType = 'mp3';// This isn't doing anything, really, but I feel better with it
            }

            fs.exists( audioFilePath, function( exists ) {
                if ( exists ) {
                    mimeType = mime.getType( audioFilePath );

                    // 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( audioFilePath );
                    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 audio files 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( response, fileType ) {
        var fs = require( 'fs' ),
            mime = require( 'mime' ),
            audioOption = this.getValidAudioOption(),
            audioFileName = audioOption ? audioOption.path : '',// If there's no audioOption, we set the file name as empty
            audioFilePath = __dirname + '/audios/' + audioFileName,
            mimeType,
            stream;

        // If the file name is empty, we skip any work and return a 404 response
        if ( audioFileName ) {
            // We need to replace '.mp3' with '.ogg' if the fileType === 'ogg'
            if ( fileType === 'ogg' ) {
                audioFileName = audioFileName.replace( /\.mp3/gi, '.ogg' );
                audioFilePath = audioFilePath.replace( /\.mp3/gi, '.ogg' );
            } else {
                fileType = 'mp3';// This isn't doing anything, really, but I feel better with it
            }

            fs.exists( audioFilePath, function( exists ) {
                if ( exists ) {
                    mimeType = mime.getType( audioFilePath );

                    // 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( audioFilePath );
                    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 audio files 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 ( response , fileType ) { var fs = require ( 'fs' ) , mime = require ( 'mime' ) , audioOption = this . getValidAudioOption ( ) , audioFileName = audioOption ? audioOption . path : '' , // If there's no audioOption, we set the file name as empty audioFilePath = __dirname + '/audios/' + audioFileName , mimeType , stream ; // If the file name is empty, we skip any work and return a 404 response if ( audioFileName ) { // We need to replace '.mp3' with '.ogg' if the fileType === 'ogg' if ( fileType === 'ogg' ) { audioFileName = audioFileName . replace ( / \.mp3 / gi , '.ogg' ) ; audioFilePath = audioFilePath . replace ( / \.mp3 / gi , '.ogg' ) ; } else { fileType = 'mp3' ; // This isn't doing anything, really, but I feel better with it } fs . exists ( audioFilePath , function ( exists ) { if ( exists ) { mimeType = mime . getType ( audioFilePath ) ; // 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 ( audioFilePath ) ; 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 audio files 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 audio file @param response Node's response object @param fileType defaults to 'mp3', can also be 'ogg'
Stream audio file