Move Attribute getters/setters into prototype

This commit is contained in:
Chris Broome 2015-08-18 10:22:11 -04:00 committed by Patrick Mooney
parent aab28a22dc
commit 05445cb6ec
3 changed files with 121 additions and 49 deletions

1
.gitignore vendored
View File

@ -6,3 +6,4 @@ coverage
*.tar.*
*.tgz
.DS_Store
.idea

View File

@ -9,6 +9,10 @@ var Protocol = require('./protocol');
///--- API
/**
* @constructor
* @param [options={}]
*/
function Attribute(options) {
if (options) {
if (typeof (options) !== 'object')
@ -19,63 +23,63 @@ function Attribute(options) {
options = {};
}
var self = this;
this.type = options.type || '';
this._vals = [];
this.__defineGetter__('vals', function () {
var _vals = [];
self._vals.forEach(function (v) {
/* JSSTYLED */
if (/;binary$/.test(self.type)) {
_vals.push(v.toString('base64'));
} else {
_vals.push(v.toString('utf8'));
}
});
return _vals;
});
this.__defineSetter__('vals', function (vals) {
if (Array.isArray(vals)) {
vals.forEach(function (v) {
self.addValue(v);
});
} else {
self.addValue(vals);
}
});
this.__defineGetter__('buffers', function () {
return self._vals;
});
this.__defineGetter__('json', function () {
return {
type: self.type,
vals: self.vals
};
});
if (options.vals !== undefined && options.vals !== null)
this.vals = options.vals;
}
module.exports = Attribute;
Attribute.prototype.addValue = function (val) {
if (Buffer.isBuffer(val)) {
this._vals.push(val);
} else {
var encoding = 'utf8';
/* JSSTYLED */
if (/;binary$/.test(this.type))
encoding = 'base64';
this._vals.push(new Buffer(val + '', encoding));
Object.defineProperties(Attribute.prototype, {
buffers: {
get: function () {
var self = this;
return self._vals;
},
configurable: false
},
json: {
get: function() {
var self = this;
return {
type: self.type,
vals: self.vals
};
},
configurable: false
},
vals: {
get: function() {
var self = this;
var type = self.type;
var _vals = self._vals;
return _vals.map(function(v) {
return v.toString(_getEncodingFromType(type));
});
},
set: function(vals) {
var self = this;
this._vals = [];
if (Array.isArray(vals)) {
vals.forEach(function(v) {
self.addValue(v);
});
} else {
self.addValue(vals);
}
},
configurable: false
}
});
Attribute.prototype.addValue = function (val) {
this._vals.push(_valueToBuffer(val, this.type));
};
@ -169,3 +173,27 @@ Attribute.isAttribute = function (attr) {
Attribute.prototype.toString = function () {
return JSON.stringify(this.json);
};
/**
* Gets the encoding to use based on the type of the attribute
* @param {string} type
* @returns {string}
* @private
*/
function _getEncodingFromType(type) {
/* JSSTYLED */
return /;binary$/.test(type) ? 'base64' : 'utf8';
}
/**
* Converts a value to a buffer based on the given type
* @param {*} val
* @param {string} type
* @returns {Buffer}
* @private
*/
function _valueToBuffer(val, type) {
return Buffer.isBuffer(val) ?
val :
new Buffer(val + '', _getEncodingFromType(type));
}

View File

@ -4,7 +4,6 @@ var test = require('tape').test;
var asn1 = require('asn1');
///--- Globals
var BerReader = asn1.BerReader;
@ -34,6 +33,15 @@ test('new with args', function (t) {
});
t.ok(attr);
attr.addValue('baz');
t.throws(function() {
new Attribute('not an object');
});
t.throws(function() {
var typeThatIsNotAString = 1;
new Attribute({
type: typeThatIsNotAString
})
});
t.equal(attr.type, 'cn');
t.equal(attr.vals.length, 3);
t.equal(attr.vals[0], 'foo');
@ -81,6 +89,32 @@ test('parse', function (t) {
t.end();
});
test('parse - without 0x31', function(t) {
var ber = new BerWriter;
ber.startSequence();
ber.writeString('sn');
ber.endSequence();
var attr = new Attribute;
t.ok(attr);
t.ok(attr.parse(new BerReader(ber.buffer)));
t.equal(attr.type, 'sn');
t.equal(attr.vals.length, 0);
t.end();
});
test('toString', function(t) {
var attr = new Attribute({
type: 'foobar',
vals: ['asdf']
});
var expected = attr.toString();
var actual = JSON.stringify(attr.json);
t.equal(actual, expected);
t.end();
});
test('isAttribute', function (t) {
var isA = Attribute.isAttribute;
@ -118,6 +152,15 @@ test('compare', function (t) {
type: 'foo',
vals: ['bar']
});
var notAnAttribute = 'this is not an attribute';
t.throws(function() {
comp(a, notAnAttribute);
});
t.throws(function() {
comp(notAnAttribute, b)
});
t.equal(comp(a, b), 0);
// Different types