+2
-2
| // Main exports from pg-cache package | ||
| export { close, pgCache, PgPoolCacheManager, teardownPgPools } from './lru'; | ||
| export { buildConnectionString, getPgPool } from './pg'; | ||
| export { close, getPgCacheConfig, pgCache, PgPoolCacheManager, teardownPgPools } from './lru'; | ||
| export { buildConnectionString, getPgPool, getPgPoolConfig } from './pg'; |
+35
-10
@@ -9,2 +9,21 @@ import { Logger } from '@pgpmjs/logger'; | ||
| const SYS_EVENTS = ['SIGTERM']; | ||
| const parseEnvInt = (val, fallback) => { | ||
| if (!val) | ||
| return fallback; | ||
| const n = parseInt(val, 10); | ||
| return isNaN(n) ? fallback : n; | ||
| }; | ||
| /** | ||
| * Read cache configuration from environment variables. | ||
| * | ||
| * Supports: | ||
| * - PG_CACHE_MAX: Maximum number of pools (default: 50) | ||
| * - PG_CACHE_TTL_MS: TTL in milliseconds (default: ONE_YEAR) | ||
| */ | ||
| export function getPgCacheConfig() { | ||
| return { | ||
| max: parseEnvInt(process.env.PG_CACHE_MAX, 50), | ||
| ttl: parseEnvInt(process.env.PG_CACHE_TTL_MS, ONE_YEAR), | ||
| }; | ||
| } | ||
| class ManagedPgPool { | ||
@@ -45,12 +64,18 @@ pool; | ||
| cleanupCallbacks = new Set(); | ||
| pgCache = new LRUCache({ | ||
| max: 10, | ||
| ttl: ONE_YEAR, | ||
| updateAgeOnGet: true, | ||
| dispose: (managedPool, key, reason) => { | ||
| log.debug(`Disposing pg pool [${key}] (${reason})`); | ||
| this.notifyCleanup(key); | ||
| this.disposePool(managedPool); | ||
| } | ||
| }); | ||
| config; | ||
| pgCache; | ||
| constructor(config) { | ||
| const defaults = getPgCacheConfig(); | ||
| this.config = { ...defaults, ...config }; | ||
| this.pgCache = new LRUCache({ | ||
| max: this.config.max, | ||
| ttl: this.config.ttl, | ||
| updateAgeOnGet: true, | ||
| dispose: (managedPool, key, reason) => { | ||
| log.debug(`Disposing pg pool [${key}] (${reason})`); | ||
| this.notifyCleanup(key); | ||
| this.disposePool(managedPool); | ||
| } | ||
| }); | ||
| } | ||
| // Register a cleanup callback to be called when pools are disposed | ||
@@ -57,0 +82,0 @@ registerCleanupCallback(callback) { |
+24
-1
@@ -7,2 +7,24 @@ import pg from 'pg'; | ||
| export const buildConnectionString = (user, password, host, port, database) => `postgres://${user}:${password}@${host}:${port}/${database}`; | ||
| const parseEnvInt = (val, fallback) => { | ||
| if (!val) | ||
| return fallback; | ||
| const n = parseInt(val, 10); | ||
| return isNaN(n) ? fallback : n; | ||
| }; | ||
| /** | ||
| * Read per-pool configuration from environment variables. | ||
| * | ||
| * Supports: | ||
| * - PG_POOL_MAX: Maximum clients per pool (default: 5) | ||
| * - PG_POOL_IDLE_TIMEOUT_MS: Close idle clients after ms (default: 30000) | ||
| * - PG_POOL_CONNECTION_TIMEOUT_MS: Fail connect() after ms (default: 5000) | ||
| */ | ||
| export function getPgPoolConfig(overrides) { | ||
| return { | ||
| max: overrides?.max ?? parseEnvInt(process.env.PG_POOL_MAX, 5), | ||
| idleTimeoutMillis: overrides?.idleTimeoutMillis ?? parseEnvInt(process.env.PG_POOL_IDLE_TIMEOUT_MS, 30000), | ||
| connectionTimeoutMillis: overrides?.connectionTimeoutMillis ?? parseEnvInt(process.env.PG_POOL_CONNECTION_TIMEOUT_MS, 5000), | ||
| ...(overrides?.allowExitOnIdle !== undefined && { allowExitOnIdle: overrides.allowExitOnIdle }), | ||
| }; | ||
| } | ||
| export const getPgPool = (pgConfig) => { | ||
@@ -17,3 +39,4 @@ const config = getPgEnvOptions(pgConfig); | ||
| const connectionString = buildConnectionString(user, password, host, port, database); | ||
| const pgPool = new pg.Pool({ connectionString }); | ||
| const poolConfig = getPgPoolConfig(pgConfig.pool); | ||
| const pgPool = new pg.Pool({ connectionString, ...poolConfig }); | ||
| /** | ||
@@ -20,0 +43,0 @@ * IMPORTANT: Pool-level error handler for idle connection errors. |
+3
-3
@@ -1,3 +0,3 @@ | ||
| export { close, pgCache, PgPoolCacheManager, teardownPgPools } from './lru'; | ||
| export { buildConnectionString, getPgPool } from './pg'; | ||
| export type { PoolCleanupCallback } from './lru'; | ||
| export { close, getPgCacheConfig, pgCache, PgPoolCacheManager, teardownPgPools } from './lru'; | ||
| export { buildConnectionString, getPgPool, getPgPoolConfig } from './pg'; | ||
| export type { PgCacheConfig, PoolCleanupCallback } from './lru'; |
+3
-1
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| exports.getPgPool = exports.buildConnectionString = exports.teardownPgPools = exports.PgPoolCacheManager = exports.pgCache = void 0; | ||
| exports.getPgPoolConfig = exports.getPgPool = exports.buildConnectionString = exports.teardownPgPools = exports.PgPoolCacheManager = exports.pgCache = exports.getPgCacheConfig = void 0; | ||
| // Main exports from pg-cache package | ||
| var lru_1 = require("./lru"); | ||
| Object.defineProperty(exports, "close", { enumerable: true, get: function () { return lru_1.close; } }); | ||
| Object.defineProperty(exports, "getPgCacheConfig", { enumerable: true, get: function () { return lru_1.getPgCacheConfig; } }); | ||
| Object.defineProperty(exports, "pgCache", { enumerable: true, get: function () { return lru_1.pgCache; } }); | ||
@@ -13,1 +14,2 @@ Object.defineProperty(exports, "PgPoolCacheManager", { enumerable: true, get: function () { return lru_1.PgPoolCacheManager; } }); | ||
| Object.defineProperty(exports, "getPgPool", { enumerable: true, get: function () { return pg_1.getPgPool; } }); | ||
| Object.defineProperty(exports, "getPgPoolConfig", { enumerable: true, get: function () { return pg_1.getPgPoolConfig; } }); |
+16
-0
| import pg from 'pg'; | ||
| type PgPoolKey = string; | ||
| export type PoolCleanupCallback = (pgPoolKey: string) => void; | ||
| export interface PgCacheConfig { | ||
| /** Maximum number of pools in the LRU cache (env: PG_CACHE_MAX, default: 50) */ | ||
| max: number; | ||
| /** TTL for cached pools in ms (default: ONE_YEAR) */ | ||
| ttl: number; | ||
| } | ||
| /** | ||
| * Read cache configuration from environment variables. | ||
| * | ||
| * Supports: | ||
| * - PG_CACHE_MAX: Maximum number of pools (default: 50) | ||
| * - PG_CACHE_TTL_MS: TTL in milliseconds (default: ONE_YEAR) | ||
| */ | ||
| export declare function getPgCacheConfig(): PgCacheConfig; | ||
| export declare class PgPoolCacheManager { | ||
@@ -8,3 +22,5 @@ private cleanupTasks; | ||
| private cleanupCallbacks; | ||
| readonly config: PgCacheConfig; | ||
| private readonly pgCache; | ||
| constructor(config?: Partial<PgCacheConfig>); | ||
| registerCleanupCallback(callback: PoolCleanupCallback): () => void; | ||
@@ -11,0 +27,0 @@ get(key: PgPoolKey): pg.Pool | undefined; |
+36
-10
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| exports.teardownPgPools = exports.close = exports.pgCache = exports.PgPoolCacheManager = void 0; | ||
| exports.getPgCacheConfig = getPgCacheConfig; | ||
| const logger_1 = require("@pgpmjs/logger"); | ||
@@ -12,2 +13,21 @@ const lru_cache_1 = require("lru-cache"); | ||
| const SYS_EVENTS = ['SIGTERM']; | ||
| const parseEnvInt = (val, fallback) => { | ||
| if (!val) | ||
| return fallback; | ||
| const n = parseInt(val, 10); | ||
| return isNaN(n) ? fallback : n; | ||
| }; | ||
| /** | ||
| * Read cache configuration from environment variables. | ||
| * | ||
| * Supports: | ||
| * - PG_CACHE_MAX: Maximum number of pools (default: 50) | ||
| * - PG_CACHE_TTL_MS: TTL in milliseconds (default: ONE_YEAR) | ||
| */ | ||
| function getPgCacheConfig() { | ||
| return { | ||
| max: parseEnvInt(process.env.PG_CACHE_MAX, 50), | ||
| ttl: parseEnvInt(process.env.PG_CACHE_TTL_MS, ONE_YEAR), | ||
| }; | ||
| } | ||
| class ManagedPgPool { | ||
@@ -48,12 +68,18 @@ pool; | ||
| cleanupCallbacks = new Set(); | ||
| pgCache = new lru_cache_1.LRUCache({ | ||
| max: 10, | ||
| ttl: ONE_YEAR, | ||
| updateAgeOnGet: true, | ||
| dispose: (managedPool, key, reason) => { | ||
| log.debug(`Disposing pg pool [${key}] (${reason})`); | ||
| this.notifyCleanup(key); | ||
| this.disposePool(managedPool); | ||
| } | ||
| }); | ||
| config; | ||
| pgCache; | ||
| constructor(config) { | ||
| const defaults = getPgCacheConfig(); | ||
| this.config = { ...defaults, ...config }; | ||
| this.pgCache = new lru_cache_1.LRUCache({ | ||
| max: this.config.max, | ||
| ttl: this.config.ttl, | ||
| updateAgeOnGet: true, | ||
| dispose: (managedPool, key, reason) => { | ||
| log.debug(`Disposing pg pool [${key}] (${reason})`); | ||
| this.notifyCleanup(key); | ||
| this.disposePool(managedPool); | ||
| } | ||
| }); | ||
| } | ||
| // Register a cleanup callback to be called when pools are disposed | ||
@@ -60,0 +86,0 @@ registerCleanupCallback(callback) { |
+4
-4
| { | ||
| "name": "pg-cache", | ||
| "version": "3.10.1", | ||
| "version": "3.11.0", | ||
| "author": "Constructive <developers@constructive.io>", | ||
@@ -33,6 +33,6 @@ "description": "PostgreSQL connection pool LRU cache manager", | ||
| "@pgpmjs/logger": "^2.11.0", | ||
| "@pgpmjs/types": "^2.27.0", | ||
| "@pgpmjs/types": "^2.28.0", | ||
| "lru-cache": "^11.2.7", | ||
| "pg": "^8.21.0", | ||
| "pg-env": "^1.14.0" | ||
| "pg-env": "^1.15.0" | ||
| }, | ||
@@ -52,3 +52,3 @@ "devDependencies": { | ||
| ], | ||
| "gitHead": "030e1144acbd4e288ee74eff2ac0021ca0382ef7" | ||
| "gitHead": "c0d04574f7719d92e67becb58d60791ae978c5f5" | ||
| } |
+13
-2
| import pg from 'pg'; | ||
| import { PgConfig } from 'pg-env'; | ||
| import { PgConfig, PgPoolConfig } from 'pg-env'; | ||
| export declare const buildConnectionString: (user: string, password: string, host: string, port: string | number, database: string) => string; | ||
| export declare const getPgPool: (pgConfig: Partial<PgConfig>) => pg.Pool; | ||
| /** | ||
| * Read per-pool configuration from environment variables. | ||
| * | ||
| * Supports: | ||
| * - PG_POOL_MAX: Maximum clients per pool (default: 5) | ||
| * - PG_POOL_IDLE_TIMEOUT_MS: Close idle clients after ms (default: 30000) | ||
| * - PG_POOL_CONNECTION_TIMEOUT_MS: Fail connect() after ms (default: 5000) | ||
| */ | ||
| export declare function getPgPoolConfig(overrides?: PgPoolConfig): pg.PoolConfig; | ||
| export declare const getPgPool: (pgConfig: Partial<PgConfig> & { | ||
| pool?: PgPoolConfig; | ||
| }) => pg.Pool; |
+25
-1
@@ -7,2 +7,3 @@ "use strict"; | ||
| exports.getPgPool = exports.buildConnectionString = void 0; | ||
| exports.getPgPoolConfig = getPgPoolConfig; | ||
| const pg_1 = __importDefault(require("pg")); | ||
@@ -15,2 +16,24 @@ const pg_env_1 = require("pg-env"); | ||
| exports.buildConnectionString = buildConnectionString; | ||
| const parseEnvInt = (val, fallback) => { | ||
| if (!val) | ||
| return fallback; | ||
| const n = parseInt(val, 10); | ||
| return isNaN(n) ? fallback : n; | ||
| }; | ||
| /** | ||
| * Read per-pool configuration from environment variables. | ||
| * | ||
| * Supports: | ||
| * - PG_POOL_MAX: Maximum clients per pool (default: 5) | ||
| * - PG_POOL_IDLE_TIMEOUT_MS: Close idle clients after ms (default: 30000) | ||
| * - PG_POOL_CONNECTION_TIMEOUT_MS: Fail connect() after ms (default: 5000) | ||
| */ | ||
| function getPgPoolConfig(overrides) { | ||
| return { | ||
| max: overrides?.max ?? parseEnvInt(process.env.PG_POOL_MAX, 5), | ||
| idleTimeoutMillis: overrides?.idleTimeoutMillis ?? parseEnvInt(process.env.PG_POOL_IDLE_TIMEOUT_MS, 30000), | ||
| connectionTimeoutMillis: overrides?.connectionTimeoutMillis ?? parseEnvInt(process.env.PG_POOL_CONNECTION_TIMEOUT_MS, 5000), | ||
| ...(overrides?.allowExitOnIdle !== undefined && { allowExitOnIdle: overrides.allowExitOnIdle }), | ||
| }; | ||
| } | ||
| const getPgPool = (pgConfig) => { | ||
@@ -25,3 +48,4 @@ const config = (0, pg_env_1.getPgEnvOptions)(pgConfig); | ||
| const connectionString = (0, exports.buildConnectionString)(user, password, host, port, database); | ||
| const pgPool = new pg_1.default.Pool({ connectionString }); | ||
| const poolConfig = getPgPoolConfig(pgConfig.pool); | ||
| const pgPool = new pg_1.default.Pool({ connectionString, ...poolConfig }); | ||
| /** | ||
@@ -28,0 +52,0 @@ * IMPORTANT: Pool-level error handler for idle connection errors. |
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 5 instances in 1 package
35678
16.08%642
24.66%10
Infinity%Updated
Updated