summary refs log tree commit diff
path: root/ReferenceClientProxyImplementation/Patches/Implementations/JSPatches/PrefetchAssetsPatch.cs
diff options
context:
space:
mode:
authorRory& <root@rory.gay>2026-02-23 02:03:20 +0100
committerRory& <root@rory.gay>2026-02-23 02:03:20 +0100
commit77a609758bb80bac9497d2e3988550f8be578407 (patch)
tree991a9d258ca4fece1132a1a344d0fe11e3b03d51 /ReferenceClientProxyImplementation/Patches/Implementations/JSPatches/PrefetchAssetsPatch.cs
downloadReferenceClientProxyImplementation-master.tar.xz
Initial commit HEAD master
Diffstat (limited to '')
-rw-r--r--ReferenceClientProxyImplementation/Patches/Implementations/JSPatches/PrefetchAssetsPatch.cs56
1 files changed, 56 insertions, 0 deletions
diff --git a/ReferenceClientProxyImplementation/Patches/Implementations/JSPatches/PrefetchAssetsPatch.cs b/ReferenceClientProxyImplementation/Patches/Implementations/JSPatches/PrefetchAssetsPatch.cs
new file mode 100644

index 0000000..bab0756 --- /dev/null +++ b/ReferenceClientProxyImplementation/Patches/Implementations/JSPatches/PrefetchAssetsPatch.cs
@@ -0,0 +1,56 @@ +// using System.Text; +// using System.Text.RegularExpressions; +// using ReferenceClientProxyImplementation.Services; +// +// namespace ReferenceClientProxyImplementation.Patches.Implementations.JSPatches; +// +// public partial class PrefetchAssetsPatch(IServiceProvider sp) : IPatch { +// public int GetOrder() => 1000000; +// +// public string GetName() => "Prefetch assets"; +// public bool Applies(string relativeName, byte[] content) => relativeName.EndsWith(".js"); +// +// private static SemaphoreSlim ss = new(2, 2); +// private static HashSet<string> alreadyKnownAssets = new(); +// +// public async Task<byte[]> Execute(string relativePath, byte[] content) { +// // Can't inject service due to loop +// var stringContent = Encoding.UTF8.GetString(content); +// var matches = PrefetchAssetsRegex().Matches(stringContent); +// +// Console.WriteLine($"Found {matches.Count} prefetch assets in {relativePath}"); +// if (matches.Count == 0) { +// return content; // No matches found, return original content +// } +// +// var clientStore = sp.GetRequiredService<ClientStoreService>(); +// +// var newAssets = matches +// .Select(x => x.Groups[1].Value) +// .Distinct() +// .Where(x => !clientStore.HasRawAsset(x) && alreadyKnownAssets.Add(x)); +// +// var tasks = newAssets +// .Select(async match => { +// await ss.WaitAsync(); +// Console.WriteLine($"Discovered prefetch asset in {relativePath}: {match}"); +// // var patches = sp.GetRequiredService<PatchSet>(); +// var res = await clientStore.GetOrDownloadRawAsset(match); +// await res.DisposeAsync(); +// ss.Release(); +// Console.WriteLine($"Prefetched asset {match} in {relativePath}"); +// }).ToList(); +// +// if (tasks.Count == 0) { +// Console.WriteLine($"No new prefetch assets found in {relativePath}, returning original content."); +// return content; // No new assets to prefetch, return original content +// } +// +// await Task.WhenAny(tasks); +// +// return content; +// } +// +// [GeneratedRegex(@".\.exports = ""((?:[a-z\d/\.]*?)\.\w{2,4})""", RegexOptions.Compiled)] +// private static partial Regex PrefetchAssetsRegex(); +// } \ No newline at end of file