From 97116ba3e5644ee0b295a49d4f92358693b0a823 Mon Sep 17 00:00:00 2001 From: nlf Date: Mon, 30 Jan 2023 08:58:29 -0800 Subject: [PATCH] fix: only flush the queue after open if not already writing (#25) --- lib/index.js | 4 +++- test/write.js | 27 +++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/lib/index.js b/lib/index.js index f3f2a88..f9d5082 100644 --- a/lib/index.js +++ b/lib/index.js @@ -290,7 +290,9 @@ class WriteStream extends EE { } else { this[_fd] = fd this.emit('open', fd) - this[_flush]() + if (!this[_writing]) { + this[_flush]() + } } } diff --git a/test/write.js b/test/write.js index 6fe54a0..a92d676 100644 --- a/test/write.js +++ b/test/write.js @@ -139,6 +139,33 @@ t.test('multiple writes', t => { s.on('finish', () => check(t)) }) }) + + t.test('async after open, writev delayed', t => { + const _fsm = t.mock('../', { + fs: { + ...fs, + writev: (...args) => { + setTimeout(fs.writev, 1000, ...args) // make writev very slow + }, + }, + }) + + const s = new _fsm.WriteStream(p) + s.on('open', fd => { + t.type(fd, 'number') + t.ok(s.write('a')) + t.notOk(s.write('b')) + t.notOk(s.write('c')) + t.notOk(s.write('d')) + t.notOk(s.write('e')) + t.notOk(s.write('f')) + t.notOk(s.write(Buffer.from('676869', 'hex'))) + t.notOk(s.write('jklm')) + t.notOk(s.write(Buffer.from('nop'))) + s.end() + s.on('finish', _ => check(t)) + }) + }) t.end() })