blob: e6a2af26cd5cce50ed150a7b7b6e5001882df9e1 [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",
CJCombrinkdfeab8d2026-03-06 07:03:56 +010067 "testUuid",
bforbisda1169d2018-10-28 11:27:38 -040068 "testStruct",
69 "testNest",
70 "testMap",
71 "testStringMap",
72 "testSet",
73 "testList",
74 "testEnum",
Cameron Martincaef0ed2025-01-15 11:58:39 +010075 "testTypedef",
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080076];
77
78function testVoid() {
Randy Abernethyd8187c52015-02-16 01:25:53 -080079 //console.log('testVoid()');
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080080}
81
82function testVoidAsync(result) {
83 result(testVoid());
84}
85
bforbisda1169d2018-10-28 11:27:38 -040086function testMapMap() {
87 const mapmap = [];
88 const pos = [];
89 const neg = [];
90 for (let i = 1; i < 5; i++) {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -080091 pos[i] = i;
92 neg[-i] = -i;
93 }
94 mapmap[4] = pos;
95 mapmap[-4] = neg;
96
97 return mapmap;
98}
99
100function testInsanity(argument) {
Randy Abernethyd8187c52015-02-16 01:25:53 -0800101 //console.log('testInsanity(');
102 //console.log(argument);
103 //console.log(')');
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800104
bforbisda1169d2018-10-28 11:27:38 -0400105 const first_map = [];
106 const second_map = [];
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800107
Jens Geyerd629ea02015-09-23 21:16:50 +0200108 first_map[ttypes.Numberz.TWO] = argument;
109 first_map[ttypes.Numberz.THREE] = argument;
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800110
bforbisda1169d2018-10-28 11:27:38 -0400111 const looney = new ttypes.Insanity();
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800112 second_map[ttypes.Numberz.SIX] = looney;
113
bforbisda1169d2018-10-28 11:27:38 -0400114 const insane = [];
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800115 insane[1] = first_map;
116 insane[2] = second_map;
117
Randy Abernethyd8187c52015-02-16 01:25:53 -0800118 //console.log('insane result:');
119 //console.log(insane);
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800120 return insane;
121}
122
bforbisda1169d2018-10-28 11:27:38 -0400123function testMulti(arg0, arg1, arg2) {
Randy Abernethyd8187c52015-02-16 01:25:53 -0800124 //console.log('testMulti()');
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800125
bforbisda1169d2018-10-28 11:27:38 -0400126 const hello = new ttypes.Xtruct();
127 hello.string_thing = "Hello2";
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800128 hello.byte_thing = arg0;
129 hello.i32_thing = arg1;
130 hello.i64_thing = arg2;
131 return hello;
132}
133
134function testMultiAsync(arg0, arg1, arg2, arg3, arg4, arg5, result) {
bforbisda1169d2018-10-28 11:27:38 -0400135 const hello = testMulti(arg0, arg1, arg2, arg3, arg4, arg5);
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800136 result(null, hello);
137}
138
139function testException(arg) {
Randy Abernethyd8187c52015-02-16 01:25:53 -0800140 //console.log('testException('+arg+')');
bforbisda1169d2018-10-28 11:27:38 -0400141 if (arg === "Xception") {
142 const x = new ttypes.Xception();
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800143 x.errorCode = 1001;
144 x.message = arg;
145 throw x;
bforbisda1169d2018-10-28 11:27:38 -0400146 } else if (arg === "TException") {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800147 throw new TException(arg);
148 } else {
149 return;
150 }
151}
152
153function testExceptionAsync(arg, result) {
Randy Abernethyd8187c52015-02-16 01:25:53 -0800154 //console.log('testException('+arg+')');
bforbisda1169d2018-10-28 11:27:38 -0400155 if (arg === "Xception") {
156 const x = new ttypes.Xception();
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800157 x.errorCode = 1001;
158 x.message = arg;
159 result(x);
bforbisda1169d2018-10-28 11:27:38 -0400160 } else if (arg === "TException") {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800161 result(new TException(arg));
162 } else {
163 result(null);
164 }
165}
166
167function testMultiException(arg0, arg1) {
Randy Abernethyd8187c52015-02-16 01:25:53 -0800168 //console.log('testMultiException(' + arg0 + ', ' + arg1 + ')');
bforbisda1169d2018-10-28 11:27:38 -0400169 if (arg0 === "Xception") {
170 const x = new ttypes.Xception();
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800171 x.errorCode = 1001;
bforbisda1169d2018-10-28 11:27:38 -0400172 x.message = "This is an Xception";
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800173 throw x;
bforbisda1169d2018-10-28 11:27:38 -0400174 } else if (arg0 === "Xception2") {
175 const x2 = new ttypes.Xception2();
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800176 x2.errorCode = 2002;
177 x2.struct_thing = new ttypes.Xtruct();
bforbisda1169d2018-10-28 11:27:38 -0400178 x2.struct_thing.string_thing = "This is an Xception2";
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800179 throw x2;
180 }
181
bforbisda1169d2018-10-28 11:27:38 -0400182 const res = new ttypes.Xtruct();
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800183 res.string_thing = arg1;
184 return res;
185}
186
187function testMultiExceptionAsync(arg0, arg1, result) {
Randy Abernethyd8187c52015-02-16 01:25:53 -0800188 //console.log('testMultiException(' + arg0 + ', ' + arg1 + ')');
bforbisda1169d2018-10-28 11:27:38 -0400189 if (arg0 === "Xception") {
190 const x = new ttypes.Xception();
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800191 x.errorCode = 1001;
bforbisda1169d2018-10-28 11:27:38 -0400192 x.message = "This is an Xception";
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800193 result(x);
bforbisda1169d2018-10-28 11:27:38 -0400194 } else if (arg0 === "Xception2") {
195 const x2 = new ttypes.Xception2();
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800196 x2.errorCode = 2002;
197 x2.struct_thing = new ttypes.Xtruct();
bforbisda1169d2018-10-28 11:27:38 -0400198 x2.struct_thing.string_thing = "This is an Xception2";
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800199 result(x2);
Randy Abernethybd60b922015-02-26 16:59:14 -0800200 } else {
bforbisda1169d2018-10-28 11:27:38 -0400201 const res = new ttypes.Xtruct();
Randy Abernethybd60b922015-02-26 16:59:14 -0800202 res.string_thing = arg1;
203 result(null, res);
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800204 }
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800205}
206
bforbisda1169d2018-10-28 11:27:38 -0400207//console.log('testOneway(' + sleepFor + ') => JavaScript (like Rust) never sleeps!');
208function testOneway() {}
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800209
bforbisda1169d2018-10-28 11:27:38 -0400210function testOnewayAsync(sleepFor) {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800211 testOneway(sleepFor);
212}
213
Cameron Martincaef0ed2025-01-15 11:58:39 +0100214identityHandlers.forEach(function (label) {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800215 syncHandlers[label] = makeSyncHandler(label);
216 asyncHandlers[label] = makeAsyncHandler(label);
217});
218
Cameron Martincaef0ed2025-01-15 11:58:39 +0100219["testMapMap", "testInsanity"].forEach(function (label) {
Randy Abernethy3b9ff4d2015-02-16 00:51:24 -0800220 asyncHandlers[label] = makeAsyncHandler(label);
221});
222
Cameron Martin21ed4a22024-04-22 11:08:19 +0100223export { asyncHandlers as ThriftTestHandler };