Skip to content

Commit

Permalink
fix: implement retries for Deno CLI download and additional logging (#…
Browse files Browse the repository at this point in the history
…100)

* fix: implement retries for Deno CLI download and additional logging
  • Loading branch information
jackiewmacharia authored Aug 18, 2022
1 parent 645812c commit 489cbd3
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 7 deletions.
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
16 changes: 11 additions & 5 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('Could not run downloaded Deno CLI', error)

throw error
}

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

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

return version[1]
} catch (error) {
this.logger.system('Error checking Deno binary version', error)
}
} catch {}
}

private async getCachedBinary() {
Expand All @@ -112,11 +113,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 +137,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

0 comments on commit 489cbd3

Please sign in to comment.