blob: 7a3c593c07fa5fee40044b579a9238ba0b89bf83 [file] [log] [blame]
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -08001#!/usr/bin/env node
Roger Meier32f39822014-06-18 22:43:17 +02002
Roger Meier8909cbd2014-01-26 11:44:27 +01003/*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * 'License'); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * 'AS IS' BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
Cameron Martin21ed4a22024-04-22 11:08:19 +010022import fs from "fs";
23import path from "path";
24import thrift from "thrift";
25import { program } from "commander";
26import helpers from "./helpers.js";
Roger Meier8909cbd2014-01-26 11:44:27 +010027
28program
bforbisda1169d2018-10-28 11:27:38 -040029 .option(
30 "-p, --protocol <protocol>",
31 "Set thrift protocol (binary|compact|json)",
Cameron Martincaef0ed2025-01-15 11:58:39 +010032 "binary",
bforbisda1169d2018-10-28 11:27:38 -040033 )
34 .option(
35 "-t, --transport <transport>",
36 "Set thrift transport (buffered|framed|http)",
Cameron Martincaef0ed2025-01-15 11:58:39 +010037 "buffered",
bforbisda1169d2018-10-28 11:27:38 -040038 )
39 .option("--ssl", "use ssl transport")
40 .option("--port <port>", "Set thrift server port", 9090)
41 .option("--domain-socket <path>", "Set thift server unix domain socket")
42 .option(
Cameron Martinfdaca5e2025-01-07 15:25:15 +000043 "--type <type>",
bforbisda1169d2018-10-28 11:27:38 -040044 "Select server type (http|multiplex|tcp|websocket)",
Cameron Martincaef0ed2025-01-15 11:58:39 +010045 "tcp",
bforbisda1169d2018-10-28 11:27:38 -040046 )
47 .option("--callback", "test with callback style functions")
48 .option("--es6", "Use es6 code")
49 .option("--es5", "Use es5 code")
Cameron Martin21ed4a22024-04-22 11:08:19 +010050 .option("--esm", "Use es modules")
Roger Meier8909cbd2014-01-26 11:44:27 +010051 .parse(process.argv);
52
Cameron Martin21ed4a22024-04-22 11:08:19 +010053const ThriftTest = await import(
54 `./${helpers.genPath}/ThriftTest.${helpers.moduleExt}`
55);
56const SecondService = await import(
57 `./${helpers.genPath}/SecondService.${helpers.moduleExt}`
58);
59import { ThriftTestHandler } from "./test_handler.mjs";
Roger Meier8909cbd2014-01-26 11:44:27 +010060
Cameron Martinfdaca5e2025-01-07 15:25:15 +000061const opts = program.opts();
62const port = opts.port;
63const domainSocket = opts.domainSocket;
64const ssl = opts.ssl;
henrique31236232014-02-23 20:16:44 +010065
Cameron Martinfdaca5e2025-01-07 15:25:15 +000066let type = opts.type;
67if (opts.transport === "http") {
68 opts.transport = "buffered";
bforbisda1169d2018-10-28 11:27:38 -040069 type = "http";
Cameron Martinfdaca5e2025-01-07 15:25:15 +000070} else if (opts.transport === "websocket") {
71 opts.transport = "buffered";
penenin1ab096c2020-05-18 12:27:31 -070072 type = "websocket";
James E. King, III375bfee2017-10-26 00:09:34 -040073}
74
bforbisda1169d2018-10-28 11:27:38 -040075let options = {
Cameron Martinfdaca5e2025-01-07 15:25:15 +000076 transport: helpers.transports[opts.transport],
77 protocol: helpers.protocols[opts.protocol],
Roger Meier57b354b2014-02-22 01:01:58 +010078};
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080079
bforbisda1169d2018-10-28 11:27:38 -040080if (type === "http" || type === "websocket") {
81 options.handler = ThriftTestHandler;
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080082 options.processor = ThriftTest;
83
84 options = {
Randy Abernethyd8187c52015-02-16 01:25:53 -080085 services: { "/test": options },
86 cors: {
Cameron Martincaef0ed2025-01-15 11:58:39 +010087 "*": true,
88 },
bforbisda1169d2018-10-28 11:27:38 -040089 };
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080090}
91
bforbisda1169d2018-10-28 11:27:38 -040092let processor;
93if (type === "multiplex") {
94 const SecondServiceHandler = {
Cameron Martincaef0ed2025-01-15 11:58:39 +010095 secondtestString: function (thing, result) {
James E. King, III58402ff2017-11-17 14:41:46 -050096 console.log('testString("' + thing + '")');
97 result(null, 'testString("' + thing + '")');
Cameron Martincaef0ed2025-01-15 11:58:39 +010098 },
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080099 };
100
bforbisda1169d2018-10-28 11:27:38 -0400101 processor = new thrift.MultiplexedProcessor();
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800102
bforbisda1169d2018-10-28 11:27:38 -0400103 processor.registerProcessor(
104 "ThriftTest",
Cameron Martincaef0ed2025-01-15 11:58:39 +0100105 new ThriftTest.Processor(ThriftTestHandler),
bforbisda1169d2018-10-28 11:27:38 -0400106 );
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800107
bforbisda1169d2018-10-28 11:27:38 -0400108 processor.registerProcessor(
109 "SecondService",
Cameron Martincaef0ed2025-01-15 11:58:39 +0100110 new SecondService.Processor(SecondServiceHandler),
bforbisda1169d2018-10-28 11:27:38 -0400111 );
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800112}
113
114if (ssl) {
bforbisda1169d2018-10-28 11:27:38 -0400115 if (
116 type === "tcp" ||
117 type === "multiplex" ||
118 type === "http" ||
119 type === "websocket"
120 ) {
Daniel Shihe41de0f2018-03-21 08:28:38 +0800121 options.tls = {
Cameron Martin21ed4a22024-04-22 11:08:19 +0100122 key: fs.readFileSync(path.resolve(import.meta.dirname, "server.key")),
123 cert: fs.readFileSync(path.resolve(import.meta.dirname, "server.crt")),
Daniel Shihe41de0f2018-03-21 08:28:38 +0800124 };
125 }
Roger Meier57b354b2014-02-22 01:01:58 +0100126}
Randy Abernethyd60f9782014-03-28 10:36:38 -0700127
bforbisda1169d2018-10-28 11:27:38 -0400128let server;
129if (type === "tcp") {
130 server = thrift.createServer(ThriftTest, ThriftTestHandler, options);
131} else if (type === "multiplex") {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800132 server = thrift.createMultiplexServer(processor, options);
bforbisda1169d2018-10-28 11:27:38 -0400133} else if (type === "http" || type === "websocket") {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800134 server = thrift.createWebServer(options);
135}
136
Daniel Shihe41de0f2018-03-21 08:28:38 +0800137if (domainSocket) {
138 server.listen(domainSocket);
bforbisda1169d2018-10-28 11:27:38 -0400139} else if (
140 type === "tcp" ||
141 type === "multiplex" ||
142 type === "http" ||
143 type === "websocket"
144) {
Daniel Shihe41de0f2018-03-21 08:28:38 +0800145 server.listen(port);
146}