From 98e3fa13dcb98b2bd0e04078220f0edfba426c81 Mon Sep 17 00:00:00 2001 From: Bryce Gibson Date: Wed, 22 Feb 2017 16:12:26 +1100 Subject: [PATCH] Read file as Buffer, process lines as strings. This avoids/mitigates maximum string length issues in V8. --- lib/persistence.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/persistence.js b/lib/persistence.js index 88a4948b..14f2088c 100755 --- a/lib/persistence.js +++ b/lib/persistence.js @@ -208,14 +208,22 @@ Persistence.prototype.persistNewState = function (newDocs, cb) { * machine understandable collection */ Persistence.prototype.treatRawData = function (rawData) { - var data = rawData.split('\n') + var data = [] + , buffer = rawData , dataById = {} , tdata = [] , i , indexes = {} , corruptItems = -1 // Last line of every data file is usually blank so not really corrupt + , index = -1 ; + while( (index = buffer.indexOf('\n')) > -1 ) { + data = data.concat(buffer.slice(0, index).toString('utf8')); + buffer = buffer.slice(index + 1); // 1 for the '\n' itself + } + data = data.concat(buffer.toString('utf8')); // last line; intentionally adds even if empty + for (i = 0; i < data.length; i += 1) { var doc; @@ -274,7 +282,7 @@ Persistence.prototype.loadDatabase = function (cb) { function (cb) { Persistence.ensureDirectoryExists(path.dirname(self.filename), function (err) { storage.ensureDatafileIntegrity(self.filename, function (err) { - storage.readFile(self.filename, 'utf8', function (err, rawData) { + storage.readFile(self.filename, function (err, rawData) { if (err) { return cb(err); } try {