From 3e813ca2513ac02c15e819bc659ee8e63fd02b5c Mon Sep 17 00:00:00 2001 From: DebaucheryLibrarian Date: Fri, 5 Feb 2021 03:13:10 +0100 Subject: [PATCH] Prevent writing to media hasher when hasher stream has closed. --- src/media.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/media.js b/src/media.js index 05642a56..a6258360 100644 --- a/src/media.js +++ b/src/media.js @@ -504,10 +504,10 @@ async function fetchSource(source, baseMedia) { const maxAttempts = source.attempts || 3; logger.silly(`Fetching media from ${source.src}`); - // attempts async function attempt(attempts = 1) { const hasher = new blake2.Hash('blake2b', { digestLength: 24 }); + let hasherReady = true; hasher.setEncoding('hex'); try { @@ -519,7 +519,10 @@ async function fetchSource(source, baseMedia) { hashStream.on('data', (chunk) => { size += chunk.length; - hasher.write(chunk); + + if (hasherReady) { + hasher.write(chunk); + } }); const { mimetype } = source.stream @@ -551,6 +554,7 @@ async function fetchSource(source, baseMedia) { }, }; } catch (error) { + hasherReady = false; hasher.end(); if (error.code !== 'VERIFY_TYPE') {