feat: unit tests for security/utility functions (isPrivateIP, isTransientError, markdown, escapeHtml)
This commit is contained in:
parent
0a002f94ef
commit
50a163b12d
10 changed files with 224 additions and 62 deletions
|
|
@ -1,23 +1,9 @@
|
|||
import pg from "pg";
|
||||
|
||||
import logger from "./logger.js";
|
||||
import { isTransientError } from "../utils/errors.js";
|
||||
const { Pool } = pg;
|
||||
|
||||
// Transient error codes from PgBouncer / PostgreSQL that warrant retry
|
||||
const TRANSIENT_ERRORS = new Set([
|
||||
"ECONNRESET",
|
||||
"ECONNREFUSED",
|
||||
"EPIPE",
|
||||
"ETIMEDOUT",
|
||||
"CONNECTION_LOST",
|
||||
"57P01", // admin_shutdown
|
||||
"57P02", // crash_shutdown
|
||||
"57P03", // cannot_connect_now
|
||||
"08006", // connection_failure
|
||||
"08003", // connection_does_not_exist
|
||||
"08001", // sqlclient_unable_to_establish_sqlconnection
|
||||
]);
|
||||
|
||||
const pool = new Pool({
|
||||
host: process.env.DATABASE_HOST || "172.17.0.1",
|
||||
port: parseInt(process.env.DATABASE_PORT || "5432", 10),
|
||||
|
|
@ -38,23 +24,7 @@ pool.on("error", (err, client) => {
|
|||
logger.error({ err }, "Unexpected error on idle PostgreSQL client — evicted from pool");
|
||||
});
|
||||
|
||||
/**
|
||||
* Determine if an error is transient (PgBouncer failover, network blip)
|
||||
*/
|
||||
export function isTransientError(err: any): boolean {
|
||||
if (!err) return false;
|
||||
const code = err.code || "";
|
||||
const msg = (err.message || "").toLowerCase();
|
||||
|
||||
if (TRANSIENT_ERRORS.has(code)) return true;
|
||||
if (msg.includes("no available server")) return true; // PgBouncer specific
|
||||
if (msg.includes("connection terminated")) return true;
|
||||
if (msg.includes("connection refused")) return true;
|
||||
if (msg.includes("server closed the connection")) return true;
|
||||
if (msg.includes("timeout expired")) return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
export { isTransientError } from "../utils/errors.js";
|
||||
|
||||
/**
|
||||
* Execute a query with automatic retry on transient errors.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue