diff options
-rw-r--r-- | lib/nodejs/lib/thrift/binary_protocol.js | 42 | ||||
-rw-r--r-- | lib/nodejs/lib/thrift/compact_protocol.js | 48 | ||||
-rw-r--r-- | lib/nodejs/lib/thrift/json_protocol.js | 24 | ||||
-rw-r--r-- | lib/nodejs/test/test_driver.js | 17 | ||||
-rw-r--r-- | test/known_failures_Linux.json | 44 |
5 files changed, 101 insertions, 74 deletions
diff --git a/lib/nodejs/lib/thrift/binary_protocol.js b/lib/nodejs/lib/thrift/binary_protocol.js index a230291ec..25e5634ee 100644 --- a/lib/nodejs/lib/thrift/binary_protocol.js +++ b/lib/nodejs/lib/thrift/binary_protocol.js @@ -146,22 +146,10 @@ TBinaryProtocol.prototype.writeDouble = function(dub) { this.trans.write(binary.writeDouble(new Buffer(8), dub)); }; -TBinaryProtocol.prototype.writeString = function(arg) { +TBinaryProtocol.prototype.writeStringOrBinary = function(name, encoding, 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); - } -}; - -TBinaryProtocol.prototype.writeBinary = function(arg) { - if (typeof(arg) === 'string') { - this.writeI32(Buffer.byteLength(arg, 'utf8')); - this.trans.write(arg, 'utf8'); + this.writeI32(Buffer.byteLength(arg, encoding)); + this.trans.write(new Buffer(arg, encoding)); } else if ((arg instanceof Buffer) || (Object.prototype.toString.call(arg) == '[object Uint8Array]')) { // Buffers in Node.js under Browserify may extend UInt8Array instead of @@ -170,10 +158,18 @@ TBinaryProtocol.prototype.writeBinary = function(arg) { this.writeI32(arg.length); this.trans.write(arg); } else { - throw new Error('writeBinary called without a string/Buffer argument: ' + arg); + throw new Error(name + ' called without a string/Buffer argument: ' + arg); } }; +TBinaryProtocol.prototype.writeString = function(arg) { + this.writeStringOrBinary('writeString', 'utf8', arg); +}; + +TBinaryProtocol.prototype.writeBinary = function(arg) { + this.writeStringOrBinary('writeBinary', 'binary', arg); +}; + TBinaryProtocol.prototype.readMessageBegin = function() { var sz = this.readI32(); var type, name, seqid; @@ -279,11 +275,25 @@ TBinaryProtocol.prototype.readDouble = function() { TBinaryProtocol.prototype.readBinary = function() { var len = this.readI32(); + if (len === 0) { + return new Buffer(); + } + + if (len < 0) { + throw new Thrift.TProtocolException(Thrift.TProtocolExceptionType.NEGATIVE_SIZE, "Negative binary size"); + } return this.trans.read(len); }; TBinaryProtocol.prototype.readString = function() { var len = this.readI32(); + if (len === 0) { + return ""; + } + + if (len < 0) { + throw new Thrift.TProtocolException(Thrift.TProtocolExceptionType.NEGATIVE_SIZE, "Negative string size"); + } return this.trans.readString(len); }; diff --git a/lib/nodejs/lib/thrift/compact_protocol.js b/lib/nodejs/lib/thrift/compact_protocol.js index 8aee808f5..deecf4844 100644 --- a/lib/nodejs/lib/thrift/compact_protocol.js +++ b/lib/nodejs/lib/thrift/compact_protocol.js @@ -429,22 +429,30 @@ TCompactProtocol.prototype.writeDouble = function(v) { this.trans.write(buff); }; -TCompactProtocol.prototype.writeString = function(arg) { - this.writeBinary(arg); -}; - -TCompactProtocol.prototype.writeBinary = function(arg) { +TCompactProtocol.prototype.writeStringOrBinary = function(name, encoding, arg) { if (typeof arg === 'string') { - this.writeVarint32(Buffer.byteLength(arg, 'utf8')) ; - this.trans.write(arg, 'utf8'); - } else if (arg instanceof Buffer) { + this.writeVarint32(Buffer.byteLength(arg, encoding)) ; + this.trans.write(new Buffer(arg, encoding)); + } else if (arg instanceof Buffer || + Object.prototype.toString.call(arg) == '[object Uint8Array]') { + // Buffers in Node.js under Browserify may extend UInt8Array instead of + // defining a new object. We detect them here so we can write them + // correctly this.writeVarint32(arg.length); this.trans.write(arg); } else { - throw new Error('writeString/writeBinary called without a string/Buffer argument: ' + arg); + throw new Error(name + ' called without a string/Buffer argument: ' + arg); } }; +TCompactProtocol.prototype.writeString = function(arg) { + this.writeStringOrBinary('writeString', 'utf8', arg); +}; + +TCompactProtocol.prototype.writeBinary = function(arg) { + this.writeStringOrBinary('writeBinary', 'binary', arg); +}; + // // Compact Protocol internal write methods @@ -752,22 +760,28 @@ TCompactProtocol.prototype.readDouble = function() { TCompactProtocol.prototype.readBinary = function() { var size = this.readVarint32(); - // Catch empty string case if (size === 0) { - return ""; + return new Buffer(); } - // Catch error cases if (size < 0) { - throw new Thrift.TProtocolException(Thrift.TProtocolExceptionType.NEGATIVE_SIZE, "Negative binary/string size"); + throw new Thrift.TProtocolException(Thrift.TProtocolExceptionType.NEGATIVE_SIZE, "Negative binary size"); } - var value = this.trans.readString(size); - - return value; + return this.trans.read(size); }; TCompactProtocol.prototype.readString = function() { - return this.readBinary(); + var size = this.readVarint32(); + // Catch empty string case + if (size === 0) { + return ""; + } + + // Catch error cases + if (size < 0) { + throw new Thrift.TProtocolException(Thrift.TProtocolExceptionType.NEGATIVE_SIZE, "Negative string size"); + } + return this.trans.readString(size); }; diff --git a/lib/nodejs/lib/thrift/json_protocol.js b/lib/nodejs/lib/thrift/json_protocol.js index 18fb0129e..436709c98 100644 --- a/lib/nodejs/lib/thrift/json_protocol.js +++ b/lib/nodejs/lib/thrift/json_protocol.js @@ -317,11 +317,19 @@ TJSONProtocol.prototype.writeDouble = function(dub) { }; /** Serializes a string */ -TJSONProtocol.prototype.writeString = function(str) { +TJSONProtocol.prototype.writeString = function(arg) { // We do not encode uri components for wire transfer: - if (str === null) { + if (arg === null) { this.tstack.push(null); } else { + if (typeof arg === 'string') { + var str = arg; + } else if (arg instanceof Buffer) { + var str = arg.toString('utf8'); + } else { + throw new Error('writeString called without a string/Buffer argument: ' + arg); + } + // concat may be slower than building a byte buffer var escapedString = ''; for (var i = 0; i < str.length; i++) { @@ -358,7 +366,15 @@ TJSONProtocol.prototype.writeString = function(str) { /** Serializes a string */ TJSONProtocol.prototype.writeBinary = function(arg) { - this.writeString(arg); + if (typeof arg === 'string') { + var buf = new Buffer(arg, 'binary'); + } else if (arg instanceof Buffer || + Object.prototype.toString.call(arg) == '[object Uint8Array]') { + var buf = arg; + } else { + throw new Error('writeBinary called without a string/Buffer argument: ' + arg); + } + this.tstack.push('"' + buf.toString('base64') + '"'); }; /** @@ -680,7 +696,7 @@ TJSONProtocol.prototype.readDouble = function() { /** Returns the an object with a value property set to the next value found in the protocol buffer */ TJSONProtocol.prototype.readBinary = function() { - return this.readString(); + return new Buffer(this.readString(), 'base64'); }; /** Returns the an object with a value property set to the diff --git a/lib/nodejs/test/test_driver.js b/lib/nodejs/test/test_driver.js index 09439e55c..590d58337 100644 --- a/lib/nodejs/test/test_driver.js +++ b/lib/nodejs/test/test_driver.js @@ -57,6 +57,23 @@ exports.ThriftTestDriver = function(client, callback) { testCases.deep.forEach(makeAsserter(assert.deepEqual)); testCases.deepUnordered.forEach(makeAsserter(makeUnorderedDeepEqual(assert))); + var arr = []; + for (var i = 0; i < 256; ++i) { + arr[i] = 255 - i; + } + var buf = new Buffer(arr); + client.testBinary(buf, function(err, response) { + assert.error(err, 'testBinary: no callback error'); + assert.equal(response.length, 256, 'testBinary'); + assert.deepEqual(response, buf, 'testBinary(Buffer)'); + }); + var buf = new Buffer(arr); + client.testBinary(buf.toString('binary'), function(err, response) { + assert.error(err, 'testBinary: no callback error'); + assert.equal(response.length, 256, 'testBinary'); + assert.deepEqual(response, buf, 'testBinary(string)'); + }); + client.testMapMap(42, function(err, response) { var expected = { "4": {"1":1, "2":2, "3":3, "4":4}, diff --git a/test/known_failures_Linux.json b/test/known_failures_Linux.json index 3625fa934..d326c59b5 100644 --- a/test/known_failures_Linux.json +++ b/test/known_failures_Linux.json @@ -1,6 +1,4 @@ [ - "c_glib-nodejs_binary_buffered-ip", - "c_glib-nodejs_binary_framed-ip", "c_glib-py_binary-accel_buffered-ip", "c_glib-py_binary-accel_framed-ip", "c_glib-py_binary_buffered-ip", @@ -9,6 +7,7 @@ "c_glib-rb_binary-accel_framed-ip", "c_glib-rb_binary_buffered-ip", "c_glib-rb_binary_framed-ip", + "cpp-cpp_binary_http-domain", "cpp-cpp_binary_http-ip", "cpp-cpp_compact_http-domain", "cpp-cpp_header_http-domain", @@ -23,6 +22,7 @@ "cpp-java_compact_http-ip-ssl", "cpp-java_json_http-ip", "cpp-java_json_http-ip-ssl", + "cpp-nodejs_json_buffered-ip-ssl", "cpp-rb_json_buffered-ip", "csharp-cpp_binary_buffered-ip-ssl", "csharp-cpp_binary_framed-ip-ssl", @@ -48,7 +48,6 @@ "csharp-nodejs_compact_framed-ip-ssl", "csharp-nodejs_json_buffered-ip", "csharp-nodejs_json_buffered-ip-ssl", - "csharp-nodejs_json_framed-ip", "csharp-nodejs_json_framed-ip-ssl", "erl-cpp_compact_buffered-ip", "erl-cpp_compact_buffered-ip-ssl", @@ -97,15 +96,7 @@ "java-hs_json_buffered-ip", "java-hs_json_fastframed-framed-ip", "java-hs_json_framed-ip", - "java-nodejs_json_buffered-ip", - "java-nodejs_json_buffered-ip-ssl", - "nodejs-cpp_compact_buffered-ip", - "nodejs-cpp_compact_buffered-ip-ssl", - "nodejs-cpp_compact_framed-ip", - "nodejs-cpp_compact_framed-ip-ssl", - "nodejs-csharp_compact_buffered-ip", "nodejs-csharp_compact_buffered-ip-ssl", - "nodejs-csharp_compact_framed-ip", "nodejs-csharp_compact_framed-ip-ssl", "nodejs-csharp_json_buffered-ip", "nodejs-csharp_json_buffered-ip-ssl", @@ -113,48 +104,27 @@ "nodejs-csharp_json_framed-ip-ssl", "nodejs-dart_json_buffered-ip", "nodejs-dart_json_framed-ip", + "nodejs-hs_binary_buffered-ip", + "nodejs-hs_binary_framed-ip", + "nodejs-hs_compact_buffered-ip", + "nodejs-hs_compact_framed-ip", "nodejs-hs_json_buffered-ip", "nodejs-hs_json_framed-ip", - "nodejs-java_compact_buffered-ip", - "nodejs-java_compact_buffered-ip-ssl", - "nodejs-java_compact_framed-fastframed-ip", - "nodejs-java_compact_framed-fastframed-ip-ssl", - "nodejs-java_compact_framed-ip", - "nodejs-java_compact_framed-ip-ssl", - "nodejs-java_json_buffered-ip-ssl", - "nodejs-py3_compact_buffered-ip", - "nodejs-py3_compact_buffered-ip-ssl", - "nodejs-py3_compact_framed-ip", - "nodejs-py3_compact_framed-ip-ssl", "nodejs-py3_json_buffered-ip", "nodejs-py3_json_buffered-ip-ssl", "nodejs-py3_json_framed-ip", "nodejs-py3_json_framed-ip-ssl", - "nodejs-py_compact_buffered-ip", - "nodejs-py_compact_buffered-ip-ssl", - "nodejs-py_compact_framed-ip", - "nodejs-py_compact_framed-ip-ssl", "nodejs-py_json_buffered-ip", "nodejs-py_json_buffered-ip-ssl", "nodejs-py_json_framed-ip", "nodejs-py_json_framed-ip-ssl", - "nodejs-rb_compact_buffered-ip", - "nodejs-rb_compact_framed-ip", "nodejs-rb_json_buffered-ip", "nodejs-rb_json_framed-ip", "perl-php_binary_framed-ip", "py-hs_json_buffered-ip", "py-hs_json_framed-ip", - "py-nodejs_json_buffered-ip", - "py-nodejs_json_buffered-ip-ssl", - "py-nodejs_json_framed-ip", - "py-nodejs_json_framed-ip-ssl", "py3-hs_json_buffered-ip", "py3-hs_json_framed-ip", - "py3-perl_binary_buffered-ip-ssl", - "py3-perl_binary_framed-ip-ssl", "rb-hs_json_buffered-ip", - "rb-hs_json_framed-ip", - "rb-nodejs_json_buffered-ip", - "rb-nodejs_json_framed-ip" + "rb-hs_json_framed-ip" ]
\ No newline at end of file |