-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch.js
More file actions
157 lines (141 loc) · 5.75 KB
/
Copy pathfetch.js
File metadata and controls
157 lines (141 loc) · 5.75 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
151
152
153
154
155
156
157
import https from 'https'
import { getCached, setCache } from './cache.js'
function toBase64(url) {
return new Promise(resolve => {
const req = https.get(url, { family: 4 }, res => { // ponytail: force IPv4, VPS IPv6 route dead → Happy Eyeballs ETIMEDOUT
if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location)
return toBase64(res.headers.location).then(resolve)
const mime = res.headers['content-type']?.split(';')[0] || 'image/png'
const chunks = []
res.on('data', c => chunks.push(c))
res.on('end', () => resolve(`data:${mime};base64,${Buffer.concat(chunks).toString('base64')}`))
res.on('error', () => resolve(null))
})
req.on('error', () => resolve(null))
req.on('timeout', () => { req.destroy(); resolve(null) })
req.setTimeout(8000)
})
}
export async function fetchRepo(input, { token } = {}) {
const slug = input.replace(/^https?:\/\/github\.com\//, '').replace(/\/$/, '')
const cached = getCached(slug)
if (cached) {
console.log(` (cache hit)`)
return cached
}
const headers = {
'User-Agent': 'repocard',
'Accept': 'application/vnd.github.v3+json',
...(token && { Authorization: `Bearer ${token}` }),
}
const [repoRes, langRes, actRes, contribRes, readmeRes, releaseRes] = await Promise.all([
fetch(`https://api.github.com/repos/${slug}`, { headers }),
fetch(`https://api.github.com/repos/${slug}/languages`, { headers }),
fetch(`https://api.github.com/repos/${slug}/stats/commit_activity`, { headers }),
fetch(`https://api.github.com/repos/${slug}/contributors?per_page=100`, { headers }),
fetch(`https://api.github.com/repos/${slug}/readme`, { headers }),
fetch(`https://api.github.com/repos/${slug}/releases/latest`, { headers }),
])
if (repoRes.status === 403 || repoRes.status === 429) {
const reset = new Date(repoRes.headers.get('x-ratelimit-reset') * 1000).toLocaleTimeString()
throw new Error(`GitHub rate limit hit. Resets at ${reset}. Set --token=ghp_xxx to raise the limit.`)
}
if (!repoRes.ok) throw new Error(`Repo not found: ${slug} (HTTP ${repoRes.status})`)
const d = await repoRes.json()
const langRaw = await langRes.json().catch(() => ({}))
const activity = actRes.status === 200 ? await actRes.json().catch(() => []) : []
const contribs = contribRes.ok ? (await contribRes.json().catch(() => [])) : []
const totalContributors = Array.isArray(contribs) ? contribs.length : 0
let latestRelease = null
if (releaseRes.ok) {
const rel = await releaseRes.json().catch(() => null)
if (rel?.tag_name) {
const days = Math.floor((Date.now() - new Date(rel.published_at)) / 86400000)
const ago = days === 0 ? 'today' : days === 1 ? 'yesterday'
: days < 30 ? `${days}d ago`
: days < 365 ? `${Math.floor(days / 30)}mo ago`
: `${Math.floor(days / 365)}y ago`
latestRelease = { tag: rel.tag_name, ago }
}
}
let readme = ''
if (readmeRes.ok) {
const rm = await readmeRes.json().catch(() => null)
if (rm?.content) {
const raw = Buffer.from(rm.content, 'base64').toString('utf8')
readme = raw
.split('\n')
.map(l => l
.replace(/^#{1,6}\s+/, '')
.replace(/<!--[\s\S]*?-->/g, '')
.replace(/<[^>]+>/g, '')
.replace(/\[!\[.*?\]\(.*?\)\]\(.*?\)/g, '')
.replace(/\[([^\]]+)\]\([^)]+\)/g, '$1')
.replace(/\*\*([^*]+)\*\*/g, '$1')
.replace(/\*([^*]+)\*/g, '$1')
.replace(/`([^`]+)`/g, '$1')
.replace(/^[-*>|]+\s*/, '')
.replace(/&[a-z]+;/g, ' ')
.replace(/\s+/g, ' ')
.trim()
)
.filter(l => l.length > 30 && !/^[[\]()!<>{}]/.test(l))
.slice(0, 3)
.join(' ')
.slice(0, 200)
.trim()
}
}
const langTotal = Object.values(langRaw).reduce((a, b) => a + b, 0)
const languages = Object.entries(langRaw)
.map(([name, bytes]) => ({ name, pct: langTotal ? (bytes / langTotal) * 100 : 0 }))
.sort((a, b) => b.pct - a.pct)
.slice(0, 6)
const [ownerAvatar, ...contributorAvatars] = await Promise.all([
toBase64(d.owner.avatar_url + '&s=64'),
...contribs.slice(0, 7).map(c => toBase64(c.avatar_url + '&s=40')),
])
const pushed = new Date(d.pushed_at)
const diffDays = Math.floor((Date.now() - pushed) / 86400000)
const updatedAgo = diffDays === 0 ? 'today'
: diffDays === 1 ? 'yesterday'
: diffDays < 30 ? `${diffDays} days ago`
: diffDays < 365 ? `${Math.floor(diffDays / 30)} months ago`
: `${Math.floor(diffDays / 365)} years ago`
const createdDays = Math.floor((Date.now() - new Date(d.created_at)) / 86400000)
const createdAgo = createdDays < 30 ? `${createdDays}d ago`
: createdDays < 365 ? `${Math.floor(createdDays / 30)}mo ago`
: `${Math.floor(createdDays / 365)}y ago`
const sizeKb = d.size
const sizeStr = sizeKb < 1024 ? `${sizeKb} KB`
: sizeKb < 1024 * 1024 ? `${(sizeKb / 1024).toFixed(1)} MB`
: `${(sizeKb / 1024 / 1024).toFixed(1)} GB`
const result = {
name: d.name,
fullName: d.full_name,
description: d.description || '',
stars: d.stargazers_count,
forks: d.forks_count,
language: d.language,
license: d.license?.spdx_id || null,
ownerLogin: d.owner.login,
ownerAvatar,
topics: d.topics?.slice(0, 5) || [],
isPrivate: d.private,
commitActivity: activity.slice(-26),
languages,
contributorAvatars: contributorAvatars.filter(Boolean),
totalContributors,
updatedAgo,
openIssues: d.open_issues_count,
watchers: d.subscribers_count || d.watchers_count,
defaultBranch: d.default_branch,
createdAgo,
size: sizeStr,
totalCommits: activity.reduce((sum, w) => sum + (w.total || 0), 0),
readme,
latestRelease,
}
setCache(slug, result)
return result
}