@@ -110,103 +110,117 @@ export class Storage {
110110 // Ignore all errors so we can catch a 404!
111111 validateStatus : ( ) => true ,
112112 } )
113- if ( resp . status === 404 ) {
114- vscode . window
115- . showErrorMessage (
116- "Coder isn't supported for your platform. Please open an issue, we'd love to support it!" ,
117- "Open an Issue" ,
118- )
119- . then ( ( value ) => {
120- if ( ! value ) {
121- return
122- }
123- const params = new URLSearchParams ( {
124- title : `Support the \`${ os } -${ arch } \` platform` ,
125- body : `I'd like to use the \`${ os } -${ arch } \` architecture with the VS Code extension.` ,
126- } )
127- const uri = vscode . Uri . parse ( `https://github.com/coder/vscode-coder/issues/new?` + params . toString ( ) )
128- vscode . env . openExternal ( uri )
129- } )
130- return
131- }
132113
133- const contentLength = Number . parseInt ( resp . headers [ "content-length" ] )
114+ switch ( resp . status ) {
115+ case 200 : {
116+ const contentLength = Number . parseInt ( resp . headers [ "content-length" ] )
117+
118+ // Ensure the binary directory exists!
119+ await fs . mkdir ( path . dirname ( binPath ) , { recursive : true } )
120+ const tempFile = binPath + ".temp-" + Math . random ( ) . toString ( 36 ) . substring ( 8 )
121+
122+ const completed = await vscode . window . withProgress < boolean > (
123+ {
124+ location : vscode . ProgressLocation . Notification ,
125+ title : `Downloading the latest binary (${ buildInfo . version } from ${ baseURI . authority } )` ,
126+ cancellable : true ,
127+ } ,
128+ async ( progress , token ) => {
129+ const readStream = resp . data as IncomingMessage
130+ let cancelled = false
131+ token . onCancellationRequested ( ( ) => {
132+ controller . abort ( )
133+ readStream . destroy ( )
134+ cancelled = true
135+ } )
134136
135- // Ensure the binary directory exists!
136- await fs . mkdir ( path . dirname ( binPath ) , { recursive : true } )
137- const tempFile = binPath + ".temp-" + Math . random ( ) . toString ( 36 ) . substring ( 8 )
137+ let contentLengthPretty = ""
138+ // Reverse proxies might not always send a content length!
139+ if ( ! Number . isNaN ( contentLength ) ) {
140+ contentLengthPretty = " / " + prettyBytes ( contentLength )
141+ }
138142
139- const completed = await vscode . window . withProgress < boolean > (
140- {
141- location : vscode . ProgressLocation . Notification ,
142- title : `Downloading the latest binary (${ buildInfo . version } from ${ baseURI . authority } )` ,
143- cancellable : true ,
144- } ,
145- async ( progress , token ) => {
146- const readStream = resp . data as IncomingMessage
147- let cancelled = false
148- token . onCancellationRequested ( ( ) => {
149- controller . abort ( )
150- readStream . destroy ( )
151- cancelled = true
152- } )
153-
154- let contentLengthPretty = ""
155- // Reverse proxies might not always send a content length!
156- if ( ! Number . isNaN ( contentLength ) ) {
157- contentLengthPretty = " / " + prettyBytes ( contentLength )
143+ const writeStream = createWriteStream ( tempFile , {
144+ autoClose : true ,
145+ mode : 0o755 ,
146+ } )
147+ let written = 0
148+ readStream . on ( "data" , ( buffer : Buffer ) => {
149+ writeStream . write ( buffer , ( ) => {
150+ written += buffer . byteLength
151+ progress . report ( {
152+ message : `${ prettyBytes ( written ) } ${ contentLengthPretty } ` ,
153+ increment : ( buffer . byteLength / contentLength ) * 100 ,
154+ } )
155+ } )
156+ } )
157+ try {
158+ await new Promise < void > ( ( resolve , reject ) => {
159+ readStream . on ( "error" , ( err ) => {
160+ reject ( err )
161+ } )
162+ readStream . on ( "close" , ( ) => {
163+ if ( cancelled ) {
164+ return reject ( )
165+ }
166+ writeStream . close ( )
167+ resolve ( )
168+ } )
169+ } )
170+ return true
171+ } catch ( ex ) {
172+ return false
173+ }
174+ } ,
175+ )
176+ if ( ! completed ) {
177+ return
158178 }
159-
160- const writeStream = createWriteStream ( tempFile , {
161- autoClose : true ,
162- mode : 0o755 ,
163- } )
164- let written = 0
165- readStream . on ( "data" , ( buffer : Buffer ) => {
166- writeStream . write ( buffer , ( ) => {
167- written += buffer . byteLength
168- progress . report ( {
169- message : `${ prettyBytes ( written ) } ${ contentLengthPretty } ` ,
170- increment : ( buffer . byteLength / contentLength ) * 100 ,
179+ this . output . appendLine ( `Downloaded binary: ${ binPath } ` )
180+ await fs . rename ( tempFile , binPath )
181+ await fs . rm ( tempFile )
182+ return binPath
183+ }
184+ case 304 : {
185+ this . output . appendLine ( `Using cached binary: ${ binPath } ` )
186+ return binPath
187+ }
188+ case 404 : {
189+ vscode . window
190+ . showErrorMessage (
191+ "Coder isn't supported for your platform. Please open an issue, we'd love to support it!" ,
192+ "Open an Issue" ,
193+ )
194+ . then ( ( value ) => {
195+ if ( ! value ) {
196+ return
197+ }
198+ const params = new URLSearchParams ( {
199+ title : `Support the \`${ os } -${ arch } \` platform` ,
200+ body : `I'd like to use the \`${ os } -${ arch } \` architecture with the VS Code extension.` ,
171201 } )
202+ const uri = vscode . Uri . parse ( `https://github.com/coder/vscode-coder/issues/new?` + params . toString ( ) )
203+ vscode . env . openExternal ( uri )
172204 } )
173- } )
174- try {
175- await new Promise < void > ( ( resolve , reject ) => {
176- readStream . on ( "error" , ( err ) => {
177- reject ( err )
178- } )
179- readStream . on ( "close" , ( ) => {
180- if ( cancelled ) {
181- return reject ( )
182- }
183- writeStream . close ( )
184- resolve ( )
205+ return undefined
206+ }
207+ default : {
208+ vscode . window
209+ . showErrorMessage ( "Failed to download binary. Please open an issue." , "Open an Issue" )
210+ . then ( ( value ) => {
211+ if ( ! value ) {
212+ return
213+ }
214+ const params = new URLSearchParams ( {
215+ title : `Failed to download binary on \` ${ os } - ${ arch } \`` ,
216+ body : `Received status code \` ${ resp . status } \` when downloading the binary.` ,
185217 } )
218+ const uri = vscode . Uri . parse ( `https://github.com/coder/vscode-coder/issues/new?` + params . toString ( ) )
219+ vscode . env . openExternal ( uri )
186220 } )
187- return true
188- } catch ( ex ) {
189- return false
190- }
191- } ,
192- )
193- if ( ! completed ) {
194- return
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
221+ return undefined
222+ }
206223 }
207-
208- this . output . appendLine ( `Using cached binary: ${ binPath } ` )
209- return binPath
210224 }
211225
212226 // getBinaryCachePath returns the path where binaries are cached.
0 commit comments