Skip to content

Commit

Permalink
feat(electron-updater): add proxy authentication support to electron-…
Browse files Browse the repository at this point in the history
…updater

Close #1530
  • Loading branch information
BurningEnlightenment authored and develar committed May 7, 2017
1 parent 0fbad33 commit a892a5b
Show file tree
Hide file tree
Showing 8 changed files with 243 additions and 186 deletions.
14 changes: 14 additions & 0 deletions docs/Auto Update.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ Emitted on progress. Only supported over Windows build, since `Squirrel.Mac` [do
* [`.setRequestHeaders(value)`](#module_electron-updater/out/api.Provider+setRequestHeaders)
* [`.getUpdateFile(versionInfo)`](#module_electron-updater/out/api.Provider+getUpdateFile) ⇒ <code>Promise&lt;[FileInfo](#FileInfo)&gt;</code>
* [.UpdaterSignal](#UpdaterSignal)
* [`.login(handler)`](#module_electron-updater/out/api.UpdaterSignal+login)
* [`.progress(handler)`](#module_electron-updater/out/api.UpdaterSignal+progress)
* [`.updateCancelled(handler)`](#module_electron-updater/out/api.UpdaterSignal+updateCancelled)
* [`.updateDownloaded(handler)`](#module_electron-updater/out/api.UpdaterSignal+updateDownloaded)
Expand Down Expand Up @@ -195,10 +196,23 @@ Emitted on progress. Only supported over Windows build, since `Squirrel.Mac` [do
**Kind**: class of [<code>electron-updater/out/api</code>](#module_electron-updater/out/api)

* [.UpdaterSignal](#UpdaterSignal)
* [`.login(handler)`](#module_electron-updater/out/api.UpdaterSignal+login)
* [`.progress(handler)`](#module_electron-updater/out/api.UpdaterSignal+progress)
* [`.updateCancelled(handler)`](#module_electron-updater/out/api.UpdaterSignal+updateCancelled)
* [`.updateDownloaded(handler)`](#module_electron-updater/out/api.UpdaterSignal+updateDownloaded)

<a name="module_electron-updater/out/api.UpdaterSignal+login"></a>

#### `updaterSignal.login(handler)`
Emitted when an authenticating proxy is asking for user credentials.

**Kind**: instance method of [<code>UpdaterSignal</code>](#UpdaterSignal)
**See**: [Electron docs](https://github.com/electron/electron/blob/master/docs/api/client-request.md#event-login)

| Param | Type |
| --- | --- |
| handler | <code>module:electron-updater/out/api.__type</code> |

<a name="module_electron-updater/out/api.UpdaterSignal+progress"></a>

#### `updaterSignal.progress(handler)`
Expand Down
6 changes: 6 additions & 0 deletions docs/api/electron-builder.md
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,7 @@ Developer API only. See [[Options]] for user documentation.
* [`.createFilter(ignoreFiles, rawFilter, excludePatterns)`](#module_electron-builder/out/fileMatcher.FileMatcher+createFilter) ⇒ <code>module:electron-builder-util/out/fs.__type</code>
* [`.isEmpty()`](#module_electron-builder/out/fileMatcher.FileMatcher+isEmpty) ⇒ <code>boolean</code>
* [`.prependPattern(pattern)`](#module_electron-builder/out/fileMatcher.FileMatcher+prependPattern)
* [`.toString()`](#module_electron-builder/out/fileMatcher.FileMatcher+toString) ⇒ <code>string</code>
* [`.copyFiles(patterns)`](#module_electron-builder/out/fileMatcher.copyFiles) ⇒ <code>Promise&lt;any&gt;</code>
* [`.createFileMatcher(info, appDir, resourcesPath, macroExpander, platformSpecificBuildOptions, buildResourceDir)`](#module_electron-builder/out/fileMatcher.createFileMatcher) ⇒ <code>[FileMatcher](#FileMatcher)</code>
* [`.getFileMatchers(config, name, defaultSrc, defaultDest, allowAdvancedMatching, macroExpander, customBuildOptions)`](#module_electron-builder/out/fileMatcher.getFileMatchers) ⇒ <code>null</code> \| <code>Array</code>
Expand All @@ -454,6 +455,7 @@ Developer API only. See [[Options]] for user documentation.
* [`.createFilter(ignoreFiles, rawFilter, excludePatterns)`](#module_electron-builder/out/fileMatcher.FileMatcher+createFilter) ⇒ <code>module:electron-builder-util/out/fs.__type</code>
* [`.isEmpty()`](#module_electron-builder/out/fileMatcher.FileMatcher+isEmpty) ⇒ <code>boolean</code>
* [`.prependPattern(pattern)`](#module_electron-builder/out/fileMatcher.FileMatcher+prependPattern)
* [`.toString()`](#module_electron-builder/out/fileMatcher.FileMatcher+toString) ⇒ <code>string</code>

<a name="module_electron-builder/out/fileMatcher.FileMatcher+addAllPattern"></a>

Expand Down Expand Up @@ -506,6 +508,10 @@ Developer API only. See [[Options]] for user documentation.
| --- | --- |
| pattern | <code>string</code> |

<a name="module_electron-builder/out/fileMatcher.FileMatcher+toString"></a>

#### `fileMatcher.toString()` ⇒ <code>string</code>
**Kind**: instance method of [<code>FileMatcher</code>](#FileMatcher)
<a name="module_electron-builder/out/fileMatcher.copyFiles"></a>

### `electron-builder/out/fileMatcher.copyFiles(patterns)` ⇒ <code>Promise&lt;any&gt;</code>
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
"depcheck": "^0.6.7",
"develar-typescript-json-schema": "0.11.0",
"globby": "^6.1.0",
"jest-cli": "^19.0.2",
"jest-cli": "^20.0.0",
"jest-environment-node-debug": "^2.0.0",
"jest-junit": "^1.5.1",
"jsdoc-to-markdown": "^3.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/electron-updater/src/AppUpdater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export abstract class AppUpdater extends EventEmitter {
}
else {
this.app = require("electron").app
executorHolder.httpExecutor = new ElectronHttpExecutor()
executorHolder.httpExecutor = new ElectronHttpExecutor((authInfo, callback) => this.emit("login", authInfo, callback))
this.untilAppReady = new BluebirdPromise(resolve => {
if (this.app.isReady()) {
if (this.logger != null) {
Expand Down
11 changes: 11 additions & 0 deletions packages/electron-updater/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ProgressInfo } from "electron-builder-http/out/ProgressCallbackTransfor
import { VersionInfo } from "electron-builder-http/out/publishOptions"
import { EventEmitter } from "events"
import { format as buggyFormat, Url } from "url"
import { LoginCallback } from "./electronHttpExecutor"

export interface FileInfo {
readonly name: string
Expand Down Expand Up @@ -56,10 +57,20 @@ export interface UpdateCheckResult {

export const DOWNLOAD_PROGRESS = "download-progress"

export type LoginHandler = (event: Event, request: Electron.LoginRequest, authInfo: Electron.LoginAuthInfo, callback: LoginCallback) => void

export class UpdaterSignal {
constructor(private emitter: EventEmitter) {
}

/**
* Emitted when an authenticating proxy is asking for user credentials.
* @see [Electron docs](https://github.com/electron/electron/blob/master/docs/api/client-request.md#event-login)
*/
login(handler: LoginHandler) {
addHandler(this.emitter, "login", handler)
}

progress(handler: (info: ProgressInfo) => void) {
addHandler(this.emitter, DOWNLOAD_PROGRESS, handler)
}
Expand Down
22 changes: 18 additions & 4 deletions packages/electron-updater/src/electronHttpExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ export const NET_SESSION_NAME = "electron-updater"

const debug = _debug("electron-builder")

export type LoginCallback = (username: string, password: string) => void

export class ElectronHttpExecutor extends HttpExecutor<Electron.RequestOptions, Electron.ClientRequest> {
constructor(private proxyLoginCallback?: (authInfo: Electron.LoginAuthInfo, callback: LoginCallback) => void) {
super()
}

async download(url: string, destination: string, options: DownloadOptions): Promise<string> {
if (options == null || !options.skipDirCreation) {
await ensureDir(path.dirname(destination))
Expand Down Expand Up @@ -40,16 +46,17 @@ export class ElectronHttpExecutor extends HttpExecutor<Electron.RequestOptions,
if (debug.enabled) {
debug(`request: ${dumpRequestOptions(options)}`)
}

return cancellationToken.createPromise<T>((resolve, reject, onCancel) => {
const request = net.request(options, response => {
const request = net.request(Object.assign({session: session.fromPartition(NET_SESSION_NAME)}, options), response => {
try {
this.handleResponse(response, options, cancellationToken, resolve, reject, redirectCount, requestProcessor)
}
catch (e) {
reject(e)
}
})
this.addProxyLoginHandler(request)
this.addTimeOutHandler(request, reject)
request.on("error", reject)
requestProcessor(request, reject)
Expand All @@ -59,7 +66,14 @@ export class ElectronHttpExecutor extends HttpExecutor<Electron.RequestOptions,


protected doRequest(options: any, callback: (response: any) => void): any {
options.session = session.fromPartition(NET_SESSION_NAME)
return net.request(options, callback)
const request = net.request(Object.assign({session: session.fromPartition(NET_SESSION_NAME)}, options), callback)
this.addProxyLoginHandler(request)
return request
}

private addProxyLoginHandler(request: Electron.ClientRequest) {
if (this.proxyLoginCallback != null) {
request.on("login", this.proxyLoginCallback)
}
}
}
2 changes: 1 addition & 1 deletion test/src/helpers/runTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ async function runTests() {
config: config,
runInBand: runInBand,
testPathPattern: args.length > 0 ? args.join("|") : null,
}, rootDir, (result: any) => {
}, [rootDir], (result: any) => {
const code = !result || result.success ? 0 : 1
removeSync(TEST_DIR)
process.exitCode = code
Expand Down
Loading

0 comments on commit a892a5b

Please sign in to comment.