function
function
Explore posts from servers
DDeno
Created by function on 10/11/2024 in #help
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);
});
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)
4 replies
DDeno
Created by function on 10/11/2024 in #help
Deno.serve() + sharp + streams
Hi, i'd like to read an image, convert it, and respond with the result. I'd like to optimise for performance (request time) and low memory usage as much as possible, which is why i tried to use Streams. Unfortunately, i couldn't get my code to work with Streams and i'm very confused between the JavaScript native Streams, Deno's stdlib, and Node's streams module. Here's my tiny example which works but is using toBuffer() which i think could be optimised using Streams:
import sharp from "npm:sharp";

Deno.serve(async () => new Response(
await sharp("img.jpg").webp().toBuffer()
));
import sharp from "npm:sharp";

Deno.serve(async () => new Response(
await sharp("img.jpg").webp().toBuffer()
));
How can i change this code to use Streams and go as fast as possible?
34 replies