ra | a2e4e56 | 2014-03-29 01:14:48 -0700 | [diff] [blame] | 1 | /* |
| 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 Abernethy | 96f4f07 | 2015-02-10 02:29:15 -0800 | [diff] [blame] | 20 | //This is the server side Node test handler for the standard |
ra | a2e4e56 | 2014-03-29 01:14:48 -0700 | [diff] [blame] | 21 | // Apache Thrift test service. |
Cameron Martin | 21ed4a2 | 2024-04-22 11:08:19 +0100 | [diff] [blame] | 22 | import helpers from "./helpers.js"; |
| 23 | import thrift from "thrift"; |
| 24 | |
| 25 | const ttypes = await helpers.importTypes(`ThriftTest_types`); |
| 26 | const TException = thrift.Thrift.TException; |
ra | a2e4e56 | 2014-03-29 01:14:48 -0700 | [diff] [blame] | 27 | |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 28 | function makeSyncHandler() { |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 29 | return function (thing) { |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 30 | return thing; |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 31 | }; |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 32 | } |
| 33 | |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 34 | const syncHandlers = { |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 35 | testVoid: testVoid, |
| 36 | testMapMap: testMapMap, |
| 37 | testInsanity: testInsanity, |
| 38 | testMulti: testMulti, |
| 39 | testException: testException, |
| 40 | testMultiException: testMultiException, |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 41 | testOneway: testOneway, |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 42 | }; |
| 43 | |
| 44 | function makeAsyncHandler(label) { |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 45 | return function (thing, result) { |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 46 | thing = syncHandlers[label](thing); |
| 47 | result(null, thing); |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 48 | }; |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 49 | } |
| 50 | |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 51 | const asyncHandlers = { |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 52 | testVoid: testVoidAsync, |
| 53 | testMulti: testMultiAsync, |
| 54 | testException: testExceptionAsync, |
| 55 | testMultiException: testMultiExceptionAsync, |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 56 | testOneway: testOnewayAsync, |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 57 | }; |
| 58 | |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 59 | const 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 Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 74 | "testTypedef", |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 75 | ]; |
| 76 | |
| 77 | function testVoid() { |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 78 | //console.log('testVoid()'); |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | function testVoidAsync(result) { |
| 82 | result(testVoid()); |
| 83 | } |
| 84 | |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 85 | function testMapMap() { |
| 86 | const mapmap = []; |
| 87 | const pos = []; |
| 88 | const neg = []; |
| 89 | for (let i = 1; i < 5; i++) { |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 90 | pos[i] = i; |
| 91 | neg[-i] = -i; |
| 92 | } |
| 93 | mapmap[4] = pos; |
| 94 | mapmap[-4] = neg; |
| 95 | |
| 96 | return mapmap; |
| 97 | } |
| 98 | |
| 99 | function testInsanity(argument) { |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 100 | //console.log('testInsanity('); |
| 101 | //console.log(argument); |
| 102 | //console.log(')'); |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 103 | |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 104 | const first_map = []; |
| 105 | const second_map = []; |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 106 | |
Jens Geyer | d629ea0 | 2015-09-23 21:16:50 +0200 | [diff] [blame] | 107 | first_map[ttypes.Numberz.TWO] = argument; |
| 108 | first_map[ttypes.Numberz.THREE] = argument; |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 109 | |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 110 | const looney = new ttypes.Insanity(); |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 111 | second_map[ttypes.Numberz.SIX] = looney; |
| 112 | |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 113 | const insane = []; |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 114 | insane[1] = first_map; |
| 115 | insane[2] = second_map; |
| 116 | |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 117 | //console.log('insane result:'); |
| 118 | //console.log(insane); |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 119 | return insane; |
| 120 | } |
| 121 | |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 122 | function testMulti(arg0, arg1, arg2) { |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 123 | //console.log('testMulti()'); |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 124 | |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 125 | const hello = new ttypes.Xtruct(); |
| 126 | hello.string_thing = "Hello2"; |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 127 | hello.byte_thing = arg0; |
| 128 | hello.i32_thing = arg1; |
| 129 | hello.i64_thing = arg2; |
| 130 | return hello; |
| 131 | } |
| 132 | |
| 133 | function testMultiAsync(arg0, arg1, arg2, arg3, arg4, arg5, result) { |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 134 | const hello = testMulti(arg0, arg1, arg2, arg3, arg4, arg5); |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 135 | result(null, hello); |
| 136 | } |
| 137 | |
| 138 | function testException(arg) { |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 139 | //console.log('testException('+arg+')'); |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 140 | if (arg === "Xception") { |
| 141 | const x = new ttypes.Xception(); |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 142 | x.errorCode = 1001; |
| 143 | x.message = arg; |
| 144 | throw x; |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 145 | } else if (arg === "TException") { |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 146 | throw new TException(arg); |
| 147 | } else { |
| 148 | return; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | function testExceptionAsync(arg, result) { |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 153 | //console.log('testException('+arg+')'); |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 154 | if (arg === "Xception") { |
| 155 | const x = new ttypes.Xception(); |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 156 | x.errorCode = 1001; |
| 157 | x.message = arg; |
| 158 | result(x); |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 159 | } else if (arg === "TException") { |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 160 | result(new TException(arg)); |
| 161 | } else { |
| 162 | result(null); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | function testMultiException(arg0, arg1) { |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 167 | //console.log('testMultiException(' + arg0 + ', ' + arg1 + ')'); |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 168 | if (arg0 === "Xception") { |
| 169 | const x = new ttypes.Xception(); |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 170 | x.errorCode = 1001; |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 171 | x.message = "This is an Xception"; |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 172 | throw x; |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 173 | } else if (arg0 === "Xception2") { |
| 174 | const x2 = new ttypes.Xception2(); |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 175 | x2.errorCode = 2002; |
| 176 | x2.struct_thing = new ttypes.Xtruct(); |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 177 | x2.struct_thing.string_thing = "This is an Xception2"; |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 178 | throw x2; |
| 179 | } |
| 180 | |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 181 | const res = new ttypes.Xtruct(); |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 182 | res.string_thing = arg1; |
| 183 | return res; |
| 184 | } |
| 185 | |
| 186 | function testMultiExceptionAsync(arg0, arg1, result) { |
Randy Abernethy | d8187c5 | 2015-02-16 01:25:53 -0800 | [diff] [blame] | 187 | //console.log('testMultiException(' + arg0 + ', ' + arg1 + ')'); |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 188 | if (arg0 === "Xception") { |
| 189 | const x = new ttypes.Xception(); |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 190 | x.errorCode = 1001; |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 191 | x.message = "This is an Xception"; |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 192 | result(x); |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 193 | } else if (arg0 === "Xception2") { |
| 194 | const x2 = new ttypes.Xception2(); |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 195 | x2.errorCode = 2002; |
| 196 | x2.struct_thing = new ttypes.Xtruct(); |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 197 | x2.struct_thing.string_thing = "This is an Xception2"; |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 198 | result(x2); |
Randy Abernethy | bd60b92 | 2015-02-26 16:59:14 -0800 | [diff] [blame] | 199 | } else { |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 200 | const res = new ttypes.Xtruct(); |
Randy Abernethy | bd60b92 | 2015-02-26 16:59:14 -0800 | [diff] [blame] | 201 | res.string_thing = arg1; |
| 202 | result(null, res); |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 203 | } |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 204 | } |
| 205 | |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 206 | //console.log('testOneway(' + sleepFor + ') => JavaScript (like Rust) never sleeps!'); |
| 207 | function testOneway() {} |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 208 | |
bforbis | da1169d | 2018-10-28 11:27:38 -0400 | [diff] [blame] | 209 | function testOnewayAsync(sleepFor) { |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 210 | testOneway(sleepFor); |
| 211 | } |
| 212 | |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 213 | identityHandlers.forEach(function (label) { |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 214 | syncHandlers[label] = makeSyncHandler(label); |
| 215 | asyncHandlers[label] = makeAsyncHandler(label); |
| 216 | }); |
| 217 | |
Cameron Martin | caef0ed | 2025-01-15 11:58:39 +0100 | [diff] [blame] | 218 | ["testMapMap", "testInsanity"].forEach(function (label) { |
Randy Abernethy | 3b9ff4d | 2015-02-16 00:51:24 -0800 | [diff] [blame] | 219 | asyncHandlers[label] = makeAsyncHandler(label); |
| 220 | }); |
| 221 | |
Cameron Martin | 21ed4a2 | 2024-04-22 11:08:19 +0100 | [diff] [blame] | 222 | export { asyncHandlers as ThriftTestHandler }; |