From: Justin Wind Date: Wed, 27 Apr 2022 22:17:50 +0000 (-0700) Subject: add option to bodyData() to not render into string X-Git-Tag: v1.2.6^2~6 X-Git-Url: https://git.squeep.com/?a=commitdiff_plain;h=87d3be22d14dc94df3bc0b3bc37b92c2c5decd71;p=squeep-api-dingus add option to bodyData() to not render into string --- 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 () {