Skip to content

Commit 3741877

Browse files
committed
Benchmark parse function
1 parent ee70132 commit 3741877

File tree

2 files changed

+32
-21
lines changed

2 files changed

+32
-21
lines changed

src/index.ts

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -61,20 +61,18 @@ namespace auth {
6161

6262
// parse header
6363
const match = CREDENTIALS_REGEXP.exec(string);
64-
65-
if (!match) {
66-
return undefined;
67-
}
64+
if (!match) return undefined;
6865

6966
// decode user pass
70-
const userPass = USER_PASS_REGEXP.exec(decodeBase64(match[1]));
71-
72-
if (!userPass) {
73-
return undefined;
74-
}
67+
const userPass = decodeBase64(match[1]);
68+
const colonIndex = userPass.indexOf(':');
69+
if (colonIndex === -1) return undefined;
7570

7671
// return credentials object
77-
return new CredentialsImpl(userPass[1], userPass[2]);
72+
return new CredentialsImpl(
73+
userPass.slice(0, colonIndex),
74+
userPass.slice(colonIndex + 1),
75+
);
7876
}
7977
}
8078

@@ -90,17 +88,6 @@ namespace auth {
9088
const CREDENTIALS_REGEXP =
9189
/^ *(?:[Bb][Aa][Ss][Ii][Cc]) +([A-Za-z0-9._~+/-]+=*) *$/;
9290

93-
/**
94-
* RegExp for basic auth user/pass
95-
*
96-
* user-pass = userid ":" password
97-
* userid = *<TEXT excluding ":">
98-
* password = *TEXT
99-
* @private
100-
*/
101-
102-
const USER_PASS_REGEXP = /^([^:]*):(.*)$/;
103-
10491
/**
10592
* Decode base64 string.
10693
* @private

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 dGVzdOnBhc3N3b3Jk'; // "test:password" in base64
7+
parse(header);
8+
});
9+
10+
bench('basic auth header with extra whitespace', () => {
11+
const header = ' Basic dGVzdOnBhc3N3b3Jk '; // "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)