Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

first commit

  • Loading branch information...
commit 25609f93045a526208eff544efdca0b8c0d38388 0 parents
Jan Dolecek authored
2  .gitignore
... ... @@ -0,0 +1,2 @@
  1 +/.idea
  2 +thrift-old.js
47 lib_class.js
... ... @@ -0,0 +1,47 @@
  1 +/**
  2 + * Working with classes easily
  3 + */
  4 +
  5 +// Working with classes
  6 +var Class = exports.Class = function Class(def) {
  7 + // pokud není konstruktor definován, použijeme nový (nechceme použít zděděný)
  8 + var constructor = def.hasOwnProperty('init') ? def.init : function() { };
  9 +
  10 + // proces vytváření třídy rozdělíme do kroků
  11 + for (var name in Class.Initializers) {
  12 + Class.Initializers[name].call(constructor, def[name], def);
  13 + }
  14 + return constructor;
  15 +};
  16 +
  17 +Class.Initializers = {
  18 + Extends: function(parent) {
  19 + if (parent) {
  20 + var F = function() { };
  21 + this._superClass = F.prototype = parent.prototype;
  22 + this.prototype = new F;
  23 + }
  24 + },
  25 +
  26 + Mixins: function(mixins, def) {
  27 + // kostruktoru přidáme metodu mixin
  28 + this.mixin = function(mixin) {
  29 + for (var key in mixin) {
  30 + if (key in Class.Initializers) continue;
  31 + this.prototype[key] = mixin[key];
  32 + }
  33 + this.prototype.constructor = this;
  34 + };
  35 + // a přidanou metodu hned využijeme pro rozšíření prototype
  36 + var objects = [def].concat(mixins || []);
  37 + for (var i = 0, l = objects.length; i < l; i++) {
  38 + this.mixin(objects[i]);
  39 + }
  40 + },
  41 +
  42 + // Add static functions
  43 + Static: function(methods) {
  44 + for(var name in methods) this[name] = methods[name];
  45 + },
  46 +};
  47 +
