aboutsummaryrefslogtreecommitdiffstats
path: root/qt-qml/src/downloader.ts
blob: ba8673baa3bcc4b7fc1311b00533753bbc7ee774 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only

import * as vscode from 'vscode';
import * as fs from 'fs';
import * as http from 'http';
import * as https from 'https';

export async function download(
  url: string,
  destPath: string,
  token?: vscode.CancellationToken,
  reportCallback?: (progress: number, max: number) => void
) {
  let downloadUrl = url;
  const MaxRedirects = 10;
  const controller = new AbortController();
  token?.onCancellationRequested(() => {
    controller.abort();
  });

  for (let i = 0; i < MaxRedirects; ++i) {
    if (!downloadUrl) {
      throw Error('Invalid download URL');
    }

    const res = await getHttps(downloadUrl, controller);
    if (!res.statusCode) {
      throw Error(`Invalid status code ${res.statusCode}`);
    }

    if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) {
      downloadUrl = res.headers.location;
      continue;
    }

    return downloadOctetStream(res, destPath, token, reportCallback);
  }
}

async function downloadOctetStream(
  res: http.IncomingMessage,
  destPath: string,
  token?: vscode.CancellationToken,
  reportCallback?: (progress: number, max: number) => void
) {
  return new Promise<void>((resolve, reject) => {
    if (!res.statusCode) {
      reject(new Error('No response'));
      return;
    }

    token?.onCancellationRequested(() => {
      reject(new Error('User canceled'));
    });

    if (res.statusCode < 200 || res.statusCode >= 300) {
      reject(new Error(`Unexpected status, ${res.statusCode}`));
      return;
    }

    if (res.headers['content-type'] !== 'application/octet-stream') {
      reject(new Error('HTTP response does not contain an octet stream'));
      return;
    }

    const fileStream = fs.createWriteStream(destPath, { mode: 0o600 });
    const pipeStream = res.pipe(fileStream);
    pipeStream.on('finish', resolve);
    pipeStream.on('error', reject);
    res.on('error', reject);

    if (reportCallback) {
      const length = res.headers['content-length'];
      const maxBytes = length ? Number.parseInt(length) : 100;
      let downloadedBytes = 0;

      res.on('data', (chunk) => {
        downloadedBytes += (chunk as Buffer).length;
        reportCallback(downloadedBytes, maxBytes);
      });
    }
  });
}

async function getHttps(url: string, controller: AbortController) {
  return new Promise<http.IncomingMessage>((resolve, reject) => {
    const request = https.get(url, { signal: controller.signal }, resolve);
    request.on('error', reject);
  });
}