Skip to content

Commit

Permalink
Use the HEAD method when fetching as it is more efficient and prevent…
Browse files Browse the repository at this point in the history
…s 30-second delay when node exits. (#219)

* Use the HEAD method when fetching as it is more efficient and prevents 30-second delay when node exits.

* resolve conflict

* make fetch method flexible

* adapt tests

---------

Co-authored-by: jliempt <>
Co-authored-by: Jordi van Liempt <[email protected]>
  • Loading branch information
ajrussellsap and jliempt authored Oct 30, 2024
1 parent a21e55f commit f8d534f
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 10 deletions.
7 changes: 4 additions & 3 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ export async function wait (delay: number): Promise<string> {
return await new Promise((resolve) => setTimeout(resolve, delay))
}

export async function fetchRetry (url: string, tries = 5, baseDelayMS = 1000): Promise<Response> {
export async function fetchRetry (url: string, method = 'GET', tries = 5, baseDelayMS = 1000): Promise<Response> {
let attempt = 0

while (tries > attempt) {
const response = await fetch(url)
const response = await fetch(url, { method })
if (response.status === 200) {
return response
}
Expand Down
3 changes: 2 additions & 1 deletion src/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ export async function buildPiperFromSource (version: string): Promise<string> {
}

async function getPiperDownloadURL (piper: string, version?: string): Promise<string> {
const response = await fetchRetry(`${GITHUB_COM_SERVER_URL}/SAP/jenkins-library/releases/${getTag(false, version)}`).catch(async (err) => {
const tagURL = `${GITHUB_COM_SERVER_URL}/SAP/jenkins-library/releases/${getTag(false, version)}`
const response = await fetchRetry(tagURL, 'HEAD').catch(async (err) => {
return await Promise.reject(new Error(`Can't get the tag: ${err}`))
})
return await Promise.resolve(response.url.replace(/tag/, 'download') + `/${piper}`)
Expand Down
6 changes: 3 additions & 3 deletions test/fetch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe('Fetch package tests', () => {
return mockResponse200
})

await fetchRetry(testURL, tries, delay)
await fetchRetry(testURL, undefined, tries, delay)
expect(core.info).toHaveBeenCalledTimes(0)
})

Expand All @@ -39,7 +39,7 @@ describe('Fetch package tests', () => {
return mockResponse200
})

await fetchRetry(testURL, tries, delay)
await fetchRetry(testURL, undefined, tries, delay)

expect(info).toHaveBeenCalledWith(`Error while fetching ${testURL}: Internal Server Error`)
expect(info).toHaveBeenCalledWith('Retrying 2 more time(s)...')
Expand All @@ -51,7 +51,7 @@ describe('Fetch package tests', () => {
return mockResponse500
})

await expect(fetchRetry(testURL, tries, delay)).rejects.toThrow(`Error fetching ${testURL}`)
await expect(fetchRetry(testURL, undefined, tries, delay)).rejects.toThrow(`Error fetching ${testURL}`)

expect(info).toHaveBeenCalledWith(`Error while fetching ${testURL}: Internal Server Error`)
expect(info).toHaveBeenCalledWith('Retrying 2 more time(s)...')
Expand Down

0 comments on commit f8d534f

Please sign in to comment.