hive/html/jquery/3rd/PapaParse-4.0.7/papaparse.js
// TODO: Many of the functions of NetworkStreamer and FileStreamer are similar or the same. Consolidate?
Many of the functions of NetworkStreamer and FileStreamer are similar or the same.
Many of the functions of NetworkStreamer and FileStreamer are similar or the same .
function NetworkStreamer(config)
    {
        config = config || {};
        if (!config.chunkSize)
            config.chunkSize = Papa.RemoteChunkSize;
        var start = 0, fileSize = 0, rowCount = 0;
        var aggregate = "";
        var partialLine = "";
        var xhr, url, nextChunk, finishedWithEntireFile;
        var userComplete, handle, configCopy;
        replaceConfig(config);
        this.resume = function()
        {
            paused = false;
            nextChunk();
        };
        this.finished = function()
        {
            return finishedWithEntireFile;
        };
        this.pause = function()
        {
            paused = true;
        };
        this.abort = function()
        {
            finishedWithEntireFile = true;
            if (isFunction(userComplete))
                userComplete({ data: [], errors: [], meta: { aborted: true } });
        };
        this.stream = function(u)
        {
            url = u;
            if (IS_WORKER)
            {
                nextChunk = function()
                {
                    readChunk();
                    chunkLoaded();
                };
            }
            else
            {
                nextChunk = function()
                {
                    readChunk();
                };
            }
            nextChunk();    
        };
        function readChunk()
        {
            if (finishedWithEntireFile)
            {
                chunkLoaded();
                return;
            }
            xhr = new XMLHttpRequest();
            if (!IS_WORKER)
            {
                xhr.onload = chunkLoaded;
                xhr.onerror = chunkError;
            }
            xhr.open("GET", url, !IS_WORKER);
            if (config.step || config.chunk)
            {
                var end = start + configCopy.chunkSize - 1;    
                if (fileSize && end > fileSize)    
                    end = fileSize;
                xhr.setRequestHeader("Range", "bytes="+start+"-"+end);
            }
            try {
                xhr.send();
            }
            catch (err) {
                chunkError(err.message);
            }
            if (IS_WORKER && xhr.status == 0)
                chunkError();
            else
                start += configCopy.chunkSize;
        }
        function chunkLoaded()
        {
            if (xhr.readyState != 4)
                return;
            if (xhr.status < 200 || xhr.status >= 400)
            {
                chunkError();
                return;
            }
            aggregate += partialLine + xhr.responseText;
            partialLine = "";
            finishedWithEntireFile = (!config.step && !config.chunk) || start > getFileSize(xhr);
            if (!finishedWithEntireFile)
            {
                var lastLineEnd = aggregate.lastIndexOf("\r");
                if (lastLineEnd == -1)
                    lastLineEnd = aggregate.lastIndexOf("\n");
                if (lastLineEnd != -1)
                {
                    partialLine = aggregate.substring(lastLineEnd + 1);    
                    aggregate = aggregate.substring(0, lastLineEnd);
                }
                else
                {
                    nextChunk();
                    return;
                }
            }
            var results = handle.parse(aggregate);
            aggregate = "";
            if (results && results.data)
                rowCount += results.data.length;
            var finishedIncludingPreview = finishedWithEntireFile || (configCopy.preview && rowCount >= configCopy.preview);
            if (IS_WORKER)
            {
                global.postMessage({
                    results: results,
                    workerId: Papa.WORKER_ID,
                    finished: finishedIncludingPreview
                });
            }
            else if (isFunction(config.chunk))
            {
                config.chunk(results, handle);
                results = undefined;
            }
            if (isFunction(userComplete) && finishedIncludingPreview)
                userComplete(results);
            if (!finishedIncludingPreview && (!results || !results.meta.paused))
                nextChunk();
        }
        function chunkError(errorMessage)
        {
            var errorText = xhr.statusText || errorMessage;
            if (isFunction(config.error))
                config.error(errorText);
            else if (IS_WORKER && config.error)
            {
                global.postMessage({
                    workerId: Papa.WORKER_ID,
                    error: errorText,
                    finished: false
                });
            }
        }
        function getFileSize(xhr)
        {
            var contentRange = xhr.getResponseHeader("Content-Range");
            return parseInt(contentRange.substr(contentRange.lastIndexOf("/") + 1));
        }
        function replaceConfig(config)
        {
            configCopy = copy(config);
            userComplete = configCopy.complete;
            configCopy.complete = undefined;
            configCopy.chunkSize = parseInt(configCopy.chunkSize);    
            handle = new ParserHandle(configCopy);
            handle.streamer = this;
        }
    }
