-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathversion-handler.ts
More file actions
150 lines (129 loc) · 5.32 KB
/
Copy pathversion-handler.ts
File metadata and controls
150 lines (129 loc) · 5.32 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import axios, { AxiosInstance } from "axios";
import path from "path";
import fs from "fs";
import os from "os";
import { DEFAULT_HTTP_TIMEOUT } from "./constants";
import { DevnetError, GithubError } from "./types";
import decompress from "decompress";
// eslint-disable-next-line @typescript-eslint/no-var-requires
const decompressTargz = require("decompress-targz");
export class VersionHandler {
static httpProvider: AxiosInstance = axios.create({
timeout: DEFAULT_HTTP_TIMEOUT,
});
static localStoragePath: string = path.join(os.tmpdir(), "devnet-versions");
private static getVersionDir(version: string): string {
return path.join(this.localStoragePath, version);
}
private static getExecutablePath(versionDir: string): string {
return path.join(versionDir, "starknet-devnet");
}
/**
* Ensure that the command corresponding to the provided `version` exists.
* @param version semver string with a prepended "v";
* should be available in https://github.com/starknet-io/starknet-devnet/releases
* @returns the path to the executable corresponding to the version
*/
static async getExecutable(version: string): Promise<string> {
const versionDir = this.getVersionDir(version);
const executable = this.getExecutablePath(versionDir);
if (fs.existsSync(executable)) {
return executable;
}
const executableUrl = await this.getArchivedExecutableUrl(version);
const archivePath = await this.fetchArchivedExecutable(executableUrl, versionDir);
await this.extract(archivePath, versionDir);
return executable;
}
private static getCompatibleArch(): string {
switch (process.arch) {
case "arm64":
return "aarch64";
case "x64":
return "x86_64";
default:
throw new DevnetError(`Incompatible architecture: ${process.platform}`);
}
}
private static getCompatiblePlatform(): string {
switch (process.platform) {
case "linux":
return "unknown-linux-gnu";
case "darwin":
return "apple-darwin";
default:
throw new DevnetError(`Incompatible platform: ${process.platform}`);
}
}
private static async getArchivedExecutableUrl(version: string): Promise<string> {
const releasesUrl = "https://api.github.com/repos/starknet-io/starknet-devnet/releases";
const releasesResp = await this.httpProvider.get(releasesUrl);
if (releasesResp.status !== axios.HttpStatusCode.Ok) {
throw new GithubError(releasesResp.statusText);
}
if (!Array.isArray(releasesResp.data)) {
throw new GithubError(`Invalid response: ${JSON.stringify(releasesResp.data)}`);
}
let versionPresent = false;
for (const release of releasesResp.data) {
if (release.name === version) {
versionPresent = true;
break;
}
}
if (!versionPresent) {
throw new GithubError(
`Version not found. If specifying an exact version, make sure you prepended the 'v' and that the version really exists in ${releasesUrl}.`,
);
}
const arch = this.getCompatibleArch();
const platform = this.getCompatiblePlatform();
return `https://github.com/starknet-io/starknet-devnet/releases/download/${version}/starknet-devnet-${arch}-${platform}.tar.gz`;
}
/**
* @param url the url of the archived executable
* @param versionDir the path to the directory where the archive will be written and extracted
* @returns the path where the archive was stored
*/
private static async fetchArchivedExecutable(url: string, versionDir: string): Promise<string> {
const resp = await this.httpProvider.get(url, {
responseType: "stream",
timeout: 120_000,
});
if (resp.status === axios.HttpStatusCode.NotFound) {
throw new GithubError(`Not found: ${url}`);
} else if (resp.status !== axios.HttpStatusCode.Ok) {
throw new GithubError(resp.statusText);
}
if (!fs.existsSync(versionDir)) {
fs.mkdirSync(versionDir, { recursive: true });
}
const archivedExecutablePath = path.join(versionDir, "archive.tar.gz");
const writer = fs.createWriteStream(archivedExecutablePath);
return new Promise((resolve, reject) => {
resp.data.pipe(writer);
let error: Error;
writer.on("error", (e) => {
error = e;
writer.close();
reject(error);
});
writer.on("close", () => {
if (!error) {
resolve(archivedExecutablePath);
}
// no need to call `reject` here, as it will have been called in the
// "error" stream;
});
});
}
/**
* Extract the content of the archive.
* @param archivePath the local path of the archive
*/
private static async extract(archivePath: string, targetDir: string): Promise<void> {
await decompress(archivePath, targetDir, {
plugins: [decompressTargz()],
});
}
}