Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

thrift

Package Overview
Dependencies
Maintainers
1
Versions
26
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

thrift - npm Package Compare versions

Comparing version 0.6.0 to 0.7.0

examples/client_multitransport.js

22

examples/client.js

@@ -0,1 +1,19 @@

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
var thrift = require('thrift');

@@ -21,3 +39,3 @@

} else {
console.log("stored:", user.uid);
console.log("client stored:", user.uid);
client.retrieve(user.uid, function(err, responseUser) {

@@ -27,3 +45,3 @@ if (err) {

} else {
console.log("retrieved:", responseUser.uid);
console.log("client retrieved:", responseUser.uid);
connection.end();

@@ -30,0 +48,0 @@ }

32

examples/README.md

@@ -0,15 +1,29 @@

# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
# Running the user example
To run the user example, first start up the server in one terminal:
#Generate the bindings:
../../../compiler/cpp/thrift --gen js:node user.thrift
node server.js
#To run the user example, first start up the server in one terminal:
NODE_PATH=../lib:../lib/thrift node server.js
Now run the client:
#Now run the client:
NODE_PATH=../lib:../lib/thrift node client.js
node client.js
# Regenerating the bindings
If you want to regenerated the bindings, you can run:
thrift --gen js:node user.thrift

@@ -0,1 +1,19 @@

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
var thrift = require('thrift');

@@ -10,3 +28,3 @@

store: function(user, success) {
console.log("stored:", user.uid);
console.log("server stored:", user.uid);
users[user.uid] = user;

@@ -17,3 +35,3 @@ success();

retrieve: function(uid, success) {
console.log("retrieved:", uid);
console.log("server retrieved:", uid);
success(users[uid]);

@@ -20,0 +38,0 @@ },

@@ -1,7 +0,20 @@

// Adapted from: http://github.com/christkv/node-mongodb-native/raw/master/lib/mongodb/bson/binary_parser.js
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
var sys = require('sys');
//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com/classes/binary-parser [v1.0]
var chr = String.fromCharCode;

@@ -8,0 +21,0 @@

@@ -0,6 +1,24 @@

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
var sys = require('sys'),
EventEmitter = require("events").EventEmitter,
net = require('net'),
TMemoryBuffer = require('./transport').TMemoryBuffer,
TBinaryProtocol = require('./protocol').TBinaryProtocol;
ttransport = require('./transport'),
tprotocol = require('./protocol');

@@ -10,51 +28,2 @@ var BinaryParser = require('./binary_parser').BinaryParser;

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;
}
// 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) {
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(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 Connection = exports.Connection = function(stream, options) {

@@ -65,4 +34,6 @@ var self = this;

this.connection = stream;
this.options = options || {};
this.transport = this.options.transport || ttransport.TFramedTransport;
this.protocol = this.options.protocol || tprotocol.TBinaryProtocol;
this.offline_queue = [];
this.options = options || {};
this.connected = false;

@@ -72,2 +43,3 @@

self.connected = true;
this.setTimeout(self.options.timeout || 0);

@@ -99,9 +71,27 @@ this.setNoDelay();

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;
}
}
}));

@@ -111,2 +101,14 @@ };

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) {

@@ -125,5 +127,5 @@ var stream = net.createConnection(port, host);

}
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);

@@ -135,17 +137,1 @@ // TODO clean this up

}
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);
BinaryParser.fromInt(buf.length).copy(msg, 0, 0, 4);
buf.copy(msg, 4, 0, buf.length);
if (!this.connected) {
this.offline_queue.push(msg);
} else {
this.connection.write(msg);
}
}

@@ -0,1 +1,19 @@

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
exports.Thrift = require('./thrift');

@@ -2,0 +20,0 @@

@@ -0,1 +1,19 @@

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
var sys = require('sys'),

@@ -2,0 +20,0 @@ Thrift = require('./thrift'),

@@ -0,10 +1,27 @@

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
var sys = require('sys'),
net = require('net');
var ttransport = require('./transport');
var BinaryParser = require('./binary_parser').BinaryParser,
TMemoryBuffer = require('./transport').TMemoryBuffer,
TBinaryProtocol = require('./protocol').TBinaryProtocol,
int32FramedReceiver = require('./connection').int32FramedReceiver;
TBinaryProtocol = require('./protocol').TBinaryProtocol;
exports.createServer = function(cls, handler) {
exports.createServer = function(cls, handler, options) {
if (cls.Processor) {

@@ -14,15 +31,24 @@ 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);
BinaryParser.fromInt(buf.length).copy(msg, 0, 0, 4);
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;
}
}
}));

