fix(fetcher): support Basic Auth

This commit is contained in:
WebStreamr 2025-07-20 18:44:37 +00:00
parent c62870a465
commit 674eb7ca6d
No known key found for this signature in database
2 changed files with 11 additions and 3 deletions

View file

@ -21,8 +21,8 @@ describe('fetch', () => {
const mockPool = mockAgent.get('https://some-url.test');
mockPool.intercept({ path: '/' }).reply(200, 'some text');
const responseText1 = await fetcher.text(ctx, new URL('https://some-url.test/'));
const responseText2 = await fetcher.text(ctx, new URL('https://some-url.test/'), { headers: { 'User-Agent': 'jest' } });
const responseText1 = await fetcher.text(ctx, new URL('https://user:pass@some-url.test/'));
const responseText2 = await fetcher.text(ctx, new URL('https://user:pass@some-url.test/'), { headers: { 'User-Agent': 'jest' } });
expect(responseText1).toBe('some text');
expect(responseText2).toStrictEqual(responseText1);
@ -30,6 +30,7 @@ describe('fetch', () => {
expect(mockAgent.getCallHistory()?.firstCall()?.headers).toMatchObject({
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language': 'en',
'Authorization': 'Basic dXNlcjpwYXNz',
'Forwarded': 'for=0.0.0.0',
'Priority': 'u=0',
'User-Agent': 'node',

View file

@ -96,6 +96,7 @@ export class Fetcher {
headers: {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language': 'en',
...(url.username && { Authorization: 'Basic ' + Buffer.from(`${url.username}:${url.password}`).toString('base64') }),
'Priority': 'u=0',
'User-Agent': this.hostUserAgentMap.get(url.host) ?? 'node',
...(cookieString && { Cookie: cookieString }),
@ -257,7 +258,13 @@ export class Fetcher {
let response;
try {
response = await fetch(url, { ...init, keepalive: true, signal: AbortSignal.timeout(init?.timeout ?? this.DEFAULT_TIMEOUT) });
const finalUrl = new URL(url.href);
finalUrl.username = '';
finalUrl.password = '';
const finalInit = { ...init, keepalive: true, signal: AbortSignal.timeout(init?.timeout ?? this.DEFAULT_TIMEOUT) };
response = await fetch(finalUrl, finalInit);
} catch (error) {
if (error instanceof DOMException && error.name === 'TimeoutError') {
await this.increaseTimeoutsCount(url);