From 8c4f56a04a25d2411e5de627281e23bca7a807af Mon Sep 17 00:00:00 2001 From: mehdi Date: Fri, 15 Oct 2021 11:01:06 +0200 Subject: [PATCH] streaming write with `Readable.from` --- lib/storage.js | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/lib/storage.js b/lib/storage.js index d244733..97cd0da 100755 --- a/lib/storage.js +++ b/lib/storage.js @@ -10,6 +10,7 @@ const fs = require('fs') const path = require('path') const async = require('async') const storage = {} +const { Readable } = require('stream') // eslint-disable-next-line node/no-callback-literal storage.exists = (path, cb) => fs.access(path, fs.constants.F_OK, (err) => { cb(!err) }) @@ -90,17 +91,20 @@ storage.crashSafeWriteFileLines = (filename, lines, callback = () => {}) => { cb => { try { const stream = fs.createWriteStream(tempFilename) - const next = () => { - const line = lines.shift() - if (line === undefined) { - stream.close(cb) - } else { + const readable = Readable.from(lines) + readable.on('data', (line) => { + try { stream.write(line) stream.write('\n') - setImmediate(next) + } catch (err) { + cb(err) } - } - next() + }) + readable.on('end', () => { + stream.close(cb) + }) + readable.on('error', () => cb) + stream.on('error', () => cb) } catch (err) { cb(err) }