Skip to content

Commit

Permalink
consistent Nats class prefix (#121)
Browse files Browse the repository at this point in the history
Signed-off-by: Caleb Lloyd <caleb@synadia.com>
  • Loading branch information
caleblloyd authored Aug 31, 2023
1 parent c9d0f36 commit 443d4cd
Show file tree
Hide file tree
Showing 17 changed files with 53 additions and 53 deletions.
18 changes: 9 additions & 9 deletions src/NATS.Client.Core/INatsSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ public interface ICountableBufferWriter : IBufferWriter<byte>
int WrittenCount { get; }
}

public sealed class JsonNatsSerializer : INatsSerializer
public sealed class NatsJsonSerializer : INatsSerializer
{
private static readonly JsonWriterOptions JsonWriterOptions = new JsonWriterOptions
private static readonly JsonWriterOptions JsonWriterOpts = new JsonWriterOptions
{
Indented = false,
SkipValidation = true,
Expand All @@ -29,11 +29,11 @@ public sealed class JsonNatsSerializer : INatsSerializer
[ThreadStatic]
private static Utf8JsonWriter? _jsonWriter;

private readonly JsonSerializerOptions _options;
private readonly JsonSerializerOptions _opts;

public JsonNatsSerializer(JsonSerializerOptions options) => _options = options;
public NatsJsonSerializer(JsonSerializerOptions opts) => _opts = opts;

public static JsonNatsSerializer Default { get; } =
public static NatsJsonSerializer Default { get; } =
new(new JsonSerializerOptions
{
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
Expand All @@ -44,15 +44,15 @@ public int Serialize<T>(ICountableBufferWriter bufferWriter, T? value)
Utf8JsonWriter writer;
if (_jsonWriter == null)
{
writer = _jsonWriter = new Utf8JsonWriter(bufferWriter, JsonWriterOptions);
writer = _jsonWriter = new Utf8JsonWriter(bufferWriter, JsonWriterOpts);
}
else
{
writer = _jsonWriter;
writer.Reset(bufferWriter);
}

JsonSerializer.Serialize(writer, value, _options);
JsonSerializer.Serialize(writer, value, _opts);

var bytesCommitted = (int)writer.BytesCommitted;
writer.Reset(NullBufferWriter.Instance);
Expand All @@ -62,13 +62,13 @@ public int Serialize<T>(ICountableBufferWriter bufferWriter, T? value)
public T? Deserialize<T>(in ReadOnlySequence<byte> buffer)
{
var reader = new Utf8JsonReader(buffer); // Utf8JsonReader is ref struct, no allocate.
return JsonSerializer.Deserialize<T>(ref reader, _options);
return JsonSerializer.Deserialize<T>(ref reader, _opts);
}

public object? Deserialize(in ReadOnlySequence<byte> buffer, Type type)
{
var reader = new Utf8JsonReader(buffer); // Utf8JsonReader is ref struct, no allocate.
return JsonSerializer.Deserialize(ref reader, type, _options);
return JsonSerializer.Deserialize(ref reader, type, _opts);
}

private sealed class NullBufferWriter : IBufferWriter<byte>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace NATS.Client.Core;

public interface IServerInfo
public interface INatsServerInfo
{
string Id { get; }

Expand Down
2 changes: 1 addition & 1 deletion src/NATS.Client.Core/Internal/ClientOpts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ private ClientOpts(NatsOpts opts)
public bool TLSRequired { get; init; }

[JsonPropertyName("nkey")]
public string? Nkey { get; set; }
public string? NKey { get; set; }

/// <summary>The JWT that identifies a user permissions and account.</summary>
[JsonPropertyName("jwt")]
Expand Down
2 changes: 1 addition & 1 deletion src/NATS.Client.Core/Internal/ServerInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace NATS.Client.Core.Internal;

// Defined from `type Info struct` in nats-server
// https://github.com/nats-io/nats-server/blob/a23b1b7/server/server.go#L61
internal sealed record ServerInfo : IServerInfo
internal sealed record ServerInfo : INatsServerInfo
{
[JsonPropertyName("server_id")]
public string Id { get; set; } = string.Empty;
Expand Down
10 changes: 5 additions & 5 deletions src/NATS.Client.Core/Internal/UserCredentials.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public UserCredentials(NatsAuthOpts authOpts)
{
Jwt = authOpts.Jwt;
Seed = authOpts.Seed;
Nkey = authOpts.Nkey;
NKey = authOpts.NKey;
Token = authOpts.Token;

if (!string.IsNullOrEmpty(authOpts.CredsFile))
Expand All @@ -19,15 +19,15 @@ public UserCredentials(NatsAuthOpts authOpts)

if (!string.IsNullOrEmpty(authOpts.NKeyFile))
{
(Seed, Nkey) = LoadNKeyFile(authOpts.NKeyFile);
(Seed, NKey) = LoadNKeyFile(authOpts.NKeyFile);
}
}

public string? Jwt { get; }

public string? Seed { get; }

public string? Nkey { get; }
public string? NKey { get; }

public string? Token { get; }

Expand All @@ -36,7 +36,7 @@ public UserCredentials(NatsAuthOpts authOpts)
if (Seed == null || nonce == null)
return null;

using var kp = Nkeys.FromSeed(Seed);
using var kp = NKeys.FromSeed(Seed);
var bytes = kp.Sign(Encoding.ASCII.GetBytes(nonce));
var sig = CryptoBytes.ToBase64String(bytes);

Expand All @@ -46,7 +46,7 @@ public UserCredentials(NatsAuthOpts authOpts)
internal void Authenticate(ClientOpts opts, ServerInfo? info)
{
opts.JWT = Jwt;
opts.Nkey = Nkey;
opts.NKey = NKey;
opts.AuthToken = Token;
opts.Sig = info is { AuthRequired: true, Nonce: { } } ? Sign(info.Nonce) : null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ namespace NATS.Client.Core;
/// Partial implementation of the NATS Ed25519 KeyPair. This is not complete, but provides enough
/// functionality to implement the client side NATS 2.0 security scheme.
/// </summary>
public class NkeyPair : IDisposable
public class NKeyPair : IDisposable
{
private byte[] _seed;
private byte[] _expandedPrivateKey;
private byte[] _key;

private bool _disposedValue = false; // To detect redundant calls

internal NkeyPair(byte[] userSeed)
internal NKeyPair(byte[] userSeed)
{
if (userSeed == null)
throw new NatsException("seed cannot be null");
Expand Down Expand Up @@ -50,8 +50,8 @@ public byte[] PrivateKeySeed
/// </summary>
public void Wipe()
{
Nkeys.Wipe(ref _seed);
Nkeys.Wipe(ref _expandedPrivateKey);
NKeys.Wipe(ref _seed);
NKeys.Wipe(ref _expandedPrivateKey);
}

/// <summary>
Expand All @@ -67,15 +67,15 @@ public byte[] Sign(byte[] src)
}

/// <summary>
/// Releases all resources used by the NkeyPair.
/// Releases all resources used by the NKeyPair.
/// </summary>
public void Dispose()
{
Dispose(true);
}

/// <summary>
/// Releases the unmanaged resources used by the NkeyPair and optionally releases the managed resources.
/// Releases the unmanaged resources used by the NKeyPair and optionally releases the managed resources.
/// </summary>
/// <param name="disposing">true to release both managed and unmanaged resources; false to release only unmanaged resources.</param>
protected virtual void Dispose(bool disposing)
Expand All @@ -93,9 +93,9 @@ protected virtual void Dispose(bool disposing)
}

/// <summary>
/// Nkeys is a class provided to manipulate Nkeys and generate NkeyPairs.
/// NKeys is a class provided to manipulate NKeys and generate NKeyPairs.
/// </summary>
public class Nkeys
public class NKeys
{
// PrefixByteSeed is the version byte used for encoded NATS Seeds
private const byte PrefixByteSeed = 18 << 3; // Base32-encodes to 'S...'
Expand Down Expand Up @@ -124,7 +124,7 @@ public class Nkeys
/// <summary>
/// Decodes a base 32 encoded NKey into a nkey seed and verifies the checksum.
/// </summary>
/// <param name="src">Base 32 encoded Nkey.</param>
/// <param name="src">Base 32 encoded NKey.</param>
/// <returns></returns>
public static byte[] Decode(string src)
{
Expand Down Expand Up @@ -163,16 +163,16 @@ public static void Wipe(string src)
}

/// <summary>
/// Creates an NkeyPair from a private seed String.
/// Creates an NKeyPair from a private seed String.
/// </summary>
/// <param name="seed"></param>
/// <returns>A NATS Ed25519 Keypair</returns>
public static NkeyPair FromSeed(string seed)
public static NKeyPair FromSeed(string seed)
{
var userSeed = DecodeSeed(seed);
try
{
var kp = new NkeyPair(userSeed);
var kp = new NKeyPair(userSeed);
return kp;
}
finally
Expand Down Expand Up @@ -215,7 +215,7 @@ public static string CreateOperatorSeed()
/// <returns>A the public key corresponding to Seed</returns>
public static string PublicKeyFromSeed(string seed)
{
var s = Nkeys.Decode(seed);
var s = NKeys.Decode(seed);
if ((s[0] & (31 << 3)) != PrefixByteSeed)
{
throw new NatsException("Not a seed");
Expand Down Expand Up @@ -258,7 +258,7 @@ internal static byte[] DecodeSeed(byte[] raw)

internal static byte[] DecodeSeed(string src)
{
return DecodeSeed(Nkeys.Decode(src));
return DecodeSeed(NKeys.Decode(src));
}

internal static string Encode(byte prefixbyte, bool seed, byte[] src)
Expand Down
2 changes: 1 addition & 1 deletion src/NATS.Client.Core/NatsAuthOpts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public record NatsAuthOpts

public string? Jwt { get; init; }

public string? Nkey { get; init; }
public string? NKey { get; init; }

public string? Seed { get; init; }

Expand Down
6 changes: 3 additions & 3 deletions src/NATS.Client.Core/NatsConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public NatsConnection(NatsOpts opts)
SubscriptionManager = new SubscriptionManager(this, InboxPrefix);
_logger = opts.LoggerFactory.CreateLogger<NatsConnection>();
_clientOpts = ClientOpts.Create(Opts);
HeaderParser = new HeaderParser(opts.HeaderEncoding);
HeaderParser = new NatsHeaderParser(opts.HeaderEncoding);
}

// events
Expand All @@ -86,9 +86,9 @@ public NatsConnection(NatsOpts opts)

public NatsConnectionState ConnectionState { get; private set; }

public IServerInfo? ServerInfo => WritableServerInfo; // server info is set when received INFO
public INatsServerInfo? ServerInfo => WritableServerInfo; // server info is set when received INFO

public HeaderParser HeaderParser { get; }
public NatsHeaderParser HeaderParser { get; }

internal SubscriptionManager SubscriptionManager { get; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// ReSharper disable PossiblyImpureMethodCallOnReadonlyVariable
namespace NATS.Client.Core;

public class HeaderParser
public class NatsHeaderParser
{
private const byte ByteCR = (byte)'\r';
private const byte ByteLF = (byte)'\n';
Expand All @@ -23,7 +23,7 @@ public class HeaderParser

private readonly Encoding _encoding;

public HeaderParser(Encoding encoding) => _encoding = encoding;
public NatsHeaderParser(Encoding encoding) => _encoding = encoding;

public bool ParseHeaders(SequenceReader<byte> reader, NatsHeaders headers)
{
Expand Down
4 changes: 2 additions & 2 deletions src/NATS.Client.Core/NatsMsg.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal static NatsMsg Build(
in ReadOnlySequence<byte>? headersBuffer,
in ReadOnlySequence<byte> payloadBuffer,
INatsConnection? connection,
HeaderParser headerParser)
NatsHeaderParser headerParser)
{
NatsHeaders? headers = null;

Expand Down Expand Up @@ -81,7 +81,7 @@ internal static NatsMsg<T> Build(
in ReadOnlySequence<byte>? headersBuffer,
in ReadOnlySequence<byte> payloadBuffer,
INatsConnection? connection,
HeaderParser headerParser,
NatsHeaderParser headerParser,
INatsSerializer serializer)
{
// Consider an empty payload as null or default value for value types. This way we are able to
Expand Down
2 changes: 1 addition & 1 deletion src/NATS.Client.Core/NatsOpts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public sealed record NatsOpts
Headers: true,
AuthOpts: NatsAuthOpts.Default,
TlsOpts: NatsTlsOpts.Default,
Serializer: JsonNatsSerializer.Default,
Serializer: NatsJsonSerializer.Default,
LoggerFactory: NullLoggerFactory.Instance,
WriterBufferSize: 65534, // 32767
ReaderBufferSize: 1048576,
Expand Down
2 changes: 1 addition & 1 deletion src/NATS.Client.Core/NatsSubBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ internal virtual IEnumerable<ICommand> GetReconnectCommands(int sid)
/// </summary>
/// <param name="subject">Subject received for this subscription. This might not be the subject you subscribed to especially when using wildcards. For example, if you subscribed to events.* you may receive events.open.</param>
/// <param name="replyTo">Subject the sender wants you to send messages back to it.</param>
/// <param name="headersBuffer">Raw headers bytes. You can use <see cref="NatsConnection"/> <see cref="HeaderParser"/> to decode them.</param>
/// <param name="headersBuffer">Raw headers bytes. You can use <see cref="NatsConnection"/> <see cref="NatsHeaderParser"/> to decode them.</param>
/// <param name="payloadBuffer">Raw payload bytes.</param>
/// <returns></returns>
protected abstract ValueTask ReceiveInternalAsync(string subject, string? replyTo, ReadOnlySequence<byte>? headersBuffer, ReadOnlySequence<byte> payloadBuffer);
Expand Down
4 changes: 2 additions & 2 deletions src/NATS.Client.JetStream/NatsJSSubConsume.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public ValueTask CallMsgNextAsync(ConsumerGetnextRequest request, CancellationTo
Connection.PubModelAsync(
subject: $"{_context.Opts.ApiPrefix}.CONSUMER.MSG.NEXT.{_stream}.{_consumer}",
data: request,
serializer: JsonNatsSerializer.Default,
serializer: NatsJsonSerializer.Default,
replyTo: Subject,
headers: default,
cancellationToken);
Expand Down Expand Up @@ -153,7 +153,7 @@ internal override IEnumerable<ICommand> GetReconnectCommands(int sid)
replyTo: Subject,
headers: default,
value: request,
serializer: JsonNatsSerializer.Default,
serializer: NatsJsonSerializer.Default,
cancellationToken: default);
}

Expand Down
4 changes: 2 additions & 2 deletions src/NATS.Client.JetStream/NatsJSSubFetch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public ValueTask CallMsgNextAsync(ConsumerGetnextRequest request, CancellationTo
Connection.PubModelAsync(
subject: $"{_context.Opts.ApiPrefix}.CONSUMER.MSG.NEXT.{_stream}.{_consumer}",
data: request,
serializer: JsonNatsSerializer.Default,
serializer: NatsJsonSerializer.Default,
replyTo: Subject,
headers: default,
cancellationToken);
Expand Down Expand Up @@ -126,7 +126,7 @@ internal override IEnumerable<ICommand> GetReconnectCommands(int sid)
replyTo: Subject,
headers: default,
value: request,
serializer: JsonNatsSerializer.Default,
serializer: NatsJsonSerializer.Default,
cancellationToken: default);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/NATS.Client.Core.Tests/LowLevelApiTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ await Retry.Until(
for (var i = 0; i < 10; i++)
{
var headers = new NatsHeaders { { "X-Test", $"value-{i}" } };
await nats.PubModelAsync<int>($"foo.data{i}", i, JsonNatsSerializer.Default, "bar", headers);
await nats.PubModelAsync<int>($"foo.data{i}", i, NatsJsonSerializer.Default, "bar", headers);
}

await nats.PubAsync("foo.done");
Expand Down
2 changes: 1 addition & 1 deletion tests/NATS.Client.Core.Tests/NatsConnectionTest.Auth.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ NatsOpts.Default with
{
AuthOpts = NatsAuthOpts.Default with
{
Nkey = "UALQSMXRSAA7ZXIGDDJBJ2JOYJVQIWM3LQVDM5KYIPG4EP3FAGJ47BOJ",
NKey = "UALQSMXRSAA7ZXIGDDJBJ2JOYJVQIWM3LQVDM5KYIPG4EP3FAGJ47BOJ",
Seed = "SUAAVWRZG6M5FA5VRRGWSCIHKTOJC7EWNIT4JV3FTOIPO4OBFR5WA7X5TE",
},
}),
Expand Down
Loading

0 comments on commit 443d4cd

Please sign in to comment.