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

rhea

Package Overview
Dependencies
Maintainers
1
Versions
83
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rhea - npm Package Compare versions

Comparing version 0.1.0 to 0.1.1

core.23793

12

examples/queue_browser.js

@@ -19,2 +19,3 @@ /*

var args = require('yargs').options({
'm': { alias: 'messages', default: 100, describe: 'number of messages to expect'},
'n': { alias: 'node', default: 'examples', describe: 'name of queue to browse'},

@@ -24,6 +25,15 @@ 'p': { alias: 'port', default: 5672, describe: 'port to connect to'}

var received = 0;
var expected = args.messages;
container.on('message', function (context) {
console.log(JSON.stringify(context.message.body))
if (expected === 0 || received < expected) {
console.log(JSON.stringify(context.message.body))
if (++received === expected) {
context.receiver.detach();
context.connection.close();
}
}
});
container.connect({'port':args.port}).attach_receiver({source:{address:args.node,distribution_mode:'copy'}});

2

examples/reconnect/echo.js

@@ -19,3 +19,3 @@ /*

var args = require('yargs').options({
'm': { alias: 'messages', default: 100, describe: 'number of messages to expect'},
'm': { alias: 'messages', default: 0, describe: 'number of messages to expect'},
'p': { alias: 'port', default: 8888, describe: 'port to connect to'}

@@ -22,0 +22,0 @@ }).help('help').argv;

@@ -17,6 +17,10 @@ /*

var container = require('rhea');
var args = require('yargs').options({
'p': { alias: 'port', default: 5672, describe: 'port to listen on'}
}).help('help').argv;
container.sasl_server_mechanisms.enable_anonymous();
var server = container.listen({'port':5672});
var server = container.listen({'port':args.port});
container.on('connection_open', function (context) {
console.log('Connected!');
});

@@ -17,2 +17,6 @@ /*

var container = require('rhea');
var args = require('yargs').options({
'p': { alias: 'port', default: 5672, describe: 'port to listen on'}
}).help('help').argv;
/**

@@ -31,5 +35,5 @@ * To authenticate using PLAIN and a simple username and password

container.sasl_server_mechanisms.enable_plain(authenticate);
var server = container.listen({'port':5672});
var server = container.listen({'port':args.port});
container.on('connection_open', function (context) {
console.log('Connected!');
});

@@ -17,2 +17,7 @@ /*

var container = require('rhea');
var args = require('yargs').options({
'username': { describe: 'username to connect with'},
'password': { describe: 'password to connect with (will use PLAIN)'},
'p': { alias: 'port', default: 5671, describe: 'port to connect to'}
}).help('help').argv;

@@ -25,7 +30,7 @@ /**

*/
if (process.argv.length > 2) {
container.options.username = process.argv[2];
if (args.username) {
container.options.username = args.username;
}
if (process.argv.length > 3) {
container.options.password = process.argv[3];
if (args.password) {
container.options.password = args.password;
}

@@ -36,2 +41,2 @@ container.on('connection_open', function (context) {

});
container.connect({'port':5672});
container.connect({'port':args.port});

@@ -36,3 +36,3 @@ /*

var m = messages[i];
console.log('sent ' + JSON.stringify(m));
console.log('sent ' + m.application_properties.colour + '-' + m.body);
sender.send(m);

@@ -39,0 +39,0 @@ }

@@ -22,6 +22,3 @@ /*

container.on('connection_open', function (context) {
context.connection.attach_receiver('examples');
if (context.connection.remote.open.offered_capabilities && context.connection.remote.open.offered_capabilities['ANONYMOUS-RELAY']) {
relay = context.connection.attach_sender();
}
context.connection.attach_receiver('examples');
});

@@ -31,16 +28,11 @@

var request = context.message;
var reply = request.properties.reply_to;
var reply_to = request.properties.reply_to;
console.log("Received: " + request.body);
var response = {to: reply, body: request.body.toString().toUpperCase()};
var response = {properties:{to: reply_to}, body: request.body.toString().toUpperCase()};
if (request.properties.correlation_id) {
response.correlation_id = request.properties.correlation_id;
}
var sender = relay ? relay : senders[reply];
if (!sender) {
sender = context.connection.attach_sender(reply);
senders[reply] = sender;
}
sender.send(response);
context.connection.send(response);
});
container.connect({'port':5672});

@@ -18,3 +18,8 @@ /*

var fs = require('fs');
var path = require('path');
var args = require('yargs').options({
'p': { alias: 'port', default: 5671, describe: 'port to connect to'}
}).help('help').argv;
container.on('connection_open', function (context) {

@@ -24,10 +29,10 @@ console.log('Connected!');

});
container.connect({port:5671, transport:'tls',
container.connect({port:args.port, transport:'tls',
//enable_sasl_external:true,
// These are necessary only if using the client certificate authentication
key: fs.readFileSync('client-key.pem'),
cert: fs.readFileSync('client-cert.pem'),
key: fs.readFileSync(path.resolve(__dirname,'client-key.pem')),
cert: fs.readFileSync(path.resolve(__dirname,'client-cert.pem')),
// This is necessary only if the server uses the self-signed certificate
ca: [ fs.readFileSync('ca-cert.pem') ]
ca: [ fs.readFileSync(path.resolve(__dirname,'ca-cert.pem')) ]
});

@@ -18,2 +18,6 @@ /*

var fs = require('fs');
var path = require('path');
var args = require('yargs').options({
'p': { alias: 'port', default: 5671, describe: 'port to listen on'}
}).help('help').argv;

@@ -26,6 +30,6 @@ container.on('connection_open', function (context) {

});
var listener = container.listen({port:5671, transport:'tls',
var listener = container.listen({port:args.port, transport:'tls',
//enable_sasl_external:true,
key: fs.readFileSync('server-key.pem'),
cert: fs.readFileSync('server-cert.pem'),
key: fs.readFileSync(path.resolve(__dirname, 'server-key.pem')),
cert: fs.readFileSync(path.resolve(__dirname,'server-cert.pem')),

@@ -35,3 +39,3 @@ // to require client authentication:

rejectUnauthorized: true,
ca: [ fs.readFileSync('ca-cert.pem') ]
ca: [ fs.readFileSync(path.resolve(__dirname,'ca-cert.pem')) ]
});

@@ -38,0 +42,0 @@ listener.on('clientError', function (error, socket) {

@@ -35,2 +35,3 @@ /*

function get_socket_id(socket) {
if (socket.get_id_string) return socket.get_id_string();
return socket.localAddress + ':' + socket.localPort + ' -> ' + socket.remoteAddress + ':' + socket.remotePort;

@@ -148,2 +149,5 @@ };

this.abort_idle = false;
this.socket_ready = false;
this.scheduled_reconnect = undefined;
this.default_sender = undefined;
};

@@ -184,2 +188,3 @@

}
this.socket_ready = false;
}

@@ -194,2 +199,3 @@

Connection.prototype.reconnect = function () {
this.scheduled_reconnect = undefined;
log.reconnect('reconnecting...');

@@ -203,3 +209,7 @@ this.reset();

Connection.prototype._connect = function (details) {
this.init(details.connect(details.port, details.host, details.options, this.connected.bind(this)));
if (details.connect) {
this.init(details.connect(details.port, details.host, details.options, this.connected.bind(this)));
} else {
this.init(get_connect_fn(details)(details.port, details.host, details.options, this.connected.bind(this)));
}
return this;

@@ -211,2 +221,3 @@ };

log.io('[' + this.id + '] client accepted: '+ get_socket_id(socket));
this.socket_ready = true;
return this.init(socket);

@@ -272,5 +283,14 @@ };

Connection.prototype.send = function(msg) {
if (this.default_sender === undefined) {
this.default_sender = this.open_sender({target:{}});
}
return this.default_sender.send(msg);
};
Connection.prototype.connected = function () {
this.socket_ready = true;
this.conn_established_counter++;
log.io('[' + this.options.id + '] connected ' + get_socket_id(this.socket));
this.output();
};

@@ -305,3 +325,3 @@ Connection.prototype.sasl_failed = function (text) {

Connection.prototype.output = function () {
if (this.socket) {
if (this.socket && this.socket_ready) {
if (this.heartbeat_out) clearTimeout(this.heartbeat_out);

@@ -353,3 +373,3 @@ this.transport.write(this.socket);

Connection.prototype._disconnected = function () {
if (!this.is_closed()) {
if (!this.is_closed() && this.scheduled_reconnect == undefined) {
if (!this.dispatch('disconnected', this._context())) {

@@ -362,3 +382,3 @@ console.log('[' + this.options.id + '] disconnected');

log.reconnect('Scheduled reconnect in ' + delay + 'ms');
setTimeout(this.reconnect.bind(this), delay);
this.scheduled_reconnect = setTimeout(this.reconnect.bind(this), delay);
}

@@ -365,0 +385,0 @@ }

@@ -86,3 +86,8 @@ /*

Container.prototype.rpc_client = function(address) { return rpc.client(this, address) };
var ws = require('./ws.js');
Container.prototype.websocket_accept = function(socket, options) {
new Connection(options, this).accept(ws.wrap(socket));
}
Container.prototype.websocket_connect = ws.connect;
module.exports = new Container();

@@ -190,3 +190,3 @@ /*

if (match('ANONYMOUS-RELAY', this.connection.remote.open.offered_capabilities)) {
var relay = this.connection.attach_sender();
var relay = this.connection.attach_sender({target:{}});
this._send = function (msg) { relay.send(msg); };

@@ -193,0 +193,0 @@ } else {

@@ -61,8 +61,2 @@ /*

CircularBuffer.prototype.foreach = function (f) {
for (var i = 0; i < size; i++) {
f(this.entries[(this.head + i) % this.capacity]);
}
};
CircularBuffer.prototype.by_id = function (id) {

@@ -69,0 +63,0 @@ if (this.size > 0) {

@@ -122,19 +122,22 @@ /*

}
if (t.category === 'fixed') {
t.prototype.encoded_size = function () {
return this.type.width;
}
} else if (t.category === 'variable') {
t.prototype.encoded_size = function () {
return this.type.width + this.value.length;
}
} else if (t.category === 'compound') {
t.prototype.encoded_size = function () {
var s = this.type.width*2;
for (i in this.value) {
s += 1/*typecode*/ + i.encoded_size();//what if i is described????
}
return s;
}
}
// not used at present:
//
//if (t.category === 'fixed') {
// t.prototype.encoded_size = function () {
// return this.type.width;
// }
//} else if (t.category === 'variable') {
// t.prototype.encoded_size = function () {
// return this.type.width + this.value.length;
// }
//} else if (t.category === 'compound') {
// t.prototype.encoded_size = function () {
// var s = this.type.width*2;
// for (i in this.value) {
// s += 1/*typecode*/ + i.encoded_size();//what if i is described????
// }
// return s;
// }
//}
if (annotations) {

@@ -159,16 +162,53 @@ for (var a in annotations) {

function write_ulong(buffer, value, offset) {
var hi = value / MAX_UINT;
var lo = value % MAX_UINT;
try {
if ((typeof value) === "number" || value instanceof Number) {
var hi = Math.round(value / MAX_UINT);
var lo = value % MAX_UINT;
buffer.writeUInt32BE(hi, offset);
} catch (e) {
throw Error('Could not write high byte of ' + value + '(' + hi + ') as uint32: ' + e);
buffer.writeUInt32BE(lo, offset + 4);
} else {
value.copy(buffer, offset);
}
try {
}
function read_ulong(buffer, offset) {
var hi = buffer.readUInt32BE(offset);
var lo = buffer.readUInt32BE(offset + 4);
if (hi < 2097153) {
return hi * MAX_UINT + lo;
} else {
return buffer.slice(offset, offset + 8);
}
}
function write_long(buffer, value, offset) {
if ((typeof value) === "number" || value instanceof Number) {
var abs = Math.abs(value);
var hi = Math.round(abs / MAX_UINT);
var lo = abs % MAX_UINT;
buffer.writeInt32BE(hi, offset);
buffer.writeUInt32BE(lo, offset + 4);
} catch (e) {
throw Error('Could not write low byte of ' + value + '(' + lo + ') as uint32: ' + e);
if (value < 0) {
var carry = 1;
for (var i = 0; i < 8; i++) {
var index = offset + (7 - i);
var value = (buffer[index] ^ 0xFF) + carry;
buffer[index] = value & 0xFF;
carry = value >> 8;
}
}
} else {
value.copy(buffer, offset);
}
}
function read_long(buffer, offset) {
var hi = buffer.readInt32BE(offset)
var lo = buffer.readUInt32BE(offset + 4);
if (hi < 2097153 && hi > -2097153) {
return hi * MAX_UINT + lo;
} else {
return buffer.slice(offset, offset + 8);
}
}
define_type('Null', 0x40, undefined, null);

@@ -183,3 +223,3 @@ define_type('Boolean', 0x56, buffer_ops('UInt8'));

define_type('Uint0', 0x43, undefined, 0);
define_type('Ulong', 0x80, {'write':write_ulong});//TODO: how to represent 64 bit numbers?
define_type('Ulong', 0x80, {'write':write_ulong, 'read':read_ulong});
define_type('SmallUlong', 0x53, buffer_ops('UInt8'));

@@ -191,3 +231,3 @@ define_type('Ulong0', 0x44, undefined, 0);

define_type('SmallInt', 0x54, buffer_ops('Int8'));
define_type('Long', 0x81);//TODO: how to represent 64 bit numbers?
define_type('Long', 0x81, {'write':write_long, 'read':read_long});
define_type('SmallLong', 0x55, buffer_ops('Int8'));

@@ -201,3 +241,3 @@ define_type('Float', 0x72, buffer_ops('Float'));

define_type('Timestamp', 0x83);//TODO: convert to/from Date
define_type('Uuid', 0x98);//TODO: convert to/from stringified form
define_type('Uuid', 0x98);//TODO: convert to/from stringified form?
define_type('Vbin8', 0xa0);

@@ -257,6 +297,6 @@ define_type('Vbin32', 0xb0);

types.wrap_long = function(l) {
return l > 255 ? new types.Long(l) : new types.SmallLong(l);
return l > 127 || l < -128 ? new types.Long(l) : new types.SmallLong(l);
};
types.wrap_int = function(l) {
return l > 255 ? new types.Int(l) : new types.SmallInt(l);
return l > 127 || l < -128 ? new types.Int(l) : new types.SmallInt(l);
};

@@ -380,12 +420,2 @@ types.wrap_short = function(l) {

function unwrap_described(o) {
if (o.descriptor) {
var c = by_descriptor[o.descriptor.value];
if (c) {
return new c(o.value)
}
}
return undefined;
};
types.unwrap = function(o, leave_described) {

@@ -392,0 +422,0 @@ if (o instanceof Typed) {

{
"name" : "rhea",
"version": "0.1.0",
"version": "0.1.1",
"description": "reactive AMQP 1.0 library",

@@ -13,3 +13,6 @@ "homepage": "http://github.com/grs/rhea",

"scripts": {
"test": "mocha"
"test": "mocha",
"coverage": "istanbul cover _mocha",
"browserify": "browserify -r rhea -o rhea.js",
"run-examples": "mocha examples/test_examples.js"
},

@@ -16,0 +19,0 @@ "keywords": ["amqp","messaging"],

@@ -96,3 +96,3 @@ # rhea

* A tls [client](examples/tls/tls_client) and
* A tls [client](examples/tls/tls_client.js) and
[server](examples/tls/tls_server.js) demonstrating connecting (and

@@ -109,2 +109,12 @@ possibly authenticating) over a tls secured socket.

* Both [node based](examples/websockets/client.js) and [web
based](examples/websockets/client.html) websocket clients along with
a [server](examples/websockets/echo.js) which will echo back any
requests received. The clients can also be used against a websocket
enabled AMQP broker with a queue or topic called 'examples'. The
node based scritps require the 'ws' node module to be installed. The
browser based example requires a browserified version of the rhea
library (this can be created e.g. by calling npm run-script
browserify).
To run the examples you will need the dependencies installed: the

@@ -171,8 +181,8 @@ library itself depends on the 'debug' module, and some of the examples

* if it is a numeric value, it is interpreted as the delay between
reconnect attempts
reconnect attempts (in milliseconds)
When enabled, reconnect can be further controlled via the
following options:
* initial_reconnect_delay
* max_reconnect_delay
* reconnect_limit
* initial_reconnect_delay (in milliseconds)
* max_reconnect_delay (in milliseconds)
* reconnect_limit (maximum number of reconnect attempts)
* connection_details - a function which is specified will be invoked

@@ -208,2 +218,18 @@ to get the options to use (e.g. this can be used to alternate

##### websocket_connect()
Returns a function that can be used to create another function
suitable for use as the value of 'connection_details' in a connect
call in order to connect over websockets. The function returned here
takes a websocket url and optional arguments. The websocket_connect
method itself take the constructor of the WebSocket implementation to
use. It has been tested with the implementation in firefox and also
that in the node module 'ws'.
##### websocket_accept()
Used to start handling an incoming websocket connection as an AMQP
connection. See the [websocket echo server
example](example/websocket/echo.js) for how to use it.
---------------------------------------------------------------------

@@ -278,2 +304,10 @@ ### Connection

##### send(message)
Sends the specified message over the default sender, which is a
sending link whose target address is null. The use of this method
depends on the peer supporting so-called 'anonymous relay' semantics,
which most AMQP 1.0 brokers do. The message should contain a
properties map with a 'to' field set to the intended destination.
##### close()

@@ -280,0 +314,0 @@

@@ -187,1 +187,40 @@ /*

});
describe('connection send', function() {
var listener;
var received = {};
beforeEach(function(done) {
var container = rhea.create_container();
container.on('message', function(context) {
received[context.message.properties.to] = context.message.body;
});
listener = container.listen({port:0});
listener.on('listening', function() {
done();
});
});
afterEach(function() {
listener.close();
received = {};
});
it('sends message via default sender', function(done) {
var container = rhea.create_container();
var c = container.connect(listener.address());
var count = 0;
c.on('accepted', function (context) {
if (++count === 2) {
assert.equal(received['a'], 'A');
assert.equal(received['b'], 'B');
context.sender.close();
context.connection.close();
done();
}
});
c.send({properties:{to:'a'},body:'A'});
c.send({properties:{to:'b'},body:'B'});
});
});

@@ -140,4 +140,3 @@ /*

var descriptor = amqp_types.unwrap(link.remote.attach.source.filter['jms-selector'].descriptor);
assert.equal(descriptor.readUInt32BE(0), 0x0000468C);
assert.equal(descriptor.readUInt32BE(4), 0x00000004);
assert.equal(descriptor, 0x0000468C00000004);
assert.equal(link.remote.attach.source.filter['jms-selector'], "colour = 'green'");

@@ -144,0 +143,0 @@ assert.ok(amqp_messaging.is_modified(link.remote.attach.source.default_outcome));

@@ -20,2 +20,3 @@ /*

var rhea = require('rhea');
var amqp_types = require('rhea/lib/types.js');

@@ -83,2 +84,43 @@ describe('message content', function() {

}));
it('sends and receives map with ulongs', transfer_test({body:{age:amqp_types.wrap_ulong(888), max:amqp_types.wrap_ulong(9007199254740992),
}}, function(message) {
assert.equal(message.body.max, 9007199254740992);
assert.equal(message.body.age, 888);
}));
it('sends and receives map with longs', transfer_test({body:{one:amqp_types.wrap_long(1),
negative_one:amqp_types.wrap_long(-1),
positive:amqp_types.wrap_long(1000),
negative:amqp_types.wrap_long(-1000),
large:amqp_types.wrap_long(1000000000),
large_negative:amqp_types.wrap_long(-1000000000),
max:amqp_types.wrap_long(9007199254740992),
min:amqp_types.wrap_long(-9007199254740992)
}}, function(message) {
assert.equal(message.body.one, 1);
assert.equal(message.body.negative_one, -1);
assert.equal(message.body.positive, 1000);
assert.equal(message.body.negative, -1000);
assert.equal(message.body.large, 1000000000);
assert.equal(message.body.large_negative, -1000000000);
assert.equal(message.body.max, 9007199254740992);
assert.equal(message.body.min, -9007199254740992);
}));
it('sends and receives map with ulongs/longs as buffers', transfer_test({body:{too_big:new amqp_types.Ulong(new Buffer([0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF])),
too_small:new amqp_types.Long(new Buffer([0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00]))
}}, function(message) {
assert.equal(message.body.too_big.length, 8);
for (var i = 0; i < 8; i++) {
assert.equal(message.body.too_big[i], 0xFF);
}
assert.equal(message.body.too_small.length, 8);
for (var i = 0; i < 8; i++) {
if (i === 0) {
assert.equal(message.body.too_small[i], 0xFF);
} else {
assert.equal(message.body.too_small[i], 0x00);
}
}
}));
});

@@ -46,2 +46,7 @@ /*

function add(map, key, value) {
map[key] = value;
return map;
}
it('reconnects successfully', function(done) {

@@ -51,3 +56,3 @@ var container = rhea.create_container();

var disconnects = 0;
var c = container.connect(listener.address());
var c = container.connect(add(listener.address(), 'reconnect_limit', 10));
c.on('disconnected', function (context) {

@@ -54,0 +59,0 @@ disconnects++;

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

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