Skip to content

Commit 204d11d

Browse files
authored
Added Source Schema Settings to FAR Format (#8501)
1 parent 647810f commit 204d11d

File tree

14 files changed

+455
-358
lines changed

14 files changed

+455
-358
lines changed

src/All.slnx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,4 +330,4 @@
330330
<Folder Name="/StrawberryShake/Tooling/test/">
331331
<Project Path="StrawberryShake/Tooling/test/Configuration.Tests/StrawberryShake.Tools.Configuration.Tests.csproj" />
332332
</Folder>
333-
</Solution>
333+
</Solution>

src/HotChocolate/Fusion-vnext/src/Fusion.Execution/Execution/Results/FetchResultStore.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,11 @@ public ImmutableArray<VariableValues> CreateVariableValueSets(
238238

239239
(next, current) = (current, next);
240240
next.Clear();
241+
242+
if (current.Count == 0)
243+
{
244+
return [];
245+
}
241246
}
242247

243248
PooledArrayWriter? buffer = null;

src/HotChocolate/Fusion-vnext/src/Fusion.Packaging/ArchiveSession.cs

Lines changed: 56 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Buffers;
12
using System.IO.Compression;
23

34
namespace HotChocolate.Fusion.Packaging;
@@ -6,15 +7,17 @@ internal sealed class ArchiveSession : IDisposable
67
{
78
private readonly Dictionary<string, FileEntry> _files = [];
89
private readonly ZipArchive _archive;
10+
private readonly FusionArchiveReadOptions _readOptions;
911
private FusionArchiveMode _mode;
1012
private bool _disposed;
1113

12-
public ArchiveSession(ZipArchive archive, FusionArchiveMode mode)
14+
public ArchiveSession(ZipArchive archive, FusionArchiveMode mode, FusionArchiveReadOptions readOptions)
1315
{
1416
ArgumentNullException.ThrowIfNull(archive);
1517

1618
_archive = archive;
1719
_mode = mode;
20+
_readOptions = readOptions;
1821
}
1922

2023
public bool HasUncommittedChanges
@@ -39,7 +42,7 @@ public IEnumerable<string> GetFiles()
3942
return files;
4043
}
4144

42-
public async Task<bool> ExistsAsync(string path, CancellationToken cancellationToken)
45+
public async Task<bool> ExistsAsync(string path, FileKind kind, CancellationToken cancellationToken)
4346
{
4447
if (_files.TryGetValue(path, out var file))
4548
{
@@ -49,12 +52,7 @@ public async Task<bool> ExistsAsync(string path, CancellationToken cancellationT
4952
if (_mode is not FusionArchiveMode.Create && _archive.GetEntry(path) is { } entry)
5053
{
5154
file = FileEntry.Read(path);
52-
#if NET10_0_OR_GREATER
53-
await entry.ExtractToFileAsync(file.TempPath, cancellationToken);
54-
#else
55-
entry.ExtractToFile(file.TempPath);
56-
await Task.CompletedTask;
57-
#endif
55+
await ExtractFileAsync(entry, file, GetAllowedSize(kind), cancellationToken);
5856
_files.Add(path, file);
5957
return true;
6058
}
@@ -72,7 +70,7 @@ public bool Exists(string path)
7270
return _mode is not FusionArchiveMode.Create && _archive.GetEntry(path) is not null;
7371
}
7472

75-
public async Task<Stream> OpenReadAsync(string path, CancellationToken cancellationToken)
73+
public async Task<Stream> OpenReadAsync(string path, FileKind kind, CancellationToken cancellationToken)
7674
{
7775
if (_files.TryGetValue(path, out var file))
7876
{
@@ -87,12 +85,7 @@ public async Task<Stream> OpenReadAsync(string path, CancellationToken cancellat
8785
if (_mode is not FusionArchiveMode.Create && _archive.GetEntry(path) is { } entry)
8886
{
8987
file = FileEntry.Read(path);
90-
#if NET10_0_OR_GREATER
91-
await entry.ExtractToFileAsync(file.TempPath, cancellationToken);
92-
#else
93-
entry.ExtractToFile(file.TempPath);
94-
await Task.CompletedTask;
95-
#endif
88+
await ExtractFileAsync(entry, file, GetAllowedSize(kind), cancellationToken);
9689
var stream = File.OpenRead(file.TempPath);
9790
_files.Add(path, file);
9891
return stream;
@@ -181,21 +174,66 @@ await _archive.CreateEntryFromFileAsync(
181174
}
182175
}
183176

177+
private static async Task ExtractFileAsync(
178+
ZipArchiveEntry zipEntry,
179+
FileEntry fileEntry,
180+
int maxAllowedSize,
181+
CancellationToken cancellationToken)
182+
{
183+
var buffer = ArrayPool<byte>.Shared.Rent(4096);
184+
var consumed = 0;
185+
186+
await using var readStream = zipEntry.Open();
187+
await using var writeStream = File.Open(fileEntry.TempPath, FileMode.Create, FileAccess.Write);
188+
189+
int read;
190+
while ((read = await readStream.ReadAsync(buffer, cancellationToken)) > 0)
191+
{
192+
consumed += read;
193+
194+
if (consumed > maxAllowedSize)
195+
{
196+
throw new InvalidOperationException(
197+
$"File is too large and exceeds the allowed size of {maxAllowedSize}.");
198+
}
199+
200+
await writeStream.WriteAsync(buffer.AsMemory(0, read), cancellationToken);
201+
}
202+
}
203+
204+
private int GetAllowedSize(FileKind kind)
205+
=> kind switch
206+
{
207+
FileKind.Schema
208+
=> _readOptions.MaxAllowedSchemaSize,
209+
FileKind.Manifest or FileKind.Settings or FileKind.Metadata or FileKind.Signature
210+
=> _readOptions.MaxAllowedSettingsSize,
211+
_ => throw new ArgumentOutOfRangeException(nameof(kind), kind, null)
212+
};
213+
184214
public void Dispose()
185215
{
186216
if (_disposed)
187217
{
188218
return;
189219
}
190220

191-
_disposed = true;
192221
foreach (var file in _files.Values)
193222
{
194-
if (file.State is not FileState.Deleted)
223+
if (file.State is not FileState.Deleted && File.Exists(file.TempPath))
195224
{
196-
File.Delete(file.TempPath);
225+
try
226+
{
227+
File.Delete(file.TempPath);
228+
}
229+
catch
230+
{
231+
// ignore
232+
}
197233
}
198234
}
235+
236+
_disposed = true;
199237
}
200238

201239
private class FileEntry
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace HotChocolate.Fusion.Packaging;
2+
3+
internal enum FileKind
4+
{
5+
Schema,
6+
Settings,
7+
Manifest,
8+
Metadata,
9+
Signature
10+
}

src/HotChocolate/Fusion-vnext/src/Fusion.Packaging/FileNames.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ internal static class FileNames
55
private const string GatewaySchemaFormat = "gateway/{0}/gateway.graphqls";
66
private const string GatewaySettingsFormat = "gateway/{0}/gateway-settings.json";
77
private const string SourceSchemaFormat = "source-schemas/{0}/schema.graphqls";
8+
private const string SourceSchemaSettingsFormat = "source-schemas/{0}/schema-settings.json";
89

910
public const string ArchiveMetadata = "archive-metadata.json";
1011
public const string CompositionSettings = "composition-settings.json";
@@ -19,4 +20,34 @@ public static string GetGatewaySettingsPath(Version version)
1920

2021
public static string GetSourceSchemaPath(string schemaName)
2122
=> string.Format(SourceSchemaFormat, schemaName);
23+
24+
public static string GetSourceSchemaSettingsPath(string schemaName)
25+
=> string.Format(SourceSchemaSettingsFormat, schemaName);
26+
27+
public static FileKind GetFileKind(string fileName)
28+
{
29+
switch (Path.GetFileName(fileName))
30+
{
31+
case "gateway.graphqls":
32+
case "schema.graphqls":
33+
return FileKind.Schema;
34+
35+
case "schema-settings.json":
36+
case "gateway-settings.json":
37+
case "composition-settings.json":
38+
return FileKind.Settings;
39+
40+
case "archive-metadata.json":
41+
return FileKind.Metadata;
42+
43+
case "manifest.json":
44+
return FileKind.Manifest;
45+
46+
case "signature.json":
47+
return FileKind.Signature;
48+
49+
default:
50+
return FileKind.Settings;
51+
}
52+
}
2253
}

0 commit comments

Comments
 (0)