node/test/parallel/test-fastutf8stream-flush-sync.js
James M Snell 5335c101a9 fs: port SonicBoom module to fs module as Utf8Stream
As a first step to porting portions of the pino structured
logger into the runtime, this commit ports the SonicBoom
module to the fs module as Utf8Stream.

This is a faithful port of the SonicBoom module with some
modern updates, such as converting to a Class and using
Symbol.dispose. The bulk of the implementation is unchanged
from the original.

PR-URL: https://github.com/nodejs/node/pull/58897
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Vinícius Lourenço Claro Cardoso <contact@viniciusl.com.br>
2025-07-28 20:27:07 -07:00

139 lines
3 KiB
JavaScript

'use strict';
const common = require('../common');
const tmpdir = require('../common/tmpdir');
const {
ok,
strictEqual,
throws,
} = require('node:assert');
const {
openSync,
readFile,
readFileSync,
writeSync,
} = require('node:fs');
const { Utf8Stream } = require('node:fs');
const { join } = require('node:path');
const { isMainThread } = require('node:worker_threads');
tmpdir.refresh();
let fileCounter = 0;
if (isMainThread) {
process.umask(0o000);
}
function getTempFile() {
return join(tmpdir.path, `fastutf8stream-${process.pid}-${Date.now()}-${fileCounter++}.log`);
}
runTests(false);
runTests(true);
function runTests(sync) {
const dest = getTempFile();
const fd = openSync(dest, 'w');
const stream = new Utf8Stream({ fd, minLength: 4096, sync });
ok(stream.write('hello world\n'));
ok(stream.write('something else\n'));
stream.flushSync();
setImmediate(common.mustCall(() => {
stream.end();
const data = readFileSync(dest, 'utf8');
strictEqual(data, 'hello world\nsomething else\n');
stream.on('close', common.mustCall());
}));
}
{
const dest = getTempFile();
const fd = openSync(dest, 'w');
let reportEagain = true;
const fsOverride = {
writeSync: common.mustCall((...args) => {
if (reportEagain) {
reportEagain = false;
const err = new Error('EAGAIN');
err.code = 'EAGAIN';
throw err;
}
writeSync(...args);
}, 2),
};
const stream = new Utf8Stream({
fd,
sync: false,
minLength: 0,
fs: fsOverride,
});
stream.on('ready', common.mustCall(() => {
ok(stream.write('hello world\n'));
ok(stream.write('something else\n'));
stream.flushSync();
stream.end();
stream.on('finish', common.mustCall(() => {
readFile(dest, 'utf8', common.mustSucceed((data) => {
strictEqual(data, 'hello world\nsomething else\n');
}));
}));
}));
}
{
const dest = getTempFile();
const fd = openSync(dest, 'w');
let retryCallCount = 0;
const err = new Error('EAGAIN');
err.code = 'EAGAIN';
let reportError = true;
const fsOverride = {
writeSync: common.mustCall((...args) => {
if (reportError) {
reportError = false;
throw err;
}
return writeSync(...args);
}, 2),
};
const stream = new Utf8Stream({
fd,
sync: false,
minLength: 1000,
retryEAGAIN: (err, writeBufferLen, remainingBufferLen) => {
retryCallCount++;
strictEqual(err.code, 'EAGAIN');
strictEqual(writeBufferLen, 12);
strictEqual(remainingBufferLen, 0);
return false; // Don't retry
},
fs: fsOverride,
});
stream.on('ready', common.mustCall(() => {
ok(stream.write('hello world\n'));
throws(() => stream.flushSync(), err);
ok(stream.write('something else\n'));
stream.flushSync();
stream.end();
stream.on('finish', common.mustCall(() => {
readFile(dest, 'utf8', common.mustSucceed((data) => {
strictEqual(data, 'hello world\nsomething else\n');
strictEqual(retryCallCount, 1);
}));
}));
}));
}