Skip to content

Commit

Permalink
fix: add error state to onAfterDownload (#42)
Browse files Browse the repository at this point in the history
* fix: add error state to after download

Co-authored-by: Eduardo Bouças <mail@eduardoboucas.com>
  • Loading branch information
jackiewmacharia and eduardoboucas committed Jun 22, 2022
1 parent ef5a8e3 commit 2cb24ac
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 19 deletions.
29 changes: 16 additions & 13 deletions src/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ import { getBinaryExtension } from './platform.js'
const DENO_VERSION_FILE = 'version.txt'
const DENO_VERSION_RANGE = '^1.20.3'

type LifecycleHook = () => void | Promise<void>
type OnBeforeDownloadHook = () => void | Promise<void>
type OnAfterDownloadHook = (error?: Error) => void | Promise<void>

interface DenoOptions {
cacheDirectory?: string
debug?: boolean
onAfterDownload?: LifecycleHook
onBeforeDownload?: LifecycleHook
onAfterDownload?: OnAfterDownloadHook
onBeforeDownload?: OnBeforeDownloadHook
useGlobal?: boolean
versionRange?: string
}
Expand All @@ -35,8 +36,8 @@ class DenoBridge {
cacheDirectory: string
currentDownload?: ReturnType<DenoBridge['downloadBinary']>
debug: boolean
onAfterDownload?: LifecycleHook
onBeforeDownload?: LifecycleHook
onAfterDownload?: OnAfterDownloadHook
onBeforeDownload?: OnBeforeDownloadHook
useGlobal: boolean
versionRange: string

Expand All @@ -50,9 +51,7 @@ class DenoBridge {
}

private async downloadBinary() {
if (this.onBeforeDownload) {
this.onBeforeDownload()
}
this.onBeforeDownload?.()

await fs.mkdir(this.cacheDirectory, { recursive: true })

Expand All @@ -65,14 +64,18 @@ class DenoBridge {
// a malformed semver range. If this does happen, let's throw an error so
// that the tests catch it.
if (downloadedVersion === undefined) {
throw new Error('Could not read downloaded binary')
const error = new Error(
'There was a problem setting up the Edge Functions environment. To try a manual installation, visit https://ntl.fyi/install-deno.',
)

this.onAfterDownload?.(error)

throw error
}

await this.writeVersionFile(downloadedVersion)

if (this.onAfterDownload) {
this.onAfterDownload()
}
this.onAfterDownload?.()

return binaryPath
}
Expand Down Expand Up @@ -204,4 +207,4 @@ class DenoBridge {
}

export { DenoBridge }
export type { LifecycleHook, ProcessRef }
export type { OnAfterDownloadHook, OnBeforeDownloadHook, ProcessRef }
6 changes: 3 additions & 3 deletions src/bundler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { join } from 'path'
import commonPathPrefix from 'common-path-prefix'
import { v4 as uuidv4 } from 'uuid'

import { DenoBridge, LifecycleHook } from './bridge.js'
import { DenoBridge, OnAfterDownloadHook, OnBeforeDownloadHook } from './bridge.js'
import type { Bundle } from './bundle.js'
import type { Declaration } from './declaration.js'
import { EdgeFunction } from './edge_function.js'
Expand All @@ -21,8 +21,8 @@ interface BundleOptions {
distImportMapPath?: string
featureFlags?: FeatureFlags
importMaps?: ImportMapFile[]
onAfterDownload?: LifecycleHook
onBeforeDownload?: LifecycleHook
onAfterDownload?: OnAfterDownloadHook
onBeforeDownload?: OnBeforeDownloadHook
}

interface BundleFormatOptions {
Expand Down
6 changes: 3 additions & 3 deletions src/server/server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { tmpName } from 'tmp-promise'

import { DenoBridge, LifecycleHook, ProcessRef } from '../bridge.js'
import { DenoBridge, OnAfterDownloadHook, OnBeforeDownloadHook, ProcessRef } from '../bridge.js'
import type { EdgeFunction } from '../edge_function.js'
import { generateStage2 } from '../formats/javascript.js'
import { ImportMap, ImportMapFile } from '../import_map.js'
Expand Down Expand Up @@ -87,8 +87,8 @@ interface ServeOptions {
distImportMapPath?: string
inspectSettings?: InspectSettings
importMaps?: ImportMapFile[]
onAfterDownload?: LifecycleHook
onBeforeDownload?: LifecycleHook
onAfterDownload?: OnAfterDownloadHook
onBeforeDownload?: OnBeforeDownloadHook
formatExportTypeError?: FormatFunction
formatImportError?: FormatFunction
port: number
Expand Down

0 comments on commit 2cb24ac

Please sign in to comment.