Skip to content

Commit

Permalink
tests: retry download file from proxy server (#1218)
Browse files Browse the repository at this point in the history
* tests: retry download file from proxy server

* add changeset
  • Loading branch information
Schniz authored Aug 11, 2024
1 parent 09b7eb8 commit 345adaf
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changeset/strange-flies-drive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"fnm": patch
---

internal: retry download in case of error in test proxy
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
}
},
"devDependencies": {
"@changesets/cli": "2.27.7",
"@changesets/changelog-github": "0.5.0",
"@changesets/cli": "2.27.7",
"@types/jest": "^29.2.3",
"@types/node": "^18.11.9",
"@types/shell-escape": "^0.2.1",
Expand All @@ -34,6 +34,7 @@
"jest": "^29.3.1",
"lerna-changelog": "2.2.0",
"node-fetch": "^3.3.0",
"p-retry": "^6.2.0",
"prettier": "3.3.3",
"pv": "1.0.1",
"shell-escape": "^0.2.0",
Expand Down
30 changes: 30 additions & 0 deletions pnpm-lock.yaml

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

47 changes: 35 additions & 12 deletions tests/proxy-server/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import fs from "node:fs"
import crypto from "node:crypto"
import fetch from "node-fetch"
import chalk from "chalk"
import pRetry from "p-retry"

const baseDir = path.join(process.cwd(), ".proxy")
try {
Expand All @@ -15,6 +16,26 @@ try {
/** @type {Map<string, Promise<{ headers: Record<string, string>, body: ArrayBuffer }>>} */
const cache = new Map()

/**
* @param {object} opts
* @param {string} opts.pathname
* @param {string} opts.headersFilename
* @param {string} opts.filename
*/
const download = async ({ pathname, filename, headersFilename }) => {
const response = await fetch(
"https://nodejs.org/dist/" + pathname.replace(/^\/+/, ""),
{
compress: false,
},
)
const headers = Object.fromEntries(response.headers.entries())
const body = await response.arrayBuffer()
fs.writeFileSync(headersFilename, JSON.stringify(headers))
fs.writeFileSync(filename, Buffer.from(body))
return { headers, body }
}

export const server = createServer((req, res) => {
const pathname = req.url ?? "/"
const hash = crypto
Expand All @@ -34,18 +55,20 @@ export const server = createServer((req, res) => {
let promise = cache.get(filename)
if (!promise) {
console.log(chalk.red.dim(`[proxy] miss: ${pathname} -> ${filename}`))
promise = fetch(
"https://nodejs.org/dist/" + pathname.replace(/^\/+/, ""),
promise = pRetry(
() => download({ pathname, headersFilename, filename }),
{
compress: false,
}
).then(async (response) => {
const headers = Object.fromEntries(response.headers.entries())
const body = await response.arrayBuffer()
fs.writeFileSync(headersFilename, JSON.stringify(headers))
fs.writeFileSync(filename, Buffer.from(body))
return { headers, body }
})
retries: 5,
maxTimeout: 5000,
onFailedAttempt: (error) => {
console.error(
chalk.red(
`[proxy] ${chalk.bold("error")}: ${error.message}, retries left: ${error.retriesLeft}`,
),
)
},
},
)
cache.set(filename, promise)
promise.finally(() => cache.delete(filename))
}
Expand All @@ -59,7 +82,7 @@ export const server = createServer((req, res) => {
console.error(err)
res.writeHead(500)
res.end()
}
},
)
}
})

0 comments on commit 345adaf

Please sign in to comment.