diff --git a/src/main/Update.ts b/src/main/Update.ts index b3a4392..afab95f 100644 --- a/src/main/Update.ts +++ b/src/main/Update.ts @@ -46,8 +46,9 @@ export class Update async update(): Promise { - let firstInfo = await this.fetchInfo(this.config) - let temp = await httpFetch(firstInfo.updateUrl) + let http_no_cache = this.updater.readField('http_no_cache', 'string', undefined) + let firstInfo = await this.fetchInfo(this.config, http_no_cache) + let temp = await httpFetch(firstInfo.updateUrl, http_no_cache) let rawData = yaml.dump(temp) let remoteFiles = this.simpleFileObjectFromList(temp) @@ -143,7 +144,7 @@ export class Update await this.workdir.append(f).delete() // 下载新文件 - await this.download(this.workdir, downloadList, firstInfo.updateSource) + await this.download(this.workdir, downloadList, firstInfo.updateSource, http_no_cache) } else { this.updater.dispatchEvent('updating_new_files', []) this.updater.dispatchEvent('updating_old_files', []) @@ -156,7 +157,7 @@ export class Update this.updater.dispatchEvent('cleanup') } - async download(dir: FileObject, downloadList: { [key: string]: number }, updateSource: string): Promise + async download(dir: FileObject, downloadList: { [key: string]: number }, updateSource: string, http_no_cache: string|undefined = undefined): Promise { // 建立下载任务 let dq = new Array() @@ -186,7 +187,7 @@ export class Update await file.makeParentDirs() await httpGetFile(url, file, e_length, (bytesReceived: number, totalReceived: number) => { this.updater.dispatchEvent('updating_downloading', r_path, bytesReceived, totalReceived, e_length) - }) + }, http_no_cache) } } @@ -205,7 +206,7 @@ export class Update } } - async fetchInfo(config: any): Promise + async fetchInfo(config: any, http_no_cache: string|undefined = undefined): Promise { LogSys.debug('-----配置文件内容-----') LogSys.debug(config); @@ -218,7 +219,7 @@ export class Update let baseurl = server.substring(0, server.lastIndexOf('/') + 1) let resp = null as any try { - resp = await httpFetch(server) + resp = await httpFetch(server, http_no_cache) } catch (ex) { if(servers.length > 1 && // 有多个server源时 ex instanceof HTTPResponseException && // 是网络原因 diff --git a/src/main/utils/httpFetch.ts b/src/main/utils/httpFetch.ts index d3fe6a1..513236f 100644 --- a/src/main/utils/httpFetch.ts +++ b/src/main/utils/httpFetch.ts @@ -1,14 +1,20 @@ import { HTTPResponseException } from "../exceptions/HTTPResponseException"; import { UnableToDecodeException } from "../exceptions/UnableToDecodeException"; import { LogSys } from "../logging/LogSys"; +import { appendQueryParam } from "./utility"; const yaml = require('js-yaml') const nodefetch = require('node-fetch'); -export async function httpFetch(url: string): Promise +export async function httpFetch(url: string, http_no_cache: string|undefined = undefined): Promise { let raw = null try { url = encodeURI(url) + + // 避免缓存 + if(http_no_cache != undefined) + url = appendQueryParam(url, http_no_cache, new Date().getTime().toString()) + let response = await nodefetch(url) // response.status >= 200 && response.status < 300 diff --git a/src/main/utils/httpGetFile.ts b/src/main/utils/httpGetFile.ts index 6a10f52..4b65c14 100644 --- a/src/main/utils/httpGetFile.ts +++ b/src/main/utils/httpGetFile.ts @@ -8,13 +8,14 @@ import { MaxRedirectionReachedException } from "../exceptions/MaxRedirectionReac import fs = require('fs/promises') import https = require('https') import http = require('http') +import { appendQueryParam } from "./utility" export async function httpGetFile(url: string, file: FileObject, lengthExpected: number, callback: ((bytesReceived: number, totalReceived: number) => void)|undefined = undefined, + http_no_cache: string|undefined = undefined, timeout = 10 -):Promise -{ +):Promise { if(! await file.parent.exists()) throw new FileNotExistException('The file can not be opened, because it\'s parent do not exist: '+file.parent.path) @@ -24,6 +25,10 @@ export async function httpGetFile(url: string, try { let loopLimit = 10 let sendRequest = async (url2: string) => { + // 避免缓存 + if(http_no_cache != undefined) + url2 = appendQueryParam(url2, http_no_cache, new Date().getTime().toString()) + if(loopLimit -- <=0) throw new MaxRedirectionReachedException() await new Promise(((a, b) => { diff --git a/src/main/utils/utility.ts b/src/main/utils/utility.ts index 8c0facc..52de3f1 100644 --- a/src/main/utils/utility.ts +++ b/src/main/utils/utility.ts @@ -11,4 +11,14 @@ export async function countFiles(dir: FileObject): Promise for (let d of await dir.files()) c += (await d.isDir())? await countFiles(d) : 1 return c +} + +export function appendQueryParam(url: string, key: string, value: string): string +{ + if(url.indexOf('?') == -1) + url += '?' + + url += (url.endsWith('?') ? '' : '&') + key + '=' + value + + return url } \ No newline at end of file