diff --git a/lib/createdb.js b/lib/createdb.js index 376bcf1..bafa424 100755 --- a/lib/createdb.js +++ b/lib/createdb.js @@ -1,8 +1,17 @@ #!/usr/bin/env node const args = require("commander"); const packageInfo = require("../package"); -var postgresClient = require("pg-native"); -var sqlite3 = require("sqlite3"); + +// Attempt to require "pg-native" as an optional dependency. +let postgresClient; +try { + postgresClient = require("pg-native"); +} catch (err) { + // pg-native not installed or cannot be loaded + postgresClient = null; +} + +const sqlite3 = require("sqlite3"); args .version(packageInfo.version) @@ -36,7 +45,13 @@ CREATE TABLE gxitproxy }; const createDbPostgres = function (sessions) { - const db = postgresClient(); + if (!postgresClient) { + console.error( + "Error: pg-native is not installed. Cannot create PostgreSQL database.", + ); + process.exit(1); + } + const db = new postgresClient(); db.connectSync(sessions); db.querySync(` CREATE TABLE IF NOT EXISTS gxitproxy ( @@ -53,6 +68,8 @@ const createDbPostgres = function (sessions) { const main = function (argv_) { const argv = argv_ || process.argv; args.parse(argv); + + // If sessions is a PostgreSQL connection string and pg-native is missing, exit. if (args.sessions.startsWith("postgresql://")) { createDbPostgres(args.sessions); } else { diff --git a/lib/mapper.js b/lib/mapper.js index 040ab8a..3d8e8fb 100644 --- a/lib/mapper.js +++ b/lib/mapper.js @@ -1,7 +1,14 @@ -var fs = require("fs"); -var sqlite3 = require("sqlite3"); -var postgresClient = require("pg-native"); -var watchFile = require("node-watch"); +const fs = require("fs"); +const sqlite3 = require("sqlite3"); +const watchFile = require("node-watch"); + +let postgresClient; +try { + postgresClient = require("pg-native"); +} catch (err) { + // pg-native not installed or cannot be loaded + postgresClient = null; +} var updateFromJson = function (path, map) { var content = fs.readFileSync(path, "utf8"); @@ -84,7 +91,11 @@ var updateFromSqlite = function (path, map) { }; var updateFromPostgres = function (path, map) { - var db = postgresClient(); + if (!postgresClient) { + console.error("Error: pg-native is not installed. Cannot update from PostgreSQL database."); + process.exit(1); + } + var db = new postgresClient(); var loadedSessions = {}; db.connectSync(path); @@ -118,6 +129,10 @@ var updateFromPostgres = function (path, map) { }; var watchPostgres = function (path, loadMap, pollingInterval) { + if (!postgresClient) { + console.error("Error: pg-native is not installed. Cannot watch PostgreSQL database."); + process.exit(1); + } // poll the database every `pollingInterval` seconds if (pollingInterval > 0) { setInterval(loadMap, pollingInterval); @@ -125,7 +140,7 @@ var watchPostgres = function (path, loadMap, pollingInterval) { // watch changes using PostgresSQL asynchronous notifications // (https://www.postgresql.org/docs/16/libpq-notify.html) - var db = postgresClient(); + var db = new postgresClient(); db.connect(path, function (err) { if (err) { throw err; diff --git a/package.json b/package.json index 6c39a2a..e30e321 100644 --- a/package.json +++ b/package.json @@ -29,9 +29,11 @@ "eventemitter3": "5.0.1", "http-proxy": "1.18.1", "node-watch": "0.7.4", - "pg-native": "^3.2.0", "sqlite3": "5.1.7" }, + "optionalDependencies": { + "pg-native": "^3.2.0" + }, "devDependencies": { "@eslint/js": "^9.17.0", "axios": "^1.7",