Skip to content

Commit

Permalink
decode only if not present in options
Browse files Browse the repository at this point in the history
  • Loading branch information
cirospaciari committed Feb 7, 2025
1 parent 9dca487 commit e6505b3
Showing 1 changed file with 6 additions and 14 deletions.
20 changes: 6 additions & 14 deletions src/js/bun/sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1058,10 +1058,9 @@ class SQLArrayParameter {
}
}

function getDBFromURL(url) {
const name = (url?.pathname ?? "").slice(1);
if (name) {
return decodeURIComponent(name);
function decodeIfValid(value) {
if (value) {
return decodeURIComponent(value);
}
return null;
}
Expand Down Expand Up @@ -1123,8 +1122,8 @@ function loadOptions(o) {
// object overrides url
hostname ||= url.hostname;
port ||= url.port;
username ||= url.username;
password ||= url.password;
username ||= decodeIfValid(url.username);
password ||= decodeIfValid(url.password);
adapter ||= url.protocol;

if (adapter[adapter.length - 1] === ":") {
Expand All @@ -1141,18 +1140,11 @@ function loadOptions(o) {
}
}
query = query.trim();
// handle special characters in username, password, and database
if (username) {
username = decodeURIComponent(username);
}
if (password) {
password = decodeURIComponent(password);
}
}
hostname ||= o.hostname || o.host || env.PGHOST || "localhost";
port ||= Number(o.port || env.PGPORT || 5432);
username ||= o.username || o.user || env.PGUSERNAME || env.PGUSER || env.USER || env.USERNAME || "postgres";
database ||= o.database || o.db || getDBFromURL(url) || env.PGDATABASE || username;
database ||= o.database || o.db || decodeIfValid((url?.pathname ?? "").slice(1)) || env.PGDATABASE || username;
password ||= o.password || o.pass || env.PGPASSWORD || "";

tls ||= o.tls || o.ssl;
Expand Down

0 comments on commit e6505b3

Please sign in to comment.