Skip to content

Commit 591873a

Browse files
committed
fix(electron-updater): Github Update Fails Due to Undefined
Close #1228
1 parent 0e9059c commit 591873a

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

packages/electron-builder-publisher/src/gitHubPublisher.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,11 @@ export class GitHubPublisher extends HttpPublisher {
187187
}
188188

189189
private githubRequest<T>(path: string, token: string | null, data: {[name: string]: any; } | null = null, method?: "GET" | "DELETE" | "PUT"): Promise<T> {
190+
// host can contains port, but node http doesn't support host as url does
191+
const baseUrl = parseUrl(`https://${this.info.host || "api.github.com"}`)
190192
return httpExecutor.request<T>(configureRequestOptions({
191-
host: this.info.host || "api.github.com",
193+
hostname: baseUrl.hostname,
194+
port: <any>baseUrl.port,
192195
path: (this.info.host != null && this.info.host !== "github.com") ? `/api/v3/${path}` : path,
193196
headers: {Accept: "application/vnd.github.v3+json"}
194197
}, token, method), this.context.cancellationToken, data)

packages/electron-updater/src/GitHubProvider.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,35 @@ import { validateUpdateInfo } from "./GenericProvider"
44
import * as path from "path"
55
import { HttpError, request } from "electron-builder-http"
66
import { CancellationToken } from "electron-builder-http/out/CancellationToken"
7+
import * as url from "url"
8+
import { RequestOptions } from "http"
79

810
export class GitHubProvider extends Provider<VersionInfo> {
11+
// so, we don't need to parse port (because node http doesn't support host as url does)
12+
private readonly baseUrl: RequestOptions
13+
914
constructor(private readonly options: GithubOptions) {
1015
super()
16+
17+
const baseUrl = url.parse(`${options.protocol || "https:"}//${options.host || "github.com"}`)
18+
this.baseUrl = {
19+
protocol: baseUrl.protocol,
20+
hostname: baseUrl.hostname,
21+
port: <any>baseUrl.port,
22+
}
1123
}
1224

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

1729
const cancellationToken = new CancellationToken()
18-
const host = this.options.host || "github.com"
19-
const protocol = `${this.options.protocol || "https"}:`
2030
try {
2131
// do not use API to avoid limit
22-
const releaseInfo = (await request<GithubReleaseInfo>({
23-
protocol: protocol,
24-
host: host,
32+
const releaseInfo = (await request<GithubReleaseInfo>(Object.assign({
2533
path: `${basePath}/latest`,
2634
headers: Object.assign({Accept: "application/json"}, this.requestHeaders)
27-
}, cancellationToken))
35+
}, this.baseUrl), cancellationToken))
2836
version = (releaseInfo.tag_name.startsWith("v")) ? releaseInfo.tag_name.substring(1) : releaseInfo.tag_name
2937
}
3038
catch (e) {
@@ -35,7 +43,7 @@ export class GitHubProvider extends Provider<VersionInfo> {
3543
const channelFile = getChannelFilename(getDefaultChannelName())
3644
const channelFileUrlPath = `${basePath}/download/v${version}/${channelFile}`
3745
try {
38-
result = await request<UpdateInfo>({protocol: protocol, host: host, path: channelFileUrlPath, headers: this.requestHeaders || undefined}, cancellationToken)
46+
result = await request<UpdateInfo>(Object.assign({path: channelFileUrlPath, headers: this.requestHeaders || undefined}, this.baseUrl), cancellationToken)
3947
}
4048
catch (e) {
4149
if (e instanceof HttpError && e.response.statusCode === 404) {
@@ -65,7 +73,7 @@ export class GitHubProvider extends Provider<VersionInfo> {
6573
const name = versionInfo.githubArtifactName || path.posix.basename(versionInfo.path).replace(/ /g, "-")
6674
return {
6775
name: name,
68-
url: `https://github.com${basePath}/download/v${versionInfo.version}/${name}`,
76+
url: url.format(Object.assign({pathname: `${basePath}/download/v${versionInfo.version}/${name}`}, this.baseUrl)),
6977
sha2: versionInfo.sha2,
7078
}
7179
}

0 commit comments

Comments
 (0)