Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Revert "upgraded node-thrift to support utf8"

This reverts commit 5b58e06.
  • Loading branch information...
commit fd6979a5ae392f3218784089aa2c06236a9261fa 1 parent 5b58e06
@Jae Jae authored
View
197 node_modules/node-int64/Int64.js
@@ -1,197 +0,0 @@
-/**
- * Support for handling 64-bit int numbers in Javascript (node.js)
- *
- * JS Numbers are IEEE-754 binary double-precision floats, which limits the
- * range of values that can be represented with integer precision to:
- *
- * 2^^53 <= N <= 2^53
- *
- * Int64 objects wrap a node Buffer that holds the 8-bytes of int64 data. These
- * objects operate directly on the buffer which means that if they are created
- * using an existing buffer then setting the value will modify the Buffer, and
- * vice-versa.
- *
- * Internal Representation
- *
- * The internal buffer format is Big Endian. I.e. the most-significant byte is
- * at buffer[0], the least-significant at buffer[7]. For the purposes of
- * converting to/from JS native numbers, the value is assumed to be a signed
- * integer stored in 2's complement form.
- *
- * For details about IEEE-754 see:
- * http://en.wikipedia.org/wiki/Double_precision_floating-point_format
- */
-
-// Useful masks and values for bit twiddling
-var MASK31 = 0x7fffffff, VAL31 = 0x80000000;
-var MASK32 = 0xffffffff, VAL32 = 0x100000000;
-
-// Map for converting hex octets to strings
-var _HEX = [];
-for (var i = 0; i < 256; i++) {
- _HEX[i] = (i > 0xF ? '' : '0') + i.toString(16);
-}
-
-//
-// Int64
-//
-
-/**
- * Constructor accepts any of the following argument types:
- *
- * new Int64(buffer[, offset=0]) - Existing Buffer with byte offset
- * new Int64(string) - Hex string (throws if n is outside int64 range)
- * new Int64(number) - Number (throws if n is outside int64 range)
- * new Int64(hi, lo) - Raw bits as two 32-bit values
- */
-var Int64 = module.exports = function(a1, a2) {
- if (a1 instanceof Buffer) {
- this.buffer = a1;
- this.offset = a2 || 0;
- } else {
- this.buffer = this.buffer || new Buffer(8);
- this.offset = 0;
- this.setValue.apply(this, arguments);
- }
-};
-
-
-// Max integer value that JS can accurately represent
-Int64.MAX_INT = Math.pow(2, 53);
-
-// Min integer value that JS can accurately represent
-Int64.MIN_INT = -Math.pow(2, 53);
-
-Int64.prototype = {
- /**
- * Do in-place 2's compliment. See
- * http://en.wikipedia.org/wiki/Two's_complement
- */
- _2scomp: function() {
- var b = this.buffer, o = this.offset, carry = 1;
- for (var i = o + 7; i >= o; i--) {
- var v = (b[i] ^ 0xff) + carry;
- b[i] = v & 0xff;
- carry = v >> 8;
- }
- },
-
- /**
- * Set the value. Takes any of the following arguments:
- *
- * setValue(string) - A hexidecimal string
- * setValue(number) - Number (throws if n is outside int64 range)
- * setValue(hi, lo) - Raw bits as two 32-bit values
- */
- setValue: function(hi, lo) {
- var negate = false;
- if (arguments.length == 1) {
- if (typeof(hi) == 'number') {
- // Simplify bitfield retrieval by using abs() value. We restore sign
- // later
- negate = hi < 0;
- hi = Math.abs(hi);
- lo = hi % VAL32;
- hi = hi / VAL32;
- if (hi > VAL32) throw new RangeError(hi + ' is outside Int64 range');
- hi = hi | 0;
- } else if (typeof(hi) == 'string') {
- hi = (hi + '').replace(/^0x/, '');
- lo = hi.substr(-8);
- hi = hi.length > 8 ? hi.substr(0, hi.length - 8) : '';
- hi = parseInt(hi, 16);
- lo = parseInt(lo, 16);
- } else {
- throw new Error(hi + ' must be a Number or String');
- }
- }
-
- // Technically we should throw if hi or lo is outside int32 range here, but
- // it's not worth the effort. Anything past the 32'nd bit is ignored.
-
- // Copy bytes to buffer
- var b = this.buffer, o = this.offset;
- for (var i = 7; i >= 0; i--) {
- b[o+i] = lo & 0xff;
- lo = i == 4 ? hi : lo >>> 8;
- }
-
- // Restore sign of passed argument
- if (negate) this._2scomp();
- },
-
- /**
- * Convert to a native JS number.
- *
- * WARNING: Do not expect this value to be accurate to integer precision for
- * large (positive or negative) numbers!
- *
- * @param allowImprecise If true, no check is performed to verify the
- * returned value is accurate to integer precision. If false, imprecise
- * numbers (very large positive or negative numbers) will be forced to +/-
- * Infinity.
- */
- toNumber: function(allowImprecise) {
- var b = this.buffer, o = this.offset;
-
- // Running sum of octets, doing a 2's complement
- var negate = b[0] & 0x80, x = 0, carry = 1;
- for (var i = 7, m = 1; i >= 0; i--, m *= 256) {
- var v = b[o+i];
-
- // 2's complement for negative numbers
- if (negate) {
- v = (v ^ 0xff) + carry;
- carry = v >> 8;
- v = v & 0xff;
- }
-
- x += v * m;
- }
-
- // Return Infinity if we've lost integer precision
- if (!allowImprecise && x >= Int64.MAX_INT) {
- return negate ? -Infinity : Infinity;
- }
-
- return negate ? -x : x;
- },
-
- /**
- * Convert to a JS Number. Returns +/-Infinity for values that can't be
- * represented to integer precision.
- */
- valueOf: function() {
- return this.toNumber(false);
- },
-
- /**
- * Return string value
- *
- * @param radix Just like Number#toString()'s radix
- */
- toString: function(radix) {
- return this.valueOf().toString(radix || 10);
- },
-
- /**
- * Return a string showing the buffer octets, with MSB on the left.
- *
- * @param sep separator string. default is '' (empty string)
- */
- toOctetString: function(sep) {
- var out = new Array(8);
- var b = this.buffer, o = this.offset;
- for (var i = 0; i < 8; i++) {
- out[i] = _HEX[b[o+i]];
- }
- return out.join(sep || '');
- },
-
- /**
- * Pretty output in console.log
- */
- inspect: function() {
- return '[Int64 value:' + this + ' octets:' + this.toOctetString(' ') + ']';
- }
-};
View
69 node_modules/node-int64/README.md
@@ -1,69 +0,0 @@
-JavaScript Numbers are represented as [IEEE 754 double-precision floats](http://steve.hollasch.net/cgindex/coding/ieeefloat.html). Unfortunately, this means they lose integer precision for values beyond +/- 2^^53. For projects that need to accurately handle 64-bit ints, such as [node-thrift](https://github.com/wadey/node-thrift), a performant, Number-like class is needed. Int64 is that class.
-
-Int64 instances look and feel much like JS-native Numbers. By way of example ...
-
- // First, let's illustrate the problem ...
- > (0x123456789).toString(16)
- '123456789' // <- what we expect.
- > (0x123456789abcdef0).toString(16)
- '123456789abcdf00' // <- Ugh! JS doesn't do big ints. :(
-
- // So let's create a couple Int64s using the above values ...
-
- // Require, of course
- > Int64 = require('node-int64')
-
- // x's value is what we expect (the decimal value of 0x123456789)
- > x = new Int64(0x123456789)
- [Int64 value:4886718345 octets:00 00 00 01 23 45 67 89]
-
- // y's value is Infinity because it's outside the range of integer
- // precision. But that's okay - it's still useful because it's internal
- // representation (octets) is what we passed in
- > y = new Int64('123456789abcdef0')
- [Int64 value:Infinity octets:12 34 56 78 9a bc de f0]
-
- // Let's do some math. Int64's behave like Numbers. (Sorry, Int64 isn't
- // for doing 64-bit integer arithmetic (yet) - it's just for carrying
- // around int64 values
- > x + 1
- 4886718346
- > y + 1
- Infinity
-
- // Int64 string operations ...
- > 'value: ' + x
- 'value: 4886718345'
- > 'value: ' + y
- 'value: Infinity'
- > x.toString(2)
- '100100011010001010110011110001001'
- > y.toString(2)
- 'Infinity'
-
- // Use JS's isFinite() method to see if the Int64 value is in the
- // integer-precise range of JS values
- > isFinite(x)
- true
- > isFinite(y)
- false
-
- // Get an octet string representation. (Yay, y is what we put in!)
- > x.toOctetString()
- '0000000123456789'
- > y.toOctetString()
- '123456789abcdef0'
-
- // Finally, some other ways to create Int64s ...
-
- // Pass hi/lo words
- > new Int64(0x12345678, 0x9abcdef0)
- [Int64 value:Infinity octets:12 34 56 78 9a bc de f0]
-
- // Pass a Buffer
- > new Int64(new Buffer([0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0]))
- [Int64 value:Infinity octets:12 34 56 78 9a bc de f0]
-
- // Pass a Buffer and offset
- > new Int64(new Buffer([0,0,0,0,0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0]), 4)
- [Int64 value:Infinity octets:12 34 56 78 9a bc de f0]
View
12 node_modules/node-int64/package.json
@@ -1,12 +0,0 @@
-{
- "name" : "node-int64",
- "description" : "Support for representing 64-bit integers in JavaScript",
- "url" : "http://github.com/broofa/node-int64",
- "keywords" : ["math", "integer", "int64"],
- "author" : "Robert Kieffer <robert@broofa.com>",
- "contributors" : [],
- "dependencies" : [],
- "lib" : ".",
- "main" : "./Int64.js",
- "version" : "0.3.0"
-}
View
31 node_modules/node-int64/test.js
@@ -1,31 +0,0 @@
-var assert = require('assert');
-var Int64 = require('./Int64');
-
-var args = [
- [0], '0000000000000000', 0,
- [1], '0000000000000001', 1,
- [-1], 'ffffffffffffffff', -1,
- [1e18], '0de0b6b3a7640000', 1e18,
- ['0001234500654321'], '0001234500654321', 0x1234500654321,
- ['0ff1234500654321'], '0ff1234500654321', 0xff1234500654300, // Imprecise!
- [0xff12345, 0x654321], '0ff1234500654321', 0xff1234500654300, // Imprecise!
- [0xfffaffff, 0xfffff700],'fffafffffffff700', -0x5000000000900,
- [0xafffffff, 0xfffff700],'affffffffffff700', -0x5000000000000800, // Imprecise!
- ['0x0000123450654321'], '0000123450654321', 0x123450654321,
- ['0xFFFFFFFFFFFFFFFF'], 'ffffffffffffffff', -1
-];
-
-// Test constructor argments
-
-for (var i = 0; i < args.length; i += 3) {
- var a = args[i], octets = args[i+1], number = args[i+2];
- console.log('Testing ' + a.join(', '));
- // Create instance
- var x = new Int64();
- Int64.apply(x, a);
-
- assert.equal(x.toOctetString(), octets,
- 'Constuctor with ' + args.join(', '));
-
- assert.equal(x.toNumber(true), number);
-}
View
145 node_modules/thrift/binary.js
@@ -1,145 +0,0 @@
-var POW_8 = Math.pow(2, 8)
-var POW_16 = Math.pow(2, 16)
-var POW_24 = Math.pow(2, 24)
-var POW_32 = Math.pow(2, 32)
-var POW_40 = Math.pow(2, 40)
-var POW_48 = Math.pow(2, 48)
-var POW_52 = Math.pow(2, 52)
-var POW_1022 = Math.pow(2, 1022)
-
-exports.readI16 = function(buff, off) {
- off = off || 0;
- var v = buff[off + 1];
- v += buff[off] << 8;
- if (buff[off] & 128) {
- v -= POW_16;
- }
- return v;
-}
-
-exports.readI32 = function(buff, off) {
- off = off || 0;
- var v = buff[off + 3];
- v += buff[off + 2] << 8;
- v += buff[off + 1] << 16;
- v += buff[off] * POW_24;
- if (buff[off] & 0x80) {
- v -= POW_32;
- }
- return v;
-}
-
-exports.writeI16 = function(buff, v) {
- buff[1] = v & 0xff;
- v >>= 8;
- buff[0] = v & 0xff;
- return buff;
-}
-
-exports.writeI32 = function(buff, v) {
- buff[3] = v & 0xff;
- v >>= 8;
- buff[2] = v & 0xff;
- v >>= 8;
- buff[1] = v & 0xff;
- v >>= 8;
- buff[0] = v & 0xff;
- return buff;
-}
-
-exports.readDouble = function(buff, off) {
- off = off || 0;
- var signed = buff[off] & 0x80;
- var e = (buff[off+1] & 0xF0) >> 4;
- e += (buff[off] & 0x7F) << 4;
-
- var m = buff[off+7];
- m += buff[off+6] << 8;
- m += buff[off+5] << 16;
- m += buff[off+4] * POW_24;
- m += buff[off+3] * POW_32;
- m += buff[off+2] * POW_40;
- m += (buff[off+1] & 0x0F) * POW_48;
-
- switch (e) {
- case 0:
- e = -1022
- break;
- case 2047:
- return m ? NaN : (signed ? -Infinity : Infinity);
- default:
- m += POW_52;
- e -= 1023;
- }
-
- if (signed) {
- m *= -1;
- }
-
- return m * Math.pow(2, e - 52);
-}
-
-/*
- * Based on code from the jspack module:
- * http://code.google.com/p/jspack/
- */
-exports.writeDouble = function(buff, v) {
- var m, e, c;
-
- buff[0] = (v < 0 ? 0x80 : 0x00)
-
- v = Math.abs(v)
- if (v !== v) {
- // NaN, use QNaN IEEE format
- m = 2251799813685248;
- e = 2047;
- } else if (v === Infinity) {
- m = 0;
- e = 2047;
- } else {
- e = Math.floor(Math.log(v) / Math.LN2)
- c = Math.pow(2, -e)
- if (v * c < 1) {
- e--;
- c *= 2;
- }
-
- if (e + 1023 >= 2047)
- {
- // Overflow
- m = 0;
- e = 2047;
- }
- else if (e + 1023 >= 1)
- {
- // Normalized - term order matters, as Math.pow(2, 52-e) and v*Math.pow(2, 52) can overflow
- m = (v*c-1) * POW_52;
- e += 1023;
- }
- else
- {
- // Denormalized - also catches the '0' case, somewhat by chance
- m = (v * POW_1022) * POW_52;
- e = 0;
- }
- }
-
- buff[1] = (e << 4) & 0xf0;
- buff[0] |= (e >> 4) & 0x7f;
-
- buff[7] = m & 0xff;
- m = Math.floor(m / POW_8);
- buff[6] = m & 0xff;
- m = Math.floor(m / POW_8);
- buff[5] = m & 0xff;
- m = Math.floor(m / POW_8);
- buff[4] = m & 0xff;
- m >>= 8;
- buff[3] = m & 0xff;
- m >>= 8;
- buff[2] = m & 0xff;
- m >>= 8;
- buff[1] |= m & 0x0f;
-
- return buff;
-}
View
261 node_modules/thrift/binary_parser.js
@@ -0,0 +1,261 @@
+// Adapted from: http://github.com/christkv/node-mongodb-native/raw/master/lib/mongodb/bson/binary_parser.js
+
+var sys = require('sys');
+
+//+ Jonas Raoni Soares Silva
+//@ http://jsfromhell.com/classes/binary-parser [v1.0]
+var chr = String.fromCharCode;
+
+var p = exports.BinaryParser = function( bigEndian, allowExceptions ){
+ this.bigEndian = bigEndian || true;
+ this.allowExceptions = allowExceptions;
+};
+p.bigEndian = true;
+
+var IBuffer = exports.BinaryParser.IBuffer = function( bigEndian, buffer ){
+ this.bigEndian = bigEndian || 1;
+ this.buffer = [];
+ this.setBuffer( buffer );
+};
+
+IBuffer.prototype.setBuffer = function( data ){
+ if( data ){
+ for( var l, i = l = data.length, b = this.buffer = new Array( l ); i; b[l - i] = data[--i] );
+ this.bigEndian && b.reverse();
+ }
+};
+
+IBuffer.prototype.hasNeededBits = function( neededBits ){
+ return this.buffer.length >= -( -neededBits >> 3 );
+};
+
+IBuffer.prototype.checkBuffer = function( neededBits ){
+ if( !this.hasNeededBits( neededBits ) ) {
+ console.log("missing: " + neededBits + " " + this.buffer.length);
+ throw new Error( "checkBuffer::missing bytes" );
+ }
+};
+
+IBuffer.prototype.readBits = function( start, length ){
+ //shl fix: Henri Torgemane ~1996 (compressed by Jonas Raoni)
+ function shl( a, b ){
+ for( ; b--; a = ( ( a %= 0x7fffffff + 1 ) & 0x40000000 ) == 0x40000000 ? a * 2 : ( a - 0x40000000 ) * 2 + 0x7fffffff + 1 );
+ return a;
+ }
+ if( start < 0 || length <= 0 )
+ return 0;
+ this.checkBuffer( start + length );
+ for( var offsetLeft, offsetRight = start % 8, curByte = this.buffer.length - ( start >> 3 ) - 1, lastByte = this.buffer.length + ( -( start + length ) >> 3 ), diff = curByte - lastByte, sum = ( ( this.buffer[ curByte ] >> offsetRight ) & ( ( 1 << ( diff ? 8 - offsetRight : length ) ) - 1 ) ) + ( diff && ( offsetLeft = ( start + length ) % 8 ) ? ( this.buffer[ lastByte++ ] & ( ( 1 << offsetLeft ) - 1 ) ) << ( diff-- << 3 ) - offsetRight : 0 ); diff; sum += shl( this.buffer[ lastByte++ ], ( diff-- << 3 ) - offsetRight ) );
+ return sum;
+};
+
+p.warn = function( msg ){
+ if( this.allowExceptions )
+ throw new Error( msg );
+ return 1;
+};
+p.decodeFloat = function( data, precisionBits, exponentBits ){
+ var b = new this.IBuffer( this.bigEndian, data );
+ b.checkBuffer( precisionBits + exponentBits + 1 );
+ var bias = Math.pow( 2, exponentBits - 1 ) - 1, signal = b.readBits( precisionBits + exponentBits, 1 ), exponent = b.readBits( precisionBits, exponentBits ), significand = 0,
+ divisor = 2, curByte = b.buffer.length + ( -precisionBits >> 3 ) - 1;
+ do{
+ for( var byteValue = b.buffer[ ++curByte ], startBit = precisionBits % 8 || 8, mask = 1 << startBit; mask >>= 1; ( byteValue & mask ) && ( significand += 1 / divisor ), divisor *= 2 );
+ }while( precisionBits -= startBit );
+ return exponent == ( bias << 1 ) + 1 ? significand ? NaN : signal ? -Infinity : +Infinity : ( 1 + signal * -2 ) * ( exponent || significand ? !exponent ? Math.pow( 2, -bias + 1 ) * significand : Math.pow( 2, exponent - bias ) * ( 1 + significand ) : 0 );
+};
+p.decodeInt = function( data, bits, signed, forceBigEndian ){
+ //console.log("decodeInt: ", data, bits, signed);
+ var b = new this.IBuffer( this.bigEndian||forceBigEndian, data ), x = b.readBits( 0, bits ), max = Math.pow( 2, bits );
+ return signed && x >= max / 2 ? x - max : x;
+};
+p.encodeFloat = function( data, precisionBits, exponentBits ){
+ var bias = Math.pow( 2, exponentBits - 1 ) - 1, minExp = -bias + 1, maxExp = bias, minUnnormExp = minExp - precisionBits,
+ status = isNaN( n = parseFloat( data ) ) || n == -Infinity || n == +Infinity ? n : 0,
+ exp = 0, len = 2 * bias + 1 + precisionBits + 3, bin = new Array( len ),
+ signal = ( n = status !== 0 ? 0 : n ) < 0, n = Math.abs( n ), intPart = Math.floor( n ), floatPart = n - intPart,
+ i, lastBit, rounded, j, result;
+ for( i = len; i; bin[--i] = 0 );
+ for( i = bias + 2; intPart && i; bin[--i] = intPart % 2, intPart = Math.floor( intPart / 2 ) );
+ for( i = bias + 1; floatPart > 0 && i; ( bin[++i] = ( ( floatPart *= 2 ) >= 1 ) - 0 ) && --floatPart );
+ for( i = -1; ++i < len && !bin[i]; );
+ if( bin[( lastBit = precisionBits - 1 + ( i = ( exp = bias + 1 - i ) >= minExp && exp <= maxExp ? i + 1 : bias + 1 - ( exp = minExp - 1 ) ) ) + 1] ){
+ if( !( rounded = bin[lastBit] ) ){
+ for( j = lastBit + 2; !rounded && j < len; rounded = bin[j++] );
+ }
+ for( j = lastBit + 1; rounded && --j >= 0; ( bin[j] = !bin[j] - 0 ) && ( rounded = 0 ) );
+ }
+ for( i = i - 2 < 0 ? -1 : i - 3; ++i < len && !bin[i]; );
+ if( ( exp = bias + 1 - i ) >= minExp && exp <= maxExp )
+ ++i;
+ else if( exp < minExp ){
+ exp != bias + 1 - len && exp < minUnnormExp && this.warn( "encodeFloat::float underflow" );
+ i = bias + 1 - ( exp = minExp - 1 );
+ }
+ if( intPart || status !== 0 ){
+ this.warn( intPart ? "encodeFloat::float overflow" : "encodeFloat::" + status );
+ exp = maxExp + 1;
+ i = bias + 2;
+ if( status == -Infinity )
+ signal = 1;
+ else if( isNaN( status ) )
+ bin[i] = 1;
+ }
+ for( n = Math.abs( exp + bias ), j = exponentBits + 1, result = ""; --j; result = ( n % 2 ) + result, n = n >>= 1 );
+ for( n = 0, j = 0, i = ( result = ( signal ? "1" : "0" ) + result + bin.slice( i, i + precisionBits ).join( "" ) ).length, r = []; i; j = ( j + 1 ) % 8 ){
+ n += ( 1 << j ) * result.charAt( --i );
+ if( j == 7 ){
+ r[r.length] = n;
+ n = 0;
+ }
+ }
+ if (n) {
+ r[r.length] = n;
+ }
+ return new Buffer( this.bigEndian ? r.reverse() : r );
+};
+p.encodeInt = function( data, bits, signed, forceBigEndian ){
+ var max = Math.pow( 2, bits );
+ ( data >= max || data < -( max / 2 ) ) && this.warn( "encodeInt::overflow" ) && ( data = 0 );
+ data < 0 && ( data += max );
+ for( var r = []; data; r[r.length] = data % 256, data = Math.floor( data / 256 ) );
+ for( bits = -( -bits >> 3 ) - r.length; bits--; r[r.length] = 0 );
+ return new Buffer((this.bigEndian||forceBigEndian) ? r.reverse() : r );
+};
+p.toSmall = function( data ){ return this.decodeInt( data, 8, true ); };
+p.fromSmall = function( data ){ return this.encodeInt( data, 8, true ); };
+p.toByte = function( data ){ return this.decodeInt( data, 8, false ); };
+p.fromByte = function( data ){ return this.encodeInt( data, 8, false ); };
+p.toShort = function( data ){ return this.decodeInt( data, 16, true ); };
+p.fromShort = function( data ){ return this.encodeInt( data, 16, true ); };
+p.toWord = function( data ){ return this.decodeInt( data, 16, false ); };
+p.fromWord = function( data ){ return this.encodeInt( data, 16, false ); };
+p.toInt = function( data ){ return this.decodeInt( data, 32, true ); };
+p.fromInt = function( data ){ return this.encodeInt( data, 32, true ); };
+p.toLong = function( data ){ return this.decodeInt( data, 64, true ); };
+p.fromLong = function( data ){ return this.encodeInt( data, 64, true ); };
+p.toDWord = function( data ){ return this.decodeInt( data, 32, false ); };
+p.fromDWord = function( data ){ return this.encodeInt( data, 32, false ); };
+p.toQWord = function( data ){ return this.decodeInt( data, 64, true ); };
+p.fromQWord = function( data ){ return this.encodeInt( data, 64, true ); };
+p.toFloat = function( data ){ return this.decodeFloat( data, 23, 8 ); };
+p.fromFloat = function( data ){ return this.encodeFloat( data, 23, 8 ); };
+p.toDouble = function( data ){ return this.decodeFloat( data, 52, 11 ); };
+p.fromDouble = function( data ){ return this.encodeFloat( data, 52, 11 ); };
+
+// Factor out the encode so it can be shared by add_header and push_int32
+p.encode_int32 = function(number) {
+ var a, b, c, d, unsigned;
+ unsigned = (number < 0) ? (number + 0x100000000) : number;
+ a = Math.floor(unsigned / 0xffffff);
+ unsigned &= 0xffffff;
+ b = Math.floor(unsigned / 0xffff);
+ unsigned &= 0xffff;
+ c = Math.floor(unsigned / 0xff);
+ unsigned &= 0xff;
+ d = Math.floor(unsigned);
+ return chr(a) + chr(b) + chr(c) + chr(d);
+};
+
+p.encode_int64 = function(number) {
+ var a, b, c, d, e, f, g, h, unsigned;
+ unsigned = (number < 0) ? (number + 0x10000000000000000) : number;
+ a = Math.floor(unsigned / 0xffffffffffffff);
+ unsigned &= 0xffffffffffffff;
+ b = Math.floor(unsigned / 0xffffffffffff);
+ unsigned &= 0xffffffffffff;
+ c = Math.floor(unsigned / 0xffffffffff);
+ unsigned &= 0xffffffffff;
+ d = Math.floor(unsigned / 0xffffffff);
+ unsigned &= 0xffffffff;
+ e = Math.floor(unsigned / 0xffffff);
+ unsigned &= 0xffffff;
+ f = Math.floor(unsigned / 0xffff);
+ unsigned &= 0xffff;
+ g = Math.floor(unsigned / 0xff);
+ unsigned &= 0xff;
+ h = Math.floor(unsigned);
+ return chr(a) + chr(b) + chr(c) + chr(d) + chr(e) + chr(f) + chr(g) + chr(h);
+};
+
+/**
+ UTF8 methods
+**/
+
+// Take a raw binary string and return a utf8 string
+p.decode_utf8 = function(a) {
+ var string = "";
+ var i = 0;
+ var c = c1 = c2 = 0;
+
+ while ( i < a.length ) {
+ c = a.charCodeAt(i);
+ if (c < 128) {
+ string += String.fromCharCode(c);
+ i++;
+ } else if((c > 191) && (c < 224)) {
+ c2 = a.charCodeAt(i+1);
+ string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
+ i += 2;
+ } else {
+ c2 = a.charCodeAt(i+1);
+ c3 = a.charCodeAt(i+2);
+ string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
+ i += 3;
+ }
+ }
+ return string;
+};
+
+// Encode a cstring correctly
+p.encode_cstring = function(s) {
+ return unescape(encodeURIComponent(s)) + p.fromByte(0);
+};
+
+// Take a utf8 string and return a binary string
+p.encode_utf8 = function(s) {
+ var a="";
+ for (var n=0; n< s.length; n++) {
+ var c=s.charCodeAt(n);
+ if (c<128) {
+ a += String.fromCharCode(c);
+ } else if ((c>127)&&(c<2048)) {
+ a += String.fromCharCode( (c>>6) | 192) ;
+ a += String.fromCharCode( (c&63) | 128);
+ } else {
+ a += String.fromCharCode( (c>>12) | 224);
+ a += String.fromCharCode( ((c>>6) & 63) | 128);
+ a += String.fromCharCode( (c&63) | 128);
+ }
+ }
+ return a;
+};
+
+p.hprint = function(s) {
+ for (var i=0; i<s.length; i++) {
+ if (s.charCodeAt(i)<32) {
+ var number = s.charCodeAt(i) <= 15 ? "0" + s.charCodeAt(i).toString(16) : s.charCodeAt(i).toString(16);
+ sys.debug(number+' : ');}
+ else {
+ var number = s.charCodeAt(i) <= 15 ? "0" + s.charCodeAt(i).toString(16) : s.charCodeAt(i).toString(16);
+ sys.debug(number+' : '+ s.charAt(i));}
+ }
+};
+
+p.to_byte_array = function(s) {
+ var array = [];
+
+ for (var i=0; i<s.length; i++) {
+ if (s.charCodeAt(i)<32) {array.push(s.charCodeAt(i));}
+ else {array.push(s.charCodeAt(i))}
+ }
+
+ sys.puts(array);
+}
+
+p.pprint = function(s) {
+ for (var i=0; i<s.length; i++) {
+ if (s.charCodeAt(i)<32) {sys.puts(s.charCodeAt(i)+' : ');}
+ else {sys.puts(s.charCodeAt(i)+' : '+ s.charAt(i));}
+ }
+};
View
132 node_modules/thrift/connection.js
@@ -1,70 +1,26 @@
var sys = require('sys'),
EventEmitter = require("events").EventEmitter,
net = require('net'),
- TMemoryBuffer = require('./transport').TMemoryBuffer,
- TBinaryProtocol = require('./protocol').TBinaryProtocol;
-
-var binary = require('./binary');
-
-var int32FramedReceiver = exports.int32FramedReceiver = function (callback) {
- var frameLeft = 0,
- framePos = 0,
- frame = null;
- var residual = null;
-
- return function(data) {
- // Prepend any residual data from our previous read
- if (residual) {
- var dat = new Buffer(data.length + residual.length);
- residual.copy(dat, 0, 0);
- data.copy(dat, residual.length, 0);
- residual = null;
- data = dat;
- }
+ ttransport = require('./transport'),
+ tprotocol = require('./protocol');
- // var buf = new Buffer(data, 'binary');
- // console.log(buf);
- // framed transport
- while (data.length) {
- if (frameLeft === 0) {
- // TODO assumes we have all 4 bytes
- if (data.length < 4) {
- residual = data;
- break;
- }
- frameLeft = binary.readI32(data);
- frame = new Buffer(frameLeft);
- framePos = 0;
- data = data.slice(4, data.length);
- }
-
- if (data.length >= frameLeft) {
- data.copy(frame, framePos, 0, frameLeft);
- data = data.slice(frameLeft, data.length);
-
- frameLeft = 0;
- callback(frame);
- } else if (data.length) {
- data.copy(frame, framePos, 0, data.length);
- frameLeft -= data.length;
- framePos += data.length;
- data = data.slice(data.length, data.length);
- }
- }
- };
-};
+var BinaryParser = require('./binary_parser').BinaryParser;
+BinaryParser.bigEndian = true;
var Connection = exports.Connection = function(stream, options) {
var self = this;
EventEmitter.call(this);
this.connection = stream;
- this.offline_queue = [];
this.options = options || {};
+ this.transport = this.options.transport || ttransport.TFramedTransport;
+ this.protocol = this.options.protocol || tprotocol.TBinaryProtocol;
+ this.offline_queue = [];
this.connected = false;
this.connection.addListener("connect", function() {
self.connected = true;
+
this.setTimeout(self.options.timeout || 0);
this.setNoDelay();
this.frameLeft = 0;
@@ -79,12 +35,7 @@ var Connection = exports.Connection = function(stream, options) {
});
this.connection.addListener("error", function(err) {
- // Only emit the error if no-one else is listening on the connection
- // or if someone is listening on us
- if (self.connection.listeners('error').length === 1
- || self.listeners('error').length > 0) {
- self.emit("error", err)
- }
+ self.emit("error", err);
});
// Add a close listener
@@ -96,17 +47,47 @@ var Connection = exports.Connection = function(stream, options) {
self.emit("timeout");
});
- this.connection.addListener("data", int32FramedReceiver(function(data) {
- // console.log(typeof(data));
- var input = new TBinaryProtocol(new TMemoryBuffer(data));
- var r = input.readMessageBegin();
- // console.log(r);
- self.client['recv_' + r.fname](input, r.mtype, r.rseqid);
- // self.emit("data", data);
+ this.connection.addListener("data", self.transport.receiver(function(transport_with_data) {
+ var message = new self.protocol(transport_with_data);
+ try {
+ var header = message.readMessageBegin();
+ var dummy_seqid = header.rseqid * -1;
+ var client = self.client;
+ client._reqs[dummy_seqid] = function(err, success){
+ transport_with_data.commitPosition();
+
+ var callback = client._reqs[header.rseqid];
+ delete client._reqs[header.rseqid];
+ if (callback) {
+ callback(err, success);
+ }
+ };
+ client['recv_' + header.fname](message, header.mtype, dummy_seqid);
+ }
+ catch (e) {
+ if (e instanceof ttransport.InputBufferUnderrunError) {
+ transport_with_data.rollbackPosition();
+ }
+ else {
+ throw e;
+ }
+ }
}));
};
sys.inherits(Connection, EventEmitter);
+Connection.prototype.end = function() {
+ this.connection.end();
+}
+
+Connection.prototype.write = function(data) {
+ if (!this.connected) {
+ this.offline_queue.push(data);
+ return;
+ }
+ this.connection.write(data);
+}
+
exports.createConnection = function(host, port, options) {
var stream = net.createConnection(port, host);
var connection = new Connection(stream, options);
@@ -120,29 +101,12 @@ exports.createClient = function(cls, connection) {
if (cls.Client) {
cls = cls.Client;
}
- var client = new cls(new TMemoryBuffer(undefined, function(buf) {
+ var client = new cls(new connection.transport(undefined, function(buf) {
connection.write(buf);
- }), TBinaryProtocol);
+ }), connection.protocol);
// TODO clean this up
connection.client = client;
return client;
}
-
-Connection.prototype.end = function() {
- this.connection.end();
-}
-
-Connection.prototype.write = function(buf) {
- // TODO: optimize this better, allocate one buffer instead of both:
- var msg = new Buffer(buf.length + 4);
- binary.writeI32(msg, buf.length);
-
- buf.copy(msg, 4, 0, buf.length);
- if (!this.connected) {
- this.offline_queue.push(msg);
- } else {
- this.connection.write(msg);
- }
-}
View
2  node_modules/thrift/index.js
@@ -6,5 +6,3 @@ exports.createClient = connection.createClient;
exports.createConnection = connection.createConnection;
exports.createServer = require('./server').createServer;
-
-exports.Int64 = require('node-int64')
View
65 node_modules/thrift/protocol.js
@@ -2,8 +2,8 @@ var sys = require('sys'),
Thrift = require('./thrift'),
Type = Thrift.Type;
-var binary = require('./binary'),
- Int64 = require('node-int64');
+var BinaryParser = require('./binary_parser').BinaryParser;
+BinaryParser.bigEndian = true;
var UNKNOWN = 0,
INVALID_DATA = 1,
@@ -104,51 +104,28 @@ TBinaryProtocol.prototype.writeBool = function(bool) {
}
TBinaryProtocol.prototype.writeByte = function(byte) {
- this.trans.write(new Buffer([byte]));
+ this.trans.write(BinaryParser.fromByte(byte));
}
TBinaryProtocol.prototype.writeI16 = function(i16) {
- this.trans.write(binary.writeI16(new Buffer(2), i16));
+ this.trans.write(BinaryParser.fromShort(i16));
}
TBinaryProtocol.prototype.writeI32 = function(i32) {
- this.trans.write(binary.writeI32(new Buffer(4), i32));
+ this.trans.write(BinaryParser.fromInt(i32));
}
TBinaryProtocol.prototype.writeI64 = function(i64) {
- if (i64.buffer) {
- this.trans.write(i64.buffer);
- } else {
- this.trans.write(new Int64(i64).buffer)
- }
+ this.trans.write(BinaryParser.fromLong(i64));
}
TBinaryProtocol.prototype.writeDouble = function(dub) {
- this.trans.write(binary.writeDouble(new Buffer(8), dub));
-}
-
-TBinaryProtocol.prototype.writeString = function(arg) {
- if (typeof(arg) === 'string') {
- this.writeI32(Buffer.byteLength(arg, 'utf8'))
- this.trans.write(arg, 'utf8');
- } else if (arg instanceof Buffer) {
- this.writeI32(arg.length)
- this.trans.write(arg);
- } else {
- throw new Error('writeString called without a string/Buffer argument: ' + arg)
- }
+ this.trans.write(BinaryParser.fromDouble(dub));
}
-TBinaryProtocol.prototype.writeBinary = function(arg) {
- if (typeof(arg) === 'string') {
- this.writeI32(Buffer.byteLength(arg, 'utf8'))
- this.trans.write(arg, 'utf8');
- } else if (arg instanceof Buffer) {
- this.writeI32(arg.length)
- this.trans.write(arg);
- } else {
- throw new Error('writeBinary called without a string/Buffer argument: ' + arg)
- }
+TBinaryProtocol.prototype.writeString = function(str) {
+ this.writeI32(str.length);
+ this.trans.write(str);
}
TBinaryProtocol.prototype.readMessageBegin = function() {
@@ -234,24 +211,31 @@ TBinaryProtocol.prototype.readBool = function() {
}
TBinaryProtocol.prototype.readByte = function() {
- return this.trans.readByte();
+ var buff = this.trans.read(1);
+ return BinaryParser.toByte(buff);
}
TBinaryProtocol.prototype.readI16 = function() {
- return this.trans.readI16();
+ var buff = this.trans.read(2);
+ return BinaryParser.toShort(buff);
}
TBinaryProtocol.prototype.readI32 = function() {
- return this.trans.readI32();
+ var buff = this.trans.read(4);
+ // console.log("read buf: ");
+ // console.log(buff);
+ // console.log("result: " + BinaryParser.toInt(buff));
+ return BinaryParser.toInt(buff);
}
TBinaryProtocol.prototype.readI64 = function() {
var buff = this.trans.read(8);
- return new Int64(buff);
+ return BinaryParser.toLong(buff);
}
TBinaryProtocol.prototype.readDouble = function() {
- return this.trans.readDouble();
+ var buff = this.trans.read(8);
+ return BinaryParser.toFloat(buff);
}
TBinaryProtocol.prototype.readBinary = function() {
@@ -260,8 +244,9 @@ TBinaryProtocol.prototype.readBinary = function() {
}
TBinaryProtocol.prototype.readString = function() {
- var len = this.readI32();
- return this.trans.readString(len);
+ var r = this.readBinary().toString('binary');
+ // console.log("readString: " + r);
+ return r;
}
TBinaryProtocol.prototype.getTransport = function() {
View
38 node_modules/thrift/server.js
@@ -1,33 +1,41 @@
var sys = require('sys'),
net = require('net');
-var binary = require('./binary'),
- TMemoryBuffer = require('./transport').TMemoryBuffer,
- TBinaryProtocol = require('./protocol').TBinaryProtocol,
- int32FramedReceiver = require('./connection').int32FramedReceiver;
+var ttransport = require('./transport');
+var BinaryParser = require('./binary_parser').BinaryParser,
+ TBinaryProtocol = require('./protocol').TBinaryProtocol;
-exports.createServer = function(cls, handler) {
+exports.createServer = function(cls, handler, options) {
if (cls.Processor) {
cls = cls.Processor;
}
var processor = new cls(handler);
+ var transport = (options && options.transport) ? options.transport : ttransport.TFramedTransport;
+ var protocol = (options && options.protocol) ? options.protocol : TBinaryProtocol;
return net.createServer(function(stream) {
- stream.on('data', int32FramedReceiver(function(data) {
- var input = new TBinaryProtocol(new TMemoryBuffer(data));
- var output = new TBinaryProtocol(new TMemoryBuffer(undefined, function(buf) {
- // TODO: optimize this better, allocate one buffer instead of both:
- var msg = new Buffer(buf.length + 4);
- binary.writeI32(msg, buf.length);
- buf.copy(msg, 4, 0, buf.length);
- stream.write(msg);
+ stream.on('data', transport.receiver(function(transport_with_data) {
+ var input = new protocol(transport_with_data);
+ var output = new protocol(new transport(undefined, function(buf) {
+ stream.write(buf);
}));
- processor.process(input, output);
+ try {
+ processor.process(input, output);
+ transport_with_data.commitPosition();
+ }
+ catch (e) {
+ if (e instanceof ttransport.InputBufferUnderrunError) {
+ transport_with_data.rollbackPosition();
+ }
+ else {
+ throw e;
+ }
+ }
}));
stream.on('end', function() {
stream.end();
});
});
-}
+};
View
195 node_modules/thrift/transport.js
@@ -1,22 +1,74 @@
+var BinaryParser = require('./binary_parser').BinaryParser;
+
var emptyBuf = new Buffer(0);
-var binary = require('./binary');
+var InputBufferUnderrunError = exports.InputBufferUnderrunError = function() {
+};
-var TMemoryBuffer = exports.TMemoryBuffer = function(buffer, callback) {
+var TFramedTransport = exports.TFramedTransport = function(buffer, callback) {
this.inBuf = buffer || emptyBuf;
this.outBuffers = [];
this.outCount = 0;
this.readPos = 0;
this.onFlush = callback;
};
+TFramedTransport.receiver = function(callback) {
+ var frameLeft = 0,
+ framePos = 0,
+ frame = null;
+ var residual = null;
+
+ return function(data) {
+ // Prepend any residual data from our previous read
+ if (residual) {
+ var dat = new Buffer(data.length + residual.length);
+ residual.copy(dat, 0, 0);
+ data.copy(dat, residual.length, 0);
+ residual = null;
+ }
+
+ // framed transport
+ while (data.length) {
+ if (frameLeft === 0) {
+ // TODO assumes we have all 4 bytes
+ if (data.length < 4) {
+ console.log("Expecting > 4 bytes, found only " + data.length);
+ residual = data;
+ break;
+ //throw Error("Expecting > 4 bytes, found only " + data.length);
+ }
+ frameLeft = BinaryParser.toInt(data.slice(0,4));
+ frame = new Buffer(frameLeft);
+ framePos = 0;
+ data = data.slice(4, data.length);
+ }
+
+ if (data.length >= frameLeft) {
+ data.copy(frame, framePos, 0, frameLeft);
+ data = data.slice(frameLeft, data.length);
+
+ frameLeft = 0;
+ callback(new TFramedTransport(frame));
+ } else if (data.length) {
+ data.copy(frame, framePos, 0, data.length);
+ frameLeft -= data.length;
+ framePos += data.length;
+ data = data.slice(data.length, data.length);
+ }
+ }
+ };
+};
+
+TFramedTransport.prototype = {
+ commitPosition: function(){},
+ rollbackPosition: function(){},
-TMemoryBuffer.prototype = {
// TODO: Implement open/close support
isOpen: function() {return true;},
open: function() {},
close: function() {},
- read: function(len) {
+ read: function(len) { // this function will be used for each frames.
var end = this.readPos + len;
if (this.inBuf.length < end) {
@@ -28,55 +80,140 @@ TMemoryBuffer.prototype = {
return buf;
},
- readByte: function() {
- return this.inBuf[this.readPos++];
+ readAll: function() {
+ return this.inBuf;
},
- readI16: function() {
- var i16 = binary.readI16(this.inBuf, this.readPos);
- this.readPos += 2;
- return i16;
+ write: function(buf, encoding) {
+ if (typeof(buf) === "string") {
+ // Defaulting to ascii encoding here since that's more like the original
+ // code, but I feel like 'utf8' would be a better choice.
+ buf = new Buffer(buf, encoding || 'ascii');
+ }
+ this.outBuffers.push(buf);
+ this.outCount += buf.length;
},
- readI32: function() {
- var i32 = binary.readI32(this.inBuf, this.readPos);
- this.readPos += 4;
- return i32;
- },
+ flush: function() {
+ var out = new Buffer(this.outCount),
+ pos = 0;
+ this.outBuffers.forEach(function(buf) {
+ buf.copy(out, pos, 0);
+ pos += buf.length;
+ });
+
+ if (this.onFlush) {
+ // TODO: optimize this better, allocate one buffer instead of both:
+ var msg = new Buffer(out.length + 4);
+ BinaryParser.fromInt(out.length).copy(msg, 0, 0, 4);
+ out.copy(msg, 4, 0, out.length);
+ this.onFlush(msg);
+ }
+
+ this.outBuffers = [];
+ this.outCount = 0;
+ }
+};
- readDouble: function() {
- var d = binary.readDouble(this.inBuf, this.readPos);
- this.readPos += 8;
- return d;
+var TBufferedTransport = exports.TBufferedTransport = function(buffer, callback) {
+ this.defaultReadBufferSize = 1024;
+ this.writeBufferSize = 512; // Soft Limit
+ this.inBuf = new Buffer(this.defaultReadBufferSize);
+ this.readCursor = 0;
+ this.writeCursor = 0; // for input buffer
+ this.outBuffers = [];
+ this.outCount = 0;
+ this.onFlush = callback;
+};
+TBufferedTransport.receiver = function(callback) {
+ var reader = new TBufferedTransport();
+
+ return function(data) {
+ if (reader.writeCursor + data.length > reader.inBuf.length) {
+ var buf = new Buffer(reader.writeCursor + data.length);
+ reader.inBuf.copy(buf, 0, 0, reader.writeCursor);
+ reader.inBuf = buf;
+ }
+ data.copy(reader.inBuf, reader.writeCursor, 0);
+ reader.writeCursor += data.length;
+
+ callback(reader);
+ };
+};
+
+TBufferedTransport.prototype = {
+ commitPosition: function(){
+ var unreadedSize = this.writeCursor - this.readCursor;
+ var bufSize = (unreadedSize * 2 > this.defaultReadBufferSize) ? unreadedSize * 2 : this.defaultReadBufferSize;
+ var buf = new Buffer(bufSize);
+ if (unreadedSize > 0) {
+ this.inBuf.copy(buf, 0, this.readCursor, unreadedSize);
+ }
+ this.readCursor = 0;
+ this.writeCursor = unreadedSize;
+ this.inBuf = buf;
},
+ rollbackPosition: function(){
+ this.readCursor = 0;
+ },
+
+ // TODO: Implement open/close support
+ isOpen: function() {return true;},
+ open: function() {},
+ close: function() {},
- readString: function(len) {
- var str = this.inBuf.toString('utf8', this.readPos, this.readPos + len);
- this.readPos += len;
- return str;
+ read: function(len) {
+ if (this.readCursor + len > this.writeCursor) {
+ throw new InputBufferUnderrunError();
+ }
+ var buf = new Buffer(len);
+ this.inBuf.copy(buf, 0, this.readCursor, this.readCursor + len);
+ this.readCursor += len;
+ return buf;
},
readAll: function() {
- return this.inBuf;
+ if (this.readCursor >= this.writeCursor) {
+ throw new InputBufferUnderrunError();
+ }
+ var buf = new Buffer(this.writeCursor - this.readCursor);
+ this.inBuf.copy(buf, 0, this.readCursor, this.writeCursor - this.readCursor);
+ this.readCursor = this.writeCursor;
+ return buf;
},
write: function(buf, encoding) {
if (typeof(buf) === "string") {
- buf = new Buffer(buf, encoding || 'utf8');
+ // Defaulting to ascii encoding here since that's more like the original
+ // code, but I feel like 'utf8' would be a better choice.
+ buf = new Buffer(buf, encoding || 'ascii');
}
+ if (this.outCount + buf.length > this.writeBufferSize) {
+ this.flush();
+ }
+
this.outBuffers.push(buf);
this.outCount += buf.length;
+
+ if (this.outCount >= this.writeBufferSize) {
+ this.flush();
+ }
},
flush: function() {
- var out = new Buffer(this.outCount), pos = 0;
+ if (this.outCount < 1) {
+ return;
+ }
+
+ var msg = new Buffer(this.outCount),
+ pos = 0;
this.outBuffers.forEach(function(buf) {
- buf.copy(out, pos, 0);
+ buf.copy(msg, pos, 0);
pos += buf.length;
});
-
+
if (this.onFlush) {
- this.onFlush(out);
+ this.onFlush(msg);
}
this.outBuffers = [];
View
2  package.json
@@ -3,7 +3,7 @@
"contributors":["Jae Lee <jlee@yetitrails.com>", "Antonio Terreno <antonio.terreno@gmail.com>", "Andy Kent"],
"name": "node-hive",
"description": "Node Hive Client Library",
- "version": "0.1.2",
+ "version": "0.1.1",
"homepage": "https://github.com/forward/node-hive",
"repository": {
"type": "git",
Please sign in to comment.
Something went wrong with that request. Please try again.