support adding custom p2p messages
This commit is contained in:
parent
05605b7b7c
commit
d4958eb05f
@ -63,10 +63,7 @@ function builder(options) {
|
||||
commands: {}
|
||||
};
|
||||
|
||||
Object.keys(exported.commandsMap).forEach(function(key) {
|
||||
|
||||
var Command = require('./commands/' + key);
|
||||
|
||||
exported.add = function(key, Command) {
|
||||
exported.commands[key] = function(obj) {
|
||||
return new Command(obj, options);
|
||||
};
|
||||
@ -78,7 +75,10 @@ function builder(options) {
|
||||
message.setPayload(buffer);
|
||||
return message;
|
||||
};
|
||||
};
|
||||
|
||||
Object.keys(exported.commandsMap).forEach(function(key) {
|
||||
exported.add(key, require('./commands/' + key));
|
||||
});
|
||||
|
||||
exported.inventoryCommands.forEach(function(command) {
|
||||
|
||||
@ -103,4 +103,9 @@ Messages.prototype._buildFromBuffer = function(command, payload) {
|
||||
return this.builder.commands[command].fromBuffer(payload);
|
||||
};
|
||||
|
||||
Messages.prototype.add = function(key, name, Command) {
|
||||
this.builder.add(key, Command);
|
||||
this[name] = this.builder.commands[key];
|
||||
};
|
||||
|
||||
module.exports = Messages;
|
||||
|
||||
@ -192,4 +192,25 @@ describe('Messages', function() {
|
||||
|
||||
});
|
||||
|
||||
describe('#add', function() {
|
||||
it('should add a custom message', function() {
|
||||
var network = bitcore.Networks.defaultNetwork;
|
||||
var messages = new Messages({
|
||||
network: network,
|
||||
Block: bitcore.Block,
|
||||
Transaction: bitcore.Transaction
|
||||
});
|
||||
|
||||
var CustomMessage = function(arg, options) {
|
||||
this.arg = arg;
|
||||
};
|
||||
|
||||
messages.add('custom', 'Custom', CustomMessage);
|
||||
should.exist(messages.Custom);
|
||||
|
||||
var message = messages.Custom('hello');
|
||||
message.arg.should.equal('hello');
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
Loading…
Reference in New Issue
Block a user