125 lib_fifo.js
... ... @@ -0,0 +1,125 @@
  1 +/**
  2 + * Implementation if FIFO (queue)
  3 + */
  4 +
  5 +var Buffer = require('buffer').Buffer,
  6 + Class = require('./lib_class').Class,
  7 + _;
  8 +
  9 +
  10 +/**
  11 +* FIFO buffer with autoresize
  12 +*/
  13 +var FIFO = exports.FIFO = Class({
  14 + init: function(size) {
  15 + this.size = size || 4096;
  16 + this.start = this.end = 0;
  17 + this.buffer = new Buffer(this.size);
  18 + },
  19 +
  20 + length: function() {
  21 + return this.end - this.start;
  22 + },
  23 +
  24 + available: function(len) {
  25 + return this.length() >= len;
  26 + },
  27 +
  28 + peek: function() {
  29 + if(this.start == this.end) return false;
  30 + else return this.buffer.slice(this.start, this.end);
  31 + },
  32 +
  33 + peekString: function() {
  34 + if(this.start == this.end) return '';
  35 + else return this.buffer.slice(this.start, this.end).toString('ascii');
  36 + },
  37 +
  38 + read: function(len, skip) {
  39 + if(this.end - this.start < len) throw new Error("Not enough data");
  40 + var ret = this.buffer.slice(this.start, this.start + len);
  41 + this.start += len;
  42 +
  43 + if(skip) this.skip(skip);
  44 +
  45 + return ret;
  46 + },
  47 +
  48 + skip: function(len) {
  49 + this.start += len;
  50 + },
  51 +
  52 + skipRegMatch: function(x) {
  53 + this.skip(x.index + x[0].length);
  54 + },
  55 +
  56 + write: function(buf) {
  57 + if(!(buf instanceof Buffer)) buf = new Buffer(buf);
  58 + var len = buf.length;
  59 +
  60 + // If not enough space, make some
  61 + if(this.end + len > this.size) this.requiredMoreSpace(len);
  62 +
  63 + // Copy new data to our buffer
  64 + buf.copy(this.buffer, this.end, 0);
  65 + this.end += len;
  66 + return len;
  67 + },
  68 +
  69 + clear: function() {
  70 + this.start = this.end = 0;
  71 + },
  72 +
  73 + // Make more space if needed
  74 + requiredMoreSpace: function(len) {
  75 + // Double size if needed
  76 + var requiredLen = this.end - this.start + len;
  77 + while(requiredLen > this.size) this.size *= 2;
  78 +
  79 + // Prepare swap buffer
  80 + var buf = new Buffer(this.size);
  81 + this.buffer.copy(buf, 0, this.start, this.end);
  82 +
  83 + // Swap 'em
  84 + this.buffer = buf;
  85 +
  86 + this.end -= this.start;
  87 + this.start = 0;
  88 + },
  89 +
  90 + // Find position of char
  91 + find: function(chr, offset) {
  92 + if(offset === undefined) offset = 0;
  93 +
  94 + for(var i = this.start + offset; i < this.end; ++i) {
  95 + if(this.buffer[i] == chr) return i - this.start;
  96 + }
  97 + }
  98 +
  99 +});
  100 +
  101 +/***********
  102 +// TESTING FOR FIFO
  103 +var f = new FIFO(5);
  104 +console.log('Size:', sys.inspect(f.size));
  105 +console.log('Length:', sys.inspect(f.length()));
  106 +console.log('Avail :', sys.inspect(f.available(2)));
  107 +
  108 +console.log('Write', "\n");
  109 +f.write("ahoj ka");
  110 +
  111 +console.log('Size:', sys.inspect(f.size));
  112 +console.log('Length:', sys.inspect(f.length()));
  113 +console.log('Avail :', sys.inspect(f.available(4)));
  114 +console.log('Peek :', sys.inspect(f.peek()));
  115 +console.log();
  116 +
  117 +console.log('Read :', sys.inspect(f.read(4)));
  118 +console.log('Size:', sys.inspect(f.size));
  119 +console.log('Length:', sys.inspect(f.length()));
  120 +console.log('Avail :', sys.inspect(f.available(4)));
  121 +console.log('Peek :', sys.inspect(f.peek()));
  122 +
  123 +return;
  124 +*****************************/
  125 +
53 lib_misc.js
... ... @@ -0,0 +1,53 @@
  1 +/**
  2 + * Some useful functions
  3 + */
  4 +
  5 +exports.merge = function merge(a, b) {
  6 + var ret = [];
  7 + for(var i = 0; i < a.length; i++) ret.push(a[i]);
  8 + for(var i = 0; i < b.length; i++) ret.push(b[i]);
  9 + return ret;
  10 +}
  11 +
  12 +exports.ip2long = function(s) {
  13 + var x = s.split(/\./);
  14 + return (x[0] << 24) | (x[1] << 16) | (x[2] << 8) | x[3];
  15 +}
  16 +
  17 +exports.long2ip = function(i) {
  18 + return [
  19 + i >> 24 & 0xff,
  20 + i >> 16 & 0xff,
  21 + i >> 8 & 0xff,
  22 + i & 0xff
  23 + ].join('.');
  24 +}
  25 +
  26 +exports.compareIP = function(a, b) {
  27 + function str(s) {
  28 + return s.split(/\./).map(function(i) { var x = parseInt(i).toString(16); return x.length == 1 ? '0' + x : x; }).join(' ');
  29 + }
  30 + var aa = str(a), bb = str(b);
  31 +
  32 + return aa == bb ? 0 : (aa < bb ? -1 : 1);
  33 +}
  34 +
  35 +// Filter for interesting IPs
  36 +exports.filterIP = function(item) {
  37 + return item
  38 + && item.match(/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/)
  39 + && !item.match(/^192\.168\.[012]\.\d+$/) // Behind NAT
  40 + && !item.match(/^10\.87\.\d+\.\d+$/) // TV, fuck off
  41 + ;
  42 +}
  43 +
  44 +// Compare two buffers
  45 +exports.buffCompare = function(buf1, buf2) {
  46 + if(!(buf1 instanceof Buffer)) return false;
  47 + if(!(buf2 instanceof Buffer)) return false;
  48 + if(buf1.length != buf2.length) return false;
  49 +
  50 + for(var i = 0; i < buf1.length; i++) if(buf1[i] != buf2[i]) return false;
  51 +
  52 + return true;
  53 +}
