Skip to content

Commit

Permalink
fix(updater): GitHub private repo for windows (#1382)
Browse files Browse the repository at this point in the history
Partial solution for #1370
  • Loading branch information
AlienHoboken authored and develar committed Mar 17, 2017
1 parent c0d299d commit 51d6e41
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
34 changes: 33 additions & 1 deletion packages/electron-updater/src/PrivateGitHubProvider.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { session } from "electron"
import { HttpError, request } from "electron-builder-http"
import { CancellationToken } from "electron-builder-http/out/CancellationToken"
import { GithubOptions, UpdateInfo } from "electron-builder-http/out/publishOptions"
import { RequestOptions } from "http"
import * as path from "path"
import { parse as parseUrl } from "url"
import { safeLoad } from "js-yaml"
import { FileInfo, formatUrl, getChannelFilename, getCurrentPlatform, getDefaultChannelName } from "./api"
import { validateUpdateInfo } from "./GenericProvider"
import { BaseGitHubProvider } from "./GitHubProvider"
Expand All @@ -13,8 +15,13 @@ export interface PrivateGitHubUpdateInfo extends UpdateInfo {
}

export class PrivateGitHubProvider extends BaseGitHubProvider<PrivateGitHubUpdateInfo> {
private sess: any

constructor(options: GithubOptions, private readonly token: string) {
super(options, "api.github.com")

this.sess = session.fromPartition('electron-updater')
this.registerHeaderRemovalListener();
}

async getLatestVersion(): Promise<PrivateGitHubUpdateInfo> {
Expand All @@ -24,11 +31,21 @@ export class PrivateGitHubProvider extends BaseGitHubProvider<PrivateGitHubUpdat

const assets = await this.getLatestVersionInfo(basePath, cancellationToken)
const requestOptions = Object.assign({
headers: this.configureHeaders("application/octet-stream")
headers: this.configureHeaders("application/octet-stream"),
session: this.sess
}, parseUrl(assets.find(it => it.name == channelFile)!.url))
let result: any
try {
result = await request<UpdateInfo>(requestOptions, cancellationToken)

if (typeof result === "string") {
if (getCurrentPlatform() === "darwin") {
result = JSON.parse(result)
}
else {
result = safeLoad(result)
}
}
}
catch (e) {
if (e instanceof HttpError && e.response.statusCode === 404) {
Expand All @@ -45,6 +62,20 @@ export class PrivateGitHubProvider extends BaseGitHubProvider<PrivateGitHubUpdat
return result
}

private registerHeaderRemovalListener(): void {
const filter = {
urls: ["*://*.amazonaws.com/*"]
};

this.sess.webRequest.onBeforeSendHeaders(filter, (details: any, callback: any) => {
if(details.requestHeaders['Authorization']) {
delete details.requestHeaders['Authorization']
}

callback({cancel: false, requestHeaders: details.requestHeaders})
});
};

private configureHeaders(accept: string) {
return Object.assign({
Accept: accept,
Expand Down Expand Up @@ -91,6 +122,7 @@ export class PrivateGitHubProvider extends BaseGitHubProvider<PrivateGitHubUpdat
url: versionInfo.assets.find(it => it.name == name)!.url,
sha2: versionInfo.sha2,
headers: headers,
session: this.sess
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion packages/electron-updater/src/electronHttpExecutor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { net } from "electron"
import { net, session } from "electron"
import { configureRequestOptions, DownloadOptions, dumpRequestOptions, HttpExecutor } from "electron-builder-http"
import { CancellationToken } from "electron-builder-http/out/CancellationToken"
import { ensureDir } from "fs-extra-p"
Expand Down Expand Up @@ -54,6 +54,7 @@ export class ElectronHttpExecutor extends HttpExecutor<Electron.RequestOptions,


protected doRequest(options: any, callback: (response: any) => void): any {
options.session = session.fromPartition('electron-updater')
return net.request(options, callback)
}
}

0 comments on commit 51d6e41

Please sign in to comment.