From 51d6e41a910464b46c7fed56b4007e50474b4f2e Mon Sep 17 00:00:00 2001 From: Jeremiah Date: Fri, 17 Mar 2017 00:26:10 -0700 Subject: [PATCH] fix(updater): GitHub private repo for windows (#1382) Partial solution for #1370 --- .../src/PrivateGitHubProvider.ts | 34 ++++++++++++++++++- .../src/electronHttpExecutor.ts | 3 +- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/packages/electron-updater/src/PrivateGitHubProvider.ts b/packages/electron-updater/src/PrivateGitHubProvider.ts index 3154217a640..62d36fa18ef 100644 --- a/packages/electron-updater/src/PrivateGitHubProvider.ts +++ b/packages/electron-updater/src/PrivateGitHubProvider.ts @@ -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" @@ -13,8 +15,13 @@ export interface PrivateGitHubUpdateInfo extends UpdateInfo { } export class PrivateGitHubProvider extends BaseGitHubProvider { + 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 { @@ -24,11 +31,21 @@ export class PrivateGitHubProvider extends BaseGitHubProvider it.name == channelFile)!.url)) let result: any try { result = await request(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) { @@ -45,6 +62,20 @@ export class PrivateGitHubProvider extends BaseGitHubProvider { + if(details.requestHeaders['Authorization']) { + delete details.requestHeaders['Authorization'] + } + + callback({cancel: false, requestHeaders: details.requestHeaders}) + }); + }; + private configureHeaders(accept: string) { return Object.assign({ Accept: accept, @@ -91,6 +122,7 @@ export class PrivateGitHubProvider extends BaseGitHubProvider it.name == name)!.url, sha2: versionInfo.sha2, headers: headers, + session: this.sess } } } diff --git a/packages/electron-updater/src/electronHttpExecutor.ts b/packages/electron-updater/src/electronHttpExecutor.ts index 6c20dd243ed..4752455d3fe 100644 --- a/packages/electron-updater/src/electronHttpExecutor.ts +++ b/packages/electron-updater/src/electronHttpExecutor.ts @@ -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" @@ -54,6 +54,7 @@ export class ElectronHttpExecutor extends HttpExecutor void): any { + options.session = session.fromPartition('electron-updater') return net.request(options, callback) } } \ No newline at end of file