Skip to content

Commit

Permalink
fix: always revalidate cache
Browse files Browse the repository at this point in the history
Close #1186
  • Loading branch information
develar committed Jan 30, 2017
1 parent eee3b71 commit 02a96f4
Show file tree
Hide file tree
Showing 14 changed files with 46 additions and 57 deletions.
1 change: 1 addition & 0 deletions .idea/dictionaries/develar.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
"path-sort": "^0.1.0",
"source-map-support": "^0.4.10",
"ts-babel": "^1.3.5",
"tslint": "^4.3.1",
"tslint": "^4.4.2",
"typescript": "^2.1.5",
"whitespace": "^2.1.0"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/electron-builder-http/src/bintray.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BintrayOptions } from "./publishOptions"
import { request, configureRequestOptions } from "./httpExecutor"

export function bintrayRequest<T>(path: string, auth: string | null, data: {[name: string]: any; } | null = null, method?: string): Promise<T> {
export function bintrayRequest<T>(path: string, auth: string | null, data: {[name: string]: any; } | null = null, method?: "GET" | "DELETE" | "PUT"): Promise<T> {
return request<T>(configureRequestOptions({hostname: "api.bintray.com", path: path}, auth, method), data)
}

Expand Down
19 changes: 9 additions & 10 deletions packages/electron-builder-http/src/httpExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,14 @@ export abstract class HttpExecutor<REQUEST_OPTS, REQUEST> {
protected readonly debug = _debug("electron-builder")

request<T>(options: RequestOptions, data?: { [name: string]: any; } | null): Promise<T> {
options = Object.assign({headers: {"User-Agent": "electron-builder"}}, options)

configureRequestOptions(options)
const encodedData = data == null ? undefined : new Buffer(JSON.stringify(data))
if (encodedData != null) {
options.method = "post"
if (options.headers == null) {
options.headers = {}
}

options.headers["Content-Type"] = "application/json"
options.headers["Content-Length"] = encodedData.length
options.headers!["Content-Type"] = "application/json"
options.headers!["Content-Length"] = encodedData.length
}
return this.doApiRequest<T>(<any>options, it => (<any>it).end(encodedData), 0)
return this.doApiRequest<T>(<REQUEST_OPTS>options, it => (<any>it).end(encodedData), 0)
}

protected abstract doApiRequest<T>(options: REQUEST_OPTS, requestProcessor: (request: REQUEST, reject: (error: Error) => void) => void, redirectCount: number): Promise<T>
Expand Down Expand Up @@ -270,7 +265,7 @@ function configurePipes(options: DownloadOptions, response: any, destination: st
fileOut.on("finish", () => (<any>fileOut.close)(callback))
}

export function configureRequestOptions(options: RequestOptions, token: string | null, method?: string): RequestOptions {
export function configureRequestOptions(options: RequestOptions, token?: string | null, method?: "GET" | "DELETE" | "PUT"): RequestOptions {
if (method != null) {
options.method = method
}
Expand All @@ -286,6 +281,10 @@ export function configureRequestOptions(options: RequestOptions, token: string |
if (headers["User-Agent"] == null) {
headers["User-Agent"] = "electron-builder"
}

if ((method == null || method === "GET") || headers["Cache-Control"] == null) {
headers["Cache-Control"] = "no-cache"
}
return options
}

Expand Down
1 change: 0 additions & 1 deletion packages/electron-builder/src/publish/BintrayPublisher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ export class BintrayPublisher extends Publisher {
path: `/content/${this.client.owner}/${this.client.repo}/${this.client.packageName}/${version.name}/${fileName}`,
method: "PUT",
headers: {
"User-Agent": "electron-builder",
"Content-Length": dataLength,
"X-Bintray-Override": "1",
"X-Bintray-Publish": "1",
Expand Down
3 changes: 1 addition & 2 deletions packages/electron-builder/src/publish/gitHubPublisher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ export class GitHubPublisher extends Publisher {
method: "POST",
headers: {
Accept: "application/vnd.github.v3+json",
"User-Agent": "electron-builder",
"Content-Type": mime.lookup(fileName),
"Content-Length": dataLength
}
Expand Down Expand Up @@ -180,7 +179,7 @@ export class GitHubPublisher extends Publisher {
warn(`Cannot delete release ${release.id}`)
}

private githubRequest<T>(path: string, token: string | null, data: {[name: string]: any; } | null = null, method?: string): Promise<T> {
private githubRequest<T>(path: string, token: string | null, data: {[name: string]: any; } | null = null, method?: "GET" | "DELETE" | "PUT"): Promise<T> {
return this.httpExecutor.request<T>(configureRequestOptions({
hostname: "api.github.com",
path: path,
Expand Down
10 changes: 4 additions & 6 deletions packages/electron-builder/src/util/nodeHttpExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import BluebirdPromise from "bluebird-lst-c"
import * as path from "path"
import { homedir } from "os"
import { parse as parseIni } from "ini"
import { HttpExecutor, DownloadOptions } from "electron-builder-http"
import { HttpExecutor, DownloadOptions, configureRequestOptions } from "electron-builder-http"
import { RequestOptions } from "https"
import { parse as parseUrl } from "url"

Expand All @@ -24,14 +24,12 @@ export class NodeHttpExecutor extends HttpExecutor<RequestOptions, ClientRequest
const agent = await this.httpsAgentPromise
return await new BluebirdPromise<string>((resolve, reject) => {
const parsedUrl = parseUrl(url)
this.doDownload({
this.doDownload(configureRequestOptions({
hostname: parsedUrl.hostname,
path: parsedUrl.path,
headers: Object.assign({
"User-Agent": "electron-builder"
}, options == null ? null : options.headers),
headers: (options == null ? null : options.headers) || undefined,
agent: agent,
}, destination, 0, options || {}, (error: Error) => {
}), destination, 0, options || {}, (error: Error) => {
if (error == null) {
resolve(destination)
}
Expand Down
12 changes: 6 additions & 6 deletions packages/electron-updater/src/AppUpdater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ export abstract class AppUpdater extends EventEmitter {

public readonly signals = new UpdaterSignal(this)

private appUpdateConfigPath: string | null
private _appUpdateConfigPath: string | null

setUpdateConfigPath(value: string | null) {
set updateConfigPath(value: string | null) {
this.clientPromise = null
this.appUpdateConfigPath = value
this._appUpdateConfigPath = value
}

protected updateAvailable = false
Expand Down Expand Up @@ -232,10 +232,10 @@ export abstract class AppUpdater extends EventEmitter {
abstract quitAndInstall(): void

async loadUpdateConfig() {
if (this.appUpdateConfigPath == null) {
this.appUpdateConfigPath = path.join(process.resourcesPath, "app-update.yml")
if (this._appUpdateConfigPath == null) {
this._appUpdateConfigPath = path.join(process.resourcesPath, "app-update.yml")
}
return safeLoad(await readFile(this.appUpdateConfigPath, "utf-8"))
return safeLoad(await readFile(this._appUpdateConfigPath, "utf-8"))
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/electron-updater/src/GenericProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class GenericProvider extends Provider<UpdateInfo> {
hostname: this.baseUrl.hostname,
path: `${pathname}${this.baseUrl.search || ""}`,
protocol: this.baseUrl.protocol,
headers: Object.assign({"Cache-Control": "no-cache, no-store, must-revalidate"}, this.requestHeaders)
headers: this.requestHeaders || undefined
}
if (this.baseUrl.port != null) {
options.port = parseInt(this.baseUrl.port, 10)
Expand Down
4 changes: 1 addition & 3 deletions packages/electron-updater/src/NsisUpdater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export class NsisUpdater extends AppUpdater {
protected async doDownloadUpdate(versionInfo: VersionInfo, fileInfo: FileInfo) {
const downloadOptions: DownloadOptions = {
skipDirCreation: true,
headers: this.requestHeaders || undefined,
}

if (this.listenerCount(DOWNLOAD_PROGRESS) > 0) {
Expand All @@ -33,9 +34,6 @@ export class NsisUpdater extends AppUpdater {
if (fileInfo != null && fileInfo.sha2 != null) {
downloadOptions.sha2 = fileInfo.sha2
}
if (this.requestHeaders != null) {
downloadOptions.headers = this.requestHeaders
}

const logger = this.logger
const tempDir = await mkdtemp(`${path.join(tmpdir(), "up")}-`)
Expand Down
12 changes: 5 additions & 7 deletions packages/electron-updater/src/electronHttpExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { net } from "electron"
import { ensureDir } from "fs-extra-p"
import BluebirdPromise from "bluebird-lst-c"
import * as path from "path"
import { HttpExecutor, DownloadOptions, dumpRequestOptions } from "electron-builder-http"
import { HttpExecutor, DownloadOptions, dumpRequestOptions, configureRequestOptions } from "electron-builder-http"
import { parse as parseUrl } from "url"

export class ElectronHttpExecutor extends HttpExecutor<Electron.RequestOptions, Electron.ClientRequest> {
Expand All @@ -14,15 +14,13 @@ export class ElectronHttpExecutor extends HttpExecutor<Electron.RequestOptions,
return await new BluebirdPromise<string>((resolve, reject) => {
const parsedUrl = parseUrl(url)

this.doDownload({
this.doDownload(configureRequestOptions({
protocol: parsedUrl.protocol,
hostname: parsedUrl.hostname,
path: parsedUrl.path,
port: parsedUrl.port ? parsedUrl.port : undefined,
headers: Object.assign({
"User-Agent": "electron-builder"
}, options == null ? null : options.headers),
}, destination, 0, options || {}, (error: Error) => {
port: parsedUrl.port ? parseInt(parsedUrl.port, 10) : undefined,
headers: (options == null ? null : options.headers) || undefined,
}), destination, 0, options || {}, (error: Error) => {
if (error == null) {
resolve(destination)
}
Expand Down
5 changes: 1 addition & 4 deletions packages/lint.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const fs = require("fs")
const path = require("path")

const configuration = {
"extends": "tslint:recommended",
"extends": "tslint:latest",
"rules": {
"no-invalid-this": [true],
"member-ordering": [
Expand Down Expand Up @@ -60,9 +60,6 @@ const configuration = {
"no-bitwise": false,
"jsdoc-format": false,
"no-for-in-array": true,
"prefer-const": true,
"interface-over-type-literal": true,
"no-string-throw": true,
}
}
const options = {
Expand Down
28 changes: 14 additions & 14 deletions test/src/nsisUpdaterTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ test("cannot find suitable file for version", async () => {

test("file url", async () => {
const updater = new NsisUpdater()
updater.setUpdateConfigPath(await writeUpdateConfig(<BintrayOptions>{
updater.updateConfigPath = await writeUpdateConfig(<BintrayOptions>{
provider: "bintray",
owner: "actperepo",
package: "TestApp",
}))
})

const actualEvents: Array<string> = []
const expectedEvents = ["checking-for-update", "update-available", "update-downloaded"]
Expand All @@ -72,10 +72,10 @@ test("file url", async () => {

test("file url generic", async () => {
const updater = new NsisUpdater()
updater.setUpdateConfigPath(await writeUpdateConfig(<GenericServerOptions>{
updater.updateConfigPath = await writeUpdateConfig(<GenericServerOptions>{
provider: "generic",
url: "https://develar.s3.amazonaws.com/test",
}))
})

const actualEvents = trackEvents(updater)

Expand All @@ -88,11 +88,11 @@ test("file url generic", async () => {

test("sha2 mismatch error event", async () => {
const updater = new NsisUpdater()
updater.setUpdateConfigPath(await writeUpdateConfig(<GenericServerOptions>{
updater.updateConfigPath = await writeUpdateConfig(<GenericServerOptions>{
provider: "generic",
url: "https://develar.s3.amazonaws.com/test",
channel: "beta",
}))
})
updater.logger = console

const actualEvents = trackEvents(updater)
Expand All @@ -106,10 +106,10 @@ test("sha2 mismatch error event", async () => {

test("file url generic - manual download", async () => {
const updater = new NsisUpdater()
updater.setUpdateConfigPath(await writeUpdateConfig(<GenericServerOptions>{
updater.updateConfigPath = await writeUpdateConfig(<GenericServerOptions>{
provider: "generic",
url: "https://develar.s3.amazonaws.com/test",
}))
})
updater.autoDownload = false

const actualEvents = trackEvents(updater)
Expand All @@ -125,10 +125,10 @@ test("file url generic - manual download", async () => {
// https://github.com/electron-userland/electron-builder/issues/1045
test("checkForUpdates several times", async () => {
const updater = new NsisUpdater()
updater.setUpdateConfigPath(await writeUpdateConfig(<GenericServerOptions>{
updater.updateConfigPath = await writeUpdateConfig(<GenericServerOptions>{
provider: "generic",
url: "https://develar.s3.amazonaws.com/test",
}))
})

const actualEvents = trackEvents(updater)

Expand All @@ -145,11 +145,11 @@ test("checkForUpdates several times", async () => {

test("file url github", async () => {
const updater = new NsisUpdater()
updater.setUpdateConfigPath(await writeUpdateConfig(<GithubOptions>{
updater.updateConfigPath = await writeUpdateConfig(<GithubOptions>{
provider: "github",
owner: "develar",
repo: "__test_nsis_release",
}))
})

const actualEvents: Array<string> = []
const expectedEvents = ["checking-for-update", "update-available", "update-downloaded"]
Expand Down Expand Up @@ -177,10 +177,10 @@ test("test error", async () => {

test("test download progress", async () => {
const updater = new NsisUpdater()
updater.setUpdateConfigPath(await writeUpdateConfig({
updater.updateConfigPath = await writeUpdateConfig({
provider: "generic",
url: "https://develar.s3.amazonaws.com/test",
}))
})
updater.autoDownload = false

const progressEvents: Array<any> = []
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2767,7 +2767,7 @@ ts-babel@^1.3.5:
markdown-it "^8.2.2"
source-map-support "^0.4.10"

tslint@^4.3.1:
tslint@^4.4.2:
version "4.4.2"
resolved "https://registry.yarnpkg.com/tslint/-/tslint-4.4.2.tgz#b14cb79ae039c72471ab4c2627226b940dda19c6"
dependencies:
Expand Down

0 comments on commit 02a96f4

Please sign in to comment.