functiondjF
Denoβ€’15mo agoβ€’
3 replies
functiondj

Stream a Sharp object / Node Readable with Deno.serve()

This works but isn't quite as fast as piping to the response object of node:http's createServer.
Can i somehow pipe the Sharp object directly to the response without writing my own ReadableStream?
import sharp from "npm:sharp";

Deno.serve(() => {
    const thing = sharp("img.png").webp(); // 'thing' implements node:streams/Duplex

    const body = new ReadableStream({
        start(controller) {
            thing.on("data", (chunk) => controller.enqueue(chunk));
            thing.on("end", () => controller.close());
        },
    });

    return new Response(body);
});
(Note: cancel() and headers omitted for brevity)
Was this page helpful?