-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.ts
212 lines (192 loc) · 5.16 KB
/
utils.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import { Context, decoder, encode, ky } from './deps.ts';
export const DEV = Deno.env.get('DEV') === 'true';
if (DEV === true) {
console.log('DEV mode enabled');
}
type Base64 = string;
interface ProfileData {
username: string;
name: string;
image: Base64;
scale: number;
}
interface GitHubAPI {
login: string;
id: number;
node_id: string;
avatar_url: string;
gravatar_id: string;
url: string;
html_url: string;
followers_url: string;
following_url: string;
gists_url: string;
starred_url: string;
subscriptions_url: string;
organizations_url: string;
repos_url: string;
events_url: string;
received_events_url: string;
type: string;
site_admin: boolean;
name: string | null;
company?: string | null;
blog: string;
location: string;
email?: string | null;
hireable?: string | boolean | null;
bio: string;
twitter_username?: string | null;
public_repos: number;
public_gists: number;
followers: number;
following: number;
created_at: Date;
updated_at: Date;
}
/**
* Replaces the given text {{token}} with the given value.
*/
export class Replacer {
#text;
constructor(text: string) {
this.#text = text;
}
replace(search: string, replace: string) {
this.#text = this.#text.replaceAll(`{{${search}}}`, replace);
}
get result() {
return this.#text;
}
}
function getDEVGitHubToken() {
try {
const data = Deno.readFileSync('.github_token');
return decoder.decode(data);
} catch {
return;
}
}
const GITHUB_TOKEN: string = Deno.env.get('GITHUB_TOKEN') ||
getDEVGitHubToken() || '';
/**
* Get name of user from GitHub API, returns object with 'err' if fails (and means an user does not exist)
*/
export async function getUserName(
username: string,
): Promise<{ name: string; err: boolean }> {
try {
const res: GitHubAPI = await ky.get(
`https://api.github.com/users/${username}`,
{
headers: {
Authorization: `token ${GITHUB_TOKEN}`,
},
},
).json();
return {
err: false,
name: res.name || '',
};
} catch {
return {
err: true,
name: '',
};
}
}
const template = decoder.decode(await Deno.readFile('./assets/template.svg'));
/**
* Returns a valid scale from the given scale number.
*/
export function scaler(scale: number) {
if (typeof scale !== 'number') {
// if scale is not provided (or invalid), use default
return 1;
}
// prevent scale to be below 0.5
return scale < 0.5 ? 0.5 : scale;
}
/**
* Return a new profile SVG (as a string) with the given params.
*/
export function Profile(params: ProfileData) {
const replacer = new Replacer(template);
const defaultHeight = 200;
const defaultWidth = 150;
const scale = params.scale;
const height = defaultHeight * scale;
const width = defaultWidth * scale;
// @ts-ignore: Delete scale to avoid unnecessary forEach loop
delete params.scale;
Object.keys(params).forEach((key: string) => {
//@ts-ignore: Cannot params[key] with key of type string
replacer.replace(key, params[key]);
});
replacer.replace('height', height.toString());
replacer.replace('width', width.toString());
return replacer.result;
}
/**
* Return a Base64 string of the given image URL.
*/
export async function getImageBase64(url: string): Promise<Base64> {
const res = await ky.get(url).arrayBuffer();
return encode(res);
}
/**
* Return a Base64 string of the given image path.
*/
export async function getLocalImageBase64(url: string): Promise<Base64> {
const res = (await Deno.readFile(url)).buffer;
return encode(res);
}
/**
* Seconds in a day
*/
export const oneDaySeconds = 24 * 60 * 60;
/**
* Blank GIF Base64 encoded
*/
const blankGif = 'R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7';
const internalProfilatorsData: { [key: string]: ProfileData } = {
'@profilator': {
username: 'Profilator',
name: 'Snap GitHub profiles',
image: await getLocalImageBase64(`${Deno.cwd()}/assets/profile.png`),
scale: 1,
},
'@blank': {
username: '',
name: '',
image: blankGif,
scale: 1,
},
'404': {
username: 'Not Found',
name: 'GitHub user not found.',
image: blankGif,
scale: 1,
},
};
/**
* Default profile profilators for internal use
*/
export const defaultProfiles = {
'@profilator': Profile(internalProfilatorsData['@profilator']),
'@blank': Profile(internalProfilatorsData['@blank']),
'404': Profile(internalProfilatorsData['404']),
};
/**
* Detect if HTML is excepted as response of the given request
*/
export function acceptsHTML(ctx: Context): boolean {
return ctx.request.accepts()?.includes('text/html') ?? false;
}
/**
* Detect if a request is made by a GitHub Camo Bot
*/
export function isGitHubCamoBot(ctx: Context): boolean {
return ctx.request.headers.get('User-Agent')?.includes('GitHub-Camo') ??
false;
}