UNPKG

@electric-sql/cli

Version:

ElectricSQL command line interface (CLI).

73 lines 2.08 kB
import fs from "fs"; import { InvalidArgumentError } from "commander"; import { appPackageJsonPath } from './paths.js'; function getAppName() { return JSON.parse(fs.readFileSync(appPackageJsonPath, "utf8")).name; } function parseIntOrFail(str, error) { const parsed = parseInt(str); if (isNaN(parsed)) { throw new InvalidArgumentError(error); } return parsed; } function parsePort(str) { return parseIntOrFail( str, `Invalid port: ${str}. Must be integer between 1 and 65535.` ); } function parseTimeout(str) { return parseIntOrFail(str, `Invalid timeout: ${str}. Must be an integer.`); } function extractDatabaseURL(url) { const parsed = new URL(url); if (!(parsed.protocol === "postgres:" || parsed.protocol === "postgresql:")) { throw new Error(`Invalid database URL scheme: ${url}`); } const user = decodeURIComponent(parsed.username); if (!user) { throw new Error(`Invalid or missing username: ${url}`); } return { user, password: decodeURIComponent(parsed.password), host: decodeURIComponent(parsed.hostname), port: parsed.port ? parseInt(parsed.port) : null, dbName: decodeURIComponent(parsed.pathname.slice(1)) || user }; } function extractServiceURL(serviceUrl) { const parsed = new URL(serviceUrl); if (!parsed.hostname) { throw new Error(`Invalid service URL: ${serviceUrl}`); } return { host: decodeURIComponent(parsed.hostname), port: parsed.port ? parseInt(parsed.port) : null }; } function parsePgProxyPort(str) { if (typeof str === "number") { return { http: false, port: str }; } else if (str.includes(":")) { const [prefix, port] = str.split(":"); return { http: prefix.toLocaleLowerCase() === "http", port: parsePort(port) }; } else if (str.toLocaleLowerCase() === "http") { return { http: true, port: 65432 }; } else { return { http: false, port: parsePort(str) }; } } export { extractDatabaseURL, extractServiceURL, getAppName, parsePgProxyPort, parsePort, parseTimeout }; //# sourceMappingURL=parse.js.map