import { streams } from "https://deno.land/x/postgres@ls/tests/test_deps.ts";
const { iterateReader } = streams;
Turns a Reader, r
, into an async iterator.
import { iterateReader } from "https://deno.land/std@0.224.0/streams/conversion.ts";
let f = await Deno.open("/etc/passwd");
for await (const chunk of iterateReader(f)) {
console.log(chunk);
}
f.close();
Second argument can be used to tune size of a buffer. Default size of the buffer is 32kB.
import { iterateReader } from "https://deno.land/std@0.224.0/streams/conversion.ts";
let f = await Deno.open("/etc/passwd");
const it = iterateReader(f, {
bufSize: 1024 * 1024
});
for await (const chunk of it) {
console.log(chunk);
}
f.close();
Iterator uses an internal buffer of fixed size for efficiency; it returns a view on that buffer on each iteration. It is therefore caller's responsibility to copy contents of the buffer if needed; otherwise the next iteration will overwrite contents of previously returned chunk.