Skip to content

Add support for custom streams #1110

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ When establishing a connection, you can set the following options:
* `localAddress`: The source IP address to use for TCP connection. (Optional)
* `socketPath`: The path to a unix domain socket to connect to. When used `host`
and `port` are ignored.
* `stream`: A function that returns a Stream object that will be used
instead of the standard Net socket.
* `user`: The MySQL user to authenticate as.
* `password`: The password of that MySQL user.
* `database`: Name of the database to use for this connection (Optional).
Expand Down
11 changes: 7 additions & 4 deletions lib/Connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,13 @@ Connection.prototype.connect = function connect(options, callback) {
if (!this._connectCalled) {
this._connectCalled = true;

// Connect either via a UNIX domain socket or a TCP socket.
this._socket = (this.config.socketPath)
? Net.createConnection(this.config.socketPath)
: Net.createConnection(this.config.port, this.config.host);
if (this.config.stream) {
this._socket = this.config.stream();
} else if (this.config.socketPath) {
this._socket = Net.createConnection(this.config.socketPath)
} else {
this._socket = Net.createConnection(this.config.port, this.config.host);
}

var connection = this;
this._protocol.on('data', function(data) {
Expand Down
1 change: 1 addition & 0 deletions lib/ConnectionConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ function ConnectionConfig(options) {
this.port = options.port || 3306;
this.localAddress = options.localAddress;
this.socketPath = options.socketPath;
this.stream = options.stream;
this.user = options.user || undefined;
this.password = options.password || undefined;
this.database = options.database;
Expand Down
39 changes: 39 additions & 0 deletions test/unit/connection/test-custom-stream.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
var assert = require('assert');
var Net = require('net');
var common = require('../../common');

function streamFactory() {
return Net.createConnection(common.fakeServerPort);
}

var connection = common.createConnection({stream: streamFactory});

var server = common.createFakeServer();
var didConnect = false;

server.listen(common.fakeServerPort, function (err) {
assert.ifError(err);

connection.connect(function (err) {
assert.ifError(err);

assert.equal(didConnect, false);
didConnect = true;

connection.destroy();
server.destroy();
});
});

var hadConnection = false;
server.on('connection', function(connection) {
connection.handshake();

assert.equal(hadConnection, false);
hadConnection = true;
});

process.on('exit', function() {
assert.equal(didConnect, true);
assert.equal(hadConnection, true);
});