Skip to content

Commit 400ba7a

Browse files
committed
refactor: only make request once
1 parent 197385a commit 400ba7a

File tree

1 file changed

+28
-36
lines changed

1 file changed

+28
-36
lines changed

src/storage.ts

Lines changed: 28 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -93,18 +93,13 @@ export class Storage {
9393
const controller = new AbortController()
9494

9595
if (exists) {
96-
this.output.appendLine(`Checking if binary outdated...`)
97-
const outdated = await this.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-
const valid = await this.checkBinaryValid(binPath)
106-
if (valid) {
107-
return binPath
96+
this.output.appendLine(`Found existing binary: ${binPath}`)
97+
const valid = await this.checkBinaryValid(binPath)
98+
if (!valid) {
99+
const removed = await this.rmBinary(binPath)
100+
if (!removed) {
101+
vscode.window.showErrorMessage("Failed to remove existing binary!")
102+
return undefined
108103
}
109104
}
110105
}
@@ -138,15 +133,12 @@ export class Storage {
138133
})
139134
return
140135
}
141-
if (resp.status !== 200) {
142-
vscode.window.showErrorMessage("Failed to fetch the Coder binary: " + resp.statusText)
143-
return
144-
}
145136

146137
const contentLength = Number.parseInt(resp.headers["content-length"])
147138

148139
// Ensure the binary directory exists!
149140
await fs.mkdir(path.dirname(binPath), { recursive: true })
141+
const tempFile = binPath + ".temp-" + Math.random().toString(36).substring(8)
150142

151143
const completed = await vscode.window.withProgress<boolean>(
152144
{
@@ -169,7 +161,7 @@ export class Storage {
169161
contentLengthPretty = " / " + prettyBytes(contentLength)
170162
}
171163

172-
const writeStream = createWriteStream(binPath, {
164+
const writeStream = createWriteStream(tempFile, {
173165
autoClose: true,
174166
mode: 0o755,
175167
})
@@ -205,8 +197,19 @@ export class Storage {
205197
if (!completed) {
206198
return
207199
}
200+
if (resp.status === 200) {
201+
this.output.appendLine(`Downloaded binary: ${binPath}`)
202+
await fs.rename(tempFile, binPath)
203+
return binPath
204+
}
205+
await fs.rm(tempFile)
206+
207+
if (resp.status !== 304) {
208+
vscode.window.showErrorMessage("Failed to fetch the Coder binary: " + resp.statusText)
209+
return undefined
210+
}
208211

209-
this.output.appendLine(`Downloaded binary: ${binPath}`)
212+
this.output.appendLine(`Using cached binary: ${binPath}`)
210213
return binPath
211214
}
212215

@@ -279,6 +282,13 @@ export class Storage {
279282
.catch(() => false)
280283
}
281284

285+
private async rmBinary(binPath: string): Promise<boolean> {
286+
return await fs
287+
.rm(binPath, { force: true })
288+
.then(() => true)
289+
.catch(() => false)
290+
}
291+
282292
private async checkBinaryValid(binPath: string): Promise<boolean> {
283293
return await new Promise<boolean>((resolve) => {
284294
try {
@@ -295,24 +305,6 @@ export class Storage {
295305
})
296306
}
297307

298-
private async checkBinaryOutdated(binName: string, baseURL: string, controller: AbortController): Promise<boolean> {
299-
const resp = await axios.get("/bin/" + binName, {
300-
signal: controller.signal,
301-
baseURL: baseURL,
302-
headers: {
303-
"If-None-Match": this.getBinaryETag(),
304-
},
305-
})
306-
307-
switch (resp.status) {
308-
case 200:
309-
return true
310-
case 304:
311-
default:
312-
return false
313-
}
314-
}
315-
316308
private async updateSessionToken() {
317309
const token = await this.getSessionToken()
318310
if (token) {

0 commit comments

Comments
 (0)