Skip to content
This repository has been archived by the owner on Jun 6, 2023. It is now read-only.

Commit

Permalink
fix: 修复cdn导致的缓存问题
Browse files Browse the repository at this point in the history
  • Loading branch information
asforest committed Jan 5, 2022
1 parent 19de37f commit 3b3d84e
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 10 deletions.
15 changes: 8 additions & 7 deletions src/main/Update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ export class Update

async update(): Promise<void>
{
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)

Expand Down Expand Up @@ -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', [])
Expand All @@ -156,7 +157,7 @@ export class Update
this.updater.dispatchEvent('cleanup')
}

async download(dir: FileObject, downloadList: { [key: string]: number }, updateSource: string): Promise<void>
async download(dir: FileObject, downloadList: { [key: string]: number }, updateSource: string, http_no_cache: string|undefined = undefined): Promise<void>
{
// 建立下载任务
let dq = new Array<DownloadTask>()
Expand Down Expand Up @@ -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)
}
}

Expand All @@ -205,7 +206,7 @@ export class Update
}
}

async fetchInfo(config: any): Promise<any>
async fetchInfo(config: any, http_no_cache: string|undefined = undefined): Promise<any>
{
LogSys.debug('-----配置文件内容-----')
LogSys.debug(config);
Expand All @@ -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 && // 是网络原因
Expand Down
8 changes: 7 additions & 1 deletion src/main/utils/httpFetch.ts
Original file line number Diff line number Diff line change
@@ -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<any>
export async function httpFetch(url: string, http_no_cache: string|undefined = undefined): Promise<any>
{
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
Expand Down
9 changes: 7 additions & 2 deletions src/main/utils/httpGetFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>
{
):Promise<void> {
if(! await file.parent.exists())
throw new FileNotExistException('The file can not be opened, because it\'s parent do not exist: '+file.parent.path)

Expand All @@ -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) => {
Expand Down
10 changes: 10 additions & 0 deletions src/main/utils/utility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,14 @@ export async function countFiles(dir: FileObject): Promise<number>
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
}

0 comments on commit 3b3d84e

Please sign in to comment.