Skip to content

Commit

Permalink
fix(electron-updater): Github Update Fails Due to Undefined
Browse files Browse the repository at this point in the history
Close #1228
  • Loading branch information
develar committed Feb 10, 2017
1 parent 0e9059c commit 591873a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
5 changes: 4 additions & 1 deletion packages/electron-builder-publisher/src/gitHubPublisher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,11 @@ export class GitHubPublisher extends HttpPublisher {
}

private githubRequest<T>(path: string, token: string | null, data: {[name: string]: any; } | null = null, method?: "GET" | "DELETE" | "PUT"): Promise<T> {
// host can contains port, but node http doesn't support host as url does
const baseUrl = parseUrl(`https://${this.info.host || "api.github.com"}`)
return httpExecutor.request<T>(configureRequestOptions({
host: this.info.host || "api.github.com",
hostname: baseUrl.hostname,
port: <any>baseUrl.port,
path: (this.info.host != null && this.info.host !== "github.com") ? `/api/v3/${path}` : path,
headers: {Accept: "application/vnd.github.v3+json"}
}, token, method), this.context.cancellationToken, data)
Expand Down
24 changes: 16 additions & 8 deletions packages/electron-updater/src/GitHubProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,35 @@ import { validateUpdateInfo } from "./GenericProvider"
import * as path from "path"
import { HttpError, request } from "electron-builder-http"
import { CancellationToken } from "electron-builder-http/out/CancellationToken"
import * as url from "url"
import { RequestOptions } from "http"

export class GitHubProvider extends Provider<VersionInfo> {
// so, we don't need to parse port (because node http doesn't support host as url does)
private readonly baseUrl: RequestOptions

constructor(private readonly options: GithubOptions) {
super()

const baseUrl = url.parse(`${options.protocol || "https:"}//${options.host || "github.com"}`)
this.baseUrl = {
protocol: baseUrl.protocol,
hostname: baseUrl.hostname,
port: <any>baseUrl.port,
}
}

async getLatestVersion(): Promise<UpdateInfo> {
const basePath = this.getBasePath()
let version

const cancellationToken = new CancellationToken()
const host = this.options.host || "github.com"
const protocol = `${this.options.protocol || "https"}:`
try {
// do not use API to avoid limit
const releaseInfo = (await request<GithubReleaseInfo>({
protocol: protocol,
host: host,
const releaseInfo = (await request<GithubReleaseInfo>(Object.assign({
path: `${basePath}/latest`,
headers: Object.assign({Accept: "application/json"}, this.requestHeaders)
}, cancellationToken))
}, this.baseUrl), cancellationToken))
version = (releaseInfo.tag_name.startsWith("v")) ? releaseInfo.tag_name.substring(1) : releaseInfo.tag_name
}
catch (e) {
Expand All @@ -35,7 +43,7 @@ export class GitHubProvider extends Provider<VersionInfo> {
const channelFile = getChannelFilename(getDefaultChannelName())
const channelFileUrlPath = `${basePath}/download/v${version}/${channelFile}`
try {
result = await request<UpdateInfo>({protocol: protocol, host: host, path: channelFileUrlPath, headers: this.requestHeaders || undefined}, cancellationToken)
result = await request<UpdateInfo>(Object.assign({path: channelFileUrlPath, headers: this.requestHeaders || undefined}, this.baseUrl), cancellationToken)
}
catch (e) {
if (e instanceof HttpError && e.response.statusCode === 404) {
Expand Down Expand Up @@ -65,7 +73,7 @@ export class GitHubProvider extends Provider<VersionInfo> {
const name = versionInfo.githubArtifactName || path.posix.basename(versionInfo.path).replace(/ /g, "-")
return {
name: name,
url: `https://github.com${basePath}/download/v${versionInfo.version}/${name}`,
url: url.format(Object.assign({pathname: `${basePath}/download/v${versionInfo.version}/${name}`}, this.baseUrl)),
sha2: versionInfo.sha2,
}
}
Expand Down

0 comments on commit 591873a

Please sign in to comment.