diff --git a/src/mono/sample/wasm/browser-advanced/Wasm.Advanced.Sample.csproj b/src/mono/sample/wasm/browser-advanced/Wasm.Advanced.Sample.csproj index fe3c5a2d63b480..70b1b0f9cd394d 100644 --- a/src/mono/sample/wasm/browser-advanced/Wasm.Advanced.Sample.csproj +++ b/src/mono/sample/wasm/browser-advanced/Wasm.Advanced.Sample.csproj @@ -16,4 +16,7 @@ + + + diff --git a/src/mono/sample/wasm/browser-advanced/main.js b/src/mono/sample/wasm/browser-advanced/main.js index 296de716612daf..175a26d6d137f1 100644 --- a/src/mono/sample/wasm/browser-advanced/main.js +++ b/src/mono/sample/wasm/browser-advanced/main.js @@ -7,6 +7,9 @@ function add(a, b) { return a + b; } +let testAbort = true; +let testError = true; + try { const { runtimeBuildInfo, setModuleImports, getAssemblyExports, runMain, getConfig, Module } = await dotnet .withElementOnExit() @@ -22,6 +25,21 @@ try { config.environmentVariables["MONO_LOG_LEVEL"] = "debug"; config.browserProfilerOptions = {}; }, + imports: { + fetch: (url, fetchArgs) => { + console.log("fetching " + url); + // we are testing that we can retry loading of the assembly + if (testAbort && url.indexOf('System.Private.Uri.dll') != -1) { + testAbort = false; + return fetch(url + "?testAbort=true", fetchArgs); + } + if (testError && url.indexOf('System.Console.dll') != -1) { + testError = false; + return fetch(url + "?testError=true", fetchArgs); + } + return fetch(url, fetchArgs); + } + }, preInit: () => { console.log('user code Module.preInit'); }, preRun: () => { console.log('user code Module.preRun'); }, onRuntimeInitialized: () => { diff --git a/src/mono/sample/wasm/simple-server/Program.cs b/src/mono/sample/wasm/simple-server/Program.cs index f6e1d4c3df9196..44da4ab762995d 100644 --- a/src/mono/sample/wasm/simple-server/Program.cs +++ b/src/mono/sample/wasm/simple-server/Program.cs @@ -156,19 +156,43 @@ private async void ServeAsync(HttpListenerContext context) string? contentType = null; if (path.EndsWith(".wasm")) contentType = "application/wasm"; - if (path.EndsWith(".webcil")) + if (path.EndsWith(".webcil") || path.EndsWith(".dll") || path.EndsWith(".pdb")) contentType = "application/octet-stream"; if (path.EndsWith(".json")) contentType = "application/json"; if (path.EndsWith(".js") || path.EndsWith(".mjs") || path.EndsWith(".cjs")) contentType = "text/javascript"; + var stream = context.Response.OutputStream; + + // test download re-try + if (url.Query.Contains("testError")) + { + Console.WriteLine("Faking 500 " + url); + context.Response.StatusCode = (int)HttpStatusCode.InternalServerError; + await stream.WriteAsync(buffer, 0, 0).ConfigureAwait(false); + await stream.FlushAsync(); + context.Response.Close(); + return; + } + if (contentType != null) context.Response.ContentType = contentType; context.Response.ContentLength64 = buffer.Length; context.Response.AppendHeader("cache-control", "public, max-age=31536000"); - var stream = context.Response.OutputStream; + + // test download re-try + if (url.Query.Contains("testAbort")) + { + Console.WriteLine("Faking abort " + url); + await stream.WriteAsync(buffer, 0, 10).ConfigureAwait(false); + await stream.FlushAsync(); + await Task.Delay(100); + context.Response.Abort(); + return; + } + try { await stream.WriteAsync(buffer).ConfigureAwait(false);