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(updater): GitHub private repo for windows, partial solution for #1370 #1382

Merged
merged 1 commit into from
Mar 17, 2017
Merged
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
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)
}
}