Skip to content

Commit ae73d2a

Browse files
ducletSergi Almacellas Abellana
authored and
Sergi Almacellas Abellana
committed
Use chunk size to determine the processed length
Fixes #736 #743
1 parent a318396 commit ae73d2a

File tree

3 files changed

+110
-4
lines changed

3 files changed

+110
-4
lines changed

papaparse.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -682,7 +682,8 @@ License: MIT
682682
return;
683683
}
684684

685-
this._start += xhr.responseText.length;
685+
// Use chunckSize as it may be a diference on reponse lentgh due to characters with more than 1 byte
686+
this._start += this._config.chunkSize ? this._config.chunkSize : xhr.responseText.length;
686687
this._finished = !this._config.chunkSize || this._start >= getFileSize(xhr);
687688
this.parseChunk(xhr.responseText);
688689
};
@@ -1100,7 +1101,10 @@ License: MIT
11001101
{
11011102
_paused = true;
11021103
_parser.abort();
1103-
_input = _input.substring(_parser.getCharIndex());
1104+
1105+
// If it is streaming via "chunking", the reader will start appending correctly already so no need to substring,
1106+
// otherwise we can get duplicate content within a row
1107+
_input = isFunction(_config.chunk) ? "" : _input.substring(_parser.getCharIndex());
11041108
};
11051109

11061110
this.resume = function()

tests/test-cases.js

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1897,6 +1897,108 @@ var CUSTOM_TESTS = [
18971897
});
18981898
}
18991899
},
1900+
{
1901+
description: "Pause and resume works for chunks with NetworkStreamer",
1902+
disabled: !XHR_ENABLED,
1903+
timeout: 30000,
1904+
expected: ["Etiam a dolor vitae est vestibulum", "84", "DEF"],
1905+
run: function(callback) {
1906+
var chunkNum = 0;
1907+
Papa.parse(BASE_PATH + "verylong-sample.csv", {
1908+
download: true,
1909+
chunkSize: 1000,
1910+
chunk: function(results, parser) {
1911+
chunkNum++;
1912+
parser.pause();
1913+
1914+
if (chunkNum === 2) {
1915+
callback(results.data[0]);
1916+
return;
1917+
}
1918+
1919+
parser.resume();
1920+
},
1921+
complete: function() {
1922+
callback(new Error("Should have found matched row before parsing whole file"));
1923+
}
1924+
});
1925+
}
1926+
},
1927+
{
1928+
description: "Pause and resume works for chunks with FileStreamer",
1929+
disabled: !XHR_ENABLED,
1930+
timeout: 30000,
1931+
expected: ["Etiam a dolor vitae est vestibulum", "84", "DEF"],
1932+
run: function(callback) {
1933+
var chunkNum = 0;
1934+
var xhr = new XMLHttpRequest();
1935+
xhr.onload = function() {
1936+
Papa.parse(new File([xhr.responseText], './verylong-sample.csv'), {
1937+
chunkSize: 1000,
1938+
chunk: function(results, parser) {
1939+
chunkNum++;
1940+
parser.pause();
1941+
1942+
if (chunkNum === 2) {
1943+
callback(results.data[0]);
1944+
return;
1945+
}
1946+
1947+
parser.resume();
1948+
},
1949+
complete: function() {
1950+
callback(new Error("Should have found matched row before parsing whole file"));
1951+
}
1952+
});
1953+
};
1954+
1955+
xhr.open("GET", BASE_PATH + "verylong-sample.csv");
1956+
try {
1957+
xhr.send();
1958+
} catch (err) {
1959+
callback(err);
1960+
return;
1961+
}
1962+
}
1963+
},
1964+
{
1965+
description: "Pause and resume works for chunks with StringStreamer",
1966+
disabled: !XHR_ENABLED,
1967+
timeout: 30000,
1968+
// Test also with string as byte size may be diferent
1969+
expected: ["Etiam a dolor vitae est vestibulum", "84", "DEF"],
1970+
run: function(callback) {
1971+
var chunkNum = 0;
1972+
var xhr = new XMLHttpRequest();
1973+
xhr.onload = function() {
1974+
Papa.parse(xhr.responseText, {
1975+
chunkSize: 1000,
1976+
chunk: function(results, parser) {
1977+
chunkNum++;
1978+
parser.pause();
1979+
1980+
if (chunkNum === 2) {
1981+
callback(results.data[0]);
1982+
return;
1983+
}
1984+
1985+
parser.resume();
1986+
},
1987+
complete: function() {
1988+
callback(new Error("Should have found matched row before parsing whole file"));
1989+
}
1990+
});
1991+
};
1992+
1993+
xhr.open("GET", BASE_PATH + "verylong-sample.csv");
1994+
try {
1995+
xhr.send();
1996+
} catch (err) {
1997+
callback(err);
1998+
return;
1999+
}
2000+
}
2001+
},
19002002
{
19012003
description: "Complete is called with all results if neither step nor chunk is defined",
19022004
expected: [['A', 'b', 'c'], ['d', 'E', 'f'], ['G', 'h', 'i']],

tests/verylong-sample.csv

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
placeholder,meaning of life,TLD
22
Lorem ipsum dolor sit,42,ABC
33
Etiam a dolor vitae est vestibulum,84,DEF
4-
Lorem ipsum dolor sit,42,ABC
4+
"Lorem ipsum dolor sit",42,ABC
55
Etiam a dolor vitae est vestibulum,84,DEF
66
Etiam a dolor vitae est vestibulum,84,DEF
77
Lorem ipsum dolor sit,42,ABC
@@ -1998,4 +1998,4 @@ Lorem ipsum dolor sit,42,ABC
19981998
Lorem ipsum dolor sit,42,ABC
19991999
Etiam a dolor vitae est vestibulum,84,DEF
20002000
Lorem ipsum dolor sit,42
2001-
Lorem ipsum dolor sit,42,ABC
2001+
Lorem ipsum dolor sit,42,ABC

0 commit comments

Comments
 (0)