mirror of
https://github.com/nodejs/node.git
synced 2025-08-15 21:58:48 +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>
111 lines
2.9 KiB
JavaScript
111 lines
2.9 KiB
JavaScript
'use strict';
|
|
|
|
const common = require('../common');
|
|
const tmpdir = require('../common/tmpdir'); ;
|
|
const {
|
|
ok,
|
|
strictEqual,
|
|
} = require('node:assert');
|
|
const {
|
|
readFile,
|
|
statSync,
|
|
writeFileSync,
|
|
} = require('node:fs');
|
|
const { join } = require('node:path');
|
|
const { Utf8Stream } = require('node:fs');
|
|
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`);
|
|
}
|
|
|
|
const isWindows = process.platform === 'win32';
|
|
|
|
|
|
runTests(false);
|
|
runTests(true);
|
|
|
|
function runTests(sync) {
|
|
{
|
|
const dest = getTempFile();
|
|
const mode = 0o666;
|
|
const stream = new Utf8Stream({ dest, sync, mode });
|
|
|
|
stream.on('ready', common.mustCall(() => {
|
|
ok(stream.write('hello world\n'));
|
|
ok(stream.write('something else\n'));
|
|
|
|
stream.end();
|
|
|
|
stream.on('finish', common.mustCall(() => {
|
|
readFile(dest, 'utf8', common.mustSucceed((data) => {
|
|
strictEqual(data, 'hello world\nsomething else\n');
|
|
// The actual mode may vary depending on the platform,
|
|
// so we check only the first bit.
|
|
strictEqual(statSync(dest).mode & 0o700, 0o600);
|
|
}));
|
|
}));
|
|
}));
|
|
}
|
|
|
|
{
|
|
const dest = getTempFile();
|
|
const defaultMode = 0o600;
|
|
const stream = new Utf8Stream({ dest, sync });
|
|
stream.on('ready', common.mustCall(() => {
|
|
ok(stream.write('hello world\n'));
|
|
ok(stream.write('something else\n'));
|
|
|
|
stream.end();
|
|
|
|
stream.on('finish', common.mustCall(() => {
|
|
readFile(dest, 'utf8', common.mustSucceed((data) => {
|
|
strictEqual(data, 'hello world\nsomething else\n');
|
|
strictEqual(statSync(dest).mode & 0o700, defaultMode);
|
|
}));
|
|
}));
|
|
}));
|
|
}
|
|
|
|
{
|
|
const dest = join(getTempFile(), 'out.log');
|
|
const mode = 0o666;
|
|
const stream = new Utf8Stream({ dest, mkdir: true, mode, sync });
|
|
|
|
stream.on('ready', common.mustCall(() => {
|
|
ok(stream.write('hello world\n'));
|
|
stream.flush();
|
|
stream.on('drain', common.mustCall(() => {
|
|
readFile(dest, 'utf8', common.mustSucceed((data) => {
|
|
strictEqual(data, 'hello world\n');
|
|
stream.end();
|
|
}));
|
|
}));
|
|
}));
|
|
}
|
|
|
|
{
|
|
const dest = getTempFile();
|
|
// Create file with writable mode first, then change mode after Utf8Stream creation
|
|
writeFileSync(dest, 'hello world\n', { encoding: 'utf8' });
|
|
const mode = isWindows ? 0o444 : 0o666;
|
|
const stream = new Utf8Stream({ dest, append: false, mode, sync });
|
|
stream.on('ready', common.mustCall(() => {
|
|
ok(stream.write('something else\n'));
|
|
stream.flush();
|
|
stream.on('drain', common.mustCall(() => {
|
|
readFile(dest, 'utf8', common.mustSucceed((data) => {
|
|
strictEqual(data, 'something else\n');
|
|
stream.end();
|
|
}));
|
|
}));
|
|
}));
|
|
}
|
|
}
|