streaming write with `Readable.from`

pull/5/head
mehdi 3 years ago
parent 7404ee1bc2
commit 8c4f56a04a
  1. 20
      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)
}

Loading…
Cancel
Save