From 3741877ce712ed8d47f161b34a56d7bf858e605e Mon Sep 17 00:00:00 2001 From: Blake Embrey Date: Tue, 7 Apr 2026 14:57:48 -0700 Subject: [PATCH 1/2] Benchmark parse function --- src/index.ts | 29 ++++++++--------------------- src/parse.bench.ts | 24 ++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 21 deletions(-) create mode 100644 src/parse.bench.ts diff --git a/src/index.ts b/src/index.ts index 98bc6c4..7617439 100644 --- a/src/index.ts +++ b/src/index.ts @@ -61,20 +61,18 @@ namespace auth { // parse header const match = CREDENTIALS_REGEXP.exec(string); - - if (!match) { - return undefined; - } + if (!match) return undefined; // decode user pass - const userPass = USER_PASS_REGEXP.exec(decodeBase64(match[1])); - - if (!userPass) { - return undefined; - } + const userPass = decodeBase64(match[1]); + const colonIndex = userPass.indexOf(':'); + if (colonIndex === -1) return undefined; // return credentials object - return new CredentialsImpl(userPass[1], userPass[2]); + return new CredentialsImpl( + userPass.slice(0, colonIndex), + userPass.slice(colonIndex + 1), + ); } } @@ -90,17 +88,6 @@ namespace auth { const CREDENTIALS_REGEXP = /^ *(?:[Bb][Aa][Ss][Ii][Cc]) +([A-Za-z0-9._~+/-]+=*) *$/; -/** - * RegExp for basic auth user/pass - * - * user-pass = userid ":" password - * userid = * - * password = *TEXT - * @private - */ - -const USER_PASS_REGEXP = /^([^:]*):(.*)$/; - /** * Decode base64 string. * @private diff --git a/src/parse.bench.ts b/src/parse.bench.ts new file mode 100644 index 0000000..eb9443a --- /dev/null +++ b/src/parse.bench.ts @@ -0,0 +1,24 @@ +import { describe, bench } from 'vitest'; +import { parse } from './index'; + +describe('parse', () => { + bench('basic auth header', () => { + const header = 'Basic dGVzdOnBhc3N3b3Jk'; // "test:password" in base64 + parse(header); + }); + + bench('basic auth header with extra whitespace', () => { + const header = ' Basic dGVzdOnBhc3N3b3Jk '; // "test:password" in base64 with extra whitespace + parse(header); + }); + + bench('invalid basic auth header', () => { + const header = 'Basic invalidbase64'; // Invalid base64 string + parse(header); + }); + + bench('non-basic auth header', () => { + const header = 'Bearer sometoken'; // Not a basic auth header + parse(header); + }); +}); From a6089af6a9eb44c5ef59cec986b8372813648783 Mon Sep 17 00:00:00 2001 From: Blake Embrey Date: Tue, 7 Apr 2026 15:03:33 -0700 Subject: [PATCH 2/2] Update base64 --- src/parse.bench.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/parse.bench.ts b/src/parse.bench.ts index eb9443a..e268573 100644 --- a/src/parse.bench.ts +++ b/src/parse.bench.ts @@ -3,12 +3,12 @@ import { parse } from './index'; describe('parse', () => { bench('basic auth header', () => { - const header = 'Basic dGVzdOnBhc3N3b3Jk'; // "test:password" in base64 + const header = 'Basic dGVzdDpwYXNzd29yZA=='; // "test:password" in base64 parse(header); }); bench('basic auth header with extra whitespace', () => { - const header = ' Basic dGVzdOnBhc3N3b3Jk '; // "test:password" in base64 with extra whitespace + const header = ' Basic dGVzdDpwYXNzd29yZA== '; // "test:password" in base64 with extra whitespace parse(header); });