mirror of
https://github.com/nodejs/node.git
synced 2025-08-15 13:48:44 +02:00

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>
105 lines
2.3 KiB
JavaScript
105 lines
2.3 KiB
JavaScript
'use strict';
|
|
|
|
const common = require('../common');
|
|
const tmpdir = require('../common/tmpdir');
|
|
const {
|
|
ok,
|
|
strictEqual,
|
|
} = require('node:assert');
|
|
const {
|
|
openSync,
|
|
fsyncSync,
|
|
writeSync,
|
|
write,
|
|
} = require('node:fs');
|
|
const { join } = require('node:path');
|
|
const { Utf8Stream } = require('node:fs');
|
|
const { isMainThread } = require('node:worker_threads');
|
|
|
|
tmpdir.refresh();
|
|
if (isMainThread) {
|
|
process.umask(0o000);
|
|
}
|
|
|
|
let fileCounter = 0;
|
|
|
|
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 fsOverride = {
|
|
fsync: common.mustNotCall(),
|
|
fsyncSync: common.mustCall(() => fsyncSync(fd)),
|
|
};
|
|
if (sync) {
|
|
fsOverride.writeSync = common.mustCall((...args) => writeSync(...args));
|
|
fsOverride.write = common.mustNotCall();
|
|
} else {
|
|
fsOverride.write = common.mustCall((...args) => write(...args));
|
|
fsOverride.writeSync = common.mustNotCall();
|
|
}
|
|
|
|
const stream = new Utf8Stream({
|
|
fd,
|
|
sync,
|
|
fsync: true,
|
|
minLength: 4096,
|
|
fs: fsOverride,
|
|
});
|
|
|
|
stream.on('ready', common.mustCall(() => {
|
|
ok(stream.write('hello world\n'));
|
|
|
|
stream.flush(common.mustSucceed(() => stream.end()));
|
|
}));
|
|
}
|
|
|
|
{
|
|
const dest = getTempFile();
|
|
const fd = openSync(dest, 'w');
|
|
|
|
const testError = new Error('fsync failed');
|
|
testError.code = 'ETEST';
|
|
|
|
const fsOverride = {
|
|
fsync: common.mustCall((fd, cb) => {
|
|
process.nextTick(() => cb(testError));
|
|
}, 2),
|
|
};
|
|
|
|
if (sync) {
|
|
fsOverride.writeSync = common.mustCall((...args) => {
|
|
return writeSync(...args);
|
|
});
|
|
} else {
|
|
fsOverride.write = common.mustCall((...args) => {
|
|
return write(...args);
|
|
});
|
|
}
|
|
|
|
const stream = new Utf8Stream({
|
|
fd,
|
|
sync,
|
|
minLength: 4096,
|
|
fs: fsOverride,
|
|
});
|
|
|
|
stream.on('ready', common.mustCall(() => {
|
|
ok(stream.write('hello world\n'));
|
|
stream.flush(common.mustCall((err) => {
|
|
ok(err, 'flush should return an error');
|
|
strictEqual(err.code, 'ETEST');
|
|
stream.end();
|
|
}));
|
|
}));
|
|
}
|
|
}
|