From e8953e678c6a2e493bd6d21f7a3a200d5fd3e922 Mon Sep 17 00:00:00 2001 From: segayuu Date: Sat, 30 May 2020 18:03:09 +0900 Subject: [PATCH] refactor(box): reduce Stream (#4333) --- lib/box/index.js | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/lib/box/index.js b/lib/box/index.js index dcb0f7a924..8622188714 100644 --- a/lib/box/index.js +++ b/lib/box/index.js @@ -216,20 +216,17 @@ function escapeBackslash(path) { } function getHash(path) { - return new Promise((resolve, reject) => { - const src = createReadStream(path); - const hasher = createSha1Hash(); - let hash_value; - - src.pipe(hasher) - .on('data', chunk => { - hash_value = chunk.toString('hex'); - }) - .on('end', () => { - resolve(hash_value); - }) - .on('error', reject); + const src = createReadStream(path); + const hasher = createSha1Hash(); + + const finishedPromise = new Promise((resolve, reject) => { + src.once('error', reject); + src.once('end', resolve); }); + + src.on('data', chunk => { hasher.update(chunk); }); + + return finishedPromise.then(() => hasher.digest('hex')); } function toRegExp(ctx, arg) {