Skip to content

Commit

Permalink
[front] Handle backpressure manually to avoid getting stuck (#9575)
Browse files Browse the repository at this point in the history
  • Loading branch information
tdraier authored Dec 21, 2024
1 parent c78d4b4 commit ef479bb
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion front/lib/api/files/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,32 @@ const extractTextFromFileAndUpload: ProcessingFunction = async (
config.getTextExtractionUrl()
).fromStream(readStream, file.contentType);

await pipeline(processedStream, writeStream);
processedStream.on("data", (chunk) => {
// Handle backpressure
const canWrite = writeStream.write(chunk);
if (!canWrite) {
readStream.pause();
writeStream.once("drain", () => readStream.resume());
}
});

processedStream.on("end", () => {
writeStream.end();
});

processedStream.on("error", (err) => {
throw err;
});

writeStream.on("error", (err) => {
throw err;
});

// Await the completion of the writeStream
await new Promise<void>((resolve, reject) => {
writeStream.on("finish", resolve);
writeStream.on("error", reject);
});

return new Ok(undefined);
} catch (err) {
Expand Down

0 comments on commit ef479bb

Please sign in to comment.