From d6326df78873f4856f7296c4de360c9a7493cebb Mon Sep 17 00:00:00 2001 From: Krzysztof Szot Date: Fri, 27 Aug 2021 09:24:31 +0200 Subject: [PATCH] fix(gatsby-core-utils): Switch `auth` option from got to username/password (#32665) Co-authored-by: LekoArts Co-authored-by: Ward Peeters --- .../src/fetch-remote-file.ts | 29 ++++++++++++------- .../src/__tests__/create-remote-file-node.js | 3 +- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/packages/gatsby-core-utils/src/fetch-remote-file.ts b/packages/gatsby-core-utils/src/fetch-remote-file.ts index 0f627da72332d..aba43adde7c98 100644 --- a/packages/gatsby-core-utils/src/fetch-remote-file.ts +++ b/packages/gatsby-core-utils/src/fetch-remote-file.ts @@ -52,7 +52,7 @@ const INCOMPLETE_RETRY_LIMIT = process.env.GATSBY_INCOMPLETE_RETRY_LIMIT * @param {String} url * @param {Headers} headers * @param {String} tmpFilename - * @param {Object} httpOpts + * @param {Object} httpOptions * @param {number} attempt * @return {Promise} Resolves with the [http Result Object]{@link https://nodejs.org/api/http.html#http_class_http_serverresponse} */ @@ -60,7 +60,7 @@ const requestRemoteNode = ( url: got.GotUrl, headers: OutgoingHttpHeaders, tmpFilename: string, - httpOpts: got.GotOptions | undefined, + httpOptions: got.GotOptions | undefined, attempt: number = 1 ): Promise => new Promise((resolve, reject) => { @@ -74,7 +74,7 @@ const requestRemoteNode = ( if (attempt < STALL_RETRY_LIMIT) { // Retry by calling ourself recursively resolve( - requestRemoteNode(url, headers, tmpFilename, httpOpts, attempt + 1) + requestRemoteNode(url, headers, tmpFilename, httpOptions, attempt + 1) ) } else { reject(`Failed to download ${url} after ${STALL_RETRY_LIMIT} attempts`) @@ -92,7 +92,7 @@ const requestRemoteNode = ( timeout: { send: CONNECTION_TIMEOUT, // https://github.com/sindresorhus/got#timeout }, - ...httpOpts, + ...httpOptions, }) let haveAllBytesBeenWritten = false @@ -140,7 +140,7 @@ const requestRemoteNode = ( url, headers, tmpFilename, - httpOpts, + httpOptions, attempt + 1 ) ) @@ -177,11 +177,15 @@ export async function fetchRemoteFile({ headers[`If-None-Match`] = cachedHeaders.etag } - // Add htaccess authentication if passed in. This isn't particularly - // extensible. We should define a proper API that we validate. - const httpOpts: got.GotOptions = {} + // Add Basic authentication if passed in: + // https://github.com/sindresorhus/got/blob/main/documentation/2-options.md#username + // The "auth" API isn't particularly extensible, we should define an API that we validate + const httpOptions: got.GotOptions = {} if (auth && (auth.htaccess_pass || auth.htaccess_user)) { - httpOpts.auth = `${auth.htaccess_user}:${auth.htaccess_pass}` + // @ts-ignore - We use outdated @types/got typings. Once we update got everywhere we can remove @types/got and have correct typings + httpOptions.username = auth.htaccess_user + // @ts-ignore - see above + httpOptions.password = auth.htaccess_pass } // Create the temp and permanent file names for the url. @@ -196,7 +200,12 @@ export async function fetchRemoteFile({ const tmpFilename = createFilePath(pluginCacheDir, `tmp-${digest}`, ext) // Fetch the file. - const response = await requestRemoteNode(url, headers, tmpFilename, httpOpts) + const response = await requestRemoteNode( + url, + headers, + tmpFilename, + httpOptions + ) if (response.statusCode === 200) { // Save the response headers for future requests. diff --git a/packages/gatsby-source-filesystem/src/__tests__/create-remote-file-node.js b/packages/gatsby-source-filesystem/src/__tests__/create-remote-file-node.js index 3b79ee5a00881..0aa9ff3e85bf1 100644 --- a/packages/gatsby-source-filesystem/src/__tests__/create-remote-file-node.js +++ b/packages/gatsby-source-filesystem/src/__tests__/create-remote-file-node.js @@ -174,7 +174,8 @@ describe(`create-remote-file-node`, () => { expect(got.stream).toHaveBeenCalledWith( expect.any(String), expect.objectContaining({ - auth: [auth.htaccess_user, auth.htaccess_pass].join(`:`), + username: auth.htaccess_user, + password: auth.htaccess_pass, }) ) })