@@ -93,18 +93,13 @@ export class Storage {
93
93
const controller = new AbortController ( )
94
94
95
95
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
108
103
}
109
104
}
110
105
}
@@ -138,15 +133,12 @@ export class Storage {
138
133
} )
139
134
return
140
135
}
141
- if ( resp . status !== 200 ) {
142
- vscode . window . showErrorMessage ( "Failed to fetch the Coder binary: " + resp . statusText )
143
- return
144
- }
145
136
146
137
const contentLength = Number . parseInt ( resp . headers [ "content-length" ] )
147
138
148
139
// Ensure the binary directory exists!
149
140
await fs . mkdir ( path . dirname ( binPath ) , { recursive : true } )
141
+ const tempFile = binPath + ".temp-" + Math . random ( ) . toString ( 36 ) . substring ( 8 )
150
142
151
143
const completed = await vscode . window . withProgress < boolean > (
152
144
{
@@ -169,7 +161,7 @@ export class Storage {
169
161
contentLengthPretty = " / " + prettyBytes ( contentLength )
170
162
}
171
163
172
- const writeStream = createWriteStream ( binPath , {
164
+ const writeStream = createWriteStream ( tempFile , {
173
165
autoClose : true ,
174
166
mode : 0o755 ,
175
167
} )
@@ -205,8 +197,19 @@ export class Storage {
205
197
if ( ! completed ) {
206
198
return
207
199
}
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
+ }
208
211
209
- this . output . appendLine ( `Downloaded binary: ${ binPath } ` )
212
+ this . output . appendLine ( `Using cached binary: ${ binPath } ` )
210
213
return binPath
211
214
}
212
215
@@ -279,6 +282,13 @@ export class Storage {
279
282
. catch ( ( ) => false )
280
283
}
281
284
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
+
282
292
private async checkBinaryValid ( binPath : string ) : Promise < boolean > {
283
293
return await new Promise < boolean > ( ( resolve ) => {
284
294
try {
@@ -295,24 +305,6 @@ export class Storage {
295
305
} )
296
306
}
297
307
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
-
316
308
private async updateSessionToken ( ) {
317
309
const token = await this . getSessionToken ( )
318
310
if ( token ) {
0 commit comments