blob: b8ca2c024c6234d62d7f80fe3f033b42270d8d45 [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 Meierb4bcbe32011-03-07 19:37:46 +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) {
T Jake Luciani322caa22010-02-15 03:24:55 +000078 this.message = message;
Roger Meierda6e6ae2011-03-15 09:55:33 +000079 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() {
T Jake Luciani322caa22010-02-15 03:24:55 +0000189
Roger Meierda6e6ae2011-03-15 09:55:33 +0000190 try { return new XMLHttpRequest(); } catch (e1) { }
191 try { return new ActiveXObject('Msxml2.XMLHTTP'); } catch (e2) { }
192 try { return new ActiveXObject('Microsoft.XMLHTTP'); } catch (e3) { }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000193
194 throw "Your browser doesn't support the XmlHttpRequest object.";
195
T Jake Luciani322caa22010-02-15 03:24:55 +0000196 },
197
Roger Meierb4bcbe32011-03-07 19:37:46 +0000198 flush: function() {
T Jake Luciani322caa22010-02-15 03:24:55 +0000199
200 //async mode
Roger Meierda6e6ae2011-03-15 09:55:33 +0000201 if (this.url === undefined || this.url === '') {
T Jake Luciani322caa22010-02-15 03:24:55 +0000202 return this.send_buf;
Roger Meierda6e6ae2011-03-15 09:55:33 +0000203 }
T Jake Luciani322caa22010-02-15 03:24:55 +0000204
Roger Meierb4bcbe32011-03-07 19:37:46 +0000205 var xreq = this.getXmlHttpRequestObject();
206
Roger Meierda6e6ae2011-03-15 09:55:33 +0000207 if (xreq.overrideMimeType) {
Roger Meierb4bcbe32011-03-07 19:37:46 +0000208 xreq.overrideMimeType('application/json');
Roger Meierda6e6ae2011-03-15 09:55:33 +0000209 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000210
211 xreq.open('POST', this.url, false);
212 xreq.send(this.send_buf);
213
Roger Meierda6e6ae2011-03-15 09:55:33 +0000214 if (xreq.readyState != 4) {
Roger Meierb4bcbe32011-03-07 19:37:46 +0000215 throw 'encountered an unknown ajax ready state: ' + xreq.readyState;
Roger Meierda6e6ae2011-03-15 09:55:33 +0000216 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000217
Roger Meierda6e6ae2011-03-15 09:55:33 +0000218 if (xreq.status != 200) {
Roger Meierb4bcbe32011-03-07 19:37:46 +0000219 throw 'encountered a unknown request status: ' + xreq.status;
Roger Meierda6e6ae2011-03-15 09:55:33 +0000220 }
T Jake Luciani322caa22010-02-15 03:24:55 +0000221
Roger Meierb4bcbe32011-03-07 19:37:46 +0000222 this.recv_buf = xreq.responseText;
223 this.recv_buf_sz = this.recv_buf.length;
224 this.wpos = this.recv_buf.length;
225 this.rpos = 0;
T Jake Luciani322caa22010-02-15 03:24:55 +0000226 },
227
Roger Meierb4bcbe32011-03-07 19:37:46 +0000228 setRecvBuffer: function(buf) {
229 this.recv_buf = buf;
230 this.recv_buf_sz = this.recv_buf.length;
231 this.wpos = this.recv_buf.length;
232 this.rpos = 0;
T Jake Luciani322caa22010-02-15 03:24:55 +0000233 },
234
Roger Meierb4bcbe32011-03-07 19:37:46 +0000235 isOpen: function() {
236 return true;
T Jake Luciani322caa22010-02-15 03:24:55 +0000237 },
238
Roger Meierb4bcbe32011-03-07 19:37:46 +0000239 open: function() {},
T Jake Luciani322caa22010-02-15 03:24:55 +0000240
241 close: function() {},
242
Roger Meierb4bcbe32011-03-07 19:37:46 +0000243 read: function(len) {
244 var avail = this.wpos - this.rpos;
T Jake Luciani322caa22010-02-15 03:24:55 +0000245
Roger Meierda6e6ae2011-03-15 09:55:33 +0000246 if (avail === 0) {
Roger Meierb4bcbe32011-03-07 19:37:46 +0000247 return '';
Roger Meierda6e6ae2011-03-15 09:55:33 +0000248 }
T Jake Luciani322caa22010-02-15 03:24:55 +0000249
Roger Meierb4bcbe32011-03-07 19:37:46 +0000250 var give = len;
T Jake Luciani322caa22010-02-15 03:24:55 +0000251
Roger Meierda6e6ae2011-03-15 09:55:33 +0000252 if (avail < len) {
Roger Meierb4bcbe32011-03-07 19:37:46 +0000253 give = avail;
Roger Meierda6e6ae2011-03-15 09:55:33 +0000254 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000255
256 var ret = this.read_buf.substr(this.rpos, give);
257 this.rpos += give;
T Jake Luciani322caa22010-02-15 03:24:55 +0000258
259 //clear buf when complete?
Roger Meierb4bcbe32011-03-07 19:37:46 +0000260 return ret;
T Jake Luciani322caa22010-02-15 03:24:55 +0000261 },
262
Roger Meierb4bcbe32011-03-07 19:37:46 +0000263 readAll: function() {
Roger Meierda6e6ae2011-03-15 09:55:33 +0000264 return this.recv_buf;
T Jake Luciani322caa22010-02-15 03:24:55 +0000265 },
266
Roger Meierb4bcbe32011-03-07 19:37:46 +0000267 write: function(buf) {
268 this.send_buf = buf;
T Jake Luciani322caa22010-02-15 03:24:55 +0000269 },
270
Roger Meierb4bcbe32011-03-07 19:37:46 +0000271 getSendBuffer: function() {
272 return this.send_buf;
T Jake Luciani322caa22010-02-15 03:24:55 +0000273 }
274
Roger Meierb4bcbe32011-03-07 19:37:46 +0000275};
T Jake Luciani322caa22010-02-15 03:24:55 +0000276
277
278
Roger Meierb4bcbe32011-03-07 19:37:46 +0000279Thrift.Protocol = function(transport) {
280 this.transport = transport;
281};
T Jake Luciani322caa22010-02-15 03:24:55 +0000282
Roger Meierb4bcbe32011-03-07 19:37:46 +0000283Thrift.Protocol.Type = {};
284Thrift.Protocol.Type[Thrift.Type.BOOL] = '"tf"';
285Thrift.Protocol.Type[Thrift.Type.BYTE] = '"i8"';
286Thrift.Protocol.Type[Thrift.Type.I16] = '"i16"';
287Thrift.Protocol.Type[Thrift.Type.I32] = '"i32"';
288Thrift.Protocol.Type[Thrift.Type.I64] = '"i64"';
289Thrift.Protocol.Type[Thrift.Type.DOUBLE] = '"dbl"';
290Thrift.Protocol.Type[Thrift.Type.STRUCT] = '"rec"';
291Thrift.Protocol.Type[Thrift.Type.STRING] = '"str"';
292Thrift.Protocol.Type[Thrift.Type.MAP] = '"map"';
293Thrift.Protocol.Type[Thrift.Type.LIST] = '"lst"';
294Thrift.Protocol.Type[Thrift.Type.SET] = '"set"';
T Jake Luciani322caa22010-02-15 03:24:55 +0000295
296
Roger Meierb4bcbe32011-03-07 19:37:46 +0000297Thrift.Protocol.RType = {};
Roger Meierda6e6ae2011-03-15 09:55:33 +0000298Thrift.Protocol.RType.tf = Thrift.Type.BOOL;
299Thrift.Protocol.RType.i8 = Thrift.Type.BYTE;
300Thrift.Protocol.RType.i16 = Thrift.Type.I16;
301Thrift.Protocol.RType.i32 = Thrift.Type.I32;
302Thrift.Protocol.RType.i64 = Thrift.Type.I64;
303Thrift.Protocol.RType.dbl = Thrift.Type.DOUBLE;
304Thrift.Protocol.RType.rec = Thrift.Type.STRUCT;
305Thrift.Protocol.RType.str = Thrift.Type.STRING;
306Thrift.Protocol.RType.map = Thrift.Type.MAP;
307Thrift.Protocol.RType.lst = Thrift.Type.LIST;
308Thrift.Protocol.RType.set = Thrift.Type.SET;
T Jake Luciani322caa22010-02-15 03:24:55 +0000309
Roger Meierb4bcbe32011-03-07 19:37:46 +0000310Thrift.Protocol.Version = 1;
T Jake Luciani322caa22010-02-15 03:24:55 +0000311
312Thrift.Protocol.prototype = {
Roger Meierb4bcbe32011-03-07 19:37:46 +0000313
314 getTransport: function() {
315 return this.transport;
T Jake Luciani322caa22010-02-15 03:24:55 +0000316 },
317
318 //Write functions
Roger Meierb4bcbe32011-03-07 19:37:46 +0000319 writeMessageBegin: function(name, messageType, seqid) {
Roger Meierda6e6ae2011-03-15 09:55:33 +0000320 this.tstack = [];
321 this.tpos = [];
322
323 this.tstack.push([Thrift.Protocol.Version, '"' +
Roger Meierb4bcbe32011-03-07 19:37:46 +0000324 name + '"', messageType, seqid]);
T Jake Luciani322caa22010-02-15 03:24:55 +0000325 },
326
Roger Meierb4bcbe32011-03-07 19:37:46 +0000327 writeMessageEnd: function() {
328 var obj = this.tstack.pop();
T Jake Luciani322caa22010-02-15 03:24:55 +0000329
Roger Meierb4bcbe32011-03-07 19:37:46 +0000330 this.wobj = this.tstack.pop();
331 this.wobj.push(obj);
332
333 this.wbuf = '[' + this.wobj.join(',') + ']';
334
335 this.transport.write(this.wbuf);
T Jake Luciani322caa22010-02-15 03:24:55 +0000336 },
337
338
Roger Meierb4bcbe32011-03-07 19:37:46 +0000339 writeStructBegin: function(name) {
340 this.tpos.push(this.tstack.length);
341 this.tstack.push({});
T Jake Luciani322caa22010-02-15 03:24:55 +0000342 },
343
Roger Meierb4bcbe32011-03-07 19:37:46 +0000344 writeStructEnd: function() {
345
346 var p = this.tpos.pop();
347 var struct = this.tstack[p];
348 var str = '{';
349 var first = true;
350 for (var key in struct) {
Roger Meierda6e6ae2011-03-15 09:55:33 +0000351 if (first) {
T Jake Luciani322caa22010-02-15 03:24:55 +0000352 first = false;
Roger Meierda6e6ae2011-03-15 09:55:33 +0000353 } else {
Roger Meierb4bcbe32011-03-07 19:37:46 +0000354 str += ',';
Roger Meierda6e6ae2011-03-15 09:55:33 +0000355 }
T Jake Luciani322caa22010-02-15 03:24:55 +0000356
Roger Meierb4bcbe32011-03-07 19:37:46 +0000357 str += key + ':' + struct[key];
358 }
T Jake Luciani322caa22010-02-15 03:24:55 +0000359
Roger Meierb4bcbe32011-03-07 19:37:46 +0000360 str += '}';
T Jake Luciani322caa22010-02-15 03:24:55 +0000361 this.tstack[p] = str;
362 },
363
Roger Meierb4bcbe32011-03-07 19:37:46 +0000364 writeFieldBegin: function(name, fieldType, fieldId) {
Roger Meierda6e6ae2011-03-15 09:55:33 +0000365 this.tpos.push(this.tstack.length);
366 this.tstack.push({ 'fieldId': '"' +
367 fieldId + '"', 'fieldType': Thrift.Protocol.Type[fieldType]
Roger Meierb4bcbe32011-03-07 19:37:46 +0000368 });
369
T Jake Luciani322caa22010-02-15 03:24:55 +0000370 },
371
Roger Meierb4bcbe32011-03-07 19:37:46 +0000372 writeFieldEnd: function() {
373 var value = this.tstack.pop();
Roger Meierda6e6ae2011-03-15 09:55:33 +0000374 var fieldInfo = this.tstack.pop();
375
376 this.tstack[this.tstack.length - 1][fieldInfo.fieldId] = '{' +
Roger Meierb4bcbe32011-03-07 19:37:46 +0000377 fieldInfo.fieldType + ':' + value + '}';
378 this.tpos.pop();
T Jake Luciani322caa22010-02-15 03:24:55 +0000379 },
380
Roger Meierb4bcbe32011-03-07 19:37:46 +0000381 writeFieldStop: function() {
T Jake Luciani322caa22010-02-15 03:24:55 +0000382 //na
383 },
384
Roger Meierb4bcbe32011-03-07 19:37:46 +0000385 writeMapBegin: function(keyType, valType, size) {
T Jake Luciani322caa22010-02-15 03:24:55 +0000386 //size is invalid, we'll set it on end.
Roger Meierda6e6ae2011-03-15 09:55:33 +0000387 this.tpos.push(this.tstack.length);
Roger Meierb4bcbe32011-03-07 19:37:46 +0000388 this.tstack.push([Thrift.Protocol.Type[keyType],
389 Thrift.Protocol.Type[valType], 0]);
T Jake Luciani322caa22010-02-15 03:24:55 +0000390 },
391
Roger Meierb4bcbe32011-03-07 19:37:46 +0000392 writeMapEnd: function() {
393 var p = this.tpos.pop();
394
Roger Meierda6e6ae2011-03-15 09:55:33 +0000395 if (p == this.tstack.length) {
T Jake Luciani322caa22010-02-15 03:24:55 +0000396 return;
Roger Meierda6e6ae2011-03-15 09:55:33 +0000397 }
T Jake Luciani322caa22010-02-15 03:24:55 +0000398
Roger Meierda6e6ae2011-03-15 09:55:33 +0000399 if ((this.tstack.length - p - 1) % 2 !== 0) {
Roger Meierb4bcbe32011-03-07 19:37:46 +0000400 this.tstack.push('');
Roger Meierda6e6ae2011-03-15 09:55:33 +0000401 }
T Jake Luciani322caa22010-02-15 03:24:55 +0000402
Roger Meierb4bcbe32011-03-07 19:37:46 +0000403 var size = (this.tstack.length - p - 1) / 2;
404
405 this.tstack[p][this.tstack[p].length - 1] = size;
406
407 var map = '}';
408 var first = true;
409 while (this.tstack.length > p + 1) {
410 var v = this.tstack.pop();
411 var k = this.tstack.pop();
412 if (first) {
413 first = false;
Roger Meierda6e6ae2011-03-15 09:55:33 +0000414 } else {
Roger Meierb4bcbe32011-03-07 19:37:46 +0000415 map = ',' + map;
T Jake Luciani322caa22010-02-15 03:24:55 +0000416 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000417
418 map = '"' + k + '":' + v + map;
T Jake Luciani322caa22010-02-15 03:24:55 +0000419 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000420 map = '{' + map;
421
422 this.tstack[p].push(map);
423 this.tstack[p] = '[' + this.tstack[p].join(',') + ']';
T Jake Luciani322caa22010-02-15 03:24:55 +0000424 },
425
Roger Meierb4bcbe32011-03-07 19:37:46 +0000426 writeListBegin: function(elemType, size) {
427 this.tpos.push(this.tstack.length);
428 this.tstack.push([Thrift.Protocol.Type[elemType], size]);
T Jake Luciani322caa22010-02-15 03:24:55 +0000429 },
430
Roger Meierb4bcbe32011-03-07 19:37:46 +0000431 writeListEnd: function() {
432 var p = this.tpos.pop();
T Jake Luciani322caa22010-02-15 03:24:55 +0000433
Roger Meierb4bcbe32011-03-07 19:37:46 +0000434 while (this.tstack.length > p + 1) {
435 var tmpVal = this.tstack[p + 1];
436 this.tstack.splice(p + 1, 1);
437 this.tstack[p].push(tmpVal);
T Jake Luciani322caa22010-02-15 03:24:55 +0000438 }
439
Roger Meierb4bcbe32011-03-07 19:37:46 +0000440 this.tstack[p] = '[' + this.tstack[p].join(',') + ']';
T Jake Luciani322caa22010-02-15 03:24:55 +0000441 },
442
Roger Meierb4bcbe32011-03-07 19:37:46 +0000443 writeSetBegin: function(elemType, size) {
444 this.tpos.push(this.tstack.length);
445 this.tstack.push([Thrift.Protocol.Type[elemType], size]);
T Jake Luciani322caa22010-02-15 03:24:55 +0000446 },
447
Roger Meierb4bcbe32011-03-07 19:37:46 +0000448 writeSetEnd: function() {
449 var p = this.tpos.pop();
T Jake Luciani322caa22010-02-15 03:24:55 +0000450
Roger Meierb4bcbe32011-03-07 19:37:46 +0000451 while (this.tstack.length > p + 1) {
452 var tmpVal = this.tstack[p + 1];
453 this.tstack.splice(p + 1, 1);
454 this.tstack[p].push(tmpVal);
T Jake Luciani322caa22010-02-15 03:24:55 +0000455 }
456
Roger Meierb4bcbe32011-03-07 19:37:46 +0000457 this.tstack[p] = '[' + this.tstack[p].join(',') + ']';
T Jake Luciani322caa22010-02-15 03:24:55 +0000458 },
459
Roger Meierb4bcbe32011-03-07 19:37:46 +0000460 writeBool: function(value) {
461 this.tstack.push(value ? 1 : 0);
T Jake Luciani322caa22010-02-15 03:24:55 +0000462 },
463
Roger Meierb4bcbe32011-03-07 19:37:46 +0000464 writeByte: function(i8) {
T Jake Luciani322caa22010-02-15 03:24:55 +0000465 this.tstack.push(i8);
466 },
467
Roger Meierb4bcbe32011-03-07 19:37:46 +0000468 writeI16: function(i16) {
T Jake Luciani322caa22010-02-15 03:24:55 +0000469 this.tstack.push(i16);
470 },
471
Roger Meierb4bcbe32011-03-07 19:37:46 +0000472 writeI32: function(i32) {
T Jake Luciani322caa22010-02-15 03:24:55 +0000473 this.tstack.push(i32);
474 },
475
Roger Meierb4bcbe32011-03-07 19:37:46 +0000476 writeI64: function(i64) {
T Jake Luciani322caa22010-02-15 03:24:55 +0000477 this.tstack.push(i64);
478 },
479
Roger Meierb4bcbe32011-03-07 19:37:46 +0000480 writeDouble: function(dbl) {
T Jake Luciani322caa22010-02-15 03:24:55 +0000481 this.tstack.push(dbl);
482 },
483
Roger Meierb4bcbe32011-03-07 19:37:46 +0000484 writeString: function(str) {
485 // We do not encode uri components for wire transfer:
486 if (str === null) {
T Jake Luciani416eea92010-09-17 23:38:25 +0000487 this.tstack.push(null);
488 } else {
489 // concat may be slower than building a byte buffer
Roger Meierb4bcbe32011-03-07 19:37:46 +0000490 var escapedString = '';
491 for (var i = 0; i < str.length; i++) {
492 var ch = str.charAt(i); // a single double quote: "
493 if (ch === '\"') {
494 escapedString += '\\\"'; // write out as: \"
495 } else if (ch === '\\') { // a single backslash: \
496 escapedString += '\\\\'; // write out as: \\
T Jake Luciani416eea92010-09-17 23:38:25 +0000497 /* Currently escaped forward slashes break TJSONProtocol.
Roger Meierb4bcbe32011-03-07 19:37:46 +0000498 * As it stands, we can simply pass forward slashes into
499 * our strings across the wire without being escaped.
T Jake Luciani416eea92010-09-17 23:38:25 +0000500 * I think this is the protocol's bug, not thrift.js
Roger Meierb4bcbe32011-03-07 19:37:46 +0000501 * } else if(ch === '/') { // a single forward slash: /
502 * escapedString += '\\/'; // write out as \/
503 * }
T Jake Luciani416eea92010-09-17 23:38:25 +0000504 */
Roger Meierb4bcbe32011-03-07 19:37:46 +0000505 } else if (ch === '\b') { // a single backspace: invisible
506 escapedString += '\\b'; // write out as: \b"
507 } else if (ch === '\f') { // a single formfeed: invisible
508 escapedString += '\\f'; // write out as: \f"
509 } else if (ch === '\n') { // a single newline: invisible
510 escapedString += '\\n'; // write out as: \n"
511 } else if (ch === '\r') { // a single return: invisible
512 escapedString += '\\r'; // write out as: \r"
513 } else if (ch === '\t') { // a single tab: invisible
514 escapedString += '\\t'; // write out as: \t"
T Jake Luciani416eea92010-09-17 23:38:25 +0000515 } else {
Roger Meierb4bcbe32011-03-07 19:37:46 +0000516 escapedString += ch; // Else it need not be escaped
T Jake Luciani416eea92010-09-17 23:38:25 +0000517 }
518 }
519 this.tstack.push('"' + escapedString + '"');
520 }
T Jake Luciani322caa22010-02-15 03:24:55 +0000521 },
522
Roger Meierb4bcbe32011-03-07 19:37:46 +0000523 writeBinary: function(str) {
T Jake Luciani322caa22010-02-15 03:24:55 +0000524 this.writeString(str);
525 },
526
527
Roger Meierb4bcbe32011-03-07 19:37:46 +0000528
T Jake Luciani322caa22010-02-15 03:24:55 +0000529 // Reading functions
Roger Meierb4bcbe32011-03-07 19:37:46 +0000530 readMessageBegin: function(name, messageType, seqid) {
Roger Meierda6e6ae2011-03-15 09:55:33 +0000531 this.rstack = [];
532 this.rpos = [];
T Jake Luciani322caa22010-02-15 03:24:55 +0000533
Roger Meierb4bcbe32011-03-07 19:37:46 +0000534 this.robj = eval(this.transport.readAll());
T Jake Luciani322caa22010-02-15 03:24:55 +0000535
T Jake Luciani322caa22010-02-15 03:24:55 +0000536 var r = {};
Roger Meierb4bcbe32011-03-07 19:37:46 +0000537 var version = this.robj.shift();
538
539 if (version != Thrift.Protocol.Version) {
540 throw 'Wrong thrift protocol version: ' + version;
T Jake Luciani322caa22010-02-15 03:24:55 +0000541 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000542
Roger Meierda6e6ae2011-03-15 09:55:33 +0000543 r.fname = this.robj.shift();
544 r.mtype = this.robj.shift();
545 r.rseqid = this.robj.shift();
Roger Meierb4bcbe32011-03-07 19:37:46 +0000546
547
548 //get to the main obj
549 this.rstack.push(this.robj.shift());
550
551 return r;
552 },
553
554
555 readMessageEnd: function() {
556 },
557
558 readStructBegin: function(name) {
559 var r = {};
Roger Meierda6e6ae2011-03-15 09:55:33 +0000560 r.fname = '';
Roger Meierb4bcbe32011-03-07 19:37:46 +0000561
562 //incase this is an array of structs
Roger Meierda6e6ae2011-03-15 09:55:33 +0000563 if (this.rstack[this.rstack.length - 1] instanceof Array) {
Roger Meierb4bcbe32011-03-07 19:37:46 +0000564 this.rstack.push(this.rstack[this.rstack.length - 1].shift());
Roger Meierda6e6ae2011-03-15 09:55:33 +0000565 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000566
567 return r;
568 },
569
570 readStructEnd: function() {
Roger Meierda6e6ae2011-03-15 09:55:33 +0000571 if (this.rstack[this.rstack.length - 2] instanceof Array) {
Roger Meierb4bcbe32011-03-07 19:37:46 +0000572 this.rstack.pop();
Roger Meierda6e6ae2011-03-15 09:55:33 +0000573 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000574 },
575
576 readFieldBegin: function() {
577 var r = {};
578
579 var fid = -1;
580 var ftype = Thrift.Type.STOP;
581
582 //get a fieldId
583 for (var f in (this.rstack[this.rstack.length - 1])) {
Roger Meierda6e6ae2011-03-15 09:55:33 +0000584 if (f === null) {
585 continue;
586 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000587
Roger Meierda6e6ae2011-03-15 09:55:33 +0000588 fid = parseInt(f, 10);
Roger Meierb4bcbe32011-03-07 19:37:46 +0000589 this.rpos.push(this.rstack.length);
590
Roger Meierda6e6ae2011-03-15 09:55:33 +0000591 var field = this.rstack[this.rstack.length - 1][fid];
Roger Meierb4bcbe32011-03-07 19:37:46 +0000592
593 //remove so we don't see it again
594 delete this.rstack[this.rstack.length - 1][fid];
595
596 this.rstack.push(field);
597
598 break;
599 }
600
601 if (fid != -1) {
602
T Jake Luciani322caa22010-02-15 03:24:55 +0000603 //should only be 1 of these but this is the only
604 //way to match a key
Roger Meierda6e6ae2011-03-15 09:55:33 +0000605 for (var i in (this.rstack[this.rstack.length - 1])) {
606 if (Thrift.Protocol.RType[i] === null) {
607 continue;
608 }
T Jake Luciani322caa22010-02-15 03:24:55 +0000609
Roger Meierda6e6ae2011-03-15 09:55:33 +0000610 ftype = Thrift.Protocol.RType[i];
Roger Meierb4bcbe32011-03-07 19:37:46 +0000611 this.rstack[this.rstack.length - 1] =
Roger Meierda6e6ae2011-03-15 09:55:33 +0000612 this.rstack[this.rstack.length - 1][i];
Roger Meierb4bcbe32011-03-07 19:37:46 +0000613 }
614 }
615
Roger Meierda6e6ae2011-03-15 09:55:33 +0000616 r.fname = '';
617 r.ftype = ftype;
618 r.fid = fid;
Roger Meierb4bcbe32011-03-07 19:37:46 +0000619
620
621 return r;
T Jake Luciani322caa22010-02-15 03:24:55 +0000622 },
623
Roger Meierb4bcbe32011-03-07 19:37:46 +0000624 readFieldEnd: function() {
625 var pos = this.rpos.pop();
626
T Jake Luciani322caa22010-02-15 03:24:55 +0000627 //get back to the right place in the stack
Roger Meierda6e6ae2011-03-15 09:55:33 +0000628 while (this.rstack.length > pos) {
T Jake Luciani322caa22010-02-15 03:24:55 +0000629 this.rstack.pop();
Roger Meierda6e6ae2011-03-15 09:55:33 +0000630 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000631
T Jake Luciani322caa22010-02-15 03:24:55 +0000632 },
633
Roger Meierb4bcbe32011-03-07 19:37:46 +0000634 readMapBegin: function(keyType, valType, size) {
635
636 var map = this.rstack.pop();
637
T Jake Luciani322caa22010-02-15 03:24:55 +0000638 var r = {};
Roger Meierda6e6ae2011-03-15 09:55:33 +0000639 r.ktype = Thrift.Protocol.RType[map.shift()];
640 r.vtype = Thrift.Protocol.RType[map.shift()];
641 r.size = map.shift();
T Jake Luciani322caa22010-02-15 03:24:55 +0000642
T Jake Luciani322caa22010-02-15 03:24:55 +0000643
T Jake Luciani322caa22010-02-15 03:24:55 +0000644 this.rpos.push(this.rstack.length);
Roger Meierb4bcbe32011-03-07 19:37:46 +0000645 this.rstack.push(map.shift());
646
T Jake Luciani322caa22010-02-15 03:24:55 +0000647 return r;
648 },
649
Roger Meierb4bcbe32011-03-07 19:37:46 +0000650 readMapEnd: function() {
651 this.readFieldEnd();
T Jake Luciani322caa22010-02-15 03:24:55 +0000652 },
653
Roger Meierb4bcbe32011-03-07 19:37:46 +0000654 readListBegin: function(elemType, size) {
655
656 var list = this.rstack[this.rstack.length - 1];
657
658 var r = {};
Roger Meierda6e6ae2011-03-15 09:55:33 +0000659 r.etype = Thrift.Protocol.RType[list.shift()];
660 r.size = list.shift();
Roger Meierb4bcbe32011-03-07 19:37:46 +0000661
662
663 this.rpos.push(this.rstack.length);
664 this.rstack.push(list);
665
666 return r;
T Jake Luciani322caa22010-02-15 03:24:55 +0000667 },
668
Roger Meierb4bcbe32011-03-07 19:37:46 +0000669 readListEnd: function() {
670 this.readFieldEnd();
T Jake Luciani322caa22010-02-15 03:24:55 +0000671 },
672
Roger Meierb4bcbe32011-03-07 19:37:46 +0000673 readSetBegin: function(elemType, size) {
674 return this.readListBegin(elemType, size);
675 },
676
677 readSetEnd: function() {
678 return this.readListEnd();
679 },
680
681 readBool: function() {
682 var r = this.readI32();
683
Roger Meierda6e6ae2011-03-15 09:55:33 +0000684 if (r !== null && r.value == '1') {
685 r.value = true;
686 } else {
687 r.value = false;
T Jake Luciani322caa22010-02-15 03:24:55 +0000688 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000689
690 return r;
T Jake Luciani322caa22010-02-15 03:24:55 +0000691 },
692
Roger Meierb4bcbe32011-03-07 19:37:46 +0000693 readByte: function() {
694 return this.readI32();
T Jake Luciani322caa22010-02-15 03:24:55 +0000695 },
696
Roger Meierb4bcbe32011-03-07 19:37:46 +0000697 readI16: function() {
698 return this.readI32();
T Jake Luciani322caa22010-02-15 03:24:55 +0000699 },
T Jake Luciani322caa22010-02-15 03:24:55 +0000700
Roger Meierb4bcbe32011-03-07 19:37:46 +0000701
702 readI32: function(f) {
Roger Meierda6e6ae2011-03-15 09:55:33 +0000703 if (f === undefined) {
Roger Meierb4bcbe32011-03-07 19:37:46 +0000704 f = this.rstack[this.rstack.length - 1];
Roger Meierda6e6ae2011-03-15 09:55:33 +0000705 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000706
707 var r = {};
708
709 if (f instanceof Array) {
Roger Meierda6e6ae2011-03-15 09:55:33 +0000710 if (f.length === 0) {
711 r.value = undefined;
712 } else {
713 r.value = f.shift();
714 }
715 } else if (f instanceof Object) {
Roger Meierb4bcbe32011-03-07 19:37:46 +0000716 for (var i in f) {
Roger Meierda6e6ae2011-03-15 09:55:33 +0000717 if (i === null) {
718 continue;
719 }
720 this.rstack.push(f[i]);
Roger Meierb4bcbe32011-03-07 19:37:46 +0000721 delete f[i];
722
Roger Meierda6e6ae2011-03-15 09:55:33 +0000723 r.value = i;
Roger Meierb4bcbe32011-03-07 19:37:46 +0000724 break;
T Jake Luciani322caa22010-02-15 03:24:55 +0000725 }
726 } else {
Roger Meierda6e6ae2011-03-15 09:55:33 +0000727 r.value = f;
Roger Meierb4bcbe32011-03-07 19:37:46 +0000728 this.rstack.pop();
T Jake Luciani322caa22010-02-15 03:24:55 +0000729 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000730
731 return r;
T Jake Luciani322caa22010-02-15 03:24:55 +0000732 },
733
Roger Meierb4bcbe32011-03-07 19:37:46 +0000734 readI64: function() {
735 return this.readI32();
T Jake Luciani322caa22010-02-15 03:24:55 +0000736 },
737
Roger Meierb4bcbe32011-03-07 19:37:46 +0000738 readDouble: function() {
739 return this.readI32();
T Jake Luciani322caa22010-02-15 03:24:55 +0000740 },
741
Roger Meierb4bcbe32011-03-07 19:37:46 +0000742 readString: function() {
743 var r = this.readI32();
744 return r;
T Jake Luciani322caa22010-02-15 03:24:55 +0000745 },
746
Roger Meierb4bcbe32011-03-07 19:37:46 +0000747 readBinary: function() {
748 return this.readString();
T Jake Luciani322caa22010-02-15 03:24:55 +0000749 },
750
Roger Meierb4bcbe32011-03-07 19:37:46 +0000751
T Jake Luciani322caa22010-02-15 03:24:55 +0000752 //Method to arbitrarily skip over data.
Roger Meierb4bcbe32011-03-07 19:37:46 +0000753 skip: function(type) {
754 throw 'skip not supported yet';
T Jake Luciani322caa22010-02-15 03:24:55 +0000755 }
Roger Meierb4bcbe32011-03-07 19:37:46 +0000756
757};