DenoDDeno
Powered by
functiondjF
Denoβ€’2y 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
node:http
's
createServer
createServer
.
Can i somehow pipe the Sharp object directly to the response without writing my own
ReadableStream
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);
});
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()
cancel()
and headers omitted for brevity)
Deno banner
DenoJoin
Chat about Deno, a modern runtime for JavaScript and TypeScript.
20,944Members
Resources
Recent Announcements

Similar Threads

Was this page helpful?

Similar Threads

Deno.serve() + sharp + streams
functiondjFfunctiondj / help
2y ago
node stream.Readable.on not a function.
JacobZwangJJacobZwang / help
2y ago
Deno.serve() doesn't stream
guest271314Gguest271314 / help
3y ago
deno serve logs?
Jack MJJack M / help
2y ago