@@ -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
}
@@ -134,15 +129,12 @@ export class Storage {
134
129
} )
135
130
return
136
131
}
137
- if ( resp . status !== 200 ) {
138
- vscode . window . showErrorMessage ( "Failed to fetch the Coder binary: " + resp . statusText )
139
- return
140
- }
141
132
142
133
const contentLength = Number . parseInt ( resp . headers [ "content-length" ] )
143
134
144
135
// Ensure the binary directory exists!
145
136
await fs . mkdir ( path . dirname ( binPath ) , { recursive : true } )
137
+ const tempFile = binPath + ".temp-" + Math . random ( ) . toString ( 36 ) . substring ( 8 )
146
138
147
139
const completed = await vscode . window . withProgress < boolean > (
148
140
{
@@ -165,7 +157,7 @@ export class Storage {
165
157
contentLengthPretty = " / " + prettyBytes ( contentLength )
166
158
}
167
159
168
- const writeStream = createWriteStream ( binPath , {
160
+ const writeStream = createWriteStream ( tempFile , {
169
161
autoClose : true ,
170
162
mode : 0o755 ,
171
163
} )
@@ -201,8 +193,19 @@ export class Storage {
201
193
if ( ! completed ) {
202
194
return
203
195
}
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
+ }
204
207
205
- this . output . appendLine ( `Downloaded binary: ${ binPath } ` )
208
+ this . output . appendLine ( `Using cached binary: ${ binPath } ` )
206
209
return binPath
207
210
}
208
211
@@ -275,6 +278,13 @@ export class Storage {
275
278
. catch ( ( ) => false )
276
279
}
277
280
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
+
278
288
private async checkBinaryValid ( binPath : string ) : Promise < boolean > {
279
289
return await new Promise < boolean > ( ( resolve ) => {
280
290
try {
@@ -291,24 +301,6 @@ export class Storage {
291
301
} )
292
302
}
293
303
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
-
312
304
private async updateSessionToken ( ) {
313
305
const token = await this . getSessionToken ( )
314
306
if ( token ) {
0 commit comments