-
Notifications
You must be signed in to change notification settings - Fork 74
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
Fixing Database emulation #278
Fixing Database emulation #278
Conversation
auth request mentions it only has a code and a message,
@@ -379,7 +379,7 @@ func _on_FirebaseAuth_request_completed(result : int, response_code : int, heade | |||
var json_result = JSON.parse(bod) | |||
if json_result.error != OK: | |||
Firebase._printerr("Error while parsing auth body json") | |||
emit_signal("auth_request", "Error while parsing auth body json", json_result) | |||
emit_signal("auth_request", ERR_PARSE_ERROR, "Error while parsing auth body json") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The documentation for this signal says error should have error codes and a message
# Refresh token countdown | ||
emit_signal("auth_request", 1, auth) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A token refresh is not an error, and 1 is expected on success
_base_url = _config.databaseURL | ||
else: | ||
var port : String = _config.emulators.ports.realtimeDatabase | ||
if port == "": | ||
Firebase._printerr("You are in 'emulated' mode, but the port for Realtime Database has not been configured.") | ||
else: | ||
_base_url = "http://localhost:{port}/?ns={projectId}".format({ port = port, projectId = _config.projectId }) + "/" | ||
_base_url = "http://localhost" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've moved the addition of the separator to the reference.gd
Also when using connect_to_host
the domain cannot contain path content (like a query string or port)
var port = -1 | ||
if Firebase.emulating: | ||
port = int(_config.emulators.ports.realtimeDatabase) | ||
_listener.connect_to_host(base_url, extended_url, port) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
-1 is the default value
The port must be passed as an additional parameter when using connect_to_host
if Firebase.emulating: | ||
remaining_path += "&ns="+_config.projectId+"-default-rtdb" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When emulating a namespace must be specified, it is your projectId + the extra content there.
func _get_list_url(with_port:bool = true) -> String: | ||
var url = Firebase.Database._base_url.trim_suffix(_separator) | ||
if with_port and Firebase.emulating: | ||
url += ":" + _config.emulators.ports.realtimeDatabase | ||
return url + _separator |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
defaults to expected behavior, but this could likely be better since with_port is only actually useful when emulating.
@@ -46,7 +46,7 @@ func _check_emulating() -> void : | |||
if port == "": | |||
Firebase._printerr("You are in 'emulated' mode, but the port for Dynamic Links has not been configured.") | |||
else: | |||
_base_url = "http://127.0.0.1:{port}/{version}/".format({ version = _API_VERSION, port = port }) | |||
_base_url = "http://localhost:{port}/{version}/".format({ version = _API_VERSION, port = port }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Noticed some spots using 127.0.0.1 instead of localhost
if not auth: | ||
Firebase._print("Unauthenticated request issued...") | ||
Firebase.Auth.login_anonymous() | ||
var result : Array = yield(Firebase.Auth, "auth_request") | ||
if result[0] != 1: | ||
_check_auth_error(result[0], result[1]) | ||
Firebase._print("Client connected as Anonymous") | ||
|
||
if not Firebase.emulating: | ||
task._headers = Array(task._headers) + [_AUTHORIZATION_HEADER + auth.idtoken] | ||
|
||
task._headers = Array(task._headers) + [_AUTHORIZATION_HEADER + auth.idtoken] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With changes to the auth.gd error codes this does not fail like it normally would.
func _parse_response_body(headers): | ||
var body = response_body.get_string_from_utf8() | ||
if body: | ||
var event_data = get_event_data(body) | ||
if event_data.event != "keep-alive" and event_data.event != continue_internal: | ||
var result = JSON.parse(event_data.data).result | ||
if response_body.size() > 0 and result: # stop here if the value doesn't parse | ||
response_body.resize(0) | ||
emit_signal("new_sse_event", headers, event_data.event, result) | ||
else: | ||
if event_data.event != continue_internal: | ||
response_body.resize(0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
moved this to it's own function (with headers just as a passthrough)
elif Firebase.emulating: | ||
# Emulation does not send the close connection header currently, so we need to manually read the response body | ||
# see issue https://github.com/firebase/firebase-tools/issues/3329 in firebase-tools | ||
# also comment https://github.com/GodotNuts/GodotFirebase/issues/154#issuecomment-831377763 which explains the issue | ||
while httpclient.connection.get_available_bytes(): | ||
var data = httpclient.connection.get_partial_data(1) | ||
if data[0] == OK: | ||
response_body.append_array(data[1]) | ||
if response_body.size() > 0: | ||
_parse_response_body(headers) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a workaround for emulators not sending the Connection: closed
header. This just looks to see if we still have data even if it wasn't detected by Godot and parses it.
"workarounds":{ | ||
"database_connection_closed_issue": false, # fixes https://github.com/firebase/firebase-tools/issues/3329 | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a flag for the workaround disabling it by default since it only effects emulation
@WolfgangSenff
@WolfgangSenff I ran this through our tools and it seems to be working fine. No errors when running the test harness |
I've added comments for all my code changes to better explain them.
Overall
Related issues:
(firebase/firebase-tools#3329)
#154