Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +0200 | [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 | |
| 20 | /*jshint evil:true*/ |
| 21 | |
| 22 | /** |
| 23 | * The Thrift namespace houses the Apache Thrift JavaScript library |
| 24 | * elements providing JavaScript bindings for the Apache Thrift RPC |
henrique | 2a7dccc | 2014-03-07 22:16:51 +0100 | [diff] [blame] | 25 | * system. End users will typically only directly make use of the |
| 26 | * Transport (TXHRTransport/TWebSocketTransport) and Protocol |
| 27 | * (TJSONPRotocol/TBinaryProtocol) constructors. |
| 28 | * |
| 29 | * Object methods beginning with a __ (e.g. __onOpen()) are internal |
| 30 | * and should not be called outside of the object's own methods. |
| 31 | * |
| 32 | * This library creates one global object: Thrift |
| 33 | * Code in this library must never create additional global identifiers, |
| 34 | * all features must be scoped within the Thrift namespace. |
Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +0200 | [diff] [blame] | 35 | * @namespace |
| 36 | * @example |
| 37 | * var transport = new Thrift.Transport("http://localhost:8585"); |
| 38 | * var protocol = new Thrift.Protocol(transport); |
| 39 | * var client = new MyThriftSvcClient(protocol); |
| 40 | * var result = client.MyMethod(); |
| 41 | */ |
| 42 | var Thrift = { |
| 43 | /** |
| 44 | * Thrift JavaScript library version. |
| 45 | * @readonly |
| 46 | * @const {string} Version |
| 47 | * @memberof Thrift |
| 48 | */ |
| 49 | Version: '1.0.0-dev', |
| 50 | |
| 51 | /** |
| 52 | * Thrift IDL type string to Id mapping. |
| 53 | * @readonly |
| 54 | * @property {number} STOP - End of a set of fields. |
| 55 | * @property {number} VOID - No value (only legal for return types). |
| 56 | * @property {number} BOOL - True/False integer. |
| 57 | * @property {number} BYTE - Signed 8 bit integer. |
| 58 | * @property {number} I08 - Signed 8 bit integer. |
| 59 | * @property {number} DOUBLE - 64 bit IEEE 854 floating point. |
| 60 | * @property {number} I16 - Signed 16 bit integer. |
| 61 | * @property {number} I32 - Signed 32 bit integer. |
| 62 | * @property {number} I64 - Signed 64 bit integer. |
| 63 | * @property {number} STRING - Array of bytes representing a string of characters. |
| 64 | * @property {number} UTF7 - Array of bytes representing a string of UTF7 encoded characters. |
| 65 | * @property {number} STRUCT - A multifield type. |
| 66 | * @property {number} MAP - A collection type (map/associative-array/dictionary). |
| 67 | * @property {number} SET - A collection type (unordered and without repeated values). |
| 68 | * @property {number} LIST - A collection type (unordered). |
| 69 | * @property {number} UTF8 - Array of bytes representing a string of UTF8 encoded characters. |
| 70 | * @property {number} UTF16 - Array of bytes representing a string of UTF16 encoded characters. |
| 71 | */ |
| 72 | Type: { |
| 73 | 'STOP' : 0, |
| 74 | 'VOID' : 1, |
| 75 | 'BOOL' : 2, |
| 76 | 'BYTE' : 3, |
| 77 | 'I08' : 3, |
| 78 | 'DOUBLE' : 4, |
| 79 | 'I16' : 6, |
| 80 | 'I32' : 8, |
| 81 | 'I64' : 10, |
| 82 | 'STRING' : 11, |
| 83 | 'UTF7' : 11, |
| 84 | 'STRUCT' : 12, |
| 85 | 'MAP' : 13, |
| 86 | 'SET' : 14, |
| 87 | 'LIST' : 15, |
| 88 | 'UTF8' : 16, |
| 89 | 'UTF16' : 17 |
| 90 | }, |
| 91 | |
| 92 | /** |
| 93 | * Thrift RPC message type string to Id mapping. |
| 94 | * @readonly |
| 95 | * @property {number} CALL - RPC call sent from client to server. |
| 96 | * @property {number} REPLY - RPC call normal response from server to client. |
| 97 | * @property {number} EXCEPTION - RPC call exception response from server to client. |
| 98 | * @property {number} ONEWAY - Oneway RPC call from client to server with no response. |
| 99 | */ |
| 100 | MessageType: { |
| 101 | 'CALL' : 1, |
| 102 | 'REPLY' : 2, |
| 103 | 'EXCEPTION' : 3, |
| 104 | 'ONEWAY' : 4 |
| 105 | }, |
| 106 | |
| 107 | /** |
| 108 | * Utility function returning the count of an object's own properties. |
| 109 | * @param {object} obj - Object to test. |
| 110 | * @returns {number} number of object's own properties |
| 111 | */ |
| 112 | objectLength: function(obj) { |
| 113 | var length = 0; |
| 114 | for (var k in obj) { |
| 115 | if (obj.hasOwnProperty(k)) { |
| 116 | length++; |
| 117 | } |
| 118 | } |
Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +0200 | [diff] [blame] | 119 | return length; |
| 120 | }, |
| 121 | |
| 122 | /** |
| 123 | * Utility function to establish prototype inheritance. |
| 124 | * @see {@link http://javascript.crockford.com/prototypal.html|Prototypal Inheritance} |
| 125 | * @param {function} constructor - Contstructor function to set as derived. |
| 126 | * @param {function} superConstructor - Contstructor function to set as base. |
| 127 | * @param {string} [name] - Type name to set as name property in derived prototype. |
| 128 | */ |
| 129 | inherits: function(constructor, superConstructor, name) { |
| 130 | function F() {} |
| 131 | F.prototype = superConstructor.prototype; |
| 132 | constructor.prototype = new F(); |
| 133 | constructor.prototype.name = name || ""; |
| 134 | } |
| 135 | }; |
| 136 | |
| 137 | /** |
| 138 | * Initializes a Thrift TException instance. |
| 139 | * @constructor |
| 140 | * @augments Error |
| 141 | * @param {string} message - The TException message (distinct from the Error message). |
| 142 | * @classdesc TException is the base class for all Thrift exceptions types. |
| 143 | */ |
| 144 | Thrift.TException = function(message) { |
| 145 | this.message = message; |
| 146 | }; |
| 147 | Thrift.inherits(Thrift.TException, Error, 'TException'); |
| 148 | |
| 149 | /** |
| 150 | * Returns the message set on the exception. |
| 151 | * @readonly |
| 152 | * @returns {string} exception message |
| 153 | */ |
| 154 | Thrift.TException.prototype.getMessage = function() { |
| 155 | return this.message; |
| 156 | }; |
| 157 | |
| 158 | /** |
| 159 | * Thrift Application Exception type string to Id mapping. |
| 160 | * @readonly |
| 161 | * @property {number} UNKNOWN - Unknown/undefined. |
| 162 | * @property {number} UNKNOWN_METHOD - Client attempted to call a method unknown to the server. |
| 163 | * @property {number} INVALID_MESSAGE_TYPE - Client passed an unknown/unsupported MessageType. |
| 164 | * @property {number} WRONG_METHOD_NAME - Unused. |
| 165 | * @property {number} BAD_SEQUENCE_ID - Unused in Thrift RPC, used to flag proprietary sequence number errors. |
| 166 | * @property {number} MISSING_RESULT - Raised by a server processor if a handler fails to supply the required return result. |
| 167 | * @property {number} INTERNAL_ERROR - Something bad happened. |
| 168 | * @property {number} PROTOCOL_ERROR - The protocol layer failed to serialize or deserialize data. |
| 169 | * @property {number} INVALID_TRANSFORM - Unused. |
| 170 | * @property {number} INVALID_PROTOCOL - The protocol (or version) is not supported. |
| 171 | * @property {number} UNSUPPORTED_CLIENT_TYPE - Unused. |
| 172 | */ |
| 173 | Thrift.TApplicationExceptionType = { |
| 174 | 'UNKNOWN' : 0, |
| 175 | 'UNKNOWN_METHOD' : 1, |
| 176 | 'INVALID_MESSAGE_TYPE' : 2, |
| 177 | 'WRONG_METHOD_NAME' : 3, |
| 178 | 'BAD_SEQUENCE_ID' : 4, |
| 179 | 'MISSING_RESULT' : 5, |
| 180 | 'INTERNAL_ERROR' : 6, |
| 181 | 'PROTOCOL_ERROR' : 7, |
| 182 | 'INVALID_TRANSFORM' : 8, |
| 183 | 'INVALID_PROTOCOL' : 9, |
| 184 | 'UNSUPPORTED_CLIENT_TYPE' : 10 |
| 185 | }; |
| 186 | |
| 187 | /** |
| 188 | * Initializes a Thrift TApplicationException instance. |
| 189 | * @constructor |
| 190 | * @augments Thrift.TException |
| 191 | * @param {string} message - The TApplicationException message (distinct from the Error message). |
| 192 | * @param {Thrift.TApplicationExceptionType} [code] - The TApplicationExceptionType code. |
| 193 | * @classdesc TApplicationException is the exception class used to propagate exceptions from an RPC server back to a calling client. |
| 194 | */ |
| 195 | Thrift.TApplicationException = function(message, code) { |
| 196 | this.message = message; |
| 197 | this.code = typeof code === "number" ? code : 0; |
| 198 | }; |
| 199 | Thrift.inherits(Thrift.TApplicationException, Thrift.TException, 'TApplicationException'); |
| 200 | |
| 201 | /** |
| 202 | * Read a TApplicationException from the supplied protocol. |
| 203 | * @param {object} input - The input protocol to read from. |
| 204 | */ |
| 205 | Thrift.TApplicationException.prototype.read = function(input) { |
| 206 | while (1) { |
| 207 | var ret = input.readFieldBegin(); |
| 208 | |
| 209 | if (ret.ftype == Thrift.Type.STOP) { |
| 210 | break; |
| 211 | } |
| 212 | |
| 213 | var fid = ret.fid; |
| 214 | |
| 215 | switch (fid) { |
| 216 | case 1: |
| 217 | if (ret.ftype == Thrift.Type.STRING) { |
| 218 | ret = input.readString(); |
| 219 | this.message = ret.value; |
| 220 | } else { |
| 221 | ret = input.skip(ret.ftype); |
| 222 | } |
| 223 | break; |
| 224 | case 2: |
| 225 | if (ret.ftype == Thrift.Type.I32) { |
| 226 | ret = input.readI32(); |
| 227 | this.code = ret.value; |
| 228 | } else { |
| 229 | ret = input.skip(ret.ftype); |
| 230 | } |
| 231 | break; |
| 232 | default: |
| 233 | ret = input.skip(ret.ftype); |
| 234 | break; |
| 235 | } |
| 236 | |
| 237 | input.readFieldEnd(); |
| 238 | } |
| 239 | |
| 240 | input.readStructEnd(); |
| 241 | }; |
| 242 | |
| 243 | /** |
| 244 | * Wite a TApplicationException to the supplied protocol. |
| 245 | * @param {object} output - The output protocol to write to. |
| 246 | */ |
| 247 | Thrift.TApplicationException.prototype.write = function(output) { |
| 248 | output.writeStructBegin('TApplicationException'); |
| 249 | |
| 250 | if (this.message) { |
| 251 | output.writeFieldBegin('message', Thrift.Type.STRING, 1); |
| 252 | output.writeString(this.getMessage()); |
| 253 | output.writeFieldEnd(); |
| 254 | } |
| 255 | |
| 256 | if (this.code) { |
| 257 | output.writeFieldBegin('type', Thrift.Type.I32, 2); |
| 258 | output.writeI32(this.code); |
| 259 | output.writeFieldEnd(); |
| 260 | } |
| 261 | |
| 262 | output.writeFieldStop(); |
| 263 | output.writeStructEnd(); |
| 264 | }; |
| 265 | |
| 266 | /** |
| 267 | * Returns the application exception code set on the exception. |
| 268 | * @readonly |
| 269 | * @returns {Thrift.TApplicationExceptionType} exception code |
| 270 | */ |
| 271 | Thrift.TApplicationException.prototype.getCode = function() { |
| 272 | return this.code; |
| 273 | }; |
| 274 | |
| 275 | /** |
henrique | 2a7dccc | 2014-03-07 22:16:51 +0100 | [diff] [blame] | 276 | * Constructor Function for the XHR transport. |
| 277 | * If you do not specify a url then you must handle XHR operations on |
| 278 | * your own. This type can also be constructed using the Transport alias |
| 279 | * for backward compatibility. |
Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +0200 | [diff] [blame] | 280 | * @constructor |
| 281 | * @param {string} [url] - The URL to connect to. |
henrique | 2a7dccc | 2014-03-07 22:16:51 +0100 | [diff] [blame] | 282 | * @classdesc The Apache Thrift Transport layer performs byte level I/O |
| 283 | * between RPC clients and servers. The JavaScript TXHRTransport object |
| 284 | * uses Http[s]/XHR. Target servers must implement the http[s] transport |
| 285 | * (see: node.js example server_http.js). |
Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +0200 | [diff] [blame] | 286 | * @example |
henrique | 2a7dccc | 2014-03-07 22:16:51 +0100 | [diff] [blame] | 287 | * var transport = new Thrift.TXHRTransport("http://localhost:8585"); |
Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +0200 | [diff] [blame] | 288 | */ |
Roger Meier | 52744ee | 2014-03-12 09:38:42 +0100 | [diff] [blame] | 289 | Thrift.Transport = Thrift.TXHRTransport = function(url, options) { |
Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +0200 | [diff] [blame] | 290 | this.url = url; |
| 291 | this.wpos = 0; |
| 292 | this.rpos = 0; |
Roger Meier | 52744ee | 2014-03-12 09:38:42 +0100 | [diff] [blame] | 293 | this.useCORS = (options && options.useCORS); |
Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +0200 | [diff] [blame] | 294 | this.send_buf = ''; |
| 295 | this.recv_buf = ''; |
| 296 | }; |
| 297 | |
henrique | 2a7dccc | 2014-03-07 22:16:51 +0100 | [diff] [blame] | 298 | Thrift.TXHRTransport.prototype = { |
Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +0200 | [diff] [blame] | 299 | /** |
| 300 | * Gets the browser specific XmlHttpRequest Object. |
| 301 | * @returns {object} the browser XHR interface object |
| 302 | */ |
| 303 | getXmlHttpRequestObject: function() { |
| 304 | try { return new XMLHttpRequest(); } catch (e1) { } |
| 305 | try { return new ActiveXObject('Msxml2.XMLHTTP'); } catch (e2) { } |
| 306 | try { return new ActiveXObject('Microsoft.XMLHTTP'); } catch (e3) { } |
| 307 | |
| 308 | throw "Your browser doesn't support XHR."; |
| 309 | }, |
| 310 | |
| 311 | /** |
henrique | 2a7dccc | 2014-03-07 22:16:51 +0100 | [diff] [blame] | 312 | * Sends the current XRH request if the transport was created with a URL |
| 313 | * and the async parameter is false. If the transport was not created with |
| 314 | * a URL, or the async parameter is True and no callback is provided, or |
| 315 | * the URL is an empty string, the current send buffer is returned. |
Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +0200 | [diff] [blame] | 316 | * @param {object} async - If true the current send buffer is returned. |
henrique | 2a7dccc | 2014-03-07 22:16:51 +0100 | [diff] [blame] | 317 | * @param {object} callback - Optional async completion callback |
Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +0200 | [diff] [blame] | 318 | * @returns {undefined|string} Nothing or the current send buffer. |
| 319 | * @throws {string} If XHR fails. |
| 320 | */ |
henrique | a2de410 | 2014-02-07 14:12:56 +0100 | [diff] [blame] | 321 | flush: function(async, callback) { |
henrique | 2a7dccc | 2014-03-07 22:16:51 +0100 | [diff] [blame] | 322 | var self = this; |
henrique | a2de410 | 2014-02-07 14:12:56 +0100 | [diff] [blame] | 323 | if ((async && !callback) || this.url === undefined || this.url === '') { |
Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +0200 | [diff] [blame] | 324 | return this.send_buf; |
| 325 | } |
| 326 | |
| 327 | var xreq = this.getXmlHttpRequestObject(); |
| 328 | |
| 329 | if (xreq.overrideMimeType) { |
henrique | eec445e | 2015-05-04 21:37:51 +1000 | [diff] [blame] | 330 | xreq.overrideMimeType('application/vnd.apache.thrift.json; charset=utf-8'); |
Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +0200 | [diff] [blame] | 331 | } |
| 332 | |
henrique | a2de410 | 2014-02-07 14:12:56 +0100 | [diff] [blame] | 333 | if (callback) { |
henrique | 2a7dccc | 2014-03-07 22:16:51 +0100 | [diff] [blame] | 334 | //Ignore XHR callbacks until the data arrives, then call the |
| 335 | // client's callback |
| 336 | xreq.onreadystatechange = |
| 337 | (function() { |
| 338 | var clientCallback = callback; |
| 339 | return function() { |
| 340 | if (this.readyState == 4 && this.status == 200) { |
| 341 | self.setRecvBuffer(this.responseText); |
| 342 | clientCallback(); |
| 343 | } |
| 344 | }; |
| 345 | }()); |
henrique | a2de410 | 2014-02-07 14:12:56 +0100 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | xreq.open('POST', this.url, !!async); |
henrique | eec445e | 2015-05-04 21:37:51 +1000 | [diff] [blame] | 349 | |
| 350 | if (xreq.setRequestHeader) { |
| 351 | xreq.setRequestHeader('Accept', 'application/vnd.apache.thrift.json; charset=utf-8'); |
| 352 | xreq.setRequestHeader('Content-Type', 'application/vnd.apache.thrift.json; charset=utf-8'); |
| 353 | } |
| 354 | |
Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +0200 | [diff] [blame] | 355 | xreq.send(this.send_buf); |
henrique | a2de410 | 2014-02-07 14:12:56 +0100 | [diff] [blame] | 356 | if (async && callback) { |
| 357 | return; |
| 358 | } |
Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +0200 | [diff] [blame] | 359 | |
| 360 | if (xreq.readyState != 4) { |
| 361 | throw 'encountered an unknown ajax ready state: ' + xreq.readyState; |
| 362 | } |
| 363 | |
| 364 | if (xreq.status != 200) { |
| 365 | throw 'encountered a unknown request status: ' + xreq.status; |
| 366 | } |
| 367 | |
| 368 | this.recv_buf = xreq.responseText; |
| 369 | this.recv_buf_sz = this.recv_buf.length; |
| 370 | this.wpos = this.recv_buf.length; |
| 371 | this.rpos = 0; |
| 372 | }, |
| 373 | |
| 374 | /** |
| 375 | * Creates a jQuery XHR object to be used for a Thrift server call. |
| 376 | * @param {object} client - The Thrift Service client object generated by the IDL compiler. |
| 377 | * @param {object} postData - The message to send to the server. |
henrique | 2a7dccc | 2014-03-07 22:16:51 +0100 | [diff] [blame] | 378 | * @param {function} args - The original call arguments with the success call back at the end. |
Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +0200 | [diff] [blame] | 379 | * @param {function} recv_method - The Thrift Service Client receive method for the call. |
| 380 | * @returns {object} A new jQuery XHR object. |
| 381 | * @throws {string} If the jQuery version is prior to 1.5 or if jQuery is not found. |
| 382 | */ |
| 383 | jqRequest: function(client, postData, args, recv_method) { |
| 384 | if (typeof jQuery === 'undefined' || |
| 385 | typeof jQuery.Deferred === 'undefined') { |
| 386 | throw 'Thrift.js requires jQuery 1.5+ to use asynchronous requests'; |
| 387 | } |
| 388 | |
| 389 | var thriftTransport = this; |
| 390 | |
| 391 | var jqXHR = jQuery.ajax({ |
| 392 | url: this.url, |
| 393 | data: postData, |
| 394 | type: 'POST', |
| 395 | cache: false, |
henrique | eec445e | 2015-05-04 21:37:51 +1000 | [diff] [blame] | 396 | contentType: 'application/vnd.apache.thrift.json; charset=utf-8', |
Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +0200 | [diff] [blame] | 397 | dataType: 'text thrift', |
| 398 | converters: { |
| 399 | 'text thrift' : function(responseData) { |
| 400 | thriftTransport.setRecvBuffer(responseData); |
| 401 | var value = recv_method.call(client); |
| 402 | return value; |
| 403 | } |
| 404 | }, |
| 405 | context: client, |
| 406 | success: jQuery.makeArray(args).pop() |
| 407 | }); |
| 408 | |
| 409 | return jqXHR; |
| 410 | }, |
| 411 | |
| 412 | /** |
henrique | 2a7dccc | 2014-03-07 22:16:51 +0100 | [diff] [blame] | 413 | * Sets the buffer to provide the protocol when deserializing. |
| 414 | * @param {string} buf - The buffer to supply the protocol. |
| 415 | */ |
| 416 | setRecvBuffer: function(buf) { |
| 417 | this.recv_buf = buf; |
| 418 | this.recv_buf_sz = this.recv_buf.length; |
| 419 | this.wpos = this.recv_buf.length; |
| 420 | this.rpos = 0; |
| 421 | }, |
| 422 | |
| 423 | /** |
| 424 | * Returns true if the transport is open, XHR always returns true. |
| 425 | * @readonly |
| 426 | * @returns {boolean} Always True. |
| 427 | */ |
| 428 | isOpen: function() { |
| 429 | return true; |
| 430 | }, |
| 431 | |
| 432 | /** |
| 433 | * Opens the transport connection, with XHR this is a nop. |
| 434 | */ |
| 435 | open: function() {}, |
| 436 | |
| 437 | /** |
| 438 | * Closes the transport connection, with XHR this is a nop. |
| 439 | */ |
| 440 | close: function() {}, |
| 441 | |
| 442 | /** |
| 443 | * Returns the specified number of characters from the response |
| 444 | * buffer. |
| 445 | * @param {number} len - The number of characters to return. |
| 446 | * @returns {string} Characters sent by the server. |
| 447 | */ |
| 448 | read: function(len) { |
| 449 | var avail = this.wpos - this.rpos; |
| 450 | |
| 451 | if (avail === 0) { |
| 452 | return ''; |
| 453 | } |
| 454 | |
| 455 | var give = len; |
| 456 | |
| 457 | if (avail < len) { |
| 458 | give = avail; |
| 459 | } |
| 460 | |
| 461 | var ret = this.read_buf.substr(this.rpos, give); |
| 462 | this.rpos += give; |
| 463 | |
| 464 | //clear buf when complete? |
| 465 | return ret; |
| 466 | }, |
| 467 | |
| 468 | /** |
| 469 | * Returns the entire response buffer. |
| 470 | * @returns {string} Characters sent by the server. |
| 471 | */ |
| 472 | readAll: function() { |
| 473 | return this.recv_buf; |
| 474 | }, |
| 475 | |
| 476 | /** |
| 477 | * Sets the send buffer to buf. |
| 478 | * @param {string} buf - The buffer to send. |
| 479 | */ |
| 480 | write: function(buf) { |
| 481 | this.send_buf = buf; |
| 482 | }, |
| 483 | |
| 484 | /** |
| 485 | * Returns the send buffer. |
| 486 | * @readonly |
| 487 | * @returns {string} The send buffer. |
| 488 | */ |
| 489 | getSendBuffer: function() { |
| 490 | return this.send_buf; |
| 491 | } |
| 492 | |
| 493 | }; |
| 494 | |
| 495 | |
| 496 | /** |
| 497 | * Constructor Function for the WebSocket transport. |
| 498 | * @constructor |
| 499 | * @param {string} [url] - The URL to connect to. |
| 500 | * @classdesc The Apache Thrift Transport layer performs byte level I/O |
| 501 | * between RPC clients and servers. The JavaScript TWebSocketTransport object |
| 502 | * uses the WebSocket protocol. Target servers must implement WebSocket. |
| 503 | * (see: node.js example server_http.js). |
| 504 | * @example |
| 505 | * var transport = new Thrift.TWebSocketTransport("http://localhost:8585"); |
| 506 | */ |
| 507 | Thrift.TWebSocketTransport = function(url) { |
| 508 | this.__reset(url); |
| 509 | }; |
| 510 | |
| 511 | Thrift.TWebSocketTransport.prototype = { |
| 512 | __reset: function(url) { |
| 513 | this.url = url; //Where to connect |
| 514 | this.socket = null; //The web socket |
| 515 | this.callbacks = []; //Pending callbacks |
| 516 | this.send_pending = []; //Buffers/Callback pairs waiting to be sent |
| 517 | this.send_buf = ''; //Outbound data, immutable until sent |
| 518 | this.recv_buf = ''; //Inbound data |
| 519 | this.rb_wpos = 0; //Network write position in receive buffer |
| 520 | this.rb_rpos = 0; //Client read position in receive buffer |
| 521 | }, |
| 522 | |
| 523 | /** |
| 524 | * Sends the current WS request and registers callback. The async |
| 525 | * parameter is ignored (WS flush is always async) and the callback |
| 526 | * function parameter is required. |
| 527 | * @param {object} async - Ignored. |
| 528 | * @param {object} callback - The client completion callback. |
| 529 | * @returns {undefined|string} Nothing (undefined) |
| 530 | */ |
| 531 | flush: function(async, callback) { |
| 532 | var self = this; |
| 533 | if (this.isOpen()) { |
| 534 | //Send data and register a callback to invoke the client callback |
| 535 | this.socket.send(this.send_buf); |
| 536 | this.callbacks.push((function() { |
| 537 | var clientCallback = callback; |
| 538 | return function(msg) { |
| 539 | self.setRecvBuffer(msg); |
| 540 | clientCallback(); |
| 541 | }; |
| 542 | }())); |
| 543 | } else { |
| 544 | //Queue the send to go out __onOpen |
| 545 | this.send_pending.push({ |
| 546 | buf: this.send_buf, |
| 547 | cb: callback |
| 548 | }); |
| 549 | } |
| 550 | }, |
| 551 | |
| 552 | __onOpen: function() { |
| 553 | var self = this; |
| 554 | if (this.send_pending.length > 0) { |
| 555 | //If the user made calls before the connection was fully |
| 556 | //open, send them now |
| 557 | this.send_pending.forEach(function(elem) { |
| 558 | this.socket.send(elem.buf); |
| 559 | this.callbacks.push((function() { |
| 560 | var clientCallback = elem.cb; |
| 561 | return function(msg) { |
| 562 | self.setRecvBuffer(msg); |
| 563 | clientCallback(); |
| 564 | }; |
| 565 | }())); |
| 566 | }); |
| 567 | this.send_pending = []; |
| 568 | } |
| 569 | }, |
| 570 | |
| 571 | __onClose: function(evt) { |
| 572 | this.__reset(this.url); |
| 573 | }, |
| 574 | |
| 575 | __onMessage: function(evt) { |
| 576 | if (this.callbacks.length) { |
| 577 | this.callbacks.shift()(evt.data); |
| 578 | } |
| 579 | }, |
| 580 | |
| 581 | __onError: function(evt) { |
| 582 | console.log("Thrift WebSocket Error: " + evt.toString()); |
| 583 | this.socket.close(); |
| 584 | }, |
| 585 | |
| 586 | /** |
Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +0200 | [diff] [blame] | 587 | * Sets the buffer to use when receiving server responses. |
| 588 | * @param {string} buf - The buffer to receive server responses. |
| 589 | */ |
| 590 | setRecvBuffer: function(buf) { |
| 591 | this.recv_buf = buf; |
| 592 | this.recv_buf_sz = this.recv_buf.length; |
| 593 | this.wpos = this.recv_buf.length; |
| 594 | this.rpos = 0; |
| 595 | }, |
| 596 | |
| 597 | /** |
henrique | 2a7dccc | 2014-03-07 22:16:51 +0100 | [diff] [blame] | 598 | * Returns true if the transport is open |
Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +0200 | [diff] [blame] | 599 | * @readonly |
henrique | 2a7dccc | 2014-03-07 22:16:51 +0100 | [diff] [blame] | 600 | * @returns {boolean} |
Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +0200 | [diff] [blame] | 601 | */ |
| 602 | isOpen: function() { |
henrique | 2a7dccc | 2014-03-07 22:16:51 +0100 | [diff] [blame] | 603 | return this.socket && this.socket.readyState == this.socket.OPEN; |
Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +0200 | [diff] [blame] | 604 | }, |
| 605 | |
| 606 | /** |
henrique | 2a7dccc | 2014-03-07 22:16:51 +0100 | [diff] [blame] | 607 | * Opens the transport connection |
Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +0200 | [diff] [blame] | 608 | */ |
henrique | 2a7dccc | 2014-03-07 22:16:51 +0100 | [diff] [blame] | 609 | open: function() { |
| 610 | //If OPEN/CONNECTING/CLOSING ignore additional opens |
| 611 | if (this.socket && this.socket.readyState != this.socket.CLOSED) { |
| 612 | return; |
| 613 | } |
| 614 | //If there is no socket or the socket is closed: |
| 615 | this.socket = new WebSocket(this.url); |
| 616 | this.socket.onopen = this.__onOpen.bind(this); |
| 617 | this.socket.onmessage = this.__onMessage.bind(this); |
| 618 | this.socket.onerror = this.__onError.bind(this); |
| 619 | this.socket.onclose = this.__onClose.bind(this); |
| 620 | }, |
Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +0200 | [diff] [blame] | 621 | |
| 622 | /** |
henrique | 2a7dccc | 2014-03-07 22:16:51 +0100 | [diff] [blame] | 623 | * Closes the transport connection |
Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +0200 | [diff] [blame] | 624 | */ |
henrique | 2a7dccc | 2014-03-07 22:16:51 +0100 | [diff] [blame] | 625 | close: function() { |
| 626 | this.socket.close(); |
| 627 | }, |
Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +0200 | [diff] [blame] | 628 | |
| 629 | /** |
| 630 | * Returns the specified number of characters from the response |
| 631 | * buffer. |
| 632 | * @param {number} len - The number of characters to return. |
| 633 | * @returns {string} Characters sent by the server. |
| 634 | */ |
| 635 | read: function(len) { |
| 636 | var avail = this.wpos - this.rpos; |
| 637 | |
| 638 | if (avail === 0) { |
| 639 | return ''; |
| 640 | } |
| 641 | |
| 642 | var give = len; |
| 643 | |
| 644 | if (avail < len) { |
| 645 | give = avail; |
| 646 | } |
| 647 | |
| 648 | var ret = this.read_buf.substr(this.rpos, give); |
| 649 | this.rpos += give; |
| 650 | |
| 651 | //clear buf when complete? |
| 652 | return ret; |
| 653 | }, |
| 654 | |
| 655 | /** |
| 656 | * Returns the entire response buffer. |
| 657 | * @returns {string} Characters sent by the server. |
| 658 | */ |
| 659 | readAll: function() { |
| 660 | return this.recv_buf; |
| 661 | }, |
| 662 | |
| 663 | /** |
| 664 | * Sets the send buffer to buf. |
| 665 | * @param {string} buf - The buffer to send. |
| 666 | */ |
| 667 | write: function(buf) { |
| 668 | this.send_buf = buf; |
| 669 | }, |
| 670 | |
| 671 | /** |
| 672 | * Returns the send buffer. |
| 673 | * @readonly |
| 674 | * @returns {string} The send buffer. |
| 675 | */ |
| 676 | getSendBuffer: function() { |
| 677 | return this.send_buf; |
| 678 | } |
| 679 | |
| 680 | }; |
| 681 | |
| 682 | /** |
| 683 | * Initializes a Thrift JSON protocol instance. |
| 684 | * @constructor |
| 685 | * @param {Thrift.Transport} transport - The transport to serialize to/from. |
| 686 | * @classdesc Apache Thrift Protocols perform serialization which enables cross |
| 687 | * language RPC. The Protocol type is the JavaScript browser implementation |
| 688 | * of the Apache Thrift TJSONProtocol. |
| 689 | * @example |
| 690 | * var protocol = new Thrift.Protocol(transport); |
| 691 | */ |
Roger Meier | 52744ee | 2014-03-12 09:38:42 +0100 | [diff] [blame] | 692 | Thrift.TJSONProtocol = Thrift.Protocol = function(transport) { |
radekg | 1d30558 | 2015-01-01 20:35:01 +0100 | [diff] [blame] | 693 | this.tstack = []; |
| 694 | this.tpos = []; |
Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +0200 | [diff] [blame] | 695 | this.transport = transport; |
| 696 | }; |
| 697 | |
| 698 | /** |
| 699 | * Thrift IDL type Id to string mapping. |
| 700 | * @readonly |
| 701 | * @see {@link Thrift.Type} |
| 702 | */ |
| 703 | Thrift.Protocol.Type = {}; |
| 704 | Thrift.Protocol.Type[Thrift.Type.BOOL] = '"tf"'; |
| 705 | Thrift.Protocol.Type[Thrift.Type.BYTE] = '"i8"'; |
| 706 | Thrift.Protocol.Type[Thrift.Type.I16] = '"i16"'; |
| 707 | Thrift.Protocol.Type[Thrift.Type.I32] = '"i32"'; |
| 708 | Thrift.Protocol.Type[Thrift.Type.I64] = '"i64"'; |
| 709 | Thrift.Protocol.Type[Thrift.Type.DOUBLE] = '"dbl"'; |
| 710 | Thrift.Protocol.Type[Thrift.Type.STRUCT] = '"rec"'; |
| 711 | Thrift.Protocol.Type[Thrift.Type.STRING] = '"str"'; |
| 712 | Thrift.Protocol.Type[Thrift.Type.MAP] = '"map"'; |
| 713 | Thrift.Protocol.Type[Thrift.Type.LIST] = '"lst"'; |
| 714 | Thrift.Protocol.Type[Thrift.Type.SET] = '"set"'; |
| 715 | |
| 716 | /** |
| 717 | * Thrift IDL type string to Id mapping. |
| 718 | * @readonly |
| 719 | * @see {@link Thrift.Type} |
| 720 | */ |
| 721 | Thrift.Protocol.RType = {}; |
| 722 | Thrift.Protocol.RType.tf = Thrift.Type.BOOL; |
| 723 | Thrift.Protocol.RType.i8 = Thrift.Type.BYTE; |
| 724 | Thrift.Protocol.RType.i16 = Thrift.Type.I16; |
| 725 | Thrift.Protocol.RType.i32 = Thrift.Type.I32; |
| 726 | Thrift.Protocol.RType.i64 = Thrift.Type.I64; |
| 727 | Thrift.Protocol.RType.dbl = Thrift.Type.DOUBLE; |
| 728 | Thrift.Protocol.RType.rec = Thrift.Type.STRUCT; |
| 729 | Thrift.Protocol.RType.str = Thrift.Type.STRING; |
| 730 | Thrift.Protocol.RType.map = Thrift.Type.MAP; |
| 731 | Thrift.Protocol.RType.lst = Thrift.Type.LIST; |
| 732 | Thrift.Protocol.RType.set = Thrift.Type.SET; |
| 733 | |
| 734 | /** |
| 735 | * The TJSONProtocol version number. |
| 736 | * @readonly |
| 737 | * @const {number} Version |
| 738 | * @memberof Thrift.Protocol |
| 739 | */ |
| 740 | Thrift.Protocol.Version = 1; |
| 741 | |
| 742 | Thrift.Protocol.prototype = { |
| 743 | /** |
| 744 | * Returns the underlying transport. |
| 745 | * @readonly |
| 746 | * @returns {Thrift.Transport} The underlying transport. |
| 747 | */ |
| 748 | getTransport: function() { |
| 749 | return this.transport; |
| 750 | }, |
| 751 | |
| 752 | /** |
| 753 | * Serializes the beginning of a Thrift RPC message. |
| 754 | * @param {string} name - The service method to call. |
| 755 | * @param {Thrift.MessageType} messageType - The type of method call. |
| 756 | * @param {number} seqid - The sequence number of this call (always 0 in Apache Thrift). |
| 757 | */ |
| 758 | writeMessageBegin: function(name, messageType, seqid) { |
| 759 | this.tstack = []; |
| 760 | this.tpos = []; |
| 761 | |
| 762 | this.tstack.push([Thrift.Protocol.Version, '"' + |
| 763 | name + '"', messageType, seqid]); |
| 764 | }, |
| 765 | |
| 766 | /** |
| 767 | * Serializes the end of a Thrift RPC message. |
| 768 | */ |
| 769 | writeMessageEnd: function() { |
| 770 | var obj = this.tstack.pop(); |
| 771 | |
| 772 | this.wobj = this.tstack.pop(); |
| 773 | this.wobj.push(obj); |
| 774 | |
| 775 | this.wbuf = '[' + this.wobj.join(',') + ']'; |
| 776 | |
| 777 | this.transport.write(this.wbuf); |
| 778 | }, |
| 779 | |
| 780 | |
| 781 | /** |
| 782 | * Serializes the beginning of a struct. |
| 783 | * @param {string} name - The name of the struct. |
| 784 | */ |
| 785 | writeStructBegin: function(name) { |
| 786 | this.tpos.push(this.tstack.length); |
| 787 | this.tstack.push({}); |
| 788 | }, |
| 789 | |
| 790 | /** |
| 791 | * Serializes the end of a struct. |
| 792 | */ |
| 793 | writeStructEnd: function() { |
| 794 | |
| 795 | var p = this.tpos.pop(); |
| 796 | var struct = this.tstack[p]; |
| 797 | var str = '{'; |
| 798 | var first = true; |
| 799 | for (var key in struct) { |
| 800 | if (first) { |
| 801 | first = false; |
| 802 | } else { |
| 803 | str += ','; |
| 804 | } |
| 805 | |
| 806 | str += key + ':' + struct[key]; |
| 807 | } |
| 808 | |
| 809 | str += '}'; |
| 810 | this.tstack[p] = str; |
| 811 | }, |
| 812 | |
| 813 | /** |
| 814 | * Serializes the beginning of a struct field. |
| 815 | * @param {string} name - The name of the field. |
| 816 | * @param {Thrift.Protocol.Type} fieldType - The data type of the field. |
| 817 | * @param {number} fieldId - The field's unique identifier. |
| 818 | */ |
| 819 | writeFieldBegin: function(name, fieldType, fieldId) { |
| 820 | this.tpos.push(this.tstack.length); |
| 821 | this.tstack.push({ 'fieldId': '"' + |
| 822 | fieldId + '"', 'fieldType': Thrift.Protocol.Type[fieldType] |
| 823 | }); |
| 824 | |
| 825 | }, |
| 826 | |
| 827 | /** |
| 828 | * Serializes the end of a field. |
| 829 | */ |
| 830 | writeFieldEnd: function() { |
| 831 | var value = this.tstack.pop(); |
| 832 | var fieldInfo = this.tstack.pop(); |
| 833 | |
| 834 | this.tstack[this.tstack.length - 1][fieldInfo.fieldId] = '{' + |
| 835 | fieldInfo.fieldType + ':' + value + '}'; |
| 836 | this.tpos.pop(); |
| 837 | }, |
| 838 | |
| 839 | /** |
| 840 | * Serializes the end of the set of fields for a struct. |
| 841 | */ |
| 842 | writeFieldStop: function() { |
| 843 | //na |
| 844 | }, |
| 845 | |
| 846 | /** |
| 847 | * Serializes the beginning of a map collection. |
| 848 | * @param {Thrift.Type} keyType - The data type of the key. |
| 849 | * @param {Thrift.Type} valType - The data type of the value. |
| 850 | * @param {number} [size] - The number of elements in the map (ignored). |
| 851 | */ |
| 852 | writeMapBegin: function(keyType, valType, size) { |
| 853 | this.tpos.push(this.tstack.length); |
| 854 | this.tstack.push([Thrift.Protocol.Type[keyType], |
| 855 | Thrift.Protocol.Type[valType], 0]); |
| 856 | }, |
| 857 | |
| 858 | /** |
| 859 | * Serializes the end of a map. |
| 860 | */ |
| 861 | writeMapEnd: function() { |
| 862 | var p = this.tpos.pop(); |
| 863 | |
| 864 | if (p == this.tstack.length) { |
| 865 | return; |
| 866 | } |
| 867 | |
| 868 | if ((this.tstack.length - p - 1) % 2 !== 0) { |
| 869 | this.tstack.push(''); |
| 870 | } |
| 871 | |
| 872 | var size = (this.tstack.length - p - 1) / 2; |
| 873 | |
| 874 | this.tstack[p][this.tstack[p].length - 1] = size; |
| 875 | |
| 876 | var map = '}'; |
| 877 | var first = true; |
| 878 | while (this.tstack.length > p + 1) { |
| 879 | var v = this.tstack.pop(); |
| 880 | var k = this.tstack.pop(); |
| 881 | if (first) { |
| 882 | first = false; |
| 883 | } else { |
| 884 | map = ',' + map; |
| 885 | } |
| 886 | |
| 887 | if (! isNaN(k)) { k = '"' + k + '"'; } //json "keys" need to be strings |
| 888 | map = k + ':' + v + map; |
| 889 | } |
| 890 | map = '{' + map; |
| 891 | |
| 892 | this.tstack[p].push(map); |
| 893 | this.tstack[p] = '[' + this.tstack[p].join(',') + ']'; |
| 894 | }, |
| 895 | |
| 896 | /** |
| 897 | * Serializes the beginning of a list collection. |
| 898 | * @param {Thrift.Type} elemType - The data type of the elements. |
| 899 | * @param {number} size - The number of elements in the list. |
| 900 | */ |
| 901 | writeListBegin: function(elemType, size) { |
| 902 | this.tpos.push(this.tstack.length); |
| 903 | this.tstack.push([Thrift.Protocol.Type[elemType], size]); |
| 904 | }, |
| 905 | |
| 906 | /** |
| 907 | * Serializes the end of a list. |
| 908 | */ |
| 909 | writeListEnd: function() { |
| 910 | var p = this.tpos.pop(); |
| 911 | |
| 912 | while (this.tstack.length > p + 1) { |
| 913 | var tmpVal = this.tstack[p + 1]; |
| 914 | this.tstack.splice(p + 1, 1); |
| 915 | this.tstack[p].push(tmpVal); |
| 916 | } |
| 917 | |
| 918 | this.tstack[p] = '[' + this.tstack[p].join(',') + ']'; |
| 919 | }, |
| 920 | |
| 921 | /** |
| 922 | * Serializes the beginning of a set collection. |
| 923 | * @param {Thrift.Type} elemType - The data type of the elements. |
| 924 | * @param {number} size - The number of elements in the list. |
| 925 | */ |
| 926 | writeSetBegin: function(elemType, size) { |
| 927 | this.tpos.push(this.tstack.length); |
| 928 | this.tstack.push([Thrift.Protocol.Type[elemType], size]); |
| 929 | }, |
| 930 | |
| 931 | /** |
| 932 | * Serializes the end of a set. |
| 933 | */ |
| 934 | writeSetEnd: function() { |
| 935 | var p = this.tpos.pop(); |
| 936 | |
| 937 | while (this.tstack.length > p + 1) { |
| 938 | var tmpVal = this.tstack[p + 1]; |
| 939 | this.tstack.splice(p + 1, 1); |
| 940 | this.tstack[p].push(tmpVal); |
| 941 | } |
| 942 | |
| 943 | this.tstack[p] = '[' + this.tstack[p].join(',') + ']'; |
| 944 | }, |
| 945 | |
| 946 | /** Serializes a boolean */ |
| 947 | writeBool: function(value) { |
| 948 | this.tstack.push(value ? 1 : 0); |
| 949 | }, |
| 950 | |
| 951 | /** Serializes a number */ |
| 952 | writeByte: function(i8) { |
| 953 | this.tstack.push(i8); |
| 954 | }, |
| 955 | |
| 956 | /** Serializes a number */ |
| 957 | writeI16: function(i16) { |
| 958 | this.tstack.push(i16); |
| 959 | }, |
| 960 | |
| 961 | /** Serializes a number */ |
| 962 | writeI32: function(i32) { |
| 963 | this.tstack.push(i32); |
| 964 | }, |
| 965 | |
| 966 | /** Serializes a number */ |
| 967 | writeI64: function(i64) { |
| 968 | this.tstack.push(i64); |
| 969 | }, |
| 970 | |
| 971 | /** Serializes a number */ |
| 972 | writeDouble: function(dbl) { |
| 973 | this.tstack.push(dbl); |
| 974 | }, |
| 975 | |
| 976 | /** Serializes a string */ |
| 977 | writeString: function(str) { |
| 978 | // We do not encode uri components for wire transfer: |
| 979 | if (str === null) { |
| 980 | this.tstack.push(null); |
| 981 | } else { |
| 982 | // concat may be slower than building a byte buffer |
| 983 | var escapedString = ''; |
| 984 | for (var i = 0; i < str.length; i++) { |
| 985 | var ch = str.charAt(i); // a single double quote: " |
| 986 | if (ch === '\"') { |
| 987 | escapedString += '\\\"'; // write out as: \" |
Roger Meier | 52744ee | 2014-03-12 09:38:42 +0100 | [diff] [blame] | 988 | } else if (ch === '\\') { // a single backslash |
| 989 | escapedString += '\\\\'; // write out as double backslash |
Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +0200 | [diff] [blame] | 990 | } else if (ch === '\b') { // a single backspace: invisible |
| 991 | escapedString += '\\b'; // write out as: \b" |
| 992 | } else if (ch === '\f') { // a single formfeed: invisible |
| 993 | escapedString += '\\f'; // write out as: \f" |
| 994 | } else if (ch === '\n') { // a single newline: invisible |
| 995 | escapedString += '\\n'; // write out as: \n" |
| 996 | } else if (ch === '\r') { // a single return: invisible |
| 997 | escapedString += '\\r'; // write out as: \r" |
| 998 | } else if (ch === '\t') { // a single tab: invisible |
| 999 | escapedString += '\\t'; // write out as: \t" |
| 1000 | } else { |
| 1001 | escapedString += ch; // Else it need not be escaped |
| 1002 | } |
| 1003 | } |
| 1004 | this.tstack.push('"' + escapedString + '"'); |
| 1005 | } |
| 1006 | }, |
| 1007 | |
| 1008 | /** Serializes a string */ |
| 1009 | writeBinary: function(str) { |
| 1010 | this.writeString(str); |
| 1011 | }, |
| 1012 | |
| 1013 | /** |
| 1014 | @class |
| 1015 | @name AnonReadMessageBeginReturn |
| 1016 | @property {string} fname - The name of the service method. |
| 1017 | @property {Thrift.MessageType} mtype - The type of message call. |
| 1018 | @property {number} rseqid - The sequence number of the message (0 in Thrift RPC). |
| 1019 | */ |
| 1020 | /** |
| 1021 | * Deserializes the beginning of a message. |
| 1022 | * @returns {AnonReadMessageBeginReturn} |
| 1023 | */ |
| 1024 | readMessageBegin: function() { |
| 1025 | this.rstack = []; |
| 1026 | this.rpos = []; |
| 1027 | |
Roger Meier | 52744ee | 2014-03-12 09:38:42 +0100 | [diff] [blame] | 1028 | if (typeof JSON !== 'undefined' && typeof JSON.parse === 'function') { |
| 1029 | this.robj = JSON.parse(this.transport.readAll()); |
| 1030 | } else if (typeof jQuery !== 'undefined') { |
Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +0200 | [diff] [blame] | 1031 | this.robj = jQuery.parseJSON(this.transport.readAll()); |
| 1032 | } else { |
| 1033 | this.robj = eval(this.transport.readAll()); |
| 1034 | } |
| 1035 | |
| 1036 | var r = {}; |
| 1037 | var version = this.robj.shift(); |
| 1038 | |
| 1039 | if (version != Thrift.Protocol.Version) { |
| 1040 | throw 'Wrong thrift protocol version: ' + version; |
| 1041 | } |
| 1042 | |
| 1043 | r.fname = this.robj.shift(); |
| 1044 | r.mtype = this.robj.shift(); |
| 1045 | r.rseqid = this.robj.shift(); |
| 1046 | |
| 1047 | |
| 1048 | //get to the main obj |
| 1049 | this.rstack.push(this.robj.shift()); |
| 1050 | |
| 1051 | return r; |
| 1052 | }, |
| 1053 | |
| 1054 | /** Deserializes the end of a message. */ |
| 1055 | readMessageEnd: function() { |
| 1056 | }, |
| 1057 | |
| 1058 | /** |
| 1059 | * Deserializes the beginning of a struct. |
| 1060 | * @param {string} [name] - The name of the struct (ignored) |
| 1061 | * @returns {object} - An object with an empty string fname property |
| 1062 | */ |
| 1063 | readStructBegin: function(name) { |
| 1064 | var r = {}; |
| 1065 | r.fname = ''; |
| 1066 | |
| 1067 | //incase this is an array of structs |
| 1068 | if (this.rstack[this.rstack.length - 1] instanceof Array) { |
| 1069 | this.rstack.push(this.rstack[this.rstack.length - 1].shift()); |
| 1070 | } |
| 1071 | |
| 1072 | return r; |
| 1073 | }, |
| 1074 | |
| 1075 | /** Deserializes the end of a struct. */ |
| 1076 | readStructEnd: function() { |
| 1077 | if (this.rstack[this.rstack.length - 2] instanceof Array) { |
| 1078 | this.rstack.pop(); |
| 1079 | } |
| 1080 | }, |
| 1081 | |
| 1082 | /** |
| 1083 | @class |
| 1084 | @name AnonReadFieldBeginReturn |
| 1085 | @property {string} fname - The name of the field (always ''). |
| 1086 | @property {Thrift.Type} ftype - The data type of the field. |
| 1087 | @property {number} fid - The unique identifier of the field. |
| 1088 | */ |
| 1089 | /** |
| 1090 | * Deserializes the beginning of a field. |
| 1091 | * @returns {AnonReadFieldBeginReturn} |
| 1092 | */ |
| 1093 | readFieldBegin: function() { |
| 1094 | var r = {}; |
| 1095 | |
| 1096 | var fid = -1; |
| 1097 | var ftype = Thrift.Type.STOP; |
| 1098 | |
| 1099 | //get a fieldId |
| 1100 | for (var f in (this.rstack[this.rstack.length - 1])) { |
| 1101 | if (f === null) { |
| 1102 | continue; |
| 1103 | } |
| 1104 | |
| 1105 | fid = parseInt(f, 10); |
| 1106 | this.rpos.push(this.rstack.length); |
| 1107 | |
| 1108 | var field = this.rstack[this.rstack.length - 1][fid]; |
| 1109 | |
| 1110 | //remove so we don't see it again |
| 1111 | delete this.rstack[this.rstack.length - 1][fid]; |
| 1112 | |
| 1113 | this.rstack.push(field); |
| 1114 | |
| 1115 | break; |
| 1116 | } |
| 1117 | |
| 1118 | if (fid != -1) { |
| 1119 | |
| 1120 | //should only be 1 of these but this is the only |
| 1121 | //way to match a key |
| 1122 | for (var i in (this.rstack[this.rstack.length - 1])) { |
| 1123 | if (Thrift.Protocol.RType[i] === null) { |
| 1124 | continue; |
| 1125 | } |
| 1126 | |
| 1127 | ftype = Thrift.Protocol.RType[i]; |
| 1128 | this.rstack[this.rstack.length - 1] = |
| 1129 | this.rstack[this.rstack.length - 1][i]; |
| 1130 | } |
| 1131 | } |
| 1132 | |
| 1133 | r.fname = ''; |
| 1134 | r.ftype = ftype; |
| 1135 | r.fid = fid; |
| 1136 | |
| 1137 | return r; |
| 1138 | }, |
| 1139 | |
| 1140 | /** Deserializes the end of a field. */ |
| 1141 | readFieldEnd: function() { |
| 1142 | var pos = this.rpos.pop(); |
| 1143 | |
| 1144 | //get back to the right place in the stack |
| 1145 | while (this.rstack.length > pos) { |
| 1146 | this.rstack.pop(); |
| 1147 | } |
| 1148 | |
| 1149 | }, |
| 1150 | |
| 1151 | /** |
| 1152 | @class |
| 1153 | @name AnonReadMapBeginReturn |
| 1154 | @property {Thrift.Type} ktype - The data type of the key. |
| 1155 | @property {Thrift.Type} vtype - The data type of the value. |
| 1156 | @property {number} size - The number of elements in the map. |
| 1157 | */ |
| 1158 | /** |
| 1159 | * Deserializes the beginning of a map. |
| 1160 | * @returns {AnonReadMapBeginReturn} |
| 1161 | */ |
| 1162 | readMapBegin: function() { |
| 1163 | var map = this.rstack.pop(); |
Liangliang He | 5d6378f | 2014-08-19 18:25:37 +0800 | [diff] [blame] | 1164 | var first = map.shift(); |
| 1165 | if (first instanceof Array) { |
| 1166 | this.rstack.push(map); |
| 1167 | map = first; |
| 1168 | first = map.shift(); |
| 1169 | } |
Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +0200 | [diff] [blame] | 1170 | |
| 1171 | var r = {}; |
Liangliang He | 5d6378f | 2014-08-19 18:25:37 +0800 | [diff] [blame] | 1172 | r.ktype = Thrift.Protocol.RType[first]; |
Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +0200 | [diff] [blame] | 1173 | r.vtype = Thrift.Protocol.RType[map.shift()]; |
| 1174 | r.size = map.shift(); |
| 1175 | |
| 1176 | |
| 1177 | this.rpos.push(this.rstack.length); |
| 1178 | this.rstack.push(map.shift()); |
| 1179 | |
| 1180 | return r; |
| 1181 | }, |
| 1182 | |
| 1183 | /** Deserializes the end of a map. */ |
| 1184 | readMapEnd: function() { |
| 1185 | this.readFieldEnd(); |
| 1186 | }, |
| 1187 | |
| 1188 | /** |
| 1189 | @class |
| 1190 | @name AnonReadColBeginReturn |
| 1191 | @property {Thrift.Type} etype - The data type of the element. |
| 1192 | @property {number} size - The number of elements in the collection. |
| 1193 | */ |
| 1194 | /** |
| 1195 | * Deserializes the beginning of a list. |
| 1196 | * @returns {AnonReadColBeginReturn} |
| 1197 | */ |
| 1198 | readListBegin: function() { |
| 1199 | var list = this.rstack[this.rstack.length - 1]; |
| 1200 | |
| 1201 | var r = {}; |
| 1202 | r.etype = Thrift.Protocol.RType[list.shift()]; |
| 1203 | r.size = list.shift(); |
| 1204 | |
| 1205 | this.rpos.push(this.rstack.length); |
Henrique Mendonça | c0e4a8d | 2015-06-01 23:23:22 +1000 | [diff] [blame] | 1206 | this.rstack.push(list); |
Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +0200 | [diff] [blame] | 1207 | |
| 1208 | return r; |
| 1209 | }, |
| 1210 | |
| 1211 | /** Deserializes the end of a list. */ |
| 1212 | readListEnd: function() { |
| 1213 | this.readFieldEnd(); |
| 1214 | }, |
| 1215 | |
| 1216 | /** |
| 1217 | * Deserializes the beginning of a set. |
| 1218 | * @returns {AnonReadColBeginReturn} |
| 1219 | */ |
| 1220 | readSetBegin: function(elemType, size) { |
| 1221 | return this.readListBegin(elemType, size); |
| 1222 | }, |
| 1223 | |
| 1224 | /** Deserializes the end of a set. */ |
| 1225 | readSetEnd: function() { |
| 1226 | return this.readListEnd(); |
| 1227 | }, |
| 1228 | |
| 1229 | /** Returns an object with a value property set to |
| 1230 | * False unless the next number in the protocol buffer |
Konrad Grochowski | 3b5dacb | 2014-11-24 10:55:31 +0100 | [diff] [blame] | 1231 | * is 1, in which case the value property is True */ |
Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +0200 | [diff] [blame] | 1232 | readBool: function() { |
| 1233 | var r = this.readI32(); |
| 1234 | |
| 1235 | if (r !== null && r.value == '1') { |
| 1236 | r.value = true; |
| 1237 | } else { |
| 1238 | r.value = false; |
| 1239 | } |
| 1240 | |
| 1241 | return r; |
| 1242 | }, |
| 1243 | |
| 1244 | /** Returns the an object with a value property set to the |
| 1245 | next value found in the protocol buffer */ |
| 1246 | readByte: function() { |
| 1247 | return this.readI32(); |
| 1248 | }, |
| 1249 | |
| 1250 | /** Returns the an object with a value property set to the |
| 1251 | next value found in the protocol buffer */ |
| 1252 | readI16: function() { |
| 1253 | return this.readI32(); |
| 1254 | }, |
| 1255 | |
| 1256 | /** Returns the an object with a value property set to the |
| 1257 | next value found in the protocol buffer */ |
| 1258 | readI32: function(f) { |
| 1259 | if (f === undefined) { |
| 1260 | f = this.rstack[this.rstack.length - 1]; |
| 1261 | } |
| 1262 | |
| 1263 | var r = {}; |
| 1264 | |
| 1265 | if (f instanceof Array) { |
| 1266 | if (f.length === 0) { |
| 1267 | r.value = undefined; |
| 1268 | } else { |
| 1269 | r.value = f.shift(); |
| 1270 | } |
| 1271 | } else if (f instanceof Object) { |
| 1272 | for (var i in f) { |
| 1273 | if (i === null) { |
| 1274 | continue; |
| 1275 | } |
| 1276 | this.rstack.push(f[i]); |
| 1277 | delete f[i]; |
| 1278 | |
| 1279 | r.value = i; |
| 1280 | break; |
| 1281 | } |
| 1282 | } else { |
| 1283 | r.value = f; |
| 1284 | this.rstack.pop(); |
| 1285 | } |
| 1286 | |
| 1287 | return r; |
| 1288 | }, |
| 1289 | |
| 1290 | /** Returns the an object with a value property set to the |
| 1291 | next value found in the protocol buffer */ |
| 1292 | readI64: function() { |
| 1293 | return this.readI32(); |
| 1294 | }, |
| 1295 | |
| 1296 | /** Returns the an object with a value property set to the |
| 1297 | next value found in the protocol buffer */ |
| 1298 | readDouble: function() { |
| 1299 | return this.readI32(); |
| 1300 | }, |
| 1301 | |
| 1302 | /** Returns the an object with a value property set to the |
| 1303 | next value found in the protocol buffer */ |
| 1304 | readString: function() { |
| 1305 | var r = this.readI32(); |
| 1306 | return r; |
| 1307 | }, |
| 1308 | |
| 1309 | /** Returns the an object with a value property set to the |
| 1310 | next value found in the protocol buffer */ |
| 1311 | readBinary: function() { |
| 1312 | return this.readString(); |
| 1313 | }, |
| 1314 | |
| 1315 | /** |
Jens Geyer | 329d59a | 2014-06-19 22:11:53 +0200 | [diff] [blame] | 1316 | * Method to arbitrarily skip over data */ |
Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +0200 | [diff] [blame] | 1317 | skip: function(type) { |
Jens Geyer | 329d59a | 2014-06-19 22:11:53 +0200 | [diff] [blame] | 1318 | var ret, i; |
| 1319 | switch (type) { |
| 1320 | case Thrift.Type.STOP: |
| 1321 | return null; |
| 1322 | |
| 1323 | case Thrift.Type.BOOL: |
| 1324 | return this.readBool(); |
| 1325 | |
| 1326 | case Thrift.Type.BYTE: |
| 1327 | return this.readByte(); |
| 1328 | |
| 1329 | case Thrift.Type.I16: |
| 1330 | return this.readI16(); |
| 1331 | |
| 1332 | case Thrift.Type.I32: |
| 1333 | return this.readI32(); |
| 1334 | |
| 1335 | case Thrift.Type.I64: |
| 1336 | return this.readI64(); |
| 1337 | |
| 1338 | case Thrift.Type.DOUBLE: |
| 1339 | return this.readDouble(); |
| 1340 | |
| 1341 | case Thrift.Type.STRING: |
| 1342 | return this.readString(); |
| 1343 | |
| 1344 | case Thrift.Type.STRUCT: |
| 1345 | this.readStructBegin(); |
| 1346 | while (true) { |
| 1347 | ret = this.readFieldBegin(); |
| 1348 | if (ret.ftype == Thrift.Type.STOP) { |
| 1349 | break; |
| 1350 | } |
| 1351 | this.skip(ret.ftype); |
| 1352 | this.readFieldEnd(); |
| 1353 | } |
| 1354 | this.readStructEnd(); |
| 1355 | return null; |
| 1356 | |
| 1357 | case Thrift.Type.MAP: |
| 1358 | ret = this.readMapBegin(); |
| 1359 | for (i = 0; i < ret.size; i++) { |
| 1360 | if (i > 0) { |
| 1361 | if (this.rstack.length > this.rpos[this.rpos.length - 1] + 1) { |
| 1362 | this.rstack.pop(); |
| 1363 | } |
| 1364 | } |
| 1365 | this.skip(ret.ktype); |
| 1366 | this.skip(ret.vtype); |
| 1367 | } |
| 1368 | this.readMapEnd(); |
| 1369 | return null; |
| 1370 | |
| 1371 | case Thrift.Type.SET: |
| 1372 | ret = this.readSetBegin(); |
| 1373 | for (i = 0; i < ret.size; i++) { |
| 1374 | this.skip(ret.etype); |
| 1375 | } |
| 1376 | this.readSetEnd(); |
| 1377 | return null; |
| 1378 | |
| 1379 | case Thrift.Type.LIST: |
| 1380 | ret = this.readListBegin(); |
| 1381 | for (i = 0; i < ret.size; i++) { |
| 1382 | this.skip(ret.etype); |
| 1383 | } |
| 1384 | this.readListEnd(); |
| 1385 | return null; |
| 1386 | } |
Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +0200 | [diff] [blame] | 1387 | } |
| 1388 | }; |
henrique | 5ba91f2 | 2013-12-20 21:13:13 +0100 | [diff] [blame] | 1389 | |
| 1390 | |
| 1391 | /** |
| 1392 | * Initializes a MutilplexProtocol Implementation as a Wrapper for Thrift.Protocol |
| 1393 | * @constructor |
| 1394 | */ |
| 1395 | Thrift.MultiplexProtocol = function (srvName, trans, strictRead, strictWrite) { |
| 1396 | Thrift.Protocol.call(this, trans, strictRead, strictWrite); |
| 1397 | this.serviceName = srvName; |
| 1398 | }; |
| 1399 | Thrift.inherits(Thrift.MultiplexProtocol, Thrift.Protocol, 'multiplexProtocol'); |
| 1400 | |
| 1401 | /** Override writeMessageBegin method of prototype*/ |
| 1402 | Thrift.MultiplexProtocol.prototype.writeMessageBegin = function (name, type, seqid) { |
| 1403 | |
| 1404 | if (type === Thrift.MessageType.CALL || type === Thrift.MessageType.ONEWAY) { |
| 1405 | Thrift.Protocol.prototype.writeMessageBegin.call(this, this.serviceName + ":" + name, type, seqid); |
| 1406 | } else { |
| 1407 | Thrift.Protocol.prototype.writeMessageBegin.call(this, name, type, seqid); |
| 1408 | } |
| 1409 | }; |
| 1410 | |
| 1411 | Thrift.Multiplexer = function () { |
| 1412 | this.seqid = 0; |
| 1413 | }; |
| 1414 | |
| 1415 | /** Instantiates a multiplexed client for a specific service |
| 1416 | * @constructor |
| 1417 | * @param {String} serviceName - The transport to serialize to/from. |
| 1418 | * @param {Thrift.ServiceClient} SCl - The Service Client Class |
| 1419 | * @param {Thrift.Transport} transport - Thrift.Transport instance which provides remote host:port |
| 1420 | * @example |
| 1421 | * var mp = new Thrift.Multiplexer(); |
| 1422 | * var transport = new Thrift.Transport("http://localhost:9090/foo.thrift"); |
| 1423 | * var protocol = new Thrift.Protocol(transport); |
| 1424 | * var client = mp.createClient('AuthService', AuthServiceClient, transport); |
| 1425 | */ |
| 1426 | Thrift.Multiplexer.prototype.createClient = function (serviceName, SCl, transport) { |
| 1427 | if (SCl.Client) { |
| 1428 | SCl = SCl.Client; |
| 1429 | } |
| 1430 | var self = this; |
| 1431 | SCl.prototype.new_seqid = function () { |
| 1432 | self.seqid += 1; |
| 1433 | return self.seqid; |
| 1434 | }; |
| 1435 | var client = new SCl(new Thrift.MultiplexProtocol(serviceName, transport)); |
| 1436 | |
| 1437 | return client; |
| 1438 | }; |
| 1439 | |
henrique | a2de410 | 2014-02-07 14:12:56 +0100 | [diff] [blame] | 1440 | |
henrique | 2a7dccc | 2014-03-07 22:16:51 +0100 | [diff] [blame] | 1441 | |