Skip to content

Commit

Permalink
refactor(sasl-frame): provide fromDescribedType for sasl frames
Browse files Browse the repository at this point in the history
  • Loading branch information
mbroadst committed Feb 8, 2016
1 parent 0b48a6e commit 1c3fe4e
Showing 1 changed file with 50 additions and 32 deletions.
82 changes: 50 additions & 32 deletions lib/frames/sasl_frame.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
}
Expand All @@ -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'];
}
Expand All @@ -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:
Expand Down Expand Up @@ -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);
Expand All @@ -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:
Expand All @@ -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']);
Expand All @@ -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);
};


/**
Expand All @@ -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']);
Expand All @@ -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);
};


/**
Expand Down Expand Up @@ -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);
Expand All @@ -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];
};

0 comments on commit 1c3fe4e

Please sign in to comment.