From 511d20474f176ed8f350d9ceb844cf7ffc0a195b Mon Sep 17 00:00:00 2001
From: cgeek <cem.moreau@gmail.com>
Date: Mon, 31 May 2021 18:28:19 +0200
Subject: [PATCH] fix: #1430 sync is keeping chunks in memory

---
 app/modules/crawler/lib/sync/v2/DownloadStream.ts | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/app/modules/crawler/lib/sync/v2/DownloadStream.ts b/app/modules/crawler/lib/sync/v2/DownloadStream.ts
index 496a55d3a..a08a7c8dc 100644
--- a/app/modules/crawler/lib/sync/v2/DownloadStream.ts
+++ b/app/modules/crawler/lib/sync/v2/DownloadStream.ts
@@ -156,7 +156,11 @@ export class DownloadStream extends Duplex {
       // Asks for next chunk: do we have it?
       if (this.chunks[this.currentChunkNumber]) {
         this.push(this.chunks[this.currentChunkNumber]);
-        delete this.chunks[this.currentChunkNumber];
+        const previousNumber = this.currentChunkNumber - 1;
+        if (previousNumber >= 0) {
+          // Delete the **previous**, not the current (because current would be downloaded again for chaining check)
+          delete this.chunks[previousNumber];
+        }
         // Let's start the download of next chunk
         this.currentChunkNumber++;
         let p = this.downloadChunk(this.currentChunkNumber);
-- 
GitLab