Skip to content

Commit 2c65b5d

Browse files
committed
refactor: only make request once
1 parent d06c873 commit 2c65b5d

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
}
@@ -134,15 +129,12 @@ export class Storage {
134129
})
135130
return
136131
}
137-
if (resp.status !== 200) {
138-
vscode.window.showErrorMessage("Failed to fetch the Coder binary: " + resp.statusText)
139-
return
140-
}
141132

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

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

147139
const completed = await vscode.window.withProgress<boolean>(
148140
{
@@ -165,7 +157,7 @@ export class Storage {
165157
contentLengthPretty = " / " + prettyBytes(contentLength)
166158
}
167159

168-
const writeStream = createWriteStream(binPath, {
160+
const writeStream = createWriteStream(tempFile, {
169161
autoClose: true,
170162
mode: 0o755,
171163
})
@@ -201,8 +193,19 @@ export class Storage {
201193
if (!completed) {
202194
return
203195
}
196+
if (resp.status === 200) {
197+
this.output.appendLine(`Downloaded binary: ${binPath}`)
198+
await fs.rename(tempFile, binPath)
199+
return binPath
200+
}
201+
await fs.rm(tempFile)
202+
203+
if (resp.status !== 304) {
204+
vscode.window.showErrorMessage("Failed to fetch the Coder binary: " + resp.statusText)
205+
return undefined
206+
}
204207

205-
this.output.appendLine(`Downloaded binary: ${binPath}`)
208+
this.output.appendLine(`Using cached binary: ${binPath}`)
206209
return binPath
207210
}
208211

@@ -275,6 +278,13 @@ export class Storage {
275278
.catch(() => false)
276279
}
277280

281+
private async rmBinary(binPath: string): Promise<boolean> {
282+
return await fs
283+
.rm(binPath, { force: true })
284+
.then(() => true)
285+
.catch(() => false)
286+
}
287+
278288
private async checkBinaryValid(binPath: string): Promise<boolean> {
279289
return await new Promise<boolean>((resolve) => {
280290
try {
@@ -291,24 +301,6 @@ export class Storage {
291301
})
292302
}
293303

294-
private async checkBinaryOutdated(binName: string, baseURL: string, controller: AbortController): Promise<boolean> {
295-
const resp = await axios.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-
case 200:
305-
return true
306-
case 304:
307-
default:
308-
return false
309-
}
310-
}
311-
312304
private async updateSessionToken() {
313305
const token = await this.getSessionToken()
314306
if (token) {

0 commit comments

Comments
 (0)