Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions lib/internal/fs/streams.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,19 @@ function _construct(callback) {
} else {
stream.fd = fd;
callback();
stream.emit('open', stream.fd);
stream.emit('ready');
// Ready must be deferred to the next tick, because callback will schedule
// some microtick work that we need to complete first.
process.nextTick(emitReady, stream);
}
});
}
}

function emitReady(stream) {
stream.emit('open', stream.fd);
stream.emit('ready');
}

// This generates an fs operations structure for a FileHandle
const FileHandleOperations = (handle) => {
return {
Expand Down
27 changes: 27 additions & 0 deletions test/parallel/test-fs-writestream-open-write.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict'

const common = require('../common');
const { strictEqual } = require('assert');
const { tmpdir } = require('os');
const { join } = require('path');
Comment thread
mcollina marked this conversation as resolved.
Outdated
const fs = require('fs');

// Regression test for https://github.com/nodejs/node/issues/51993

const file = join(tmpdir(), `test-fs-writestream-open-write-${process.pid}.txt`);
Comment thread
mcollina marked this conversation as resolved.
Outdated

const w = fs.createWriteStream(file);

w.on('open', common.mustCall(() => {
w.write('hello');

process.nextTick(() => {
w.write('world');
w.end();
});
}));

w.on('close', common.mustCall(() => {
strictEqual(fs.readFileSync(file, 'utf8'), 'helloworld');
fs.unlinkSync(file);
}));