brioche/file_sink
FileSink
created by Bun. A Bun FileSink
represents an in-memory buffer
storing data that will be periodically flushed on disk. FileSink
are used
for writing large files, or for writing to a file over a long period of
time, in an incremental manner. A FileSink
can easily be obtained from a
Bun File
, with writer
function.
When you need to manage or read files, heads up to
brioche/file.
instead.
Functions
pub fn end(sink: FileSink) -> Promise(Result(Int, Nil))
Closes the FileSink
, and let the process stops if needed.
import brioche/file
import brioche/file_sink
import gleam/javascript/promise
pub fn main() {
let csv = file.new("/tmp/my/file.csv")
let writer = file.writer(csv)
file_sink.write_bytes(writer, <<"example data">>)
use _ <- promise.await(file_sink.end(writer))
promise.resolve(Nil)
}
pub fn flush(sink: FileSink) -> Promise(Result(Int, Nil))
Flushes the buffer content on disk.
import brioche/file
import brioche/file_sink
import gleam/javascript/promise
pub fn main() {
let csv = file.new("/tmp/my/file.csv")
let writer = file.writer(csv)
file_sink.write_bytes(writer, <<"example data">>)
use _ <- promise.await(file_sink.flush(writer))
promise.resolve(Nil)
}
pub fn ref(sink: FileSink) -> FileSink
By default, Bun will not stop the process while a FileSink
is opened.
After a call to unref
, the FileSink
could be considered as
non-essential. ref
restores the default behaviour.
Returns the same file sink to allow chaining calls if needed.
pub fn unref(sink: FileSink) -> FileSink
By default, Bun will not stop the process while a FileSink
is opened.
unref
changes that behaviour, and considers FileSink
as non-essential.
ref
restores the default behaviour.
Returns the same file sink to allow chaining calls if needed.
pub fn write_bytes(sink: FileSink, data: BitArray) -> Int
Incrementally write the binary content in the file.
import brioche/file
import brioche/file_sink
pub fn main() {
let csv = file.new("/tmp/my/file.csv")
let writer = file.writer(csv)
file_sink.write_bytes(writer, <<"example data">>)
}
pub fn write_text(sink: FileSink, data: String) -> Int
Incrementally write the text content in the file.
import brioche/file
import brioche/file_sink
pub fn main() {
let csv = file.new("/tmp/my/file.csv")
let writer = file.writer(csv)
file_sink.write_text(writer, "example data")
}