Allow URL encoded host in parseDSN #409
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR allows the connection string host to be url encoded, which is needed for Google Cloud run to connect to the UNIX socket.
When connecting to Google Cloud SQL from Google Cloud Run, the default and recommended way to is to use a provided UNIX socket located at
/cloudsql/<project-id>:<region-name>:<sql-instance>
. However, to connect to that socket, the host must be provided as a URL-encoded string like%2Fcloudsql%2F<project-id>%3A<region-name>%3Ar<sql-instance>
. Beforeslonik@25.1.0
, this worked properly because the connection URI was passed directly topg
. However, the newparseDSN
util no longer url-decodes the host before passing it in topg
, meaning that these url-encoded sockets will stay encoded and cannot be understood by pg, resulting in an "Address Not Found" warning.To fix this, this PR calls
decodeURICompenent
on the hostname before it is assigned tohost
. This should not cause any issues with existing urls that had no url-unsafe characters, and will allow users of GCP to connect from cloud run to cloud sql without needing to do additional setup to connect via TCP.I have tested this in a live Google Cloud Run instance and it works great! @gajus Please let me know if I'm missing anything here.