blob: cf89236e1e4195742328afa29e0cd7cd9482b690 [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 = {
Jake Farrell6fcecd42012-10-11 20:34:25 +000020 Version: '1.0.0-dev',
Roger Meier0244e932011-03-09 15:25:01 +000021/*
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
T Jake Luciani322caa22010-02-15 03:24:55 +000075
Roger Meier6b0d4562011-07-08 12:44:29 +000076Thrift.TException = function(message) {
77 this.message = message;
78};
79Thrift.inherits(Thrift.TException, Error);
80Thrift.TException.prototype.name = 'TException';
T Jake Luciani322caa22010-02-15 03:24:55 +000081
T Jake Lucianiefabb892010-07-28 22:31:12 +000082Thrift.TApplicationExceptionType = {
Roger Meierb4bcbe32011-03-07 19:37:46 +000083 'UNKNOWN' : 0,
84 'UNKNOWN_METHOD' : 1,
85 'INVALID_MESSAGE_TYPE' : 2,
86 'WRONG_METHOD_NAME' : 3,
87 'BAD_SEQUENCE_ID' : 4,
Roger Meier345ecc72011-08-03 09:49:27 +000088 'MISSING_RESULT' : 5,
89 'INTERNAL_ERROR' : 6,
90 'PROTOCOL_ERROR' : 7
Roger Meierb4bcbe32011-03-07 19:37:46 +000091};
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};
Roger Meier6b0d4562011-07-08 12:44:29 +000097Thrift.inherits(Thrift.TApplicationException, Thrift.TException);
98Thrift.TApplicationException.prototype.name = 'TApplicationException';
T Jake Luciani322caa22010-02-15 03:24:55 +000099
Roger Meier6b0d4562011-07-08 12:44:29 +0000100Thrift.TApplicationException.prototype.read = function(input) {
101 while (1) {
102 var ret = input.readFieldBegin();
Roger Meierb4bcbe32011-03-07 19:37:46 +0000103
Roger Meier6b0d4562011-07-08 12:44:29 +0000104 if (ret.ftype == Thrift.Type.STOP) {
105 break;
106 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000107
Roger Meier6b0d4562011-07-08 12:44:29 +0000108 var fid = ret.fid;
Roger Meierb4bcbe32011-03-07 19:37:46 +0000109
Roger Meier6b0d4562011-07-08 12:44:29 +0000110 switch (fid) {
111 case 1:
112 if (ret.ftype == Thrift.Type.STRING) {
113 ret = input.readString();
114 this.message = ret.value;
115 } else {
Roger Meierb4bcbe32011-03-07 19:37:46 +0000116 ret = input.skip(ret.ftype);
Roger Meier6b0d4562011-07-08 12:44:29 +0000117 }
118 break;
119 case 2:
120 if (ret.ftype == Thrift.Type.I32) {
121 ret = input.readI32();
122 this.code = ret.value;
123 } else {
124 ret = input.skip(ret.ftype);
125 }
126 break;
127 default:
128 ret = input.skip(ret.ftype);
129 break;
T Jake Luciani322caa22010-02-15 03:24:55 +0000130 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000131
Roger Meier6b0d4562011-07-08 12:44:29 +0000132 input.readFieldEnd();
T Jake Luciani322caa22010-02-15 03:24:55 +0000133 }
Roger Meier6b0d4562011-07-08 12:44:29 +0000134
135 input.readStructEnd();
Roger Meierb4bcbe32011-03-07 19:37:46 +0000136};
T Jake Luciani322caa22010-02-15 03:24:55 +0000137
Roger Meier6b0d4562011-07-08 12:44:29 +0000138Thrift.TApplicationException.prototype.write = function(output) {
139 var xfer = 0;
T Jake Luciani322caa22010-02-15 03:24:55 +0000140
Roger Meier6b0d4562011-07-08 12:44:29 +0000141 output.writeStructBegin('TApplicationException');
142
143 if (this.message) {
144 output.writeFieldBegin('message', Thrift.Type.STRING, 1);
145 output.writeString(this.getMessage());
146 output.writeFieldEnd();
147 }
148
149 if (this.code) {
150 output.writeFieldBegin('type', Thrift.Type.I32, 2);
151 output.writeI32(this.code);
152 output.writeFieldEnd();
153 }
154
155 output.writeFieldStop();
156 output.writeStructEnd();
157};
158
159Thrift.TApplicationException.prototype.getCode = function() {
160 return this.code;
161};
162
163Thrift.TApplicationException.prototype.getMessage = function() {
164 return this.message;
165};
T Jake Luciani322caa22010-02-15 03:24:55 +0000166
167/**
168 *If you do not specify a url then you must handle ajax on your own.
169 *This is how to use js bindings in a async fashion.
170 */
Roger Meierb4bcbe32011-03-07 19:37:46 +0000171Thrift.Transport = function(url) {
172 this.url = url;
173 this.wpos = 0;
174 this.rpos = 0;
T Jake Luciani322caa22010-02-15 03:24:55 +0000175
Roger Meierb4bcbe32011-03-07 19:37:46 +0000176 this.send_buf = '';
177 this.recv_buf = '';
178};
T Jake Luciani322caa22010-02-15 03:24:55 +0000179
180Thrift.Transport.prototype = {
181
182 //Gets the browser specific XmlHttpRequest Object
Roger Meierb4bcbe32011-03-07 19:37:46 +0000183 getXmlHttpRequestObject: function() {
Roger Meierda6e6ae2011-03-15 09:55:33 +0000184 try { return new XMLHttpRequest(); } catch (e1) { }
185 try { return new ActiveXObject('Msxml2.XMLHTTP'); } catch (e2) { }
186 try { return new ActiveXObject('Microsoft.XMLHTTP'); } catch (e3) { }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000187
188 throw "Your browser doesn't support the XmlHttpRequest object.";
T Jake Luciani322caa22010-02-15 03:24:55 +0000189 },
190
Roger Meier08b30992011-04-06 21:30:53 +0000191 flush: function(async) {
T Jake Luciani322caa22010-02-15 03:24:55 +0000192 //async mode
Roger Meier08b30992011-04-06 21:30:53 +0000193 if (async || this.url === undefined || this.url === '') {
T Jake Luciani322caa22010-02-15 03:24:55 +0000194 return this.send_buf;
Roger Meierda6e6ae2011-03-15 09:55:33 +0000195 }
T Jake Luciani322caa22010-02-15 03:24:55 +0000196
Roger Meierb4bcbe32011-03-07 19:37:46 +0000197 var xreq = this.getXmlHttpRequestObject();
198
Roger Meierda6e6ae2011-03-15 09:55:33 +0000199 if (xreq.overrideMimeType) {
Roger Meierb4bcbe32011-03-07 19:37:46 +0000200 xreq.overrideMimeType('application/json');
Roger Meierda6e6ae2011-03-15 09:55:33 +0000201 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000202
203 xreq.open('POST', this.url, false);
204 xreq.send(this.send_buf);
205
Roger Meierda6e6ae2011-03-15 09:55:33 +0000206 if (xreq.readyState != 4) {
Roger Meierb4bcbe32011-03-07 19:37:46 +0000207 throw 'encountered an unknown ajax ready state: ' + xreq.readyState;
Roger Meierda6e6ae2011-03-15 09:55:33 +0000208 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000209
Roger Meierda6e6ae2011-03-15 09:55:33 +0000210 if (xreq.status != 200) {
Roger Meierb4bcbe32011-03-07 19:37:46 +0000211 throw 'encountered a unknown request status: ' + xreq.status;
Roger Meierda6e6ae2011-03-15 09:55:33 +0000212 }
T Jake Luciani322caa22010-02-15 03:24:55 +0000213
Roger Meierb4bcbe32011-03-07 19:37:46 +0000214 this.recv_buf = xreq.responseText;
215 this.recv_buf_sz = this.recv_buf.length;
216 this.wpos = this.recv_buf.length;
217 this.rpos = 0;
T Jake Luciani322caa22010-02-15 03:24:55 +0000218 },
219
Roger Meier08b30992011-04-06 21:30:53 +0000220 jqRequest: function(client, postData, args, recv_method) {
221 if (typeof jQuery === 'undefined' ||
222 typeof jQuery.Deferred === 'undefined') {
223 throw 'Thrift.js requires jQuery 1.5+ to use asynchronous requests';
224 }
225
Roger Meierf666d072012-05-01 11:20:12 +0000226 var thriftTransport = this;
Roger Meier08b30992011-04-06 21:30:53 +0000227
228 var jqXHR = jQuery.ajax({
229 url: this.url,
230 data: postData,
231 type: 'POST',
232 cache: false,
Roger Meier741e7152012-12-11 21:21:00 +0100233 contentType: 'application/json',
Roger Meierf666d072012-05-01 11:20:12 +0000234 dataType: 'text thrift',
235 converters: {
236 'text thrift' : function(responseData) {
237 thriftTransport.setRecvBuffer(responseData);
238 var value = recv_method.call(client);
239 return value;
240 }
Roger Meier08b30992011-04-06 21:30:53 +0000241 },
Roger Meierf666d072012-05-01 11:20:12 +0000242 context: client,
243 success: jQuery.makeArray(args).pop()
Roger Meier08b30992011-04-06 21:30:53 +0000244 });
245
Roger Meierf666d072012-05-01 11:20:12 +0000246 return jqXHR;
Roger Meier08b30992011-04-06 21:30:53 +0000247 },
248
Roger Meierb4bcbe32011-03-07 19:37:46 +0000249 setRecvBuffer: function(buf) {
250 this.recv_buf = buf;
251 this.recv_buf_sz = this.recv_buf.length;
252 this.wpos = this.recv_buf.length;
253 this.rpos = 0;
T Jake Luciani322caa22010-02-15 03:24:55 +0000254 },
255
Roger Meierb4bcbe32011-03-07 19:37:46 +0000256 isOpen: function() {
257 return true;
T Jake Luciani322caa22010-02-15 03:24:55 +0000258 },
259
Roger Meierb4bcbe32011-03-07 19:37:46 +0000260 open: function() {},
T Jake Luciani322caa22010-02-15 03:24:55 +0000261
262 close: function() {},
263
Roger Meierb4bcbe32011-03-07 19:37:46 +0000264 read: function(len) {
265 var avail = this.wpos - this.rpos;
T Jake Luciani322caa22010-02-15 03:24:55 +0000266
Roger Meierda6e6ae2011-03-15 09:55:33 +0000267 if (avail === 0) {
Roger Meierb4bcbe32011-03-07 19:37:46 +0000268 return '';
Roger Meierda6e6ae2011-03-15 09:55:33 +0000269 }
T Jake Luciani322caa22010-02-15 03:24:55 +0000270
Roger Meierb4bcbe32011-03-07 19:37:46 +0000271 var give = len;
T Jake Luciani322caa22010-02-15 03:24:55 +0000272
Roger Meierda6e6ae2011-03-15 09:55:33 +0000273 if (avail < len) {
Roger Meierb4bcbe32011-03-07 19:37:46 +0000274 give = avail;
Roger Meierda6e6ae2011-03-15 09:55:33 +0000275 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000276
277 var ret = this.read_buf.substr(this.rpos, give);
278 this.rpos += give;
T Jake Luciani322caa22010-02-15 03:24:55 +0000279
280 //clear buf when complete?
Roger Meierb4bcbe32011-03-07 19:37:46 +0000281 return ret;
T Jake Luciani322caa22010-02-15 03:24:55 +0000282 },
283
Roger Meierb4bcbe32011-03-07 19:37:46 +0000284 readAll: function() {
Roger Meierda6e6ae2011-03-15 09:55:33 +0000285 return this.recv_buf;
T Jake Luciani322caa22010-02-15 03:24:55 +0000286 },
287
Roger Meierb4bcbe32011-03-07 19:37:46 +0000288 write: function(buf) {
289 this.send_buf = buf;
T Jake Luciani322caa22010-02-15 03:24:55 +0000290 },
291
Roger Meierb4bcbe32011-03-07 19:37:46 +0000292 getSendBuffer: function() {
293 return this.send_buf;
T Jake Luciani322caa22010-02-15 03:24:55 +0000294 }
295
Roger Meierb4bcbe32011-03-07 19:37:46 +0000296};
T Jake Luciani322caa22010-02-15 03:24:55 +0000297
298
299
Roger Meierb4bcbe32011-03-07 19:37:46 +0000300Thrift.Protocol = function(transport) {
301 this.transport = transport;
302};
T Jake Luciani322caa22010-02-15 03:24:55 +0000303
Roger Meierb4bcbe32011-03-07 19:37:46 +0000304Thrift.Protocol.Type = {};
305Thrift.Protocol.Type[Thrift.Type.BOOL] = '"tf"';
306Thrift.Protocol.Type[Thrift.Type.BYTE] = '"i8"';
307Thrift.Protocol.Type[Thrift.Type.I16] = '"i16"';
308Thrift.Protocol.Type[Thrift.Type.I32] = '"i32"';
309Thrift.Protocol.Type[Thrift.Type.I64] = '"i64"';
310Thrift.Protocol.Type[Thrift.Type.DOUBLE] = '"dbl"';
311Thrift.Protocol.Type[Thrift.Type.STRUCT] = '"rec"';
312Thrift.Protocol.Type[Thrift.Type.STRING] = '"str"';
313Thrift.Protocol.Type[Thrift.Type.MAP] = '"map"';
314Thrift.Protocol.Type[Thrift.Type.LIST] = '"lst"';
315Thrift.Protocol.Type[Thrift.Type.SET] = '"set"';
T Jake Luciani322caa22010-02-15 03:24:55 +0000316
317
Roger Meierb4bcbe32011-03-07 19:37:46 +0000318Thrift.Protocol.RType = {};
Roger Meierda6e6ae2011-03-15 09:55:33 +0000319Thrift.Protocol.RType.tf = Thrift.Type.BOOL;
320Thrift.Protocol.RType.i8 = Thrift.Type.BYTE;
321Thrift.Protocol.RType.i16 = Thrift.Type.I16;
322Thrift.Protocol.RType.i32 = Thrift.Type.I32;
323Thrift.Protocol.RType.i64 = Thrift.Type.I64;
324Thrift.Protocol.RType.dbl = Thrift.Type.DOUBLE;
325Thrift.Protocol.RType.rec = Thrift.Type.STRUCT;
326Thrift.Protocol.RType.str = Thrift.Type.STRING;
327Thrift.Protocol.RType.map = Thrift.Type.MAP;
328Thrift.Protocol.RType.lst = Thrift.Type.LIST;
329Thrift.Protocol.RType.set = Thrift.Type.SET;
T Jake Luciani322caa22010-02-15 03:24:55 +0000330
Roger Meierb4bcbe32011-03-07 19:37:46 +0000331Thrift.Protocol.Version = 1;
T Jake Luciani322caa22010-02-15 03:24:55 +0000332
333Thrift.Protocol.prototype = {
Roger Meierb4bcbe32011-03-07 19:37:46 +0000334
335 getTransport: function() {
336 return this.transport;
T Jake Luciani322caa22010-02-15 03:24:55 +0000337 },
338
339 //Write functions
Roger Meierb4bcbe32011-03-07 19:37:46 +0000340 writeMessageBegin: function(name, messageType, seqid) {
Roger Meierda6e6ae2011-03-15 09:55:33 +0000341 this.tstack = [];
342 this.tpos = [];
343
344 this.tstack.push([Thrift.Protocol.Version, '"' +
Roger Meierb4bcbe32011-03-07 19:37:46 +0000345 name + '"', messageType, seqid]);
T Jake Luciani322caa22010-02-15 03:24:55 +0000346 },
347
Roger Meierb4bcbe32011-03-07 19:37:46 +0000348 writeMessageEnd: function() {
349 var obj = this.tstack.pop();
T Jake Luciani322caa22010-02-15 03:24:55 +0000350
Roger Meierb4bcbe32011-03-07 19:37:46 +0000351 this.wobj = this.tstack.pop();
352 this.wobj.push(obj);
353
354 this.wbuf = '[' + this.wobj.join(',') + ']';
355
356 this.transport.write(this.wbuf);
T Jake Luciani322caa22010-02-15 03:24:55 +0000357 },
358
359
Roger Meierb4bcbe32011-03-07 19:37:46 +0000360 writeStructBegin: function(name) {
361 this.tpos.push(this.tstack.length);
362 this.tstack.push({});
T Jake Luciani322caa22010-02-15 03:24:55 +0000363 },
364
Roger Meierb4bcbe32011-03-07 19:37:46 +0000365 writeStructEnd: function() {
366
367 var p = this.tpos.pop();
368 var struct = this.tstack[p];
369 var str = '{';
370 var first = true;
371 for (var key in struct) {
Roger Meierda6e6ae2011-03-15 09:55:33 +0000372 if (first) {
T Jake Luciani322caa22010-02-15 03:24:55 +0000373 first = false;
Roger Meierda6e6ae2011-03-15 09:55:33 +0000374 } else {
Roger Meierb4bcbe32011-03-07 19:37:46 +0000375 str += ',';
Roger Meierda6e6ae2011-03-15 09:55:33 +0000376 }
T Jake Luciani322caa22010-02-15 03:24:55 +0000377
Roger Meierb4bcbe32011-03-07 19:37:46 +0000378 str += key + ':' + struct[key];
379 }
T Jake Luciani322caa22010-02-15 03:24:55 +0000380
Roger Meierb4bcbe32011-03-07 19:37:46 +0000381 str += '}';
T Jake Luciani322caa22010-02-15 03:24:55 +0000382 this.tstack[p] = str;
383 },
384
Roger Meierb4bcbe32011-03-07 19:37:46 +0000385 writeFieldBegin: function(name, fieldType, fieldId) {
Roger Meierda6e6ae2011-03-15 09:55:33 +0000386 this.tpos.push(this.tstack.length);
387 this.tstack.push({ 'fieldId': '"' +
388 fieldId + '"', 'fieldType': Thrift.Protocol.Type[fieldType]
Roger Meierb4bcbe32011-03-07 19:37:46 +0000389 });
390
T Jake Luciani322caa22010-02-15 03:24:55 +0000391 },
392
Roger Meierb4bcbe32011-03-07 19:37:46 +0000393 writeFieldEnd: function() {
394 var value = this.tstack.pop();
Roger Meierda6e6ae2011-03-15 09:55:33 +0000395 var fieldInfo = this.tstack.pop();
396
397 this.tstack[this.tstack.length - 1][fieldInfo.fieldId] = '{' +
Roger Meierb4bcbe32011-03-07 19:37:46 +0000398 fieldInfo.fieldType + ':' + value + '}';
399 this.tpos.pop();
T Jake Luciani322caa22010-02-15 03:24:55 +0000400 },
401
Roger Meierb4bcbe32011-03-07 19:37:46 +0000402 writeFieldStop: function() {
T Jake Luciani322caa22010-02-15 03:24:55 +0000403 //na
404 },
405
Roger Meierb4bcbe32011-03-07 19:37:46 +0000406 writeMapBegin: function(keyType, valType, size) {
T Jake Luciani322caa22010-02-15 03:24:55 +0000407 //size is invalid, we'll set it on end.
Roger Meierda6e6ae2011-03-15 09:55:33 +0000408 this.tpos.push(this.tstack.length);
Roger Meierb4bcbe32011-03-07 19:37:46 +0000409 this.tstack.push([Thrift.Protocol.Type[keyType],
410 Thrift.Protocol.Type[valType], 0]);
T Jake Luciani322caa22010-02-15 03:24:55 +0000411 },
412
Roger Meierb4bcbe32011-03-07 19:37:46 +0000413 writeMapEnd: function() {
414 var p = this.tpos.pop();
415
Roger Meierda6e6ae2011-03-15 09:55:33 +0000416 if (p == this.tstack.length) {
T Jake Luciani322caa22010-02-15 03:24:55 +0000417 return;
Roger Meierda6e6ae2011-03-15 09:55:33 +0000418 }
T Jake Luciani322caa22010-02-15 03:24:55 +0000419
Roger Meierda6e6ae2011-03-15 09:55:33 +0000420 if ((this.tstack.length - p - 1) % 2 !== 0) {
Roger Meierb4bcbe32011-03-07 19:37:46 +0000421 this.tstack.push('');
Roger Meierda6e6ae2011-03-15 09:55:33 +0000422 }
T Jake Luciani322caa22010-02-15 03:24:55 +0000423
Roger Meierb4bcbe32011-03-07 19:37:46 +0000424 var size = (this.tstack.length - p - 1) / 2;
425
426 this.tstack[p][this.tstack[p].length - 1] = size;
427
428 var map = '}';
429 var first = true;
430 while (this.tstack.length > p + 1) {
431 var v = this.tstack.pop();
432 var k = this.tstack.pop();
433 if (first) {
434 first = false;
Roger Meierda6e6ae2011-03-15 09:55:33 +0000435 } else {
Roger Meierb4bcbe32011-03-07 19:37:46 +0000436 map = ',' + map;
T Jake Luciani322caa22010-02-15 03:24:55 +0000437 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000438
Roger Meier2079bfe2011-06-21 14:09:13 +0000439 if (! isNaN(k)) { k = '"' + k + '"'; } //json "keys" need to be strings
Roger Meier9d8e8f82011-06-14 19:38:27 +0000440 map = k + ':' + v + map;
T Jake Luciani322caa22010-02-15 03:24:55 +0000441 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000442 map = '{' + map;
443
444 this.tstack[p].push(map);
445 this.tstack[p] = '[' + this.tstack[p].join(',') + ']';
T Jake Luciani322caa22010-02-15 03:24:55 +0000446 },
447
Roger Meierb4bcbe32011-03-07 19:37:46 +0000448 writeListBegin: function(elemType, size) {
449 this.tpos.push(this.tstack.length);
450 this.tstack.push([Thrift.Protocol.Type[elemType], size]);
T Jake Luciani322caa22010-02-15 03:24:55 +0000451 },
452
Roger Meierb4bcbe32011-03-07 19:37:46 +0000453 writeListEnd: function() {
454 var p = this.tpos.pop();
T Jake Luciani322caa22010-02-15 03:24:55 +0000455
Roger Meierb4bcbe32011-03-07 19:37:46 +0000456 while (this.tstack.length > p + 1) {
457 var tmpVal = this.tstack[p + 1];
458 this.tstack.splice(p + 1, 1);
459 this.tstack[p].push(tmpVal);
T Jake Luciani322caa22010-02-15 03:24:55 +0000460 }
461
Roger Meierb4bcbe32011-03-07 19:37:46 +0000462 this.tstack[p] = '[' + this.tstack[p].join(',') + ']';
T Jake Luciani322caa22010-02-15 03:24:55 +0000463 },
464
Roger Meierb4bcbe32011-03-07 19:37:46 +0000465 writeSetBegin: function(elemType, size) {
466 this.tpos.push(this.tstack.length);
467 this.tstack.push([Thrift.Protocol.Type[elemType], size]);
T Jake Luciani322caa22010-02-15 03:24:55 +0000468 },
469
Roger Meierb4bcbe32011-03-07 19:37:46 +0000470 writeSetEnd: function() {
471 var p = this.tpos.pop();
T Jake Luciani322caa22010-02-15 03:24:55 +0000472
Roger Meierb4bcbe32011-03-07 19:37:46 +0000473 while (this.tstack.length > p + 1) {
474 var tmpVal = this.tstack[p + 1];
475 this.tstack.splice(p + 1, 1);
476 this.tstack[p].push(tmpVal);
T Jake Luciani322caa22010-02-15 03:24:55 +0000477 }
478
Roger Meierb4bcbe32011-03-07 19:37:46 +0000479 this.tstack[p] = '[' + this.tstack[p].join(',') + ']';
T Jake Luciani322caa22010-02-15 03:24:55 +0000480 },
481
Roger Meierb4bcbe32011-03-07 19:37:46 +0000482 writeBool: function(value) {
483 this.tstack.push(value ? 1 : 0);
T Jake Luciani322caa22010-02-15 03:24:55 +0000484 },
485
Roger Meierb4bcbe32011-03-07 19:37:46 +0000486 writeByte: function(i8) {
T Jake Luciani322caa22010-02-15 03:24:55 +0000487 this.tstack.push(i8);
488 },
489
Roger Meierb4bcbe32011-03-07 19:37:46 +0000490 writeI16: function(i16) {
T Jake Luciani322caa22010-02-15 03:24:55 +0000491 this.tstack.push(i16);
492 },
493
Roger Meierb4bcbe32011-03-07 19:37:46 +0000494 writeI32: function(i32) {
T Jake Luciani322caa22010-02-15 03:24:55 +0000495 this.tstack.push(i32);
496 },
497
Roger Meierb4bcbe32011-03-07 19:37:46 +0000498 writeI64: function(i64) {
T Jake Luciani322caa22010-02-15 03:24:55 +0000499 this.tstack.push(i64);
500 },
501
Roger Meierb4bcbe32011-03-07 19:37:46 +0000502 writeDouble: function(dbl) {
T Jake Luciani322caa22010-02-15 03:24:55 +0000503 this.tstack.push(dbl);
504 },
505
Roger Meierb4bcbe32011-03-07 19:37:46 +0000506 writeString: function(str) {
507 // We do not encode uri components for wire transfer:
508 if (str === null) {
T Jake Luciani416eea92010-09-17 23:38:25 +0000509 this.tstack.push(null);
510 } else {
511 // concat may be slower than building a byte buffer
Roger Meierb4bcbe32011-03-07 19:37:46 +0000512 var escapedString = '';
513 for (var i = 0; i < str.length; i++) {
514 var ch = str.charAt(i); // a single double quote: "
515 if (ch === '\"') {
516 escapedString += '\\\"'; // write out as: \"
517 } else if (ch === '\\') { // a single backslash: \
518 escapedString += '\\\\'; // write out as: \\
T Jake Luciani416eea92010-09-17 23:38:25 +0000519 /* Currently escaped forward slashes break TJSONProtocol.
Roger Meierb4bcbe32011-03-07 19:37:46 +0000520 * As it stands, we can simply pass forward slashes into
521 * our strings across the wire without being escaped.
T Jake Luciani416eea92010-09-17 23:38:25 +0000522 * I think this is the protocol's bug, not thrift.js
Roger Meierb4bcbe32011-03-07 19:37:46 +0000523 * } else if(ch === '/') { // a single forward slash: /
524 * escapedString += '\\/'; // write out as \/
525 * }
T Jake Luciani416eea92010-09-17 23:38:25 +0000526 */
Roger Meierb4bcbe32011-03-07 19:37:46 +0000527 } else if (ch === '\b') { // a single backspace: invisible
528 escapedString += '\\b'; // write out as: \b"
529 } else if (ch === '\f') { // a single formfeed: invisible
530 escapedString += '\\f'; // write out as: \f"
531 } else if (ch === '\n') { // a single newline: invisible
532 escapedString += '\\n'; // write out as: \n"
533 } else if (ch === '\r') { // a single return: invisible
534 escapedString += '\\r'; // write out as: \r"
535 } else if (ch === '\t') { // a single tab: invisible
536 escapedString += '\\t'; // write out as: \t"
T Jake Luciani416eea92010-09-17 23:38:25 +0000537 } else {
Roger Meierb4bcbe32011-03-07 19:37:46 +0000538 escapedString += ch; // Else it need not be escaped
T Jake Luciani416eea92010-09-17 23:38:25 +0000539 }
540 }
541 this.tstack.push('"' + escapedString + '"');
542 }
T Jake Luciani322caa22010-02-15 03:24:55 +0000543 },
544
Roger Meierb4bcbe32011-03-07 19:37:46 +0000545 writeBinary: function(str) {
T Jake Luciani322caa22010-02-15 03:24:55 +0000546 this.writeString(str);
547 },
548
549
Roger Meierb4bcbe32011-03-07 19:37:46 +0000550
T Jake Luciani322caa22010-02-15 03:24:55 +0000551 // Reading functions
Roger Meierb4bcbe32011-03-07 19:37:46 +0000552 readMessageBegin: function(name, messageType, seqid) {
Roger Meierda6e6ae2011-03-15 09:55:33 +0000553 this.rstack = [];
554 this.rpos = [];
T Jake Luciani322caa22010-02-15 03:24:55 +0000555
Roger Meier08b30992011-04-06 21:30:53 +0000556 if (typeof jQuery !== 'undefined') {
557 this.robj = jQuery.parseJSON(this.transport.readAll());
558 } else {
559 this.robj = eval(this.transport.readAll());
560 }
T Jake Luciani322caa22010-02-15 03:24:55 +0000561
T Jake Luciani322caa22010-02-15 03:24:55 +0000562 var r = {};
Roger Meierb4bcbe32011-03-07 19:37:46 +0000563 var version = this.robj.shift();
564
565 if (version != Thrift.Protocol.Version) {
566 throw 'Wrong thrift protocol version: ' + version;
T Jake Luciani322caa22010-02-15 03:24:55 +0000567 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000568
Roger Meierda6e6ae2011-03-15 09:55:33 +0000569 r.fname = this.robj.shift();
570 r.mtype = this.robj.shift();
571 r.rseqid = this.robj.shift();
Roger Meierb4bcbe32011-03-07 19:37:46 +0000572
573
574 //get to the main obj
575 this.rstack.push(this.robj.shift());
576
577 return r;
578 },
579
Roger Meierb4bcbe32011-03-07 19:37:46 +0000580 readMessageEnd: function() {
581 },
582
583 readStructBegin: function(name) {
584 var r = {};
Roger Meierda6e6ae2011-03-15 09:55:33 +0000585 r.fname = '';
Roger Meierb4bcbe32011-03-07 19:37:46 +0000586
587 //incase this is an array of structs
Roger Meierda6e6ae2011-03-15 09:55:33 +0000588 if (this.rstack[this.rstack.length - 1] instanceof Array) {
Roger Meierb4bcbe32011-03-07 19:37:46 +0000589 this.rstack.push(this.rstack[this.rstack.length - 1].shift());
Roger Meierda6e6ae2011-03-15 09:55:33 +0000590 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000591
592 return r;
593 },
594
595 readStructEnd: function() {
Roger Meierda6e6ae2011-03-15 09:55:33 +0000596 if (this.rstack[this.rstack.length - 2] instanceof Array) {
Roger Meierb4bcbe32011-03-07 19:37:46 +0000597 this.rstack.pop();
Roger Meierda6e6ae2011-03-15 09:55:33 +0000598 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000599 },
600
601 readFieldBegin: function() {
602 var r = {};
603
604 var fid = -1;
605 var ftype = Thrift.Type.STOP;
606
607 //get a fieldId
608 for (var f in (this.rstack[this.rstack.length - 1])) {
Roger Meierda6e6ae2011-03-15 09:55:33 +0000609 if (f === null) {
610 continue;
611 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000612
Roger Meierda6e6ae2011-03-15 09:55:33 +0000613 fid = parseInt(f, 10);
Roger Meierb4bcbe32011-03-07 19:37:46 +0000614 this.rpos.push(this.rstack.length);
615
Roger Meierda6e6ae2011-03-15 09:55:33 +0000616 var field = this.rstack[this.rstack.length - 1][fid];
Roger Meierb4bcbe32011-03-07 19:37:46 +0000617
618 //remove so we don't see it again
619 delete this.rstack[this.rstack.length - 1][fid];
620
621 this.rstack.push(field);
622
623 break;
624 }
625
626 if (fid != -1) {
627
T Jake Luciani322caa22010-02-15 03:24:55 +0000628 //should only be 1 of these but this is the only
629 //way to match a key
Roger Meierda6e6ae2011-03-15 09:55:33 +0000630 for (var i in (this.rstack[this.rstack.length - 1])) {
631 if (Thrift.Protocol.RType[i] === null) {
632 continue;
633 }
T Jake Luciani322caa22010-02-15 03:24:55 +0000634
Roger Meierda6e6ae2011-03-15 09:55:33 +0000635 ftype = Thrift.Protocol.RType[i];
Roger Meierb4bcbe32011-03-07 19:37:46 +0000636 this.rstack[this.rstack.length - 1] =
Roger Meierda6e6ae2011-03-15 09:55:33 +0000637 this.rstack[this.rstack.length - 1][i];
Roger Meierb4bcbe32011-03-07 19:37:46 +0000638 }
639 }
640
Roger Meierda6e6ae2011-03-15 09:55:33 +0000641 r.fname = '';
642 r.ftype = ftype;
643 r.fid = fid;
Roger Meierb4bcbe32011-03-07 19:37:46 +0000644
Roger Meierb4bcbe32011-03-07 19:37:46 +0000645 return r;
T Jake Luciani322caa22010-02-15 03:24:55 +0000646 },
647
Roger Meierb4bcbe32011-03-07 19:37:46 +0000648 readFieldEnd: function() {
649 var pos = this.rpos.pop();
650
T Jake Luciani322caa22010-02-15 03:24:55 +0000651 //get back to the right place in the stack
Roger Meierda6e6ae2011-03-15 09:55:33 +0000652 while (this.rstack.length > pos) {
T Jake Luciani322caa22010-02-15 03:24:55 +0000653 this.rstack.pop();
Roger Meierda6e6ae2011-03-15 09:55:33 +0000654 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000655
T Jake Luciani322caa22010-02-15 03:24:55 +0000656 },
657
Roger Meierb4bcbe32011-03-07 19:37:46 +0000658 readMapBegin: function(keyType, valType, size) {
Roger Meierb4bcbe32011-03-07 19:37:46 +0000659 var map = this.rstack.pop();
660
T Jake Luciani322caa22010-02-15 03:24:55 +0000661 var r = {};
Roger Meierda6e6ae2011-03-15 09:55:33 +0000662 r.ktype = Thrift.Protocol.RType[map.shift()];
663 r.vtype = Thrift.Protocol.RType[map.shift()];
664 r.size = map.shift();
T Jake Luciani322caa22010-02-15 03:24:55 +0000665
T Jake Luciani322caa22010-02-15 03:24:55 +0000666
T Jake Luciani322caa22010-02-15 03:24:55 +0000667 this.rpos.push(this.rstack.length);
Roger Meierb4bcbe32011-03-07 19:37:46 +0000668 this.rstack.push(map.shift());
669
T Jake Luciani322caa22010-02-15 03:24:55 +0000670 return r;
671 },
672
Roger Meierb4bcbe32011-03-07 19:37:46 +0000673 readMapEnd: function() {
674 this.readFieldEnd();
T Jake Luciani322caa22010-02-15 03:24:55 +0000675 },
676
Roger Meierb4bcbe32011-03-07 19:37:46 +0000677 readListBegin: function(elemType, size) {
Roger Meierb4bcbe32011-03-07 19:37:46 +0000678 var list = this.rstack[this.rstack.length - 1];
679
680 var r = {};
Roger Meierda6e6ae2011-03-15 09:55:33 +0000681 r.etype = Thrift.Protocol.RType[list.shift()];
682 r.size = list.shift();
Roger Meierb4bcbe32011-03-07 19:37:46 +0000683
Roger Meierb4bcbe32011-03-07 19:37:46 +0000684 this.rpos.push(this.rstack.length);
685 this.rstack.push(list);
686
687 return r;
T Jake Luciani322caa22010-02-15 03:24:55 +0000688 },
689
Roger Meierb4bcbe32011-03-07 19:37:46 +0000690 readListEnd: function() {
691 this.readFieldEnd();
T Jake Luciani322caa22010-02-15 03:24:55 +0000692 },
693
Roger Meierb4bcbe32011-03-07 19:37:46 +0000694 readSetBegin: function(elemType, size) {
695 return this.readListBegin(elemType, size);
696 },
697
698 readSetEnd: function() {
699 return this.readListEnd();
700 },
701
702 readBool: function() {
703 var r = this.readI32();
704
Roger Meierda6e6ae2011-03-15 09:55:33 +0000705 if (r !== null && r.value == '1') {
706 r.value = true;
707 } else {
708 r.value = false;
T Jake Luciani322caa22010-02-15 03:24:55 +0000709 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000710
711 return r;
T Jake Luciani322caa22010-02-15 03:24:55 +0000712 },
713
Roger Meierb4bcbe32011-03-07 19:37:46 +0000714 readByte: function() {
715 return this.readI32();
T Jake Luciani322caa22010-02-15 03:24:55 +0000716 },
717
Roger Meierb4bcbe32011-03-07 19:37:46 +0000718 readI16: function() {
719 return this.readI32();
T Jake Luciani322caa22010-02-15 03:24:55 +0000720 },
T Jake Luciani322caa22010-02-15 03:24:55 +0000721
Roger Meierb4bcbe32011-03-07 19:37:46 +0000722 readI32: function(f) {
Roger Meierda6e6ae2011-03-15 09:55:33 +0000723 if (f === undefined) {
Roger Meierb4bcbe32011-03-07 19:37:46 +0000724 f = this.rstack[this.rstack.length - 1];
Roger Meierda6e6ae2011-03-15 09:55:33 +0000725 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000726
727 var r = {};
728
729 if (f instanceof Array) {
Roger Meierda6e6ae2011-03-15 09:55:33 +0000730 if (f.length === 0) {
731 r.value = undefined;
732 } else {
733 r.value = f.shift();
734 }
735 } else if (f instanceof Object) {
Roger Meierb4bcbe32011-03-07 19:37:46 +0000736 for (var i in f) {
Roger Meierda6e6ae2011-03-15 09:55:33 +0000737 if (i === null) {
738 continue;
739 }
740 this.rstack.push(f[i]);
Roger Meierb4bcbe32011-03-07 19:37:46 +0000741 delete f[i];
742
Roger Meierda6e6ae2011-03-15 09:55:33 +0000743 r.value = i;
Roger Meierb4bcbe32011-03-07 19:37:46 +0000744 break;
T Jake Luciani322caa22010-02-15 03:24:55 +0000745 }
746 } else {
Roger Meierda6e6ae2011-03-15 09:55:33 +0000747 r.value = f;
Roger Meierb4bcbe32011-03-07 19:37:46 +0000748 this.rstack.pop();
T Jake Luciani322caa22010-02-15 03:24:55 +0000749 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000750
751 return r;
T Jake Luciani322caa22010-02-15 03:24:55 +0000752 },
753
Roger Meierb4bcbe32011-03-07 19:37:46 +0000754 readI64: function() {
755 return this.readI32();
T Jake Luciani322caa22010-02-15 03:24:55 +0000756 },
757
Roger Meierb4bcbe32011-03-07 19:37:46 +0000758 readDouble: function() {
759 return this.readI32();
T Jake Luciani322caa22010-02-15 03:24:55 +0000760 },
761
Roger Meierb4bcbe32011-03-07 19:37:46 +0000762 readString: function() {
763 var r = this.readI32();
764 return r;
T Jake Luciani322caa22010-02-15 03:24:55 +0000765 },
766
Roger Meierb4bcbe32011-03-07 19:37:46 +0000767 readBinary: function() {
768 return this.readString();
T Jake Luciani322caa22010-02-15 03:24:55 +0000769 },
770
Roger Meierb4bcbe32011-03-07 19:37:46 +0000771
T Jake Luciani322caa22010-02-15 03:24:55 +0000772 //Method to arbitrarily skip over data.
Roger Meierb4bcbe32011-03-07 19:37:46 +0000773 skip: function(type) {
774 throw 'skip not supported yet';
T Jake Luciani322caa22010-02-15 03:24:55 +0000775 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000776};