Skip to content

Commit dab3aeb

Browse files
author
Noah Andrews
authored
fix: handle differential downloads when the blockmap HTTP response is compressed (#7544)
Don't use the content-length HTTP header to check the size of the data, as the content-length will differ from the actual size of the data if the HTTP response is compressed.
1 parent 7c05c36 commit dab3aeb

File tree

2 files changed

+15
-32
lines changed

2 files changed

+15
-32
lines changed

.changeset/little-spies-double.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"builder-util-runtime": patch
3+
"electron-updater": patch
4+
---
5+
6+
Fix differential downloads when the server compresses the blockmap file HTTP response

packages/builder-util-runtime/src/httpExecutor.ts

+9-32
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ Please double check that your authentication token is correct. Due to security r
217217

218218
async downloadToBuffer(url: URL, options: DownloadOptions): Promise<Buffer> {
219219
return await options.cancellationToken.createPromise<Buffer>((resolve, reject, onCancel) => {
220-
let result: Buffer | null = null
220+
const responseChunks: Buffer[] = []
221221
const requestOptions = {
222222
headers: options.headers || undefined,
223223
// because PrivateGitHubProvider requires HttpExecutor.prepareRedirectUrlOptions logic, so, we need to redirect manually
@@ -233,46 +233,23 @@ Please double check that your authentication token is correct. Due to security r
233233
onCancel,
234234
callback: error => {
235235
if (error == null) {
236-
resolve(result!)
236+
resolve(Buffer.concat(responseChunks))
237237
} else {
238238
reject(error)
239239
}
240240
},
241241
responseHandler: (response, callback) => {
242-
const contentLength = safeGetHeader(response, "content-length")
243-
let position = -1
244-
if (contentLength != null) {
245-
const size = parseInt(contentLength, 10)
246-
if (size > 0) {
247-
if (size > 524288000) {
248-
callback(new Error("Maximum allowed size is 500 MB"))
249-
return
250-
}
251-
252-
result = Buffer.alloc(size)
253-
position = 0
254-
}
255-
}
242+
let receivedLength = 0
256243
response.on("data", (chunk: Buffer) => {
257-
if (position !== -1) {
258-
chunk.copy(result!, position)
259-
position += chunk.length
260-
} else if (result == null) {
261-
result = chunk
262-
} else {
263-
if (result.length > 524288000) {
264-
callback(new Error("Maximum allowed size is 500 MB"))
265-
return
266-
}
267-
result = Buffer.concat([result, chunk])
244+
receivedLength += chunk.length
245+
if (receivedLength > 524288000) {
246+
callback(new Error("Maximum allowed size is 500 MB"))
247+
return
268248
}
249+
responseChunks.push(chunk)
269250
})
270251
response.on("end", () => {
271-
if (result != null && position !== -1 && position !== result.length) {
272-
callback(new Error(`Received data length ${position} is not equal to expected ${result.length}`))
273-
} else {
274-
callback(null)
275-
}
252+
callback(null)
276253
})
277254
},
278255
},

0 commit comments

Comments
 (0)