95 lib_pack.js
... ... @@ -0,0 +1,95 @@
  1 +/**
  2 + * Packing un unpacking
  3 + * Decoders are based on code from http://github.com/mranney/node_pcap/ with some modifications
  4 + */
  5 +
  6 +
  7 +// Pack numbers or other data stuctures to binary format
  8 +var pack = {
  9 + ethernet_addr: function (mac, buf, offset) {
  10 + var parts = mac.split(':');
  11 + if(parts.length != 6) return false;
  12 +
  13 + for(var i = 0; i < 6; i++) buf[offset + i] = parseInt(parts[i], 16);
  14 + return true;
  15 + },
  16 +
  17 + uint16: function (num, buf, offset) {
  18 + buf[offset] = (num >> 8) & 0xff;
  19 + buf[offset + 1] = num & 0xff;
  20 + return true;
  21 + },
  22 +
  23 + uint32: function (num, buf, offset) {
  24 + buf[offset] = (num >> 24) & 0xff;
  25 + buf[offset + 1] = (num >> 16) & 0xff;
  26 + buf[offset + 2] = (num >> 8) & 0xff;
  27 + buf[offset + 3] = (num) & 0xff;
  28 + },
  29 +
  30 + uint64: function (num, buf, offset) {
  31 + buf[offset] = (num >> 56) & 0xff;
  32 + buf[offset + 1] = (num >> 48) & 0xff;
  33 + buf[offset + 2] = (num >> 40) & 0xff;
  34 + buf[offset + 3] = (num >> 32) & 0xff;
  35 + buf[offset + 4] = (num >> 24) & 0xff;
  36 + buf[offset + 5] = (num >> 16) & 0xff;
  37 + buf[offset + 6] = (num >> 8) & 0xff;
  38 + buf[offset + 7] = (num) & 0xff;
  39 + },
  40 +
  41 + ipv4_addr: function (ip, buf, offset) {
  42 + var parts = ip.split('.');
  43 + if(parts.length != 4) return false;
  44 +
  45 + for(var i = 0; i < 4; i++) buf[offset + i] = parseInt(parts[i]);
  46 + return true;
  47 + },
  48 +};
  49 +exports.pack = pack;
  50 +
  51 +
  52 +var unpack = {
  53 + ethernet_addr: function (raw_packet, offset) {
  54 + return [
  55 + lpad(raw_packet[offset].toString(16), 2),
  56 + lpad(raw_packet[offset + 1].toString(16), 2),
  57 + lpad(raw_packet[offset + 2].toString(16), 2),
  58 + lpad(raw_packet[offset + 3].toString(16), 2),
  59 + lpad(raw_packet[offset + 4].toString(16), 2),
  60 + lpad(raw_packet[offset + 5].toString(16), 2)
  61 + ].join(":");
  62 + },
  63 + uint16: function (raw_packet, offset) {
  64 + return ((raw_packet[offset] * 256) + raw_packet[offset + 1]);
  65 + },
  66 + uint32: function (raw_packet, offset) {
  67 + return (
  68 + (raw_packet[offset] * 16777216) +
  69 + (raw_packet[offset + 1] * 65536) +
  70 + (raw_packet[offset + 2] * 256) +
  71 + raw_packet[offset + 3]
  72 + );
  73 + },
  74 + uint64: function (raw_packet, offset) {
  75 + return (
  76 + (raw_packet[offset] * 72057594037927936) +
  77 + (raw_packet[offset + 1] * 281474976710656) +
  78 + (raw_packet[offset + 2] * 1099511627776) +
  79 + (raw_packet[offset + 3] * 4294967296) +
  80 + (raw_packet[offset + 4] * 16777216) +
  81 + (raw_packet[offset + 5] * 65536) +
  82 + (raw_packet[offset + 6] * 256) +
  83 + raw_packet[offset + 7]
  84 + );
  85 + },
  86 + ipv4_addr: function (raw_packet, offset) {
  87 + return [
  88 + raw_packet[offset],
  89 + raw_packet[offset + 1],
  90 + raw_packet[offset + 2],
  91 + raw_packet[offset + 3]
  92 + ].join('.');
  93 + }
  94 +};
  95 +exports.unpack = unpack;
