Align behavior of treatRawData and treatRawStream in handling last blank line; On error, result is null

pull/5/head
eliot-akira 3 years ago
parent af5c5d7611
commit a826a9c3e0
  1. 15
      lib/persistence.js
  2. 2
      test/persistence.test.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) {

@ -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)

Loading…
Cancel
Save