Skip to content
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
57 changes: 57 additions & 0 deletions src/Symfony/Component/AssetMapper/ImportMap/BatchHttpClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\AssetMapper\ImportMap;

use Symfony\Component\HttpClient\DecoratorTrait;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;

/**
* @internal
*/
class BatchHttpClient implements HttpClientInterface
{
use DecoratorTrait;

private const BATCH_SIZE = 250;

private \WeakMap $pendingRequests;

public function request(string $method, string $url, array $options = []): ResponseInterface
{
$this->pendingRequests ??= new \WeakMap();
$pendingRequests = [];

foreach ($this->pendingRequests as $response => $_) {
if ($response->getInfo('http_code')) {
$this->pendingRequests->offsetUnset($response);
} else {
$pendingRequests[] = $response;
}
}

if (\count($pendingRequests) >= self::BATCH_SIZE) {
foreach ($this->client->stream($pendingRequests) as $response => $chunk) {

Check failure on line 43 in src/Symfony/Component/AssetMapper/ImportMap/BatchHttpClient.php

View workflow job for this annotation

GitHub Actions / Psalm

InvalidArgument

src/Symfony/Component/AssetMapper/ImportMap/BatchHttpClient.php:43:44: InvalidArgument: Argument 1 of Symfony\Contracts\HttpClient\HttpClientInterface::stream expects Symfony\Contracts\HttpClient\ResponseInterface|iterable<array-key, Symfony\Contracts\HttpClient\ResponseInterface>, but list<(TKey:WeakMap as object)|object> provided (see https://psalm.dev/004)

Check failure on line 43 in src/Symfony/Component/AssetMapper/ImportMap/BatchHttpClient.php

View workflow job for this annotation

GitHub Actions / Psalm

InvalidArgument

src/Symfony/Component/AssetMapper/ImportMap/BatchHttpClient.php:43:44: InvalidArgument: Argument 1 of Symfony\Contracts\HttpClient\HttpClientInterface::stream expects Symfony\Contracts\HttpClient\ResponseInterface|iterable<array-key, Symfony\Contracts\HttpClient\ResponseInterface>, but list<(TKey:WeakMap as object)|object> provided (see https://psalm.dev/004)
if (!$chunk->isTimeout() && $chunk->isFirst()) {
$response->getStatusCode(); // ignore 3/4/5xx
$this->pendingRequests->offsetUnset($response);
break;
}
}
}

$response = $this->client->request($method, $url, $options);
$this->pendingRequests[$response] = true;

return $response;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,20 @@

namespace Symfony\Component\AssetMapper\ImportMap;

use Symfony\Component\HttpClient\HttpClient;
use Symfony\Contracts\HttpClient\HttpClientInterface;

class ImportMapUpdateChecker
{
private const URL_PACKAGE_METADATA = 'https://registry.npmjs.org/%s';

private readonly HttpClientInterface $httpClient;

public function __construct(
private readonly ImportMapConfigReader $importMapConfigReader,
private readonly HttpClientInterface $httpClient,
?HttpClientInterface $httpClient = null,
) {
$this->httpClient = new BatchHttpClient($httpClient ?? HttpClient::create());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ class ImportMapVersionChecker
{
private const PACKAGE_METADATA_PATTERN = 'https://registry.npmjs.org/%package%/%version%';

private HttpClientInterface $httpClient;
private readonly HttpClientInterface $httpClient;

public function __construct(
private ImportMapConfigReader $importMapConfigReader,
private RemotePackageDownloader $packageDownloader,
?HttpClientInterface $httpClient = null,
) {
$this->httpClient = $httpClient ?? HttpClient::create();
$this->httpClient = new BatchHttpClient($httpClient ?? HttpClient::create());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Symfony\Component\AssetMapper\Compiler\CssAssetUrlCompiler;
use Symfony\Component\AssetMapper\Exception\RuntimeException;
use Symfony\Component\AssetMapper\ImportMap\BatchHttpClient;
use Symfony\Component\AssetMapper\ImportMap\ImportMapEntry;
use Symfony\Component\AssetMapper\ImportMap\ImportMapType;
use Symfony\Component\AssetMapper\ImportMap\PackageRequireOptions;
Expand All @@ -32,12 +33,12 @@ final class JsDelivrEsmResolver implements PackageResolverInterface

private const ES_MODULE_SHIMS = 'es-module-shims';

private HttpClientInterface $httpClient;
private readonly HttpClientInterface $httpClient;

public function __construct(
?HttpClientInterface $httpClient = null,
) {
$this->httpClient = $httpClient ?? HttpClient::create();
$this->httpClient = new BatchHttpClient($httpClient ?? HttpClient::create());
Copy link
Member

Choose a reason for hiding this comment

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

🤝

}

public function resolvePackages(array $packagesToRequire): array
Expand Down
Loading