|
| 1 | +using System.Buffers.Text; |
| 2 | +using System.Runtime.CompilerServices; |
| 3 | +using System.Security.Cryptography; |
| 4 | +using System.Text; |
| 5 | + |
| 6 | +namespace PostgreSignalR; |
| 7 | + |
| 8 | +internal abstract class ChannelNameProvider |
| 9 | +{ |
| 10 | + public ChannelNameProvider(string returnServerName) |
| 11 | + { |
| 12 | + All = Normalize("all"); |
| 13 | + GroupManagement = Normalize("internal_groups"); |
| 14 | + ReturnResults = Normalize($"internal_return_{returnServerName}"); |
| 15 | + } |
| 16 | + |
| 17 | + /// <summary> |
| 18 | + /// Postgres limits channel names to identifiers up to 63 characters. |
| 19 | + /// Normalization should ensure any channel names meet this. |
| 20 | + /// </summary> |
| 21 | + /// <param name="name">The raw name of the channel</param> |
| 22 | + /// <returns>The normalized channel name</returns> |
| 23 | + internal abstract string Normalize(string name); |
| 24 | + |
| 25 | + /// <summary> |
| 26 | + /// Gets the name of the channel for sending to all connections. |
| 27 | + /// </summary> |
| 28 | + /// <remarks> |
| 29 | + /// The payload on this channel is <see cref="PostgresInvocation"/> objects containing |
| 30 | + /// invocations to be sent to all connections |
| 31 | + /// </remarks> |
| 32 | + public string All { get; init; } |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// Gets the name of the internal channel for group management messages. |
| 36 | + /// </summary> |
| 37 | + public string GroupManagement { get; } |
| 38 | + |
| 39 | + /// <summary> |
| 40 | + /// Gets the name of the internal channel for receiving client results. |
| 41 | + /// </summary> |
| 42 | + public string ReturnResults { get; } |
| 43 | + |
| 44 | + /// <summary> |
| 45 | + /// Gets the name of the channel for sending a message to a specific connection. |
| 46 | + /// </summary> |
| 47 | + /// <param name="connectionId">The ID of the connection to get the channel for.</param> |
| 48 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 49 | + public string Connection(string connectionId) => Normalize($"connection_{connectionId}"); |
| 50 | + |
| 51 | + /// <summary> |
| 52 | + /// Gets the name of the channel for sending a message to a named group of connections. |
| 53 | + /// </summary> |
| 54 | + /// <param name="groupName">The name of the group to get the channel for.</param> |
| 55 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 56 | + public string Group(string groupName) => Normalize($"group_{groupName}"); |
| 57 | + |
| 58 | + /// <summary> |
| 59 | + /// Gets the name of the channel for sending a message to all collections associated with a user. |
| 60 | + /// </summary> |
| 61 | + /// <param name="userId">The ID of the user to get the channel for.</param>32 |
| 62 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 63 | + public string User(string userId) => Normalize($"user_{userId}"); |
| 64 | + |
| 65 | + /// <summary> |
| 66 | + /// Gets the name of the acknowledgement channel for the specified server. |
| 67 | + /// </summary> |
| 68 | + /// <param name="serverName">The name of the server to get the acknowledgement channel for.</param> |
| 69 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 70 | + public string Ack(string serverName) => Normalize($"internal_ack_{serverName}"); |
| 71 | +} |
| 72 | + |
| 73 | +internal sealed class TruncatingChannelNameProvider(string prefix, string returnServerName) : ChannelNameProvider(returnServerName) |
| 74 | +{ |
| 75 | + internal override string Normalize(string name) |
| 76 | + { |
| 77 | + name = prefix + name; |
| 78 | + |
| 79 | + if (name.Length <= 63) |
| 80 | + { |
| 81 | + return name; |
| 82 | + } |
| 83 | + |
| 84 | + var hash = Convert.ToHexString(SHA256.HashData(Encoding.UTF8.GetBytes(name))).ToLowerInvariant(); |
| 85 | + var suffix = $"_{hash[..8]}"; |
| 86 | + var trimLength = Math.Max(63 - suffix.Length, 0); |
| 87 | + |
| 88 | + return $"{name[..trimLength]}{suffix}"; |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +internal sealed class HashingChannelNameProvider(string prefix, string returnServerName) : ChannelNameProvider(returnServerName) |
| 93 | +{ |
| 94 | + internal override string Normalize(string name) |
| 95 | + { |
| 96 | + Span<byte> utf8 = stackalloc byte[1024]; |
| 97 | + var utf8Length = Encoding.UTF8.GetBytes(name.AsSpan(), utf8); |
| 98 | + |
| 99 | + Span<byte> hash = stackalloc byte[32]; |
| 100 | + SHA256.HashData(utf8[..utf8Length], hash); |
| 101 | + |
| 102 | + Span<byte> base64 = stackalloc byte[44]; |
| 103 | + Base64.EncodeToUtf8(hash, base64, out _, out var written); |
| 104 | + |
| 105 | + return prefix + string.Create(written-1, base64.ToArray(), static (destination, source) => |
| 106 | + { |
| 107 | + for (int i = 0; i < destination.Length; i++) |
| 108 | + { |
| 109 | + var c = source[i]; |
| 110 | + destination[i] = c == (byte)'+' || c == (byte)'/' ? '_' : (char)c; |
| 111 | + } |
| 112 | + }); |
| 113 | + } |
| 114 | +} |
0 commit comments