Skip to content

Commit 465c115

Browse files
committed
Support Node.js 9.x
1 parent 8dcc005 commit 465c115

File tree

4 files changed

+70
-25
lines changed

4 files changed

+70
-25
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ node_js:
1313
- "6.13"
1414
- "7.10"
1515
- "8.9"
16+
- "9.8"
1617
env:
1718
global:
1819
# Necessary to build Node.js 0.6 on Travis CI images

Changes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ you spot any mistakes.
88

99
* Add Amazon RDS GovCloud SSL certificates #1876
1010
* Include connection ID in debug output
11+
* Support Node.js 9.x
1112
* Update Amazon RDS SSL certificates
1213
* Update `bignumber.js` to 4.1.0
1314
* Update `readable-stream` to 2.3.5

appveyor.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ environment:
1717
- nodejs_version: "6.13"
1818
- nodejs_version: "7.10"
1919
- nodejs_version: "8.9"
20+
- nodejs_version: "9.8"
2021

2122
services:
2223
- mysql

lib/Connection.js

Lines changed: 67 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,28 +22,16 @@ function Connection(options) {
2222
this.threadId = null;
2323
}
2424

25-
function bindToCurrentDomain(callback) {
26-
if (!callback) {
27-
return undefined;
28-
}
29-
30-
var domain = process.domain;
31-
32-
return domain
33-
? domain.bind(callback)
34-
: callback;
35-
}
36-
3725
Connection.createQuery = function createQuery(sql, values, callback) {
3826
if (sql instanceof Query) {
3927
return sql;
4028
}
4129

42-
var cb = bindToCurrentDomain(callback);
30+
var cb = wrapCallbackInDomain(null, callback);
4331
var options = {};
4432

4533
if (typeof sql === 'function') {
46-
cb = bindToCurrentDomain(sql);
34+
cb = wrapCallbackInDomain(null, sql);
4735
return new Query(options, cb);
4836
}
4937

@@ -53,7 +41,7 @@ Connection.createQuery = function createQuery(sql, values, callback) {
5341
}
5442

5543
if (typeof values === 'function') {
56-
cb = bindToCurrentDomain(values);
44+
cb = wrapCallbackInDomain(null, values);
5745
} else if (values !== undefined) {
5846
options.values = values;
5947
}
@@ -65,7 +53,7 @@ Connection.createQuery = function createQuery(sql, values, callback) {
6553
options.values = values;
6654

6755
if (typeof values === 'function') {
68-
cb = bindToCurrentDomain(values);
56+
cb = wrapCallbackInDomain(null, values);
6957
options.values = undefined;
7058
}
7159

@@ -99,15 +87,15 @@ Connection.prototype.connect = function connect(options, callback) {
9987
this._protocol.on('data', function(data) {
10088
connection._socket.write(data);
10189
});
102-
this._socket.on('data', function(data) {
90+
this._socket.on('data', wrapToDomain(connection, function (data) {
10391
connection._protocol.write(data);
104-
});
92+
}));
10593
this._protocol.on('end', function() {
10694
connection._socket.end();
10795
});
108-
this._socket.on('end', function() {
96+
this._socket.on('end', wrapToDomain(connection, function () {
10997
connection._protocol.end();
110-
});
98+
}));
11199

112100
this._socket.on('error', this._handleNetworkError.bind(this));
113101
this._socket.on('connect', this._handleProtocolConnect.bind(this));
@@ -127,7 +115,7 @@ Connection.prototype.connect = function connect(options, callback) {
127115
}
128116
}
129117

130-
this._protocol.handshake(options, bindToCurrentDomain(callback));
118+
this._protocol.handshake(options, wrapCallbackInDomain(this, callback));
131119
};
132120

133121
Connection.prototype.changeUser = function changeUser(options, callback) {
@@ -149,7 +137,7 @@ Connection.prototype.changeUser = function changeUser(options, callback) {
149137
timeout : options.timeout,
150138
charsetNumber : charsetNumber,
151139
currentConfig : this.config
152-
}, bindToCurrentDomain(callback));
140+
}, wrapCallbackInDomain(this, callback));
153141
};
154142

155143
Connection.prototype.beginTransaction = function beginTransaction(options, callback) {
@@ -203,6 +191,10 @@ Connection.prototype.query = function query(sql, values, cb) {
203191
query.sql = this.format(query.sql, query.values);
204192
}
205193

194+
if (query._callback) {
195+
query._callback = wrapCallbackInDomain(this, query._callback);
196+
}
197+
206198
this._implyConnect();
207199

208200
return this._protocol._enqueue(query);
@@ -215,7 +207,7 @@ Connection.prototype.ping = function ping(options, callback) {
215207
}
216208

217209
this._implyConnect();
218-
this._protocol.ping(options, bindToCurrentDomain(callback));
210+
this._protocol.ping(options, wrapCallbackInDomain(this, callback));
219211
};
220212

221213
Connection.prototype.statistics = function statistics(options, callback) {
@@ -225,7 +217,7 @@ Connection.prototype.statistics = function statistics(options, callback) {
225217
}
226218

227219
this._implyConnect();
228-
this._protocol.stats(options, bindToCurrentDomain(callback));
220+
this._protocol.stats(options, wrapCallbackInDomain(this, callback));
229221
};
230222

231223
Connection.prototype.end = function end(options, callback) {
@@ -246,7 +238,7 @@ Connection.prototype.end = function end(options, callback) {
246238
}
247239

248240
this._implyConnect();
249-
this._protocol.quit(opts, bindToCurrentDomain(cb));
241+
this._protocol.quit(opts, wrapCallbackInDomain(this, cb));
250242
};
251243

252244
Connection.prototype.destroy = function() {
@@ -461,3 +453,53 @@ Connection.prototype._implyConnect = function() {
461453
this.connect();
462454
}
463455
};
456+
457+
function unwrapFromDomain(fn) {
458+
return function () {
459+
var domains = [];
460+
var ret;
461+
462+
while (process.domain) {
463+
domains.shift(process.domain);
464+
process.domain.exit();
465+
}
466+
467+
try {
468+
ret = fn.apply(this, arguments);
469+
} finally {
470+
for (var i = 0; i < domains.length; i++) {
471+
domains[i].enter();
472+
}
473+
}
474+
475+
return ret;
476+
};
477+
}
478+
479+
function wrapCallbackInDomain(ee, fn) {
480+
if (typeof fn !== 'function' || fn.domain) {
481+
return fn;
482+
}
483+
484+
var domain = process.domain;
485+
486+
if (domain) {
487+
return domain.bind(fn);
488+
} else if (ee) {
489+
return unwrapFromDomain(wrapToDomain(ee, fn));
490+
} else {
491+
return fn;
492+
}
493+
}
494+
495+
function wrapToDomain(ee, fn) {
496+
return function () {
497+
if (Events.usingDomains && ee.domain) {
498+
ee.domain.enter();
499+
fn.apply(this, arguments);
500+
ee.domain.exit();
501+
} else {
502+
fn.apply(this, arguments);
503+
}
504+
};
505+
}

0 commit comments

Comments
 (0)