-
Notifications
You must be signed in to change notification settings - Fork 5
/
github.ts
153 lines (128 loc) · 3.74 KB
/
github.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
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
151
152
153
import { Match, SemverValid, SemverGT } from "../deps.ts";
import { json, notFound, noUpdateAvailable } from "../util/response.ts";
import { validatePlatform, AVAILABLE_PLATFORMS } from "../util/platform.ts";
import {
GITHUB_TOKEN,
GITHUB_ACCOUNT,
GITHUB_REPO,
} from "../util/constants.ts";
export default async function (req: Request, match: Match): Promise<Response> {
const { platform, version } = match.params;
// Make sure the platform is valid
if (!platform || !validatePlatform(platform)) {
return notFound();
}
// Make sure our version is semver valid
if (!version || !SemverValid(version)) {
return notFound();
}
// Github api URL for latest version
const reqUrl = new URL(
`https://api.github.com/repos/${GITHUB_ACCOUNT}/${GITHUB_REPO}/releases/latest`
);
// Headers
const headers: HeadersInit = { Accept: "application/vnd.github.preview" };
// Add github token if provided
if (GITHUB_TOKEN && GITHUB_TOKEN.length > 0) {
headers.Authorization = `token ${GITHUB_TOKEN}`;
}
// Get JSON from github
const release = await (
await fetch(reqUrl.toString(), {
method: "GET",
headers,
})
).json();
console.log({ release });
// Sanitize our version
const remoteVersion = sanitizeVersion(release.tag_name.toLowerCase());
// Make sure we found a valid version
if (!remoteVersion || !SemverValid(remoteVersion)) {
return notFound();
}
// Check if the user is running older version or not
const shouldUpdate = SemverGT(remoteVersion, version);
if (!shouldUpdate) {
return noUpdateAvailable();
}
for (const asset of release.assets) {
const { name, browser_download_url } = asset;
const findPlatform = checkPlatform(platform, name);
if (!findPlatform) {
continue;
}
// try to find signature for this asset
const signature = await findAssetSignature(name, release.assets);
return json({
name: release.tag_name,
notes: release.body,
pub_date: release.published_at,
signature,
url: browser_download_url,
});
}
return notFound();
}
function sanitizeVersion(version: string): string {
// if it start with v1.0.0 remove the `v`
if (version.charAt(0) === "v") {
return version.substring(1);
}
return version;
}
function checkPlatform(platform: string, fileName: string) {
const extension = extname(fileName);
// OSX we should have our .app tar.gz
if (
(fileName.includes(".app") ||
fileName.includes("darwin") ||
fileName.includes("osx")) &&
extension === "gz" &&
platform === AVAILABLE_PLATFORMS.MacOS
) {
return "darwin";
}
// Windows 64 bits
if (
(fileName.includes("x64") || fileName.includes("win64")) &&
extension === "zip" &&
platform === AVAILABLE_PLATFORMS.Win64
) {
return "win64";
}
// Windows 32 bits
if (
(fileName.includes("x32") || fileName.includes("win32")) &&
extension === "zip" &&
platform === AVAILABLE_PLATFORMS.Win32
) {
return "win32";
}
// Linux app image
if (
fileName.includes("AppImage") &&
extension === "gz" &&
platform === AVAILABLE_PLATFORMS.Linux
) {
return "linux";
}
}
function extname(filename: string) {
return filename.split(".").pop() || "";
}
async function findAssetSignature(fileName: string, assets: any[]) {
// check in our assets if we have a file: `fileName.sig`
// by example fileName can be: App-1.0.0.zip
const foundSignature = assets.find(
(asset) => asset.name.toLowerCase() === `${fileName.toLowerCase()}.sig`
);
if (!foundSignature) {
return null;
}
const response = await fetch(foundSignature.browser_download_url);
if (response.status !== 200) {
return null;
}
const signature = await response.text();
return signature;
}