THRIFT-4653: ES6 classes support (#1615)
* ES6 classes support
* Lint generated code
* ES6 Tests for NodeJS
* Add eslint rules for nodejs
* Run prettier/eslint on nodejs test code
diff --git a/lib/nodejs/test/client.js b/lib/nodejs/test/client.js
index 55839f6..49e3a5e 100644
--- a/lib/nodejs/test/client.js
+++ b/lib/nodejs/test/client.js
@@ -19,125 +19,149 @@
* under the License.
*/
-var fs = require('fs');
-var assert = require('assert');
-var thrift = require('thrift');
-var helpers = require('./helpers');
-var ThriftTest = require('./gen-nodejs/ThriftTest');
-var ThriftTestDriver = require('./test_driver').ThriftTestDriver;
-var ThriftTestDriverPromise = require('./test_driver').ThriftTestDriverPromise;
-var SecondService = require('./gen-nodejs/SecondService');
-var ttypes = require('./gen-nodejs/ThriftTest_types');
+const assert = require("assert");
+const thrift = require("thrift");
+const helpers = require("./helpers");
-var program = require('commander');
+const ThriftTest = require(`./${helpers.genPath}/ThriftTest`);
+const ThriftTestDriver = require("./test_driver").ThriftTestDriver;
+const ThriftTestDriverPromise = require("./test_driver")
+ .ThriftTestDriverPromise;
+const SecondService = require(`./${helpers.genPath}/SecondService`);
+
+const program = require("commander");
program
- .option('-p, --protocol <protocol>', 'Set thrift protocol (binary|compact|json) [protocol]')
- .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')
+ .option(
+ "-p, --protocol <protocol>",
+ "Set thrift protocol (binary|compact|json) [protocol]"
+ )
+ .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("--callback", "test with callback style functions")
+ .option(
+ "-t, --type <type>",
+ "Select server type (http|multiplex|tcp|websocket)",
+ "tcp"
+ )
+ .option("--es6", "Use es6 code")
+ .option("--es5", "Use es5 code")
.parse(process.argv);
-var host = program.host;
-var port = program.port;
-var domainSocket = program.domainSocket;
-var type = program.type;
-var ssl = program.ssl;
-var promise = program.promise;
+const host = program.host;
+const port = program.port;
+const domainSocket = program.domainSocket;
+const ssl = program.ssl;
+let type = program.type;
/* for compatibility with cross test invocation for http transport testing */
-if (program.transport === 'http') {
- program.transport = 'buffered';
- type = 'http';
+if (program.transport === "http") {
+ program.transport = "buffered";
+ type = "http";
}
-var options = {
+const options = {
transport: helpers.transports[program.transport],
protocol: helpers.protocols[program.protocol]
};
-if (type === 'http' || type === 'websocket') {
- options.path = '/test';
+if (type === "http" || type === "websocket") {
+ options.path = "/test";
}
-if (type === 'http') {
- options.headers = {"Connection": "close"};
+if (type === "http") {
+ options.headers = { Connection: "close" };
}
if (ssl) {
- if (type === 'tcp' || type === 'multiplex') {
+ if (type === "tcp" || type === "multiplex") {
options.rejectUnauthorized = false;
- } else if (type === 'http') {
+ } else if (type === "http") {
options.nodeOptions = { rejectUnauthorized: false };
options.https = true;
- } else if (type === 'websocket') {
+ } else if (type === "websocket") {
options.wsOptions = { rejectUnauthorized: false };
options.secure = true;
}
}
-var connection;
-var client;
-var testDriver = promise ? ThriftTestDriverPromise : ThriftTestDriver;
+let connection;
+let client;
+const testDriver = program.callback
+ ? ThriftTestDriver
+ : ThriftTestDriverPromise;
+if (helpers.ecmaMode === "es6" && program.callback) {
+ console.log("ES6 does not support callback style");
+ process.exit(0);
+}
-if (type === 'tcp' || type === 'multiplex') {
+if (type === "tcp" || type === "multiplex") {
if (domainSocket) {
connection = thrift.createUDSConnection(domainSocket, options);
} else {
- connection = ssl ?
- thrift.createSSLConnection(host, port, options) :
- thrift.createConnection(host, port, options);
+ connection = ssl
+ ? thrift.createSSLConnection(host, port, options)
+ : thrift.createConnection(host, port, options);
}
-} else if (type === 'http') {
+} else if (type === "http") {
if (domainSocket) {
connection = thrift.createHttpUDSConnection(domainSocket, options);
} else {
connection = thrift.createHttpConnection(host, port, options);
}
-} else if (type === 'websocket') {
+} else if (type === "websocket") {
connection = thrift.createWSConnection(host, port, options);
connection.open();
}
-connection.on('error', function(err) {
- assert(false, err);
+connection.on("error", function(err) {
+ assert(false, err);
});
-if (type === 'tcp') {
+if (type === "tcp") {
client = thrift.createClient(ThriftTest, connection);
runTests();
-} else if (type === 'multiplex') {
- var mp = new thrift.Multiplexer();
+} else if (type === "multiplex") {
+ const mp = new thrift.Multiplexer();
client = mp.createClient("ThriftTest", ThriftTest, connection);
- secondclient = mp.createClient("SecondService", SecondService, connection);
+ const secondclient = mp.createClient(
+ "SecondService",
+ SecondService,
+ connection
+ );
- connection.on('connect', function() {
+ connection.on("connect", function() {
secondclient.secondtestString("Test", function(err, response) {
assert(!err);
- assert.equal("testString(\"Test\")", response);
+ assert.equal('testString("Test")', response);
});
runTests();
});
-} else if (type === 'http') {
+} else if (type === "http") {
client = thrift.createHttpClient(ThriftTest, connection);
runTests();
-} else if (type === 'websocket') {
+} else if (type === "websocket") {
client = thrift.createWSClient(ThriftTest, connection);
runTests();
}
function runTests() {
- testDriver(client, function (status) {
+ testDriver(client, function(status) {
console.log(status);
- if (type !== 'http' && type !== 'websocket') {
+ if (type !== "http" && type !== "websocket") {
connection.end();
}
- if (type !== 'multiplex') {
+ if (type !== "multiplex") {
process.exit(0);
}
});