Skip to content

Commit 0ca16ce

Browse files
authored
use registry.npmjs.org (#1697)
1 parent e08f0b9 commit 0ca16ce

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

src/npm.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {mkdir, readFile, readdir, writeFile} from "node:fs/promises";
33
import {dirname, extname, join} from "node:path/posix";
44
import type {CallExpression} from "acorn";
55
import {simple} from "acorn-walk";
6-
import {rsort, satisfies} from "semver";
6+
import {maxSatisfying, rsort, satisfies, validRange} from "semver";
77
import {isEnoent} from "./error.js";
88
import type {ExportNode, ImportNode, ImportReference} from "./javascript/imports.js";
99
import {isImportMetaResolve, parseImports} from "./javascript/imports.js";
@@ -221,15 +221,18 @@ async function resolveNpmVersion(root: string, {name, range}: NpmSpecifier): Pro
221221
const cache = await getNpmVersionCache(root);
222222
const versions = cache.get(name);
223223
if (versions) for (const version of versions) if (!range || satisfies(version, range)) return version;
224-
const href = `https://data.jsdelivr.com/v1/packages/npm/${name}/resolved${range ? `?specifier=${range}` : ""}`;
224+
if (range === undefined) range = "latest";
225+
const disttag = validRange(range) ? null : range;
226+
const href = `https://registry.npmjs.org/${name}${disttag ? `/${disttag}` : ""}`;
225227
let promise = npmVersionRequests.get(href);
226228
if (promise) return promise; // coalesce concurrent requests
227229
promise = (async function () {
228230
const input = formatNpmSpecifier({name, range});
229231
process.stdout.write(`npm:${input} ${faint("→")} `);
230-
const response = await fetch(href);
232+
const response = await fetch(href, {...(!disttag && {headers: {Accept: "application/vnd.npm.install-v1+json"}})});
231233
if (!response.ok) throw new Error(`unable to fetch: ${href}`);
232-
const {version} = await response.json();
234+
const body = await response.json();
235+
const version = disttag ? body.version : maxSatisfying(Object.keys(body.versions), range);
233236
if (!version) throw new Error(`unable to resolve version: ${input}`);
234237
const output = formatNpmSpecifier({name, range: version});
235238
process.stdout.write(`npm:${output}\n`);

test/mocks/jsdelivr.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,17 @@ export function mockJsDelivr() {
3333
mockAgent();
3434
before(async () => {
3535
const agent = getCurrentAgent();
36-
const dataClient = agent.get("https://data.jsdelivr.com");
36+
const registryClient = agent.get("https://registry.npmjs.org");
3737
const cdnClient = agent.get("https://cdn.jsdelivr.net");
3838
for (const [name, pkg] of packages) {
39-
dataClient
40-
.intercept({path: new RegExp(`^/v1/packages/npm/${name}/resolved(\\?specifier=|$)`), method: "GET"})
39+
registryClient
40+
.intercept({path: `/${name}/latest`, method: "GET"})
4141
.reply(200, {version: pkg.version}, {headers: {"content-type": "application/json; charset=utf-8"}})
4242
.persist();
43+
registryClient
44+
.intercept({path: `/${name}`, method: "GET"})
45+
.reply(200, {versions: {[pkg.version]: {}}}, {headers: {"content-type": "application/json; charset=utf-8"}})
46+
.persist();
4347
cdnClient
4448
.intercept({path: `/npm/${name}@${pkg.version}/package.json`, method: "GET"})
4549
.reply(200, pkg, {headers: {"content-type": "application/json; charset=utf-8"}})

0 commit comments

Comments
 (0)