Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit369a823

Browse files
committed
refactor: switch on status to avoid piping
1 parent400ba7a commit369a823

File tree

1 file changed

+102
-88
lines changed

1 file changed

+102
-88
lines changed

‎src/storage.ts

Lines changed: 102 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -114,103 +114,117 @@ export class Storage {
114114
// Ignore all errors so we can catch a 404!
115115
validateStatus:()=>true,
116116
})
117-
if(resp.status===404){
118-
vscode.window
119-
.showErrorMessage(
120-
"Coder isn't supported for your platform. Please open an issue, we'd love to support it!",
121-
"Open an Issue",
122-
)
123-
.then((value)=>{
124-
if(!value){
125-
return
126-
}
127-
constparams=newURLSearchParams({
128-
title:`Support the \`${os}-${arch}\` platform`,
129-
body:`I'd like to use the \`${os}-${arch}\` architecture with the VS Code extension.`,
130-
})
131-
consturi=vscode.Uri.parse(`https://github.com/coder/vscode-coder/issues/new?`+params.toString())
132-
vscode.env.openExternal(uri)
133-
})
134-
return
135-
}
136-
137-
constcontentLength=Number.parseInt(resp.headers["content-length"])
138117

139-
// Ensure the binary directory exists!
140-
awaitfs.mkdir(path.dirname(binPath),{recursive:true})
141-
consttempFile=binPath+".temp-"+Math.random().toString(36).substring(8)
118+
switch(resp.status){
119+
case200:{
120+
constcontentLength=Number.parseInt(resp.headers["content-length"])
121+
122+
// Ensure the binary directory exists!
123+
awaitfs.mkdir(path.dirname(binPath),{recursive:true})
124+
consttempFile=binPath+".temp-"+Math.random().toString(36).substring(8)
125+
126+
constcompleted=awaitvscode.window.withProgress<boolean>(
127+
{
128+
location:vscode.ProgressLocation.Notification,
129+
title:`Downloading the latest binary (${buildInfo.version} from${baseURI.authority})`,
130+
cancellable:true,
131+
},
132+
async(progress,token)=>{
133+
constreadStream=resp.dataasIncomingMessage
134+
letcancelled=false
135+
token.onCancellationRequested(()=>{
136+
controller.abort()
137+
readStream.destroy()
138+
cancelled=true
139+
})
142140

143-
constcompleted=awaitvscode.window.withProgress<boolean>(
144-
{
145-
location:vscode.ProgressLocation.Notification,
146-
title:`Downloading the latest binary (${buildInfo.version} from${baseURI.authority})`,
147-
cancellable:true,
148-
},
149-
async(progress,token)=>{
150-
constreadStream=resp.dataasIncomingMessage
151-
letcancelled=false
152-
token.onCancellationRequested(()=>{
153-
controller.abort()
154-
readStream.destroy()
155-
cancelled=true
156-
})
141+
letcontentLengthPretty=""
142+
// Reverse proxies might not always send a content length!
143+
if(!Number.isNaN(contentLength)){
144+
contentLengthPretty=" / "+prettyBytes(contentLength)
145+
}
157146

158-
letcontentLengthPretty=""
159-
// Reverse proxies might not always send a content length!
160-
if(!Number.isNaN(contentLength)){
161-
contentLengthPretty=" / "+prettyBytes(contentLength)
147+
constwriteStream=createWriteStream(tempFile,{
148+
autoClose:true,
149+
mode:0o755,
150+
})
151+
letwritten=0
152+
readStream.on("data",(buffer:Buffer)=>{
153+
writeStream.write(buffer,()=>{
154+
written+=buffer.byteLength
155+
progress.report({
156+
message:`${prettyBytes(written)}${contentLengthPretty}`,
157+
increment:(buffer.byteLength/contentLength)*100,
158+
})
159+
})
160+
})
161+
try{
162+
awaitnewPromise<void>((resolve,reject)=>{
163+
readStream.on("error",(err)=>{
164+
reject(err)
165+
})
166+
readStream.on("close",()=>{
167+
if(cancelled){
168+
returnreject()
169+
}
170+
writeStream.close()
171+
resolve()
172+
})
173+
})
174+
returntrue
175+
}catch(ex){
176+
returnfalse
177+
}
178+
},
179+
)
180+
if(!completed){
181+
return
162182
}
163-
164-
constwriteStream=createWriteStream(tempFile,{
165-
autoClose:true,
166-
mode:0o755,
167-
})
168-
letwritten=0
169-
readStream.on("data",(buffer:Buffer)=>{
170-
writeStream.write(buffer,()=>{
171-
written+=buffer.byteLength
172-
progress.report({
173-
message:`${prettyBytes(written)}${contentLengthPretty}`,
174-
increment:(buffer.byteLength/contentLength)*100,
183+
this.output.appendLine(`Downloaded binary:${binPath}`)
184+
awaitfs.rename(tempFile,binPath)
185+
awaitfs.rm(tempFile)
186+
returnbinPath
187+
}
188+
case304:{
189+
this.output.appendLine(`Using cached binary:${binPath}`)
190+
returnbinPath
191+
}
192+
case404:{
193+
vscode.window
194+
.showErrorMessage(
195+
"Coder isn't supported for your platform. Please open an issue, we'd love to support it!",
196+
"Open an Issue",
197+
)
198+
.then((value)=>{
199+
if(!value){
200+
return
201+
}
202+
constparams=newURLSearchParams({
203+
title:`Support the \`${os}-${arch}\` platform`,
204+
body:`I'd like to use the \`${os}-${arch}\` architecture with the VS Code extension.`,
175205
})
206+
consturi=vscode.Uri.parse(`https://github.com/coder/vscode-coder/issues/new?`+params.toString())
207+
vscode.env.openExternal(uri)
176208
})
177-
})
178-
try{
179-
awaitnewPromise<void>((resolve,reject)=>{
180-
readStream.on("error",(err)=>{
181-
reject(err)
182-
})
183-
readStream.on("close",()=>{
184-
if(cancelled){
185-
returnreject()
186-
}
187-
writeStream.close()
188-
resolve()
209+
returnundefined
210+
}
211+
default:{
212+
vscode.window
213+
.showErrorMessage("Failed to download binary. Please open an issue.","Open an Issue")
214+
.then((value)=>{
215+
if(!value){
216+
return
217+
}
218+
constparams=newURLSearchParams({
219+
title:`Failed to download binary on \`${os}-${arch}\``,
220+
body:`Received status code \`${resp.status}\` when downloading the binary.`,
189221
})
222+
consturi=vscode.Uri.parse(`https://github.com/coder/vscode-coder/issues/new?`+params.toString())
223+
vscode.env.openExternal(uri)
190224
})
191-
returntrue
192-
}catch(ex){
193-
returnfalse
194-
}
195-
},
196-
)
197-
if(!completed){
198-
return
199-
}
200-
if(resp.status===200){
201-
this.output.appendLine(`Downloaded binary:${binPath}`)
202-
awaitfs.rename(tempFile,binPath)
203-
returnbinPath
204-
}
205-
awaitfs.rm(tempFile)
206-
207-
if(resp.status!==304){
208-
vscode.window.showErrorMessage("Failed to fetch the Coder binary: "+resp.statusText)
209-
returnundefined
225+
returnundefined
226+
}
210227
}
211-
212-
this.output.appendLine(`Using cached binary:${binPath}`)
213-
returnbinPath
214228
}
215229

216230
// getBinaryCachePath returns the path where binaries are cached.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp