X-Git-Url: http://git.squeep.com/?p=squeep-api-dingus;a=blobdiff_plain;f=lib%2Fdingus.js;h=51da6fab6b2185680cace1f8d1ad48921283d59d;hp=1098ea7dc54041f57a6987bfad6b9560a149ae94;hb=3b70d88ed735041cb0ec358dfc478b825c1c12e7;hpb=4ec136ae8257a1a9f40d83ad61c57954226e79e0 diff --git a/lib/dingus.js b/lib/dingus.js index 1098ea7..51da6fa 100644 --- a/lib/dingus.js +++ b/lib/dingus.js @@ -311,21 +311,27 @@ class Dingus { /** - * Parse rawBody from ctx as contentType into parsedBody. - * @param {string} contentType - * @param {object} ctx - */ - parseBody(contentType, ctx) { + * Parse rawBody as contentType into ctx.parsedBody. + * @param {string} contentType + * @param {object} ctx + * @param {string|buffer} + */ + parseBody(contentType, ctx, rawBody) { const _scope = _fileScope('parseBody'); + if (!rawBody) { + // 1.2.4 and earlier expected rawBody on context + rawBody = ctx.rawBody; + } + switch (contentType) { case Enum.ContentType.ApplicationForm: - ctx.parsedBody = this.querystring.parse(ctx.rawBody); + ctx.parsedBody = this.querystring.parse(rawBody); break; case Enum.ContentType.ApplicationJson: try { - ctx.parsedBody = JSON.parse(ctx.rawBody); + ctx.parsedBody = JSON.parse(rawBody); } catch (e) { this.logger.debug(_scope, 'JSON parse failed', { requestId: ctx.requestId, error: e }); throw new ResponseError(Enum.ErrorResponse.BadRequest, e.message); @@ -371,11 +377,19 @@ class Dingus { * @param {http.ClientRequest} req * @param {http.ServerResponse} res * @param {object} ctx - */ - async ingestBody(req, res, ctx) { - ctx.rawBody = await this.bodyData(req); - const contentType = Dingus.getRequestContentType(req); - this.parseBody(contentType, ctx); + * @param {object} + * @param {Boolean} .parseEmptyBody + * @param {Boolean} .persistRawBody + */ + async ingestBody(req, res, ctx, { parseEmptyBody = true, persistRawBody = false, maximumBodySize } = {}) { + const rawBody = await this.bodyData(req, maximumBodySize); + if (persistRawBody) { + ctx.rawBody = rawBody; + } + if (rawBody || parseEmptyBody) { + const contentType = Dingus.getRequestContentType(req); + this.parseBody(contentType, ctx, rawBody); + } }