Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: implement retries for Deno CLI download and additional logging #100

Merged
merged 11 commits into from
Aug 18, 2022
48 changes: 48 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
"glob-to-regexp": "^0.4.1",
"node-fetch": "^3.1.1",
"node-stream-zip": "^1.15.0",
"p-retry": "^5.1.1",
"p-wait-for": "^4.1.0",
"path-key": "^4.0.0",
"semver": "^7.3.5",
Expand Down
12 changes: 10 additions & 2 deletions src/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class DenoBridge {

this.logger.system(`Downloading Deno CLI to ${this.cacheDirectory}`)

const binaryPath = await download(this.cacheDirectory, this.versionRange)
const binaryPath = await download(this.cacheDirectory, this.versionRange, this.logger)
const downloadedVersion = await this.getBinaryVersion(binaryPath)

// We should never get here, because it means that `DENO_VERSION_RANGE` is
Expand All @@ -80,6 +80,8 @@ class DenoBridge {

await this.onAfterDownload?.(error)

this.logger.system('Undefined downloadedVersion error', error)

throw error
}

Expand All @@ -96,6 +98,7 @@ class DenoBridge {
const version = stdout.match(/^deno ([\d.]+)/)

if (!version) {
this.logger.system(`getBinaryVersion no version found. binaryPath ${binaryPath}`)
return
}

Expand All @@ -112,11 +115,13 @@ class DenoBridge {

try {
cachedVersion = await fs.readFile(versionFilePath, 'utf8')
} catch {
} catch (error) {
this.logger.system('Error getting cached binary', error)
return
}

if (!semver.satisfies(cachedVersion, this.versionRange)) {
this.logger.system(`semver not satisfied. cachedVersion: ${cachedVersion}, versionRange: ${this.versionRange}`)
return
}

Expand All @@ -134,6 +139,9 @@ class DenoBridge {
const globalVersion = await this.getBinaryVersion(globalBinaryName)

if (globalVersion === undefined || !semver.satisfies(globalVersion, this.versionRange)) {
this.logger.system(
`No globalVersion or semver not satisfied. globalVersion: ${globalVersion}, versionRange: ${this.versionRange}`,
)
return
}

Expand Down
14 changes: 12 additions & 2 deletions src/downloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ import path from 'path'

import fetch from 'node-fetch'
import StreamZip from 'node-stream-zip'
import pRetry from 'p-retry'
import semver from 'semver'

import { Logger } from './logger.js'
import { getBinaryExtension, getPlatformTarget } from './platform.js'

const download = async (targetDirectory: string, versionRange: string) => {
const download = async (targetDirectory: string, versionRange: string, logger: Logger) => {
const zipPath = path.join(targetDirectory, 'deno-cli-latest.zip')
const data = await downloadVersion(versionRange)
const data = await downloadVersionWithRetry(versionRange, logger)
const binaryName = `deno${getBinaryExtension()}`
const binaryPath = path.join(targetDirectory, binaryName)
const file = fs.createWriteStream(zipPath)
Expand Down Expand Up @@ -43,6 +45,14 @@ const downloadVersion = async (versionRange: string) => {
return res.body
}

const downloadVersionWithRetry = async (versionRange: string, logger: Logger) =>
await pRetry(async () => await downloadVersion(versionRange), {
retries: 3,
onFailedAttempt: (error) => {
logger.system('Deno CLI download retry attempt error', error)
},
})

const extractBinaryFromZip = async (zipPath: string, binaryPath: string, binaryName: string) => {
const { async: StreamZipAsync } = StreamZip
const zip = new StreamZipAsync({ file: zipPath })
Expand Down