Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use some utf8 string literals #6591

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,7 @@ namespace HotChocolate.AspNetCore.Subscriptions.Protocols.Apollo;

internal static class Utf8MessageBodies
{
private static readonly byte[] _keepAlive =
{
(byte)'{',
(byte)'"',
(byte)'t',
(byte)'y',
(byte)'p',
(byte)'e',
(byte)'"',
(byte)':',
(byte)'"',
(byte)'k',
(byte)'a',
(byte)'"',
(byte)'}'
};
private static readonly byte[] _keepAlive = "{\"type\":\"ka\"}"u8.ToArray();

public static ReadOnlyMemory<byte> KeepAlive => _keepAlive;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ internal static class Utf8MessageProperties
// This uses C# compiler's ability to refer to static data directly.
// For more information see https://vcsjones.dev/2019/02/01/csharp-readonly-span-bytes-static
public static ReadOnlySpan<byte> Id
=> new[] { (byte)'i', (byte)'d' };
=> "id"u8;

public static ReadOnlySpan<byte> Type
=> new[] { (byte)'t', (byte)'y', (byte)'p', (byte)'e' };
=> "type"u8;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,138 +4,29 @@ internal static class Utf8Messages
{
// This uses C# compiler's ability to refer to static data directly. For more information see https://vcsjones.dev/2019/02/01/csharp-readonly-span-bytes-static
public static ReadOnlySpan<byte> ConnectionInitialize =>
new[]
{
(byte)'c',
(byte)'o',
(byte)'n',
(byte)'n',
(byte)'e',
(byte)'c',
(byte)'t',
(byte)'i',
(byte)'o',
(byte)'n',
(byte)'_',
(byte)'i',
(byte)'n',
(byte)'i',
(byte)'t'
};
"connection_init"u8;

public static ReadOnlySpan<byte> ConnectionAccept =>
new[]
{
(byte)'c',
(byte)'o',
(byte)'n',
(byte)'n',
(byte)'e',
(byte)'c',
(byte)'t',
(byte)'i',
(byte)'o',
(byte)'n',
(byte)'_',
(byte)'a',
(byte)'c',
(byte)'k'
};
"connection_ack"u8;

public static ReadOnlySpan<byte> ConnectionError =>
new[]
{
(byte)'c',
(byte)'o',
(byte)'n',
(byte)'n',
(byte)'e',
(byte)'c',
(byte)'t',
(byte)'i',
(byte)'o',
(byte)'n',
(byte)'_',
(byte)'e',
(byte)'r',
(byte)'r',
(byte)'o',
(byte)'r'
};
"connection_error"u8;

public static ReadOnlySpan<byte> ConnectionTerminate =>
new[]
{
(byte)'c',
(byte)'o',
(byte)'n',
(byte)'n',
(byte)'e',
(byte)'c',
(byte)'t',
(byte)'i',
(byte)'o',
(byte)'n',
(byte)'_',
(byte)'t',
(byte)'e',
(byte)'r',
(byte)'m',
(byte)'i',
(byte)'n',
(byte)'a',
(byte)'t',
(byte)'e'
};
"connection_terminate"u8;

public static ReadOnlySpan<byte> Start =>
new[]
{
(byte)'s',
(byte)'t',
(byte)'a',
(byte)'r',
(byte)'t'
};
"start"u8;

public static ReadOnlySpan<byte> Stop =>
new[]
{
(byte)'s',
(byte)'t',
(byte)'o',
(byte)'p',
};
"stop"u8;

public static ReadOnlySpan<byte> Data =>
new[]
{
(byte)'d',
(byte)'a',
(byte)'t',
(byte)'a',
};
"data"u8;

public static ReadOnlySpan<byte> Error =>
new[]
{
(byte)'e',
(byte)'r',
(byte)'r',
(byte)'o',
(byte)'r'
};
"error"u8;

public static ReadOnlySpan<byte> Complete =>
new[]
{
(byte)'c',
(byte)'o',
(byte)'m',
(byte)'p',
(byte)'l',
(byte)'e',
(byte)'t',
(byte)'e'
};
"complete"u8;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,9 @@ namespace HotChocolate.AspNetCore.Subscriptions.Protocols.GraphQLOverWebSocket;

internal static class Utf8MessageBodies
{
private static readonly byte[] _defaultPong =
{
(byte)'{',
(byte)'"',
(byte)'t',
(byte)'y',
(byte)'p',
(byte)'e',
(byte)'"',
(byte)':',
(byte)'"',
(byte)'p',
(byte)'o',
(byte)'n',
(byte)'g',
(byte)'"',
(byte)'}',
};
private static readonly byte[] _defaultPong = "{\"type\":\"pong\"}"u8.ToArray();

private static readonly byte[] _defaultPing =
{
(byte)'{',
(byte)'"',
(byte)'t',
(byte)'y',
(byte)'p',
(byte)'e',
(byte)'"',
(byte)':',
(byte)'"',
(byte)'p',
(byte)'i',
(byte)'n',
(byte)'g',
(byte)'"',
(byte)'}',
};
private static readonly byte[] _defaultPing = "{\"type\":\"ping\"}"u8.ToArray();

public static ReadOnlyMemory<byte> DefaultPing => _defaultPing;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ internal static class Utf8MessageProperties
// This uses C# compiler's ability to refer to static data directly.
// For more information see https://vcsjones.dev/2019/02/01/csharp-readonly-span-bytes-static
public static ReadOnlySpan<byte> Type
=> new[] { (byte)'t', (byte)'y', (byte)'p', (byte)'e' };
=> "type"u8;

public static ReadOnlySpan<byte> Payload
=> new[] { (byte)'p', (byte)'a', (byte)'y', (byte)'l', (byte)'o', (byte)'a', (byte)'d' };
=> "payload"u8;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,105 +4,26 @@ internal static class Utf8Messages
{
// This uses C# compiler's ability to refer to static data directly. For more information see https://vcsjones.dev/2019/02/01/csharp-readonly-span-bytes-static
public static ReadOnlySpan<byte> ConnectionInitialize =>
new[]
{
(byte)'c',
(byte)'o',
(byte)'n',
(byte)'n',
(byte)'e',
(byte)'c',
(byte)'t',
(byte)'i',
(byte)'o',
(byte)'n',
(byte)'_',
(byte)'i',
(byte)'n',
(byte)'i',
(byte)'t'
};
"connection_init"u8;

public static ReadOnlySpan<byte> ConnectionAccept =>
new[]
{
(byte)'c',
(byte)'o',
(byte)'n',
(byte)'n',
(byte)'e',
(byte)'c',
(byte)'t',
(byte)'i',
(byte)'o',
(byte)'n',
(byte)'_',
(byte)'a',
(byte)'c',
(byte)'k'
};
"connection_ack"u8;

public static ReadOnlySpan<byte> Subscribe =>
new[]
{
(byte)'s',
(byte)'u',
(byte)'b',
(byte)'s',
(byte)'c',
(byte)'r',
(byte)'i',
(byte)'b',
(byte)'e'
};
"subscribe"u8;

public static ReadOnlySpan<byte> Next =>
new[]
{
(byte)'n',
(byte)'e',
(byte)'x',
(byte)'t'
};
"next"u8;

public static ReadOnlySpan<byte> Error =>
new[]
{
(byte)'e',
(byte)'r',
(byte)'r',
(byte)'o',
(byte)'r'
};
"error"u8;

public static ReadOnlySpan<byte> Complete =>
new[]
{
(byte)'c',
(byte)'o',
(byte)'m',
(byte)'p',
(byte)'l',
(byte)'e',
(byte)'t',
(byte)'e'
};
"complete"u8;

public static ReadOnlySpan<byte> Ping =>
new[]
{
(byte)'p',
(byte)'i',
(byte)'n',
(byte)'g'
};
"ping"u8;

public static ReadOnlySpan<byte> Pong =>
new[]
{
(byte)'p',
(byte)'o',
(byte)'n',
(byte)'g'
};
"pong"u8;
}
Loading
Loading