Files
chrnx 8b5d1d47e1 fix: close three README and version gaps found in review
A README could borrow the site's own classes (card__link::after is a
full-page overlay), so classes are now limited to what shiki writes.
<source srcset> was not rebased, which broke the common dark-mode logo.
Yanked JSR versions were listed as current; the registry hides them and
so does the listing now.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
2026-09-24 10:03:18 -05:00

203 lines
8.0 KiB
JavaScript

// Every registry call lives here, and both the validator and the site build import it. One
// implementation on purpose: if the check that gates a merge and the data that renders a card came
// from two readers, a plugin could pass validation and then render wrong.
/** The four runtimes, in the order every card shows them. */
export const RUNTIMES = Object.freeze(['node', 'deno', 'bun', 'workerd']);
/**
* Where the strictness lives.
*
* In GitHub Actions a package that does not answer is a failure — that is the check, and it runs
* against the live registries on every pull request and every push to main. Locally it is a
* warning and the entry is left out, so the site can still be looked at while one package is
* broken — without rendering data nobody read from a registry.
*/
export const STRICT = process.env.CI === 'true';
/** Compare two semver strings. Enough for CalVer + `-beta.N`; no dependency for a dozen lines. */
function compare(a, b) {
const split = (v) => {
const [core, pre] = v.split('-');
return [core.split('.').map(Number), pre ? pre.split('.') : null];
};
const [an, ap] = split(a);
const [bn, bp] = split(b);
for (let i = 0; i < 3; i += 1) if (an[i] !== bn[i]) return an[i] - bn[i];
// A release outranks its own prerelease: 1.0.0 > 1.0.0-beta.1.
if (!ap && !bp) return 0;
if (!ap) return 1;
if (!bp) return -1;
for (let i = 0; i < Math.max(ap.length, bp.length); i += 1) {
const x = ap[i];
const y = bp[i];
if (x === y) continue;
if (x === undefined) return -1;
if (y === undefined) return 1;
const numeric = /^\d+$/.test(x) && /^\d+$/.test(y);
return numeric ? Number(x) - Number(y) : String(x).localeCompare(String(y));
}
return 0;
}
/**
* The newest of a version list.
*
* Never npm's `dist-tags.latest` and never JSR's `latest`: JSR returns `null` there while a
* package has only prereleases, which is every green-tea package today — verified against
* `@green-tea/core`, which has two published versions and `"latest": null`.
*/
export function newestVersion(versions) {
return byNewest(versions)[0];
}
/** Versions sorted newest first. */
export function byNewest(versions) {
return [...versions].sort(compare).reverse();
}
/**
* ESM only, with no CJS way in at all — dual packages included, because green-tea is ESM end to
* end and a `require` path is what breaks first when the Node floor moves.
*/
export function isEsmOnly({ type, main, exports }) {
if (type !== 'module') return false;
const cjs = (node, key) => {
if (key === 'require') return true;
if (typeof node === 'string') return node.endsWith('.cjs');
if (node && typeof node === 'object') return Object.entries(node).some(([k, v]) => cjs(v, k));
return false;
};
return !cjs(main) && !cjs(exports);
}
/** JSR's version list minus yanked ones — the registry hides those, so the listing must too. */
export function publishedVersions(items) {
return items.filter((item) => !item.yanked).map((item) => item.version);
}
/** npm stores this literal string when a package ships no README. */
export function npmReadme(packument) {
const readme = packument.readme ?? '';
return readme.startsWith('ERROR: No README data found') ? '' : readme;
}
async function getJson(url, what) {
const response = await fetch(url, { headers: { accept: 'application/json' } });
if (!response.ok) throw new Error(`${what}: ${url} answered ${response.status}`);
return response.json();
}
async function getText(url, what) {
const response = await fetch(url);
if (!response.ok) throw new Error(`${what}: ${url} answered ${response.status}`);
return response.text();
}
/** The npm packument, reduced to what validation and the card need. */
export async function readNpm(name) {
const packument = await getJson(`https://registry.npmjs.org/${name.replace('/', '%2f')}`, `npm ${name}`);
const versions = Object.keys(packument.versions ?? {});
const newest = newestVersion(versions);
const version = newest ? packument.versions[newest] : {};
return {
versions,
newest,
description: version.description ?? '',
engines: version.engines ?? {},
// All three maps: a check treats core in `devDependencies` as correct and core in
// `dependencies` or `peerDependencies` as a convention smell worth a warning, so it has to be
// able to tell them apart rather than just miss it.
devDependencies: version.devDependencies ?? {},
dependencies: version.dependencies ?? {},
peerDependencies: version.peerDependencies ?? {},
type: version.type,
main: version.main,
exports: version.exports,
readme: npmReadme(packument),
};
}
/** JSR, reduced the same way. Three calls, because JSR splits what npm returns in one document. */
export async function readJsr(name) {
const [scope, pkg] = name.replace(/^@/, '').split('/');
const base = `https://api.jsr.io/scopes/${scope}/packages/${pkg}`;
const info = await getJson(base, `jsr ${name}`);
const listing = await getJson(`${base}/versions`, `jsr ${name} versions`);
// `items`, not `latestVersion`. See newestVersion.
const versions = publishedVersions(listing.items ?? []);
const newest = newestVersion(versions);
const dependencies = newest ? await getJson(`${base}/versions/${newest}/dependencies`, `jsr ${name} deps`) : [];
const meta = newest ? await getJson(`${base}/versions/${newest}`, `jsr ${name} ${newest}`) : {};
const readme = meta.readmePath
? await getText(`https://jsr.io/${name}/${newest}${meta.readmePath}`, `jsr ${name} readme`)
: '';
return { versions, newest, runtimeCompat: info.runtimeCompat ?? {}, dependencies, readme };
}
/**
* Which of the four runtimes a package declares.
*
* npm says it in `engines`, which also carries non-runtime keys like `npm` — hence the filter
* against RUNTIMES rather than taking every key. JSR says it in `runtimeCompat`, where `false` is
* a declaration that it does *not* run there and must never be read as support.
*/
export function declaredRuntimes({ engines, runtimeCompat }) {
if (runtimeCompat && Object.keys(runtimeCompat).length > 0) {
return RUNTIMES.filter((runtime) => runtimeCompat[runtime] === true);
}
return RUNTIMES.filter((runtime) => engines?.[runtime] !== undefined);
}
/**
* Everything a card and a plugin page need, live.
*
* Under `CI` a registry that does not answer throws and fails the build, which is the rule that
* stops a stale or half-empty listing reaching production. Off CI it warns and returns `null`, and
* the caller leaves the entry out.
*/
export async function readEntry(entry) {
try {
const npm = entry.npm ? await readNpm(entry.npm) : undefined;
if (npm && !isEsmOnly(npm)) {
throw new Error(
`npm ${entry.npm}@${npm.newest} is not ESM only (type: ${npm.type ?? 'unset'}, main: ${npm.main ?? '-'}, exports: ${JSON.stringify(npm.exports ?? null)})`,
);
}
const jsr = entry.jsr ? await readJsr(entry.jsr) : undefined;
// JSR is the recommendation, so it is the source of the page whenever the entry declares it.
const from = jsr ? { registry: 'jsr', pkg: entry.jsr, info: jsr } : { registry: 'npm', pkg: entry.npm, info: npm };
const version = from.info.newest;
return {
registry: from.registry,
pkg: from.pkg,
version,
versions: byNewest(from.info.versions),
// npm's `engines` is the richer of the two — JSR's `runtimeCompat` is empty far more often,
// including on `@green-tea/core` itself — so it wins when both exist.
runtimes: declaredRuntimes({ engines: npm?.engines, runtimeCompat: jsr?.runtimeCompat }),
readme: from.info.readme,
// Where the README's relative links point: the files of this exact published version.
readmeBase:
from.registry === 'jsr'
? `https://jsr.io/${from.pkg}/${version}/`
: `https://cdn.jsdelivr.net/npm/${from.pkg}@${version}/`,
};
} catch (error) {
if (STRICT) throw error;
console.warn(` left out: ${entry.jsr ?? entry.npm} (${error.message})`);
return null;
}
}