From b5ba2b25cdf26bb142435629613c26454231f1ae Mon Sep 17 00:00:00 2001 From: Felix Kaspar Date: Sat, 21 Oct 2023 20:10:07 +0200 Subject: [PATCH] Fixed fs write error. --- public/wasm/pdfcpu-wrapper-node.js | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/public/wasm/pdfcpu-wrapper-node.js b/public/wasm/pdfcpu-wrapper-node.js index df6937b3..9e64c9e6 100644 --- a/public/wasm/pdfcpu-wrapper-node.js +++ b/public/wasm/pdfcpu-wrapper-node.js @@ -79,13 +79,21 @@ export async function oneToOne(wasmArray, snapshot) { await checkExistsWithTimeout("/output.pdf", 1000); console.log("Write started..."); - // TODO: [Important] This fails for large PDFs. Need to a way to check if file write is definitely done. - // We need to wait for the file write in memfs to finish in node for some reason - await new Promise((resolve, reject) => { - setTimeout(() => { - resolve(); - }, 1000); - }); + + // TODO: Make this more elegant, this waits for the write to finish. + // Maybe replace wasmfs with https://github.com/streamich/memfs + let fileSize; + while (true) { + fileSize = fs.statSync("/output.pdf").size; + await new Promise((resolve, reject) => { + setTimeout(() => { + resolve(); + }, 50); + }); + if(fileSize > 0 && fileSize == fs.statSync("/output.pdf").size) // Wait for file Size not changing anymore. + break; + } + console.log("Could be done?"); fs.unlinkSync("input.pdf");