diff options
-rw-r--r-- | editors/code/package-lock.json | 25 | ||||
-rw-r--r-- | editors/code/package.json | 6 | ||||
-rw-r--r-- | editors/code/src/github/download_file.ts | 26 | ||||
-rw-r--r-- | editors/code/src/github/fetch_latest_artifact_metadata.ts | 55 |
4 files changed, 111 insertions, 1 deletions
diff --git a/editors/code/package-lock.json b/editors/code/package-lock.json index 353af06bf..5c056463e 100644 --- a/editors/code/package-lock.json +++ b/editors/code/package-lock.json | |||
@@ -82,6 +82,15 @@ | |||
82 | "integrity": "sha512-nf1LMGZvgFX186geVZR1xMZKKblJiRfiASTHw85zED2kI1yDKHDwTKMdkaCbTlXoRKlGKaDfYywt+V0As30q3w==", | 82 | "integrity": "sha512-nf1LMGZvgFX186geVZR1xMZKKblJiRfiASTHw85zED2kI1yDKHDwTKMdkaCbTlXoRKlGKaDfYywt+V0As30q3w==", |
83 | "dev": true | 83 | "dev": true |
84 | }, | 84 | }, |
85 | "@types/node-fetch": { | ||
86 | "version": "2.5.4", | ||
87 | "resolved": "https://registry.npmjs.org/@types/node-fetch/-/node-fetch-2.5.4.tgz", | ||
88 | "integrity": "sha512-Oz6id++2qAOFuOlE1j0ouk1dzl3mmI1+qINPNBhi9nt/gVOz0G+13Ao6qjhdF0Ys+eOkhu6JnFmt38bR3H0POQ==", | ||
89 | "dev": true, | ||
90 | "requires": { | ||
91 | "@types/node": "*" | ||
92 | } | ||
93 | }, | ||
85 | "@types/resolve": { | 94 | "@types/resolve": { |
86 | "version": "0.0.8", | 95 | "version": "0.0.8", |
87 | "resolved": "https://registry.npmjs.org/@types/resolve/-/resolve-0.0.8.tgz", | 96 | "resolved": "https://registry.npmjs.org/@types/resolve/-/resolve-0.0.8.tgz", |
@@ -91,6 +100,12 @@ | |||
91 | "@types/node": "*" | 100 | "@types/node": "*" |
92 | } | 101 | } |
93 | }, | 102 | }, |
103 | "@types/throttle-debounce": { | ||
104 | "version": "2.1.0", | ||
105 | "resolved": "https://registry.npmjs.org/@types/throttle-debounce/-/throttle-debounce-2.1.0.tgz", | ||
106 | "integrity": "sha512-5eQEtSCoESnh2FsiLTxE121IiE60hnMqcb435fShf4bpLRjEu1Eoekht23y6zXS9Ts3l+Szu3TARnTsA0GkOkQ==", | ||
107 | "dev": true | ||
108 | }, | ||
94 | "@types/vscode": { | 109 | "@types/vscode": { |
95 | "version": "1.41.0", | 110 | "version": "1.41.0", |
96 | "resolved": "https://registry.npmjs.org/@types/vscode/-/vscode-1.41.0.tgz", | 111 | "resolved": "https://registry.npmjs.org/@types/vscode/-/vscode-1.41.0.tgz", |
@@ -536,6 +551,11 @@ | |||
536 | "integrity": "sha512-nnbWWOkoWyUsTjKrhgD0dcz22mdkSnpYqbEjIm2nhwhuxlSkpywJmBo8h0ZqJdkp73mb90SssHkN4rsRaBAfAA==", | 551 | "integrity": "sha512-nnbWWOkoWyUsTjKrhgD0dcz22mdkSnpYqbEjIm2nhwhuxlSkpywJmBo8h0ZqJdkp73mb90SssHkN4rsRaBAfAA==", |
537 | "dev": true | 552 | "dev": true |
538 | }, | 553 | }, |
554 | "node-fetch": { | ||
555 | "version": "2.6.0", | ||
556 | "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.0.tgz", | ||
557 | "integrity": "sha512-8dG4H5ujfvFiqDmVu9fQ5bOHUC15JMjMY/Zumv26oOvvVJjM67KF8koCWIabKQ1GJIa9r2mMZscBq/TbdOcmNA==" | ||
558 | }, | ||
539 | "nth-check": { | 559 | "nth-check": { |
540 | "version": "1.0.2", | 560 | "version": "1.0.2", |
541 | "resolved": "https://registry.npmjs.org/nth-check/-/nth-check-1.0.2.tgz", | 561 | "resolved": "https://registry.npmjs.org/nth-check/-/nth-check-1.0.2.tgz", |
@@ -719,6 +739,11 @@ | |||
719 | "has-flag": "^3.0.0" | 739 | "has-flag": "^3.0.0" |
720 | } | 740 | } |
721 | }, | 741 | }, |
742 | "throttle-debounce": { | ||
743 | "version": "2.1.0", | ||
744 | "resolved": "https://registry.npmjs.org/throttle-debounce/-/throttle-debounce-2.1.0.tgz", | ||
745 | "integrity": "sha512-AOvyNahXQuU7NN+VVvOOX+uW6FPaWdAOdRP5HfwYxAfCzXTFKRMoIMk+n+po318+ktcChx+F1Dd91G3YHeMKyg==" | ||
746 | }, | ||
722 | "tmp": { | 747 | "tmp": { |
723 | "version": "0.0.29", | 748 | "version": "0.0.29", |
724 | "resolved": "https://registry.npmjs.org/tmp/-/tmp-0.0.29.tgz", | 749 | "resolved": "https://registry.npmjs.org/tmp/-/tmp-0.0.29.tgz", |
diff --git a/editors/code/package.json b/editors/code/package.json index 11d37053e..8e23718cd 100644 --- a/editors/code/package.json +++ b/editors/code/package.json | |||
@@ -25,18 +25,22 @@ | |||
25 | }, | 25 | }, |
26 | "dependencies": { | 26 | "dependencies": { |
27 | "jsonc-parser": "^2.1.0", | 27 | "jsonc-parser": "^2.1.0", |
28 | "node-fetch": "^2.6.0", | ||
29 | "throttle-debounce": "^2.1.0", | ||
28 | "vscode-languageclient": "^6.1.0" | 30 | "vscode-languageclient": "^6.1.0" |
29 | }, | 31 | }, |
30 | "devDependencies": { | 32 | "devDependencies": { |
31 | "@rollup/plugin-commonjs": "^11.0.2", | 33 | "@rollup/plugin-commonjs": "^11.0.2", |
32 | "@rollup/plugin-node-resolve": "^7.1.1", | 34 | "@rollup/plugin-node-resolve": "^7.1.1", |
33 | "@types/node": "^12.12.25", | 35 | "@types/node": "^12.12.25", |
36 | "@types/node-fetch": "^2.5.4", | ||
37 | "@types/throttle-debounce": "^2.1.0", | ||
34 | "@types/vscode": "^1.41.0", | 38 | "@types/vscode": "^1.41.0", |
35 | "rollup": "^1.31.0", | 39 | "rollup": "^1.31.0", |
36 | "tslib": "^1.10.0", | 40 | "tslib": "^1.10.0", |
37 | "tslint": "^5.20.1", | 41 | "tslint": "^5.20.1", |
38 | "typescript-formatter": "^7.2.2", | ||
39 | "typescript": "^3.7.5", | 42 | "typescript": "^3.7.5", |
43 | "typescript-formatter": "^7.2.2", | ||
40 | "vsce": "^1.71.0" | 44 | "vsce": "^1.71.0" |
41 | }, | 45 | }, |
42 | "activationEvents": [ | 46 | "activationEvents": [ |
diff --git a/editors/code/src/github/download_file.ts b/editors/code/src/github/download_file.ts new file mode 100644 index 000000000..f40750be9 --- /dev/null +++ b/editors/code/src/github/download_file.ts | |||
@@ -0,0 +1,26 @@ | |||
1 | import fetch from "node-fetch"; | ||
2 | import { throttle } from "throttle-debounce"; | ||
3 | import * as fs from "fs"; | ||
4 | |||
5 | export async function downloadFile( | ||
6 | url: string, | ||
7 | destFilePath: fs.PathLike, | ||
8 | onProgress: (readBytes: number, totalBytes: number) => void | ||
9 | ): Promise<void> { | ||
10 | onProgress = throttle(100, /* noTrailing: */ true, onProgress); | ||
11 | |||
12 | const response = await fetch(url); | ||
13 | |||
14 | const totalBytes = Number(response.headers.get('content-length')); | ||
15 | let readBytes = 0; | ||
16 | |||
17 | return new Promise<void>((resolve, reject) => response.body | ||
18 | .on("data", (chunk: Buffer) => { | ||
19 | readBytes += chunk.length; | ||
20 | onProgress(readBytes, totalBytes); | ||
21 | }) | ||
22 | .on("end", resolve) | ||
23 | .on("error", reject) | ||
24 | .pipe(fs.createWriteStream(destFilePath)) | ||
25 | ); | ||
26 | } | ||
diff --git a/editors/code/src/github/fetch_latest_artifact_metadata.ts b/editors/code/src/github/fetch_latest_artifact_metadata.ts new file mode 100644 index 000000000..52641ca67 --- /dev/null +++ b/editors/code/src/github/fetch_latest_artifact_metadata.ts | |||
@@ -0,0 +1,55 @@ | |||
1 | import fetch from "node-fetch"; | ||
2 | |||
3 | const GITHUB_API_ENDPOINT_URL = "https://api.github.com"; | ||
4 | |||
5 | export interface FetchLatestArtifactMetadataOpts { | ||
6 | repoName: string; | ||
7 | repoOwner: string; | ||
8 | artifactFileName: string; | ||
9 | } | ||
10 | |||
11 | export interface ArtifactMetadata { | ||
12 | releaseName: string; | ||
13 | releaseDate: Date; | ||
14 | downloadUrl: string; | ||
15 | } | ||
16 | |||
17 | export async function fetchLatestArtifactMetadata( | ||
18 | opts: FetchLatestArtifactMetadataOpts | ||
19 | ): Promise<ArtifactMetadata | null> { | ||
20 | |||
21 | const repoOwner = encodeURIComponent(opts.repoOwner); | ||
22 | const repoName = encodeURIComponent(opts.repoName); | ||
23 | |||
24 | const apiEndpointPath = `/repos/${repoOwner}/${repoName}/releases/latest`; | ||
25 | const requestUrl = GITHUB_API_ENDPOINT_URL + apiEndpointPath; | ||
26 | |||
27 | // We skip runtime type checks for simplicity (here we cast from `any` to `Release`) | ||
28 | |||
29 | const response: GithubRelease = await fetch(requestUrl, { | ||
30 | headers: { Accept: "application/vnd.github.v3+json" } | ||
31 | }) | ||
32 | .then(res => res.json()); | ||
33 | |||
34 | const artifact = response.assets.find(artifact => artifact.name === opts.artifactFileName); | ||
35 | |||
36 | return !artifact ? null : { | ||
37 | releaseName: response.name, | ||
38 | releaseDate: new Date(response.published_at), | ||
39 | downloadUrl: artifact.browser_download_url | ||
40 | }; | ||
41 | |||
42 | // Noise denotes tremendous amount of data that we are not using here | ||
43 | interface GithubRelease { | ||
44 | name: string; | ||
45 | published_at: Date; | ||
46 | assets: Array<{ | ||
47 | browser_download_url: string; | ||
48 | |||
49 | [noise: string]: unknown; | ||
50 | }>; | ||
51 | |||
52 | [noise: string]: unknown; | ||
53 | } | ||
54 | |||
55 | } | ||