Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: avoid fake .then() method on mysql2 Query class #501

Merged
merged 2 commits into from
May 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions packages/mysql/lib/mysql_p.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,26 @@ module.exports = function captureMySQL(mysql) {
return mysql;
};

function isPromise(maybePromise) {
if (maybePromise != null && maybePromise.then instanceof Function) {
// mysql2 has a `Query` class with a `then` method which always throws an error when called.
// We want to avoid calling this, so we need to check for more than just the presence of a `then` method.
// See https://github.com/sidorares/node-mysql2/blob/dbb344e89a1cc8bb457b24e67b07cdb3013fe844/lib/commands/query.js#L38-L44
// Since it's highly unlikely that any Promise implementation would name their class `Query`,
// we can safely use this to determine whether or not this is actually a Promise.
const constructorName = maybePromise.constructor != null ? maybePromise.constructor.name : undefined;
return constructorName !== 'Query';
}
return false;
}

function patchCreateConnection(mysql) {
var baseFcn = '__createConnection';
mysql[baseFcn] = mysql['createConnection'];

mysql['createConnection'] = function patchedCreateConnection() {
var connection = mysql[baseFcn].apply(connection, arguments);
if (connection && connection.then instanceof Function) {
if (isPromise(connection)) {
connection = connection.then((result) => {
patchObject(result.connection);
return result;
Expand All @@ -57,7 +70,7 @@ function patchCreatePool(mysql) {

mysql['createPool'] = function patchedCreatePool() {
var pool = mysql[baseFcn].apply(pool, arguments);
if (pool && pool.then instanceof Function) {
if (isPromise(pool)) {
pool = pool.then((result) => {
patchObject(result.pool);
return result;
Expand Down Expand Up @@ -112,7 +125,7 @@ function patchGetConnection(pool) {
}

var result = pool[baseFcn].apply(pool, args);
if (result && result.then instanceof Function) {
if (isPromise(result)) {
return result.then(patchObject);
} else {
return result;
Expand Down Expand Up @@ -232,7 +245,7 @@ function captureOperation(name) {
}
};

if (command.then instanceof Function) {
if (isPromise(command)) {
command.then(() => {
subsegment.close();
});
Expand Down
50 changes: 50 additions & 0 deletions packages/mysql/test/unit/mysql_p.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var sinon = require('sinon');
var sinonChai = require('sinon-chai');

var AWSXRay = require('aws-xray-sdk-core');
var EventEmitter = require('events').EventEmitter;

var captureMySQL = require('../../lib/mysql_p');
var Segment = AWSXRay.Segment;
Expand Down Expand Up @@ -34,6 +35,55 @@ describe('captureMySQL', function() {
assert.equal(patched.createPool.name, 'patchedCreatePool');
assert.equal(patched.createPoolCluster.name, 'patchedCreatePoolCluster');
});

describe('handles mysql2 quirks', function() {
var conn, connectionObj, mysql, sandbox, segment;

// See https://github.com/sidorares/node-mysql2/blob/dbb344e89a1cc8bb457b24e67b07cdb3013fe844/lib/commands/query.js#L38-L44
class Query extends EventEmitter {
then() {
throw new Error('You have tried to call .then(), .catch(), or invoked await on the result of query that is not a promise [...]');
}
}

before(function() {
conn = {
config: {
user: 'mcmuls',
host: 'database.location',
port: '8080',
database: 'myTestDb'
},
query: function() {
return new Query();
}
};

mysql = { createConnection: function() {
return conn;
} };
mysql = captureMySQL(mysql);
connectionObj = mysql.createConnection();
});

beforeEach(function() {
sandbox = sinon.createSandbox();
segment = new Segment('test');
segment.addNewSubsegment('testSub');

sandbox = sinon.createSandbox();
sandbox.stub(AWSXRay, 'getSegment').returns(segment);
sandbox.stub(AWSXRay, 'isAutomaticMode').returns(true);
});

afterEach(function() {
sandbox.restore();
});

it('should not call fake .then() on mysql2 Query class', function() {
(() => connectionObj.query()).should.not.throw();
});
});
});

describe('#captureQuery', function() {
Expand Down