From 74422ec6f494bf684bf04d9b4fc29311c267ed6e Mon Sep 17 00:00:00 2001 From: Connor Anderson Date: Thu, 25 Aug 2022 23:41:54 -0400 Subject: [PATCH] Bugfix: Fix application/pdf response type handling This commit corresponds to https://github.com/docusign/docusign-esign-node-client/issues/305 and provides a fix in line with the suggestions made therein. --- src/ApiClient.js | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/ApiClient.js b/src/ApiClient.js index 11c9c7cd5..e86a61763 100644 --- a/src/ApiClient.js +++ b/src/ApiClient.js @@ -569,16 +569,15 @@ } if (request.header['Accept'] === 'application/pdf') { + // Copy of https://github.com/visionmedia/superagent/blob/master/src/node/parsers/image.js until superagent can be upgraded request.parse( function (res, fn) { - res.data = ''; - res.setEncoding('binary'); - res.on( 'data', function (chunk) { res.data += chunk; } ); - res.on( 'end', function () { - try { - fn( null, res.data ); - } catch ( err ) { - fn( err ); - } + res.data = []; // Binary data needs binary storage + + res.on('data', (chunk) => { + res.data.push(chunk); + }); + res.on('end', () => { + fn(null, Buffer.concat(res.data)); }); }) }