swc/bundler/tests/.cache/untrusted/6971d39e1246263b1aecf13a035727b1e046bea0.ts
강동윤 bbaf619f63
fix(bundler): Fix bugs (#1437)
swc_bundler:
 - [x] Fix wrapped esms. (denoland/deno#9307)
 - [x] Make test secure.
2021-03-02 17:33:03 +09:00

70 lines
1.7 KiB
TypeScript

// Loaded from https://raw.githubusercontent.com/denjucks/dex/master/lib/util/parse-connection.js
import url from '../deps/@jspm/core@1.1.0/nodelibs/url.js';
import parse from '../deps/pg-connection-string@2.2.0/index.js';
const parsePG = parse;
const isWindows = Deno.build.os === 'win';
export default function parseConnectionString(str) {
const parsed = url.parse(str, true);
let { protocol } = parsed;
const isDriveLetter = isWindows && protocol && protocol.length === 2;
if (protocol === null || isDriveLetter) {
return {
client: 'sqlite3',
connection: {
filename: str,
},
};
}
if (protocol.slice(-1) === ':') {
protocol = protocol.slice(0, -1);
}
const isPG = ['postgresql', 'postgres'].includes(protocol);
return {
client: protocol,
connection: isPG ? parsePG(str) : connectionObject(parsed),
};
};
function connectionObject(parsed) {
const connection = {};
let db = parsed.pathname;
if (db[0] === '/') {
db = db.slice(1);
}
connection.database = db;
if (parsed.hostname) {
if (parsed.protocol.indexOf('mssql') === 0) {
connection.server = parsed.hostname;
} else {
connection.host = parsed.hostname;
}
}
if (parsed.port) {
connection.port = parsed.port;
}
if (parsed.auth) {
const idx = parsed.auth.indexOf(':');
if (idx !== -1) {
connection.user = parsed.auth.slice(0, idx);
if (idx < parsed.auth.length - 1) {
connection.password = parsed.auth.slice(idx + 1);
}
} else {
connection.user = parsed.auth;
}
}
if (parsed.query) {
for (const key in parsed.query) {
connection[key] = parsed.query[key];
}
}
return connection;
}