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: add error state to onAfterDownload #42

Merged
merged 13 commits into from
Jun 22, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 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 | null) => void | Promise<void>
danez marked this conversation as resolved.
Show resolved Hide resolved

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 Down Expand Up @@ -65,13 +66,21 @@ 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 and it's unfortunately not possible to run Edge Functions from this platform. More on supported platforms here: https://deno.land/manual/getting_started/installation.",
jackiewmacharia marked this conversation as resolved.
Show resolved Hide resolved
)

if (this.onAfterDownload) {
this.onAfterDownload(error)
}
danez marked this conversation as resolved.
Show resolved Hide resolved

throw error
}

await this.writeVersionFile(downloadedVersion)

if (this.onAfterDownload) {
this.onAfterDownload()
this.onAfterDownload(null)
danez marked this conversation as resolved.
Show resolved Hide resolved
}

return binaryPath
Expand Down Expand Up @@ -201,7 +210,8 @@ class DenoBridge {
ref.ps = ps
}
}
// eslint-disable-next-line max-lines
}

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 { FeatureFlags, getFlags } from './feature_flags.js'
Expand All @@ -20,8 +20,8 @@ interface BundleOptions {
distImportMapPath?: string
featureFlags?: FeatureFlags
importMaps?: ImportMapFile[]
onAfterDownload?: LifecycleHook
onBeforeDownload?: LifecycleHook
onAfterDownload?: OnAfterDownloadHook
onBeforeDownload?: OnBeforeDownloadHook
}

const bundle = async (
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