Taming the Streams API in Deno

This is a working guide to the Streams API following the latest web standards.

Using Deno to fetch a large file and stream it to disk

async function fetchAndWriteToDisk(url: string, destinationPath: string) {
    const response = await fetch(url);
    const readableStream = response.body;
    if (!readableStream) {
        throw new Error("Failed to fetch readableStream");
    }

    const diskWritableStream = await Deno.open(destinationPath, {
        create: true,
        write: true,
    });

    await readableStream.pipeTo(diskWritableStream.writable);
    console.log(`File successfully written to ${destinationPath}`);
}


const destinationPath = "largefile.jpg";
const fileUrl = "https://i.pcmag.com/imagery/articles/02KW1KBEjvhGDCkfRNdcLxb-17..v1569486814.jpg";

await fetchAndWriteToDisk(fileUrl, destinationPath);

This snippet fetches a file and writes it to disk. It uses the pipeTo method to pipe the ReadableStream from the fetch response to the WritableStream of the Deno.open call.

Updated April 17, 2023