Skip to content

Commit d6326df

Browse files
satrix321LekoArtswardpeet
authored
fix(gatsby-core-utils): Switch auth option from got to username/password (#32665)
Co-authored-by: LekoArts <lekoarts@gmail.com> Co-authored-by: Ward Peeters <ward@coding-tech.com>
1 parent cf9c066 commit d6326df

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

packages/gatsby-core-utils/src/fetch-remote-file.ts

+19-10
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,15 @@ const INCOMPLETE_RETRY_LIMIT = process.env.GATSBY_INCOMPLETE_RETRY_LIMIT
5252
* @param {String} url
5353
* @param {Headers} headers
5454
* @param {String} tmpFilename
55-
* @param {Object} httpOpts
55+
* @param {Object} httpOptions
5656
* @param {number} attempt
5757
* @return {Promise<Object>} Resolves with the [http Result Object]{@link https://nodejs.org/api/http.html#http_class_http_serverresponse}
5858
*/
5959
const requestRemoteNode = (
6060
url: got.GotUrl,
6161
headers: OutgoingHttpHeaders,
6262
tmpFilename: string,
63-
httpOpts: got.GotOptions<string | null> | undefined,
63+
httpOptions: got.GotOptions<string | null> | undefined,
6464
attempt: number = 1
6565
): Promise<IncomingMessage> =>
6666
new Promise((resolve, reject) => {
@@ -74,7 +74,7 @@ const requestRemoteNode = (
7474
if (attempt < STALL_RETRY_LIMIT) {
7575
// Retry by calling ourself recursively
7676
resolve(
77-
requestRemoteNode(url, headers, tmpFilename, httpOpts, attempt + 1)
77+
requestRemoteNode(url, headers, tmpFilename, httpOptions, attempt + 1)
7878
)
7979
} else {
8080
reject(`Failed to download ${url} after ${STALL_RETRY_LIMIT} attempts`)
@@ -92,7 +92,7 @@ const requestRemoteNode = (
9292
timeout: {
9393
send: CONNECTION_TIMEOUT, // https://github.com/sindresorhus/got#timeout
9494
},
95-
...httpOpts,
95+
...httpOptions,
9696
})
9797

9898
let haveAllBytesBeenWritten = false
@@ -140,7 +140,7 @@ const requestRemoteNode = (
140140
url,
141141
headers,
142142
tmpFilename,
143-
httpOpts,
143+
httpOptions,
144144
attempt + 1
145145
)
146146
)
@@ -177,11 +177,15 @@ export async function fetchRemoteFile({
177177
headers[`If-None-Match`] = cachedHeaders.etag
178178
}
179179

180-
// Add htaccess authentication if passed in. This isn't particularly
181-
// extensible. We should define a proper API that we validate.
182-
const httpOpts: got.GotOptions<string | null> = {}
180+
// Add Basic authentication if passed in:
181+
// https://github.com/sindresorhus/got/blob/main/documentation/2-options.md#username
182+
// The "auth" API isn't particularly extensible, we should define an API that we validate
183+
const httpOptions: got.GotOptions<string | null> = {}
183184
if (auth && (auth.htaccess_pass || auth.htaccess_user)) {
184-
httpOpts.auth = `${auth.htaccess_user}:${auth.htaccess_pass}`
185+
// @ts-ignore - We use outdated @types/got typings. Once we update got everywhere we can remove @types/got and have correct typings
186+
httpOptions.username = auth.htaccess_user
187+
// @ts-ignore - see above
188+
httpOptions.password = auth.htaccess_pass
185189
}
186190

187191
// Create the temp and permanent file names for the url.
@@ -196,7 +200,12 @@ export async function fetchRemoteFile({
196200
const tmpFilename = createFilePath(pluginCacheDir, `tmp-${digest}`, ext)
197201

198202
// Fetch the file.
199-
const response = await requestRemoteNode(url, headers, tmpFilename, httpOpts)
203+
const response = await requestRemoteNode(
204+
url,
205+
headers,
206+
tmpFilename,
207+
httpOptions
208+
)
200209

201210
if (response.statusCode === 200) {
202211
// Save the response headers for future requests.

packages/gatsby-source-filesystem/src/__tests__/create-remote-file-node.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,8 @@ describe(`create-remote-file-node`, () => {
174174
expect(got.stream).toHaveBeenCalledWith(
175175
expect.any(String),
176176
expect.objectContaining({
177-
auth: [auth.htaccess_user, auth.htaccess_pass].join(`:`),
177+
username: auth.htaccess_user,
178+
password: auth.htaccess_pass,
178179
})
179180
)
180181
})

0 commit comments

Comments
 (0)