Skip to content

Commit

Permalink
refactor: electron-updater using non-existent Electron classes "Reque…
Browse files Browse the repository at this point in the history
…stOptions" and "LoginAuthInfo"

Close #1572
  • Loading branch information
develar committed May 31, 2017
1 parent 5f43249 commit 81c95f1
Show file tree
Hide file tree
Showing 16 changed files with 7,831 additions and 31 deletions.
5 changes: 5 additions & 0 deletions docs/Auto Update.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ Emitted on progress. Only supported over Windows build, since `Squirrel.Mac` [do
* [`.getCurrentPlatform()`](#module_electron-updater.getCurrentPlatform) ⇒ <code>any</code>
* [`.getCustomChannelName(channel)`](#module_electron-updater.getCustomChannelName) ⇒ <code>string</code>
* [`.getDefaultChannelName()`](#module_electron-updater.getDefaultChannelName) ⇒ <code>string</code>
* [`.isUseOldMacProvider()`](#module_electron-updater.isUseOldMacProvider) ⇒ <code>boolean</code>

<a name="FileInfo"></a>

Expand Down Expand Up @@ -446,5 +447,9 @@ Emitted when an authenticating proxy is asking for user credentials.

### `electron-updater.getDefaultChannelName()`<code>string</code>
**Kind**: method of [<code>electron-updater</code>](#module_electron-updater)
<a name="module_electron-updater.isUseOldMacProvider"></a>

### `electron-updater.isUseOldMacProvider()`<code>boolean</code>
**Kind**: method of [<code>electron-updater</code>](#module_electron-updater)

<!-- end of generated block -->
2 changes: 1 addition & 1 deletion docs/api/electron-builder-http.md
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@

| Param | Type |
| --- | --- |
| options | <code>module:electron-builder-http.REQUEST_OPTS</code> |
| options | <code>any</code> |
| cancellationToken | <code>[CancellationToken](#CancellationToken)</code> |
| requestProcessor | <code>callback</code> |
| redirectCount | <code>number</code> |
Expand Down
2 changes: 1 addition & 1 deletion docs/api/electron-updater.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ Developer API only. See [[Auto Update]] for user documentation.

| Param | Type |
| --- | --- |
| options | <code>Electron:RequestOptions</code> |
| options | <code>any</code> |
| cancellationToken | <code>[CancellationToken](electron-builder-http#CancellationToken)</code> |
| requestProcessor | <code>callback</code> |
| redirectCount | <code>number</code> |
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@
"yargs": "^8.0.1"
},
"devDependencies": {
"@types/electron": "^1.4.38",
"@types/ini": "^1.3.29",
"@types/jest": "^19.2.3",
"@types/js-yaml": "^3.5.30",
Expand Down
22 changes: 11 additions & 11 deletions packages/electron-builder-http/src/httpExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { createHash } from "crypto"
import _debug from "debug"
import { EventEmitter } from "events"
import { createWriteStream } from "fs-extra-p"
import { RequestOptions } from "http"
import { IncomingMessage, RequestOptions } from "http"
import { Socket } from "net"
import { Transform } from "stream"
import { parse as parseUrl } from "url"
Expand Down Expand Up @@ -35,16 +35,16 @@ export interface DownloadOptions {
}

export class HttpExecutorHolder {
private _httpExecutor: HttpExecutor<any, any>
private _httpExecutor: HttpExecutor<any>

get httpExecutor(): HttpExecutor<any, any> {
get httpExecutor(): HttpExecutor<any> {
if (this._httpExecutor == null) {
this._httpExecutor = new (require("electron-builder-util/out/nodeHttpExecutor").NodeHttpExecutor)()
}
return this._httpExecutor
}

set httpExecutor(value: HttpExecutor<any, any>) {
set httpExecutor(value: HttpExecutor<any>) {
this._httpExecutor = value
}
}
Expand All @@ -63,7 +63,7 @@ export class HttpError extends Error {
}
}

export abstract class HttpExecutor<REQUEST_OPTS, REQUEST> {
export abstract class HttpExecutor<REQUEST> {
protected readonly maxRedirects = 10

request<T>(options: RequestOptions, cancellationToken: CancellationToken, data?: { [name: string]: any; } | null): Promise<T> {
Expand All @@ -74,10 +74,10 @@ export abstract class HttpExecutor<REQUEST_OPTS, REQUEST> {
options.headers!["Content-Type"] = "application/json"
options.headers!["Content-Length"] = encodedData.length
}
return this.doApiRequest<T>(<REQUEST_OPTS>options, cancellationToken, it => (<any>it).end(encodedData), 0)
return this.doApiRequest<T>(options, cancellationToken, it => (<any>it).end(encodedData), 0)
}

protected abstract doApiRequest<T>(options: REQUEST_OPTS, cancellationToken: CancellationToken, requestProcessor: (request: REQUEST, reject: (error: Error) => void) => void, redirectCount: number): Promise<T>
protected abstract doApiRequest<T>(options: any, cancellationToken: CancellationToken, requestProcessor: (request: REQUEST, reject: (error: Error) => void) => void, redirectCount: number): Promise<T>

abstract download(url: string, destination: string, options: DownloadOptions): Promise<string>

Expand Down Expand Up @@ -109,7 +109,7 @@ export abstract class HttpExecutor<REQUEST_OPTS, REQUEST> {
}

const newUrl = parseUrl(redirectUrl)
this.doApiRequest(<REQUEST_OPTS>Object.assign({}, options, newUrl), cancellationToken, requestProcessor, redirectCount)
this.doApiRequest(Object.assign({}, options, newUrl), cancellationToken, requestProcessor, redirectCount)
.then(resolve)
.catch(reject)
return
Expand Down Expand Up @@ -147,8 +147,8 @@ export abstract class HttpExecutor<REQUEST_OPTS, REQUEST> {
protected abstract doRequest(options: any, callback: (response: any) => void): any

protected doDownload(requestOptions: any, destination: string, redirectCount: number, options: DownloadOptions, callback: (error: Error | null) => void, onCancel: (callback: () => void) => void) {
const request = this.doRequest(requestOptions, (response: Electron.IncomingMessage) => {
if (response.statusCode >= 400) {
const request = this.doRequest(requestOptions, (response: IncomingMessage) => {
if (response.statusCode! >= 400) {
callback(new Error(`Cannot download "${requestOptions.protocol || "https:"}//${requestOptions.hostname}${requestOptions.path}", status ${response.statusCode}: ${response.statusMessage}`))
return
}
Expand Down Expand Up @@ -295,7 +295,7 @@ export function configureRequestOptions(options: RequestOptions, token?: string
}

// do not specify for node (in any case we use https module)
if (options.protocol == null && process.versions["electron"] != null) {
if (options.protocol == null && (<any>process.versions)["electron"] != null) {
options.protocol = "https:"
}
return options
Expand Down
2 changes: 1 addition & 1 deletion packages/electron-builder-util/src/nodeHttpExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { parse as parseUrl } from "url"

const debug = _debug("electron-builder")

export class NodeHttpExecutor extends HttpExecutor<RequestOptions, ClientRequest> {
export class NodeHttpExecutor extends HttpExecutor<ClientRequest> {
private httpsAgentPromise: Promise<Agent> | null

async download(url: string, destination: string, options: DownloadOptions): Promise<string> {
Expand Down
3 changes: 3 additions & 0 deletions packages/electron-builder/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,8 @@
"include": [
"src/**/*.ts",
"../../typings/*.d.ts"
],
"exclude": [
"../../typings/electron.d.ts"
]
}
2 changes: 1 addition & 1 deletion packages/electron-updater/src/AppUpdater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ export abstract class AppUpdater extends EventEmitter {

async loadUpdateConfig() {
if (this._appUpdateConfigPath == null) {
this._appUpdateConfigPath = require("electron-is-dev") ? path.join(this.app.getAppPath(), "dev-app-update.yml") : path.join(process.resourcesPath, "app-update.yml")
this._appUpdateConfigPath = require("electron-is-dev") ? path.join(this.app.getAppPath(), "dev-app-update.yml") : path.join(process.resourcesPath!, "app-update.yml")
}
return safeLoad(await readFile(this._appUpdateConfigPath, "utf-8"))
}
Expand Down
2 changes: 1 addition & 1 deletion packages/electron-updater/src/NsisUpdater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export class NsisUpdater extends AppUpdater {
}

try {
spawn(path.join(process.resourcesPath, "elevate.exe"), [setupPath].concat(args), spawnOptions)
spawn(path.join(process.resourcesPath!, "elevate.exe"), [setupPath].concat(args), spawnOptions)
.unref()
}
catch (e) {
Expand Down
2 changes: 1 addition & 1 deletion packages/electron-updater/src/PrivateGitHubProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export interface PrivateGitHubUpdateInfo extends UpdateInfo {
}

export class PrivateGitHubProvider extends BaseGitHubProvider<PrivateGitHubUpdateInfo> {
private readonly netSession = session.fromPartition(NET_SESSION_NAME)
private readonly netSession = (<any>session).fromPartition(NET_SESSION_NAME)

constructor(options: GithubOptions, private readonly token: string) {
super(options, "api.github.com")
Expand Down
10 changes: 5 additions & 5 deletions packages/electron-updater/src/electronHttpExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ 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) {
export class ElectronHttpExecutor extends HttpExecutor<Electron.ClientRequest> {
constructor(private proxyLoginCallback?: (authInfo: any, callback: LoginCallback) => void) {
super()
}

Expand Down Expand Up @@ -42,13 +42,13 @@ export class ElectronHttpExecutor extends HttpExecutor<Electron.RequestOptions,
})
}

doApiRequest<T>(options: Electron.RequestOptions, cancellationToken: CancellationToken, requestProcessor: (request: Electron.ClientRequest, reject: (error: Error) => void) => void, redirectCount: number = 0): Promise<T> {
doApiRequest<T>(options: any, cancellationToken: CancellationToken, requestProcessor: (request: Electron.ClientRequest, reject: (error: Error) => void) => void, redirectCount: number = 0): Promise<T> {
if (debug.enabled) {
debug(`request: ${dumpRequestOptions(options)}`)
}

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


protected doRequest(options: any, callback: (response: any) => void): any {
const request = net.request(Object.assign({session: session.fromPartition(NET_SESSION_NAME)}, options), callback)
const request = (<any>net).request(Object.assign({session: (<any>session).fromPartition(NET_SESSION_NAME)}, options), callback)
this.addProxyLoginHandler(request)
return request
}
Expand Down
2 changes: 1 addition & 1 deletion packages/electron-updater/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export interface UpdateCheckResult {

export const DOWNLOAD_PROGRESS = "download-progress"

export type LoginHandler = (authInfo: Electron.LoginAuthInfo, callback: LoginCallback) => void
export type LoginHandler = (authInfo: any, callback: LoginCallback) => void

export class UpdaterSignal {
constructor(private emitter: EventEmitter) {
Expand Down
3 changes: 2 additions & 1 deletion packages/electron-updater/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
},
"files": [
"../../typings/semver.d.ts",
"../../typings/debug.d.ts"
"../../typings/debug.d.ts",
"../../typings/electron.d.ts"
],
"include": [
"src/**/*.ts"
Expand Down
3 changes: 3 additions & 0 deletions test/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,8 @@
"typings/*.d.ts",
"src/**/*.ts",
"../packages/*/out/*.d.ts"
],
"exclude": [
"../typings/electron.d.ts"
]
}
Loading

0 comments on commit 81c95f1

Please sign in to comment.