-
-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(config): add config.auth param to provide basic auth credentials (…
…#55)
- Loading branch information
Showing
8 changed files
with
45 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,9 +5,9 @@ import { ensureDirSync } from 'fs-extra' | |
import sanitize from 'sanitize-filename' | ||
import slugify from 'slugify' | ||
import { hasProtocol, joinURL, withLeadingSlash, withTrailingSlash, withoutLeadingSlash, withoutTrailingSlash } from 'ufo' | ||
import type { AxiosResponse } from 'axios' | ||
import type { AxiosRequestConfig, AxiosResponse } from 'axios' | ||
import axios from 'axios' | ||
import type { NormalisedRoute, UnlighthouseRouteReport } from './types' | ||
import type { NormalisedRoute, ResolvedUserConfig, UnlighthouseRouteReport } from './types' | ||
import { useUnlighthouse } from './unlighthouse' | ||
|
||
export const ReportArtifacts = { | ||
|
@@ -118,16 +118,27 @@ export const formatBytes = (bytes: number, decimals = 2) => { | |
return `${parseFloat((bytes / k ** i).toFixed(dm))} ${sizes[i]}` | ||
} | ||
|
||
export async function fetchUrlRaw(url: string): Promise<{ error?: any; redirected?: boolean; redirectUrl?: string; valid: boolean; response?: AxiosResponse }> { | ||
export async function fetchUrlRaw(url: string, resolvedConfig: ResolvedUserConfig): Promise<{ error?: any; redirected?: boolean; redirectUrl?: string; valid: boolean; response?: AxiosResponse }> { | ||
const axiosOptions: AxiosRequestConfig = {} | ||
if (resolvedConfig.auth) { | ||
axiosOptions.auth = resolvedConfig.auth | ||
} | ||
|
||
try { | ||
const response = await axios.get(url, { | ||
// allow all SSL's | ||
httpsAgent: new https.Agent({ | ||
rejectUnauthorized: false, | ||
}), | ||
...axiosOptions, | ||
}) | ||
const redirected = response.request.res.responseUrl && response.request.res.responseUrl !== url | ||
const redirectUrl = response.request.res.responseUrl | ||
let responseUrl = response.request.res.responseUrl | ||
if (responseUrl && axiosOptions.auth) { | ||
// remove auth credentials from url (e.g. https://user:[email protected]) | ||
responseUrl = responseUrl.replace(/(?<=https?:\/\/)(.+@)/g, '') | ||
} | ||
const redirected = responseUrl && responseUrl !== url; | ||
const redirectUrl = responseUrl; | ||
if (response.status < 200 || (response.status >= 300 && !redirected)) { | ||
return { | ||
valid: false, | ||
|