114 lib_tools.js
... ... @@ -0,0 +1,114 @@
  1 +/**
  2 + * Basic tools
  3 + *
  4 + * Add some useful features to Object, Array and Function, just call
  5 + * require('./lib_tools').setup(Object, Array, Function)
  6 + *
  7 + */
  8 +
  9 +function extend(dst, src) {
  10 + for(var i in src) dst[i] = src[i];
  11 + return dst;
  12 +}
  13 +
  14 +
  15 +/**
  16 +* Basic setup
  17 +*/
  18 +exports.setup = function(Object, Array, Function) {
  19 +
  20 +// Object methods
  21 +extend(Object, {
  22 + extend: extend,
  23 +
  24 + isArray: function(x) {
  25 + return !!x.__proto__.constructor.toString().match(/function Array/);
  26 + },
  27 +
  28 + values: function(x) {
  29 + var ret = [];
  30 + for(var i in x) ret.push(x[i]);
  31 + return ret;
  32 + },
  33 +
  34 + keys: function(x) {
  35 + var ret = [];
  36 + for(var i in x) ret.push(i);
  37 + return ret;
  38 + },
  39 +
  40 + clone: function(x) {
  41 + var ret = {};
  42 + for(var i in x) ret[i] = x[i];
  43 + return ret;
  44 + },
  45 +
  46 + cloneDeep: function(x) {
  47 + return JSON.parse(JSON.stringify(x));
  48 + },
  49 +});
  50 +
  51 +
  52 +// Array static methods
  53 +extend(Array, {
  54 + merge: function(a, b) {
  55 + return a.copy().merge(b);
  56 + },
  57 +
  58 + copy: function(a) {
  59 + return a.copy();
  60 + },
  61 +
  62 + is: function(a) {
  63 + return Object.isArray(a);
  64 + },
  65 +
  66 +});
  67 +
  68 +
  69 +// Array methods
  70 +extend(Array.prototype, {
  71 + merge: function(b) {
  72 + for(var i = 0; i < b.length; i++) this.push(b[i]);
  73 + return this;
  74 + },
  75 +
  76 + copy: function() {
  77 + return this.slice(0, this.length);
  78 + },
  79 +
  80 + include: function(object) {
  81 + return (this.indexOf(object) !== -1);
  82 + },
  83 +
  84 + uniq: function() {
  85 + var ret = [];
  86 + this.forEach(function(i) { if(ret.indexOf(i) === -1) ret.push(i); });
  87 + return ret;
  88 + },
  89 +
  90 + pluck: function(prop) {
  91 + return this.map(function(item) { return item && item[prop]; });
  92 + },
  93 +
  94 + compact: function() {
  95 + return this.filter(function(value) {
  96 + return value != null;
  97 + });
  98 + },
  99 +
  100 +
  101 +});
  102 +
  103 +
  104 +extend(Function.prototype, {
  105 + getClassName: function() {
  106 + try {
  107 + return this.__proto__.constructor.toString().match(/function (.*)\(/)[1];
  108 + }
  109 + catch(e) {}
  110 + },
  111 +});
  112 +
  113 +
  114 +} // eof: setup
47 tbase.js
... ... @@ -0,0 +1,47 @@
  1 +/**
  2 + * Basic Thrift
  3 + */
  4 +
  5 +// Field types
  6 +exports.types = {
  7 + "STOP" : 0,
  8 + "VOID" : 1,
  9 + "BOOL" : 2,
  10 + "BYTE" : 3,
  11 + "I08" : 3,
  12 + "DOUBLE" : 4,
  13 + "I16" : 6,
  14 + "I32" : 8,
  15 + "I64" : 10,
  16 + "STRING" : 11,
  17 + "UTF7" : 11,
  18 + "STRUCT" : 12,
  19 + "MAP" : 13,
  20 + "SET" : 14,
  21 + "LIST" : 15,
  22 + "UTF8" : 16,
  23 + "UTF16" : 17
  24 +};
  25 +
  26 +// Message types
  27 +exports.messageTypes = {
  28 + "CALL" : 1,
  29 + "REPLY" : 2,
  30 + "EXCEPTION" : 3
  31 +};
  32 +
  33 +
  34 +/**
  35 + * TProtocol:
  36 + * - init(transport)
  37 + * - write/read methods
  38 + *
  39 + *
  40 + * TTransport:
  41 + * - init(...)
  42 + * - write(buf)
  43 + * - flush()
  44 + * - read(len)
  45 + * - peek() -> bool - are some data available
  46 + * - readAll() - reads all data
  47 + */
340 tprotocol-json.js
... ... @@ -0,0 +1,340 @@
  1 +/**
  2 + * JSON protocol
  3 + */
  4 +require('./lib_tools').setup(Object, Array, Function); // Add useful stuff
  5 +var TBase = require('./tbase'),
  6 + types = TBase.types,
  7 + Class = require('./lib_class').Class,
  8 + FIFO = require('./lib_fifo').FIFO,
  9 + Buffer = require('buffer').Buffer,
  10 + _;
  11 +
  12 +
  13 +// Map of types
  14 +var mapType = exports.mapType = {};
  15 +mapType[types.BOOL ] = 'tf';
  16 +mapType[types.BYTE ] = 'i8';
  17 +mapType[types.I16 ] = 'i16';
  18 +mapType[types.I32 ] = 'i32';
  19 +mapType[types.I64 ] = 'i64';
  20 +mapType[types.DOUBLE ] = 'dbl';
  21 +mapType[types.STRUCT ] = 'rec';
  22 +mapType[types.STRING ] = 'str';
  23 +mapType[types.MAP ] = 'map';
  24 +mapType[types.LIST ] = 'lst';
  25 +mapType[types.SET ] = 'set';
  26 +
  27 +// Create reversed map of types
  28 +var mapRType = exports.mapRType = {};
  29 +for(var i in mapType) mapRType[mapType[i]] = i;
  30 +
  31 +// Comma reference
  32 +var comma = {};
  33 +
  34 +// Write as JSON string
  35 +function str(x) {
  36 + return JSON.stringify(String(x));
  37 +}
  38 +function toVal(x) {
  39 + return { value: x };
  40 +}
  41 +
  42 +var Proto = exports.ThriftJSONProtocol = Class({
  43 + /**
  44 + * Initializes new protocol
  45 + * @param transport
  46 + */
  47 + init: function(transport) {
  48 + this.transport = transport;
  49 + this.context = [ [ 'TOP' ] ];
  50 +
  51 + this.fifo = null; // Output queue (which can serve as buffer as well)
  52 + this.incoming = null;
  53 + },
  54 +
  55 + // Protocol version
  56 + version: 1,
  57 +
  58 +
  59 + /************* Write methods **********/
  60 +
  61 + /**
  62 + * Write all arguments to buffer
  63 + * @return int Number of written bytes
  64 + */
  65 + _write: function() {
  66 + var item, sum = 0;
  67 +
  68 + // We're writing map, writing key, and we're not the first one
  69 + if(this.context[0][0] === 'map' && this.context[0][1] && this.context[0][2]++ > 0) sum += this.fifo.write(', ');
  70 +
  71 + for(var i = 0; i < arguments.length; i++) {
  72 + var item = arguments[i];
  73 +
  74 + // Convert to string if needed
  75 + if(item === comma) item = ', ';
  76 + else if(item instanceof Buffer) { }
  77 + else if(typeof item !== 'string') item = String(item);
  78 +
  79 + sum += this.fifo.write(item);
  80 + }
  81 +
  82 + // We are in map
  83 + if(this.context[0][0] == 'map') {
  84 + if(!(this.context[0][1] = !this.context[0][1])) {
  85 + sum += this.fifo.write(': ');
  86 + }
  87 + }
  88 +
  89 + return sum;
  90 + },
  91 +
  92 + writeMessageBegin: function(name, messageType, seqid) {
  93 + this.fifo = new FIFO; // Initialize new buffer
  94 + return this._write('[ ', this.version, comma, str(name), comma, messageType, comma, seqid, comma);
  95 + },
  96 +
  97 + writeMessageEnd: function() {
  98 + var x = this._write(' ]');
  99 +
  100 + // Write to transport and flush it
  101 + this.transport.write(this.fifo.peek());
  102 + this.transport.flush();
  103 +
  104 + return x;
  105 + },
  106 +
  107 + writeStructBegin: function(name) {
  108 + this.context.unshift([ 'struct', 0 ]); // Struct + counter
  109 + return this._write('/** struct ' + name + '*/ { ');
  110 + },
  111 +
  112 + writeStructureEnd: function() {
  113 + this.context.shift();
  114 +
  115 + return this._write(' } ');
  116 + },
  117 +
  118 + writeFieldBegin: function(name, fieldType, fieldId) {
  119 + return this._write(
  120 + (this.context[0][1]++ ? ', ' : '') +
  121 + ' { ' + str(fieldId) + ': { ' + str(mapType[fieldType]) + ': '
  122 + );
  123 + },
  124 +
  125 + writeFieldEnd: function() {
  126 + return this._write(' } } ');
  127 + },
  128 +
  129 + writeFieldStop: function() {
  130 + // nothing to do
  131 + },
  132 +
  133 + writeMapBegin: function(keyType, valType, size) {
  134 + this.context.unshift( [ 'map', true, 0 ] ); // Map + writingKey? + counter
  135 + return this._write(' [ ', str(mapType[keyType]), comma, str(mapType[valType]), comma, size, comma, ' { ');
  136 + },
  137 +
  138 + writeMapEnd: function() {
  139 + this.context.shift();
  140 + return this._write(' } ] ');
  141 + },
  142 +
  143 + writeListBegin: function(elemType, size) {
  144 + this.context.unshift('list');
  145 + return this._write(' [ ', str(mapType(elemType)), comma, size, comma);
  146 + },
  147 +
  148 + writeListEnd: function() {
  149 + this.context.shift();
  150 + return this._write(' ] ');
  151 + },
  152 +
  153 + writeSetBegin: function(elemType, size) {
  154 + this.context.unshift('set');
  155 + return this._write(' [ ', str(mapType(elemType)), comma, size, comma);
  156 + },
  157 +
  158 + writeSetEnd: function() {
  159 + this.context.shift();
  160 + return this._write(' ] ');
  161 + },
  162 +
  163 + writeBool: function(value) {
  164 + return this._write(value ? 1 : 0);
  165 + },
  166 +
  167 + writeByte: function(i8) {
  168 + return this._write(i8);
  169 + },
  170 +
  171 + writeI16 : function(i16){
  172 + return this._write(i16);
  173 + },
  174 +
  175 + writeI32 : function(i32){
  176 + return this._write(i32);
  177 + },
  178 +
  179 + writeI64 : function(i64){
  180 + return this._write(i64);
  181 + },
  182 +
  183 + writeDouble : function(dbl){
  184 + return this._write(dbl);
  185 + },
  186 +
  187 + writeString: function(x) {
  188 + return this._write(str(x))
  189 + },
  190 +
  191 + writeBinary: function(x) {
  192 + // TODO: encode base64
  193 + return this._write(x);
  194 + },
  195 +
  196 +
  197 +
  198 + /************* Read methods **********/
  199 +
  200 + readMessageBegin: function() {
  201 + // Parse incoming JSON object
  202 + var buf = JSON.parse(this.transport.readAll());
  203 +
  204 + if(buf[0] != this.version) throw new Error('Unknown version: ' + buf[0]);
  205 +
  206 + var ret = {
  207 + version: buf[0],
  208 + fname: buf[1],
  209 + mtype: buf[2],
  210 + rseqid: buf[3]
  211 + };
  212 +
  213 + // Populate rest
  214 + this.incoming = buf.slice(4);
  215 +
  216 + return ret;
  217 + },
  218 +
  219 + readMessageEnd: function() {
  220 + // nothing to do
  221 + },
  222 +
  223 + readStructBegin: function() {
  224 + this.context.unshift([ 'struct', this.incoming.shift() ]); // Struct + contents as object
  225 + return { fname: '' }; // Name it not there
  226 + },
  227 +
  228 + readStructureEnd: function() {
  229 + this.context.shift();
  230 + },
  231 +
  232 + readFieldBegin: function() {
  233 + // Take first key from object
  234 + for(var fId in this.context[0][1]) break;
  235 + if(typeof fId === 'undefined') throw new Error('Key is missing');
  236 +
  237 + // Take type
  238 + for(var fType in this.context[0][1][fId]) break;
  239 +
  240 + var ret = {
  241 + fid: fId,
  242 + ftype: mapRType[fType],
  243 + fname: ''
  244 + };
  245 +
  246 + // Put value back to queue
  247 + this.incoming.unshift(this.context[0][1][fId][fType]);
  248 +
  249 + // Remote it from struct-context
  250 + delete this.context[0][1][fId];
  251 +
  252 + return ret;
  253 + },
  254 +
  255 + readFieldEnd: function() {
  256 + // n/a
  257 + },
  258 +
  259 + readMapBegin: function() {
  260 + var map = this.incoming.shift();
  261 + var ret = {
  262 + ktype: mapRType[map[0]],
  263 + vtype: mapRType[map[1]],
  264 + size: map[2]
  265 + };
  266 +
  267 + // Make simple list from rest of it
  268 + var contents = map[3], list = [];
  269 + for(var i in contents) {
  270 + list.push(i, contents[i]);
  271 + }
  272 +
  273 + // Put this list at the beginning of incoming
  274 + Array.prototype.unshift.apply(this.incoming, list);
  275 +
  276 + return ret;
  277 + },
  278 +
  279 + readMapEnd: function() {
  280 + // n/a
  281 + },
  282 +
  283 + readListBegin: function() {
  284 + var lst = this.incoming.shift();
  285 + var ret = {
  286 + etype: mapRType[lst[0]],
  287 + size: lst[1]
  288 + };
  289 +
  290 + // Put this list at the beginning of incoming
  291 + Array.prototype.unshift.apply(this.incoming, list.slice(2));
  292 +
  293 + return ret;
  294 + },
  295 +
  296 + readListEnd: function() {
  297 + // n/a
  298 + },
  299 +
  300 + readSetBegin: function(elemType, size) {
  301 + return this.readListBegin();
  302 + },
  303 +
  304 + readSetEnd: function() {
  305 + return this.readListEnd();
  306 + },
  307 +
  308 + readBool: function(value) {
  309 + return toVal(this.incoming.shift() ? true : false);
  310 + },
  311 +
  312 + readByte: function(i8) {
  313 + return toVal(this.incoming.shift());
  314 + },
  315 +
  316 + readI16 : function(i16){
  317 + return toVal(this.incoming.shift());
  318 + },
  319 +
  320 + readI32 : function(i32){
  321 + return toVal(this.incoming.shift());
  322 + },
  323 +
  324 + readI64 : function(i64){
  325 + return toVal(this.incoming.shift());
  326 + },
  327 +
  328 + readDouble : function(dbl){
  329 + return toVal(this.incoming.shift());
  330 + },
  331 +
  332 + readString: function(x) {
  333 + return toVal(this.incoming.shift());
  334 + },
  335 +
  336 + readBinary: function(x) {
  337 + // TODO: decode base64
  338 + return toVal(this.incoming.shift());
  339 + }
  340 +});

0 comments on commit 25609f9

Please sign in to comment.
Something went wrong with that request. Please try again.