-
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathconnection-string-parser.ts
More file actions
168 lines (145 loc) · 5.23 KB
/
Copy pathconnection-string-parser.ts
File metadata and controls
168 lines (145 loc) · 5.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import { DatabaseType } from "@/lib/types";
export interface ParsedConnection {
type: DatabaseType;
host?: string;
port?: string;
user?: string;
password?: string;
database?: string;
connectionString?: string;
}
/**
* Parse a database connection string URL into its components.
* Supports: postgres://, postgresql://, mysql://, mongodb://, mongodb+srv://, redis://
*/
export function parseConnectionString(input: string): ParsedConnection | null {
const trimmed = input.trim();
if (!trimmed) return null;
// MongoDB connection strings
if (trimmed.startsWith("mongodb://") || trimmed.startsWith("mongodb+srv://")) {
return parseMongoDBString(trimmed);
}
// PostgreSQL
if (trimmed.startsWith("postgres://") || trimmed.startsWith("postgresql://")) {
return parseGenericURL(trimmed, "postgres", "5432");
}
// MySQL
if (trimmed.startsWith("mysql://")) {
return parseGenericURL(trimmed, "mysql", "3306");
}
// Redis
if (trimmed.startsWith("redis://") || trimmed.startsWith("rediss://")) {
return parseGenericURL(trimmed, "redis", "6379");
}
// Oracle
if (trimmed.startsWith("oracle://")) {
return parseGenericURL(trimmed, "oracle", "1521");
}
// MSSQL / SQL Server
if (trimmed.startsWith("mssql://") || trimmed.startsWith("sqlserver://")) {
return parseGenericURL(trimmed, "mssql", "1433");
}
// ADO.NET format: Server=host;Database=db;User Id=user;Password=pass;
if (/^Server\s*=/i.test(trimmed)) {
return parseADONetString(trimmed);
}
return null;
}
function parseMongoDBString(uri: string): ParsedConnection {
const result: ParsedConnection = {
type: "mongodb",
connectionString: uri,
};
try {
// For mongodb+srv, we can't use URL directly for host/port
// but we can extract user/pass/database
const isSRV = uri.startsWith("mongodb+srv://");
// Extract database from path
const withoutProtocol = uri.replace(/^mongodb(\+srv)?:\/\//, "");
const atIndex = withoutProtocol.indexOf("@");
const afterAuth = atIndex >= 0 ? withoutProtocol.slice(atIndex + 1) : withoutProtocol;
// Split host(s) from path
const slashIndex = afterAuth.indexOf("/");
if (slashIndex >= 0) {
const pathPart = afterAuth.slice(slashIndex + 1);
const dbName = pathPart.split("?")[0];
if (dbName) result.database = decodeURIComponent(dbName);
}
// Extract credentials
if (atIndex >= 0) {
const authPart = withoutProtocol.slice(0, atIndex);
const colonIndex = authPart.indexOf(":");
if (colonIndex >= 0) {
result.user = decodeURIComponent(authPart.slice(0, colonIndex));
result.password = decodeURIComponent(authPart.slice(colonIndex + 1));
} else {
result.user = decodeURIComponent(authPart);
}
}
// Extract host/port for non-SRV
if (!isSRV && slashIndex >= 0) {
const hostPart = afterAuth.slice(0, slashIndex);
const firstHost = hostPart.split(",")[0]; // take first host for replica sets
const [host, port] = firstHost.split(":");
if (host) result.host = host;
if (port) result.port = port;
}
} catch {
// If parsing fails, we still have the connectionString
}
return result;
}
function parseADONetString(input: string): ParsedConnection | null {
try {
const params: Record<string, string> = {};
input.split(";").forEach((part) => {
const eq = part.indexOf("=");
if (eq > 0) {
const key = part.slice(0, eq).trim().toLowerCase();
const val = part.slice(eq + 1).trim();
params[key] = val;
}
});
const host = params["server"] || params["data source"] || "localhost";
const [hostPart, portPart] = host.split(",");
return {
type: "mssql",
host: hostPart || "localhost",
port: portPart || "1433",
user: params["user id"] || params["uid"] || undefined,
password: params["password"] || params["pwd"] || undefined,
database: params["database"] || params["initial catalog"] || undefined,
};
} catch {
return null;
}
}
function parseGenericURL(uri: string, type: DatabaseType, defaultPort: string): ParsedConnection | null {
try {
const url = new URL(uri);
return {
type,
host: url.hostname || "localhost",
port: url.port || defaultPort,
user: url.username ? decodeURIComponent(url.username) : undefined,
password: url.password ? decodeURIComponent(url.password) : undefined,
database: url.pathname.slice(1) || undefined, // remove leading /
};
} catch {
return null;
}
}
/**
* Detect the database type from a connection string.
*/
export function detectConnectionStringType(input: string): DatabaseType | null {
const trimmed = input.trim().toLowerCase();
if (trimmed.startsWith("postgres://") || trimmed.startsWith("postgresql://")) return "postgres";
if (trimmed.startsWith("mysql://")) return "mysql";
if (trimmed.startsWith("mongodb://") || trimmed.startsWith("mongodb+srv://")) return "mongodb";
if (trimmed.startsWith("redis://") || trimmed.startsWith("rediss://")) return "redis";
if (trimmed.startsWith("oracle://")) return "oracle";
if (trimmed.startsWith("mssql://") || trimmed.startsWith("sqlserver://")) return "mssql";
if (/^server\s*=/i.test(trimmed)) return "mssql";
return null;
}