0

I have a problem with next.js and PDFKit stream. When I create new PDFDocument, convert the stream into new Readable stream and push the data to response, client receives data when doc.end() is fired even though readableStream receives data immediately.

function convertStream(data: any): ReadableStream { const readableStream = new ReadableStream({ start(controller) { data.on("data", (chunk: any) => { controller.enqueue(chunk); }); data.on("end", () => { controller.close(); }); data.on("error", (err: Error) => { controller.error(err); }); }, cancel() { data.end(); }, }); return readableStream; } const doc = new PDFDocument(); const stream = convertStream(doc); //doc content doc.end(); return new Response(stream, headers); 

When I use the same convert function on fs.createReadStream data is pushed to client right away. Please help.

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.