electron-builder: Differential download must fail after 10 seconds

When useMultipleRangeRequest is true, differential download must fail after 10 seconds.

DifferentialDownloader.ts:

private async doDownloadFile(tasks: Array<Operation>, fdList: Array<OpenedFile>): Promise<any> {
    ...

    await new Promise((resolve, reject) => {
      let w: any
      if (this.options.isUseMultipleRangeRequest) {
        w = executeTasksUsingMultipleRangeRequests(this, tasks, firstStream, oldFileFd, reject)
        w(0)
        return
      }

      ...
   })
  }

multipleRangeDownloader.ts:

export function executeTasksUsingMultipleRangeRequests(
  differentialDownloader: DifferentialDownloader,
  tasks: Array<Operation>,
  out: Writable,
  oldFileFd: number,
  reject: (error: Error) => void
): (taskOffset: number) => void {
  const w = (taskOffset: number): void => {
     ...

    const nextOffset = taskOffset + 1000
    doExecuteTasks(
      differentialDownloader,
      {
        tasks,
        start: taskOffset,
        end: Math.min(tasks.length, nextOffset),
        oldFileFd,
      },
      out,
      () => w(nextOffset),
      reject
    )
  }
  return w
}

function doExecuteTasks(differentialDownloader: DifferentialDownloader, options: PartListDataTask, out: Writable, resolve: () => void, reject: (error: Error) => void): void {
  ...

  const requestOptions = differentialDownloader.createRequestOptions()
  requestOptions.headers!.Range = ranges.substring(0, ranges.length - 2)
  const request = differentialDownloader.httpExecutor.createRequest(requestOptions, response => {
    if (!checkIsRangesSupported(response, reject)) {
      return
    }

    const contentType = safeGetHeader(response, "content-type")
    const m = /^multipart\/.+?(?:; boundary=(?:(?:"(.+)")|(?:([^\s]+))))$/i.exec(contentType)
    if (m == null) {
      reject(new Error(`Content-Type "multipart/byteranges" is expected, but got "${contentType}"`))
      return
    }

    const dicer = new DataSplitter(out, options, partIndexToTaskIndex, m[1] || m[2], partIndexToLength, resolve)
    dicer.on("error", reject)
    response.pipe(dicer)

    response.on("end", () => {
      setTimeout(() => {
        request.abort()
        reject(new Error("Response ends without calling any handlers"))
      }, 10000) <-- The timer isn't canceled after data processing is completed.
    })
  })
  differentialDownloader.httpExecutor.addErrorAndTimeoutHandlers(request, reject)
  request.end()
}

About this issue

Commits related to this issue

Most upvoted comments

Okay, here’s how you can configure Nginx to support multiple HTTP Range requests:

Nginx supports single HTTP Range requests (e.g., Range: bytes=0-1023) by default. If you want Nginx to handle requests containing multiple ranges (e.g., Range: bytes=0-499, 1000-1499) and respond with a multipart/byteranges content type, you need to use the max_ranges directive.

Add the max_ranges directive to the http, server, or location block where you want to enable this feature. Set its value to the maximum number of ranges you want to allow. Setting it to 0 disables multipart/byteranges responses entirely.

Here’s an example configuration within a location block, allowing up to 1000 ranges:

server {
    listen 80;
    server_name your_domain.com; # Replace with your domain
    root /var/www/html;         # Replace with your document root

    location / {
        # ... other location directives ...

        # Set the maximum number of allowed ranges
        # 0: Disables multipart/byteranges
        # 1 (or default): Allows only a single range
        # >1: Allows multiple ranges, up to this number
        max_ranges 1000; # Allow up to 1000 ranges

        # Ensure Nginx can serve the files directly
        try_files $uri $uri/ =404;
    }

    # ... other server directives ...
}

Explanation:

  1. max_ranges 10;: This line tells Nginx to process up to 10 byte ranges specified in the Range request header. If a request contains more than 10 ranges, Nginx might ignore the Range header or return an error.
  2. Context: You can place max_ranges in the http block (global effect), server block (affects the entire virtual host), or location block (affects requests matching the location). Placing it in the specific location block serving large files is common.
  3. Prerequisite: Nginx needs to be serving the static files directly (e.g., using root or alias). For requests handled via proxy_pass, range processing is typically the responsibility of the backend server unless Nginx is configured with caching and has the full file cached.

After modifying the configuration, remember to reload or restart the Nginx service for the changes to take effect:

sudo nginx -t          # Test configuration syntax
sudo systemctl reload nginx # Reload configuration (recommended)
# or
sudo systemctl restart nginx # Restart Nginx service