blob: 2de5f1a30c54bfb11ef22001bf9260a2b52eef0e [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 Farrell99010692011-11-30 02:09:46 +000020 Version: '0.9.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 Meierf666d072012-05-01 11:20:12 +0000233 dataType: 'text thrift',
234 converters: {
235 'text thrift' : function(responseData) {
236 thriftTransport.setRecvBuffer(responseData);
237 var value = recv_method.call(client);
238 return value;
239 }
Roger Meier08b30992011-04-06 21:30:53 +0000240 },
Roger Meierf666d072012-05-01 11:20:12 +0000241 context: client,
242 success: jQuery.makeArray(args).pop()
Roger Meier08b30992011-04-06 21:30:53 +0000243 });
244
Roger Meierf666d072012-05-01 11:20:12 +0000245 return jqXHR;
Roger Meier08b30992011-04-06 21:30:53 +0000246 },
247
Roger Meierb4bcbe32011-03-07 19:37:46 +0000248 setRecvBuffer: function(buf) {
249 this.recv_buf = buf;
250 this.recv_buf_sz = this.recv_buf.length;
251 this.wpos = this.recv_buf.length;
252 this.rpos = 0;
T Jake Luciani322caa22010-02-15 03:24:55 +0000253 },
254
Roger Meierb4bcbe32011-03-07 19:37:46 +0000255 isOpen: function() {
256 return true;
T Jake Luciani322caa22010-02-15 03:24:55 +0000257 },
258
Roger Meierb4bcbe32011-03-07 19:37:46 +0000259 open: function() {},
T Jake Luciani322caa22010-02-15 03:24:55 +0000260
261 close: function() {},
262
Roger Meierb4bcbe32011-03-07 19:37:46 +0000263 read: function(len) {
264 var avail = this.wpos - this.rpos;
T Jake Luciani322caa22010-02-15 03:24:55 +0000265
Roger Meierda6e6ae2011-03-15 09:55:33 +0000266 if (avail === 0) {
Roger Meierb4bcbe32011-03-07 19:37:46 +0000267 return '';
Roger Meierda6e6ae2011-03-15 09:55:33 +0000268 }
T Jake Luciani322caa22010-02-15 03:24:55 +0000269
Roger Meierb4bcbe32011-03-07 19:37:46 +0000270 var give = len;
T Jake Luciani322caa22010-02-15 03:24:55 +0000271
Roger Meierda6e6ae2011-03-15 09:55:33 +0000272 if (avail < len) {
Roger Meierb4bcbe32011-03-07 19:37:46 +0000273 give = avail;
Roger Meierda6e6ae2011-03-15 09:55:33 +0000274 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000275
276 var ret = this.read_buf.substr(this.rpos, give);
277 this.rpos += give;
T Jake Luciani322caa22010-02-15 03:24:55 +0000278
279 //clear buf when complete?
Roger Meierb4bcbe32011-03-07 19:37:46 +0000280 return ret;
T Jake Luciani322caa22010-02-15 03:24:55 +0000281 },
282
Roger Meierb4bcbe32011-03-07 19:37:46 +0000283 readAll: function() {
Roger Meierda6e6ae2011-03-15 09:55:33 +0000284 return this.recv_buf;
T Jake Luciani322caa22010-02-15 03:24:55 +0000285 },
286
Roger Meierb4bcbe32011-03-07 19:37:46 +0000287 write: function(buf) {
288 this.send_buf = buf;
T Jake Luciani322caa22010-02-15 03:24:55 +0000289 },
290
Roger Meierb4bcbe32011-03-07 19:37:46 +0000291 getSendBuffer: function() {
292 return this.send_buf;
T Jake Luciani322caa22010-02-15 03:24:55 +0000293 }
294
Roger Meierb4bcbe32011-03-07 19:37:46 +0000295};
T Jake Luciani322caa22010-02-15 03:24:55 +0000296
297
298
Roger Meierb4bcbe32011-03-07 19:37:46 +0000299Thrift.Protocol = function(transport) {
300 this.transport = transport;
301};
T Jake Luciani322caa22010-02-15 03:24:55 +0000302
Roger Meierb4bcbe32011-03-07 19:37:46 +0000303Thrift.Protocol.Type = {};
304Thrift.Protocol.Type[Thrift.Type.BOOL] = '"tf"';
305Thrift.Protocol.Type[Thrift.Type.BYTE] = '"i8"';
306Thrift.Protocol.Type[Thrift.Type.I16] = '"i16"';
307Thrift.Protocol.Type[Thrift.Type.I32] = '"i32"';
308Thrift.Protocol.Type[Thrift.Type.I64] = '"i64"';
309Thrift.Protocol.Type[Thrift.Type.DOUBLE] = '"dbl"';
310Thrift.Protocol.Type[Thrift.Type.STRUCT] = '"rec"';
311Thrift.Protocol.Type[Thrift.Type.STRING] = '"str"';
312Thrift.Protocol.Type[Thrift.Type.MAP] = '"map"';
313Thrift.Protocol.Type[Thrift.Type.LIST] = '"lst"';
314Thrift.Protocol.Type[Thrift.Type.SET] = '"set"';
T Jake Luciani322caa22010-02-15 03:24:55 +0000315
316
Roger Meierb4bcbe32011-03-07 19:37:46 +0000317Thrift.Protocol.RType = {};
Roger Meierda6e6ae2011-03-15 09:55:33 +0000318Thrift.Protocol.RType.tf = Thrift.Type.BOOL;
319Thrift.Protocol.RType.i8 = Thrift.Type.BYTE;
320Thrift.Protocol.RType.i16 = Thrift.Type.I16;
321Thrift.Protocol.RType.i32 = Thrift.Type.I32;
322Thrift.Protocol.RType.i64 = Thrift.Type.I64;
323Thrift.Protocol.RType.dbl = Thrift.Type.DOUBLE;
324Thrift.Protocol.RType.rec = Thrift.Type.STRUCT;
325Thrift.Protocol.RType.str = Thrift.Type.STRING;
326Thrift.Protocol.RType.map = Thrift.Type.MAP;
327Thrift.Protocol.RType.lst = Thrift.Type.LIST;
328Thrift.Protocol.RType.set = Thrift.Type.SET;
T Jake Luciani322caa22010-02-15 03:24:55 +0000329
Roger Meierb4bcbe32011-03-07 19:37:46 +0000330Thrift.Protocol.Version = 1;
T Jake Luciani322caa22010-02-15 03:24:55 +0000331
332Thrift.Protocol.prototype = {
Roger Meierb4bcbe32011-03-07 19:37:46 +0000333
334 getTransport: function() {
335 return this.transport;
T Jake Luciani322caa22010-02-15 03:24:55 +0000336 },
337
338 //Write functions
Roger Meierb4bcbe32011-03-07 19:37:46 +0000339 writeMessageBegin: function(name, messageType, seqid) {
Roger Meierda6e6ae2011-03-15 09:55:33 +0000340 this.tstack = [];
341 this.tpos = [];
342
343 this.tstack.push([Thrift.Protocol.Version, '"' +
Roger Meierb4bcbe32011-03-07 19:37:46 +0000344 name + '"', messageType, seqid]);
T Jake Luciani322caa22010-02-15 03:24:55 +0000345 },
346
Roger Meierb4bcbe32011-03-07 19:37:46 +0000347 writeMessageEnd: function() {
348 var obj = this.tstack.pop();
T Jake Luciani322caa22010-02-15 03:24:55 +0000349
Roger Meierb4bcbe32011-03-07 19:37:46 +0000350 this.wobj = this.tstack.pop();
351 this.wobj.push(obj);
352
353 this.wbuf = '[' + this.wobj.join(',') + ']';
354
355 this.transport.write(this.wbuf);
T Jake Luciani322caa22010-02-15 03:24:55 +0000356 },
357
358
Roger Meierb4bcbe32011-03-07 19:37:46 +0000359 writeStructBegin: function(name) {
360 this.tpos.push(this.tstack.length);
361 this.tstack.push({});
T Jake Luciani322caa22010-02-15 03:24:55 +0000362 },
363
Roger Meierb4bcbe32011-03-07 19:37:46 +0000364 writeStructEnd: function() {
365
366 var p = this.tpos.pop();
367 var struct = this.tstack[p];
368 var str = '{';
369 var first = true;
370 for (var key in struct) {
Roger Meierda6e6ae2011-03-15 09:55:33 +0000371 if (first) {
T Jake Luciani322caa22010-02-15 03:24:55 +0000372 first = false;
Roger Meierda6e6ae2011-03-15 09:55:33 +0000373 } else {
Roger Meierb4bcbe32011-03-07 19:37:46 +0000374 str += ',';
Roger Meierda6e6ae2011-03-15 09:55:33 +0000375 }
T Jake Luciani322caa22010-02-15 03:24:55 +0000376
Roger Meierb4bcbe32011-03-07 19:37:46 +0000377 str += key + ':' + struct[key];
378 }
T Jake Luciani322caa22010-02-15 03:24:55 +0000379
Roger Meierb4bcbe32011-03-07 19:37:46 +0000380 str += '}';
T Jake Luciani322caa22010-02-15 03:24:55 +0000381 this.tstack[p] = str;
382 },
383
Roger Meierb4bcbe32011-03-07 19:37:46 +0000384 writeFieldBegin: function(name, fieldType, fieldId) {
Roger Meierda6e6ae2011-03-15 09:55:33 +0000385 this.tpos.push(this.tstack.length);
386 this.tstack.push({ 'fieldId': '"' +
387 fieldId + '"', 'fieldType': Thrift.Protocol.Type[fieldType]
Roger Meierb4bcbe32011-03-07 19:37:46 +0000388 });
389
T Jake Luciani322caa22010-02-15 03:24:55 +0000390 },
391
Roger Meierb4bcbe32011-03-07 19:37:46 +0000392 writeFieldEnd: function() {
393 var value = this.tstack.pop();
Roger Meierda6e6ae2011-03-15 09:55:33 +0000394 var fieldInfo = this.tstack.pop();
395
396 this.tstack[this.tstack.length - 1][fieldInfo.fieldId] = '{' +
Roger Meierb4bcbe32011-03-07 19:37:46 +0000397 fieldInfo.fieldType + ':' + value + '}';
398 this.tpos.pop();
T Jake Luciani322caa22010-02-15 03:24:55 +0000399 },
400
Roger Meierb4bcbe32011-03-07 19:37:46 +0000401 writeFieldStop: function() {
T Jake Luciani322caa22010-02-15 03:24:55 +0000402 //na
403 },
404
Roger Meierb4bcbe32011-03-07 19:37:46 +0000405 writeMapBegin: function(keyType, valType, size) {
T Jake Luciani322caa22010-02-15 03:24:55 +0000406 //size is invalid, we'll set it on end.
Roger Meierda6e6ae2011-03-15 09:55:33 +0000407 this.tpos.push(this.tstack.length);
Roger Meierb4bcbe32011-03-07 19:37:46 +0000408 this.tstack.push([Thrift.Protocol.Type[keyType],
409 Thrift.Protocol.Type[valType], 0]);
T Jake Luciani322caa22010-02-15 03:24:55 +0000410 },
411
Roger Meierb4bcbe32011-03-07 19:37:46 +0000412 writeMapEnd: function() {
413 var p = this.tpos.pop();
414
Roger Meierda6e6ae2011-03-15 09:55:33 +0000415 if (p == this.tstack.length) {
T Jake Luciani322caa22010-02-15 03:24:55 +0000416 return;
Roger Meierda6e6ae2011-03-15 09:55:33 +0000417 }
T Jake Luciani322caa22010-02-15 03:24:55 +0000418
Roger Meierda6e6ae2011-03-15 09:55:33 +0000419 if ((this.tstack.length - p - 1) % 2 !== 0) {
Roger Meierb4bcbe32011-03-07 19:37:46 +0000420 this.tstack.push('');
Roger Meierda6e6ae2011-03-15 09:55:33 +0000421 }
T Jake Luciani322caa22010-02-15 03:24:55 +0000422
Roger Meierb4bcbe32011-03-07 19:37:46 +0000423 var size = (this.tstack.length - p - 1) / 2;
424
425 this.tstack[p][this.tstack[p].length - 1] = size;
426
427 var map = '}';
428 var first = true;
429 while (this.tstack.length > p + 1) {
430 var v = this.tstack.pop();
431 var k = this.tstack.pop();
432 if (first) {
433 first = false;
Roger Meierda6e6ae2011-03-15 09:55:33 +0000434 } else {
Roger Meierb4bcbe32011-03-07 19:37:46 +0000435 map = ',' + map;
T Jake Luciani322caa22010-02-15 03:24:55 +0000436 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000437
Roger Meier2079bfe2011-06-21 14:09:13 +0000438 if (! isNaN(k)) { k = '"' + k + '"'; } //json "keys" need to be strings
Roger Meier9d8e8f82011-06-14 19:38:27 +0000439 map = k + ':' + v + map;
T Jake Luciani322caa22010-02-15 03:24:55 +0000440 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000441 map = '{' + map;
442
443 this.tstack[p].push(map);
444 this.tstack[p] = '[' + this.tstack[p].join(',') + ']';
T Jake Luciani322caa22010-02-15 03:24:55 +0000445 },
446
Roger Meierb4bcbe32011-03-07 19:37:46 +0000447 writeListBegin: function(elemType, size) {
448 this.tpos.push(this.tstack.length);
449 this.tstack.push([Thrift.Protocol.Type[elemType], size]);
T Jake Luciani322caa22010-02-15 03:24:55 +0000450 },
451
Roger Meierb4bcbe32011-03-07 19:37:46 +0000452 writeListEnd: function() {
453 var p = this.tpos.pop();
T Jake Luciani322caa22010-02-15 03:24:55 +0000454
Roger Meierb4bcbe32011-03-07 19:37:46 +0000455 while (this.tstack.length > p + 1) {
456 var tmpVal = this.tstack[p + 1];
457 this.tstack.splice(p + 1, 1);
458 this.tstack[p].push(tmpVal);
T Jake Luciani322caa22010-02-15 03:24:55 +0000459 }
460
Roger Meierb4bcbe32011-03-07 19:37:46 +0000461 this.tstack[p] = '[' + this.tstack[p].join(',') + ']';
T Jake Luciani322caa22010-02-15 03:24:55 +0000462 },
463
Roger Meierb4bcbe32011-03-07 19:37:46 +0000464 writeSetBegin: function(elemType, size) {
465 this.tpos.push(this.tstack.length);
466 this.tstack.push([Thrift.Protocol.Type[elemType], size]);
T Jake Luciani322caa22010-02-15 03:24:55 +0000467 },
468
Roger Meierb4bcbe32011-03-07 19:37:46 +0000469 writeSetEnd: function() {
470 var p = this.tpos.pop();
T Jake Luciani322caa22010-02-15 03:24:55 +0000471
Roger Meierb4bcbe32011-03-07 19:37:46 +0000472 while (this.tstack.length > p + 1) {
473 var tmpVal = this.tstack[p + 1];
474 this.tstack.splice(p + 1, 1);
475 this.tstack[p].push(tmpVal);
T Jake Luciani322caa22010-02-15 03:24:55 +0000476 }
477
Roger Meierb4bcbe32011-03-07 19:37:46 +0000478 this.tstack[p] = '[' + this.tstack[p].join(',') + ']';
T Jake Luciani322caa22010-02-15 03:24:55 +0000479 },
480
Roger Meierb4bcbe32011-03-07 19:37:46 +0000481 writeBool: function(value) {
482 this.tstack.push(value ? 1 : 0);
T Jake Luciani322caa22010-02-15 03:24:55 +0000483 },
484
Roger Meierb4bcbe32011-03-07 19:37:46 +0000485 writeByte: function(i8) {
T Jake Luciani322caa22010-02-15 03:24:55 +0000486 this.tstack.push(i8);
487 },
488
Roger Meierb4bcbe32011-03-07 19:37:46 +0000489 writeI16: function(i16) {
T Jake Luciani322caa22010-02-15 03:24:55 +0000490 this.tstack.push(i16);
491 },
492
Roger Meierb4bcbe32011-03-07 19:37:46 +0000493 writeI32: function(i32) {
T Jake Luciani322caa22010-02-15 03:24:55 +0000494 this.tstack.push(i32);
495 },
496
Roger Meierb4bcbe32011-03-07 19:37:46 +0000497 writeI64: function(i64) {
T Jake Luciani322caa22010-02-15 03:24:55 +0000498 this.tstack.push(i64);
499 },
500
Roger Meierb4bcbe32011-03-07 19:37:46 +0000501 writeDouble: function(dbl) {
T Jake Luciani322caa22010-02-15 03:24:55 +0000502 this.tstack.push(dbl);
503 },
504
Roger Meierb4bcbe32011-03-07 19:37:46 +0000505 writeString: function(str) {
506 // We do not encode uri components for wire transfer:
507 if (str === null) {
T Jake Luciani416eea92010-09-17 23:38:25 +0000508 this.tstack.push(null);
509 } else {
510 // concat may be slower than building a byte buffer
Roger Meierb4bcbe32011-03-07 19:37:46 +0000511 var escapedString = '';
512 for (var i = 0; i < str.length; i++) {
513 var ch = str.charAt(i); // a single double quote: "
514 if (ch === '\"') {
515 escapedString += '\\\"'; // write out as: \"
516 } else if (ch === '\\') { // a single backslash: \
517 escapedString += '\\\\'; // write out as: \\
T Jake Luciani416eea92010-09-17 23:38:25 +0000518 /* Currently escaped forward slashes break TJSONProtocol.
Roger Meierb4bcbe32011-03-07 19:37:46 +0000519 * As it stands, we can simply pass forward slashes into
520 * our strings across the wire without being escaped.
T Jake Luciani416eea92010-09-17 23:38:25 +0000521 * I think this is the protocol's bug, not thrift.js
Roger Meierb4bcbe32011-03-07 19:37:46 +0000522 * } else if(ch === '/') { // a single forward slash: /
523 * escapedString += '\\/'; // write out as \/
524 * }
T Jake Luciani416eea92010-09-17 23:38:25 +0000525 */
Roger Meierb4bcbe32011-03-07 19:37:46 +0000526 } else if (ch === '\b') { // a single backspace: invisible
527 escapedString += '\\b'; // write out as: \b"
528 } else if (ch === '\f') { // a single formfeed: invisible
529 escapedString += '\\f'; // write out as: \f"
530 } else if (ch === '\n') { // a single newline: invisible
531 escapedString += '\\n'; // write out as: \n"
532 } else if (ch === '\r') { // a single return: invisible
533 escapedString += '\\r'; // write out as: \r"
534 } else if (ch === '\t') { // a single tab: invisible
535 escapedString += '\\t'; // write out as: \t"
T Jake Luciani416eea92010-09-17 23:38:25 +0000536 } else {
Roger Meierb4bcbe32011-03-07 19:37:46 +0000537 escapedString += ch; // Else it need not be escaped
T Jake Luciani416eea92010-09-17 23:38:25 +0000538 }
539 }
540 this.tstack.push('"' + escapedString + '"');
541 }
T Jake Luciani322caa22010-02-15 03:24:55 +0000542 },
543
Roger Meierb4bcbe32011-03-07 19:37:46 +0000544 writeBinary: function(str) {
T Jake Luciani322caa22010-02-15 03:24:55 +0000545 this.writeString(str);
546 },
547
548
Roger Meierb4bcbe32011-03-07 19:37:46 +0000549
T Jake Luciani322caa22010-02-15 03:24:55 +0000550 // Reading functions
Roger Meierb4bcbe32011-03-07 19:37:46 +0000551 readMessageBegin: function(name, messageType, seqid) {
Roger Meierda6e6ae2011-03-15 09:55:33 +0000552 this.rstack = [];
553 this.rpos = [];
T Jake Luciani322caa22010-02-15 03:24:55 +0000554
Roger Meier08b30992011-04-06 21:30:53 +0000555 if (typeof jQuery !== 'undefined') {
556 this.robj = jQuery.parseJSON(this.transport.readAll());
557 } else {
558 this.robj = eval(this.transport.readAll());
559 }
T Jake Luciani322caa22010-02-15 03:24:55 +0000560
T Jake Luciani322caa22010-02-15 03:24:55 +0000561 var r = {};
Roger Meierb4bcbe32011-03-07 19:37:46 +0000562 var version = this.robj.shift();
563
564 if (version != Thrift.Protocol.Version) {
565 throw 'Wrong thrift protocol version: ' + version;
T Jake Luciani322caa22010-02-15 03:24:55 +0000566 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000567
Roger Meierda6e6ae2011-03-15 09:55:33 +0000568 r.fname = this.robj.shift();
569 r.mtype = this.robj.shift();
570 r.rseqid = this.robj.shift();
Roger Meierb4bcbe32011-03-07 19:37:46 +0000571
572
573 //get to the main obj
574 this.rstack.push(this.robj.shift());
575
576 return r;
577 },
578
Roger Meierb4bcbe32011-03-07 19:37:46 +0000579 readMessageEnd: function() {
580 },
581
582 readStructBegin: function(name) {
583 var r = {};
Roger Meierda6e6ae2011-03-15 09:55:33 +0000584 r.fname = '';
Roger Meierb4bcbe32011-03-07 19:37:46 +0000585
586 //incase this is an array of structs
Roger Meierda6e6ae2011-03-15 09:55:33 +0000587 if (this.rstack[this.rstack.length - 1] instanceof Array) {
Roger Meierb4bcbe32011-03-07 19:37:46 +0000588 this.rstack.push(this.rstack[this.rstack.length - 1].shift());
Roger Meierda6e6ae2011-03-15 09:55:33 +0000589 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000590
591 return r;
592 },
593
594 readStructEnd: function() {
Roger Meierda6e6ae2011-03-15 09:55:33 +0000595 if (this.rstack[this.rstack.length - 2] instanceof Array) {
Roger Meierb4bcbe32011-03-07 19:37:46 +0000596 this.rstack.pop();
Roger Meierda6e6ae2011-03-15 09:55:33 +0000597 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000598 },
599
600 readFieldBegin: function() {
601 var r = {};
602
603 var fid = -1;
604 var ftype = Thrift.Type.STOP;
605
606 //get a fieldId
607 for (var f in (this.rstack[this.rstack.length - 1])) {
Roger Meierda6e6ae2011-03-15 09:55:33 +0000608 if (f === null) {
609 continue;
610 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000611
Roger Meierda6e6ae2011-03-15 09:55:33 +0000612 fid = parseInt(f, 10);
Roger Meierb4bcbe32011-03-07 19:37:46 +0000613 this.rpos.push(this.rstack.length);
614
Roger Meierda6e6ae2011-03-15 09:55:33 +0000615 var field = this.rstack[this.rstack.length - 1][fid];
Roger Meierb4bcbe32011-03-07 19:37:46 +0000616
617 //remove so we don't see it again
618 delete this.rstack[this.rstack.length - 1][fid];
619
620 this.rstack.push(field);
621
622 break;
623 }
624
625 if (fid != -1) {
626
T Jake Luciani322caa22010-02-15 03:24:55 +0000627 //should only be 1 of these but this is the only
628 //way to match a key
Roger Meierda6e6ae2011-03-15 09:55:33 +0000629 for (var i in (this.rstack[this.rstack.length - 1])) {
630 if (Thrift.Protocol.RType[i] === null) {
631 continue;
632 }
T Jake Luciani322caa22010-02-15 03:24:55 +0000633
Roger Meierda6e6ae2011-03-15 09:55:33 +0000634 ftype = Thrift.Protocol.RType[i];
Roger Meierb4bcbe32011-03-07 19:37:46 +0000635 this.rstack[this.rstack.length - 1] =
Roger Meierda6e6ae2011-03-15 09:55:33 +0000636 this.rstack[this.rstack.length - 1][i];
Roger Meierb4bcbe32011-03-07 19:37:46 +0000637 }
638 }
639
Roger Meierda6e6ae2011-03-15 09:55:33 +0000640 r.fname = '';
641 r.ftype = ftype;
642 r.fid = fid;
Roger Meierb4bcbe32011-03-07 19:37:46 +0000643
Roger Meierb4bcbe32011-03-07 19:37:46 +0000644 return r;
T Jake Luciani322caa22010-02-15 03:24:55 +0000645 },
646
Roger Meierb4bcbe32011-03-07 19:37:46 +0000647 readFieldEnd: function() {
648 var pos = this.rpos.pop();
649
T Jake Luciani322caa22010-02-15 03:24:55 +0000650 //get back to the right place in the stack
Roger Meierda6e6ae2011-03-15 09:55:33 +0000651 while (this.rstack.length > pos) {
T Jake Luciani322caa22010-02-15 03:24:55 +0000652 this.rstack.pop();
Roger Meierda6e6ae2011-03-15 09:55:33 +0000653 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000654
T Jake Luciani322caa22010-02-15 03:24:55 +0000655 },
656
Roger Meierb4bcbe32011-03-07 19:37:46 +0000657 readMapBegin: function(keyType, valType, size) {
Roger Meierb4bcbe32011-03-07 19:37:46 +0000658 var map = this.rstack.pop();
659
T Jake Luciani322caa22010-02-15 03:24:55 +0000660 var r = {};
Roger Meierda6e6ae2011-03-15 09:55:33 +0000661 r.ktype = Thrift.Protocol.RType[map.shift()];
662 r.vtype = Thrift.Protocol.RType[map.shift()];
663 r.size = map.shift();
T Jake Luciani322caa22010-02-15 03:24:55 +0000664
T Jake Luciani322caa22010-02-15 03:24:55 +0000665
T Jake Luciani322caa22010-02-15 03:24:55 +0000666 this.rpos.push(this.rstack.length);
Roger Meierb4bcbe32011-03-07 19:37:46 +0000667 this.rstack.push(map.shift());
668
T Jake Luciani322caa22010-02-15 03:24:55 +0000669 return r;
670 },
671
Roger Meierb4bcbe32011-03-07 19:37:46 +0000672 readMapEnd: function() {
673 this.readFieldEnd();
T Jake Luciani322caa22010-02-15 03:24:55 +0000674 },
675
Roger Meierb4bcbe32011-03-07 19:37:46 +0000676 readListBegin: function(elemType, size) {
Roger Meierb4bcbe32011-03-07 19:37:46 +0000677 var list = this.rstack[this.rstack.length - 1];
678
679 var r = {};
Roger Meierda6e6ae2011-03-15 09:55:33 +0000680 r.etype = Thrift.Protocol.RType[list.shift()];
681 r.size = list.shift();
Roger Meierb4bcbe32011-03-07 19:37:46 +0000682
Roger Meierb4bcbe32011-03-07 19:37:46 +0000683 this.rpos.push(this.rstack.length);
684 this.rstack.push(list);
685
686 return r;
T Jake Luciani322caa22010-02-15 03:24:55 +0000687 },
688
Roger Meierb4bcbe32011-03-07 19:37:46 +0000689 readListEnd: function() {
690 this.readFieldEnd();
T Jake Luciani322caa22010-02-15 03:24:55 +0000691 },
692
Roger Meierb4bcbe32011-03-07 19:37:46 +0000693 readSetBegin: function(elemType, size) {
694 return this.readListBegin(elemType, size);
695 },
696
697 readSetEnd: function() {
698 return this.readListEnd();
699 },
700
701 readBool: function() {
702 var r = this.readI32();
703
Roger Meierda6e6ae2011-03-15 09:55:33 +0000704 if (r !== null && r.value == '1') {
705 r.value = true;
706 } else {
707 r.value = false;
T Jake Luciani322caa22010-02-15 03:24:55 +0000708 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000709
710 return r;
T Jake Luciani322caa22010-02-15 03:24:55 +0000711 },
712
Roger Meierb4bcbe32011-03-07 19:37:46 +0000713 readByte: function() {
714 return this.readI32();
T Jake Luciani322caa22010-02-15 03:24:55 +0000715 },
716
Roger Meierb4bcbe32011-03-07 19:37:46 +0000717 readI16: function() {
718 return this.readI32();
T Jake Luciani322caa22010-02-15 03:24:55 +0000719 },
T Jake Luciani322caa22010-02-15 03:24:55 +0000720
Roger Meierb4bcbe32011-03-07 19:37:46 +0000721 readI32: function(f) {
Roger Meierda6e6ae2011-03-15 09:55:33 +0000722 if (f === undefined) {
Roger Meierb4bcbe32011-03-07 19:37:46 +0000723 f = this.rstack[this.rstack.length - 1];
Roger Meierda6e6ae2011-03-15 09:55:33 +0000724 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000725
726 var r = {};
727
728 if (f instanceof Array) {
Roger Meierda6e6ae2011-03-15 09:55:33 +0000729 if (f.length === 0) {
730 r.value = undefined;
731 } else {
732 r.value = f.shift();
733 }
734 } else if (f instanceof Object) {
Roger Meierb4bcbe32011-03-07 19:37:46 +0000735 for (var i in f) {
Roger Meierda6e6ae2011-03-15 09:55:33 +0000736 if (i === null) {
737 continue;
738 }
739 this.rstack.push(f[i]);
Roger Meierb4bcbe32011-03-07 19:37:46 +0000740 delete f[i];
741
Roger Meierda6e6ae2011-03-15 09:55:33 +0000742 r.value = i;
Roger Meierb4bcbe32011-03-07 19:37:46 +0000743 break;
T Jake Luciani322caa22010-02-15 03:24:55 +0000744 }
745 } else {
Roger Meierda6e6ae2011-03-15 09:55:33 +0000746 r.value = f;
Roger Meierb4bcbe32011-03-07 19:37:46 +0000747 this.rstack.pop();
T Jake Luciani322caa22010-02-15 03:24:55 +0000748 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000749
750 return r;
T Jake Luciani322caa22010-02-15 03:24:55 +0000751 },
752
Roger Meierb4bcbe32011-03-07 19:37:46 +0000753 readI64: function() {
754 return this.readI32();
T Jake Luciani322caa22010-02-15 03:24:55 +0000755 },
756
Roger Meierb4bcbe32011-03-07 19:37:46 +0000757 readDouble: function() {
758 return this.readI32();
T Jake Luciani322caa22010-02-15 03:24:55 +0000759 },
760
Roger Meierb4bcbe32011-03-07 19:37:46 +0000761 readString: function() {
762 var r = this.readI32();
763 return r;
T Jake Luciani322caa22010-02-15 03:24:55 +0000764 },
765
Roger Meierb4bcbe32011-03-07 19:37:46 +0000766 readBinary: function() {
767 return this.readString();
T Jake Luciani322caa22010-02-15 03:24:55 +0000768 },
769
Roger Meierb4bcbe32011-03-07 19:37:46 +0000770
T Jake Luciani322caa22010-02-15 03:24:55 +0000771 //Method to arbitrarily skip over data.
Roger Meierb4bcbe32011-03-07 19:37:46 +0000772 skip: function(type) {
773 throw 'skip not supported yet';
T Jake Luciani322caa22010-02-15 03:24:55 +0000774 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000775};