From 56e1e4ae25d09eea1bb8dea54bd0f9cd428d2b6c 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 6576a46..51f0742 100644 --- a/src/index.ts +++ b/src/index.ts @@ -31,20 +31,18 @@ export function parse(string: string): Credentials | undefined { // 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), + ); } /** @@ -59,17 +57,6 @@ export function parse(string: string): Credentials | undefined { 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..e268573 --- /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 dGVzdDpwYXNzd29yZA=='; // "test:password" in base64 + parse(header); + }); + + bench('basic auth header with extra whitespace', () => { + const header = ' Basic dGVzdDpwYXNzd29yZA== '; // "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 38c850b1b898a285953dce1892449f5f2b806d95 Mon Sep 17 00:00:00 2001 From: Blake Embrey Date: Thu, 23 Apr 2026 16:09:44 -0700 Subject: [PATCH 2/2] Remove CredentialsImpl --- package.json | 1 + src/index.ts | 19 ++++--------------- 2 files changed, 5 insertions(+), 15 deletions(-) diff --git a/package.json b/package.json index dde152b..e18a931 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "dist/" ], "scripts": { + "bench": "vitest bench", "build": "ts-scripts build", "format": "ts-scripts format", "lint": "ts-scripts lint", diff --git a/src/index.ts b/src/index.ts index 51f0742..7132042 100644 --- a/src/index.ts +++ b/src/index.ts @@ -38,11 +38,10 @@ export function parse(string: string): Credentials | undefined { const colonIndex = userPass.indexOf(':'); if (colonIndex === -1) return undefined; - // return credentials object - return new CredentialsImpl( - userPass.slice(0, colonIndex), - userPass.slice(colonIndex + 1), - ); + return { + name: userPass.slice(0, colonIndex), + pass: userPass.slice(colonIndex + 1), + }; } /** @@ -65,13 +64,3 @@ const CREDENTIALS_REGEXP = function decodeBase64(str: string): string { return Buffer.from(str, 'base64').toString(); } - -class CredentialsImpl implements Credentials { - name: string; - pass: string; - - constructor(name: string, pass: string) { - this.name = name; - this.pass = pass; - } -}