fs: add stream utilities to `FileHandle`
Adding filehandle.createReadStream
and filehandle.createWriteStream
methods to FileHandle
class. This is useful to be able create ReadStream
or WriteStream
from fs/promises
.
import { open } from 'node:fs/promises';
import { stdout } from 'node:process';
try {
const textFile = await open(new URL('./file.txt', import.meta.url));
textFile.createReadStream().pipe(stdout);
} catch (err) {
// error while opening the file
// ...
}
It also accept the same functions as fs.createReadStream
and fs.createWriteStream
respectively:
import { open } from 'node:fs/promises';
import { stdout } from 'node:process';
import { finished } from 'node:stream/promises';
let textFile;
try {
textFile = await open(new URL('./file.txt', import.meta.url));
await finished(textFile.createReadStream({ autoClose: false, encoding: 'utf8' }).pipe(stdout));
} catch (err) {
// error while opening the file or while streaming the file
// ...
} finally {
await textFile?.close();
}