@@ -34,2 +60,2 @@

});
}
};

@@ -0,1 +1,19 @@

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
var sys = require('sys');

@@ -42,3 +60,5 @@

BAD_SEQUENCE_ID: 4,
MISSING_RESULT: 5
MISSING_RESULT: 5,
INTERNAL_ERROR: 6,
PROTOCOL_ERROR: 7
}

@@ -45,0 +65,0 @@

@@ -0,4 +1,27 @@

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
var BinaryParser = require('./binary_parser').BinaryParser;
var emptyBuf = new Buffer(0);
var TMemoryBuffer = exports.TMemoryBuffer = function(buffer, callback) {
var InputBufferUnderrunError = exports.InputBufferUnderrunError = function() {
};
var TFramedTransport = exports.TFramedTransport = function(buffer, callback) {
this.inBuf = buffer || emptyBuf;

@@ -10,4 +33,53 @@ this.outBuffers = [];

};
TFramedTransport.receiver = function(callback) {
var frameLeft = 0,
framePos = 0,
frame = null;
var residual = null;
TMemoryBuffer.prototype = {
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(){},
// TODO: Implement open/close support

@@ -18,3 +90,3 @@ isOpen: function() {return true;},

read: function(len) {
read: function(len) { // this function will be used for each frames.
var end = this.readPos + len;

@@ -46,3 +118,4 @@

flush: function() {
var out = new Buffer(this.outCount), pos = 0;
var out = new Buffer(this.outCount),
pos = 0;
this.outBuffers.forEach(function(buf) {

@@ -52,5 +125,115 @@ buf.copy(out, pos, 0);

});
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;
}
};
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() {},
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() {
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") {
// 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() {
if (this.outCount < 1) {
return;
}
var msg = new Buffer(this.outCount),
pos = 0;
this.outBuffers.forEach(function(buf) {
buf.copy(msg, pos, 0);
pos += buf.length;
});
if (this.onFlush) {
this.onFlush(out);
this.onFlush(msg);
}

@@ -57,0 +240,0 @@

{
"name": "thrift",
"description": "node-thrift",
"version": "0.6.0",
"author": "Wade Simmons <wade@wades.im>",
"description": "node.js bindings for the Apache Thrift RPC system",
"homepage": "http://thrift.apache.org/",
"repository":
{ "type" : "svn",
"url" : "http://svn.apache.org/repos/asf/thrift/trunk/"
},
"version": "0.7.0",
"author":
{ "name": "Apache Thrift Developers",
"email": "dev@thrift.apache.org",
"url": "http://thrift.apache.org"
},
"licenses":
[ { "type": "Apache-2.0",
"url": "http://www.apache.org/licenses/LICENSE-2.0"
}
],
"bugs":
{ "mail": "dev@thrift.apache.org",
"web": "https://issues.apache.org/jira/browse/THRIFT"
},
"directories" : { "lib" : "./lib/thrift" },
"main": "./lib/thrift",
"engines": { "node": ">= 0.2.4" },
"engines": { "node": ">= 0.2.4" }
}

@@ -8,6 +8,7 @@ # node-thrift

NOTE: you must use the framed thrift transport, TFramedTransport in most
implementations, on the server side. Using a popular example, this is enabled
by default in Cassandra 0.7 (but configuration must be changed in Cassandra
0.6.x and earlier).
NOTE: By default, node-thrift uses TFramedTransport. Using a popular
example, this is enabled by default in Cassandra 0.7 (but configuration must be
changed in Cassandra 0.6.x and earlier). See the
[examples](https://github.com/wadey/node-thrift/tree/master/examples) folder
to see how to enable TBufferedTransport (added in 0.7.0).

@@ -49,4 +50,8 @@ ## Install

## Libraries using node-thrift
* [yukim/node_cassandra](https://github.com/yukim/node_cassandra)
## Custom client and server example
An example based on the one shown on the Thrift front page is included in the [examples](https://github.com/wadey/node-thrift/tree/master/examples) folder.

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc