Skip to content

Commit 369a823

Browse files
committed
refactor: switch on status to avoid piping
1 parent 400ba7a commit 369a823

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-
const params = new URLSearchParams({
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-
const uri = vscode.Uri.parse(`https://github.com/coder/vscode-coder/issues/new?` + params.toString())
132-
vscode.env.openExternal(uri)
133-
})
134-
return
135-
}
136-
137-
const contentLength = Number.parseInt(resp.headers["content-length"])
138117

139-
// Ensure the binary directory exists!
140-
await fs.mkdir(path.dirname(binPath), { recursive: true })
141-
const tempFile = binPath + ".temp-" + Math.random().toString(36).substring(8)
118+
switch (resp.status) {
119+
case 200: {
120+
const contentLength = Number.parseInt(resp.headers["content-length"])
121+
122+
// Ensure the binary directory exists!
123+
await fs.mkdir(path.dirname(binPath), { recursive: true })
124+
const tempFile = binPath + ".temp-" + Math.random().toString(36).substring(8)
125+
126+
const completed = await vscode.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+
const readStream = resp.data as IncomingMessage
134+
let cancelled = false
135+
token.onCancellationRequested(() => {
136+
controller.abort()
137+
readStream.destroy()
138+
cancelled = true
139+
})
142140

143-
const completed = await vscode.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-
const readStream = resp.data as IncomingMessage
151-
let cancelled = false
152-
token.onCancellationRequested(() => {
153-
controller.abort()
154-
readStream.destroy()
155-
cancelled = true
156-
})
141+
let contentLengthPretty = ""
142+
// Reverse proxies might not always send a content length!
143+
if (!Number.isNaN(contentLength)) {
144+
contentLengthPretty = " / " + prettyBytes(contentLength)
145+
}
157146

158-
let contentLengthPretty = ""
159-
// Reverse proxies might not always send a content length!
160-
if (!Number.isNaN(contentLength)) {
161-
contentLengthPretty = " / " + prettyBytes(contentLength)
147+
const writeStream = createWriteStream(tempFile, {
148+
autoClose: true,
149+
mode: 0o755,
150+
})
151+
let written = 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+
await new Promise<void>((resolve, reject) => {
163+
readStream.on("error", (err) => {
164+
reject(err)
165+
})
166+
readStream.on("close", () => {
167+
if (cancelled) {
168+
return reject()
169+
}
170+
writeStream.close()
171+
resolve()
172+
})
173+
})
174+
return true
175+
} catch (ex) {
176+
return false
177+
}
178+
},
179+
)
180+
if (!completed) {
181+
return
162182
}
163-
164-
const writeStream = createWriteStream(tempFile, {
165-
autoClose: true,
166-
mode: 0o755,
167-
})
168-
let written = 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+
await fs.rename(tempFile, binPath)
185+
await fs.rm(tempFile)
186+
return binPath
187+
}
188+
case 304: {
189+
this.output.appendLine(`Using cached binary: ${binPath}`)
190+
return binPath
191+
}
192+
case 404: {
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+
const params = new URLSearchParams({
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+
const uri = vscode.Uri.parse(`https://github.com/coder/vscode-coder/issues/new?` + params.toString())
207+
vscode.env.openExternal(uri)
176208
})
177-
})
178-
try {
179-
await new Promise<void>((resolve, reject) => {
180-
readStream.on("error", (err) => {
181-
reject(err)
182-
})
183-
readStream.on("close", () => {
184-
if (cancelled) {
185-
return reject()
186-
}
187-
writeStream.close()
188-
resolve()
209+
return undefined
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+
const params = new URLSearchParams({
219+
title: `Failed to download binary on \`${os}-${arch}\``,
220+
body: `Received status code \`${resp.status}\` when downloading the binary.`,
189221
})
222+
const uri = vscode.Uri.parse(`https://github.com/coder/vscode-coder/issues/new?` + params.toString())
223+
vscode.env.openExternal(uri)
190224
})
191-
return true
192-
} catch (ex) {
193-
return false
194-
}
195-
},
196-
)
197-
if (!completed) {
198-
return
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
225+
return undefined
226+
}
210227
}
211-
212-
this.output.appendLine(`Using cached binary: ${binPath}`)
213-
return binPath
214228
}
215229

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

0 commit comments

Comments
 (0)