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

Commit36da848

Browse files
committed
refactor: switch on status to avoid piping
1 parent2c65b5d commit36da848

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
@@ -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-
constparams=newURLSearchParams({
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-
consturi=vscode.Uri.parse(`https://github.com/coder/vscode-coder/issues/new?`+params.toString())
128-
vscode.env.openExternal(uri)
129-
})
130-
return
131-
}
132113

133-
constcontentLength=Number.parseInt(resp.headers["content-length"])
114+
switch(resp.status){
115+
case200:{
116+
constcontentLength=Number.parseInt(resp.headers["content-length"])
117+
118+
// Ensure the binary directory exists!
119+
awaitfs.mkdir(path.dirname(binPath),{recursive:true})
120+
consttempFile=binPath+".temp-"+Math.random().toString(36).substring(8)
121+
122+
constcompleted=awaitvscode.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+
constreadStream=resp.dataasIncomingMessage
130+
letcancelled=false
131+
token.onCancellationRequested(()=>{
132+
controller.abort()
133+
readStream.destroy()
134+
cancelled=true
135+
})
134136

135-
// Ensure the binary directory exists!
136-
awaitfs.mkdir(path.dirname(binPath),{recursive:true})
137-
consttempFile=binPath+".temp-"+Math.random().toString(36).substring(8)
137+
letcontentLengthPretty=""
138+
// Reverse proxies might not always send a content length!
139+
if(!Number.isNaN(contentLength)){
140+
contentLengthPretty=" / "+prettyBytes(contentLength)
141+
}
138142

139-
constcompleted=awaitvscode.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-
constreadStream=resp.dataasIncomingMessage
147-
letcancelled=false
148-
token.onCancellationRequested(()=>{
149-
controller.abort()
150-
readStream.destroy()
151-
cancelled=true
152-
})
153-
154-
letcontentLengthPretty=""
155-
// Reverse proxies might not always send a content length!
156-
if(!Number.isNaN(contentLength)){
157-
contentLengthPretty=" / "+prettyBytes(contentLength)
143+
constwriteStream=createWriteStream(tempFile,{
144+
autoClose:true,
145+
mode:0o755,
146+
})
147+
letwritten=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+
awaitnewPromise<void>((resolve,reject)=>{
159+
readStream.on("error",(err)=>{
160+
reject(err)
161+
})
162+
readStream.on("close",()=>{
163+
if(cancelled){
164+
returnreject()
165+
}
166+
writeStream.close()
167+
resolve()
168+
})
169+
})
170+
returntrue
171+
}catch(ex){
172+
returnfalse
173+
}
174+
},
175+
)
176+
if(!completed){
177+
return
158178
}
159-
160-
constwriteStream=createWriteStream(tempFile,{
161-
autoClose:true,
162-
mode:0o755,
163-
})
164-
letwritten=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+
awaitfs.rename(tempFile,binPath)
181+
awaitfs.rm(tempFile)
182+
returnbinPath
183+
}
184+
case304:{
185+
this.output.appendLine(`Using cached binary:${binPath}`)
186+
returnbinPath
187+
}
188+
case404:{
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+
constparams=newURLSearchParams({
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+
consturi=vscode.Uri.parse(`https://github.com/coder/vscode-coder/issues/new?`+params.toString())
203+
vscode.env.openExternal(uri)
172204
})
173-
})
174-
try{
175-
awaitnewPromise<void>((resolve,reject)=>{
176-
readStream.on("error",(err)=>{
177-
reject(err)
178-
})
179-
readStream.on("close",()=>{
180-
if(cancelled){
181-
returnreject()
182-
}
183-
writeStream.close()
184-
resolve()
205+
returnundefined
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+
constparams=newURLSearchParams({
215+
title:`Failed to download binary on \`${os}-${arch}\``,
216+
body:`Received status code \`${resp.status}\` when downloading the binary.`,
185217
})
218+
consturi=vscode.Uri.parse(`https://github.com/coder/vscode-coder/issues/new?`+params.toString())
219+
vscode.env.openExternal(uri)
186220
})
187-
returntrue
188-
}catch(ex){
189-
returnfalse
190-
}
191-
},
192-
)
193-
if(!completed){
194-
return
195-
}
196-
if(resp.status===200){
197-
this.output.appendLine(`Downloaded binary:${binPath}`)
198-
awaitfs.rename(tempFile,binPath)
199-
returnbinPath
200-
}
201-
awaitfs.rm(tempFile)
202-
203-
if(resp.status!==304){
204-
vscode.window.showErrorMessage("Failed to fetch the Coder binary: "+resp.statusText)
205-
returnundefined
221+
returnundefined
222+
}
206223
}
207-
208-
this.output.appendLine(`Using cached binary:${binPath}`)
209-
returnbinPath
210224
}
211225

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

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp