8000 [AssetMapper] Using ?specifier=* does not match unstable packages on jsdelivr by weaverryan · Pull Request #52119 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[AssetMapper] Using ?specifier=* does not match unstable packages on jsdelivr #52119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

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

public function __construct(
HttpClientInterface $httpClient = null,
private readonly string $versionUrlPattern = self::URL_PATTERN_VERSION,
private readonly string $distUrlPattern = self::URL_PATTERN_DIST,
private readonly string $distUrlCssPattern = self::URL_PATTERN_DIST_CSS
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated: but it really doesn't make sense to have these URLs configurable. We are hitting very exact URLs.

) {
$this->httpClient = $httpClient ?? HttpClient::create();
}
Expand All @@ -49,7 +46,6 @@ public function resolvePackages(array $packagesToRequire): array
$requiredPackages = [];
foreach ($packagesToRequire as $options) {
$packageSpecifier = trim($options->packageModuleSpecifier, '/');
$constraint = $options->versionConstraint ?? '*';

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

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

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

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

$version = $response->toArray()['version'];
$pattern = str_ends_with($filePath, '.css') ? $this->distUrlCssPattern : $this->distUrlPattern;
if (null === $version) {
throw new RuntimeException(sprintf('Unable to find the latest version for package "%s" - try specifying the version manually.', $packageName));
}

$pattern = str_ends_with($filePath, '.css') ? self::URL_PATTERN_DIST_CSS : self::URL_PATTERN_DIST;
$requiredPackages[$i][1] = $this->httpClient->request('GET', sprintf($pattern, $packageName, $version, $filePath));
$requiredPackages[$i][4] = $version;

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

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

$responses[$package] = $this->httpClient->request('GET', $url);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public static function provideResolvePackagesTests(): iterable
'packages' => [new PackageRequireOptions('lodash')],
'expectedRequests' => [
[
'url' => '/v1/packages/npm/lodash/resolved?specifier=%2A',
'url' => '/v1/packages/npm/lodash/resolved',
'response' => ['body' => ['version' => '1.2.3']],
],
[
Expand Down Expand Up @@ -196,7 +196,7 @@ public static function provideResolvePackagesTests(): iterable
'packages' => [new PackageRequireOptions('bootstrap/dist/css/bootstrap.min.css')],
'expectedRequests' => [
[
'url' => '/v1/packages/npm/bootstrap/resolved?specifier=%2A',
'url' => '/v1/packages/npm/bootstrap/resolved',
'response' => ['body' => ['version' => '3.3.0']],
],
[
Expand Down
0