From 1b9242788ae334be6577dc4fbfcc1bc9a42df484 Mon Sep 17 00:00:00 2001 From: Rahul Padigela Date: Fri, 6 Jul 2018 15:22:25 -0700 Subject: [PATCH] bugfix: ZENKO-632 check if destroy method is available s3-data returns Readable unlike sproxydclient which returns an instance of http.IncomingMessage which implements Readable stream and extends it with destroy method --- lib/s3routes/routesUtils.js | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/s3routes/routesUtils.js b/lib/s3routes/routesUtils.js index 9286a1fb9..35c97c617 100644 --- a/lib/s3routes/routesUtils.js +++ b/lib/s3routes/routesUtils.js @@ -284,13 +284,19 @@ function retrieveData(locations, retrieveDataFn, response, log) { response.destroy(); responseDestroyed = true; }; + + const _destroyReadable = readable => { + // s3-data sends Readable stream only which does not implement destroy + if (readable && readable.destroy) { + readable.destroy(); + } + }; + // the S3-client might close the connection while we are processing it response.once('close', () => { log.debug('received close event before response end'); responseDestroyed = true; - if (currentStream) { - currentStream.destroy(); - } + _destroyReadable(currentStream); }); return eachSeries(locations, @@ -311,7 +317,7 @@ function retrieveData(locations, retrieveDataFn, response, log) { if (responseDestroyed || response.isclosed) { log.debug( 'response destroyed before readable could stream'); - readable.destroy(); + _destroyReadable(readable); return next(responseErr); } // readable stream successfully consumed