summaryrefslogtreecommitdiff
path: root/lib/nodejs
diff options
context:
space:
mode:
authorDaniel Shih <hotingwow@gmail.com>2018-03-21 08:28:38 +0800
committerJames E. King III <jking@apache.org>2018-03-21 07:48:31 -0400
commite41de0fbcb43c51a0f1ede40207c80bd3409f99f (patch)
treed9655f2e0356e737ee8f39ab096e3e049d420745 /lib/nodejs
parent9aaf295806d32eab5715b4f0681f7da9b64c1fa5 (diff)
downloadthrift-e41de0fbcb43c51a0f1ede40207c80bd3409f99f.tar.gz
THRIFT-4489: Add unix domain socket support for nodejs
Client: nodejs This closes #1491
Diffstat (limited to 'lib/nodejs')
-rw-r--r--lib/nodejs/lib/thrift/connection.js14
-rw-r--r--lib/nodejs/lib/thrift/http_connection.js23
-rw-r--r--lib/nodejs/lib/thrift/index.js2
-rw-r--r--lib/nodejs/test/client.js18
-rw-r--r--lib/nodejs/test/server.js18
5 files changed, 57 insertions, 18 deletions
diff --git a/lib/nodejs/lib/thrift/connection.js b/lib/nodejs/lib/thrift/connection.js
index 273339b1f..b54545436 100644
--- a/lib/nodejs/lib/thrift/connection.js
+++ b/lib/nodejs/lib/thrift/connection.js
@@ -232,7 +232,11 @@ Connection.prototype.connection_gone = function () {
return;
}
- self.connection.connect(self.port, self.host);
+ if (self.path !== undefined) {
+ self.connection.connect(self.path);
+ } else {
+ self.connection.connect(self.port, self.host);
+ }
self.retry_timer = null;
}, this.retry_delay);
};
@@ -246,6 +250,14 @@ exports.createConnection = function(host, port, options) {
return connection;
};
+exports.createUDSConnection = function(path, options) {
+ var stream = net.createConnection(path);
+ var connection = new Connection(stream, options);
+ connection.path = path;
+
+ return connection;
+};
+
exports.createSSLConnection = function(host, port, options) {
if (!('secureProtocol' in options) && !('secureOptions' in options)) {
options.secureProtocol = "SSLv23_method";
diff --git a/lib/nodejs/lib/thrift/http_connection.js b/lib/nodejs/lib/thrift/http_connection.js
index 508553801..4f5378f43 100644
--- a/lib/nodejs/lib/thrift/http_connection.js
+++ b/lib/nodejs/lib/thrift/http_connection.js
@@ -59,8 +59,6 @@ var createClient = require('./create_client');
* Initializes a Thrift HttpConnection instance (use createHttpConnection() rather than
* instantiating directly).
* @constructor
- * @param {string} host - The host name or IP to connect to.
- * @param {number} port - The TCP port to connect to.
* @param {ConnectOptions} options - The configuration options to use.
* @throws {error} Exceptions other than InputBufferUnderrunError are rethrown
* @event {error} The "error" event is fired when a Node.js error event occurs during
@@ -71,15 +69,16 @@ var createClient = require('./create_client');
* semantics implemented over the Node.js http.request() method.
* @see {@link createHttpConnection}
*/
-var HttpConnection = exports.HttpConnection = function(host, port, options) {
+var HttpConnection = exports.HttpConnection = function(options) {
//Initialize the emitter base object
EventEmitter.call(this);
//Set configuration
var self = this;
this.options = options || {};
- this.host = host;
- this.port = port;
+ this.host = this.options.host;
+ this.port = this.options.port;
+ this.socketPath = this.options.socketPath;
this.https = this.options.https || false;
this.transport = this.options.transport || TBufferedTransport;
this.protocol = this.options.protocol || TBinaryProtocol;
@@ -87,7 +86,8 @@ var HttpConnection = exports.HttpConnection = function(host, port, options) {
//Prepare Node.js options
this.nodeOptions = {
host: this.host,
- port: this.port || 80,
+ port: this.port,
+ socketPath: this.socketPath,
path: this.options.path || '/',
method: 'POST',
headers: this.options.headers || {},
@@ -238,7 +238,14 @@ HttpConnection.prototype.write = function(data) {
* @see {@link ConnectOptions}
*/
exports.createHttpConnection = function(host, port, options) {
- return new HttpConnection(host, port, options);
+ options.host = host;
+ options.port = port || 80;
+ return new HttpConnection(options);
+};
+
+exports.createHttpUDSConnection = function(path, options) {
+ options.socketPath = path;
+ return new HttpConnection(options);
};
exports.createHttpClient = createClient
@@ -253,4 +260,4 @@ function THTTPException(statusCode, response) {
this.type = thrift.TApplicationExceptionType.PROTOCOL_ERROR;
this.message = "Received a response with a bad HTTP status code: " + response.statusCode;
}
-util.inherits(THTTPException, thrift.TApplicationException); \ No newline at end of file
+util.inherits(THTTPException, thrift.TApplicationException);
diff --git a/lib/nodejs/lib/thrift/index.js b/lib/nodejs/lib/thrift/index.js
index 020726d57..b09953d71 100644
--- a/lib/nodejs/lib/thrift/index.js
+++ b/lib/nodejs/lib/thrift/index.js
@@ -27,6 +27,7 @@ var connection = require('./connection');
exports.Connection = connection.Connection;
exports.createClient = connection.createClient;
exports.createConnection = connection.createConnection;
+exports.createUDSConnection = connection.createUDSConnection;
exports.createSSLConnection = connection.createSSLConnection;
exports.createStdIOClient = connection.createStdIOClient;
exports.createStdIOConnection = connection.createStdIOConnection;
@@ -34,6 +35,7 @@ exports.createStdIOConnection = connection.createStdIOConnection;
var httpConnection = require('./http_connection');
exports.HttpConnection = httpConnection.HttpConnection;
exports.createHttpConnection = httpConnection.createHttpConnection;
+exports.createHttpUDSConnection = httpConnection.createHttpUDSConnection;
exports.createHttpClient = httpConnection.createHttpClient;
var wsConnection = require('./ws_connection');
diff --git a/lib/nodejs/test/client.js b/lib/nodejs/test/client.js
index 006fad2dc..55839f616 100644
--- a/lib/nodejs/test/client.js
+++ b/lib/nodejs/test/client.js
@@ -36,6 +36,7 @@ program
.option('-t, --transport <transport>', 'Set thrift transport (buffered|framed|http) [transport]')
.option('--port <port>', 'Set thrift server port number to connect', 9090)
.option('--host <host>', 'Set thrift server host to connect', 'localhost')
+ .option('--domain-socket <path>', 'Set thrift server unix domain socket to connect')
.option('--ssl', 'use SSL transport')
.option('--promise', 'test with promise style functions')
.option('-t, --type <type>', 'Select server type (http|multiplex|tcp|websocket)', 'tcp')
@@ -43,6 +44,7 @@ program
var host = program.host;
var port = program.port;
+var domainSocket = program.domainSocket;
var type = program.type;
var ssl = program.ssl;
var promise = program.promise;
@@ -83,11 +85,19 @@ var client;
var testDriver = promise ? ThriftTestDriverPromise : ThriftTestDriver;
if (type === 'tcp' || type === 'multiplex') {
- connection = ssl ?
- thrift.createSSLConnection(host, port, options) :
- thrift.createConnection(host, port, options);
+ if (domainSocket) {
+ connection = thrift.createUDSConnection(domainSocket, options);
+ } else {
+ connection = ssl ?
+ thrift.createSSLConnection(host, port, options) :
+ thrift.createConnection(host, port, options);
+ }
} else if (type === 'http') {
- connection = thrift.createHttpConnection(host, port, options);
+ if (domainSocket) {
+ connection = thrift.createHttpUDSConnection(domainSocket, options);
+ } else {
+ connection = thrift.createHttpConnection(host, port, options);
+ }
} else if (type === 'websocket') {
connection = thrift.createWSConnection(host, port, options);
connection.open();
diff --git a/lib/nodejs/test/server.js b/lib/nodejs/test/server.js
index 8f2e06b8d..030d28b38 100644
--- a/lib/nodejs/test/server.js
+++ b/lib/nodejs/test/server.js
@@ -36,11 +36,13 @@ program
.option('-t, --transport <transport>', 'Set thrift transport (buffered|framed|http)', 'buffered')
.option('--ssl', 'use ssl transport')
.option('--port <port>', 'Set thrift server port', 9090)
+ .option('--domain-socket <path>', 'Set thift server unix domain socket')
.option('--promise', 'test with promise style functions')
.option('-t, --type <type>', 'Select server type (http|multiplex|tcp|websocket)', 'tcp')
.parse(process.argv);
var port = program.port;
+var domainSocket = program.domainSocket;
var type = program.type;
var ssl = program.ssl;
var promise = program.promise;
@@ -88,10 +90,12 @@ if (type === 'multiplex') {
}
if (ssl) {
- options.tls = {
- key: fs.readFileSync(path.resolve(__dirname, 'server.key')),
- cert: fs.readFileSync(path.resolve(__dirname, 'server.crt'))
- };
+ if (type === 'tcp' || type === 'multiplex' || type === 'http' || type === 'websocket') {
+ options.tls = {
+ key: fs.readFileSync(path.resolve(__dirname, 'server.key')),
+ cert: fs.readFileSync(path.resolve(__dirname, 'server.crt'))
+ };
+ }
}
var server;
@@ -103,4 +107,8 @@ if (type === 'tcp') {
server = thrift.createWebServer(options);
}
-server.listen(port);
+if (domainSocket) {
+ server.listen(domainSocket);
+} else if (type === 'tcp' || type === 'multiplex' || type === 'http' || type === 'websocket') {
+ server.listen(port);
+}