Path: redb-route/redb.Route.As4/Mime/As4Spool.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route.As4/Mime/As4Spool.cs
----
using redb.Route.Abstractions;
using redb.Route.Configuration;
using redb.Route.Core;
using redb.Route.Extensions;
namespace redb.Route.As4.Mime;
///
/// Where the connector spools request bodies and payloads: the engine's stream cache options (threshold, temporary
/// directory), as .StreamCaching() resolves them ().
///
internal static class As4Spool
{
/// The stream cache options of .
public static StreamCacheOptions Options(IRouteContext? context) => context.GetStreamCacheOptions();
/// The asynchronous form of , for a step that reads a foreign stream (the exchange body).
public static async Task RunAsync(StreamCacheOptions options, Func write, CancellationToken ct)
{
ArgumentNullException.ThrowIfNull(options);
ArgumentNullException.ThrowIfNull(write);
var spool = new StreamCache(options);
try
{
await write(spool, ct).ConfigureAwait(false);
spool.CompleteWriting();
return spool;
}
catch
{
await spool.DisposeAsync().ConfigureAwait(false);
throw;
}
}
///
/// Runs one processing step, , into a new spool (memory, or a temporary file past the
/// threshold). The spool is returned readable and rewound; if the step throws, it is disposed and nothing of its
/// output survives. Decryption relies on that, since GCM authenticates only at the end.
///
public static Stream Run(StreamCacheOptions options, Action write)
{
ArgumentNullException.ThrowIfNull(options);
ArgumentNullException.ThrowIfNull(write);
var spool = new StreamCache(options);
try
{
write(spool);
spool.CompleteWriting();
return spool;
}
catch
{
spool.Dispose();
throw;
}
}
}