Skip to content

Commit 08b56ca

Browse files
authored
Improve parse perf with benchmarks (#96)
1 parent b7be5af commit 08b56ca

3 files changed

Lines changed: 34 additions & 33 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"dist/"
1919
],
2020
"scripts": {
21+
"bench": "vitest bench",
2122
"build": "ts-scripts build",
2223
"format": "ts-scripts format",
2324
"lint": "ts-scripts lint",

src/index.ts

Lines changed: 9 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,17 @@ export function parse(string: string): Credentials | undefined {
3131

3232
// parse header
3333
const match = CREDENTIALS_REGEXP.exec(string);
34-
35-
if (!match) {
36-
return undefined;
37-
}
34+
if (!match) return undefined;
3835

3936
// decode user pass
40-
const userPass = USER_PASS_REGEXP.exec(decodeBase64(match[1]));
41-
42-
if (!userPass) {
43-
return undefined;
44-
}
45-
46-
// return credentials object
47-
return new CredentialsImpl(userPass[1], userPass[2]);
37+
const userPass = decodeBase64(match[1]);
38+
const colonIndex = userPass.indexOf(':');
39+
if (colonIndex === -1) return undefined;
40+
41+
return {
42+
name: userPass.slice(0, colonIndex),
43+
pass: userPass.slice(colonIndex + 1),
44+
};
4845
}
4946

5047
/**
@@ -59,17 +56,6 @@ export function parse(string: string): Credentials | undefined {
5956
const CREDENTIALS_REGEXP =
6057
/^ *(?:[Bb][Aa][Ss][Ii][Cc]) +([A-Za-z0-9._~+/-]+=*) *$/;
6158

62-
/**
63-
* RegExp for basic auth user/pass
64-
*
65-
* user-pass = userid ":" password
66-
* userid = *<TEXT excluding ":">
67-
* password = *TEXT
68-
* @private
69-
*/
70-
71-
const USER_PASS_REGEXP = /^([^:]*):(.*)$/;
72-
7359
/**
7460
* Decode base64 string.
7561
* @private
@@ -78,13 +64,3 @@ const USER_PASS_REGEXP = /^([^:]*):(.*)$/;
7864
function decodeBase64(str: string): string {
7965
return Buffer.from(str, 'base64').toString();
8066
}
81-
82-
class CredentialsImpl implements Credentials {
83-
name: string;
84-
pass: string;
85-
86-
constructor(name: string, pass: string) {
87-
this.name = name;
88-
this.pass = pass;
89-
}
90-
}

src/parse.bench.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { describe, bench } from 'vitest';
2+
import { parse } from './index';
3+
4+
describe('parse', () => {
5+
bench('basic auth header', () => {
6+
const header = 'Basic dGVzdDpwYXNzd29yZA=='; // "test:password" in base64
7+
parse(header);
8+
});
9+
10+
bench('basic auth header with extra whitespace', () => {
11+
const header = ' Basic dGVzdDpwYXNzd29yZA== '; // "test:password" in base64 with extra whitespace
12+
parse(header);
13+
});
14+
15+
bench('invalid basic auth header', () => {
16+
const header = 'Basic invalidbase64'; // Invalid base64 string
17+
parse(header);
18+
});
19+
20+
bench('non-basic auth header', () => {
21+
const header = 'Bearer sometoken'; // Not a basic auth header
22+
parse(header);
23+
});
24+
});

0 commit comments

Comments
 (0)