From a826a9c3e08791dc24f368803145fdd77e75e97d Mon Sep 17 00:00:00 2001 From: eliot-akira Date: Tue, 5 Oct 2021 12:11:36 +0200 Subject: [PATCH] Align behavior of treatRawData and treatRawStream in handling last blank line; On error, result is null --- lib/persistence.js | 15 ++++++++++----- test/persistence.test.js | 2 +- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/lib/persistence.js b/lib/persistence.js index 81a9e71..4839693 100755 --- a/lib/persistence.js +++ b/lib/persistence.js @@ -158,6 +158,8 @@ class Persistence { const dataById = {} const tdata = [] const indexes = {} + + // Last line of every data file is usually blank so not really corrupt let corruptItems = -1 for (const datum of data) { @@ -192,9 +194,11 @@ class Persistence { const dataById = {} const tdata = [] const indexes = {} - let corruptItems = 0 - const lineStream = byline(rawStream) + // Last line of every data file is usually blank so not really corrupt + let corruptItems = -1 + + const lineStream = byline(rawStream, { keepEmptyLines: true }) let length = 0 lineStream.on('data', (line) => { @@ -214,16 +218,17 @@ class Persistence { lineStream.on('end', () => { // A bit lenient on corruption - let err = null if (length > 0 && corruptItems / length > this.corruptAlertThreshold) { - err = new Error(`More than ${Math.floor(100 * this.corruptAlertThreshold)}% of the data file is corrupt, the wrong beforeDeserialization hook may be used. Cautiously refusing to start NeDB to prevent dataloss`) + const err = new Error(`More than ${Math.floor(100 * this.corruptAlertThreshold)}% of the data file is corrupt, the wrong beforeDeserialization hook may be used. Cautiously refusing to start NeDB to prevent dataloss`) + cb(err, null) + return } Object.keys(dataById).forEach(function (k) { tdata.push(dataById[k]) }) - cb(err, { data: tdata, indexes: indexes }) + cb(null, { data: tdata, indexes: indexes }) }) lineStream.on('error', function (err) { diff --git a/test/persistence.test.js b/test/persistence.test.js index ba614c7..ec35f02 100755 --- a/test/persistence.test.js +++ b/test/persistence.test.js @@ -102,7 +102,7 @@ describe('Persistence', function () { stream.push(null) d.persistence.treatRawStream(stream, function (err, result) { - assert.isNotNull(err) + assert.isNull(err) const treatedData = result.data treatedData.sort(function (a, b) { return a._id - b._id }) treatedData.length.should.equal(2)