|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useEffect, useState } from "react"; |
| 4 | + |
| 5 | +// The site publishes out/packages/release-info.json on every deployment, built |
| 6 | +// from the GitHub release the package repository actually mirrors. It is the |
| 7 | +// only authoritative statement of "what version is on documentdb.io", so the |
| 8 | +// UI derives its version strings from it rather than repeating them. |
| 9 | +// |
| 10 | +// Before this module the versions were hardcoded in the page, and they drifted: |
| 11 | +// the page still advertised 0.114-0 (and 0.113-0 for the repository examples) |
| 12 | +// after v0.116-0 had been published and mirrored. |
| 13 | + |
| 14 | +export type ReleaseInfo = { |
| 15 | + /** Git tag of the mirrored release, e.g. "v0.116-0". */ |
| 16 | + tagName: string; |
| 17 | + /** Extension package version on DEB, e.g. "0.116-0". */ |
| 18 | + aptVersion: string; |
| 19 | + /** Extension package version on RPM, e.g. "0.116.0-1.el9". */ |
| 20 | + rpmVersion: string; |
| 21 | + /** Version of every non-extension package, e.g. "0.116.0". */ |
| 22 | + metaVersion: string; |
| 23 | + releaseUrl: string; |
| 24 | + assetNames: readonly string[]; |
| 25 | +}; |
| 26 | + |
| 27 | +// Used until the fetch resolves, and permanently if it fails. A stale-but-valid |
| 28 | +// page is much better than a blank one, so this is a real release rather than a |
| 29 | +// placeholder. Keep it in step with the newest release; the drift check in CI |
| 30 | +// fails the build when it falls behind release-info.json. |
| 31 | +export const FALLBACK_RELEASE: ReleaseInfo = { |
| 32 | + tagName: "v0.116-0", |
| 33 | + aptVersion: "0.116-0", |
| 34 | + rpmVersion: "0.116.0-1.el9", |
| 35 | + metaVersion: "0.116.0", |
| 36 | + releaseUrl: "https://github.com/documentdb/documentdb/releases/tag/v0.116-0", |
| 37 | + assetNames: [], |
| 38 | +}; |
| 39 | + |
| 40 | +type RawReleaseInfo = { |
| 41 | + tag_name?: unknown; |
| 42 | + html_url?: unknown; |
| 43 | + assets?: unknown; |
| 44 | +}; |
| 45 | + |
| 46 | +function assetNamesOf(raw: RawReleaseInfo): string[] { |
| 47 | + if (!Array.isArray(raw.assets)) { |
| 48 | + return []; |
| 49 | + } |
| 50 | + return raw.assets |
| 51 | + .map((asset) => |
| 52 | + asset && typeof asset === "object" && typeof (asset as { name?: unknown }).name === "string" |
| 53 | + ? (asset as { name: string }).name |
| 54 | + : null, |
| 55 | + ) |
| 56 | + .filter((name): name is string => name !== null); |
| 57 | +} |
| 58 | + |
| 59 | +function firstMatch(names: readonly string[], pattern: RegExp): string | null { |
| 60 | + for (const name of names) { |
| 61 | + const match = pattern.exec(name); |
| 62 | + if (match?.[1]) { |
| 63 | + return match[1]; |
| 64 | + } |
| 65 | + } |
| 66 | + return null; |
| 67 | +} |
| 68 | + |
| 69 | +/** |
| 70 | + * Derives the display versions from a release-info.json payload. |
| 71 | + * |
| 72 | + * Each field falls back independently: a release that stops shipping one |
| 73 | + * package shape must not blank out the versions that are still present. |
| 74 | + */ |
| 75 | +export function parseReleaseInfo(payload: unknown): ReleaseInfo { |
| 76 | + if (!payload || typeof payload !== "object") { |
| 77 | + return FALLBACK_RELEASE; |
| 78 | + } |
| 79 | + const raw = payload as RawReleaseInfo; |
| 80 | + const names = assetNamesOf(raw); |
| 81 | + |
| 82 | + const tagName = typeof raw.tag_name === "string" ? raw.tag_name : FALLBACK_RELEASE.tagName; |
| 83 | + const releaseUrl = |
| 84 | + typeof raw.html_url === "string" |
| 85 | + ? raw.html_url |
| 86 | + : `https://github.com/documentdb/documentdb/releases/tag/${tagName}`; |
| 87 | + |
| 88 | + // The extension keeps the control-file form (0.116-0) on DEB, while RPM |
| 89 | + // splits it into Version/Release and renders 0.116.0-1.el9. Everything else |
| 90 | + // uses the flat dotted form. Read all three off real filenames so the page |
| 91 | + // cannot claim a shape the release does not contain. |
| 92 | + const aptVersion = |
| 93 | + firstMatch(names, /^ubuntu[\d.]+-postgresql-\d+-documentdb_([^_]+)_/) ?? |
| 94 | + firstMatch(names, /^deb\d+-postgresql-\d+-documentdb_([^_]+)_/) ?? |
| 95 | + FALLBACK_RELEASE.aptVersion; |
| 96 | + |
| 97 | + const rpmVersion = |
| 98 | + firstMatch(names, /^rhel\d+-postgresql\d+-documentdb-(.+)\.(?:x86_64|aarch64)\.rpm$/) ?? |
| 99 | + FALLBACK_RELEASE.rpmVersion; |
| 100 | + |
| 101 | + const metaVersion = |
| 102 | + firstMatch(names, /^ubuntu[\d.]+-documentdb_([^_]+)_all\.deb$/) ?? |
| 103 | + firstMatch(names, /^documentdb-(\d+\.\d+\.\d+)-\d+\.noarch\.rpm$/) ?? |
| 104 | + FALLBACK_RELEASE.metaVersion; |
| 105 | + |
| 106 | + return { tagName, aptVersion, rpmVersion, metaVersion, releaseUrl, assetNames: names }; |
| 107 | +} |
| 108 | + |
| 109 | +/** |
| 110 | + * Reads the mirrored release description published alongside the packages. |
| 111 | + * |
| 112 | + * Returns the fallback synchronously so the first paint is always correct-ish, |
| 113 | + * then swaps in the live values. The site is a static export, so this has to |
| 114 | + * happen in the browser; NEXT_PUBLIC_BASE_PATH is the one base-path value Next |
| 115 | + * keeps in the client bundle. |
| 116 | + */ |
| 117 | +export function useReleaseInfo(): ReleaseInfo { |
| 118 | + const [release, setRelease] = useState<ReleaseInfo>(FALLBACK_RELEASE); |
| 119 | + |
| 120 | + useEffect(() => { |
| 121 | + let cancelled = false; |
| 122 | + const basePath = process.env.NEXT_PUBLIC_BASE_PATH ?? ""; |
| 123 | + |
| 124 | + fetch(`${basePath}/packages/release-info.json`) |
| 125 | + .then((response) => (response.ok ? response.json() : Promise.reject(response.status))) |
| 126 | + .then((payload) => { |
| 127 | + if (!cancelled) { |
| 128 | + setRelease(parseReleaseInfo(payload)); |
| 129 | + } |
| 130 | + }) |
| 131 | + .catch(() => { |
| 132 | + // Keep the fallback: an unreachable or malformed feed must not empty |
| 133 | + // the install commands the page exists to show. |
| 134 | + }); |
| 135 | + |
| 136 | + return () => { |
| 137 | + cancelled = true; |
| 138 | + }; |
| 139 | + }, []); |
| 140 | + |
| 141 | + return release; |
| 142 | +} |
0 commit comments