MCPcopy Create free account
hub / github.com/apache/fesod / fetchAvatarAsBase64

Function fetchAvatarAsBase64

website/github-avatar.js:43–76  ·  view source on GitHub ↗

* Fetches avatar image from GitHub and converts it to base64 * @param {string} githubId - GitHub ID * @returns {Promise } Base64 encoded avatar image

(githubId)

Source from the content-addressed store, hash-verified

41 * @returns {Promise<string>} Base64 encoded avatar image
42 */
43function fetchAvatarAsBase64(githubId) {
44 return new Promise((resolve, reject) => {
45 const avatarUrl = `https://avatars.githubusercontent.com/u/${githubId}?v=4&s=${avatarSize}`;
46
47 https
48 .get(avatarUrl, (response) => {
49 // Check if request was successful
50 if (response.statusCode !== 200) {
51 reject(new Error(`Failed to fetch avatar from ${avatarUrl}: ${response.statusCode}`));
52 return;
53 }
54
55 const chunks = [];
56
57 // Collect data chunks
58 response.on("data", (chunk) => {
59 chunks.push(chunk);
60 });
61
62 // Convert to base64 when complete
63 response.on("end", () => {
64 const buffer = Buffer.concat(chunks);
65 const base64 = buffer.toString("base64");
66 resolve(base64);
67 });
68 })
69 .setTimeout(10000, () => {
70 reject(new Error(`Failed to fetch avatar ${avatarUrl}: timed out`));
71 })
72 .on("error", (error) => {
73 reject(error);
74 });
75 });
76}
77
78/**
79 * Get GitHub name from URL

Callers 1

processAvatarsFunction · 0.85

Calls 2

getMethod · 0.65
toStringMethod · 0.45

Tested by

no test coverage detected