feat: accept npm plugins only when they are ESM only

green-tea is ESM end to end and its Node floor is moving up; a CJS or
dual package brings a require path that is the first thing to break.
The check lives in readEntry, so it bites on CI with every other
registry check, and validate.mjs inherits it when it is written.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
chrnx committed 2026-09-24 09:53:32 -05:00
1 parent 4b8d56e8e7
commit 2ce1e0c4e5
3 files changed
+79 -3

No files matched your search

+1 -1
View File
@@ -10,7 +10,7 @@
"dev": "astro dev",
"build": "astro build",
"preview": "astro preview",
"test": "node scripts/deploy.mjs --self-test",
"test": "node scripts/deploy.mjs --self-test && node --test scripts/registry.test.mjs",
"deploy": "npm run build && node scripts/deploy.mjs",
"astro": "astro"
},
+40 -2
View File
@@ -48,7 +48,36 @@ function compare(a, b) {
* `@green-tea/core`, which has two published versions and `"latest": null`.
*/
export function newestVersion(versions) {
return [...versions].sort(compare).pop();
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);
}
/** 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) {
@@ -76,6 +105,10 @@ export async function readNpm(name) {
devDependencies: version.devDependencies ?? {},
dependencies: version.dependencies ?? {},
peerDependencies: version.peerDependencies ?? {},
type: version.type,
main: version.main,
exports: version.exports,
readme: npmReadme(packument),
};
}
@@ -118,6 +151,11 @@ export function declaredRuntimes({ engines, runtimeCompat }) {
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;
return {
@@ -129,7 +167,7 @@ export async function readEntry(entry) {
};
} catch (error) {
if (STRICT) throw error;
console.warn(` left out, registry did not answer: ${entry.jsr ?? entry.npm} (${error.message})`);
console.warn(` left out: ${entry.jsr ?? entry.npm} (${error.message})`);
return null;
}
+38
View File
@@ -0,0 +1,38 @@
// Pure pieces of the registry reader. Network calls are not tested here; `CI=true npm run build`
// exercises them against the live registries.
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { isEsmOnly, byNewest, npmReadme } from './registry.mjs';
test('isEsmOnly: type module with ESM exports passes', () => {
assert.equal(isEsmOnly({ type: 'module', exports: { '.': { import: './dist/index.js', types: './dist/index.d.ts' } } }), true);
});
test('isEsmOnly: no type module is CJS', () => {
assert.equal(isEsmOnly({ main: './index.js' }), false);
});
test('isEsmOnly: dual package is rejected', () => {
assert.equal(isEsmOnly({ type: 'module', exports: { '.': { import: './index.js', require: './index.cjs' } } }), false);
});
test('isEsmOnly: a .cjs anywhere in exports or main is rejected', () => {
assert.equal(isEsmOnly({ type: 'module', exports: { './legacy': './legacy.cjs' } }), false);
assert.equal(isEsmOnly({ type: 'module', main: './index.cjs' }), false);
});
test('byNewest: releases outrank their prereleases, betas order numerically', () => {
assert.deepEqual(byNewest(['26.9.0-beta.2', '26.9.0', '26.9.0-beta.10', '26.8.1']), [
'26.9.0',
'26.9.0-beta.10',
'26.9.0-beta.2',
'26.8.1',
]);
});
test('npmReadme: npm placeholder means no README', () => {
assert.equal(npmReadme({ readme: 'ERROR: No README data found!' }), '');
assert.equal(npmReadme({}), '');
assert.equal(npmReadme({ readme: '# hi' }), '# hi');
});