From c8aebe23b0f1c4c9486e39555d132c9be08cf328 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Denis=20F=C3=A4cke?= Date: Sat, 5 Jan 2019 17:46:35 +0000 Subject: [PATCH] fix finish not being emitted after end() while reopening (#24) --- index.js | 8 ++++++++ test.js | 22 ++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/index.js b/index.js index 4c1c04f..7ef4a22 100644 --- a/index.js +++ b/index.js @@ -6,6 +6,7 @@ const flatstr = require('flatstr') const inherits = require('util').inherits function openFile (file, sonic) { + sonic._opening = true sonic._writing = true sonic.file = file fs.open(file, 'a', (err, fd) => { @@ -15,6 +16,7 @@ function openFile (file, sonic) { } sonic.fd = fd + sonic._opening = false sonic._writing = false sonic.emit('ready') @@ -197,6 +199,12 @@ SonicBoom.prototype.end = function () { return } + if (this._writing && this._opening) { + this.once('ready', () => { + actualWrite(this) + }) + } + if (this._writing) { return } diff --git a/test.js b/test.js index 28e75df..37ee045 100644 --- a/test.js +++ b/test.js @@ -348,6 +348,28 @@ function buildTests (test, sync) { }) }) + test('end after reopen', (t) => { + t.plan(4) + + const dest = file() + const stream = new SonicBoom(dest, 4096, sync) + + stream.once('ready', () => { + t.pass('ready emitted') + const after = dest + '-moved' + stream.reopen(after) + stream.write('after reopen\n') + stream.on('finish', () => { + t.pass('finish emitted') + fs.readFile(after, 'utf8', (err, data) => { + t.error(err) + t.equal(data, 'after reopen\n') + }) + }) + stream.end() + }) + }) + test('reopen with file', (t) => { t.plan(9)