You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@thrift.apache.org by ra...@apache.org on 2014/12/02 14:48:08 UTC
thrift git commit: THRIFT-2844:Add Browserify support to node Client:
Node Patch Cameron Ring
Repository: thrift
Updated Branches:
refs/heads/master 3a5252999 -> 878483759
THRIFT-2844:Add Browserify support to node
Client: Node
Patch Cameron Ring
Adds Node support for ArrayBuffer when run in Browser
under Browserify.
Project: http://git-wip-us.apache.org/repos/asf/thrift/repo
Commit: http://git-wip-us.apache.org/repos/asf/thrift/commit/87848375
Tree: http://git-wip-us.apache.org/repos/asf/thrift/tree/87848375
Diff: http://git-wip-us.apache.org/repos/asf/thrift/diff/87848375
Branch: refs/heads/master
Commit: 878483759c55365ef5da07ad8915e9a8e4af88d9
Parents: 3a52529
Author: Randy Abernethy <ra...@apache.org>
Authored: Tue Dec 2 05:42:00 2014 -0800
Committer: Randy Abernethy <ra...@apache.org>
Committed: Tue Dec 2 05:42:00 2014 -0800
----------------------------------------------------------------------
lib/nodejs/lib/thrift/http_connection.js | 18 ++++++++++++++----
lib/nodejs/lib/thrift/protocol.js | 6 +++++-
2 files changed, 19 insertions(+), 5 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/thrift/blob/87848375/lib/nodejs/lib/thrift/http_connection.js
----------------------------------------------------------------------
diff --git a/lib/nodejs/lib/thrift/http_connection.js b/lib/nodejs/lib/thrift/http_connection.js
index ccf882f..ced1352 100644
--- a/lib/nodejs/lib/thrift/http_connection.js
+++ b/lib/nodejs/lib/thrift/http_connection.js
@@ -61,7 +61,7 @@ var tprotocol = require('./protocol');
* @throws {error} Exceptions other than ttransport.InputBufferUnderrunError are rethrown
* @event {error} The "error" event is fired when a Node.js error event occurs during
* request or response processing, in which case the node error is passed on. An "error"
- * event may also be fired when the connectison can not map a response back to the
+ * event may also be fired when the connection can not map a response back to the
* appropriate client (an internal error), generating a TApplicationException.
* @classdesc HttpConnection objects provide Thrift end point transport
* semantics implemented over the Node.js http.request() method.
@@ -86,7 +86,8 @@ var HttpConnection = exports.HttpConnection = function(host, port, options) {
port: this.port || 80,
path: this.options.path || '/',
method: 'POST',
- headers: this.options.headers || {}
+ headers: this.options.headers || {},
+ responseType: this.options.responseType || null
};
for (var attrname in this.options.nodeOptions) {
this.nodeOptions[attrname] = this.options.nodeOptions[attrname];
@@ -166,8 +167,17 @@ var HttpConnection = exports.HttpConnection = function(host, port, options) {
self.emit("error", e);
});
+ // When running directly under node, chunk will be a buffer,
+ // however, when running in a Browser (e.g. Browserify), chunk
+ // will be a string or an ArrayBuffer.
response.on('data', function (chunk) {
- data.push(chunk);
+ if ((typeof chunk == 'string') ||
+ (Object.prototype.toString.call(chunk) == '[object Uint8Array]')) {
+ // Wrap ArrayBuffer/string in a Buffer so data[i].copy will work
+ data.push(new Buffer(chunk));
+ } else {
+ data.push(chunk);
+ }
dataLen += chunk.length;
});
@@ -177,7 +187,7 @@ var HttpConnection = exports.HttpConnection = function(host, port, options) {
data[i].copy(buf, pos);
pos += data[i].length;
}
- //Get thre receiver function for the transport and
+ //Get the receiver function for the transport and
// call it with the buffer
self.transport.receiver(decodeCallback)(buf);
});
http://git-wip-us.apache.org/repos/asf/thrift/blob/87848375/lib/nodejs/lib/thrift/protocol.js
----------------------------------------------------------------------
diff --git a/lib/nodejs/lib/thrift/protocol.js b/lib/nodejs/lib/thrift/protocol.js
index c8e8a4e..782b767 100644
--- a/lib/nodejs/lib/thrift/protocol.js
+++ b/lib/nodejs/lib/thrift/protocol.js
@@ -154,7 +154,11 @@ 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) {
+ } 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.writeI32(arg.length);
this.trans.write(arg);
} else {