diff --git a/lib/frames/sasl_frame.js b/lib/frames/sasl_frame.js index e7dc037..55a8a9e 100644 --- a/lib/frames/sasl_frame.js +++ b/lib/frames/sasl_frame.js @@ -105,15 +105,13 @@ module.exports.SaslFrame = SaslFrame; */ function SaslMechanisms(options) { SaslMechanisms.super_.call(this); + if (options instanceof DescribedType) { + return this.fromDescribedType(options); + } + if (options) { - if (options instanceof DescribedType) { - this.mechanisms = (options.value[0] instanceof Array) ? - options.value[0].map(function(x) { return x.contents; }) : - [options.value[0].contents]; - } else { - this.mechanisms = options instanceof Array ? - options : (options.mechanisms || []); - } + this.mechanisms = + (options instanceof Array) ? options : (options.mechanisms || []); } else { this.mechanisms = []; } @@ -123,7 +121,7 @@ util.inherits(SaslMechanisms, SaslFrame); module.exports.SaslMechanisms = SaslMechanisms; SaslMechanisms.prototype.Descriptor = { code: 0x40, name: 'amqp:sasl-mechanisms:list' }; -SaslMechanisms.prototype.toDescribedType = function(b) { +SaslMechanisms.prototype.toDescribedType = function() { if (!this.mechanisms || this.mechanisms.length === 0) { this.mechanisms = ['ANONYMOUS']; } @@ -133,7 +131,11 @@ SaslMechanisms.prototype.toDescribedType = function(b) { return new DescribedType(SaslMechanisms, value); }; - +SaslMechanisms.prototype.fromDescribedType = function(described) { + this.mechanisms = (described.value[0] instanceof Array) ? + described.value[0].map(function(x) { return x.contents; }) : + [ described.value[0].contents ]; +}; /** * SASL Init frame, containing the following fields: @@ -182,16 +184,13 @@ SaslMechanisms.prototype.toDescribedType = function(b) { function SaslInit(options) { SaslInit.super_.call(this); if (options instanceof DescribedType) { - this.mechanism = up.onUndef(options, 0, 'ANONYMOUS'); - if (this.mechanism instanceof AMQPSymbol) this.mechanism = this.mechanism.contents; - this.initialResponse = up.get(options, 1); - this.hostname = up.get(options, 2); - } else { - u.assertArguments(options, ['mechanism']); - this.mechanism = u.coerce(options.mechanism, AMQPSymbol); - this.initialResponse = options.initialResponse; - this.hostname = options.hostname; + return this.fromDescribedType(options); } + + u.assertArguments(options, ['mechanism']); + this.mechanism = u.coerce(options.mechanism, AMQPSymbol); + this.initialResponse = options.initialResponse; + this.hostname = options.hostname; } util.inherits(SaslInit, SaslFrame); @@ -206,7 +205,12 @@ SaslInit.prototype.toDescribedType = function() { ]); }; - +SaslInit.prototype.fromDescribedType = function(described) { + this.mechanism = up.onUndef(described, 0, 'ANONYMOUS'); + if (this.mechanism instanceof AMQPSymbol) this.mechanism = this.mechanism.contents; + this.initialResponse = up.get(described, 1); + this.hostname = up.get(described, 2); +}; /** * SASL Challenge frame, containing the following field: @@ -226,8 +230,10 @@ SaslInit.prototype.toDescribedType = function() { function SaslChallenge(options) { SaslChallenge.super_.call(this); if (options instanceof DescribedType) { - this.challenge = up.orNull(options, 0); - } else if (options instanceof Buffer) { + return this.fromDescribedType(options); + } + + if (options instanceof Buffer) { this.challenge = options; } else { u.assertArguments(options, ['challenge']); @@ -243,6 +249,9 @@ SaslChallenge.prototype.toDescribedType = function() { return new DescribedType(SaslChallenge, [ this.challenge ]); }; +SaslChallenge.prototype.fromDescribedType = function(described) { + this.challenge = up.orNull(described, 0); +}; /** @@ -263,8 +272,10 @@ SaslChallenge.prototype.toDescribedType = function() { function SaslResponse(options) { SaslResponse.super_.call(this); if (options instanceof DescribedType) { - this.response = up.orNull(options, 0); - } else if (options instanceof Buffer) { + return this.fromDescribedType(options); + } + + if (options instanceof Buffer) { this.response = options; } else { u.assertArguments(options, ['response']); @@ -280,6 +291,9 @@ SaslResponse.prototype.toDescribedType = function() { return new DescribedType(SaslResponse, [ this.response ]); }; +SaslResponse.prototype.fromDescribedType = function(described) { + this.response = up.orNull(described, 0); +}; /** @@ -329,15 +343,12 @@ SaslResponse.prototype.toDescribedType = function() { function SaslOutcome(options) { SaslOutcome.super_.call(this); if (options instanceof DescribedType) { - up.assert(options, 0, 'code'); - this.code = up.get(options, 0); - this.additionalData = up.get(options, 1); - this.details = constants.saslOutcomes[this.code]; - } else { - u.assertArguments(options, ['code']); - this.code = options.code; - this.additionalData = options.additionalData; + return this.fromDescribedType(options); } + + u.assertArguments(options, ['code']); + this.code = options.code; + this.additionalData = options.additionalData; } util.inherits(SaslOutcome, SaslFrame); @@ -350,3 +361,10 @@ SaslOutcome.prototype.toDescribedType = function() { u.orNull(this.additionalData) ]); }; + +SaslOutcome.prototype.fromDescribedType = function(described) { + up.assert(described, 0, 'code'); + this.code = up.get(described, 0); + this.additionalData = up.get(described, 1); + this.details = constants.saslOutcomes[this.code]; +};