Skip to content

Commit

Permalink
Make pg-native an optional dependency and handle its absence gracefully
Browse files Browse the repository at this point in the history
  • Loading branch information
dannon committed Jan 15, 2025
1 parent 8621852 commit 2e1ca60
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 10 deletions.
23 changes: 20 additions & 3 deletions lib/createdb.js
Original file line number Diff line number Diff line change
@@ -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) {

Check failure on line 9 in lib/createdb.js

View workflow job for this annotation

GitHub Actions / build (14.x)

'err' is defined but never used

Check failure on line 9 in lib/createdb.js

View workflow job for this annotation

GitHub Actions / build (16.x)

'err' is defined but never used

Check failure on line 9 in lib/createdb.js

View workflow job for this annotation

GitHub Actions / build (18.x)

'err' is defined but never used
// pg-native not installed or cannot be loaded
postgresClient = null;
}

const sqlite3 = require("sqlite3");

args
.version(packageInfo.version)
Expand Down Expand Up @@ -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 (
Expand All @@ -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 {
Expand Down
27 changes: 21 additions & 6 deletions lib/mapper.js
Original file line number Diff line number Diff line change
@@ -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) {

Check failure on line 8 in lib/mapper.js

View workflow job for this annotation

GitHub Actions / build (14.x)

'err' is defined but never used

Check failure on line 8 in lib/mapper.js

View workflow job for this annotation

GitHub Actions / build (16.x)

'err' is defined but never used

Check failure on line 8 in lib/mapper.js

View workflow job for this annotation

GitHub Actions / build (18.x)

'err' is defined but never used
// pg-native not installed or cannot be loaded
postgresClient = null;
}

var updateFromJson = function (path, map) {
var content = fs.readFileSync(path, "utf8");
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -118,14 +129,18 @@ 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);
}

// 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;
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 2e1ca60

Please sign in to comment.