blob: 8b1733e3bfe3141b9e02fcdb0ade70f3fadfcd4d [file] [log] [blame]
T Jake Luciani322caa22010-02-15 03:24:55 +00001/*
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 */
19var Thrift = {
Roger Meier0244e932011-03-09 15:25:01 +000020 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 Luciani322caa22010-02-15 03:24:55 +000028
Roger Meierb4bcbe32011-03-07 19:37:46 +000029 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 Luciani322caa22010-02-15 03:24:55 +000047 },
48
Roger Meierb4bcbe32011-03-07 19:37:46 +000049 MessageType: {
50 'CALL' : 1,
51 'REPLY' : 2,
52 'EXCEPTION' : 3
Roger Meier55ea68f2011-02-16 19:29:50 +000053 },
Roger Meierb4bcbe32011-03-07 19:37:46 +000054
55 objectLength: function(obj) {
56 var length = 0;
Roger Meierda6e6ae2011-03-15 09:55:33 +000057 for (var k in obj) {
58 if (obj.hasOwnProperty(k)) {
Roger Meier08b30992011-04-06 21:30:53 +000059 length++;
Roger Meierda6e6ae2011-03-15 09:55:33 +000060 }
61 }
62
Roger Meierb4bcbe32011-03-07 19:37:46 +000063 return length;
Roger Meier55ea68f2011-02-16 19:29:50 +000064 },
65
Roger Meierda6e6ae2011-03-15 09:55:33 +000066 inherits: function(constructor, superConstructor) {
Roger Meierb4bcbe32011-03-07 19:37:46 +000067 //Prototypal Inheritance http://javascript.crockford.com/prototypal.html
68 function F() {}
69 F.prototype = superConstructor.prototype;
70 constructor.prototype = new F();
T Jake Luciani322caa22010-02-15 03:24:55 +000071 }
Roger Meierb4bcbe32011-03-07 19:37:46 +000072};
T Jake Luciani322caa22010-02-15 03:24:55 +000073
Roger Meier55ea68f2011-02-16 19:29:50 +000074
Roger Meierb4bcbe32011-03-07 19:37:46 +000075Thrift.TException = {};
76Thrift.TException.prototype = {
77 initialize: function(message, code) {
Roger Meier08b30992011-04-06 21:30:53 +000078 this.message = message;
79 this.code = (code === null) ? 0 : code;
T Jake Luciani322caa22010-02-15 03:24:55 +000080 }
Roger Meierb4bcbe32011-03-07 19:37:46 +000081};
T Jake Luciani322caa22010-02-15 03:24:55 +000082
83
T Jake Lucianiefabb892010-07-28 22:31:12 +000084Thrift.TApplicationExceptionType = {
Roger Meierb4bcbe32011-03-07 19:37:46 +000085 '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 Luciani322caa22010-02-15 03:24:55 +000092
Roger Meierb4bcbe32011-03-07 19:37:46 +000093Thrift.TApplicationException = function(message, code) {
94 this.message = message;
Roger Meierda6e6ae2011-03-15 09:55:33 +000095 this.code = (code === null) ? 0 : code;
Roger Meierb4bcbe32011-03-07 19:37:46 +000096};
T Jake Luciani322caa22010-02-15 03:24:55 +000097
Roger Meierb4bcbe32011-03-07 19:37:46 +000098Thrift.TApplicationException.prototype = {
99
100 read: function(input) {
101 while (1) {
Roger Meierda6e6ae2011-03-15 09:55:33 +0000102 var ret = input.readFieldBegin();
Roger Meierb4bcbe32011-03-07 19:37:46 +0000103
Roger Meierda6e6ae2011-03-15 09:55:33 +0000104 if (ret.ftype == Thrift.Type.STOP) {
Roger Meierb4bcbe32011-03-07 19:37:46 +0000105 break;
Roger Meierda6e6ae2011-03-15 09:55:33 +0000106 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000107
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 Luciani322caa22010-02-15 03:24:55 +0000115 } else {
Roger Meierb4bcbe32011-03-07 19:37:46 +0000116 ret = input.skip(ret.ftype);
T Jake Luciani322caa22010-02-15 03:24:55 +0000117 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000118
119 break;
T Jake Luciani322caa22010-02-15 03:24:55 +0000120 case 2:
Roger Meierb4bcbe32011-03-07 19:37:46 +0000121 if (ret.ftype == Thrift.Type.I32) {
122 ret = input.readI32();
123 this.code = ret.value;
T Jake Luciani322caa22010-02-15 03:24:55 +0000124 } else {
Roger Meierb4bcbe32011-03-07 19:37:46 +0000125 ret = input.skip(ret.ftype);
T Jake Luciani322caa22010-02-15 03:24:55 +0000126 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000127 break;
128
T Jake Luciani322caa22010-02-15 03:24:55 +0000129 default:
Roger Meierb4bcbe32011-03-07 19:37:46 +0000130 ret = input.skip(ret.ftype);
131 break;
T Jake Luciani322caa22010-02-15 03:24:55 +0000132 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000133
134 input.readFieldEnd();
T Jake Luciani322caa22010-02-15 03:24:55 +0000135 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000136
137 input.readStructEnd();
T Jake Luciani322caa22010-02-15 03:24:55 +0000138 },
Roger Meierb4bcbe32011-03-07 19:37:46 +0000139
140 write: function(output) {
141 var xfer = 0;
142
T Jake Luciani322caa22010-02-15 03:24:55 +0000143 output.writeStructBegin('TApplicationException');
Roger Meierb4bcbe32011-03-07 19:37:46 +0000144
T Jake Luciani322caa22010-02-15 03:24:55 +0000145 if (this.message) {
Roger Meierb4bcbe32011-03-07 19:37:46 +0000146 output.writeFieldBegin('message', Thrift.Type.STRING, 1);
147 output.writeString(this.getMessage());
148 output.writeFieldEnd();
T Jake Luciani322caa22010-02-15 03:24:55 +0000149 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000150
T Jake Luciani322caa22010-02-15 03:24:55 +0000151 if (this.code) {
Roger Meierb4bcbe32011-03-07 19:37:46 +0000152 output.writeFieldBegin('type', Thrift.Type.I32, 2);
153 output.writeI32(this.code);
154 output.writeFieldEnd();
T Jake Luciani322caa22010-02-15 03:24:55 +0000155 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000156
157 output.writeFieldStop();
158 output.writeStructEnd();
T Jake Luciani322caa22010-02-15 03:24:55 +0000159 },
Roger Meierb4bcbe32011-03-07 19:37:46 +0000160
161 getCode: function() {
162 return this.code;
T Jake Luciani322caa22010-02-15 03:24:55 +0000163 },
Roger Meierb4bcbe32011-03-07 19:37:46 +0000164
165 getMessage: function() {
166 return this.message;
T Jake Luciani322caa22010-02-15 03:24:55 +0000167 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000168};
T Jake Luciani322caa22010-02-15 03:24:55 +0000169
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 Meierb4bcbe32011-03-07 19:37:46 +0000176Thrift.Transport = function(url) {
177 this.url = url;
178 this.wpos = 0;
179 this.rpos = 0;
T Jake Luciani322caa22010-02-15 03:24:55 +0000180
Roger Meierb4bcbe32011-03-07 19:37:46 +0000181 this.send_buf = '';
182 this.recv_buf = '';
183};
T Jake Luciani322caa22010-02-15 03:24:55 +0000184
185Thrift.Transport.prototype = {
186
187 //Gets the browser specific XmlHttpRequest Object
Roger Meierb4bcbe32011-03-07 19:37:46 +0000188 getXmlHttpRequestObject: function() {
Roger Meierda6e6ae2011-03-15 09:55:33 +0000189 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 Meierb4bcbe32011-03-07 19:37:46 +0000192
193 throw "Your browser doesn't support the XmlHttpRequest object.";
T Jake Luciani322caa22010-02-15 03:24:55 +0000194 },
195
Roger Meier08b30992011-04-06 21:30:53 +0000196 flush: function(async) {
T Jake Luciani322caa22010-02-15 03:24:55 +0000197 //async mode
Roger Meier08b30992011-04-06 21:30:53 +0000198 if (async || this.url === undefined || this.url === '') {
T Jake Luciani322caa22010-02-15 03:24:55 +0000199 return this.send_buf;
Roger Meierda6e6ae2011-03-15 09:55:33 +0000200 }
T Jake Luciani322caa22010-02-15 03:24:55 +0000201
Roger Meierb4bcbe32011-03-07 19:37:46 +0000202 var xreq = this.getXmlHttpRequestObject();
203
Roger Meierda6e6ae2011-03-15 09:55:33 +0000204 if (xreq.overrideMimeType) {
Roger Meierb4bcbe32011-03-07 19:37:46 +0000205 xreq.overrideMimeType('application/json');
Roger Meierda6e6ae2011-03-15 09:55:33 +0000206 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000207
208 xreq.open('POST', this.url, false);
209 xreq.send(this.send_buf);
210
Roger Meierda6e6ae2011-03-15 09:55:33 +0000211 if (xreq.readyState != 4) {
Roger Meierb4bcbe32011-03-07 19:37:46 +0000212 throw 'encountered an unknown ajax ready state: ' + xreq.readyState;
Roger Meierda6e6ae2011-03-15 09:55:33 +0000213 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000214
Roger Meierda6e6ae2011-03-15 09:55:33 +0000215 if (xreq.status != 200) {
Roger Meierb4bcbe32011-03-07 19:37:46 +0000216 throw 'encountered a unknown request status: ' + xreq.status;
Roger Meierda6e6ae2011-03-15 09:55:33 +0000217 }
T Jake Luciani322caa22010-02-15 03:24:55 +0000218
Roger Meierb4bcbe32011-03-07 19:37:46 +0000219 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 Luciani322caa22010-02-15 03:24:55 +0000223 },
224
Roger Meier08b30992011-04-06 21:30:53 +0000225 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 Meierb4bcbe32011-03-07 19:37:46 +0000273 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 Luciani322caa22010-02-15 03:24:55 +0000278 },
279
Roger Meierb4bcbe32011-03-07 19:37:46 +0000280 isOpen: function() {
281 return true;
T Jake Luciani322caa22010-02-15 03:24:55 +0000282 },
283
Roger Meierb4bcbe32011-03-07 19:37:46 +0000284 open: function() {},
T Jake Luciani322caa22010-02-15 03:24:55 +0000285
286 close: function() {},
287
Roger Meierb4bcbe32011-03-07 19:37:46 +0000288 read: function(len) {
289 var avail = this.wpos - this.rpos;
T Jake Luciani322caa22010-02-15 03:24:55 +0000290
Roger Meierda6e6ae2011-03-15 09:55:33 +0000291 if (avail === 0) {
Roger Meierb4bcbe32011-03-07 19:37:46 +0000292 return '';
Roger Meierda6e6ae2011-03-15 09:55:33 +0000293 }
T Jake Luciani322caa22010-02-15 03:24:55 +0000294
Roger Meierb4bcbe32011-03-07 19:37:46 +0000295 var give = len;
T Jake Luciani322caa22010-02-15 03:24:55 +0000296
Roger Meierda6e6ae2011-03-15 09:55:33 +0000297 if (avail < len) {
Roger Meierb4bcbe32011-03-07 19:37:46 +0000298 give = avail;
Roger Meierda6e6ae2011-03-15 09:55:33 +0000299 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000300
301 var ret = this.read_buf.substr(this.rpos, give);
302 this.rpos += give;
T Jake Luciani322caa22010-02-15 03:24:55 +0000303
304 //clear buf when complete?
Roger Meierb4bcbe32011-03-07 19:37:46 +0000305 return ret;
T Jake Luciani322caa22010-02-15 03:24:55 +0000306 },
307
Roger Meierb4bcbe32011-03-07 19:37:46 +0000308 readAll: function() {
Roger Meierda6e6ae2011-03-15 09:55:33 +0000309 return this.recv_buf;
T Jake Luciani322caa22010-02-15 03:24:55 +0000310 },
311
Roger Meierb4bcbe32011-03-07 19:37:46 +0000312 write: function(buf) {
313 this.send_buf = buf;
T Jake Luciani322caa22010-02-15 03:24:55 +0000314 },
315
Roger Meierb4bcbe32011-03-07 19:37:46 +0000316 getSendBuffer: function() {
317 return this.send_buf;
T Jake Luciani322caa22010-02-15 03:24:55 +0000318 }
319
Roger Meierb4bcbe32011-03-07 19:37:46 +0000320};
T Jake Luciani322caa22010-02-15 03:24:55 +0000321
322
323
Roger Meierb4bcbe32011-03-07 19:37:46 +0000324Thrift.Protocol = function(transport) {
325 this.transport = transport;
326};
T Jake Luciani322caa22010-02-15 03:24:55 +0000327
Roger Meierb4bcbe32011-03-07 19:37:46 +0000328Thrift.Protocol.Type = {};
329Thrift.Protocol.Type[Thrift.Type.BOOL] = '"tf"';
330Thrift.Protocol.Type[Thrift.Type.BYTE] = '"i8"';
331Thrift.Protocol.Type[Thrift.Type.I16] = '"i16"';
332Thrift.Protocol.Type[Thrift.Type.I32] = '"i32"';
333Thrift.Protocol.Type[Thrift.Type.I64] = '"i64"';
334Thrift.Protocol.Type[Thrift.Type.DOUBLE] = '"dbl"';
335Thrift.Protocol.Type[Thrift.Type.STRUCT] = '"rec"';
336Thrift.Protocol.Type[Thrift.Type.STRING] = '"str"';
337Thrift.Protocol.Type[Thrift.Type.MAP] = '"map"';
338Thrift.Protocol.Type[Thrift.Type.LIST] = '"lst"';
339Thrift.Protocol.Type[Thrift.Type.SET] = '"set"';
T Jake Luciani322caa22010-02-15 03:24:55 +0000340
341
Roger Meierb4bcbe32011-03-07 19:37:46 +0000342Thrift.Protocol.RType = {};
Roger Meierda6e6ae2011-03-15 09:55:33 +0000343Thrift.Protocol.RType.tf = Thrift.Type.BOOL;
344Thrift.Protocol.RType.i8 = Thrift.Type.BYTE;
345Thrift.Protocol.RType.i16 = Thrift.Type.I16;
346Thrift.Protocol.RType.i32 = Thrift.Type.I32;
347Thrift.Protocol.RType.i64 = Thrift.Type.I64;
348Thrift.Protocol.RType.dbl = Thrift.Type.DOUBLE;
349Thrift.Protocol.RType.rec = Thrift.Type.STRUCT;
350Thrift.Protocol.RType.str = Thrift.Type.STRING;
351Thrift.Protocol.RType.map = Thrift.Type.MAP;
352Thrift.Protocol.RType.lst = Thrift.Type.LIST;
353Thrift.Protocol.RType.set = Thrift.Type.SET;
T Jake Luciani322caa22010-02-15 03:24:55 +0000354
Roger Meierb4bcbe32011-03-07 19:37:46 +0000355Thrift.Protocol.Version = 1;
T Jake Luciani322caa22010-02-15 03:24:55 +0000356
357Thrift.Protocol.prototype = {
Roger Meierb4bcbe32011-03-07 19:37:46 +0000358
359 getTransport: function() {
360 return this.transport;
T Jake Luciani322caa22010-02-15 03:24:55 +0000361 },
362
363 //Write functions
Roger Meierb4bcbe32011-03-07 19:37:46 +0000364 writeMessageBegin: function(name, messageType, seqid) {
Roger Meierda6e6ae2011-03-15 09:55:33 +0000365 this.tstack = [];
366 this.tpos = [];
367
368 this.tstack.push([Thrift.Protocol.Version, '"' +
Roger Meierb4bcbe32011-03-07 19:37:46 +0000369 name + '"', messageType, seqid]);
T Jake Luciani322caa22010-02-15 03:24:55 +0000370 },
371
Roger Meierb4bcbe32011-03-07 19:37:46 +0000372 writeMessageEnd: function() {
373 var obj = this.tstack.pop();
T Jake Luciani322caa22010-02-15 03:24:55 +0000374
Roger Meierb4bcbe32011-03-07 19:37:46 +0000375 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 Luciani322caa22010-02-15 03:24:55 +0000381 },
382
383
Roger Meierb4bcbe32011-03-07 19:37:46 +0000384 writeStructBegin: function(name) {
385 this.tpos.push(this.tstack.length);
386 this.tstack.push({});
T Jake Luciani322caa22010-02-15 03:24:55 +0000387 },
388
Roger Meierb4bcbe32011-03-07 19:37:46 +0000389 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 Meierda6e6ae2011-03-15 09:55:33 +0000396 if (first) {
T Jake Luciani322caa22010-02-15 03:24:55 +0000397 first = false;
Roger Meierda6e6ae2011-03-15 09:55:33 +0000398 } else {
Roger Meierb4bcbe32011-03-07 19:37:46 +0000399 str += ',';
Roger Meierda6e6ae2011-03-15 09:55:33 +0000400 }
T Jake Luciani322caa22010-02-15 03:24:55 +0000401
Roger Meierb4bcbe32011-03-07 19:37:46 +0000402 str += key + ':' + struct[key];
403 }
T Jake Luciani322caa22010-02-15 03:24:55 +0000404
Roger Meierb4bcbe32011-03-07 19:37:46 +0000405 str += '}';
T Jake Luciani322caa22010-02-15 03:24:55 +0000406 this.tstack[p] = str;
407 },
408
Roger Meierb4bcbe32011-03-07 19:37:46 +0000409 writeFieldBegin: function(name, fieldType, fieldId) {
Roger Meierda6e6ae2011-03-15 09:55:33 +0000410 this.tpos.push(this.tstack.length);
411 this.tstack.push({ 'fieldId': '"' +
412 fieldId + '"', 'fieldType': Thrift.Protocol.Type[fieldType]
Roger Meierb4bcbe32011-03-07 19:37:46 +0000413 });
414
T Jake Luciani322caa22010-02-15 03:24:55 +0000415 },
416
Roger Meierb4bcbe32011-03-07 19:37:46 +0000417 writeFieldEnd: function() {
418 var value = this.tstack.pop();
Roger Meierda6e6ae2011-03-15 09:55:33 +0000419 var fieldInfo = this.tstack.pop();
420
421 this.tstack[this.tstack.length - 1][fieldInfo.fieldId] = '{' +
Roger Meierb4bcbe32011-03-07 19:37:46 +0000422 fieldInfo.fieldType + ':' + value + '}';
423 this.tpos.pop();
T Jake Luciani322caa22010-02-15 03:24:55 +0000424 },
425
Roger Meierb4bcbe32011-03-07 19:37:46 +0000426 writeFieldStop: function() {
T Jake Luciani322caa22010-02-15 03:24:55 +0000427 //na
428 },
429
Roger Meierb4bcbe32011-03-07 19:37:46 +0000430 writeMapBegin: function(keyType, valType, size) {
T Jake Luciani322caa22010-02-15 03:24:55 +0000431 //size is invalid, we'll set it on end.
Roger Meierda6e6ae2011-03-15 09:55:33 +0000432 this.tpos.push(this.tstack.length);
Roger Meierb4bcbe32011-03-07 19:37:46 +0000433 this.tstack.push([Thrift.Protocol.Type[keyType],
434 Thrift.Protocol.Type[valType], 0]);
T Jake Luciani322caa22010-02-15 03:24:55 +0000435 },
436
Roger Meierb4bcbe32011-03-07 19:37:46 +0000437 writeMapEnd: function() {
438 var p = this.tpos.pop();
439
Roger Meierda6e6ae2011-03-15 09:55:33 +0000440 if (p == this.tstack.length) {
T Jake Luciani322caa22010-02-15 03:24:55 +0000441 return;
Roger Meierda6e6ae2011-03-15 09:55:33 +0000442 }
T Jake Luciani322caa22010-02-15 03:24:55 +0000443
Roger Meierda6e6ae2011-03-15 09:55:33 +0000444 if ((this.tstack.length - p - 1) % 2 !== 0) {
Roger Meierb4bcbe32011-03-07 19:37:46 +0000445 this.tstack.push('');
Roger Meierda6e6ae2011-03-15 09:55:33 +0000446 }
T Jake Luciani322caa22010-02-15 03:24:55 +0000447
Roger Meierb4bcbe32011-03-07 19:37:46 +0000448 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 Meierda6e6ae2011-03-15 09:55:33 +0000459 } else {
Roger Meierb4bcbe32011-03-07 19:37:46 +0000460 map = ',' + map;
T Jake Luciani322caa22010-02-15 03:24:55 +0000461 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000462
Roger Meier2079bfe2011-06-21 14:09:13 +0000463 if (! isNaN(k)) { k = '"' + k + '"'; } //json "keys" need to be strings
Roger Meier9d8e8f82011-06-14 19:38:27 +0000464 map = k + ':' + v + map;
T Jake Luciani322caa22010-02-15 03:24:55 +0000465 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000466 map = '{' + map;
467
468 this.tstack[p].push(map);
469 this.tstack[p] = '[' + this.tstack[p].join(',') + ']';
T Jake Luciani322caa22010-02-15 03:24:55 +0000470 },
471
Roger Meierb4bcbe32011-03-07 19:37:46 +0000472 writeListBegin: function(elemType, size) {
473 this.tpos.push(this.tstack.length);
474 this.tstack.push([Thrift.Protocol.Type[elemType], size]);
T Jake Luciani322caa22010-02-15 03:24:55 +0000475 },
476
Roger Meierb4bcbe32011-03-07 19:37:46 +0000477 writeListEnd: function() {
478 var p = this.tpos.pop();
T Jake Luciani322caa22010-02-15 03:24:55 +0000479
Roger Meierb4bcbe32011-03-07 19:37:46 +0000480 while (this.tstack.length > p + 1) {
481 var tmpVal = this.tstack[p + 1];
482 this.tstack.splice(p + 1, 1);
483 this.tstack[p].push(tmpVal);
T Jake Luciani322caa22010-02-15 03:24:55 +0000484 }
485
Roger Meierb4bcbe32011-03-07 19:37:46 +0000486 this.tstack[p] = '[' + this.tstack[p].join(',') + ']';
T Jake Luciani322caa22010-02-15 03:24:55 +0000487 },
488
Roger Meierb4bcbe32011-03-07 19:37:46 +0000489 writeSetBegin: function(elemType, size) {
490 this.tpos.push(this.tstack.length);
491 this.tstack.push([Thrift.Protocol.Type[elemType], size]);
T Jake Luciani322caa22010-02-15 03:24:55 +0000492 },
493
Roger Meierb4bcbe32011-03-07 19:37:46 +0000494 writeSetEnd: function() {
495 var p = this.tpos.pop();
T Jake Luciani322caa22010-02-15 03:24:55 +0000496
Roger Meierb4bcbe32011-03-07 19:37:46 +0000497 while (this.tstack.length > p + 1) {
498 var tmpVal = this.tstack[p + 1];
499 this.tstack.splice(p + 1, 1);
500 this.tstack[p].push(tmpVal);
T Jake Luciani322caa22010-02-15 03:24:55 +0000501 }
502
Roger Meierb4bcbe32011-03-07 19:37:46 +0000503 this.tstack[p] = '[' + this.tstack[p].join(',') + ']';
T Jake Luciani322caa22010-02-15 03:24:55 +0000504 },
505
Roger Meierb4bcbe32011-03-07 19:37:46 +0000506 writeBool: function(value) {
507 this.tstack.push(value ? 1 : 0);
T Jake Luciani322caa22010-02-15 03:24:55 +0000508 },
509
Roger Meierb4bcbe32011-03-07 19:37:46 +0000510 writeByte: function(i8) {
T Jake Luciani322caa22010-02-15 03:24:55 +0000511 this.tstack.push(i8);
512 },
513
Roger Meierb4bcbe32011-03-07 19:37:46 +0000514 writeI16: function(i16) {
T Jake Luciani322caa22010-02-15 03:24:55 +0000515 this.tstack.push(i16);
516 },
517
Roger Meierb4bcbe32011-03-07 19:37:46 +0000518 writeI32: function(i32) {
T Jake Luciani322caa22010-02-15 03:24:55 +0000519 this.tstack.push(i32);
520 },
521
Roger Meierb4bcbe32011-03-07 19:37:46 +0000522 writeI64: function(i64) {
T Jake Luciani322caa22010-02-15 03:24:55 +0000523 this.tstack.push(i64);
524 },
525
Roger Meierb4bcbe32011-03-07 19:37:46 +0000526 writeDouble: function(dbl) {
T Jake Luciani322caa22010-02-15 03:24:55 +0000527 this.tstack.push(dbl);
528 },
529
Roger Meierb4bcbe32011-03-07 19:37:46 +0000530 writeString: function(str) {
531 // We do not encode uri components for wire transfer:
532 if (str === null) {
T Jake Luciani416eea92010-09-17 23:38:25 +0000533 this.tstack.push(null);
534 } else {
535 // concat may be slower than building a byte buffer
Roger Meierb4bcbe32011-03-07 19:37:46 +0000536 var escapedString = '';
537 for (var i = 0; i < str.length; i++) {
538 var ch = str.charAt(i); // a single double quote: "
539 if (ch === '\"') {
540 escapedString += '\\\"'; // write out as: \"
541 } else if (ch === '\\') { // a single backslash: \
542 escapedString += '\\\\'; // write out as: \\
T Jake Luciani416eea92010-09-17 23:38:25 +0000543 /* Currently escaped forward slashes break TJSONProtocol.
Roger Meierb4bcbe32011-03-07 19:37:46 +0000544 * As it stands, we can simply pass forward slashes into
545 * our strings across the wire without being escaped.
T Jake Luciani416eea92010-09-17 23:38:25 +0000546 * I think this is the protocol's bug, not thrift.js
Roger Meierb4bcbe32011-03-07 19:37:46 +0000547 * } else if(ch === '/') { // a single forward slash: /
548 * escapedString += '\\/'; // write out as \/
549 * }
T Jake Luciani416eea92010-09-17 23:38:25 +0000550 */
Roger Meierb4bcbe32011-03-07 19:37:46 +0000551 } else if (ch === '\b') { // a single backspace: invisible
552 escapedString += '\\b'; // write out as: \b"
553 } else if (ch === '\f') { // a single formfeed: invisible
554 escapedString += '\\f'; // write out as: \f"
555 } else if (ch === '\n') { // a single newline: invisible
556 escapedString += '\\n'; // write out as: \n"
557 } else if (ch === '\r') { // a single return: invisible
558 escapedString += '\\r'; // write out as: \r"
559 } else if (ch === '\t') { // a single tab: invisible
560 escapedString += '\\t'; // write out as: \t"
T Jake Luciani416eea92010-09-17 23:38:25 +0000561 } else {
Roger Meierb4bcbe32011-03-07 19:37:46 +0000562 escapedString += ch; // Else it need not be escaped
T Jake Luciani416eea92010-09-17 23:38:25 +0000563 }
564 }
565 this.tstack.push('"' + escapedString + '"');
566 }
T Jake Luciani322caa22010-02-15 03:24:55 +0000567 },
568
Roger Meierb4bcbe32011-03-07 19:37:46 +0000569 writeBinary: function(str) {
T Jake Luciani322caa22010-02-15 03:24:55 +0000570 this.writeString(str);
571 },
572
573
Roger Meierb4bcbe32011-03-07 19:37:46 +0000574
T Jake Luciani322caa22010-02-15 03:24:55 +0000575 // Reading functions
Roger Meierb4bcbe32011-03-07 19:37:46 +0000576 readMessageBegin: function(name, messageType, seqid) {
Roger Meierda6e6ae2011-03-15 09:55:33 +0000577 this.rstack = [];
578 this.rpos = [];
T Jake Luciani322caa22010-02-15 03:24:55 +0000579
Roger Meier08b30992011-04-06 21:30:53 +0000580 if (typeof jQuery !== 'undefined') {
581 this.robj = jQuery.parseJSON(this.transport.readAll());
582 } else {
583 this.robj = eval(this.transport.readAll());
584 }
T Jake Luciani322caa22010-02-15 03:24:55 +0000585
T Jake Luciani322caa22010-02-15 03:24:55 +0000586 var r = {};
Roger Meierb4bcbe32011-03-07 19:37:46 +0000587 var version = this.robj.shift();
588
589 if (version != Thrift.Protocol.Version) {
590 throw 'Wrong thrift protocol version: ' + version;
T Jake Luciani322caa22010-02-15 03:24:55 +0000591 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000592
Roger Meierda6e6ae2011-03-15 09:55:33 +0000593 r.fname = this.robj.shift();
594 r.mtype = this.robj.shift();
595 r.rseqid = this.robj.shift();
Roger Meierb4bcbe32011-03-07 19:37:46 +0000596
597
598 //get to the main obj
599 this.rstack.push(this.robj.shift());
600
601 return r;
602 },
603
Roger Meierb4bcbe32011-03-07 19:37:46 +0000604 readMessageEnd: function() {
605 },
606
607 readStructBegin: function(name) {
608 var r = {};
Roger Meierda6e6ae2011-03-15 09:55:33 +0000609 r.fname = '';
Roger Meierb4bcbe32011-03-07 19:37:46 +0000610
611 //incase this is an array of structs
Roger Meierda6e6ae2011-03-15 09:55:33 +0000612 if (this.rstack[this.rstack.length - 1] instanceof Array) {
Roger Meierb4bcbe32011-03-07 19:37:46 +0000613 this.rstack.push(this.rstack[this.rstack.length - 1].shift());
Roger Meierda6e6ae2011-03-15 09:55:33 +0000614 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000615
616 return r;
617 },
618
619 readStructEnd: function() {
Roger Meierda6e6ae2011-03-15 09:55:33 +0000620 if (this.rstack[this.rstack.length - 2] instanceof Array) {
Roger Meierb4bcbe32011-03-07 19:37:46 +0000621 this.rstack.pop();
Roger Meierda6e6ae2011-03-15 09:55:33 +0000622 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000623 },
624
625 readFieldBegin: function() {
626 var r = {};
627
628 var fid = -1;
629 var ftype = Thrift.Type.STOP;
630
631 //get a fieldId
632 for (var f in (this.rstack[this.rstack.length - 1])) {
Roger Meierda6e6ae2011-03-15 09:55:33 +0000633 if (f === null) {
634 continue;
635 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000636
Roger Meierda6e6ae2011-03-15 09:55:33 +0000637 fid = parseInt(f, 10);
Roger Meierb4bcbe32011-03-07 19:37:46 +0000638 this.rpos.push(this.rstack.length);
639
Roger Meierda6e6ae2011-03-15 09:55:33 +0000640 var field = this.rstack[this.rstack.length - 1][fid];
Roger Meierb4bcbe32011-03-07 19:37:46 +0000641
642 //remove so we don't see it again
643 delete this.rstack[this.rstack.length - 1][fid];
644
645 this.rstack.push(field);
646
647 break;
648 }
649
650 if (fid != -1) {
651
T Jake Luciani322caa22010-02-15 03:24:55 +0000652 //should only be 1 of these but this is the only
653 //way to match a key
Roger Meierda6e6ae2011-03-15 09:55:33 +0000654 for (var i in (this.rstack[this.rstack.length - 1])) {
655 if (Thrift.Protocol.RType[i] === null) {
656 continue;
657 }
T Jake Luciani322caa22010-02-15 03:24:55 +0000658
Roger Meierda6e6ae2011-03-15 09:55:33 +0000659 ftype = Thrift.Protocol.RType[i];
Roger Meierb4bcbe32011-03-07 19:37:46 +0000660 this.rstack[this.rstack.length - 1] =
Roger Meierda6e6ae2011-03-15 09:55:33 +0000661 this.rstack[this.rstack.length - 1][i];
Roger Meierb4bcbe32011-03-07 19:37:46 +0000662 }
663 }
664
Roger Meierda6e6ae2011-03-15 09:55:33 +0000665 r.fname = '';
666 r.ftype = ftype;
667 r.fid = fid;
Roger Meierb4bcbe32011-03-07 19:37:46 +0000668
Roger Meierb4bcbe32011-03-07 19:37:46 +0000669 return r;
T Jake Luciani322caa22010-02-15 03:24:55 +0000670 },
671
Roger Meierb4bcbe32011-03-07 19:37:46 +0000672 readFieldEnd: function() {
673 var pos = this.rpos.pop();
674
T Jake Luciani322caa22010-02-15 03:24:55 +0000675 //get back to the right place in the stack
Roger Meierda6e6ae2011-03-15 09:55:33 +0000676 while (this.rstack.length > pos) {
T Jake Luciani322caa22010-02-15 03:24:55 +0000677 this.rstack.pop();
Roger Meierda6e6ae2011-03-15 09:55:33 +0000678 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000679
T Jake Luciani322caa22010-02-15 03:24:55 +0000680 },
681
Roger Meierb4bcbe32011-03-07 19:37:46 +0000682 readMapBegin: function(keyType, valType, size) {
Roger Meierb4bcbe32011-03-07 19:37:46 +0000683 var map = this.rstack.pop();
684
T Jake Luciani322caa22010-02-15 03:24:55 +0000685 var r = {};
Roger Meierda6e6ae2011-03-15 09:55:33 +0000686 r.ktype = Thrift.Protocol.RType[map.shift()];
687 r.vtype = Thrift.Protocol.RType[map.shift()];
688 r.size = map.shift();
T Jake Luciani322caa22010-02-15 03:24:55 +0000689
T Jake Luciani322caa22010-02-15 03:24:55 +0000690
T Jake Luciani322caa22010-02-15 03:24:55 +0000691 this.rpos.push(this.rstack.length);
Roger Meierb4bcbe32011-03-07 19:37:46 +0000692 this.rstack.push(map.shift());
693
T Jake Luciani322caa22010-02-15 03:24:55 +0000694 return r;
695 },
696
Roger Meierb4bcbe32011-03-07 19:37:46 +0000697 readMapEnd: function() {
698 this.readFieldEnd();
T Jake Luciani322caa22010-02-15 03:24:55 +0000699 },
700
Roger Meierb4bcbe32011-03-07 19:37:46 +0000701 readListBegin: function(elemType, size) {
Roger Meierb4bcbe32011-03-07 19:37:46 +0000702 var list = this.rstack[this.rstack.length - 1];
703
704 var r = {};
Roger Meierda6e6ae2011-03-15 09:55:33 +0000705 r.etype = Thrift.Protocol.RType[list.shift()];
706 r.size = list.shift();
Roger Meierb4bcbe32011-03-07 19:37:46 +0000707
Roger Meierb4bcbe32011-03-07 19:37:46 +0000708 this.rpos.push(this.rstack.length);
709 this.rstack.push(list);
710
711 return r;
T Jake Luciani322caa22010-02-15 03:24:55 +0000712 },
713
Roger Meierb4bcbe32011-03-07 19:37:46 +0000714 readListEnd: function() {
715 this.readFieldEnd();
T Jake Luciani322caa22010-02-15 03:24:55 +0000716 },
717
Roger Meierb4bcbe32011-03-07 19:37:46 +0000718 readSetBegin: function(elemType, size) {
719 return this.readListBegin(elemType, size);
720 },
721
722 readSetEnd: function() {
723 return this.readListEnd();
724 },
725
726 readBool: function() {
727 var r = this.readI32();
728
Roger Meierda6e6ae2011-03-15 09:55:33 +0000729 if (r !== null && r.value == '1') {
730 r.value = true;
731 } else {
732 r.value = false;
T Jake Luciani322caa22010-02-15 03:24:55 +0000733 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000734
735 return r;
T Jake Luciani322caa22010-02-15 03:24:55 +0000736 },
737
Roger Meierb4bcbe32011-03-07 19:37:46 +0000738 readByte: function() {
739 return this.readI32();
T Jake Luciani322caa22010-02-15 03:24:55 +0000740 },
741
Roger Meierb4bcbe32011-03-07 19:37:46 +0000742 readI16: function() {
743 return this.readI32();
T Jake Luciani322caa22010-02-15 03:24:55 +0000744 },
T Jake Luciani322caa22010-02-15 03:24:55 +0000745
Roger Meierb4bcbe32011-03-07 19:37:46 +0000746 readI32: function(f) {
Roger Meierda6e6ae2011-03-15 09:55:33 +0000747 if (f === undefined) {
Roger Meierb4bcbe32011-03-07 19:37:46 +0000748 f = this.rstack[this.rstack.length - 1];
Roger Meierda6e6ae2011-03-15 09:55:33 +0000749 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000750
751 var r = {};
752
753 if (f instanceof Array) {
Roger Meierda6e6ae2011-03-15 09:55:33 +0000754 if (f.length === 0) {
755 r.value = undefined;
756 } else {
757 r.value = f.shift();
758 }
759 } else if (f instanceof Object) {
Roger Meierb4bcbe32011-03-07 19:37:46 +0000760 for (var i in f) {
Roger Meierda6e6ae2011-03-15 09:55:33 +0000761 if (i === null) {
762 continue;
763 }
764 this.rstack.push(f[i]);
Roger Meierb4bcbe32011-03-07 19:37:46 +0000765 delete f[i];
766
Roger Meierda6e6ae2011-03-15 09:55:33 +0000767 r.value = i;
Roger Meierb4bcbe32011-03-07 19:37:46 +0000768 break;
T Jake Luciani322caa22010-02-15 03:24:55 +0000769 }
770 } else {
Roger Meierda6e6ae2011-03-15 09:55:33 +0000771 r.value = f;
Roger Meierb4bcbe32011-03-07 19:37:46 +0000772 this.rstack.pop();
T Jake Luciani322caa22010-02-15 03:24:55 +0000773 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000774
775 return r;
T Jake Luciani322caa22010-02-15 03:24:55 +0000776 },
777
Roger Meierb4bcbe32011-03-07 19:37:46 +0000778 readI64: function() {
779 return this.readI32();
T Jake Luciani322caa22010-02-15 03:24:55 +0000780 },
781
Roger Meierb4bcbe32011-03-07 19:37:46 +0000782 readDouble: function() {
783 return this.readI32();
T Jake Luciani322caa22010-02-15 03:24:55 +0000784 },
785
Roger Meierb4bcbe32011-03-07 19:37:46 +0000786 readString: function() {
787 var r = this.readI32();
788 return r;
T Jake Luciani322caa22010-02-15 03:24:55 +0000789 },
790
Roger Meierb4bcbe32011-03-07 19:37:46 +0000791 readBinary: function() {
792 return this.readString();
T Jake Luciani322caa22010-02-15 03:24:55 +0000793 },
794
Roger Meierb4bcbe32011-03-07 19:37:46 +0000795
T Jake Luciani322caa22010-02-15 03:24:55 +0000796 //Method to arbitrarily skip over data.
Roger Meierb4bcbe32011-03-07 19:37:46 +0000797 skip: function(type) {
798 throw 'skip not supported yet';
T Jake Luciani322caa22010-02-15 03:24:55 +0000799 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000800};