function NetworkStreamer ( config ) { config = config || { } ; if ( ! config . chunkSize ) config . chunkSize = Papa . RemoteChunkSize ; var start = 0 , fileSize = 0 , rowCount = 0 ; var aggregate = "" ; var partialLine = "" ; var xhr , url , nextChunk , finishedWithEntireFile ; var userComplete , handle , configCopy ; replaceConfig ( config ) ; this . resume = function ( ) { paused = false ; nextChunk ( ) ; } ; this . finished = function ( ) { return finishedWithEntireFile ; } ; this . pause = function ( ) { paused = true ; } ; this . abort = function ( ) { finishedWithEntireFile = true ; if ( isFunction ( userComplete ) ) userComplete ( { data : [ ] , errors : [ ] , meta : { aborted : true } } ) ; } ; this . stream = function ( u ) { url = u ; if ( IS_WORKER ) { nextChunk = function ( ) { readChunk ( ) ; chunkLoaded ( ) ; } ; } else { nextChunk = function ( ) { readChunk ( ) ; } ; } nextChunk ( ) ; } ; function readChunk ( ) { if ( finishedWithEntireFile ) { chunkLoaded ( ) ; return ; } xhr = new XMLHttpRequest ( ) ; if ( ! IS_WORKER ) { xhr . onload = chunkLoaded ; xhr . onerror = chunkError ; } xhr . open ( "GET" , url , ! IS_WORKER ) ; if ( config . step || config . chunk ) { var end = start + configCopy . chunkSize - 1 ; if ( fileSize && end > fileSize ) end = fileSize ; xhr . setRequestHeader ( "Range" , "bytes=" + start + "-" + end ) ; } try { xhr . send ( ) ; } catch ( err ) { chunkError ( err . message ) ; } if ( IS_WORKER && xhr . status == 0 ) chunkError ( ) ; else start += configCopy . chunkSize ; } function chunkLoaded ( ) { if ( xhr . readyState != 4 ) return ; if ( xhr . status < 200 || xhr . status >= 400 ) { chunkError ( ) ; return ; } aggregate += partialLine + xhr . responseText ; partialLine = "" ; finishedWithEntireFile = ( ! config . step && ! config . chunk ) || start > getFileSize ( xhr ) ; if ( ! finishedWithEntireFile ) { var lastLineEnd = aggregate . lastIndexOf ( "\r" ) ; if ( lastLineEnd == - 1 ) lastLineEnd = aggregate . lastIndexOf ( "\n" ) ; if ( lastLineEnd != - 1 ) { partialLine = aggregate . substring ( lastLineEnd + 1 ) ; aggregate = aggregate . substring ( 0 , lastLineEnd ) ; } else { nextChunk ( ) ; return ; } } var results = handle . parse ( aggregate ) ; aggregate = "" ; if ( results && results . data ) rowCount += results . data . length ; var finishedIncludingPreview = finishedWithEntireFile || ( configCopy . preview && rowCount >= configCopy . preview ) ; if ( IS_WORKER ) { global . postMessage ( { results : results , workerId : Papa . WORKER_ID , finished : finishedIncludingPreview } ) ; } else if ( isFunction ( config . chunk ) ) { config . chunk ( results , handle ) ; results = undefined ; } if ( isFunction ( userComplete ) && finishedIncludingPreview ) userComplete ( results ) ; if ( ! finishedIncludingPreview && ( ! results || ! results . meta . paused ) ) nextChunk ( ) ; } function chunkError ( errorMessage ) { var errorText = xhr . statusText || errorMessage ; if ( isFunction ( config . error ) ) config . error ( errorText ) ; else if ( IS_WORKER && config . error ) { global . postMessage ( { workerId : Papa . WORKER_ID , error : errorText , finished : false } ) ; } } function getFileSize ( xhr ) { var contentRange = xhr . getResponseHeader ( "Content-Range" ) ; return parseInt ( contentRange . substr ( contentRange . lastIndexOf ( "/" ) + 1 ) ) ; } function replaceConfig ( config ) { configCopy = copy ( config ) ; userComplete = configCopy . complete ; configCopy . complete = undefined ; configCopy . chunkSize = parseInt ( configCopy . chunkSize ) ; handle = new ParserHandle ( configCopy ) ; handle . streamer = this ; } }
TODO: Many of the functions of NetworkStreamer and FileStreamer are similar or the same.
TODO : Many of the functions of NetworkStreamer and FileStreamer are similar or the same .
// Starts streaming // minus one because byte range is inclusive // Hack around a Chrome bug: http://stackoverflow.com/q/24745095/1048862 // Rejoin the line we likely just split in two by chunking the file // skip the line ending character // For chunk sizes smaller than a line (a line could not fit in a single chunk) // we simply build our aggregate by reading in the next chunk, until we find a newline // Deep-copy the config so we can edit it; we need // to call the complete function if we are to ensure // that the last chunk callback, if any, will be called // BEFORE the complete function. // VERY important so we don't concatenate strings!