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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
|
import { parseDocument } from "htmlparser2";
import { type Element } from "domhandler";
import { getAttributeValue, getInnerHTML, textContent } from "domutils";
import * as cssSelect from "css-select";
export class GitHubHTTPError extends Error {
status: number;
constructor(status: number, message: string) {
super(message);
this.name = "GitHubHTTPError";
this.status = status;
}
}
export class RedirectError extends Error {
location: string;
constructor(location: string) {
super(`Redirect to ${location}`);
this.name = "RedirectError";
this.location = location;
}
}
interface TreeItem {
contentType: "directory" | "file";
name: string;
path: string;
}
interface OverviewFile {
displayName: string;
richText?: string;
}
interface IssueNode {
number: number;
title?: string;
titleHtml?: string;
author?: {
login: string;
};
createdAt: string;
state: string;
labels?: {
edges: {
node: {
name: string;
};
}[];
};
}
interface IssueIndexPageQuery {
queryName: "IssueIndexPageQuery";
result: {
data: {
repository: {
search: {
edges: {
node: IssueNode;
}[];
};
};
};
};
}
type StylingDirective = [number, number, string];
interface Commit {
oid: string;
url: string;
authoredDate: string;
committedDate: string;
shortMessage: string;
bodyMessageHtml: string;
authors: {
login: string;
displayName: string;
avatarUrl: string;
path: string;
}[];
}
export interface CommitGroup {
title: string;
commits: Commit[];
}
interface CodeViewLayoutRoute {
repo: {
ownerLogin: string;
name: string;
};
refInfo: {
name: string;
};
path: string;
}
interface CodeViewRepoRoute {
tree: {
items: TreeItem[];
};
overview?: {
overviewFiles?: OverviewFile[];
};
}
interface CodeViewTreeRoute {
tree: {
items: TreeItem[];
readme?: OverviewFile;
};
}
interface BlobHeaderInfo {
blobSize: string;
lineInfo?: {
truncatedLoc: string | null;
truncatedSloc: string | null;
};
}
interface CodeViewBlobLayoutRoute {
blob: {
headerInfo: BlobHeaderInfo;
language: string | null;
image: boolean;
};
}
interface CodeViewBlobLayoutRouteStyledBlob {
rawLines: string[] | null;
colorizedLines: string[] | null;
stylingDirectives: StylingDirective[][] | null;
}
interface CodeViewBlobRoute {
richText: string | null;
}
interface AppPayload {
codeViewLayoutRoute?: CodeViewLayoutRoute;
codeViewRepoRoute?: CodeViewRepoRoute;
codeViewTreeRoute?: CodeViewTreeRoute;
codeViewBlobLayoutRoute?: CodeViewBlobLayoutRoute;
"codeViewBlobLayoutRoute.StyledBlob"?: CodeViewBlobLayoutRouteStyledBlob;
codeViewBlobRoute?: CodeViewBlobRoute;
commitGroups?: CommitGroup[];
// Actually there may be other query types.
preloadedQueries?: IssueIndexPageQuery[];
}
function escapeHtml(text: string): string {
return text
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
}
// Renders directives[i..] within [rangeStart, rangeEnd], producing nested spans.
// Returns the rendered content and the index of the first directive not consumed.
function renderRange(
line: string,
directives: StylingDirective[],
i: number,
rangeStart: number,
rangeEnd: number,
): [string, number] {
let result = "";
let pos = rangeStart;
while (i < directives.length) {
const [start, end, className] = directives[i];
// Stop at directives that start outside or extend past this range.
if (start >= rangeEnd || end > rangeEnd) break;
// Combine classes for directives with identical ranges.
let classes = className;
let j = i + 1;
while (j < directives.length && directives[j][0] === start && directives[j][1] === end) {
classes += " " + directives[j][2];
j++;
}
result += escapeHtml(line.substring(pos, start));
const [innerContent, nextI] = renderRange(line, directives, j, start, end);
result += `<span class="${classes}">${innerContent}</span>`;
pos = end;
i = nextI;
}
result += escapeHtml(line.substring(pos, rangeEnd));
return [result, i];
}
function applyStyling(line: string, directives: StylingDirective[]): string {
// Sort by start ascending, then end descending so outer spans precede nested ones.
const sorted = [...directives].sort((a, b) => (a[0] !== b[0] ? a[0] - b[0] : b[1] - a[1]));
const [result] = renderRange(line, sorted, 0, 0, line.length);
return result;
}
interface GitHubCommon {
repo: {
owner: string;
name: string;
};
}
interface GitHubNav extends GitHubCommon {
branch: string;
path: string;
}
export interface GitHubTree extends GitHubNav {
items: TreeItem[];
overviewHtml?: Record<string, string>;
}
export interface RepoInfo {
description: string | null;
website: string | null;
stars: string | null;
watchers: string | null;
forks: string | null;
numReleases?: number;
latestReleaseTag?: string;
latestReleaseDate?: string;
}
export interface GitHubRepo extends GitHubTree {
info: RepoInfo;
}
export interface GitHubBlob extends GitHubNav {
language: string | null;
size: string;
image: boolean;
textLines: string[] | null;
htmlLines: string[] | null;
htmlContent: string | null;
}
interface Issue {
number: number;
title: string;
state: string;
createdAt: string;
numReplies?: number;
numPRs?: number;
}
export interface GitHubIssues extends GitHubCommon {
issues: Issue[];
}
export interface GitHubCommits extends GitHubNav {
commitGroups: CommitGroup[];
}
interface Release {
tagName: string;
title: string;
publishedAt: string;
bodyHtml: string;
assets?: ReleaseAsset[];
}
export interface ReleaseAsset {
name: string;
url: string;
size?: string;
publishedAt?: string;
}
export interface GitHubReleases extends GitHubCommon {
releases: Release[];
}
export interface GitHubRelease extends GitHubCommon {
release: Release;
}
async function fetchGitHubPage(path: string): Promise<string> {
// GitHub throttles/blocks requests without realistic browser headers.
// These headers make the request appear as a standard browser visit.
const headers = {
"User-Agent":
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36",
Accept: "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
"Accept-Language": "en-US,en;q=0.5",
};
const response = await fetch(`https://github.com/${path}`, { headers, redirect: "manual" });
if (response.status === 301 || response.status === 302) {
const location = response.headers.get("location");
if (location) {
const url = new URL(location);
if (url.hostname === "github.com") {
throw new RedirectError(url.pathname);
}
}
}
if (!response.ok) throw new GitHubHTTPError(response.status, response.statusText);
return response.text();
}
function parsePayload(html: string): AppPayload | null {
const regex = new RegExp(
`<script type="application/json" data-target="react-app.embeddedData">([^<]+)</script>`,
"g",
);
match: for (const match of html.matchAll(regex)) {
let data;
try {
data = JSON.parse(match[1]);
} catch {
continue;
}
if (data.payload === undefined) continue match;
return data.payload;
}
return null;
}
function parseRepoInfo(html: string): RepoInfo {
const document = parseDocument(html);
const aboutSection = cssSelect.selectOne(".BorderGrid-cell h2:contains('About')", document);
let description: string | null = null;
let website: string | null = null;
if (aboutSection) {
const parent = aboutSection.parent;
if (parent) {
const descElem = cssSelect.selectOne("p.f4", parent);
if (descElem) {
description = textContent(descElem).trim();
}
const websiteElem = cssSelect.selectOne(
"a[rel='noopener noreferrer nofollow']",
parent,
) as Element | null;
if (websiteElem) {
website = getAttributeValue(websiteElem, "href") || null;
}
}
}
const starsElem = cssSelect.selectOne("#repo-stars-counter-star", document) as Element | null;
const stars = starsElem
? getAttributeValue(starsElem, "title") || textContent(starsElem).trim()
: null;
const watchersElem = cssSelect.selectOne("a[href$='/watchers'] strong", document);
const watchers = watchersElem ? textContent(watchersElem).trim() : null;
const forksElem = cssSelect.selectOne("#repo-network-counter", document) as Element | null;
const forks = forksElem
? getAttributeValue(forksElem, "title") || textContent(forksElem).trim()
: null;
const releaseElem = cssSelect.selectOne(
"a[href$='/releases'] .Counter",
document,
) as Element | null;
let numReleases: number | undefined;
let latestReleaseTag: string | undefined;
let latestReleaseDate: string | undefined;
if (releaseElem) {
const releaseText = getAttributeValue(releaseElem, "title") || textContent(releaseElem);
numReleases = parseInt(releaseText.replace(/,/g, ""), 10);
// Find the latest release tag and date if available
const cell = (releaseElem.parent as Element)?.parent?.parent as Element; // span -> a -> h2 -> cell
if (cell) {
const latestLink = cssSelect.selectOne("a[href*='/releases/tag/']", cell) as Element;
if (latestLink) {
const tagElem = cssSelect.selectOne(".css-truncate-target", latestLink);
if (tagElem) {
latestReleaseTag = textContent(tagElem).trim();
}
const timeElem = cssSelect.selectOne("relative-time", latestLink);
if (timeElem) {
latestReleaseDate = getAttributeValue(timeElem, "datetime") || undefined;
}
}
}
}
return {
description,
website,
stars,
watchers,
forks,
numReleases,
latestReleaseTag,
latestReleaseDate,
};
}
function extractOverviewHtml(
overviewFiles: OverviewFile[] = [],
): Record<string, string> | undefined {
const result: Record<string, string> = {};
for (const file of overviewFiles) {
if (file.richText !== undefined) {
result[file.displayName] = file.richText;
}
}
return Object.keys(result).length > 0 ? result : undefined;
}
function extractGitHub<T>(payload: AppPayload | null, extra: T): GitHubNav & T {
const layoutRoute = payload?.codeViewLayoutRoute;
if (layoutRoute === undefined) {
throw new Error("Missing codeViewLayoutRoute");
}
return {
repo: {
owner: layoutRoute.repo.ownerLogin,
name: layoutRoute.repo.name,
},
branch: layoutRoute.refInfo.name,
path: layoutRoute.path,
...extra,
};
}
export async function getGitHubRepo(owner: string, repo: string): Promise<GitHubRepo> {
const html = await fetchGitHubPage(`${owner}/${repo}`);
const payload = parsePayload(html);
const repoRoute = payload?.codeViewRepoRoute;
if (repoRoute?.tree === undefined) {
throw new Error("Could not find tree data in embedded JSON");
}
const info = parseRepoInfo(html);
const overviewHtml = extractOverviewHtml(repoRoute.overview?.overviewFiles);
return extractGitHub(payload, { items: repoRoute.tree.items, info, overviewHtml });
}
export async function getGitHubTree(
owner: string,
repo: string,
branch: string,
path: string,
): Promise<GitHubTree> {
const html = await fetchGitHubPage(`${owner}/${repo}/tree/${branch}/${path}`);
const payload = parsePayload(html);
const treeRoute = payload?.codeViewTreeRoute;
if (treeRoute?.tree === undefined) {
throw new Error("Could not find tree data in embedded JSON");
}
const overviewHtml = extractOverviewHtml(treeRoute.tree.readme ? [treeRoute.tree.readme] : []);
return extractGitHub(payload, { items: treeRoute.tree.items, overviewHtml });
}
export async function getGitHubBlob(
owner: string,
repo: string,
branch: string,
path: string,
): Promise<GitHubBlob> {
const html = await fetchGitHubPage(`${owner}/${repo}/blob/${branch}/${path}`);
const payload = parsePayload(html);
const blob = payload?.codeViewBlobLayoutRoute?.blob;
const styledBlob = payload?.["codeViewBlobLayoutRoute.StyledBlob"];
if (blob === undefined) {
throw new Error("Could not find blob data in embedded JSON");
}
let htmlLines = styledBlob?.colorizedLines ?? null;
const stylingDirectives = styledBlob?.stylingDirectives;
if (!htmlLines && stylingDirectives && styledBlob?.rawLines) {
htmlLines = styledBlob.rawLines.map((line, i) => applyStyling(line, stylingDirectives[i]));
}
let size = blob.headerInfo.blobSize;
if (blob.headerInfo.lineInfo?.truncatedLoc) {
size += ` / ${blob.headerInfo.lineInfo.truncatedLoc} lines`;
}
if (blob.headerInfo.lineInfo?.truncatedSloc) {
size += ` / ${blob.headerInfo.lineInfo.truncatedSloc} loc`;
}
return extractGitHub(payload, {
language: blob.language || null,
size,
image: blob.image,
textLines: styledBlob?.rawLines ?? null,
htmlLines,
htmlContent: payload?.codeViewBlobRoute?.richText ?? null,
});
}
export async function getGitHubIssues(owner: string, repo: string): Promise<GitHubIssues> {
const html = await fetchGitHubPage(`${owner}/${repo}/issues`);
const payload = parsePayload(html);
let issuesQuery: IssueIndexPageQuery | undefined;
for (const query of payload?.preloadedQueries ?? []) {
if (query.queryName === "IssueIndexPageQuery") {
issuesQuery = query;
}
}
if (issuesQuery === undefined) {
throw new Error("Could not find IssueIndexPageQuery in embedded JSON");
}
const issues: Issue[] = issuesQuery.result.data.repository.search.edges.map((edge) => {
const { number, title, titleHtml, state, createdAt } = edge.node;
const finalTitle = (title || titleHtml || "").replace(/<[^>]+>/g, "");
return { number, title: finalTitle, state, createdAt };
});
return {
repo: {
owner,
name: repo,
},
issues,
};
}
export async function getGitHubCommits(
owner: string,
repo: string,
branch: string,
path: string,
): Promise<GitHubCommits> {
const urlPath = path
? `${owner}/${repo}/commits/${branch}/${path}`
: `${owner}/${repo}/commits/${branch}`;
const html = await fetchGitHubPage(urlPath);
const payload = parsePayload(html);
if (payload?.commitGroups === undefined) {
throw new Error("Could not find commit history in embedded JSON");
}
return {
repo: {
owner,
name: repo,
},
branch,
path,
commitGroups: payload.commitGroups,
};
}
function parseRelease(section: Element, title: string): Release {
// Extract tag name from link
const tagLink = cssSelect.selectOne('a[href*="/tree/"]', section);
let tagName = title;
if (tagLink) {
const href = getAttributeValue(tagLink, "href");
if (href) {
tagName = href.split("/").pop() || title;
}
}
// Extract published date
const relativeTime = cssSelect.selectOne("relative-time", section);
const publishedAt = relativeTime ? getAttributeValue(relativeTime, "datetime") || "" : "";
// Extract body HTML (markdown content)
const body = cssSelect.selectOne(".markdown-body", section);
const bodyHtml = body ? getInnerHTML(body).trim() : "";
return {
tagName,
title,
publishedAt,
bodyHtml,
};
}
export async function getGitHubReleases(owner: string, repo: string): Promise<GitHubReleases> {
const html = await fetchGitHubPage(`${owner}/${repo}/releases`);
const document = parseDocument(html);
const releases: Release[] = [];
// Not really sure the right way to express this type.
const sections = cssSelect.selectAll("section", document) as unknown as Element[];
for (const section of sections) {
const h2 = cssSelect.selectOne("h2[id]", section);
if (!h2) continue;
const title = textContent(h2).trim();
releases.push(parseRelease(section, title));
}
return {
repo: {
owner,
name: repo,
},
releases,
};
}
export async function getGitHubReleaseBase(
owner: string,
repo: string,
tag: string,
): Promise<GitHubRelease> {
const html = await fetchGitHubPage(`${owner}/${repo}/releases/tag/${tag}`);
const document = parseDocument(html);
const box = cssSelect.selectOne(".Box:has(h1)", document) as Element | null;
if (!box) throw new Error("Could not find release box");
const h1 = cssSelect.selectOne("h1", box);
const title = h1 ? textContent(h1).trim() : "";
const release = parseRelease(box, title);
return {
repo: {
owner,
name: repo,
},
release,
};
}
export async function getGitHubReleaseAssets(
owner: string,
repo: string,
tag: string,
): Promise<ReleaseAsset[]> {
const html = await fetchGitHubPage(`${owner}/${repo}/releases/expanded_assets/${tag}`);
const document = parseDocument(html);
const assets: ReleaseAsset[] = [];
for (const row of cssSelect.selectAll("li", document) as unknown as Element[]) {
const link = cssSelect.selectOne("a[href]", row);
if (!link) continue;
const url = getAttributeValue(link, "href") || "";
// Filename is in the link, often split into multiple spans
const name = textContent(link).replace(/\s+/g, " ").trim();
let size: string | undefined;
let publishedAt: string | undefined;
for (const s of cssSelect.selectAll("span.color-fg-muted.text-right", row)) {
// Size is in a span with color-fg-muted and text-right, but avoid the one with relative-time
const timeElem = cssSelect.selectOne("relative-time", row);
if (timeElem) {
publishedAt = timeElem ? getAttributeValue(timeElem, "datetime") || "" : "";
} else {
const text = textContent(s).trim();
if (text) size = text;
}
}
assets.push({
name,
url: url.startsWith("/") ? `https://github.com${url}` : url,
size,
publishedAt,
});
}
return assets;
}
export async function getGitHubRelease(
owner: string,
repo: string,
tag: string,
): Promise<GitHubRelease> {
const [base, assets] = await Promise.all([
getGitHubReleaseBase(owner, repo, tag),
getGitHubReleaseAssets(owner, repo, tag),
]);
base.release.assets = assets;
return base;
}
|