Skip to content

Commit a892a5b

Browse files
BurningEnlightenmentdevelar
authored andcommitted
feat(electron-updater): add proxy authentication support to electron-updater
Close #1530
1 parent 0fbad33 commit a892a5b

File tree

8 files changed

+243
-186
lines changed

8 files changed

+243
-186
lines changed

docs/Auto Update.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ Emitted on progress. Only supported over Windows build, since `Squirrel.Mac` [do
116116
* [`.setRequestHeaders(value)`](#module_electron-updater/out/api.Provider+setRequestHeaders)
117117
* [`.getUpdateFile(versionInfo)`](#module_electron-updater/out/api.Provider+getUpdateFile) ⇒ <code>Promise&lt;[FileInfo](#FileInfo)&gt;</code>
118118
* [.UpdaterSignal](#UpdaterSignal)
119+
* [`.login(handler)`](#module_electron-updater/out/api.UpdaterSignal+login)
119120
* [`.progress(handler)`](#module_electron-updater/out/api.UpdaterSignal+progress)
120121
* [`.updateCancelled(handler)`](#module_electron-updater/out/api.UpdaterSignal+updateCancelled)
121122
* [`.updateDownloaded(handler)`](#module_electron-updater/out/api.UpdaterSignal+updateDownloaded)
@@ -195,10 +196,23 @@ Emitted on progress. Only supported over Windows build, since `Squirrel.Mac` [do
195196
**Kind**: class of [<code>electron-updater/out/api</code>](#module_electron-updater/out/api)
196197

197198
* [.UpdaterSignal](#UpdaterSignal)
199+
* [`.login(handler)`](#module_electron-updater/out/api.UpdaterSignal+login)
198200
* [`.progress(handler)`](#module_electron-updater/out/api.UpdaterSignal+progress)
199201
* [`.updateCancelled(handler)`](#module_electron-updater/out/api.UpdaterSignal+updateCancelled)
200202
* [`.updateDownloaded(handler)`](#module_electron-updater/out/api.UpdaterSignal+updateDownloaded)
201203

204+
<a name="module_electron-updater/out/api.UpdaterSignal+login"></a>
205+
206+
#### `updaterSignal.login(handler)`
207+
Emitted when an authenticating proxy is asking for user credentials.
208+
209+
**Kind**: instance method of [<code>UpdaterSignal</code>](#UpdaterSignal)
210+
**See**: [Electron docs](https://github.com/electron/electron/blob/master/docs/api/client-request.md#event-login)
211+
212+
| Param | Type |
213+
| --- | --- |
214+
| handler | <code>module:electron-updater/out/api.__type</code> |
215+
202216
<a name="module_electron-updater/out/api.UpdaterSignal+progress"></a>
203217

204218
#### `updaterSignal.progress(handler)`

docs/api/electron-builder.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,7 @@ Developer API only. See [[Options]] for user documentation.
430430
* [`.createFilter(ignoreFiles, rawFilter, excludePatterns)`](#module_electron-builder/out/fileMatcher.FileMatcher+createFilter) ⇒ <code>module:electron-builder-util/out/fs.__type</code>
431431
* [`.isEmpty()`](#module_electron-builder/out/fileMatcher.FileMatcher+isEmpty) ⇒ <code>boolean</code>
432432
* [`.prependPattern(pattern)`](#module_electron-builder/out/fileMatcher.FileMatcher+prependPattern)
433+
* [`.toString()`](#module_electron-builder/out/fileMatcher.FileMatcher+toString) ⇒ <code>string</code>
433434
* [`.copyFiles(patterns)`](#module_electron-builder/out/fileMatcher.copyFiles) ⇒ <code>Promise&lt;any&gt;</code>
434435
* [`.createFileMatcher(info, appDir, resourcesPath, macroExpander, platformSpecificBuildOptions, buildResourceDir)`](#module_electron-builder/out/fileMatcher.createFileMatcher) ⇒ <code>[FileMatcher](#FileMatcher)</code>
435436
* [`.getFileMatchers(config, name, defaultSrc, defaultDest, allowAdvancedMatching, macroExpander, customBuildOptions)`](#module_electron-builder/out/fileMatcher.getFileMatchers) ⇒ <code>null</code> \| <code>Array</code>
@@ -454,6 +455,7 @@ Developer API only. See [[Options]] for user documentation.
454455
* [`.createFilter(ignoreFiles, rawFilter, excludePatterns)`](#module_electron-builder/out/fileMatcher.FileMatcher+createFilter) ⇒ <code>module:electron-builder-util/out/fs.__type</code>
455456
* [`.isEmpty()`](#module_electron-builder/out/fileMatcher.FileMatcher+isEmpty) ⇒ <code>boolean</code>
456457
* [`.prependPattern(pattern)`](#module_electron-builder/out/fileMatcher.FileMatcher+prependPattern)
458+
* [`.toString()`](#module_electron-builder/out/fileMatcher.FileMatcher+toString) ⇒ <code>string</code>
457459

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

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

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

511517
### `electron-builder/out/fileMatcher.copyFiles(patterns)` ⇒ <code>Promise&lt;any&gt;</code>

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@
8383
"depcheck": "^0.6.7",
8484
"develar-typescript-json-schema": "0.11.0",
8585
"globby": "^6.1.0",
86-
"jest-cli": "^19.0.2",
86+
"jest-cli": "^20.0.0",
8787
"jest-environment-node-debug": "^2.0.0",
8888
"jest-junit": "^1.5.1",
8989
"jsdoc-to-markdown": "^3.0.0",

packages/electron-updater/src/AppUpdater.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ export abstract class AppUpdater extends EventEmitter {
9494
}
9595
else {
9696
this.app = require("electron").app
97-
executorHolder.httpExecutor = new ElectronHttpExecutor()
97+
executorHolder.httpExecutor = new ElectronHttpExecutor((authInfo, callback) => this.emit("login", authInfo, callback))
9898
this.untilAppReady = new BluebirdPromise(resolve => {
9999
if (this.app.isReady()) {
100100
if (this.logger != null) {

packages/electron-updater/src/api.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { ProgressInfo } from "electron-builder-http/out/ProgressCallbackTransfor
44
import { VersionInfo } from "electron-builder-http/out/publishOptions"
55
import { EventEmitter } from "events"
66
import { format as buggyFormat, Url } from "url"
7+
import { LoginCallback } from "./electronHttpExecutor"
78

89
export interface FileInfo {
910
readonly name: string
@@ -56,10 +57,20 @@ export interface UpdateCheckResult {
5657

5758
export const DOWNLOAD_PROGRESS = "download-progress"
5859

60+
export type LoginHandler = (event: Event, request: Electron.LoginRequest, authInfo: Electron.LoginAuthInfo, callback: LoginCallback) => void
61+
5962
export class UpdaterSignal {
6063
constructor(private emitter: EventEmitter) {
6164
}
6265

66+
/**
67+
* Emitted when an authenticating proxy is asking for user credentials.
68+
* @see [Electron docs](https://github.com/electron/electron/blob/master/docs/api/client-request.md#event-login)
69+
*/
70+
login(handler: LoginHandler) {
71+
addHandler(this.emitter, "login", handler)
72+
}
73+
6374
progress(handler: (info: ProgressInfo) => void) {
6475
addHandler(this.emitter, DOWNLOAD_PROGRESS, handler)
6576
}

packages/electron-updater/src/electronHttpExecutor.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,13 @@ export const NET_SESSION_NAME = "electron-updater"
1010

1111
const debug = _debug("electron-builder")
1212

13+
export type LoginCallback = (username: string, password: string) => void
14+
1315
export class ElectronHttpExecutor extends HttpExecutor<Electron.RequestOptions, Electron.ClientRequest> {
16+
constructor(private proxyLoginCallback?: (authInfo: Electron.LoginAuthInfo, callback: LoginCallback) => void) {
17+
super()
18+
}
19+
1420
async download(url: string, destination: string, options: DownloadOptions): Promise<string> {
1521
if (options == null || !options.skipDirCreation) {
1622
await ensureDir(path.dirname(destination))
@@ -40,16 +46,17 @@ export class ElectronHttpExecutor extends HttpExecutor<Electron.RequestOptions,
4046
if (debug.enabled) {
4147
debug(`request: ${dumpRequestOptions(options)}`)
4248
}
43-
49+
4450
return cancellationToken.createPromise<T>((resolve, reject, onCancel) => {
45-
const request = net.request(options, response => {
51+
const request = net.request(Object.assign({session: session.fromPartition(NET_SESSION_NAME)}, options), response => {
4652
try {
4753
this.handleResponse(response, options, cancellationToken, resolve, reject, redirectCount, requestProcessor)
4854
}
4955
catch (e) {
5056
reject(e)
5157
}
5258
})
59+
this.addProxyLoginHandler(request)
5360
this.addTimeOutHandler(request, reject)
5461
request.on("error", reject)
5562
requestProcessor(request, reject)
@@ -59,7 +66,14 @@ export class ElectronHttpExecutor extends HttpExecutor<Electron.RequestOptions,
5966

6067

6168
protected doRequest(options: any, callback: (response: any) => void): any {
62-
options.session = session.fromPartition(NET_SESSION_NAME)
63-
return net.request(options, callback)
69+
const request = net.request(Object.assign({session: session.fromPartition(NET_SESSION_NAME)}, options), callback)
70+
this.addProxyLoginHandler(request)
71+
return request
72+
}
73+
74+
private addProxyLoginHandler(request: Electron.ClientRequest) {
75+
if (this.proxyLoginCallback != null) {
76+
request.on("login", this.proxyLoginCallback)
77+
}
6478
}
6579
}

test/src/helpers/runTests.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ async function runTests() {
168168
config: config,
169169
runInBand: runInBand,
170170
testPathPattern: args.length > 0 ? args.join("|") : null,
171-
}, rootDir, (result: any) => {
171+
}, [rootDir], (result: any) => {
172172
const code = !result || result.success ? 0 : 1
173173
removeSync(TEST_DIR)
174174
process.exitCode = code

0 commit comments

Comments
 (0)