blob: a378fe16e7275a215f4f3963bdb7963f99d3da2b [file] [log] [blame]
raa2e4e562014-03-29 01:14:48 -07001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * 'License'); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * 'AS IS' BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
Randy Abernethy96f4f072015-02-10 02:29:15 -080020//This is the server side Node test handler for the standard
raa2e4e562014-03-29 01:14:48 -070021// Apache Thrift test service.
Cameron Martin21ed4a22024-04-22 11:08:19 +010022import helpers from "./helpers.js";
23import thrift from "thrift";
24
25const ttypes = await helpers.importTypes(`ThriftTest_types`);
26const TException = thrift.Thrift.TException;
raa2e4e562014-03-29 01:14:48 -070027
bforbisda1169d2018-10-28 11:27:38 -040028function makeSyncHandler() {
Cameron Martincaef0ed2025-01-15 11:58:39 +010029 return function (thing) {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080030 return thing;
bforbisda1169d2018-10-28 11:27:38 -040031 };
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080032}
33
bforbisda1169d2018-10-28 11:27:38 -040034const syncHandlers = {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080035 testVoid: testVoid,
36 testMapMap: testMapMap,
37 testInsanity: testInsanity,
38 testMulti: testMulti,
39 testException: testException,
40 testMultiException: testMultiException,
Cameron Martincaef0ed2025-01-15 11:58:39 +010041 testOneway: testOneway,
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080042};
43
44function makeAsyncHandler(label) {
Cameron Martincaef0ed2025-01-15 11:58:39 +010045 return function (thing, result) {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080046 thing = syncHandlers[label](thing);
47 result(null, thing);
bforbisda1169d2018-10-28 11:27:38 -040048 };
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080049}
50
bforbisda1169d2018-10-28 11:27:38 -040051const asyncHandlers = {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080052 testVoid: testVoidAsync,
53 testMulti: testMultiAsync,
54 testException: testExceptionAsync,
55 testMultiException: testMultiExceptionAsync,
Cameron Martincaef0ed2025-01-15 11:58:39 +010056 testOneway: testOnewayAsync,
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080057};
58
bforbisda1169d2018-10-28 11:27:38 -040059const identityHandlers = [
60 "testString",
61 "testBool",
62 "testByte",
63 "testI32",
64 "testI64",
65 "testDouble",
66 "testBinary",
67 "testStruct",
68 "testNest",
69 "testMap",
70 "testStringMap",
71 "testSet",
72 "testList",
73 "testEnum",
Cameron Martincaef0ed2025-01-15 11:58:39 +010074 "testTypedef",
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080075];
76
77function testVoid() {
Randy Abernethyd8187c52015-02-16 01:25:53 -080078 //console.log('testVoid()');
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080079}
80
81function testVoidAsync(result) {
82 result(testVoid());
83}
84
bforbisda1169d2018-10-28 11:27:38 -040085function testMapMap() {
86 const mapmap = [];
87 const pos = [];
88 const neg = [];
89 for (let i = 1; i < 5; i++) {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080090 pos[i] = i;
91 neg[-i] = -i;
92 }
93 mapmap[4] = pos;
94 mapmap[-4] = neg;
95
96 return mapmap;
97}
98
99function testInsanity(argument) {
Randy Abernethyd8187c52015-02-16 01:25:53 -0800100 //console.log('testInsanity(');
101 //console.log(argument);
102 //console.log(')');
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800103
bforbisda1169d2018-10-28 11:27:38 -0400104 const first_map = [];
105 const second_map = [];
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800106
Jens Geyerd629ea02015-09-23 21:16:50 +0200107 first_map[ttypes.Numberz.TWO] = argument;
108 first_map[ttypes.Numberz.THREE] = argument;
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800109
bforbisda1169d2018-10-28 11:27:38 -0400110 const looney = new ttypes.Insanity();
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800111 second_map[ttypes.Numberz.SIX] = looney;
112
bforbisda1169d2018-10-28 11:27:38 -0400113 const insane = [];
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800114 insane[1] = first_map;
115 insane[2] = second_map;
116
Randy Abernethyd8187c52015-02-16 01:25:53 -0800117 //console.log('insane result:');
118 //console.log(insane);
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800119 return insane;
120}
121
bforbisda1169d2018-10-28 11:27:38 -0400122function testMulti(arg0, arg1, arg2) {
Randy Abernethyd8187c52015-02-16 01:25:53 -0800123 //console.log('testMulti()');
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800124
bforbisda1169d2018-10-28 11:27:38 -0400125 const hello = new ttypes.Xtruct();
126 hello.string_thing = "Hello2";
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800127 hello.byte_thing = arg0;
128 hello.i32_thing = arg1;
129 hello.i64_thing = arg2;
130 return hello;
131}
132
133function testMultiAsync(arg0, arg1, arg2, arg3, arg4, arg5, result) {
bforbisda1169d2018-10-28 11:27:38 -0400134 const hello = testMulti(arg0, arg1, arg2, arg3, arg4, arg5);
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800135 result(null, hello);
136}
137
138function testException(arg) {
Randy Abernethyd8187c52015-02-16 01:25:53 -0800139 //console.log('testException('+arg+')');
bforbisda1169d2018-10-28 11:27:38 -0400140 if (arg === "Xception") {
141 const x = new ttypes.Xception();
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800142 x.errorCode = 1001;
143 x.message = arg;
144 throw x;
bforbisda1169d2018-10-28 11:27:38 -0400145 } else if (arg === "TException") {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800146 throw new TException(arg);
147 } else {
148 return;
149 }
150}
151
152function testExceptionAsync(arg, result) {
Randy Abernethyd8187c52015-02-16 01:25:53 -0800153 //console.log('testException('+arg+')');
bforbisda1169d2018-10-28 11:27:38 -0400154 if (arg === "Xception") {
155 const x = new ttypes.Xception();
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800156 x.errorCode = 1001;
157 x.message = arg;
158 result(x);
bforbisda1169d2018-10-28 11:27:38 -0400159 } else if (arg === "TException") {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800160 result(new TException(arg));
161 } else {
162 result(null);
163 }
164}
165
166function testMultiException(arg0, arg1) {
Randy Abernethyd8187c52015-02-16 01:25:53 -0800167 //console.log('testMultiException(' + arg0 + ', ' + arg1 + ')');
bforbisda1169d2018-10-28 11:27:38 -0400168 if (arg0 === "Xception") {
169 const x = new ttypes.Xception();
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800170 x.errorCode = 1001;
bforbisda1169d2018-10-28 11:27:38 -0400171 x.message = "This is an Xception";
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800172 throw x;
bforbisda1169d2018-10-28 11:27:38 -0400173 } else if (arg0 === "Xception2") {
174 const x2 = new ttypes.Xception2();
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800175 x2.errorCode = 2002;
176 x2.struct_thing = new ttypes.Xtruct();
bforbisda1169d2018-10-28 11:27:38 -0400177 x2.struct_thing.string_thing = "This is an Xception2";
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800178 throw x2;
179 }
180
bforbisda1169d2018-10-28 11:27:38 -0400181 const res = new ttypes.Xtruct();
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800182 res.string_thing = arg1;
183 return res;
184}
185
186function testMultiExceptionAsync(arg0, arg1, result) {
Randy Abernethyd8187c52015-02-16 01:25:53 -0800187 //console.log('testMultiException(' + arg0 + ', ' + arg1 + ')');
bforbisda1169d2018-10-28 11:27:38 -0400188 if (arg0 === "Xception") {
189 const x = new ttypes.Xception();
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800190 x.errorCode = 1001;
bforbisda1169d2018-10-28 11:27:38 -0400191 x.message = "This is an Xception";
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800192 result(x);
bforbisda1169d2018-10-28 11:27:38 -0400193 } else if (arg0 === "Xception2") {
194 const x2 = new ttypes.Xception2();
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800195 x2.errorCode = 2002;
196 x2.struct_thing = new ttypes.Xtruct();
bforbisda1169d2018-10-28 11:27:38 -0400197 x2.struct_thing.string_thing = "This is an Xception2";
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800198 result(x2);
Randy Abernethybd60b922015-02-26 16:59:14 -0800199 } else {
bforbisda1169d2018-10-28 11:27:38 -0400200 const res = new ttypes.Xtruct();
Randy Abernethybd60b922015-02-26 16:59:14 -0800201 res.string_thing = arg1;
202 result(null, res);
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800203 }
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800204}
205
bforbisda1169d2018-10-28 11:27:38 -0400206//console.log('testOneway(' + sleepFor + ') => JavaScript (like Rust) never sleeps!');
207function testOneway() {}
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800208
bforbisda1169d2018-10-28 11:27:38 -0400209function testOnewayAsync(sleepFor) {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800210 testOneway(sleepFor);
211}
212
Cameron Martincaef0ed2025-01-15 11:58:39 +0100213identityHandlers.forEach(function (label) {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800214 syncHandlers[label] = makeSyncHandler(label);
215 asyncHandlers[label] = makeAsyncHandler(label);
216});
217
Cameron Martincaef0ed2025-01-15 11:58:39 +0100218["testMapMap", "testInsanity"].forEach(function (label) {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800219 asyncHandlers[label] = makeAsyncHandler(label);
220});
221
Cameron Martin21ed4a22024-04-22 11:08:19 +0100222export { asyncHandlers as ThriftTestHandler };