Skip to content

Commit

Permalink
Add timeout to manifest loading (closes #11) (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
jiripudil authored Jun 4, 2021
1 parent 11d1f10 commit def8991
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 6 deletions.
13 changes: 13 additions & 0 deletions .docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,19 @@ webpack:
You can also implement your own mapper, simply extend `Contributte\Webpack\Manifest\ManifestMapper` and implement its `map()` method. It takes the parsed JSON content of the manifest file and is expected to return a flat array mapping asset names to file names.



#### Manifest loading timeout

You can specify a timeout for manifest loading from webpack-dev-server. The timeout defaults to 1 second.

```neon
webpack:
manifest:
name: manifest.json
timeout: 0.5
```


### Debugger

In development environment, this package registers its own debug bar panel into Tracy, giving you the overview of
Expand Down
6 changes: 4 additions & 2 deletions src/DI/WebpackExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public function getConfigSchema(): Schema
'name' => Expect::string()->nullable(),
'optimize' => Expect::bool(!$this->debugMode && (!$this->consoleMode || (bool) \getenv('CONTRIBUTTE_WEBPACK_OPTIMIZE_MANIFEST'))),
'mapper' => Expect::anyOf(Expect::string(), Expect::type(Statement::class))->default(WebpackManifestPluginMapper::class),
'timeout' => Expect::anyOf(Expect::float(), Expect::int())->default(1),
])->castTo('array'),
])->castTo('array');
}
Expand Down Expand Up @@ -151,7 +152,8 @@ private function setupAssetResolver(array $config): ServiceDefinition
if (!$config['manifest']['optimize']) {
$loader = $builder->addDefinition($this->prefix('manifestLoader'))
->setFactory(ManifestLoader::class, [
1 => new Statement($config['manifest']['mapper']),
'manifestMapper' => new Statement($config['manifest']['mapper']),
'timeout' => $config['manifest']['timeout'],
])
->setAutowired(false);

Expand All @@ -165,7 +167,7 @@ private function setupAssetResolver(array $config): ServiceDefinition
$mapperInstance = new $config['manifest']['mapper']();

$directoryProviderInstance = new BuildDirectoryProvider($config['build']['directory'], $devServerInstance);
$loaderInstance = new ManifestLoader($directoryProviderInstance, $mapperInstance);
$loaderInstance = new ManifestLoader($directoryProviderInstance, $mapperInstance, $config['manifest']['timeout']);
$manifestCache = $loaderInstance->loadManifest($config['manifest']['name']);

$assetResolver->setFactory(AssetNameResolver\StaticAssetNameResolver::class, [$manifestCache]);
Expand Down
19 changes: 16 additions & 3 deletions src/Manifest/ManifestLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,16 @@ final class ManifestLoader

private ManifestMapper $manifestMapper;

public function __construct(BuildDirectoryProvider $directoryProvider, ManifestMapper $manifestMapper)
{
private float $timeout;

public function __construct(
BuildDirectoryProvider $directoryProvider,
ManifestMapper $manifestMapper,
float $timeout
) {
$this->directoryProvider = $directoryProvider;
$this->manifestMapper = $manifestMapper;
$this->timeout = $timeout;
}

/**
Expand All @@ -39,9 +45,16 @@ public function loadManifest(string $fileName): array
$manifest = false;
} else {
\curl_setopt_array($ch, [
\CURLOPT_CUSTOMREQUEST => 'GET',
\CURLOPT_PROTOCOLS => \CURLPROTO_HTTP | \CURLPROTO_HTTPS,

\CURLOPT_RETURNTRANSFER => true,
\CURLOPT_FAILONERROR => true,

// setup timeout; this requires NOSIGNAL for values below 1s
\CURLOPT_TIMEOUT_MS => $this->timeout * 1000,
\CURLOPT_NOSIGNAL => $this->timeout < 1 && \PHP_OS_FAMILY !== 'Windows',

// allow self-signed certificates
\CURLOPT_SSL_VERIFYHOST => 0,
\CURLOPT_SSL_VERIFYPEER => false,
Expand All @@ -61,7 +74,7 @@ public function loadManifest(string $fileName): array
throw new CannotLoadManifestException(\sprintf(
"Manifest file '%s' could not be loaded: %s",
$path,
$errorMessage ?? (\error_get_last()['message'] ?? 'unknown error')
$errorMessage ?? \error_get_last()['message'] ?? 'unknown error',
));
}

Expand Down
2 changes: 2 additions & 0 deletions tests/AssetNameResolver/ManifestAssetNameResolverTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ final class ManifestAssetNameResolverTest extends TestCase
$manifestLoader = new ManifestLoader(
createBuildDirectoryProvider(__DIR__),
new WebpackManifestPluginMapper(),
1,
);

$resolver = new ManifestAssetNameResolver('manifest.json', $manifestLoader);
Expand All @@ -40,6 +41,7 @@ final class ManifestAssetNameResolverTest extends TestCase
// parent directory doesn't contain manifest.json => throws CannotLoadManifestException
createBuildDirectoryProvider(__DIR__ . '/Webpack'),
new WebpackManifestPluginMapper(),
1,
);

$resolver = new ManifestAssetNameResolver('manifest.json', $manifestLoader);
Expand Down
2 changes: 1 addition & 1 deletion tests/Manifest/ManifestLoaderTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ final class ManifestLoaderTest extends TestCase
}
};

$manifestLoader = new ManifestLoader($buildDirProvider, $manifestMapper);
$manifestLoader = new ManifestLoader($buildDirProvider, $manifestMapper, 1);

Assert::same(__DIR__ . '/manifest.json', $manifestLoader->getManifestPath('manifest.json'));
Assert::same(['asset.js' => 'mapped.asset.js'], $manifestLoader->loadManifest('manifest.json'));
Expand Down

0 comments on commit def8991

Please sign in to comment.