From ec08f8f04e2286251b5f624cac438d9c162aa024 Mon Sep 17 00:00:00 2001 From: Timo Hocker <35867059+TimoHocker@users.noreply.github.com> Date: Wed, 26 Jan 2022 09:08:45 +0100 Subject: [PATCH] option to enable body parsing --- CHANGELOG.md | 4 ++++ README.md | 5 +++-- lib/AuthHandler.ts | 25 +++++++++++++++---------- package.json | 2 +- test/spec/AuthHandler.ts | 3 ++- 5 files changed, 25 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 53c6f2f..c385171 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## 3.1.0 + +- Option to enable body parsing + ## 3.0.0 - Allows Cookies Parameters to be set diff --git a/README.md b/README.md index e1ad6ac..77682fa 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # auth-server-helper -version: 3.0.x +version: 3.1.x customizable and simple authentication @@ -104,7 +104,8 @@ const handler = create_auth_handler( }, }, cookie: { name: 'auth_cookie' }, // if defined, access tokens will be stored in this cookie, - refresh_cookie: { name: 'refresh_cookie' } // if defined, refresh tokens will be stored in this cookie + refresh_cookie: { name: 'refresh_cookie' }, // if defined, refresh tokens will be stored in this cookie + parse_body: true // read the request body into a string (default false) } ); diff --git a/lib/AuthHandler.ts b/lib/AuthHandler.ts index 0950f29..a0968a8 100644 --- a/lib/AuthHandler.ts +++ b/lib/AuthHandler.ts @@ -225,14 +225,17 @@ interface CreateHandlerOptions { modules?: Record; cookie?: CookieSettings; refresh_cookie?: CookieSettings; + parse_body?: boolean; } +type ProcessRequestOptions = Omit + // eslint-disable-next-line max-lines-per-function function process_request ( request: AuthRequest, token: RegExpExecArray | null, default_handler: AuthRequestHandler, - options?: CreateHandlerOptions + options?: ProcessRequestOptions ): Promise | void { if (token === null) return default_handler (request); @@ -313,15 +316,17 @@ export default function create_auth_handler ( req: IncomingMessage, res: ServerResponse ): Promise => { - const body: string = await new Promise ((resolve) => { - let data = ''; - req.on ('data', (c) => { - data += c; - }); - req.on ('end', () => { - resolve (data); - }); - }); + const body: string = options?.parse_body + ? await new Promise ((resolve) => { + let data = ''; + req.on ('data', (c) => { + data += c; + }); + req.on ('end', () => { + resolve (data); + }); + }) + : ''; const request = new AuthRequest ( req, diff --git a/package.json b/package.json index 0a8fad4..040a754 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@sapphirecode/auth-server-helper", - "version": "3.0.0", + "version": "3.1.0", "main": "dist/index.js", "author": { "name": "Timo Hocker", diff --git a/test/spec/AuthHandler.ts b/test/spec/AuthHandler.ts index 0840c62..0330366 100644 --- a/test/spec/AuthHandler.ts +++ b/test/spec/AuthHandler.ts @@ -129,7 +129,8 @@ describe ('auth handler', () => { } else { request.deny (); } } - } + }, + parse_body: true }); server = http.createServer (async (