-
Notifications
You must be signed in to change notification settings - Fork 276
fix(website): keep extension stars fresh #834
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
benvinegar
wants to merge
1
commit into
main
Choose a base branch
from
fix/fresh-extension-stars
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "hunkdiff": patch | ||
| --- | ||
|
|
||
| Keep community extension stars and update dates fresh between hunk.dev deployments. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| import { describe, expect, test } from "bun:test"; | ||
| import { handleExtensionActivityRequest } from "./extension-activity"; | ||
|
|
||
| /** Build a fetch-compatible stub around one concise test callback. */ | ||
| function createTestFetch( | ||
| callback: (input: string | URL | Request, init?: RequestInit) => Response | Promise<Response>, | ||
| ) { | ||
| return callback as typeof fetch; | ||
| } | ||
|
|
||
| describe("extension activity endpoint", () => { | ||
| test("returns compact GitHub activity with shared CDN caching", async () => { | ||
| let requestedUrl = ""; | ||
| const fetchUpstream = createTestFetch((input) => { | ||
| requestedUrl = String(input); | ||
| return Response.json({ | ||
| items: [ | ||
| { | ||
| full_name: "Elucid/Hunk-Less-Search", | ||
| stargazers_count: 12, | ||
| pushed_at: "2026-08-20T03:25:47Z", | ||
| created_at: "2026-08-16T22:57:51Z", | ||
| description: "This upstream field must not be forwarded", | ||
| }, | ||
| ], | ||
| }); | ||
| }); | ||
|
|
||
| const response = await handleExtensionActivityRequest( | ||
| new Request("https://hunk.dev/api/extension-activity"), | ||
| fetchUpstream, | ||
| ); | ||
|
|
||
| expect(requestedUrl).toContain("api.github.com/search/repositories"); | ||
| expect(response.status).toBe(200); | ||
| expect(response.headers.get("Cache-Control")).toBe( | ||
| "public, max-age=60, s-maxage=3600, stale-while-revalidate=86400", | ||
| ); | ||
| expect(await response.json()).toMatchObject({ | ||
| fetchedAt: expect.any(String), | ||
| repositories: [ | ||
| { | ||
| repo: "elucid/hunk-less-search", | ||
| stars: 12, | ||
| pushedAt: "2026-08-20T03:25:47Z", | ||
| createdAt: "2026-08-16T22:57:51Z", | ||
| }, | ||
| ], | ||
| }); | ||
| }); | ||
|
|
||
| test("does not cache upstream failures", async () => { | ||
| const response = await handleExtensionActivityRequest( | ||
| new Request("https://hunk.dev/api/extension-activity"), | ||
| createTestFetch(() => new Response("rate limited", { status: 429 })), | ||
| ); | ||
|
|
||
| expect(response.status).toBe(502); | ||
| expect(response.headers.get("Cache-Control")).toBe("no-store"); | ||
| expect(await response.json()).toEqual({ | ||
| error: "Extension activity is temporarily unavailable", | ||
| }); | ||
| }); | ||
|
|
||
| test("rejects non-GET requests without calling GitHub", async () => { | ||
| let called = false; | ||
| const response = await handleExtensionActivityRequest( | ||
| new Request("https://hunk.dev/api/extension-activity", { method: "POST" }), | ||
| createTestFetch(() => { | ||
| called = true; | ||
| return new Response(); | ||
| }), | ||
| ); | ||
|
|
||
| expect(called).toBe(false); | ||
| expect(response.status).toBe(405); | ||
| expect(response.headers.get("Allow")).toBe("GET"); | ||
| expect(response.headers.get("Cache-Control")).toBe("no-store"); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| import { | ||
| createExtensionActivityPayload, | ||
| githubTopicActivityUrl, | ||
| indexActivityByRepo, | ||
| } from "../website/src/data/extensionActivity"; | ||
|
|
||
| const CACHE_CONTROL = "public, max-age=60, s-maxage=3600, stale-while-revalidate=86400"; | ||
| const NO_STORE = "no-store"; | ||
|
|
||
| /** Build the authenticated GitHub headers available only to the server. */ | ||
| function githubHeaders() { | ||
| const token = process.env.GITHUB_TOKEN; | ||
| return { | ||
| Accept: "application/vnd.github+json", | ||
| "User-Agent": "hunk.dev-extension-directory", | ||
| ...(token ? { Authorization: `Bearer ${token}` } : {}), | ||
| }; | ||
| } | ||
|
|
||
| /** Return one JSON response with an explicit browser and CDN cache policy. */ | ||
| function jsonResponse(payload: unknown, status: number, cacheControl: string) { | ||
| return Response.json(payload, { | ||
| status, | ||
| headers: { "Cache-Control": cacheControl }, | ||
| }); | ||
| } | ||
|
|
||
| /** Serve compact extension activity through Vercel's shared CDN cache. */ | ||
| export async function handleExtensionActivityRequest( | ||
| request: Request, | ||
| fetchUpstream: typeof fetch = fetch, | ||
| ) { | ||
| if (request.method !== "GET") { | ||
| const response = jsonResponse({ error: "Method not allowed" }, 405, NO_STORE); | ||
| response.headers.set("Allow", "GET"); | ||
| return response; | ||
| } | ||
|
|
||
| try { | ||
| const upstream = await fetchUpstream(githubTopicActivityUrl(), { | ||
| headers: githubHeaders(), | ||
| signal: AbortSignal.timeout(8000), | ||
| }); | ||
| if (!upstream.ok) { | ||
| return jsonResponse( | ||
| { error: "Extension activity is temporarily unavailable" }, | ||
| 502, | ||
| NO_STORE, | ||
| ); | ||
| } | ||
|
|
||
| const activity = indexActivityByRepo(await upstream.json()); | ||
| if (!activity.size) { | ||
| return jsonResponse( | ||
| { error: "Extension activity is temporarily unavailable" }, | ||
| 502, | ||
| NO_STORE, | ||
| ); | ||
| } | ||
|
|
||
| return jsonResponse(createExtensionActivityPayload(activity), 200, CACHE_CONTROL); | ||
| } catch { | ||
| return jsonResponse({ error: "Extension activity is temporarily unavailable" }, 502, NO_STORE); | ||
| } | ||
| } | ||
|
|
||
| export default { | ||
| /** Adapt the web-standard handler to Vercel's fetch function contract. */ | ||
| fetch(request: Request) { | ||
| return handleExtensionActivityRequest(request); | ||
| }, | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| /** GitHub topic an author adds to be listed. */ | ||
| export const HUNK_EXTENSION_TOPIC = "hunk-extension"; | ||
|
|
||
| /** Repository facts fetched from GitHub, absent when GitHub omits them. */ | ||
| export interface ExtensionActivity { | ||
| stars?: number; | ||
| pushedAt?: string; | ||
| createdAt?: string; | ||
| } | ||
|
|
||
| /** One compact repository record returned by Hunk's cached activity endpoint. */ | ||
| export interface PublishedExtensionActivity extends ExtensionActivity { | ||
| repo: string; | ||
| } | ||
|
|
||
| /** Browser-safe response from Hunk's cached extension activity endpoint. */ | ||
| export interface ExtensionActivityPayload { | ||
| fetchedAt: string; | ||
| repositories: PublishedExtensionActivity[]; | ||
| } | ||
|
|
||
| /** Phrase one ISO timestamp as the coarse recency a directory card wants. */ | ||
| export function formatUpdated(pushedAt: string, now = new Date()) { | ||
| const days = Math.floor((now.getTime() - new Date(pushedAt).getTime()) / 86_400_000); | ||
| if (!Number.isFinite(days) || days < 0) return undefined; | ||
| if (days === 0) return "today"; | ||
| if (days === 1) return "yesterday"; | ||
| if (days < 30) return `${days} days ago`; | ||
| const months = Math.floor(days / 30); | ||
| if (months < 12) return `${months} month${months === 1 ? "" : "s"} ago`; | ||
| const years = Math.floor(days / 365); | ||
| return `${years} year${years === 1 ? "" : "s"} ago`; | ||
| } | ||
|
|
||
| /** Read one repository's volatile facts out of a GitHub API repository object. */ | ||
| export function readActivity(value: unknown): ExtensionActivity { | ||
| if (typeof value !== "object" || value === null) return {}; | ||
| const repository = value as Record<string, unknown>; | ||
| return { | ||
| stars: | ||
| typeof repository.stargazers_count === "number" ? repository.stargazers_count : undefined, | ||
| pushedAt: typeof repository.pushed_at === "string" ? repository.pushed_at : undefined, | ||
| createdAt: typeof repository.created_at === "string" ? repository.created_at : undefined, | ||
| }; | ||
| } | ||
|
|
||
| /** Index one topic-search response by lowercased `owner/name`. */ | ||
| export function indexActivityByRepo(payload: unknown): Map<string, ExtensionActivity> { | ||
| const items = | ||
| typeof payload === "object" && payload !== null | ||
| ? (payload as { items?: unknown }).items | ||
| : undefined; | ||
| if (!Array.isArray(items)) return new Map(); | ||
|
|
||
| const byRepo = new Map<string, ExtensionActivity>(); | ||
| for (const item of items) { | ||
| const fullName = | ||
| typeof item === "object" && item !== null | ||
| ? (item as { full_name?: unknown }).full_name | ||
| : undefined; | ||
| if (typeof fullName !== "string") continue; | ||
| byRepo.set(fullName.toLowerCase(), readActivity(item)); | ||
| } | ||
|
|
||
| return byRepo; | ||
| } | ||
|
|
||
| /** Build the one GitHub topic-search URL used by builds and the cached endpoint. */ | ||
| export function githubTopicActivityUrl() { | ||
| const query = encodeURIComponent(`topic:${HUNK_EXTENSION_TOPIC} is:public`); | ||
| return `https://api.github.com/search/repositories?q=${query}&per_page=100`; | ||
| } | ||
|
|
||
| /** Serialize indexed GitHub facts into the compact first-party response shape. */ | ||
| export function createExtensionActivityPayload( | ||
| activity: ReadonlyMap<string, ExtensionActivity>, | ||
| fetchedAt = new Date(), | ||
| ): ExtensionActivityPayload { | ||
| return { | ||
| fetchedAt: fetchedAt.toISOString(), | ||
| repositories: [...activity].map(([repo, facts]) => ({ repo, ...facts })), | ||
| }; | ||
| } | ||
|
|
||
| /** Index one first-party activity response, ignoring malformed records. */ | ||
| export function indexPublishedActivity(payload: unknown): Map<string, ExtensionActivity> { | ||
| const repositories = | ||
| typeof payload === "object" && payload !== null | ||
| ? (payload as { repositories?: unknown }).repositories | ||
| : undefined; | ||
| if (!Array.isArray(repositories)) return new Map(); | ||
|
|
||
| const activity = new Map<string, ExtensionActivity>(); | ||
| for (const record of repositories) { | ||
| if (typeof record !== "object" || record === null) continue; | ||
| const value = record as Record<string, unknown>; | ||
| if (typeof value.repo !== "string") continue; | ||
| activity.set(value.repo.toLowerCase(), { | ||
| stars: typeof value.stars === "number" ? value.stars : undefined, | ||
| pushedAt: typeof value.pushedAt === "string" ? value.pushedAt : undefined, | ||
| createdAt: typeof value.createdAt === "string" ? value.createdAt : undefined, | ||
| }); | ||
| } | ||
| return activity; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new function reads
GITHUB_TOKENdirectly fromprocess.env, bypassing the repository’s required Varlock validation and leaving configuration mistakes unchecked until deployment.Context Used: guidelines.mdc Cursor rule (source)
Prompt To Fix With AI
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!