-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.ts
104 lines (90 loc) · 2.78 KB
/
install.ts
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
#!/usr/bin/env node
import * as path from 'path';
import * as os from 'os';
import tar from "tar";
import fetch from "node-fetch";
import * as unzipper from 'unzipper';
import PJ from "./package.json";
interface BinaryConfig {
arch: 'arm64' | 'x64';
platform: 'darwin' | 'linux' | 'win32';
baseUrl: string;
}
class BinaryDownloader {
private config: BinaryConfig;
constructor(config: BinaryConfig) {
this.config = config
}
private URL(): string {
const { arch, platform, baseUrl } = this.config;
let version = PJ.version.trim();
let archieveType = ""
switch (platform) {
case 'darwin':
case 'linux':
archieveType = '.tar.gz';
break;
case 'win32':
archieveType = '.exe';
break;
}
return `${baseUrl}/${version}/qstash-server_${version}_${platform}_${arch}${archieveType}`;
}
public async download(): Promise<NodeJS.ReadableStream> {
return new Promise((resolve, reject) => {
const url = this.URL();
fetch(url).then((res) => {
if (res.status !== 200) {
throw new Error(`Error downloading binary; invalid response status code: ${res.status}`);
}
if (!res.body) {
return reject(new Error("No body to pipe"));
}
resolve(res.body);
}).catch(reject);
});
}
public async extract(stream: NodeJS.ReadableStream): Promise<void> {
return new Promise((resolve, reject) => {
const bin = path.resolve("./bin");
switch (this.config.platform) {
case "darwin":
case "linux":
const untar = tar.extract({ cwd: bin });
stream
.pipe(untar)
.on('close', () => resolve())
.on('error', reject)
break;
case "win32":
stream
.pipe(unzipper.Extract({ path: bin }))
.on('close', () => resolve())
.on('error', reject);
}
})
}
}
function getSysInfo(): { arch: BinaryConfig['arch'], platform: BinaryConfig['platform'] } {
const arch = os.arch() === 'arm64' ? 'arm64' : 'x64';
const platform = os.platform() as BinaryConfig['platform'];
if (!['darwin', 'linux', 'win32'].includes(platform)) {
throw new Error(`Unsupported platform: ${platform}`);
}
return { arch, platform };
}
(async () => {
try {
const { arch, platform } = getSysInfo();
const downloader = new BinaryDownloader({
arch,
platform,
baseUrl: 'https://artifacts.upstash.com/qstash/versions'
});
const stream = await downloader.download();
await downloader.extract(stream);
} catch (error) {
console.error(error);
process.exit(1);
}
})();