Skip to content

Commit 26877ea

Browse files
committed
Keep track of unnamed chunk files
1 parent d6f3498 commit 26877ea

File tree

4 files changed

+59
-11
lines changed

4 files changed

+59
-11
lines changed

composer.json

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
{
2+
"scripts": {
3+
"test": "phpstan analyse"
4+
},
25
"require": {
36
"phpstan/phpstan": "^1.4"
47
}

generate_manifest_urls.mjs

+29-1
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,26 @@ for (const { file, cdn } of files) {
4545
const code = await readFile(file);
4646
const ast = parse(code, { ecmaVersion: latestEcmaVersion, loc: true });
4747

48+
const seenIds = new Set();
49+
4850
traverse(ast, {
4951
enter: function (node) {
52+
if (
53+
node.type === Syntax.BinaryExpression &&
54+
node.left.type === Syntax.BinaryExpression &&
55+
node.left.right.type === Syntax.Literal &&
56+
/\.(css|js)\?contenthash=/.test(node.left.right.value) &&
57+
node.right.type === Syntax.MemberExpression &&
58+
node.right.object.type === Syntax.ObjectExpression
59+
) {
60+
for (const property of node.right.object.properties) {
61+
if (property.value.type === Syntax.Literal) {
62+
seenIds.add(property.key.raw);
63+
}
64+
}
65+
}
66+
67+
// Useful: https://astexplorer.net/
5068
if (
5169
node.type === Syntax.BinaryExpression &&
5270
node.left.type === Syntax.Literal &&
@@ -67,12 +85,15 @@ for (const { file, cdn } of files) {
6785
return;
6886
}
6987

70-
const obj = node.right.type === Syntax.LogicalExpression ? node.right.left.object : node.right.object;
88+
const partialFileNames = node.right.type === Syntax.LogicalExpression;
89+
const obj = partialFileNames ? node.right.left.object : node.right.object;
7190

7291
for (const property of obj.properties) {
7392
if (property.value.type === Syntax.Literal) {
7493
const name = property.value.value;
7594

95+
seenIds.delete(property.key.raw);
96+
7697
if (name.endsWith("-json") && !name.endsWith("_english-json")) {
7798
continue;
7899
}
@@ -85,6 +106,13 @@ for (const { file, cdn } of files) {
85106
}
86107
}
87108

109+
for (const id of seenIds) {
110+
const fullUrl = cdn + folder + id + suffix;
111+
112+
urls.add(fullUrl);
113+
}
114+
115+
seenIds.clear();
88116
this.skip();
89117
}
90118
},

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"prettier": "prettier --write"
88
},
99
"dependencies": {
10-
"espree": "^9.3.1",
10+
"espree": "^10.0.1",
1111
"estraverse": "^5.3.0",
1212
"prettier": "^3.0.0"
1313
},

update.php

+26-9
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
if( \function_exists( 'sys_getloadavg' ) )
77
{
8-
if( \sys_getloadavg()[ 1 ] > 5.0 )
8+
if( ( \sys_getloadavg()[ 1 ] ?? 0.0 ) > 5.0 )
99
{
1010
echo 'Not running due to high cpu load';
1111
exit;
@@ -530,6 +530,7 @@ private function Fetch( array $URLs, int $WindowSize ) : void
530530

531531
while( $Done = curl_multi_info_read( $Master ) )
532532
{
533+
/** @var CurlHandle */
533534
$Handle = $Done[ 'handle' ];
534535
$URL = curl_getinfo( $Handle, CURLINFO_EFFECTIVE_URL );
535536
$Code = curl_getinfo( $Handle, CURLINFO_HTTP_CODE );
@@ -554,7 +555,7 @@ private function Fetch( array $URLs, int $WindowSize ) : void
554555
{
555556
$this->Log( '{yellow}Error ' . $Code . '{normal} - ' . $URL );
556557

557-
if( $Code < 400 && $Code > 499 )
558+
if( $Code < 100 || $Code >= 500 )
558559
{
559560
$this->URLsToFetch[ ] =
560561
[
@@ -786,11 +787,13 @@ private function ProcessManifests( array $KnownUrls ) : array
786787
// Find and delete old chunk~ files
787788
$Folders = [];
788789

790+
$IsChunkFile = static fn( string $Filename ) : bool => preg_match( '/^(chunk~|libraries~|[0-9]+\.)/', $Filename, $Matches ) === 1;
791+
789792
foreach( $URLsToFetch as $Url )
790793
{
791794
$Filename = basename( $Url[ 'File' ] );
792795

793-
if( str_starts_with( $Filename, 'chunk~' ) || str_starts_with( $Filename, 'libraries~' ) )
796+
if( $IsChunkFile( $Filename ) )
794797
{
795798
$Folder = __DIR__ . '/' . dirname( $Url[ 'File' ] ) . '/';
796799

@@ -805,18 +808,32 @@ private function ProcessManifests( array $KnownUrls ) : array
805808

806809
foreach( $Folders as $Folder => $NewChunks )
807810
{
811+
foreach( new DirectoryIterator( $Folder ) as $FileInfo )
812+
{
813+
if( $FileInfo->isDot() )
814+
{
815+
continue;
816+
}
817+
818+
$Filename = $FileInfo->getFilename();
819+
820+
if( $IsChunkFile( $Filename ) && !isset( $NewChunks[ $Filename ] ) )
821+
{
822+
$FilepathOnDisk = $FileInfo->getRealPath();
823+
824+
$this->Log( 'Chunk ' . $FilepathOnDisk . ' no longer exists in manifest' );
825+
826+
unlink( $FilepathOnDisk );
827+
}
828+
}
829+
808830
foreach( [ 'chunk~*', 'libraries~*' ] as $Glob )
809831
{
810832
foreach( glob( $Folder . $Glob ) as $FilepathOnDisk )
811833
{
812834
$Filename = basename( $FilepathOnDisk );
813835

814-
if( !isset( $NewChunks[ $Filename ] ) )
815-
{
816-
$this->Log( 'Chunk ' . $FilepathOnDisk . ' no longer exists in manifest' );
817-
818-
unlink( $FilepathOnDisk );
819-
}
836+
820837
}
821838
}
822839
}

0 commit comments

Comments
 (0)