Skip to content

Commit

Permalink
Gracefully handle global promise pollution
Browse files Browse the repository at this point in the history
- Update QueryInterface to proxy to only standard promise methods
- Update QueryInterface type to be explicit about the methods being
  proxied

This primarily handles pollution of global Promise (either the type or
the global object at runtime) by other libraries.

Fixes: #3422 (comment)
  • Loading branch information
lorefnon committed Oct 29, 2019
1 parent d806ec0 commit 707c90b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 26 deletions.
31 changes: 6 additions & 25 deletions lib/interface.js
Expand Up @@ -82,29 +82,10 @@ module.exports = function(Target) {

// Creates a method which "coerces" to a promise, by calling a
// "then" method on the current `Target`
each(
[
'bind',
'catch',
'finally',
'asCallback',
'spread',
'map',
'reduce',
'thenReturn',
'return',
'yield',
'ensure',
'reflect',
'get',
'mapSeries',
'delay',
],
function(method) {
Target.prototype[method] = function() {
const promise = this.then();
return promise[method].apply(promise, arguments);
};
}
);
each(['catch', 'finally'], function(method) {
Target.prototype[method] = function() {
const promise = this.then();
return promise[method].apply(promise, arguments);
};
});
};
2 changes: 1 addition & 1 deletion types/index.d.ts
Expand Up @@ -1401,7 +1401,7 @@ declare namespace Knex {
// Chainable interface
//

interface ChainableInterface<T = any> extends Promise<T> {
interface ChainableInterface<T = any> extends Pick<Promise<T>, "then" | "catch" | "finally"> {
toQuery(): string;
options(options: { [key: string]: any }): this;
connection(connection: any): this;
Expand Down

0 comments on commit 707c90b

Please sign in to comment.