2017-04-11 00:24:42 +00:00
|
|
|
// Flags: --expose-internals
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const common = require('../common');
|
|
|
|
const assert = require('assert');
|
|
|
|
const fs = require('fs');
|
2018-05-15 19:34:49 +00:00
|
|
|
const SyncWriteStream = require('internal/fs/sync_write_stream');
|
2017-04-11 00:24:42 +00:00
|
|
|
|
2017-12-25 06:38:11 +00:00
|
|
|
const tmpdir = require('../common/tmpdir');
|
|
|
|
tmpdir.refresh();
|
2017-04-11 00:24:42 +00:00
|
|
|
|
2023-08-15 13:45:44 +00:00
|
|
|
const filename = tmpdir.resolve('sync-write-stream.txt');
|
2017-04-11 00:24:42 +00:00
|
|
|
|
2017-06-16 15:49:20 +00:00
|
|
|
// Verify constructing the instance with default options.
|
2017-04-11 00:24:42 +00:00
|
|
|
{
|
|
|
|
const stream = new SyncWriteStream(1);
|
|
|
|
|
|
|
|
assert.strictEqual(stream.fd, 1);
|
|
|
|
assert.strictEqual(stream.readable, false);
|
|
|
|
assert.strictEqual(stream.autoClose, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify constructing the instance with specified options.
|
|
|
|
{
|
|
|
|
const stream = new SyncWriteStream(1, { autoClose: false });
|
|
|
|
|
|
|
|
assert.strictEqual(stream.fd, 1);
|
|
|
|
assert.strictEqual(stream.readable, false);
|
|
|
|
assert.strictEqual(stream.autoClose, false);
|
|
|
|
}
|
|
|
|
|
2018-01-06 18:34:27 +00:00
|
|
|
// Verify that the file will be written synchronously.
|
2017-04-11 00:24:42 +00:00
|
|
|
{
|
|
|
|
const fd = fs.openSync(filename, 'w');
|
|
|
|
const stream = new SyncWriteStream(fd);
|
|
|
|
const chunk = Buffer.from('foo');
|
|
|
|
|
2023-06-26 09:01:35 +00:00
|
|
|
let calledSynchronously = false;
|
|
|
|
stream._write(chunk, null, common.mustCall(() => {
|
|
|
|
calledSynchronously = true;
|
|
|
|
}, 1));
|
|
|
|
|
|
|
|
assert.ok(calledSynchronously);
|
2017-04-11 00:24:42 +00:00
|
|
|
assert.strictEqual(fs.readFileSync(filename).equals(chunk), true);
|
2019-07-30 06:50:21 +00:00
|
|
|
|
|
|
|
fs.closeSync(fd);
|
2017-04-11 00:24:42 +00:00
|
|
|
}
|
|
|
|
|
2017-06-16 15:49:20 +00:00
|
|
|
// Verify that the stream will unset the fd after destroy().
|
2017-04-11 00:24:42 +00:00
|
|
|
{
|
|
|
|
const fd = fs.openSync(filename, 'w');
|
|
|
|
const stream = new SyncWriteStream(fd);
|
|
|
|
|
2019-03-15 18:58:42 +00:00
|
|
|
stream.on('close', common.mustCall());
|
|
|
|
assert.strictEqual(stream.destroy(), stream);
|
|
|
|
assert.strictEqual(stream.fd, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that the stream will unset the fd after destroySoon().
|
|
|
|
{
|
|
|
|
const fd = fs.openSync(filename, 'w');
|
|
|
|
const stream = new SyncWriteStream(fd);
|
2017-04-11 00:24:42 +00:00
|
|
|
|
2019-03-15 18:58:42 +00:00
|
|
|
stream.on('close', common.mustCall());
|
|
|
|
assert.strictEqual(stream.destroySoon(), stream);
|
2017-04-11 00:24:42 +00:00
|
|
|
assert.strictEqual(stream.fd, null);
|
|
|
|
}
|
|
|
|
|
2024-09-14 14:31:13 +00:00
|
|
|
// Verify that the file is not closed when autoClose=false
|
|
|
|
{
|
|
|
|
const fd = fs.openSync(filename, 'w');
|
|
|
|
const stream = new SyncWriteStream(fd, { autoClose: false });
|
|
|
|
|
|
|
|
stream.on('close', common.mustCall());
|
|
|
|
|
|
|
|
assert.strictEqual(stream.destroy(), stream);
|
|
|
|
fs.fstatSync(fd); // Does not throw
|
|
|
|
fs.closeSync(fd);
|
|
|
|
}
|
|
|
|
|
2019-03-15 18:58:42 +00:00
|
|
|
// Verify that calling end() will also destroy the stream.
|
2017-04-11 00:24:42 +00:00
|
|
|
{
|
|
|
|
const fd = fs.openSync(filename, 'w');
|
|
|
|
const stream = new SyncWriteStream(fd);
|
|
|
|
|
|
|
|
assert.strictEqual(stream.fd, fd);
|
|
|
|
|
2019-03-15 18:58:42 +00:00
|
|
|
stream.end();
|
2019-11-30 14:39:44 +00:00
|
|
|
stream.on('close', common.mustCall(() => {
|
|
|
|
assert.strictEqual(stream.fd, null);
|
|
|
|
}));
|
2017-04-11 00:24:42 +00:00
|
|
|
}
|
2023-06-26 14:18:58 +00:00
|
|
|
|
|
|
|
// Verify that an error on _write() triggers an 'error' event.
|
|
|
|
{
|
|
|
|
const fd = fs.openSync(filename, 'w');
|
|
|
|
const stream = new SyncWriteStream(fd);
|
|
|
|
|
|
|
|
assert.strictEqual(stream.fd, fd);
|
|
|
|
stream._write({}, null, common.mustCall((err) => {
|
|
|
|
assert(err);
|
|
|
|
fs.closeSync(fd);
|
|
|
|
}));
|
|
|
|
}
|