Skip to content

Commit 97bbd4e

Browse files
committed
bug symfony#52119 [AssetMapper] Using ?specifier=* does not match unstable packages on jsdelivr (weaverryan)
This PR was merged into the 6.4 branch. Discussion ---------- [AssetMapper] Using ?specifier=* does not match unstable packages on jsdelivr | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix symfony#52106 | License | MIT To find which version of a package to grab, we use this API endpoint - https://data.jsdelivr.com/v1/packages/npm/`@tabler`/core/resolved. If a version constraint is specified (e.g. `importmap:require 'bootstrap@^5'`), we add a `?specifier=5`. If no version is specified, previously we added `?specifier=*`. However, this seems to ignore beta releases (returning `null` for ``@table`/core`, whose only has a 1.0 beta). Omitting `?specifier` seems to grab the latest, no matter what it is. Also, if for some reason, we still can't find a `version`, we know blow up with a much clearer exception. Cheers! Commits ------- 370f6dc [AssetMapper] Using ?specifier=* does not match unstable packages on jsdelivr
2 parents 3f2d9d7 + 370f6dc commit 97bbd4e

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

src/Symfony/Component/AssetMapper/ImportMap/Resolver/JsDelivrEsmResolver.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
final class JsDelivrEsmResolver implements PackageResolverInterface
2323
{
24-
public const URL_PATTERN_VERSION = 'https://data.jsdelivr.com/v1/packages/npm/%s/resolved?specifier=%s';
24+
public const URL_PATTERN_VERSION = 'https://data.jsdelivr.com/v1/packages/npm/%s/resolved';
2525
public const URL_PATTERN_DIST_CSS = 'https://cdn.jsdelivr.net/npm/%s@%s%s';
2626
public const URL_PATTERN_DIST = self::URL_PATTERN_DIST_CSS.'/+esm';
2727
public const URL_PATTERN_ENTRYPOINT = 'https://data.jsdelivr.com/v1/packages/npm/%s@%s/entrypoints';
@@ -32,9 +32,6 @@ final class JsDelivrEsmResolver implements PackageResolverInterface
3232

3333
public function __construct(
3434
HttpClientInterface $httpClient = null,
35-
private readonly string $versionUrlPattern = self::URL_PATTERN_VERSION,
36-
private readonly string $distUrlPattern = self::URL_PATTERN_DIST,
37-
private readonly string $distUrlCssPattern = self::URL_PATTERN_DIST_CSS
3835
) {
3936
$this->httpClient = $httpClient ?? HttpClient::create();
4037
}
@@ -49,7 +46,6 @@ public function resolvePackages(array $packagesToRequire): array
4946
$requiredPackages = [];
5047
foreach ($packagesToRequire as $options) {
5148
$packageSpecifier = trim($options->packageModuleSpecifier, '/');
52-
$constraint = $options->versionConstraint ?? '*';
5349

5450
// avoid resolving the same package twice
5551
if (isset($resolvedPackages[$packageSpecifier])) {
@@ -58,7 +54,11 @@ public function resolvePackages(array $packagesToRequire): array
5854

5955
[$packageName, $filePath] = ImportMapEntry::splitPackageNameAndFilePath($packageSpecifier);
6056

61-
$response = $this->httpClient->request('GET', sprintf($this->versionUrlPattern, $packageName, urlencode($constraint)));
57+
$versionUrl = sprintf(self::URL_PATTERN_VERSION, $packageName);
58+
if (null !== $options->versionConstraint) {
59+
$versionUrl .= '?specifier='.urlencode($options->versionConstraint);
60+
}
61+
$response = $this->httpClient->request('GET', $versionUrl);
6262
$requiredPackages[] = [$options, $response, $packageName, $filePath, /* resolved version */ null];
6363
}
6464

@@ -72,7 +72,11 @@ public function resolvePackages(array $packagesToRequire): array
7272
}
7373

7474
$version = $response->toArray()['version'];
75-
$pattern = str_ends_with($filePath, '.css') ? $this->distUrlCssPattern : $this->distUrlPattern;
75+
if (null === $version) {
76+
throw new RuntimeException(sprintf('Unable to find the latest version for package "%s" - try specifying the version manually.', $packageName));
77+
}
78+
79+
$pattern = str_ends_with($filePath, '.css') ? self::URL_PATTERN_DIST_CSS : self::URL_PATTERN_DIST;
7680
$requiredPackages[$i][1] = $this->httpClient->request('GET', sprintf($pattern, $packageName, $version, $filePath));
7781
$requiredPackages[$i][4] = $version;
7882

@@ -163,7 +167,7 @@ public function downloadPackages(array $importMapEntries, callable $progressCall
163167
throw new \InvalidArgumentException(sprintf('The entry "%s" is not a remote package.', $entry->importName));
164168
}
165169

166-
$pattern = ImportMapType::CSS === $entry->type ? $this->distUrlCssPattern : $this->distUrlPattern;
170+
$pattern = ImportMapType::CSS === $entry->type ? self::URL_PATTERN_DIST_CSS : self::URL_PATTERN_DIST;
167171
$url = sprintf($pattern, $entry->getPackageName(), $entry->version, $entry->getPackagePathString());
168172

169173
$responses[$package] = $this->httpClient->request('GET', $url);

src/Symfony/Component/AssetMapper/Tests/ImportMap/Resolver/JsDelivrEsmResolverTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public static function provideResolvePackagesTests(): iterable
6161
'packages' => [new PackageRequireOptions('lodash')],
6262
'expectedRequests' => [
6363
[
64-
'url' => '/v1/packages/npm/lodash/resolved?specifier=%2A',
64+
'url' => '/v1/packages/npm/lodash/resolved',
6565
'response' => ['body' => ['version' => '1.2.3']],
6666
],
6767
[
@@ -196,7 +196,7 @@ public static function provideResolvePackagesTests(): iterable
196196
'packages' => [new PackageRequireOptions('bootstrap/dist/css/bootstrap.min.css')],
197197
'expectedRequests' => [
198198
[
199-
'url' => '/v1/packages/npm/bootstrap/resolved?specifier=%2A',
199+
'url' => '/v1/packages/npm/bootstrap/resolved',
200200
'response' => ['body' => ['version' => '3.3.0']],
201201
],
202202
[

0 commit comments

Comments
 (0)