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

Re-write InjectLiveReloadScriptAsync using async/await #6

Merged
Merged
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
32 changes: 18 additions & 14 deletions Westwind.AspnetCore.LiveReload/WebsocketScriptInjectionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,29 +49,33 @@ public static string InjectLiveReloadScript(string html, HttpContext context)
/// <param name="context"></param>
/// <param name="baseStream">The raw Response Stream</param>
/// <returns></returns>
public static Task InjectLiveReloadScriptAsync(byte[] buffer, HttpContext context, Stream baseStream)
public static async Task InjectLiveReloadScriptAsync(byte[] buffer, HttpContext context, Stream baseStream)
{
Span<byte> spanBuffer = buffer;
var index = buffer.LastIndexOf(_markerBytes);

var index = spanBuffer.LastIndexOf(_markerBytes);
if (index > -1)
return baseStream.WriteAsync(buffer, 0, buffer.Length);

index = spanBuffer.LastIndexOf(_bodyBytes.ToArray());
{
await baseStream.WriteAsync(buffer, 0, buffer.Length);
return;
}

index = buffer.LastIndexOf(_bodyBytes);
if (index == -1)
return baseStream.WriteAsync(buffer, 0, buffer.Length);
{
await baseStream.WriteAsync(buffer, 0, buffer.Length);
return;
}

var endIndex = index + _bodyBytes.Length;

return baseStream.WriteAsync(buffer, 0, index - 1)
.ContinueWith(tb =>
{
byte[] scriptBytes = Encoding.UTF8.GetBytes(GetWebSocketClientJavaScript(context));
return baseStream.WriteAsync(scriptBytes, 0, scriptBytes.Length);
})
.ContinueWith(tb => baseStream.WriteAsync(buffer, endIndex, buffer.Length - endIndex));
await baseStream.WriteAsync(buffer, 0, index - 1);
var scriptBytes = Encoding.UTF8.GetBytes(GetWebSocketClientJavaScript(context));
await baseStream.WriteAsync(scriptBytes, 0, scriptBytes.Length);
await baseStream.WriteAsync(buffer, endIndex, buffer.Length - endIndex);
}

static int LastIndexOf<T>(this T[] array, T[] sought) where T : IEquatable<T> =>
array.AsSpan().LastIndexOf(sought);

/// <summary>
/// Adds Live Reload WebSocket script into the page before the body tag.
Expand Down