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(electron-auto-updater) Checking for updates from github was failing #1068

Closed
Closed
Show file tree
Hide file tree
Changes from all 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
23 changes: 16 additions & 7 deletions nsis-auto-updater/src/GitHubProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,27 @@ export class GitHubProvider implements Provider<VersionInfo> {
async getLatestVersion(): Promise<UpdateInfo> {
// do not use API to avoid limit
const basePath = this.getBasePath()
let version = (await request<Redirect>({hostname: "github.com", path: `${basePath}/latest`})).location
const versionPosition = version.lastIndexOf("/") + 1
let version = (await request<ReleaseInfo>(
{hostname: "github.com", path: `${basePath}/latest`},
null,
null,
"GET",
{"Accept": "application/json"}
)).tag_name

try {
version = version.substring(version[versionPosition] === "v" ? versionPosition + 1 : versionPosition)
version = (version.startsWith("v")) ? version.substring(1) : version
}
catch (e) {
throw new Error(`Cannot parse extract version from location "${version}": ${e.stack || e.message}`)
throw new Error(`Cannot parse determine latest version from github "${version}": ${e.stack || e.message}`)
}

let result: UpdateInfo | null = null
try {
result = await request<UpdateInfo>({hostname: "github.com", path: `https://github.com${basePath}/download/v${version}/latest.yml`})
result = await request<UpdateInfo>({
hostname: "github.com",
path: `https://github.com${basePath}/download/v${version}/latest.yml`
})
}
catch (e) {
if (e instanceof HttpError && e.response.statusCode === 404) {
Expand Down Expand Up @@ -51,6 +60,6 @@ export class GitHubProvider implements Provider<VersionInfo> {
}
}

interface Redirect {
readonly location: string
interface ReleaseInfo {
readonly tag_name: string
}
30 changes: 14 additions & 16 deletions nsis-auto-updater/src/electronHttpExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,17 @@ export class ElectronHttpExecutor implements HttpExecutor {

private readonly maxRedirects = 10

request<T>(url: Url, token: string | null = null, data: {[name: string]: any; } | null = null, method: string = "GET"): Promise<T> {
request<T>(url: Url, token: string | null = null, data: {[name: string]: any; } | null = null, method: string = "GET", headers: any = {}): Promise<T> {

headers = Object.assign({"User-Agent": "electron-builder"}, headers)

const options: any = Object.assign({
method: method,
headers: {
"User-Agent": "electron-builder"
}
headers: headers
}, url)

if (url.hostname!!.includes("github") && !url.path!.endsWith(".yml")) {
options.headers.Accept = "application/vnd.github.v3+json"
if (url.hostname!!.includes("github") && !url.path!.endsWith(".yml") && !headers.Accept) {
options.headers["Accept"] = "application/vnd.github.v3+json"
}

const encodedData = data == null ? undefined : new Buffer(JSON.stringify(data))
Expand Down Expand Up @@ -162,14 +163,10 @@ Please double check that your authentication token is correct. Due to security r
return
}

if (options.path!.endsWith("/latest")) {
resolve(<any>{location: redirectUrl})
}
else {
this.doApiRequest(Object.assign({}, options, parseUrl(redirectUrl)), token, requestProcessor)
.then(<any>resolve)
.catch(reject)
}
this.doApiRequest(Object.assign({}, options, parseUrl(redirectUrl)), token, requestProcessor)
.then(<any>resolve)
.catch(reject)

return
}

Expand All @@ -182,7 +179,8 @@ Please double check that your authentication token is correct. Due to security r
response.on("end", () => {
try {
const contentType = response.headers["content-type"]
const isJson = contentType != null && contentType.includes("json")
const isJson = contentType != null && contentType.find((i) => i.indexOf("json") !== -1)
const isYaml = options.path!.includes(".yml")
if (response.statusCode >= 400) {
if (isJson) {
reject(new HttpError(response, JSON.parse(data)))
Expand All @@ -192,7 +190,7 @@ Please double check that your authentication token is correct. Due to security r
}
}
else {
resolve(data.length === 0 ? null : (isJson || !options.path!.includes(".yml")) ? JSON.parse(data) : safeLoad(data))
resolve(data.length === 0 ? null : (isJson ? JSON.parse(data) : isYaml ? safeLoad(data) : data))
}
}
catch (e) {
Expand Down
6 changes: 3 additions & 3 deletions src/util/httpExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class HttpExecutorHolder {
}

export interface HttpExecutor {
request<T>(url: Url, token?: string | null, data?: {[name: string]: any; } | null, method?: string): Promise<T>
request<T>(url: Url, token?: string | null, data?: {[name: string]: any; } | null, method?: string, headers?: any): Promise<T>

download(url: string, destination: string, options?: DownloadOptions | null): Promise<string>
}
Expand Down Expand Up @@ -59,8 +59,8 @@ export function githubRequest<T>(path: string, token: string | null, data: {[nam
return request<T>({hostname: "api.github.com", path: path}, token, data, method)
}

export function request<T>(url: Url, token: string | null = null, data: {[name: string]: any; } | null = null, method: string = "GET"): Promise<T> {
return executorHolder.httpExecutor.request(url, token, data, method)
export function request<T>(url: Url, token: string | null = null, data: {[name: string]: any; } | null = null, method: string = "GET", headers: any = {}): Promise<T> {
return executorHolder.httpExecutor.request(url, token, data, method, headers)
}

export function checkSha2(sha2Header: string | null | undefined, sha2: string | null | undefined, callback: (error: Error | null) => void): boolean {
Expand Down
26 changes: 12 additions & 14 deletions src/util/nodeHttpExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,16 @@ export class NodeHttpExecutor implements HttpExecutor {

private httpsAgent: Promise<Agent> | null = null

request<T>(url: Url, token: string | null = null, data: {[name: string]: any; } | null = null, method: string = "GET"): Promise<T> {
request<T>(url: Url, token: string | null = null, data: {[name: string]: any; } | null = null, method: string = "GET", headers: any = {}): Promise<T> {

headers = Object.assign({"User-Agent": "electron-builder"}, headers)

const options: any = Object.assign({
method: method,
headers: {
"User-Agent": "electron-builder"
}
headers: headers
}, url)

if (url.hostname!!.includes("github") && !url.path!.endsWith(".yml")) {
if (url.hostname!!.includes("github") && !url.path!.endsWith(".yml") && !headers.Accept) {
options.headers.Accept = "application/vnd.github.v3+json"
}

Expand Down Expand Up @@ -159,14 +160,10 @@ Please double check that your authentication token is correct. Due to security r
return
}

if (options.path!.endsWith("/latest")) {
resolve(<any>{location: redirectUrl})
}
else {
this.doApiRequest(Object.assign({}, options, parseUrl(redirectUrl)), token, requestProcessor)
.then(<any>resolve)
.catch(reject)
}
this.doApiRequest(Object.assign({}, options, parseUrl(redirectUrl)), token, requestProcessor)
.then(<any>resolve)
.catch(reject)

return
}

Expand All @@ -180,6 +177,7 @@ Please double check that your authentication token is correct. Due to security r
try {
const contentType = response.headers["content-type"]
const isJson = contentType != null && contentType.includes("json")
const isYaml = options.path!.includes(".yml")
if (response.statusCode >= 400) {
if (isJson) {
reject(new HttpError(response, JSON.parse(data)))
Expand All @@ -189,7 +187,7 @@ Please double check that your authentication token is correct. Due to security r
}
}
else {
resolve(data.length === 0 ? null : (isJson || !options.path!.includes(".yml")) ? JSON.parse(data) : safeLoad(data))
resolve(data.length === 0 ? null : (isJson ? JSON.parse(data) : isYaml ? safeLoad(data) : data))
}
}
catch (e) {
Expand Down