T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [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 | var Thrift = { |
Roger Meier | 0244e93 | 2011-03-09 15:25:01 +0000 | [diff] [blame] | 20 | Version: '0.7.0-dev', |
| 21 | /* |
| 22 | Description: 'JavaScript bindings for the Apache Thrift RPC system', |
| 23 | License: 'http://www.apache.org/licenses/LICENSE-2.0', |
| 24 | Homepage: 'http://thrift.apache.org', |
| 25 | BugReports: 'https://issues.apache.org/jira/browse/THRIFT', |
| 26 | Maintainer: 'dev@thrift.apache.org', |
| 27 | */ |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 28 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 29 | Type: { |
| 30 | 'STOP' : 0, |
| 31 | 'VOID' : 1, |
| 32 | 'BOOL' : 2, |
| 33 | 'BYTE' : 3, |
| 34 | 'I08' : 3, |
| 35 | 'DOUBLE' : 4, |
| 36 | 'I16' : 6, |
| 37 | 'I32' : 8, |
| 38 | 'I64' : 10, |
| 39 | 'STRING' : 11, |
| 40 | 'UTF7' : 11, |
| 41 | 'STRUCT' : 12, |
| 42 | 'MAP' : 13, |
| 43 | 'SET' : 14, |
| 44 | 'LIST' : 15, |
| 45 | 'UTF8' : 16, |
| 46 | 'UTF16' : 17 |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 47 | }, |
| 48 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 49 | MessageType: { |
| 50 | 'CALL' : 1, |
| 51 | 'REPLY' : 2, |
| 52 | 'EXCEPTION' : 3 |
Roger Meier | 55ea68f | 2011-02-16 19:29:50 +0000 | [diff] [blame] | 53 | }, |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 54 | |
| 55 | objectLength: function(obj) { |
| 56 | var length = 0; |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 57 | for (var k in obj) { |
| 58 | if (obj.hasOwnProperty(k)) { |
Roger Meier | 08b3099 | 2011-04-06 21:30:53 +0000 | [diff] [blame] | 59 | length++; |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 63 | return length; |
Roger Meier | 55ea68f | 2011-02-16 19:29:50 +0000 | [diff] [blame] | 64 | }, |
| 65 | |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 66 | inherits: function(constructor, superConstructor) { |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 67 | //Prototypal Inheritance http://javascript.crockford.com/prototypal.html |
| 68 | function F() {} |
| 69 | F.prototype = superConstructor.prototype; |
| 70 | constructor.prototype = new F(); |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 71 | } |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 72 | }; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 73 | |
Roger Meier | 55ea68f | 2011-02-16 19:29:50 +0000 | [diff] [blame] | 74 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 75 | Thrift.TException = {}; |
| 76 | Thrift.TException.prototype = { |
| 77 | initialize: function(message, code) { |
Roger Meier | 08b3099 | 2011-04-06 21:30:53 +0000 | [diff] [blame] | 78 | this.message = message; |
| 79 | this.code = (code === null) ? 0 : code; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 80 | } |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 81 | }; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 82 | |
| 83 | |
T Jake Luciani | efabb89 | 2010-07-28 22:31:12 +0000 | [diff] [blame] | 84 | Thrift.TApplicationExceptionType = { |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 85 | 'UNKNOWN' : 0, |
| 86 | 'UNKNOWN_METHOD' : 1, |
| 87 | 'INVALID_MESSAGE_TYPE' : 2, |
| 88 | 'WRONG_METHOD_NAME' : 3, |
| 89 | 'BAD_SEQUENCE_ID' : 4, |
| 90 | 'MISSING_RESULT' : 5 |
| 91 | }; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 92 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 93 | Thrift.TApplicationException = function(message, code) { |
| 94 | this.message = message; |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 95 | this.code = (code === null) ? 0 : code; |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 96 | }; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 97 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 98 | Thrift.TApplicationException.prototype = { |
| 99 | |
| 100 | read: function(input) { |
| 101 | while (1) { |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 102 | var ret = input.readFieldBegin(); |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 103 | |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 104 | if (ret.ftype == Thrift.Type.STOP) { |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 105 | break; |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 106 | } |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 107 | |
| 108 | var fid = ret.fid; |
| 109 | |
| 110 | switch (fid) { |
| 111 | case 1: |
| 112 | if (ret.ftype == Thrift.Type.STRING) { |
| 113 | ret = input.readString(); |
| 114 | this.message = ret.value; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 115 | } else { |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 116 | ret = input.skip(ret.ftype); |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 117 | } |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 118 | |
| 119 | break; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 120 | case 2: |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 121 | if (ret.ftype == Thrift.Type.I32) { |
| 122 | ret = input.readI32(); |
| 123 | this.code = ret.value; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 124 | } else { |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 125 | ret = input.skip(ret.ftype); |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 126 | } |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 127 | break; |
| 128 | |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 129 | default: |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 130 | ret = input.skip(ret.ftype); |
| 131 | break; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 132 | } |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 133 | |
| 134 | input.readFieldEnd(); |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 135 | } |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 136 | |
| 137 | input.readStructEnd(); |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 138 | }, |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 139 | |
| 140 | write: function(output) { |
| 141 | var xfer = 0; |
| 142 | |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 143 | output.writeStructBegin('TApplicationException'); |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 144 | |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 145 | if (this.message) { |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 146 | output.writeFieldBegin('message', Thrift.Type.STRING, 1); |
| 147 | output.writeString(this.getMessage()); |
| 148 | output.writeFieldEnd(); |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 149 | } |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 150 | |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 151 | if (this.code) { |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 152 | output.writeFieldBegin('type', Thrift.Type.I32, 2); |
| 153 | output.writeI32(this.code); |
| 154 | output.writeFieldEnd(); |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 155 | } |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 156 | |
| 157 | output.writeFieldStop(); |
| 158 | output.writeStructEnd(); |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 159 | }, |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 160 | |
| 161 | getCode: function() { |
| 162 | return this.code; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 163 | }, |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 164 | |
| 165 | getMessage: function() { |
| 166 | return this.message; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 167 | } |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 168 | }; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 169 | |
| 170 | |
| 171 | |
| 172 | /** |
| 173 | *If you do not specify a url then you must handle ajax on your own. |
| 174 | *This is how to use js bindings in a async fashion. |
| 175 | */ |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 176 | Thrift.Transport = function(url) { |
| 177 | this.url = url; |
| 178 | this.wpos = 0; |
| 179 | this.rpos = 0; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 180 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 181 | this.send_buf = ''; |
| 182 | this.recv_buf = ''; |
| 183 | }; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 184 | |
| 185 | Thrift.Transport.prototype = { |
| 186 | |
| 187 | //Gets the browser specific XmlHttpRequest Object |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 188 | getXmlHttpRequestObject: function() { |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 189 | try { return new XMLHttpRequest(); } catch (e1) { } |
| 190 | try { return new ActiveXObject('Msxml2.XMLHTTP'); } catch (e2) { } |
| 191 | try { return new ActiveXObject('Microsoft.XMLHTTP'); } catch (e3) { } |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 192 | |
| 193 | throw "Your browser doesn't support the XmlHttpRequest object."; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 194 | }, |
| 195 | |
Roger Meier | 08b3099 | 2011-04-06 21:30:53 +0000 | [diff] [blame] | 196 | flush: function(async) { |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 197 | //async mode |
Roger Meier | 08b3099 | 2011-04-06 21:30:53 +0000 | [diff] [blame] | 198 | if (async || this.url === undefined || this.url === '') { |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 199 | return this.send_buf; |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 200 | } |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 201 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 202 | var xreq = this.getXmlHttpRequestObject(); |
| 203 | |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 204 | if (xreq.overrideMimeType) { |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 205 | xreq.overrideMimeType('application/json'); |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 206 | } |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 207 | |
| 208 | xreq.open('POST', this.url, false); |
| 209 | xreq.send(this.send_buf); |
| 210 | |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 211 | if (xreq.readyState != 4) { |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 212 | throw 'encountered an unknown ajax ready state: ' + xreq.readyState; |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 213 | } |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 214 | |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 215 | if (xreq.status != 200) { |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 216 | throw 'encountered a unknown request status: ' + xreq.status; |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 217 | } |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 218 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 219 | this.recv_buf = xreq.responseText; |
| 220 | this.recv_buf_sz = this.recv_buf.length; |
| 221 | this.wpos = this.recv_buf.length; |
| 222 | this.rpos = 0; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 223 | }, |
| 224 | |
Roger Meier | 08b3099 | 2011-04-06 21:30:53 +0000 | [diff] [blame] | 225 | jqRequest: function(client, postData, args, recv_method) { |
| 226 | if (typeof jQuery === 'undefined' || |
| 227 | typeof jQuery.Deferred === 'undefined') { |
| 228 | throw 'Thrift.js requires jQuery 1.5+ to use asynchronous requests'; |
| 229 | } |
| 230 | |
| 231 | // Deferreds |
| 232 | var deferred = jQuery.Deferred(); |
| 233 | var completeDfd = jQuery._Deferred(); |
| 234 | var dfd = deferred.promise(); |
| 235 | dfd.success = dfd.done; |
| 236 | dfd.error = dfd.fail; |
| 237 | dfd.complete = completeDfd.done; |
| 238 | |
| 239 | var jqXHR = jQuery.ajax({ |
| 240 | url: this.url, |
| 241 | data: postData, |
| 242 | type: 'POST', |
| 243 | cache: false, |
| 244 | dataType: 'text', |
| 245 | context: this, |
| 246 | success: this.jqResponse, |
| 247 | error: function(xhr, status, e) { |
| 248 | deferred.rejectWith(client, jQuery.merge([e], xhr.tArgs)); |
| 249 | }, |
| 250 | complete: function(xhr, status) { |
| 251 | completeDfd.resolveWith(client, [xhr, status]); |
| 252 | } |
| 253 | }); |
| 254 | |
| 255 | deferred.done(jQuery.makeArray(args).pop()); //pop callback from args |
| 256 | jqXHR.tArgs = args; |
| 257 | jqXHR.tClient = client; |
| 258 | jqXHR.tRecvFn = recv_method; |
| 259 | jqXHR.tDfd = deferred; |
| 260 | return dfd; |
| 261 | }, |
| 262 | |
| 263 | jqResponse: function(responseData, textStatus, jqXHR) { |
| 264 | this.setRecvBuffer(responseData); |
| 265 | try { |
| 266 | var value = jqXHR.tRecvFn.call(jqXHR.tClient); |
| 267 | jqXHR.tDfd.resolveWith(jqXHR, jQuery.merge([value], jqXHR.tArgs)); |
| 268 | } catch (ex) { |
| 269 | jqXHR.tDfd.rejectWith(jqXHR, jQuery.merge([ex], jqXHR.tArgs)); |
| 270 | } |
| 271 | }, |
| 272 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 273 | setRecvBuffer: function(buf) { |
| 274 | this.recv_buf = buf; |
| 275 | this.recv_buf_sz = this.recv_buf.length; |
| 276 | this.wpos = this.recv_buf.length; |
| 277 | this.rpos = 0; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 278 | }, |
| 279 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 280 | isOpen: function() { |
| 281 | return true; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 282 | }, |
| 283 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 284 | open: function() {}, |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 285 | |
| 286 | close: function() {}, |
| 287 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 288 | read: function(len) { |
| 289 | var avail = this.wpos - this.rpos; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 290 | |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 291 | if (avail === 0) { |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 292 | return ''; |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 293 | } |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 294 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 295 | var give = len; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 296 | |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 297 | if (avail < len) { |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 298 | give = avail; |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 299 | } |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 300 | |
| 301 | var ret = this.read_buf.substr(this.rpos, give); |
| 302 | this.rpos += give; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 303 | |
| 304 | //clear buf when complete? |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 305 | return ret; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 306 | }, |
| 307 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 308 | readAll: function() { |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 309 | return this.recv_buf; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 310 | }, |
| 311 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 312 | write: function(buf) { |
| 313 | this.send_buf = buf; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 314 | }, |
| 315 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 316 | getSendBuffer: function() { |
| 317 | return this.send_buf; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 318 | } |
| 319 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 320 | }; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 321 | |
| 322 | |
| 323 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 324 | Thrift.Protocol = function(transport) { |
| 325 | this.transport = transport; |
| 326 | }; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 327 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 328 | Thrift.Protocol.Type = {}; |
| 329 | Thrift.Protocol.Type[Thrift.Type.BOOL] = '"tf"'; |
| 330 | Thrift.Protocol.Type[Thrift.Type.BYTE] = '"i8"'; |
| 331 | Thrift.Protocol.Type[Thrift.Type.I16] = '"i16"'; |
| 332 | Thrift.Protocol.Type[Thrift.Type.I32] = '"i32"'; |
| 333 | Thrift.Protocol.Type[Thrift.Type.I64] = '"i64"'; |
| 334 | Thrift.Protocol.Type[Thrift.Type.DOUBLE] = '"dbl"'; |
| 335 | Thrift.Protocol.Type[Thrift.Type.STRUCT] = '"rec"'; |
| 336 | Thrift.Protocol.Type[Thrift.Type.STRING] = '"str"'; |
| 337 | Thrift.Protocol.Type[Thrift.Type.MAP] = '"map"'; |
| 338 | Thrift.Protocol.Type[Thrift.Type.LIST] = '"lst"'; |
| 339 | Thrift.Protocol.Type[Thrift.Type.SET] = '"set"'; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 340 | |
| 341 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 342 | Thrift.Protocol.RType = {}; |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 343 | Thrift.Protocol.RType.tf = Thrift.Type.BOOL; |
| 344 | Thrift.Protocol.RType.i8 = Thrift.Type.BYTE; |
| 345 | Thrift.Protocol.RType.i16 = Thrift.Type.I16; |
| 346 | Thrift.Protocol.RType.i32 = Thrift.Type.I32; |
| 347 | Thrift.Protocol.RType.i64 = Thrift.Type.I64; |
| 348 | Thrift.Protocol.RType.dbl = Thrift.Type.DOUBLE; |
| 349 | Thrift.Protocol.RType.rec = Thrift.Type.STRUCT; |
| 350 | Thrift.Protocol.RType.str = Thrift.Type.STRING; |
| 351 | Thrift.Protocol.RType.map = Thrift.Type.MAP; |
| 352 | Thrift.Protocol.RType.lst = Thrift.Type.LIST; |
| 353 | Thrift.Protocol.RType.set = Thrift.Type.SET; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 354 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 355 | Thrift.Protocol.Version = 1; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 356 | |
| 357 | Thrift.Protocol.prototype = { |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 358 | |
| 359 | getTransport: function() { |
| 360 | return this.transport; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 361 | }, |
| 362 | |
| 363 | //Write functions |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 364 | writeMessageBegin: function(name, messageType, seqid) { |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 365 | this.tstack = []; |
| 366 | this.tpos = []; |
| 367 | |
| 368 | this.tstack.push([Thrift.Protocol.Version, '"' + |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 369 | name + '"', messageType, seqid]); |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 370 | }, |
| 371 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 372 | writeMessageEnd: function() { |
| 373 | var obj = this.tstack.pop(); |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 374 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 375 | this.wobj = this.tstack.pop(); |
| 376 | this.wobj.push(obj); |
| 377 | |
| 378 | this.wbuf = '[' + this.wobj.join(',') + ']'; |
| 379 | |
| 380 | this.transport.write(this.wbuf); |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 381 | }, |
| 382 | |
| 383 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 384 | writeStructBegin: function(name) { |
| 385 | this.tpos.push(this.tstack.length); |
| 386 | this.tstack.push({}); |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 387 | }, |
| 388 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 389 | writeStructEnd: function() { |
| 390 | |
| 391 | var p = this.tpos.pop(); |
| 392 | var struct = this.tstack[p]; |
| 393 | var str = '{'; |
| 394 | var first = true; |
| 395 | for (var key in struct) { |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 396 | if (first) { |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 397 | first = false; |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 398 | } else { |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 399 | str += ','; |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 400 | } |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 401 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 402 | str += key + ':' + struct[key]; |
| 403 | } |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 404 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 405 | str += '}'; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 406 | this.tstack[p] = str; |
| 407 | }, |
| 408 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 409 | writeFieldBegin: function(name, fieldType, fieldId) { |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 410 | this.tpos.push(this.tstack.length); |
| 411 | this.tstack.push({ 'fieldId': '"' + |
| 412 | fieldId + '"', 'fieldType': Thrift.Protocol.Type[fieldType] |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 413 | }); |
| 414 | |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 415 | }, |
| 416 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 417 | writeFieldEnd: function() { |
| 418 | var value = this.tstack.pop(); |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 419 | var fieldInfo = this.tstack.pop(); |
| 420 | |
| 421 | this.tstack[this.tstack.length - 1][fieldInfo.fieldId] = '{' + |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 422 | fieldInfo.fieldType + ':' + value + '}'; |
| 423 | this.tpos.pop(); |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 424 | }, |
| 425 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 426 | writeFieldStop: function() { |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 427 | //na |
| 428 | }, |
| 429 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 430 | writeMapBegin: function(keyType, valType, size) { |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 431 | //size is invalid, we'll set it on end. |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 432 | this.tpos.push(this.tstack.length); |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 433 | this.tstack.push([Thrift.Protocol.Type[keyType], |
| 434 | Thrift.Protocol.Type[valType], 0]); |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 435 | }, |
| 436 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 437 | writeMapEnd: function() { |
| 438 | var p = this.tpos.pop(); |
| 439 | |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 440 | if (p == this.tstack.length) { |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 441 | return; |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 442 | } |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 443 | |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 444 | if ((this.tstack.length - p - 1) % 2 !== 0) { |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 445 | this.tstack.push(''); |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 446 | } |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 447 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 448 | var size = (this.tstack.length - p - 1) / 2; |
| 449 | |
| 450 | this.tstack[p][this.tstack[p].length - 1] = size; |
| 451 | |
| 452 | var map = '}'; |
| 453 | var first = true; |
| 454 | while (this.tstack.length > p + 1) { |
| 455 | var v = this.tstack.pop(); |
| 456 | var k = this.tstack.pop(); |
| 457 | if (first) { |
| 458 | first = false; |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 459 | } else { |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 460 | map = ',' + map; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 461 | } |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 462 | |
| 463 | map = '"' + k + '":' + v + map; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 464 | } |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 465 | map = '{' + map; |
| 466 | |
| 467 | this.tstack[p].push(map); |
| 468 | this.tstack[p] = '[' + this.tstack[p].join(',') + ']'; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 469 | }, |
| 470 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 471 | writeListBegin: function(elemType, size) { |
| 472 | this.tpos.push(this.tstack.length); |
| 473 | this.tstack.push([Thrift.Protocol.Type[elemType], size]); |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 474 | }, |
| 475 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 476 | writeListEnd: function() { |
| 477 | var p = this.tpos.pop(); |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 478 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 479 | while (this.tstack.length > p + 1) { |
| 480 | var tmpVal = this.tstack[p + 1]; |
| 481 | this.tstack.splice(p + 1, 1); |
| 482 | this.tstack[p].push(tmpVal); |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 483 | } |
| 484 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 485 | this.tstack[p] = '[' + this.tstack[p].join(',') + ']'; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 486 | }, |
| 487 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 488 | writeSetBegin: function(elemType, size) { |
| 489 | this.tpos.push(this.tstack.length); |
| 490 | this.tstack.push([Thrift.Protocol.Type[elemType], size]); |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 491 | }, |
| 492 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 493 | writeSetEnd: function() { |
| 494 | var p = this.tpos.pop(); |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 495 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 496 | while (this.tstack.length > p + 1) { |
| 497 | var tmpVal = this.tstack[p + 1]; |
| 498 | this.tstack.splice(p + 1, 1); |
| 499 | this.tstack[p].push(tmpVal); |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 500 | } |
| 501 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 502 | this.tstack[p] = '[' + this.tstack[p].join(',') + ']'; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 503 | }, |
| 504 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 505 | writeBool: function(value) { |
| 506 | this.tstack.push(value ? 1 : 0); |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 507 | }, |
| 508 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 509 | writeByte: function(i8) { |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 510 | this.tstack.push(i8); |
| 511 | }, |
| 512 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 513 | writeI16: function(i16) { |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 514 | this.tstack.push(i16); |
| 515 | }, |
| 516 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 517 | writeI32: function(i32) { |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 518 | this.tstack.push(i32); |
| 519 | }, |
| 520 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 521 | writeI64: function(i64) { |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 522 | this.tstack.push(i64); |
| 523 | }, |
| 524 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 525 | writeDouble: function(dbl) { |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 526 | this.tstack.push(dbl); |
| 527 | }, |
| 528 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 529 | writeString: function(str) { |
| 530 | // We do not encode uri components for wire transfer: |
| 531 | if (str === null) { |
T Jake Luciani | 416eea9 | 2010-09-17 23:38:25 +0000 | [diff] [blame] | 532 | this.tstack.push(null); |
| 533 | } else { |
| 534 | // concat may be slower than building a byte buffer |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 535 | var escapedString = ''; |
| 536 | for (var i = 0; i < str.length; i++) { |
| 537 | var ch = str.charAt(i); // a single double quote: " |
| 538 | if (ch === '\"') { |
| 539 | escapedString += '\\\"'; // write out as: \" |
| 540 | } else if (ch === '\\') { // a single backslash: \ |
| 541 | escapedString += '\\\\'; // write out as: \\ |
T Jake Luciani | 416eea9 | 2010-09-17 23:38:25 +0000 | [diff] [blame] | 542 | /* Currently escaped forward slashes break TJSONProtocol. |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 543 | * As it stands, we can simply pass forward slashes into |
| 544 | * our strings across the wire without being escaped. |
T Jake Luciani | 416eea9 | 2010-09-17 23:38:25 +0000 | [diff] [blame] | 545 | * I think this is the protocol's bug, not thrift.js |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 546 | * } else if(ch === '/') { // a single forward slash: / |
| 547 | * escapedString += '\\/'; // write out as \/ |
| 548 | * } |
T Jake Luciani | 416eea9 | 2010-09-17 23:38:25 +0000 | [diff] [blame] | 549 | */ |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 550 | } else if (ch === '\b') { // a single backspace: invisible |
| 551 | escapedString += '\\b'; // write out as: \b" |
| 552 | } else if (ch === '\f') { // a single formfeed: invisible |
| 553 | escapedString += '\\f'; // write out as: \f" |
| 554 | } else if (ch === '\n') { // a single newline: invisible |
| 555 | escapedString += '\\n'; // write out as: \n" |
| 556 | } else if (ch === '\r') { // a single return: invisible |
| 557 | escapedString += '\\r'; // write out as: \r" |
| 558 | } else if (ch === '\t') { // a single tab: invisible |
| 559 | escapedString += '\\t'; // write out as: \t" |
T Jake Luciani | 416eea9 | 2010-09-17 23:38:25 +0000 | [diff] [blame] | 560 | } else { |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 561 | escapedString += ch; // Else it need not be escaped |
T Jake Luciani | 416eea9 | 2010-09-17 23:38:25 +0000 | [diff] [blame] | 562 | } |
| 563 | } |
| 564 | this.tstack.push('"' + escapedString + '"'); |
| 565 | } |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 566 | }, |
| 567 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 568 | writeBinary: function(str) { |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 569 | this.writeString(str); |
| 570 | }, |
| 571 | |
| 572 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 573 | |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 574 | // Reading functions |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 575 | readMessageBegin: function(name, messageType, seqid) { |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 576 | this.rstack = []; |
| 577 | this.rpos = []; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 578 | |
Roger Meier | 08b3099 | 2011-04-06 21:30:53 +0000 | [diff] [blame] | 579 | if (typeof jQuery !== 'undefined') { |
| 580 | this.robj = jQuery.parseJSON(this.transport.readAll()); |
| 581 | } else { |
| 582 | this.robj = eval(this.transport.readAll()); |
| 583 | } |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 584 | |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 585 | var r = {}; |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 586 | var version = this.robj.shift(); |
| 587 | |
| 588 | if (version != Thrift.Protocol.Version) { |
| 589 | throw 'Wrong thrift protocol version: ' + version; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 590 | } |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 591 | |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 592 | r.fname = this.robj.shift(); |
| 593 | r.mtype = this.robj.shift(); |
| 594 | r.rseqid = this.robj.shift(); |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 595 | |
| 596 | |
| 597 | //get to the main obj |
| 598 | this.rstack.push(this.robj.shift()); |
| 599 | |
| 600 | return r; |
| 601 | }, |
| 602 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 603 | readMessageEnd: function() { |
| 604 | }, |
| 605 | |
| 606 | readStructBegin: function(name) { |
| 607 | var r = {}; |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 608 | r.fname = ''; |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 609 | |
| 610 | //incase this is an array of structs |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 611 | if (this.rstack[this.rstack.length - 1] instanceof Array) { |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 612 | this.rstack.push(this.rstack[this.rstack.length - 1].shift()); |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 613 | } |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 614 | |
| 615 | return r; |
| 616 | }, |
| 617 | |
| 618 | readStructEnd: function() { |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 619 | if (this.rstack[this.rstack.length - 2] instanceof Array) { |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 620 | this.rstack.pop(); |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 621 | } |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 622 | }, |
| 623 | |
| 624 | readFieldBegin: function() { |
| 625 | var r = {}; |
| 626 | |
| 627 | var fid = -1; |
| 628 | var ftype = Thrift.Type.STOP; |
| 629 | |
| 630 | //get a fieldId |
| 631 | for (var f in (this.rstack[this.rstack.length - 1])) { |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 632 | if (f === null) { |
| 633 | continue; |
| 634 | } |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 635 | |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 636 | fid = parseInt(f, 10); |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 637 | this.rpos.push(this.rstack.length); |
| 638 | |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 639 | var field = this.rstack[this.rstack.length - 1][fid]; |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 640 | |
| 641 | //remove so we don't see it again |
| 642 | delete this.rstack[this.rstack.length - 1][fid]; |
| 643 | |
| 644 | this.rstack.push(field); |
| 645 | |
| 646 | break; |
| 647 | } |
| 648 | |
| 649 | if (fid != -1) { |
| 650 | |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 651 | //should only be 1 of these but this is the only |
| 652 | //way to match a key |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 653 | for (var i in (this.rstack[this.rstack.length - 1])) { |
| 654 | if (Thrift.Protocol.RType[i] === null) { |
| 655 | continue; |
| 656 | } |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 657 | |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 658 | ftype = Thrift.Protocol.RType[i]; |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 659 | this.rstack[this.rstack.length - 1] = |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 660 | this.rstack[this.rstack.length - 1][i]; |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 661 | } |
| 662 | } |
| 663 | |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 664 | r.fname = ''; |
| 665 | r.ftype = ftype; |
| 666 | r.fid = fid; |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 667 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 668 | return r; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 669 | }, |
| 670 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 671 | readFieldEnd: function() { |
| 672 | var pos = this.rpos.pop(); |
| 673 | |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 674 | //get back to the right place in the stack |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 675 | while (this.rstack.length > pos) { |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 676 | this.rstack.pop(); |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 677 | } |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 678 | |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 679 | }, |
| 680 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 681 | readMapBegin: function(keyType, valType, size) { |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 682 | var map = this.rstack.pop(); |
| 683 | |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 684 | var r = {}; |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 685 | r.ktype = Thrift.Protocol.RType[map.shift()]; |
| 686 | r.vtype = Thrift.Protocol.RType[map.shift()]; |
| 687 | r.size = map.shift(); |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 688 | |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 689 | |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 690 | this.rpos.push(this.rstack.length); |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 691 | this.rstack.push(map.shift()); |
| 692 | |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 693 | return r; |
| 694 | }, |
| 695 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 696 | readMapEnd: function() { |
| 697 | this.readFieldEnd(); |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 698 | }, |
| 699 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 700 | readListBegin: function(elemType, size) { |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 701 | var list = this.rstack[this.rstack.length - 1]; |
| 702 | |
| 703 | var r = {}; |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 704 | r.etype = Thrift.Protocol.RType[list.shift()]; |
| 705 | r.size = list.shift(); |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 706 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 707 | this.rpos.push(this.rstack.length); |
| 708 | this.rstack.push(list); |
| 709 | |
| 710 | return r; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 711 | }, |
| 712 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 713 | readListEnd: function() { |
| 714 | this.readFieldEnd(); |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 715 | }, |
| 716 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 717 | readSetBegin: function(elemType, size) { |
| 718 | return this.readListBegin(elemType, size); |
| 719 | }, |
| 720 | |
| 721 | readSetEnd: function() { |
| 722 | return this.readListEnd(); |
| 723 | }, |
| 724 | |
| 725 | readBool: function() { |
| 726 | var r = this.readI32(); |
| 727 | |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 728 | if (r !== null && r.value == '1') { |
| 729 | r.value = true; |
| 730 | } else { |
| 731 | r.value = false; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 732 | } |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 733 | |
| 734 | return r; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 735 | }, |
| 736 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 737 | readByte: function() { |
| 738 | return this.readI32(); |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 739 | }, |
| 740 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 741 | readI16: function() { |
| 742 | return this.readI32(); |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 743 | }, |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 744 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 745 | readI32: function(f) { |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 746 | if (f === undefined) { |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 747 | f = this.rstack[this.rstack.length - 1]; |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 748 | } |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 749 | |
| 750 | var r = {}; |
| 751 | |
| 752 | if (f instanceof Array) { |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 753 | if (f.length === 0) { |
| 754 | r.value = undefined; |
| 755 | } else { |
| 756 | r.value = f.shift(); |
| 757 | } |
| 758 | } else if (f instanceof Object) { |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 759 | for (var i in f) { |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 760 | if (i === null) { |
| 761 | continue; |
| 762 | } |
| 763 | this.rstack.push(f[i]); |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 764 | delete f[i]; |
| 765 | |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 766 | r.value = i; |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 767 | break; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 768 | } |
| 769 | } else { |
Roger Meier | da6e6ae | 2011-03-15 09:55:33 +0000 | [diff] [blame] | 770 | r.value = f; |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 771 | this.rstack.pop(); |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 772 | } |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 773 | |
| 774 | return r; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 775 | }, |
| 776 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 777 | readI64: function() { |
| 778 | return this.readI32(); |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 779 | }, |
| 780 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 781 | readDouble: function() { |
| 782 | return this.readI32(); |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 783 | }, |
| 784 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 785 | readString: function() { |
| 786 | var r = this.readI32(); |
| 787 | return r; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 788 | }, |
| 789 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 790 | readBinary: function() { |
| 791 | return this.readString(); |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 792 | }, |
| 793 | |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 794 | |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 795 | //Method to arbitrarily skip over data. |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 796 | skip: function(type) { |
| 797 | throw 'skip not supported yet'; |
T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 798 | } |
Roger Meier | b4bcbe3 | 2011-03-07 19:37:46 +0000 | [diff] [blame] | 799 | }; |