using System; using System.Collections.Generic; using System.IO; using System.IO.Compression; using System.Text; using System.Text.Json; using System.Threading; using System.Threading.Tasks; using StellaOps.Signals.Models; namespace StellaOps.Signals.Parsing; internal static class RuntimeFactsNdjsonReader { private static readonly JsonSerializerOptions SerializerOptions = new(JsonSerializerDefaults.Web); public static async Task> ReadAsync( Stream input, bool gzipEncoded, CancellationToken cancellationToken) { ArgumentNullException.ThrowIfNull(input); Stream effectiveStream = input; if (gzipEncoded) { effectiveStream = new GZipStream(input, CompressionMode.Decompress, leaveOpen: true); } using var reader = new StreamReader(effectiveStream, Encoding.UTF8, detectEncodingFromByteOrderMarks: true, leaveOpen: !gzipEncoded); var events = new List(); string? line; while ((line = await reader.ReadLineAsync(cancellationToken).ConfigureAwait(false)) != null) { if (string.IsNullOrWhiteSpace(line)) { continue; } var evt = JsonSerializer.Deserialize(line, SerializerOptions); if (evt is null || string.IsNullOrWhiteSpace(evt.SymbolId)) { continue; } events.Add(evt); } return events; } }