From d782eb32323e894e1ababafbcd4653a4c186d0e0 Mon Sep 17 00:00:00 2001 From: Niicck Date: Sat, 2 Feb 2019 00:10:17 -0600 Subject: [PATCH 1/4] fix(postgraphile): check pgConfig constructor's function, not its name --- src/postgraphile/postgraphile.ts | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/postgraphile/postgraphile.ts b/src/postgraphile/postgraphile.ts index e2b1ab9df9..033c0238d3 100644 --- a/src/postgraphile/postgraphile.ts +++ b/src/postgraphile/postgraphile.ts @@ -190,13 +190,13 @@ function handleFatalError(error: Error, when: string): never { return null as never; } -function constructorName(obj: mixed): string | null { +function hasPoolConstructor(obj: mixed): boolean { return ( (obj && typeof obj.constructor === 'function' && - obj.constructor.name && - String(obj.constructor.name)) || - null + obj.constructor === (Pool as any).super_ + ) || + false ); } @@ -220,12 +220,11 @@ function toPgPool(poolOrConfig: any): Pool { // tslint:disable-next-line no-any function quacksLikePgPool(pgConfig: any): pgConfig is Pool { - if (pgConfig instanceof Pool) return true; + if ((pgConfig instanceof Pool) || (pgConfig instanceof EventEmitter)) return true; // A diagnosis of exclusion if (!pgConfig || typeof pgConfig !== 'object') return false; - if (constructorName(pgConfig) !== 'Pool' && constructorName(pgConfig) !== 'BoundPool') - return false; + if (!hasPoolConstructor(pgConfig)) return false; if (!pgConfig['Client']) return false; if (!pgConfig['options']) return false; if (typeof pgConfig['connect'] !== 'function') return false; From d0ab4a01171febf0d47bb7362f387f9d53a59f36 Mon Sep 17 00:00:00 2001 From: Niicck Date: Sat, 9 Feb 2019 00:49:40 -0600 Subject: [PATCH 2/4] run prettier --- src/postgraphile/postgraphile.ts | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/postgraphile/postgraphile.ts b/src/postgraphile/postgraphile.ts index 033c0238d3..399fc3d791 100644 --- a/src/postgraphile/postgraphile.ts +++ b/src/postgraphile/postgraphile.ts @@ -192,10 +192,7 @@ function handleFatalError(error: Error, when: string): never { function hasPoolConstructor(obj: mixed): boolean { return ( - (obj && - typeof obj.constructor === 'function' && - obj.constructor === (Pool as any).super_ - ) || + (obj && typeof obj.constructor === 'function' && obj.constructor === (Pool as any).super_) || false ); } @@ -220,7 +217,7 @@ function toPgPool(poolOrConfig: any): Pool { // tslint:disable-next-line no-any function quacksLikePgPool(pgConfig: any): pgConfig is Pool { - if ((pgConfig instanceof Pool) || (pgConfig instanceof EventEmitter)) return true; + if (pgConfig instanceof Pool || pgConfig instanceof EventEmitter) return true; // A diagnosis of exclusion if (!pgConfig || typeof pgConfig !== 'object') return false; From cc83aa589ac50be0aedece38ab144923c664bc5e Mon Sep 17 00:00:00 2001 From: Niicck Date: Sat, 9 Feb 2019 01:04:46 -0600 Subject: [PATCH 3/4] disable tslint on 'any' fix --- src/postgraphile/postgraphile.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/postgraphile/postgraphile.ts b/src/postgraphile/postgraphile.ts index 399fc3d791..e1b4634a80 100644 --- a/src/postgraphile/postgraphile.ts +++ b/src/postgraphile/postgraphile.ts @@ -192,6 +192,7 @@ function handleFatalError(error: Error, when: string): never { function hasPoolConstructor(obj: mixed): boolean { return ( + // tslint:disable-next-line no-any (obj && typeof obj.constructor === 'function' && obj.constructor === (Pool as any).super_) || false ); From aec3ffb45e1c33ede68da7b9435e04f01e18e1d2 Mon Sep 17 00:00:00 2001 From: Niicck Date: Sat, 9 Feb 2019 09:45:42 -0600 Subject: [PATCH 4/4] apply changes to preserve existing logic --- src/postgraphile/postgraphile.ts | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/postgraphile/postgraphile.ts b/src/postgraphile/postgraphile.ts index e1b4634a80..3564a1244b 100644 --- a/src/postgraphile/postgraphile.ts +++ b/src/postgraphile/postgraphile.ts @@ -198,6 +198,16 @@ function hasPoolConstructor(obj: mixed): boolean { ); } +function constructorName(obj: mixed): string | null { + return ( + (obj && + typeof obj.constructor === 'function' && + obj.constructor.name && + String(obj.constructor.name)) || + null + ); +} + // tslint:disable-next-line no-any function toPgPool(poolOrConfig: any): Pool { if (quacksLikePgPool(poolOrConfig)) { @@ -218,11 +228,13 @@ function toPgPool(poolOrConfig: any): Pool { // tslint:disable-next-line no-any function quacksLikePgPool(pgConfig: any): pgConfig is Pool { - if (pgConfig instanceof Pool || pgConfig instanceof EventEmitter) return true; + if (pgConfig instanceof Pool) return true; + if (hasPoolConstructor(pgConfig)) return true; // A diagnosis of exclusion if (!pgConfig || typeof pgConfig !== 'object') return false; - if (!hasPoolConstructor(pgConfig)) return false; + if (constructorName(pgConfig) !== 'Pool' && constructorName(pgConfig) !== 'BoundPool') + return false; if (!pgConfig['Client']) return false; if (!pgConfig['options']) return false; if (typeof pgConfig['connect'] !== 'function') return false;