From a87f878f77715eec439894dc2ec645666c350f53 Mon Sep 17 00:00:00 2001 From: Fabio Alessandrelli Date: Sun, 7 Apr 2024 17:57:33 +0200 Subject: [PATCH 1/3] [Web] Fix browser opening too early with serve.py --- platform/web/serve.py | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/platform/web/serve.py b/platform/web/serve.py index f0b0ec962271..4e1521449b0d 100755 --- a/platform/web/serve.py +++ b/platform/web/serve.py @@ -6,7 +6,7 @@ import socket import subprocess import sys -from http.server import HTTPServer, SimpleHTTPRequestHandler, test # type: ignore +from http.server import HTTPServer, SimpleHTTPRequestHandler from pathlib import Path @@ -38,12 +38,24 @@ def shell_open(url): def serve(root, port, run_browser): os.chdir(root) + address = ("", port) + httpd = DualStackServer(address, CORSRequestHandler) + + url = f"http://127.0.0.1:{port}" if run_browser: # Open the served page in the user's default browser. - print("Opening the served URL in the default browser (use `--no-browser` or `-n` to disable this).") - shell_open(f"http://127.0.0.1:{port}") + print(f"Opening the served URL in the default browser (use `--no-browser` or `-n` to disable this): {url}") + shell_open(url) + else: + print(f"Serving at: {url}") - test(CORSRequestHandler, DualStackServer, port=port) + try: + httpd.serve_forever() + except KeyboardInterrupt: + print("\nKeyboard interrupt received, stopping server.") + finally: + # Clean-up server + httpd.server_close() if __name__ == "__main__": From 9b8a5196c213424a89cff2274861801b21b98954 Mon Sep 17 00:00:00 2001 From: Adam Scott Date: Wed, 2 Oct 2024 11:22:07 -0400 Subject: [PATCH 2/3] [Web] Make audio bus fetching more resilient to errors --- platform/web/js/libs/library_godot_audio.js | 38 +++++++++++++++++---- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/platform/web/js/libs/library_godot_audio.js b/platform/web/js/libs/library_godot_audio.js index 40fb0c356ca0..aaf986b9661b 100644 --- a/platform/web/js/libs/library_godot_audio.js +++ b/platform/web/js/libs/library_godot_audio.js @@ -868,7 +868,10 @@ class Bus { * @returns {void} */ static move(fromIndex, toIndex) { - const movedBus = GodotAudio.Bus.getBus(fromIndex); + const movedBus = GodotAudio.Bus.getBusOrNull(fromIndex); + if (movedBus == null) { + return; + } const buses = GodotAudio.buses.filter((_, i) => i !== fromIndex); // Inserts at index. buses.splice(toIndex - 1, 0, movedBus); @@ -1424,7 +1427,10 @@ const _GodotAudio = { * @returns {void} */ remove_sample_bus: function (index) { - const bus = GodotAudio.Bus.getBus(index); + const bus = GodotAudio.Bus.getBusOrNull(index); + if (bus == null) { + return; + } bus.clear(); }, @@ -1454,8 +1460,17 @@ const _GodotAudio = { * @returns {void} */ set_sample_bus_send: function (busIndex, sendIndex) { - const bus = GodotAudio.Bus.getBus(busIndex); - bus.setSend(GodotAudio.Bus.getBus(sendIndex)); + const bus = GodotAudio.Bus.getBusOrNull(busIndex); + if (bus == null) { + // Cannot send from an invalid bus. + return; + } + let targetBus = GodotAudio.Bus.getBusOrNull(sendIndex); + if (targetBus == null) { + // Send to master. + targetBus = GodotAudio.Bus.getBus(0); + } + bus.setSend(targetBus); }, /** @@ -1465,7 +1480,10 @@ const _GodotAudio = { * @returns {void} */ set_sample_bus_volume_db: function (busIndex, volumeDb) { - const bus = GodotAudio.Bus.getBus(busIndex); + const bus = GodotAudio.Bus.getBusOrNull(busIndex); + if (bus == null) { + return; + } bus.setVolumeDb(volumeDb); }, @@ -1476,7 +1494,10 @@ const _GodotAudio = { * @returns {void} */ set_sample_bus_solo: function (busIndex, enable) { - const bus = GodotAudio.Bus.getBus(busIndex); + const bus = GodotAudio.Bus.getBusOrNull(busIndex); + if (bus == null) { + return; + } bus.solo(enable); }, @@ -1487,7 +1508,10 @@ const _GodotAudio = { * @returns {void} */ set_sample_bus_mute: function (busIndex, enable) { - const bus = GodotAudio.Bus.getBus(busIndex); + const bus = GodotAudio.Bus.getBusOrNull(busIndex); + if (bus == null) { + return; + } bus.mute(enable); }, }, From 762094c963450441d9c0adf008f517e1f0e39273 Mon Sep 17 00:00:00 2001 From: Robbie Lodico Date: Tue, 22 Oct 2024 15:38:56 -0400 Subject: [PATCH 3/3] Fix GodotFetch glue code for null response bodies The spec says that Response.body can be null (in the event of requests that should have no body, like HEAD requests) and Firefox adheres to it which results in request failure for HEAD requests on Firefox for web exports. This commit addresses that by treating a null body as an "empty" body (without using a polyfill) and avoids changing the request lifecycle as much as possible. PR review changes: - Use == instead of strict === - Do not use ?? null - Comment formatting --- platform/web/js/libs/library_godot_fetch.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/platform/web/js/libs/library_godot_fetch.js b/platform/web/js/libs/library_godot_fetch.js index 00616bc1a5d4..eeb397825648 100644 --- a/platform/web/js/libs/library_godot_fetch.js +++ b/platform/web/js/libs/library_godot_fetch.js @@ -59,7 +59,12 @@ const GodotFetch = { }); obj.status = response.status; obj.response = response; - obj.reader = response.body.getReader(); + // `body` can be null per spec (for example, in cases where the request method is HEAD). + // As of the time of writing, Chromium (127.0.6533.72) does not follow the spec but Firefox (131.0.3) does. + // See godotengine/godot#76825 for more information. + // See Chromium revert (of the change to follow the spec): + // https://chromium.googlesource.com/chromium/src/+/135354b7bdb554cd03c913af7c90aceead03c4d4 + obj.reader = response.body?.getReader(); obj.chunked = chunked; }, @@ -121,6 +126,10 @@ const GodotFetch = { } obj.reading = true; obj.reader.read().then(GodotFetch.onread.bind(null, id)).catch(GodotFetch.onerror.bind(null, id)); + } else if (obj.reader == null && obj.response.body == null) { + // Emulate a stream closure to maintain the request lifecycle. + obj.reading = true; + GodotFetch.onread(id, { value: undefined, done: true }); } }, }, @@ -159,7 +168,10 @@ const GodotFetch = { if (!obj.response) { return 0; } - if (obj.reader) { + // If the reader is nullish, but there is no body, and the request is not marked as done, + // the same status should be returned as though the request is currently being read + // so that the proper lifecycle closure can be handled in `read()`. + if (obj.reader || (obj.response.body == null && !obj.done)) { return 1; } if (obj.done) {