| File: ServerSentEvents\SseParser.cs | Web Access |
| Project: src\src\Shared\Shared.csproj (Shared) |
// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. using System.IO; using System.Text; #pragma warning disable S2333 // Redundant modifiers should not be used namespace System.Net.ServerSentEvents { /// <summary>Provides a parser for parsing server-sent events.</summary> [System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] internal static class SseParser { /// <summary>The default <see cref="SseItem{T}.EventType"/> ("message") for an event that did not explicitly specify a type.</summary> public const string EventTypeDefault = "message"; /// <summary>Creates a parser for parsing a <paramref name="sseStream"/> of server-sent events into a sequence of <see cref="SseItem{String}"/> values.</summary> /// <param name="sseStream">The stream containing the data to parse.</param> /// <returns> /// The enumerable of strings, which can be enumerated synchronously or asynchronously. The strings /// are decoded from the UTF8-encoded bytes of the payload of each event. /// </returns> /// <exception cref="ArgumentNullException"><paramref name="sseStream"/> is null.</exception> /// <remarks> /// This overload has behavior equivalent to calling <see cref="Create{T}(Stream, SseItemParser{T})"/> with a delegate /// that decodes the data of each event using <see cref="Encoding.UTF8"/>'s GetString method. /// </remarks> public static SseParser<string> Create(Stream sseStream) => Create(sseStream, static (_, bytes) => Helpers.Utf8GetString(bytes)); /// <summary>Creates a parser for parsing a <paramref name="sseStream"/> of server-sent events into a sequence of <see cref="SseItem{T}"/> values.</summary> /// <typeparam name="T">Specifies the type of data in each event.</typeparam> /// <param name="sseStream">The stream containing the data to parse.</param> /// <param name="itemParser">The parser to use to transform each payload of bytes into a data element.</param> /// <returns>The enumerable, which can be enumerated synchronously or asynchronously.</returns> /// <exception cref="ArgumentNullException"><paramref name="sseStream"/> or <paramref name="itemParser"/> is null.</exception> public static SseParser<T> Create<T>(Stream sseStream, SseItemParser<T> itemParser) { if (sseStream is null) { ThrowHelper.ThrowArgumentNullException(nameof(sseStream)); } if (itemParser is null) { ThrowHelper.ThrowArgumentNullException(nameof(itemParser)); } return new SseParser<T>(sseStream, itemParser); } } }