@@ -3,7 +3,7 @@ import {mkdir, readFile, readdir, writeFile} from "node:fs/promises";
3
3
import { dirname , extname , join } from "node:path/posix" ;
4
4
import type { CallExpression } from "acorn" ;
5
5
import { simple } from "acorn-walk" ;
6
- import { rsort , satisfies } from "semver" ;
6
+ import { maxSatisfying , rsort , satisfies , validRange } from "semver" ;
7
7
import { isEnoent } from "./error.js" ;
8
8
import type { ExportNode , ImportNode , ImportReference } from "./javascript/imports.js" ;
9
9
import { isImportMetaResolve , parseImports } from "./javascript/imports.js" ;
@@ -221,15 +221,18 @@ async function resolveNpmVersion(root: string, {name, range}: NpmSpecifier): Pro
221
221
const cache = await getNpmVersionCache ( root ) ;
222
222
const versions = cache . get ( name ) ;
223
223
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 } ` : "" } ` ;
225
227
let promise = npmVersionRequests . get ( href ) ;
226
228
if ( promise ) return promise ; // coalesce concurrent requests
227
229
promise = ( async function ( ) {
228
230
const input = formatNpmSpecifier ( { name, range} ) ;
229
231
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" } } ) } ) ;
231
233
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 ) ;
233
236
if ( ! version ) throw new Error ( `unable to resolve version: ${ input } ` ) ;
234
237
const output = formatNpmSpecifier ( { name, range : version } ) ;
235
238
process . stdout . write ( `npm:${ output } \n` ) ;
0 commit comments