From 87d3be22d14dc94df3bc0b3bc37b92c2c5decd71 Mon Sep 17 00:00:00 2001 From: Justin Wind Date: Wed, 27 Apr 2022 15:17:50 -0700 Subject: [PATCH] add option to bodyData() to not render into string --- lib/dingus.js | 8 ++++++-- test/lib/dingus.js | 8 ++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/dingus.js b/lib/dingus.js index 4eded66..3ca710e 100644 --- a/lib/dingus.js +++ b/lib/dingus.js @@ -349,8 +349,9 @@ class Dingus { * Return all body data from a request. * @param {http.ClientRequest} req * @param {Number=} maximumBodySize + * @param {Boolean=} toString */ - async bodyData(req, maximumBodySize) { + async bodyData(req, maximumBodySize, toString = true) { const _scope = _fileScope('bodyData'); return new Promise((resolve, reject) => { const body = []; @@ -363,7 +364,10 @@ class Dingus { reject(new ResponseError(Enum.ErrorResponse.RequestEntityTooLarge)); } }); - req.on('end', () => resolve(Buffer.concat(body).toString())); + req.on('end', () => { + const bodyBuffer = Buffer.concat(body); + resolve(toString ? bodyBuffer.toString() : bodyBuffer); + }); req.on('error', (e) => { this.logger.error(_scope, 'failed', { error: e }); reject(e); diff --git a/test/lib/dingus.js b/test/lib/dingus.js index 356974f..c889215 100644 --- a/test/lib/dingus.js +++ b/test/lib/dingus.js @@ -535,6 +535,14 @@ describe('Dingus', function () { assert.strictEqual(e.statusCode, 413); } }); + it('provides buffer', async function () { + const p = dingus.bodyData(res, 0, false); + const expected = Buffer.from('bleat'); + resEvents['data'](expected); + resEvents['end'](); + const result = await p; + assert.deepStrictEqual(result, expected); + }); }); // bodyData describe('ingestBody', function () { -- 2.45.2