From a504bc05859c302a483c468de92371548bba9e4a Mon Sep 17 00:00:00 2001 From: Vadim Demedes Date: Thu, 1 Jun 2017 21:30:11 +0300 Subject: [PATCH] reshape progress events --- index.js | 36 ++++++++++++------------------------ test/progress.js | 18 +++++++----------- 2 files changed, 19 insertions(+), 35 deletions(-) diff --git a/index.js b/index.js index 7337c47c3..7dc7dd197 100644 --- a/index.js +++ b/index.js @@ -113,10 +113,8 @@ function requestAsEventEmitter(opts) { ee.emit('uploadProgress', { percent: 1, - size: { - transferred: uploaded, - total: uploadBodySize - } + transferred: uploaded, + total: uploadBodySize }); const statusCode = res.statusCode; @@ -170,10 +168,8 @@ function requestAsEventEmitter(opts) { if (percent < 1) { ee.emit('downloadProgress', { percent, - size: { - transferred: downloaded, - total: downloadBodySize - } + transferred: downloaded, + total: downloadBodySize }); } @@ -183,10 +179,8 @@ function requestAsEventEmitter(opts) { flush(callback) { ee.emit('downloadProgress', { percent: 1, - size: { - transferred: downloaded, - total: downloadBodySize - } + transferred: downloaded, + total: downloadBodySize }); callback(); @@ -213,10 +207,8 @@ function requestAsEventEmitter(opts) { ee.emit('downloadProgress', { percent: 0, - size: { - transferred: 0, - total: downloadBodySize - } + transferred: 0, + total: downloadBodySize }); res.pipe(progressStream); @@ -239,10 +231,8 @@ function requestAsEventEmitter(opts) { ee.on('request', req => { ee.emit('uploadProgress', { percent: 0, - size: { - transferred: 0, - total: uploadBodySize - } + transferred: 0, + total: uploadBodySize }); req.connection.on('connect', () => { @@ -261,10 +251,8 @@ function requestAsEventEmitter(opts) { ee.emit('uploadProgress', { percent: uploadBodySize ? uploaded / uploadBodySize : 0, - size: { - transferred: uploaded, - total: uploadBodySize - } + transferred: uploaded, + total: uploadBodySize }); }, 150); }); diff --git a/test/progress.js b/test/progress.js index d3ca34ce1..69c1309f8 100644 --- a/test/progress.js +++ b/test/progress.js @@ -20,15 +20,15 @@ const checkEvents = (t, events, bodySize = null) => { events.forEach((event, index) => { if (hasBodySize) { - t.is(event.percent, event.size.transferred / bodySize); + t.is(event.percent, event.transferred / bodySize); t.true(event.percent > lastEvent.percent); } else { const isLastEvent = index === events.length - 1; t.is(event.percent, isLastEvent ? 1 : 0); } - t.true(event.size.transferred >= lastEvent.size.transferred); - t.is(event.size.total, bodySize); + t.true(event.transferred >= lastEvent.transferred); + t.is(event.total, bodySize); lastEvent = event; }); @@ -181,17 +181,13 @@ test('upload progress - no body', async t => { t.deepEqual(events, [ { percent: 0, - size: { - transferred: 0, - total: 0 - } + transferred: 0, + total: 0 }, { percent: 1, - size: { - transferred: 0, - total: 0 - } + transferred: 0, + total: 0 } ]); });