Repository navigation
[C] Public releases API and Stack release UI #19
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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,215 @@ | ||
| /** | ||
| * Same-origin fetch of GET /api/releases for the Stack page. | ||
| * State thresholds mirror worker/src/release-display.ts. | ||
| */ | ||
|
|
||
| const RELEASE_STALE_AFTER_MS = 12 * 60 * 60 * 1000; | ||
| const SAFE_REPO = /^[A-Za-z0-9_.-]+$/; | ||
| const SAFE_TAG = /^[A-Za-z0-9._+/~^-]+$/; | ||
| const GITHUB_ORG_PREFIX = 'https://github.com/pegma-dev/'; | ||
|
|
||
| const statusNode = document.querySelector('#release-status'); | ||
| const releaseLines = document.querySelectorAll('.release-line[data-repository]'); | ||
|
|
||
| function classifyReleaseState(current, nowMs, fetchFailed) { | ||
| if (fetchFailed) { | ||
| return 'unavailable'; | ||
| } | ||
| if (current === null || current === undefined) { | ||
| return 'empty'; | ||
| } | ||
| const observedMs = Date.parse(current.observedAt); | ||
| if ( | ||
| Number.isFinite(observedMs) && | ||
| nowMs - observedMs > RELEASE_STALE_AFTER_MS | ||
| ) { | ||
| return 'stale'; | ||
| } | ||
| return 'populated'; | ||
| } | ||
|
|
||
| function formatReleaseLabel(current) { | ||
| const publishedMs = Date.parse(current.publishedAt); | ||
| if (!Number.isFinite(publishedMs)) { | ||
| return current.tagName; | ||
| } | ||
| const published = new Date(publishedMs).toISOString().slice(0, 10); | ||
| return `${current.tagName} · ${published}`; | ||
| } | ||
|
|
||
| function isSafeCurrent(current, repositoryName) { | ||
| if ( | ||
| current === null || | ||
| typeof current !== 'object' || | ||
| typeof current.tagName !== 'string' || | ||
| typeof current.releaseUrl !== 'string' || | ||
| typeof current.publishedAt !== 'string' || | ||
| typeof current.observedAt !== 'string' | ||
| ) { | ||
| return false; | ||
| } | ||
| if (!SAFE_REPO.test(repositoryName) || !SAFE_TAG.test(current.tagName)) { | ||
| return false; | ||
| } | ||
| const expected = `${GITHUB_ORG_PREFIX}${repositoryName}/releases/tag/${current.tagName}`; | ||
| return current.releaseUrl === expected; | ||
| } | ||
|
|
||
| function setStatus(state, message) { | ||
| if (!(statusNode instanceof HTMLElement)) { | ||
| return; | ||
| } | ||
| statusNode.dataset.state = state; | ||
| statusNode.textContent = message; | ||
| } | ||
|
|
||
| function setLineState(line, state, content) { | ||
| const value = line.querySelector('.release-value'); | ||
| if (!(value instanceof HTMLElement)) { | ||
| return; | ||
| } | ||
| value.dataset.state = state; | ||
| value.replaceChildren(); | ||
| if (typeof content === 'string') { | ||
| value.textContent = content; | ||
| return; | ||
| } | ||
| value.append(content); | ||
| } | ||
|
|
||
| function renderPopulated(line, current) { | ||
| const link = document.createElement('a'); | ||
| link.href = current.releaseUrl; | ||
| link.textContent = formatReleaseLabel(current); | ||
| link.rel = 'noopener noreferrer'; | ||
| setLineState(line, 'populated', link); | ||
| } | ||
|
|
||
| function renderStale(line, current) { | ||
| const wrap = document.createDocumentFragment(); | ||
| const link = document.createElement('a'); | ||
| link.href = current.releaseUrl; | ||
| link.textContent = formatReleaseLabel(current); | ||
| link.rel = 'noopener noreferrer'; | ||
| wrap.append(link); | ||
| wrap.append(document.createTextNode(' (may be stale)')); | ||
| setLineState(line, 'stale', wrap); | ||
| } | ||
|
|
||
| function applyReleases(body, fetchFailed) { | ||
| const nowMs = Date.now(); | ||
| const byName = new Map(); | ||
| if ( | ||
| body !== null && | ||
| typeof body === 'object' && | ||
| Array.isArray(body.releases) | ||
| ) { | ||
| for (const entry of body.releases) { | ||
| if ( | ||
| entry !== null && | ||
| typeof entry === 'object' && | ||
| typeof entry.repositoryName === 'string' | ||
| ) { | ||
| byName.set(entry.repositoryName, entry); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| let populated = 0; | ||
| let stale = 0; | ||
|
|
||
| for (const line of releaseLines) { | ||
| if (!(line instanceof HTMLElement)) { | ||
| continue; | ||
| } | ||
| const repositoryName = line.dataset.repository ?? ''; | ||
| const entry = byName.get(repositoryName); | ||
| const rawCurrent = entry?.current ?? null; | ||
| const current = | ||
| rawCurrent !== null && isSafeCurrent(rawCurrent, repositoryName) | ||
| ? rawCurrent | ||
| : null; | ||
| const state = classifyReleaseState(current, nowMs, fetchFailed); | ||
|
|
||
| if (state === 'unavailable') { | ||
| setLineState(line, 'unavailable', 'Temporarily unavailable'); | ||
| continue; | ||
| } | ||
| if (state === 'empty' || current === null) { | ||
| setLineState(line, 'empty', 'No stable release recorded yet'); | ||
| continue; | ||
| } | ||
| if (state === 'stale') { | ||
| stale += 1; | ||
| renderStale(line, current); | ||
| continue; | ||
| } | ||
| populated += 1; | ||
| renderPopulated(line, current); | ||
| } | ||
|
|
||
| if (fetchFailed) { | ||
| setStatus( | ||
| 'unavailable', | ||
| 'Current release versions are temporarily unavailable. Repository links below still work.', | ||
| ); | ||
| return; | ||
| } | ||
|
|
||
| if (populated === 0 && stale === 0) { | ||
| setStatus( | ||
| 'empty', | ||
| 'No stable release versions are recorded yet. Repository links below still work.', | ||
| ); | ||
| return; | ||
| } | ||
|
|
||
| if (stale > 0 && populated === 0) { | ||
| setStatus( | ||
| 'stale', | ||
| 'Release versions are available but may be stale. Repository links remain authoritative.', | ||
| ); | ||
| return; | ||
| } | ||
|
|
||
| if (stale > 0) { | ||
| setStatus( | ||
| 'stale', | ||
| `Showing current stable releases (${stale} may be stale). Values come from D1, not a site rebuild.`, | ||
| ); | ||
| return; | ||
| } | ||
|
|
||
| setStatus( | ||
| 'populated', | ||
| 'Showing current stable releases from the live API (no site rebuild required).', | ||
| ); | ||
| } | ||
|
|
||
| async function start() { | ||
| try { | ||
| const response = await fetch('/api/releases', { | ||
| credentials: 'same-origin', | ||
| headers: { Accept: 'application/json' }, | ||
| }); | ||
| if (!response.ok) { | ||
| applyReleases(null, true); | ||
| return; | ||
| } | ||
| const body = await response.json(); | ||
| if ( | ||
| body === null || | ||
| typeof body !== 'object' || | ||
| body.schema !== 'pegma.releases.v1' || | ||
| !Array.isArray(body.releases) | ||
| ) { | ||
| applyReleases(null, true); | ||
| return; | ||
| } | ||
| applyReleases(body, false); | ||
| } catch { | ||
| applyReleases(null, true); | ||
| } | ||
| } | ||
|
|
||
| await start(); | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.