2018-03-27 00:44:14 -05:00
|
|
|
<?php
|
|
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2018-03-27 00:44:14 -05:00
|
|
|
namespace Pterodactyl\Services\Helpers;
|
|
|
|
|
|
2022-10-14 10:59:20 -06:00
|
|
|
use Illuminate\Contracts\Filesystem\Filesystem;
|
2026-10-05 18:10:18 -04:00
|
|
|
use Illuminate\Filesystem\FilesystemManager;
|
2022-10-31 12:29:10 -04:00
|
|
|
use Pterodactyl\Exceptions\ManifestDoesNotExistException;
|
2026-10-11 20:11:13 -04:00
|
|
|
use UnexpectedValueException;
|
2018-03-27 00:44:14 -05:00
|
|
|
|
|
|
|
|
class AssetHashService
|
|
|
|
|
{
|
2026-10-05 18:10:18 -04:00
|
|
|
public const string MANIFEST_PATH = 'assets/manifest.json';
|
|
|
|
|
|
|
|
|
|
public const string MAIN_ENTRY = 'resources/scripts/index.tsx';
|
|
|
|
|
|
2026-10-12 21:24:43 -04:00
|
|
|
/** The panel stylesheet, a Vite input of its own that a theme's stylesheet is linked after. */
|
2026-10-12 07:47:42 -04:00
|
|
|
public const string STYLESHEET_ENTRY = 'resources/scripts/assets/tailwind.css';
|
|
|
|
|
|
2018-03-27 00:44:14 -05:00
|
|
|
/**
|
2026-10-05 18:10:18 -04:00
|
|
|
* Bare module specifiers shared with runtime-loaded extension bundles, mapped to
|
|
|
|
|
* the source keys of the runtime entries in Vite's native manifest. The
|
|
|
|
|
* import map built from these lets extension bundles resolve "react" etc. to the
|
|
|
|
|
* exact module instances the panel bundle itself uses.
|
2018-03-27 00:44:14 -05:00
|
|
|
*/
|
2026-10-05 18:10:18 -04:00
|
|
|
public const array EXTENSION_RUNTIME_IMPORTS = [
|
|
|
|
|
'react' => 'resources/scripts/ext-runtime/react.ts',
|
|
|
|
|
'react/jsx-runtime' => 'resources/scripts/ext-runtime/react-jsx-runtime.ts',
|
|
|
|
|
'react-dom' => 'resources/scripts/ext-runtime/react-dom.ts',
|
|
|
|
|
'react-dom/client' => 'resources/scripts/ext-runtime/react-dom-client.ts',
|
|
|
|
|
'@tanstack/react-query' => 'resources/scripts/ext-runtime/tanstack-react-query.ts',
|
|
|
|
|
'@pterodactyl/sdk' => 'resources/scripts/sdk/index.ts',
|
|
|
|
|
'@pterodactyl/sdk/api' => 'resources/scripts/sdk/api.ts',
|
2026-10-12 07:54:37 -04:00
|
|
|
'@pterodactyl/sdk/theme' => 'resources/scripts/sdk/theme.ts',
|
2026-10-05 18:10:18 -04:00
|
|
|
];
|
2018-03-27 00:44:14 -05:00
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
/** @var ViteManifest|null */
|
|
|
|
|
protected static ?array $manifest = null;
|
2018-03-27 00:44:14 -05:00
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
private readonly Filesystem $filesystem;
|
2018-03-27 00:44:14 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* AssetHashService constructor.
|
2026-10-05 18:10:18 -04:00
|
|
|
*
|
|
|
|
|
* @param string|null $publicPath Directory holding the built assets; defaults to the public path.
|
2018-03-27 00:44:14 -05:00
|
|
|
*/
|
2026-10-05 18:10:18 -04:00
|
|
|
public function __construct(FilesystemManager $filesystem, ?string $publicPath = null)
|
2018-03-27 00:44:14 -05:00
|
|
|
{
|
2026-10-05 18:10:18 -04:00
|
|
|
$this->filesystem = $filesystem->createLocalDriver(['root' => $publicPath ?? public_path()]);
|
2018-03-27 00:44:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Modify a URL to append the asset hash.
|
|
|
|
|
*/
|
2018-03-31 15:52:11 -05:00
|
|
|
public function url(string $resource): string
|
2018-03-27 00:44:14 -05:00
|
|
|
{
|
2026-10-05 18:10:18 -04:00
|
|
|
$entry = $this->entry($resource);
|
2018-03-27 00:44:14 -05:00
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
return $entry === null ? $resource : $this->assetUrl($entry['file']);
|
2018-06-03 19:35:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return the data integrity hash for a resource.
|
|
|
|
|
*/
|
|
|
|
|
public function integrity(string $resource): string
|
|
|
|
|
{
|
2026-10-05 18:10:18 -04:00
|
|
|
return $this->entry($resource)['integrity'] ?? '';
|
2018-03-27 00:44:14 -05:00
|
|
|
}
|
|
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
/**
|
|
|
|
|
* Return stylesheet tags for CSS emitted with a JS entry.
|
|
|
|
|
*/
|
|
|
|
|
public function cssImports(string $resource): string
|
|
|
|
|
{
|
|
|
|
|
$entry = $this->entry($resource);
|
|
|
|
|
if ($entry === null) {
|
|
|
|
|
return '';
|
2020-09-01 19:37:05 -07:00
|
|
|
}
|
|
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
$tags = [];
|
|
|
|
|
foreach ($entry['css'] ?? [] as $file) {
|
|
|
|
|
if ($file === '') {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$tags[] = $this->stylesheetTag(
|
|
|
|
|
$this->assetUrl($file),
|
|
|
|
|
$entry['cssIntegrity'][$file] ?? ''
|
|
|
|
|
);
|
2020-09-01 19:37:05 -07:00
|
|
|
}
|
|
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
return implode('', $tags);
|
2018-03-27 00:44:14 -05:00
|
|
|
}
|
|
|
|
|
|
2026-10-12 07:47:42 -04:00
|
|
|
/**
|
|
|
|
|
* Return the stylesheet tag for a CSS entry of the build.
|
|
|
|
|
*/
|
|
|
|
|
public function css(string $resource): string
|
|
|
|
|
{
|
|
|
|
|
$entry = $this->entry($resource);
|
|
|
|
|
|
|
|
|
|
return $entry === null ? '' : $this->stylesheetTag($this->assetUrl($entry['file']), $entry['integrity'] ?? '');
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-27 00:44:14 -05:00
|
|
|
/**
|
2018-03-31 15:52:11 -05:00
|
|
|
* Return a built JS import using the provided URL.
|
2018-03-27 00:44:14 -05:00
|
|
|
*/
|
2018-03-31 15:52:11 -05:00
|
|
|
public function js(string $resource): string
|
2018-03-27 00:44:14 -05:00
|
|
|
{
|
2020-09-01 19:37:05 -07:00
|
|
|
$attributes = [
|
|
|
|
|
'src' => $this->url($resource),
|
|
|
|
|
'crossorigin' => 'anonymous',
|
2026-10-05 18:10:18 -04:00
|
|
|
// Vite emits ES modules (required for code-splitting / dynamic import),
|
|
|
|
|
// so the entry script must be loaded as a module.
|
|
|
|
|
'type' => 'module',
|
2020-09-01 19:37:05 -07:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
if (config('pterodactyl.assets.use_hash')) {
|
|
|
|
|
$attributes['integrity'] = $this->integrity($resource);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$output = '<script';
|
|
|
|
|
foreach ($attributes as $key => $value) {
|
|
|
|
|
$output .= " $key=\"$value\"";
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
return $output.'></script>';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Build the <script type="importmap"> tag resolving the shared extension-runtime
|
|
|
|
|
* specifiers. Must be emitted before the main module script. Returns an empty
|
|
|
|
|
* string when the manifest predates the extension runtime (older builds).
|
|
|
|
|
*/
|
|
|
|
|
public function importMap(): string
|
|
|
|
|
{
|
|
|
|
|
$imports = [];
|
|
|
|
|
$integrity = [];
|
|
|
|
|
|
|
|
|
|
foreach (self::EXTENSION_RUNTIME_IMPORTS as $specifier => $source) {
|
|
|
|
|
$entry = $this->entry($source);
|
|
|
|
|
if ($entry === null) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$url = $this->assetUrl($entry['file']);
|
|
|
|
|
$imports[$specifier] = $url;
|
|
|
|
|
if (! empty($entry['integrity'])) {
|
|
|
|
|
$integrity[$url] = $entry['integrity'];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($imports === []) {
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$map = ['imports' => $imports];
|
|
|
|
|
if (config('pterodactyl.assets.use_hash') && $integrity !== []) {
|
|
|
|
|
$map['integrity'] = $integrity;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return '<script type="importmap">'.json_encode($map, JSON_UNESCAPED_SLASHES).'</script>';
|
2018-03-27 00:44:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2018-03-31 15:52:11 -05:00
|
|
|
* Get the asset manifest and store it in the cache for quicker lookups.
|
2026-10-05 18:10:18 -04:00
|
|
|
*
|
|
|
|
|
* @return ViteManifest Vite's manifest, keyed by source path.
|
2018-03-27 00:44:14 -05:00
|
|
|
*/
|
2018-03-31 15:52:11 -05:00
|
|
|
protected function manifest(): array
|
2018-03-27 00:44:14 -05:00
|
|
|
{
|
2022-10-14 11:00:10 -06:00
|
|
|
if (static::$manifest === null) {
|
2026-10-05 18:10:18 -04:00
|
|
|
$contents = $this->filesystem->get(self::MANIFEST_PATH);
|
|
|
|
|
throw_if($contents === null, ManifestDoesNotExistException::class);
|
|
|
|
|
|
|
|
|
|
self::$manifest = $this->parseManifest($contents);
|
2022-10-14 11:00:10 -06:00
|
|
|
}
|
|
|
|
|
|
2022-10-31 12:29:10 -04:00
|
|
|
$manifest = static::$manifest;
|
2026-10-05 18:10:18 -04:00
|
|
|
throw_if($manifest === null, ManifestDoesNotExistException::class);
|
|
|
|
|
|
|
|
|
|
return $manifest;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function stylesheetTag(string $href, string $integrity = ''): string
|
|
|
|
|
{
|
|
|
|
|
$attributes = [
|
|
|
|
|
'href' => $href,
|
|
|
|
|
'rel' => 'stylesheet',
|
|
|
|
|
'crossorigin' => 'anonymous',
|
|
|
|
|
'referrerpolicy' => 'no-referrer',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
if (config('pterodactyl.assets.use_hash') && $integrity !== '') {
|
|
|
|
|
$attributes['integrity'] = $integrity;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$output = '<link';
|
|
|
|
|
foreach ($attributes as $key => $value) {
|
|
|
|
|
$output .= " $key=\"$value\"";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $output.'>';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @return ViteManifestEntry|null */
|
|
|
|
|
private function entry(string $resource): ?array
|
|
|
|
|
{
|
|
|
|
|
$key = $resource === 'main.js' ? self::MAIN_ENTRY : $resource;
|
|
|
|
|
|
|
|
|
|
return $this->manifest()[$key] ?? null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function assetUrl(string $file): string
|
|
|
|
|
{
|
|
|
|
|
return '/assets/'.mb_ltrim($file, '/');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @return ViteManifest */
|
|
|
|
|
private function parseManifest(string $contents): array
|
|
|
|
|
{
|
2026-10-11 20:11:13 -04:00
|
|
|
$decoded = json_decode($contents, true, flags: JSON_THROW_ON_ERROR);
|
|
|
|
|
throw_unless(is_array($decoded), UnexpectedValueException::class, 'The asset manifest must be a JSON object.');
|
|
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
$manifest = [];
|
|
|
|
|
|
|
|
|
|
foreach ($decoded as $source => $value) {
|
|
|
|
|
if (! is_string($source) || ! is_array($value) || ! is_string($value['file'] ?? null)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$entry = ['file' => $value['file']];
|
|
|
|
|
if (is_string($value['integrity'] ?? null)) {
|
|
|
|
|
$entry['integrity'] = $value['integrity'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$css = $value['css'] ?? null;
|
|
|
|
|
if (is_array($css) && array_is_list($css)) {
|
|
|
|
|
$files = [];
|
|
|
|
|
foreach ($css as $file) {
|
|
|
|
|
if (is_string($file)) {
|
|
|
|
|
$files[] = $file;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$entry['css'] = $files;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$cssIntegrity = $value['cssIntegrity'] ?? null;
|
|
|
|
|
if (is_array($cssIntegrity)) {
|
|
|
|
|
$hashes = [];
|
|
|
|
|
foreach ($cssIntegrity as $file => $hash) {
|
|
|
|
|
if (is_string($file) && is_string($hash)) {
|
|
|
|
|
$hashes[$file] = $hash;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$entry['cssIntegrity'] = $hashes;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$manifest[$source] = $entry;
|
2022-10-31 12:29:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $manifest;
|
2018-03-27 00:44:14 -05:00
|
|
|
}
|
|
|
|
|
}
|