MCPcopy Create free account
hub / github.com/denodrivers/postgres / parseConnectionUri

Function parseConnectionUri

utils/utils.ts:65–127  ·  view source on GitHub ↗
(uri: string)

Source from the content-addressed store, hash-verified

63 * The only exception to this rule are multi-host connection strings
64 */
65export function parseConnectionUri(uri: string): Uri {
66 const parsed_uri = uri.match(
67 /(?<driver>\w+):\/{2}((?<user>[^\/?#\s:]+?)?(:(?<password>[^\/?#\s]+)?)?@)?(?<full_host>[^\/?#\s]+)?(\/(?<path>[^?#\s]*))?(\?(?<params>[^#\s]+))?.*/,
68 );
69 if (!parsed_uri) throw new Error("Could not parse the provided URL");
70
71 let {
72 driver = "",
73 full_host = "",
74 params = "",
75 password = "",
76 path = "",
77 user = "",
78 }: ConnectionInfo = parsed_uri.groups ?? {};
79
80 const parsed_host = full_host.match(
81 /(?<host>(\[.+\])|(.*?))(:(?<port>[\w]*))?$/,
82 );
83 if (!parsed_host) throw new Error(`Could not parse "${full_host}" host`);
84
85 let {
86 host = "",
87 port = "",
88 }: ParsedHost = parsed_host.groups ?? {};
89
90 try {
91 if (host) {
92 host = decodeURIComponent(host);
93 }
94 } catch (_e) {
95 console.error(
96 bold(`${yellow("Failed to decode URL host")}\nDefaulting to raw host`),
97 );
98 }
99
100 if (port && Number.isNaN(Number(port))) {
101 throw new Error(`The provided port "${port}" is not a valid number`);
102 }
103
104 try {
105 if (password) {
106 password = decodeURIComponent(password);
107 }
108 } catch (_e) {
109 console.error(
110 bold(
111 `${
112 yellow("Failed to decode URL password")
113 }\nDefaulting to raw password`,
114 ),
115 );
116 }
117
118 return {
119 driver,
120 host,
121 params: Object.fromEntries(new URLSearchParams(params).entries()),
122 password,

Callers 2

parseOptionsFromUriFunction · 0.90
utils_test.tsFile · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected