Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd06c873

Browse files
committed
refactor: update logic for downloading binary
This makes significant changes to the `fetchBinary` logic.First, it refactors a couple pieces of logic into methods on the`Storage` class to make the code more readable.Then it modifies the flow to first check if the binary is outdated. Ifit is, then it downloads the latest version.
1 parentc598fdb commitd06c873

File tree

1 file changed

+64
-25
lines changed

1 file changed

+64
-25
lines changed

‎src/storage.ts

Lines changed: 64 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
importaxiosfrom"axios"
22
import{execFile}from"child_process"
33
import{getBuildInfo}from"coder/site/src/api/api"
4+
import*ascryptofrom"crypto"
45
import{createWriteStream}from"fs"
56
import{ensureDir}from"fs-extra"
67
importfsfrom"fs/promises"
@@ -81,31 +82,7 @@ export class Storage {
8182

8283
constbuildInfo=awaitgetBuildInfo()
8384
constbinPath=this.binaryPath()
84-
constexists=awaitfs
85-
.stat(binPath)
86-
.then(()=>true)
87-
.catch(()=>false)
88-
if(exists){
89-
// Even if the file exists, it could be corrupted.
90-
// We run `coder version` to ensure the binary can be executed.
91-
this.output.appendLine(`Using cached binary:${binPath}`)
92-
constvalid=awaitnewPromise<boolean>((resolve)=>{
93-
try{
94-
execFile(binPath,["version"],(err)=>{
95-
if(err){
96-
this.output.appendLine("Check for binary corruption: "+err)
97-
}
98-
resolve(err===null)
99-
})
100-
}catch(ex){
101-
this.output.appendLine("The cached binary cannot be executed: "+ex)
102-
resolve(false)
103-
}
104-
})
105-
if(valid){
106-
returnbinPath
107-
}
108-
}
85+
constexists=awaitthis.checkBinaryExists(binPath)
10986
constos=goos()
11087
constarch=goarch()
11188
letbinName=`coder-${os}-${arch}`
@@ -114,6 +91,23 @@ export class Storage {
11491
binName+=".exe"
11592
}
11693
constcontroller=newAbortController()
94+
95+
if(exists){
96+
this.output.appendLine(`Checking if binary outdated...`)
97+
constoutdated=awaitthis.checkBinaryOutdated(binName,baseURL,controller)
98+
// If it's outdated, we fall through to the download logic.
99+
if(outdated){
100+
this.output.appendLine(`Found outdated version.`)
101+
}else{
102+
// Even if the file exists, it could be corrupted.
103+
// We run `coder version` to ensure the binary can be executed.
104+
this.output.appendLine(`Using existing binary:${binPath}`)
105+
constvalid=awaitthis.checkBinaryValid(binPath)
106+
if(valid){
107+
returnbinPath
108+
}
109+
}
110+
}
117111
constresp=awaitaxios.get("/bin/"+binName,{
118112
signal:controller.signal,
119113
baseURL:baseURL,
@@ -236,6 +230,10 @@ export class Storage {
236230
returnpath.join(this.globalStorageUri.fsPath,"url")
237231
}
238232

233+
publicgetBinaryETag():string{
234+
returncrypto.createHash("sha1").update(this.binaryPath()).digest("hex")
235+
}
236+
239237
privateappDataDir():string{
240238
switch(process.platform){
241239
case"darwin":
@@ -270,6 +268,47 @@ export class Storage {
270268
returnbinPath
271269
}
272270

271+
privateasynccheckBinaryExists(binPath:string):Promise<boolean>{
272+
returnawaitfs
273+
.stat(binPath)
274+
.then(()=>true)
275+
.catch(()=>false)
276+
}
277+
278+
privateasynccheckBinaryValid(binPath:string):Promise<boolean>{
279+
returnawaitnewPromise<boolean>((resolve)=>{
280+
try{
281+
execFile(binPath,["version"],(err)=>{
282+
if(err){
283+
this.output.appendLine("Check for binary corruption: "+err)
284+
}
285+
resolve(err===null)
286+
})
287+
}catch(ex){
288+
this.output.appendLine("The cached binary cannot be executed: "+ex)
289+
resolve(false)
290+
}
291+
})
292+
}
293+
294+
privateasynccheckBinaryOutdated(binName:string,baseURL:string,controller:AbortController):Promise<boolean>{
295+
constresp=awaitaxios.get("/bin/"+binName,{
296+
signal:controller.signal,
297+
baseURL:baseURL,
298+
headers:{
299+
"If-None-Match":this.getBinaryETag(),
300+
},
301+
})
302+
303+
switch(resp.status){
304+
case200:
305+
returntrue
306+
case304:
307+
default:
308+
returnfalse
309+
}
310+
}
311+
273312
privateasyncupdateSessionToken(){
274313
consttoken=awaitthis.getSessionToken()
275314
if(token){

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp