diff options
Diffstat (limited to 'editors')
-rw-r--r-- | editors/code/src/installation/download_file.ts | 10 | ||||
-rw-r--r-- | editors/code/src/installation/interfaces.ts | 33 |
2 files changed, 39 insertions, 4 deletions
diff --git a/editors/code/src/installation/download_file.ts b/editors/code/src/installation/download_file.ts index 7b537e114..0cc5fc0cb 100644 --- a/editors/code/src/installation/download_file.ts +++ b/editors/code/src/installation/download_file.ts | |||
@@ -1,17 +1,25 @@ | |||
1 | import fetch from "node-fetch"; | 1 | import fetch from "node-fetch"; |
2 | import { throttle } from "throttle-debounce"; | 2 | import { throttle } from "throttle-debounce"; |
3 | import * as fs from "fs"; | 3 | import * as fs from "fs"; |
4 | import { strict as assert } from "assert"; | ||
4 | 5 | ||
6 | /** | ||
7 | * Downloads file from `url` and stores it at `destFilePath`. | ||
8 | * `onProgress` callback is periodically called to track the progress of downloading, | ||
9 | * it gets the already read and total amount of bytes to read as its parameters. | ||
10 | */ | ||
5 | export async function downloadFile( | 11 | export async function downloadFile( |
6 | url: string, | 12 | url: string, |
7 | destFilePath: fs.PathLike, | 13 | destFilePath: fs.PathLike, |
8 | onProgress: (readBytes: number, totalBytes: number) => void | 14 | onProgress: (readBytes: number, totalBytes: number) => void |
9 | ): Promise<void> { | 15 | ): Promise<void> { |
10 | onProgress = throttle(1000, /* noTrailing: */ true, onProgress); | 16 | onProgress = throttle(500, /* noTrailing: */ true, onProgress); |
11 | 17 | ||
12 | const response = await fetch(url); | 18 | const response = await fetch(url); |
13 | 19 | ||
14 | const totalBytes = Number(response.headers.get('content-length')); | 20 | const totalBytes = Number(response.headers.get('content-length')); |
21 | assert(!Number.isNaN(totalBytes), "Sanity check of content-length protocol"); | ||
22 | |||
15 | let readBytes = 0; | 23 | let readBytes = 0; |
16 | 24 | ||
17 | return new Promise<void>((resolve, reject) => response.body | 25 | return new Promise<void>((resolve, reject) => response.body |
diff --git a/editors/code/src/installation/interfaces.ts b/editors/code/src/installation/interfaces.ts index f54e24e26..03eac5b79 100644 --- a/editors/code/src/installation/interfaces.ts +++ b/editors/code/src/installation/interfaces.ts | |||
@@ -3,24 +3,51 @@ export interface GithubRepo { | |||
3 | owner: string; | 3 | owner: string; |
4 | } | 4 | } |
5 | 5 | ||
6 | /** | ||
7 | * Metadata about particular artifact retrieved from GitHub releases. | ||
8 | */ | ||
6 | export interface ArtifactMetadata { | 9 | export interface ArtifactMetadata { |
7 | releaseName: string; | 10 | releaseName: string; |
8 | downloadUrl: string; | 11 | downloadUrl: string; |
9 | } | 12 | } |
10 | 13 | ||
11 | 14 | /** | |
15 | * Type tag for `BinarySource` discriminated union. | ||
16 | */ | ||
12 | export enum BinarySourceType { ExplicitPath, GithubBinary } | 17 | export enum BinarySourceType { ExplicitPath, GithubBinary } |
13 | 18 | ||
14 | export type BinarySource = EplicitPathSource | GithubBinarySource; | 19 | /** |
20 | * Represents the source of a binary artifact which is either specified by the user | ||
21 | * explicitly, or bundled by this extension from GitHub releases. | ||
22 | */ | ||
23 | export type BinarySource = ExplicitPathSource | GithubBinarySource; | ||
24 | |||
15 | 25 | ||
16 | export interface EplicitPathSource { | 26 | export interface ExplicitPathSource { |
17 | type: BinarySourceType.ExplicitPath; | 27 | type: BinarySourceType.ExplicitPath; |
28 | |||
29 | /** | ||
30 | * Filesystem path to the binary specified by the user explicitly. | ||
31 | */ | ||
18 | path: string; | 32 | path: string; |
19 | } | 33 | } |
20 | 34 | ||
21 | export interface GithubBinarySource { | 35 | export interface GithubBinarySource { |
22 | type: BinarySourceType.GithubBinary; | 36 | type: BinarySourceType.GithubBinary; |
37 | |||
38 | /** | ||
39 | * Repository where the binary is stored. | ||
40 | */ | ||
23 | repo: GithubRepo; | 41 | repo: GithubRepo; |
42 | |||
43 | /** | ||
44 | * Directory on the filesystem where the bundled binary is stored. | ||
45 | */ | ||
24 | dir: string; | 46 | dir: string; |
47 | |||
48 | /** | ||
49 | * Name of the binary file. It is stored under the same name on GitHub releases | ||
50 | * and in local `.dir`. | ||
51 | */ | ||
25 | file: string; | 52 | file: string; |
26 | } | 53 | } |